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
)
384 return (signed short)num
;
387 return (signed short)(num
>> 16);
390 return (signed short)((num
+ 0x8000) >> 16);
398 static tilepro_bundle_bits
399 emit_tilepro_instruction (tilepro_bundle_bits bits
,
401 const unsigned char *operands
,
402 expressionS
*operand_values
,
407 for (i
= 0; i
< num_operands
; i
++)
409 const struct tilepro_operand
*operand
=
410 &tilepro_operands
[operands
[i
]];
411 expressionS
*operand_exp
= &operand_values
[i
];
412 int is_pc_relative
= operand
->is_pc_relative
;
414 if (operand_exp
->X_op
== O_register
415 || (operand_exp
->X_op
== O_constant
&& !is_pc_relative
))
417 /* We know what the bits are right now, so insert them. */
418 bits
= insert_operand (bits
, operand
, operand_exp
->X_add_number
,
423 bfd_reloc_code_real_type reloc
= operand
->default_reloc
;
425 int die
= 0, use_subexp
= 0, require_symbol
= 0;
428 /* Take an expression like hi16(x) and turn it into x with
429 a different reloc type. */
430 switch (operand_exp
->X_op
)
432 #define HANDLE_OP16(suffix) \
435 case BFD_RELOC_TILEPRO_IMM16_X0: \
436 reloc = BFD_RELOC_TILEPRO_IMM16_X0_##suffix; \
438 case BFD_RELOC_TILEPRO_IMM16_X1: \
439 reloc = BFD_RELOC_TILEPRO_IMM16_X1_##suffix; \
465 HANDLE_OP16 (GOT_LO
);
470 HANDLE_OP16 (GOT_HI
);
475 HANDLE_OP16 (GOT_HA
);
480 HANDLE_OP16 (TLS_GD
);
485 HANDLE_OP16 (TLS_GD_LO
);
490 HANDLE_OP16 (TLS_GD_HI
);
495 HANDLE_OP16 (TLS_GD_HA
);
500 HANDLE_OP16 (TLS_IE
);
505 HANDLE_OP16 (TLS_IE_LO
);
510 HANDLE_OP16 (TLS_IE_HI
);
515 HANDLE_OP16 (TLS_IE_HA
);
524 case BFD_RELOC_TILEPRO_JOFFLONG_X1
:
525 reloc
= BFD_RELOC_TILEPRO_JOFFLONG_X1_PLT
;
542 as_bad (_("Invalid operator for operand."));
546 /* Now that we've changed the reloc, change ha16(x) into x,
549 if (operand_exp
->X_add_symbol
->sy_value
.X_md
)
551 /* HACK: We used X_md to mark this symbol as a fake wrapper
552 around a real expression. To unwrap it, we just grab its
554 operand_exp
= &operand_exp
->X_add_symbol
->sy_value
;
558 /* Look at the expression, and reject it if it's not a
560 if (operand_exp
->X_op
!= O_symbol
561 || operand_exp
->X_add_number
!= 0)
562 as_bad (_("Operator may only be applied to symbols."));
567 /* The value of this expression is an actual symbol, so
568 turn that into an expression. */
569 memset (&subexp
, 0, sizeof subexp
);
570 subexp
.X_op
= O_symbol
;
571 subexp
.X_add_symbol
= operand_exp
->X_add_symbol
;
572 operand_exp
= &subexp
;
576 /* Create a fixup to handle this later. */
577 fixP
= fix_new_exp (frag_now
,
578 bundle_start
- frag_now
->fr_literal
,
579 (operand
->num_bits
+ 7) >> 3,
583 fixP
->tc_fix_data
= operand
;
585 /* Don't do overflow checking if we are applying a function like
587 fixP
->fx_no_overflow
|= use_subexp
;
594 /* Detects and complains if two instructions in current_bundle write
595 to the same register, either implicitly or explicitly, or if a
596 read-only register is written. */
598 check_illegal_reg_writes (void)
600 BFD_HOST_U_64_BIT all_regs_written
= 0;
603 for (j
= 0; j
< current_bundle_index
; j
++)
605 const struct tilepro_instruction
*instr
= ¤t_bundle
[j
];
607 BFD_HOST_U_64_BIT regs
=
608 ((BFD_HOST_U_64_BIT
)1) << instr
->opcode
->implicitly_written_register
;
609 BFD_HOST_U_64_BIT conflict
;
611 for (k
= 0; k
< instr
->opcode
->num_operands
; k
++)
613 const struct tilepro_operand
*operand
=
614 &tilepro_operands
[instr
->opcode
->operands
[instr
->pipe
][k
]];
616 if (operand
->is_dest_reg
)
618 int regno
= instr
->operand_values
[k
].X_add_number
;
619 BFD_HOST_U_64_BIT mask
= ((BFD_HOST_U_64_BIT
)1) << regno
;
621 if ((mask
& ( (((BFD_HOST_U_64_BIT
)1) << TREG_IDN1
)
622 | (((BFD_HOST_U_64_BIT
)1) << TREG_UDN1
)
623 | (((BFD_HOST_U_64_BIT
)1) << TREG_UDN2
)
624 | (((BFD_HOST_U_64_BIT
)1) << TREG_UDN3
))) != 0
625 && !allow_suspicious_bundles
)
627 as_bad (_("Writes to register '%s' are not allowed."),
628 tilepro_register_names
[regno
]);
635 /* Writing to the zero register doesn't count. */
636 regs
&= ~(((BFD_HOST_U_64_BIT
)1) << TREG_ZERO
);
638 conflict
= all_regs_written
& regs
;
639 if (conflict
!= 0 && !allow_suspicious_bundles
)
641 /* Find which register caused the conflict. */
642 const char *conflicting_reg_name
= "???";
645 for (i
= 0; i
< TILEPRO_NUM_REGISTERS
; i
++)
647 if (((conflict
>> i
) & 1) != 0)
649 conflicting_reg_name
= tilepro_register_names
[i
];
654 as_bad (_("Two instructions in the same bundle both write "
655 "to register %s, which is not allowed."),
656 conflicting_reg_name
);
659 all_regs_written
|= regs
;
665 tilepro_flush_bundle (void)
669 unsigned compatible_pipes
;
670 const struct bundle_template
*match
;
675 switch (current_bundle_index
)
678 /* No instructions. */
681 if (current_bundle
[0].opcode
->can_bundle
)
683 /* Simplify later logic by adding an explicit fnop. */
684 prepend_nop_to_bundle (TILEPRO_OPC_FNOP
);
688 /* This instruction cannot be bundled with anything else.
689 Prepend an explicit 'nop', rather than an 'fnop', because
690 fnops can be replaced by later binary-processing tools
691 while nops cannot. */
692 prepend_nop_to_bundle (TILEPRO_OPC_NOP
);
696 if (!allow_suspicious_bundles
)
698 /* Make sure all instructions can be bundled with other
700 const struct tilepro_opcode
*cannot_bundle
= NULL
;
701 bfd_boolean seen_non_nop
= FALSE
;
703 for (j
= 0; j
< current_bundle_index
; j
++)
705 const struct tilepro_opcode
*op
= current_bundle
[j
].opcode
;
707 if (!op
->can_bundle
&& cannot_bundle
== NULL
)
709 else if (op
->mnemonic
!= TILEPRO_OPC_NOP
710 && op
->mnemonic
!= TILEPRO_OPC_INFO
711 && op
->mnemonic
!= TILEPRO_OPC_INFOL
)
715 if (cannot_bundle
!= NULL
&& seen_non_nop
)
717 current_bundle_index
= 0;
718 as_bad (_("'%s' may not be bundled with other instructions."),
719 cannot_bundle
->name
);
727 BUNDLE_TEMPLATE_MASK(current_bundle
[0].opcode
->pipes
,
728 current_bundle
[1].opcode
->pipes
,
729 (current_bundle_index
== 3
730 ? current_bundle
[2].opcode
->pipes
731 : (1 << NO_PIPELINE
)));
733 /* Find a template that works, if any. */
735 for (i
= 0; i
< sizeof bundle_templates
/ sizeof bundle_templates
[0]; i
++)
737 const struct bundle_template
*b
= &bundle_templates
[i
];
738 if ((b
->pipe_mask
& compatible_pipes
) == b
->pipe_mask
)
747 current_bundle_index
= 0;
748 as_bad (_("Invalid combination of instructions for bundle."));
752 /* If the section seems to have no alignment set yet, go ahead and
753 make it large enough to hold code. */
754 if (bfd_get_section_alignment (stdoutput
, now_seg
) == 0)
755 bfd_set_section_alignment (stdoutput
, now_seg
,
756 TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES
);
758 for (j
= 0; j
< current_bundle_index
; j
++)
759 current_bundle
[j
].pipe
= match
->pipe
[j
];
761 if (current_bundle_index
== 2 && !tilepro_is_x_pipeline(match
->pipe
[0]))
763 /* We are in Y mode with only two instructions, so add an FNOP. */
764 prepend_nop_to_bundle (TILEPRO_OPC_FNOP
);
766 /* Figure out what pipe the fnop must be in via arithmetic.
767 * p0 + p1 + p2 must sum to the sum of TILEPRO_PIPELINE_Y[012]. */
768 current_bundle
[0].pipe
=
769 (tilepro_pipeline
)((TILEPRO_PIPELINE_Y0
770 + TILEPRO_PIPELINE_Y1
771 + TILEPRO_PIPELINE_Y2
) -
772 (current_bundle
[1].pipe
+ current_bundle
[2].pipe
));
775 check_illegal_reg_writes ();
777 f
= frag_more (TILEPRO_BUNDLE_SIZE_IN_BYTES
);
779 /* Check to see if this bundle is at an offset that is a multiple of 8-bytes
780 from the start of the frag. */
781 addr_mod
= frag_now_fix () & (TILEPRO_BUNDLE_ALIGNMENT_IN_BYTES
- 1);
782 if (frag_now
->has_code
&& frag_now
->insn_addr
!= addr_mod
)
783 as_bad (_("instruction address is not a multiple of 8"));
784 frag_now
->insn_addr
= addr_mod
;
785 frag_now
->has_code
= 1;
787 tilepro_bundle_bits bits
= 0;
788 for (j
= 0; j
< current_bundle_index
; j
++)
790 struct tilepro_instruction
*instr
= ¤t_bundle
[j
];
791 tilepro_pipeline pipeline
= instr
->pipe
;
792 const struct tilepro_opcode
*opcode
= instr
->opcode
;
794 bits
|= emit_tilepro_instruction (opcode
->fixed_bit_values
[pipeline
],
795 opcode
->num_operands
,
796 &opcode
->operands
[pipeline
][0],
797 instr
->operand_values
,
801 number_to_chars_littleendian (f
, (unsigned int)bits
, 4);
802 number_to_chars_littleendian (f
+ 4, (unsigned int)(bits
>> 32), 4);
803 current_bundle_index
= 0;
805 /* Emit DWARF2 debugging information. */
806 dwarf2_emit_insn (TILEPRO_BUNDLE_SIZE_IN_BYTES
);
810 /* Extend the expression parser to handle hi16(label), etc.
811 as well as SPR names when in the context of parsing an SPR. */
813 tilepro_parse_name (char *name
, expressionS
*e
, char *nextcharP
)
815 operatorT op
= O_illegal
;
819 void *val
= hash_find (spr_hash
, name
);
823 memset (e
, 0, sizeof *e
);
824 e
->X_op
= O_constant
;
825 e
->X_add_number
= ((const struct tilepro_spr
*)val
)->number
;
829 if (*nextcharP
!= '(')
831 /* hi16, etc. not followed by a paren is just a label with that
837 /* Look up the operator in our table. */
838 void *val
= hash_find (special_operator_hash
, name
);
841 op
= (operatorT
)(long)val
;
844 /* Restore old '(' and skip it. */
845 *input_line_pointer
= '(';
846 ++input_line_pointer
;
850 if (*input_line_pointer
!= ')')
852 as_bad (_("Missing ')'"));
853 *nextcharP
= *input_line_pointer
;
857 ++input_line_pointer
;
859 if (e
->X_op
== O_register
|| e
->X_op
== O_absent
)
861 as_bad (_("Invalid expression."));
862 e
->X_op
= O_constant
;
867 /* Wrap subexpression with a unary operator. */
868 symbolS
*sym
= make_expr_symbol (e
);
870 if (sym
!= e
->X_add_symbol
)
872 /* HACK: mark this symbol as a temporary wrapper around a proper
873 expression, so we can unwrap it later once we have communicated
874 the relocation type. */
875 sym
->sy_value
.X_md
= 1;
878 memset (e
, 0, sizeof *e
);
880 e
->X_add_symbol
= sym
;
884 *nextcharP
= *input_line_pointer
;
889 /* Parses an expression which must be a register name. */
892 parse_reg_expression (expressionS
* expression
)
894 /* Zero everything to make sure we don't miss any flags. */
895 memset (expression
, 0, sizeof *expression
);
897 char* regname
= input_line_pointer
;
898 char terminating_char
= get_symbol_end ();
900 void* pval
= hash_find (main_reg_hash
, regname
);
903 as_bad (_("Expected register, got '%s'."), regname
);
905 int regno_and_flags
= (int)(size_t)pval
;
906 int regno
= EXTRACT_REGNO(regno_and_flags
);
908 if ((regno_and_flags
& NONCANONICAL_REG_NAME_FLAG
)
909 && require_canonical_reg_names
)
910 as_warn (_("Found use of non-canonical register name %s; "
912 regname
, tilepro_register_names
[regno
]);
914 /* Restore the old character following the register name. */
915 *input_line_pointer
= terminating_char
;
917 /* Fill in the expression fields to indicate it's a register. */
918 expression
->X_op
= O_register
;
919 expression
->X_add_number
= regno
;
923 /* Parses and type-checks comma-separated operands in input_line_pointer. */
925 parse_operands (const char *opcode_name
,
926 const unsigned char *operands
,
928 expressionS
*operand_values
)
932 memset (operand_values
, 0, num_operands
* sizeof operand_values
[0]);
935 for (i
= 0; i
< num_operands
; i
++)
937 tilepro_operand_type type
= tilepro_operands
[operands
[i
]].type
;
941 if (type
== TILEPRO_OP_TYPE_REGISTER
)
943 parse_reg_expression (&operand_values
[i
]);
945 else if (*input_line_pointer
== '}')
947 operand_values
[i
].X_op
= O_absent
;
949 else if (type
== TILEPRO_OP_TYPE_SPR
)
951 /* Modify the expression parser to add SPRs to the namespace. */
953 expression (&operand_values
[i
]);
958 expression (&operand_values
[i
]);
963 if (i
+ 1 < num_operands
)
965 int separator
= (unsigned char)*input_line_pointer
++;
967 if (is_end_of_line
[separator
] || (separator
== '}'))
969 as_bad (_("Too few operands to '%s'."), opcode_name
);
972 else if (separator
!= ',')
974 as_bad (_("Unexpected character '%c' after operand %d to %s."),
975 (char)separator
, i
+ 1, opcode_name
);
980 /* Arbitrarily use the first valid pipe to get the operand type,
981 since they are all the same. */
982 switch (tilepro_operands
[operands
[i
]].type
)
984 case TILEPRO_OP_TYPE_REGISTER
:
985 /* Handled in parse_reg_expression already. */
987 case TILEPRO_OP_TYPE_SPR
:
989 case TILEPRO_OP_TYPE_IMMEDIATE
:
991 case TILEPRO_OP_TYPE_ADDRESS
:
992 if ( operand_values
[i
].X_op
== O_register
993 || operand_values
[i
].X_op
== O_illegal
994 || operand_values
[i
].X_op
== O_absent
)
995 as_bad (_("Expected immediate expression"));
1002 if (!is_end_of_line
[(unsigned char)*input_line_pointer
])
1004 switch (*input_line_pointer
)
1008 as_bad (_("Found '}' when not bundling."));
1009 ++input_line_pointer
;
1011 demand_empty_rest_of_line ();
1015 as_bad (_("Too many operands"));
1019 /* Use default error for unrecognized garbage. */
1020 demand_empty_rest_of_line ();
1027 /* This is the guts of the machine-dependent assembler. STR points to a
1028 machine dependent instruction. This function is supposed to emit
1029 the frags/bytes it assembles to. */
1031 md_assemble (char *str
)
1035 char *old_input_line_pointer
;
1036 const struct tilepro_opcode
*op
;
1039 /* Split off the opcode and look it up. */
1040 opname_len
= strcspn (str
, " {}");
1041 old_char
= str
[opname_len
];
1042 str
[opname_len
] = '\0';
1044 op
= hash_find(op_hash
, str
);
1045 str
[opname_len
] = old_char
;
1048 as_bad (_("Unknown opcode `%.*s'."), (int)opname_len
, str
);
1052 /* Prepare to parse the operands. */
1053 old_input_line_pointer
= input_line_pointer
;
1054 input_line_pointer
= str
+ opname_len
;
1057 if (current_bundle_index
== TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE
)
1059 as_bad (_("Too many instructions for bundle."));
1060 tilepro_flush_bundle ();
1063 /* Make sure we have room for the upcoming bundle before we
1064 create any fixups. Otherwise if we have to switch to a new
1065 frag the fixup dot_value fields will be wrong. */
1066 frag_grow (TILEPRO_BUNDLE_SIZE_IN_BYTES
);
1068 /* Find a valid pipe for this opcode. */
1069 for (first_pipe
= 0; (op
->pipes
& (1 << first_pipe
)) == 0; first_pipe
++)
1072 /* Call the function that assembles this instruction. */
1073 current_bundle
[current_bundle_index
].opcode
= op
;
1074 parse_operands (op
->name
,
1075 &op
->operands
[first_pipe
][0],
1077 current_bundle
[current_bundle_index
].operand_values
);
1078 ++current_bundle_index
;
1080 /* Restore the saved value of input_line_pointer. */
1081 input_line_pointer
= old_input_line_pointer
;
1083 /* If we weren't inside curly braces, go ahead and emit
1084 this lone instruction as a bundle right now. */
1086 tilepro_flush_bundle ();
1090 s_require_canonical_reg_names (int require
)
1092 demand_empty_rest_of_line ();
1093 require_canonical_reg_names
= require
;
1097 s_allow_suspicious_bundles (int allow
)
1099 demand_empty_rest_of_line ();
1100 allow_suspicious_bundles
= allow
;
1103 const pseudo_typeS md_pseudo_table
[] =
1105 {"align", s_align_bytes
, 0}, /* Defaulting is invalid (0). */
1107 {"require_canonical_reg_names", s_require_canonical_reg_names
, 1 },
1108 {"no_require_canonical_reg_names", s_require_canonical_reg_names
, 0 },
1109 {"allow_suspicious_bundles", s_allow_suspicious_bundles
, 1 },
1110 {"no_allow_suspicious_bundles", s_allow_suspicious_bundles
, 0 },
1114 /* Equal to MAX_PRECISION in atof-ieee.c */
1115 #define MAX_LITTLENUMS 6
1117 /* Turn the string pointed to by litP into a floating point constant
1118 of type TYPE, and emit the appropriate bytes. The number of
1119 LITTLENUMS emitted is stored in *SIZEP. An error message is
1120 returned, or NULL on OK. */
1123 md_atof (int type
, char *litP
, int *sizeP
)
1126 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1127 LITTLENUM_TYPE
*wordP
;
1144 return _("Bad call to md_atof ()");
1146 t
= atof_ieee (input_line_pointer
, type
, words
);
1148 input_line_pointer
= t
;
1150 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1151 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
1152 the bigendian 386. */
1153 for (wordP
= words
+ prec
- 1; prec
--;)
1155 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
1156 litP
+= sizeof (LITTLENUM_TYPE
);
1162 /* We have no need to default values of symbols. */
1165 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
1172 tilepro_cons_fix_new (fragS
*frag
,
1178 bfd_reloc_code_real_type reloc
= BFD_RELOC_NONE
;
1179 int no_overflow
= 0;
1182 /* See if it's one of our special functions. */
1186 reloc
= BFD_RELOC_LO16
;
1190 reloc
= BFD_RELOC_HI16
;
1194 reloc
= BFD_RELOC_HI16_S
;
1203 if (reloc
!= BFD_RELOC_NONE
)
1207 as_bad (_("This operator only produces two byte values."));
1211 memset (&subexp
, 0, sizeof subexp
);
1212 subexp
.X_op
= O_symbol
;
1213 subexp
.X_add_symbol
= exp
->X_add_symbol
;
1221 reloc
= BFD_RELOC_8
;
1224 reloc
= BFD_RELOC_16
;
1227 reloc
= BFD_RELOC_32
;
1230 reloc
= BFD_RELOC_64
;
1233 as_bad (_("unsupported BFD relocation size %d"), nbytes
);
1234 reloc
= BFD_RELOC_32
;
1239 fixP
= fix_new_exp (frag
, where
, nbytes
, exp
, 0, reloc
);
1240 fixP
->tc_fix_data
= NULL
;
1241 fixP
->fx_no_overflow
|= no_overflow
;
1246 md_apply_fix (fixS
*fixP
, valueT
* valP
, segT seg ATTRIBUTE_UNUSED
)
1248 const struct tilepro_operand
*operand
;
1249 valueT value
= *valP
;
1252 /* Leave these for the linker. */
1253 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1254 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1257 if (fixP
->fx_subsy
!= (symbolS
*) NULL
)
1259 /* We can't actually support subtracting a symbol. */
1260 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("expression too complex"));
1263 /* Correct relocation types for pc-relativeness. */
1264 switch (fixP
->fx_r_type
)
1266 #define FIX_PCREL(rtype) \
1268 if (fixP->fx_pcrel) \
1269 fixP->fx_r_type = rtype##_PCREL; \
1272 case rtype##_PCREL: \
1273 if (!fixP->fx_pcrel) \
1274 fixP->fx_r_type = rtype; \
1277 FIX_PCREL (BFD_RELOC_8
);
1278 FIX_PCREL (BFD_RELOC_16
);
1279 FIX_PCREL (BFD_RELOC_32
);
1280 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0
);
1281 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1
);
1282 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_LO
);
1283 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_LO
);
1284 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HI
);
1285 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HI
);
1286 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HA
);
1287 FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HA
);
1296 if (fixP
->fx_addsy
!= NULL
)
1299 switch (fixP
->fx_r_type
)
1301 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD
:
1302 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD
:
1303 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE
:
1304 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE
:
1305 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_LO
:
1306 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_LO
:
1307 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_LO
:
1308 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_LO
:
1309 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HI
:
1310 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HI
:
1311 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HI
:
1312 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HI
:
1313 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HA
:
1314 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HA
:
1315 case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HA
:
1316 case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HA
:
1317 case BFD_RELOC_TILEPRO_TLS_DTPMOD32
:
1318 case BFD_RELOC_TILEPRO_TLS_DTPOFF32
:
1319 case BFD_RELOC_TILEPRO_TLS_TPOFF32
:
1320 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
1331 /* Apply lo16, hi16, ha16, etc. munging. */
1332 switch (fixP
->fx_r_type
)
1334 case BFD_RELOC_LO16
:
1335 case BFD_RELOC_TILEPRO_IMM16_X0_LO
:
1336 case BFD_RELOC_TILEPRO_IMM16_X1_LO
:
1337 case BFD_RELOC_TILEPRO_IMM16_X0_LO_PCREL
:
1338 case BFD_RELOC_TILEPRO_IMM16_X1_LO_PCREL
:
1339 *valP
= value
= apply_special_operator (O_lo16
, value
);
1342 case BFD_RELOC_HI16
:
1343 case BFD_RELOC_TILEPRO_IMM16_X0_HI
:
1344 case BFD_RELOC_TILEPRO_IMM16_X1_HI
:
1345 case BFD_RELOC_TILEPRO_IMM16_X0_HI_PCREL
:
1346 case BFD_RELOC_TILEPRO_IMM16_X1_HI_PCREL
:
1347 *valP
= value
= apply_special_operator (O_hi16
, value
);
1350 case BFD_RELOC_HI16_S
:
1351 case BFD_RELOC_TILEPRO_IMM16_X0_HA
:
1352 case BFD_RELOC_TILEPRO_IMM16_X1_HA
:
1353 case BFD_RELOC_TILEPRO_IMM16_X0_HA_PCREL
:
1354 case BFD_RELOC_TILEPRO_IMM16_X1_HA_PCREL
:
1355 *valP
= value
= apply_special_operator (O_ha16
, value
);
1363 p
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1365 operand
= fixP
->tc_fix_data
;
1366 if (operand
!= NULL
)
1368 /* It's an instruction operand. */
1369 tilepro_bundle_bits bits
=
1370 insert_operand (0, operand
, value
, fixP
->fx_file
, fixP
->fx_line
);
1372 /* Note that we might either be writing out bits for a bundle or a
1373 static network instruction, which are different sizes, so it's
1374 important to stop touching memory once we run out of bits. ORing in
1375 values is OK since we know the existing bits for this operand are
1377 for (; bits
!= 0; bits
>>= 8)
1382 /* Some other kind of relocation. */
1383 switch (fixP
->fx_r_type
)
1386 case BFD_RELOC_8_PCREL
:
1387 md_number_to_chars (p
, value
, 1);
1391 case BFD_RELOC_16_PCREL
:
1392 md_number_to_chars (p
, value
, 2);
1396 case BFD_RELOC_32_PCREL
:
1397 md_number_to_chars (p
, value
, 4);
1401 /* Leave it for the linker. */
1410 /* Generate the BFD reloc to be stuck in the object file from the
1411 fixup used internally in the assembler. */
1414 tc_gen_reloc (asection
*sec ATTRIBUTE_UNUSED
, fixS
*fixp
)
1418 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
1419 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
1420 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
1421 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1423 /* Make sure none of our internal relocations make it this far.
1424 They'd better have been fully resolved by this point. */
1425 gas_assert ((int) fixp
->fx_r_type
> 0);
1427 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
1428 if (reloc
->howto
== NULL
)
1430 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1431 _("cannot represent `%s' relocation in object file"),
1432 bfd_get_reloc_code_name (fixp
->fx_r_type
));
1436 if (!fixp
->fx_pcrel
!= !reloc
->howto
->pc_relative
)
1438 as_fatal (_("internal error? cannot generate `%s' relocation (%d, %d)"),
1439 bfd_get_reloc_code_name (fixp
->fx_r_type
),
1440 fixp
->fx_pcrel
, reloc
->howto
->pc_relative
);
1442 gas_assert (!fixp
->fx_pcrel
== !reloc
->howto
->pc_relative
);
1444 reloc
->addend
= fixp
->fx_offset
;
1450 /* The location from which a PC relative jump should be calculated,
1451 given a PC relative reloc. */
1454 md_pcrel_from (fixS
*fixP
)
1456 return fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
1460 /* Return 1 if it's OK to adjust a reloc by replacing the symbol with
1461 a section symbol plus some offset. */
1463 tilepro_fix_adjustable (fixS
*fix
)
1465 /* Prevent all adjustments to global symbols */
1466 if (S_IS_EXTERNAL (fix
->fx_addsy
) || S_IS_WEAK (fix
->fx_addsy
))
1474 tilepro_unrecognized_line (int ch
)
1481 as_bad (_("Found '{' when already bundling."));
1486 current_bundle_index
= 0;
1493 as_bad (_("Found '}' when not bundling."));
1497 tilepro_flush_bundle ();
1500 /* Allow '{' to follow on the same line. We also allow ";;", but that
1501 happens automatically because ';' is an end of line marker. */
1503 if (input_line_pointer
[0] == '{')
1505 input_line_pointer
++;
1506 return tilepro_unrecognized_line ('{');
1509 demand_empty_rest_of_line ();
1516 /* Not a valid line. */
1521 /* This is called from HANDLE_ALIGN in write.c. Fill in the contents
1522 of an rs_align_code fragment. */
1525 tilepro_handle_align (fragS
*fragp
)
1530 if (fragp
->fr_type
!= rs_align_code
)
1533 bytes
= fragp
->fr_next
->fr_address
- fragp
->fr_address
- fragp
->fr_fix
;
1534 p
= fragp
->fr_literal
+ fragp
->fr_fix
;
1537 /* Determine the bits for NOP. */
1538 const struct tilepro_opcode
*nop_opcode
=
1539 &tilepro_opcodes
[TILEPRO_OPC_NOP
];
1540 tilepro_bundle_bits nop
=
1541 ( nop_opcode
->fixed_bit_values
[TILEPRO_PIPELINE_X0
]
1542 | nop_opcode
->fixed_bit_values
[TILEPRO_PIPELINE_X1
]);
1544 if ((bytes
& (TILEPRO_BUNDLE_SIZE_IN_BYTES
- 1)) != 0)
1546 fix
= bytes
& (TILEPRO_BUNDLE_SIZE_IN_BYTES
- 1);
1552 number_to_chars_littleendian (p
, (unsigned int)nop
, 4);
1553 number_to_chars_littleendian (p
+ 4, (unsigned int)(nop
>> 32), 4);
1554 fragp
->fr_fix
+= fix
;
1555 fragp
->fr_var
= TILEPRO_BUNDLE_SIZE_IN_BYTES
;
1558 /* Standard calling conventions leave the CFA at SP on entry. */
1560 tilepro_cfi_frame_initial_instructions (void)
1562 cfi_add_CFA_def_cfa_register (54);
1566 tc_tilepro_regname_to_dw2regnum (char *regname
)
1570 for (i
= 0; i
< TILEPRO_NUM_REGISTERS
; i
++)
1572 if (!strcmp (regname
, tilepro_register_names
[i
]))