Properly create and use a bfd_arch_info_type for 32-bit tilegx.
[deliverable/binutils-gdb.git] / gas / config / tc-tilegx.c
CommitLineData
aa137e4d
NC
1/* tc-tilegx.c -- Assemble for a Tile-Gx chip.
2 Copyright 2011 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21#include "as.h"
22#include "struc-symbol.h"
23#include "subsegs.h"
24
25#include "elf/tilegx.h"
26#include "opcode/tilegx.h"
27
28#include "dwarf2dbg.h"
29#include "dw2gencfi.h"
30
31#include "safe-ctype.h"
32
33
34/* Special registers. */
35#define TREG_IDN0 57
36#define TREG_IDN1 58
37#define TREG_UDN0 59
38#define TREG_UDN1 60
39#define TREG_UDN2 61
40#define TREG_UDN3 62
41#define TREG_ZERO 63
42
43
44/* Generic assembler global variables which must be defined by all
45 targets. */
46
47/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
48int tilegx_cie_data_alignment;
49
50/* Characters which always start a comment. */
51const char comment_chars[] = "#";
52
53/* Characters which start a comment at the beginning of a line. */
54const char line_comment_chars[] = "#";
55
56/* Characters which may be used to separate multiple commands on a
57 single line. */
58const char line_separator_chars[] = ";";
59
60/* Characters which are used to indicate an exponent in a floating
61 point number. */
62const char EXP_CHARS[] = "eE";
63
64/* Characters which mean that a number is a floating point constant,
65 as in 0d1.0. */
66const char FLT_CHARS[] = "rRsSfFdDxXpP";
67
68/* Either 32 or 64. */
69static int tilegx_arch_size = 64;
70
71
72const char *
73tilegx_target_format (void)
74{
75 return tilegx_arch_size == 64 ? "elf64-tilegx" : "elf32-tilegx";
76}
77
78
79#define OPTION_32 (OPTION_MD_BASE + 0)
80#define OPTION_64 (OPTION_MD_BASE + 1)
81
82const char *md_shortopts = "VQ:";
83
84struct option md_longopts[] =
85{
86 {"32", no_argument, NULL, OPTION_32},
87 {"64", no_argument, NULL, OPTION_64},
88 {NULL, no_argument, NULL, 0}
89};
90
91size_t md_longopts_size = sizeof (md_longopts);
92
93int
94md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
95{
96 switch (c)
97 {
98 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
99 should be emitted or not. FIXME: Not implemented. */
100 case 'Q':
101 break;
102
103 /* -V: SVR4 argument to print version ID. */
104 case 'V':
105 print_version_id ();
106 break;
107
108 case OPTION_32:
109 tilegx_arch_size = 32;
110 break;
111
112 case OPTION_64:
113 tilegx_arch_size = 64;
114 break;
115
116 default:
117 return 0;
118 }
119
120 return 1;
121}
122
123void
124md_show_usage (FILE *stream)
125{
126 fprintf (stream, _("\
127 -Q ignored\n\
128 -V print assembler version number\n\
129 --32/--64 generate 32bit/64bit code\n"));
130}
131
132
133/* Extra expression types. */
134
135#define O_hw0 O_md1
136#define O_hw1 O_md2
137#define O_hw2 O_md3
138#define O_hw3 O_md4
139#define O_hw0_last O_md5
140#define O_hw1_last O_md6
141#define O_hw2_last O_md7
142#define O_hw0_got O_md8
143#define O_hw1_got O_md9
144#define O_hw2_got O_md10
145#define O_hw3_got O_md11
146#define O_hw0_last_got O_md12
147#define O_hw1_last_got O_md13
148#define O_hw2_last_got O_md14
149#define O_plt O_md15
150#define O_hw0_tls_gd O_md16
151#define O_hw1_tls_gd O_md17
152#define O_hw2_tls_gd O_md18
153#define O_hw3_tls_gd O_md19
154#define O_hw0_last_tls_gd O_md20
155#define O_hw1_last_tls_gd O_md21
156#define O_hw2_last_tls_gd O_md22
157#define O_hw0_tls_ie O_md23
158#define O_hw1_tls_ie O_md24
159#define O_hw2_tls_ie O_md25
160#define O_hw3_tls_ie O_md26
161#define O_hw0_last_tls_ie O_md27
162#define O_hw1_last_tls_ie O_md28
163#define O_hw2_last_tls_ie O_md29
164
165static struct hash_control *special_operator_hash;
166
167/* Hash tables for instruction mnemonic lookup. */
168static struct hash_control *op_hash;
169
170/* Hash table for spr lookup. */
171static struct hash_control *spr_hash;
172
173/* True temporarily while parsing an SPR expression. This changes the
174 * namespace to include SPR names. */
175static int parsing_spr;
176
177/* Are we currently inside `{ ... }'? */
178static int inside_bundle;
179
180struct tilegx_instruction
181{
182 const struct tilegx_opcode *opcode;
183 tilegx_pipeline pipe;
184 expressionS operand_values[TILEGX_MAX_OPERANDS];
185};
186
187/* This keeps track of the current bundle being built up. */
188static struct tilegx_instruction current_bundle[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
189
190/* Index in current_bundle for the next instruction to parse. */
191static int current_bundle_index;
192
193/* Allow 'r63' in addition to 'zero', etc. Normally we disallow this as
194 'zero' is not a real register, so using it accidentally would be a
195 nasty bug. For other registers, such as 'sp', code using multiple names
196 for the same physical register is excessively confusing.
197
198 The '.require_canonical_reg_names' pseudo-op turns this error on,
199 and the '.no_require_canonical_reg_names' pseudo-op turns this off.
200 By default the error is on. */
201static int require_canonical_reg_names;
202
203/* Allow bundles that do undefined or suspicious things like write
204 two different values to the same register at the same time.
205
206 The '.no_allow_suspicious_bundles' pseudo-op turns this error on,
207 and the '.allow_suspicious_bundles' pseudo-op turns this off. */
208static int allow_suspicious_bundles;
209
210
211/* A hash table of main processor registers, mapping each register name
212 to its index.
213
214 Furthermore, if the register number is greater than the number
215 of registers for that processor, the user used an illegal alias
216 for that register (e.g. r63 instead of zero), so we should generate
217 a warning. The attempted register number can be found by clearing
218 NONCANONICAL_REG_NAME_FLAG. */
219static struct hash_control *main_reg_hash;
220
221
222/* We cannot unambiguously store a 0 in a hash table and look it up,
223 so we OR in this flag to every canonical register. */
224#define CANONICAL_REG_NAME_FLAG 0x1000
225
226/* By default we disallow register aliases like r63, but we record
227 them in the hash table in case the .no_require_canonical_reg_names
228 directive is used. Noncanonical names have this value added to them. */
229#define NONCANONICAL_REG_NAME_FLAG 0x2000
230
231/* Discards flags for register hash table entries and returns the
232 reg number. */
233#define EXTRACT_REGNO(p) ((p) & 63)
234
235/* This function is called once, at assembler startup time. It should
236 set up all the tables, etc., that the MD part of the assembler will
237 need. */
238
239void
240md_begin (void)
241{
242 const struct tilegx_opcode *op;
243 int i;
82590249
WL
244 int mach = (tilegx_arch_size == 64) ? bfd_mach_tilegx : bfd_mach_tilegx32;
245
246 if (! bfd_set_arch_mach (stdoutput, bfd_arch_tilegx, mach))
247 as_warn (_("Could not set architecture and machine"));
aa137e4d
NC
248
249 /* Guarantee text section is aligned. */
250 bfd_set_section_alignment (stdoutput, text_section,
251 TILEGX_LOG2_BUNDLE_ALIGNMENT_IN_BYTES);
252
253 require_canonical_reg_names = 1;
254 allow_suspicious_bundles = 0;
255 current_bundle_index = 0;
256 inside_bundle = 0;
257
258 tilegx_cie_data_alignment = (tilegx_arch_size == 64 ? -8 : -4);
259
260 /* Initialize special operator hash table. */
261 special_operator_hash = hash_new ();
262#define INSERT_SPECIAL_OP(name) \
263 hash_insert (special_operator_hash, #name, (void *)O_##name)
264
265 INSERT_SPECIAL_OP (hw0);
266 INSERT_SPECIAL_OP (hw1);
267 INSERT_SPECIAL_OP (hw2);
268 INSERT_SPECIAL_OP (hw3);
269 INSERT_SPECIAL_OP (hw0_last);
270 INSERT_SPECIAL_OP (hw1_last);
271 INSERT_SPECIAL_OP (hw2_last);
272 /* hw3_last is a convenience alias for the equivalent hw3. */
273 hash_insert (special_operator_hash, "hw3_last", (void*)O_hw3);
274 INSERT_SPECIAL_OP (hw0_got);
275 INSERT_SPECIAL_OP (hw1_got);
276 INSERT_SPECIAL_OP (hw2_got);
277 INSERT_SPECIAL_OP (hw3_got);
278 INSERT_SPECIAL_OP (hw0_last_got);
279 INSERT_SPECIAL_OP (hw1_last_got);
280 INSERT_SPECIAL_OP (hw2_last_got);
281 INSERT_SPECIAL_OP(plt);
282 INSERT_SPECIAL_OP (hw0_tls_gd);
283 INSERT_SPECIAL_OP (hw1_tls_gd);
284 INSERT_SPECIAL_OP (hw2_tls_gd);
285 INSERT_SPECIAL_OP (hw3_tls_gd);
286 INSERT_SPECIAL_OP (hw0_last_tls_gd);
287 INSERT_SPECIAL_OP (hw1_last_tls_gd);
288 INSERT_SPECIAL_OP (hw2_last_tls_gd);
289 INSERT_SPECIAL_OP (hw0_tls_ie);
290 INSERT_SPECIAL_OP (hw1_tls_ie);
291 INSERT_SPECIAL_OP (hw2_tls_ie);
292 INSERT_SPECIAL_OP (hw3_tls_ie);
293 INSERT_SPECIAL_OP (hw0_last_tls_ie);
294 INSERT_SPECIAL_OP (hw1_last_tls_ie);
295 INSERT_SPECIAL_OP (hw2_last_tls_ie);
296#undef INSERT_SPECIAL_OP
297
298 /* Initialize op_hash hash table. */
299 op_hash = hash_new ();
300 for (op = &tilegx_opcodes[0]; op->name != NULL; op++)
301 {
302 const char *hash_err = hash_insert (op_hash, op->name, (void *)op);
303 if (hash_err != NULL)
304 as_fatal (_("Internal Error: Can't hash %s: %s"), op->name, hash_err);
305 }
306
307 /* Initialize the spr hash table. */
308 parsing_spr = 0;
309 spr_hash = hash_new ();
310 for (i = 0; i < tilegx_num_sprs; i++)
311 hash_insert (spr_hash, tilegx_sprs[i].name,
312 (void *) &tilegx_sprs[i]);
313
314 /* Set up the main_reg_hash table. We use this instead of
315 creating a symbol in the register section to avoid ambiguities
316 with labels that have the same names as registers. */
317 main_reg_hash = hash_new ();
318 for (i = 0; i < TILEGX_NUM_REGISTERS; i++)
319 {
320 char buf[64];
321
322 hash_insert (main_reg_hash, tilegx_register_names[i],
323 (void *) (long) (i | CANONICAL_REG_NAME_FLAG));
324
325 /* See if we should insert a noncanonical alias, like r63. */
326 sprintf (buf, "r%d", i);
327 if (strcmp (buf, tilegx_register_names[i]) != 0)
328 hash_insert (main_reg_hash, xstrdup (buf),
329 (void *) (long) (i | NONCANONICAL_REG_NAME_FLAG));
330 }
331}
332
333#define BUNDLE_TEMPLATE_MASK(p0, p1, p2) \
334 ((p0) | ((p1) << 8) | ((p2) << 16))
335#define BUNDLE_TEMPLATE(p0, p1, p2) \
336 { { (p0), (p1), (p2) }, \
337 BUNDLE_TEMPLATE_MASK(1 << (p0), 1 << (p1), (1 << (p2))) \
338 }
339
340#define NO_PIPELINE TILEGX_NUM_PIPELINE_ENCODINGS
341
342struct bundle_template
343{
344 tilegx_pipeline pipe[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
345 unsigned int pipe_mask;
346};
347
348static const struct bundle_template bundle_templates[] =
349{
350 /* In Y format we must always have something in Y2, since it has
351 no fnop, so this conveys that Y2 must always be used. */
352 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y0, TILEGX_PIPELINE_Y2, NO_PIPELINE),
353 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y1, TILEGX_PIPELINE_Y2, NO_PIPELINE),
354 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y2, TILEGX_PIPELINE_Y0, NO_PIPELINE),
355 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y2, TILEGX_PIPELINE_Y1, NO_PIPELINE),
356
357 /* Y format has three instructions. */
358 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y0,TILEGX_PIPELINE_Y1,TILEGX_PIPELINE_Y2),
359 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y0,TILEGX_PIPELINE_Y2,TILEGX_PIPELINE_Y1),
360 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y1,TILEGX_PIPELINE_Y0,TILEGX_PIPELINE_Y2),
361 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y1,TILEGX_PIPELINE_Y2,TILEGX_PIPELINE_Y0),
362 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y2,TILEGX_PIPELINE_Y0,TILEGX_PIPELINE_Y1),
363 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y2,TILEGX_PIPELINE_Y1,TILEGX_PIPELINE_Y0),
364
365 /* X format has only two instructions. */
366 BUNDLE_TEMPLATE(TILEGX_PIPELINE_X0, TILEGX_PIPELINE_X1, NO_PIPELINE),
367 BUNDLE_TEMPLATE(TILEGX_PIPELINE_X1, TILEGX_PIPELINE_X0, NO_PIPELINE)
368};
369
370
371static void
372prepend_nop_to_bundle (tilegx_mnemonic mnemonic)
373{
374 memmove (&current_bundle[1], &current_bundle[0],
375 current_bundle_index * sizeof current_bundle[0]);
376 current_bundle[0].opcode = &tilegx_opcodes[mnemonic];
377 ++current_bundle_index;
378}
379
380static tilegx_bundle_bits
381insert_operand (tilegx_bundle_bits bits,
382 const struct tilegx_operand *operand,
383 int operand_value,
384 char *file,
385 unsigned lineno)
386{
387 /* Range-check the immediate. */
388 int num_bits = operand->num_bits;
389
390 operand_value >>= operand->rightshift;
391
392 if (bfd_check_overflow (operand->is_signed
393 ? complain_overflow_signed
394 : complain_overflow_unsigned,
395 num_bits,
396 0,
397 bfd_arch_bits_per_address (stdoutput),
398 operand_value)
399 != bfd_reloc_ok)
400 {
401 offsetT min, max;
402 if (operand->is_signed)
403 {
404 min = -(1 << (num_bits - 1));
405 max = (1 << (num_bits - 1)) - 1;
406 }
407 else
408 {
409 min = 0;
410 max = (1 << num_bits) - 1;
411 }
412 as_bad_value_out_of_range (_("operand"), operand_value, min, max,
413 file, lineno);
414 }
415
416 /* Write out the bits for the immediate. */
417 return bits | operand->insert (operand_value);
418}
419
420
421static int
422apply_special_operator (operatorT op, offsetT num, char *file, unsigned lineno)
423{
424 int ret;
425 int check_shift = -1;
426
427 switch (op)
428 {
429 case O_hw0_last_tls_gd:
430 case O_hw0_last_tls_ie:
431 case O_hw0_last:
432 check_shift = 0;
433 /* Fall through. */
434 case O_hw0_tls_gd:
435 case O_hw0_tls_ie:
436 case O_hw0:
437 ret = (signed short)num;
438 break;
439
440 case O_hw1_last_tls_gd:
441 case O_hw1_last_tls_ie:
442 case O_hw1_last:
443 check_shift = 16;
444 /* Fall through. */
445 case O_hw1_tls_gd:
446 case O_hw1_tls_ie:
447 case O_hw1:
448 ret = (signed short)(num >> 16);
449 break;
450
451 case O_hw2_last_tls_gd:
452 case O_hw2_last_tls_ie:
453 case O_hw2_last:
454 check_shift = 32;
455 /* Fall through. */
456 case O_hw2_tls_gd:
457 case O_hw2_tls_ie:
458 case O_hw2:
459 ret = (signed short)(num >> 32);
460 break;
461
462 case O_hw3_tls_gd:
463 case O_hw3_tls_ie:
464 case O_hw3:
465 ret = (signed short)(num >> 48);
466 break;
467
468 default:
469 abort ();
470 break;
471 }
472
473 if (check_shift >= 0 && ret != (num >> check_shift))
474 {
475 as_bad_value_out_of_range (_("operand"), num,
476 ~0ULL << (check_shift + 16 - 1),
477 ~0ULL >> (64 - (check_shift + 16 - 1)),
478 file, lineno);
479 }
480
481 return ret;
482}
483
484static tilegx_bundle_bits
485emit_tilegx_instruction (tilegx_bundle_bits bits,
486 int num_operands,
487 const unsigned char *operands,
488 expressionS *operand_values,
489 char *bundle_start)
490{
491 int i;
492
493 for (i = 0; i < num_operands; i++)
494 {
495 const struct tilegx_operand *operand =
496 &tilegx_operands[operands[i]];
497 expressionS *operand_exp = &operand_values[i];
498 int is_pc_relative = operand->is_pc_relative;
499
500 if (operand_exp->X_op == O_register
501 || (operand_exp->X_op == O_constant && !is_pc_relative))
502 {
503 /* We know what the bits are right now, so insert them. */
504 bits = insert_operand (bits, operand, operand_exp->X_add_number,
505 NULL, 0);
506 }
507 else
508 {
509 bfd_reloc_code_real_type reloc = operand->default_reloc;
510 expressionS subexp;
511 int die = 0, use_subexp = 0, require_symbol = 0;
512 fixS *fixP;
513
514 /* Take an expression like hw0(x) and turn it into x with
515 a different reloc type. */
516 switch (operand_exp->X_op)
517 {
518#define HANDLE_OP16(suffix) \
519 switch (reloc) \
520 { \
521 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST: \
522 reloc = BFD_RELOC_TILEGX_IMM16_X0_##suffix; \
523 break; \
524 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST: \
525 reloc = BFD_RELOC_TILEGX_IMM16_X1_##suffix; \
526 break; \
527 default: \
528 die = 1; \
529 break; \
530 } \
531 use_subexp = 1
532
533 case O_hw0:
534 HANDLE_OP16 (HW0);
535 break;
536
537 case O_hw1:
538 HANDLE_OP16 (HW1);
539 break;
540
541 case O_hw2:
542 HANDLE_OP16 (HW2);
543 break;
544
545 case O_hw3:
546 HANDLE_OP16 (HW3);
547 break;
548
549 case O_hw0_last:
550 HANDLE_OP16 (HW0_LAST);
551 break;
552
553 case O_hw1_last:
554 HANDLE_OP16 (HW1_LAST);
555 break;
556
557 case O_hw2_last:
558 HANDLE_OP16 (HW2_LAST);
559 break;
560
561 case O_hw0_got:
562 HANDLE_OP16 (HW0_GOT);
563 require_symbol = 1;
564 break;
565
566 case O_hw1_got:
567 HANDLE_OP16 (HW1_GOT);
568 require_symbol = 1;
569 break;
570
571 case O_hw2_got:
572 HANDLE_OP16 (HW2_GOT);
573 require_symbol = 1;
574 break;
575
576 case O_hw3_got:
577 HANDLE_OP16 (HW3_GOT);
578 require_symbol = 1;
579 break;
580
581 case O_hw0_last_got:
582 HANDLE_OP16 (HW0_LAST_GOT);
583 require_symbol = 1;
584 break;
585
586 case O_hw1_last_got:
587 HANDLE_OP16 (HW1_LAST_GOT);
588 require_symbol = 1;
589 break;
590
591 case O_hw2_last_got:
592 HANDLE_OP16 (HW2_LAST_GOT);
593 require_symbol = 1;
594 break;
595
596 case O_hw0_tls_gd:
597 HANDLE_OP16 (HW0_TLS_GD);
598 require_symbol = 1;
599 break;
600
601 case O_hw1_tls_gd:
602 HANDLE_OP16 (HW1_TLS_GD);
603 require_symbol = 1;
604 break;
605
606 case O_hw2_tls_gd:
607 HANDLE_OP16 (HW2_TLS_GD);
608 require_symbol = 1;
609 break;
610
611 case O_hw3_tls_gd:
612 HANDLE_OP16 (HW3_TLS_GD);
613 require_symbol = 1;
614 break;
615
616 case O_hw0_last_tls_gd:
617 HANDLE_OP16 (HW0_LAST_TLS_GD);
618 require_symbol = 1;
619 break;
620
621 case O_hw1_last_tls_gd:
622 HANDLE_OP16 (HW1_LAST_TLS_GD);
623 require_symbol = 1;
624 break;
625
626 case O_hw2_last_tls_gd:
627 HANDLE_OP16 (HW2_LAST_TLS_GD);
628 require_symbol = 1;
629 break;
630
631 case O_hw0_tls_ie:
632 HANDLE_OP16 (HW0_TLS_IE);
633 require_symbol = 1;
634 break;
635
636 case O_hw1_tls_ie:
637 HANDLE_OP16 (HW1_TLS_IE);
638 require_symbol = 1;
639 break;
640
641 case O_hw2_tls_ie:
642 HANDLE_OP16 (HW2_TLS_IE);
643 require_symbol = 1;
644 break;
645
646 case O_hw3_tls_ie:
647 HANDLE_OP16 (HW3_TLS_IE);
648 require_symbol = 1;
649 break;
650
651 case O_hw0_last_tls_ie:
652 HANDLE_OP16 (HW0_LAST_TLS_IE);
653 require_symbol = 1;
654 break;
655
656 case O_hw1_last_tls_ie:
657 HANDLE_OP16 (HW1_LAST_TLS_IE);
658 require_symbol = 1;
659 break;
660
661 case O_hw2_last_tls_ie:
662 HANDLE_OP16 (HW2_LAST_TLS_IE);
663 require_symbol = 1;
664 break;
665
666#undef HANDLE_OP16
667
668 case O_plt:
669 switch (reloc)
670 {
671 case BFD_RELOC_TILEGX_JUMPOFF_X1:
672 reloc = BFD_RELOC_TILEGX_JUMPOFF_X1_PLT;
673 break;
674 default:
675 die = 1;
676 break;
677 }
678 use_subexp = 1;
679 require_symbol = 1;
680 break;
681
682 default:
683 /* Do nothing. */
684 break;
685 }
686
687 if (die)
688 {
689 as_bad (_("Invalid operator for operand."));
690 }
691 else if (use_subexp)
692 {
693 /* Now that we've changed the reloc, change ha16(x) into x,
694 etc. */
695
696 if (operand_exp->X_add_symbol->sy_value.X_md)
697 {
aa137e4d
NC
698 /* HACK: We used X_md to mark this symbol as a fake wrapper
699 around a real expression. To unwrap it, we just grab its
700 value here. */
701 operand_exp = &operand_exp->X_add_symbol->sy_value;
e8b9f508
WL
702
703 if (require_symbol)
704 {
705 /* Look at the expression, and reject it if it's not a
706 plain symbol. */
707 if (operand_exp->X_op != O_symbol
708 || operand_exp->X_add_number != 0)
709 as_bad (_("Operator may only be applied to symbols."));
710 }
aa137e4d
NC
711 }
712 else
713 {
714 /* The value of this expression is an actual symbol, so
715 turn that into an expression. */
716 memset (&subexp, 0, sizeof subexp);
717 subexp.X_op = O_symbol;
718 subexp.X_add_symbol = operand_exp->X_add_symbol;
719 operand_exp = &subexp;
720 }
721 }
722
723 /* Create a fixup to handle this later. */
724 fixP = fix_new_exp (frag_now,
725 bundle_start - frag_now->fr_literal,
726 (operand->num_bits + 7) >> 3,
727 operand_exp,
728 is_pc_relative,
729 reloc);
730 fixP->tc_fix_data = operand;
731
732 /* Don't do overflow checking if we are applying a function like
733 ha16. */
734 fixP->fx_no_overflow |= use_subexp;
735 }
736 }
737 return bits;
738}
739
740
741/* Detects and complains if two instructions in current_bundle write
742 to the same register, either implicitly or explicitly, or if a
743 read-only register is written. */
744static void
745check_illegal_reg_writes (void)
746{
747 BFD_HOST_U_64_BIT all_regs_written = 0;
748 int j;
749
750 for (j = 0; j < current_bundle_index; j++)
751 {
752 const struct tilegx_instruction *instr = &current_bundle[j];
753 int k;
754 BFD_HOST_U_64_BIT regs =
755 ((BFD_HOST_U_64_BIT)1) << instr->opcode->implicitly_written_register;
756 BFD_HOST_U_64_BIT conflict;
757
758 for (k = 0; k < instr->opcode->num_operands; k++)
759 {
760 const struct tilegx_operand *operand =
761 &tilegx_operands[instr->opcode->operands[instr->pipe][k]];
762
763 if (operand->is_dest_reg)
764 {
765 int regno = instr->operand_values[k].X_add_number;
766 BFD_HOST_U_64_BIT mask = ((BFD_HOST_U_64_BIT)1) << regno;
767
768 if ((mask & ( (((BFD_HOST_U_64_BIT)1) << TREG_IDN1)
769 | (((BFD_HOST_U_64_BIT)1) << TREG_UDN1)
770 | (((BFD_HOST_U_64_BIT)1) << TREG_UDN2)
771 | (((BFD_HOST_U_64_BIT)1) << TREG_UDN3))) != 0
772 && !allow_suspicious_bundles)
773 {
774 as_bad (_("Writes to register '%s' are not allowed."),
775 tilegx_register_names[regno]);
776 }
777
778 regs |= mask;
779 }
780 }
781
782 /* Writing to the zero register doesn't count. */
783 regs &= ~(((BFD_HOST_U_64_BIT)1) << TREG_ZERO);
784
785 conflict = all_regs_written & regs;
786 if (conflict != 0 && !allow_suspicious_bundles)
787 {
788 /* Find which register caused the conflict. */
789 const char *conflicting_reg_name = "???";
790 int i;
791
792 for (i = 0; i < TILEGX_NUM_REGISTERS; i++)
793 {
794 if (((conflict >> i) & 1) != 0)
795 {
796 conflicting_reg_name = tilegx_register_names[i];
797 break;
798 }
799 }
800
801 as_bad (_("Two instructions in the same bundle both write "
802 "to register %s, which is not allowed."),
803 conflicting_reg_name);
804 }
805
806 all_regs_written |= regs;
807 }
808}
809
810
811static void
812tilegx_flush_bundle (void)
813{
814 unsigned i;
815 int j;
816 addressT addr_mod;
817 unsigned compatible_pipes;
818 const struct bundle_template *match;
819 char *f;
820
821 inside_bundle = 0;
822
823 switch (current_bundle_index)
824 {
825 case 0:
826 /* No instructions. */
827 return;
828 case 1:
829 if (current_bundle[0].opcode->can_bundle)
830 {
831 /* Simplify later logic by adding an explicit fnop. */
832 prepend_nop_to_bundle (TILEGX_OPC_FNOP);
833 }
834 else
835 {
836 /* This instruction cannot be bundled with anything else.
837 Prepend an explicit 'nop', rather than an 'fnop', because
838 fnops can be replaced by later binary-processing tools while
839 nops cannot. */
840 prepend_nop_to_bundle (TILEGX_OPC_NOP);
841 }
842 break;
843 default:
844 if (!allow_suspicious_bundles)
845 {
846 /* Make sure all instructions can be bundled with other
847 instructions. */
848 const struct tilegx_opcode *cannot_bundle = NULL;
849 bfd_boolean seen_non_nop = FALSE;
850
851 for (j = 0; j < current_bundle_index; j++)
852 {
853 const struct tilegx_opcode *op = current_bundle[j].opcode;
854
855 if (!op->can_bundle && cannot_bundle == NULL)
856 cannot_bundle = op;
857 else if (op->mnemonic != TILEGX_OPC_NOP
858 && op->mnemonic != TILEGX_OPC_INFO
859 && op->mnemonic != TILEGX_OPC_INFOL)
860 seen_non_nop = TRUE;
861 }
862
863 if (cannot_bundle != NULL && seen_non_nop)
864 {
865 current_bundle_index = 0;
866 as_bad (_("'%s' may not be bundled with other instructions."),
867 cannot_bundle->name);
868 return;
869 }
870 }
871 break;
872 }
873
874 compatible_pipes =
875 BUNDLE_TEMPLATE_MASK(current_bundle[0].opcode->pipes,
876 current_bundle[1].opcode->pipes,
877 (current_bundle_index == 3
878 ? current_bundle[2].opcode->pipes
879 : (1 << NO_PIPELINE)));
880
881 /* Find a template that works, if any. */
882 match = NULL;
883 for (i = 0; i < sizeof bundle_templates / sizeof bundle_templates[0]; i++)
884 {
885 const struct bundle_template *b = &bundle_templates[i];
886 if ((b->pipe_mask & compatible_pipes) == b->pipe_mask)
887 {
888 match = b;
889 break;
890 }
891 }
892
893 if (match == NULL)
894 {
895 current_bundle_index = 0;
896 as_bad (_("Invalid combination of instructions for bundle."));
897 return;
898 }
899
900 /* If the section seems to have no alignment set yet, go ahead and
901 make it large enough to hold code. */
902 if (bfd_get_section_alignment (stdoutput, now_seg) == 0)
903 bfd_set_section_alignment (stdoutput, now_seg,
904 TILEGX_LOG2_BUNDLE_ALIGNMENT_IN_BYTES);
905
906 for (j = 0; j < current_bundle_index; j++)
907 current_bundle[j].pipe = match->pipe[j];
908
909 if (current_bundle_index == 2 && !tilegx_is_x_pipeline (match->pipe[0]))
910 {
911 /* We are in Y mode with only two instructions, so add an FNOP. */
912 prepend_nop_to_bundle (TILEGX_OPC_FNOP);
913
914 /* Figure out what pipe the fnop must be in via arithmetic.
915 * p0 + p1 + p2 must sum to the sum of TILEGX_PIPELINE_Y[012]. */
916 current_bundle[0].pipe =
917 (tilegx_pipeline)((TILEGX_PIPELINE_Y0
918 + TILEGX_PIPELINE_Y1
919 + TILEGX_PIPELINE_Y2) -
920 (current_bundle[1].pipe + current_bundle[2].pipe));
921 }
922
923 check_illegal_reg_writes ();
924
925 f = frag_more (TILEGX_BUNDLE_SIZE_IN_BYTES);
926
927 /* Check to see if this bundle is at an offset that is a multiple of 8-bytes
928 from the start of the frag. */
929 addr_mod = frag_now_fix () & (TILEGX_BUNDLE_ALIGNMENT_IN_BYTES - 1);
930 if (frag_now->has_code && frag_now->insn_addr != addr_mod)
931 as_bad (_("instruction address is not a multiple of 8"));
932 frag_now->insn_addr = addr_mod;
933 frag_now->has_code = 1;
934
935 tilegx_bundle_bits bits = 0;
936 for (j = 0; j < current_bundle_index; j++)
937 {
938 struct tilegx_instruction *instr = &current_bundle[j];
939 tilegx_pipeline pipeline = instr->pipe;
940 const struct tilegx_opcode *opcode = instr->opcode;
941
942 bits |= emit_tilegx_instruction (opcode->fixed_bit_values[pipeline],
943 opcode->num_operands,
944 &opcode->operands[pipeline][0],
945 instr->operand_values,
946 f);
947 }
948
949 number_to_chars_littleendian (f, bits, 8);
950 current_bundle_index = 0;
951
952 /* Emit DWARF2 debugging information. */
953 dwarf2_emit_insn (TILEGX_BUNDLE_SIZE_IN_BYTES);
954}
955
956
957/* Extend the expression parser to handle hw0(label), etc.
958 as well as SPR names when in the context of parsing an SPR. */
959
960int
961tilegx_parse_name (char *name, expressionS *e, char *nextcharP)
962{
963 operatorT op = O_illegal;
964
965 if (parsing_spr)
966 {
967 void* val = hash_find (spr_hash, name);
968 if (val == NULL)
969 return 0;
970
971 memset (e, 0, sizeof *e);
972 e->X_op = O_constant;
973 e->X_add_number = ((const struct tilegx_spr *)val)->number;
974 return 1;
975 }
976
977 if (*nextcharP != '(')
978 {
979 /* hw0, etc. not followed by a paren is just a label with that name. */
980 return 0;
981 }
982 else
983 {
984 /* Look up the operator in our table. */
985 void* val = hash_find (special_operator_hash, name);
986 if (val == 0)
987 return 0;
988 op = (operatorT)(long)val;
989 }
990
991 /* Restore old '(' and skip it. */
992 *input_line_pointer = '(';
993 ++input_line_pointer;
994
995 expression (e);
996
997 if (*input_line_pointer != ')')
998 {
999 as_bad (_("Missing ')'"));
1000 *nextcharP = *input_line_pointer;
1001 return 0;
1002 }
1003 /* Skip ')'. */
1004 ++input_line_pointer;
1005
1006 if (e->X_op == O_register || e->X_op == O_absent)
1007 {
1008 as_bad (_("Invalid expression."));
1009 e->X_op = O_constant;
1010 e->X_add_number = 0;
1011 }
1012 else
1013 {
1014 /* Wrap subexpression with a unary operator. */
1015 symbolS *sym = make_expr_symbol (e);
1016
1017 if (sym != e->X_add_symbol)
1018 {
1019 /* HACK: mark this symbol as a temporary wrapper around a proper
1020 expression, so we can unwrap it later once we have communicated
1021 the relocation type. */
1022 sym->sy_value.X_md = 1;
1023 }
1024
1025 memset (e, 0, sizeof *e);
1026 e->X_op = op;
1027 e->X_add_symbol = sym;
1028 e->X_add_number = 0;
1029 }
1030
1031 *nextcharP = *input_line_pointer;
1032 return 1;
1033}
1034
1035
1036/* Parses an expression which must be a register name. */
1037
1038static void
1039parse_reg_expression (expressionS* expression)
1040{
1041 /* Zero everything to make sure we don't miss any flags. */
1042 memset (expression, 0, sizeof *expression);
1043
1044 char* regname = input_line_pointer;
1045 char terminating_char = get_symbol_end ();
1046
1047 void* pval = hash_find (main_reg_hash, regname);
1048
1049 if (pval == NULL)
1050 {
1051 as_bad (_("Expected register, got '%s'."), regname);
1052 }
1053
1054 int regno_and_flags = (int)(size_t)pval;
1055 int regno = EXTRACT_REGNO(regno_and_flags);
1056
1057 if ((regno_and_flags & NONCANONICAL_REG_NAME_FLAG)
1058 && require_canonical_reg_names)
1059 {
1060 as_warn (_("Found use of non-canonical register name %s; "
1061 "use %s instead."),
1062 regname,
1063 tilegx_register_names[regno]);
1064 }
1065
1066 /* Restore the old character following the register name. */
1067 *input_line_pointer = terminating_char;
1068
1069 /* Fill in the expression fields to indicate it's a register. */
1070 expression->X_op = O_register;
1071 expression->X_add_number = regno;
1072}
1073
1074
1075/* Parses and type-checks comma-separated operands in input_line_pointer. */
1076
1077static void
1078parse_operands (const char *opcode_name,
1079 const unsigned char *operands,
1080 int num_operands,
1081 expressionS *operand_values)
1082{
1083 int i;
1084
1085 memset (operand_values, 0, num_operands * sizeof operand_values[0]);
1086
1087 SKIP_WHITESPACE ();
1088 for (i = 0; i < num_operands; i++)
1089 {
1090 tilegx_operand_type type = tilegx_operands[operands[i]].type;
1091
1092 SKIP_WHITESPACE ();
1093
1094 if (type == TILEGX_OP_TYPE_REGISTER)
1095 {
1096 parse_reg_expression (&operand_values[i]);
1097 }
1098 else if (*input_line_pointer == '}')
1099 {
1100 operand_values[i].X_op = O_absent;
1101 }
1102 else if (type == TILEGX_OP_TYPE_SPR)
1103 {
1104 /* Modify the expression parser to add SPRs to the namespace. */
1105 parsing_spr = 1;
1106 expression (&operand_values[i]);
1107 parsing_spr = 0;
1108 }
1109 else
1110 {
1111 expression (&operand_values[i]);
1112 }
1113
1114 SKIP_WHITESPACE ();
1115
1116 if (i + 1 < num_operands)
1117 {
1118 int separator = (unsigned char)*input_line_pointer++;
1119
1120 if (is_end_of_line[separator] || (separator == '}'))
1121 {
1122 as_bad (_("Too few operands to '%s'."), opcode_name);
1123 return;
1124 }
1125 else if (separator != ',')
1126 {
1127 as_bad (_("Unexpected character '%c' after operand %d to %s."),
1128 (char)separator, i + 1, opcode_name);
1129 return;
1130 }
1131 }
1132
1133 /* Arbitrarily use the first valid pipe to get the operand type,
1134 since they are all the same. */
1135 switch (tilegx_operands[operands[i]].type)
1136 {
1137 case TILEGX_OP_TYPE_REGISTER:
1138 /* Handled in parse_reg_expression already. */
1139 break;
1140 case TILEGX_OP_TYPE_SPR:
1141 /* Fall through */
1142 case TILEGX_OP_TYPE_IMMEDIATE:
1143 /* Fall through */
1144 case TILEGX_OP_TYPE_ADDRESS:
1145 if ( operand_values[i].X_op == O_register
1146 || operand_values[i].X_op == O_illegal
1147 || operand_values[i].X_op == O_absent)
1148 as_bad (_("Expected immediate expression"));
1149 break;
1150 default:
1151 abort();
1152 }
1153 }
1154
1155 if (!is_end_of_line[(unsigned char)*input_line_pointer])
1156 {
1157 switch (*input_line_pointer)
1158 {
1159 case '}':
1160 if (!inside_bundle)
1161 as_bad (_("Found '}' when not bundling."));
1162 ++input_line_pointer;
1163 inside_bundle = 0;
1164 demand_empty_rest_of_line ();
1165 break;
1166
1167 case ',':
1168 as_bad (_("Too many operands"));
1169 break;
1170
1171 default:
1172 /* Use default error for unrecognized garbage. */
1173 demand_empty_rest_of_line ();
1174 break;
1175 }
1176 }
1177}
1178
1179
1180/* This is the guts of the machine-dependent assembler. STR points to a
1181 machine dependent instruction. This function is supposed to emit the
1182 frags/bytes it assembles to. */
1183
1184void
1185md_assemble (char *str)
1186{
1187 char old_char;
1188 size_t opname_len;
1189 char *old_input_line_pointer;
1190 const struct tilegx_opcode *op;
1191 int first_pipe;
1192
1193 /* Split off the opcode and look it up. */
1194 opname_len = strcspn (str, " {}");
1195 old_char = str[opname_len];
1196 str[opname_len] = '\0';
1197
1198 op = hash_find(op_hash, str);
1199 str[opname_len] = old_char;
1200 if (op == NULL)
1201 {
1202 as_bad (_("Unknown opcode `%.*s'."), (int)opname_len, str);
1203 return;
1204 }
1205
1206 /* Prepare to parse the operands. */
1207 old_input_line_pointer = input_line_pointer;
1208 input_line_pointer = str + opname_len;
1209 SKIP_WHITESPACE ();
1210
1211 if (current_bundle_index == TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE)
1212 {
1213 as_bad (_("Too many instructions for bundle."));
1214 tilegx_flush_bundle ();
1215 }
1216
1217 /* Make sure we have room for the upcoming bundle before we
1218 create any fixups. Otherwise if we have to switch to a new
1219 frag the fixup dot_value fields will be wrong. */
1220 frag_grow (TILEGX_BUNDLE_SIZE_IN_BYTES);
1221
1222 /* Find a valid pipe for this opcode. */
1223 for (first_pipe = 0; (op->pipes & (1 << first_pipe)) == 0; first_pipe++)
1224 ;
1225
1226 /* Call the function that assembles this instruction. */
1227 current_bundle[current_bundle_index].opcode = op;
1228 parse_operands (op->name,
1229 &op->operands[first_pipe][0],
1230 op->num_operands,
1231 current_bundle[current_bundle_index].operand_values);
1232 ++current_bundle_index;
1233
1234 /* Restore the saved value of input_line_pointer. */
1235 input_line_pointer = old_input_line_pointer;
1236
1237 /* If we weren't inside curly braces, go ahead and emit
1238 this lone instruction as a bundle right now. */
1239 if (!inside_bundle)
1240 tilegx_flush_bundle ();
1241}
1242
1243
1244static void
1245s_require_canonical_reg_names (int require)
1246{
1247 demand_empty_rest_of_line ();
1248 require_canonical_reg_names = require;
1249}
1250
1251static void
1252s_allow_suspicious_bundles (int allow)
1253{
1254 demand_empty_rest_of_line ();
1255 allow_suspicious_bundles = allow;
1256}
1257
1258const pseudo_typeS md_pseudo_table[] =
1259{
1260 {"align", s_align_bytes, 0}, /* Defaulting is invalid (0). */
1261 {"word", cons, 4},
1262 {"require_canonical_reg_names", s_require_canonical_reg_names, 1 },
1263 {"no_require_canonical_reg_names", s_require_canonical_reg_names, 0 },
1264 {"allow_suspicious_bundles", s_allow_suspicious_bundles, 1 },
1265 {"no_allow_suspicious_bundles", s_allow_suspicious_bundles, 0 },
1266 { NULL, 0, 0 }
1267};
1268
1269/* Equal to MAX_PRECISION in atof-ieee.c */
1270#define MAX_LITTLENUMS 6
1271
1272/* Turn the string pointed to by litP into a floating point constant
1273 of type TYPE, and emit the appropriate bytes. The number of
1274 LITTLENUMS emitted is stored in *SIZEP. An error message is
1275 returned, or NULL on OK. */
1276
1277char *
1278md_atof (int type, char *litP, int *sizeP)
1279{
1280 int prec;
1281 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1282 LITTLENUM_TYPE *wordP;
1283 char *t;
1284
1285 switch (type)
1286 {
1287 case 'f':
1288 case 'F':
1289 prec = 2;
1290 break;
1291
1292 case 'd':
1293 case 'D':
1294 prec = 4;
1295 break;
1296
1297 default:
1298 *sizeP = 0;
1299 return _("Bad call to md_atof ()");
1300 }
1301 t = atof_ieee (input_line_pointer, type, words);
1302 if (t)
1303 input_line_pointer = t;
1304
1305 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1306 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
1307 the bigendian 386. */
1308 for (wordP = words + prec - 1; prec--;)
1309 {
1310 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
1311 litP += sizeof (LITTLENUM_TYPE);
1312 }
1313 return 0;
1314}
1315
1316
1317/* We have no need to default values of symbols. */
1318
1319symbolS *
1320md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1321{
1322 return NULL;
1323}
1324
1325
1326void
1327tilegx_cons_fix_new (fragS *frag,
1328 int where,
1329 int nbytes,
1330 expressionS *exp)
1331{
1332 expressionS subexp;
1333 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
1334 int no_overflow = 0;
1335 fixS *fixP;
1336
1337 /* See if it's one of our special functions. */
1338 switch (exp->X_op)
1339 {
1340 case O_hw0:
1341 reloc = BFD_RELOC_TILEGX_HW0;
1342 no_overflow = 1;
1343 break;
1344 case O_hw1:
1345 reloc = BFD_RELOC_TILEGX_HW1;
1346 no_overflow = 1;
1347 break;
1348 case O_hw2:
1349 reloc = BFD_RELOC_TILEGX_HW2;
1350 no_overflow = 1;
1351 break;
1352 case O_hw3:
1353 reloc = BFD_RELOC_TILEGX_HW3;
1354 no_overflow = 1;
1355 break;
1356 case O_hw0_last:
1357 reloc = BFD_RELOC_TILEGX_HW0_LAST;
1358 break;
1359 case O_hw1_last:
1360 reloc = BFD_RELOC_TILEGX_HW1_LAST;
1361 break;
1362 case O_hw2_last:
1363 reloc = BFD_RELOC_TILEGX_HW2_LAST;
1364 break;
1365
1366 default:
1367 /* Do nothing. */
1368 break;
1369 }
1370
1371 if (reloc != BFD_RELOC_NONE)
1372 {
1373 if (nbytes != 2)
1374 {
1375 as_bad (_("This operator only produces two byte values."));
1376 nbytes = 2;
1377 }
1378
1379 memset (&subexp, 0, sizeof subexp);
1380 subexp.X_op = O_symbol;
1381 subexp.X_add_symbol = exp->X_add_symbol;
1382 exp = &subexp;
1383 }
1384 else
1385 {
1386 switch (nbytes)
1387 {
1388 case 1:
1389 reloc = BFD_RELOC_8;
1390 break;
1391 case 2:
1392 reloc = BFD_RELOC_16;
1393 break;
1394 case 4:
1395 reloc = BFD_RELOC_32;
1396 break;
1397 case 8:
1398 reloc = BFD_RELOC_64;
1399 break;
1400 default:
1401 as_bad (_("unsupported BFD relocation size %d"), nbytes);
1402 reloc = BFD_RELOC_64;
1403 break;
1404 }
1405 }
1406
1407 fixP = fix_new_exp (frag, where, nbytes, exp, 0, reloc);
1408 fixP->tc_fix_data = NULL;
1409 fixP->fx_no_overflow |= no_overflow;
1410}
1411
1412
1413void
1414md_apply_fix (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
1415{
1416 const struct tilegx_operand *operand;
1417 valueT value = *valP;
1418 operatorT special;
1419 char *p;
1420
1421 /* Leave these for the linker. */
1422 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1423 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1424 return;
1425
1426 if (fixP->fx_subsy != (symbolS *) NULL)
1427 {
1428 /* We can't actually support subtracting a symbol. */
1429 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
1430 }
1431
1432 /* Correct relocation types for pc-relativeness. */
1433 switch (fixP->fx_r_type)
1434 {
1435#define FIX_PCREL(rtype) \
1436 case rtype: \
1437 if (fixP->fx_pcrel) \
1438 fixP->fx_r_type = rtype##_PCREL; \
1439 break; \
1440 \
1441 case rtype##_PCREL: \
1442 if (!fixP->fx_pcrel) \
1443 fixP->fx_r_type = rtype; \
1444 break
1445
1446 FIX_PCREL (BFD_RELOC_8);
1447 FIX_PCREL (BFD_RELOC_16);
1448 FIX_PCREL (BFD_RELOC_32);
1449 FIX_PCREL (BFD_RELOC_64);
1450 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW0);
1451 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW0);
1452 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW1);
1453 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW1);
1454 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW2);
1455 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW2);
1456 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW3);
1457 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW3);
1458 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST);
1459 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST);
1460 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST);
1461 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST);
1462 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST);
1463 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST);
1464
1465#undef FIX_PCREL
1466
1467 default:
1468 /* Do nothing */
1469 break;
1470 }
1471
1472 if (fixP->fx_addsy != NULL)
1473 {
1474#ifdef OBJ_ELF
1475 switch (fixP->fx_r_type)
1476 {
1477 case BFD_RELOC_TILEGX_IMM16_X0_HW0_TLS_GD:
1478 case BFD_RELOC_TILEGX_IMM16_X1_HW0_TLS_GD:
1479 case BFD_RELOC_TILEGX_IMM16_X0_HW0_TLS_IE:
1480 case BFD_RELOC_TILEGX_IMM16_X1_HW0_TLS_IE:
1481 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
1482 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
1483 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
1484 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
1485 case BFD_RELOC_TILEGX_IMM16_X0_HW1_TLS_GD:
1486 case BFD_RELOC_TILEGX_IMM16_X1_HW1_TLS_GD:
1487 case BFD_RELOC_TILEGX_IMM16_X0_HW1_TLS_IE:
1488 case BFD_RELOC_TILEGX_IMM16_X1_HW1_TLS_IE:
1489 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
1490 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
1491 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
1492 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
1493 case BFD_RELOC_TILEGX_IMM16_X0_HW2_TLS_GD:
1494 case BFD_RELOC_TILEGX_IMM16_X1_HW2_TLS_GD:
1495 case BFD_RELOC_TILEGX_IMM16_X0_HW2_TLS_IE:
1496 case BFD_RELOC_TILEGX_IMM16_X1_HW2_TLS_IE:
1497 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_TLS_GD:
1498 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_TLS_GD:
1499 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_TLS_IE:
1500 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_TLS_IE:
1501 case BFD_RELOC_TILEGX_IMM16_X0_HW3_TLS_GD:
1502 case BFD_RELOC_TILEGX_IMM16_X1_HW3_TLS_GD:
1503 case BFD_RELOC_TILEGX_IMM16_X0_HW3_TLS_IE:
1504 case BFD_RELOC_TILEGX_IMM16_X1_HW3_TLS_IE:
1505 case BFD_RELOC_TILEGX_TLS_DTPMOD64:
1506 case BFD_RELOC_TILEGX_TLS_DTPOFF64:
1507 case BFD_RELOC_TILEGX_TLS_TPOFF64:
1508 case BFD_RELOC_TILEGX_TLS_DTPMOD32:
1509 case BFD_RELOC_TILEGX_TLS_DTPOFF32:
1510 case BFD_RELOC_TILEGX_TLS_TPOFF32:
1511 S_SET_THREAD_LOCAL (fixP->fx_addsy);
1512 break;
1513
1514 default:
1515 /* Do nothing */
1516 break;
1517 }
1518#endif
1519 return;
1520 }
1521
1522 /* Apply hw0, etc. */
1523 special = O_illegal;
1524 switch (fixP->fx_r_type)
1525 {
1526 case BFD_RELOC_TILEGX_HW0:
1527 case BFD_RELOC_TILEGX_IMM16_X0_HW0:
1528 case BFD_RELOC_TILEGX_IMM16_X1_HW0:
1529 case BFD_RELOC_TILEGX_IMM16_X0_HW0_PCREL:
1530 case BFD_RELOC_TILEGX_IMM16_X1_HW0_PCREL:
1531 case BFD_RELOC_TILEGX_IMM16_X0_HW0_GOT:
1532 case BFD_RELOC_TILEGX_IMM16_X1_HW0_GOT:
1533 case BFD_RELOC_TILEGX_IMM16_X0_HW0_TLS_GD:
1534 case BFD_RELOC_TILEGX_IMM16_X1_HW0_TLS_GD:
1535 case BFD_RELOC_TILEGX_IMM16_X0_HW0_TLS_IE:
1536 case BFD_RELOC_TILEGX_IMM16_X1_HW0_TLS_IE:
1537 special = O_hw0;
1538 break;
1539
1540 case BFD_RELOC_TILEGX_HW0_LAST:
1541 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST:
1542 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST:
1543 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_PCREL:
1544 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_PCREL:
1545 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_GOT:
1546 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_GOT:
1547 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
1548 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
1549 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
1550 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
1551 special = O_hw0_last;
1552 break;
1553
1554 case BFD_RELOC_TILEGX_HW1:
1555 case BFD_RELOC_TILEGX_IMM16_X0_HW1:
1556 case BFD_RELOC_TILEGX_IMM16_X1_HW1:
1557 case BFD_RELOC_TILEGX_IMM16_X0_HW1_PCREL:
1558 case BFD_RELOC_TILEGX_IMM16_X1_HW1_PCREL:
1559 case BFD_RELOC_TILEGX_IMM16_X0_HW1_GOT:
1560 case BFD_RELOC_TILEGX_IMM16_X1_HW1_GOT:
1561 case BFD_RELOC_TILEGX_IMM16_X0_HW1_TLS_GD:
1562 case BFD_RELOC_TILEGX_IMM16_X1_HW1_TLS_GD:
1563 case BFD_RELOC_TILEGX_IMM16_X0_HW1_TLS_IE:
1564 case BFD_RELOC_TILEGX_IMM16_X1_HW1_TLS_IE:
1565 special = O_hw1;
1566 break;
1567
1568 case BFD_RELOC_TILEGX_HW1_LAST:
1569 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST:
1570 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST:
1571 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_PCREL:
1572 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_PCREL:
1573 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_GOT:
1574 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_GOT:
1575 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
1576 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
1577 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
1578 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
1579 special = O_hw1_last;
1580 break;
1581
1582 case BFD_RELOC_TILEGX_HW2:
1583 case BFD_RELOC_TILEGX_IMM16_X0_HW2:
1584 case BFD_RELOC_TILEGX_IMM16_X1_HW2:
1585 case BFD_RELOC_TILEGX_IMM16_X0_HW2_PCREL:
1586 case BFD_RELOC_TILEGX_IMM16_X1_HW2_PCREL:
1587 case BFD_RELOC_TILEGX_IMM16_X0_HW2_GOT:
1588 case BFD_RELOC_TILEGX_IMM16_X1_HW2_GOT:
1589 case BFD_RELOC_TILEGX_IMM16_X0_HW2_TLS_GD:
1590 case BFD_RELOC_TILEGX_IMM16_X1_HW2_TLS_GD:
1591 case BFD_RELOC_TILEGX_IMM16_X0_HW2_TLS_IE:
1592 case BFD_RELOC_TILEGX_IMM16_X1_HW2_TLS_IE:
1593 special = O_hw2;
1594 break;
1595
1596 case BFD_RELOC_TILEGX_HW2_LAST:
1597 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST:
1598 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST:
1599 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_PCREL:
1600 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_PCREL:
1601 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_GOT:
1602 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_GOT:
1603 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_TLS_GD:
1604 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_TLS_GD:
1605 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_TLS_IE:
1606 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_TLS_IE:
1607 special = O_hw2_last;
1608 break;
1609
1610 case BFD_RELOC_TILEGX_HW3:
1611 case BFD_RELOC_TILEGX_IMM16_X0_HW3:
1612 case BFD_RELOC_TILEGX_IMM16_X1_HW3:
1613 case BFD_RELOC_TILEGX_IMM16_X0_HW3_PCREL:
1614 case BFD_RELOC_TILEGX_IMM16_X1_HW3_PCREL:
1615 case BFD_RELOC_TILEGX_IMM16_X0_HW3_GOT:
1616 case BFD_RELOC_TILEGX_IMM16_X1_HW3_GOT:
1617 case BFD_RELOC_TILEGX_IMM16_X0_HW3_TLS_GD:
1618 case BFD_RELOC_TILEGX_IMM16_X1_HW3_TLS_GD:
1619 case BFD_RELOC_TILEGX_IMM16_X0_HW3_TLS_IE:
1620 case BFD_RELOC_TILEGX_IMM16_X1_HW3_TLS_IE:
1621 special = O_hw3;
1622 break;
1623
1624 default:
1625 /* Do nothing */
1626 break;
1627 }
1628
1629 if (special != O_illegal)
1630 {
1631 *valP = value = apply_special_operator (special, value,
1632 fixP->fx_file, fixP->fx_line);
1633 }
1634
1635 p = fixP->fx_frag->fr_literal + fixP->fx_where;
1636
1637 operand = fixP->tc_fix_data;
1638 if (operand != NULL)
1639 {
1640 /* It's an instruction operand. */
1641 tilegx_bundle_bits bits =
1642 insert_operand (0, operand, value, fixP->fx_file, fixP->fx_line);
1643
1644 /* Note that we might either be writing out bits for a bundle
1645 or a static network instruction, which are different sizes, so it's
1646 important to stop touching memory once we run out of bits.
1647 ORing in values is OK since we know the existing bits for
1648 this operand are zero. */
1649 for (; bits != 0; bits >>= 8)
1650 *p++ |= (char)bits;
1651 }
1652 else
1653 {
1654 /* Some other kind of relocation. */
1655 switch (fixP->fx_r_type)
1656 {
1657 case BFD_RELOC_8:
1658 case BFD_RELOC_8_PCREL:
1659 md_number_to_chars (p, value, 1);
1660 break;
1661
1662 case BFD_RELOC_16:
1663 case BFD_RELOC_16_PCREL:
1664 md_number_to_chars (p, value, 2);
1665 break;
1666
1667 case BFD_RELOC_32:
1668 case BFD_RELOC_32_PCREL:
1669 md_number_to_chars (p, value, 4);
1670 break;
1671
1672 case BFD_RELOC_64:
1673 case BFD_RELOC_64_PCREL:
1674 md_number_to_chars (p, value, 8);
1675 break;
1676
1677 default:
1678 /* Leave it for the linker. */
1679 return;
1680 }
1681 }
1682
1683 fixP->fx_done = 1;
1684}
1685
1686
1687/* Generate the BFD reloc to be stuck in the object file from the
1688 fixup used internally in the assembler. */
1689
1690arelent *
1691tc_gen_reloc (asection *sec ATTRIBUTE_UNUSED, fixS *fixp)
1692{
1693 arelent *reloc;
1694
1695 reloc = (arelent *) xmalloc (sizeof (arelent));
1696 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1697 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1698 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1699
1700 /* Make sure none of our internal relocations make it this far.
1701 They'd better have been fully resolved by this point. */
1702 gas_assert ((int) fixp->fx_r_type > 0);
1703
1704 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1705 if (reloc->howto == NULL)
1706 {
1707 as_bad_where (fixp->fx_file, fixp->fx_line,
1708 _("cannot represent `%s' relocation in object file"),
1709 bfd_get_reloc_code_name (fixp->fx_r_type));
1710 return NULL;
1711 }
1712
1713 if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
1714 {
1715 as_fatal (_("internal error? cannot generate `%s' relocation (%d, %d)"),
1716 bfd_get_reloc_code_name (fixp->fx_r_type),
1717 fixp->fx_pcrel, reloc->howto->pc_relative);
1718 }
1719 gas_assert (!fixp->fx_pcrel == !reloc->howto->pc_relative);
1720
1721 reloc->addend = fixp->fx_offset;
1722
1723 return reloc;
1724}
1725
1726
1727/* The location from which a PC relative jump should be calculated,
1728 given a PC relative reloc. */
1729
1730long
1731md_pcrel_from (fixS *fixP)
1732{
1733 return fixP->fx_frag->fr_address + fixP->fx_where;
1734}
1735
1736
1737/* Return 1 if it's OK to adjust a reloc by replacing the symbol with
1738 a section symbol plus some offset. */
1739int
1740tilegx_fix_adjustable (fixS *fix)
1741{
1742 /* Prevent all adjustments to global symbols */
1743 if (S_IS_EXTERNAL (fix->fx_addsy) || S_IS_WEAK (fix->fx_addsy))
1744 return 0;
1745
1746 return 1;
1747}
1748
1749
1750int
1751tilegx_unrecognized_line (int ch)
1752{
1753 switch (ch)
1754 {
1755 case '{':
1756 if (inside_bundle)
1757 {
1758 as_bad (_("Found '{' when already bundling."));
1759 }
1760 else
1761 {
1762 inside_bundle = 1;
1763 current_bundle_index = 0;
1764 }
1765 return 1;
1766
1767 case '}':
1768 if (!inside_bundle)
1769 {
1770 as_bad (_("Found '}' when not bundling."));
1771 }
1772 else
1773 {
1774 tilegx_flush_bundle ();
1775 }
1776
1777 /* Allow '{' to follow on the same line. We also allow ";;", but that
1778 happens automatically because ';' is an end of line marker. */
1779 SKIP_WHITESPACE ();
1780 if (input_line_pointer[0] == '{')
1781 {
1782 input_line_pointer++;
1783 return tilegx_unrecognized_line ('{');
1784 }
1785
1786 demand_empty_rest_of_line ();
1787 return 1;
1788
1789 default:
1790 break;
1791 }
1792
1793 /* Not a valid line. */
1794 return 0;
1795}
1796
1797
1798/* This is called from HANDLE_ALIGN in write.c. Fill in the contents
1799 of an rs_align_code fragment. */
1800
1801void
1802tilegx_handle_align (fragS *fragp)
1803{
1804 addressT bytes, fix;
1805 char *p;
1806
1807 if (fragp->fr_type != rs_align_code)
1808 return;
1809
1810 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
1811 p = fragp->fr_literal + fragp->fr_fix;
1812 fix = 0;
1813
1814 /* Determine the bits for NOP. */
1815 const struct tilegx_opcode *nop_opcode =
1816 &tilegx_opcodes[TILEGX_OPC_NOP];
1817 tilegx_bundle_bits nop =
1818 ( nop_opcode->fixed_bit_values[TILEGX_PIPELINE_X0]
1819 | nop_opcode->fixed_bit_values[TILEGX_PIPELINE_X1]);
1820
1821 if ((bytes & (TILEGX_BUNDLE_SIZE_IN_BYTES - 1)) != 0)
1822 {
1823 fix = bytes & (TILEGX_BUNDLE_SIZE_IN_BYTES - 1);
1824 memset (p, 0, fix);
1825 p += fix;
1826 bytes -= fix;
1827 }
1828
1829 number_to_chars_littleendian (p, nop, 8);
1830 fragp->fr_fix += fix;
1831 fragp->fr_var = TILEGX_BUNDLE_SIZE_IN_BYTES;
1832}
1833
1834/* Standard calling conventions leave the CFA at SP on entry. */
1835void
1836tilegx_cfi_frame_initial_instructions (void)
1837{
1838 cfi_add_CFA_def_cfa_register (54);
1839}
1840
1841int
1842tc_tilegx_regname_to_dw2regnum (char *regname)
1843{
1844 int i;
1845 for (i = 0; i < TILEGX_NUM_REGISTERS; i++)
1846 {
1847 if (!strcmp (regname, tilegx_register_names[i]))
1848 return i;
1849 }
1850
1851 return -1;
1852}
This page took 0.115139 seconds and 4 git commands to generate.