1 /* tc-tilepro.c -- Assemble for a TILEPro 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. */
22 #include "struc-symbol.h"
25 #include "elf/tilepro.h"
26 #include "opcode/tilepro.h"
28 #include "dwarf2dbg.h"
29 #include "dw2gencfi.h"
31 #include "safe-ctype.h"
34 /* Special registers. */
44 /* Generic assembler global variables which must be defined by all
47 /* Characters which always start a comment. */
48 const char comment_chars
[] = "#";
50 /* Characters which start a comment at the beginning of a line. */
51 const char line_comment_chars
[] = "#";
53 /* Characters which may be used to separate multiple commands on a
55 const char line_separator_chars
[] = ";";
57 /* Characters which are used to indicate an exponent in a floating
59 const char EXP_CHARS
[] = "eE";
61 /* Characters which mean that a number is a floating point constant,
63 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
65 const char *md_shortopts
= "VQ:";
67 struct option md_longopts
[] =
69 {NULL
, no_argument
, NULL
, 0}
72 size_t md_longopts_size
= sizeof (md_longopts
);
75 md_parse_option (int c
, char *arg ATTRIBUTE_UNUSED
)
79 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
80 should be emitted or not. FIXME: Not implemented. */
84 /* -V: SVR4 argument to print version ID. */
97 md_show_usage (FILE *stream
)
101 -V print assembler version number\n"));
104 /* Extra expression types. */
110 #define O_got_lo16 O_md5
111 #define O_got_hi16 O_md6
112 #define O_got_ha16 O_md7
114 #define O_tls_gd O_md9
115 #define O_tls_gd_lo16 O_md10
116 #define O_tls_gd_hi16 O_md11
117 #define O_tls_gd_ha16 O_md12
118 #define O_tls_ie O_md13
119 #define O_tls_ie_lo16 O_md14
120 #define O_tls_ie_hi16 O_md15
121 #define O_tls_ie_ha16 O_md16
123 static struct hash_control
*special_operator_hash
;
125 /* Hash tables for instruction mnemonic lookup. */
126 static struct hash_control
*op_hash
;
128 /* Hash table for spr lookup. */
129 static struct hash_control
*spr_hash
;
131 /* True temporarily while parsing an SPR expression. This changes the
132 * namespace to include SPR names. */
133 static int parsing_spr
;
135 /* Are we currently inside `{ ... }'? */
136 static int inside_bundle
;
138 struct tilepro_instruction
140 const struct tilepro_opcode
*opcode
;
141 tilepro_pipeline pipe
;
142 expressionS operand_values
[TILEPRO_MAX_OPERANDS
];
145 /* This keeps track of the current bundle being built up. */
146 static struct tilepro_instruction
147 current_bundle
[TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE
];
149 /* Index in current_bundle for the next instruction to parse. */
150 static int current_bundle_index
;
152 /* Allow 'r63' in addition to 'zero', etc. Normally we disallow this as
153 'zero' is not a real register, so using it accidentally would be a
154 nasty bug. For other registers, such as 'sp', code using multiple names
155 for the same physical register is excessively confusing.
157 The '.require_canonical_reg_names' pseudo-op turns this error on,
158 and the '.no_require_canonical_reg_names' pseudo-op turns this off.
159 By default the error is on. */
160 static int require_canonical_reg_names
;
162 /* Allow bundles that do undefined or suspicious things like write
163 two different values to the same register at the same time.
165 The '.no_allow_suspicious_bundles' pseudo-op turns this error on,
166 and the '.allow_suspicious_bundles' pseudo-op turns this off. */
167 static int allow_suspicious_bundles
;
170 /* A hash table of main processor registers, mapping each register name
173 Furthermore, if the register number is greater than the number
174 of registers for that processor, the user used an illegal alias
175 for that register (e.g. r63 instead of zero), so we should generate
176 a warning. The attempted register number can be found by clearing
177 NONCANONICAL_REG_NAME_FLAG. */
178 static struct hash_control
*main_reg_hash
;
181 /* We cannot unambiguously store a 0 in a hash table and look it up,
182 so we OR in this flag to every canonical register. */
183 #define CANONICAL_REG_NAME_FLAG 0x1000
185 /* By default we disallow register aliases like r63, but we record
186 them in the hash table in case the .no_require_canonical_reg_names
187 directive is used. Noncanonical names have this value added to them. */
188 #define NONCANONICAL_REG_NAME_FLAG 0x2000
190 /* Discards flags for register hash table entries and returns the
192 #define EXTRACT_REGNO(p) ((p) & 63)
194 /* This function is called once, at assembler startup time. It should
195 set up all the tables, etc., that the MD part of the assembler will
200 const struct tilepro_opcode
*op
;
203 /* Guarantee text section is aligned. */
204 bfd_set_section_alignment (stdoutput
, text_section
,
205 TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES
);
207 require_canonical_reg_names
= 1;
208 allow_suspicious_bundles
= 0;
209 current_bundle_index
= 0;
212 /* Initialize special operator hash table. */
213 special_operator_hash
= hash_new ();
214 #define INSERT_SPECIAL_OP(name) \
215 hash_insert (special_operator_hash, #name, (void *)O_##name)
217 INSERT_SPECIAL_OP(lo16
);
218 INSERT_SPECIAL_OP(hi16
);
219 INSERT_SPECIAL_OP(ha16
);
220 INSERT_SPECIAL_OP(got
);
221 INSERT_SPECIAL_OP(got_lo16
);
222 INSERT_SPECIAL_OP(got_hi16
);
223 INSERT_SPECIAL_OP(got_ha16
);
224 INSERT_SPECIAL_OP(plt
);
225 INSERT_SPECIAL_OP(tls_gd
);
226 INSERT_SPECIAL_OP(tls_gd_lo16
);
227 INSERT_SPECIAL_OP(tls_gd_hi16
);
228 INSERT_SPECIAL_OP(tls_gd_ha16
);
229 INSERT_SPECIAL_OP(tls_ie
);
230 INSERT_SPECIAL_OP(tls_ie_lo16
);
231 INSERT_SPECIAL_OP(tls_ie_hi16
);
232 INSERT_SPECIAL_OP(tls_ie_ha16
);
233 #undef INSERT_SPECIAL_OP
235 /* Initialize op_hash hash table. */
236 op_hash
= hash_new ();
237 for (op
= &tilepro_opcodes
[0]; op
->name
!= NULL
; op
++)
239 const char *hash_err
= hash_insert (op_hash
, op
->name
, (void *)op
);
240 if (hash_err
!= NULL
)
242 as_fatal (_("Internal Error: Can't hash %s: %s"),
247 /* Initialize the spr hash table. */
249 spr_hash
= hash_new ();
250 for (i
= 0; i
< tilepro_num_sprs
; i
++)
251 hash_insert (spr_hash
, tilepro_sprs
[i
].name
,
252 (void *) &tilepro_sprs
[i
]);
254 /* Set up the main_reg_hash table. We use this instead of
255 * creating a symbol in the register section to avoid ambiguities
256 * with labels that have the same names as registers. */
257 main_reg_hash
= hash_new ();
258 for (i
= 0; i
< TILEPRO_NUM_REGISTERS
; i
++)
262 hash_insert (main_reg_hash
, tilepro_register_names
[i
],
263 (void *) (long)(i
| CANONICAL_REG_NAME_FLAG
));
265 /* See if we should insert a noncanonical alias, like r63. */
266 sprintf (buf
, "r%d", i
);
267 if (strcmp (buf
, tilepro_register_names
[i
]) != 0)
268 hash_insert (main_reg_hash
, xstrdup (buf
),
269 (void *) (long)(i
| NONCANONICAL_REG_NAME_FLAG
));
272 /* Insert obsolete backwards-compatibility register names. */
273 hash_insert (main_reg_hash
, "io0",
274 (void *) (long) (TREG_IDN0
| CANONICAL_REG_NAME_FLAG
));
275 hash_insert (main_reg_hash
, "io1",
276 (void *) (long) (TREG_IDN1
| CANONICAL_REG_NAME_FLAG
));
277 hash_insert (main_reg_hash
, "us0",
278 (void *) (long) (TREG_UDN0
| CANONICAL_REG_NAME_FLAG
));
279 hash_insert (main_reg_hash
, "us1",
280 (void *) (long) (TREG_UDN1
| CANONICAL_REG_NAME_FLAG
));
281 hash_insert (main_reg_hash
, "us2",
282 (void *) (long) (TREG_UDN2
| CANONICAL_REG_NAME_FLAG
));
283 hash_insert (main_reg_hash
, "us3",
284 (void *) (long) (TREG_UDN3
| CANONICAL_REG_NAME_FLAG
));
289 #define BUNDLE_TEMPLATE_MASK(p0, p1, p2) \
290 ((p0) | ((p1) << 8) | ((p2) << 16))
291 #define BUNDLE_TEMPLATE(p0, p1, p2) \
292 { { (p0), (p1), (p2) }, \
293 BUNDLE_TEMPLATE_MASK(1 << (p0), 1 << (p1), (1 << (p2))) \
296 #define NO_PIPELINE TILEPRO_NUM_PIPELINE_ENCODINGS
298 struct bundle_template
300 tilepro_pipeline pipe
[TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE
];
301 unsigned int pipe_mask
;
304 static const struct bundle_template bundle_templates
[] =
306 /* In Y format we must always have something in Y2, since it has
307 * no fnop, so this conveys that Y2 must always be used. */
308 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y0
, TILEPRO_PIPELINE_Y2
, NO_PIPELINE
),
309 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y1
, TILEPRO_PIPELINE_Y2
, NO_PIPELINE
),
310 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2
, TILEPRO_PIPELINE_Y0
, NO_PIPELINE
),
311 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2
, TILEPRO_PIPELINE_Y1
, NO_PIPELINE
),
313 /* Y format has three instructions. */
314 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y0
, TILEPRO_PIPELINE_Y1
, TILEPRO_PIPELINE_Y2
),
315 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y0
, TILEPRO_PIPELINE_Y2
, TILEPRO_PIPELINE_Y1
),
316 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y1
, TILEPRO_PIPELINE_Y0
, TILEPRO_PIPELINE_Y2
),
317 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y1
, TILEPRO_PIPELINE_Y2
, TILEPRO_PIPELINE_Y0
),
318 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2
, TILEPRO_PIPELINE_Y0
, TILEPRO_PIPELINE_Y1
),
319 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2
, TILEPRO_PIPELINE_Y1
, TILEPRO_PIPELINE_Y0
),
321 /* X format has only two instructions. */
322 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_X0
, TILEPRO_PIPELINE_X1
, NO_PIPELINE
),
323 BUNDLE_TEMPLATE(TILEPRO_PIPELINE_X1
, TILEPRO_PIPELINE_X0
, NO_PIPELINE
)
328 prepend_nop_to_bundle (tilepro_mnemonic mnemonic
)
330 memmove (¤t_bundle
[1], ¤t_bundle
[0],
331 current_bundle_index
* sizeof current_bundle
[0]);
332 current_bundle
[0].opcode
= &tilepro_opcodes
[mnemonic
];
333 ++current_bundle_index
;
337 static tilepro_bundle_bits
338 insert_operand (tilepro_bundle_bits bits
,
339 const struct tilepro_operand
*operand
,
344 /* Range-check the immediate. */
345 int num_bits
= operand
->num_bits
;
347 operand_value
>>= operand
->rightshift
;
349 if (bfd_check_overflow (operand
->is_signed
350 ? complain_overflow_signed
351 : complain_overflow_unsigned
,
354 bfd_arch_bits_per_address (stdoutput
),
359 if (operand
->is_signed
)
361 min
= -(1 << (num_bits
- 1));
362 max
= (1 << (num_bits
- 1)) - 1;
367 max
= (1 << num_bits
) - 1;
369 as_bad_value_out_of_range (_("operand"), operand_value
, min
, max
,
373 /* Write out the bits for the immediate. */
374 return bits
| operand
->insert (operand_value
);
379 apply_special_operator (operatorT op
, int num
)
390 return (signed short)num
;
396 return (signed short)(num
>> 16);
402 return (signed short)((num
+ 0x8000) >> 16);
410 static tilepro_bundle_bits
411 emit_tilepro_instruction (tilepro_bundle_bits bits
,
413 const unsigned char *operands
,
414 expressionS
*operand_values
,
419 for (i
= 0; i
< num_operands
; i
++)
421 const struct tilepro_operand
*operand
=
422 &tilepro_operands
[operands
[i
]];
423 expressionS
*operand_exp
= &operand_values
[i
];
424 int is_pc_relative
= operand
->is_pc_relative
;
426 if (operand_exp
->X_op
== O_register
427 || (operand_exp
->X_op
== O_constant
&& !is_pc_relative
))
429 /* We know what the bits are right now, so insert them. */
430 bits
= insert_operand (bits
, operand
, operand_exp
->X_add_number
,
435 bfd_reloc_code_real_type reloc
= operand
->default_reloc
;
437 int die
= 0, use_subexp
= 0, require_symbol
= 0;
440 /* Take an expression like hi16(x) and turn it into x with
441 a different reloc type. */
442 switch (operand_exp
->X_op
)
444 #define HANDLE_OP16(suffix) \
447 case BFD_RELOC_TILEPRO_IMM16_X0: \
448 reloc = BFD_RELOC_TILEPRO_IMM16_X0_##suffix; \
450 case BFD_RELOC_TILEPRO_IMM16_X1: \
451 reloc = BFD_RELOC_TILEPRO_IMM16_X1_##suffix; \
477 HANDLE_OP16 (GOT_LO
);
482 HANDLE_OP16 (GOT_HI
);
487 HANDLE_OP16 (GOT_HA
);
492 HANDLE_OP16 (TLS_GD
);
497 HANDLE_OP16 (TLS_GD_LO
);
502 HANDLE_OP16 (TLS_GD_HI
);
507 HANDLE_OP16 (TLS_GD_HA
);
512 HANDLE_OP16 (TLS_IE
);
517 HANDLE_OP16 (TLS_IE_LO
);
522 HANDLE_OP16 (TLS_IE_HI
);
527 HANDLE_OP16 (TLS_IE_HA
);
536 case BFD_RELOC_TILEPRO_JOFFLONG_X1
:
537 reloc
= BFD_RELOC_TILEPRO_JOFFLONG_X1_PLT
;
554 as_bad (_("Invalid operator for operand."));
558 /* Now that we've changed the reloc, change ha16(x) into x,
561 if (operand_exp
->X_add_symbol
->sy_value
.X_md
)
563 /* HACK: We used X_md to mark this symbol as a fake wrapper
564 around a real expression. To unwrap it, we just grab its
566 operand_exp
= &operand_exp
->X_add_symbol
->sy_value
;
570 /* Look at the expression, and reject it if it's not a
572 if (operand_exp
->X_op
!= O_symbol
573 || operand_exp
->X_add_number
!= 0)
574 as_bad (_("Operator may only be applied to symbols."));
579 /* The value of this expression is an actual symbol, so
580 turn that into an expression. */
581 memset (&subexp
, 0, sizeof subexp
);
582 subexp
.X_op
= O_symbol
;
583 subexp
.X_add_symbol
= operand_exp
->X_add_symbol
;
584 operand_exp
= &subexp
;
588 /* Create a fixup to handle this later. */
589 fixP
= fix_new_exp (frag_now
,
590 bundle_start
- frag_now
->fr_literal
,
591 (operand
->num_bits
+ 7) >> 3,
595 fixP
->tc_fix_data
= operand
;
597 /* Don't do overflow checking if we are applying a function like
599 fixP
->fx_no_overflow
|= use_subexp
;
606 /* Detects and complains if two instructions in current_bundle write
607 to the same register, either implicitly or explicitly, or if a
608 read-only register is written. */
610 check_illegal_reg_writes (void)
612 BFD_HOST_U_64_BIT all_regs_written
= 0;
615 for (j
= 0; j
< current_bundle_index
; j
++)
617 const struct tilepro_instruction
*instr
= ¤t_bundle
[j
];
619 BFD_HOST_U_64_BIT regs
=
620 ((BFD_HOST_U_64_BIT
)1) << instr
->opcode
->implicitly_written_register
;
621 BFD_HOST_U_64_BIT conflict
;
623 for (k
= 0; k
< instr
->opcode
->num_operands
; k
++)
625 const struct tilepro_operand
*operand
=
626 &tilepro_operands
[instr
->opcode
->operands
[instr
->pipe
][k
]];
628 if (operand
->is_dest_reg
)
630 int regno
= instr
->operand_values
[k
].X_add_number
;
631 BFD_HOST_U_64_BIT mask
= ((BFD_HOST_U_64_BIT
)1) << regno
;
633 if ((mask
& ( (((BFD_HOST_U_64_BIT
)1) << TREG_IDN1
)
634 | (((BFD_HOST_U_64_BIT
)1) << TREG_UDN1
)
635 | (((BFD_HOST_U_64_BIT
)1) << TREG_UDN2
)
636 | (((BFD_HOST_U_64_BIT
)1) << TREG_UDN3
))) != 0
637 && !allow_suspicious_bundles
)
639 as_bad (_("Writes to register '%s' are not allowed."),
640 tilepro_register_names
[regno
]);
647 /* Writing to the zero register doesn't count. */
648 regs
&= ~(((BFD_HOST_U_64_BIT
)1) << TREG_ZERO
);
650 conflict
= all_regs_written
& regs
;
651 if (conflict
!= 0 && !allow_suspicious_bundles
)
653 /* Find which register caused the conflict. */
654 const char *conflicting_reg_name
= "???";
657 for (i
= 0; i
< TILEPRO_NUM_REGISTERS
; i
++)
659 if (((conflict
>> i
) & 1) != 0)
661 conflicting_reg_name
= tilepro_register_names
[i
];
666 as_bad (_("Two instructions in the same bundle both write "
667 "to register %s, which is not allowed."),
668 conflicting_reg_name
);
671 all_regs_written
|= regs
;
677 tilepro_flush_bundle (void)
681 unsigned compatible_pipes
;
682 const struct bundle_template
*match
;
687 switch (current_bundle_index
)
690 /* No instructions. */
693 if (current_bundle
[0].opcode
->can_bundle
)
695 /* Simplify later logic by adding an explicit fnop. */
696 prepend_nop_to_bundle (TILEPRO_OPC_FNOP
);
700 /* This instruction cannot be bundled with anything else.
701 Prepend an explicit 'nop', rather than an 'fnop', because
702 fnops can be replaced by later binary-processing tools
703 while nops cannot. */
704 prepend_nop_to_bundle (TILEPRO_OPC_NOP
);
708 if (!allow_suspicious_bundles
)
710 /* Make sure all instructions can be bundled with other
712 const struct tilepro_opcode
*cannot_bundle
= NULL
;
713 bfd_boolean seen_non_nop
= FALSE
;
715 for (j
= 0; j
< current_bundle_index
; j
++)
717 const struct tilepro_opcode
*op
= current_bundle
[j
].opcode
;
719 if (!op
->can_bundle
&& cannot_bundle
== NULL
)
721 else if (op
->mnemonic
!= TILEPRO_OPC_NOP
722 && op
->mnemonic
!= TILEPRO_OPC_INFO
723 && op
->mnemonic
!= TILEPRO_OPC_INFOL
)
727 if (cannot_bundle
!= NULL
&& seen_non_nop
)
729 current_bundle_index
= 0;
730 as_bad (_("'%s' may not be bundled with other instructions."),
731 cannot_bundle
->name
);
739 BUNDLE_TEMPLATE_MASK(current_bundle
[0].opcode
->pipes
,
740 current_bundle
[1].opcode
->pipes
,
741 (current_bundle_index
== 3
742 ? current_bundle
[2].opcode
->pipes
743 : (1 << NO_PIPELINE
)));
745 /* Find a template that works, if any. */
747 for (i
= 0; i
< sizeof bundle_templates
/ sizeof bundle_templates
[0]; i
++)
749 const struct bundle_template
*b
= &bundle_templates
[i
];
750 if ((b
->pipe_mask
& compatible_pipes
) == b
->pipe_mask
)
759 current_bundle_index
= 0;
760 as_bad (_("Invalid combination of instructions for bundle."));
764 /* If the section seems to have no alignment set yet, go ahead and
765 make it large enough to hold code. */
766 if (bfd_get_section_alignment (stdoutput
, now_seg
) == 0)
767 bfd_set_section_alignment (stdoutput
, now_seg
,
768 TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES
);
770 for (j
= 0; j
< current_bundle_index
; j
++)
771 current_bundle
[j
].pipe
= match
->pipe
[j
];
773 if (current_bundle_index
== 2 && !tilepro_is_x_pipeline(match
->pipe
[0]))
775 /* We are in Y mode with only two instructions, so add an FNOP. */
776 prepend_nop_to_bundle (TILEPRO_OPC_FNOP
);
778 /* Figure out what pipe the fnop must be in via arithmetic.
779 * p0 + p1 + p2 must sum to the sum of TILEPRO_PIPELINE_Y[012]. */
780 current_bundle
[0].pipe
=
781 (tilepro_pipeline
)((TILEPRO_PIPELINE_Y0
782 + TILEPRO_PIPELINE_Y1
783 + TILEPRO_PIPELINE_Y2
) -
784 (current_bundle
[1].pipe
+ current_bundle
[2].pipe
));
787 check_illegal_reg_writes ();
789 f
= frag_more (TILEPRO_BUNDLE_SIZE_IN_BYTES
);
791 /* Check to see if this bundle is at an offset that is a multiple of 8-bytes
792 from the start of the frag. */
793 addr_mod
= frag_now_fix () & (TILEPRO_BUNDLE_ALIGNMENT_IN_BYTES
- 1);
794 if (frag_now
->has_code
&& frag_now
->insn_addr
!= addr_mod
)
795 as_bad (_("instruction address is not a multiple of 8"));
796 frag_now
->insn_addr
= addr_mod
;
797 frag_now
->has_code
= 1;
799 tilepro_bundle_bits bits
= 0;
800 for (j
= 0; j
< current_bundle_index
; j
++)
802 struct tilepro_instruction
*instr
= ¤t_bundle
[j
];
803 tilepro_pipeline pipeline
= instr
->pipe
;
804 const struct tilepro_opcode
*opcode
= instr
->opcode
;
806 bits
|= emit_tilepro_instruction (opcode
->fixed_bit_values
[pipeline
],
807 opcode
->num_operands
,
808 &opcode
->operands
[pipeline
][0],
809 instr
->operand_values
,
813 number_to_chars_littleendian (f
, (unsigned int)bits
, 4);
814 number_to_chars_littleendian (f
+ 4, (unsigned int)(bits
>> 32), 4);
815 current_bundle_index
= 0;
817 /* Emit DWARF2 debugging information. */
818 dwarf2_emit_insn (TILEPRO_BUNDLE_SIZE_IN_BYTES
);
822 /* Extend the expression parser to handle hi16(label), etc.
823 as well as SPR names when in the context of parsing an SPR. */
825 tilepro_parse_name (char *name
, expressionS
*e
, char *nextcharP
)
827 operatorT op
= O_illegal
;
831 void *val
= hash_find (spr_hash
, name
);
835 memset (e
, 0, sizeof *e
);
836 e
->X_op
= O_constant
;
837 e
->X_add_number
= ((const struct tilepro_spr
*)val
)->number
;
841 if (*nextcharP
!= '(')
843 /* hi16, etc. not followed by a paren is just a label with that
849 /* Look up the operator in our table. */
850 void *val
= hash_find (special_operator_hash
, name
);
853 op
= (operatorT
)(long)val
;
856 /* Restore old '(' and skip it. */
857 *input_line_pointer
= '(';
858 ++input_line_pointer
;
862 if (*input_line_pointer
!= ')')
864 as_bad (_("Missing ')'"));
865 *nextcharP
= *input_line_pointer
;
869 ++input_line_pointer
;
871 if (e
->X_op
== O_register
|| e
->X_op
== O_absent
)
873 as_bad (_("Invalid expression."));
874 e
->X_op
= O_constant
;
879 /* Wrap subexpression with a unary operator. */
880 symbolS
*sym
= make_expr_symbol (e
);
882 if (sym
!= e
->X_add_symbol
)
884 /* HACK: mark this symbol as a temporary wrapper around a proper
885 expression, so we can unwrap it later once we have communicated
886 the relocation type. */
887 sym
->sy_value
.X_md
= 1;
890 memset (e
, 0, sizeof *e
);
892 e
->X_add_symbol
= sym
;
896 *nextcharP
= *input_line_pointer
;
901 /* Parses an expression which must be a register name. */
904 parse_reg_expression (expressionS
* expression
)
906 /* Zero everything to make sure we don't miss any flags. */
907 memset (expression
, 0, sizeof *expression
);
909 char* regname
= input_line_pointer
;
910 char terminating_char
= get_symbol_end ();
912 void* pval
= hash_find (main_reg_hash
, regname
);
915 as_bad (_("Expected register, got '%s'."), regname
);
917 int regno_and_flags
= (int)(size_t)pval
;
918 int regno
= EXTRACT_REGNO(regno_and_flags
);
920 if ((regno_and_flags
& NONCANONICAL_REG_NAME_FLAG
)
921 && require_canonical_reg_names
)
922 as_warn (_("Found use of non-canonical register name %s; "
924 regname
, tilepro_register_names
[regno
]);
926 /* Restore the old character following the register name. */
927 *input_line_pointer
= terminating_char
;
929 /* Fill in the expression fields to indicate it's a register. */
930 expression
->X_op
= O_register
;
931 expression
->X_add_number
= regno
;
935 /* Parses and type-checks comma-separated operands in input_line_pointer. */
937 parse_operands (const char *opcode_name
,
938 const unsigned char *operands
,
940 expressionS
*operand_values
)
944 memset (operand_values
, 0, num_operands
* sizeof operand_values
[0]);
947 for (i
= 0; i
< num_operands
; i
++)
949 tilepro_operand_type type
= tilepro_operands
[operands
[i
]].type
;
953 if (type
== TILEPRO_OP_TYPE_REGISTER
)
955 parse_reg_expression (&operand_values
[i
]);
957 else if (*input_line_pointer
== '}')
959 operand_values
[i
].X_op
= O_absent
;
961 else if (type
== TILEPRO_OP_TYPE_SPR
)
963 /* Modify the expression parser to add SPRs to the namespace. */
965 expression (&operand_values
[i
]);
970 expression (&operand_values
[i
]);
975 if (i
+ 1 < num_operands
)
977 int separator
= (unsigned char)*input_line_pointer
++;
979 if (is_end_of_line
[separator
] || (separator
== '}'))
981 as_bad (_("Too few operands to '%s'."), opcode_name
);
984 else if (separator
!= ',')
986 as_bad (_("Unexpected character '%c' after operand %d to %s."),
987 (char)separator
, i
+ 1, opcode_name
);
992 /* Arbitrarily use the first valid pipe to get the operand type,
993 since they are all the same. */
994 switch (tilepro_operands
[operands
[i
]].type
)
996 case TILEPRO_OP_TYPE_REGISTER
:
997 /* Handled in parse_reg_expression already. */
999 case TILEPRO_OP_TYPE_SPR
:
1001 case TILEPRO_OP_TYPE_IMMEDIATE
:
1003 case TILEPRO_OP_TYPE_ADDRESS
:
1004 if ( operand_values
[i
].X_op
== O_register
1005 || operand_values
[i
].X_op
== O_illegal
1006 || operand_values
[i
].X_op
== O_absent
)
1007 as_bad (_("Expected immediate expression"));
1014 if (!is_end_of_line
[(unsigned char)*input_line_pointer
])
1016 switch (*input_line_pointer
)
1020 as_bad (_("Found '}' when not bundling."));
1021 ++input_line_pointer
;
1023 demand_empty_rest_of_line ();
1027 as_bad (_("Too many operands"));
1031 /* Use default error for unrecognized garbage. */
1032 demand_empty_rest_of_line ();
1039 /* This is the guts of the machine-dependent assembler. STR points to a
1040 machine dependent instruction. This function is supposed to emit
1041 the frags/bytes it assembles to. */
1043 md_assemble (char *str
)
1047 char *old_input_line_pointer
;
1048 const struct tilepro_opcode
*op
;
1051 /* Split off the opcode and look it up. */
1052 opname_len
= strcspn (str
, " {}");
1053 old_char
= str
[opname_len
];
1054 str
[opname_len
] = '\0';
1056 op
= hash_find(op_hash
, str
);
1057 str
[opname_len
] = old_char
;
1060 as_bad (_("Unknown opcode `%.*s'."), (int)opname_len
, str
);
1064 /* Prepare to parse the operands. */
1065 old_input_line_pointer
= input_line_pointer
;
1066 input_line_pointer
= str
+ opname_len
;
1069 if (current_bundle_index
== TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE
)
1071 as_bad (_("Too many instructions for bundle."));
1072 tilepro_flush_bundle ();
1075 /* Make sure we have room for the upcoming bundle before we
1076 create any fixups. Otherwise if we have to switch to a new
1077 frag the fixup dot_value fields will be wrong. */
1078 frag_grow (TILEPRO_BUNDLE_SIZE_IN_BYTES
);
1080 /* Find a valid pipe for this opcode. */
1081 for (first_pipe
= 0; (op
->pipes
& (1 << first_pipe
)) == 0; first_pipe
++)
1084 /* Call the function that assembles this instruction. */
1085 current_bundle
[current_bundle_index
].opcode
= op
;
1086 parse_operands (op
->name
,
1087 &op
->operands
[first_pipe
][0],
1089 current_bundle
[current_bundle_index
].operand_values
);
1090 ++current_bundle_index
;
1092 /* Restore the saved value of input_line_pointer. */
1093 input_line_pointer
= old_input_line_pointer
;
1095 /* If we weren't inside curly braces, go ahead and emit
1096 this lone instruction as a bundle right now. */
1098 tilepro_flush_bundle ();
1102 s_require_canonical_reg_names (int require
)
1104 demand_empty_rest_of_line ();
1105 require_canonical_reg_names
= require
;
1109 s_allow_suspicious_bundles (int allow
)
1111 demand_empty_rest_of_line ();
1112 allow_suspicious_bundles
= allow
;
1115 const pseudo_typeS md_pseudo_table
[] =
1117 {"align", s_align_bytes
, 0}, /* Defaulting is invalid (0). */
1119 {"require_canonical_reg_names", s_require_canonical_reg_names
, 1 },
1120 {"no_require_canonical_reg_names", s_require_canonical_reg_names
, 0 },
1121 {"allow_suspicious_bundles", s_allow_suspicious_bundles
, 1 },
1122 {"no_allow_suspicious_bundles", s_allow_suspicious_bundles
, 0 },
1126 /* Equal to MAX_PRECISION in atof-ieee.c */
1127 #define MAX_LITTLENUMS 6
1129 /* Turn the string pointed to by litP into a floating point constant
1130 of type TYPE, and emit the appropriate bytes. The number of
1131 LITTLENUMS emitted is stored in *SIZEP. An error message is
1132 returned, or NULL on OK. */
1135 md_atof (int type
, char *litP
, int *sizeP
)
1138 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1139 LITTLENUM_TYPE
*wordP
;
1156 return _("Bad call to md_atof ()");
1158 t
= atof_ieee (input_line_pointer
, type
, words
);
1160 input_line_pointer
= t
;
1162 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1163 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
1164 the bigendian 386. */
1165 for (wordP
= words
+ prec
- 1; prec
--;)
1167 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
1168 litP
+= sizeof (LITTLENUM_TYPE
);
1174 /* We have no need to default values of symbols. */
1177 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
1184 tilepro_cons_fix_new (fragS
*frag
,
1190 bfd_reloc_code_real_type reloc
= BFD_RELOC_NONE
;
1191 int no_overflow
= 0;
1194 /* See if it's one of our special functions. */
1198 reloc
= BFD_RELOC_LO16
;
1202 reloc
= BFD_RELOC_HI16
;
1206 reloc
= BFD_RELOC_HI16_S
;
1215 if (reloc
!= BFD_RELOC_NONE
)
1219 as_bad (_("This operator only produces two byte values."));
1223 memset (&subexp
, 0, sizeof subexp
);
1224 subexp
.X_op
= O_symbol
;
1225 subexp
.X_add_symbol
= exp
->X_add_symbol
;
1233 reloc
= BFD_RELOC_8
;
1236 reloc
= BFD_RELOC_16
;
1239 reloc
= BFD_RELOC_32
;
1242 reloc
= BFD_RELOC_64
;
1245 as_bad (_("unsupported BFD relocation size %d"), nbytes
);
1246 reloc
= BFD_RELOC_32
;
1251 fixP
= fix_new_exp (frag
, where
, nbytes
, exp
, 0, reloc
);
1252 fixP
->tc_fix_data
= NULL
;
1253 fixP
->fx_no_overflow
|= no_overflow
;
1258 md_apply_fix (fixS
*fixP
, valueT
* valP
, segT seg ATTRIBUTE_UNUSED
)
1260 const struct tilepro_operand
*operand
;
1261 valueT value
= *valP
;
1264 /* Leave these for the linker. */
1265 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1266 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1269 if (fixP
->fx_subsy
!= (symbolS
*) NULL
)
1271 /* We can't actually support subtracting a symbol. */
1272 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("expression too complex"));
1275 /* Correct relocation types for pc-relativeness. */
1276 switch (fixP
->fx_r_type
)
1278 #define FIX_PCREL(rtype) \
1280 if (fixP->fx_pcrel) \
1281 fixP->fx_r_type = rtype##_PCREL; \
1284 case rtype##_PCREL: \
1285 if (!fixP->fx_pcrel) \
1286 fixP->fx_r_type = rtype; \
1289 FIX_PCREL (BFD_RELOC_8
);
1290 FIX_PCREL (BFD_RELOC_16
);
1291 FIX_PCREL (BFD_RELOC_32
);
1292 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0
);
1293 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1
);
1294 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_LO
);
1295 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_LO
);
1296 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HI
);
1297 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HI
);
1298 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HA
);
1299 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HA
);
1308 if (fixP
->fx_addsy
!= NULL
)
1311 switch (fixP
->fx_r_type
)
1313 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD
:
1314 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD
:
1315 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE
:
1316 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE
:
1317 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_LO
:
1318 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_LO
:
1319 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_LO
:
1320 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_LO
:
1321 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HI
:
1322 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HI
:
1323 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HI
:
1324 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HI
:
1325 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HA
:
1326 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HA
:
1327 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HA
:
1328 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HA
:
1329 case BFD_RELOC_TILEPRO_TLS_DTPMOD32
:
1330 case BFD_RELOC_TILEPRO_TLS_DTPOFF32
:
1331 case BFD_RELOC_TILEPRO_TLS_TPOFF32
:
1332 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
1343 /* Apply lo16, hi16, ha16, etc. munging. */
1344 switch (fixP
->fx_r_type
)
1346 case BFD_RELOC_TILEPRO_IMM16_X0_GOT
:
1347 case BFD_RELOC_TILEPRO_IMM16_X1_GOT
:
1348 *valP
= value
= apply_special_operator (O_got
, value
);
1351 case BFD_RELOC_TILEPRO_IMM16_X0_GOT_LO
:
1352 case BFD_RELOC_TILEPRO_IMM16_X1_GOT_LO
:
1353 *valP
= value
= apply_special_operator (O_got_lo16
, value
);
1356 case BFD_RELOC_TILEPRO_IMM16_X0_GOT_HI
:
1357 case BFD_RELOC_TILEPRO_IMM16_X1_GOT_HI
:
1358 *valP
= value
= apply_special_operator (O_got_hi16
, value
);
1361 case BFD_RELOC_TILEPRO_IMM16_X0_GOT_HA
:
1362 case BFD_RELOC_TILEPRO_IMM16_X1_GOT_HA
:
1363 *valP
= value
= apply_special_operator (O_got_ha16
, value
);
1366 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD
:
1367 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD
:
1368 *valP
= value
= apply_special_operator (O_tls_gd
, value
);
1371 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE
:
1372 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE
:
1373 *valP
= value
= apply_special_operator (O_tls_ie
, value
);
1376 case BFD_RELOC_LO16
:
1377 case BFD_RELOC_TILEPRO_IMM16_X0_LO
:
1378 case BFD_RELOC_TILEPRO_IMM16_X1_LO
:
1379 case BFD_RELOC_TILEPRO_IMM16_X0_LO_PCREL
:
1380 case BFD_RELOC_TILEPRO_IMM16_X1_LO_PCREL
:
1381 *valP
= value
= apply_special_operator (O_lo16
, value
);
1384 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_LO
:
1385 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_LO
:
1386 *valP
= value
= apply_special_operator (O_tls_gd_lo16
, value
);
1389 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_LO
:
1390 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_LO
:
1391 *valP
= value
= apply_special_operator (O_tls_ie_lo16
, value
);
1394 case BFD_RELOC_HI16
:
1395 case BFD_RELOC_TILEPRO_IMM16_X0_HI
:
1396 case BFD_RELOC_TILEPRO_IMM16_X1_HI
:
1397 case BFD_RELOC_TILEPRO_IMM16_X0_HI_PCREL
:
1398 case BFD_RELOC_TILEPRO_IMM16_X1_HI_PCREL
:
1399 *valP
= value
= apply_special_operator (O_hi16
, value
);
1402 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HI
:
1403 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HI
:
1404 *valP
= value
= apply_special_operator (O_tls_gd_hi16
, value
);
1407 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HI
:
1408 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HI
:
1409 *valP
= value
= apply_special_operator (O_tls_ie_hi16
, value
);
1412 case BFD_RELOC_HI16_S
:
1413 case BFD_RELOC_TILEPRO_IMM16_X0_HA
:
1414 case BFD_RELOC_TILEPRO_IMM16_X1_HA
:
1415 case BFD_RELOC_TILEPRO_IMM16_X0_HA_PCREL
:
1416 case BFD_RELOC_TILEPRO_IMM16_X1_HA_PCREL
:
1417 *valP
= value
= apply_special_operator (O_ha16
, value
);
1420 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HA
:
1421 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HA
:
1422 *valP
= value
= apply_special_operator (O_tls_gd_ha16
, value
);
1425 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HA
:
1426 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HA
:
1427 *valP
= value
= apply_special_operator (O_tls_ie_ha16
, value
);
1435 p
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1437 operand
= fixP
->tc_fix_data
;
1438 if (operand
!= NULL
)
1440 /* It's an instruction operand. */
1441 tilepro_bundle_bits bits
=
1442 insert_operand (0, operand
, value
, fixP
->fx_file
, fixP
->fx_line
);
1444 /* Note that we might either be writing out bits for a bundle or a
1445 static network instruction, which are different sizes, so it's
1446 important to stop touching memory once we run out of bits. ORing in
1447 values is OK since we know the existing bits for this operand are
1449 for (; bits
!= 0; bits
>>= 8)
1454 /* Some other kind of relocation. */
1455 switch (fixP
->fx_r_type
)
1458 case BFD_RELOC_8_PCREL
:
1459 md_number_to_chars (p
, value
, 1);
1463 case BFD_RELOC_16_PCREL
:
1464 md_number_to_chars (p
, value
, 2);
1468 case BFD_RELOC_32_PCREL
:
1469 md_number_to_chars (p
, value
, 4);
1473 /* Leave it for the linker. */
1482 /* Generate the BFD reloc to be stuck in the object file from the
1483 fixup used internally in the assembler. */
1486 tc_gen_reloc (asection
*sec ATTRIBUTE_UNUSED
, fixS
*fixp
)
1490 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
1491 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
1492 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
1493 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1495 /* Make sure none of our internal relocations make it this far.
1496 They'd better have been fully resolved by this point. */
1497 gas_assert ((int) fixp
->fx_r_type
> 0);
1499 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
1500 if (reloc
->howto
== NULL
)
1502 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1503 _("cannot represent `%s' relocation in object file"),
1504 bfd_get_reloc_code_name (fixp
->fx_r_type
));
1508 if (!fixp
->fx_pcrel
!= !reloc
->howto
->pc_relative
)
1510 as_fatal (_("internal error? cannot generate `%s' relocation (%d, %d)"),
1511 bfd_get_reloc_code_name (fixp
->fx_r_type
),
1512 fixp
->fx_pcrel
, reloc
->howto
->pc_relative
);
1514 gas_assert (!fixp
->fx_pcrel
== !reloc
->howto
->pc_relative
);
1516 reloc
->addend
= fixp
->fx_offset
;
1522 /* The location from which a PC relative jump should be calculated,
1523 given a PC relative reloc. */
1526 md_pcrel_from (fixS
*fixP
)
1528 return fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
1532 /* Return 1 if it's OK to adjust a reloc by replacing the symbol with
1533 a section symbol plus some offset. */
1535 tilepro_fix_adjustable (fixS
*fix
)
1537 /* Prevent all adjustments to global symbols */
1538 if (S_IS_EXTERNAL (fix
->fx_addsy
) || S_IS_WEAK (fix
->fx_addsy
))
1546 tilepro_unrecognized_line (int ch
)
1553 as_bad (_("Found '{' when already bundling."));
1558 current_bundle_index
= 0;
1565 as_bad (_("Found '}' when not bundling."));
1569 tilepro_flush_bundle ();
1572 /* Allow '{' to follow on the same line. We also allow ";;", but that
1573 happens automatically because ';' is an end of line marker. */
1575 if (input_line_pointer
[0] == '{')
1577 input_line_pointer
++;
1578 return tilepro_unrecognized_line ('{');
1581 demand_empty_rest_of_line ();
1588 /* Not a valid line. */
1593 /* This is called from HANDLE_ALIGN in write.c. Fill in the contents
1594 of an rs_align_code fragment. */
1597 tilepro_handle_align (fragS
*fragp
)
1602 if (fragp
->fr_type
!= rs_align_code
)
1605 bytes
= fragp
->fr_next
->fr_address
- fragp
->fr_address
- fragp
->fr_fix
;
1606 p
= fragp
->fr_literal
+ fragp
->fr_fix
;
1609 /* Determine the bits for NOP. */
1610 const struct tilepro_opcode
*nop_opcode
=
1611 &tilepro_opcodes
[TILEPRO_OPC_NOP
];
1612 tilepro_bundle_bits nop
=
1613 ( nop_opcode
->fixed_bit_values
[TILEPRO_PIPELINE_X0
]
1614 | nop_opcode
->fixed_bit_values
[TILEPRO_PIPELINE_X1
]);
1616 if ((bytes
& (TILEPRO_BUNDLE_SIZE_IN_BYTES
- 1)) != 0)
1618 fix
= bytes
& (TILEPRO_BUNDLE_SIZE_IN_BYTES
- 1);
1624 number_to_chars_littleendian (p
, (unsigned int)nop
, 4);
1625 number_to_chars_littleendian (p
+ 4, (unsigned int)(nop
>> 32), 4);
1626 fragp
->fr_fix
+= fix
;
1627 fragp
->fr_var
= TILEPRO_BUNDLE_SIZE_IN_BYTES
;
1630 /* Standard calling conventions leave the CFA at SP on entry. */
1632 tilepro_cfi_frame_initial_instructions (void)
1634 cfi_add_CFA_def_cfa_register (54);
1638 tc_tilepro_regname_to_dw2regnum (char *regname
)
1642 for (i
= 0; i
< TILEPRO_NUM_REGISTERS
; i
++)
1644 if (!strcmp (regname
, tilepro_register_names
[i
]))