Add big-endian support for tilegx.
[binutils.git] / gas / config / tc-tilegx.c
blob01237d1286365542dd9fb78f1af708a4c75595c6
1 /* tc-tilegx.c -- Assemble for a Tile-Gx chip.
2 Copyright 2011 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
21 #include "as.h"
22 #include "struc-symbol.h"
23 #include "subsegs.h"
25 #include "elf/tilegx.h"
26 #include "opcode/tilegx.h"
28 #include "dwarf2dbg.h"
29 #include "dw2gencfi.h"
31 #include "safe-ctype.h"
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
44 /* Generic assembler global variables which must be defined by all
45 targets. */
47 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
48 int tilegx_cie_data_alignment;
50 /* Characters which always start a comment. */
51 const char comment_chars[] = "#";
53 /* Characters which start a comment at the beginning of a line. */
54 const char line_comment_chars[] = "#";
56 /* Characters which may be used to separate multiple commands on a
57 single line. */
58 const char line_separator_chars[] = ";";
60 /* Characters which are used to indicate an exponent in a floating
61 point number. */
62 const char EXP_CHARS[] = "eE";
64 /* Characters which mean that a number is a floating point constant,
65 as in 0d1.0. */
66 const char FLT_CHARS[] = "rRsSfFdDxXpP";
68 /* Either 32 or 64. */
69 static int tilegx_arch_size = 64;
72 const char *
73 tilegx_target_format (void)
75 if (target_big_endian) {
76 return tilegx_arch_size == 64 ? "elf64-tilegx-be" : "elf32-tilegx-be";
77 } else {
78 return tilegx_arch_size == 64 ? "elf64-tilegx-le" : "elf32-tilegx-le";
83 #define OPTION_32 (OPTION_MD_BASE + 0)
84 #define OPTION_64 (OPTION_MD_BASE + 1)
85 #define OPTION_EB (OPTION_MD_BASE + 2)
86 #define OPTION_EL (OPTION_MD_BASE + 3)
88 const char *md_shortopts = "VQ:";
90 struct option md_longopts[] =
92 {"32", no_argument, NULL, OPTION_32},
93 {"64", no_argument, NULL, OPTION_64},
94 {"EB", no_argument, NULL, OPTION_EB },
95 {"EL", no_argument, NULL, OPTION_EL },
96 {NULL, no_argument, NULL, 0}
99 size_t md_longopts_size = sizeof (md_longopts);
102 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
104 switch (c)
106 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
107 should be emitted or not. FIXME: Not implemented. */
108 case 'Q':
109 break;
111 /* -V: SVR4 argument to print version ID. */
112 case 'V':
113 print_version_id ();
114 break;
116 case OPTION_32:
117 tilegx_arch_size = 32;
118 break;
120 case OPTION_64:
121 tilegx_arch_size = 64;
122 break;
124 case OPTION_EB:
125 target_big_endian = 1;
126 break;
128 case OPTION_EL:
129 target_big_endian = 0;
130 break;
132 default:
133 return 0;
136 return 1;
139 void
140 md_show_usage (FILE *stream)
142 fprintf (stream, _("\
143 -Q ignored\n\
144 -V print assembler version number\n\
145 -EB/-EL generate big-endian/little-endian code\n\
146 --32/--64 generate 32bit/64bit code\n"));
150 /* Extra expression types. */
152 #define O_hw0 O_md1
153 #define O_hw1 O_md2
154 #define O_hw2 O_md3
155 #define O_hw3 O_md4
156 #define O_hw0_last O_md5
157 #define O_hw1_last O_md6
158 #define O_hw2_last O_md7
159 #define O_hw0_got O_md8
160 #define O_hw1_got O_md9
161 #define O_hw2_got O_md10
162 #define O_hw3_got O_md11
163 #define O_hw0_last_got O_md12
164 #define O_hw1_last_got O_md13
165 #define O_hw2_last_got O_md14
166 #define O_plt O_md15
167 #define O_hw0_tls_gd O_md16
168 #define O_hw1_tls_gd O_md17
169 #define O_hw2_tls_gd O_md18
170 #define O_hw3_tls_gd O_md19
171 #define O_hw0_last_tls_gd O_md20
172 #define O_hw1_last_tls_gd O_md21
173 #define O_hw2_last_tls_gd O_md22
174 #define O_hw0_tls_ie O_md23
175 #define O_hw1_tls_ie O_md24
176 #define O_hw2_tls_ie O_md25
177 #define O_hw3_tls_ie O_md26
178 #define O_hw0_last_tls_ie O_md27
179 #define O_hw1_last_tls_ie O_md28
180 #define O_hw2_last_tls_ie O_md29
182 static struct hash_control *special_operator_hash;
184 /* Hash tables for instruction mnemonic lookup. */
185 static struct hash_control *op_hash;
187 /* Hash table for spr lookup. */
188 static struct hash_control *spr_hash;
190 /* True temporarily while parsing an SPR expression. This changes the
191 * namespace to include SPR names. */
192 static int parsing_spr;
194 /* Are we currently inside `{ ... }'? */
195 static int inside_bundle;
197 struct tilegx_instruction
199 const struct tilegx_opcode *opcode;
200 tilegx_pipeline pipe;
201 expressionS operand_values[TILEGX_MAX_OPERANDS];
204 /* This keeps track of the current bundle being built up. */
205 static struct tilegx_instruction current_bundle[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
207 /* Index in current_bundle for the next instruction to parse. */
208 static int current_bundle_index;
210 /* Allow 'r63' in addition to 'zero', etc. Normally we disallow this as
211 'zero' is not a real register, so using it accidentally would be a
212 nasty bug. For other registers, such as 'sp', code using multiple names
213 for the same physical register is excessively confusing.
215 The '.require_canonical_reg_names' pseudo-op turns this error on,
216 and the '.no_require_canonical_reg_names' pseudo-op turns this off.
217 By default the error is on. */
218 static int require_canonical_reg_names;
220 /* Allow bundles that do undefined or suspicious things like write
221 two different values to the same register at the same time.
223 The '.no_allow_suspicious_bundles' pseudo-op turns this error on,
224 and the '.allow_suspicious_bundles' pseudo-op turns this off. */
225 static int allow_suspicious_bundles;
228 /* A hash table of main processor registers, mapping each register name
229 to its index.
231 Furthermore, if the register number is greater than the number
232 of registers for that processor, the user used an illegal alias
233 for that register (e.g. r63 instead of zero), so we should generate
234 a warning. The attempted register number can be found by clearing
235 NONCANONICAL_REG_NAME_FLAG. */
236 static struct hash_control *main_reg_hash;
239 /* We cannot unambiguously store a 0 in a hash table and look it up,
240 so we OR in this flag to every canonical register. */
241 #define CANONICAL_REG_NAME_FLAG 0x1000
243 /* By default we disallow register aliases like r63, but we record
244 them in the hash table in case the .no_require_canonical_reg_names
245 directive is used. Noncanonical names have this value added to them. */
246 #define NONCANONICAL_REG_NAME_FLAG 0x2000
248 /* Discards flags for register hash table entries and returns the
249 reg number. */
250 #define EXTRACT_REGNO(p) ((p) & 63)
252 /* This function is called once, at assembler startup time. It should
253 set up all the tables, etc., that the MD part of the assembler will
254 need. */
256 void
257 md_begin (void)
259 const struct tilegx_opcode *op;
260 int i;
261 int mach = (tilegx_arch_size == 64) ? bfd_mach_tilegx : bfd_mach_tilegx32;
263 if (! bfd_set_arch_mach (stdoutput, bfd_arch_tilegx, mach))
264 as_warn (_("Could not set architecture and machine"));
266 /* Guarantee text section is aligned. */
267 bfd_set_section_alignment (stdoutput, text_section,
268 TILEGX_LOG2_BUNDLE_ALIGNMENT_IN_BYTES);
270 require_canonical_reg_names = 1;
271 allow_suspicious_bundles = 0;
272 current_bundle_index = 0;
273 inside_bundle = 0;
275 tilegx_cie_data_alignment = (tilegx_arch_size == 64 ? -8 : -4);
277 /* Initialize special operator hash table. */
278 special_operator_hash = hash_new ();
279 #define INSERT_SPECIAL_OP(name) \
280 hash_insert (special_operator_hash, #name, (void *)O_##name)
282 INSERT_SPECIAL_OP (hw0);
283 INSERT_SPECIAL_OP (hw1);
284 INSERT_SPECIAL_OP (hw2);
285 INSERT_SPECIAL_OP (hw3);
286 INSERT_SPECIAL_OP (hw0_last);
287 INSERT_SPECIAL_OP (hw1_last);
288 INSERT_SPECIAL_OP (hw2_last);
289 /* hw3_last is a convenience alias for the equivalent hw3. */
290 hash_insert (special_operator_hash, "hw3_last", (void*)O_hw3);
291 INSERT_SPECIAL_OP (hw0_got);
292 INSERT_SPECIAL_OP (hw1_got);
293 INSERT_SPECIAL_OP (hw2_got);
294 INSERT_SPECIAL_OP (hw3_got);
295 INSERT_SPECIAL_OP (hw0_last_got);
296 INSERT_SPECIAL_OP (hw1_last_got);
297 INSERT_SPECIAL_OP (hw2_last_got);
298 INSERT_SPECIAL_OP(plt);
299 INSERT_SPECIAL_OP (hw0_tls_gd);
300 INSERT_SPECIAL_OP (hw1_tls_gd);
301 INSERT_SPECIAL_OP (hw2_tls_gd);
302 INSERT_SPECIAL_OP (hw3_tls_gd);
303 INSERT_SPECIAL_OP (hw0_last_tls_gd);
304 INSERT_SPECIAL_OP (hw1_last_tls_gd);
305 INSERT_SPECIAL_OP (hw2_last_tls_gd);
306 INSERT_SPECIAL_OP (hw0_tls_ie);
307 INSERT_SPECIAL_OP (hw1_tls_ie);
308 INSERT_SPECIAL_OP (hw2_tls_ie);
309 INSERT_SPECIAL_OP (hw3_tls_ie);
310 INSERT_SPECIAL_OP (hw0_last_tls_ie);
311 INSERT_SPECIAL_OP (hw1_last_tls_ie);
312 INSERT_SPECIAL_OP (hw2_last_tls_ie);
313 #undef INSERT_SPECIAL_OP
315 /* Initialize op_hash hash table. */
316 op_hash = hash_new ();
317 for (op = &tilegx_opcodes[0]; op->name != NULL; op++)
319 const char *hash_err = hash_insert (op_hash, op->name, (void *)op);
320 if (hash_err != NULL)
321 as_fatal (_("Internal Error: Can't hash %s: %s"), op->name, hash_err);
324 /* Initialize the spr hash table. */
325 parsing_spr = 0;
326 spr_hash = hash_new ();
327 for (i = 0; i < tilegx_num_sprs; i++)
328 hash_insert (spr_hash, tilegx_sprs[i].name,
329 (void *) &tilegx_sprs[i]);
331 /* Set up the main_reg_hash table. We use this instead of
332 creating a symbol in the register section to avoid ambiguities
333 with labels that have the same names as registers. */
334 main_reg_hash = hash_new ();
335 for (i = 0; i < TILEGX_NUM_REGISTERS; i++)
337 char buf[64];
339 hash_insert (main_reg_hash, tilegx_register_names[i],
340 (void *) (long) (i | CANONICAL_REG_NAME_FLAG));
342 /* See if we should insert a noncanonical alias, like r63. */
343 sprintf (buf, "r%d", i);
344 if (strcmp (buf, tilegx_register_names[i]) != 0)
345 hash_insert (main_reg_hash, xstrdup (buf),
346 (void *) (long) (i | NONCANONICAL_REG_NAME_FLAG));
350 #define BUNDLE_TEMPLATE_MASK(p0, p1, p2) \
351 ((p0) | ((p1) << 8) | ((p2) << 16))
352 #define BUNDLE_TEMPLATE(p0, p1, p2) \
353 { { (p0), (p1), (p2) }, \
354 BUNDLE_TEMPLATE_MASK(1 << (p0), 1 << (p1), (1 << (p2))) \
357 #define NO_PIPELINE TILEGX_NUM_PIPELINE_ENCODINGS
359 struct bundle_template
361 tilegx_pipeline pipe[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
362 unsigned int pipe_mask;
365 static const struct bundle_template bundle_templates[] =
367 /* In Y format we must always have something in Y2, since it has
368 no fnop, so this conveys that Y2 must always be used. */
369 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y0, TILEGX_PIPELINE_Y2, NO_PIPELINE),
370 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y1, TILEGX_PIPELINE_Y2, NO_PIPELINE),
371 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y2, TILEGX_PIPELINE_Y0, NO_PIPELINE),
372 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y2, TILEGX_PIPELINE_Y1, NO_PIPELINE),
374 /* Y format has three instructions. */
375 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y0,TILEGX_PIPELINE_Y1,TILEGX_PIPELINE_Y2),
376 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y0,TILEGX_PIPELINE_Y2,TILEGX_PIPELINE_Y1),
377 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y1,TILEGX_PIPELINE_Y0,TILEGX_PIPELINE_Y2),
378 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y1,TILEGX_PIPELINE_Y2,TILEGX_PIPELINE_Y0),
379 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y2,TILEGX_PIPELINE_Y0,TILEGX_PIPELINE_Y1),
380 BUNDLE_TEMPLATE(TILEGX_PIPELINE_Y2,TILEGX_PIPELINE_Y1,TILEGX_PIPELINE_Y0),
382 /* X format has only two instructions. */
383 BUNDLE_TEMPLATE(TILEGX_PIPELINE_X0, TILEGX_PIPELINE_X1, NO_PIPELINE),
384 BUNDLE_TEMPLATE(TILEGX_PIPELINE_X1, TILEGX_PIPELINE_X0, NO_PIPELINE)
388 static void
389 prepend_nop_to_bundle (tilegx_mnemonic mnemonic)
391 memmove (&current_bundle[1], &current_bundle[0],
392 current_bundle_index * sizeof current_bundle[0]);
393 current_bundle[0].opcode = &tilegx_opcodes[mnemonic];
394 ++current_bundle_index;
397 static tilegx_bundle_bits
398 insert_operand (tilegx_bundle_bits bits,
399 const struct tilegx_operand *operand,
400 int operand_value,
401 char *file,
402 unsigned lineno)
404 /* Range-check the immediate. */
405 int num_bits = operand->num_bits;
407 operand_value >>= operand->rightshift;
409 if (bfd_check_overflow (operand->is_signed
410 ? complain_overflow_signed
411 : complain_overflow_unsigned,
412 num_bits,
414 bfd_arch_bits_per_address (stdoutput),
415 operand_value)
416 != bfd_reloc_ok)
418 offsetT min, max;
419 if (operand->is_signed)
421 min = -(1 << (num_bits - 1));
422 max = (1 << (num_bits - 1)) - 1;
424 else
426 min = 0;
427 max = (1 << num_bits) - 1;
429 as_bad_value_out_of_range (_("operand"), operand_value, min, max,
430 file, lineno);
433 /* Write out the bits for the immediate. */
434 return bits | operand->insert (operand_value);
438 static int
439 apply_special_operator (operatorT op, offsetT num, char *file, unsigned lineno)
441 int ret;
442 int check_shift = -1;
444 switch (op)
446 case O_hw0_last_tls_gd:
447 case O_hw0_last_tls_ie:
448 case O_hw0_last:
449 check_shift = 0;
450 /* Fall through. */
451 case O_hw0_tls_gd:
452 case O_hw0_tls_ie:
453 case O_hw0:
454 ret = (signed short)num;
455 break;
457 case O_hw1_last_tls_gd:
458 case O_hw1_last_tls_ie:
459 case O_hw1_last:
460 check_shift = 16;
461 /* Fall through. */
462 case O_hw1_tls_gd:
463 case O_hw1_tls_ie:
464 case O_hw1:
465 ret = (signed short)(num >> 16);
466 break;
468 case O_hw2_last_tls_gd:
469 case O_hw2_last_tls_ie:
470 case O_hw2_last:
471 check_shift = 32;
472 /* Fall through. */
473 case O_hw2_tls_gd:
474 case O_hw2_tls_ie:
475 case O_hw2:
476 ret = (signed short)(num >> 32);
477 break;
479 case O_hw3_tls_gd:
480 case O_hw3_tls_ie:
481 case O_hw3:
482 ret = (signed short)(num >> 48);
483 break;
485 default:
486 abort ();
487 break;
490 if (check_shift >= 0 && ret != (num >> check_shift))
492 as_bad_value_out_of_range (_("operand"), num,
493 ~0ULL << (check_shift + 16 - 1),
494 ~0ULL >> (64 - (check_shift + 16 - 1)),
495 file, lineno);
498 return ret;
501 static tilegx_bundle_bits
502 emit_tilegx_instruction (tilegx_bundle_bits bits,
503 int num_operands,
504 const unsigned char *operands,
505 expressionS *operand_values,
506 char *bundle_start)
508 int i;
510 for (i = 0; i < num_operands; i++)
512 const struct tilegx_operand *operand =
513 &tilegx_operands[operands[i]];
514 expressionS *operand_exp = &operand_values[i];
515 int is_pc_relative = operand->is_pc_relative;
517 if (operand_exp->X_op == O_register
518 || (operand_exp->X_op == O_constant && !is_pc_relative))
520 /* We know what the bits are right now, so insert them. */
521 bits = insert_operand (bits, operand, operand_exp->X_add_number,
522 NULL, 0);
524 else
526 bfd_reloc_code_real_type reloc = operand->default_reloc;
527 expressionS subexp;
528 int die = 0, use_subexp = 0, require_symbol = 0;
529 fixS *fixP;
531 /* Take an expression like hw0(x) and turn it into x with
532 a different reloc type. */
533 switch (operand_exp->X_op)
535 #define HANDLE_OP16(suffix) \
536 switch (reloc) \
538 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST: \
539 reloc = BFD_RELOC_TILEGX_IMM16_X0_##suffix; \
540 break; \
541 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST: \
542 reloc = BFD_RELOC_TILEGX_IMM16_X1_##suffix; \
543 break; \
544 default: \
545 die = 1; \
546 break; \
548 use_subexp = 1
550 case O_hw0:
551 HANDLE_OP16 (HW0);
552 break;
554 case O_hw1:
555 HANDLE_OP16 (HW1);
556 break;
558 case O_hw2:
559 HANDLE_OP16 (HW2);
560 break;
562 case O_hw3:
563 HANDLE_OP16 (HW3);
564 break;
566 case O_hw0_last:
567 HANDLE_OP16 (HW0_LAST);
568 break;
570 case O_hw1_last:
571 HANDLE_OP16 (HW1_LAST);
572 break;
574 case O_hw2_last:
575 HANDLE_OP16 (HW2_LAST);
576 break;
578 case O_hw0_got:
579 HANDLE_OP16 (HW0_GOT);
580 require_symbol = 1;
581 break;
583 case O_hw1_got:
584 HANDLE_OP16 (HW1_GOT);
585 require_symbol = 1;
586 break;
588 case O_hw2_got:
589 HANDLE_OP16 (HW2_GOT);
590 require_symbol = 1;
591 break;
593 case O_hw3_got:
594 HANDLE_OP16 (HW3_GOT);
595 require_symbol = 1;
596 break;
598 case O_hw0_last_got:
599 HANDLE_OP16 (HW0_LAST_GOT);
600 require_symbol = 1;
601 break;
603 case O_hw1_last_got:
604 HANDLE_OP16 (HW1_LAST_GOT);
605 require_symbol = 1;
606 break;
608 case O_hw2_last_got:
609 HANDLE_OP16 (HW2_LAST_GOT);
610 require_symbol = 1;
611 break;
613 case O_hw0_tls_gd:
614 HANDLE_OP16 (HW0_TLS_GD);
615 require_symbol = 1;
616 break;
618 case O_hw1_tls_gd:
619 HANDLE_OP16 (HW1_TLS_GD);
620 require_symbol = 1;
621 break;
623 case O_hw2_tls_gd:
624 HANDLE_OP16 (HW2_TLS_GD);
625 require_symbol = 1;
626 break;
628 case O_hw3_tls_gd:
629 HANDLE_OP16 (HW3_TLS_GD);
630 require_symbol = 1;
631 break;
633 case O_hw0_last_tls_gd:
634 HANDLE_OP16 (HW0_LAST_TLS_GD);
635 require_symbol = 1;
636 break;
638 case O_hw1_last_tls_gd:
639 HANDLE_OP16 (HW1_LAST_TLS_GD);
640 require_symbol = 1;
641 break;
643 case O_hw2_last_tls_gd:
644 HANDLE_OP16 (HW2_LAST_TLS_GD);
645 require_symbol = 1;
646 break;
648 case O_hw0_tls_ie:
649 HANDLE_OP16 (HW0_TLS_IE);
650 require_symbol = 1;
651 break;
653 case O_hw1_tls_ie:
654 HANDLE_OP16 (HW1_TLS_IE);
655 require_symbol = 1;
656 break;
658 case O_hw2_tls_ie:
659 HANDLE_OP16 (HW2_TLS_IE);
660 require_symbol = 1;
661 break;
663 case O_hw3_tls_ie:
664 HANDLE_OP16 (HW3_TLS_IE);
665 require_symbol = 1;
666 break;
668 case O_hw0_last_tls_ie:
669 HANDLE_OP16 (HW0_LAST_TLS_IE);
670 require_symbol = 1;
671 break;
673 case O_hw1_last_tls_ie:
674 HANDLE_OP16 (HW1_LAST_TLS_IE);
675 require_symbol = 1;
676 break;
678 case O_hw2_last_tls_ie:
679 HANDLE_OP16 (HW2_LAST_TLS_IE);
680 require_symbol = 1;
681 break;
683 #undef HANDLE_OP16
685 case O_plt:
686 switch (reloc)
688 case BFD_RELOC_TILEGX_JUMPOFF_X1:
689 reloc = BFD_RELOC_TILEGX_JUMPOFF_X1_PLT;
690 break;
691 default:
692 die = 1;
693 break;
695 use_subexp = 1;
696 require_symbol = 1;
697 break;
699 default:
700 /* Do nothing. */
701 break;
704 if (die)
706 as_bad (_("Invalid operator for operand."));
708 else if (use_subexp)
710 /* Now that we've changed the reloc, change ha16(x) into x,
711 etc. */
713 if (operand_exp->X_add_symbol->sy_value.X_md)
715 /* HACK: We used X_md to mark this symbol as a fake wrapper
716 around a real expression. To unwrap it, we just grab its
717 value here. */
718 operand_exp = &operand_exp->X_add_symbol->sy_value;
720 if (require_symbol)
722 /* Look at the expression, and reject it if it's not a
723 plain symbol. */
724 if (operand_exp->X_op != O_symbol
725 || operand_exp->X_add_number != 0)
726 as_bad (_("Operator may only be applied to symbols."));
729 else
731 /* The value of this expression is an actual symbol, so
732 turn that into an expression. */
733 memset (&subexp, 0, sizeof subexp);
734 subexp.X_op = O_symbol;
735 subexp.X_add_symbol = operand_exp->X_add_symbol;
736 operand_exp = &subexp;
740 /* Create a fixup to handle this later. */
741 fixP = fix_new_exp (frag_now,
742 bundle_start - frag_now->fr_literal,
743 (operand->num_bits + 7) >> 3,
744 operand_exp,
745 is_pc_relative,
746 reloc);
747 fixP->tc_fix_data = operand;
749 /* Don't do overflow checking if we are applying a function like
750 ha16. */
751 fixP->fx_no_overflow |= use_subexp;
754 return bits;
758 /* Detects and complains if two instructions in current_bundle write
759 to the same register, either implicitly or explicitly, or if a
760 read-only register is written. */
761 static void
762 check_illegal_reg_writes (void)
764 BFD_HOST_U_64_BIT all_regs_written = 0;
765 int j;
767 for (j = 0; j < current_bundle_index; j++)
769 const struct tilegx_instruction *instr = &current_bundle[j];
770 int k;
771 BFD_HOST_U_64_BIT regs =
772 ((BFD_HOST_U_64_BIT)1) << instr->opcode->implicitly_written_register;
773 BFD_HOST_U_64_BIT conflict;
775 for (k = 0; k < instr->opcode->num_operands; k++)
777 const struct tilegx_operand *operand =
778 &tilegx_operands[instr->opcode->operands[instr->pipe][k]];
780 if (operand->is_dest_reg)
782 int regno = instr->operand_values[k].X_add_number;
783 BFD_HOST_U_64_BIT mask = ((BFD_HOST_U_64_BIT)1) << regno;
785 if ((mask & ( (((BFD_HOST_U_64_BIT)1) << TREG_IDN1)
786 | (((BFD_HOST_U_64_BIT)1) << TREG_UDN1)
787 | (((BFD_HOST_U_64_BIT)1) << TREG_UDN2)
788 | (((BFD_HOST_U_64_BIT)1) << TREG_UDN3))) != 0
789 && !allow_suspicious_bundles)
791 as_bad (_("Writes to register '%s' are not allowed."),
792 tilegx_register_names[regno]);
795 regs |= mask;
799 /* Writing to the zero register doesn't count. */
800 regs &= ~(((BFD_HOST_U_64_BIT)1) << TREG_ZERO);
802 conflict = all_regs_written & regs;
803 if (conflict != 0 && !allow_suspicious_bundles)
805 /* Find which register caused the conflict. */
806 const char *conflicting_reg_name = "???";
807 int i;
809 for (i = 0; i < TILEGX_NUM_REGISTERS; i++)
811 if (((conflict >> i) & 1) != 0)
813 conflicting_reg_name = tilegx_register_names[i];
814 break;
818 as_bad (_("Two instructions in the same bundle both write "
819 "to register %s, which is not allowed."),
820 conflicting_reg_name);
823 all_regs_written |= regs;
828 static void
829 tilegx_flush_bundle (void)
831 unsigned i;
832 int j;
833 addressT addr_mod;
834 unsigned compatible_pipes;
835 const struct bundle_template *match;
836 char *f;
838 inside_bundle = 0;
840 switch (current_bundle_index)
842 case 0:
843 /* No instructions. */
844 return;
845 case 1:
846 if (current_bundle[0].opcode->can_bundle)
848 /* Simplify later logic by adding an explicit fnop. */
849 prepend_nop_to_bundle (TILEGX_OPC_FNOP);
851 else
853 /* This instruction cannot be bundled with anything else.
854 Prepend an explicit 'nop', rather than an 'fnop', because
855 fnops can be replaced by later binary-processing tools while
856 nops cannot. */
857 prepend_nop_to_bundle (TILEGX_OPC_NOP);
859 break;
860 default:
861 if (!allow_suspicious_bundles)
863 /* Make sure all instructions can be bundled with other
864 instructions. */
865 const struct tilegx_opcode *cannot_bundle = NULL;
866 bfd_boolean seen_non_nop = FALSE;
868 for (j = 0; j < current_bundle_index; j++)
870 const struct tilegx_opcode *op = current_bundle[j].opcode;
872 if (!op->can_bundle && cannot_bundle == NULL)
873 cannot_bundle = op;
874 else if (op->mnemonic != TILEGX_OPC_NOP
875 && op->mnemonic != TILEGX_OPC_INFO
876 && op->mnemonic != TILEGX_OPC_INFOL)
877 seen_non_nop = TRUE;
880 if (cannot_bundle != NULL && seen_non_nop)
882 current_bundle_index = 0;
883 as_bad (_("'%s' may not be bundled with other instructions."),
884 cannot_bundle->name);
885 return;
888 break;
891 compatible_pipes =
892 BUNDLE_TEMPLATE_MASK(current_bundle[0].opcode->pipes,
893 current_bundle[1].opcode->pipes,
894 (current_bundle_index == 3
895 ? current_bundle[2].opcode->pipes
896 : (1 << NO_PIPELINE)));
898 /* Find a template that works, if any. */
899 match = NULL;
900 for (i = 0; i < sizeof bundle_templates / sizeof bundle_templates[0]; i++)
902 const struct bundle_template *b = &bundle_templates[i];
903 if ((b->pipe_mask & compatible_pipes) == b->pipe_mask)
905 match = b;
906 break;
910 if (match == NULL)
912 current_bundle_index = 0;
913 as_bad (_("Invalid combination of instructions for bundle."));
914 return;
917 /* If the section seems to have no alignment set yet, go ahead and
918 make it large enough to hold code. */
919 if (bfd_get_section_alignment (stdoutput, now_seg) == 0)
920 bfd_set_section_alignment (stdoutput, now_seg,
921 TILEGX_LOG2_BUNDLE_ALIGNMENT_IN_BYTES);
923 for (j = 0; j < current_bundle_index; j++)
924 current_bundle[j].pipe = match->pipe[j];
926 if (current_bundle_index == 2 && !tilegx_is_x_pipeline (match->pipe[0]))
928 /* We are in Y mode with only two instructions, so add an FNOP. */
929 prepend_nop_to_bundle (TILEGX_OPC_FNOP);
931 /* Figure out what pipe the fnop must be in via arithmetic.
932 * p0 + p1 + p2 must sum to the sum of TILEGX_PIPELINE_Y[012]. */
933 current_bundle[0].pipe =
934 (tilegx_pipeline)((TILEGX_PIPELINE_Y0
935 + TILEGX_PIPELINE_Y1
936 + TILEGX_PIPELINE_Y2) -
937 (current_bundle[1].pipe + current_bundle[2].pipe));
940 check_illegal_reg_writes ();
942 f = frag_more (TILEGX_BUNDLE_SIZE_IN_BYTES);
944 /* Check to see if this bundle is at an offset that is a multiple of 8-bytes
945 from the start of the frag. */
946 addr_mod = frag_now_fix () & (TILEGX_BUNDLE_ALIGNMENT_IN_BYTES - 1);
947 if (frag_now->has_code && frag_now->insn_addr != addr_mod)
948 as_bad (_("instruction address is not a multiple of 8"));
949 frag_now->insn_addr = addr_mod;
950 frag_now->has_code = 1;
952 tilegx_bundle_bits bits = 0;
953 for (j = 0; j < current_bundle_index; j++)
955 struct tilegx_instruction *instr = &current_bundle[j];
956 tilegx_pipeline pipeline = instr->pipe;
957 const struct tilegx_opcode *opcode = instr->opcode;
959 bits |= emit_tilegx_instruction (opcode->fixed_bit_values[pipeline],
960 opcode->num_operands,
961 &opcode->operands[pipeline][0],
962 instr->operand_values,
966 number_to_chars_littleendian (f, bits, 8);
967 current_bundle_index = 0;
969 /* Emit DWARF2 debugging information. */
970 dwarf2_emit_insn (TILEGX_BUNDLE_SIZE_IN_BYTES);
974 /* Extend the expression parser to handle hw0(label), etc.
975 as well as SPR names when in the context of parsing an SPR. */
978 tilegx_parse_name (char *name, expressionS *e, char *nextcharP)
980 operatorT op = O_illegal;
982 if (parsing_spr)
984 void* val = hash_find (spr_hash, name);
985 if (val == NULL)
986 return 0;
988 memset (e, 0, sizeof *e);
989 e->X_op = O_constant;
990 e->X_add_number = ((const struct tilegx_spr *)val)->number;
991 return 1;
994 if (*nextcharP != '(')
996 /* hw0, etc. not followed by a paren is just a label with that name. */
997 return 0;
999 else
1001 /* Look up the operator in our table. */
1002 void* val = hash_find (special_operator_hash, name);
1003 if (val == 0)
1004 return 0;
1005 op = (operatorT)(long)val;
1008 /* Restore old '(' and skip it. */
1009 *input_line_pointer = '(';
1010 ++input_line_pointer;
1012 expression (e);
1014 if (*input_line_pointer != ')')
1016 as_bad (_("Missing ')'"));
1017 *nextcharP = *input_line_pointer;
1018 return 0;
1020 /* Skip ')'. */
1021 ++input_line_pointer;
1023 if (e->X_op == O_register || e->X_op == O_absent)
1025 as_bad (_("Invalid expression."));
1026 e->X_op = O_constant;
1027 e->X_add_number = 0;
1029 else
1031 /* Wrap subexpression with a unary operator. */
1032 symbolS *sym = make_expr_symbol (e);
1034 if (sym != e->X_add_symbol)
1036 /* HACK: mark this symbol as a temporary wrapper around a proper
1037 expression, so we can unwrap it later once we have communicated
1038 the relocation type. */
1039 sym->sy_value.X_md = 1;
1042 memset (e, 0, sizeof *e);
1043 e->X_op = op;
1044 e->X_add_symbol = sym;
1045 e->X_add_number = 0;
1048 *nextcharP = *input_line_pointer;
1049 return 1;
1053 /* Parses an expression which must be a register name. */
1055 static void
1056 parse_reg_expression (expressionS* expression)
1058 /* Zero everything to make sure we don't miss any flags. */
1059 memset (expression, 0, sizeof *expression);
1061 char* regname = input_line_pointer;
1062 char terminating_char = get_symbol_end ();
1064 void* pval = hash_find (main_reg_hash, regname);
1066 if (pval == NULL)
1068 as_bad (_("Expected register, got '%s'."), regname);
1071 int regno_and_flags = (int)(size_t)pval;
1072 int regno = EXTRACT_REGNO(regno_and_flags);
1074 if ((regno_and_flags & NONCANONICAL_REG_NAME_FLAG)
1075 && require_canonical_reg_names)
1077 as_warn (_("Found use of non-canonical register name %s; "
1078 "use %s instead."),
1079 regname,
1080 tilegx_register_names[regno]);
1083 /* Restore the old character following the register name. */
1084 *input_line_pointer = terminating_char;
1086 /* Fill in the expression fields to indicate it's a register. */
1087 expression->X_op = O_register;
1088 expression->X_add_number = regno;
1092 /* Parses and type-checks comma-separated operands in input_line_pointer. */
1094 static void
1095 parse_operands (const char *opcode_name,
1096 const unsigned char *operands,
1097 int num_operands,
1098 expressionS *operand_values)
1100 int i;
1102 memset (operand_values, 0, num_operands * sizeof operand_values[0]);
1104 SKIP_WHITESPACE ();
1105 for (i = 0; i < num_operands; i++)
1107 tilegx_operand_type type = tilegx_operands[operands[i]].type;
1109 SKIP_WHITESPACE ();
1111 if (type == TILEGX_OP_TYPE_REGISTER)
1113 parse_reg_expression (&operand_values[i]);
1115 else if (*input_line_pointer == '}')
1117 operand_values[i].X_op = O_absent;
1119 else if (type == TILEGX_OP_TYPE_SPR)
1121 /* Modify the expression parser to add SPRs to the namespace. */
1122 parsing_spr = 1;
1123 expression (&operand_values[i]);
1124 parsing_spr = 0;
1126 else
1128 expression (&operand_values[i]);
1131 SKIP_WHITESPACE ();
1133 if (i + 1 < num_operands)
1135 int separator = (unsigned char)*input_line_pointer++;
1137 if (is_end_of_line[separator] || (separator == '}'))
1139 as_bad (_("Too few operands to '%s'."), opcode_name);
1140 return;
1142 else if (separator != ',')
1144 as_bad (_("Unexpected character '%c' after operand %d to %s."),
1145 (char)separator, i + 1, opcode_name);
1146 return;
1150 /* Arbitrarily use the first valid pipe to get the operand type,
1151 since they are all the same. */
1152 switch (tilegx_operands[operands[i]].type)
1154 case TILEGX_OP_TYPE_REGISTER:
1155 /* Handled in parse_reg_expression already. */
1156 break;
1157 case TILEGX_OP_TYPE_SPR:
1158 /* Fall through */
1159 case TILEGX_OP_TYPE_IMMEDIATE:
1160 /* Fall through */
1161 case TILEGX_OP_TYPE_ADDRESS:
1162 if ( operand_values[i].X_op == O_register
1163 || operand_values[i].X_op == O_illegal
1164 || operand_values[i].X_op == O_absent)
1165 as_bad (_("Expected immediate expression"));
1166 break;
1167 default:
1168 abort();
1172 if (!is_end_of_line[(unsigned char)*input_line_pointer])
1174 switch (*input_line_pointer)
1176 case '}':
1177 if (!inside_bundle)
1178 as_bad (_("Found '}' when not bundling."));
1179 ++input_line_pointer;
1180 inside_bundle = 0;
1181 demand_empty_rest_of_line ();
1182 break;
1184 case ',':
1185 as_bad (_("Too many operands"));
1186 break;
1188 default:
1189 /* Use default error for unrecognized garbage. */
1190 demand_empty_rest_of_line ();
1191 break;
1197 /* This is the guts of the machine-dependent assembler. STR points to a
1198 machine dependent instruction. This function is supposed to emit the
1199 frags/bytes it assembles to. */
1201 void
1202 md_assemble (char *str)
1204 char old_char;
1205 size_t opname_len;
1206 char *old_input_line_pointer;
1207 const struct tilegx_opcode *op;
1208 int first_pipe;
1210 /* Split off the opcode and look it up. */
1211 opname_len = strcspn (str, " {}");
1212 old_char = str[opname_len];
1213 str[opname_len] = '\0';
1215 op = hash_find(op_hash, str);
1216 str[opname_len] = old_char;
1217 if (op == NULL)
1219 as_bad (_("Unknown opcode `%.*s'."), (int)opname_len, str);
1220 return;
1223 /* Prepare to parse the operands. */
1224 old_input_line_pointer = input_line_pointer;
1225 input_line_pointer = str + opname_len;
1226 SKIP_WHITESPACE ();
1228 if (current_bundle_index == TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE)
1230 as_bad (_("Too many instructions for bundle."));
1231 tilegx_flush_bundle ();
1234 /* Make sure we have room for the upcoming bundle before we
1235 create any fixups. Otherwise if we have to switch to a new
1236 frag the fixup dot_value fields will be wrong. */
1237 frag_grow (TILEGX_BUNDLE_SIZE_IN_BYTES);
1239 /* Find a valid pipe for this opcode. */
1240 for (first_pipe = 0; (op->pipes & (1 << first_pipe)) == 0; first_pipe++)
1243 /* Call the function that assembles this instruction. */
1244 current_bundle[current_bundle_index].opcode = op;
1245 parse_operands (op->name,
1246 &op->operands[first_pipe][0],
1247 op->num_operands,
1248 current_bundle[current_bundle_index].operand_values);
1249 ++current_bundle_index;
1251 /* Restore the saved value of input_line_pointer. */
1252 input_line_pointer = old_input_line_pointer;
1254 /* If we weren't inside curly braces, go ahead and emit
1255 this lone instruction as a bundle right now. */
1256 if (!inside_bundle)
1257 tilegx_flush_bundle ();
1261 static void
1262 s_require_canonical_reg_names (int require)
1264 demand_empty_rest_of_line ();
1265 require_canonical_reg_names = require;
1268 static void
1269 s_allow_suspicious_bundles (int allow)
1271 demand_empty_rest_of_line ();
1272 allow_suspicious_bundles = allow;
1275 const pseudo_typeS md_pseudo_table[] =
1277 {"align", s_align_bytes, 0}, /* Defaulting is invalid (0). */
1278 {"word", cons, 4},
1279 {"require_canonical_reg_names", s_require_canonical_reg_names, 1 },
1280 {"no_require_canonical_reg_names", s_require_canonical_reg_names, 0 },
1281 {"allow_suspicious_bundles", s_allow_suspicious_bundles, 1 },
1282 {"no_allow_suspicious_bundles", s_allow_suspicious_bundles, 0 },
1283 { NULL, 0, 0 }
1286 /* Equal to MAX_PRECISION in atof-ieee.c */
1287 #define MAX_LITTLENUMS 6
1289 void
1290 md_number_to_chars (char * buf, valueT val, int n)
1292 if (target_big_endian)
1293 number_to_chars_bigendian (buf, val, n);
1294 else
1295 number_to_chars_littleendian (buf, val, n);
1298 /* Turn the string pointed to by litP into a floating point constant
1299 of type TYPE, and emit the appropriate bytes. The number of
1300 LITTLENUMS emitted is stored in *SIZEP. An error message is
1301 returned, or NULL on OK. */
1303 char *
1304 md_atof (int type, char *litP, int *sizeP)
1306 int prec;
1307 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1308 LITTLENUM_TYPE *wordP;
1309 char *t;
1311 switch (type)
1313 case 'f':
1314 case 'F':
1315 prec = 2;
1316 break;
1318 case 'd':
1319 case 'D':
1320 prec = 4;
1321 break;
1323 default:
1324 *sizeP = 0;
1325 return _("Bad call to md_atof ()");
1327 t = atof_ieee (input_line_pointer, type, words);
1328 if (t)
1329 input_line_pointer = t;
1331 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1332 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
1333 the bigendian 386. */
1334 for (wordP = words + prec - 1; prec--;)
1336 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
1337 litP += sizeof (LITTLENUM_TYPE);
1339 return 0;
1343 /* We have no need to default values of symbols. */
1345 symbolS *
1346 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1348 return NULL;
1352 void
1353 tilegx_cons_fix_new (fragS *frag,
1354 int where,
1355 int nbytes,
1356 expressionS *exp)
1358 expressionS subexp;
1359 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
1360 int no_overflow = 0;
1361 fixS *fixP;
1363 /* See if it's one of our special functions. */
1364 switch (exp->X_op)
1366 case O_hw0:
1367 reloc = BFD_RELOC_TILEGX_HW0;
1368 no_overflow = 1;
1369 break;
1370 case O_hw1:
1371 reloc = BFD_RELOC_TILEGX_HW1;
1372 no_overflow = 1;
1373 break;
1374 case O_hw2:
1375 reloc = BFD_RELOC_TILEGX_HW2;
1376 no_overflow = 1;
1377 break;
1378 case O_hw3:
1379 reloc = BFD_RELOC_TILEGX_HW3;
1380 no_overflow = 1;
1381 break;
1382 case O_hw0_last:
1383 reloc = BFD_RELOC_TILEGX_HW0_LAST;
1384 break;
1385 case O_hw1_last:
1386 reloc = BFD_RELOC_TILEGX_HW1_LAST;
1387 break;
1388 case O_hw2_last:
1389 reloc = BFD_RELOC_TILEGX_HW2_LAST;
1390 break;
1392 default:
1393 /* Do nothing. */
1394 break;
1397 if (reloc != BFD_RELOC_NONE)
1399 if (nbytes != 2)
1401 as_bad (_("This operator only produces two byte values."));
1402 nbytes = 2;
1405 memset (&subexp, 0, sizeof subexp);
1406 subexp.X_op = O_symbol;
1407 subexp.X_add_symbol = exp->X_add_symbol;
1408 exp = &subexp;
1410 else
1412 switch (nbytes)
1414 case 1:
1415 reloc = BFD_RELOC_8;
1416 break;
1417 case 2:
1418 reloc = BFD_RELOC_16;
1419 break;
1420 case 4:
1421 reloc = BFD_RELOC_32;
1422 break;
1423 case 8:
1424 reloc = BFD_RELOC_64;
1425 break;
1426 default:
1427 as_bad (_("unsupported BFD relocation size %d"), nbytes);
1428 reloc = BFD_RELOC_64;
1429 break;
1433 fixP = fix_new_exp (frag, where, nbytes, exp, 0, reloc);
1434 fixP->tc_fix_data = NULL;
1435 fixP->fx_no_overflow |= no_overflow;
1439 void
1440 md_apply_fix (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
1442 const struct tilegx_operand *operand;
1443 valueT value = *valP;
1444 operatorT special;
1445 char *p;
1447 /* Leave these for the linker. */
1448 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1449 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1450 return;
1452 if (fixP->fx_subsy != (symbolS *) NULL)
1454 /* We can't actually support subtracting a symbol. */
1455 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
1458 /* Correct relocation types for pc-relativeness. */
1459 switch (fixP->fx_r_type)
1461 #define FIX_PCREL(rtype) \
1462 case rtype: \
1463 if (fixP->fx_pcrel) \
1464 fixP->fx_r_type = rtype##_PCREL; \
1465 break; \
1467 case rtype##_PCREL: \
1468 if (!fixP->fx_pcrel) \
1469 fixP->fx_r_type = rtype; \
1470 break
1472 FIX_PCREL (BFD_RELOC_8);
1473 FIX_PCREL (BFD_RELOC_16);
1474 FIX_PCREL (BFD_RELOC_32);
1475 FIX_PCREL (BFD_RELOC_64);
1476 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW0);
1477 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW0);
1478 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW1);
1479 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW1);
1480 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW2);
1481 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW2);
1482 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW3);
1483 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW3);
1484 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST);
1485 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST);
1486 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST);
1487 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST);
1488 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST);
1489 FIX_PCREL (BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST);
1491 #undef FIX_PCREL
1493 default:
1494 /* Do nothing */
1495 break;
1498 if (fixP->fx_addsy != NULL)
1500 #ifdef OBJ_ELF
1501 switch (fixP->fx_r_type)
1503 case BFD_RELOC_TILEGX_IMM16_X0_HW0_TLS_GD:
1504 case BFD_RELOC_TILEGX_IMM16_X1_HW0_TLS_GD:
1505 case BFD_RELOC_TILEGX_IMM16_X0_HW0_TLS_IE:
1506 case BFD_RELOC_TILEGX_IMM16_X1_HW0_TLS_IE:
1507 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
1508 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
1509 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
1510 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
1511 case BFD_RELOC_TILEGX_IMM16_X0_HW1_TLS_GD:
1512 case BFD_RELOC_TILEGX_IMM16_X1_HW1_TLS_GD:
1513 case BFD_RELOC_TILEGX_IMM16_X0_HW1_TLS_IE:
1514 case BFD_RELOC_TILEGX_IMM16_X1_HW1_TLS_IE:
1515 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
1516 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
1517 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
1518 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
1519 case BFD_RELOC_TILEGX_IMM16_X0_HW2_TLS_GD:
1520 case BFD_RELOC_TILEGX_IMM16_X1_HW2_TLS_GD:
1521 case BFD_RELOC_TILEGX_IMM16_X0_HW2_TLS_IE:
1522 case BFD_RELOC_TILEGX_IMM16_X1_HW2_TLS_IE:
1523 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_TLS_GD:
1524 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_TLS_GD:
1525 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_TLS_IE:
1526 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_TLS_IE:
1527 case BFD_RELOC_TILEGX_IMM16_X0_HW3_TLS_GD:
1528 case BFD_RELOC_TILEGX_IMM16_X1_HW3_TLS_GD:
1529 case BFD_RELOC_TILEGX_IMM16_X0_HW3_TLS_IE:
1530 case BFD_RELOC_TILEGX_IMM16_X1_HW3_TLS_IE:
1531 case BFD_RELOC_TILEGX_TLS_DTPMOD64:
1532 case BFD_RELOC_TILEGX_TLS_DTPOFF64:
1533 case BFD_RELOC_TILEGX_TLS_TPOFF64:
1534 case BFD_RELOC_TILEGX_TLS_DTPMOD32:
1535 case BFD_RELOC_TILEGX_TLS_DTPOFF32:
1536 case BFD_RELOC_TILEGX_TLS_TPOFF32:
1537 S_SET_THREAD_LOCAL (fixP->fx_addsy);
1538 break;
1540 default:
1541 /* Do nothing */
1542 break;
1544 #endif
1545 return;
1548 /* Apply hw0, etc. */
1549 special = O_illegal;
1550 switch (fixP->fx_r_type)
1552 case BFD_RELOC_TILEGX_HW0:
1553 case BFD_RELOC_TILEGX_IMM16_X0_HW0:
1554 case BFD_RELOC_TILEGX_IMM16_X1_HW0:
1555 case BFD_RELOC_TILEGX_IMM16_X0_HW0_PCREL:
1556 case BFD_RELOC_TILEGX_IMM16_X1_HW0_PCREL:
1557 case BFD_RELOC_TILEGX_IMM16_X0_HW0_GOT:
1558 case BFD_RELOC_TILEGX_IMM16_X1_HW0_GOT:
1559 case BFD_RELOC_TILEGX_IMM16_X0_HW0_TLS_GD:
1560 case BFD_RELOC_TILEGX_IMM16_X1_HW0_TLS_GD:
1561 case BFD_RELOC_TILEGX_IMM16_X0_HW0_TLS_IE:
1562 case BFD_RELOC_TILEGX_IMM16_X1_HW0_TLS_IE:
1563 special = O_hw0;
1564 break;
1566 case BFD_RELOC_TILEGX_HW0_LAST:
1567 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST:
1568 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST:
1569 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_PCREL:
1570 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_PCREL:
1571 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_GOT:
1572 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_GOT:
1573 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_TLS_GD:
1574 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_TLS_GD:
1575 case BFD_RELOC_TILEGX_IMM16_X0_HW0_LAST_TLS_IE:
1576 case BFD_RELOC_TILEGX_IMM16_X1_HW0_LAST_TLS_IE:
1577 special = O_hw0_last;
1578 break;
1580 case BFD_RELOC_TILEGX_HW1:
1581 case BFD_RELOC_TILEGX_IMM16_X0_HW1:
1582 case BFD_RELOC_TILEGX_IMM16_X1_HW1:
1583 case BFD_RELOC_TILEGX_IMM16_X0_HW1_PCREL:
1584 case BFD_RELOC_TILEGX_IMM16_X1_HW1_PCREL:
1585 case BFD_RELOC_TILEGX_IMM16_X0_HW1_GOT:
1586 case BFD_RELOC_TILEGX_IMM16_X1_HW1_GOT:
1587 case BFD_RELOC_TILEGX_IMM16_X0_HW1_TLS_GD:
1588 case BFD_RELOC_TILEGX_IMM16_X1_HW1_TLS_GD:
1589 case BFD_RELOC_TILEGX_IMM16_X0_HW1_TLS_IE:
1590 case BFD_RELOC_TILEGX_IMM16_X1_HW1_TLS_IE:
1591 special = O_hw1;
1592 break;
1594 case BFD_RELOC_TILEGX_HW1_LAST:
1595 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST:
1596 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST:
1597 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_PCREL:
1598 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_PCREL:
1599 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_GOT:
1600 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_GOT:
1601 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_TLS_GD:
1602 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_TLS_GD:
1603 case BFD_RELOC_TILEGX_IMM16_X0_HW1_LAST_TLS_IE:
1604 case BFD_RELOC_TILEGX_IMM16_X1_HW1_LAST_TLS_IE:
1605 special = O_hw1_last;
1606 break;
1608 case BFD_RELOC_TILEGX_HW2:
1609 case BFD_RELOC_TILEGX_IMM16_X0_HW2:
1610 case BFD_RELOC_TILEGX_IMM16_X1_HW2:
1611 case BFD_RELOC_TILEGX_IMM16_X0_HW2_PCREL:
1612 case BFD_RELOC_TILEGX_IMM16_X1_HW2_PCREL:
1613 case BFD_RELOC_TILEGX_IMM16_X0_HW2_GOT:
1614 case BFD_RELOC_TILEGX_IMM16_X1_HW2_GOT:
1615 case BFD_RELOC_TILEGX_IMM16_X0_HW2_TLS_GD:
1616 case BFD_RELOC_TILEGX_IMM16_X1_HW2_TLS_GD:
1617 case BFD_RELOC_TILEGX_IMM16_X0_HW2_TLS_IE:
1618 case BFD_RELOC_TILEGX_IMM16_X1_HW2_TLS_IE:
1619 special = O_hw2;
1620 break;
1622 case BFD_RELOC_TILEGX_HW2_LAST:
1623 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST:
1624 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST:
1625 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_PCREL:
1626 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_PCREL:
1627 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_GOT:
1628 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_GOT:
1629 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_TLS_GD:
1630 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_TLS_GD:
1631 case BFD_RELOC_TILEGX_IMM16_X0_HW2_LAST_TLS_IE:
1632 case BFD_RELOC_TILEGX_IMM16_X1_HW2_LAST_TLS_IE:
1633 special = O_hw2_last;
1634 break;
1636 case BFD_RELOC_TILEGX_HW3:
1637 case BFD_RELOC_TILEGX_IMM16_X0_HW3:
1638 case BFD_RELOC_TILEGX_IMM16_X1_HW3:
1639 case BFD_RELOC_TILEGX_IMM16_X0_HW3_PCREL:
1640 case BFD_RELOC_TILEGX_IMM16_X1_HW3_PCREL:
1641 case BFD_RELOC_TILEGX_IMM16_X0_HW3_GOT:
1642 case BFD_RELOC_TILEGX_IMM16_X1_HW3_GOT:
1643 case BFD_RELOC_TILEGX_IMM16_X0_HW3_TLS_GD:
1644 case BFD_RELOC_TILEGX_IMM16_X1_HW3_TLS_GD:
1645 case BFD_RELOC_TILEGX_IMM16_X0_HW3_TLS_IE:
1646 case BFD_RELOC_TILEGX_IMM16_X1_HW3_TLS_IE:
1647 special = O_hw3;
1648 break;
1650 default:
1651 /* Do nothing */
1652 break;
1655 if (special != O_illegal)
1657 *valP = value = apply_special_operator (special, value,
1658 fixP->fx_file, fixP->fx_line);
1661 p = fixP->fx_frag->fr_literal + fixP->fx_where;
1663 operand = fixP->tc_fix_data;
1664 if (operand != NULL)
1666 /* It's an instruction operand. */
1667 tilegx_bundle_bits bits =
1668 insert_operand (0, operand, value, fixP->fx_file, fixP->fx_line);
1670 /* Note that we might either be writing out bits for a bundle
1671 or a static network instruction, which are different sizes, so it's
1672 important to stop touching memory once we run out of bits.
1673 ORing in values is OK since we know the existing bits for
1674 this operand are zero. */
1675 for (; bits != 0; bits >>= 8)
1676 *p++ |= (char)bits;
1678 else
1680 /* Some other kind of relocation. */
1681 switch (fixP->fx_r_type)
1683 case BFD_RELOC_8:
1684 case BFD_RELOC_8_PCREL:
1685 md_number_to_chars (p, value, 1);
1686 break;
1688 case BFD_RELOC_16:
1689 case BFD_RELOC_16_PCREL:
1690 md_number_to_chars (p, value, 2);
1691 break;
1693 case BFD_RELOC_32:
1694 case BFD_RELOC_32_PCREL:
1695 md_number_to_chars (p, value, 4);
1696 break;
1698 case BFD_RELOC_64:
1699 case BFD_RELOC_64_PCREL:
1700 md_number_to_chars (p, value, 8);
1701 break;
1703 default:
1704 /* Leave it for the linker. */
1705 return;
1709 fixP->fx_done = 1;
1713 /* Generate the BFD reloc to be stuck in the object file from the
1714 fixup used internally in the assembler. */
1716 arelent *
1717 tc_gen_reloc (asection *sec ATTRIBUTE_UNUSED, fixS *fixp)
1719 arelent *reloc;
1721 reloc = (arelent *) xmalloc (sizeof (arelent));
1722 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1723 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1724 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1726 /* Make sure none of our internal relocations make it this far.
1727 They'd better have been fully resolved by this point. */
1728 gas_assert ((int) fixp->fx_r_type > 0);
1730 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1731 if (reloc->howto == NULL)
1733 as_bad_where (fixp->fx_file, fixp->fx_line,
1734 _("cannot represent `%s' relocation in object file"),
1735 bfd_get_reloc_code_name (fixp->fx_r_type));
1736 return NULL;
1739 if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
1741 as_fatal (_("internal error? cannot generate `%s' relocation (%d, %d)"),
1742 bfd_get_reloc_code_name (fixp->fx_r_type),
1743 fixp->fx_pcrel, reloc->howto->pc_relative);
1745 gas_assert (!fixp->fx_pcrel == !reloc->howto->pc_relative);
1747 reloc->addend = fixp->fx_offset;
1749 return reloc;
1753 /* The location from which a PC relative jump should be calculated,
1754 given a PC relative reloc. */
1756 long
1757 md_pcrel_from (fixS *fixP)
1759 return fixP->fx_frag->fr_address + fixP->fx_where;
1763 /* Return 1 if it's OK to adjust a reloc by replacing the symbol with
1764 a section symbol plus some offset. */
1766 tilegx_fix_adjustable (fixS *fix)
1768 /* Prevent all adjustments to global symbols */
1769 if (S_IS_EXTERNAL (fix->fx_addsy) || S_IS_WEAK (fix->fx_addsy))
1770 return 0;
1772 return 1;
1777 tilegx_unrecognized_line (int ch)
1779 switch (ch)
1781 case '{':
1782 if (inside_bundle)
1784 as_bad (_("Found '{' when already bundling."));
1786 else
1788 inside_bundle = 1;
1789 current_bundle_index = 0;
1791 return 1;
1793 case '}':
1794 if (!inside_bundle)
1796 as_bad (_("Found '}' when not bundling."));
1798 else
1800 tilegx_flush_bundle ();
1803 /* Allow '{' to follow on the same line. We also allow ";;", but that
1804 happens automatically because ';' is an end of line marker. */
1805 SKIP_WHITESPACE ();
1806 if (input_line_pointer[0] == '{')
1808 input_line_pointer++;
1809 return tilegx_unrecognized_line ('{');
1812 demand_empty_rest_of_line ();
1813 return 1;
1815 default:
1816 break;
1819 /* Not a valid line. */
1820 return 0;
1824 /* This is called from HANDLE_ALIGN in write.c. Fill in the contents
1825 of an rs_align_code fragment. */
1827 void
1828 tilegx_handle_align (fragS *fragp)
1830 addressT bytes, fix;
1831 char *p;
1833 if (fragp->fr_type != rs_align_code)
1834 return;
1836 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
1837 p = fragp->fr_literal + fragp->fr_fix;
1838 fix = 0;
1840 /* Determine the bits for NOP. */
1841 const struct tilegx_opcode *nop_opcode =
1842 &tilegx_opcodes[TILEGX_OPC_NOP];
1843 tilegx_bundle_bits nop =
1844 ( nop_opcode->fixed_bit_values[TILEGX_PIPELINE_X0]
1845 | nop_opcode->fixed_bit_values[TILEGX_PIPELINE_X1]);
1847 if ((bytes & (TILEGX_BUNDLE_SIZE_IN_BYTES - 1)) != 0)
1849 fix = bytes & (TILEGX_BUNDLE_SIZE_IN_BYTES - 1);
1850 memset (p, 0, fix);
1851 p += fix;
1852 bytes -= fix;
1855 number_to_chars_littleendian (p, nop, 8);
1856 fragp->fr_fix += fix;
1857 fragp->fr_var = TILEGX_BUNDLE_SIZE_IN_BYTES;
1860 /* Standard calling conventions leave the CFA at SP on entry. */
1861 void
1862 tilegx_cfi_frame_initial_instructions (void)
1864 cfi_add_CFA_def_cfa_register (54);
1868 tc_tilegx_regname_to_dw2regnum (char *regname)
1870 int i;
1871 for (i = 0; i < TILEGX_NUM_REGISTERS; i++)
1873 if (!strcmp (regname, tilegx_register_names[i]))
1874 return i;
1877 return -1;