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
)
565 as_bad (_("Operator may only be applied to symbols."));
568 /* HACK: We used X_md to mark this symbol as a fake wrapper
569 around a real expression. To unwrap it, we just grab its
571 operand_exp
= &operand_exp
->X_add_symbol
->sy_value
;
575 /* The value of this expression is an actual symbol, so
576 turn that into an expression. */
577 memset (&subexp
, 0, sizeof subexp
);
578 subexp
.X_op
= O_symbol
;
579 subexp
.X_add_symbol
= operand_exp
->X_add_symbol
;
580 operand_exp
= &subexp
;
584 /* Create a fixup to handle this later. */
585 fixP
= fix_new_exp (frag_now
,
586 bundle_start
- frag_now
->fr_literal
,
587 (operand
->num_bits
+ 7) >> 3,
591 fixP
->tc_fix_data
= operand
;
593 /* Don't do overflow checking if we are applying a function like
595 fixP
->fx_no_overflow
|= use_subexp
;
602 /* Detects and complains if two instructions in current_bundle write
603 to the same register, either implicitly or explicitly, or if a
604 read-only register is written. */
606 check_illegal_reg_writes (void)
608 BFD_HOST_U_64_BIT all_regs_written
= 0;
611 for (j
= 0; j
< current_bundle_index
; j
++)
613 const struct tilepro_instruction
*instr
= ¤t_bundle
[j
];
615 BFD_HOST_U_64_BIT regs
=
616 ((BFD_HOST_U_64_BIT
)1) << instr
->opcode
->implicitly_written_register
;
617 BFD_HOST_U_64_BIT conflict
;
619 for (k
= 0; k
< instr
->opcode
->num_operands
; k
++)
621 const struct tilepro_operand
*operand
=
622 &tilepro_operands
[instr
->opcode
->operands
[instr
->pipe
][k
]];
624 if (operand
->is_dest_reg
)
626 int regno
= instr
->operand_values
[k
].X_add_number
;
627 BFD_HOST_U_64_BIT mask
= ((BFD_HOST_U_64_BIT
)1) << regno
;
629 if ((mask
& ( (((BFD_HOST_U_64_BIT
)1) << TREG_IDN1
)
630 | (((BFD_HOST_U_64_BIT
)1) << TREG_UDN1
)
631 | (((BFD_HOST_U_64_BIT
)1) << TREG_UDN2
)
632 | (((BFD_HOST_U_64_BIT
)1) << TREG_UDN3
))) != 0
633 && !allow_suspicious_bundles
)
635 as_bad (_("Writes to register '%s' are not allowed."),
636 tilepro_register_names
[regno
]);
643 /* Writing to the zero register doesn't count. */
644 regs
&= ~(((BFD_HOST_U_64_BIT
)1) << TREG_ZERO
);
646 conflict
= all_regs_written
& regs
;
647 if (conflict
!= 0 && !allow_suspicious_bundles
)
649 /* Find which register caused the conflict. */
650 const char *conflicting_reg_name
= "???";
653 for (i
= 0; i
< TILEPRO_NUM_REGISTERS
; i
++)
655 if (((conflict
>> i
) & 1) != 0)
657 conflicting_reg_name
= tilepro_register_names
[i
];
662 as_bad (_("Two instructions in the same bundle both write "
663 "to register %s, which is not allowed."),
664 conflicting_reg_name
);
667 all_regs_written
|= regs
;
673 tilepro_flush_bundle (void)
677 unsigned compatible_pipes
;
678 const struct bundle_template
*match
;
683 switch (current_bundle_index
)
686 /* No instructions. */
689 if (current_bundle
[0].opcode
->can_bundle
)
691 /* Simplify later logic by adding an explicit fnop. */
692 prepend_nop_to_bundle (TILEPRO_OPC_FNOP
);
696 /* This instruction cannot be bundled with anything else.
697 Prepend an explicit 'nop', rather than an 'fnop', because
698 fnops can be replaced by later binary-processing tools
699 while nops cannot. */
700 prepend_nop_to_bundle (TILEPRO_OPC_NOP
);
704 if (!allow_suspicious_bundles
)
706 /* Make sure all instructions can be bundled with other
708 const struct tilepro_opcode
*cannot_bundle
= NULL
;
709 bfd_boolean seen_non_nop
= FALSE
;
711 for (j
= 0; j
< current_bundle_index
; j
++)
713 const struct tilepro_opcode
*op
= current_bundle
[j
].opcode
;
715 if (!op
->can_bundle
&& cannot_bundle
== NULL
)
717 else if (op
->mnemonic
!= TILEPRO_OPC_NOP
718 && op
->mnemonic
!= TILEPRO_OPC_INFO
719 && op
->mnemonic
!= TILEPRO_OPC_INFOL
)
723 if (cannot_bundle
!= NULL
&& seen_non_nop
)
725 current_bundle_index
= 0;
726 as_bad (_("'%s' may not be bundled with other instructions."),
727 cannot_bundle
->name
);
735 BUNDLE_TEMPLATE_MASK(current_bundle
[0].opcode
->pipes
,
736 current_bundle
[1].opcode
->pipes
,
737 (current_bundle_index
== 3
738 ? current_bundle
[2].opcode
->pipes
739 : (1 << NO_PIPELINE
)));
741 /* Find a template that works, if any. */
743 for (i
= 0; i
< sizeof bundle_templates
/ sizeof bundle_templates
[0]; i
++)
745 const struct bundle_template
*b
= &bundle_templates
[i
];
746 if ((b
->pipe_mask
& compatible_pipes
) == b
->pipe_mask
)
755 current_bundle_index
= 0;
756 as_bad (_("Invalid combination of instructions for bundle."));
760 /* If the section seems to have no alignment set yet, go ahead and
761 make it large enough to hold code. */
762 if (bfd_get_section_alignment (stdoutput
, now_seg
) == 0)
763 bfd_set_section_alignment (stdoutput
, now_seg
,
764 TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES
);
766 for (j
= 0; j
< current_bundle_index
; j
++)
767 current_bundle
[j
].pipe
= match
->pipe
[j
];
769 if (current_bundle_index
== 2 && !tilepro_is_x_pipeline(match
->pipe
[0]))
771 /* We are in Y mode with only two instructions, so add an FNOP. */
772 prepend_nop_to_bundle (TILEPRO_OPC_FNOP
);
774 /* Figure out what pipe the fnop must be in via arithmetic.
775 * p0 + p1 + p2 must sum to the sum of TILEPRO_PIPELINE_Y[012]. */
776 current_bundle
[0].pipe
=
777 (tilepro_pipeline
)((TILEPRO_PIPELINE_Y0
778 + TILEPRO_PIPELINE_Y1
779 + TILEPRO_PIPELINE_Y2
) -
780 (current_bundle
[1].pipe
+ current_bundle
[2].pipe
));
783 check_illegal_reg_writes ();
785 f
= frag_more (TILEPRO_BUNDLE_SIZE_IN_BYTES
);
787 /* Check to see if this bundle is at an offset that is a multiple of 8-bytes
788 from the start of the frag. */
789 addr_mod
= frag_now_fix () & (TILEPRO_BUNDLE_ALIGNMENT_IN_BYTES
- 1);
790 if (frag_now
->has_code
&& frag_now
->insn_addr
!= addr_mod
)
791 as_bad (_("instruction address is not a multiple of 8"));
792 frag_now
->insn_addr
= addr_mod
;
793 frag_now
->has_code
= 1;
795 tilepro_bundle_bits bits
= 0;
796 for (j
= 0; j
< current_bundle_index
; j
++)
798 struct tilepro_instruction
*instr
= ¤t_bundle
[j
];
799 tilepro_pipeline pipeline
= instr
->pipe
;
800 const struct tilepro_opcode
*opcode
= instr
->opcode
;
802 bits
|= emit_tilepro_instruction (opcode
->fixed_bit_values
[pipeline
],
803 opcode
->num_operands
,
804 &opcode
->operands
[pipeline
][0],
805 instr
->operand_values
,
809 number_to_chars_littleendian (f
, (unsigned int)bits
, 4);
810 number_to_chars_littleendian (f
+ 4, (unsigned int)(bits
>> 32), 4);
811 current_bundle_index
= 0;
813 /* Emit DWARF2 debugging information. */
814 dwarf2_emit_insn (TILEPRO_BUNDLE_SIZE_IN_BYTES
);
818 /* Extend the expression parser to handle hi16(label), etc.
819 as well as SPR names when in the context of parsing an SPR. */
821 tilepro_parse_name (char *name
, expressionS
*e
, char *nextcharP
)
823 operatorT op
= O_illegal
;
827 void *val
= hash_find (spr_hash
, name
);
831 memset (e
, 0, sizeof *e
);
832 e
->X_op
= O_constant
;
833 e
->X_add_number
= ((const struct tilepro_spr
*)val
)->number
;
837 if (*nextcharP
!= '(')
839 /* hi16, etc. not followed by a paren is just a label with that
845 /* Look up the operator in our table. */
846 void *val
= hash_find (special_operator_hash
, name
);
849 op
= (operatorT
)(long)val
;
852 /* Restore old '(' and skip it. */
853 *input_line_pointer
= '(';
854 ++input_line_pointer
;
858 if (*input_line_pointer
!= ')')
860 as_bad (_("Missing ')'"));
861 *nextcharP
= *input_line_pointer
;
865 ++input_line_pointer
;
867 if (e
->X_op
== O_register
|| e
->X_op
== O_absent
)
869 as_bad (_("Invalid expression."));
870 e
->X_op
= O_constant
;
875 /* Wrap subexpression with a unary operator. */
876 symbolS
*sym
= make_expr_symbol (e
);
878 if (sym
!= e
->X_add_symbol
)
880 /* HACK: mark this symbol as a temporary wrapper around a proper
881 expression, so we can unwrap it later once we have communicated
882 the relocation type. */
883 sym
->sy_value
.X_md
= 1;
886 memset (e
, 0, sizeof *e
);
888 e
->X_add_symbol
= sym
;
892 *nextcharP
= *input_line_pointer
;
897 /* Parses an expression which must be a register name. */
900 parse_reg_expression (expressionS
* expression
)
902 /* Zero everything to make sure we don't miss any flags. */
903 memset (expression
, 0, sizeof *expression
);
905 char* regname
= input_line_pointer
;
906 char terminating_char
= get_symbol_end ();
908 void* pval
= hash_find (main_reg_hash
, regname
);
911 as_bad (_("Expected register, got '%s'."), regname
);
913 int regno_and_flags
= (int)(size_t)pval
;
914 int regno
= EXTRACT_REGNO(regno_and_flags
);
916 if ((regno_and_flags
& NONCANONICAL_REG_NAME_FLAG
)
917 && require_canonical_reg_names
)
918 as_warn (_("Found use of non-canonical register name %s; "
920 regname
, tilepro_register_names
[regno
]);
922 /* Restore the old character following the register name. */
923 *input_line_pointer
= terminating_char
;
925 /* Fill in the expression fields to indicate it's a register. */
926 expression
->X_op
= O_register
;
927 expression
->X_add_number
= regno
;
931 /* Parses and type-checks comma-separated operands in input_line_pointer. */
933 parse_operands (const char *opcode_name
,
934 const unsigned char *operands
,
936 expressionS
*operand_values
)
940 memset (operand_values
, 0, num_operands
* sizeof operand_values
[0]);
943 for (i
= 0; i
< num_operands
; i
++)
945 tilepro_operand_type type
= tilepro_operands
[operands
[i
]].type
;
949 if (type
== TILEPRO_OP_TYPE_REGISTER
)
951 parse_reg_expression (&operand_values
[i
]);
953 else if (*input_line_pointer
== '}')
955 operand_values
[i
].X_op
= O_absent
;
957 else if (type
== TILEPRO_OP_TYPE_SPR
)
959 /* Modify the expression parser to add SPRs to the namespace. */
961 expression (&operand_values
[i
]);
966 expression (&operand_values
[i
]);
971 if (i
+ 1 < num_operands
)
973 int separator
= (unsigned char)*input_line_pointer
++;
975 if (is_end_of_line
[separator
] || (separator
== '}'))
977 as_bad (_("Too few operands to '%s'."), opcode_name
);
980 else if (separator
!= ',')
982 as_bad (_("Unexpected character '%c' after operand %d to %s."),
983 (char)separator
, i
+ 1, opcode_name
);
988 /* Arbitrarily use the first valid pipe to get the operand type,
989 since they are all the same. */
990 switch (tilepro_operands
[operands
[i
]].type
)
992 case TILEPRO_OP_TYPE_REGISTER
:
993 /* Handled in parse_reg_expression already. */
995 case TILEPRO_OP_TYPE_SPR
:
997 case TILEPRO_OP_TYPE_IMMEDIATE
:
999 case TILEPRO_OP_TYPE_ADDRESS
:
1000 if ( operand_values
[i
].X_op
== O_register
1001 || operand_values
[i
].X_op
== O_illegal
1002 || operand_values
[i
].X_op
== O_absent
)
1003 as_bad (_("Expected immediate expression"));
1010 if (!is_end_of_line
[(unsigned char)*input_line_pointer
])
1012 switch (*input_line_pointer
)
1016 as_bad (_("Found '}' when not bundling."));
1017 ++input_line_pointer
;
1019 demand_empty_rest_of_line ();
1023 as_bad (_("Too many operands"));
1027 /* Use default error for unrecognized garbage. */
1028 demand_empty_rest_of_line ();
1035 /* This is the guts of the machine-dependent assembler. STR points to a
1036 machine dependent instruction. This function is supposed to emit
1037 the frags/bytes it assembles to. */
1039 md_assemble (char *str
)
1043 char *old_input_line_pointer
;
1044 const struct tilepro_opcode
*op
;
1047 /* Split off the opcode and look it up. */
1048 opname_len
= strcspn (str
, " {}");
1049 old_char
= str
[opname_len
];
1050 str
[opname_len
] = '\0';
1052 op
= hash_find(op_hash
, str
);
1053 str
[opname_len
] = old_char
;
1056 as_bad (_("Unknown opcode `%.*s'."), (int)opname_len
, str
);
1060 /* Prepare to parse the operands. */
1061 old_input_line_pointer
= input_line_pointer
;
1062 input_line_pointer
= str
+ opname_len
;
1065 if (current_bundle_index
== TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE
)
1067 as_bad (_("Too many instructions for bundle."));
1068 tilepro_flush_bundle ();
1071 /* Make sure we have room for the upcoming bundle before we
1072 create any fixups. Otherwise if we have to switch to a new
1073 frag the fixup dot_value fields will be wrong. */
1074 frag_grow (TILEPRO_BUNDLE_SIZE_IN_BYTES
);
1076 /* Find a valid pipe for this opcode. */
1077 for (first_pipe
= 0; (op
->pipes
& (1 << first_pipe
)) == 0; first_pipe
++)
1080 /* Call the function that assembles this instruction. */
1081 current_bundle
[current_bundle_index
].opcode
= op
;
1082 parse_operands (op
->name
,
1083 &op
->operands
[first_pipe
][0],
1085 current_bundle
[current_bundle_index
].operand_values
);
1086 ++current_bundle_index
;
1088 /* Restore the saved value of input_line_pointer. */
1089 input_line_pointer
= old_input_line_pointer
;
1091 /* If we weren't inside curly braces, go ahead and emit
1092 this lone instruction as a bundle right now. */
1094 tilepro_flush_bundle ();
1098 s_require_canonical_reg_names (int require
)
1100 demand_empty_rest_of_line ();
1101 require_canonical_reg_names
= require
;
1105 s_allow_suspicious_bundles (int allow
)
1107 demand_empty_rest_of_line ();
1108 allow_suspicious_bundles
= allow
;
1111 const pseudo_typeS md_pseudo_table
[] =
1113 {"align", s_align_bytes
, 0}, /* Defaulting is invalid (0). */
1115 {"require_canonical_reg_names", s_require_canonical_reg_names
, 1 },
1116 {"no_require_canonical_reg_names", s_require_canonical_reg_names
, 0 },
1117 {"allow_suspicious_bundles", s_allow_suspicious_bundles
, 1 },
1118 {"no_allow_suspicious_bundles", s_allow_suspicious_bundles
, 0 },
1122 /* Equal to MAX_PRECISION in atof-ieee.c */
1123 #define MAX_LITTLENUMS 6
1125 /* Turn the string pointed to by litP into a floating point constant
1126 of type TYPE, and emit the appropriate bytes. The number of
1127 LITTLENUMS emitted is stored in *SIZEP. An error message is
1128 returned, or NULL on OK. */
1131 md_atof (int type
, char *litP
, int *sizeP
)
1134 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1135 LITTLENUM_TYPE
*wordP
;
1152 return _("Bad call to md_atof ()");
1154 t
= atof_ieee (input_line_pointer
, type
, words
);
1156 input_line_pointer
= t
;
1158 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1159 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
1160 the bigendian 386. */
1161 for (wordP
= words
+ prec
- 1; prec
--;)
1163 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
1164 litP
+= sizeof (LITTLENUM_TYPE
);
1170 /* We have no need to default values of symbols. */
1173 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
1180 tilepro_cons_fix_new (fragS
*frag
,
1186 bfd_reloc_code_real_type reloc
= BFD_RELOC_NONE
;
1187 int no_overflow
= 0;
1190 /* See if it's one of our special functions. */
1194 reloc
= BFD_RELOC_LO16
;
1198 reloc
= BFD_RELOC_HI16
;
1202 reloc
= BFD_RELOC_HI16_S
;
1211 if (reloc
!= BFD_RELOC_NONE
)
1215 as_bad (_("This operator only produces two byte values."));
1219 memset (&subexp
, 0, sizeof subexp
);
1220 subexp
.X_op
= O_symbol
;
1221 subexp
.X_add_symbol
= exp
->X_add_symbol
;
1229 reloc
= BFD_RELOC_8
;
1232 reloc
= BFD_RELOC_16
;
1235 reloc
= BFD_RELOC_32
;
1238 reloc
= BFD_RELOC_64
;
1241 as_bad (_("unsupported BFD relocation size %d"), nbytes
);
1242 reloc
= BFD_RELOC_32
;
1247 fixP
= fix_new_exp (frag
, where
, nbytes
, exp
, 0, reloc
);
1248 fixP
->tc_fix_data
= NULL
;
1249 fixP
->fx_no_overflow
|= no_overflow
;
1254 md_apply_fix (fixS
*fixP
, valueT
* valP
, segT seg ATTRIBUTE_UNUSED
)
1256 const struct tilepro_operand
*operand
;
1257 valueT value
= *valP
;
1260 /* Leave these for the linker. */
1261 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1262 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1265 if (fixP
->fx_subsy
!= (symbolS
*) NULL
)
1267 /* We can't actually support subtracting a symbol. */
1268 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("expression too complex"));
1271 /* Correct relocation types for pc-relativeness. */
1272 switch (fixP
->fx_r_type
)
1274 #define FIX_PCREL(rtype) \
1276 if (fixP->fx_pcrel) \
1277 fixP->fx_r_type = rtype##_PCREL; \
1280 case rtype##_PCREL: \
1281 if (!fixP->fx_pcrel) \
1282 fixP->fx_r_type = rtype; \
1285 FIX_PCREL (BFD_RELOC_8
);
1286 FIX_PCREL (BFD_RELOC_16
);
1287 FIX_PCREL (BFD_RELOC_32
);
1288 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0
);
1289 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1
);
1290 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_LO
);
1291 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_LO
);
1292 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HI
);
1293 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HI
);
1294 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HA
);
1295 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HA
);
1304 if (fixP
->fx_addsy
!= NULL
)
1307 switch (fixP
->fx_r_type
)
1309 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD
:
1310 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD
:
1311 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE
:
1312 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE
:
1313 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_LO
:
1314 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_LO
:
1315 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_LO
:
1316 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_LO
:
1317 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HI
:
1318 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HI
:
1319 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HI
:
1320 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HI
:
1321 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HA
:
1322 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HA
:
1323 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HA
:
1324 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HA
:
1325 case BFD_RELOC_TILEPRO_TLS_DTPMOD32
:
1326 case BFD_RELOC_TILEPRO_TLS_DTPOFF32
:
1327 case BFD_RELOC_TILEPRO_TLS_TPOFF32
:
1328 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
1339 /* Apply lo16, hi16, ha16, etc. munging. */
1340 switch (fixP
->fx_r_type
)
1342 case BFD_RELOC_TILEPRO_IMM16_X0_GOT
:
1343 case BFD_RELOC_TILEPRO_IMM16_X1_GOT
:
1344 *valP
= value
= apply_special_operator (O_got
, value
);
1347 case BFD_RELOC_TILEPRO_IMM16_X0_GOT_LO
:
1348 case BFD_RELOC_TILEPRO_IMM16_X1_GOT_LO
:
1349 *valP
= value
= apply_special_operator (O_got_lo16
, value
);
1352 case BFD_RELOC_TILEPRO_IMM16_X0_GOT_HI
:
1353 case BFD_RELOC_TILEPRO_IMM16_X1_GOT_HI
:
1354 *valP
= value
= apply_special_operator (O_got_hi16
, value
);
1357 case BFD_RELOC_TILEPRO_IMM16_X0_GOT_HA
:
1358 case BFD_RELOC_TILEPRO_IMM16_X1_GOT_HA
:
1359 *valP
= value
= apply_special_operator (O_got_ha16
, value
);
1362 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD
:
1363 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD
:
1364 *valP
= value
= apply_special_operator (O_tls_gd
, value
);
1367 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE
:
1368 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE
:
1369 *valP
= value
= apply_special_operator (O_tls_ie
, value
);
1372 case BFD_RELOC_LO16
:
1373 case BFD_RELOC_TILEPRO_IMM16_X0_LO
:
1374 case BFD_RELOC_TILEPRO_IMM16_X1_LO
:
1375 case BFD_RELOC_TILEPRO_IMM16_X0_LO_PCREL
:
1376 case BFD_RELOC_TILEPRO_IMM16_X1_LO_PCREL
:
1377 *valP
= value
= apply_special_operator (O_lo16
, value
);
1380 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_LO
:
1381 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_LO
:
1382 *valP
= value
= apply_special_operator (O_tls_gd_lo16
, value
);
1385 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_LO
:
1386 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_LO
:
1387 *valP
= value
= apply_special_operator (O_tls_ie_lo16
, value
);
1390 case BFD_RELOC_HI16
:
1391 case BFD_RELOC_TILEPRO_IMM16_X0_HI
:
1392 case BFD_RELOC_TILEPRO_IMM16_X1_HI
:
1393 case BFD_RELOC_TILEPRO_IMM16_X0_HI_PCREL
:
1394 case BFD_RELOC_TILEPRO_IMM16_X1_HI_PCREL
:
1395 *valP
= value
= apply_special_operator (O_hi16
, value
);
1398 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HI
:
1399 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HI
:
1400 *valP
= value
= apply_special_operator (O_tls_gd_hi16
, value
);
1403 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HI
:
1404 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HI
:
1405 *valP
= value
= apply_special_operator (O_tls_ie_hi16
, value
);
1408 case BFD_RELOC_HI16_S
:
1409 case BFD_RELOC_TILEPRO_IMM16_X0_HA
:
1410 case BFD_RELOC_TILEPRO_IMM16_X1_HA
:
1411 case BFD_RELOC_TILEPRO_IMM16_X0_HA_PCREL
:
1412 case BFD_RELOC_TILEPRO_IMM16_X1_HA_PCREL
:
1413 *valP
= value
= apply_special_operator (O_ha16
, value
);
1416 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HA
:
1417 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HA
:
1418 *valP
= value
= apply_special_operator (O_tls_gd_ha16
, value
);
1421 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HA
:
1422 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HA
:
1423 *valP
= value
= apply_special_operator (O_tls_ie_ha16
, value
);
1431 p
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1433 operand
= fixP
->tc_fix_data
;
1434 if (operand
!= NULL
)
1436 /* It's an instruction operand. */
1437 tilepro_bundle_bits bits
=
1438 insert_operand (0, operand
, value
, fixP
->fx_file
, fixP
->fx_line
);
1440 /* Note that we might either be writing out bits for a bundle or a
1441 static network instruction, which are different sizes, so it's
1442 important to stop touching memory once we run out of bits. ORing in
1443 values is OK since we know the existing bits for this operand are
1445 for (; bits
!= 0; bits
>>= 8)
1450 /* Some other kind of relocation. */
1451 switch (fixP
->fx_r_type
)
1454 case BFD_RELOC_8_PCREL
:
1455 md_number_to_chars (p
, value
, 1);
1459 case BFD_RELOC_16_PCREL
:
1460 md_number_to_chars (p
, value
, 2);
1464 case BFD_RELOC_32_PCREL
:
1465 md_number_to_chars (p
, value
, 4);
1469 /* Leave it for the linker. */
1478 /* Generate the BFD reloc to be stuck in the object file from the
1479 fixup used internally in the assembler. */
1482 tc_gen_reloc (asection
*sec ATTRIBUTE_UNUSED
, fixS
*fixp
)
1486 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
1487 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
1488 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
1489 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1491 /* Make sure none of our internal relocations make it this far.
1492 They'd better have been fully resolved by this point. */
1493 gas_assert ((int) fixp
->fx_r_type
> 0);
1495 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
1496 if (reloc
->howto
== NULL
)
1498 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1499 _("cannot represent `%s' relocation in object file"),
1500 bfd_get_reloc_code_name (fixp
->fx_r_type
));
1504 if (!fixp
->fx_pcrel
!= !reloc
->howto
->pc_relative
)
1506 as_fatal (_("internal error? cannot generate `%s' relocation (%d, %d)"),
1507 bfd_get_reloc_code_name (fixp
->fx_r_type
),
1508 fixp
->fx_pcrel
, reloc
->howto
->pc_relative
);
1510 gas_assert (!fixp
->fx_pcrel
== !reloc
->howto
->pc_relative
);
1512 reloc
->addend
= fixp
->fx_offset
;
1518 /* The location from which a PC relative jump should be calculated,
1519 given a PC relative reloc. */
1522 md_pcrel_from (fixS
*fixP
)
1524 return fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
1528 /* Return 1 if it's OK to adjust a reloc by replacing the symbol with
1529 a section symbol plus some offset. */
1531 tilepro_fix_adjustable (fixS
*fix
)
1533 /* Prevent all adjustments to global symbols */
1534 if (S_IS_EXTERNAL (fix
->fx_addsy
) || S_IS_WEAK (fix
->fx_addsy
))
1542 tilepro_unrecognized_line (int ch
)
1549 as_bad (_("Found '{' when already bundling."));
1554 current_bundle_index
= 0;
1561 as_bad (_("Found '}' when not bundling."));
1565 tilepro_flush_bundle ();
1568 /* Allow '{' to follow on the same line. We also allow ";;", but that
1569 happens automatically because ';' is an end of line marker. */
1571 if (input_line_pointer
[0] == '{')
1573 input_line_pointer
++;
1574 return tilepro_unrecognized_line ('{');
1577 demand_empty_rest_of_line ();
1584 /* Not a valid line. */
1589 /* This is called from HANDLE_ALIGN in write.c. Fill in the contents
1590 of an rs_align_code fragment. */
1593 tilepro_handle_align (fragS
*fragp
)
1598 if (fragp
->fr_type
!= rs_align_code
)
1601 bytes
= fragp
->fr_next
->fr_address
- fragp
->fr_address
- fragp
->fr_fix
;
1602 p
= fragp
->fr_literal
+ fragp
->fr_fix
;
1605 /* Determine the bits for NOP. */
1606 const struct tilepro_opcode
*nop_opcode
=
1607 &tilepro_opcodes
[TILEPRO_OPC_NOP
];
1608 tilepro_bundle_bits nop
=
1609 ( nop_opcode
->fixed_bit_values
[TILEPRO_PIPELINE_X0
]
1610 | nop_opcode
->fixed_bit_values
[TILEPRO_PIPELINE_X1
]);
1612 if ((bytes
& (TILEPRO_BUNDLE_SIZE_IN_BYTES
- 1)) != 0)
1614 fix
= bytes
& (TILEPRO_BUNDLE_SIZE_IN_BYTES
- 1);
1620 number_to_chars_littleendian (p
, (unsigned int)nop
, 4);
1621 number_to_chars_littleendian (p
+ 4, (unsigned int)(nop
>> 32), 4);
1622 fragp
->fr_fix
+= fix
;
1623 fragp
->fr_var
= TILEPRO_BUNDLE_SIZE_IN_BYTES
;
1626 /* Standard calling conventions leave the CFA at SP on entry. */
1628 tilepro_cfi_frame_initial_instructions (void)
1630 cfi_add_CFA_def_cfa_register (54);
1634 tc_tilepro_regname_to_dw2regnum (char *regname
)
1638 for (i
= 0; i
< TILEPRO_NUM_REGISTERS
; i
++)
1640 if (!strcmp (regname
, tilepro_register_names
[i
]))