1 /* tc-tilepro.c -- Assemble for a TILEPro chip.
2 Copyright (C) 2011-2019 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. */
24 #include "elf/tilepro.h"
25 #include "opcode/tilepro.h"
27 #include "dwarf2dbg.h"
28 #include "dw2gencfi.h"
30 #include "safe-ctype.h"
33 /* Special registers. */
43 /* Generic assembler global variables which must be defined by all
46 /* Characters which always start a comment. */
47 const char comment_chars[] = "#";
49 /* Characters which start a comment at the beginning of a line. */
50 const char line_comment_chars[] = "#";
52 /* Characters which may be used to separate multiple commands on a
54 const char line_separator_chars[] = ";";
56 /* Characters which are used to indicate an exponent in a floating
58 const char EXP_CHARS[] = "eE";
60 /* Characters which mean that a number is a floating point constant,
62 const char FLT_CHARS[] = "rRsSfFdDxXpP";
64 const char *md_shortopts = "VQ:";
66 struct option md_longopts[] =
68 {NULL, no_argument, NULL, 0}
71 size_t md_longopts_size = sizeof (md_longopts);
74 md_parse_option (int c, const char *arg ATTRIBUTE_UNUSED)
78 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
79 should be emitted or not. FIXME: Not implemented. */
83 /* -V: SVR4 argument to print version ID. */
96 md_show_usage (FILE *stream)
100 -V print assembler version number\n"));
103 /* Extra expression types. */
109 #define O_got_lo16 O_md5
110 #define O_got_hi16 O_md6
111 #define O_got_ha16 O_md7
113 #define O_tls_gd O_md9
114 #define O_tls_gd_lo16 O_md10
115 #define O_tls_gd_hi16 O_md11
116 #define O_tls_gd_ha16 O_md12
117 #define O_tls_ie O_md13
118 #define O_tls_ie_lo16 O_md14
119 #define O_tls_ie_hi16 O_md15
120 #define O_tls_ie_ha16 O_md16
121 #define O_tls_le O_md17
122 #define O_tls_le_lo16 O_md18
123 #define O_tls_le_hi16 O_md19
124 #define O_tls_le_ha16 O_md20
125 #define O_tls_gd_call O_md21
126 #define O_tls_gd_add O_md22
127 #define O_tls_ie_load O_md23
129 static struct hash_control *special_operator_hash;
131 /* Hash tables for instruction mnemonic lookup. */
132 static struct hash_control *op_hash;
134 /* Hash table for spr lookup. */
135 static struct hash_control *spr_hash;
137 /* True temporarily while parsing an SPR expression. This changes the
138 * namespace to include SPR names. */
139 static int parsing_spr;
141 /* Are we currently inside `{ ... }'? */
142 static int inside_bundle;
144 struct tilepro_instruction
146 const struct tilepro_opcode *opcode;
147 tilepro_pipeline pipe;
148 expressionS operand_values[TILEPRO_MAX_OPERANDS];
151 /* This keeps track of the current bundle being built up. */
152 static struct tilepro_instruction
153 current_bundle[TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE];
155 /* Index in current_bundle for the next instruction to parse. */
156 static int current_bundle_index;
158 /* Allow 'r63' in addition to 'zero', etc. Normally we disallow this as
159 'zero' is not a real register, so using it accidentally would be a
160 nasty bug. For other registers, such as 'sp', code using multiple names
161 for the same physical register is excessively confusing.
163 The '.require_canonical_reg_names' pseudo-op turns this error on,
164 and the '.no_require_canonical_reg_names' pseudo-op turns this off.
165 By default the error is on. */
166 static int require_canonical_reg_names;
168 /* Allow bundles that do undefined or suspicious things like write
169 two different values to the same register at the same time.
171 The '.no_allow_suspicious_bundles' pseudo-op turns this error on,
172 and the '.allow_suspicious_bundles' pseudo-op turns this off. */
173 static int allow_suspicious_bundles;
176 /* A hash table of main processor registers, mapping each register name
179 Furthermore, if the register number is greater than the number
180 of registers for that processor, the user used an illegal alias
181 for that register (e.g. r63 instead of zero), so we should generate
182 a warning. The attempted register number can be found by clearing
183 NONCANONICAL_REG_NAME_FLAG. */
184 static struct hash_control *main_reg_hash;
187 /* We cannot unambiguously store a 0 in a hash table and look it up,
188 so we OR in this flag to every canonical register. */
189 #define CANONICAL_REG_NAME_FLAG 0x1000
191 /* By default we disallow register aliases like r63, but we record
192 them in the hash table in case the .no_require_canonical_reg_names
193 directive is used. Noncanonical names have this value added to them. */
194 #define NONCANONICAL_REG_NAME_FLAG 0x2000
196 /* Discards flags for register hash table entries and returns the
198 #define EXTRACT_REGNO(p) ((p) & 63)
200 /* This function is called once, at assembler startup time. It should
201 set up all the tables, etc., that the MD part of the assembler will
206 const struct tilepro_opcode *op;
209 /* Guarantee text section is aligned. */
210 bfd_set_section_alignment (stdoutput, text_section,
211 TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES);
213 require_canonical_reg_names = 1;
214 allow_suspicious_bundles = 0;
215 current_bundle_index = 0;
218 /* Initialize special operator hash table. */
219 special_operator_hash = hash_new ();
220 #define INSERT_SPECIAL_OP(name) \
221 hash_insert (special_operator_hash, #name, (void *)O_##name)
223 INSERT_SPECIAL_OP(lo16);
224 INSERT_SPECIAL_OP(hi16);
225 INSERT_SPECIAL_OP(ha16);
226 INSERT_SPECIAL_OP(got);
227 INSERT_SPECIAL_OP(got_lo16);
228 INSERT_SPECIAL_OP(got_hi16);
229 INSERT_SPECIAL_OP(got_ha16);
230 INSERT_SPECIAL_OP(plt);
231 INSERT_SPECIAL_OP(tls_gd);
232 INSERT_SPECIAL_OP(tls_gd_lo16);
233 INSERT_SPECIAL_OP(tls_gd_hi16);
234 INSERT_SPECIAL_OP(tls_gd_ha16);
235 INSERT_SPECIAL_OP(tls_ie);
236 INSERT_SPECIAL_OP(tls_ie_lo16);
237 INSERT_SPECIAL_OP(tls_ie_hi16);
238 INSERT_SPECIAL_OP(tls_ie_ha16);
239 INSERT_SPECIAL_OP(tls_le);
240 INSERT_SPECIAL_OP(tls_le_lo16);
241 INSERT_SPECIAL_OP(tls_le_hi16);
242 INSERT_SPECIAL_OP(tls_le_ha16);
243 INSERT_SPECIAL_OP(tls_gd_call);
244 INSERT_SPECIAL_OP(tls_gd_add);
245 INSERT_SPECIAL_OP(tls_ie_load);
246 #undef INSERT_SPECIAL_OP
248 /* Initialize op_hash hash table. */
249 op_hash = hash_new ();
250 for (op = &tilepro_opcodes[0]; op->name != NULL; op++)
252 const char *hash_err = hash_insert (op_hash, op->name, (void *)op);
253 if (hash_err != NULL)
255 as_fatal (_("Internal Error: Can't hash %s: %s"),
260 /* Initialize the spr hash table. */
262 spr_hash = hash_new ();
263 for (i = 0; i < tilepro_num_sprs; i++)
264 hash_insert (spr_hash, tilepro_sprs[i].name,
265 (void *) &tilepro_sprs[i]);
267 /* Set up the main_reg_hash table. We use this instead of
268 * creating a symbol in the register section to avoid ambiguities
269 * with labels that have the same names as registers. */
270 main_reg_hash = hash_new ();
271 for (i = 0; i < TILEPRO_NUM_REGISTERS; i++)
275 hash_insert (main_reg_hash, tilepro_register_names[i],
276 (void *) (long)(i | CANONICAL_REG_NAME_FLAG));
278 /* See if we should insert a noncanonical alias, like r63. */
279 sprintf (buf, "r%d", i);
280 if (strcmp (buf, tilepro_register_names[i]) != 0)
281 hash_insert (main_reg_hash, xstrdup (buf),
282 (void *) (long)(i | NONCANONICAL_REG_NAME_FLAG));
285 /* Insert obsolete backwards-compatibility register names. */
286 hash_insert (main_reg_hash, "io0",
287 (void *) (long) (TREG_IDN0 | CANONICAL_REG_NAME_FLAG));
288 hash_insert (main_reg_hash, "io1",
289 (void *) (long) (TREG_IDN1 | CANONICAL_REG_NAME_FLAG));
290 hash_insert (main_reg_hash, "us0",
291 (void *) (long) (TREG_UDN0 | CANONICAL_REG_NAME_FLAG));
292 hash_insert (main_reg_hash, "us1",
293 (void *) (long) (TREG_UDN1 | CANONICAL_REG_NAME_FLAG));
294 hash_insert (main_reg_hash, "us2",
295 (void *) (long) (TREG_UDN2 | CANONICAL_REG_NAME_FLAG));
296 hash_insert (main_reg_hash, "us3",
297 (void *) (long) (TREG_UDN3 | CANONICAL_REG_NAME_FLAG));
302 #define BUNDLE_TEMPLATE_MASK(p0, p1, p2) \
303 ((p0) | ((p1) << 8) | ((p2) << 16))
304 #define BUNDLE_TEMPLATE(p0, p1, p2) \
305 { { (p0), (p1), (p2) }, \
306 BUNDLE_TEMPLATE_MASK(1 << (p0), 1 << (p1), (1 << (p2))) \
309 #define NO_PIPELINE TILEPRO_NUM_PIPELINE_ENCODINGS
311 struct bundle_template
313 tilepro_pipeline pipe[TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE];
314 unsigned int pipe_mask;
317 static const struct bundle_template bundle_templates[] =
319 /* In Y format we must always have something in Y2, since it has
320 * no fnop, so this conveys that Y2 must always be used. */
321 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y0, TILEPRO_PIPELINE_Y2, NO_PIPELINE),
322 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y1, TILEPRO_PIPELINE_Y2, NO_PIPELINE),
323 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y0, NO_PIPELINE),
324 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y1, NO_PIPELINE),
326 /* Y format has three instructions. */
327 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y0, TILEPRO_PIPELINE_Y1, TILEPRO_PIPELINE_Y2),
328 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y0, TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y1),
329 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y1, TILEPRO_PIPELINE_Y0, TILEPRO_PIPELINE_Y2),
330 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y1, TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y0),
331 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y0, TILEPRO_PIPELINE_Y1),
332 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y1, TILEPRO_PIPELINE_Y0),
334 /* X format has only two instructions. */
335 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_X0, TILEPRO_PIPELINE_X1, NO_PIPELINE),
336 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_X1, TILEPRO_PIPELINE_X0, NO_PIPELINE)
341 prepend_nop_to_bundle (tilepro_mnemonic mnemonic)
343 memmove (¤t_bundle[1], ¤t_bundle[0],
344 current_bundle_index * sizeof current_bundle[0]);
345 current_bundle[0].opcode = &tilepro_opcodes[mnemonic];
346 ++current_bundle_index;
350 static tilepro_bundle_bits
351 insert_operand (tilepro_bundle_bits bits,
352 const struct tilepro_operand *operand,
357 /* Range-check the immediate. */
358 int num_bits = operand->num_bits;
360 operand_value >>= operand->rightshift;
362 if (bfd_check_overflow (operand->is_signed
363 ? complain_overflow_signed
364 : complain_overflow_unsigned,
367 bfd_arch_bits_per_address (stdoutput),
372 if (operand->is_signed)
374 min = -(1 << (num_bits - 1));
375 max = (1 << (num_bits - 1)) - 1;
380 max = (1 << num_bits) - 1;
382 as_bad_value_out_of_range (_("operand"), operand_value, min, max,
386 /* Write out the bits for the immediate. */
387 return bits | operand->insert (operand_value);
392 apply_special_operator (operatorT op, int num)
397 return (signed short)num;
400 return (signed short)(num >> 16);
403 return (signed short)((num + 0x8000) >> 16);
411 static tilepro_bundle_bits
412 emit_tilepro_instruction (tilepro_bundle_bits bits,
414 const unsigned char *operands,
415 expressionS *operand_values,
420 for (i = 0; i < num_operands; i++)
422 const struct tilepro_operand *operand =
423 &tilepro_operands[operands[i]];
424 expressionS *operand_exp = &operand_values[i];
425 int is_pc_relative = operand->is_pc_relative;
427 if (operand_exp->X_op == O_register
428 || (operand_exp->X_op == O_constant && !is_pc_relative))
430 /* We know what the bits are right now, so insert them. */
431 bits = insert_operand (bits, operand, operand_exp->X_add_number,
436 bfd_reloc_code_real_type reloc = operand->default_reloc;
438 int die = 0, use_subexp = 0, require_symbol = 0;
441 /* Take an expression like hi16(x) and turn it into x with
442 a different reloc type. */
443 switch (operand_exp->X_op)
445 #define HANDLE_OP16(suffix) \
448 case BFD_RELOC_TILEPRO_IMM16_X0: \
449 reloc = BFD_RELOC_TILEPRO_IMM16_X0_##suffix; \
451 case BFD_RELOC_TILEPRO_IMM16_X1: \
452 reloc = BFD_RELOC_TILEPRO_IMM16_X1_##suffix; \
478 HANDLE_OP16 (GOT_LO);
483 HANDLE_OP16 (GOT_HI);
488 HANDLE_OP16 (GOT_HA);
493 HANDLE_OP16 (TLS_GD);
498 HANDLE_OP16 (TLS_GD_LO);
503 HANDLE_OP16 (TLS_GD_HI);
508 HANDLE_OP16 (TLS_GD_HA);
513 HANDLE_OP16 (TLS_IE);
518 HANDLE_OP16 (TLS_IE_LO);
523 HANDLE_OP16 (TLS_IE_HI);
528 HANDLE_OP16 (TLS_IE_HA);
533 HANDLE_OP16 (TLS_LE);
538 HANDLE_OP16 (TLS_LE_LO);
543 HANDLE_OP16 (TLS_LE_HI);
548 HANDLE_OP16 (TLS_LE_HA);
557 case BFD_RELOC_TILEPRO_JOFFLONG_X1:
558 reloc = BFD_RELOC_TILEPRO_JOFFLONG_X1_PLT;
571 case BFD_RELOC_TILEPRO_JOFFLONG_X1:
572 reloc = BFD_RELOC_TILEPRO_TLS_GD_CALL;
585 case BFD_RELOC_TILEPRO_IMM8_X0:
586 reloc = BFD_RELOC_TILEPRO_IMM8_X0_TLS_GD_ADD;
588 case BFD_RELOC_TILEPRO_IMM8_X1:
589 reloc = BFD_RELOC_TILEPRO_IMM8_X1_TLS_GD_ADD;
591 case BFD_RELOC_TILEPRO_IMM8_Y0:
592 reloc = BFD_RELOC_TILEPRO_IMM8_Y0_TLS_GD_ADD;
594 case BFD_RELOC_TILEPRO_IMM8_Y1:
595 reloc = BFD_RELOC_TILEPRO_IMM8_Y1_TLS_GD_ADD;
608 case BFD_RELOC_TILEPRO_IMM8_X1:
609 reloc = BFD_RELOC_TILEPRO_TLS_IE_LOAD;
626 as_bad (_("Invalid operator for operand."));
630 expressionS *sval = NULL;
631 /* Now that we've changed the reloc, change ha16(x) into x,
634 if (symbol_symbolS (operand_exp->X_add_symbol))
635 sval = symbol_get_value_expression (operand_exp->X_add_symbol);
636 if (sval && sval->X_md)
638 /* HACK: We used X_md to mark this symbol as a fake wrapper
639 around a real expression. To unwrap it, we just grab its
645 /* Look at the expression, and reject it if it's not a
647 if (operand_exp->X_op != O_symbol
648 || operand_exp->X_add_number != 0)
649 as_bad (_("Operator may only be applied to symbols."));
654 /* The value of this expression is an actual symbol, so
655 turn that into an expression. */
656 memset (&subexp, 0, sizeof subexp);
657 subexp.X_op = O_symbol;
658 subexp.X_add_symbol = operand_exp->X_add_symbol;
659 operand_exp = &subexp;
663 /* Create a fixup to handle this later. */
664 fixP = fix_new_exp (frag_now,
665 bundle_start - frag_now->fr_literal,
666 (operand->num_bits + 7) >> 3,
670 fixP->tc_fix_data = operand;
672 /* Don't do overflow checking if we are applying a function like
674 fixP->fx_no_overflow |= use_subexp;
681 /* Detects and complains if two instructions in current_bundle write
682 to the same register, either implicitly or explicitly, or if a
683 read-only register is written. */
685 check_illegal_reg_writes (void)
687 BFD_HOST_U_64_BIT all_regs_written = 0;
690 for (j = 0; j < current_bundle_index; j++)
692 const struct tilepro_instruction *instr = ¤t_bundle[j];
694 BFD_HOST_U_64_BIT regs =
695 ((BFD_HOST_U_64_BIT)1) << instr->opcode->implicitly_written_register;
696 BFD_HOST_U_64_BIT conflict;
698 for (k = 0; k < instr->opcode->num_operands; k++)
700 const struct tilepro_operand *operand =
701 &tilepro_operands[instr->opcode->operands[instr->pipe][k]];
703 if (operand->is_dest_reg)
705 int regno = instr->operand_values[k].X_add_number;
706 BFD_HOST_U_64_BIT mask = ((BFD_HOST_U_64_BIT)1) << regno;
708 if ((mask & ( (((BFD_HOST_U_64_BIT)1) << TREG_IDN1)
709 | (((BFD_HOST_U_64_BIT)1) << TREG_UDN1)
710 | (((BFD_HOST_U_64_BIT)1) << TREG_UDN2)
711 | (((BFD_HOST_U_64_BIT)1) << TREG_UDN3))) != 0
712 && !allow_suspicious_bundles)
714 as_bad (_("Writes to register '%s' are not allowed."),
715 tilepro_register_names[regno]);
722 /* Writing to the zero register doesn't count. */
723 regs &= ~(((BFD_HOST_U_64_BIT)1) << TREG_ZERO);
725 conflict = all_regs_written & regs;
726 if (conflict != 0 && !allow_suspicious_bundles)
728 /* Find which register caused the conflict. */
729 const char *conflicting_reg_name = "???";
732 for (i = 0; i < TILEPRO_NUM_REGISTERS; i++)
734 if (((conflict >> i) & 1) != 0)
736 conflicting_reg_name = tilepro_register_names[i];
741 as_bad (_("Two instructions in the same bundle both write "
742 "to register %s, which is not allowed."),
743 conflicting_reg_name);
746 all_regs_written |= regs;
752 tilepro_flush_bundle (void)
756 unsigned compatible_pipes;
757 const struct bundle_template *match;
762 switch (current_bundle_index)
765 /* No instructions. */
768 if (current_bundle[0].opcode->can_bundle)
770 /* Simplify later logic by adding an explicit fnop. */
771 prepend_nop_to_bundle (TILEPRO_OPC_FNOP);
775 /* This instruction cannot be bundled with anything else.
776 Prepend an explicit 'nop', rather than an 'fnop', because
777 fnops can be replaced by later binary-processing tools
778 while nops cannot. */
779 prepend_nop_to_bundle (TILEPRO_OPC_NOP);
783 if (!allow_suspicious_bundles)
785 /* Make sure all instructions can be bundled with other
787 const struct tilepro_opcode *cannot_bundle = NULL;
788 bfd_boolean seen_non_nop = FALSE;
790 for (j = 0; j < current_bundle_index; j++)
792 const struct tilepro_opcode *op = current_bundle[j].opcode;
794 if (!op->can_bundle && cannot_bundle == NULL)
796 else if (op->mnemonic != TILEPRO_OPC_NOP
797 && op->mnemonic != TILEPRO_OPC_INFO
798 && op->mnemonic != TILEPRO_OPC_INFOL)
802 if (cannot_bundle != NULL && seen_non_nop)
804 current_bundle_index = 0;
805 as_bad (_("'%s' may not be bundled with other instructions."),
806 cannot_bundle->name);
814 BUNDLE_TEMPLATE_MASK(current_bundle[0].opcode->pipes,
815 current_bundle[1].opcode->pipes,
816 (current_bundle_index == 3
817 ? current_bundle[2].opcode->pipes
818 : (1 << NO_PIPELINE)));
820 /* Find a template that works, if any. */
822 for (i = 0; i < sizeof bundle_templates / sizeof bundle_templates[0]; i++)
824 const struct bundle_template *b = &bundle_templates[i];
825 if ((b->pipe_mask & compatible_pipes) == b->pipe_mask)
834 current_bundle_index = 0;
835 as_bad (_("Invalid combination of instructions for bundle."));
839 /* If the section seems to have no alignment set yet, go ahead and
840 make it large enough to hold code. */
841 if (bfd_get_section_alignment (stdoutput, now_seg) == 0)
842 bfd_set_section_alignment (stdoutput, now_seg,
843 TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES);
845 for (j = 0; j < current_bundle_index; j++)
846 current_bundle[j].pipe = match->pipe[j];
848 if (current_bundle_index == 2 && !tilepro_is_x_pipeline(match->pipe[0]))
850 /* We are in Y mode with only two instructions, so add an FNOP. */
851 prepend_nop_to_bundle (TILEPRO_OPC_FNOP);
853 /* Figure out what pipe the fnop must be in via arithmetic.
854 * p0 + p1 + p2 must sum to the sum of TILEPRO_PIPELINE_Y[012]. */
855 current_bundle[0].pipe =
856 (tilepro_pipeline)((TILEPRO_PIPELINE_Y0
857 + TILEPRO_PIPELINE_Y1
858 + TILEPRO_PIPELINE_Y2) -
859 (current_bundle[1].pipe + current_bundle[2].pipe));
862 check_illegal_reg_writes ();
864 f = frag_more (TILEPRO_BUNDLE_SIZE_IN_BYTES);
866 /* Check to see if this bundle is at an offset that is a multiple of 8-bytes
867 from the start of the frag. */
868 addr_mod = frag_now_fix () & (TILEPRO_BUNDLE_ALIGNMENT_IN_BYTES - 1);
869 if (frag_now->has_code && frag_now->insn_addr != addr_mod)
870 as_bad (_("instruction address is not a multiple of 8"));
871 frag_now->insn_addr = addr_mod;
872 frag_now->has_code = 1;
874 tilepro_bundle_bits bits = 0;
875 for (j = 0; j < current_bundle_index; j++)
877 struct tilepro_instruction *instr = ¤t_bundle[j];
878 tilepro_pipeline pipeline = instr->pipe;
879 const struct tilepro_opcode *opcode = instr->opcode;
881 bits |= emit_tilepro_instruction (opcode->fixed_bit_values[pipeline],
882 opcode->num_operands,
883 &opcode->operands[pipeline][0],
884 instr->operand_values,
888 number_to_chars_littleendian (f, (unsigned int)bits, 4);
889 number_to_chars_littleendian (f + 4, (unsigned int)(bits >> 32), 4);
890 current_bundle_index = 0;
892 /* Emit DWARF2 debugging information. */
893 dwarf2_emit_insn (TILEPRO_BUNDLE_SIZE_IN_BYTES);
897 /* Extend the expression parser to handle hi16(label), etc.
898 as well as SPR names when in the context of parsing an SPR. */
900 tilepro_parse_name (char *name, expressionS *e, char *nextcharP)
902 operatorT op = O_illegal;
906 void *val = hash_find (spr_hash, name);
910 memset (e, 0, sizeof *e);
911 e->X_op = O_constant;
912 e->X_add_number = ((const struct tilepro_spr *)val)->number;
916 if (*nextcharP != '(')
918 /* hi16, etc. not followed by a paren is just a label with that
924 /* Look up the operator in our table. */
925 void *val = hash_find (special_operator_hash, name);
928 op = (operatorT)(long)val;
931 /* Restore old '(' and skip it. */
932 *input_line_pointer = '(';
933 ++input_line_pointer;
937 if (*input_line_pointer != ')')
939 as_bad (_("Missing ')'"));
940 *nextcharP = *input_line_pointer;
944 ++input_line_pointer;
946 if (e->X_op == O_register || e->X_op == O_absent)
948 as_bad (_("Invalid expression."));
949 e->X_op = O_constant;
954 /* Wrap subexpression with a unary operator. */
955 symbolS *sym = make_expr_symbol (e);
957 if (sym != e->X_add_symbol)
959 /* HACK: mark this symbol as a temporary wrapper around a proper
960 expression, so we can unwrap it later once we have communicated
961 the relocation type. */
962 symbol_get_value_expression (sym)->X_md = 1;
965 memset (e, 0, sizeof *e);
967 e->X_add_symbol = sym;
971 *nextcharP = *input_line_pointer;
976 /* Parses an expression which must be a register name. */
979 parse_reg_expression (expressionS* expression)
981 /* Zero everything to make sure we don't miss any flags. */
982 memset (expression, 0, sizeof *expression);
985 char terminating_char = get_symbol_name (®name);
987 void* pval = hash_find (main_reg_hash, regname);
990 as_bad (_("Expected register, got '%s'."), regname);
992 int regno_and_flags = (int)(size_t)pval;
993 int regno = EXTRACT_REGNO(regno_and_flags);
995 if ((regno_and_flags & NONCANONICAL_REG_NAME_FLAG)
996 && require_canonical_reg_names)
997 as_warn (_("Found use of non-canonical register name %s; "
999 regname, tilepro_register_names[regno]);
1001 /* Restore the old character following the register name. */
1002 (void) restore_line_pointer (terminating_char);
1004 /* Fill in the expression fields to indicate it's a register. */
1005 expression->X_op = O_register;
1006 expression->X_add_number = regno;
1010 /* Parses and type-checks comma-separated operands in input_line_pointer. */
1012 parse_operands (const char *opcode_name,
1013 const unsigned char *operands,
1015 expressionS *operand_values)
1019 memset (operand_values, 0, num_operands * sizeof operand_values[0]);
1022 for (i = 0; i < num_operands; i++)
1024 tilepro_operand_type type = tilepro_operands[operands[i]].type;
1028 if (type == TILEPRO_OP_TYPE_REGISTER)
1030 parse_reg_expression (&operand_values[i]);
1032 else if (*input_line_pointer == '}')
1034 operand_values[i].X_op = O_absent;
1036 else if (type == TILEPRO_OP_TYPE_SPR)
1038 /* Modify the expression parser to add SPRs to the namespace. */
1040 expression (&operand_values[i]);
1045 expression (&operand_values[i]);
1050 if (i + 1 < num_operands)
1052 int separator = (unsigned char)*input_line_pointer++;
1054 if (is_end_of_line[separator] || (separator == '}'))
1056 as_bad (_("Too few operands to '%s'."), opcode_name);
1059 else if (separator != ',')
1061 as_bad (_("Unexpected character '%c' after operand %d to %s."),
1062 (char)separator, i + 1, opcode_name);
1067 /* Arbitrarily use the first valid pipe to get the operand type,
1068 since they are all the same. */
1069 switch (tilepro_operands[operands[i]].type)
1071 case TILEPRO_OP_TYPE_REGISTER:
1072 /* Handled in parse_reg_expression already. */
1074 case TILEPRO_OP_TYPE_SPR:
1076 case TILEPRO_OP_TYPE_IMMEDIATE:
1078 case TILEPRO_OP_TYPE_ADDRESS:
1079 if ( operand_values[i].X_op == O_register
1080 || operand_values[i].X_op == O_illegal
1081 || operand_values[i].X_op == O_absent)
1082 as_bad (_("Expected immediate expression"));
1089 if (!is_end_of_line[(unsigned char)*input_line_pointer])
1091 switch (*input_line_pointer)
1095 as_bad (_("Found '}' when not bundling."));
1096 ++input_line_pointer;
1098 demand_empty_rest_of_line ();
1102 as_bad (_("Too many operands"));
1106 /* Use default error for unrecognized garbage. */
1107 demand_empty_rest_of_line ();
1114 /* This is the guts of the machine-dependent assembler. STR points to a
1115 machine dependent instruction. This function is supposed to emit
1116 the frags/bytes it assembles to. */
1118 md_assemble (char *str)
1122 char *old_input_line_pointer;
1123 const struct tilepro_opcode *op;
1126 /* Split off the opcode and look it up. */
1127 opname_len = strcspn (str, " {}");
1128 old_char = str[opname_len];
1129 str[opname_len] = '\0';
1131 op = hash_find(op_hash, str);
1132 str[opname_len] = old_char;
1135 as_bad (_("Unknown opcode `%.*s'."), (int)opname_len, str);
1139 /* Prepare to parse the operands. */
1140 old_input_line_pointer = input_line_pointer;
1141 input_line_pointer = str + opname_len;
1144 if (current_bundle_index == TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE)
1146 as_bad (_("Too many instructions for bundle."));
1147 tilepro_flush_bundle ();
1150 /* Make sure we have room for the upcoming bundle before we
1151 create any fixups. Otherwise if we have to switch to a new
1152 frag the fixup dot_value fields will be wrong. */
1153 frag_grow (TILEPRO_BUNDLE_SIZE_IN_BYTES);
1155 /* Find a valid pipe for this opcode. */
1156 for (first_pipe = 0; (op->pipes & (1 << first_pipe)) == 0; first_pipe++)
1159 /* Call the function that assembles this instruction. */
1160 current_bundle[current_bundle_index].opcode = op;
1161 parse_operands (op->name,
1162 &op->operands[first_pipe][0],
1164 current_bundle[current_bundle_index].operand_values);
1165 ++current_bundle_index;
1167 /* Restore the saved value of input_line_pointer. */
1168 input_line_pointer = old_input_line_pointer;
1170 /* If we weren't inside curly braces, go ahead and emit
1171 this lone instruction as a bundle right now. */
1173 tilepro_flush_bundle ();
1177 s_require_canonical_reg_names (int require)
1179 demand_empty_rest_of_line ();
1180 require_canonical_reg_names = require;
1184 s_allow_suspicious_bundles (int allow)
1186 demand_empty_rest_of_line ();
1187 allow_suspicious_bundles = allow;
1190 const pseudo_typeS md_pseudo_table[] =
1192 {"align", s_align_bytes, 0}, /* Defaulting is invalid (0). */
1194 {"require_canonical_reg_names", s_require_canonical_reg_names, 1 },
1195 {"no_require_canonical_reg_names", s_require_canonical_reg_names, 0 },
1196 {"allow_suspicious_bundles", s_allow_suspicious_bundles, 1 },
1197 {"no_allow_suspicious_bundles", s_allow_suspicious_bundles, 0 },
1201 /* Turn the string pointed to by litP into a floating point constant
1202 of type TYPE, and emit the appropriate bytes. The number of
1203 LITTLENUMS emitted is stored in *SIZEP. An error message is
1204 returned, or NULL on OK. */
1207 md_atof (int type, char *litP, int *sizeP)
1210 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1211 LITTLENUM_TYPE *wordP;
1228 return _("Bad call to md_atof ()");
1230 t = atof_ieee (input_line_pointer, type, words);
1232 input_line_pointer = t;
1234 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1235 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
1236 the bigendian 386. */
1237 for (wordP = words + prec - 1; prec--;)
1239 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
1240 litP += sizeof (LITTLENUM_TYPE);
1246 /* We have no need to default values of symbols. */
1249 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1256 tilepro_cons_fix_new (fragS *frag,
1262 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
1263 int no_overflow = 0;
1266 /* See if it's one of our special functions. */
1270 reloc = BFD_RELOC_LO16;
1274 reloc = BFD_RELOC_HI16;
1278 reloc = BFD_RELOC_HI16_S;
1287 if (reloc != BFD_RELOC_NONE)
1291 as_bad (_("This operator only produces two byte values."));
1295 memset (&subexp, 0, sizeof subexp);
1296 subexp.X_op = O_symbol;
1297 subexp.X_add_symbol = exp->X_add_symbol;
1305 reloc = BFD_RELOC_8;
1308 reloc = BFD_RELOC_16;
1311 reloc = BFD_RELOC_32;
1314 reloc = BFD_RELOC_64;
1317 as_bad (_("unsupported BFD relocation size %d"), nbytes);
1318 reloc = BFD_RELOC_32;
1323 fixP = fix_new_exp (frag, where, nbytes, exp, 0, reloc);
1324 fixP->tc_fix_data = NULL;
1325 fixP->fx_no_overflow |= no_overflow;
1330 md_apply_fix (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
1332 const struct tilepro_operand *operand;
1333 valueT value = *valP;
1336 /* Leave these for the linker. */
1337 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1338 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1341 if (fixP->fx_subsy != (symbolS *) NULL)
1343 /* We can't actually support subtracting a symbol. */
1344 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
1347 /* Correct relocation types for pc-relativeness. */
1348 switch (fixP->fx_r_type)
1350 #define FIX_PCREL(rtype) \
1352 if (fixP->fx_pcrel) \
1353 fixP->fx_r_type = rtype##_PCREL; \
1356 case rtype##_PCREL: \
1357 if (!fixP->fx_pcrel) \
1358 fixP->fx_r_type = rtype; \
1361 FIX_PCREL (BFD_RELOC_8);
1362 FIX_PCREL (BFD_RELOC_16);
1363 FIX_PCREL (BFD_RELOC_32);
1364 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0);
1365 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1);
1366 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_LO);
1367 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_LO);
1368 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HI);
1369 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HI);
1370 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HA);
1371 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HA);
1380 if (fixP->fx_addsy != NULL)
1383 switch (fixP->fx_r_type)
1385 case BFD_RELOC_TILEPRO_IMM8_X0_TLS_GD_ADD:
1386 case BFD_RELOC_TILEPRO_IMM8_X1_TLS_GD_ADD:
1387 case BFD_RELOC_TILEPRO_IMM8_Y0_TLS_GD_ADD:
1388 case BFD_RELOC_TILEPRO_IMM8_Y1_TLS_GD_ADD:
1389 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD:
1390 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD:
1391 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_LO:
1392 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_LO:
1393 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HI:
1394 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HI:
1395 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HA:
1396 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HA:
1397 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE:
1398 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE:
1399 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_LO:
1400 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_LO:
1401 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HI:
1402 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HI:
1403 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HA:
1404 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HA:
1405 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_LE:
1406 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_LE:
1407 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_LE_LO:
1408 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_LE_LO:
1409 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_LE_HI:
1410 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_LE_HI:
1411 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_LE_HA:
1412 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_LE_HA:
1413 case BFD_RELOC_TILEPRO_TLS_GD_CALL:
1414 case BFD_RELOC_TILEPRO_TLS_IE_LOAD:
1415 case BFD_RELOC_TILEPRO_TLS_DTPMOD32:
1416 case BFD_RELOC_TILEPRO_TLS_DTPOFF32:
1417 case BFD_RELOC_TILEPRO_TLS_TPOFF32:
1418 S_SET_THREAD_LOCAL (fixP->fx_addsy);
1429 /* Apply lo16, hi16, ha16, etc. munging. */
1430 switch (fixP->fx_r_type)
1432 case BFD_RELOC_LO16:
1433 case BFD_RELOC_TILEPRO_IMM16_X0_LO:
1434 case BFD_RELOC_TILEPRO_IMM16_X1_LO:
1435 case BFD_RELOC_TILEPRO_IMM16_X0_LO_PCREL:
1436 case BFD_RELOC_TILEPRO_IMM16_X1_LO_PCREL:
1437 *valP = value = apply_special_operator (O_lo16, value);
1440 case BFD_RELOC_HI16:
1441 case BFD_RELOC_TILEPRO_IMM16_X0_HI:
1442 case BFD_RELOC_TILEPRO_IMM16_X1_HI:
1443 case BFD_RELOC_TILEPRO_IMM16_X0_HI_PCREL:
1444 case BFD_RELOC_TILEPRO_IMM16_X1_HI_PCREL:
1445 *valP = value = apply_special_operator (O_hi16, value);
1448 case BFD_RELOC_HI16_S:
1449 case BFD_RELOC_TILEPRO_IMM16_X0_HA:
1450 case BFD_RELOC_TILEPRO_IMM16_X1_HA:
1451 case BFD_RELOC_TILEPRO_IMM16_X0_HA_PCREL:
1452 case BFD_RELOC_TILEPRO_IMM16_X1_HA_PCREL:
1453 *valP = value = apply_special_operator (O_ha16, value);
1461 p = fixP->fx_frag->fr_literal + fixP->fx_where;
1463 operand = fixP->tc_fix_data;
1464 if (operand != NULL)
1466 /* It's an instruction operand. */
1467 tilepro_bundle_bits bits =
1468 insert_operand (0, operand, value, fixP->fx_file, fixP->fx_line);
1470 /* Note that we might either be writing out bits for a bundle or a
1471 static network instruction, which are different sizes, so it's
1472 important to stop touching memory once we run out of bits. ORing in
1473 values is OK since we know the existing bits for this operand are
1475 for (; bits != 0; bits >>= 8)
1480 /* Some other kind of relocation. */
1481 switch (fixP->fx_r_type)
1484 case BFD_RELOC_8_PCREL:
1485 md_number_to_chars (p, value, 1);
1489 case BFD_RELOC_16_PCREL:
1490 md_number_to_chars (p, value, 2);
1494 case BFD_RELOC_32_PCREL:
1495 md_number_to_chars (p, value, 4);
1499 /* Leave it for the linker. */
1508 /* Generate the BFD reloc to be stuck in the object file from the
1509 fixup used internally in the assembler. */
1512 tc_gen_reloc (asection *sec ATTRIBUTE_UNUSED, fixS *fixp)
1516 reloc = XNEW (arelent);
1517 reloc->sym_ptr_ptr = XNEW (asymbol *);
1518 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1519 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1521 /* Make sure none of our internal relocations make it this far.
1522 They'd better have been fully resolved by this point. */
1523 gas_assert ((int) fixp->fx_r_type > 0);
1525 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1526 if (reloc->howto == NULL)
1528 as_bad_where (fixp->fx_file, fixp->fx_line,
1529 _("cannot represent `%s' relocation in object file"),
1530 bfd_get_reloc_code_name (fixp->fx_r_type));
1534 if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
1536 as_fatal (_("internal error? cannot generate `%s' relocation (%d, %d)"),
1537 bfd_get_reloc_code_name (fixp->fx_r_type),
1538 fixp->fx_pcrel, reloc->howto->pc_relative);
1540 gas_assert (!fixp->fx_pcrel == !reloc->howto->pc_relative);
1542 reloc->addend = fixp->fx_offset;
1548 /* The location from which a PC relative jump should be calculated,
1549 given a PC relative reloc. */
1552 md_pcrel_from (fixS *fixP)
1554 return fixP->fx_frag->fr_address + fixP->fx_where;
1558 /* Return 1 if it's OK to adjust a reloc by replacing the symbol with
1559 a section symbol plus some offset. */
1561 tilepro_fix_adjustable (fixS *fix)
1563 /* Prevent all adjustments to global symbols */
1564 if (S_IS_EXTERNAL (fix->fx_addsy) || S_IS_WEAK (fix->fx_addsy))
1572 tilepro_unrecognized_line (int ch)
1579 as_bad (_("Found '{' when already bundling."));
1584 current_bundle_index = 0;
1591 as_bad (_("Found '}' when not bundling."));
1595 tilepro_flush_bundle ();
1598 /* Allow '{' to follow on the same line. We also allow ";;", but that
1599 happens automatically because ';' is an end of line marker. */
1601 if (input_line_pointer[0] == '{')
1603 input_line_pointer++;
1604 return tilepro_unrecognized_line ('{');
1607 demand_empty_rest_of_line ();
1614 /* Not a valid line. */
1619 /* This is called from HANDLE_ALIGN in write.c. Fill in the contents
1620 of an rs_align_code fragment. */
1623 tilepro_handle_align (fragS *fragp)
1628 if (fragp->fr_type != rs_align_code)
1631 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
1632 p = fragp->fr_literal + fragp->fr_fix;
1635 /* Determine the bits for NOP. */
1636 const struct tilepro_opcode *nop_opcode =
1637 &tilepro_opcodes[TILEPRO_OPC_NOP];
1638 tilepro_bundle_bits nop =
1639 ( nop_opcode->fixed_bit_values[TILEPRO_PIPELINE_X0]
1640 | nop_opcode->fixed_bit_values[TILEPRO_PIPELINE_X1]);
1642 if ((bytes & (TILEPRO_BUNDLE_SIZE_IN_BYTES - 1)) != 0)
1644 fix = bytes & (TILEPRO_BUNDLE_SIZE_IN_BYTES - 1);
1650 number_to_chars_littleendian (p, (unsigned int)nop, 4);
1651 number_to_chars_littleendian (p + 4, (unsigned int)(nop >> 32), 4);
1652 fragp->fr_fix += fix;
1653 fragp->fr_var = TILEPRO_BUNDLE_SIZE_IN_BYTES;
1656 /* Standard calling conventions leave the CFA at SP on entry. */
1658 tilepro_cfi_frame_initial_instructions (void)
1660 cfi_add_CFA_def_cfa_register (54);
1664 tc_tilepro_regname_to_dw2regnum (char *regname)
1668 for (i = 0; i < TILEPRO_NUM_REGISTERS; i++)
1670 if (!strcmp (regname, tilepro_register_names[i]))