]>
Commit | Line | Data |
---|---|---|
1 | /* tc-xtensa.c -- Assemble Xtensa instructions. | |
2 | Copyright (C) 2003-2022 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GAS, the GNU Assembler. | |
5 | ||
6 | GAS 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, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GAS 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. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GAS; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, | |
19 | MA 02110-1301, USA. */ | |
20 | ||
21 | #include "as.h" | |
22 | #include <limits.h> | |
23 | #include "sb.h" | |
24 | #include "safe-ctype.h" | |
25 | #include "tc-xtensa.h" | |
26 | #include "subsegs.h" | |
27 | #include "xtensa-relax.h" | |
28 | #include "dwarf2dbg.h" | |
29 | #include "xtensa-istack.h" | |
30 | #include "xtensa-dynconfig.h" | |
31 | #include "elf/xtensa.h" | |
32 | ||
33 | #ifndef uint32 | |
34 | #define uint32 unsigned int | |
35 | #endif | |
36 | #ifndef int32 | |
37 | #define int32 signed int | |
38 | #endif | |
39 | ||
40 | /* Notes: | |
41 | ||
42 | Naming conventions (used somewhat inconsistently): | |
43 | The xtensa_ functions are exported | |
44 | The xg_ functions are internal | |
45 | ||
46 | We also have a couple of different extensibility mechanisms. | |
47 | 1) The idiom replacement: | |
48 | This is used when a line is first parsed to | |
49 | replace an instruction pattern with another instruction | |
50 | It is currently limited to replacements of instructions | |
51 | with constant operands. | |
52 | 2) The xtensa-relax.c mechanism that has stronger instruction | |
53 | replacement patterns. When an instruction's immediate field | |
54 | does not fit the next instruction sequence is attempted. | |
55 | In addition, "narrow" opcodes are supported this way. */ | |
56 | ||
57 | ||
58 | /* Define characters with special meanings to GAS. */ | |
59 | const char comment_chars[] = "#"; | |
60 | const char line_comment_chars[] = "#"; | |
61 | const char line_separator_chars[] = ";"; | |
62 | const char EXP_CHARS[] = "eE"; | |
63 | const char FLT_CHARS[] = "rRsSfFdDxXpP"; | |
64 | ||
65 | ||
66 | /* Flags to indicate whether the hardware supports the density and | |
67 | absolute literals options. */ | |
68 | ||
69 | bool density_supported; | |
70 | bool absolute_literals_supported; | |
71 | ||
72 | static unsigned microarch_earliest; | |
73 | ||
74 | static vliw_insn cur_vinsn; | |
75 | ||
76 | unsigned xtensa_num_pipe_stages; | |
77 | unsigned xtensa_fetch_width; | |
78 | ||
79 | static enum debug_info_type xt_saved_debug_type = DEBUG_NONE; | |
80 | ||
81 | /* Some functions are only valid in the front end. This variable | |
82 | allows us to assert that we haven't crossed over into the | |
83 | back end. */ | |
84 | static bool past_xtensa_md_finish = false; | |
85 | ||
86 | /* Flags for properties of the last instruction in a segment. */ | |
87 | #define FLAG_IS_A0_WRITER 0x1 | |
88 | #define FLAG_IS_BAD_LOOPEND 0x2 | |
89 | ||
90 | ||
91 | /* We define a special segment names ".literal" to place literals | |
92 | into. The .fini and .init sections are special because they | |
93 | contain code that is moved together by the linker. We give them | |
94 | their own special .fini.literal and .init.literal sections. */ | |
95 | ||
96 | #define LITERAL_SECTION_NAME xtensa_section_rename (".literal") | |
97 | #define LIT4_SECTION_NAME xtensa_section_rename (".lit4") | |
98 | #define INIT_SECTION_NAME xtensa_section_rename (".init") | |
99 | #define FINI_SECTION_NAME xtensa_section_rename (".fini") | |
100 | ||
101 | ||
102 | /* This type is used for the directive_stack to keep track of the | |
103 | state of the literal collection pools. If lit_prefix is set, it is | |
104 | used to determine the literal section names; otherwise, the literal | |
105 | sections are determined based on the current text section. The | |
106 | lit_seg and lit4_seg fields cache these literal sections, with the | |
107 | current_text_seg field used a tag to indicate whether the cached | |
108 | values are valid. */ | |
109 | ||
110 | typedef struct lit_state_struct | |
111 | { | |
112 | char *lit_prefix; | |
113 | segT current_text_seg; | |
114 | segT lit_seg; | |
115 | segT lit4_seg; | |
116 | } lit_state; | |
117 | ||
118 | static lit_state default_lit_sections; | |
119 | ||
120 | ||
121 | /* We keep a list of literal segments. The seg_list type is the node | |
122 | for this list. The literal_head pointer is the head of the list, | |
123 | with the literal_head_h dummy node at the start. */ | |
124 | ||
125 | typedef struct seg_list_struct | |
126 | { | |
127 | struct seg_list_struct *next; | |
128 | segT seg; | |
129 | } seg_list; | |
130 | ||
131 | static seg_list literal_head_h; | |
132 | static seg_list *literal_head = &literal_head_h; | |
133 | ||
134 | ||
135 | /* Lists of symbols. We keep a list of symbols that label the current | |
136 | instruction, so that we can adjust the symbols when inserting alignment | |
137 | for various instructions. We also keep a list of all the symbols on | |
138 | literals, so that we can fix up those symbols when the literals are | |
139 | later moved into the text sections. */ | |
140 | ||
141 | typedef struct sym_list_struct | |
142 | { | |
143 | struct sym_list_struct *next; | |
144 | symbolS *sym; | |
145 | } sym_list; | |
146 | ||
147 | static sym_list *insn_labels = NULL; | |
148 | static sym_list *free_insn_labels = NULL; | |
149 | static sym_list *saved_insn_labels = NULL; | |
150 | ||
151 | static sym_list *literal_syms; | |
152 | ||
153 | ||
154 | /* Flags to determine whether to prefer const16 or l32r | |
155 | if both options are available. */ | |
156 | int prefer_const16 = 0; | |
157 | int prefer_l32r = 0; | |
158 | ||
159 | /* Global flag to indicate when we are emitting literals. */ | |
160 | int generating_literals = 0; | |
161 | ||
162 | /* The following PROPERTY table definitions are copied from | |
163 | <elf/xtensa.h> and must be kept in sync with the code there. */ | |
164 | ||
165 | /* Flags in the property tables to specify whether blocks of memory | |
166 | are literals, instructions, data, or unreachable. For | |
167 | instructions, blocks that begin loop targets and branch targets are | |
168 | designated. Blocks that do not allow density, instruction | |
169 | reordering or transformation are also specified. Finally, for | |
170 | branch targets, branch target alignment priority is included. | |
171 | Alignment of the next block is specified in the current block | |
172 | and the size of the current block does not include any fill required | |
173 | to align to the next block. */ | |
174 | ||
175 | #define XTENSA_PROP_LITERAL 0x00000001 | |
176 | #define XTENSA_PROP_INSN 0x00000002 | |
177 | #define XTENSA_PROP_DATA 0x00000004 | |
178 | #define XTENSA_PROP_UNREACHABLE 0x00000008 | |
179 | /* Instruction only properties at beginning of code. */ | |
180 | #define XTENSA_PROP_INSN_LOOP_TARGET 0x00000010 | |
181 | #define XTENSA_PROP_INSN_BRANCH_TARGET 0x00000020 | |
182 | /* Instruction only properties about code. */ | |
183 | #define XTENSA_PROP_INSN_NO_DENSITY 0x00000040 | |
184 | #define XTENSA_PROP_INSN_NO_REORDER 0x00000080 | |
185 | /* Historically, NO_TRANSFORM was a property of instructions, | |
186 | but it should apply to literals under certain circumstances. */ | |
187 | #define XTENSA_PROP_NO_TRANSFORM 0x00000100 | |
188 | ||
189 | /* Branch target alignment information. This transmits information | |
190 | to the linker optimization about the priority of aligning a | |
191 | particular block for branch target alignment: None, low priority, | |
192 | high priority, or required. These only need to be checked in | |
193 | instruction blocks marked as XTENSA_PROP_INSN_BRANCH_TARGET. | |
194 | Common usage is | |
195 | ||
196 | switch (GET_XTENSA_PROP_BT_ALIGN (flags)) | |
197 | case XTENSA_PROP_BT_ALIGN_NONE: | |
198 | case XTENSA_PROP_BT_ALIGN_LOW: | |
199 | case XTENSA_PROP_BT_ALIGN_HIGH: | |
200 | case XTENSA_PROP_BT_ALIGN_REQUIRE: | |
201 | */ | |
202 | #define XTENSA_PROP_BT_ALIGN_MASK 0x00000600 | |
203 | ||
204 | /* No branch target alignment. */ | |
205 | #define XTENSA_PROP_BT_ALIGN_NONE 0x0 | |
206 | /* Low priority branch target alignment. */ | |
207 | #define XTENSA_PROP_BT_ALIGN_LOW 0x1 | |
208 | /* High priority branch target alignment. */ | |
209 | #define XTENSA_PROP_BT_ALIGN_HIGH 0x2 | |
210 | /* Required branch target alignment. */ | |
211 | #define XTENSA_PROP_BT_ALIGN_REQUIRE 0x3 | |
212 | ||
213 | #define SET_XTENSA_PROP_BT_ALIGN(flag, align) \ | |
214 | (((flag) & (~XTENSA_PROP_BT_ALIGN_MASK)) | \ | |
215 | (((align) << 9) & XTENSA_PROP_BT_ALIGN_MASK)) | |
216 | ||
217 | ||
218 | /* Alignment is specified in the block BEFORE the one that needs | |
219 | alignment. Up to 5 bits. Use GET_XTENSA_PROP_ALIGNMENT(flags) to | |
220 | get the required alignment specified as a power of 2. Use | |
221 | SET_XTENSA_PROP_ALIGNMENT(flags, pow2) to set the required | |
222 | alignment. Be careful of side effects since the SET will evaluate | |
223 | flags twice. Also, note that the SIZE of a block in the property | |
224 | table does not include the alignment size, so the alignment fill | |
225 | must be calculated to determine if two blocks are contiguous. | |
226 | TEXT_ALIGN is not currently implemented but is a placeholder for a | |
227 | possible future implementation. */ | |
228 | ||
229 | #define XTENSA_PROP_ALIGN 0x00000800 | |
230 | ||
231 | #define XTENSA_PROP_ALIGNMENT_MASK 0x0001f000 | |
232 | ||
233 | #define SET_XTENSA_PROP_ALIGNMENT(flag, align) \ | |
234 | (((flag) & (~XTENSA_PROP_ALIGNMENT_MASK)) | \ | |
235 | (((align) << 12) & XTENSA_PROP_ALIGNMENT_MASK)) | |
236 | ||
237 | #define XTENSA_PROP_INSN_ABSLIT 0x00020000 | |
238 | ||
239 | ||
240 | /* Structure for saving instruction and alignment per-fragment data | |
241 | that will be written to the object file. This structure is | |
242 | equivalent to the actual data that will be written out to the file | |
243 | but is easier to use. We provide a conversion to file flags | |
244 | in frag_flags_to_number. */ | |
245 | ||
246 | typedef struct frag_flags_struct frag_flags; | |
247 | ||
248 | struct frag_flags_struct | |
249 | { | |
250 | /* is_literal should only be used after xtensa_move_literals. | |
251 | If you need to check if you are generating a literal fragment, | |
252 | then use the generating_literals global. */ | |
253 | ||
254 | unsigned is_literal : 1; | |
255 | unsigned is_insn : 1; | |
256 | unsigned is_data : 1; | |
257 | unsigned is_unreachable : 1; | |
258 | ||
259 | /* is_specific_opcode implies no_transform. */ | |
260 | unsigned is_no_transform : 1; | |
261 | ||
262 | struct | |
263 | { | |
264 | unsigned is_loop_target : 1; | |
265 | unsigned is_branch_target : 1; /* Branch targets have a priority. */ | |
266 | unsigned bt_align_priority : 2; | |
267 | ||
268 | unsigned is_no_density : 1; | |
269 | /* no_longcalls flag does not need to be placed in the object file. */ | |
270 | ||
271 | unsigned is_no_reorder : 1; | |
272 | ||
273 | /* Uses absolute literal addressing for l32r. */ | |
274 | unsigned is_abslit : 1; | |
275 | } insn; | |
276 | unsigned is_align : 1; | |
277 | unsigned alignment : 5; | |
278 | }; | |
279 | ||
280 | ||
281 | /* Structure for saving information about a block of property data | |
282 | for frags that have the same flags. */ | |
283 | struct xtensa_block_info_struct | |
284 | { | |
285 | segT sec; | |
286 | bfd_vma offset; | |
287 | size_t size; | |
288 | frag_flags flags; | |
289 | struct xtensa_block_info_struct *next; | |
290 | }; | |
291 | ||
292 | ||
293 | /* Structure for saving the current state before emitting literals. */ | |
294 | typedef struct emit_state_struct | |
295 | { | |
296 | const char *name; | |
297 | segT now_seg; | |
298 | subsegT now_subseg; | |
299 | int generating_literals; | |
300 | } emit_state; | |
301 | ||
302 | ||
303 | /* Opcode placement information */ | |
304 | ||
305 | typedef unsigned long long bitfield; | |
306 | #define bit_is_set(bit, bf) ((bf) & (0x01ll << (bit))) | |
307 | #define set_bit(bit, bf) ((bf) |= (0x01ll << (bit))) | |
308 | #define clear_bit(bit, bf) ((bf) &= ~(0x01ll << (bit))) | |
309 | ||
310 | #define MAX_FORMATS 32 | |
311 | ||
312 | typedef struct op_placement_info_struct | |
313 | { | |
314 | int num_formats; | |
315 | /* A number describing how restrictive the issue is for this | |
316 | opcode. For example, an opcode that fits lots of different | |
317 | formats has a high freedom, as does an opcode that fits | |
318 | only one format but many slots in that format. The most | |
319 | restrictive is the opcode that fits only one slot in one | |
320 | format. */ | |
321 | int issuef; | |
322 | xtensa_format narrowest; | |
323 | char narrowest_size; | |
324 | char narrowest_slot; | |
325 | ||
326 | /* formats is a bitfield with the Nth bit set | |
327 | if the opcode fits in the Nth xtensa_format. */ | |
328 | bitfield formats; | |
329 | ||
330 | /* slots[N]'s Mth bit is set if the op fits in the | |
331 | Mth slot of the Nth xtensa_format. */ | |
332 | bitfield slots[MAX_FORMATS]; | |
333 | ||
334 | /* A count of the number of slots in a given format | |
335 | an op can fit (i.e., the bitcount of the slot field above). */ | |
336 | char slots_in_format[MAX_FORMATS]; | |
337 | ||
338 | } op_placement_info, *op_placement_info_table; | |
339 | ||
340 | op_placement_info_table op_placement_table; | |
341 | ||
342 | ||
343 | /* Extra expression types. */ | |
344 | ||
345 | #define O_pltrel O_md1 /* like O_symbol but use a PLT reloc */ | |
346 | #define O_hi16 O_md2 /* use high 16 bits of symbolic value */ | |
347 | #define O_lo16 O_md3 /* use low 16 bits of symbolic value */ | |
348 | #define O_pcrel O_md4 /* value is a PC-relative offset */ | |
349 | #define O_tlsfunc O_md5 /* TLS_FUNC/TLSDESC_FN relocation */ | |
350 | #define O_tlsarg O_md6 /* TLS_ARG/TLSDESC_ARG relocation */ | |
351 | #define O_tlscall O_md7 /* TLS_CALL relocation */ | |
352 | #define O_tpoff O_md8 /* TPOFF relocation */ | |
353 | #define O_dtpoff O_md9 /* DTPOFF relocation */ | |
354 | ||
355 | struct suffix_reloc_map | |
356 | { | |
357 | const char *suffix; | |
358 | int length; | |
359 | bfd_reloc_code_real_type reloc; | |
360 | operatorT operator; | |
361 | }; | |
362 | ||
363 | #define SUFFIX_MAP(str, reloc, op) { str, sizeof (str) - 1, reloc, op } | |
364 | ||
365 | static struct suffix_reloc_map suffix_relocs[] = | |
366 | { | |
367 | SUFFIX_MAP ("l", BFD_RELOC_LO16, O_lo16), | |
368 | SUFFIX_MAP ("h", BFD_RELOC_HI16, O_hi16), | |
369 | SUFFIX_MAP ("plt", BFD_RELOC_XTENSA_PLT, O_pltrel), | |
370 | SUFFIX_MAP ("pcrel", BFD_RELOC_32_PCREL, O_pcrel), | |
371 | SUFFIX_MAP ("tlsfunc", BFD_RELOC_XTENSA_TLS_FUNC, O_tlsfunc), | |
372 | SUFFIX_MAP ("tlsarg", BFD_RELOC_XTENSA_TLS_ARG, O_tlsarg), | |
373 | SUFFIX_MAP ("tlscall", BFD_RELOC_XTENSA_TLS_CALL, O_tlscall), | |
374 | SUFFIX_MAP ("tpoff", BFD_RELOC_XTENSA_TLS_TPOFF, O_tpoff), | |
375 | SUFFIX_MAP ("dtpoff", BFD_RELOC_XTENSA_TLS_DTPOFF, O_dtpoff), | |
376 | }; | |
377 | ||
378 | ||
379 | /* Directives. */ | |
380 | ||
381 | typedef enum | |
382 | { | |
383 | directive_none = 0, | |
384 | directive_literal, | |
385 | directive_density, | |
386 | directive_transform, | |
387 | directive_freeregs, | |
388 | directive_longcalls, | |
389 | directive_literal_prefix, | |
390 | directive_schedule, | |
391 | directive_absolute_literals, | |
392 | directive_last_directive | |
393 | } directiveE; | |
394 | ||
395 | typedef struct | |
396 | { | |
397 | const char *name; | |
398 | bool can_be_negated; | |
399 | } directive_infoS; | |
400 | ||
401 | const directive_infoS directive_info[] = | |
402 | { | |
403 | { "none", false }, | |
404 | { "literal", false }, | |
405 | { "density", true }, | |
406 | { "transform", true }, | |
407 | { "freeregs", false }, | |
408 | { "longcalls", true }, | |
409 | { "literal_prefix", false }, | |
410 | { "schedule", true }, | |
411 | { "absolute-literals", true } | |
412 | }; | |
413 | ||
414 | bool directive_state[] = | |
415 | { | |
416 | false, /* none */ | |
417 | false, /* literal */ | |
418 | false, /* density */ | |
419 | true, /* transform */ | |
420 | false, /* freeregs */ | |
421 | false, /* longcalls */ | |
422 | false, /* literal_prefix */ | |
423 | false, /* schedule */ | |
424 | false /* absolute_literals */ | |
425 | }; | |
426 | ||
427 | /* A circular list of all potential and actual literal pool locations | |
428 | in a segment. */ | |
429 | struct litpool_frag | |
430 | { | |
431 | struct litpool_frag *next; | |
432 | struct litpool_frag *prev; | |
433 | fragS *fragP; | |
434 | addressT addr; | |
435 | short priority; /* 1, 2, or 3 -- 1 is highest */ | |
436 | short original_priority; | |
437 | int literal_count; | |
438 | }; | |
439 | ||
440 | /* Map a segment to its litpool_frag list. */ | |
441 | struct litpool_seg | |
442 | { | |
443 | struct litpool_seg *next; | |
444 | asection *seg; | |
445 | struct litpool_frag frag_list; | |
446 | int frag_count; /* since last litpool location */ | |
447 | }; | |
448 | ||
449 | static struct litpool_seg litpool_seg_list; | |
450 | ||
451 | /* Limit maximal size of auto litpool by half of the j range. */ | |
452 | #define MAX_AUTO_POOL_LITERALS 16384 | |
453 | ||
454 | /* Limit maximal size of explicit literal pool by l32r range. */ | |
455 | #define MAX_EXPLICIT_POOL_LITERALS 65536 | |
456 | ||
457 | #define MAX_POOL_LITERALS \ | |
458 | (auto_litpools ? MAX_AUTO_POOL_LITERALS : MAX_EXPLICIT_POOL_LITERALS) | |
459 | ||
460 | /* Directive functions. */ | |
461 | ||
462 | static void xtensa_begin_directive (int); | |
463 | static void xtensa_end_directive (int); | |
464 | static void xtensa_literal_prefix (void); | |
465 | static void xtensa_literal_position (int); | |
466 | static void xtensa_literal_pseudo (int); | |
467 | static void xtensa_frequency_pseudo (int); | |
468 | static void xtensa_elf_cons (int); | |
469 | static void xtensa_leb128 (int); | |
470 | ||
471 | /* Parsing and Idiom Translation. */ | |
472 | ||
473 | static bfd_reloc_code_real_type xtensa_elf_suffix (char **, expressionS *); | |
474 | ||
475 | /* Various Other Internal Functions. */ | |
476 | ||
477 | extern bool xg_is_single_relaxable_insn (TInsn *, TInsn *, bool); | |
478 | static bool xg_build_to_insn (TInsn *, TInsn *, BuildInstr *); | |
479 | static void xtensa_mark_literal_pool_location (void); | |
480 | static addressT get_expanded_loop_offset (xtensa_opcode); | |
481 | static fragS *get_literal_pool_location (segT); | |
482 | static void set_literal_pool_location (segT, fragS *); | |
483 | static void xtensa_set_frag_assembly_state (fragS *); | |
484 | static void finish_vinsn (vliw_insn *); | |
485 | static bool emit_single_op (TInsn *); | |
486 | static int total_frag_text_expansion (fragS *); | |
487 | static bool use_trampolines = true; | |
488 | static void xtensa_check_frag_count (void); | |
489 | static void xtensa_create_trampoline_frag (bool); | |
490 | static void xtensa_maybe_create_trampoline_frag (void); | |
491 | struct trampoline_frag; | |
492 | static int init_trampoline_frag (fragS *); | |
493 | static fixS *xg_append_jump (fragS *fragP, symbolS *sym, offsetT offset); | |
494 | static void xtensa_maybe_create_literal_pool_frag (bool, bool); | |
495 | static bool auto_litpools = false; | |
496 | static int auto_litpool_limit = 0; | |
497 | static bool xtensa_is_init_fini (segT seg); | |
498 | ||
499 | /* Alignment Functions. */ | |
500 | ||
501 | static int get_text_align_power (unsigned); | |
502 | static int get_text_align_max_fill_size (int, bool, bool); | |
503 | static int branch_align_power (segT); | |
504 | ||
505 | /* Helpers for xtensa_relax_frag(). */ | |
506 | ||
507 | static long relax_frag_add_nop (fragS *); | |
508 | ||
509 | /* Accessors for additional per-subsegment information. */ | |
510 | ||
511 | static unsigned get_last_insn_flags (segT, subsegT); | |
512 | static void set_last_insn_flags (segT, subsegT, unsigned, bool); | |
513 | static float get_subseg_total_freq (segT, subsegT); | |
514 | static float get_subseg_target_freq (segT, subsegT); | |
515 | static void set_subseg_freq (segT, subsegT, float, float); | |
516 | ||
517 | /* Segment list functions. */ | |
518 | ||
519 | static void xtensa_move_literals (void); | |
520 | static void xtensa_reorder_segments (void); | |
521 | static void xtensa_switch_to_literal_fragment (emit_state *); | |
522 | static void xtensa_switch_to_non_abs_literal_fragment (emit_state *); | |
523 | static void xtensa_switch_section_emit_state (emit_state *, segT, subsegT); | |
524 | static void xtensa_restore_emit_state (emit_state *); | |
525 | static segT cache_literal_section (bool); | |
526 | ||
527 | /* op_placement_info functions. */ | |
528 | ||
529 | static void init_op_placement_info_table (void); | |
530 | extern bool opcode_fits_format_slot (xtensa_opcode, xtensa_format, int); | |
531 | static int xg_get_single_size (xtensa_opcode); | |
532 | static xtensa_format xg_get_single_format (xtensa_opcode); | |
533 | static int xg_get_single_slot (xtensa_opcode); | |
534 | ||
535 | /* TInsn and IStack functions. */ | |
536 | ||
537 | static bool tinsn_has_symbolic_operands (const TInsn *); | |
538 | static bool tinsn_has_invalid_symbolic_operands (const TInsn *); | |
539 | static bool tinsn_has_complex_operands (const TInsn *); | |
540 | static bool tinsn_to_insnbuf (TInsn *, xtensa_insnbuf); | |
541 | static bool tinsn_check_arguments (const TInsn *); | |
542 | static void tinsn_from_chars (TInsn *, char *, int); | |
543 | static void tinsn_immed_from_frag (TInsn *, fragS *, int); | |
544 | static int get_num_stack_text_bytes (IStack *); | |
545 | static int get_num_stack_literal_bytes (IStack *); | |
546 | static bool tinsn_to_slotbuf (xtensa_format, int, TInsn *, xtensa_insnbuf); | |
547 | ||
548 | /* vliw_insn functions. */ | |
549 | ||
550 | static void xg_init_vinsn (vliw_insn *); | |
551 | static void xg_copy_vinsn (vliw_insn *, vliw_insn *); | |
552 | static void xg_clear_vinsn (vliw_insn *); | |
553 | static bool vinsn_has_specific_opcodes (vliw_insn *); | |
554 | static void xg_free_vinsn (vliw_insn *); | |
555 | static bool vinsn_to_insnbuf | |
556 | (vliw_insn *, char *, fragS *, bool); | |
557 | static void vinsn_from_chars (vliw_insn *, char *); | |
558 | ||
559 | /* Expression Utilities. */ | |
560 | ||
561 | bool expr_is_const (const expressionS *); | |
562 | offsetT get_expr_const (const expressionS *); | |
563 | void set_expr_const (expressionS *, offsetT); | |
564 | bool expr_is_register (const expressionS *); | |
565 | offsetT get_expr_register (const expressionS *); | |
566 | void set_expr_symbol_offset (expressionS *, symbolS *, offsetT); | |
567 | bool expr_is_equal (expressionS *, expressionS *); | |
568 | static void copy_expr (expressionS *, const expressionS *); | |
569 | ||
570 | /* Section renaming. */ | |
571 | ||
572 | static void build_section_rename (const char *); | |
573 | ||
574 | ||
575 | /* ISA imported from bfd. */ | |
576 | extern xtensa_isa xtensa_default_isa; | |
577 | ||
578 | extern int target_big_endian; | |
579 | ||
580 | static xtensa_opcode xtensa_addi_opcode; | |
581 | static xtensa_opcode xtensa_addmi_opcode; | |
582 | static xtensa_opcode xtensa_call0_opcode; | |
583 | static xtensa_opcode xtensa_call4_opcode; | |
584 | static xtensa_opcode xtensa_call8_opcode; | |
585 | static xtensa_opcode xtensa_call12_opcode; | |
586 | static xtensa_opcode xtensa_callx0_opcode; | |
587 | static xtensa_opcode xtensa_callx4_opcode; | |
588 | static xtensa_opcode xtensa_callx8_opcode; | |
589 | static xtensa_opcode xtensa_callx12_opcode; | |
590 | static xtensa_opcode xtensa_const16_opcode; | |
591 | static xtensa_opcode xtensa_entry_opcode; | |
592 | static xtensa_opcode xtensa_extui_opcode; | |
593 | static xtensa_opcode xtensa_movi_opcode; | |
594 | static xtensa_opcode xtensa_movi_n_opcode; | |
595 | static xtensa_opcode xtensa_isync_opcode; | |
596 | static xtensa_opcode xtensa_j_opcode; | |
597 | static xtensa_opcode xtensa_jx_opcode; | |
598 | static xtensa_opcode xtensa_l32r_opcode; | |
599 | static xtensa_opcode xtensa_loop_opcode; | |
600 | static xtensa_opcode xtensa_loopnez_opcode; | |
601 | static xtensa_opcode xtensa_loopgtz_opcode; | |
602 | static xtensa_opcode xtensa_nop_opcode; | |
603 | static xtensa_opcode xtensa_nop_n_opcode; | |
604 | static xtensa_opcode xtensa_or_opcode; | |
605 | static xtensa_opcode xtensa_ret_opcode; | |
606 | static xtensa_opcode xtensa_ret_n_opcode; | |
607 | static xtensa_opcode xtensa_retw_opcode; | |
608 | static xtensa_opcode xtensa_retw_n_opcode; | |
609 | static xtensa_opcode xtensa_rsr_lcount_opcode; | |
610 | static xtensa_opcode xtensa_waiti_opcode; | |
611 | static int config_max_slots = 0; | |
612 | ||
613 | \f | |
614 | /* Command-line Options. */ | |
615 | ||
616 | bool use_literal_section = true; | |
617 | enum flix_level produce_flix = FLIX_ALL; | |
618 | static bool align_targets = true; | |
619 | static bool warn_unaligned_branch_targets = false; | |
620 | static bool has_a0_b_retw = false; | |
621 | static bool workaround_a0_b_retw = false; | |
622 | static bool workaround_b_j_loop_end = false; | |
623 | static bool workaround_short_loop = false; | |
624 | static bool maybe_has_short_loop = false; | |
625 | static bool workaround_close_loop_end = false; | |
626 | static bool maybe_has_close_loop_end = false; | |
627 | static bool enforce_three_byte_loop_align = false; | |
628 | static bool opt_linkrelax = true; | |
629 | ||
630 | /* When workaround_short_loops is TRUE, all loops with early exits must | |
631 | have at least 3 instructions. workaround_all_short_loops is a modifier | |
632 | to the workaround_short_loop flag. In addition to the | |
633 | workaround_short_loop actions, all straightline loopgtz and loopnez | |
634 | must have at least 3 instructions. */ | |
635 | ||
636 | static bool workaround_all_short_loops = false; | |
637 | ||
638 | /* Generate individual property section for every section. | |
639 | This option is defined in BDF library. */ | |
640 | extern bool elf32xtensa_separate_props; | |
641 | ||
642 | /* Xtensa ABI. | |
643 | This option is defined in BDF library. */ | |
644 | extern int elf32xtensa_abi; | |
645 | ||
646 | static void | |
647 | xtensa_setup_hw_workarounds (int earliest, int latest) | |
648 | { | |
649 | if (earliest > latest) | |
650 | as_fatal (_("illegal range of target hardware versions")); | |
651 | ||
652 | /* Enable all workarounds for pre-T1050.0 hardware. */ | |
653 | if (earliest < 105000 || latest < 105000) | |
654 | { | |
655 | workaround_a0_b_retw |= true; | |
656 | workaround_b_j_loop_end |= true; | |
657 | workaround_short_loop |= true; | |
658 | workaround_close_loop_end |= true; | |
659 | workaround_all_short_loops |= true; | |
660 | enforce_three_byte_loop_align = true; | |
661 | } | |
662 | } | |
663 | ||
664 | ||
665 | enum | |
666 | { | |
667 | option_density = OPTION_MD_BASE, | |
668 | option_no_density, | |
669 | ||
670 | option_flix, | |
671 | option_no_generate_flix, | |
672 | option_no_flix, | |
673 | ||
674 | option_relax, | |
675 | option_no_relax, | |
676 | ||
677 | option_link_relax, | |
678 | option_no_link_relax, | |
679 | ||
680 | option_generics, | |
681 | option_no_generics, | |
682 | ||
683 | option_transform, | |
684 | option_no_transform, | |
685 | ||
686 | option_text_section_literals, | |
687 | option_no_text_section_literals, | |
688 | ||
689 | option_absolute_literals, | |
690 | option_no_absolute_literals, | |
691 | ||
692 | option_align_targets, | |
693 | option_no_align_targets, | |
694 | ||
695 | option_warn_unaligned_targets, | |
696 | ||
697 | option_longcalls, | |
698 | option_no_longcalls, | |
699 | ||
700 | option_workaround_a0_b_retw, | |
701 | option_no_workaround_a0_b_retw, | |
702 | ||
703 | option_workaround_b_j_loop_end, | |
704 | option_no_workaround_b_j_loop_end, | |
705 | ||
706 | option_workaround_short_loop, | |
707 | option_no_workaround_short_loop, | |
708 | ||
709 | option_workaround_all_short_loops, | |
710 | option_no_workaround_all_short_loops, | |
711 | ||
712 | option_workaround_close_loop_end, | |
713 | option_no_workaround_close_loop_end, | |
714 | ||
715 | option_no_workarounds, | |
716 | ||
717 | option_rename_section_name, | |
718 | ||
719 | option_prefer_l32r, | |
720 | option_prefer_const16, | |
721 | ||
722 | option_target_hardware, | |
723 | ||
724 | option_trampolines, | |
725 | option_no_trampolines, | |
726 | ||
727 | option_auto_litpools, | |
728 | option_no_auto_litpools, | |
729 | option_auto_litpool_limit, | |
730 | ||
731 | option_separate_props, | |
732 | option_no_separate_props, | |
733 | ||
734 | option_abi_windowed, | |
735 | option_abi_call0, | |
736 | }; | |
737 | ||
738 | const char *md_shortopts = ""; | |
739 | ||
740 | struct option md_longopts[] = | |
741 | { | |
742 | { "density", no_argument, NULL, option_density }, | |
743 | { "no-density", no_argument, NULL, option_no_density }, | |
744 | ||
745 | { "flix", no_argument, NULL, option_flix }, | |
746 | { "no-generate-flix", no_argument, NULL, option_no_generate_flix }, | |
747 | { "no-allow-flix", no_argument, NULL, option_no_flix }, | |
748 | ||
749 | /* Both "relax" and "generics" are deprecated and treated as equivalent | |
750 | to the "transform" option. */ | |
751 | { "relax", no_argument, NULL, option_relax }, | |
752 | { "no-relax", no_argument, NULL, option_no_relax }, | |
753 | { "generics", no_argument, NULL, option_generics }, | |
754 | { "no-generics", no_argument, NULL, option_no_generics }, | |
755 | ||
756 | { "transform", no_argument, NULL, option_transform }, | |
757 | { "no-transform", no_argument, NULL, option_no_transform }, | |
758 | { "text-section-literals", no_argument, NULL, option_text_section_literals }, | |
759 | { "no-text-section-literals", no_argument, NULL, | |
760 | option_no_text_section_literals }, | |
761 | { "absolute-literals", no_argument, NULL, option_absolute_literals }, | |
762 | { "no-absolute-literals", no_argument, NULL, option_no_absolute_literals }, | |
763 | /* This option was changed from -align-target to -target-align | |
764 | because it conflicted with the "-al" option. */ | |
765 | { "target-align", no_argument, NULL, option_align_targets }, | |
766 | { "no-target-align", no_argument, NULL, option_no_align_targets }, | |
767 | { "warn-unaligned-targets", no_argument, NULL, | |
768 | option_warn_unaligned_targets }, | |
769 | { "longcalls", no_argument, NULL, option_longcalls }, | |
770 | { "no-longcalls", no_argument, NULL, option_no_longcalls }, | |
771 | ||
772 | { "no-workaround-a0-b-retw", no_argument, NULL, | |
773 | option_no_workaround_a0_b_retw }, | |
774 | { "workaround-a0-b-retw", no_argument, NULL, option_workaround_a0_b_retw }, | |
775 | ||
776 | { "no-workaround-b-j-loop-end", no_argument, NULL, | |
777 | option_no_workaround_b_j_loop_end }, | |
778 | { "workaround-b-j-loop-end", no_argument, NULL, | |
779 | option_workaround_b_j_loop_end }, | |
780 | ||
781 | { "no-workaround-short-loops", no_argument, NULL, | |
782 | option_no_workaround_short_loop }, | |
783 | { "workaround-short-loops", no_argument, NULL, | |
784 | option_workaround_short_loop }, | |
785 | ||
786 | { "no-workaround-all-short-loops", no_argument, NULL, | |
787 | option_no_workaround_all_short_loops }, | |
788 | { "workaround-all-short-loop", no_argument, NULL, | |
789 | option_workaround_all_short_loops }, | |
790 | ||
791 | { "prefer-l32r", no_argument, NULL, option_prefer_l32r }, | |
792 | { "prefer-const16", no_argument, NULL, option_prefer_const16 }, | |
793 | ||
794 | { "no-workarounds", no_argument, NULL, option_no_workarounds }, | |
795 | ||
796 | { "no-workaround-close-loop-end", no_argument, NULL, | |
797 | option_no_workaround_close_loop_end }, | |
798 | { "workaround-close-loop-end", no_argument, NULL, | |
799 | option_workaround_close_loop_end }, | |
800 | ||
801 | { "rename-section", required_argument, NULL, option_rename_section_name }, | |
802 | ||
803 | { "link-relax", no_argument, NULL, option_link_relax }, | |
804 | { "no-link-relax", no_argument, NULL, option_no_link_relax }, | |
805 | ||
806 | { "target-hardware", required_argument, NULL, option_target_hardware }, | |
807 | ||
808 | { "trampolines", no_argument, NULL, option_trampolines }, | |
809 | { "no-trampolines", no_argument, NULL, option_no_trampolines }, | |
810 | ||
811 | { "auto-litpools", no_argument, NULL, option_auto_litpools }, | |
812 | { "no-auto-litpools", no_argument, NULL, option_no_auto_litpools }, | |
813 | { "auto-litpool-limit", required_argument, NULL, option_auto_litpool_limit }, | |
814 | ||
815 | { "separate-prop-tables", no_argument, NULL, option_separate_props }, | |
816 | ||
817 | { "abi-windowed", no_argument, NULL, option_abi_windowed }, | |
818 | { "abi-call0", no_argument, NULL, option_abi_call0 }, | |
819 | ||
820 | { NULL, no_argument, NULL, 0 } | |
821 | }; | |
822 | ||
823 | size_t md_longopts_size = sizeof md_longopts; | |
824 | ||
825 | ||
826 | int | |
827 | md_parse_option (int c, const char *arg) | |
828 | { | |
829 | switch (c) | |
830 | { | |
831 | case option_density: | |
832 | as_warn (_("--density option is ignored")); | |
833 | return 1; | |
834 | case option_no_density: | |
835 | as_warn (_("--no-density option is ignored")); | |
836 | return 1; | |
837 | case option_link_relax: | |
838 | opt_linkrelax = true; | |
839 | return 1; | |
840 | case option_no_link_relax: | |
841 | opt_linkrelax = false; | |
842 | return 1; | |
843 | case option_flix: | |
844 | produce_flix = FLIX_ALL; | |
845 | return 1; | |
846 | case option_no_generate_flix: | |
847 | produce_flix = FLIX_NO_GENERATE; | |
848 | return 1; | |
849 | case option_no_flix: | |
850 | produce_flix = FLIX_NONE; | |
851 | return 1; | |
852 | case option_generics: | |
853 | as_warn (_("--generics is deprecated; use --transform instead")); | |
854 | return md_parse_option (option_transform, arg); | |
855 | case option_no_generics: | |
856 | as_warn (_("--no-generics is deprecated; use --no-transform instead")); | |
857 | return md_parse_option (option_no_transform, arg); | |
858 | case option_relax: | |
859 | as_warn (_("--relax is deprecated; use --transform instead")); | |
860 | return md_parse_option (option_transform, arg); | |
861 | case option_no_relax: | |
862 | as_warn (_("--no-relax is deprecated; use --no-transform instead")); | |
863 | return md_parse_option (option_no_transform, arg); | |
864 | case option_longcalls: | |
865 | directive_state[directive_longcalls] = true; | |
866 | return 1; | |
867 | case option_no_longcalls: | |
868 | directive_state[directive_longcalls] = false; | |
869 | return 1; | |
870 | case option_text_section_literals: | |
871 | use_literal_section = false; | |
872 | return 1; | |
873 | case option_no_text_section_literals: | |
874 | use_literal_section = true; | |
875 | return 1; | |
876 | case option_absolute_literals: | |
877 | if (!absolute_literals_supported) | |
878 | { | |
879 | as_fatal (_("--absolute-literals option not supported in this Xtensa configuration")); | |
880 | return 0; | |
881 | } | |
882 | directive_state[directive_absolute_literals] = true; | |
883 | return 1; | |
884 | case option_no_absolute_literals: | |
885 | directive_state[directive_absolute_literals] = false; | |
886 | return 1; | |
887 | ||
888 | case option_workaround_a0_b_retw: | |
889 | workaround_a0_b_retw = true; | |
890 | return 1; | |
891 | case option_no_workaround_a0_b_retw: | |
892 | workaround_a0_b_retw = false; | |
893 | return 1; | |
894 | case option_workaround_b_j_loop_end: | |
895 | workaround_b_j_loop_end = true; | |
896 | return 1; | |
897 | case option_no_workaround_b_j_loop_end: | |
898 | workaround_b_j_loop_end = false; | |
899 | return 1; | |
900 | ||
901 | case option_workaround_short_loop: | |
902 | workaround_short_loop = true; | |
903 | return 1; | |
904 | case option_no_workaround_short_loop: | |
905 | workaround_short_loop = false; | |
906 | return 1; | |
907 | ||
908 | case option_workaround_all_short_loops: | |
909 | workaround_all_short_loops = true; | |
910 | return 1; | |
911 | case option_no_workaround_all_short_loops: | |
912 | workaround_all_short_loops = false; | |
913 | return 1; | |
914 | ||
915 | case option_workaround_close_loop_end: | |
916 | workaround_close_loop_end = true; | |
917 | return 1; | |
918 | case option_no_workaround_close_loop_end: | |
919 | workaround_close_loop_end = false; | |
920 | return 1; | |
921 | ||
922 | case option_no_workarounds: | |
923 | workaround_a0_b_retw = false; | |
924 | workaround_b_j_loop_end = false; | |
925 | workaround_short_loop = false; | |
926 | workaround_all_short_loops = false; | |
927 | workaround_close_loop_end = false; | |
928 | return 1; | |
929 | ||
930 | case option_align_targets: | |
931 | align_targets = true; | |
932 | return 1; | |
933 | case option_no_align_targets: | |
934 | align_targets = false; | |
935 | return 1; | |
936 | ||
937 | case option_warn_unaligned_targets: | |
938 | warn_unaligned_branch_targets = true; | |
939 | return 1; | |
940 | ||
941 | case option_rename_section_name: | |
942 | build_section_rename (arg); | |
943 | return 1; | |
944 | ||
945 | case 'Q': | |
946 | /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section | |
947 | should be emitted or not. FIXME: Not implemented. */ | |
948 | return 1; | |
949 | ||
950 | case option_prefer_l32r: | |
951 | if (prefer_const16) | |
952 | as_fatal (_("prefer-l32r conflicts with prefer-const16")); | |
953 | prefer_l32r = 1; | |
954 | return 1; | |
955 | ||
956 | case option_prefer_const16: | |
957 | if (prefer_l32r) | |
958 | as_fatal (_("prefer-const16 conflicts with prefer-l32r")); | |
959 | prefer_const16 = 1; | |
960 | return 1; | |
961 | ||
962 | case option_target_hardware: | |
963 | { | |
964 | int earliest, latest = 0; | |
965 | char *end; | |
966 | if (*arg == 0 || *arg == '-') | |
967 | as_fatal (_("invalid target hardware version")); | |
968 | ||
969 | earliest = strtol (arg, &end, 0); | |
970 | ||
971 | if (*end == 0) | |
972 | latest = earliest; | |
973 | else if (*end == '-') | |
974 | { | |
975 | if (*++end == 0) | |
976 | as_fatal (_("invalid target hardware version")); | |
977 | latest = strtol (end, &end, 0); | |
978 | } | |
979 | if (*end != 0) | |
980 | as_fatal (_("invalid target hardware version")); | |
981 | ||
982 | xtensa_setup_hw_workarounds (earliest, latest); | |
983 | return 1; | |
984 | } | |
985 | ||
986 | case option_transform: | |
987 | /* This option has no affect other than to use the defaults, | |
988 | which are already set. */ | |
989 | return 1; | |
990 | ||
991 | case option_no_transform: | |
992 | /* This option turns off all transformations of any kind. | |
993 | However, because we want to preserve the state of other | |
994 | directives, we only change its own field. Thus, before | |
995 | you perform any transformation, always check if transform | |
996 | is available. If you use the functions we provide for this | |
997 | purpose, you will be ok. */ | |
998 | directive_state[directive_transform] = false; | |
999 | return 1; | |
1000 | ||
1001 | case option_trampolines: | |
1002 | use_trampolines = true; | |
1003 | return 1; | |
1004 | ||
1005 | case option_no_trampolines: | |
1006 | use_trampolines = false; | |
1007 | return 1; | |
1008 | ||
1009 | case option_auto_litpools: | |
1010 | auto_litpools = true; | |
1011 | use_literal_section = false; | |
1012 | if (auto_litpool_limit <= 0) | |
1013 | auto_litpool_limit = MAX_AUTO_POOL_LITERALS / 2; | |
1014 | return 1; | |
1015 | ||
1016 | case option_no_auto_litpools: | |
1017 | auto_litpools = false; | |
1018 | auto_litpool_limit = -1; | |
1019 | return 1; | |
1020 | ||
1021 | case option_auto_litpool_limit: | |
1022 | { | |
1023 | int value = 0; | |
1024 | char *end; | |
1025 | if (auto_litpool_limit < 0) | |
1026 | as_fatal (_("no-auto-litpools is incompatible with auto-litpool-limit")); | |
1027 | if (*arg == 0 || *arg == '-') | |
1028 | as_fatal (_("invalid auto-litpool-limit argument")); | |
1029 | value = strtol (arg, &end, 10); | |
1030 | if (*end != 0) | |
1031 | as_fatal (_("invalid auto-litpool-limit argument")); | |
1032 | if (value < 100 || value > 10000) | |
1033 | as_fatal (_("invalid auto-litpool-limit argument (range is 100-10000)")); | |
1034 | auto_litpool_limit = value; | |
1035 | auto_litpools = true; | |
1036 | use_literal_section = false; | |
1037 | return 1; | |
1038 | } | |
1039 | ||
1040 | case option_separate_props: | |
1041 | elf32xtensa_separate_props = true; | |
1042 | return 1; | |
1043 | ||
1044 | case option_no_separate_props: | |
1045 | elf32xtensa_separate_props = false; | |
1046 | return 1; | |
1047 | ||
1048 | case option_abi_windowed: | |
1049 | elf32xtensa_abi = XTHAL_ABI_WINDOWED; | |
1050 | return 1; | |
1051 | ||
1052 | case option_abi_call0: | |
1053 | elf32xtensa_abi = XTHAL_ABI_CALL0; | |
1054 | return 1; | |
1055 | ||
1056 | default: | |
1057 | return 0; | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | ||
1062 | void | |
1063 | md_show_usage (FILE *stream) | |
1064 | { | |
1065 | fputs ("\n\ | |
1066 | Xtensa options:\n\ | |
1067 | --[no-]text-section-literals\n\ | |
1068 | [Do not] put literals in the text section\n\ | |
1069 | --[no-]absolute-literals\n\ | |
1070 | [Do not] default to use non-PC-relative literals\n\ | |
1071 | --[no-]target-align [Do not] try to align branch targets\n\ | |
1072 | --[no-]longcalls [Do not] emit 32-bit call sequences\n\ | |
1073 | --[no-]transform [Do not] transform instructions\n\ | |
1074 | --flix both allow hand-written and generate flix bundles\n\ | |
1075 | --no-generate-flix allow hand-written but do not generate\n\ | |
1076 | flix bundles\n\ | |
1077 | --no-allow-flix neither allow hand-written nor generate\n\ | |
1078 | flix bundles\n\ | |
1079 | --rename-section old=new Rename section 'old' to 'new'\n\ | |
1080 | --[no-]trampolines [Do not] generate trampolines (jumps to jumps)\n\ | |
1081 | when jumps do not reach their targets\n\ | |
1082 | --[no-]auto-litpools [Do not] automatically create literal pools\n\ | |
1083 | --auto-litpool-limit=<value>\n\ | |
1084 | (range 100-10000) Maximum number of blocks of\n\ | |
1085 | instructions to emit between literal pool\n\ | |
1086 | locations; implies --auto-litpools flag\n\ | |
1087 | --[no-]separate-prop-tables\n\ | |
1088 | [Do not] place Xtensa property records into\n\ | |
1089 | individual property sections for each section.\n\ | |
1090 | Default is to generate single property section.\n", stream); | |
1091 | } | |
1092 | ||
1093 | \f | |
1094 | /* Functions related to the list of current label symbols. */ | |
1095 | ||
1096 | static void | |
1097 | xtensa_add_insn_label (symbolS *sym) | |
1098 | { | |
1099 | sym_list *l; | |
1100 | ||
1101 | if (!free_insn_labels) | |
1102 | l = XNEW (sym_list); | |
1103 | else | |
1104 | { | |
1105 | l = free_insn_labels; | |
1106 | free_insn_labels = l->next; | |
1107 | } | |
1108 | ||
1109 | l->sym = sym; | |
1110 | l->next = insn_labels; | |
1111 | insn_labels = l; | |
1112 | } | |
1113 | ||
1114 | ||
1115 | static void | |
1116 | xtensa_clear_insn_labels (void) | |
1117 | { | |
1118 | sym_list **pl; | |
1119 | ||
1120 | for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next) | |
1121 | ; | |
1122 | *pl = insn_labels; | |
1123 | insn_labels = NULL; | |
1124 | } | |
1125 | ||
1126 | ||
1127 | static void | |
1128 | xtensa_move_labels (fragS *new_frag, valueT new_offset) | |
1129 | { | |
1130 | sym_list *lit; | |
1131 | ||
1132 | for (lit = insn_labels; lit; lit = lit->next) | |
1133 | { | |
1134 | symbolS *lit_sym = lit->sym; | |
1135 | S_SET_VALUE (lit_sym, new_offset); | |
1136 | symbol_set_frag (lit_sym, new_frag); | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | \f | |
1141 | /* Directive data and functions. */ | |
1142 | ||
1143 | typedef struct state_stackS_struct | |
1144 | { | |
1145 | directiveE directive; | |
1146 | bool negated; | |
1147 | bool old_state; | |
1148 | const char *file; | |
1149 | unsigned int line; | |
1150 | const void *datum; | |
1151 | struct state_stackS_struct *prev; | |
1152 | } state_stackS; | |
1153 | ||
1154 | state_stackS *directive_state_stack; | |
1155 | ||
1156 | const pseudo_typeS md_pseudo_table[] = | |
1157 | { | |
1158 | { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0). */ | |
1159 | { "literal_position", xtensa_literal_position, 0 }, | |
1160 | { "frame", s_ignore, 0 }, /* Formerly used for STABS debugging. */ | |
1161 | { "long", xtensa_elf_cons, 4 }, | |
1162 | { "word", xtensa_elf_cons, 4 }, | |
1163 | { "4byte", xtensa_elf_cons, 4 }, | |
1164 | { "short", xtensa_elf_cons, 2 }, | |
1165 | { "2byte", xtensa_elf_cons, 2 }, | |
1166 | { "sleb128", xtensa_leb128, 1}, | |
1167 | { "uleb128", xtensa_leb128, 0}, | |
1168 | { "begin", xtensa_begin_directive, 0 }, | |
1169 | { "end", xtensa_end_directive, 0 }, | |
1170 | { "literal", xtensa_literal_pseudo, 0 }, | |
1171 | { "frequency", xtensa_frequency_pseudo, 0 }, | |
1172 | { NULL, 0, 0 }, | |
1173 | }; | |
1174 | ||
1175 | ||
1176 | static bool | |
1177 | use_transform (void) | |
1178 | { | |
1179 | /* After md_finish, you should be checking frag by frag, rather | |
1180 | than state directives. */ | |
1181 | gas_assert (!past_xtensa_md_finish); | |
1182 | return directive_state[directive_transform]; | |
1183 | } | |
1184 | ||
1185 | ||
1186 | static bool | |
1187 | do_align_targets (void) | |
1188 | { | |
1189 | /* Do not use this function after md_finish; just look at align_targets | |
1190 | instead. There is no target-align directive, so alignment is either | |
1191 | enabled for all frags or not done at all. */ | |
1192 | gas_assert (!past_xtensa_md_finish); | |
1193 | return align_targets && use_transform (); | |
1194 | } | |
1195 | ||
1196 | ||
1197 | static void | |
1198 | directive_push (directiveE directive, bool negated, const void *datum) | |
1199 | { | |
1200 | const char *file; | |
1201 | unsigned int line; | |
1202 | state_stackS *stack = XNEW (state_stackS); | |
1203 | ||
1204 | file = as_where (&line); | |
1205 | ||
1206 | stack->directive = directive; | |
1207 | stack->negated = negated; | |
1208 | stack->old_state = directive_state[directive]; | |
1209 | stack->file = file; | |
1210 | stack->line = line; | |
1211 | stack->datum = datum; | |
1212 | stack->prev = directive_state_stack; | |
1213 | directive_state_stack = stack; | |
1214 | ||
1215 | directive_state[directive] = !negated; | |
1216 | } | |
1217 | ||
1218 | ||
1219 | static void | |
1220 | directive_pop (directiveE *directive, | |
1221 | bool *negated, | |
1222 | const char **file, | |
1223 | unsigned int *line, | |
1224 | const void **datum) | |
1225 | { | |
1226 | state_stackS *top = directive_state_stack; | |
1227 | ||
1228 | if (!directive_state_stack) | |
1229 | { | |
1230 | as_bad (_("unmatched .end directive")); | |
1231 | *directive = directive_none; | |
1232 | return; | |
1233 | } | |
1234 | ||
1235 | directive_state[directive_state_stack->directive] = top->old_state; | |
1236 | *directive = top->directive; | |
1237 | *negated = top->negated; | |
1238 | *file = top->file; | |
1239 | *line = top->line; | |
1240 | *datum = top->datum; | |
1241 | directive_state_stack = top->prev; | |
1242 | free (top); | |
1243 | } | |
1244 | ||
1245 | ||
1246 | static void | |
1247 | directive_balance (void) | |
1248 | { | |
1249 | while (directive_state_stack) | |
1250 | { | |
1251 | directiveE directive; | |
1252 | bool negated; | |
1253 | const char *file; | |
1254 | unsigned int line; | |
1255 | const void *datum; | |
1256 | ||
1257 | directive_pop (&directive, &negated, &file, &line, &datum); | |
1258 | as_warn_where ((char *) file, line, | |
1259 | _(".begin directive with no matching .end directive")); | |
1260 | } | |
1261 | } | |
1262 | ||
1263 | ||
1264 | static bool | |
1265 | inside_directive (directiveE dir) | |
1266 | { | |
1267 | state_stackS *top = directive_state_stack; | |
1268 | ||
1269 | while (top && top->directive != dir) | |
1270 | top = top->prev; | |
1271 | ||
1272 | return (top != NULL); | |
1273 | } | |
1274 | ||
1275 | ||
1276 | static void | |
1277 | get_directive (directiveE *directive, bool *negated) | |
1278 | { | |
1279 | int len; | |
1280 | unsigned i; | |
1281 | const char *directive_string; | |
1282 | ||
1283 | if (!startswith (input_line_pointer, "no-")) | |
1284 | *negated = false; | |
1285 | else | |
1286 | { | |
1287 | *negated = true; | |
1288 | input_line_pointer += 3; | |
1289 | } | |
1290 | ||
1291 | len = strspn (input_line_pointer, | |
1292 | "abcdefghijklmnopqrstuvwxyz_-/0123456789."); | |
1293 | ||
1294 | /* This code is a hack to make .begin [no-][generics|relax] exactly | |
1295 | equivalent to .begin [no-]transform. We should remove it when | |
1296 | we stop accepting those options. */ | |
1297 | ||
1298 | if (startswith (input_line_pointer, "generics")) | |
1299 | { | |
1300 | as_warn (_("[no-]generics is deprecated; use [no-]transform instead")); | |
1301 | directive_string = "transform"; | |
1302 | } | |
1303 | else if (startswith (input_line_pointer, "relax")) | |
1304 | { | |
1305 | as_warn (_("[no-]relax is deprecated; use [no-]transform instead")); | |
1306 | directive_string = "transform"; | |
1307 | } | |
1308 | else | |
1309 | directive_string = input_line_pointer; | |
1310 | ||
1311 | for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i) | |
1312 | { | |
1313 | if (strncmp (directive_string, directive_info[i].name, len) == 0) | |
1314 | { | |
1315 | input_line_pointer += len; | |
1316 | *directive = (directiveE) i; | |
1317 | if (*negated && !directive_info[i].can_be_negated) | |
1318 | as_bad (_("directive %s cannot be negated"), | |
1319 | directive_info[i].name); | |
1320 | return; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | as_bad (_("unknown directive")); | |
1325 | *directive = (directiveE) XTENSA_UNDEFINED; | |
1326 | } | |
1327 | ||
1328 | ||
1329 | static void | |
1330 | xtensa_begin_directive (int ignore ATTRIBUTE_UNUSED) | |
1331 | { | |
1332 | directiveE directive; | |
1333 | bool negated; | |
1334 | emit_state *state; | |
1335 | lit_state *ls; | |
1336 | ||
1337 | get_directive (&directive, &negated); | |
1338 | if (directive == (directiveE) XTENSA_UNDEFINED) | |
1339 | { | |
1340 | discard_rest_of_line (); | |
1341 | return; | |
1342 | } | |
1343 | ||
1344 | if (cur_vinsn.inside_bundle) | |
1345 | as_bad (_("directives are not valid inside bundles")); | |
1346 | ||
1347 | switch (directive) | |
1348 | { | |
1349 | case directive_literal: | |
1350 | if (!inside_directive (directive_literal)) | |
1351 | { | |
1352 | /* Previous labels go with whatever follows this directive, not with | |
1353 | the literal, so save them now. */ | |
1354 | saved_insn_labels = insn_labels; | |
1355 | insn_labels = NULL; | |
1356 | } | |
1357 | as_warn (_(".begin literal is deprecated; use .literal instead")); | |
1358 | state = XNEW (emit_state); | |
1359 | xtensa_switch_to_literal_fragment (state); | |
1360 | directive_push (directive_literal, negated, state); | |
1361 | break; | |
1362 | ||
1363 | case directive_literal_prefix: | |
1364 | /* Have to flush pending output because a movi relaxed to an l32r | |
1365 | might produce a literal. */ | |
1366 | md_flush_pending_output (); | |
1367 | /* Check to see if the current fragment is a literal | |
1368 | fragment. If it is, then this operation is not allowed. */ | |
1369 | if (generating_literals) | |
1370 | { | |
1371 | as_bad (_("cannot set literal_prefix inside literal fragment")); | |
1372 | return; | |
1373 | } | |
1374 | ||
1375 | /* Allocate the literal state for this section and push | |
1376 | onto the directive stack. */ | |
1377 | ls = XNEW (lit_state); | |
1378 | gas_assert (ls); | |
1379 | ||
1380 | *ls = default_lit_sections; | |
1381 | directive_push (directive_literal_prefix, negated, ls); | |
1382 | ||
1383 | /* Process the new prefix. */ | |
1384 | xtensa_literal_prefix (); | |
1385 | break; | |
1386 | ||
1387 | case directive_freeregs: | |
1388 | /* This information is currently unused, but we'll accept the statement | |
1389 | and just discard the rest of the line. This won't check the syntax, | |
1390 | but it will accept every correct freeregs directive. */ | |
1391 | input_line_pointer += strcspn (input_line_pointer, "\n"); | |
1392 | directive_push (directive_freeregs, negated, 0); | |
1393 | break; | |
1394 | ||
1395 | case directive_schedule: | |
1396 | md_flush_pending_output (); | |
1397 | frag_var (rs_fill, 0, 0, frag_now->fr_subtype, | |
1398 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
1399 | directive_push (directive_schedule, negated, 0); | |
1400 | xtensa_set_frag_assembly_state (frag_now); | |
1401 | break; | |
1402 | ||
1403 | case directive_density: | |
1404 | as_warn (_(".begin [no-]density is ignored")); | |
1405 | break; | |
1406 | ||
1407 | case directive_absolute_literals: | |
1408 | md_flush_pending_output (); | |
1409 | if (!absolute_literals_supported && !negated) | |
1410 | { | |
1411 | as_warn (_("Xtensa absolute literals option not supported; ignored")); | |
1412 | break; | |
1413 | } | |
1414 | xtensa_set_frag_assembly_state (frag_now); | |
1415 | directive_push (directive, negated, 0); | |
1416 | break; | |
1417 | ||
1418 | default: | |
1419 | md_flush_pending_output (); | |
1420 | xtensa_set_frag_assembly_state (frag_now); | |
1421 | directive_push (directive, negated, 0); | |
1422 | break; | |
1423 | } | |
1424 | ||
1425 | demand_empty_rest_of_line (); | |
1426 | } | |
1427 | ||
1428 | ||
1429 | static void | |
1430 | xtensa_end_directive (int ignore ATTRIBUTE_UNUSED) | |
1431 | { | |
1432 | directiveE begin_directive, end_directive; | |
1433 | bool begin_negated, end_negated; | |
1434 | const char *file; | |
1435 | unsigned int line; | |
1436 | emit_state *state; | |
1437 | emit_state **state_ptr; | |
1438 | lit_state *s; | |
1439 | ||
1440 | if (cur_vinsn.inside_bundle) | |
1441 | as_bad (_("directives are not valid inside bundles")); | |
1442 | ||
1443 | get_directive (&end_directive, &end_negated); | |
1444 | ||
1445 | md_flush_pending_output (); | |
1446 | ||
1447 | switch ((int) end_directive) | |
1448 | { | |
1449 | case XTENSA_UNDEFINED: | |
1450 | discard_rest_of_line (); | |
1451 | return; | |
1452 | ||
1453 | case (int) directive_density: | |
1454 | as_warn (_(".end [no-]density is ignored")); | |
1455 | demand_empty_rest_of_line (); | |
1456 | break; | |
1457 | ||
1458 | case (int) directive_absolute_literals: | |
1459 | if (!absolute_literals_supported && !end_negated) | |
1460 | { | |
1461 | as_warn (_("Xtensa absolute literals option not supported; ignored")); | |
1462 | demand_empty_rest_of_line (); | |
1463 | return; | |
1464 | } | |
1465 | break; | |
1466 | ||
1467 | default: | |
1468 | break; | |
1469 | } | |
1470 | ||
1471 | state_ptr = &state; /* use state_ptr to avoid type-punning warning */ | |
1472 | directive_pop (&begin_directive, &begin_negated, &file, &line, | |
1473 | (const void **) state_ptr); | |
1474 | ||
1475 | if (begin_directive != directive_none) | |
1476 | { | |
1477 | if (begin_directive != end_directive || begin_negated != end_negated) | |
1478 | { | |
1479 | as_bad (_("does not match begin %s%s at %s:%d"), | |
1480 | begin_negated ? "no-" : "", | |
1481 | directive_info[begin_directive].name, file, line); | |
1482 | } | |
1483 | else | |
1484 | { | |
1485 | switch (end_directive) | |
1486 | { | |
1487 | case directive_literal: | |
1488 | frag_var (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
1489 | xtensa_restore_emit_state (state); | |
1490 | xtensa_set_frag_assembly_state (frag_now); | |
1491 | free (state); | |
1492 | if (!inside_directive (directive_literal)) | |
1493 | { | |
1494 | /* Restore the list of current labels. */ | |
1495 | xtensa_clear_insn_labels (); | |
1496 | insn_labels = saved_insn_labels; | |
1497 | } | |
1498 | break; | |
1499 | ||
1500 | case directive_literal_prefix: | |
1501 | /* Restore the default collection sections from saved state. */ | |
1502 | s = (lit_state *) state; | |
1503 | gas_assert (s); | |
1504 | default_lit_sections = *s; | |
1505 | ||
1506 | /* Free the state storage. */ | |
1507 | free (s->lit_prefix); | |
1508 | free (s); | |
1509 | break; | |
1510 | ||
1511 | case directive_schedule: | |
1512 | case directive_freeregs: | |
1513 | break; | |
1514 | ||
1515 | default: | |
1516 | xtensa_set_frag_assembly_state (frag_now); | |
1517 | break; | |
1518 | } | |
1519 | } | |
1520 | } | |
1521 | ||
1522 | demand_empty_rest_of_line (); | |
1523 | } | |
1524 | ||
1525 | ||
1526 | /* Place an aligned literal fragment at the current location. */ | |
1527 | ||
1528 | static void | |
1529 | xtensa_literal_position (int ignore ATTRIBUTE_UNUSED) | |
1530 | { | |
1531 | md_flush_pending_output (); | |
1532 | ||
1533 | if (inside_directive (directive_literal)) | |
1534 | as_warn (_(".literal_position inside literal directive; ignoring")); | |
1535 | xtensa_mark_literal_pool_location (); | |
1536 | ||
1537 | demand_empty_rest_of_line (); | |
1538 | xtensa_clear_insn_labels (); | |
1539 | } | |
1540 | ||
1541 | ||
1542 | /* Support .literal label, expr, ... */ | |
1543 | ||
1544 | static void | |
1545 | xtensa_literal_pseudo (int ignored ATTRIBUTE_UNUSED) | |
1546 | { | |
1547 | emit_state state; | |
1548 | char *p, *base_name; | |
1549 | char c; | |
1550 | ||
1551 | if (inside_directive (directive_literal)) | |
1552 | { | |
1553 | as_bad (_(".literal not allowed inside .begin literal region")); | |
1554 | ignore_rest_of_line (); | |
1555 | return; | |
1556 | } | |
1557 | ||
1558 | md_flush_pending_output (); | |
1559 | ||
1560 | /* Previous labels go with whatever follows this directive, not with | |
1561 | the literal, so save them now. */ | |
1562 | saved_insn_labels = insn_labels; | |
1563 | insn_labels = NULL; | |
1564 | ||
1565 | base_name = input_line_pointer; | |
1566 | ||
1567 | xtensa_switch_to_literal_fragment (&state); | |
1568 | ||
1569 | /* All literals are aligned to four-byte boundaries. */ | |
1570 | frag_align (2, 0, 0); | |
1571 | record_alignment (now_seg, 2); | |
1572 | ||
1573 | c = get_symbol_name (&base_name); | |
1574 | /* Just after name is now '\0'. */ | |
1575 | p = input_line_pointer; | |
1576 | *p = c; | |
1577 | SKIP_WHITESPACE_AFTER_NAME (); | |
1578 | ||
1579 | if (*input_line_pointer != ',' && *input_line_pointer != ':') | |
1580 | { | |
1581 | as_bad (_("expected comma or colon after symbol name; " | |
1582 | "rest of line ignored")); | |
1583 | ignore_rest_of_line (); | |
1584 | xtensa_restore_emit_state (&state); | |
1585 | return; | |
1586 | } | |
1587 | ||
1588 | *p = 0; | |
1589 | colon (base_name); | |
1590 | *p = c; | |
1591 | ||
1592 | input_line_pointer++; /* skip ',' or ':' */ | |
1593 | ||
1594 | xtensa_elf_cons (4); | |
1595 | ||
1596 | xtensa_restore_emit_state (&state); | |
1597 | ||
1598 | /* Restore the list of current labels. */ | |
1599 | xtensa_clear_insn_labels (); | |
1600 | insn_labels = saved_insn_labels; | |
1601 | } | |
1602 | ||
1603 | ||
1604 | static void | |
1605 | xtensa_literal_prefix (void) | |
1606 | { | |
1607 | char *name; | |
1608 | int len; | |
1609 | ||
1610 | /* Parse the new prefix from the input_line_pointer. */ | |
1611 | SKIP_WHITESPACE (); | |
1612 | len = strspn (input_line_pointer, | |
1613 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
1614 | "abcdefghijklmnopqrstuvwxyz_/0123456789.$"); | |
1615 | ||
1616 | /* Get a null-terminated copy of the name. */ | |
1617 | name = xmemdup0 (input_line_pointer, len); | |
1618 | ||
1619 | /* Skip the name in the input line. */ | |
1620 | input_line_pointer += len; | |
1621 | ||
1622 | default_lit_sections.lit_prefix = name; | |
1623 | ||
1624 | /* Clear cached literal sections, since the prefix has changed. */ | |
1625 | default_lit_sections.lit_seg = NULL; | |
1626 | default_lit_sections.lit4_seg = NULL; | |
1627 | } | |
1628 | ||
1629 | ||
1630 | /* Support ".frequency branch_target_frequency fall_through_frequency". */ | |
1631 | ||
1632 | static void | |
1633 | xtensa_frequency_pseudo (int ignored ATTRIBUTE_UNUSED) | |
1634 | { | |
1635 | float fall_through_f, target_f; | |
1636 | ||
1637 | fall_through_f = (float) strtod (input_line_pointer, &input_line_pointer); | |
1638 | if (fall_through_f < 0) | |
1639 | { | |
1640 | as_bad (_("fall through frequency must be greater than 0")); | |
1641 | ignore_rest_of_line (); | |
1642 | return; | |
1643 | } | |
1644 | ||
1645 | target_f = (float) strtod (input_line_pointer, &input_line_pointer); | |
1646 | if (target_f < 0) | |
1647 | { | |
1648 | as_bad (_("branch target frequency must be greater than 0")); | |
1649 | ignore_rest_of_line (); | |
1650 | return; | |
1651 | } | |
1652 | ||
1653 | set_subseg_freq (now_seg, now_subseg, target_f + fall_through_f, target_f); | |
1654 | ||
1655 | demand_empty_rest_of_line (); | |
1656 | } | |
1657 | ||
1658 | ||
1659 | /* Like normal .long/.short/.word, except support @plt, etc. | |
1660 | Clobbers input_line_pointer, checks end-of-line. */ | |
1661 | ||
1662 | static void | |
1663 | xtensa_elf_cons (int nbytes) | |
1664 | { | |
1665 | expressionS exp; | |
1666 | bfd_reloc_code_real_type reloc; | |
1667 | ||
1668 | md_flush_pending_output (); | |
1669 | ||
1670 | if (cur_vinsn.inside_bundle) | |
1671 | as_bad (_("directives are not valid inside bundles")); | |
1672 | ||
1673 | if (is_it_end_of_statement ()) | |
1674 | { | |
1675 | demand_empty_rest_of_line (); | |
1676 | return; | |
1677 | } | |
1678 | ||
1679 | do | |
1680 | { | |
1681 | expression (&exp); | |
1682 | if (exp.X_op == O_symbol | |
1683 | && *input_line_pointer == '@' | |
1684 | && ((reloc = xtensa_elf_suffix (&input_line_pointer, &exp)) | |
1685 | != BFD_RELOC_NONE)) | |
1686 | { | |
1687 | reloc_howto_type *reloc_howto = | |
1688 | bfd_reloc_type_lookup (stdoutput, reloc); | |
1689 | ||
1690 | if (reloc == BFD_RELOC_UNUSED || !reloc_howto) | |
1691 | as_bad (_("unsupported relocation")); | |
1692 | else if ((reloc >= BFD_RELOC_XTENSA_SLOT0_OP | |
1693 | && reloc <= BFD_RELOC_XTENSA_SLOT14_OP) | |
1694 | || (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT | |
1695 | && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT)) | |
1696 | as_bad (_("opcode-specific %s relocation used outside " | |
1697 | "an instruction"), reloc_howto->name); | |
1698 | else if (nbytes != (int) bfd_get_reloc_size (reloc_howto)) | |
1699 | as_bad (ngettext ("%s relocations do not fit in %d byte", | |
1700 | "%s relocations do not fit in %d bytes", | |
1701 | nbytes), | |
1702 | reloc_howto->name, nbytes); | |
1703 | else if (reloc == BFD_RELOC_XTENSA_TLS_FUNC | |
1704 | || reloc == BFD_RELOC_XTENSA_TLS_ARG | |
1705 | || reloc == BFD_RELOC_XTENSA_TLS_CALL) | |
1706 | as_bad (_("invalid use of %s relocation"), reloc_howto->name); | |
1707 | else | |
1708 | { | |
1709 | char *p = frag_more ((int) nbytes); | |
1710 | xtensa_set_frag_assembly_state (frag_now); | |
1711 | fix_new_exp (frag_now, p - frag_now->fr_literal, | |
1712 | nbytes, &exp, reloc_howto->pc_relative, reloc); | |
1713 | } | |
1714 | } | |
1715 | else | |
1716 | { | |
1717 | xtensa_set_frag_assembly_state (frag_now); | |
1718 | emit_expr (&exp, (unsigned int) nbytes); | |
1719 | } | |
1720 | } | |
1721 | while (*input_line_pointer++ == ','); | |
1722 | ||
1723 | input_line_pointer--; /* Put terminator back into stream. */ | |
1724 | demand_empty_rest_of_line (); | |
1725 | } | |
1726 | ||
1727 | static bool is_leb128_expr; | |
1728 | ||
1729 | static void | |
1730 | xtensa_leb128 (int sign) | |
1731 | { | |
1732 | is_leb128_expr = true; | |
1733 | s_leb128 (sign); | |
1734 | is_leb128_expr = false; | |
1735 | } | |
1736 | ||
1737 | \f | |
1738 | /* Parsing and Idiom Translation. */ | |
1739 | ||
1740 | /* Parse @plt, etc. and return the desired relocation. */ | |
1741 | static bfd_reloc_code_real_type | |
1742 | xtensa_elf_suffix (char **str_p, expressionS *exp_p) | |
1743 | { | |
1744 | char ident[20]; | |
1745 | char *str = *str_p; | |
1746 | char *str2; | |
1747 | int ch; | |
1748 | int len; | |
1749 | unsigned int i; | |
1750 | ||
1751 | if (*str++ != '@') | |
1752 | return BFD_RELOC_NONE; | |
1753 | ||
1754 | for (ch = *str, str2 = ident; | |
1755 | (str2 < ident + sizeof (ident) - 1 | |
1756 | && (ISALNUM (ch) || ch == '@')); | |
1757 | ch = *++str) | |
1758 | { | |
1759 | *str2++ = (ISLOWER (ch)) ? ch : TOLOWER (ch); | |
1760 | } | |
1761 | ||
1762 | *str2 = '\0'; | |
1763 | len = str2 - ident; | |
1764 | ||
1765 | ch = ident[0]; | |
1766 | for (i = 0; i < ARRAY_SIZE (suffix_relocs); i++) | |
1767 | if (ch == suffix_relocs[i].suffix[0] | |
1768 | && len == suffix_relocs[i].length | |
1769 | && memcmp (ident, suffix_relocs[i].suffix, suffix_relocs[i].length) == 0) | |
1770 | { | |
1771 | /* Now check for "identifier@suffix+constant". */ | |
1772 | if (*str == '-' || *str == '+') | |
1773 | { | |
1774 | char *orig_line = input_line_pointer; | |
1775 | expressionS new_exp; | |
1776 | ||
1777 | input_line_pointer = str; | |
1778 | expression (&new_exp); | |
1779 | if (new_exp.X_op == O_constant) | |
1780 | { | |
1781 | exp_p->X_add_number += new_exp.X_add_number; | |
1782 | str = input_line_pointer; | |
1783 | } | |
1784 | ||
1785 | if (&input_line_pointer != str_p) | |
1786 | input_line_pointer = orig_line; | |
1787 | } | |
1788 | ||
1789 | *str_p = str; | |
1790 | return suffix_relocs[i].reloc; | |
1791 | } | |
1792 | ||
1793 | return BFD_RELOC_UNUSED; | |
1794 | } | |
1795 | ||
1796 | ||
1797 | /* Find the matching operator type. */ | |
1798 | static operatorT | |
1799 | map_suffix_reloc_to_operator (bfd_reloc_code_real_type reloc) | |
1800 | { | |
1801 | operatorT operator = O_illegal; | |
1802 | unsigned int i; | |
1803 | ||
1804 | for (i = 0; i < ARRAY_SIZE (suffix_relocs); i++) | |
1805 | { | |
1806 | if (suffix_relocs[i].reloc == reloc) | |
1807 | { | |
1808 | operator = suffix_relocs[i].operator; | |
1809 | break; | |
1810 | } | |
1811 | } | |
1812 | gas_assert (operator != O_illegal); | |
1813 | return operator; | |
1814 | } | |
1815 | ||
1816 | ||
1817 | /* Find the matching reloc type. */ | |
1818 | static bfd_reloc_code_real_type | |
1819 | map_operator_to_reloc (unsigned char operator, bool is_literal) | |
1820 | { | |
1821 | unsigned int i; | |
1822 | bfd_reloc_code_real_type reloc = BFD_RELOC_UNUSED; | |
1823 | ||
1824 | for (i = 0; i < ARRAY_SIZE (suffix_relocs); i++) | |
1825 | { | |
1826 | if (suffix_relocs[i].operator == operator) | |
1827 | { | |
1828 | reloc = suffix_relocs[i].reloc; | |
1829 | break; | |
1830 | } | |
1831 | } | |
1832 | ||
1833 | if (is_literal) | |
1834 | { | |
1835 | if (reloc == BFD_RELOC_XTENSA_TLS_FUNC) | |
1836 | return BFD_RELOC_XTENSA_TLSDESC_FN; | |
1837 | else if (reloc == BFD_RELOC_XTENSA_TLS_ARG) | |
1838 | return BFD_RELOC_XTENSA_TLSDESC_ARG; | |
1839 | } | |
1840 | ||
1841 | if (reloc == BFD_RELOC_UNUSED) | |
1842 | return BFD_RELOC_32; | |
1843 | ||
1844 | return reloc; | |
1845 | } | |
1846 | ||
1847 | ||
1848 | static const char * | |
1849 | expression_end (const char *name) | |
1850 | { | |
1851 | while (1) | |
1852 | { | |
1853 | switch (*name) | |
1854 | { | |
1855 | case '}': | |
1856 | case ';': | |
1857 | case '\0': | |
1858 | case ',': | |
1859 | case ':': | |
1860 | return name; | |
1861 | case ' ': | |
1862 | case '\t': | |
1863 | ++name; | |
1864 | continue; | |
1865 | default: | |
1866 | return 0; | |
1867 | } | |
1868 | } | |
1869 | } | |
1870 | ||
1871 | ||
1872 | #define ERROR_REG_NUM ((unsigned) -1) | |
1873 | ||
1874 | static unsigned | |
1875 | tc_get_register (const char *prefix) | |
1876 | { | |
1877 | unsigned reg; | |
1878 | const char *next_expr; | |
1879 | const char *old_line_pointer; | |
1880 | ||
1881 | SKIP_WHITESPACE (); | |
1882 | old_line_pointer = input_line_pointer; | |
1883 | ||
1884 | if (*input_line_pointer == '$') | |
1885 | ++input_line_pointer; | |
1886 | ||
1887 | /* Accept "sp" as a synonym for "a1". */ | |
1888 | if (input_line_pointer[0] == 's' && input_line_pointer[1] == 'p' | |
1889 | && expression_end (input_line_pointer + 2)) | |
1890 | { | |
1891 | input_line_pointer += 2; | |
1892 | return 1; /* AR[1] */ | |
1893 | } | |
1894 | ||
1895 | while (*input_line_pointer++ == *prefix++) | |
1896 | ; | |
1897 | --input_line_pointer; | |
1898 | --prefix; | |
1899 | ||
1900 | if (*prefix) | |
1901 | { | |
1902 | as_bad (_("bad register name: %s"), old_line_pointer); | |
1903 | return ERROR_REG_NUM; | |
1904 | } | |
1905 | ||
1906 | if (!ISDIGIT ((unsigned char) *input_line_pointer)) | |
1907 | { | |
1908 | as_bad (_("bad register number: %s"), input_line_pointer); | |
1909 | return ERROR_REG_NUM; | |
1910 | } | |
1911 | ||
1912 | reg = 0; | |
1913 | ||
1914 | while (ISDIGIT ((int) *input_line_pointer)) | |
1915 | reg = reg * 10 + *input_line_pointer++ - '0'; | |
1916 | ||
1917 | if (!(next_expr = expression_end (input_line_pointer))) | |
1918 | { | |
1919 | as_bad (_("bad register name: %s"), old_line_pointer); | |
1920 | return ERROR_REG_NUM; | |
1921 | } | |
1922 | ||
1923 | input_line_pointer = (char *) next_expr; | |
1924 | ||
1925 | return reg; | |
1926 | } | |
1927 | ||
1928 | ||
1929 | static void | |
1930 | expression_maybe_register (xtensa_opcode opc, int opnd, expressionS *tok) | |
1931 | { | |
1932 | xtensa_isa isa = xtensa_default_isa; | |
1933 | ||
1934 | /* Check if this is an immediate operand. */ | |
1935 | if (xtensa_operand_is_register (isa, opc, opnd) == 0) | |
1936 | { | |
1937 | bfd_reloc_code_real_type reloc; | |
1938 | segT t = expression (tok); | |
1939 | ||
1940 | if (t == absolute_section | |
1941 | && xtensa_operand_is_PCrelative (isa, opc, opnd) == 1) | |
1942 | { | |
1943 | gas_assert (tok->X_op == O_constant); | |
1944 | tok->X_op = O_symbol; | |
1945 | tok->X_add_symbol = &abs_symbol; | |
1946 | } | |
1947 | ||
1948 | if ((tok->X_op == O_constant || tok->X_op == O_symbol) | |
1949 | && ((reloc = xtensa_elf_suffix (&input_line_pointer, tok)) | |
1950 | != BFD_RELOC_NONE)) | |
1951 | { | |
1952 | switch (reloc) | |
1953 | { | |
1954 | case BFD_RELOC_LO16: | |
1955 | if (tok->X_op == O_constant) | |
1956 | { | |
1957 | tok->X_add_number &= 0xffff; | |
1958 | return; | |
1959 | } | |
1960 | break; | |
1961 | case BFD_RELOC_HI16: | |
1962 | if (tok->X_op == O_constant) | |
1963 | { | |
1964 | tok->X_add_number = ((unsigned) tok->X_add_number) >> 16; | |
1965 | return; | |
1966 | } | |
1967 | break; | |
1968 | case BFD_RELOC_UNUSED: | |
1969 | as_bad (_("unsupported relocation")); | |
1970 | return; | |
1971 | case BFD_RELOC_32_PCREL: | |
1972 | as_bad (_("pcrel relocation not allowed in an instruction")); | |
1973 | return; | |
1974 | default: | |
1975 | break; | |
1976 | } | |
1977 | tok->X_op = map_suffix_reloc_to_operator (reloc); | |
1978 | } | |
1979 | } | |
1980 | else | |
1981 | { | |
1982 | xtensa_regfile opnd_rf = xtensa_operand_regfile (isa, opc, opnd); | |
1983 | unsigned reg = tc_get_register (xtensa_regfile_shortname (isa, opnd_rf)); | |
1984 | ||
1985 | if (reg != ERROR_REG_NUM) /* Already errored */ | |
1986 | { | |
1987 | uint32 buf = reg; | |
1988 | if (xtensa_operand_encode (isa, opc, opnd, &buf)) | |
1989 | as_bad (_("register number out of range")); | |
1990 | } | |
1991 | ||
1992 | tok->X_op = O_register; | |
1993 | tok->X_add_symbol = 0; | |
1994 | tok->X_add_number = reg; | |
1995 | } | |
1996 | } | |
1997 | ||
1998 | ||
1999 | /* Split up the arguments for an opcode or pseudo-op. */ | |
2000 | ||
2001 | static int | |
2002 | tokenize_arguments (char **args, char *str) | |
2003 | { | |
2004 | char *old_input_line_pointer; | |
2005 | bool saw_comma = false; | |
2006 | bool saw_arg = false; | |
2007 | bool saw_colon = false; | |
2008 | int num_args = 0; | |
2009 | char *arg_end, *arg; | |
2010 | int arg_len; | |
2011 | ||
2012 | /* Save and restore input_line_pointer around this function. */ | |
2013 | old_input_line_pointer = input_line_pointer; | |
2014 | input_line_pointer = str; | |
2015 | ||
2016 | while (*input_line_pointer) | |
2017 | { | |
2018 | SKIP_WHITESPACE (); | |
2019 | switch (*input_line_pointer) | |
2020 | { | |
2021 | case '\0': | |
2022 | case '}': | |
2023 | goto fini; | |
2024 | ||
2025 | case ':': | |
2026 | input_line_pointer++; | |
2027 | if (saw_comma || saw_colon || !saw_arg) | |
2028 | goto err; | |
2029 | saw_colon = true; | |
2030 | break; | |
2031 | ||
2032 | case ',': | |
2033 | input_line_pointer++; | |
2034 | if (saw_comma || saw_colon || !saw_arg) | |
2035 | goto err; | |
2036 | saw_comma = true; | |
2037 | break; | |
2038 | ||
2039 | default: | |
2040 | if (!saw_comma && !saw_colon && saw_arg) | |
2041 | goto err; | |
2042 | ||
2043 | arg_end = input_line_pointer + 1; | |
2044 | while (!expression_end (arg_end)) | |
2045 | arg_end += 1; | |
2046 | ||
2047 | arg_len = arg_end - input_line_pointer; | |
2048 | arg = XNEWVEC (char, (saw_colon ? 1 : 0) + arg_len + 1); | |
2049 | args[num_args] = arg; | |
2050 | ||
2051 | if (saw_colon) | |
2052 | *arg++ = ':'; | |
2053 | strncpy (arg, input_line_pointer, arg_len); | |
2054 | arg[arg_len] = '\0'; | |
2055 | ||
2056 | input_line_pointer = arg_end; | |
2057 | num_args += 1; | |
2058 | saw_comma = false; | |
2059 | saw_colon = false; | |
2060 | saw_arg = true; | |
2061 | break; | |
2062 | } | |
2063 | } | |
2064 | ||
2065 | fini: | |
2066 | if (saw_comma || saw_colon) | |
2067 | goto err; | |
2068 | input_line_pointer = old_input_line_pointer; | |
2069 | return num_args; | |
2070 | ||
2071 | err: | |
2072 | if (saw_comma) | |
2073 | as_bad (_("extra comma")); | |
2074 | else if (saw_colon) | |
2075 | as_bad (_("extra colon")); | |
2076 | else if (!saw_arg) | |
2077 | as_bad (_("missing argument")); | |
2078 | else | |
2079 | as_bad (_("missing comma or colon")); | |
2080 | input_line_pointer = old_input_line_pointer; | |
2081 | return -1; | |
2082 | } | |
2083 | ||
2084 | ||
2085 | /* Parse the arguments to an opcode. Return TRUE on error. */ | |
2086 | ||
2087 | static bool | |
2088 | parse_arguments (TInsn *insn, int num_args, char **arg_strings) | |
2089 | { | |
2090 | expressionS *tok, *last_tok; | |
2091 | xtensa_opcode opcode = insn->opcode; | |
2092 | bool had_error = true; | |
2093 | xtensa_isa isa = xtensa_default_isa; | |
2094 | int n, num_regs = 0; | |
2095 | int opcode_operand_count; | |
2096 | int opnd_cnt, last_opnd_cnt; | |
2097 | unsigned int next_reg = 0; | |
2098 | char *old_input_line_pointer; | |
2099 | ||
2100 | if (insn->insn_type == ITYPE_LITERAL) | |
2101 | opcode_operand_count = 1; | |
2102 | else | |
2103 | opcode_operand_count = xtensa_opcode_num_operands (isa, opcode); | |
2104 | ||
2105 | tok = insn->tok; | |
2106 | memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS); | |
2107 | ||
2108 | /* Save and restore input_line_pointer around this function. */ | |
2109 | old_input_line_pointer = input_line_pointer; | |
2110 | ||
2111 | last_tok = 0; | |
2112 | last_opnd_cnt = -1; | |
2113 | opnd_cnt = 0; | |
2114 | ||
2115 | /* Skip invisible operands. */ | |
2116 | while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0) | |
2117 | { | |
2118 | opnd_cnt += 1; | |
2119 | tok++; | |
2120 | } | |
2121 | ||
2122 | for (n = 0; n < num_args; n++) | |
2123 | { | |
2124 | input_line_pointer = arg_strings[n]; | |
2125 | if (*input_line_pointer == ':') | |
2126 | { | |
2127 | xtensa_regfile opnd_rf; | |
2128 | input_line_pointer++; | |
2129 | if (num_regs == 0) | |
2130 | goto err; | |
2131 | gas_assert (opnd_cnt > 0); | |
2132 | num_regs--; | |
2133 | opnd_rf = xtensa_operand_regfile (isa, opcode, last_opnd_cnt); | |
2134 | if (next_reg | |
2135 | != tc_get_register (xtensa_regfile_shortname (isa, opnd_rf))) | |
2136 | as_warn (_("incorrect register number, ignoring")); | |
2137 | next_reg++; | |
2138 | } | |
2139 | else | |
2140 | { | |
2141 | if (opnd_cnt >= opcode_operand_count) | |
2142 | { | |
2143 | as_warn (_("too many arguments")); | |
2144 | goto err; | |
2145 | } | |
2146 | gas_assert (opnd_cnt < MAX_INSN_ARGS); | |
2147 | ||
2148 | expression_maybe_register (opcode, opnd_cnt, tok); | |
2149 | next_reg = tok->X_add_number + 1; | |
2150 | ||
2151 | if (tok->X_op == O_illegal || tok->X_op == O_absent) | |
2152 | goto err; | |
2153 | if (xtensa_operand_is_register (isa, opcode, opnd_cnt) == 1) | |
2154 | { | |
2155 | num_regs = xtensa_operand_num_regs (isa, opcode, opnd_cnt) - 1; | |
2156 | /* minus 1 because we are seeing one right now */ | |
2157 | } | |
2158 | else | |
2159 | num_regs = 0; | |
2160 | ||
2161 | last_tok = tok; | |
2162 | last_opnd_cnt = opnd_cnt; | |
2163 | demand_empty_rest_of_line (); | |
2164 | ||
2165 | do | |
2166 | { | |
2167 | opnd_cnt += 1; | |
2168 | tok++; | |
2169 | } | |
2170 | while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0); | |
2171 | } | |
2172 | } | |
2173 | ||
2174 | if (num_regs > 0 && ((int) next_reg != last_tok->X_add_number + 1)) | |
2175 | goto err; | |
2176 | ||
2177 | insn->ntok = tok - insn->tok; | |
2178 | had_error = false; | |
2179 | ||
2180 | err: | |
2181 | input_line_pointer = old_input_line_pointer; | |
2182 | return had_error; | |
2183 | } | |
2184 | ||
2185 | ||
2186 | static int | |
2187 | get_invisible_operands (TInsn *insn) | |
2188 | { | |
2189 | xtensa_isa isa = xtensa_default_isa; | |
2190 | static xtensa_insnbuf slotbuf = NULL; | |
2191 | xtensa_format fmt; | |
2192 | xtensa_opcode opc = insn->opcode; | |
2193 | int slot, opnd, fmt_found; | |
2194 | unsigned val; | |
2195 | ||
2196 | if (!slotbuf) | |
2197 | slotbuf = xtensa_insnbuf_alloc (isa); | |
2198 | ||
2199 | /* Find format/slot where this can be encoded. */ | |
2200 | fmt_found = 0; | |
2201 | slot = 0; | |
2202 | for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++) | |
2203 | { | |
2204 | for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++) | |
2205 | { | |
2206 | if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opc) == 0) | |
2207 | { | |
2208 | fmt_found = 1; | |
2209 | break; | |
2210 | } | |
2211 | } | |
2212 | if (fmt_found) break; | |
2213 | } | |
2214 | ||
2215 | if (!fmt_found) | |
2216 | { | |
2217 | as_bad (_("cannot encode opcode \"%s\""), xtensa_opcode_name (isa, opc)); | |
2218 | return -1; | |
2219 | } | |
2220 | ||
2221 | /* First encode all the visible operands | |
2222 | (to deal with shared field operands). */ | |
2223 | for (opnd = 0; opnd < insn->ntok; opnd++) | |
2224 | { | |
2225 | if (xtensa_operand_is_visible (isa, opc, opnd) == 1 | |
2226 | && (insn->tok[opnd].X_op == O_register | |
2227 | || insn->tok[opnd].X_op == O_constant)) | |
2228 | { | |
2229 | val = insn->tok[opnd].X_add_number; | |
2230 | xtensa_operand_encode (isa, opc, opnd, &val); | |
2231 | xtensa_operand_set_field (isa, opc, opnd, fmt, slot, slotbuf, val); | |
2232 | } | |
2233 | } | |
2234 | ||
2235 | /* Then pull out the values for the invisible ones. */ | |
2236 | for (opnd = 0; opnd < insn->ntok; opnd++) | |
2237 | { | |
2238 | if (xtensa_operand_is_visible (isa, opc, opnd) == 0) | |
2239 | { | |
2240 | xtensa_operand_get_field (isa, opc, opnd, fmt, slot, slotbuf, &val); | |
2241 | xtensa_operand_decode (isa, opc, opnd, &val); | |
2242 | insn->tok[opnd].X_add_number = val; | |
2243 | if (xtensa_operand_is_register (isa, opc, opnd) == 1) | |
2244 | insn->tok[opnd].X_op = O_register; | |
2245 | else | |
2246 | insn->tok[opnd].X_op = O_constant; | |
2247 | } | |
2248 | } | |
2249 | ||
2250 | return 0; | |
2251 | } | |
2252 | ||
2253 | ||
2254 | static void | |
2255 | xg_reverse_shift_count (char **cnt_argp) | |
2256 | { | |
2257 | char *cnt_arg, *new_arg; | |
2258 | cnt_arg = *cnt_argp; | |
2259 | ||
2260 | /* replace the argument with "31-(argument)" */ | |
2261 | new_arg = concat ("31-(", cnt_arg, ")", (char *) NULL); | |
2262 | ||
2263 | free (cnt_arg); | |
2264 | *cnt_argp = new_arg; | |
2265 | } | |
2266 | ||
2267 | ||
2268 | /* If "arg" is a constant expression, return non-zero with the value | |
2269 | in *valp. */ | |
2270 | ||
2271 | static int | |
2272 | xg_arg_is_constant (char *arg, offsetT *valp) | |
2273 | { | |
2274 | expressionS exp; | |
2275 | char *save_ptr = input_line_pointer; | |
2276 | ||
2277 | input_line_pointer = arg; | |
2278 | expression (&exp); | |
2279 | input_line_pointer = save_ptr; | |
2280 | ||
2281 | if (exp.X_op == O_constant) | |
2282 | { | |
2283 | *valp = exp.X_add_number; | |
2284 | return 1; | |
2285 | } | |
2286 | ||
2287 | return 0; | |
2288 | } | |
2289 | ||
2290 | ||
2291 | static void | |
2292 | xg_replace_opname (char **popname, const char *newop) | |
2293 | { | |
2294 | free (*popname); | |
2295 | *popname = xstrdup (newop); | |
2296 | } | |
2297 | ||
2298 | ||
2299 | static int | |
2300 | xg_check_num_args (int *pnum_args, | |
2301 | int expected_num, | |
2302 | char *opname, | |
2303 | char **arg_strings) | |
2304 | { | |
2305 | int num_args = *pnum_args; | |
2306 | ||
2307 | if (num_args < expected_num) | |
2308 | { | |
2309 | as_bad (_("not enough operands (%d) for '%s'; expected %d"), | |
2310 | num_args, opname, expected_num); | |
2311 | return -1; | |
2312 | } | |
2313 | ||
2314 | if (num_args > expected_num) | |
2315 | { | |
2316 | as_warn (_("too many operands (%d) for '%s'; expected %d"), | |
2317 | num_args, opname, expected_num); | |
2318 | while (num_args-- > expected_num) | |
2319 | { | |
2320 | free (arg_strings[num_args]); | |
2321 | arg_strings[num_args] = 0; | |
2322 | } | |
2323 | *pnum_args = expected_num; | |
2324 | return -1; | |
2325 | } | |
2326 | ||
2327 | return 0; | |
2328 | } | |
2329 | ||
2330 | ||
2331 | /* If the register is not specified as part of the opcode, | |
2332 | then get it from the operand and move it to the opcode. */ | |
2333 | ||
2334 | static int | |
2335 | xg_translate_sysreg_op (char **popname, int *pnum_args, char **arg_strings) | |
2336 | { | |
2337 | xtensa_isa isa = xtensa_default_isa; | |
2338 | xtensa_sysreg sr; | |
2339 | char *opname, *new_opname; | |
2340 | const char *sr_name; | |
2341 | int is_user, is_write; | |
2342 | ||
2343 | opname = *popname; | |
2344 | if (*opname == '_') | |
2345 | opname += 1; | |
2346 | is_user = (opname[1] == 'u'); | |
2347 | is_write = (opname[0] == 'w'); | |
2348 | ||
2349 | /* Opname == [rw]ur or [rwx]sr... */ | |
2350 | ||
2351 | if (xg_check_num_args (pnum_args, 2, opname, arg_strings)) | |
2352 | return -1; | |
2353 | ||
2354 | /* Check if the argument is a symbolic register name. */ | |
2355 | sr = xtensa_sysreg_lookup_name (isa, arg_strings[1]); | |
2356 | /* Handle WSR to "INTSET" as a special case. */ | |
2357 | if (sr == XTENSA_UNDEFINED && is_write && !is_user | |
2358 | && !strcasecmp (arg_strings[1], "intset")) | |
2359 | sr = xtensa_sysreg_lookup_name (isa, "interrupt"); | |
2360 | if (sr == XTENSA_UNDEFINED | |
2361 | || (xtensa_sysreg_is_user (isa, sr) == 1) != is_user) | |
2362 | { | |
2363 | /* Maybe it's a register number.... */ | |
2364 | offsetT val; | |
2365 | if (!xg_arg_is_constant (arg_strings[1], &val)) | |
2366 | { | |
2367 | as_bad (_("invalid register '%s' for '%s' instruction"), | |
2368 | arg_strings[1], opname); | |
2369 | return -1; | |
2370 | } | |
2371 | sr = xtensa_sysreg_lookup (isa, val, is_user); | |
2372 | if (sr == XTENSA_UNDEFINED) | |
2373 | { | |
2374 | as_bad (_("invalid register number (%ld) for '%s' instruction"), | |
2375 | (long) val, opname); | |
2376 | return -1; | |
2377 | } | |
2378 | } | |
2379 | ||
2380 | /* Remove the last argument, which is now part of the opcode. */ | |
2381 | free (arg_strings[1]); | |
2382 | arg_strings[1] = 0; | |
2383 | *pnum_args = 1; | |
2384 | ||
2385 | /* Translate the opcode. */ | |
2386 | sr_name = xtensa_sysreg_name (isa, sr); | |
2387 | /* Another special case for "WSR.INTSET".... */ | |
2388 | if (is_write && !is_user && !strcasecmp ("interrupt", sr_name)) | |
2389 | sr_name = "intset"; | |
2390 | new_opname = concat (*popname, ".", sr_name, (char *) NULL); | |
2391 | free (*popname); | |
2392 | *popname = new_opname; | |
2393 | ||
2394 | return 0; | |
2395 | } | |
2396 | ||
2397 | ||
2398 | static int | |
2399 | xtensa_translate_old_userreg_ops (char **popname) | |
2400 | { | |
2401 | xtensa_isa isa = xtensa_default_isa; | |
2402 | xtensa_sysreg sr; | |
2403 | char *opname, *new_opname; | |
2404 | const char *sr_name; | |
2405 | bool has_underbar = false; | |
2406 | ||
2407 | opname = *popname; | |
2408 | if (opname[0] == '_') | |
2409 | { | |
2410 | has_underbar = true; | |
2411 | opname += 1; | |
2412 | } | |
2413 | ||
2414 | sr = xtensa_sysreg_lookup_name (isa, opname + 1); | |
2415 | if (sr != XTENSA_UNDEFINED) | |
2416 | { | |
2417 | /* The new default name ("nnn") is different from the old default | |
2418 | name ("URnnn"). The old default is handled below, and we don't | |
2419 | want to recognize [RW]nnn, so do nothing if the name is the (new) | |
2420 | default. */ | |
2421 | static char namebuf[10]; | |
2422 | sprintf (namebuf, "%d", xtensa_sysreg_number (isa, sr)); | |
2423 | if (strcmp (namebuf, opname + 1) == 0) | |
2424 | return 0; | |
2425 | } | |
2426 | else | |
2427 | { | |
2428 | offsetT val; | |
2429 | char *end; | |
2430 | ||
2431 | /* Only continue if the reg name is "URnnn". */ | |
2432 | if (opname[1] != 'u' || opname[2] != 'r') | |
2433 | return 0; | |
2434 | val = strtoul (opname + 3, &end, 10); | |
2435 | if (*end != '\0') | |
2436 | return 0; | |
2437 | ||
2438 | sr = xtensa_sysreg_lookup (isa, val, 1); | |
2439 | if (sr == XTENSA_UNDEFINED) | |
2440 | { | |
2441 | as_bad (_("invalid register number (%ld) for '%s'"), | |
2442 | (long) val, opname); | |
2443 | return -1; | |
2444 | } | |
2445 | } | |
2446 | ||
2447 | /* Translate the opcode. */ | |
2448 | sr_name = xtensa_sysreg_name (isa, sr); | |
2449 | new_opname = XNEWVEC (char, strlen (sr_name) + 6); | |
2450 | sprintf (new_opname, "%s%cur.%s", (has_underbar ? "_" : ""), | |
2451 | opname[0], sr_name); | |
2452 | free (*popname); | |
2453 | *popname = new_opname; | |
2454 | ||
2455 | return 0; | |
2456 | } | |
2457 | ||
2458 | ||
2459 | static int | |
2460 | xtensa_translate_zero_immed (const char *old_op, | |
2461 | const char *new_op, | |
2462 | char **popname, | |
2463 | int *pnum_args, | |
2464 | char **arg_strings) | |
2465 | { | |
2466 | char *opname; | |
2467 | offsetT val; | |
2468 | ||
2469 | opname = *popname; | |
2470 | gas_assert (opname[0] != '_'); | |
2471 | ||
2472 | if (strcmp (opname, old_op) != 0) | |
2473 | return 0; | |
2474 | ||
2475 | if (xg_check_num_args (pnum_args, 3, opname, arg_strings)) | |
2476 | return -1; | |
2477 | if (xg_arg_is_constant (arg_strings[1], &val) && val == 0) | |
2478 | { | |
2479 | xg_replace_opname (popname, new_op); | |
2480 | free (arg_strings[1]); | |
2481 | arg_strings[1] = arg_strings[2]; | |
2482 | arg_strings[2] = 0; | |
2483 | *pnum_args = 2; | |
2484 | } | |
2485 | ||
2486 | return 0; | |
2487 | } | |
2488 | ||
2489 | ||
2490 | /* If the instruction is an idiom (i.e., a built-in macro), translate it. | |
2491 | Returns non-zero if an error was found. */ | |
2492 | ||
2493 | static int | |
2494 | xg_translate_idioms (char **popname, int *pnum_args, char **arg_strings) | |
2495 | { | |
2496 | char *opname = *popname; | |
2497 | bool has_underbar = false; | |
2498 | ||
2499 | if (*opname == '_') | |
2500 | { | |
2501 | has_underbar = true; | |
2502 | opname += 1; | |
2503 | } | |
2504 | ||
2505 | if (strcmp (opname, "mov") == 0) | |
2506 | { | |
2507 | if (use_transform () && !has_underbar && density_supported) | |
2508 | xg_replace_opname (popname, "mov.n"); | |
2509 | else | |
2510 | { | |
2511 | if (xg_check_num_args (pnum_args, 2, opname, arg_strings)) | |
2512 | return -1; | |
2513 | xg_replace_opname (popname, (has_underbar ? "_or" : "or")); | |
2514 | arg_strings[2] = xstrdup (arg_strings[1]); | |
2515 | *pnum_args = 3; | |
2516 | } | |
2517 | return 0; | |
2518 | } | |
2519 | ||
2520 | /* Without an operand, this is given a default immediate operand of 0. */ | |
2521 | if ((strcmp (opname, "simcall") == 0 && microarch_earliest >= 280000)) | |
2522 | { | |
2523 | if (*pnum_args == 0) | |
2524 | { | |
2525 | arg_strings[0] = (char *) xmalloc (2); | |
2526 | strcpy (arg_strings[0], "0"); | |
2527 | *pnum_args = 1; | |
2528 | } | |
2529 | return 0; | |
2530 | } | |
2531 | ||
2532 | if (strcmp (opname, "bbsi.l") == 0) | |
2533 | { | |
2534 | if (xg_check_num_args (pnum_args, 3, opname, arg_strings)) | |
2535 | return -1; | |
2536 | xg_replace_opname (popname, (has_underbar ? "_bbsi" : "bbsi")); | |
2537 | if (target_big_endian) | |
2538 | xg_reverse_shift_count (&arg_strings[1]); | |
2539 | return 0; | |
2540 | } | |
2541 | ||
2542 | if (strcmp (opname, "bbci.l") == 0) | |
2543 | { | |
2544 | if (xg_check_num_args (pnum_args, 3, opname, arg_strings)) | |
2545 | return -1; | |
2546 | xg_replace_opname (popname, (has_underbar ? "_bbci" : "bbci")); | |
2547 | if (target_big_endian) | |
2548 | xg_reverse_shift_count (&arg_strings[1]); | |
2549 | return 0; | |
2550 | } | |
2551 | ||
2552 | /* Don't do anything special with NOPs inside FLIX instructions. They | |
2553 | are handled elsewhere. Real NOP instructions are always available | |
2554 | in configurations with FLIX, so this should never be an issue but | |
2555 | check for it anyway. */ | |
2556 | if (!cur_vinsn.inside_bundle && xtensa_nop_opcode == XTENSA_UNDEFINED | |
2557 | && strcmp (opname, "nop") == 0) | |
2558 | { | |
2559 | if (use_transform () && !has_underbar && density_supported) | |
2560 | xg_replace_opname (popname, "nop.n"); | |
2561 | else | |
2562 | { | |
2563 | if (xg_check_num_args (pnum_args, 0, opname, arg_strings)) | |
2564 | return -1; | |
2565 | xg_replace_opname (popname, (has_underbar ? "_or" : "or")); | |
2566 | arg_strings[0] = xstrdup ("a1"); | |
2567 | arg_strings[1] = xstrdup ("a1"); | |
2568 | arg_strings[2] = xstrdup ("a1"); | |
2569 | *pnum_args = 3; | |
2570 | } | |
2571 | return 0; | |
2572 | } | |
2573 | ||
2574 | /* Recognize [RW]UR and [RWX]SR. */ | |
2575 | if ((((opname[0] == 'r' || opname[0] == 'w') | |
2576 | && (opname[1] == 'u' || opname[1] == 's')) | |
2577 | || (opname[0] == 'x' && opname[1] == 's')) | |
2578 | && opname[2] == 'r' | |
2579 | && opname[3] == '\0') | |
2580 | return xg_translate_sysreg_op (popname, pnum_args, arg_strings); | |
2581 | ||
2582 | /* Backward compatibility for RUR and WUR: Recognize [RW]UR<nnn> and | |
2583 | [RW]<name> if <name> is the non-default name of a user register. */ | |
2584 | if ((opname[0] == 'r' || opname[0] == 'w') | |
2585 | && xtensa_opcode_lookup (xtensa_default_isa, opname) == XTENSA_UNDEFINED) | |
2586 | return xtensa_translate_old_userreg_ops (popname); | |
2587 | ||
2588 | /* Relax branches that don't allow comparisons against an immediate value | |
2589 | of zero to the corresponding branches with implicit zero immediates. */ | |
2590 | if (!has_underbar && use_transform ()) | |
2591 | { | |
2592 | if (xtensa_translate_zero_immed ("bnei", "bnez", popname, | |
2593 | pnum_args, arg_strings)) | |
2594 | return -1; | |
2595 | ||
2596 | if (xtensa_translate_zero_immed ("beqi", "beqz", popname, | |
2597 | pnum_args, arg_strings)) | |
2598 | return -1; | |
2599 | ||
2600 | if (xtensa_translate_zero_immed ("bgei", "bgez", popname, | |
2601 | pnum_args, arg_strings)) | |
2602 | return -1; | |
2603 | ||
2604 | if (xtensa_translate_zero_immed ("blti", "bltz", popname, | |
2605 | pnum_args, arg_strings)) | |
2606 | return -1; | |
2607 | } | |
2608 | ||
2609 | return 0; | |
2610 | } | |
2611 | ||
2612 | \f | |
2613 | /* Functions for dealing with the Xtensa ISA. */ | |
2614 | ||
2615 | /* Currently the assembler only allows us to use a single target per | |
2616 | fragment. Because of this, only one operand for a given | |
2617 | instruction may be symbolic. If there is a PC-relative operand, | |
2618 | the last one is chosen. Otherwise, the result is the number of the | |
2619 | last immediate operand, and if there are none of those, we fail and | |
2620 | return -1. */ | |
2621 | ||
2622 | static int | |
2623 | get_relaxable_immed (xtensa_opcode opcode) | |
2624 | { | |
2625 | int last_immed = -1; | |
2626 | int noperands, opi; | |
2627 | ||
2628 | if (opcode == XTENSA_UNDEFINED) | |
2629 | return -1; | |
2630 | ||
2631 | noperands = xtensa_opcode_num_operands (xtensa_default_isa, opcode); | |
2632 | for (opi = noperands - 1; opi >= 0; opi--) | |
2633 | { | |
2634 | if (xtensa_operand_is_visible (xtensa_default_isa, opcode, opi) == 0) | |
2635 | continue; | |
2636 | if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, opi) == 1) | |
2637 | return opi; | |
2638 | if (last_immed == -1 | |
2639 | && xtensa_operand_is_register (xtensa_default_isa, opcode, opi) == 0) | |
2640 | last_immed = opi; | |
2641 | } | |
2642 | return last_immed; | |
2643 | } | |
2644 | ||
2645 | ||
2646 | static xtensa_opcode | |
2647 | get_opcode_from_buf (const char *buf, int slot) | |
2648 | { | |
2649 | static xtensa_insnbuf insnbuf = NULL; | |
2650 | static xtensa_insnbuf slotbuf = NULL; | |
2651 | xtensa_isa isa = xtensa_default_isa; | |
2652 | xtensa_format fmt; | |
2653 | ||
2654 | if (!insnbuf) | |
2655 | { | |
2656 | insnbuf = xtensa_insnbuf_alloc (isa); | |
2657 | slotbuf = xtensa_insnbuf_alloc (isa); | |
2658 | } | |
2659 | ||
2660 | xtensa_insnbuf_from_chars (isa, insnbuf, (const unsigned char *) buf, 0); | |
2661 | fmt = xtensa_format_decode (isa, insnbuf); | |
2662 | if (fmt == XTENSA_UNDEFINED) | |
2663 | return XTENSA_UNDEFINED; | |
2664 | ||
2665 | if (slot >= xtensa_format_num_slots (isa, fmt)) | |
2666 | return XTENSA_UNDEFINED; | |
2667 | ||
2668 | xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf); | |
2669 | return xtensa_opcode_decode (isa, fmt, slot, slotbuf); | |
2670 | } | |
2671 | ||
2672 | ||
2673 | #ifdef TENSILICA_DEBUG | |
2674 | ||
2675 | /* For debugging, print out the mapping of opcode numbers to opcodes. */ | |
2676 | ||
2677 | static void | |
2678 | xtensa_print_insn_table (void) | |
2679 | { | |
2680 | int num_opcodes, num_operands; | |
2681 | xtensa_opcode opcode; | |
2682 | xtensa_isa isa = xtensa_default_isa; | |
2683 | ||
2684 | num_opcodes = xtensa_isa_num_opcodes (xtensa_default_isa); | |
2685 | for (opcode = 0; opcode < num_opcodes; opcode++) | |
2686 | { | |
2687 | int opn; | |
2688 | fprintf (stderr, "%d: %s: ", opcode, xtensa_opcode_name (isa, opcode)); | |
2689 | num_operands = xtensa_opcode_num_operands (isa, opcode); | |
2690 | for (opn = 0; opn < num_operands; opn++) | |
2691 | { | |
2692 | if (xtensa_operand_is_visible (isa, opcode, opn) == 0) | |
2693 | continue; | |
2694 | if (xtensa_operand_is_register (isa, opcode, opn) == 1) | |
2695 | { | |
2696 | xtensa_regfile opnd_rf = | |
2697 | xtensa_operand_regfile (isa, opcode, opn); | |
2698 | fprintf (stderr, "%s ", xtensa_regfile_shortname (isa, opnd_rf)); | |
2699 | } | |
2700 | else if (xtensa_operand_is_PCrelative (isa, opcode, opn) == 1) | |
2701 | fputs ("[lLr] ", stderr); | |
2702 | else | |
2703 | fputs ("i ", stderr); | |
2704 | } | |
2705 | fprintf (stderr, "\n"); | |
2706 | } | |
2707 | } | |
2708 | ||
2709 | ||
2710 | static void | |
2711 | print_vliw_insn (xtensa_insnbuf vbuf) | |
2712 | { | |
2713 | xtensa_isa isa = xtensa_default_isa; | |
2714 | xtensa_format f = xtensa_format_decode (isa, vbuf); | |
2715 | xtensa_insnbuf sbuf = xtensa_insnbuf_alloc (isa); | |
2716 | int op; | |
2717 | ||
2718 | fprintf (stderr, "format = %d\n", f); | |
2719 | ||
2720 | for (op = 0; op < xtensa_format_num_slots (isa, f); op++) | |
2721 | { | |
2722 | xtensa_opcode opcode; | |
2723 | const char *opname; | |
2724 | int operands; | |
2725 | ||
2726 | xtensa_format_get_slot (isa, f, op, vbuf, sbuf); | |
2727 | opcode = xtensa_opcode_decode (isa, f, op, sbuf); | |
2728 | opname = xtensa_opcode_name (isa, opcode); | |
2729 | ||
2730 | fprintf (stderr, "op in slot %i is %s;\n", op, opname); | |
2731 | fprintf (stderr, " operands = "); | |
2732 | for (operands = 0; | |
2733 | operands < xtensa_opcode_num_operands (isa, opcode); | |
2734 | operands++) | |
2735 | { | |
2736 | unsigned int val; | |
2737 | if (xtensa_operand_is_visible (isa, opcode, operands) == 0) | |
2738 | continue; | |
2739 | xtensa_operand_get_field (isa, opcode, operands, f, op, sbuf, &val); | |
2740 | xtensa_operand_decode (isa, opcode, operands, &val); | |
2741 | fprintf (stderr, "%d ", val); | |
2742 | } | |
2743 | fprintf (stderr, "\n"); | |
2744 | } | |
2745 | xtensa_insnbuf_free (isa, sbuf); | |
2746 | } | |
2747 | ||
2748 | #endif /* TENSILICA_DEBUG */ | |
2749 | ||
2750 | ||
2751 | static bool | |
2752 | is_direct_call_opcode (xtensa_opcode opcode) | |
2753 | { | |
2754 | xtensa_isa isa = xtensa_default_isa; | |
2755 | int n, num_operands; | |
2756 | ||
2757 | if (xtensa_opcode_is_call (isa, opcode) != 1) | |
2758 | return false; | |
2759 | ||
2760 | num_operands = xtensa_opcode_num_operands (isa, opcode); | |
2761 | for (n = 0; n < num_operands; n++) | |
2762 | { | |
2763 | if (xtensa_operand_is_register (isa, opcode, n) == 0 | |
2764 | && xtensa_operand_is_PCrelative (isa, opcode, n) == 1) | |
2765 | return true; | |
2766 | } | |
2767 | return false; | |
2768 | } | |
2769 | ||
2770 | ||
2771 | /* Convert from BFD relocation type code to slot and operand number. | |
2772 | Returns non-zero on failure. */ | |
2773 | ||
2774 | static int | |
2775 | decode_reloc (bfd_reloc_code_real_type reloc, int *slot, bool *is_alt) | |
2776 | { | |
2777 | if (reloc >= BFD_RELOC_XTENSA_SLOT0_OP | |
2778 | && reloc <= BFD_RELOC_XTENSA_SLOT14_OP) | |
2779 | { | |
2780 | *slot = reloc - BFD_RELOC_XTENSA_SLOT0_OP; | |
2781 | *is_alt = false; | |
2782 | } | |
2783 | else if (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT | |
2784 | && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT) | |
2785 | { | |
2786 | *slot = reloc - BFD_RELOC_XTENSA_SLOT0_ALT; | |
2787 | *is_alt = true; | |
2788 | } | |
2789 | else | |
2790 | return -1; | |
2791 | ||
2792 | return 0; | |
2793 | } | |
2794 | ||
2795 | ||
2796 | /* Convert from slot number to BFD relocation type code for the | |
2797 | standard PC-relative relocations. Return BFD_RELOC_NONE on | |
2798 | failure. */ | |
2799 | ||
2800 | static bfd_reloc_code_real_type | |
2801 | encode_reloc (int slot) | |
2802 | { | |
2803 | if (slot < 0 || slot > 14) | |
2804 | return BFD_RELOC_NONE; | |
2805 | ||
2806 | return BFD_RELOC_XTENSA_SLOT0_OP + slot; | |
2807 | } | |
2808 | ||
2809 | ||
2810 | /* Convert from slot numbers to BFD relocation type code for the | |
2811 | "alternate" relocations. Return BFD_RELOC_NONE on failure. */ | |
2812 | ||
2813 | static bfd_reloc_code_real_type | |
2814 | encode_alt_reloc (int slot) | |
2815 | { | |
2816 | if (slot < 0 || slot > 14) | |
2817 | return BFD_RELOC_NONE; | |
2818 | ||
2819 | return BFD_RELOC_XTENSA_SLOT0_ALT + slot; | |
2820 | } | |
2821 | ||
2822 | ||
2823 | static void | |
2824 | xtensa_insnbuf_set_operand (xtensa_insnbuf slotbuf, | |
2825 | xtensa_format fmt, | |
2826 | int slot, | |
2827 | xtensa_opcode opcode, | |
2828 | int operand, | |
2829 | uint32 value, | |
2830 | const char *file, | |
2831 | unsigned int line) | |
2832 | { | |
2833 | uint32 valbuf = value; | |
2834 | ||
2835 | if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf)) | |
2836 | { | |
2837 | if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, operand) | |
2838 | == 1) | |
2839 | as_bad_where ((char *) file, line, | |
2840 | _("operand %d of '%s' has out of range value '%u'"), | |
2841 | operand + 1, | |
2842 | xtensa_opcode_name (xtensa_default_isa, opcode), | |
2843 | value); | |
2844 | else | |
2845 | as_bad_where ((char *) file, line, | |
2846 | _("operand %d of '%s' has invalid value '%u'"), | |
2847 | operand + 1, | |
2848 | xtensa_opcode_name (xtensa_default_isa, opcode), | |
2849 | value); | |
2850 | return; | |
2851 | } | |
2852 | ||
2853 | xtensa_operand_set_field (xtensa_default_isa, opcode, operand, fmt, slot, | |
2854 | slotbuf, valbuf); | |
2855 | } | |
2856 | ||
2857 | ||
2858 | static uint32 | |
2859 | xtensa_insnbuf_get_operand (xtensa_insnbuf slotbuf, | |
2860 | xtensa_format fmt, | |
2861 | int slot, | |
2862 | xtensa_opcode opcode, | |
2863 | int opnum) | |
2864 | { | |
2865 | uint32 val = 0; | |
2866 | (void) xtensa_operand_get_field (xtensa_default_isa, opcode, opnum, | |
2867 | fmt, slot, slotbuf, &val); | |
2868 | (void) xtensa_operand_decode (xtensa_default_isa, opcode, opnum, &val); | |
2869 | return val; | |
2870 | } | |
2871 | ||
2872 | \f | |
2873 | /* Checks for rules from xtensa-relax tables. */ | |
2874 | ||
2875 | /* The routine xg_instruction_matches_option_term must return TRUE | |
2876 | when a given option term is true. The meaning of all of the option | |
2877 | terms is given interpretation by this function. */ | |
2878 | ||
2879 | static bool | |
2880 | xg_instruction_matches_option_term (TInsn *insn, const ReqOrOption *option) | |
2881 | { | |
2882 | if (strcmp (option->option_name, "realnop") == 0 | |
2883 | || startswith (option->option_name, "IsaUse")) | |
2884 | { | |
2885 | /* These conditions were evaluated statically when building the | |
2886 | relaxation table. There's no need to reevaluate them now. */ | |
2887 | return true; | |
2888 | } | |
2889 | else if (strcmp (option->option_name, "FREEREG") == 0) | |
2890 | return insn->extra_arg.X_op == O_register; | |
2891 | else | |
2892 | { | |
2893 | as_fatal (_("internal error: unknown option name '%s'"), | |
2894 | option->option_name); | |
2895 | } | |
2896 | } | |
2897 | ||
2898 | ||
2899 | static bool | |
2900 | xg_instruction_matches_or_options (TInsn *insn, | |
2901 | const ReqOrOptionList *or_option) | |
2902 | { | |
2903 | const ReqOrOption *option; | |
2904 | /* Must match each of the AND terms. */ | |
2905 | for (option = or_option; option != NULL; option = option->next) | |
2906 | { | |
2907 | if (xg_instruction_matches_option_term (insn, option)) | |
2908 | return true; | |
2909 | } | |
2910 | return false; | |
2911 | } | |
2912 | ||
2913 | ||
2914 | static bool | |
2915 | xg_instruction_matches_options (TInsn *insn, const ReqOptionList *options) | |
2916 | { | |
2917 | const ReqOption *req_options; | |
2918 | /* Must match each of the AND terms. */ | |
2919 | for (req_options = options; | |
2920 | req_options != NULL; | |
2921 | req_options = req_options->next) | |
2922 | { | |
2923 | /* Must match one of the OR clauses. */ | |
2924 | if (!xg_instruction_matches_or_options (insn, | |
2925 | req_options->or_option_terms)) | |
2926 | return false; | |
2927 | } | |
2928 | return true; | |
2929 | } | |
2930 | ||
2931 | ||
2932 | /* Return the transition rule that matches or NULL if none matches. */ | |
2933 | ||
2934 | static bool | |
2935 | xg_instruction_matches_rule (TInsn *insn, TransitionRule *rule) | |
2936 | { | |
2937 | PreconditionList *condition_l; | |
2938 | ||
2939 | if (rule->opcode != insn->opcode) | |
2940 | return false; | |
2941 | ||
2942 | for (condition_l = rule->conditions; | |
2943 | condition_l != NULL; | |
2944 | condition_l = condition_l->next) | |
2945 | { | |
2946 | expressionS *exp1; | |
2947 | expressionS *exp2; | |
2948 | Precondition *cond = condition_l->precond; | |
2949 | ||
2950 | switch (cond->typ) | |
2951 | { | |
2952 | case OP_CONSTANT: | |
2953 | /* The expression must be the constant. */ | |
2954 | gas_assert (cond->op_num < insn->ntok); | |
2955 | exp1 = &insn->tok[cond->op_num]; | |
2956 | if (expr_is_const (exp1)) | |
2957 | { | |
2958 | switch (cond->cmp) | |
2959 | { | |
2960 | case OP_EQUAL: | |
2961 | if (get_expr_const (exp1) != cond->op_data) | |
2962 | return false; | |
2963 | break; | |
2964 | case OP_NOTEQUAL: | |
2965 | if (get_expr_const (exp1) == cond->op_data) | |
2966 | return false; | |
2967 | break; | |
2968 | default: | |
2969 | return false; | |
2970 | } | |
2971 | } | |
2972 | else if (expr_is_register (exp1)) | |
2973 | { | |
2974 | switch (cond->cmp) | |
2975 | { | |
2976 | case OP_EQUAL: | |
2977 | if (get_expr_register (exp1) != cond->op_data) | |
2978 | return false; | |
2979 | break; | |
2980 | case OP_NOTEQUAL: | |
2981 | if (get_expr_register (exp1) == cond->op_data) | |
2982 | return false; | |
2983 | break; | |
2984 | default: | |
2985 | return false; | |
2986 | } | |
2987 | } | |
2988 | else | |
2989 | return false; | |
2990 | break; | |
2991 | ||
2992 | case OP_OPERAND: | |
2993 | gas_assert (cond->op_num < insn->ntok); | |
2994 | gas_assert (cond->op_data < insn->ntok); | |
2995 | exp1 = &insn->tok[cond->op_num]; | |
2996 | exp2 = &insn->tok[cond->op_data]; | |
2997 | ||
2998 | switch (cond->cmp) | |
2999 | { | |
3000 | case OP_EQUAL: | |
3001 | if (!expr_is_equal (exp1, exp2)) | |
3002 | return false; | |
3003 | break; | |
3004 | case OP_NOTEQUAL: | |
3005 | if (expr_is_equal (exp1, exp2)) | |
3006 | return false; | |
3007 | break; | |
3008 | } | |
3009 | break; | |
3010 | ||
3011 | case OP_LITERAL: | |
3012 | case OP_LABEL: | |
3013 | default: | |
3014 | return false; | |
3015 | } | |
3016 | } | |
3017 | if (!xg_instruction_matches_options (insn, rule->options)) | |
3018 | return false; | |
3019 | ||
3020 | return true; | |
3021 | } | |
3022 | ||
3023 | ||
3024 | static int | |
3025 | transition_rule_cmp (const TransitionRule *a, const TransitionRule *b) | |
3026 | { | |
3027 | bool a_greater = false; | |
3028 | bool b_greater = false; | |
3029 | ||
3030 | ReqOptionList *l_a = a->options; | |
3031 | ReqOptionList *l_b = b->options; | |
3032 | ||
3033 | /* We only care if they both are the same except for | |
3034 | a const16 vs. an l32r. */ | |
3035 | ||
3036 | while (l_a && l_b && ((l_a->next == NULL) == (l_b->next == NULL))) | |
3037 | { | |
3038 | ReqOrOptionList *l_or_a = l_a->or_option_terms; | |
3039 | ReqOrOptionList *l_or_b = l_b->or_option_terms; | |
3040 | while (l_or_a && l_or_b && ((l_a->next == NULL) == (l_b->next == NULL))) | |
3041 | { | |
3042 | if (l_or_a->is_true != l_or_b->is_true) | |
3043 | return 0; | |
3044 | if (strcmp (l_or_a->option_name, l_or_b->option_name) != 0) | |
3045 | { | |
3046 | /* This is the case we care about. */ | |
3047 | if (strcmp (l_or_a->option_name, "IsaUseConst16") == 0 | |
3048 | && strcmp (l_or_b->option_name, "IsaUseL32R") == 0) | |
3049 | { | |
3050 | if (prefer_const16) | |
3051 | a_greater = true; | |
3052 | else | |
3053 | b_greater = true; | |
3054 | } | |
3055 | else if (strcmp (l_or_a->option_name, "IsaUseL32R") == 0 | |
3056 | && strcmp (l_or_b->option_name, "IsaUseConst16") == 0) | |
3057 | { | |
3058 | if (prefer_const16) | |
3059 | b_greater = true; | |
3060 | else | |
3061 | a_greater = true; | |
3062 | } | |
3063 | else | |
3064 | return 0; | |
3065 | } | |
3066 | l_or_a = l_or_a->next; | |
3067 | l_or_b = l_or_b->next; | |
3068 | } | |
3069 | if (l_or_a || l_or_b) | |
3070 | return 0; | |
3071 | ||
3072 | l_a = l_a->next; | |
3073 | l_b = l_b->next; | |
3074 | } | |
3075 | if (l_a || l_b) | |
3076 | return 0; | |
3077 | ||
3078 | /* Incomparable if the substitution was used differently in two cases. */ | |
3079 | if (a_greater && b_greater) | |
3080 | return 0; | |
3081 | ||
3082 | if (b_greater) | |
3083 | return 1; | |
3084 | if (a_greater) | |
3085 | return -1; | |
3086 | ||
3087 | return 0; | |
3088 | } | |
3089 | ||
3090 | ||
3091 | static TransitionRule * | |
3092 | xg_instruction_match (TInsn *insn) | |
3093 | { | |
3094 | TransitionTable *table = xg_build_simplify_table (&transition_rule_cmp); | |
3095 | TransitionList *l; | |
3096 | gas_assert (insn->opcode < table->num_opcodes); | |
3097 | ||
3098 | /* Walk through all of the possible transitions. */ | |
3099 | for (l = table->table[insn->opcode]; l != NULL; l = l->next) | |
3100 | { | |
3101 | TransitionRule *rule = l->rule; | |
3102 | if (xg_instruction_matches_rule (insn, rule)) | |
3103 | return rule; | |
3104 | } | |
3105 | return NULL; | |
3106 | } | |
3107 | ||
3108 | \f | |
3109 | /* Various Other Internal Functions. */ | |
3110 | ||
3111 | static bool | |
3112 | is_unique_insn_expansion (TransitionRule *r) | |
3113 | { | |
3114 | if (!r->to_instr || r->to_instr->next != NULL) | |
3115 | return false; | |
3116 | if (r->to_instr->typ != INSTR_INSTR) | |
3117 | return false; | |
3118 | return true; | |
3119 | } | |
3120 | ||
3121 | ||
3122 | /* Check if there is exactly one relaxation for INSN that converts it to | |
3123 | another instruction of equal or larger size. If so, and if TARG is | |
3124 | non-null, go ahead and generate the relaxed instruction into TARG. If | |
3125 | NARROW_ONLY is true, then only consider relaxations that widen a narrow | |
3126 | instruction, i.e., ignore relaxations that convert to an instruction of | |
3127 | equal size. In some contexts where this function is used, only | |
3128 | a single widening is allowed and the NARROW_ONLY argument is used to | |
3129 | exclude cases like ADDI being "widened" to an ADDMI, which may | |
3130 | later be relaxed to an ADDMI/ADDI pair. */ | |
3131 | ||
3132 | bool | |
3133 | xg_is_single_relaxable_insn (TInsn *insn, TInsn *targ, bool narrow_only) | |
3134 | { | |
3135 | TransitionTable *table = xg_build_widen_table (&transition_rule_cmp); | |
3136 | TransitionList *l; | |
3137 | TransitionRule *match = 0; | |
3138 | ||
3139 | gas_assert (insn->insn_type == ITYPE_INSN); | |
3140 | gas_assert (insn->opcode < table->num_opcodes); | |
3141 | ||
3142 | for (l = table->table[insn->opcode]; l != NULL; l = l->next) | |
3143 | { | |
3144 | TransitionRule *rule = l->rule; | |
3145 | ||
3146 | if (xg_instruction_matches_rule (insn, rule) | |
3147 | && is_unique_insn_expansion (rule) | |
3148 | && (xg_get_single_size (insn->opcode) + (narrow_only ? 1 : 0) | |
3149 | <= xg_get_single_size (rule->to_instr->opcode))) | |
3150 | { | |
3151 | if (match) | |
3152 | return false; | |
3153 | match = rule; | |
3154 | } | |
3155 | } | |
3156 | if (!match) | |
3157 | return false; | |
3158 | ||
3159 | if (targ) | |
3160 | xg_build_to_insn (targ, insn, match->to_instr); | |
3161 | return true; | |
3162 | } | |
3163 | ||
3164 | ||
3165 | /* Return the maximum number of bytes this opcode can expand to. */ | |
3166 | ||
3167 | static int | |
3168 | xg_get_max_insn_widen_size (xtensa_opcode opcode) | |
3169 | { | |
3170 | TransitionTable *table = xg_build_widen_table (&transition_rule_cmp); | |
3171 | TransitionList *l; | |
3172 | int max_size = xg_get_single_size (opcode); | |
3173 | ||
3174 | gas_assert (opcode < table->num_opcodes); | |
3175 | ||
3176 | for (l = table->table[opcode]; l != NULL; l = l->next) | |
3177 | { | |
3178 | TransitionRule *rule = l->rule; | |
3179 | BuildInstr *build_list; | |
3180 | int this_size = 0; | |
3181 | ||
3182 | if (!rule) | |
3183 | continue; | |
3184 | build_list = rule->to_instr; | |
3185 | if (is_unique_insn_expansion (rule)) | |
3186 | { | |
3187 | gas_assert (build_list->typ == INSTR_INSTR); | |
3188 | this_size = xg_get_max_insn_widen_size (build_list->opcode); | |
3189 | } | |
3190 | else | |
3191 | for (; build_list != NULL; build_list = build_list->next) | |
3192 | { | |
3193 | switch (build_list->typ) | |
3194 | { | |
3195 | case INSTR_INSTR: | |
3196 | this_size += xg_get_single_size (build_list->opcode); | |
3197 | break; | |
3198 | case INSTR_LITERAL_DEF: | |
3199 | case INSTR_LABEL_DEF: | |
3200 | default: | |
3201 | break; | |
3202 | } | |
3203 | } | |
3204 | if (this_size > max_size) | |
3205 | max_size = this_size; | |
3206 | } | |
3207 | return max_size; | |
3208 | } | |
3209 | ||
3210 | ||
3211 | /* Return the maximum number of literal bytes this opcode can generate. */ | |
3212 | ||
3213 | static int | |
3214 | xg_get_max_insn_widen_literal_size (xtensa_opcode opcode) | |
3215 | { | |
3216 | TransitionTable *table = xg_build_widen_table (&transition_rule_cmp); | |
3217 | TransitionList *l; | |
3218 | int max_size = 0; | |
3219 | ||
3220 | gas_assert (opcode < table->num_opcodes); | |
3221 | ||
3222 | for (l = table->table[opcode]; l != NULL; l = l->next) | |
3223 | { | |
3224 | TransitionRule *rule = l->rule; | |
3225 | BuildInstr *build_list; | |
3226 | int this_size = 0; | |
3227 | ||
3228 | if (!rule) | |
3229 | continue; | |
3230 | build_list = rule->to_instr; | |
3231 | if (is_unique_insn_expansion (rule)) | |
3232 | { | |
3233 | gas_assert (build_list->typ == INSTR_INSTR); | |
3234 | this_size = xg_get_max_insn_widen_literal_size (build_list->opcode); | |
3235 | } | |
3236 | else | |
3237 | for (; build_list != NULL; build_list = build_list->next) | |
3238 | { | |
3239 | switch (build_list->typ) | |
3240 | { | |
3241 | case INSTR_LITERAL_DEF: | |
3242 | /* Hard-coded 4-byte literal. */ | |
3243 | this_size += 4; | |
3244 | break; | |
3245 | case INSTR_INSTR: | |
3246 | case INSTR_LABEL_DEF: | |
3247 | default: | |
3248 | break; | |
3249 | } | |
3250 | } | |
3251 | if (this_size > max_size) | |
3252 | max_size = this_size; | |
3253 | } | |
3254 | return max_size; | |
3255 | } | |
3256 | ||
3257 | ||
3258 | static bool | |
3259 | xg_is_relaxable_insn (TInsn *insn, int lateral_steps) | |
3260 | { | |
3261 | int steps_taken = 0; | |
3262 | TransitionTable *table = xg_build_widen_table (&transition_rule_cmp); | |
3263 | TransitionList *l; | |
3264 | ||
3265 | gas_assert (insn->insn_type == ITYPE_INSN); | |
3266 | gas_assert (insn->opcode < table->num_opcodes); | |
3267 | ||
3268 | for (l = table->table[insn->opcode]; l != NULL; l = l->next) | |
3269 | { | |
3270 | TransitionRule *rule = l->rule; | |
3271 | ||
3272 | if (xg_instruction_matches_rule (insn, rule)) | |
3273 | { | |
3274 | if (steps_taken == lateral_steps) | |
3275 | return true; | |
3276 | steps_taken++; | |
3277 | } | |
3278 | } | |
3279 | return false; | |
3280 | } | |
3281 | ||
3282 | ||
3283 | static symbolS * | |
3284 | get_special_literal_symbol (void) | |
3285 | { | |
3286 | static symbolS *sym = NULL; | |
3287 | ||
3288 | if (sym == NULL) | |
3289 | sym = symbol_find_or_make ("SPECIAL_LITERAL0\001"); | |
3290 | return sym; | |
3291 | } | |
3292 | ||
3293 | ||
3294 | static symbolS * | |
3295 | get_special_label_symbol (void) | |
3296 | { | |
3297 | static symbolS *sym = NULL; | |
3298 | ||
3299 | if (sym == NULL) | |
3300 | sym = symbol_find_or_make ("SPECIAL_LABEL0\001"); | |
3301 | return sym; | |
3302 | } | |
3303 | ||
3304 | ||
3305 | static bool | |
3306 | xg_valid_literal_expression (const expressionS *exp) | |
3307 | { | |
3308 | switch (exp->X_op) | |
3309 | { | |
3310 | case O_constant: | |
3311 | case O_symbol: | |
3312 | case O_big: | |
3313 | case O_uminus: | |
3314 | case O_subtract: | |
3315 | case O_pltrel: | |
3316 | case O_pcrel: | |
3317 | case O_tlsfunc: | |
3318 | case O_tlsarg: | |
3319 | case O_tpoff: | |
3320 | case O_dtpoff: | |
3321 | return true; | |
3322 | default: | |
3323 | return false; | |
3324 | } | |
3325 | } | |
3326 | ||
3327 | ||
3328 | /* This will check to see if the value can be converted into the | |
3329 | operand type. It will return TRUE if it does not fit. */ | |
3330 | ||
3331 | static bool | |
3332 | xg_check_operand (int32 value, xtensa_opcode opcode, int operand) | |
3333 | { | |
3334 | uint32 valbuf = value; | |
3335 | if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf)) | |
3336 | return true; | |
3337 | return false; | |
3338 | } | |
3339 | ||
3340 | ||
3341 | /* Assumes: All immeds are constants. Check that all constants fit | |
3342 | into their immeds; return FALSE if not. */ | |
3343 | ||
3344 | static bool | |
3345 | xg_immeds_fit (const TInsn *insn) | |
3346 | { | |
3347 | xtensa_isa isa = xtensa_default_isa; | |
3348 | int i; | |
3349 | ||
3350 | int n = insn->ntok; | |
3351 | gas_assert (insn->insn_type == ITYPE_INSN); | |
3352 | for (i = 0; i < n; ++i) | |
3353 | { | |
3354 | const expressionS *exp = &insn->tok[i]; | |
3355 | ||
3356 | if (xtensa_operand_is_register (isa, insn->opcode, i) == 1) | |
3357 | continue; | |
3358 | ||
3359 | switch (exp->X_op) | |
3360 | { | |
3361 | case O_register: | |
3362 | case O_constant: | |
3363 | if (xg_check_operand (exp->X_add_number, insn->opcode, i)) | |
3364 | return false; | |
3365 | break; | |
3366 | ||
3367 | default: | |
3368 | /* The symbol should have a fixup associated with it. */ | |
3369 | gas_assert (false); | |
3370 | break; | |
3371 | } | |
3372 | } | |
3373 | return true; | |
3374 | } | |
3375 | ||
3376 | ||
3377 | /* This should only be called after we have an initial | |
3378 | estimate of the addresses. */ | |
3379 | ||
3380 | static bool | |
3381 | xg_symbolic_immeds_fit (const TInsn *insn, | |
3382 | segT pc_seg, | |
3383 | fragS *pc_frag, | |
3384 | offsetT pc_offset, | |
3385 | long stretch) | |
3386 | { | |
3387 | xtensa_isa isa = xtensa_default_isa; | |
3388 | symbolS *symbolP; | |
3389 | fragS *sym_frag; | |
3390 | offsetT target, pc; | |
3391 | uint32 new_offset; | |
3392 | int i; | |
3393 | int n = insn->ntok; | |
3394 | ||
3395 | gas_assert (insn->insn_type == ITYPE_INSN); | |
3396 | ||
3397 | for (i = 0; i < n; ++i) | |
3398 | { | |
3399 | const expressionS *exp = &insn->tok[i]; | |
3400 | ||
3401 | if (xtensa_operand_is_register (isa, insn->opcode, i) == 1) | |
3402 | continue; | |
3403 | ||
3404 | switch (exp->X_op) | |
3405 | { | |
3406 | case O_register: | |
3407 | case O_constant: | |
3408 | if (xg_check_operand (exp->X_add_number, insn->opcode, i)) | |
3409 | return false; | |
3410 | break; | |
3411 | ||
3412 | case O_lo16: | |
3413 | case O_hi16: | |
3414 | /* Check for the worst case. */ | |
3415 | if (xg_check_operand (0xffff, insn->opcode, i)) | |
3416 | return false; | |
3417 | break; | |
3418 | ||
3419 | case O_symbol: | |
3420 | /* We only allow symbols for PC-relative references. | |
3421 | If pc_frag == 0, then we don't have frag locations yet. */ | |
3422 | if (pc_frag == 0 | |
3423 | || xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 0) | |
3424 | return false; | |
3425 | ||
3426 | /* If it is a weak symbol or a symbol in a different section, | |
3427 | it cannot be known to fit at assembly time. */ | |
3428 | if (S_IS_WEAK (exp->X_add_symbol) | |
3429 | || S_GET_SEGMENT (exp->X_add_symbol) != pc_seg) | |
3430 | { | |
3431 | /* For a direct call with --no-longcalls, be optimistic and | |
3432 | assume it will be in range. If the symbol is weak and | |
3433 | undefined, it may remain undefined at link-time, in which | |
3434 | case it will have a zero value and almost certainly be out | |
3435 | of range for a direct call; thus, relax for undefined weak | |
3436 | symbols even if longcalls is not enabled. */ | |
3437 | if (is_direct_call_opcode (insn->opcode) | |
3438 | && ! pc_frag->tc_frag_data.use_longcalls | |
3439 | && (! S_IS_WEAK (exp->X_add_symbol) | |
3440 | || S_IS_DEFINED (exp->X_add_symbol))) | |
3441 | return true; | |
3442 | ||
3443 | return false; | |
3444 | } | |
3445 | ||
3446 | symbolP = exp->X_add_symbol; | |
3447 | sym_frag = symbol_get_frag (symbolP); | |
3448 | target = S_GET_VALUE (symbolP) + exp->X_add_number; | |
3449 | pc = pc_frag->fr_address + pc_offset; | |
3450 | ||
3451 | /* If frag has yet to be reached on this pass, assume it | |
3452 | will move by STRETCH just as we did. If this is not so, | |
3453 | it will be because some frag between grows, and that will | |
3454 | force another pass. Beware zero-length frags. There | |
3455 | should be a faster way to do this. */ | |
3456 | ||
3457 | if (stretch != 0 | |
3458 | && sym_frag->relax_marker != pc_frag->relax_marker | |
3459 | && S_GET_SEGMENT (symbolP) == pc_seg) | |
3460 | { | |
3461 | target += stretch; | |
3462 | } | |
3463 | ||
3464 | new_offset = target; | |
3465 | xtensa_operand_do_reloc (isa, insn->opcode, i, &new_offset, pc); | |
3466 | if (xg_check_operand (new_offset, insn->opcode, i)) | |
3467 | return false; | |
3468 | break; | |
3469 | ||
3470 | default: | |
3471 | /* The symbol should have a fixup associated with it. */ | |
3472 | return false; | |
3473 | } | |
3474 | } | |
3475 | ||
3476 | return true; | |
3477 | } | |
3478 | ||
3479 | ||
3480 | /* Return TRUE on success. */ | |
3481 | ||
3482 | static bool | |
3483 | xg_build_to_insn (TInsn *targ, TInsn *insn, BuildInstr *bi) | |
3484 | { | |
3485 | BuildOp *op; | |
3486 | symbolS *sym; | |
3487 | ||
3488 | tinsn_init (targ); | |
3489 | targ->debug_line = insn->debug_line; | |
3490 | targ->loc_directive_seen = insn->loc_directive_seen; | |
3491 | switch (bi->typ) | |
3492 | { | |
3493 | case INSTR_INSTR: | |
3494 | op = bi->ops; | |
3495 | targ->opcode = bi->opcode; | |
3496 | targ->insn_type = ITYPE_INSN; | |
3497 | targ->is_specific_opcode = false; | |
3498 | ||
3499 | for (; op != NULL; op = op->next) | |
3500 | { | |
3501 | int op_num = op->op_num; | |
3502 | int op_data = op->op_data; | |
3503 | ||
3504 | gas_assert (op->op_num < MAX_INSN_ARGS); | |
3505 | ||
3506 | if (targ->ntok <= op_num) | |
3507 | targ->ntok = op_num + 1; | |
3508 | ||
3509 | switch (op->typ) | |
3510 | { | |
3511 | case OP_CONSTANT: | |
3512 | set_expr_const (&targ->tok[op_num], op_data); | |
3513 | break; | |
3514 | case OP_OPERAND: | |
3515 | gas_assert (op_data < insn->ntok); | |
3516 | copy_expr (&targ->tok[op_num], &insn->tok[op_data]); | |
3517 | break; | |
3518 | case OP_FREEREG: | |
3519 | if (insn->extra_arg.X_op != O_register) | |
3520 | return false; | |
3521 | copy_expr (&targ->tok[op_num], &insn->extra_arg); | |
3522 | break; | |
3523 | case OP_LITERAL: | |
3524 | sym = get_special_literal_symbol (); | |
3525 | set_expr_symbol_offset (&targ->tok[op_num], sym, 0); | |
3526 | if (insn->tok[op_data].X_op == O_tlsfunc | |
3527 | || insn->tok[op_data].X_op == O_tlsarg) | |
3528 | copy_expr (&targ->extra_arg, &insn->tok[op_data]); | |
3529 | break; | |
3530 | case OP_LABEL: | |
3531 | sym = get_special_label_symbol (); | |
3532 | set_expr_symbol_offset (&targ->tok[op_num], sym, 0); | |
3533 | break; | |
3534 | case OP_OPERAND_HI16U: | |
3535 | case OP_OPERAND_LOW16U: | |
3536 | gas_assert (op_data < insn->ntok); | |
3537 | if (expr_is_const (&insn->tok[op_data])) | |
3538 | { | |
3539 | long val; | |
3540 | copy_expr (&targ->tok[op_num], &insn->tok[op_data]); | |
3541 | val = xg_apply_userdef_op_fn (op->typ, | |
3542 | targ->tok[op_num]. | |
3543 | X_add_number); | |
3544 | targ->tok[op_num].X_add_number = val; | |
3545 | } | |
3546 | else | |
3547 | { | |
3548 | /* For const16 we can create relocations for these. */ | |
3549 | if (targ->opcode == XTENSA_UNDEFINED | |
3550 | || (targ->opcode != xtensa_const16_opcode)) | |
3551 | return false; | |
3552 | gas_assert (op_data < insn->ntok); | |
3553 | /* Need to build a O_lo16 or O_hi16. */ | |
3554 | copy_expr (&targ->tok[op_num], &insn->tok[op_data]); | |
3555 | if (targ->tok[op_num].X_op == O_symbol) | |
3556 | { | |
3557 | if (op->typ == OP_OPERAND_HI16U) | |
3558 | targ->tok[op_num].X_op = O_hi16; | |
3559 | else if (op->typ == OP_OPERAND_LOW16U) | |
3560 | targ->tok[op_num].X_op = O_lo16; | |
3561 | else | |
3562 | return false; | |
3563 | } | |
3564 | } | |
3565 | break; | |
3566 | default: | |
3567 | /* currently handles: | |
3568 | OP_OPERAND_LOW8 | |
3569 | OP_OPERAND_HI24S | |
3570 | OP_OPERAND_F32MINUS */ | |
3571 | if (xg_has_userdef_op_fn (op->typ)) | |
3572 | { | |
3573 | gas_assert (op_data < insn->ntok); | |
3574 | if (expr_is_const (&insn->tok[op_data])) | |
3575 | { | |
3576 | long val; | |
3577 | copy_expr (&targ->tok[op_num], &insn->tok[op_data]); | |
3578 | val = xg_apply_userdef_op_fn (op->typ, | |
3579 | targ->tok[op_num]. | |
3580 | X_add_number); | |
3581 | targ->tok[op_num].X_add_number = val; | |
3582 | } | |
3583 | else | |
3584 | return false; /* We cannot use a relocation for this. */ | |
3585 | break; | |
3586 | } | |
3587 | gas_assert (0); | |
3588 | break; | |
3589 | } | |
3590 | } | |
3591 | break; | |
3592 | ||
3593 | case INSTR_LITERAL_DEF: | |
3594 | op = bi->ops; | |
3595 | targ->opcode = XTENSA_UNDEFINED; | |
3596 | targ->insn_type = ITYPE_LITERAL; | |
3597 | targ->is_specific_opcode = false; | |
3598 | for (; op != NULL; op = op->next) | |
3599 | { | |
3600 | int op_num = op->op_num; | |
3601 | int op_data = op->op_data; | |
3602 | gas_assert (op->op_num < MAX_INSN_ARGS); | |
3603 | ||
3604 | if (targ->ntok <= op_num) | |
3605 | targ->ntok = op_num + 1; | |
3606 | ||
3607 | switch (op->typ) | |
3608 | { | |
3609 | case OP_OPERAND: | |
3610 | gas_assert (op_data < insn->ntok); | |
3611 | /* We can only pass resolvable literals through. */ | |
3612 | if (!xg_valid_literal_expression (&insn->tok[op_data])) | |
3613 | return false; | |
3614 | copy_expr (&targ->tok[op_num], &insn->tok[op_data]); | |
3615 | break; | |
3616 | case OP_LITERAL: | |
3617 | case OP_CONSTANT: | |
3618 | case OP_LABEL: | |
3619 | default: | |
3620 | gas_assert (0); | |
3621 | break; | |
3622 | } | |
3623 | } | |
3624 | break; | |
3625 | ||
3626 | case INSTR_LABEL_DEF: | |
3627 | op = bi->ops; | |
3628 | targ->opcode = XTENSA_UNDEFINED; | |
3629 | targ->insn_type = ITYPE_LABEL; | |
3630 | targ->is_specific_opcode = false; | |
3631 | /* Literal with no ops is a label? */ | |
3632 | gas_assert (op == NULL); | |
3633 | break; | |
3634 | ||
3635 | default: | |
3636 | gas_assert (0); | |
3637 | } | |
3638 | ||
3639 | return true; | |
3640 | } | |
3641 | ||
3642 | ||
3643 | /* Return TRUE on success. */ | |
3644 | ||
3645 | static bool | |
3646 | xg_build_to_stack (IStack *istack, TInsn *insn, BuildInstr *bi) | |
3647 | { | |
3648 | for (; bi != NULL; bi = bi->next) | |
3649 | { | |
3650 | TInsn *next_insn = istack_push_space (istack); | |
3651 | ||
3652 | if (!xg_build_to_insn (next_insn, insn, bi)) | |
3653 | return false; | |
3654 | } | |
3655 | return true; | |
3656 | } | |
3657 | ||
3658 | ||
3659 | /* Return TRUE on valid expansion. */ | |
3660 | ||
3661 | static bool | |
3662 | xg_expand_to_stack (IStack *istack, TInsn *insn, int lateral_steps) | |
3663 | { | |
3664 | int stack_size = istack->ninsn; | |
3665 | int steps_taken = 0; | |
3666 | TransitionTable *table = xg_build_widen_table (&transition_rule_cmp); | |
3667 | TransitionList *l; | |
3668 | ||
3669 | gas_assert (insn->insn_type == ITYPE_INSN); | |
3670 | gas_assert (insn->opcode < table->num_opcodes); | |
3671 | ||
3672 | for (l = table->table[insn->opcode]; l != NULL; l = l->next) | |
3673 | { | |
3674 | TransitionRule *rule = l->rule; | |
3675 | ||
3676 | if (xg_instruction_matches_rule (insn, rule)) | |
3677 | { | |
3678 | if (lateral_steps == steps_taken) | |
3679 | { | |
3680 | int i; | |
3681 | ||
3682 | /* This is it. Expand the rule to the stack. */ | |
3683 | if (!xg_build_to_stack (istack, insn, rule->to_instr)) | |
3684 | return false; | |
3685 | ||
3686 | /* Check to see if it fits. */ | |
3687 | for (i = stack_size; i < istack->ninsn; i++) | |
3688 | { | |
3689 | TInsn *tinsn = &istack->insn[i]; | |
3690 | ||
3691 | if (tinsn->insn_type == ITYPE_INSN | |
3692 | && !tinsn_has_symbolic_operands (tinsn) | |
3693 | && !xg_immeds_fit (tinsn)) | |
3694 | { | |
3695 | istack->ninsn = stack_size; | |
3696 | return false; | |
3697 | } | |
3698 | } | |
3699 | return true; | |
3700 | } | |
3701 | steps_taken++; | |
3702 | } | |
3703 | } | |
3704 | return false; | |
3705 | } | |
3706 | ||
3707 | \f | |
3708 | /* Relax the assembly instruction at least "min_steps". | |
3709 | Return the number of steps taken. | |
3710 | ||
3711 | For relaxation to correctly terminate, every relaxation chain must | |
3712 | terminate in one of two ways: | |
3713 | ||
3714 | 1. If the chain from one instruction to the next consists entirely of | |
3715 | single instructions, then the chain *must* handle all possible | |
3716 | immediates without failing. It must not ever fail because an | |
3717 | immediate is out of range. The MOVI.N -> MOVI -> L32R relaxation | |
3718 | chain is one example. L32R loads 32 bits, and there cannot be an | |
3719 | immediate larger than 32 bits, so it satisfies this condition. | |
3720 | Single instruction relaxation chains are as defined by | |
3721 | xg_is_single_relaxable_instruction. | |
3722 | ||
3723 | 2. Otherwise, the chain must end in a multi-instruction expansion: e.g., | |
3724 | BNEZ.N -> BNEZ -> BNEZ.W15 -> BENZ.N/J | |
3725 | ||
3726 | Strictly speaking, in most cases you can violate condition 1 and be OK | |
3727 | -- in particular when the last two instructions have the same single | |
3728 | size. But nevertheless, you should guarantee the above two conditions. | |
3729 | ||
3730 | We could fix this so that single-instruction expansions correctly | |
3731 | terminate when they can't handle the range, but the error messages are | |
3732 | worse, and it actually turns out that in every case but one (18-bit wide | |
3733 | branches), you need a multi-instruction expansion to get the full range | |
3734 | anyway. And because 18-bit branches are handled identically to 15-bit | |
3735 | branches, there isn't any point in changing it. */ | |
3736 | ||
3737 | static int | |
3738 | xg_assembly_relax (IStack *istack, | |
3739 | TInsn *insn, | |
3740 | segT pc_seg, | |
3741 | fragS *pc_frag, /* if pc_frag == 0, not pc-relative */ | |
3742 | offsetT pc_offset, /* offset in fragment */ | |
3743 | int min_steps, /* minimum conversion steps */ | |
3744 | long stretch) /* number of bytes stretched so far */ | |
3745 | { | |
3746 | int steps_taken = 0; | |
3747 | ||
3748 | /* Some of its immeds don't fit. Try to build a relaxed version. | |
3749 | This may go through a couple of stages of single instruction | |
3750 | transformations before we get there. */ | |
3751 | ||
3752 | TInsn single_target; | |
3753 | TInsn current_insn; | |
3754 | int lateral_steps = 0; | |
3755 | int istack_size = istack->ninsn; | |
3756 | ||
3757 | if (xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch) | |
3758 | && steps_taken >= min_steps) | |
3759 | { | |
3760 | istack_push (istack, insn); | |
3761 | return steps_taken; | |
3762 | } | |
3763 | current_insn = *insn; | |
3764 | ||
3765 | /* Walk through all of the single instruction expansions. */ | |
3766 | while (xg_is_single_relaxable_insn (¤t_insn, &single_target, false)) | |
3767 | { | |
3768 | steps_taken++; | |
3769 | if (xg_symbolic_immeds_fit (&single_target, pc_seg, pc_frag, pc_offset, | |
3770 | stretch)) | |
3771 | { | |
3772 | if (steps_taken >= min_steps) | |
3773 | { | |
3774 | istack_push (istack, &single_target); | |
3775 | return steps_taken; | |
3776 | } | |
3777 | } | |
3778 | current_insn = single_target; | |
3779 | } | |
3780 | ||
3781 | /* Now check for a multi-instruction expansion. */ | |
3782 | while (xg_is_relaxable_insn (¤t_insn, lateral_steps)) | |
3783 | { | |
3784 | if (xg_symbolic_immeds_fit (¤t_insn, pc_seg, pc_frag, pc_offset, | |
3785 | stretch)) | |
3786 | { | |
3787 | if (steps_taken >= min_steps) | |
3788 | { | |
3789 | istack_push (istack, ¤t_insn); | |
3790 | return steps_taken; | |
3791 | } | |
3792 | } | |
3793 | steps_taken++; | |
3794 | if (xg_expand_to_stack (istack, ¤t_insn, lateral_steps)) | |
3795 | { | |
3796 | if (steps_taken >= min_steps) | |
3797 | return steps_taken; | |
3798 | } | |
3799 | lateral_steps++; | |
3800 | istack->ninsn = istack_size; | |
3801 | } | |
3802 | ||
3803 | /* It's not going to work -- use the original. */ | |
3804 | istack_push (istack, insn); | |
3805 | return steps_taken; | |
3806 | } | |
3807 | ||
3808 | ||
3809 | static void | |
3810 | xg_finish_frag (char *last_insn, | |
3811 | enum xtensa_relax_statesE frag_state, | |
3812 | enum xtensa_relax_statesE slot0_state, | |
3813 | int max_growth, | |
3814 | bool is_insn) | |
3815 | { | |
3816 | /* Finish off this fragment so that it has at LEAST the desired | |
3817 | max_growth. If it doesn't fit in this fragment, close this one | |
3818 | and start a new one. In either case, return a pointer to the | |
3819 | beginning of the growth area. */ | |
3820 | ||
3821 | fragS *old_frag; | |
3822 | ||
3823 | frag_grow (max_growth); | |
3824 | old_frag = frag_now; | |
3825 | ||
3826 | frag_now->fr_opcode = last_insn; | |
3827 | if (is_insn) | |
3828 | frag_now->tc_frag_data.is_insn = true; | |
3829 | ||
3830 | frag_var (rs_machine_dependent, max_growth, max_growth, | |
3831 | frag_state, frag_now->fr_symbol, frag_now->fr_offset, last_insn); | |
3832 | ||
3833 | old_frag->tc_frag_data.slot_subtypes[0] = slot0_state; | |
3834 | xtensa_set_frag_assembly_state (frag_now); | |
3835 | ||
3836 | /* Just to make sure that we did not split it up. */ | |
3837 | gas_assert (old_frag->fr_next == frag_now); | |
3838 | } | |
3839 | ||
3840 | ||
3841 | /* Return TRUE if the target frag is one of the next non-empty frags. */ | |
3842 | ||
3843 | static bool | |
3844 | is_next_frag_target (const fragS *fragP, const fragS *target) | |
3845 | { | |
3846 | if (fragP == NULL) | |
3847 | return false; | |
3848 | ||
3849 | for (; fragP; fragP = fragP->fr_next) | |
3850 | { | |
3851 | if (fragP == target) | |
3852 | return true; | |
3853 | if (fragP->fr_fix != 0) | |
3854 | return false; | |
3855 | if (fragP->fr_type == rs_fill && fragP->fr_offset != 0) | |
3856 | return false; | |
3857 | if ((fragP->fr_type == rs_align || fragP->fr_type == rs_align_code) | |
3858 | && ((fragP->fr_address % (1 << fragP->fr_offset)) != 0)) | |
3859 | return false; | |
3860 | if (fragP->fr_type == rs_space) | |
3861 | return false; | |
3862 | } | |
3863 | return false; | |
3864 | } | |
3865 | ||
3866 | ||
3867 | static bool | |
3868 | is_branch_jmp_to_next (TInsn *insn, fragS *fragP) | |
3869 | { | |
3870 | xtensa_isa isa = xtensa_default_isa; | |
3871 | int i; | |
3872 | int num_ops = xtensa_opcode_num_operands (isa, insn->opcode); | |
3873 | int target_op = -1; | |
3874 | symbolS *sym; | |
3875 | fragS *target_frag; | |
3876 | ||
3877 | if (xtensa_opcode_is_branch (isa, insn->opcode) != 1 | |
3878 | && xtensa_opcode_is_jump (isa, insn->opcode) != 1) | |
3879 | return false; | |
3880 | ||
3881 | for (i = 0; i < num_ops; i++) | |
3882 | { | |
3883 | if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1) | |
3884 | { | |
3885 | target_op = i; | |
3886 | break; | |
3887 | } | |
3888 | } | |
3889 | if (target_op == -1) | |
3890 | return false; | |
3891 | ||
3892 | if (insn->ntok <= target_op) | |
3893 | return false; | |
3894 | ||
3895 | if (insn->tok[target_op].X_op != O_symbol) | |
3896 | return false; | |
3897 | ||
3898 | sym = insn->tok[target_op].X_add_symbol; | |
3899 | if (sym == NULL) | |
3900 | return false; | |
3901 | ||
3902 | if (insn->tok[target_op].X_add_number != 0) | |
3903 | return false; | |
3904 | ||
3905 | target_frag = symbol_get_frag (sym); | |
3906 | if (target_frag == NULL) | |
3907 | return false; | |
3908 | ||
3909 | if (is_next_frag_target (fragP->fr_next, target_frag) | |
3910 | && S_GET_VALUE (sym) == target_frag->fr_address) | |
3911 | return true; | |
3912 | ||
3913 | return false; | |
3914 | } | |
3915 | ||
3916 | ||
3917 | static void | |
3918 | xg_add_branch_and_loop_targets (TInsn *insn) | |
3919 | { | |
3920 | xtensa_isa isa = xtensa_default_isa; | |
3921 | int num_ops = xtensa_opcode_num_operands (isa, insn->opcode); | |
3922 | ||
3923 | if (xtensa_opcode_is_loop (isa, insn->opcode) == 1) | |
3924 | { | |
3925 | int i = 1; | |
3926 | if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1 | |
3927 | && insn->tok[i].X_op == O_symbol) | |
3928 | symbol_get_tc (insn->tok[i].X_add_symbol)->is_loop_target = true; | |
3929 | return; | |
3930 | } | |
3931 | ||
3932 | if (xtensa_opcode_is_branch (isa, insn->opcode) == 1 | |
3933 | || xtensa_opcode_is_loop (isa, insn->opcode) == 1) | |
3934 | { | |
3935 | int i; | |
3936 | ||
3937 | for (i = 0; i < insn->ntok && i < num_ops; i++) | |
3938 | { | |
3939 | if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1 | |
3940 | && insn->tok[i].X_op == O_symbol) | |
3941 | { | |
3942 | symbolS *sym = insn->tok[i].X_add_symbol; | |
3943 | symbol_get_tc (sym)->is_branch_target = true; | |
3944 | if (S_IS_DEFINED (sym)) | |
3945 | symbol_get_frag (sym)->tc_frag_data.is_branch_target = true; | |
3946 | } | |
3947 | } | |
3948 | } | |
3949 | } | |
3950 | ||
3951 | ||
3952 | /* Return FALSE if no error. */ | |
3953 | ||
3954 | static bool | |
3955 | xg_build_token_insn (BuildInstr *instr_spec, TInsn *old_insn, TInsn *new_insn) | |
3956 | { | |
3957 | int num_ops = 0; | |
3958 | BuildOp *b_op; | |
3959 | ||
3960 | switch (instr_spec->typ) | |
3961 | { | |
3962 | case INSTR_INSTR: | |
3963 | new_insn->insn_type = ITYPE_INSN; | |
3964 | new_insn->opcode = instr_spec->opcode; | |
3965 | break; | |
3966 | case INSTR_LITERAL_DEF: | |
3967 | new_insn->insn_type = ITYPE_LITERAL; | |
3968 | new_insn->opcode = XTENSA_UNDEFINED; | |
3969 | break; | |
3970 | case INSTR_LABEL_DEF: | |
3971 | abort (); | |
3972 | } | |
3973 | new_insn->is_specific_opcode = false; | |
3974 | new_insn->debug_line = old_insn->debug_line; | |
3975 | new_insn->loc_directive_seen = old_insn->loc_directive_seen; | |
3976 | ||
3977 | for (b_op = instr_spec->ops; b_op != NULL; b_op = b_op->next) | |
3978 | { | |
3979 | expressionS *exp; | |
3980 | const expressionS *src_exp; | |
3981 | ||
3982 | num_ops++; | |
3983 | switch (b_op->typ) | |
3984 | { | |
3985 | case OP_CONSTANT: | |
3986 | /* The expression must be the constant. */ | |
3987 | gas_assert (b_op->op_num < MAX_INSN_ARGS); | |
3988 | exp = &new_insn->tok[b_op->op_num]; | |
3989 | set_expr_const (exp, b_op->op_data); | |
3990 | break; | |
3991 | ||
3992 | case OP_OPERAND: | |
3993 | gas_assert (b_op->op_num < MAX_INSN_ARGS); | |
3994 | gas_assert (b_op->op_data < (unsigned) old_insn->ntok); | |
3995 | src_exp = &old_insn->tok[b_op->op_data]; | |
3996 | exp = &new_insn->tok[b_op->op_num]; | |
3997 | copy_expr (exp, src_exp); | |
3998 | break; | |
3999 | ||
4000 | case OP_LITERAL: | |
4001 | case OP_LABEL: | |
4002 | as_bad (_("can't handle generation of literal/labels yet")); | |
4003 | gas_assert (0); | |
4004 | ||
4005 | default: | |
4006 | as_bad (_("can't handle undefined OP TYPE")); | |
4007 | gas_assert (0); | |
4008 | } | |
4009 | } | |
4010 | ||
4011 | new_insn->ntok = num_ops; | |
4012 | return false; | |
4013 | } | |
4014 | ||
4015 | ||
4016 | /* Return TRUE if it was simplified. */ | |
4017 | ||
4018 | static bool | |
4019 | xg_simplify_insn (TInsn *old_insn, TInsn *new_insn) | |
4020 | { | |
4021 | TransitionRule *rule; | |
4022 | BuildInstr *insn_spec; | |
4023 | ||
4024 | if (old_insn->is_specific_opcode || !density_supported) | |
4025 | return false; | |
4026 | ||
4027 | rule = xg_instruction_match (old_insn); | |
4028 | if (rule == NULL) | |
4029 | return false; | |
4030 | ||
4031 | insn_spec = rule->to_instr; | |
4032 | /* There should only be one. */ | |
4033 | gas_assert (insn_spec != NULL); | |
4034 | gas_assert (insn_spec->next == NULL); | |
4035 | if (insn_spec->next != NULL) | |
4036 | return false; | |
4037 | ||
4038 | xg_build_token_insn (insn_spec, old_insn, new_insn); | |
4039 | ||
4040 | return true; | |
4041 | } | |
4042 | ||
4043 | ||
4044 | /* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i -> | |
4045 | l32i.n. (2) Check the number of operands. (3) Place the instruction | |
4046 | tokens into the stack or relax it and place multiple | |
4047 | instructions/literals onto the stack. Return FALSE if no error. */ | |
4048 | ||
4049 | static bool | |
4050 | xg_expand_assembly_insn (IStack *istack, TInsn *orig_insn) | |
4051 | { | |
4052 | int noperands; | |
4053 | TInsn new_insn; | |
4054 | bool do_expand; | |
4055 | ||
4056 | tinsn_init (&new_insn); | |
4057 | ||
4058 | /* Narrow it if we can. xg_simplify_insn now does all the | |
4059 | appropriate checking (e.g., for the density option). */ | |
4060 | if (xg_simplify_insn (orig_insn, &new_insn)) | |
4061 | orig_insn = &new_insn; | |
4062 | ||
4063 | noperands = xtensa_opcode_num_operands (xtensa_default_isa, | |
4064 | orig_insn->opcode); | |
4065 | if (orig_insn->ntok < noperands) | |
4066 | { | |
4067 | as_bad (ngettext ("found %d operand for '%s': Expected %d", | |
4068 | "found %d operands for '%s': Expected %d", | |
4069 | orig_insn->ntok), | |
4070 | orig_insn->ntok, | |
4071 | xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode), | |
4072 | noperands); | |
4073 | return true; | |
4074 | } | |
4075 | if (orig_insn->ntok > noperands) | |
4076 | as_warn (ngettext ("found %d operand for '%s': Expected %d", | |
4077 | "found %d operands for '%s': Expected %d", | |
4078 | orig_insn->ntok), | |
4079 | orig_insn->ntok, | |
4080 | xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode), | |
4081 | noperands); | |
4082 | ||
4083 | /* If there are not enough operands, we will assert above. If there | |
4084 | are too many, just cut out the extras here. */ | |
4085 | orig_insn->ntok = noperands; | |
4086 | ||
4087 | if (tinsn_has_invalid_symbolic_operands (orig_insn)) | |
4088 | return true; | |
4089 | ||
4090 | /* Special case for extui opcode which has constraints not handled | |
4091 | by the ordinary operand encoding checks. The number of operands | |
4092 | and related syntax issues have already been checked. */ | |
4093 | if (orig_insn->opcode == xtensa_extui_opcode) | |
4094 | { | |
4095 | int shiftimm = orig_insn->tok[2].X_add_number; | |
4096 | int maskimm = orig_insn->tok[3].X_add_number; | |
4097 | if (shiftimm + maskimm > 32) | |
4098 | { | |
4099 | as_bad (_("immediate operands sum to greater than 32")); | |
4100 | return true; | |
4101 | } | |
4102 | } | |
4103 | ||
4104 | /* If the instruction will definitely need to be relaxed, it is better | |
4105 | to expand it now for better scheduling. Decide whether to expand | |
4106 | now.... */ | |
4107 | do_expand = (!orig_insn->is_specific_opcode && use_transform ()); | |
4108 | ||
4109 | /* Calls should be expanded to longcalls only in the backend relaxation | |
4110 | so that the assembly scheduler will keep the L32R/CALLX instructions | |
4111 | adjacent. */ | |
4112 | if (is_direct_call_opcode (orig_insn->opcode)) | |
4113 | do_expand = false; | |
4114 | ||
4115 | if (tinsn_has_symbolic_operands (orig_insn)) | |
4116 | { | |
4117 | /* The values of symbolic operands are not known yet, so only expand | |
4118 | now if an operand is "complex" (e.g., difference of symbols) and | |
4119 | will have to be stored as a literal regardless of the value. */ | |
4120 | if (!tinsn_has_complex_operands (orig_insn)) | |
4121 | do_expand = false; | |
4122 | } | |
4123 | else if (xg_immeds_fit (orig_insn)) | |
4124 | do_expand = false; | |
4125 | ||
4126 | if (do_expand) | |
4127 | xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0); | |
4128 | else | |
4129 | istack_push (istack, orig_insn); | |
4130 | ||
4131 | return false; | |
4132 | } | |
4133 | ||
4134 | ||
4135 | /* Return TRUE if the section flags are marked linkonce | |
4136 | or the name is .gnu.linkonce.*. */ | |
4137 | ||
4138 | static int linkonce_len = sizeof (".gnu.linkonce.") - 1; | |
4139 | ||
4140 | static bool | |
4141 | get_is_linkonce_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec) | |
4142 | { | |
4143 | flagword flags, link_once_flags; | |
4144 | ||
4145 | flags = bfd_section_flags (sec); | |
4146 | link_once_flags = (flags & SEC_LINK_ONCE); | |
4147 | ||
4148 | /* Flags might not be set yet. */ | |
4149 | if (!link_once_flags | |
4150 | && strncmp (segment_name (sec), ".gnu.linkonce.", linkonce_len) == 0) | |
4151 | link_once_flags = SEC_LINK_ONCE; | |
4152 | ||
4153 | return (link_once_flags != 0); | |
4154 | } | |
4155 | ||
4156 | ||
4157 | static void | |
4158 | xtensa_add_literal_sym (symbolS *sym) | |
4159 | { | |
4160 | sym_list *l; | |
4161 | ||
4162 | l = XNEW (sym_list); | |
4163 | l->sym = sym; | |
4164 | l->next = literal_syms; | |
4165 | literal_syms = l; | |
4166 | } | |
4167 | ||
4168 | ||
4169 | static symbolS * | |
4170 | xtensa_create_literal_symbol (segT sec, fragS *frag) | |
4171 | { | |
4172 | static int lit_num = 0; | |
4173 | static char name[256]; | |
4174 | symbolS *symbolP; | |
4175 | ||
4176 | sprintf (name, ".L_lit_sym%d", lit_num); | |
4177 | ||
4178 | /* Create a local symbol. If it is in a linkonce section, we have to | |
4179 | be careful to make sure that if it is used in a relocation that the | |
4180 | symbol will be in the output file. */ | |
4181 | if (get_is_linkonce_section (stdoutput, sec)) | |
4182 | { | |
4183 | symbolP = symbol_new (name, sec, frag, 0); | |
4184 | S_CLEAR_EXTERNAL (symbolP); | |
4185 | /* symbolP->local = 1; */ | |
4186 | } | |
4187 | else | |
4188 | symbolP = symbol_new (name, sec, frag, 0); | |
4189 | ||
4190 | xtensa_add_literal_sym (symbolP); | |
4191 | ||
4192 | lit_num++; | |
4193 | return symbolP; | |
4194 | } | |
4195 | ||
4196 | ||
4197 | /* Currently all literals that are generated here are 32-bit L32R targets. */ | |
4198 | ||
4199 | static symbolS * | |
4200 | xg_assemble_literal (/* const */ TInsn *insn) | |
4201 | { | |
4202 | emit_state state; | |
4203 | symbolS *lit_sym = NULL; | |
4204 | bfd_reloc_code_real_type reloc; | |
4205 | bool pcrel = false; | |
4206 | char *p; | |
4207 | ||
4208 | /* size = 4 for L32R. It could easily be larger when we move to | |
4209 | larger constants. Add a parameter later. */ | |
4210 | offsetT litsize = 4; | |
4211 | offsetT litalign = 2; /* 2^2 = 4 */ | |
4212 | expressionS saved_loc; | |
4213 | expressionS * emit_val; | |
4214 | ||
4215 | set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ()); | |
4216 | ||
4217 | gas_assert (insn->insn_type == ITYPE_LITERAL); | |
4218 | gas_assert (insn->ntok == 1); /* must be only one token here */ | |
4219 | ||
4220 | xtensa_switch_to_literal_fragment (&state); | |
4221 | ||
4222 | emit_val = &insn->tok[0]; | |
4223 | if (emit_val->X_op == O_big) | |
4224 | { | |
4225 | int size = emit_val->X_add_number * CHARS_PER_LITTLENUM; | |
4226 | if (size > litsize) | |
4227 | { | |
4228 | /* This happens when someone writes a "movi a2, big_number". */ | |
4229 | as_bad_where (frag_now->fr_file, frag_now->fr_line, | |
4230 | _("invalid immediate")); | |
4231 | xtensa_restore_emit_state (&state); | |
4232 | return NULL; | |
4233 | } | |
4234 | } | |
4235 | ||
4236 | /* Force a 4-byte align here. Note that this opens a new frag, so all | |
4237 | literals done with this function have a frag to themselves. That's | |
4238 | important for the way text section literals work. */ | |
4239 | frag_align (litalign, 0, 0); | |
4240 | record_alignment (now_seg, litalign); | |
4241 | ||
4242 | switch (emit_val->X_op) | |
4243 | { | |
4244 | case O_pcrel: | |
4245 | pcrel = true; | |
4246 | /* fall through */ | |
4247 | case O_pltrel: | |
4248 | case O_tlsfunc: | |
4249 | case O_tlsarg: | |
4250 | case O_tpoff: | |
4251 | case O_dtpoff: | |
4252 | p = frag_more (litsize); | |
4253 | xtensa_set_frag_assembly_state (frag_now); | |
4254 | reloc = map_operator_to_reloc (emit_val->X_op, true); | |
4255 | if (emit_val->X_add_symbol) | |
4256 | emit_val->X_op = O_symbol; | |
4257 | else | |
4258 | emit_val->X_op = O_constant; | |
4259 | fix_new_exp (frag_now, p - frag_now->fr_literal, | |
4260 | litsize, emit_val, pcrel, reloc); | |
4261 | break; | |
4262 | ||
4263 | default: | |
4264 | emit_expr (emit_val, litsize); | |
4265 | break; | |
4266 | } | |
4267 | ||
4268 | gas_assert (frag_now->tc_frag_data.literal_frag == NULL); | |
4269 | frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg); | |
4270 | frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now); | |
4271 | lit_sym = frag_now->fr_symbol; | |
4272 | ||
4273 | /* Go back. */ | |
4274 | xtensa_restore_emit_state (&state); | |
4275 | return lit_sym; | |
4276 | } | |
4277 | ||
4278 | ||
4279 | static void | |
4280 | xg_assemble_literal_space (/* const */ int size, int slot) | |
4281 | { | |
4282 | emit_state state; | |
4283 | /* We might have to do something about this alignment. It only | |
4284 | takes effect if something is placed here. */ | |
4285 | offsetT litalign = 2; /* 2^2 = 4 */ | |
4286 | fragS *lit_saved_frag; | |
4287 | ||
4288 | gas_assert (size % 4 == 0); | |
4289 | ||
4290 | xtensa_switch_to_literal_fragment (&state); | |
4291 | ||
4292 | /* Force a 4-byte align here. */ | |
4293 | frag_align (litalign, 0, 0); | |
4294 | record_alignment (now_seg, litalign); | |
4295 | ||
4296 | frag_grow (size); | |
4297 | ||
4298 | lit_saved_frag = frag_now; | |
4299 | frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg); | |
4300 | frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now); | |
4301 | xg_finish_frag (0, RELAX_LITERAL, 0, size, false); | |
4302 | ||
4303 | /* Go back. */ | |
4304 | xtensa_restore_emit_state (&state); | |
4305 | frag_now->tc_frag_data.literal_frags[slot] = lit_saved_frag; | |
4306 | } | |
4307 | ||
4308 | ||
4309 | /* Put in a fixup record based on the opcode. | |
4310 | Return TRUE on success. */ | |
4311 | ||
4312 | static bool | |
4313 | xg_add_opcode_fix (TInsn *tinsn, | |
4314 | int opnum, | |
4315 | xtensa_format fmt, | |
4316 | int slot, | |
4317 | expressionS *exp, | |
4318 | fragS *fragP, | |
4319 | offsetT offset) | |
4320 | { | |
4321 | xtensa_opcode opcode = tinsn->opcode; | |
4322 | bfd_reloc_code_real_type reloc; | |
4323 | reloc_howto_type *howto; | |
4324 | int fmt_length; | |
4325 | fixS *the_fix; | |
4326 | ||
4327 | reloc = BFD_RELOC_NONE; | |
4328 | ||
4329 | /* First try the special cases for "alternate" relocs. */ | |
4330 | if (opcode == xtensa_l32r_opcode) | |
4331 | { | |
4332 | if (fragP->tc_frag_data.use_absolute_literals) | |
4333 | reloc = encode_alt_reloc (slot); | |
4334 | } | |
4335 | else if (opcode == xtensa_const16_opcode) | |
4336 | { | |
4337 | if (exp->X_op == O_lo16) | |
4338 | { | |
4339 | reloc = encode_reloc (slot); | |
4340 | exp->X_op = O_symbol; | |
4341 | } | |
4342 | else if (exp->X_op == O_hi16) | |
4343 | { | |
4344 | reloc = encode_alt_reloc (slot); | |
4345 | exp->X_op = O_symbol; | |
4346 | } | |
4347 | } | |
4348 | ||
4349 | if (opnum != get_relaxable_immed (opcode)) | |
4350 | { | |
4351 | as_bad (_("invalid relocation for operand %i of '%s'"), | |
4352 | opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode)); | |
4353 | return false; | |
4354 | } | |
4355 | ||
4356 | /* Handle erroneous "@h" and "@l" expressions here before they propagate | |
4357 | into the symbol table where the generic portions of the assembler | |
4358 | won't know what to do with them. */ | |
4359 | if (exp->X_op == O_lo16 || exp->X_op == O_hi16) | |
4360 | { | |
4361 | as_bad (_("invalid expression for operand %i of '%s'"), | |
4362 | opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode)); | |
4363 | return false; | |
4364 | } | |
4365 | ||
4366 | /* Next try the generic relocs. */ | |
4367 | if (reloc == BFD_RELOC_NONE) | |
4368 | reloc = encode_reloc (slot); | |
4369 | if (reloc == BFD_RELOC_NONE) | |
4370 | { | |
4371 | as_bad (_("invalid relocation in instruction slot %i"), slot); | |
4372 | return false; | |
4373 | } | |
4374 | ||
4375 | howto = bfd_reloc_type_lookup (stdoutput, reloc); | |
4376 | if (!howto) | |
4377 | { | |
4378 | as_bad (_("undefined symbol for opcode \"%s\""), | |
4379 | xtensa_opcode_name (xtensa_default_isa, opcode)); | |
4380 | return false; | |
4381 | } | |
4382 | ||
4383 | fmt_length = xtensa_format_length (xtensa_default_isa, fmt); | |
4384 | the_fix = fix_new_exp (fragP, offset, fmt_length, exp, | |
4385 | howto->pc_relative, reloc); | |
4386 | the_fix->fx_no_overflow = 1; | |
4387 | the_fix->tc_fix_data.X_add_symbol = exp->X_add_symbol; | |
4388 | the_fix->tc_fix_data.X_add_number = exp->X_add_number; | |
4389 | the_fix->tc_fix_data.slot = slot; | |
4390 | ||
4391 | return true; | |
4392 | } | |
4393 | ||
4394 | ||
4395 | static bool | |
4396 | xg_emit_insn_to_buf (TInsn *tinsn, | |
4397 | char *buf, | |
4398 | fragS *fragP, | |
4399 | offsetT offset, | |
4400 | bool build_fix) | |
4401 | { | |
4402 | static xtensa_insnbuf insnbuf = NULL; | |
4403 | bool has_symbolic_immed = false; | |
4404 | bool ok = true; | |
4405 | ||
4406 | if (!insnbuf) | |
4407 | insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa); | |
4408 | ||
4409 | has_symbolic_immed = tinsn_to_insnbuf (tinsn, insnbuf); | |
4410 | if (has_symbolic_immed && build_fix) | |
4411 | { | |
4412 | /* Add a fixup. */ | |
4413 | xtensa_format fmt = xg_get_single_format (tinsn->opcode); | |
4414 | int slot = xg_get_single_slot (tinsn->opcode); | |
4415 | int opnum = get_relaxable_immed (tinsn->opcode); | |
4416 | expressionS *exp = &tinsn->tok[opnum]; | |
4417 | ||
4418 | if (!xg_add_opcode_fix (tinsn, opnum, fmt, slot, exp, fragP, offset)) | |
4419 | ok = false; | |
4420 | } | |
4421 | fragP->tc_frag_data.is_insn = true; | |
4422 | xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, | |
4423 | (unsigned char *) buf, 0); | |
4424 | return ok; | |
4425 | } | |
4426 | ||
4427 | ||
4428 | static void | |
4429 | xg_resolve_literals (TInsn *insn, symbolS *lit_sym) | |
4430 | { | |
4431 | symbolS *sym = get_special_literal_symbol (); | |
4432 | int i; | |
4433 | if (lit_sym == 0) | |
4434 | return; | |
4435 | gas_assert (insn->insn_type == ITYPE_INSN); | |
4436 | for (i = 0; i < insn->ntok; i++) | |
4437 | if (insn->tok[i].X_add_symbol == sym) | |
4438 | insn->tok[i].X_add_symbol = lit_sym; | |
4439 | ||
4440 | } | |
4441 | ||
4442 | ||
4443 | static void | |
4444 | xg_resolve_labels (TInsn *insn, symbolS *label_sym) | |
4445 | { | |
4446 | symbolS *sym = get_special_label_symbol (); | |
4447 | int i; | |
4448 | for (i = 0; i < insn->ntok; i++) | |
4449 | if (insn->tok[i].X_add_symbol == sym) | |
4450 | insn->tok[i].X_add_symbol = label_sym; | |
4451 | ||
4452 | } | |
4453 | ||
4454 | ||
4455 | /* Return TRUE if the instruction can write to the specified | |
4456 | integer register. */ | |
4457 | ||
4458 | static bool | |
4459 | is_register_writer (const TInsn *insn, const char *regset, int regnum) | |
4460 | { | |
4461 | int i; | |
4462 | int num_ops; | |
4463 | xtensa_isa isa = xtensa_default_isa; | |
4464 | ||
4465 | num_ops = xtensa_opcode_num_operands (isa, insn->opcode); | |
4466 | ||
4467 | for (i = 0; i < num_ops; i++) | |
4468 | { | |
4469 | char inout; | |
4470 | inout = xtensa_operand_inout (isa, insn->opcode, i); | |
4471 | if ((inout == 'o' || inout == 'm') | |
4472 | && xtensa_operand_is_register (isa, insn->opcode, i) == 1) | |
4473 | { | |
4474 | xtensa_regfile opnd_rf = | |
4475 | xtensa_operand_regfile (isa, insn->opcode, i); | |
4476 | if (!strcmp (xtensa_regfile_shortname (isa, opnd_rf), regset)) | |
4477 | { | |
4478 | if ((insn->tok[i].X_op == O_register) | |
4479 | && (insn->tok[i].X_add_number == regnum)) | |
4480 | return true; | |
4481 | } | |
4482 | } | |
4483 | } | |
4484 | return false; | |
4485 | } | |
4486 | ||
4487 | ||
4488 | static bool | |
4489 | is_bad_loopend_opcode (const TInsn *tinsn) | |
4490 | { | |
4491 | xtensa_opcode opcode = tinsn->opcode; | |
4492 | ||
4493 | if (opcode == XTENSA_UNDEFINED) | |
4494 | return false; | |
4495 | ||
4496 | if (opcode == xtensa_call0_opcode | |
4497 | || opcode == xtensa_callx0_opcode | |
4498 | || opcode == xtensa_call4_opcode | |
4499 | || opcode == xtensa_callx4_opcode | |
4500 | || opcode == xtensa_call8_opcode | |
4501 | || opcode == xtensa_callx8_opcode | |
4502 | || opcode == xtensa_call12_opcode | |
4503 | || opcode == xtensa_callx12_opcode | |
4504 | || opcode == xtensa_isync_opcode | |
4505 | || opcode == xtensa_ret_opcode | |
4506 | || opcode == xtensa_ret_n_opcode | |
4507 | || opcode == xtensa_retw_opcode | |
4508 | || opcode == xtensa_retw_n_opcode | |
4509 | || opcode == xtensa_waiti_opcode | |
4510 | || opcode == xtensa_rsr_lcount_opcode) | |
4511 | return true; | |
4512 | ||
4513 | return false; | |
4514 | } | |
4515 | ||
4516 | ||
4517 | /* Labels that begin with ".Ln" or ".LM" are unaligned. | |
4518 | This allows the debugger to add unaligned labels. | |
4519 | Also, the assembler generates stabs labels that need | |
4520 | not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */ | |
4521 | ||
4522 | static bool | |
4523 | is_unaligned_label (symbolS *sym) | |
4524 | { | |
4525 | const char *name = S_GET_NAME (sym); | |
4526 | static size_t fake_size = 0; | |
4527 | ||
4528 | if (name | |
4529 | && name[0] == '.' | |
4530 | && name[1] == 'L' && (name[2] == 'n' || name[2] == 'M')) | |
4531 | return true; | |
4532 | ||
4533 | /* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */ | |
4534 | if (fake_size == 0) | |
4535 | fake_size = strlen (FAKE_LABEL_NAME); | |
4536 | ||
4537 | if (name | |
4538 | && strncmp (FAKE_LABEL_NAME, name, fake_size) == 0 | |
4539 | && (name[fake_size] == 'F' | |
4540 | || name[fake_size] == 'L' | |
4541 | || (name[fake_size] == 'e' | |
4542 | && startswith (name + fake_size, "endfunc")))) | |
4543 | return true; | |
4544 | ||
4545 | return false; | |
4546 | } | |
4547 | ||
4548 | ||
4549 | static fragS * | |
4550 | next_non_empty_frag (const fragS *fragP) | |
4551 | { | |
4552 | fragS *next_fragP = fragP->fr_next; | |
4553 | ||
4554 | /* Sometimes an empty will end up here due storage allocation issues. | |
4555 | So we have to skip until we find something legit. */ | |
4556 | while (next_fragP && next_fragP->fr_fix == 0) | |
4557 | next_fragP = next_fragP->fr_next; | |
4558 | ||
4559 | if (next_fragP == NULL || next_fragP->fr_fix == 0) | |
4560 | return NULL; | |
4561 | ||
4562 | return next_fragP; | |
4563 | } | |
4564 | ||
4565 | ||
4566 | static bool | |
4567 | next_frag_opcode_is_loop (const fragS *fragP, xtensa_opcode *opcode) | |
4568 | { | |
4569 | xtensa_opcode out_opcode; | |
4570 | const fragS *next_fragP = next_non_empty_frag (fragP); | |
4571 | ||
4572 | if (next_fragP == NULL) | |
4573 | return false; | |
4574 | ||
4575 | out_opcode = get_opcode_from_buf (next_fragP->fr_literal, 0); | |
4576 | if (xtensa_opcode_is_loop (xtensa_default_isa, out_opcode) == 1) | |
4577 | { | |
4578 | *opcode = out_opcode; | |
4579 | return true; | |
4580 | } | |
4581 | return false; | |
4582 | } | |
4583 | ||
4584 | ||
4585 | static int | |
4586 | frag_format_size (const fragS *fragP) | |
4587 | { | |
4588 | static xtensa_insnbuf insnbuf = NULL; | |
4589 | xtensa_isa isa = xtensa_default_isa; | |
4590 | xtensa_format fmt; | |
4591 | int fmt_size; | |
4592 | ||
4593 | if (!insnbuf) | |
4594 | insnbuf = xtensa_insnbuf_alloc (isa); | |
4595 | ||
4596 | if (fragP == NULL) | |
4597 | return XTENSA_UNDEFINED; | |
4598 | ||
4599 | xtensa_insnbuf_from_chars (isa, insnbuf, | |
4600 | (unsigned char *) fragP->fr_literal, 0); | |
4601 | ||
4602 | fmt = xtensa_format_decode (isa, insnbuf); | |
4603 | if (fmt == XTENSA_UNDEFINED) | |
4604 | return XTENSA_UNDEFINED; | |
4605 | fmt_size = xtensa_format_length (isa, fmt); | |
4606 | ||
4607 | /* If the next format won't be changing due to relaxation, just | |
4608 | return the length of the first format. */ | |
4609 | if (fragP->fr_opcode != fragP->fr_literal) | |
4610 | return fmt_size; | |
4611 | ||
4612 | /* If during relaxation we have to pull an instruction out of a | |
4613 | multi-slot instruction, we will return the more conservative | |
4614 | number. This works because alignment on bigger instructions | |
4615 | is more restrictive than alignment on smaller instructions. | |
4616 | This is more conservative than we would like, but it happens | |
4617 | infrequently. */ | |
4618 | ||
4619 | if (xtensa_format_num_slots (xtensa_default_isa, fmt) > 1) | |
4620 | return fmt_size; | |
4621 | ||
4622 | /* If we aren't doing one of our own relaxations or it isn't | |
4623 | slot-based, then the insn size won't change. */ | |
4624 | if (fragP->fr_type != rs_machine_dependent) | |
4625 | return fmt_size; | |
4626 | if (fragP->fr_subtype != RELAX_SLOTS) | |
4627 | return fmt_size; | |
4628 | ||
4629 | /* If an instruction is about to grow, return the longer size. */ | |
4630 | if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP1 | |
4631 | || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP2 | |
4632 | || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP3) | |
4633 | { | |
4634 | /* For most frags at RELAX_IMMED_STEPX, with X > 0, the first | |
4635 | instruction in the relaxed version is of length 3. (The case | |
4636 | where we have to pull the instruction out of a FLIX bundle | |
4637 | is handled conservatively above.) However, frags with opcodes | |
4638 | that are expanding to wide branches end up having formats that | |
4639 | are not determinable by the RELAX_IMMED_STEPX enumeration, and | |
4640 | we can't tell directly what format the relaxer picked. This | |
4641 | is a wart in the design of the relaxer that should someday be | |
4642 | fixed, but would require major changes, or at least should | |
4643 | be accompanied by major changes to make use of that data. | |
4644 | ||
4645 | In any event, we can tell that we are expanding from a single-slot | |
4646 | format to a wider one with the logic below. */ | |
4647 | ||
4648 | int i; | |
4649 | int relaxed_size = fmt_size + fragP->tc_frag_data.text_expansion[0]; | |
4650 | ||
4651 | for (i = 0; i < xtensa_isa_num_formats (isa); i++) | |
4652 | { | |
4653 | if (relaxed_size == xtensa_format_length (isa, i)) | |
4654 | return relaxed_size; | |
4655 | } | |
4656 | ||
4657 | return 3; | |
4658 | } | |
4659 | ||
4660 | if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW) | |
4661 | return 2 + fragP->tc_frag_data.text_expansion[0]; | |
4662 | ||
4663 | return fmt_size; | |
4664 | } | |
4665 | ||
4666 | ||
4667 | static int | |
4668 | next_frag_format_size (const fragS *fragP) | |
4669 | { | |
4670 | const fragS *next_fragP = next_non_empty_frag (fragP); | |
4671 | return frag_format_size (next_fragP); | |
4672 | } | |
4673 | ||
4674 | ||
4675 | /* In early Xtensa Processors, for reasons that are unclear, the ISA | |
4676 | required two-byte instructions to be treated as three-byte instructions | |
4677 | for loop instruction alignment. This restriction was removed beginning | |
4678 | with Xtensa LX. Now the only requirement on loop instruction alignment | |
4679 | is that the first instruction of the loop must appear at an address that | |
4680 | does not cross a fetch boundary. */ | |
4681 | ||
4682 | static int | |
4683 | get_loop_align_size (int insn_size) | |
4684 | { | |
4685 | if (insn_size == XTENSA_UNDEFINED) | |
4686 | return xtensa_fetch_width; | |
4687 | ||
4688 | if (enforce_three_byte_loop_align && insn_size == 2) | |
4689 | return 3; | |
4690 | ||
4691 | return insn_size; | |
4692 | } | |
4693 | ||
4694 | ||
4695 | /* If the next legit fragment is an end-of-loop marker, | |
4696 | switch its state so it will instantiate a NOP. */ | |
4697 | ||
4698 | static void | |
4699 | update_next_frag_state (fragS *fragP) | |
4700 | { | |
4701 | fragS *next_fragP = fragP->fr_next; | |
4702 | fragS *new_target = NULL; | |
4703 | ||
4704 | if (align_targets) | |
4705 | { | |
4706 | /* We are guaranteed there will be one of these... */ | |
4707 | while (!(next_fragP->fr_type == rs_machine_dependent | |
4708 | && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE | |
4709 | || next_fragP->fr_subtype == RELAX_UNREACHABLE))) | |
4710 | next_fragP = next_fragP->fr_next; | |
4711 | ||
4712 | gas_assert (next_fragP->fr_type == rs_machine_dependent | |
4713 | && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE | |
4714 | || next_fragP->fr_subtype == RELAX_UNREACHABLE)); | |
4715 | ||
4716 | /* ...and one of these. */ | |
4717 | new_target = next_fragP->fr_next; | |
4718 | while (!(new_target->fr_type == rs_machine_dependent | |
4719 | && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN | |
4720 | || new_target->fr_subtype == RELAX_DESIRE_ALIGN))) | |
4721 | new_target = new_target->fr_next; | |
4722 | ||
4723 | gas_assert (new_target->fr_type == rs_machine_dependent | |
4724 | && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN | |
4725 | || new_target->fr_subtype == RELAX_DESIRE_ALIGN)); | |
4726 | } | |
4727 | ||
4728 | while (next_fragP && next_fragP->fr_fix == 0) | |
4729 | { | |
4730 | if (next_fragP->fr_type == rs_machine_dependent | |
4731 | && next_fragP->fr_subtype == RELAX_LOOP_END) | |
4732 | { | |
4733 | next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP; | |
4734 | return; | |
4735 | } | |
4736 | ||
4737 | next_fragP = next_fragP->fr_next; | |
4738 | } | |
4739 | } | |
4740 | ||
4741 | ||
4742 | static bool | |
4743 | next_frag_is_branch_target (const fragS *fragP) | |
4744 | { | |
4745 | /* Sometimes an empty will end up here due to storage allocation issues, | |
4746 | so we have to skip until we find something legit. */ | |
4747 | for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next) | |
4748 | { | |
4749 | if (fragP->tc_frag_data.is_branch_target) | |
4750 | return true; | |
4751 | if (fragP->fr_fix != 0) | |
4752 | break; | |
4753 | } | |
4754 | return false; | |
4755 | } | |
4756 | ||
4757 | ||
4758 | static bool | |
4759 | next_frag_is_loop_target (const fragS *fragP) | |
4760 | { | |
4761 | /* Sometimes an empty will end up here due storage allocation issues. | |
4762 | So we have to skip until we find something legit. */ | |
4763 | for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next) | |
4764 | { | |
4765 | if (fragP->tc_frag_data.is_loop_target) | |
4766 | return true; | |
4767 | if (fragP->fr_fix != 0) | |
4768 | break; | |
4769 | } | |
4770 | return false; | |
4771 | } | |
4772 | ||
4773 | ||
4774 | /* As specified in the relaxation table, when a loop instruction is | |
4775 | relaxed, there are 24 bytes between the loop instruction itself and | |
4776 | the first instruction in the loop. */ | |
4777 | ||
4778 | #define RELAXED_LOOP_INSN_BYTES 24 | |
4779 | ||
4780 | static addressT | |
4781 | next_frag_pre_opcode_bytes (const fragS *fragp) | |
4782 | { | |
4783 | const fragS *next_fragp = fragp->fr_next; | |
4784 | xtensa_opcode next_opcode; | |
4785 | ||
4786 | if (!next_frag_opcode_is_loop (fragp, &next_opcode)) | |
4787 | return 0; | |
4788 | ||
4789 | /* Sometimes an empty will end up here due to storage allocation issues, | |
4790 | so we have to skip until we find something legit. */ | |
4791 | while (next_fragp->fr_fix == 0) | |
4792 | next_fragp = next_fragp->fr_next; | |
4793 | ||
4794 | if (next_fragp->fr_type != rs_machine_dependent) | |
4795 | return 0; | |
4796 | ||
4797 | /* There is some implicit knowledge encoded in here. | |
4798 | The LOOP instructions that are NOT RELAX_IMMED have | |
4799 | been relaxed. Note that we can assume that the LOOP | |
4800 | instruction is in slot 0 because loops aren't bundleable. */ | |
4801 | if (next_fragp->tc_frag_data.slot_subtypes[0] > RELAX_IMMED) | |
4802 | return get_expanded_loop_offset (next_opcode) + RELAXED_LOOP_INSN_BYTES; | |
4803 | ||
4804 | return 0; | |
4805 | } | |
4806 | ||
4807 | ||
4808 | /* Mark a location where we can later insert literal frags. Update | |
4809 | the section's literal_pool_loc, so subsequent literals can be | |
4810 | placed nearest to their use. */ | |
4811 | ||
4812 | static void | |
4813 | xtensa_mark_literal_pool_location (void) | |
4814 | { | |
4815 | /* Any labels pointing to the current location need | |
4816 | to be adjusted to after the literal pool. */ | |
4817 | fragS *pool_location; | |
4818 | ||
4819 | if (use_literal_section) | |
4820 | return; | |
4821 | ||
4822 | /* We stash info in these frags so we can later move the literal's | |
4823 | fixes into this frchain's fix list. */ | |
4824 | pool_location = frag_now; | |
4825 | frag_now->tc_frag_data.lit_frchain = frchain_now; | |
4826 | frag_now->tc_frag_data.literal_frag = frag_now; | |
4827 | /* Just record this frag. */ | |
4828 | xtensa_maybe_create_literal_pool_frag (false, false); | |
4829 | frag_variant (rs_machine_dependent, 0, 0, | |
4830 | RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL); | |
4831 | xtensa_set_frag_assembly_state (frag_now); | |
4832 | frag_now->tc_frag_data.lit_seg = now_seg; | |
4833 | frag_variant (rs_machine_dependent, 0, 0, | |
4834 | RELAX_LITERAL_POOL_END, NULL, 0, NULL); | |
4835 | xtensa_set_frag_assembly_state (frag_now); | |
4836 | ||
4837 | set_literal_pool_location (now_seg, pool_location); | |
4838 | } | |
4839 | ||
4840 | ||
4841 | /* Build a nop of the correct size into tinsn. */ | |
4842 | ||
4843 | static void | |
4844 | build_nop (TInsn *tinsn, int size) | |
4845 | { | |
4846 | tinsn_init (tinsn); | |
4847 | switch (size) | |
4848 | { | |
4849 | case 2: | |
4850 | tinsn->opcode = xtensa_nop_n_opcode; | |
4851 | tinsn->ntok = 0; | |
4852 | if (tinsn->opcode == XTENSA_UNDEFINED) | |
4853 | as_fatal (_("opcode 'NOP.N' unavailable in this configuration")); | |
4854 | break; | |
4855 | ||
4856 | case 3: | |
4857 | if (xtensa_nop_opcode == XTENSA_UNDEFINED) | |
4858 | { | |
4859 | tinsn->opcode = xtensa_or_opcode; | |
4860 | set_expr_const (&tinsn->tok[0], 1); | |
4861 | set_expr_const (&tinsn->tok[1], 1); | |
4862 | set_expr_const (&tinsn->tok[2], 1); | |
4863 | tinsn->ntok = 3; | |
4864 | } | |
4865 | else | |
4866 | tinsn->opcode = xtensa_nop_opcode; | |
4867 | ||
4868 | gas_assert (tinsn->opcode != XTENSA_UNDEFINED); | |
4869 | } | |
4870 | } | |
4871 | ||
4872 | ||
4873 | /* Assemble a NOP of the requested size in the buffer. User must have | |
4874 | allocated "buf" with at least "size" bytes. */ | |
4875 | ||
4876 | static void | |
4877 | assemble_nop (int size, char *buf) | |
4878 | { | |
4879 | static xtensa_insnbuf insnbuf = NULL; | |
4880 | TInsn tinsn; | |
4881 | ||
4882 | build_nop (&tinsn, size); | |
4883 | ||
4884 | if (!insnbuf) | |
4885 | insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa); | |
4886 | ||
4887 | tinsn_to_insnbuf (&tinsn, insnbuf); | |
4888 | xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, | |
4889 | (unsigned char *) buf, 0); | |
4890 | } | |
4891 | ||
4892 | ||
4893 | /* Return the number of bytes for the offset of the expanded loop | |
4894 | instruction. This should be incorporated into the relaxation | |
4895 | specification but is hard-coded here. This is used to auto-align | |
4896 | the loop instruction. It is invalid to call this function if the | |
4897 | configuration does not have loops or if the opcode is not a loop | |
4898 | opcode. */ | |
4899 | ||
4900 | static addressT | |
4901 | get_expanded_loop_offset (xtensa_opcode opcode) | |
4902 | { | |
4903 | /* This is the OFFSET of the loop instruction in the expanded loop. | |
4904 | This MUST correspond directly to the specification of the loop | |
4905 | expansion. It will be validated on fragment conversion. */ | |
4906 | gas_assert (opcode != XTENSA_UNDEFINED); | |
4907 | if (opcode == xtensa_loop_opcode) | |
4908 | return 0; | |
4909 | if (opcode == xtensa_loopnez_opcode) | |
4910 | return 3; | |
4911 | if (opcode == xtensa_loopgtz_opcode) | |
4912 | return 6; | |
4913 | as_fatal (_("get_expanded_loop_offset: invalid opcode")); | |
4914 | return 0; | |
4915 | } | |
4916 | ||
4917 | ||
4918 | static fragS * | |
4919 | get_literal_pool_location (segT seg) | |
4920 | { | |
4921 | if (auto_litpools) | |
4922 | { | |
4923 | struct litpool_seg *lps = litpool_seg_list.next; | |
4924 | struct litpool_frag *lpf; | |
4925 | for ( ; lps && lps->seg->id != seg->id; lps = lps->next) | |
4926 | ; | |
4927 | if (lps) | |
4928 | { | |
4929 | for (lpf = lps->frag_list.prev; lpf->fragP; lpf = lpf->prev) | |
4930 | { /* Skip "candidates" for now. */ | |
4931 | if (lpf->fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN && | |
4932 | lpf->priority == 1) | |
4933 | return lpf->fragP; | |
4934 | } | |
4935 | /* Must convert a lower-priority pool. */ | |
4936 | for (lpf = lps->frag_list.prev; lpf->fragP; lpf = lpf->prev) | |
4937 | { | |
4938 | if (lpf->fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN) | |
4939 | return lpf->fragP; | |
4940 | } | |
4941 | /* Still no match -- try for a low priority pool. */ | |
4942 | for (lpf = lps->frag_list.prev; lpf->fragP; lpf = lpf->prev) | |
4943 | { | |
4944 | if (lpf->fragP->fr_subtype == RELAX_LITERAL_POOL_CANDIDATE_BEGIN) | |
4945 | return lpf->fragP; | |
4946 | } | |
4947 | } | |
4948 | } | |
4949 | return seg_info (seg)->tc_segment_info_data.literal_pool_loc; | |
4950 | } | |
4951 | ||
4952 | ||
4953 | static void | |
4954 | set_literal_pool_location (segT seg, fragS *literal_pool_loc) | |
4955 | { | |
4956 | seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc; | |
4957 | } | |
4958 | ||
4959 | ||
4960 | /* Set frag assembly state should be called when a new frag is | |
4961 | opened and after a frag has been closed. */ | |
4962 | ||
4963 | static void | |
4964 | xtensa_set_frag_assembly_state (fragS *fragP) | |
4965 | { | |
4966 | if (!density_supported) | |
4967 | fragP->tc_frag_data.is_no_density = true; | |
4968 | ||
4969 | /* This function is called from subsegs_finish, which is called | |
4970 | after xtensa_md_finish, so we can't use "use_transform" or | |
4971 | "use_schedule" here. */ | |
4972 | if (!directive_state[directive_transform]) | |
4973 | fragP->tc_frag_data.is_no_transform = true; | |
4974 | if (directive_state[directive_longcalls]) | |
4975 | fragP->tc_frag_data.use_longcalls = true; | |
4976 | fragP->tc_frag_data.use_absolute_literals = | |
4977 | directive_state[directive_absolute_literals]; | |
4978 | fragP->tc_frag_data.is_assembly_state_set = true; | |
4979 | } | |
4980 | ||
4981 | ||
4982 | static bool | |
4983 | relaxable_section (asection *sec) | |
4984 | { | |
4985 | return ((sec->flags & SEC_DEBUGGING) == 0 | |
4986 | && strcmp (sec->name, ".eh_frame") != 0); | |
4987 | } | |
4988 | ||
4989 | ||
4990 | static void | |
4991 | xtensa_mark_frags_for_org (void) | |
4992 | { | |
4993 | segT *seclist; | |
4994 | ||
4995 | /* Walk over each fragment of all of the current segments. If we find | |
4996 | a .org frag in any of the segments, mark all frags prior to it as | |
4997 | "no transform", which will prevent linker optimizations from messing | |
4998 | up the .org distance. This should be done after | |
4999 | xtensa_find_unmarked_state_frags, because we don't want to worry here | |
5000 | about that function trashing the data we save here. */ | |
5001 | ||
5002 | for (seclist = &stdoutput->sections; | |
5003 | seclist && *seclist; | |
5004 | seclist = &(*seclist)->next) | |
5005 | { | |
5006 | segT sec = *seclist; | |
5007 | segment_info_type *seginfo; | |
5008 | fragS *fragP; | |
5009 | flagword flags; | |
5010 | flags = bfd_section_flags (sec); | |
5011 | if (flags & SEC_DEBUGGING) | |
5012 | continue; | |
5013 | if (!(flags & SEC_ALLOC)) | |
5014 | continue; | |
5015 | ||
5016 | seginfo = seg_info (sec); | |
5017 | if (seginfo && seginfo->frchainP) | |
5018 | { | |
5019 | fragS *last_fragP = seginfo->frchainP->frch_root; | |
5020 | for (fragP = seginfo->frchainP->frch_root; fragP; | |
5021 | fragP = fragP->fr_next) | |
5022 | { | |
5023 | /* cvt_frag_to_fill has changed the fr_type of org frags to | |
5024 | rs_fill, so use the value as cached in rs_subtype here. */ | |
5025 | if (fragP->fr_subtype == RELAX_ORG) | |
5026 | { | |
5027 | while (last_fragP != fragP->fr_next) | |
5028 | { | |
5029 | last_fragP->tc_frag_data.is_no_transform = true; | |
5030 | last_fragP = last_fragP->fr_next; | |
5031 | } | |
5032 | } | |
5033 | } | |
5034 | } | |
5035 | } | |
5036 | } | |
5037 | ||
5038 | ||
5039 | static void | |
5040 | xtensa_find_unmarked_state_frags (void) | |
5041 | { | |
5042 | segT *seclist; | |
5043 | ||
5044 | /* Walk over each fragment of all of the current segments. For each | |
5045 | unmarked fragment, mark it with the same info as the previous | |
5046 | fragment. */ | |
5047 | for (seclist = &stdoutput->sections; | |
5048 | seclist && *seclist; | |
5049 | seclist = &(*seclist)->next) | |
5050 | { | |
5051 | segT sec = *seclist; | |
5052 | segment_info_type *seginfo; | |
5053 | fragS *fragP; | |
5054 | flagword flags; | |
5055 | flags = bfd_section_flags (sec); | |
5056 | if (flags & SEC_DEBUGGING) | |
5057 | continue; | |
5058 | if (!(flags & SEC_ALLOC)) | |
5059 | continue; | |
5060 | ||
5061 | seginfo = seg_info (sec); | |
5062 | if (seginfo && seginfo->frchainP) | |
5063 | { | |
5064 | fragS *last_fragP = 0; | |
5065 | for (fragP = seginfo->frchainP->frch_root; fragP; | |
5066 | fragP = fragP->fr_next) | |
5067 | { | |
5068 | if (fragP->fr_fix != 0 | |
5069 | && !fragP->tc_frag_data.is_assembly_state_set) | |
5070 | { | |
5071 | if (last_fragP == 0) | |
5072 | { | |
5073 | as_warn_where (fragP->fr_file, fragP->fr_line, | |
5074 | _("assembly state not set for first frag in section %s"), | |
5075 | sec->name); | |
5076 | } | |
5077 | else | |
5078 | { | |
5079 | fragP->tc_frag_data.is_assembly_state_set = true; | |
5080 | fragP->tc_frag_data.is_no_density = | |
5081 | last_fragP->tc_frag_data.is_no_density; | |
5082 | fragP->tc_frag_data.is_no_transform = | |
5083 | last_fragP->tc_frag_data.is_no_transform; | |
5084 | fragP->tc_frag_data.use_longcalls = | |
5085 | last_fragP->tc_frag_data.use_longcalls; | |
5086 | fragP->tc_frag_data.use_absolute_literals = | |
5087 | last_fragP->tc_frag_data.use_absolute_literals; | |
5088 | } | |
5089 | } | |
5090 | if (fragP->tc_frag_data.is_assembly_state_set) | |
5091 | last_fragP = fragP; | |
5092 | } | |
5093 | } | |
5094 | } | |
5095 | } | |
5096 | ||
5097 | ||
5098 | static void | |
5099 | xtensa_find_unaligned_branch_targets (bfd *abfd ATTRIBUTE_UNUSED, | |
5100 | asection *sec, | |
5101 | void *unused ATTRIBUTE_UNUSED) | |
5102 | { | |
5103 | flagword flags = bfd_section_flags (sec); | |
5104 | segment_info_type *seginfo = seg_info (sec); | |
5105 | fragS *frag = seginfo->frchainP->frch_root; | |
5106 | ||
5107 | if (flags & SEC_CODE) | |
5108 | { | |
5109 | xtensa_isa isa = xtensa_default_isa; | |
5110 | xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa); | |
5111 | while (frag != NULL) | |
5112 | { | |
5113 | if (frag->tc_frag_data.is_branch_target) | |
5114 | { | |
5115 | int op_size; | |
5116 | addressT branch_align, frag_addr; | |
5117 | xtensa_format fmt; | |
5118 | ||
5119 | xtensa_insnbuf_from_chars | |
5120 | (isa, insnbuf, (unsigned char *) frag->fr_literal, 0); | |
5121 | fmt = xtensa_format_decode (isa, insnbuf); | |
5122 | op_size = xtensa_format_length (isa, fmt); | |
5123 | branch_align = 1 << branch_align_power (sec); | |
5124 | frag_addr = frag->fr_address % branch_align; | |
5125 | if (frag_addr + op_size > branch_align) | |
5126 | as_warn_where (frag->fr_file, frag->fr_line, | |
5127 | _("unaligned branch target: %d bytes at 0x%lx"), | |
5128 | op_size, (long) frag->fr_address); | |
5129 | } | |
5130 | frag = frag->fr_next; | |
5131 | } | |
5132 | xtensa_insnbuf_free (isa, insnbuf); | |
5133 | } | |
5134 | } | |
5135 | ||
5136 | ||
5137 | static void | |
5138 | xtensa_find_unaligned_loops (bfd *abfd ATTRIBUTE_UNUSED, | |
5139 | asection *sec, | |
5140 | void *unused ATTRIBUTE_UNUSED) | |
5141 | { | |
5142 | flagword flags = bfd_section_flags (sec); | |
5143 | segment_info_type *seginfo = seg_info (sec); | |
5144 | fragS *frag = seginfo->frchainP->frch_root; | |
5145 | xtensa_isa isa = xtensa_default_isa; | |
5146 | ||
5147 | if (flags & SEC_CODE) | |
5148 | { | |
5149 | xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa); | |
5150 | while (frag != NULL) | |
5151 | { | |
5152 | if (frag->tc_frag_data.is_first_loop_insn) | |
5153 | { | |
5154 | int op_size; | |
5155 | addressT frag_addr; | |
5156 | xtensa_format fmt; | |
5157 | ||
5158 | if (frag->fr_fix == 0) | |
5159 | frag = next_non_empty_frag (frag); | |
5160 | ||
5161 | if (frag) | |
5162 | { | |
5163 | xtensa_insnbuf_from_chars | |
5164 | (isa, insnbuf, (unsigned char *) frag->fr_literal, 0); | |
5165 | fmt = xtensa_format_decode (isa, insnbuf); | |
5166 | op_size = xtensa_format_length (isa, fmt); | |
5167 | frag_addr = frag->fr_address % xtensa_fetch_width; | |
5168 | ||
5169 | if (frag_addr + op_size > xtensa_fetch_width) | |
5170 | as_warn_where (frag->fr_file, frag->fr_line, | |
5171 | _("unaligned loop: %d bytes at 0x%lx"), | |
5172 | op_size, (long) frag->fr_address); | |
5173 | } | |
5174 | } | |
5175 | frag = frag->fr_next; | |
5176 | } | |
5177 | xtensa_insnbuf_free (isa, insnbuf); | |
5178 | } | |
5179 | } | |
5180 | ||
5181 | ||
5182 | static int | |
5183 | xg_apply_fix_value (fixS *fixP, valueT val) | |
5184 | { | |
5185 | xtensa_isa isa = xtensa_default_isa; | |
5186 | static xtensa_insnbuf insnbuf = NULL; | |
5187 | static xtensa_insnbuf slotbuf = NULL; | |
5188 | xtensa_format fmt; | |
5189 | int slot; | |
5190 | bool alt_reloc; | |
5191 | xtensa_opcode opcode; | |
5192 | char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where; | |
5193 | ||
5194 | if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc) | |
5195 | || alt_reloc) | |
5196 | as_fatal (_("unexpected fix")); | |
5197 | ||
5198 | if (!insnbuf) | |
5199 | { | |
5200 | insnbuf = xtensa_insnbuf_alloc (isa); | |
5201 | slotbuf = xtensa_insnbuf_alloc (isa); | |
5202 | } | |
5203 | ||
5204 | xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) fixpos, 0); | |
5205 | fmt = xtensa_format_decode (isa, insnbuf); | |
5206 | if (fmt == XTENSA_UNDEFINED) | |
5207 | as_fatal (_("undecodable fix")); | |
5208 | xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf); | |
5209 | opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf); | |
5210 | if (opcode == XTENSA_UNDEFINED) | |
5211 | as_fatal (_("undecodable fix")); | |
5212 | ||
5213 | /* CONST16 immediates are not PC-relative, despite the fact that we | |
5214 | reuse the normal PC-relative operand relocations for the low part | |
5215 | of a CONST16 operand. */ | |
5216 | if (opcode == xtensa_const16_opcode) | |
5217 | return 0; | |
5218 | ||
5219 | xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode, | |
5220 | get_relaxable_immed (opcode), val, | |
5221 | fixP->fx_file, fixP->fx_line); | |
5222 | ||
5223 | xtensa_format_set_slot (isa, fmt, slot, insnbuf, slotbuf); | |
5224 | xtensa_insnbuf_to_chars (isa, insnbuf, (unsigned char *) fixpos, 0); | |
5225 | ||
5226 | return 1; | |
5227 | } | |
5228 | ||
5229 | \f | |
5230 | /* External Functions and Other GAS Hooks. */ | |
5231 | ||
5232 | const char * | |
5233 | xtensa_target_format (void) | |
5234 | { | |
5235 | return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le"); | |
5236 | } | |
5237 | ||
5238 | ||
5239 | void | |
5240 | xtensa_file_arch_init (bfd *abfd) | |
5241 | { | |
5242 | bfd_set_private_flags (abfd, 0x100 | 0x200); | |
5243 | } | |
5244 | ||
5245 | ||
5246 | void | |
5247 | md_number_to_chars (char *buf, valueT val, int n) | |
5248 | { | |
5249 | if (target_big_endian) | |
5250 | number_to_chars_bigendian (buf, val, n); | |
5251 | else | |
5252 | number_to_chars_littleendian (buf, val, n); | |
5253 | } | |
5254 | ||
5255 | static void | |
5256 | xg_init_global_config (void) | |
5257 | { | |
5258 | target_big_endian = XCHAL_HAVE_BE; | |
5259 | ||
5260 | density_supported = XCHAL_HAVE_DENSITY; | |
5261 | absolute_literals_supported = XSHAL_USE_ABSOLUTE_LITERALS; | |
5262 | xtensa_fetch_width = XCHAL_INST_FETCH_WIDTH; | |
5263 | ||
5264 | directive_state[directive_density] = XCHAL_HAVE_DENSITY; | |
5265 | directive_state[directive_absolute_literals] = XSHAL_USE_ABSOLUTE_LITERALS; | |
5266 | ||
5267 | microarch_earliest = XTENSA_MARCH_EARLIEST; | |
5268 | } | |
5269 | ||
5270 | void | |
5271 | xtensa_init (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) | |
5272 | { | |
5273 | xg_init_global_config (); | |
5274 | } | |
5275 | ||
5276 | /* This function is called once, at assembler startup time. It should | |
5277 | set up all the tables, etc. that the MD part of the assembler will | |
5278 | need. */ | |
5279 | ||
5280 | void | |
5281 | md_begin (void) | |
5282 | { | |
5283 | segT current_section = now_seg; | |
5284 | int current_subsec = now_subseg; | |
5285 | xtensa_isa isa; | |
5286 | int i; | |
5287 | ||
5288 | xtensa_default_isa = xtensa_isa_init (0, 0); | |
5289 | isa = xtensa_default_isa; | |
5290 | ||
5291 | linkrelax = opt_linkrelax; | |
5292 | ||
5293 | /* Set up the literal sections. */ | |
5294 | memset (&default_lit_sections, 0, sizeof (default_lit_sections)); | |
5295 | ||
5296 | subseg_set (current_section, current_subsec); | |
5297 | ||
5298 | xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi"); | |
5299 | xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi"); | |
5300 | xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0"); | |
5301 | xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4"); | |
5302 | xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8"); | |
5303 | xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12"); | |
5304 | xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0"); | |
5305 | xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4"); | |
5306 | xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8"); | |
5307 | xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12"); | |
5308 | xtensa_const16_opcode = xtensa_opcode_lookup (isa, "const16"); | |
5309 | xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry"); | |
5310 | xtensa_extui_opcode = xtensa_opcode_lookup (isa, "extui"); | |
5311 | xtensa_movi_opcode = xtensa_opcode_lookup (isa, "movi"); | |
5312 | xtensa_movi_n_opcode = xtensa_opcode_lookup (isa, "movi.n"); | |
5313 | xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync"); | |
5314 | xtensa_j_opcode = xtensa_opcode_lookup (isa, "j"); | |
5315 | xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx"); | |
5316 | xtensa_l32r_opcode = xtensa_opcode_lookup (isa, "l32r"); | |
5317 | xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop"); | |
5318 | xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez"); | |
5319 | xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz"); | |
5320 | xtensa_nop_opcode = xtensa_opcode_lookup (isa, "nop"); | |
5321 | xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n"); | |
5322 | xtensa_or_opcode = xtensa_opcode_lookup (isa, "or"); | |
5323 | xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret"); | |
5324 | xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n"); | |
5325 | xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw"); | |
5326 | xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n"); | |
5327 | xtensa_rsr_lcount_opcode = xtensa_opcode_lookup (isa, "rsr.lcount"); | |
5328 | xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti"); | |
5329 | ||
5330 | for (i = 0; i < xtensa_isa_num_formats (isa); i++) | |
5331 | { | |
5332 | int format_slots = xtensa_format_num_slots (isa, i); | |
5333 | if (format_slots > config_max_slots) | |
5334 | config_max_slots = format_slots; | |
5335 | } | |
5336 | ||
5337 | xg_init_vinsn (&cur_vinsn); | |
5338 | ||
5339 | xtensa_num_pipe_stages = xtensa_isa_num_pipe_stages (isa); | |
5340 | ||
5341 | init_op_placement_info_table (); | |
5342 | ||
5343 | /* Set up the assembly state. */ | |
5344 | if (!frag_now->tc_frag_data.is_assembly_state_set) | |
5345 | xtensa_set_frag_assembly_state (frag_now); | |
5346 | ||
5347 | if (!use_literal_section) | |
5348 | xtensa_mark_literal_pool_location (); | |
5349 | } | |
5350 | ||
5351 | ||
5352 | /* TC_INIT_FIX_DATA hook */ | |
5353 | ||
5354 | void | |
5355 | xtensa_init_fix_data (fixS *x) | |
5356 | { | |
5357 | x->tc_fix_data.slot = 0; | |
5358 | x->tc_fix_data.X_add_symbol = NULL; | |
5359 | x->tc_fix_data.X_add_number = 0; | |
5360 | } | |
5361 | ||
5362 | ||
5363 | /* tc_frob_label hook */ | |
5364 | ||
5365 | void | |
5366 | xtensa_frob_label (symbolS *sym) | |
5367 | { | |
5368 | float freq; | |
5369 | ||
5370 | if (cur_vinsn.inside_bundle) | |
5371 | { | |
5372 | as_bad (_("labels are not valid inside bundles")); | |
5373 | return; | |
5374 | } | |
5375 | ||
5376 | freq = get_subseg_target_freq (now_seg, now_subseg); | |
5377 | ||
5378 | /* Since the label was already attached to a frag associated with the | |
5379 | previous basic block, it now needs to be reset to the current frag. */ | |
5380 | symbol_set_frag (sym, frag_now); | |
5381 | S_SET_VALUE (sym, (valueT) frag_now_fix ()); | |
5382 | ||
5383 | if (generating_literals) | |
5384 | xtensa_add_literal_sym (sym); | |
5385 | else | |
5386 | xtensa_add_insn_label (sym); | |
5387 | ||
5388 | if (symbol_get_tc (sym)->is_loop_target) | |
5389 | { | |
5390 | if ((get_last_insn_flags (now_seg, now_subseg) | |
5391 | & FLAG_IS_BAD_LOOPEND) != 0) | |
5392 | as_bad (_("invalid last instruction for a zero-overhead loop")); | |
5393 | ||
5394 | xtensa_set_frag_assembly_state (frag_now); | |
5395 | frag_var (rs_machine_dependent, 4, 4, RELAX_LOOP_END, | |
5396 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
5397 | ||
5398 | xtensa_set_frag_assembly_state (frag_now); | |
5399 | xtensa_move_labels (frag_now, 0); | |
5400 | } | |
5401 | ||
5402 | /* No target aligning in the absolute section. */ | |
5403 | if (now_seg != absolute_section | |
5404 | && !is_unaligned_label (sym) | |
5405 | && !generating_literals) | |
5406 | { | |
5407 | xtensa_set_frag_assembly_state (frag_now); | |
5408 | ||
5409 | if (do_align_targets ()) | |
5410 | frag_var (rs_machine_dependent, 0, (int) freq, | |
5411 | RELAX_DESIRE_ALIGN_IF_TARGET, frag_now->fr_symbol, | |
5412 | frag_now->fr_offset, NULL); | |
5413 | else | |
5414 | frag_var (rs_fill, 0, 0, frag_now->fr_subtype, | |
5415 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
5416 | xtensa_set_frag_assembly_state (frag_now); | |
5417 | xtensa_move_labels (frag_now, 0); | |
5418 | } | |
5419 | ||
5420 | /* We need to mark the following properties even if we aren't aligning. */ | |
5421 | ||
5422 | /* If the label is already known to be a branch target, i.e., a | |
5423 | forward branch, mark the frag accordingly. Backward branches | |
5424 | are handled by xg_add_branch_and_loop_targets. */ | |
5425 | if (symbol_get_tc (sym)->is_branch_target) | |
5426 | symbol_get_frag (sym)->tc_frag_data.is_branch_target = true; | |
5427 | ||
5428 | /* Loops only go forward, so they can be identified here. */ | |
5429 | if (symbol_get_tc (sym)->is_loop_target) | |
5430 | symbol_get_frag (sym)->tc_frag_data.is_loop_target = true; | |
5431 | ||
5432 | dwarf2_emit_label (sym); | |
5433 | } | |
5434 | ||
5435 | ||
5436 | /* tc_unrecognized_line hook */ | |
5437 | ||
5438 | int | |
5439 | xtensa_unrecognized_line (int ch) | |
5440 | { | |
5441 | switch (ch) | |
5442 | { | |
5443 | case '{' : | |
5444 | if (cur_vinsn.inside_bundle == 0) | |
5445 | { | |
5446 | /* PR8110: Cannot emit line number info inside a FLIX bundle | |
5447 | when using --gstabs. Temporarily disable debug info. */ | |
5448 | generate_lineno_debug (); | |
5449 | if (debug_type == DEBUG_STABS) | |
5450 | { | |
5451 | xt_saved_debug_type = debug_type; | |
5452 | debug_type = DEBUG_NONE; | |
5453 | } | |
5454 | ||
5455 | cur_vinsn.inside_bundle = 1; | |
5456 | } | |
5457 | else | |
5458 | { | |
5459 | as_bad (_("extra opening brace")); | |
5460 | return 0; | |
5461 | } | |
5462 | break; | |
5463 | ||
5464 | case '}' : | |
5465 | if (cur_vinsn.inside_bundle) | |
5466 | finish_vinsn (&cur_vinsn); | |
5467 | else | |
5468 | { | |
5469 | as_bad (_("extra closing brace")); | |
5470 | return 0; | |
5471 | } | |
5472 | break; | |
5473 | default: | |
5474 | as_bad (_("syntax error")); | |
5475 | return 0; | |
5476 | } | |
5477 | return 1; | |
5478 | } | |
5479 | ||
5480 | ||
5481 | /* md_flush_pending_output hook */ | |
5482 | ||
5483 | void | |
5484 | xtensa_flush_pending_output (void) | |
5485 | { | |
5486 | /* This line fixes a bug where automatically generated gstabs info | |
5487 | separates a function label from its entry instruction, ending up | |
5488 | with the literal position between the function label and the entry | |
5489 | instruction and crashing code. It only happens with --gstabs and | |
5490 | --text-section-literals, and when several other obscure relaxation | |
5491 | conditions are met. */ | |
5492 | if (outputting_stabs_line_debug) | |
5493 | return; | |
5494 | ||
5495 | if (cur_vinsn.inside_bundle) | |
5496 | as_bad (_("missing closing brace")); | |
5497 | ||
5498 | /* If there is a non-zero instruction fragment, close it. */ | |
5499 | if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn) | |
5500 | { | |
5501 | frag_wane (frag_now); | |
5502 | frag_new (0); | |
5503 | xtensa_set_frag_assembly_state (frag_now); | |
5504 | } | |
5505 | frag_now->tc_frag_data.is_insn = false; | |
5506 | ||
5507 | xtensa_clear_insn_labels (); | |
5508 | } | |
5509 | ||
5510 | ||
5511 | /* We had an error while parsing an instruction. The string might look | |
5512 | like this: "insn arg1, arg2 }". If so, we need to see the closing | |
5513 | brace and reset some fields. Otherwise, the vinsn never gets closed | |
5514 | and the num_slots field will grow past the end of the array of slots, | |
5515 | and bad things happen. */ | |
5516 | ||
5517 | static void | |
5518 | error_reset_cur_vinsn (void) | |
5519 | { | |
5520 | if (cur_vinsn.inside_bundle) | |
5521 | { | |
5522 | if (*input_line_pointer == '}' | |
5523 | || *(input_line_pointer - 1) == '}' | |
5524 | || *(input_line_pointer - 2) == '}') | |
5525 | xg_clear_vinsn (&cur_vinsn); | |
5526 | } | |
5527 | } | |
5528 | ||
5529 | ||
5530 | void | |
5531 | md_assemble (char *str) | |
5532 | { | |
5533 | xtensa_isa isa = xtensa_default_isa; | |
5534 | char *opname; | |
5535 | unsigned opnamelen; | |
5536 | bool has_underbar = false; | |
5537 | char *arg_strings[MAX_INSN_ARGS]; | |
5538 | int num_args; | |
5539 | TInsn orig_insn; /* Original instruction from the input. */ | |
5540 | ||
5541 | tinsn_init (&orig_insn); | |
5542 | ||
5543 | /* Split off the opcode. */ | |
5544 | opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789."); | |
5545 | opname = xstrndup (str, opnamelen); | |
5546 | ||
5547 | num_args = tokenize_arguments (arg_strings, str + opnamelen); | |
5548 | if (num_args == -1) | |
5549 | { | |
5550 | as_bad (_("syntax error")); | |
5551 | return; | |
5552 | } | |
5553 | ||
5554 | if (xg_translate_idioms (&opname, &num_args, arg_strings)) | |
5555 | return; | |
5556 | ||
5557 | /* Check for an underbar prefix. */ | |
5558 | if (*opname == '_') | |
5559 | { | |
5560 | has_underbar = true; | |
5561 | opname += 1; | |
5562 | } | |
5563 | ||
5564 | orig_insn.insn_type = ITYPE_INSN; | |
5565 | orig_insn.ntok = 0; | |
5566 | orig_insn.is_specific_opcode = (has_underbar || !use_transform ()); | |
5567 | orig_insn.opcode = xtensa_opcode_lookup (isa, opname); | |
5568 | ||
5569 | /* Special case: Check for "CALLXn.TLS" pseudo op. If found, grab its | |
5570 | extra argument and set the opcode to "CALLXn". */ | |
5571 | if (orig_insn.opcode == XTENSA_UNDEFINED | |
5572 | && strncasecmp (opname, "callx", 5) == 0) | |
5573 | { | |
5574 | unsigned long window_size; | |
5575 | char *suffix; | |
5576 | ||
5577 | window_size = strtoul (opname + 5, &suffix, 10); | |
5578 | if (suffix != opname + 5 | |
5579 | && (window_size == 0 | |
5580 | || window_size == 4 | |
5581 | || window_size == 8 | |
5582 | || window_size == 12) | |
5583 | && strcasecmp (suffix, ".tls") == 0) | |
5584 | { | |
5585 | switch (window_size) | |
5586 | { | |
5587 | case 0: orig_insn.opcode = xtensa_callx0_opcode; break; | |
5588 | case 4: orig_insn.opcode = xtensa_callx4_opcode; break; | |
5589 | case 8: orig_insn.opcode = xtensa_callx8_opcode; break; | |
5590 | case 12: orig_insn.opcode = xtensa_callx12_opcode; break; | |
5591 | } | |
5592 | ||
5593 | if (num_args != 2) | |
5594 | as_bad (_("wrong number of operands for '%s'"), opname); | |
5595 | else | |
5596 | { | |
5597 | bfd_reloc_code_real_type reloc; | |
5598 | char *old_input_line_pointer; | |
5599 | expressionS *tok = &orig_insn.extra_arg; | |
5600 | ||
5601 | old_input_line_pointer = input_line_pointer; | |
5602 | input_line_pointer = arg_strings[num_args - 1]; | |
5603 | ||
5604 | expression (tok); | |
5605 | if (tok->X_op == O_symbol | |
5606 | && ((reloc = xtensa_elf_suffix (&input_line_pointer, tok)) | |
5607 | == BFD_RELOC_XTENSA_TLS_CALL)) | |
5608 | tok->X_op = map_suffix_reloc_to_operator (reloc); | |
5609 | else | |
5610 | as_bad (_("bad relocation expression for '%s'"), opname); | |
5611 | ||
5612 | input_line_pointer = old_input_line_pointer; | |
5613 | num_args -= 1; | |
5614 | } | |
5615 | } | |
5616 | } | |
5617 | ||
5618 | /* Special case: Check for "j.l" pseudo op. */ | |
5619 | if (orig_insn.opcode == XTENSA_UNDEFINED | |
5620 | && strncasecmp (opname, "j.l", 3) == 0) | |
5621 | { | |
5622 | if (num_args != 2) | |
5623 | as_bad (_("wrong number of operands for '%s'"), opname); | |
5624 | else | |
5625 | { | |
5626 | char *old_input_line_pointer; | |
5627 | expressionS *tok = &orig_insn.extra_arg; | |
5628 | ||
5629 | old_input_line_pointer = input_line_pointer; | |
5630 | input_line_pointer = arg_strings[num_args - 1]; | |
5631 | ||
5632 | expression_maybe_register (xtensa_jx_opcode, 0, tok); | |
5633 | input_line_pointer = old_input_line_pointer; | |
5634 | ||
5635 | num_args -= 1; | |
5636 | orig_insn.opcode = xtensa_j_opcode; | |
5637 | } | |
5638 | } | |
5639 | ||
5640 | if (orig_insn.opcode == XTENSA_UNDEFINED) | |
5641 | { | |
5642 | xtensa_format fmt = xtensa_format_lookup (isa, opname); | |
5643 | if (fmt == XTENSA_UNDEFINED) | |
5644 | { | |
5645 | as_bad (_("unknown opcode or format name '%s'"), opname); | |
5646 | error_reset_cur_vinsn (); | |
5647 | return; | |
5648 | } | |
5649 | if (!cur_vinsn.inside_bundle) | |
5650 | { | |
5651 | as_bad (_("format names only valid inside bundles")); | |
5652 | error_reset_cur_vinsn (); | |
5653 | return; | |
5654 | } | |
5655 | if (cur_vinsn.format != XTENSA_UNDEFINED) | |
5656 | as_warn (_("multiple formats specified for one bundle; using '%s'"), | |
5657 | opname); | |
5658 | cur_vinsn.format = fmt; | |
5659 | free (has_underbar ? opname - 1 : opname); | |
5660 | error_reset_cur_vinsn (); | |
5661 | return; | |
5662 | } | |
5663 | ||
5664 | /* Parse the arguments. */ | |
5665 | if (parse_arguments (&orig_insn, num_args, arg_strings)) | |
5666 | { | |
5667 | as_bad (_("syntax error")); | |
5668 | error_reset_cur_vinsn (); | |
5669 | return; | |
5670 | } | |
5671 | ||
5672 | /* Free the opcode and argument strings, now that they've been parsed. */ | |
5673 | free (has_underbar ? opname - 1 : opname); | |
5674 | opname = 0; | |
5675 | while (num_args-- > 0) | |
5676 | free (arg_strings[num_args]); | |
5677 | ||
5678 | /* Get expressions for invisible operands. */ | |
5679 | if (get_invisible_operands (&orig_insn)) | |
5680 | { | |
5681 | error_reset_cur_vinsn (); | |
5682 | return; | |
5683 | } | |
5684 | ||
5685 | /* Check for the right number and type of arguments. */ | |
5686 | if (tinsn_check_arguments (&orig_insn)) | |
5687 | { | |
5688 | error_reset_cur_vinsn (); | |
5689 | return; | |
5690 | } | |
5691 | ||
5692 | /* Record the line number for each TInsn, because a FLIX bundle may be | |
5693 | spread across multiple input lines and individual instructions may be | |
5694 | moved around in some cases. */ | |
5695 | orig_insn.loc_directive_seen = dwarf2_loc_directive_seen; | |
5696 | dwarf2_where (&orig_insn.debug_line); | |
5697 | dwarf2_consume_line_info (); | |
5698 | ||
5699 | xg_add_branch_and_loop_targets (&orig_insn); | |
5700 | ||
5701 | /* Check that immediate value for ENTRY is >= 16. */ | |
5702 | if (orig_insn.opcode == xtensa_entry_opcode && orig_insn.ntok >= 3) | |
5703 | { | |
5704 | expressionS *exp = &orig_insn.tok[2]; | |
5705 | if (exp->X_op == O_constant && exp->X_add_number < 16) | |
5706 | as_warn (_("entry instruction with stack decrement < 16")); | |
5707 | } | |
5708 | ||
5709 | /* Finish it off: | |
5710 | assemble_tokens (opcode, tok, ntok); | |
5711 | expand the tokens from the orig_insn into the | |
5712 | stack of instructions that will not expand | |
5713 | unless required at relaxation time. */ | |
5714 | ||
5715 | if (!cur_vinsn.inside_bundle) | |
5716 | emit_single_op (&orig_insn); | |
5717 | else /* We are inside a bundle. */ | |
5718 | { | |
5719 | cur_vinsn.slots[cur_vinsn.num_slots] = orig_insn; | |
5720 | cur_vinsn.num_slots++; | |
5721 | if (*input_line_pointer == '}' | |
5722 | || *(input_line_pointer - 1) == '}' | |
5723 | || *(input_line_pointer - 2) == '}') | |
5724 | finish_vinsn (&cur_vinsn); | |
5725 | } | |
5726 | ||
5727 | /* We've just emitted a new instruction so clear the list of labels. */ | |
5728 | xtensa_clear_insn_labels (); | |
5729 | ||
5730 | xtensa_check_frag_count (); | |
5731 | } | |
5732 | ||
5733 | ||
5734 | /* HANDLE_ALIGN hook */ | |
5735 | ||
5736 | /* For a .align directive, we mark the previous block with the alignment | |
5737 | information. This will be placed in the object file in the | |
5738 | property section corresponding to this section. */ | |
5739 | ||
5740 | void | |
5741 | xtensa_handle_align (fragS *fragP) | |
5742 | { | |
5743 | if (linkrelax | |
5744 | && ! fragP->tc_frag_data.is_literal | |
5745 | && (fragP->fr_type == rs_align | |
5746 | || fragP->fr_type == rs_align_code) | |
5747 | && fragP->fr_offset > 0 | |
5748 | && now_seg != bss_section) | |
5749 | { | |
5750 | fragP->tc_frag_data.is_align = true; | |
5751 | fragP->tc_frag_data.alignment = fragP->fr_offset; | |
5752 | } | |
5753 | ||
5754 | if (fragP->fr_type == rs_align_test) | |
5755 | { | |
5756 | int count; | |
5757 | count = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix; | |
5758 | if (count != 0) | |
5759 | as_bad_where (fragP->fr_file, fragP->fr_line, | |
5760 | _("unaligned entry instruction")); | |
5761 | } | |
5762 | ||
5763 | if (linkrelax && fragP->fr_type == rs_org) | |
5764 | fragP->fr_subtype = RELAX_ORG; | |
5765 | } | |
5766 | ||
5767 | ||
5768 | /* TC_FRAG_INIT hook */ | |
5769 | ||
5770 | void | |
5771 | xtensa_frag_init (fragS *frag) | |
5772 | { | |
5773 | xtensa_set_frag_assembly_state (frag); | |
5774 | } | |
5775 | ||
5776 | ||
5777 | symbolS * | |
5778 | md_undefined_symbol (char *name ATTRIBUTE_UNUSED) | |
5779 | { | |
5780 | return NULL; | |
5781 | } | |
5782 | ||
5783 | ||
5784 | /* Round up a section size to the appropriate boundary. */ | |
5785 | ||
5786 | valueT | |
5787 | md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size) | |
5788 | { | |
5789 | return size; /* Byte alignment is fine. */ | |
5790 | } | |
5791 | ||
5792 | ||
5793 | long | |
5794 | md_pcrel_from (fixS *fixP) | |
5795 | { | |
5796 | char *insn_p; | |
5797 | static xtensa_insnbuf insnbuf = NULL; | |
5798 | static xtensa_insnbuf slotbuf = NULL; | |
5799 | int opnum; | |
5800 | uint32 opnd_value; | |
5801 | xtensa_opcode opcode; | |
5802 | xtensa_format fmt; | |
5803 | int slot; | |
5804 | xtensa_isa isa = xtensa_default_isa; | |
5805 | valueT addr = fixP->fx_where + fixP->fx_frag->fr_address; | |
5806 | bool alt_reloc; | |
5807 | ||
5808 | if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND) | |
5809 | return 0; | |
5810 | ||
5811 | if (fixP->fx_r_type == BFD_RELOC_32_PCREL) | |
5812 | return addr; | |
5813 | ||
5814 | if (!insnbuf) | |
5815 | { | |
5816 | insnbuf = xtensa_insnbuf_alloc (isa); | |
5817 | slotbuf = xtensa_insnbuf_alloc (isa); | |
5818 | } | |
5819 | ||
5820 | insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where]; | |
5821 | xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) insn_p, 0); | |
5822 | fmt = xtensa_format_decode (isa, insnbuf); | |
5823 | ||
5824 | if (fmt == XTENSA_UNDEFINED) | |
5825 | as_fatal (_("bad instruction format")); | |
5826 | ||
5827 | if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc) != 0) | |
5828 | as_fatal (_("invalid relocation")); | |
5829 | ||
5830 | xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf); | |
5831 | opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf); | |
5832 | ||
5833 | /* Check for "alternate" relocations (operand not specified). None | |
5834 | of the current uses for these are really PC-relative. */ | |
5835 | if (alt_reloc || opcode == xtensa_const16_opcode) | |
5836 | { | |
5837 | if (opcode != xtensa_l32r_opcode | |
5838 | && opcode != xtensa_const16_opcode) | |
5839 | as_fatal (_("invalid relocation for '%s' instruction"), | |
5840 | xtensa_opcode_name (isa, opcode)); | |
5841 | return 0; | |
5842 | } | |
5843 | ||
5844 | opnum = get_relaxable_immed (opcode); | |
5845 | opnd_value = 0; | |
5846 | if (xtensa_operand_is_PCrelative (isa, opcode, opnum) != 1 | |
5847 | || xtensa_operand_do_reloc (isa, opcode, opnum, &opnd_value, addr)) | |
5848 | { | |
5849 | as_bad_where (fixP->fx_file, | |
5850 | fixP->fx_line, | |
5851 | _("invalid relocation for operand %d of '%s'"), | |
5852 | opnum, xtensa_opcode_name (isa, opcode)); | |
5853 | return 0; | |
5854 | } | |
5855 | return 0 - opnd_value; | |
5856 | } | |
5857 | ||
5858 | ||
5859 | /* TC_FORCE_RELOCATION hook */ | |
5860 | ||
5861 | int | |
5862 | xtensa_force_relocation (fixS *fix) | |
5863 | { | |
5864 | switch (fix->fx_r_type) | |
5865 | { | |
5866 | case BFD_RELOC_XTENSA_ASM_EXPAND: | |
5867 | case BFD_RELOC_XTENSA_SLOT0_ALT: | |
5868 | case BFD_RELOC_XTENSA_SLOT1_ALT: | |
5869 | case BFD_RELOC_XTENSA_SLOT2_ALT: | |
5870 | case BFD_RELOC_XTENSA_SLOT3_ALT: | |
5871 | case BFD_RELOC_XTENSA_SLOT4_ALT: | |
5872 | case BFD_RELOC_XTENSA_SLOT5_ALT: | |
5873 | case BFD_RELOC_XTENSA_SLOT6_ALT: | |
5874 | case BFD_RELOC_XTENSA_SLOT7_ALT: | |
5875 | case BFD_RELOC_XTENSA_SLOT8_ALT: | |
5876 | case BFD_RELOC_XTENSA_SLOT9_ALT: | |
5877 | case BFD_RELOC_XTENSA_SLOT10_ALT: | |
5878 | case BFD_RELOC_XTENSA_SLOT11_ALT: | |
5879 | case BFD_RELOC_XTENSA_SLOT12_ALT: | |
5880 | case BFD_RELOC_XTENSA_SLOT13_ALT: | |
5881 | case BFD_RELOC_XTENSA_SLOT14_ALT: | |
5882 | return 1; | |
5883 | default: | |
5884 | break; | |
5885 | } | |
5886 | ||
5887 | if (linkrelax && fix->fx_addsy | |
5888 | && relaxable_section (S_GET_SEGMENT (fix->fx_addsy))) | |
5889 | return 1; | |
5890 | ||
5891 | return generic_force_reloc (fix); | |
5892 | } | |
5893 | ||
5894 | ||
5895 | /* TC_VALIDATE_FIX_SUB hook */ | |
5896 | ||
5897 | int | |
5898 | xtensa_validate_fix_sub (fixS *fix) | |
5899 | { | |
5900 | segT add_symbol_segment, sub_symbol_segment; | |
5901 | ||
5902 | /* The difference of two symbols should be resolved by the assembler when | |
5903 | linkrelax is not set. If the linker may relax the section containing | |
5904 | the symbols, then an Xtensa DIFF relocation must be generated so that | |
5905 | the linker knows to adjust the difference value. */ | |
5906 | if (!linkrelax || fix->fx_addsy == NULL) | |
5907 | return 0; | |
5908 | ||
5909 | /* Make sure both symbols are in the same segment, and that segment is | |
5910 | "normal" and relaxable. If the segment is not "normal", then the | |
5911 | fix is not valid. If the segment is not "relaxable", then the fix | |
5912 | should have been handled earlier. */ | |
5913 | add_symbol_segment = S_GET_SEGMENT (fix->fx_addsy); | |
5914 | if (! SEG_NORMAL (add_symbol_segment) || | |
5915 | ! relaxable_section (add_symbol_segment)) | |
5916 | return 0; | |
5917 | sub_symbol_segment = S_GET_SEGMENT (fix->fx_subsy); | |
5918 | return (sub_symbol_segment == add_symbol_segment); | |
5919 | } | |
5920 | ||
5921 | ||
5922 | /* NO_PSEUDO_DOT hook */ | |
5923 | ||
5924 | /* This function has nothing to do with pseudo dots, but this is the | |
5925 | nearest macro to where the check needs to take place. FIXME: This | |
5926 | seems wrong. */ | |
5927 | ||
5928 | bool | |
5929 | xtensa_check_inside_bundle (void) | |
5930 | { | |
5931 | if (cur_vinsn.inside_bundle && input_line_pointer[-1] == '.') | |
5932 | as_bad (_("directives are not valid inside bundles")); | |
5933 | ||
5934 | /* This function must always return FALSE because it is called via a | |
5935 | macro that has nothing to do with bundling. */ | |
5936 | return false; | |
5937 | } | |
5938 | ||
5939 | ||
5940 | /* md_elf_section_change_hook */ | |
5941 | ||
5942 | void | |
5943 | xtensa_elf_section_change_hook (void) | |
5944 | { | |
5945 | /* Set up the assembly state. */ | |
5946 | if (!frag_now->tc_frag_data.is_assembly_state_set) | |
5947 | xtensa_set_frag_assembly_state (frag_now); | |
5948 | ||
5949 | if (!use_literal_section | |
5950 | && seg_info (now_seg)->tc_segment_info_data.literal_pool_loc == NULL | |
5951 | && !xtensa_is_init_fini (now_seg)) | |
5952 | xtensa_mark_literal_pool_location (); | |
5953 | } | |
5954 | ||
5955 | ||
5956 | /* tc_fix_adjustable hook */ | |
5957 | ||
5958 | bool | |
5959 | xtensa_fix_adjustable (fixS *fixP) | |
5960 | { | |
5961 | /* We need the symbol name for the VTABLE entries. */ | |
5962 | if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT | |
5963 | || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY) | |
5964 | return 0; | |
5965 | ||
5966 | return 1; | |
5967 | } | |
5968 | ||
5969 | ||
5970 | /* tc_symbol_new_hook */ | |
5971 | ||
5972 | symbolS *expr_symbols = NULL; | |
5973 | ||
5974 | void | |
5975 | xtensa_symbol_new_hook (symbolS *sym) | |
5976 | { | |
5977 | if (is_leb128_expr && S_GET_SEGMENT (sym) == expr_section) | |
5978 | { | |
5979 | symbol_get_tc (sym)->next_expr_symbol = expr_symbols; | |
5980 | expr_symbols = sym; | |
5981 | } | |
5982 | } | |
5983 | ||
5984 | ||
5985 | void | |
5986 | md_apply_fix (fixS *fixP, valueT *valP, segT seg) | |
5987 | { | |
5988 | char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where; | |
5989 | valueT val = 0; | |
5990 | ||
5991 | /* Subtracted symbols are only allowed for a few relocation types, and | |
5992 | unless linkrelax is enabled, they should not make it to this point. */ | |
5993 | if (fixP->fx_subsy && !(linkrelax && (fixP->fx_r_type == BFD_RELOC_32 | |
5994 | || fixP->fx_r_type == BFD_RELOC_16 | |
5995 | || fixP->fx_r_type == BFD_RELOC_8))) | |
5996 | as_bad_subtract (fixP); | |
5997 | ||
5998 | switch (fixP->fx_r_type) | |
5999 | { | |
6000 | case BFD_RELOC_32_PCREL: | |
6001 | case BFD_RELOC_32: | |
6002 | case BFD_RELOC_16: | |
6003 | case BFD_RELOC_8: | |
6004 | if (fixP->fx_subsy) | |
6005 | { | |
6006 | bool neg = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset | |
6007 | < S_GET_VALUE (fixP->fx_subsy)); | |
6008 | ||
6009 | switch (fixP->fx_r_type) | |
6010 | { | |
6011 | case BFD_RELOC_8: | |
6012 | fixP->fx_r_type = neg | |
6013 | ? BFD_RELOC_XTENSA_NDIFF8 : BFD_RELOC_XTENSA_PDIFF8; | |
6014 | fixP->fx_signed = 0; | |
6015 | break; | |
6016 | case BFD_RELOC_16: | |
6017 | fixP->fx_r_type = neg | |
6018 | ? BFD_RELOC_XTENSA_NDIFF16 : BFD_RELOC_XTENSA_PDIFF16; | |
6019 | fixP->fx_signed = 0; | |
6020 | break; | |
6021 | case BFD_RELOC_32: | |
6022 | fixP->fx_r_type = neg | |
6023 | ? BFD_RELOC_XTENSA_NDIFF32 : BFD_RELOC_XTENSA_PDIFF32; | |
6024 | fixP->fx_signed = 0; | |
6025 | break; | |
6026 | default: | |
6027 | break; | |
6028 | } | |
6029 | ||
6030 | val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset | |
6031 | - S_GET_VALUE (fixP->fx_subsy)); | |
6032 | ||
6033 | /* The difference value gets written out, and the DIFF reloc | |
6034 | identifies the address of the subtracted symbol (i.e., the one | |
6035 | with the lowest address). */ | |
6036 | *valP = val; | |
6037 | fixP->fx_offset -= val; | |
6038 | fixP->fx_subsy = NULL; | |
6039 | } | |
6040 | else if (! fixP->fx_addsy) | |
6041 | { | |
6042 | val = *valP; | |
6043 | fixP->fx_done = 1; | |
6044 | } | |
6045 | else if (S_GET_SEGMENT (fixP->fx_addsy) == absolute_section) | |
6046 | { | |
6047 | val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset; | |
6048 | *valP = val; | |
6049 | fixP->fx_done = 1; | |
6050 | } | |
6051 | /* fall through */ | |
6052 | ||
6053 | case BFD_RELOC_XTENSA_PLT: | |
6054 | md_number_to_chars (fixpos, val, fixP->fx_size); | |
6055 | fixP->fx_no_overflow = 0; /* Use the standard overflow check. */ | |
6056 | break; | |
6057 | ||
6058 | case BFD_RELOC_XTENSA_TLSDESC_FN: | |
6059 | case BFD_RELOC_XTENSA_TLSDESC_ARG: | |
6060 | case BFD_RELOC_XTENSA_TLS_TPOFF: | |
6061 | case BFD_RELOC_XTENSA_TLS_DTPOFF: | |
6062 | S_SET_THREAD_LOCAL (fixP->fx_addsy); | |
6063 | md_number_to_chars (fixpos, 0, fixP->fx_size); | |
6064 | fixP->fx_no_overflow = 0; /* Use the standard overflow check. */ | |
6065 | break; | |
6066 | ||
6067 | case BFD_RELOC_XTENSA_SLOT0_OP: | |
6068 | case BFD_RELOC_XTENSA_SLOT1_OP: | |
6069 | case BFD_RELOC_XTENSA_SLOT2_OP: | |
6070 | case BFD_RELOC_XTENSA_SLOT3_OP: | |
6071 | case BFD_RELOC_XTENSA_SLOT4_OP: | |
6072 | case BFD_RELOC_XTENSA_SLOT5_OP: | |
6073 | case BFD_RELOC_XTENSA_SLOT6_OP: | |
6074 | case BFD_RELOC_XTENSA_SLOT7_OP: | |
6075 | case BFD_RELOC_XTENSA_SLOT8_OP: | |
6076 | case BFD_RELOC_XTENSA_SLOT9_OP: | |
6077 | case BFD_RELOC_XTENSA_SLOT10_OP: | |
6078 | case BFD_RELOC_XTENSA_SLOT11_OP: | |
6079 | case BFD_RELOC_XTENSA_SLOT12_OP: | |
6080 | case BFD_RELOC_XTENSA_SLOT13_OP: | |
6081 | case BFD_RELOC_XTENSA_SLOT14_OP: | |
6082 | if (linkrelax) | |
6083 | { | |
6084 | /* Write the tentative value of a PC-relative relocation to a | |
6085 | local symbol into the instruction. The value will be ignored | |
6086 | by the linker, and it makes the object file disassembly | |
6087 | readable when all branch targets are encoded in relocations. */ | |
6088 | ||
6089 | gas_assert (fixP->fx_addsy); | |
6090 | if (S_GET_SEGMENT (fixP->fx_addsy) == seg | |
6091 | && !S_FORCE_RELOC (fixP->fx_addsy, 1)) | |
6092 | { | |
6093 | val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset | |
6094 | - md_pcrel_from (fixP)); | |
6095 | (void) xg_apply_fix_value (fixP, val); | |
6096 | } | |
6097 | } | |
6098 | else if (! fixP->fx_addsy) | |
6099 | { | |
6100 | val = *valP; | |
6101 | if (xg_apply_fix_value (fixP, val)) | |
6102 | fixP->fx_done = 1; | |
6103 | } | |
6104 | break; | |
6105 | ||
6106 | case BFD_RELOC_XTENSA_ASM_EXPAND: | |
6107 | case BFD_RELOC_XTENSA_TLS_FUNC: | |
6108 | case BFD_RELOC_XTENSA_TLS_ARG: | |
6109 | case BFD_RELOC_XTENSA_TLS_CALL: | |
6110 | case BFD_RELOC_XTENSA_SLOT0_ALT: | |
6111 | case BFD_RELOC_XTENSA_SLOT1_ALT: | |
6112 | case BFD_RELOC_XTENSA_SLOT2_ALT: | |
6113 | case BFD_RELOC_XTENSA_SLOT3_ALT: | |
6114 | case BFD_RELOC_XTENSA_SLOT4_ALT: | |
6115 | case BFD_RELOC_XTENSA_SLOT5_ALT: | |
6116 | case BFD_RELOC_XTENSA_SLOT6_ALT: | |
6117 | case BFD_RELOC_XTENSA_SLOT7_ALT: | |
6118 | case BFD_RELOC_XTENSA_SLOT8_ALT: | |
6119 | case BFD_RELOC_XTENSA_SLOT9_ALT: | |
6120 | case BFD_RELOC_XTENSA_SLOT10_ALT: | |
6121 | case BFD_RELOC_XTENSA_SLOT11_ALT: | |
6122 | case BFD_RELOC_XTENSA_SLOT12_ALT: | |
6123 | case BFD_RELOC_XTENSA_SLOT13_ALT: | |
6124 | case BFD_RELOC_XTENSA_SLOT14_ALT: | |
6125 | /* These all need to be resolved at link-time. Do nothing now. */ | |
6126 | break; | |
6127 | ||
6128 | case BFD_RELOC_VTABLE_INHERIT: | |
6129 | case BFD_RELOC_VTABLE_ENTRY: | |
6130 | fixP->fx_done = 0; | |
6131 | break; | |
6132 | ||
6133 | default: | |
6134 | as_bad (_("unhandled local relocation fix %s"), | |
6135 | bfd_get_reloc_code_name (fixP->fx_r_type)); | |
6136 | } | |
6137 | } | |
6138 | ||
6139 | ||
6140 | const char * | |
6141 | md_atof (int type, char *litP, int *sizeP) | |
6142 | { | |
6143 | return ieee_md_atof (type, litP, sizeP, target_big_endian); | |
6144 | } | |
6145 | ||
6146 | ||
6147 | int | |
6148 | md_estimate_size_before_relax (fragS *fragP, segT seg ATTRIBUTE_UNUSED) | |
6149 | { | |
6150 | return total_frag_text_expansion (fragP); | |
6151 | } | |
6152 | ||
6153 | ||
6154 | /* Translate internal representation of relocation info to BFD target | |
6155 | format. */ | |
6156 | ||
6157 | arelent * | |
6158 | tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp) | |
6159 | { | |
6160 | arelent *reloc; | |
6161 | ||
6162 | reloc = XNEW (arelent); | |
6163 | reloc->sym_ptr_ptr = XNEW (asymbol *); | |
6164 | *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy); | |
6165 | reloc->address = fixp->fx_frag->fr_address + fixp->fx_where; | |
6166 | ||
6167 | /* Make sure none of our internal relocations make it this far. | |
6168 | They'd better have been fully resolved by this point. */ | |
6169 | gas_assert ((int) fixp->fx_r_type > 0); | |
6170 | ||
6171 | reloc->addend = fixp->fx_offset; | |
6172 | ||
6173 | reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type); | |
6174 | if (reloc->howto == NULL) | |
6175 | { | |
6176 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
6177 | _("cannot represent `%s' relocation in object file"), | |
6178 | bfd_get_reloc_code_name (fixp->fx_r_type)); | |
6179 | free (reloc->sym_ptr_ptr); | |
6180 | free (reloc); | |
6181 | return NULL; | |
6182 | } | |
6183 | ||
6184 | if (!fixp->fx_pcrel != !reloc->howto->pc_relative) | |
6185 | as_fatal (_("internal error; cannot generate `%s' relocation"), | |
6186 | bfd_get_reloc_code_name (fixp->fx_r_type)); | |
6187 | ||
6188 | return reloc; | |
6189 | } | |
6190 | ||
6191 | \f | |
6192 | /* Checks for resource conflicts between instructions. */ | |
6193 | ||
6194 | /* The func unit stuff could be implemented as bit-vectors rather | |
6195 | than the iterative approach here. If it ends up being too | |
6196 | slow, we will switch it. */ | |
6197 | ||
6198 | resource_table * | |
6199 | new_resource_table (void *data, | |
6200 | int cycles, | |
6201 | int nu, | |
6202 | unit_num_copies_func uncf, | |
6203 | opcode_num_units_func onuf, | |
6204 | opcode_funcUnit_use_unit_func ouuf, | |
6205 | opcode_funcUnit_use_stage_func ousf) | |
6206 | { | |
6207 | int i; | |
6208 | resource_table *rt = XNEW (resource_table); | |
6209 | rt->data = data; | |
6210 | rt->cycles = cycles; | |
6211 | rt->allocated_cycles = cycles; | |
6212 | rt->num_units = nu; | |
6213 | rt->unit_num_copies = uncf; | |
6214 | rt->opcode_num_units = onuf; | |
6215 | rt->opcode_unit_use = ouuf; | |
6216 | rt->opcode_unit_stage = ousf; | |
6217 | ||
6218 | rt->units = XCNEWVEC (unsigned char *, cycles); | |
6219 | for (i = 0; i < cycles; i++) | |
6220 | rt->units[i] = XCNEWVEC (unsigned char, nu); | |
6221 | ||
6222 | return rt; | |
6223 | } | |
6224 | ||
6225 | ||
6226 | void | |
6227 | clear_resource_table (resource_table *rt) | |
6228 | { | |
6229 | int i, j; | |
6230 | for (i = 0; i < rt->allocated_cycles; i++) | |
6231 | for (j = 0; j < rt->num_units; j++) | |
6232 | rt->units[i][j] = 0; | |
6233 | } | |
6234 | ||
6235 | ||
6236 | /* We never shrink it, just fake it into thinking so. */ | |
6237 | ||
6238 | void | |
6239 | resize_resource_table (resource_table *rt, int cycles) | |
6240 | { | |
6241 | int i, old_cycles; | |
6242 | ||
6243 | rt->cycles = cycles; | |
6244 | if (cycles <= rt->allocated_cycles) | |
6245 | return; | |
6246 | ||
6247 | old_cycles = rt->allocated_cycles; | |
6248 | rt->allocated_cycles = cycles; | |
6249 | ||
6250 | rt->units = XRESIZEVEC (unsigned char *, rt->units, rt->allocated_cycles); | |
6251 | for (i = 0; i < old_cycles; i++) | |
6252 | rt->units[i] = XRESIZEVEC (unsigned char, rt->units[i], rt->num_units); | |
6253 | for (i = old_cycles; i < cycles; i++) | |
6254 | rt->units[i] = XCNEWVEC (unsigned char, rt->num_units); | |
6255 | } | |
6256 | ||
6257 | ||
6258 | bool | |
6259 | resources_available (resource_table *rt, xtensa_opcode opcode, int cycle) | |
6260 | { | |
6261 | int i; | |
6262 | int uses = (rt->opcode_num_units) (rt->data, opcode); | |
6263 | ||
6264 | for (i = 0; i < uses; i++) | |
6265 | { | |
6266 | xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i); | |
6267 | int stage = (rt->opcode_unit_stage) (rt->data, opcode, i); | |
6268 | int copies_in_use = rt->units[stage + cycle][unit]; | |
6269 | int copies = (rt->unit_num_copies) (rt->data, unit); | |
6270 | if (copies_in_use >= copies) | |
6271 | return false; | |
6272 | } | |
6273 | return true; | |
6274 | } | |
6275 | ||
6276 | ||
6277 | void | |
6278 | reserve_resources (resource_table *rt, xtensa_opcode opcode, int cycle) | |
6279 | { | |
6280 | int i; | |
6281 | int uses = (rt->opcode_num_units) (rt->data, opcode); | |
6282 | ||
6283 | for (i = 0; i < uses; i++) | |
6284 | { | |
6285 | xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i); | |
6286 | int stage = (rt->opcode_unit_stage) (rt->data, opcode, i); | |
6287 | /* Note that this allows resources to be oversubscribed. That's | |
6288 | essential to the way the optional scheduler works. | |
6289 | resources_available reports when a resource is over-subscribed, | |
6290 | so it's easy to tell. */ | |
6291 | rt->units[stage + cycle][unit]++; | |
6292 | } | |
6293 | } | |
6294 | ||
6295 | ||
6296 | void | |
6297 | release_resources (resource_table *rt, xtensa_opcode opcode, int cycle) | |
6298 | { | |
6299 | int i; | |
6300 | int uses = (rt->opcode_num_units) (rt->data, opcode); | |
6301 | ||
6302 | for (i = 0; i < uses; i++) | |
6303 | { | |
6304 | xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i); | |
6305 | int stage = (rt->opcode_unit_stage) (rt->data, opcode, i); | |
6306 | gas_assert (rt->units[stage + cycle][unit] > 0); | |
6307 | rt->units[stage + cycle][unit]--; | |
6308 | } | |
6309 | } | |
6310 | ||
6311 | ||
6312 | /* Wrapper functions make parameterized resource reservation | |
6313 | more convenient. */ | |
6314 | ||
6315 | int | |
6316 | opcode_funcUnit_use_unit (void *data, xtensa_opcode opcode, int idx) | |
6317 | { | |
6318 | xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx); | |
6319 | return use->unit; | |
6320 | } | |
6321 | ||
6322 | ||
6323 | int | |
6324 | opcode_funcUnit_use_stage (void *data, xtensa_opcode opcode, int idx) | |
6325 | { | |
6326 | xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx); | |
6327 | return use->stage; | |
6328 | } | |
6329 | ||
6330 | ||
6331 | /* Note that this function does not check issue constraints, but | |
6332 | solely whether the hardware is available to execute the given | |
6333 | instructions together. It also doesn't check if the tinsns | |
6334 | write the same state, or access the same tieports. That is | |
6335 | checked by check_t1_t2_reads_and_writes. */ | |
6336 | ||
6337 | static bool | |
6338 | resources_conflict (vliw_insn *vinsn) | |
6339 | { | |
6340 | int i; | |
6341 | static resource_table *rt = NULL; | |
6342 | ||
6343 | /* This is the most common case by far. Optimize it. */ | |
6344 | if (vinsn->num_slots == 1) | |
6345 | return false; | |
6346 | ||
6347 | if (rt == NULL) | |
6348 | { | |
6349 | xtensa_isa isa = xtensa_default_isa; | |
6350 | rt = new_resource_table | |
6351 | (isa, xtensa_num_pipe_stages, | |
6352 | xtensa_isa_num_funcUnits (isa), | |
6353 | (unit_num_copies_func) xtensa_funcUnit_num_copies, | |
6354 | (opcode_num_units_func) xtensa_opcode_num_funcUnit_uses, | |
6355 | opcode_funcUnit_use_unit, | |
6356 | opcode_funcUnit_use_stage); | |
6357 | } | |
6358 | ||
6359 | clear_resource_table (rt); | |
6360 | ||
6361 | for (i = 0; i < vinsn->num_slots; i++) | |
6362 | { | |
6363 | if (!resources_available (rt, vinsn->slots[i].opcode, 0)) | |
6364 | return true; | |
6365 | reserve_resources (rt, vinsn->slots[i].opcode, 0); | |
6366 | } | |
6367 | ||
6368 | return false; | |
6369 | } | |
6370 | ||
6371 | \f | |
6372 | /* finish_vinsn, emit_single_op and helper functions. */ | |
6373 | ||
6374 | static bool find_vinsn_conflicts (vliw_insn *); | |
6375 | static xtensa_format xg_find_narrowest_format (vliw_insn *); | |
6376 | static void xg_assemble_vliw_tokens (vliw_insn *); | |
6377 | ||
6378 | ||
6379 | /* We have reached the end of a bundle; emit into the frag. */ | |
6380 | ||
6381 | static void | |
6382 | finish_vinsn (vliw_insn *vinsn) | |
6383 | { | |
6384 | IStack slotstack; | |
6385 | int i; | |
6386 | int slots; | |
6387 | ||
6388 | if (find_vinsn_conflicts (vinsn)) | |
6389 | { | |
6390 | xg_clear_vinsn (vinsn); | |
6391 | return; | |
6392 | } | |
6393 | ||
6394 | /* First, find a format that works. */ | |
6395 | if (vinsn->format == XTENSA_UNDEFINED) | |
6396 | vinsn->format = xg_find_narrowest_format (vinsn); | |
6397 | ||
6398 | slots = xtensa_format_num_slots (xtensa_default_isa, vinsn->format); | |
6399 | if (slots > 1 | |
6400 | && produce_flix == FLIX_NONE) | |
6401 | { | |
6402 | as_bad (_("The option \"--no-allow-flix\" prohibits multi-slot flix.")); | |
6403 | xg_clear_vinsn (vinsn); | |
6404 | return; | |
6405 | } | |
6406 | ||
6407 | if (vinsn->format == XTENSA_UNDEFINED) | |
6408 | { | |
6409 | as_bad (_("couldn't find a valid instruction format")); | |
6410 | fprintf (stderr, _(" ops were: ")); | |
6411 | for (i = 0; i < vinsn->num_slots; i++) | |
6412 | fprintf (stderr, _(" %s;"), | |
6413 | xtensa_opcode_name (xtensa_default_isa, | |
6414 | vinsn->slots[i].opcode)); | |
6415 | fprintf (stderr, _("\n")); | |
6416 | xg_clear_vinsn (vinsn); | |
6417 | return; | |
6418 | } | |
6419 | ||
6420 | if (vinsn->num_slots != slots) | |
6421 | { | |
6422 | as_bad (_("mismatch for format '%s': #slots = %d, #opcodes = %d"), | |
6423 | xtensa_format_name (xtensa_default_isa, vinsn->format), | |
6424 | slots, vinsn->num_slots); | |
6425 | xg_clear_vinsn (vinsn); | |
6426 | return; | |
6427 | } | |
6428 | ||
6429 | if (resources_conflict (vinsn)) | |
6430 | { | |
6431 | as_bad (_("illegal resource usage in bundle")); | |
6432 | fprintf (stderr, " ops were: "); | |
6433 | for (i = 0; i < vinsn->num_slots; i++) | |
6434 | fprintf (stderr, " %s;", | |
6435 | xtensa_opcode_name (xtensa_default_isa, | |
6436 | vinsn->slots[i].opcode)); | |
6437 | fprintf (stderr, "\n"); | |
6438 | xg_clear_vinsn (vinsn); | |
6439 | return; | |
6440 | } | |
6441 | ||
6442 | for (i = 0; i < vinsn->num_slots; i++) | |
6443 | { | |
6444 | if (vinsn->slots[i].opcode != XTENSA_UNDEFINED) | |
6445 | { | |
6446 | symbolS *lit_sym = NULL; | |
6447 | int j; | |
6448 | bool e = false; | |
6449 | bool saved_density = density_supported; | |
6450 | ||
6451 | /* We don't want to narrow ops inside multi-slot bundles. */ | |
6452 | if (vinsn->num_slots > 1) | |
6453 | density_supported = false; | |
6454 | ||
6455 | istack_init (&slotstack); | |
6456 | if (vinsn->slots[i].opcode == xtensa_nop_opcode) | |
6457 | { | |
6458 | vinsn->slots[i].opcode = | |
6459 | xtensa_format_slot_nop_opcode (xtensa_default_isa, | |
6460 | vinsn->format, i); | |
6461 | vinsn->slots[i].ntok = 0; | |
6462 | } | |
6463 | ||
6464 | if (xg_expand_assembly_insn (&slotstack, &vinsn->slots[i])) | |
6465 | { | |
6466 | e = true; | |
6467 | continue; | |
6468 | } | |
6469 | ||
6470 | density_supported = saved_density; | |
6471 | ||
6472 | if (e) | |
6473 | { | |
6474 | xg_clear_vinsn (vinsn); | |
6475 | return; | |
6476 | } | |
6477 | ||
6478 | for (j = 0; j < slotstack.ninsn; j++) | |
6479 | { | |
6480 | TInsn *insn = &slotstack.insn[j]; | |
6481 | if (insn->insn_type == ITYPE_LITERAL) | |
6482 | { | |
6483 | gas_assert (lit_sym == NULL); | |
6484 | lit_sym = xg_assemble_literal (insn); | |
6485 | } | |
6486 | else | |
6487 | { | |
6488 | gas_assert (insn->insn_type == ITYPE_INSN); | |
6489 | if (lit_sym) | |
6490 | xg_resolve_literals (insn, lit_sym); | |
6491 | if (j != slotstack.ninsn - 1) | |
6492 | emit_single_op (insn); | |
6493 | } | |
6494 | } | |
6495 | ||
6496 | if (vinsn->num_slots > 1) | |
6497 | { | |
6498 | if (opcode_fits_format_slot | |
6499 | (slotstack.insn[slotstack.ninsn - 1].opcode, | |
6500 | vinsn->format, i)) | |
6501 | { | |
6502 | vinsn->slots[i] = slotstack.insn[slotstack.ninsn - 1]; | |
6503 | } | |
6504 | else | |
6505 | { | |
6506 | emit_single_op (&slotstack.insn[slotstack.ninsn - 1]); | |
6507 | if (vinsn->format == XTENSA_UNDEFINED) | |
6508 | vinsn->slots[i].opcode = xtensa_nop_opcode; | |
6509 | else | |
6510 | vinsn->slots[i].opcode | |
6511 | = xtensa_format_slot_nop_opcode (xtensa_default_isa, | |
6512 | vinsn->format, i); | |
6513 | ||
6514 | vinsn->slots[i].ntok = 0; | |
6515 | } | |
6516 | } | |
6517 | else | |
6518 | { | |
6519 | vinsn->slots[0] = slotstack.insn[slotstack.ninsn - 1]; | |
6520 | vinsn->format = XTENSA_UNDEFINED; | |
6521 | } | |
6522 | } | |
6523 | } | |
6524 | ||
6525 | /* Now check resource conflicts on the modified bundle. */ | |
6526 | if (resources_conflict (vinsn)) | |
6527 | { | |
6528 | as_bad (_("illegal resource usage in bundle")); | |
6529 | fprintf (stderr, " ops were: "); | |
6530 | for (i = 0; i < vinsn->num_slots; i++) | |
6531 | fprintf (stderr, " %s;", | |
6532 | xtensa_opcode_name (xtensa_default_isa, | |
6533 | vinsn->slots[i].opcode)); | |
6534 | fprintf (stderr, "\n"); | |
6535 | xg_clear_vinsn (vinsn); | |
6536 | return; | |
6537 | } | |
6538 | ||
6539 | /* First, find a format that works. */ | |
6540 | if (vinsn->format == XTENSA_UNDEFINED) | |
6541 | vinsn->format = xg_find_narrowest_format (vinsn); | |
6542 | ||
6543 | xg_assemble_vliw_tokens (vinsn); | |
6544 | ||
6545 | xg_clear_vinsn (vinsn); | |
6546 | ||
6547 | xtensa_check_frag_count (); | |
6548 | } | |
6549 | ||
6550 | ||
6551 | /* Given an vliw instruction, what conflicts are there in register | |
6552 | usage and in writes to states and queues? | |
6553 | ||
6554 | This function does two things: | |
6555 | 1. Reports an error when a vinsn contains illegal combinations | |
6556 | of writes to registers states or queues. | |
6557 | 2. Marks individual tinsns as not relaxable if the combination | |
6558 | contains antidependencies. | |
6559 | ||
6560 | Job 2 handles things like swap semantics in instructions that need | |
6561 | to be relaxed. For example, | |
6562 | ||
6563 | addi a0, a1, 100000 | |
6564 | ||
6565 | normally would be relaxed to | |
6566 | ||
6567 | l32r a0, some_label | |
6568 | add a0, a1, a0 | |
6569 | ||
6570 | _but_, if the above instruction is bundled with an a0 reader, e.g., | |
6571 | ||
6572 | { addi a0, a1, 10000 ; add a2, a0, a4 ; } | |
6573 | ||
6574 | then we can't relax it into | |
6575 | ||
6576 | l32r a0, some_label | |
6577 | { add a0, a1, a0 ; add a2, a0, a4 ; } | |
6578 | ||
6579 | because the value of a0 is trashed before the second add can read it. */ | |
6580 | ||
6581 | static char check_t1_t2_reads_and_writes (TInsn *, TInsn *); | |
6582 | ||
6583 | static bool | |
6584 | find_vinsn_conflicts (vliw_insn *vinsn) | |
6585 | { | |
6586 | int i, j; | |
6587 | int branches = 0; | |
6588 | xtensa_isa isa = xtensa_default_isa; | |
6589 | ||
6590 | gas_assert (!past_xtensa_md_finish); | |
6591 | ||
6592 | for (i = 0 ; i < vinsn->num_slots; i++) | |
6593 | { | |
6594 | TInsn *op1 = &vinsn->slots[i]; | |
6595 | if (op1->is_specific_opcode) | |
6596 | op1->keep_wide = true; | |
6597 | else | |
6598 | op1->keep_wide = false; | |
6599 | } | |
6600 | ||
6601 | for (i = 0 ; i < vinsn->num_slots; i++) | |
6602 | { | |
6603 | TInsn *op1 = &vinsn->slots[i]; | |
6604 | ||
6605 | if (xtensa_opcode_is_branch (isa, op1->opcode) == 1) | |
6606 | branches++; | |
6607 | ||
6608 | for (j = 0; j < vinsn->num_slots; j++) | |
6609 | { | |
6610 | if (i != j) | |
6611 | { | |
6612 | TInsn *op2 = &vinsn->slots[j]; | |
6613 | char conflict_type = check_t1_t2_reads_and_writes (op1, op2); | |
6614 | switch (conflict_type) | |
6615 | { | |
6616 | case 'c': | |
6617 | as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same register"), | |
6618 | xtensa_opcode_name (isa, op1->opcode), i, | |
6619 | xtensa_opcode_name (isa, op2->opcode), j); | |
6620 | return true; | |
6621 | case 'd': | |
6622 | as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same state"), | |
6623 | xtensa_opcode_name (isa, op1->opcode), i, | |
6624 | xtensa_opcode_name (isa, op2->opcode), j); | |
6625 | return true; | |
6626 | case 'e': | |
6627 | as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same port"), | |
6628 | xtensa_opcode_name (isa, op1->opcode), i, | |
6629 | xtensa_opcode_name (isa, op2->opcode), j); | |
6630 | return true; | |
6631 | case 'f': | |
6632 | as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) both have volatile port accesses"), | |
6633 | xtensa_opcode_name (isa, op1->opcode), i, | |
6634 | xtensa_opcode_name (isa, op2->opcode), j); | |
6635 | return true; | |
6636 | default: | |
6637 | /* Everything is OK. */ | |
6638 | break; | |
6639 | } | |
6640 | op2->is_specific_opcode = (op2->is_specific_opcode | |
6641 | || conflict_type == 'a'); | |
6642 | } | |
6643 | } | |
6644 | } | |
6645 | ||
6646 | if (branches > 1) | |
6647 | { | |
6648 | as_bad (_("multiple branches or jumps in the same bundle")); | |
6649 | return true; | |
6650 | } | |
6651 | ||
6652 | return false; | |
6653 | } | |
6654 | ||
6655 | ||
6656 | /* Check how the state used by t1 and t2 relate. | |
6657 | Cases found are: | |
6658 | ||
6659 | case A: t1 reads a register t2 writes (an antidependency within a bundle) | |
6660 | case B: no relationship between what is read and written (both could | |
6661 | read the same reg though) | |
6662 | case C: t1 writes a register t2 writes (a register conflict within a | |
6663 | bundle) | |
6664 | case D: t1 writes a state that t2 also writes | |
6665 | case E: t1 writes a tie queue that t2 also writes | |
6666 | case F: two volatile queue accesses | |
6667 | */ | |
6668 | ||
6669 | static char | |
6670 | check_t1_t2_reads_and_writes (TInsn *t1, TInsn *t2) | |
6671 | { | |
6672 | xtensa_isa isa = xtensa_default_isa; | |
6673 | xtensa_regfile t1_regfile, t2_regfile; | |
6674 | int t1_reg, t2_reg; | |
6675 | int t1_base_reg, t1_last_reg; | |
6676 | int t2_base_reg, t2_last_reg; | |
6677 | char t1_inout, t2_inout; | |
6678 | int i, j; | |
6679 | char conflict = 'b'; | |
6680 | int t1_states; | |
6681 | int t2_states; | |
6682 | int t1_interfaces; | |
6683 | int t2_interfaces; | |
6684 | bool t1_volatile = false; | |
6685 | bool t2_volatile = false; | |
6686 | ||
6687 | /* Check registers. */ | |
6688 | for (j = 0; j < t2->ntok; j++) | |
6689 | { | |
6690 | if (xtensa_operand_is_register (isa, t2->opcode, j) != 1) | |
6691 | continue; | |
6692 | ||
6693 | t2_regfile = xtensa_operand_regfile (isa, t2->opcode, j); | |
6694 | t2_base_reg = t2->tok[j].X_add_number; | |
6695 | t2_last_reg = t2_base_reg + xtensa_operand_num_regs (isa, t2->opcode, j); | |
6696 | ||
6697 | for (i = 0; i < t1->ntok; i++) | |
6698 | { | |
6699 | if (xtensa_operand_is_register (isa, t1->opcode, i) != 1) | |
6700 | continue; | |
6701 | ||
6702 | t1_regfile = xtensa_operand_regfile (isa, t1->opcode, i); | |
6703 | ||
6704 | if (t1_regfile != t2_regfile) | |
6705 | continue; | |
6706 | ||
6707 | t1_inout = xtensa_operand_inout (isa, t1->opcode, i); | |
6708 | t2_inout = xtensa_operand_inout (isa, t2->opcode, j); | |
6709 | ||
6710 | if (xtensa_operand_is_known_reg (isa, t1->opcode, i) == 0 | |
6711 | || xtensa_operand_is_known_reg (isa, t2->opcode, j) == 0) | |
6712 | { | |
6713 | if (t1_inout == 'm' || t1_inout == 'o' | |
6714 | || t2_inout == 'm' || t2_inout == 'o') | |
6715 | { | |
6716 | conflict = 'a'; | |
6717 | continue; | |
6718 | } | |
6719 | } | |
6720 | ||
6721 | t1_base_reg = t1->tok[i].X_add_number; | |
6722 | t1_last_reg = (t1_base_reg | |
6723 | + xtensa_operand_num_regs (isa, t1->opcode, i)); | |
6724 | ||
6725 | for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++) | |
6726 | { | |
6727 | for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++) | |
6728 | { | |
6729 | if (t1_reg != t2_reg) | |
6730 | continue; | |
6731 | ||
6732 | if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o')) | |
6733 | { | |
6734 | conflict = 'a'; | |
6735 | continue; | |
6736 | } | |
6737 | ||
6738 | if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o')) | |
6739 | { | |
6740 | conflict = 'a'; | |
6741 | continue; | |
6742 | } | |
6743 | ||
6744 | if (t1_inout != 'i' && t2_inout != 'i') | |
6745 | return 'c'; | |
6746 | } | |
6747 | } | |
6748 | } | |
6749 | } | |
6750 | ||
6751 | /* Check states. */ | |
6752 | t1_states = xtensa_opcode_num_stateOperands (isa, t1->opcode); | |
6753 | t2_states = xtensa_opcode_num_stateOperands (isa, t2->opcode); | |
6754 | for (j = 0; j < t2_states; j++) | |
6755 | { | |
6756 | xtensa_state t2_so = xtensa_stateOperand_state (isa, t2->opcode, j); | |
6757 | t2_inout = xtensa_stateOperand_inout (isa, t2->opcode, j); | |
6758 | for (i = 0; i < t1_states; i++) | |
6759 | { | |
6760 | xtensa_state t1_so = xtensa_stateOperand_state (isa, t1->opcode, i); | |
6761 | t1_inout = xtensa_stateOperand_inout (isa, t1->opcode, i); | |
6762 | if (t1_so != t2_so || xtensa_state_is_shared_or (isa, t1_so) == 1) | |
6763 | continue; | |
6764 | ||
6765 | if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o')) | |
6766 | { | |
6767 | conflict = 'a'; | |
6768 | continue; | |
6769 | } | |
6770 | ||
6771 | if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o')) | |
6772 | { | |
6773 | conflict = 'a'; | |
6774 | continue; | |
6775 | } | |
6776 | ||
6777 | if (t1_inout != 'i' && t2_inout != 'i') | |
6778 | return 'd'; | |
6779 | } | |
6780 | } | |
6781 | ||
6782 | /* Check tieports. */ | |
6783 | t1_interfaces = xtensa_opcode_num_interfaceOperands (isa, t1->opcode); | |
6784 | t2_interfaces = xtensa_opcode_num_interfaceOperands (isa, t2->opcode); | |
6785 | for (j = 0; j < t2_interfaces; j++) | |
6786 | { | |
6787 | xtensa_interface t2_int | |
6788 | = xtensa_interfaceOperand_interface (isa, t2->opcode, j); | |
6789 | int t2_class = xtensa_interface_class_id (isa, t2_int); | |
6790 | ||
6791 | t2_inout = xtensa_interface_inout (isa, t2_int); | |
6792 | if (xtensa_interface_has_side_effect (isa, t2_int) == 1) | |
6793 | t2_volatile = true; | |
6794 | ||
6795 | for (i = 0; i < t1_interfaces; i++) | |
6796 | { | |
6797 | xtensa_interface t1_int | |
6798 | = xtensa_interfaceOperand_interface (isa, t1->opcode, j); | |
6799 | int t1_class = xtensa_interface_class_id (isa, t1_int); | |
6800 | ||
6801 | t1_inout = xtensa_interface_inout (isa, t1_int); | |
6802 | if (xtensa_interface_has_side_effect (isa, t1_int) == 1) | |
6803 | t1_volatile = true; | |
6804 | ||
6805 | if (t1_volatile && t2_volatile && (t1_class == t2_class)) | |
6806 | return 'f'; | |
6807 | ||
6808 | if (t1_int != t2_int) | |
6809 | continue; | |
6810 | ||
6811 | if (t2_inout == 'i' && t1_inout == 'o') | |
6812 | { | |
6813 | conflict = 'a'; | |
6814 | continue; | |
6815 | } | |
6816 | ||
6817 | if (t1_inout == 'i' && t2_inout == 'o') | |
6818 | { | |
6819 | conflict = 'a'; | |
6820 | continue; | |
6821 | } | |
6822 | ||
6823 | if (t1_inout != 'i' && t2_inout != 'i') | |
6824 | return 'e'; | |
6825 | } | |
6826 | } | |
6827 | ||
6828 | return conflict; | |
6829 | } | |
6830 | ||
6831 | ||
6832 | static xtensa_format | |
6833 | xg_find_narrowest_format (vliw_insn *vinsn) | |
6834 | { | |
6835 | /* Right now we assume that the ops within the vinsn are properly | |
6836 | ordered for the slots that the programmer wanted them in. In | |
6837 | other words, we don't rearrange the ops in hopes of finding a | |
6838 | better format. The scheduler handles that. */ | |
6839 | ||
6840 | xtensa_isa isa = xtensa_default_isa; | |
6841 | xtensa_format format; | |
6842 | xtensa_opcode nop_opcode = xtensa_nop_opcode; | |
6843 | ||
6844 | if (vinsn->num_slots == 1) | |
6845 | return xg_get_single_format (vinsn->slots[0].opcode); | |
6846 | ||
6847 | for (format = 0; format < xtensa_isa_num_formats (isa); format++) | |
6848 | { | |
6849 | vliw_insn v_copy; | |
6850 | xg_copy_vinsn (&v_copy, vinsn); | |
6851 | if (xtensa_format_num_slots (isa, format) == v_copy.num_slots) | |
6852 | { | |
6853 | int slot; | |
6854 | int fit = 0; | |
6855 | for (slot = 0; slot < v_copy.num_slots; slot++) | |
6856 | { | |
6857 | if (v_copy.slots[slot].opcode == nop_opcode) | |
6858 | { | |
6859 | v_copy.slots[slot].opcode = | |
6860 | xtensa_format_slot_nop_opcode (isa, format, slot); | |
6861 | v_copy.slots[slot].ntok = 0; | |
6862 | } | |
6863 | ||
6864 | if (opcode_fits_format_slot (v_copy.slots[slot].opcode, | |
6865 | format, slot)) | |
6866 | fit++; | |
6867 | else if (v_copy.num_slots > 1) | |
6868 | { | |
6869 | TInsn widened; | |
6870 | /* Try the widened version. */ | |
6871 | if (!v_copy.slots[slot].keep_wide | |
6872 | && !v_copy.slots[slot].is_specific_opcode | |
6873 | && xg_is_single_relaxable_insn (&v_copy.slots[slot], | |
6874 | &widened, true) | |
6875 | && opcode_fits_format_slot (widened.opcode, | |
6876 | format, slot)) | |
6877 | { | |
6878 | v_copy.slots[slot] = widened; | |
6879 | fit++; | |
6880 | } | |
6881 | } | |
6882 | } | |
6883 | if (fit == v_copy.num_slots) | |
6884 | { | |
6885 | xg_copy_vinsn (vinsn, &v_copy); | |
6886 | xtensa_format_encode (isa, format, vinsn->insnbuf); | |
6887 | vinsn->format = format; | |
6888 | break; | |
6889 | } | |
6890 | } | |
6891 | } | |
6892 | ||
6893 | if (format == xtensa_isa_num_formats (isa)) | |
6894 | return XTENSA_UNDEFINED; | |
6895 | ||
6896 | return format; | |
6897 | } | |
6898 | ||
6899 | ||
6900 | /* Return the additional space needed in a frag | |
6901 | for possible relaxations of any ops in a VLIW insn. | |
6902 | Also fill out the relaxations that might be required of | |
6903 | each tinsn in the vinsn. */ | |
6904 | ||
6905 | static int | |
6906 | relaxation_requirements (vliw_insn *vinsn, bool *pfinish_frag) | |
6907 | { | |
6908 | bool finish_frag = false; | |
6909 | int extra_space = 0; | |
6910 | int slot; | |
6911 | ||
6912 | for (slot = 0; slot < vinsn->num_slots; slot++) | |
6913 | { | |
6914 | TInsn *tinsn = &vinsn->slots[slot]; | |
6915 | if (!tinsn_has_symbolic_operands (tinsn)) | |
6916 | { | |
6917 | /* A narrow instruction could be widened later to help | |
6918 | alignment issues. */ | |
6919 | if (xg_is_single_relaxable_insn (tinsn, 0, true) | |
6920 | && !tinsn->is_specific_opcode | |
6921 | && vinsn->num_slots == 1) | |
6922 | { | |
6923 | /* Difference in bytes between narrow and wide insns... */ | |
6924 | extra_space += 1; | |
6925 | tinsn->subtype = RELAX_NARROW; | |
6926 | } | |
6927 | } | |
6928 | else | |
6929 | { | |
6930 | if (workaround_b_j_loop_end | |
6931 | && tinsn->opcode == xtensa_jx_opcode | |
6932 | && use_transform ()) | |
6933 | { | |
6934 | /* Add 2 of these. */ | |
6935 | extra_space += 3; /* for the nop size */ | |
6936 | tinsn->subtype = RELAX_ADD_NOP_IF_PRE_LOOP_END; | |
6937 | } | |
6938 | ||
6939 | /* Need to assemble it with space for the relocation. */ | |
6940 | if (xg_is_relaxable_insn (tinsn, 0) | |
6941 | && !tinsn->is_specific_opcode) | |
6942 | { | |
6943 | int max_size = xg_get_max_insn_widen_size (tinsn->opcode); | |
6944 | int max_literal_size = | |
6945 | xg_get_max_insn_widen_literal_size (tinsn->opcode); | |
6946 | ||
6947 | tinsn->literal_space = max_literal_size; | |
6948 | ||
6949 | tinsn->subtype = RELAX_IMMED; | |
6950 | extra_space += max_size; | |
6951 | } | |
6952 | else | |
6953 | { | |
6954 | /* A fix record will be added for this instruction prior | |
6955 | to relaxation, so make it end the frag. */ | |
6956 | finish_frag = true; | |
6957 | } | |
6958 | } | |
6959 | } | |
6960 | *pfinish_frag = finish_frag; | |
6961 | return extra_space; | |
6962 | } | |
6963 | ||
6964 | ||
6965 | static void | |
6966 | bundle_tinsn (TInsn *tinsn, vliw_insn *vinsn) | |
6967 | { | |
6968 | xtensa_isa isa = xtensa_default_isa; | |
6969 | int slot, chosen_slot; | |
6970 | ||
6971 | vinsn->format = xg_get_single_format (tinsn->opcode); | |
6972 | gas_assert (vinsn->format != XTENSA_UNDEFINED); | |
6973 | vinsn->num_slots = xtensa_format_num_slots (isa, vinsn->format); | |
6974 | ||
6975 | chosen_slot = xg_get_single_slot (tinsn->opcode); | |
6976 | for (slot = 0; slot < vinsn->num_slots; slot++) | |
6977 | { | |
6978 | if (slot == chosen_slot) | |
6979 | vinsn->slots[slot] = *tinsn; | |
6980 | else | |
6981 | { | |
6982 | vinsn->slots[slot].opcode = | |
6983 | xtensa_format_slot_nop_opcode (isa, vinsn->format, slot); | |
6984 | vinsn->slots[slot].ntok = 0; | |
6985 | vinsn->slots[slot].insn_type = ITYPE_INSN; | |
6986 | } | |
6987 | } | |
6988 | } | |
6989 | ||
6990 | ||
6991 | static bool | |
6992 | emit_single_op (TInsn *orig_insn) | |
6993 | { | |
6994 | int i; | |
6995 | IStack istack; /* put instructions into here */ | |
6996 | symbolS *lit_sym = NULL; | |
6997 | symbolS *label_sym = NULL; | |
6998 | ||
6999 | istack_init (&istack); | |
7000 | ||
7001 | /* Special-case for "movi aX, foo" which is guaranteed to need relaxing. | |
7002 | Because the scheduling and bundling characteristics of movi and | |
7003 | l32r or const16 are so different, we can do much better if we relax | |
7004 | it prior to scheduling and bundling, rather than after. */ | |
7005 | if ((orig_insn->opcode == xtensa_movi_opcode | |
7006 | || orig_insn->opcode == xtensa_movi_n_opcode) | |
7007 | && !cur_vinsn.inside_bundle | |
7008 | && (orig_insn->tok[1].X_op == O_symbol | |
7009 | || orig_insn->tok[1].X_op == O_pltrel | |
7010 | || orig_insn->tok[1].X_op == O_tlsfunc | |
7011 | || orig_insn->tok[1].X_op == O_tlsarg | |
7012 | || orig_insn->tok[1].X_op == O_tpoff | |
7013 | || orig_insn->tok[1].X_op == O_dtpoff) | |
7014 | && !orig_insn->is_specific_opcode && use_transform ()) | |
7015 | xg_assembly_relax (&istack, orig_insn, now_seg, frag_now, 0, 1, 0); | |
7016 | else | |
7017 | if (xg_expand_assembly_insn (&istack, orig_insn)) | |
7018 | return true; | |
7019 | ||
7020 | for (i = 0; i < istack.ninsn; i++) | |
7021 | { | |
7022 | TInsn *insn = &istack.insn[i]; | |
7023 | switch (insn->insn_type) | |
7024 | { | |
7025 | case ITYPE_LITERAL: | |
7026 | gas_assert (lit_sym == NULL); | |
7027 | lit_sym = xg_assemble_literal (insn); | |
7028 | break; | |
7029 | case ITYPE_LABEL: | |
7030 | { | |
7031 | static int relaxed_sym_idx = 0; | |
7032 | char *label = XNEWVEC (char, strlen (FAKE_LABEL_NAME) + 12); | |
7033 | sprintf (label, "%s_rl_%x", FAKE_LABEL_NAME, relaxed_sym_idx++); | |
7034 | colon (label); | |
7035 | gas_assert (label_sym == NULL); | |
7036 | label_sym = symbol_find_or_make (label); | |
7037 | gas_assert (label_sym); | |
7038 | free (label); | |
7039 | } | |
7040 | break; | |
7041 | case ITYPE_INSN: | |
7042 | { | |
7043 | vliw_insn v; | |
7044 | if (lit_sym) | |
7045 | xg_resolve_literals (insn, lit_sym); | |
7046 | if (label_sym) | |
7047 | xg_resolve_labels (insn, label_sym); | |
7048 | xg_init_vinsn (&v); | |
7049 | bundle_tinsn (insn, &v); | |
7050 | finish_vinsn (&v); | |
7051 | xg_free_vinsn (&v); | |
7052 | } | |
7053 | break; | |
7054 | default: | |
7055 | gas_assert (0); | |
7056 | break; | |
7057 | } | |
7058 | } | |
7059 | return false; | |
7060 | } | |
7061 | ||
7062 | ||
7063 | static int | |
7064 | total_frag_text_expansion (fragS *fragP) | |
7065 | { | |
7066 | int slot; | |
7067 | int total_expansion = 0; | |
7068 | ||
7069 | for (slot = 0; slot < config_max_slots; slot++) | |
7070 | total_expansion += fragP->tc_frag_data.text_expansion[slot]; | |
7071 | ||
7072 | return total_expansion; | |
7073 | } | |
7074 | ||
7075 | ||
7076 | /* Emit a vliw instruction to the current fragment. */ | |
7077 | ||
7078 | static void | |
7079 | xg_assemble_vliw_tokens (vliw_insn *vinsn) | |
7080 | { | |
7081 | bool finish_frag; | |
7082 | bool is_jump = false; | |
7083 | bool is_branch = false; | |
7084 | xtensa_isa isa = xtensa_default_isa; | |
7085 | int insn_size; | |
7086 | int extra_space; | |
7087 | char *f = NULL; | |
7088 | int slot; | |
7089 | struct dwarf2_line_info debug_line; | |
7090 | bool loc_directive_seen = false; | |
7091 | TInsn *tinsn; | |
7092 | ||
7093 | memset (&debug_line, 0, sizeof (struct dwarf2_line_info)); | |
7094 | ||
7095 | if (generating_literals) | |
7096 | { | |
7097 | static int reported = 0; | |
7098 | if (reported < 4) | |
7099 | as_bad_where (frag_now->fr_file, frag_now->fr_line, | |
7100 | _("cannot assemble into a literal fragment")); | |
7101 | if (reported == 3) | |
7102 | as_bad (_("...")); | |
7103 | reported++; | |
7104 | return; | |
7105 | } | |
7106 | ||
7107 | if (frag_now_fix () != 0 | |
7108 | && (! frag_now->tc_frag_data.is_insn | |
7109 | || (vinsn_has_specific_opcodes (vinsn) && use_transform ()) | |
7110 | || (!use_transform ()) != frag_now->tc_frag_data.is_no_transform | |
7111 | || (directive_state[directive_longcalls] | |
7112 | != frag_now->tc_frag_data.use_longcalls) | |
7113 | || (directive_state[directive_absolute_literals] | |
7114 | != frag_now->tc_frag_data.use_absolute_literals))) | |
7115 | { | |
7116 | frag_wane (frag_now); | |
7117 | frag_new (0); | |
7118 | xtensa_set_frag_assembly_state (frag_now); | |
7119 | } | |
7120 | ||
7121 | if (workaround_a0_b_retw | |
7122 | && vinsn->num_slots == 1 | |
7123 | && (get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0 | |
7124 | && xtensa_opcode_is_branch (isa, vinsn->slots[0].opcode) == 1 | |
7125 | && use_transform ()) | |
7126 | { | |
7127 | has_a0_b_retw = true; | |
7128 | ||
7129 | /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW. | |
7130 | After the first assembly pass we will check all of them and | |
7131 | add a nop if needed. */ | |
7132 | frag_now->tc_frag_data.is_insn = true; | |
7133 | frag_var (rs_machine_dependent, 4, 4, | |
7134 | RELAX_ADD_NOP_IF_A0_B_RETW, | |
7135 | frag_now->fr_symbol, | |
7136 | frag_now->fr_offset, | |
7137 | NULL); | |
7138 | xtensa_set_frag_assembly_state (frag_now); | |
7139 | frag_now->tc_frag_data.is_insn = true; | |
7140 | frag_var (rs_machine_dependent, 4, 4, | |
7141 | RELAX_ADD_NOP_IF_A0_B_RETW, | |
7142 | frag_now->fr_symbol, | |
7143 | frag_now->fr_offset, | |
7144 | NULL); | |
7145 | xtensa_set_frag_assembly_state (frag_now); | |
7146 | } | |
7147 | ||
7148 | for (slot = 0; slot < vinsn->num_slots; slot++) | |
7149 | { | |
7150 | tinsn = &vinsn->slots[slot]; | |
7151 | ||
7152 | /* See if the instruction implies an aligned section. */ | |
7153 | if (xtensa_opcode_is_loop (isa, tinsn->opcode) == 1) | |
7154 | record_alignment (now_seg, 2); | |
7155 | ||
7156 | /* Determine the best line number for debug info. */ | |
7157 | if ((tinsn->loc_directive_seen || !loc_directive_seen) | |
7158 | && (tinsn->debug_line.filenum != debug_line.filenum | |
7159 | || tinsn->debug_line.line < debug_line.line | |
7160 | || tinsn->debug_line.column < debug_line.column)) | |
7161 | debug_line = tinsn->debug_line; | |
7162 | if (tinsn->loc_directive_seen) | |
7163 | loc_directive_seen = true; | |
7164 | } | |
7165 | ||
7166 | /* Special cases for instructions that force an alignment... */ | |
7167 | /* None of these opcodes are bundle-able. */ | |
7168 | if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1) | |
7169 | { | |
7170 | int max_fill; | |
7171 | ||
7172 | /* Remember the symbol that marks the end of the loop in the frag | |
7173 | that marks the start of the loop. This way we can easily find | |
7174 | the end of the loop at the beginning, without adding special code | |
7175 | to mark the loop instructions themselves. */ | |
7176 | symbolS *target_sym = NULL; | |
7177 | if (vinsn->slots[0].tok[1].X_op == O_symbol) | |
7178 | target_sym = vinsn->slots[0].tok[1].X_add_symbol; | |
7179 | ||
7180 | xtensa_set_frag_assembly_state (frag_now); | |
7181 | frag_now->tc_frag_data.is_insn = true; | |
7182 | ||
7183 | max_fill = get_text_align_max_fill_size | |
7184 | (get_text_align_power (xtensa_fetch_width), | |
7185 | true, frag_now->tc_frag_data.is_no_density); | |
7186 | ||
7187 | if (use_transform ()) | |
7188 | frag_var (rs_machine_dependent, max_fill, max_fill, | |
7189 | RELAX_ALIGN_NEXT_OPCODE, target_sym, 0, NULL); | |
7190 | else | |
7191 | frag_var (rs_machine_dependent, 0, 0, | |
7192 | RELAX_CHECK_ALIGN_NEXT_OPCODE, target_sym, 0, NULL); | |
7193 | xtensa_set_frag_assembly_state (frag_now); | |
7194 | } | |
7195 | ||
7196 | if (vinsn->slots[0].opcode == xtensa_entry_opcode | |
7197 | && !vinsn->slots[0].is_specific_opcode) | |
7198 | { | |
7199 | xtensa_mark_literal_pool_location (); | |
7200 | xtensa_move_labels (frag_now, 0); | |
7201 | frag_var (rs_align_test, 1, 1, 0, NULL, 2, NULL); | |
7202 | } | |
7203 | ||
7204 | if (vinsn->num_slots == 1) | |
7205 | { | |
7206 | if (workaround_a0_b_retw && use_transform ()) | |
7207 | set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER, | |
7208 | is_register_writer (&vinsn->slots[0], "a", 0)); | |
7209 | ||
7210 | set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND, | |
7211 | is_bad_loopend_opcode (&vinsn->slots[0])); | |
7212 | } | |
7213 | else | |
7214 | set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND, false); | |
7215 | ||
7216 | insn_size = xtensa_format_length (isa, vinsn->format); | |
7217 | ||
7218 | extra_space = relaxation_requirements (vinsn, &finish_frag); | |
7219 | ||
7220 | /* vinsn_to_insnbuf will produce the error. */ | |
7221 | if (vinsn->format != XTENSA_UNDEFINED) | |
7222 | { | |
7223 | f = frag_more (insn_size + extra_space); | |
7224 | xtensa_set_frag_assembly_state (frag_now); | |
7225 | frag_now->tc_frag_data.is_insn = true; | |
7226 | } | |
7227 | ||
7228 | vinsn_to_insnbuf (vinsn, f, frag_now, false); | |
7229 | if (vinsn->format == XTENSA_UNDEFINED) | |
7230 | return; | |
7231 | ||
7232 | xtensa_insnbuf_to_chars (isa, vinsn->insnbuf, (unsigned char *) f, 0); | |
7233 | ||
7234 | if (debug_type == DEBUG_DWARF2 || loc_directive_seen) | |
7235 | dwarf2_gen_line_info (frag_now_fix () - (insn_size + extra_space), | |
7236 | &debug_line); | |
7237 | ||
7238 | for (slot = 0; slot < vinsn->num_slots; slot++) | |
7239 | { | |
7240 | tinsn = &vinsn->slots[slot]; | |
7241 | frag_now->tc_frag_data.slot_subtypes[slot] = tinsn->subtype; | |
7242 | frag_now->tc_frag_data.slot_symbols[slot] = tinsn->symbol; | |
7243 | frag_now->tc_frag_data.slot_offsets[slot] = tinsn->offset; | |
7244 | frag_now->tc_frag_data.literal_frags[slot] = tinsn->literal_frag; | |
7245 | if (tinsn->opcode == xtensa_l32r_opcode) | |
7246 | frag_now->tc_frag_data.literal_frags[slot] | |
7247 | = symbol_get_frag (tinsn->tok[1].X_add_symbol); | |
7248 | if (tinsn->literal_space != 0) | |
7249 | xg_assemble_literal_space (tinsn->literal_space, slot); | |
7250 | frag_now->tc_frag_data.free_reg[slot] = tinsn->extra_arg; | |
7251 | ||
7252 | if (tinsn->subtype == RELAX_NARROW) | |
7253 | gas_assert (vinsn->num_slots == 1); | |
7254 | if (xtensa_opcode_is_jump (isa, tinsn->opcode) == 1) | |
7255 | is_jump = true; | |
7256 | if (xtensa_opcode_is_branch (isa, tinsn->opcode) == 1) | |
7257 | is_branch = true; | |
7258 | ||
7259 | if (tinsn->subtype || tinsn->symbol || tinsn->offset | |
7260 | || tinsn->literal_frag || is_jump || is_branch) | |
7261 | finish_frag = true; | |
7262 | } | |
7263 | ||
7264 | if (vinsn_has_specific_opcodes (vinsn) && use_transform ()) | |
7265 | frag_now->tc_frag_data.is_specific_opcode = true; | |
7266 | ||
7267 | if (finish_frag) | |
7268 | { | |
7269 | frag_variant (rs_machine_dependent, | |
7270 | extra_space, extra_space, RELAX_SLOTS, | |
7271 | frag_now->fr_symbol, frag_now->fr_offset, f); | |
7272 | xtensa_set_frag_assembly_state (frag_now); | |
7273 | } | |
7274 | ||
7275 | /* Special cases for loops: | |
7276 | close_loop_end should be inserted AFTER short_loop. | |
7277 | Make sure that CLOSE loops are processed BEFORE short_loops | |
7278 | when converting them. */ | |
7279 | ||
7280 | /* "short_loop": Add a NOP if the loop is < 4 bytes. */ | |
7281 | if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1 | |
7282 | && !vinsn->slots[0].is_specific_opcode) | |
7283 | { | |
7284 | if (workaround_short_loop && use_transform ()) | |
7285 | { | |
7286 | maybe_has_short_loop = true; | |
7287 | frag_now->tc_frag_data.is_insn = true; | |
7288 | frag_var (rs_machine_dependent, 4, 4, | |
7289 | RELAX_ADD_NOP_IF_SHORT_LOOP, | |
7290 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
7291 | frag_now->tc_frag_data.is_insn = true; | |
7292 | frag_var (rs_machine_dependent, 4, 4, | |
7293 | RELAX_ADD_NOP_IF_SHORT_LOOP, | |
7294 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
7295 | } | |
7296 | ||
7297 | /* "close_loop_end": Add up to 12 bytes of NOPs to keep a | |
7298 | loop at least 12 bytes away from another loop's end. */ | |
7299 | if (workaround_close_loop_end && use_transform ()) | |
7300 | { | |
7301 | maybe_has_close_loop_end = true; | |
7302 | frag_now->tc_frag_data.is_insn = true; | |
7303 | frag_var (rs_machine_dependent, 12, 12, | |
7304 | RELAX_ADD_NOP_IF_CLOSE_LOOP_END, | |
7305 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
7306 | } | |
7307 | } | |
7308 | ||
7309 | if (use_transform ()) | |
7310 | { | |
7311 | if (is_jump) | |
7312 | { | |
7313 | gas_assert (finish_frag); | |
7314 | frag_var (rs_machine_dependent, | |
7315 | xtensa_fetch_width, xtensa_fetch_width, | |
7316 | RELAX_UNREACHABLE, | |
7317 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
7318 | xtensa_set_frag_assembly_state (frag_now); | |
7319 | xtensa_maybe_create_trampoline_frag (); | |
7320 | /* Always create one here. */ | |
7321 | xtensa_maybe_create_literal_pool_frag (true, false); | |
7322 | } | |
7323 | else if (is_branch && do_align_targets ()) | |
7324 | { | |
7325 | gas_assert (finish_frag); | |
7326 | frag_var (rs_machine_dependent, | |
7327 | xtensa_fetch_width, xtensa_fetch_width, | |
7328 | RELAX_MAYBE_UNREACHABLE, | |
7329 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
7330 | xtensa_set_frag_assembly_state (frag_now); | |
7331 | frag_var (rs_machine_dependent, | |
7332 | 0, 0, | |
7333 | RELAX_MAYBE_DESIRE_ALIGN, | |
7334 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
7335 | xtensa_set_frag_assembly_state (frag_now); | |
7336 | } | |
7337 | } | |
7338 | ||
7339 | /* Now, if the original opcode was a call... */ | |
7340 | if (do_align_targets () | |
7341 | && xtensa_opcode_is_call (isa, vinsn->slots[0].opcode) == 1) | |
7342 | { | |
7343 | float freq = get_subseg_total_freq (now_seg, now_subseg); | |
7344 | frag_now->tc_frag_data.is_insn = true; | |
7345 | frag_var (rs_machine_dependent, 4, (int) freq, RELAX_DESIRE_ALIGN, | |
7346 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
7347 | xtensa_set_frag_assembly_state (frag_now); | |
7348 | } | |
7349 | ||
7350 | if (vinsn_has_specific_opcodes (vinsn) && use_transform ()) | |
7351 | { | |
7352 | frag_wane (frag_now); | |
7353 | frag_new (0); | |
7354 | xtensa_set_frag_assembly_state (frag_now); | |
7355 | } | |
7356 | } | |
7357 | ||
7358 | \f | |
7359 | /* xtensa_md_finish and helper functions. */ | |
7360 | ||
7361 | static void xtensa_cleanup_align_frags (void); | |
7362 | static void xtensa_fix_target_frags (void); | |
7363 | static void xtensa_mark_narrow_branches (void); | |
7364 | static void xtensa_mark_zcl_first_insns (void); | |
7365 | static void xtensa_mark_difference_of_two_symbols (void); | |
7366 | static void xtensa_fix_a0_b_retw_frags (void); | |
7367 | static void xtensa_fix_b_j_loop_end_frags (void); | |
7368 | static void xtensa_fix_close_loop_end_frags (void); | |
7369 | static void xtensa_fix_short_loop_frags (void); | |
7370 | static void xtensa_sanity_check (void); | |
7371 | static void xtensa_add_config_info (void); | |
7372 | ||
7373 | void | |
7374 | xtensa_md_finish (void) | |
7375 | { | |
7376 | directive_balance (); | |
7377 | xtensa_flush_pending_output (); | |
7378 | ||
7379 | past_xtensa_md_finish = true; | |
7380 | ||
7381 | xtensa_move_literals (); | |
7382 | ||
7383 | xtensa_reorder_segments (); | |
7384 | xtensa_cleanup_align_frags (); | |
7385 | xtensa_fix_target_frags (); | |
7386 | if (workaround_a0_b_retw && has_a0_b_retw) | |
7387 | xtensa_fix_a0_b_retw_frags (); | |
7388 | if (workaround_b_j_loop_end) | |
7389 | xtensa_fix_b_j_loop_end_frags (); | |
7390 | ||
7391 | /* "close_loop_end" should be processed BEFORE "short_loop". */ | |
7392 | if (workaround_close_loop_end && maybe_has_close_loop_end) | |
7393 | xtensa_fix_close_loop_end_frags (); | |
7394 | ||
7395 | if (workaround_short_loop && maybe_has_short_loop) | |
7396 | xtensa_fix_short_loop_frags (); | |
7397 | if (align_targets) | |
7398 | xtensa_mark_narrow_branches (); | |
7399 | xtensa_mark_zcl_first_insns (); | |
7400 | ||
7401 | xtensa_sanity_check (); | |
7402 | ||
7403 | xtensa_add_config_info (); | |
7404 | ||
7405 | xtensa_check_frag_count (); | |
7406 | } | |
7407 | ||
7408 | struct trampoline_chain_entry | |
7409 | { | |
7410 | symbolS *sym; | |
7411 | addressT offset; | |
7412 | }; | |
7413 | ||
7414 | /* Trampoline chain for a given (sym, offset) pair is a sorted array | |
7415 | of locations of trampoline jumps leading there. Jumps are represented | |
7416 | as pairs (sym, offset): trampoline frag symbol and offset of the jump | |
7417 | inside the frag. */ | |
7418 | struct trampoline_chain | |
7419 | { | |
7420 | struct trampoline_chain_entry target; | |
7421 | struct trampoline_chain_entry *entry; | |
7422 | size_t n_entries; | |
7423 | size_t n_max; | |
7424 | bool needs_sorting; | |
7425 | }; | |
7426 | ||
7427 | struct trampoline_chain_index | |
7428 | { | |
7429 | struct trampoline_chain *entry; | |
7430 | size_t n_entries; | |
7431 | size_t n_max; | |
7432 | bool needs_sorting; | |
7433 | }; | |
7434 | ||
7435 | struct trampoline_index | |
7436 | { | |
7437 | fragS **entry; | |
7438 | size_t n_entries; | |
7439 | size_t n_max; | |
7440 | }; | |
7441 | ||
7442 | struct trampoline_seg | |
7443 | { | |
7444 | struct trampoline_seg *next; | |
7445 | asection *seg; | |
7446 | /* Trampolines ordered by their frag fr_address */ | |
7447 | struct trampoline_index index; | |
7448 | /* Known trampoline chains ordered by (sym, offset) pair */ | |
7449 | struct trampoline_chain_index chain_index; | |
7450 | }; | |
7451 | ||
7452 | static struct trampoline_seg trampoline_seg_list; | |
7453 | #define J_RANGE (128 * 1024) | |
7454 | #define J_MARGIN 4096 | |
7455 | ||
7456 | static int unreachable_count = 0; | |
7457 | ||
7458 | ||
7459 | static void | |
7460 | xtensa_maybe_create_trampoline_frag (void) | |
7461 | { | |
7462 | if (!use_trampolines) | |
7463 | return; | |
7464 | ||
7465 | /* We create an area for possible trampolines every 10 unreachable frags. | |
7466 | These are preferred over the ones not preceded by an unreachable frag, | |
7467 | because we don't have to jump around them. This function is called after | |
7468 | each RELAX_UNREACHABLE frag is created. */ | |
7469 | ||
7470 | if (++unreachable_count > 10) | |
7471 | { | |
7472 | xtensa_create_trampoline_frag (false); | |
7473 | clear_frag_count (); | |
7474 | unreachable_count = 0; | |
7475 | } | |
7476 | } | |
7477 | ||
7478 | static void | |
7479 | xtensa_check_frag_count (void) | |
7480 | { | |
7481 | if (!use_trampolines || frag_now->tc_frag_data.is_no_transform) | |
7482 | return; | |
7483 | ||
7484 | /* We create an area for possible trampolines every 8000 frags or so. This | |
7485 | is an estimate based on the max range of a "j" insn (+/-128K) divided | |
7486 | by a typical frag byte count (16), minus a few for safety. This function | |
7487 | is called after each source line is processed. */ | |
7488 | ||
7489 | if (get_frag_count () > 8000) | |
7490 | { | |
7491 | xtensa_create_trampoline_frag (true); | |
7492 | clear_frag_count (); | |
7493 | unreachable_count = 0; | |
7494 | } | |
7495 | ||
7496 | /* We create an area for a possible literal pool every N (default 5000) | |
7497 | frags or so. */ | |
7498 | xtensa_maybe_create_literal_pool_frag (true, true); | |
7499 | } | |
7500 | ||
7501 | static xtensa_insnbuf trampoline_buf = NULL; | |
7502 | static xtensa_insnbuf trampoline_slotbuf = NULL; | |
7503 | ||
7504 | static xtensa_insnbuf litpool_buf = NULL; | |
7505 | static xtensa_insnbuf litpool_slotbuf = NULL; | |
7506 | ||
7507 | #define TRAMPOLINE_FRAG_SIZE 3000 | |
7508 | ||
7509 | static struct trampoline_seg * | |
7510 | find_trampoline_seg (asection *seg) | |
7511 | { | |
7512 | struct trampoline_seg *ts = trampoline_seg_list.next; | |
7513 | static struct trampoline_seg *mr; | |
7514 | ||
7515 | if (mr && mr->seg == seg) | |
7516 | return mr; | |
7517 | ||
7518 | for ( ; ts; ts = ts->next) | |
7519 | { | |
7520 | if (ts->seg == seg) | |
7521 | { | |
7522 | mr = ts; | |
7523 | return ts; | |
7524 | } | |
7525 | } | |
7526 | ||
7527 | return NULL; | |
7528 | } | |
7529 | ||
7530 | static size_t xg_find_trampoline (const struct trampoline_index *idx, | |
7531 | addressT addr) | |
7532 | { | |
7533 | size_t a = 0; | |
7534 | size_t b = idx->n_entries; | |
7535 | ||
7536 | while (b - a > 1) | |
7537 | { | |
7538 | size_t c = (a + b) / 2; | |
7539 | ||
7540 | if (idx->entry[c]->fr_address <= addr) | |
7541 | a = c; | |
7542 | else | |
7543 | b = c; | |
7544 | } | |
7545 | return a; | |
7546 | } | |
7547 | ||
7548 | static void xg_add_trampoline_to_index (struct trampoline_index *idx, | |
7549 | fragS *fragP) | |
7550 | { | |
7551 | if (idx->n_entries == idx->n_max) | |
7552 | { | |
7553 | idx->n_max = (idx->n_entries + 1) * 2; | |
7554 | idx->entry = xrealloc (idx->entry, | |
7555 | sizeof (*idx->entry) * idx->n_max); | |
7556 | } | |
7557 | idx->entry[idx->n_entries] = fragP; | |
7558 | ++idx->n_entries; | |
7559 | } | |
7560 | ||
7561 | static void xg_remove_trampoline_from_index (struct trampoline_index *idx, | |
7562 | size_t i) | |
7563 | { | |
7564 | gas_assert (i < idx->n_entries); | |
7565 | memmove (idx->entry + i, idx->entry + i + 1, | |
7566 | (idx->n_entries - i - 1) * sizeof (*idx->entry)); | |
7567 | --idx->n_entries; | |
7568 | } | |
7569 | ||
7570 | static void xg_add_trampoline_to_seg (struct trampoline_seg *ts, | |
7571 | fragS *fragP) | |
7572 | { | |
7573 | xg_add_trampoline_to_index (&ts->index, fragP); | |
7574 | } | |
7575 | ||
7576 | static void | |
7577 | xtensa_create_trampoline_frag (bool needs_jump_around) | |
7578 | { | |
7579 | /* Emit a frag where we can place intermediate jump instructions, | |
7580 | in case we need to jump farther than 128K bytes. | |
7581 | Each jump instruction takes three bytes. | |
7582 | We allocate enough for 1000 trampolines in each frag. | |
7583 | If that's not enough, oh well. */ | |
7584 | ||
7585 | struct trampoline_seg *ts = find_trampoline_seg (now_seg); | |
7586 | char *varP; | |
7587 | fragS *fragP; | |
7588 | int size = TRAMPOLINE_FRAG_SIZE; | |
7589 | ||
7590 | if (ts == NULL) | |
7591 | { | |
7592 | ts = XCNEW(struct trampoline_seg); | |
7593 | ts->next = trampoline_seg_list.next; | |
7594 | trampoline_seg_list.next = ts; | |
7595 | ts->seg = now_seg; | |
7596 | } | |
7597 | ||
7598 | frag_wane (frag_now); | |
7599 | frag_new (0); | |
7600 | xtensa_set_frag_assembly_state (frag_now); | |
7601 | varP = frag_var (rs_machine_dependent, size, size, RELAX_TRAMPOLINE, NULL, 0, NULL); | |
7602 | fragP = (fragS *)(varP - SIZEOF_STRUCT_FRAG); | |
7603 | if (trampoline_buf == NULL) | |
7604 | { | |
7605 | trampoline_buf = xtensa_insnbuf_alloc (xtensa_default_isa); | |
7606 | trampoline_slotbuf = xtensa_insnbuf_alloc (xtensa_default_isa); | |
7607 | } | |
7608 | fragP->tc_frag_data.needs_jump_around = needs_jump_around; | |
7609 | xg_add_trampoline_to_seg (ts, fragP); | |
7610 | } | |
7611 | ||
7612 | static bool xg_is_trampoline_frag_full (const fragS *fragP) | |
7613 | { | |
7614 | return fragP->fr_var < 3; | |
7615 | } | |
7616 | ||
7617 | static int xg_order_trampoline_chain_entry (const void *a, const void *b) | |
7618 | { | |
7619 | const struct trampoline_chain_entry *pa = a; | |
7620 | const struct trampoline_chain_entry *pb = b; | |
7621 | ||
7622 | if (pa->sym != pb->sym) | |
7623 | { | |
7624 | valueT aval = S_GET_VALUE (pa->sym); | |
7625 | valueT bval = S_GET_VALUE (pb->sym); | |
7626 | ||
7627 | if (aval != bval) | |
7628 | return aval < bval ? -1 : 1; | |
7629 | } | |
7630 | if (pa->offset != pb->offset) | |
7631 | return pa->offset < pb->offset ? -1 : 1; | |
7632 | return 0; | |
7633 | } | |
7634 | ||
7635 | static void xg_sort_trampoline_chain (struct trampoline_chain *tc) | |
7636 | { | |
7637 | qsort (tc->entry, tc->n_entries, sizeof (*tc->entry), | |
7638 | xg_order_trampoline_chain_entry); | |
7639 | tc->needs_sorting = false; | |
7640 | } | |
7641 | ||
7642 | /* Find entry index in the given chain with maximal address <= source. */ | |
7643 | static size_t xg_find_chain_entry (struct trampoline_chain *tc, | |
7644 | addressT source) | |
7645 | { | |
7646 | size_t a = 0; | |
7647 | size_t b = tc->n_entries; | |
7648 | ||
7649 | if (tc->needs_sorting) | |
7650 | xg_sort_trampoline_chain (tc); | |
7651 | ||
7652 | while (b - a > 1) | |
7653 | { | |
7654 | size_t c = (a + b) / 2; | |
7655 | struct trampoline_chain_entry *e = tc->entry + c; | |
7656 | ||
7657 | if (S_GET_VALUE(e->sym) + e->offset <= source) | |
7658 | a = c; | |
7659 | else | |
7660 | b = c; | |
7661 | } | |
7662 | return a; | |
7663 | } | |
7664 | ||
7665 | /* Find the best jump target for the source in the given trampoline chain. | |
7666 | The best jump target is the one that results in the shortest path to the | |
7667 | final target, it's the location of the jump closest to the final target, | |
7668 | but within the J_RANGE - J_MARGIN from the source. */ | |
7669 | static struct trampoline_chain_entry * | |
7670 | xg_get_best_chain_entry (struct trampoline_chain *tc, addressT source) | |
7671 | { | |
7672 | addressT target = S_GET_VALUE(tc->target.sym) + tc->target.offset; | |
7673 | size_t i = xg_find_chain_entry (tc, source); | |
7674 | struct trampoline_chain_entry *e = tc->entry + i; | |
7675 | int step = target < source ? -1 : 1; | |
7676 | addressT chained_target; | |
7677 | offsetT off; | |
7678 | ||
7679 | if (target > source && | |
7680 | S_GET_VALUE(e->sym) + e->offset <= source && | |
7681 | i + 1 < tc->n_entries) | |
7682 | ++i; | |
7683 | ||
7684 | while (i + step < tc->n_entries) | |
7685 | { | |
7686 | struct trampoline_chain_entry *next = tc->entry + i + step; | |
7687 | ||
7688 | chained_target = S_GET_VALUE(next->sym) + next->offset; | |
7689 | off = source - chained_target; | |
7690 | ||
7691 | if (labs (off) >= J_RANGE - J_MARGIN) | |
7692 | break; | |
7693 | ||
7694 | i += step; | |
7695 | } | |
7696 | ||
7697 | e = tc->entry + i; | |
7698 | chained_target = S_GET_VALUE(e->sym) + e->offset; | |
7699 | off = source - chained_target; | |
7700 | ||
7701 | if (labs (off) < J_MARGIN || | |
7702 | labs (off) >= J_RANGE - J_MARGIN) | |
7703 | return &tc->target; | |
7704 | return tc->entry + i; | |
7705 | } | |
7706 | ||
7707 | static int xg_order_trampoline_chain (const void *a, const void *b) | |
7708 | { | |
7709 | const struct trampoline_chain *_pa = a; | |
7710 | const struct trampoline_chain *_pb = b; | |
7711 | const struct trampoline_chain_entry *pa = &_pa->target; | |
7712 | const struct trampoline_chain_entry *pb = &_pb->target; | |
7713 | symbolS *s1 = pa->sym; | |
7714 | symbolS *s2 = pb->sym; | |
7715 | ||
7716 | if (s1 != s2) | |
7717 | { | |
7718 | symbolS *tmp = symbol_symbolS (s1); | |
7719 | if (tmp) | |
7720 | s1 = tmp; | |
7721 | ||
7722 | tmp = symbol_symbolS (s2); | |
7723 | if (tmp) | |
7724 | s2 = tmp; | |
7725 | ||
7726 | if (s1 != s2) | |
7727 | return s1 < s2 ? -1 : 1; | |
7728 | } | |
7729 | ||
7730 | if (pa->offset != pb->offset) | |
7731 | return pa->offset < pb->offset ? -1 : 1; | |
7732 | return 0; | |
7733 | } | |
7734 | ||
7735 | static struct trampoline_chain * | |
7736 | xg_get_trampoline_chain (struct trampoline_seg *ts, | |
7737 | symbolS *sym, | |
7738 | addressT offset) | |
7739 | { | |
7740 | struct trampoline_chain_index *idx = &ts->chain_index; | |
7741 | struct trampoline_chain c; | |
7742 | ||
7743 | if (idx->n_entries == 0) | |
7744 | return NULL; | |
7745 | ||
7746 | if (idx->needs_sorting) | |
7747 | { | |
7748 | qsort (idx->entry, idx->n_entries, sizeof (*idx->entry), | |
7749 | xg_order_trampoline_chain); | |
7750 | idx->needs_sorting = false; | |
7751 | } | |
7752 | c.target.sym = sym; | |
7753 | c.target.offset = offset; | |
7754 | return bsearch (&c, idx->entry, idx->n_entries, | |
7755 | sizeof (struct trampoline_chain), | |
7756 | xg_order_trampoline_chain); | |
7757 | } | |
7758 | ||
7759 | /* Find trampoline chain in the given trampoline segment that is going | |
7760 | to the *sym + *offset. If found, replace *sym and *offset with the | |
7761 | best jump target in that chain. */ | |
7762 | static struct trampoline_chain * | |
7763 | xg_find_best_eq_target (struct trampoline_seg *ts, | |
7764 | addressT source, symbolS **sym, | |
7765 | addressT *offset) | |
7766 | { | |
7767 | struct trampoline_chain *tc = xg_get_trampoline_chain (ts, *sym, *offset); | |
7768 | ||
7769 | if (tc) | |
7770 | { | |
7771 | struct trampoline_chain_entry *e = xg_get_best_chain_entry (tc, source); | |
7772 | ||
7773 | *sym = e->sym; | |
7774 | *offset = e->offset; | |
7775 | } | |
7776 | return tc; | |
7777 | } | |
7778 | ||
7779 | static void xg_add_location_to_chain (struct trampoline_chain *tc, | |
7780 | symbolS *sym, addressT offset) | |
7781 | { | |
7782 | struct trampoline_chain_entry *e; | |
7783 | ||
7784 | if (tc->n_entries == tc->n_max) | |
7785 | { | |
7786 | tc->n_max = (tc->n_max + 1) * 2; | |
7787 | tc->entry = xrealloc (tc->entry, sizeof (*tc->entry) * tc->n_max); | |
7788 | } | |
7789 | e = tc->entry + tc->n_entries; | |
7790 | e->sym = sym; | |
7791 | e->offset = offset; | |
7792 | ++tc->n_entries; | |
7793 | tc->needs_sorting = true; | |
7794 | } | |
7795 | ||
7796 | static struct trampoline_chain * | |
7797 | xg_create_trampoline_chain (struct trampoline_seg *ts, | |
7798 | symbolS *sym, addressT offset) | |
7799 | { | |
7800 | struct trampoline_chain_index *idx = &ts->chain_index; | |
7801 | struct trampoline_chain *tc; | |
7802 | ||
7803 | if (idx->n_entries == idx->n_max) | |
7804 | { | |
7805 | idx->n_max = (idx->n_max + 1) * 2; | |
7806 | idx->entry = xrealloc (idx->entry, | |
7807 | sizeof (*idx->entry) * idx->n_max); | |
7808 | } | |
7809 | ||
7810 | tc = idx->entry + idx->n_entries; | |
7811 | tc->target.sym = sym; | |
7812 | tc->target.offset = offset; | |
7813 | tc->entry = NULL; | |
7814 | tc->n_entries = 0; | |
7815 | tc->n_max = 0; | |
7816 | xg_add_location_to_chain (tc, sym, offset); | |
7817 | ||
7818 | ++idx->n_entries; | |
7819 | idx->needs_sorting = true; | |
7820 | ||
7821 | return tc; | |
7822 | } | |
7823 | ||
7824 | void dump_trampolines (void); | |
7825 | ||
7826 | void | |
7827 | dump_trampolines (void) | |
7828 | { | |
7829 | struct trampoline_seg *ts = trampoline_seg_list.next; | |
7830 | ||
7831 | for ( ; ts; ts = ts->next) | |
7832 | { | |
7833 | size_t i; | |
7834 | asection *seg = ts->seg; | |
7835 | ||
7836 | if (seg == NULL) | |
7837 | continue; | |
7838 | fprintf(stderr, "SECTION %s\n", seg->name); | |
7839 | ||
7840 | for (i = 0; i < ts->index.n_entries; ++i) | |
7841 | { | |
7842 | fragS *tf = ts->index.entry[i]; | |
7843 | ||
7844 | fprintf(stderr, " 0x%08x: fix=%d, jump_around=%s\n", | |
7845 | (int)tf->fr_address, (int)tf->fr_fix, | |
7846 | tf->tc_frag_data.needs_jump_around ? "T" : "F"); | |
7847 | } | |
7848 | } | |
7849 | } | |
7850 | ||
7851 | static void dump_litpools (void) __attribute__ ((unused)); | |
7852 | ||
7853 | static void | |
7854 | dump_litpools (void) | |
7855 | { | |
7856 | struct litpool_seg *lps = litpool_seg_list.next; | |
7857 | struct litpool_frag *lpf; | |
7858 | ||
7859 | for ( ; lps ; lps = lps->next ) | |
7860 | { | |
7861 | printf("litpool seg %s\n", lps->seg->name); | |
7862 | for ( lpf = lps->frag_list.next; lpf->fragP; lpf = lpf->next ) | |
7863 | { | |
7864 | fragS *litfrag = lpf->fragP->fr_next; | |
7865 | int count = 0; | |
7866 | while (litfrag && litfrag->fr_subtype != RELAX_LITERAL_POOL_END) | |
7867 | { | |
7868 | if (litfrag->fr_fix == 4) | |
7869 | count++; | |
7870 | litfrag = litfrag->fr_next; | |
7871 | } | |
7872 | printf(" %ld <%d:%d> (%d) [%d]: ", | |
7873 | lpf->addr, lpf->priority, lpf->original_priority, | |
7874 | lpf->fragP->fr_line, count); | |
7875 | /* dump_frag(lpf->fragP); */ | |
7876 | } | |
7877 | } | |
7878 | } | |
7879 | ||
7880 | static void | |
7881 | xtensa_maybe_create_literal_pool_frag (bool create, bool only_if_needed) | |
7882 | { | |
7883 | struct litpool_seg *lps = litpool_seg_list.next; | |
7884 | fragS *fragP; | |
7885 | struct litpool_frag *lpf; | |
7886 | bool needed = false; | |
7887 | ||
7888 | if (use_literal_section || !auto_litpools) | |
7889 | return; | |
7890 | ||
7891 | for ( ; lps ; lps = lps->next ) | |
7892 | { | |
7893 | if (lps->seg == now_seg) | |
7894 | break; | |
7895 | } | |
7896 | ||
7897 | if (lps == NULL) | |
7898 | { | |
7899 | lps = XCNEW (struct litpool_seg); | |
7900 | lps->next = litpool_seg_list.next; | |
7901 | litpool_seg_list.next = lps; | |
7902 | lps->seg = now_seg; | |
7903 | lps->frag_list.next = &lps->frag_list; | |
7904 | lps->frag_list.prev = &lps->frag_list; | |
7905 | /* Put candidate literal pool at the beginning of every section, | |
7906 | so that even when section starts with literal load there's a | |
7907 | literal pool available. */ | |
7908 | lps->frag_count = auto_litpool_limit; | |
7909 | } | |
7910 | ||
7911 | lps->frag_count++; | |
7912 | ||
7913 | if (create) | |
7914 | { | |
7915 | if (only_if_needed) | |
7916 | { | |
7917 | if (past_xtensa_md_finish || !use_transform() || | |
7918 | frag_now->tc_frag_data.is_no_transform) | |
7919 | { | |
7920 | return; | |
7921 | } | |
7922 | if (auto_litpool_limit <= 0) | |
7923 | { | |
7924 | /* Don't create a litpool based only on frag count. */ | |
7925 | return; | |
7926 | } | |
7927 | else if (lps->frag_count > auto_litpool_limit) | |
7928 | { | |
7929 | needed = true; | |
7930 | } | |
7931 | else | |
7932 | { | |
7933 | return; | |
7934 | } | |
7935 | } | |
7936 | else | |
7937 | { | |
7938 | needed = true; | |
7939 | } | |
7940 | } | |
7941 | ||
7942 | if (needed) | |
7943 | { | |
7944 | int size = (only_if_needed) ? 3 : 0; /* Space for a "j" insn. */ | |
7945 | /* Create a potential site for a literal pool. */ | |
7946 | frag_wane (frag_now); | |
7947 | frag_new (0); | |
7948 | xtensa_set_frag_assembly_state (frag_now); | |
7949 | fragP = frag_now; | |
7950 | fragP->tc_frag_data.lit_frchain = frchain_now; | |
7951 | fragP->tc_frag_data.literal_frag = fragP; | |
7952 | frag_var (rs_machine_dependent, size, size, | |
7953 | (only_if_needed) ? | |
7954 | RELAX_LITERAL_POOL_CANDIDATE_BEGIN : | |
7955 | RELAX_LITERAL_POOL_BEGIN, | |
7956 | NULL, 0, NULL); | |
7957 | frag_now->tc_frag_data.lit_seg = now_seg; | |
7958 | frag_variant (rs_machine_dependent, 0, 0, | |
7959 | RELAX_LITERAL_POOL_END, NULL, 0, NULL); | |
7960 | xtensa_set_frag_assembly_state (frag_now); | |
7961 | } | |
7962 | else | |
7963 | { | |
7964 | /* RELAX_LITERAL_POOL_BEGIN frag is being created; | |
7965 | just record it here. */ | |
7966 | fragP = frag_now; | |
7967 | } | |
7968 | ||
7969 | lpf = XNEW (struct litpool_frag); | |
7970 | /* Insert at tail of circular list. */ | |
7971 | lpf->addr = 0; | |
7972 | lps->frag_list.prev->next = lpf; | |
7973 | lpf->next = &lps->frag_list; | |
7974 | lpf->prev = lps->frag_list.prev; | |
7975 | lps->frag_list.prev = lpf; | |
7976 | lpf->fragP = fragP; | |
7977 | lpf->priority = (needed) ? (only_if_needed) ? 3 : 2 : 1; | |
7978 | lpf->original_priority = lpf->priority; | |
7979 | lpf->literal_count = 0; | |
7980 | ||
7981 | lps->frag_count = 0; | |
7982 | } | |
7983 | ||
7984 | static void | |
7985 | xtensa_cleanup_align_frags (void) | |
7986 | { | |
7987 | frchainS *frchP; | |
7988 | asection *s; | |
7989 | ||
7990 | for (s = stdoutput->sections; s; s = s->next) | |
7991 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
7992 | { | |
7993 | fragS *fragP; | |
7994 | /* Walk over all of the fragments in a subsection. */ | |
7995 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
7996 | { | |
7997 | if ((fragP->fr_type == rs_align | |
7998 | || fragP->fr_type == rs_align_code | |
7999 | || (fragP->fr_type == rs_machine_dependent | |
8000 | && (fragP->fr_subtype == RELAX_DESIRE_ALIGN | |
8001 | || fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET))) | |
8002 | && fragP->fr_fix == 0) | |
8003 | { | |
8004 | fragS *next = fragP->fr_next; | |
8005 | ||
8006 | while (next | |
8007 | && next->fr_fix == 0 | |
8008 | && next->fr_type == rs_machine_dependent | |
8009 | && next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET) | |
8010 | { | |
8011 | frag_wane (next); | |
8012 | next = next->fr_next; | |
8013 | } | |
8014 | } | |
8015 | /* If we don't widen branch targets, then they | |
8016 | will be easier to align. */ | |
8017 | if (fragP->tc_frag_data.is_branch_target | |
8018 | && fragP->fr_opcode == fragP->fr_literal | |
8019 | && fragP->fr_type == rs_machine_dependent | |
8020 | && fragP->fr_subtype == RELAX_SLOTS | |
8021 | && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW) | |
8022 | frag_wane (fragP); | |
8023 | if (fragP->fr_type == rs_machine_dependent | |
8024 | && fragP->fr_subtype == RELAX_UNREACHABLE) | |
8025 | fragP->tc_frag_data.is_unreachable = true; | |
8026 | } | |
8027 | } | |
8028 | } | |
8029 | ||
8030 | ||
8031 | /* Re-process all of the fragments looking to convert all of the | |
8032 | RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch | |
8033 | target in the next fragment, convert this to RELAX_DESIRE_ALIGN. | |
8034 | Otherwise, convert to a .fill 0. */ | |
8035 | ||
8036 | static void | |
8037 | xtensa_fix_target_frags (void) | |
8038 | { | |
8039 | frchainS *frchP; | |
8040 | asection *s; | |
8041 | ||
8042 | /* When this routine is called, all of the subsections are still intact | |
8043 | so we walk over subsections instead of sections. */ | |
8044 | for (s = stdoutput->sections; s; s = s->next) | |
8045 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
8046 | { | |
8047 | fragS *fragP; | |
8048 | ||
8049 | /* Walk over all of the fragments in a subsection. */ | |
8050 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
8051 | { | |
8052 | if (fragP->fr_type == rs_machine_dependent | |
8053 | && fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET) | |
8054 | { | |
8055 | if (next_frag_is_branch_target (fragP)) | |
8056 | fragP->fr_subtype = RELAX_DESIRE_ALIGN; | |
8057 | else | |
8058 | frag_wane (fragP); | |
8059 | } | |
8060 | } | |
8061 | } | |
8062 | } | |
8063 | ||
8064 | ||
8065 | static bool is_narrow_branch_guaranteed_in_range (fragS *, TInsn *); | |
8066 | ||
8067 | static void | |
8068 | xtensa_mark_narrow_branches (void) | |
8069 | { | |
8070 | frchainS *frchP; | |
8071 | asection *s; | |
8072 | ||
8073 | for (s = stdoutput->sections; s; s = s->next) | |
8074 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
8075 | { | |
8076 | fragS *fragP; | |
8077 | /* Walk over all of the fragments in a subsection. */ | |
8078 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
8079 | { | |
8080 | if (fragP->fr_type == rs_machine_dependent | |
8081 | && fragP->fr_subtype == RELAX_SLOTS | |
8082 | && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED) | |
8083 | { | |
8084 | vliw_insn vinsn; | |
8085 | ||
8086 | vinsn_from_chars (&vinsn, fragP->fr_opcode); | |
8087 | tinsn_immed_from_frag (&vinsn.slots[0], fragP, 0); | |
8088 | ||
8089 | if (vinsn.num_slots == 1 | |
8090 | && xtensa_opcode_is_branch (xtensa_default_isa, | |
8091 | vinsn.slots[0].opcode) == 1 | |
8092 | && xg_get_single_size (vinsn.slots[0].opcode) == 2 | |
8093 | && is_narrow_branch_guaranteed_in_range (fragP, | |
8094 | &vinsn.slots[0])) | |
8095 | { | |
8096 | fragP->fr_subtype = RELAX_SLOTS; | |
8097 | fragP->tc_frag_data.slot_subtypes[0] = RELAX_NARROW; | |
8098 | fragP->tc_frag_data.is_aligning_branch = 1; | |
8099 | } | |
8100 | } | |
8101 | } | |
8102 | } | |
8103 | } | |
8104 | ||
8105 | ||
8106 | /* A branch is typically widened only when its target is out of | |
8107 | range. However, we would like to widen them to align a subsequent | |
8108 | branch target when possible. | |
8109 | ||
8110 | Because the branch relaxation code is so convoluted, the optimal solution | |
8111 | (combining the two cases) is difficult to get right in all circumstances. | |
8112 | We therefore go with an "almost as good" solution, where we only | |
8113 | use for alignment narrow branches that definitely will not expand to a | |
8114 | jump and a branch. These functions find and mark these cases. */ | |
8115 | ||
8116 | /* The range in bytes of BNEZ.N and BEQZ.N. The target operand is encoded | |
8117 | as PC + 4 + imm6, where imm6 is a 6-bit immediate ranging from 0 to 63. | |
8118 | We start counting beginning with the frag after the 2-byte branch, so the | |
8119 | maximum offset is (4 - 2) + 63 = 65. */ | |
8120 | #define MAX_IMMED6 65 | |
8121 | ||
8122 | static offsetT unrelaxed_frag_max_size (fragS *); | |
8123 | ||
8124 | static bool | |
8125 | is_narrow_branch_guaranteed_in_range (fragS *fragP, TInsn *tinsn) | |
8126 | { | |
8127 | const expressionS *exp = &tinsn->tok[1]; | |
8128 | symbolS *symbolP = exp->X_add_symbol; | |
8129 | offsetT max_distance = exp->X_add_number; | |
8130 | fragS *target_frag; | |
8131 | ||
8132 | if (exp->X_op != O_symbol) | |
8133 | return false; | |
8134 | ||
8135 | target_frag = symbol_get_frag (symbolP); | |
8136 | ||
8137 | max_distance += (S_GET_VALUE (symbolP) - target_frag->fr_address); | |
8138 | if (is_branch_jmp_to_next (tinsn, fragP)) | |
8139 | return false; | |
8140 | ||
8141 | /* The branch doesn't branch over it's own frag, | |
8142 | but over the subsequent ones. */ | |
8143 | fragP = fragP->fr_next; | |
8144 | while (fragP != NULL && fragP != target_frag && max_distance <= MAX_IMMED6) | |
8145 | { | |
8146 | max_distance += unrelaxed_frag_max_size (fragP); | |
8147 | fragP = fragP->fr_next; | |
8148 | } | |
8149 | if (max_distance <= MAX_IMMED6 && fragP == target_frag) | |
8150 | return true; | |
8151 | return false; | |
8152 | } | |
8153 | ||
8154 | ||
8155 | static void | |
8156 | xtensa_mark_zcl_first_insns (void) | |
8157 | { | |
8158 | frchainS *frchP; | |
8159 | asection *s; | |
8160 | ||
8161 | for (s = stdoutput->sections; s; s = s->next) | |
8162 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
8163 | { | |
8164 | fragS *fragP; | |
8165 | /* Walk over all of the fragments in a subsection. */ | |
8166 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
8167 | { | |
8168 | if (fragP->fr_type == rs_machine_dependent | |
8169 | && (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE | |
8170 | || fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)) | |
8171 | { | |
8172 | /* Find the loop frag. */ | |
8173 | fragS *loop_frag = next_non_empty_frag (fragP); | |
8174 | /* Find the first insn frag. */ | |
8175 | fragS *targ_frag = next_non_empty_frag (loop_frag); | |
8176 | ||
8177 | /* Handle a corner case that comes up in hardware | |
8178 | diagnostics. The original assembly looks like this: | |
8179 | ||
8180 | loop aX, LabelA | |
8181 | <empty_frag>--not found by next_non_empty_frag | |
8182 | loop aY, LabelB | |
8183 | ||
8184 | Depending on the start address, the assembler may or | |
8185 | may not change it to look something like this: | |
8186 | ||
8187 | loop aX, LabelA | |
8188 | nop--frag isn't empty anymore | |
8189 | loop aY, LabelB | |
8190 | ||
8191 | So set up to check the alignment of the nop if it | |
8192 | exists */ | |
8193 | while (loop_frag != targ_frag) | |
8194 | { | |
8195 | if (loop_frag->fr_type == rs_machine_dependent | |
8196 | && (loop_frag->fr_subtype == RELAX_ALIGN_NEXT_OPCODE | |
8197 | || loop_frag->fr_subtype | |
8198 | == RELAX_CHECK_ALIGN_NEXT_OPCODE)) | |
8199 | targ_frag = loop_frag; | |
8200 | else | |
8201 | loop_frag = loop_frag->fr_next; | |
8202 | } | |
8203 | ||
8204 | /* Of course, sometimes (mostly for toy test cases) a | |
8205 | zero-cost loop instruction is the last in a section. */ | |
8206 | if (targ_frag) | |
8207 | { | |
8208 | targ_frag->tc_frag_data.is_first_loop_insn = true; | |
8209 | /* Do not widen a frag that is the first instruction of a | |
8210 | zero-cost loop. It makes that loop harder to align. */ | |
8211 | if (targ_frag->fr_type == rs_machine_dependent | |
8212 | && targ_frag->fr_subtype == RELAX_SLOTS | |
8213 | && (targ_frag->tc_frag_data.slot_subtypes[0] | |
8214 | == RELAX_NARROW)) | |
8215 | { | |
8216 | if (targ_frag->tc_frag_data.is_aligning_branch) | |
8217 | targ_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED; | |
8218 | else | |
8219 | { | |
8220 | frag_wane (targ_frag); | |
8221 | targ_frag->tc_frag_data.slot_subtypes[0] = 0; | |
8222 | } | |
8223 | } | |
8224 | } | |
8225 | if (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE) | |
8226 | frag_wane (fragP); | |
8227 | } | |
8228 | } | |
8229 | } | |
8230 | } | |
8231 | ||
8232 | ||
8233 | /* When a difference-of-symbols expression is encoded as a uleb128 or | |
8234 | sleb128 value, the linker is unable to adjust that value to account for | |
8235 | link-time relaxation. Mark all the code between such symbols so that | |
8236 | its size cannot be changed by linker relaxation. */ | |
8237 | ||
8238 | static void | |
8239 | xtensa_mark_difference_of_two_symbols (void) | |
8240 | { | |
8241 | symbolS *expr_sym; | |
8242 | ||
8243 | for (expr_sym = expr_symbols; expr_sym; | |
8244 | expr_sym = symbol_get_tc (expr_sym)->next_expr_symbol) | |
8245 | { | |
8246 | expressionS *exp = symbol_get_value_expression (expr_sym); | |
8247 | ||
8248 | if (exp->X_op == O_subtract) | |
8249 | { | |
8250 | symbolS *left = exp->X_add_symbol; | |
8251 | symbolS *right = exp->X_op_symbol; | |
8252 | ||
8253 | /* Difference of two symbols not in the same section | |
8254 | are handled with relocations in the linker. */ | |
8255 | if (S_GET_SEGMENT (left) == S_GET_SEGMENT (right)) | |
8256 | { | |
8257 | fragS *start; | |
8258 | fragS *end; | |
8259 | fragS *walk; | |
8260 | ||
8261 | if (symbol_get_frag (left)->fr_address | |
8262 | <= symbol_get_frag (right)->fr_address) | |
8263 | { | |
8264 | start = symbol_get_frag (left); | |
8265 | end = symbol_get_frag (right); | |
8266 | } | |
8267 | else | |
8268 | { | |
8269 | start = symbol_get_frag (right); | |
8270 | end = symbol_get_frag (left); | |
8271 | } | |
8272 | ||
8273 | if (start->tc_frag_data.no_transform_end != NULL) | |
8274 | walk = start->tc_frag_data.no_transform_end; | |
8275 | else | |
8276 | walk = start; | |
8277 | do | |
8278 | { | |
8279 | walk->tc_frag_data.is_no_transform = 1; | |
8280 | walk = walk->fr_next; | |
8281 | } | |
8282 | while (walk && walk->fr_address < end->fr_address); | |
8283 | ||
8284 | start->tc_frag_data.no_transform_end = walk; | |
8285 | } | |
8286 | } | |
8287 | } | |
8288 | } | |
8289 | ||
8290 | ||
8291 | /* Re-process all of the fragments looking to convert all of the | |
8292 | RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a | |
8293 | conditional branch or a retw/retw.n, convert this frag to one that | |
8294 | will generate a NOP. In any case close it off with a .fill 0. */ | |
8295 | ||
8296 | static bool next_instrs_are_b_retw (fragS *); | |
8297 | ||
8298 | static void | |
8299 | xtensa_fix_a0_b_retw_frags (void) | |
8300 | { | |
8301 | frchainS *frchP; | |
8302 | asection *s; | |
8303 | ||
8304 | /* When this routine is called, all of the subsections are still intact | |
8305 | so we walk over subsections instead of sections. */ | |
8306 | for (s = stdoutput->sections; s; s = s->next) | |
8307 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
8308 | { | |
8309 | fragS *fragP; | |
8310 | ||
8311 | /* Walk over all of the fragments in a subsection. */ | |
8312 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
8313 | { | |
8314 | if (fragP->fr_type == rs_machine_dependent | |
8315 | && fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW) | |
8316 | { | |
8317 | if (next_instrs_are_b_retw (fragP)) | |
8318 | { | |
8319 | if (fragP->tc_frag_data.is_no_transform) | |
8320 | as_bad (_("instruction sequence (write a0, branch, retw) may trigger hardware errata")); | |
8321 | else | |
8322 | relax_frag_add_nop (fragP); | |
8323 | } | |
8324 | frag_wane (fragP); | |
8325 | } | |
8326 | } | |
8327 | } | |
8328 | } | |
8329 | ||
8330 | ||
8331 | static bool | |
8332 | next_instrs_are_b_retw (fragS *fragP) | |
8333 | { | |
8334 | xtensa_opcode opcode; | |
8335 | xtensa_format fmt; | |
8336 | const fragS *next_fragP = next_non_empty_frag (fragP); | |
8337 | static xtensa_insnbuf insnbuf = NULL; | |
8338 | static xtensa_insnbuf slotbuf = NULL; | |
8339 | xtensa_isa isa = xtensa_default_isa; | |
8340 | unsigned int offset = 0; | |
8341 | int slot; | |
8342 | bool branch_seen = false; | |
8343 | ||
8344 | if (!insnbuf) | |
8345 | { | |
8346 | insnbuf = xtensa_insnbuf_alloc (isa); | |
8347 | slotbuf = xtensa_insnbuf_alloc (isa); | |
8348 | } | |
8349 | ||
8350 | if (next_fragP == NULL) | |
8351 | return false; | |
8352 | ||
8353 | /* Check for the conditional branch. */ | |
8354 | xtensa_insnbuf_from_chars | |
8355 | (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0); | |
8356 | fmt = xtensa_format_decode (isa, insnbuf); | |
8357 | if (fmt == XTENSA_UNDEFINED) | |
8358 | return false; | |
8359 | ||
8360 | for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++) | |
8361 | { | |
8362 | xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf); | |
8363 | opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf); | |
8364 | ||
8365 | branch_seen = (branch_seen | |
8366 | || xtensa_opcode_is_branch (isa, opcode) == 1); | |
8367 | } | |
8368 | ||
8369 | if (!branch_seen) | |
8370 | return false; | |
8371 | ||
8372 | offset += xtensa_format_length (isa, fmt); | |
8373 | if (offset == next_fragP->fr_fix) | |
8374 | { | |
8375 | next_fragP = next_non_empty_frag (next_fragP); | |
8376 | offset = 0; | |
8377 | } | |
8378 | ||
8379 | if (next_fragP == NULL) | |
8380 | return false; | |
8381 | ||
8382 | /* Check for the retw/retw.n. */ | |
8383 | xtensa_insnbuf_from_chars | |
8384 | (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0); | |
8385 | fmt = xtensa_format_decode (isa, insnbuf); | |
8386 | ||
8387 | /* Because RETW[.N] is not bundleable, a VLIW bundle here means that we | |
8388 | have no problems. */ | |
8389 | if (fmt == XTENSA_UNDEFINED | |
8390 | || xtensa_format_num_slots (isa, fmt) != 1) | |
8391 | return false; | |
8392 | ||
8393 | xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf); | |
8394 | opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf); | |
8395 | ||
8396 | if (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode) | |
8397 | return true; | |
8398 | ||
8399 | return false; | |
8400 | } | |
8401 | ||
8402 | ||
8403 | /* Re-process all of the fragments looking to convert all of the | |
8404 | RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a | |
8405 | loop end label, convert this frag to one that will generate a NOP. | |
8406 | In any case close it off with a .fill 0. */ | |
8407 | ||
8408 | static bool next_instr_is_loop_end (fragS *); | |
8409 | ||
8410 | static void | |
8411 | xtensa_fix_b_j_loop_end_frags (void) | |
8412 | { | |
8413 | frchainS *frchP; | |
8414 | asection *s; | |
8415 | ||
8416 | /* When this routine is called, all of the subsections are still intact | |
8417 | so we walk over subsections instead of sections. */ | |
8418 | for (s = stdoutput->sections; s; s = s->next) | |
8419 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
8420 | { | |
8421 | fragS *fragP; | |
8422 | ||
8423 | /* Walk over all of the fragments in a subsection. */ | |
8424 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
8425 | { | |
8426 | if (fragP->fr_type == rs_machine_dependent | |
8427 | && fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END) | |
8428 | { | |
8429 | if (next_instr_is_loop_end (fragP)) | |
8430 | { | |
8431 | if (fragP->tc_frag_data.is_no_transform) | |
8432 | as_bad (_("branching or jumping to a loop end may trigger hardware errata")); | |
8433 | else | |
8434 | relax_frag_add_nop (fragP); | |
8435 | } | |
8436 | frag_wane (fragP); | |
8437 | } | |
8438 | } | |
8439 | } | |
8440 | } | |
8441 | ||
8442 | ||
8443 | static bool | |
8444 | next_instr_is_loop_end (fragS *fragP) | |
8445 | { | |
8446 | const fragS *next_fragP; | |
8447 | ||
8448 | if (next_frag_is_loop_target (fragP)) | |
8449 | return false; | |
8450 | ||
8451 | next_fragP = next_non_empty_frag (fragP); | |
8452 | if (next_fragP == NULL) | |
8453 | return false; | |
8454 | ||
8455 | if (!next_frag_is_loop_target (next_fragP)) | |
8456 | return false; | |
8457 | ||
8458 | /* If the size is >= 3 then there is more than one instruction here. | |
8459 | The hardware bug will not fire. */ | |
8460 | if (next_fragP->fr_fix > 3) | |
8461 | return false; | |
8462 | ||
8463 | return true; | |
8464 | } | |
8465 | ||
8466 | ||
8467 | /* Re-process all of the fragments looking to convert all of the | |
8468 | RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is | |
8469 | not MY loop's loop end within 12 bytes, add enough nops here to | |
8470 | make it at least 12 bytes away. In any case close it off with a | |
8471 | .fill 0. */ | |
8472 | ||
8473 | static offsetT min_bytes_to_other_loop_end | |
8474 | (fragS *, fragS *, offsetT); | |
8475 | ||
8476 | static void | |
8477 | xtensa_fix_close_loop_end_frags (void) | |
8478 | { | |
8479 | frchainS *frchP; | |
8480 | asection *s; | |
8481 | ||
8482 | /* When this routine is called, all of the subsections are still intact | |
8483 | so we walk over subsections instead of sections. */ | |
8484 | for (s = stdoutput->sections; s; s = s->next) | |
8485 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
8486 | { | |
8487 | fragS *fragP; | |
8488 | ||
8489 | fragS *current_target = NULL; | |
8490 | ||
8491 | /* Walk over all of the fragments in a subsection. */ | |
8492 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
8493 | { | |
8494 | if (fragP->fr_type == rs_machine_dependent | |
8495 | && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE) | |
8496 | || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE))) | |
8497 | current_target = symbol_get_frag (fragP->fr_symbol); | |
8498 | ||
8499 | if (current_target | |
8500 | && fragP->fr_type == rs_machine_dependent | |
8501 | && fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END) | |
8502 | { | |
8503 | offsetT min_bytes; | |
8504 | int bytes_added = 0; | |
8505 | ||
8506 | #define REQUIRED_LOOP_DIVIDING_BYTES 12 | |
8507 | /* Max out at 12. */ | |
8508 | min_bytes = min_bytes_to_other_loop_end | |
8509 | (fragP->fr_next, current_target, REQUIRED_LOOP_DIVIDING_BYTES); | |
8510 | ||
8511 | if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES) | |
8512 | { | |
8513 | if (fragP->tc_frag_data.is_no_transform) | |
8514 | as_bad (_("loop end too close to another loop end may trigger hardware errata")); | |
8515 | else | |
8516 | { | |
8517 | while (min_bytes + bytes_added | |
8518 | < REQUIRED_LOOP_DIVIDING_BYTES) | |
8519 | { | |
8520 | int length = 3; | |
8521 | ||
8522 | if (fragP->fr_var < length) | |
8523 | as_fatal (_("fr_var %lu < length %d"), | |
8524 | (long) fragP->fr_var, length); | |
8525 | else | |
8526 | { | |
8527 | assemble_nop (length, | |
8528 | fragP->fr_literal + fragP->fr_fix); | |
8529 | fragP->fr_fix += length; | |
8530 | fragP->fr_var -= length; | |
8531 | } | |
8532 | bytes_added += length; | |
8533 | } | |
8534 | } | |
8535 | } | |
8536 | frag_wane (fragP); | |
8537 | } | |
8538 | gas_assert (fragP->fr_type != rs_machine_dependent | |
8539 | || fragP->fr_subtype != RELAX_ADD_NOP_IF_CLOSE_LOOP_END); | |
8540 | } | |
8541 | } | |
8542 | } | |
8543 | ||
8544 | ||
8545 | static offsetT unrelaxed_frag_min_size (fragS *); | |
8546 | ||
8547 | static offsetT | |
8548 | min_bytes_to_other_loop_end (fragS *fragP, | |
8549 | fragS *current_target, | |
8550 | offsetT max_size) | |
8551 | { | |
8552 | offsetT offset = 0; | |
8553 | fragS *current_fragP; | |
8554 | ||
8555 | for (current_fragP = fragP; | |
8556 | current_fragP; | |
8557 | current_fragP = current_fragP->fr_next) | |
8558 | { | |
8559 | if (current_fragP->tc_frag_data.is_loop_target | |
8560 | && current_fragP != current_target) | |
8561 | return offset; | |
8562 | ||
8563 | offset += unrelaxed_frag_min_size (current_fragP); | |
8564 | ||
8565 | if (offset >= max_size) | |
8566 | return max_size; | |
8567 | } | |
8568 | return max_size; | |
8569 | } | |
8570 | ||
8571 | ||
8572 | static offsetT | |
8573 | unrelaxed_frag_min_size (fragS *fragP) | |
8574 | { | |
8575 | offsetT size = fragP->fr_fix; | |
8576 | ||
8577 | /* Add fill size. */ | |
8578 | if (fragP->fr_type == rs_fill) | |
8579 | size += fragP->fr_offset; | |
8580 | ||
8581 | return size; | |
8582 | } | |
8583 | ||
8584 | ||
8585 | static offsetT | |
8586 | unrelaxed_frag_max_size (fragS *fragP) | |
8587 | { | |
8588 | offsetT size = fragP->fr_fix; | |
8589 | switch (fragP->fr_type) | |
8590 | { | |
8591 | case 0: | |
8592 | /* Empty frags created by the obstack allocation scheme | |
8593 | end up with type 0. */ | |
8594 | break; | |
8595 | case rs_fill: | |
8596 | case rs_org: | |
8597 | case rs_space: | |
8598 | size += fragP->fr_offset; | |
8599 | break; | |
8600 | case rs_align: | |
8601 | case rs_align_code: | |
8602 | case rs_align_test: | |
8603 | case rs_leb128: | |
8604 | case rs_cfa: | |
8605 | case rs_dwarf2dbg: | |
8606 | case rs_sframe: | |
8607 | /* No further adjustments needed. */ | |
8608 | break; | |
8609 | case rs_machine_dependent: | |
8610 | if (fragP->fr_subtype != RELAX_DESIRE_ALIGN) | |
8611 | size += fragP->fr_var; | |
8612 | break; | |
8613 | default: | |
8614 | /* We had darn well better know how big it is. */ | |
8615 | gas_assert (0); | |
8616 | break; | |
8617 | } | |
8618 | ||
8619 | return size; | |
8620 | } | |
8621 | ||
8622 | ||
8623 | /* Re-process all of the fragments looking to convert all | |
8624 | of the RELAX_ADD_NOP_IF_SHORT_LOOP. If: | |
8625 | ||
8626 | A) | |
8627 | 1) the instruction size count to the loop end label | |
8628 | is too short (<= 2 instructions), | |
8629 | 2) loop has a jump or branch in it | |
8630 | ||
8631 | or B) | |
8632 | 1) workaround_all_short_loops is TRUE | |
8633 | 2) The generating loop was a 'loopgtz' or 'loopnez' | |
8634 | 3) the instruction size count to the loop end label is too short | |
8635 | (<= 2 instructions) | |
8636 | then convert this frag (and maybe the next one) to generate a NOP. | |
8637 | In any case close it off with a .fill 0. */ | |
8638 | ||
8639 | static int count_insns_to_loop_end (fragS *, bool, int); | |
8640 | static bool branch_before_loop_end (fragS *); | |
8641 | ||
8642 | static void | |
8643 | xtensa_fix_short_loop_frags (void) | |
8644 | { | |
8645 | frchainS *frchP; | |
8646 | asection *s; | |
8647 | ||
8648 | /* When this routine is called, all of the subsections are still intact | |
8649 | so we walk over subsections instead of sections. */ | |
8650 | for (s = stdoutput->sections; s; s = s->next) | |
8651 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
8652 | { | |
8653 | fragS *fragP; | |
8654 | xtensa_opcode current_opcode = XTENSA_UNDEFINED; | |
8655 | ||
8656 | /* Walk over all of the fragments in a subsection. */ | |
8657 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
8658 | { | |
8659 | if (fragP->fr_type == rs_machine_dependent | |
8660 | && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE) | |
8661 | || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE))) | |
8662 | { | |
8663 | TInsn t_insn; | |
8664 | fragS *loop_frag = next_non_empty_frag (fragP); | |
8665 | tinsn_from_chars (&t_insn, loop_frag->fr_opcode, 0); | |
8666 | current_opcode = t_insn.opcode; | |
8667 | gas_assert (xtensa_opcode_is_loop (xtensa_default_isa, | |
8668 | current_opcode) == 1); | |
8669 | } | |
8670 | ||
8671 | if (fragP->fr_type == rs_machine_dependent | |
8672 | && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP) | |
8673 | { | |
8674 | if (count_insns_to_loop_end (fragP->fr_next, true, 3) < 3 | |
8675 | && (branch_before_loop_end (fragP->fr_next) | |
8676 | || (workaround_all_short_loops | |
8677 | && current_opcode != XTENSA_UNDEFINED | |
8678 | && current_opcode != xtensa_loop_opcode))) | |
8679 | { | |
8680 | if (fragP->tc_frag_data.is_no_transform) | |
8681 | as_bad (_("loop containing less than three instructions may trigger hardware errata")); | |
8682 | else | |
8683 | relax_frag_add_nop (fragP); | |
8684 | } | |
8685 | frag_wane (fragP); | |
8686 | } | |
8687 | } | |
8688 | } | |
8689 | } | |
8690 | ||
8691 | ||
8692 | static int unrelaxed_frag_min_insn_count (fragS *); | |
8693 | ||
8694 | static int | |
8695 | count_insns_to_loop_end (fragS *base_fragP, | |
8696 | bool count_relax_add, | |
8697 | int max_count) | |
8698 | { | |
8699 | fragS *fragP = NULL; | |
8700 | int insn_count = 0; | |
8701 | ||
8702 | fragP = base_fragP; | |
8703 | ||
8704 | for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next) | |
8705 | { | |
8706 | insn_count += unrelaxed_frag_min_insn_count (fragP); | |
8707 | if (insn_count >= max_count) | |
8708 | return max_count; | |
8709 | ||
8710 | if (count_relax_add) | |
8711 | { | |
8712 | if (fragP->fr_type == rs_machine_dependent | |
8713 | && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP) | |
8714 | { | |
8715 | /* In order to add the appropriate number of | |
8716 | NOPs, we count an instruction for downstream | |
8717 | occurrences. */ | |
8718 | insn_count++; | |
8719 | if (insn_count >= max_count) | |
8720 | return max_count; | |
8721 | } | |
8722 | } | |
8723 | } | |
8724 | return insn_count; | |
8725 | } | |
8726 | ||
8727 | ||
8728 | static int | |
8729 | unrelaxed_frag_min_insn_count (fragS *fragP) | |
8730 | { | |
8731 | xtensa_isa isa = xtensa_default_isa; | |
8732 | static xtensa_insnbuf insnbuf = NULL; | |
8733 | int insn_count = 0; | |
8734 | unsigned int offset = 0; | |
8735 | ||
8736 | if (!fragP->tc_frag_data.is_insn) | |
8737 | return insn_count; | |
8738 | ||
8739 | if (!insnbuf) | |
8740 | insnbuf = xtensa_insnbuf_alloc (isa); | |
8741 | ||
8742 | /* Decode the fixed instructions. */ | |
8743 | while (offset < fragP->fr_fix) | |
8744 | { | |
8745 | xtensa_format fmt; | |
8746 | ||
8747 | xtensa_insnbuf_from_chars | |
8748 | (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0); | |
8749 | fmt = xtensa_format_decode (isa, insnbuf); | |
8750 | ||
8751 | if (fmt == XTENSA_UNDEFINED) | |
8752 | { | |
8753 | as_fatal (_("undecodable instruction in instruction frag")); | |
8754 | return insn_count; | |
8755 | } | |
8756 | offset += xtensa_format_length (isa, fmt); | |
8757 | insn_count++; | |
8758 | } | |
8759 | ||
8760 | return insn_count; | |
8761 | } | |
8762 | ||
8763 | ||
8764 | static bool unrelaxed_frag_has_b_j (fragS *); | |
8765 | ||
8766 | static bool | |
8767 | branch_before_loop_end (fragS *base_fragP) | |
8768 | { | |
8769 | fragS *fragP; | |
8770 | ||
8771 | for (fragP = base_fragP; | |
8772 | fragP && !fragP->tc_frag_data.is_loop_target; | |
8773 | fragP = fragP->fr_next) | |
8774 | { | |
8775 | if (unrelaxed_frag_has_b_j (fragP)) | |
8776 | return true; | |
8777 | } | |
8778 | return false; | |
8779 | } | |
8780 | ||
8781 | ||
8782 | static bool | |
8783 | unrelaxed_frag_has_b_j (fragS *fragP) | |
8784 | { | |
8785 | static xtensa_insnbuf insnbuf = NULL; | |
8786 | xtensa_isa isa = xtensa_default_isa; | |
8787 | unsigned int offset = 0; | |
8788 | ||
8789 | if (!fragP->tc_frag_data.is_insn) | |
8790 | return false; | |
8791 | ||
8792 | if (!insnbuf) | |
8793 | insnbuf = xtensa_insnbuf_alloc (isa); | |
8794 | ||
8795 | /* Decode the fixed instructions. */ | |
8796 | while (offset < fragP->fr_fix) | |
8797 | { | |
8798 | xtensa_format fmt; | |
8799 | int slot; | |
8800 | ||
8801 | xtensa_insnbuf_from_chars | |
8802 | (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0); | |
8803 | fmt = xtensa_format_decode (isa, insnbuf); | |
8804 | if (fmt == XTENSA_UNDEFINED) | |
8805 | return false; | |
8806 | ||
8807 | for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++) | |
8808 | { | |
8809 | xtensa_opcode opcode = | |
8810 | get_opcode_from_buf (fragP->fr_literal + offset, slot); | |
8811 | if (xtensa_opcode_is_branch (isa, opcode) == 1 | |
8812 | || xtensa_opcode_is_jump (isa, opcode) == 1) | |
8813 | return true; | |
8814 | } | |
8815 | offset += xtensa_format_length (isa, fmt); | |
8816 | } | |
8817 | return false; | |
8818 | } | |
8819 | ||
8820 | ||
8821 | /* Checks to be made after initial assembly but before relaxation. */ | |
8822 | ||
8823 | static bool is_empty_loop (const TInsn *, fragS *); | |
8824 | static bool is_local_forward_loop (const TInsn *, fragS *); | |
8825 | ||
8826 | static void | |
8827 | xtensa_sanity_check (void) | |
8828 | { | |
8829 | const char *file_name; | |
8830 | unsigned line; | |
8831 | frchainS *frchP; | |
8832 | asection *s; | |
8833 | ||
8834 | file_name = as_where (&line); | |
8835 | for (s = stdoutput->sections; s; s = s->next) | |
8836 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
8837 | { | |
8838 | fragS *fragP; | |
8839 | ||
8840 | /* Walk over all of the fragments in a subsection. */ | |
8841 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
8842 | { | |
8843 | if (fragP->fr_type == rs_machine_dependent | |
8844 | && fragP->fr_subtype == RELAX_SLOTS | |
8845 | && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED) | |
8846 | { | |
8847 | static xtensa_insnbuf insnbuf = NULL; | |
8848 | TInsn t_insn; | |
8849 | ||
8850 | if (fragP->fr_opcode != NULL) | |
8851 | { | |
8852 | if (!insnbuf) | |
8853 | insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa); | |
8854 | tinsn_from_chars (&t_insn, fragP->fr_opcode, 0); | |
8855 | tinsn_immed_from_frag (&t_insn, fragP, 0); | |
8856 | ||
8857 | if (xtensa_opcode_is_loop (xtensa_default_isa, | |
8858 | t_insn.opcode) == 1) | |
8859 | { | |
8860 | if (is_empty_loop (&t_insn, fragP)) | |
8861 | { | |
8862 | new_logical_line (fragP->fr_file, fragP->fr_line); | |
8863 | as_bad (_("invalid empty loop")); | |
8864 | } | |
8865 | if (!is_local_forward_loop (&t_insn, fragP)) | |
8866 | { | |
8867 | new_logical_line (fragP->fr_file, fragP->fr_line); | |
8868 | as_bad (_("loop target does not follow " | |
8869 | "loop instruction in section")); | |
8870 | } | |
8871 | } | |
8872 | } | |
8873 | } | |
8874 | } | |
8875 | } | |
8876 | new_logical_line (file_name, line); | |
8877 | } | |
8878 | ||
8879 | ||
8880 | #define LOOP_IMMED_OPN 1 | |
8881 | ||
8882 | /* Return TRUE if the loop target is the next non-zero fragment. */ | |
8883 | ||
8884 | static bool | |
8885 | is_empty_loop (const TInsn *insn, fragS *fragP) | |
8886 | { | |
8887 | const expressionS *exp; | |
8888 | symbolS *symbolP; | |
8889 | fragS *next_fragP; | |
8890 | ||
8891 | if (insn->insn_type != ITYPE_INSN) | |
8892 | return false; | |
8893 | ||
8894 | if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1) | |
8895 | return false; | |
8896 | ||
8897 | if (insn->ntok <= LOOP_IMMED_OPN) | |
8898 | return false; | |
8899 | ||
8900 | exp = &insn->tok[LOOP_IMMED_OPN]; | |
8901 | ||
8902 | if (exp->X_op != O_symbol) | |
8903 | return false; | |
8904 | ||
8905 | symbolP = exp->X_add_symbol; | |
8906 | if (!symbolP) | |
8907 | return false; | |
8908 | ||
8909 | if (symbol_get_frag (symbolP) == NULL) | |
8910 | return false; | |
8911 | ||
8912 | if (S_GET_VALUE (symbolP) != 0) | |
8913 | return false; | |
8914 | ||
8915 | /* Walk through the zero-size fragments from this one. If we find | |
8916 | the target fragment, then this is a zero-size loop. */ | |
8917 | ||
8918 | for (next_fragP = fragP->fr_next; | |
8919 | next_fragP != NULL; | |
8920 | next_fragP = next_fragP->fr_next) | |
8921 | { | |
8922 | if (next_fragP == symbol_get_frag (symbolP)) | |
8923 | return true; | |
8924 | if (next_fragP->fr_fix != 0) | |
8925 | return false; | |
8926 | } | |
8927 | return false; | |
8928 | } | |
8929 | ||
8930 | ||
8931 | static bool | |
8932 | is_local_forward_loop (const TInsn *insn, fragS *fragP) | |
8933 | { | |
8934 | const expressionS *exp; | |
8935 | symbolS *symbolP; | |
8936 | fragS *next_fragP; | |
8937 | ||
8938 | if (insn->insn_type != ITYPE_INSN) | |
8939 | return false; | |
8940 | ||
8941 | if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1) | |
8942 | return false; | |
8943 | ||
8944 | if (insn->ntok <= LOOP_IMMED_OPN) | |
8945 | return false; | |
8946 | ||
8947 | exp = &insn->tok[LOOP_IMMED_OPN]; | |
8948 | ||
8949 | if (exp->X_op != O_symbol) | |
8950 | return false; | |
8951 | ||
8952 | symbolP = exp->X_add_symbol; | |
8953 | if (!symbolP) | |
8954 | return false; | |
8955 | ||
8956 | if (symbol_get_frag (symbolP) == NULL) | |
8957 | return false; | |
8958 | ||
8959 | /* Walk through fragments until we find the target. | |
8960 | If we do not find the target, then this is an invalid loop. */ | |
8961 | ||
8962 | for (next_fragP = fragP->fr_next; | |
8963 | next_fragP != NULL; | |
8964 | next_fragP = next_fragP->fr_next) | |
8965 | { | |
8966 | if (next_fragP == symbol_get_frag (symbolP)) | |
8967 | return true; | |
8968 | } | |
8969 | ||
8970 | return false; | |
8971 | } | |
8972 | ||
8973 | #define XTINFO_NAME "Xtensa_Info" | |
8974 | #define XTINFO_NAMESZ 12 | |
8975 | #define XTINFO_TYPE 1 | |
8976 | ||
8977 | static void | |
8978 | xtensa_add_config_info (void) | |
8979 | { | |
8980 | asection *info_sec; | |
8981 | char *data, *p; | |
8982 | int sz; | |
8983 | ||
8984 | info_sec = subseg_new (".xtensa.info", 0); | |
8985 | bfd_set_section_flags (info_sec, SEC_HAS_CONTENTS | SEC_READONLY); | |
8986 | ||
8987 | data = XNEWVEC (char, 100); | |
8988 | sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n", | |
8989 | XSHAL_USE_ABSOLUTE_LITERALS, xtensa_abi_choice ()); | |
8990 | sz = strlen (data) + 1; | |
8991 | ||
8992 | /* Add enough null terminators to pad to a word boundary. */ | |
8993 | do | |
8994 | data[sz++] = 0; | |
8995 | while ((sz & 3) != 0); | |
8996 | ||
8997 | /* Follow the standard note section layout: | |
8998 | First write the length of the name string. */ | |
8999 | p = frag_more (4); | |
9000 | md_number_to_chars (p, (valueT) XTINFO_NAMESZ, 4); | |
9001 | ||
9002 | /* Next comes the length of the "descriptor", i.e., the actual data. */ | |
9003 | p = frag_more (4); | |
9004 | md_number_to_chars (p, (valueT) sz, 4); | |
9005 | ||
9006 | /* Write the note type. */ | |
9007 | p = frag_more (4); | |
9008 | md_number_to_chars (p, (valueT) XTINFO_TYPE, 4); | |
9009 | ||
9010 | /* Write the name field. */ | |
9011 | p = frag_more (XTINFO_NAMESZ); | |
9012 | memcpy (p, XTINFO_NAME, XTINFO_NAMESZ); | |
9013 | ||
9014 | /* Finally, write the descriptor. */ | |
9015 | p = frag_more (sz); | |
9016 | memcpy (p, data, sz); | |
9017 | ||
9018 | free (data); | |
9019 | } | |
9020 | ||
9021 | \f | |
9022 | /* Alignment Functions. */ | |
9023 | ||
9024 | static int | |
9025 | get_text_align_power (unsigned target_size) | |
9026 | { | |
9027 | if (target_size <= 4) | |
9028 | return 2; | |
9029 | ||
9030 | if (target_size <= 8) | |
9031 | return 3; | |
9032 | ||
9033 | if (target_size <= 16) | |
9034 | return 4; | |
9035 | ||
9036 | if (target_size <= 32) | |
9037 | return 5; | |
9038 | ||
9039 | if (target_size <= 64) | |
9040 | return 6; | |
9041 | ||
9042 | if (target_size <= 128) | |
9043 | return 7; | |
9044 | ||
9045 | if (target_size <= 256) | |
9046 | return 8; | |
9047 | ||
9048 | if (target_size <= 512) | |
9049 | return 9; | |
9050 | ||
9051 | if (target_size <= 1024) | |
9052 | return 10; | |
9053 | ||
9054 | gas_assert (0); | |
9055 | return 0; | |
9056 | } | |
9057 | ||
9058 | ||
9059 | static int | |
9060 | get_text_align_max_fill_size (int align_pow, | |
9061 | bool use_nops, | |
9062 | bool use_no_density) | |
9063 | { | |
9064 | if (!use_nops) | |
9065 | return (1 << align_pow); | |
9066 | if (use_no_density) | |
9067 | return 3 * (1 << align_pow); | |
9068 | ||
9069 | return 1 + (1 << align_pow); | |
9070 | } | |
9071 | ||
9072 | ||
9073 | /* Calculate the minimum bytes of fill needed at "address" to align a | |
9074 | target instruction of size "target_size" so that it does not cross a | |
9075 | power-of-two boundary specified by "align_pow". If "use_nops" is FALSE, | |
9076 | the fill can be an arbitrary number of bytes. Otherwise, the space must | |
9077 | be filled by NOP instructions. */ | |
9078 | ||
9079 | static int | |
9080 | get_text_align_fill_size (addressT address, | |
9081 | int align_pow, | |
9082 | int target_size, | |
9083 | bool use_nops, | |
9084 | bool use_no_density) | |
9085 | { | |
9086 | addressT alignment, fill, fill_limit, fill_step; | |
9087 | bool skip_one = false; | |
9088 | ||
9089 | alignment = (1 << align_pow); | |
9090 | gas_assert (target_size > 0 && alignment >= (addressT) target_size); | |
9091 | ||
9092 | if (!use_nops) | |
9093 | { | |
9094 | fill_limit = alignment; | |
9095 | fill_step = 1; | |
9096 | } | |
9097 | else if (!use_no_density) | |
9098 | { | |
9099 | /* Combine 2- and 3-byte NOPs to fill anything larger than one. */ | |
9100 | fill_limit = alignment * 2; | |
9101 | fill_step = 1; | |
9102 | skip_one = true; | |
9103 | } | |
9104 | else | |
9105 | { | |
9106 | /* Fill with 3-byte NOPs -- can only fill multiples of 3. */ | |
9107 | fill_limit = alignment * 3; | |
9108 | fill_step = 3; | |
9109 | } | |
9110 | ||
9111 | /* Try all fill sizes until finding one that works. */ | |
9112 | for (fill = 0; fill < fill_limit; fill += fill_step) | |
9113 | { | |
9114 | if (skip_one && fill == 1) | |
9115 | continue; | |
9116 | if ((address + fill) >> align_pow | |
9117 | == (address + fill + target_size - 1) >> align_pow) | |
9118 | return fill; | |
9119 | } | |
9120 | gas_assert (0); | |
9121 | return 0; | |
9122 | } | |
9123 | ||
9124 | ||
9125 | static int | |
9126 | branch_align_power (segT sec) | |
9127 | { | |
9128 | /* If the Xtensa processor has a fetch width of X, and | |
9129 | the section is aligned to at least that boundary, then a branch | |
9130 | target need only fit within that aligned block of memory to avoid | |
9131 | a stall. Otherwise, try to fit branch targets within 4-byte | |
9132 | aligned blocks (which may be insufficient, e.g., if the section | |
9133 | has no alignment, but it's good enough). */ | |
9134 | int fetch_align = get_text_align_power(xtensa_fetch_width); | |
9135 | int sec_align = get_recorded_alignment (sec); | |
9136 | ||
9137 | if (sec_align >= fetch_align) | |
9138 | return fetch_align; | |
9139 | ||
9140 | return 2; | |
9141 | } | |
9142 | ||
9143 | ||
9144 | /* This will assert if it is not possible. */ | |
9145 | ||
9146 | static int | |
9147 | get_text_align_nop_count (offsetT fill_size, bool use_no_density) | |
9148 | { | |
9149 | int count = 0; | |
9150 | ||
9151 | if (use_no_density) | |
9152 | { | |
9153 | gas_assert (fill_size % 3 == 0); | |
9154 | return (fill_size / 3); | |
9155 | } | |
9156 | ||
9157 | gas_assert (fill_size != 1); /* Bad argument. */ | |
9158 | ||
9159 | while (fill_size > 1) | |
9160 | { | |
9161 | int insn_size = 3; | |
9162 | if (fill_size == 2 || fill_size == 4) | |
9163 | insn_size = 2; | |
9164 | fill_size -= insn_size; | |
9165 | count++; | |
9166 | } | |
9167 | gas_assert (fill_size != 1); /* Bad algorithm. */ | |
9168 | return count; | |
9169 | } | |
9170 | ||
9171 | ||
9172 | static int | |
9173 | get_text_align_nth_nop_size (offsetT fill_size, | |
9174 | int n, | |
9175 | bool use_no_density) | |
9176 | { | |
9177 | int count = 0; | |
9178 | ||
9179 | if (use_no_density) | |
9180 | return 3; | |
9181 | ||
9182 | gas_assert (fill_size != 1); /* Bad argument. */ | |
9183 | ||
9184 | while (fill_size > 1) | |
9185 | { | |
9186 | int insn_size = 3; | |
9187 | if (fill_size == 2 || fill_size == 4) | |
9188 | insn_size = 2; | |
9189 | fill_size -= insn_size; | |
9190 | count++; | |
9191 | if (n + 1 == count) | |
9192 | return insn_size; | |
9193 | } | |
9194 | gas_assert (0); | |
9195 | return 0; | |
9196 | } | |
9197 | ||
9198 | ||
9199 | /* For the given fragment, find the appropriate address | |
9200 | for it to begin at if we are using NOPs to align it. */ | |
9201 | ||
9202 | static addressT | |
9203 | get_noop_aligned_address (fragS *fragP, addressT address) | |
9204 | { | |
9205 | /* The rule is: get next fragment's FIRST instruction. Find | |
9206 | the smallest number of bytes that need to be added to | |
9207 | ensure that the next fragment's FIRST instruction will fit | |
9208 | in a single word. | |
9209 | ||
9210 | E.G., 2 bytes : 0, 1, 2 mod 4 | |
9211 | 3 bytes: 0, 1 mod 4 | |
9212 | ||
9213 | If the FIRST instruction MIGHT be relaxed, | |
9214 | assume that it will become a 3-byte instruction. | |
9215 | ||
9216 | Note again here that LOOP instructions are not bundleable, | |
9217 | and this relaxation only applies to LOOP opcodes. */ | |
9218 | ||
9219 | int fill_size = 0; | |
9220 | int first_insn_size; | |
9221 | int loop_insn_size; | |
9222 | addressT pre_opcode_bytes; | |
9223 | int align_power; | |
9224 | fragS *first_insn; | |
9225 | xtensa_opcode opcode; | |
9226 | bool is_loop; | |
9227 | ||
9228 | gas_assert (fragP->fr_type == rs_machine_dependent); | |
9229 | gas_assert (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE); | |
9230 | ||
9231 | /* Find the loop frag. */ | |
9232 | first_insn = next_non_empty_frag (fragP); | |
9233 | /* Now find the first insn frag. */ | |
9234 | first_insn = next_non_empty_frag (first_insn); | |
9235 | ||
9236 | is_loop = next_frag_opcode_is_loop (fragP, &opcode); | |
9237 | gas_assert (is_loop); | |
9238 | loop_insn_size = xg_get_single_size (opcode); | |
9239 | ||
9240 | pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP); | |
9241 | pre_opcode_bytes += loop_insn_size; | |
9242 | ||
9243 | /* For loops, the alignment depends on the size of the | |
9244 | instruction following the loop, not the LOOP instruction. */ | |
9245 | ||
9246 | if (first_insn == NULL) | |
9247 | first_insn_size = xtensa_fetch_width; | |
9248 | else | |
9249 | first_insn_size = get_loop_align_size (frag_format_size (first_insn)); | |
9250 | ||
9251 | /* If it was 8, then we'll need a larger alignment for the section. */ | |
9252 | align_power = get_text_align_power (first_insn_size); | |
9253 | record_alignment (now_seg, align_power); | |
9254 | ||
9255 | fill_size = get_text_align_fill_size | |
9256 | (address + pre_opcode_bytes, align_power, first_insn_size, true, | |
9257 | fragP->tc_frag_data.is_no_density); | |
9258 | ||
9259 | return address + fill_size; | |
9260 | } | |
9261 | ||
9262 | ||
9263 | /* 3 mechanisms for relaxing an alignment: | |
9264 | ||
9265 | Align to a power of 2. | |
9266 | Align so the next fragment's instruction does not cross a word boundary. | |
9267 | Align the current instruction so that if the next instruction | |
9268 | were 3 bytes, it would not cross a word boundary. | |
9269 | ||
9270 | We can align with: | |
9271 | ||
9272 | zeros - This is easy; always insert zeros. | |
9273 | nops - 3-byte and 2-byte instructions | |
9274 | 2 - 2-byte nop | |
9275 | 3 - 3-byte nop | |
9276 | 4 - 2 2-byte nops | |
9277 | >=5 : 3-byte instruction + fn (n-3) | |
9278 | widening - widen previous instructions. */ | |
9279 | ||
9280 | static offsetT | |
9281 | get_aligned_diff (fragS *fragP, addressT address, offsetT *max_diff) | |
9282 | { | |
9283 | addressT target_address, loop_insn_offset; | |
9284 | int target_size; | |
9285 | xtensa_opcode loop_opcode; | |
9286 | bool is_loop; | |
9287 | int align_power; | |
9288 | offsetT opt_diff; | |
9289 | offsetT branch_align; | |
9290 | fragS *loop_frag; | |
9291 | ||
9292 | gas_assert (fragP->fr_type == rs_machine_dependent); | |
9293 | switch (fragP->fr_subtype) | |
9294 | { | |
9295 | case RELAX_DESIRE_ALIGN: | |
9296 | target_size = next_frag_format_size (fragP); | |
9297 | if (target_size == XTENSA_UNDEFINED) | |
9298 | target_size = 3; | |
9299 | align_power = branch_align_power (now_seg); | |
9300 | branch_align = 1 << align_power; | |
9301 | /* Don't count on the section alignment being as large as the target. */ | |
9302 | if (target_size > branch_align) | |
9303 | target_size = branch_align; | |
9304 | opt_diff = get_text_align_fill_size (address, align_power, | |
9305 | target_size, false, false); | |
9306 | ||
9307 | *max_diff = (opt_diff + branch_align | |
9308 | - (target_size + ((address + opt_diff) % branch_align))); | |
9309 | gas_assert (*max_diff >= opt_diff); | |
9310 | return opt_diff; | |
9311 | ||
9312 | case RELAX_ALIGN_NEXT_OPCODE: | |
9313 | /* The next non-empty frag after this one holds the LOOP instruction | |
9314 | that needs to be aligned. The required alignment depends on the | |
9315 | size of the next non-empty frag after the loop frag, i.e., the | |
9316 | first instruction in the loop. */ | |
9317 | loop_frag = next_non_empty_frag (fragP); | |
9318 | target_size = get_loop_align_size (next_frag_format_size (loop_frag)); | |
9319 | loop_insn_offset = 0; | |
9320 | is_loop = next_frag_opcode_is_loop (fragP, &loop_opcode); | |
9321 | gas_assert (is_loop); | |
9322 | ||
9323 | /* If the loop has been expanded then the LOOP instruction | |
9324 | could be at an offset from this fragment. */ | |
9325 | if (loop_frag->tc_frag_data.slot_subtypes[0] != RELAX_IMMED) | |
9326 | loop_insn_offset = get_expanded_loop_offset (loop_opcode); | |
9327 | ||
9328 | /* In an ideal world, which is what we are shooting for here, | |
9329 | we wouldn't need to use any NOPs immediately prior to the | |
9330 | LOOP instruction. If this approach fails, relax_frag_loop_align | |
9331 | will call get_noop_aligned_address. */ | |
9332 | target_address = | |
9333 | address + loop_insn_offset + xg_get_single_size (loop_opcode); | |
9334 | align_power = get_text_align_power (target_size); | |
9335 | opt_diff = get_text_align_fill_size (target_address, align_power, | |
9336 | target_size, false, false); | |
9337 | ||
9338 | *max_diff = xtensa_fetch_width | |
9339 | - ((target_address + opt_diff) % xtensa_fetch_width) | |
9340 | - target_size + opt_diff; | |
9341 | gas_assert (*max_diff >= opt_diff); | |
9342 | return opt_diff; | |
9343 | ||
9344 | default: | |
9345 | break; | |
9346 | } | |
9347 | gas_assert (0); | |
9348 | return 0; | |
9349 | } | |
9350 | ||
9351 | \f | |
9352 | /* md_relax_frag Hook and Helper Functions. */ | |
9353 | ||
9354 | static long relax_frag_loop_align (fragS *, long); | |
9355 | static long relax_frag_for_align (fragS *, long); | |
9356 | static long relax_frag_immed | |
9357 | (segT, fragS *, long, int, xtensa_format, int, int *, bool); | |
9358 | ||
9359 | /* Get projected address for the first fulcrum on a path from source to | |
9360 | target. */ | |
9361 | static addressT xg_get_fulcrum (addressT source, addressT target) | |
9362 | { | |
9363 | offsetT delta = target - source; | |
9364 | int n; | |
9365 | ||
9366 | n = (labs (delta) + J_RANGE - J_MARGIN - 1) / (J_RANGE - J_MARGIN); | |
9367 | return source + delta / n; | |
9368 | } | |
9369 | ||
9370 | /* Given trampoline index, source and target of a jump find the best | |
9371 | candidate trampoline for the first fulcrum. The best trampoline is | |
9372 | the one in the reach of "j' instruction from the source, closest to | |
9373 | the projected fulcrum address, and preferrably w/o a jump around or | |
9374 | with already initialized jump around. */ | |
9375 | static size_t xg_find_best_trampoline (struct trampoline_index *idx, | |
9376 | addressT source, addressT target) | |
9377 | { | |
9378 | addressT fulcrum = xg_get_fulcrum (source, target); | |
9379 | size_t dist = 0; | |
9380 | size_t best = -1; | |
9381 | size_t base_tr = xg_find_trampoline (idx, fulcrum); | |
9382 | int checked = 1; | |
9383 | ||
9384 | /* Check trampoline frags around the base_tr to find the best. */ | |
9385 | for (dist = 0; checked; ++dist) | |
9386 | { | |
9387 | int i; | |
9388 | size_t tr = base_tr - dist; | |
9389 | ||
9390 | checked = 0; | |
9391 | ||
9392 | /* Trampolines are checked in the following order: | |
9393 | base_tr, base_tr + 1, base_tr - 1, base_tr + 2, base_tr - 2 */ | |
9394 | for (i = 0; i < 2; ++i, tr = base_tr + dist + 1) | |
9395 | if (tr < idx->n_entries) | |
9396 | { | |
9397 | fragS *trampoline_frag = idx->entry[tr]; | |
9398 | offsetT off; | |
9399 | ||
9400 | /* Don't check trampolines outside source - target interval. */ | |
9401 | if ((trampoline_frag->fr_address < source && | |
9402 | trampoline_frag->fr_address < target) || | |
9403 | (trampoline_frag->fr_address > source && | |
9404 | trampoline_frag->fr_address > target)) | |
9405 | continue; | |
9406 | ||
9407 | /* Don't choose trampoline that contains the source. */ | |
9408 | if (source >= trampoline_frag->fr_address | |
9409 | && source <= trampoline_frag->fr_address + | |
9410 | trampoline_frag->fr_fix) | |
9411 | continue; | |
9412 | ||
9413 | off = trampoline_frag->fr_address - fulcrum; | |
9414 | /* Stop if some trampoline is found and the search is more than | |
9415 | J_RANGE / 4 from the projected fulcrum. A trampoline w/o jump | |
9416 | around is nice, but it shouldn't have much overhead. */ | |
9417 | if (best < idx->n_entries && labs (off) > J_RANGE / 4) | |
9418 | return best; | |
9419 | ||
9420 | off = trampoline_frag->fr_address - source; | |
9421 | if (labs (off) < J_RANGE - J_MARGIN) | |
9422 | { | |
9423 | ++checked; | |
9424 | /* Stop if a trampoline w/o jump around is found or initialized | |
9425 | trampoline with jump around is found. */ | |
9426 | if (!trampoline_frag->tc_frag_data.needs_jump_around || | |
9427 | trampoline_frag->fr_fix) | |
9428 | return tr; | |
9429 | else if (best >= idx->n_entries) | |
9430 | best = tr; | |
9431 | } | |
9432 | } | |
9433 | } | |
9434 | ||
9435 | if (best < idx->n_entries) | |
9436 | return best; | |
9437 | else | |
9438 | as_fatal (_("cannot find suitable trampoline")); | |
9439 | } | |
9440 | ||
9441 | static fixS *xg_relax_fixup (struct trampoline_index *idx, fixS *fixP) | |
9442 | { | |
9443 | symbolS *s = fixP->fx_addsy; | |
9444 | addressT source = fixP->fx_frag->fr_address; | |
9445 | addressT target = S_GET_VALUE (s) + fixP->fx_offset; | |
9446 | size_t tr = xg_find_best_trampoline (idx, source, target); | |
9447 | fragS *trampoline_frag = idx->entry[tr]; | |
9448 | fixS *newfixP; | |
9449 | ||
9450 | init_trampoline_frag (trampoline_frag); | |
9451 | newfixP = xg_append_jump (trampoline_frag, | |
9452 | fixP->fx_addsy, fixP->fx_offset); | |
9453 | ||
9454 | /* Adjust the fixup for the original "j" instruction to | |
9455 | point to the newly added jump. */ | |
9456 | fixP->fx_addsy = trampoline_frag->fr_symbol; | |
9457 | fixP->fx_offset = trampoline_frag->fr_fix - 3; | |
9458 | fixP->tc_fix_data.X_add_symbol = trampoline_frag->fr_symbol; | |
9459 | fixP->tc_fix_data.X_add_number = trampoline_frag->fr_fix - 3; | |
9460 | ||
9461 | trampoline_frag->tc_frag_data.relax_seen = false; | |
9462 | ||
9463 | if (xg_is_trampoline_frag_full (trampoline_frag)) | |
9464 | xg_remove_trampoline_from_index (idx, tr); | |
9465 | ||
9466 | return newfixP; | |
9467 | } | |
9468 | ||
9469 | static bool xg_is_relaxable_fixup (fixS *fixP) | |
9470 | { | |
9471 | xtensa_isa isa = xtensa_default_isa; | |
9472 | addressT addr = fixP->fx_frag->fr_address; | |
9473 | addressT target; | |
9474 | offsetT delta; | |
9475 | symbolS *s = fixP->fx_addsy; | |
9476 | int slot; | |
9477 | xtensa_format fmt; | |
9478 | xtensa_opcode opcode; | |
9479 | ||
9480 | if (fixP->fx_r_type < BFD_RELOC_XTENSA_SLOT0_OP || | |
9481 | fixP->fx_r_type > BFD_RELOC_XTENSA_SLOT14_OP) | |
9482 | return false; | |
9483 | ||
9484 | target = S_GET_VALUE (s) + fixP->fx_offset; | |
9485 | delta = target - addr; | |
9486 | ||
9487 | if (labs (delta) < J_RANGE - J_MARGIN) | |
9488 | return false; | |
9489 | ||
9490 | xtensa_insnbuf_from_chars (isa, trampoline_buf, | |
9491 | (unsigned char *) fixP->fx_frag->fr_literal + | |
9492 | fixP->fx_where, 0); | |
9493 | fmt = xtensa_format_decode (isa, trampoline_buf); | |
9494 | gas_assert (fmt != XTENSA_UNDEFINED); | |
9495 | slot = fixP->tc_fix_data.slot; | |
9496 | xtensa_format_get_slot (isa, fmt, slot, trampoline_buf, trampoline_slotbuf); | |
9497 | opcode = xtensa_opcode_decode (isa, fmt, slot, trampoline_slotbuf); | |
9498 | return opcode == xtensa_j_opcode; | |
9499 | } | |
9500 | ||
9501 | static void xg_relax_fixups (struct trampoline_seg *ts) | |
9502 | { | |
9503 | struct trampoline_index *idx = &ts->index; | |
9504 | segment_info_type *seginfo = seg_info (now_seg); | |
9505 | fixS *fx; | |
9506 | ||
9507 | for (fx = seginfo->fix_root; fx; fx = fx->fx_next) | |
9508 | { | |
9509 | fixS *fixP = fx; | |
9510 | struct trampoline_chain *tc = NULL; | |
9511 | ||
9512 | if (xg_is_relaxable_fixup (fixP)) | |
9513 | { | |
9514 | tc = xg_find_best_eq_target (ts, fixP->fx_frag->fr_address, | |
9515 | &fixP->fx_addsy, &fixP->fx_offset); | |
9516 | if (!tc) | |
9517 | tc = xg_create_trampoline_chain (ts, fixP->fx_addsy, | |
9518 | fixP->fx_offset); | |
9519 | gas_assert (tc); | |
9520 | } | |
9521 | ||
9522 | while (xg_is_relaxable_fixup (fixP)) | |
9523 | { | |
9524 | fixP = xg_relax_fixup (idx, fixP); | |
9525 | xg_add_location_to_chain (tc, fixP->fx_frag->fr_symbol, | |
9526 | fixP->fx_where); | |
9527 | } | |
9528 | } | |
9529 | } | |
9530 | ||
9531 | /* Given a trampoline frag relax all jumps that might want to use this | |
9532 | trampoline. Only do real work once per relaxation cycle, when | |
9533 | xg_relax_trampoline is called for the first trampoline in the now_seg. | |
9534 | Don't use stretch, don't update new_stretch: place fulcrums with a | |
9535 | slack to tolerate code movement. In the worst case if a jump between | |
9536 | two trampolines wouldn't reach the next relaxation pass will fix it. */ | |
9537 | static void xg_relax_trampoline (fragS *fragP, long stretch ATTRIBUTE_UNUSED, | |
9538 | long *new_stretch ATTRIBUTE_UNUSED) | |
9539 | { | |
9540 | struct trampoline_seg *ts = find_trampoline_seg (now_seg); | |
9541 | ||
9542 | if (ts->index.n_entries && ts->index.entry[0] == fragP) | |
9543 | xg_relax_fixups (ts); | |
9544 | } | |
9545 | ||
9546 | /* Return the number of bytes added to this fragment, given that the | |
9547 | input has been stretched already by "stretch". */ | |
9548 | ||
9549 | long | |
9550 | xtensa_relax_frag (fragS *fragP, long stretch, int *stretched_p) | |
9551 | { | |
9552 | xtensa_isa isa = xtensa_default_isa; | |
9553 | int unreported = fragP->tc_frag_data.unreported_expansion; | |
9554 | long new_stretch = 0; | |
9555 | const char *file_name; | |
9556 | unsigned line; | |
9557 | int lit_size; | |
9558 | static xtensa_insnbuf vbuf = NULL; | |
9559 | int slot, num_slots; | |
9560 | xtensa_format fmt; | |
9561 | ||
9562 | file_name = as_where (&line); | |
9563 | new_logical_line (fragP->fr_file, fragP->fr_line); | |
9564 | ||
9565 | fragP->tc_frag_data.unreported_expansion = 0; | |
9566 | ||
9567 | switch (fragP->fr_subtype) | |
9568 | { | |
9569 | case RELAX_ALIGN_NEXT_OPCODE: | |
9570 | /* Always convert. */ | |
9571 | if (fragP->tc_frag_data.relax_seen) | |
9572 | new_stretch = relax_frag_loop_align (fragP, stretch); | |
9573 | break; | |
9574 | ||
9575 | case RELAX_LOOP_END: | |
9576 | /* Do nothing. */ | |
9577 | break; | |
9578 | ||
9579 | case RELAX_LOOP_END_ADD_NOP: | |
9580 | /* Add a NOP and switch to .fill 0. */ | |
9581 | new_stretch = relax_frag_add_nop (fragP); | |
9582 | frag_wane (fragP); | |
9583 | break; | |
9584 | ||
9585 | case RELAX_DESIRE_ALIGN: | |
9586 | /* Do nothing. The narrowing before this frag will either align | |
9587 | it or not. */ | |
9588 | break; | |
9589 | ||
9590 | case RELAX_LITERAL: | |
9591 | case RELAX_LITERAL_FINAL: | |
9592 | return 0; | |
9593 | ||
9594 | case RELAX_LITERAL_NR: | |
9595 | lit_size = 4; | |
9596 | fragP->fr_subtype = RELAX_LITERAL_FINAL; | |
9597 | gas_assert (unreported == lit_size); | |
9598 | memset (&fragP->fr_literal[fragP->fr_fix], 0, 4); | |
9599 | fragP->fr_var -= lit_size; | |
9600 | fragP->fr_fix += lit_size; | |
9601 | new_stretch = 4; | |
9602 | break; | |
9603 | ||
9604 | case RELAX_SLOTS: | |
9605 | if (vbuf == NULL) | |
9606 | vbuf = xtensa_insnbuf_alloc (isa); | |
9607 | ||
9608 | xtensa_insnbuf_from_chars | |
9609 | (isa, vbuf, (unsigned char *) fragP->fr_opcode, 0); | |
9610 | fmt = xtensa_format_decode (isa, vbuf); | |
9611 | num_slots = xtensa_format_num_slots (isa, fmt); | |
9612 | ||
9613 | for (slot = 0; slot < num_slots; slot++) | |
9614 | { | |
9615 | switch (fragP->tc_frag_data.slot_subtypes[slot]) | |
9616 | { | |
9617 | case RELAX_NARROW: | |
9618 | if (fragP->tc_frag_data.relax_seen) | |
9619 | new_stretch += relax_frag_for_align (fragP, stretch); | |
9620 | break; | |
9621 | ||
9622 | case RELAX_IMMED: | |
9623 | case RELAX_IMMED_STEP1: | |
9624 | case RELAX_IMMED_STEP2: | |
9625 | case RELAX_IMMED_STEP3: | |
9626 | /* Place the immediate. */ | |
9627 | new_stretch += relax_frag_immed | |
9628 | (now_seg, fragP, stretch, | |
9629 | fragP->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED, | |
9630 | fmt, slot, stretched_p, false); | |
9631 | break; | |
9632 | ||
9633 | default: | |
9634 | /* This is OK; see the note in xg_assemble_vliw_tokens. */ | |
9635 | break; | |
9636 | } | |
9637 | } | |
9638 | break; | |
9639 | ||
9640 | case RELAX_LITERAL_POOL_BEGIN: | |
9641 | if (fragP->fr_var != 0) | |
9642 | { | |
9643 | /* We have a converted "candidate" literal pool; | |
9644 | assemble a jump around it. */ | |
9645 | TInsn insn; | |
9646 | if (!litpool_slotbuf) | |
9647 | { | |
9648 | litpool_buf = xtensa_insnbuf_alloc (isa); | |
9649 | litpool_slotbuf = xtensa_insnbuf_alloc (isa); | |
9650 | } | |
9651 | new_stretch += 3; | |
9652 | fragP->tc_frag_data.relax_seen = false; /* Need another pass. */ | |
9653 | fragP->tc_frag_data.is_insn = true; | |
9654 | tinsn_init (&insn); | |
9655 | insn.insn_type = ITYPE_INSN; | |
9656 | insn.opcode = xtensa_j_opcode; | |
9657 | insn.ntok = 1; | |
9658 | set_expr_symbol_offset (&insn.tok[0], fragP->fr_symbol, | |
9659 | fragP->fr_fix); | |
9660 | fmt = xg_get_single_format (xtensa_j_opcode); | |
9661 | tinsn_to_slotbuf (fmt, 0, &insn, litpool_slotbuf); | |
9662 | xtensa_format_set_slot (isa, fmt, 0, litpool_buf, litpool_slotbuf); | |
9663 | xtensa_insnbuf_to_chars (isa, litpool_buf, | |
9664 | (unsigned char *)fragP->fr_literal + | |
9665 | fragP->fr_fix, 3); | |
9666 | fragP->fr_fix += 3; | |
9667 | fragP->fr_var -= 3; | |
9668 | /* Add a fix-up. */ | |
9669 | fix_new (fragP, 0, 3, fragP->fr_symbol, 0, true, | |
9670 | BFD_RELOC_XTENSA_SLOT0_OP); | |
9671 | } | |
9672 | break; | |
9673 | ||
9674 | case RELAX_LITERAL_POOL_END: | |
9675 | case RELAX_LITERAL_POOL_CANDIDATE_BEGIN: | |
9676 | case RELAX_MAYBE_UNREACHABLE: | |
9677 | case RELAX_MAYBE_DESIRE_ALIGN: | |
9678 | /* No relaxation required. */ | |
9679 | break; | |
9680 | ||
9681 | case RELAX_FILL_NOP: | |
9682 | case RELAX_UNREACHABLE: | |
9683 | if (fragP->tc_frag_data.relax_seen) | |
9684 | new_stretch += relax_frag_for_align (fragP, stretch); | |
9685 | break; | |
9686 | ||
9687 | case RELAX_TRAMPOLINE: | |
9688 | if (fragP->tc_frag_data.relax_seen) | |
9689 | xg_relax_trampoline (fragP, stretch, &new_stretch); | |
9690 | break; | |
9691 | ||
9692 | default: | |
9693 | as_bad (_("bad relaxation state")); | |
9694 | } | |
9695 | ||
9696 | /* Tell gas we need another relaxation pass. */ | |
9697 | if (! fragP->tc_frag_data.relax_seen) | |
9698 | { | |
9699 | fragP->tc_frag_data.relax_seen = true; | |
9700 | *stretched_p = 1; | |
9701 | } | |
9702 | ||
9703 | new_logical_line (file_name, line); | |
9704 | return new_stretch; | |
9705 | } | |
9706 | ||
9707 | ||
9708 | static long | |
9709 | relax_frag_loop_align (fragS *fragP, long stretch) | |
9710 | { | |
9711 | addressT old_address, old_next_address, old_size; | |
9712 | addressT new_address, new_next_address, new_size; | |
9713 | addressT growth; | |
9714 | ||
9715 | /* All the frags with relax_frag_for_alignment prior to this one in the | |
9716 | section have been done, hopefully eliminating the need for a NOP here. | |
9717 | But, this will put it in if necessary. */ | |
9718 | ||
9719 | /* Calculate the old address of this fragment and the next fragment. */ | |
9720 | old_address = fragP->fr_address - stretch; | |
9721 | old_next_address = (fragP->fr_address - stretch + fragP->fr_fix + | |
9722 | fragP->tc_frag_data.text_expansion[0]); | |
9723 | old_size = old_next_address - old_address; | |
9724 | ||
9725 | /* Calculate the new address of this fragment and the next fragment. */ | |
9726 | new_address = fragP->fr_address; | |
9727 | new_next_address = | |
9728 | get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix); | |
9729 | new_size = new_next_address - new_address; | |
9730 | ||
9731 | growth = new_size - old_size; | |
9732 | ||
9733 | /* Fix up the text_expansion field and return the new growth. */ | |
9734 | fragP->tc_frag_data.text_expansion[0] += growth; | |
9735 | return growth; | |
9736 | } | |
9737 | ||
9738 | ||
9739 | /* Add a NOP instruction. */ | |
9740 | ||
9741 | static long | |
9742 | relax_frag_add_nop (fragS *fragP) | |
9743 | { | |
9744 | char *nop_buf = fragP->fr_literal + fragP->fr_fix; | |
9745 | int length = fragP->tc_frag_data.is_no_density ? 3 : 2; | |
9746 | assemble_nop (length, nop_buf); | |
9747 | fragP->tc_frag_data.is_insn = true; | |
9748 | ||
9749 | if (fragP->fr_var < length) | |
9750 | { | |
9751 | as_fatal (_("fr_var (%ld) < length (%d)"), (long) fragP->fr_var, length); | |
9752 | return 0; | |
9753 | } | |
9754 | ||
9755 | fragP->fr_fix += length; | |
9756 | fragP->fr_var -= length; | |
9757 | return length; | |
9758 | } | |
9759 | ||
9760 | ||
9761 | static long future_alignment_required (fragS *, long); | |
9762 | ||
9763 | static long | |
9764 | relax_frag_for_align (fragS *fragP, long stretch) | |
9765 | { | |
9766 | /* Overview of the relaxation procedure for alignment: | |
9767 | We can widen with NOPs or by widening instructions or by filling | |
9768 | bytes after jump instructions. Find the opportune places and widen | |
9769 | them if necessary. */ | |
9770 | ||
9771 | long stretch_me; | |
9772 | long diff; | |
9773 | ||
9774 | gas_assert (fragP->fr_subtype == RELAX_FILL_NOP | |
9775 | || fragP->fr_subtype == RELAX_UNREACHABLE | |
9776 | || (fragP->fr_subtype == RELAX_SLOTS | |
9777 | && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)); | |
9778 | ||
9779 | stretch_me = future_alignment_required (fragP, stretch); | |
9780 | diff = stretch_me - fragP->tc_frag_data.text_expansion[0]; | |
9781 | if (diff == 0) | |
9782 | return 0; | |
9783 | ||
9784 | if (diff < 0) | |
9785 | { | |
9786 | /* We expanded on a previous pass. Can we shrink now? */ | |
9787 | long shrink = fragP->tc_frag_data.text_expansion[0] - stretch_me; | |
9788 | if (shrink <= stretch && stretch > 0) | |
9789 | { | |
9790 | fragP->tc_frag_data.text_expansion[0] = stretch_me; | |
9791 | return -shrink; | |
9792 | } | |
9793 | return 0; | |
9794 | } | |
9795 | ||
9796 | /* Below here, diff > 0. */ | |
9797 | fragP->tc_frag_data.text_expansion[0] = stretch_me; | |
9798 | ||
9799 | return diff; | |
9800 | } | |
9801 | ||
9802 | ||
9803 | /* Return the address of the next frag that should be aligned. | |
9804 | ||
9805 | By "address" we mean the address it _would_ be at if there | |
9806 | is no action taken to align it between here and the target frag. | |
9807 | In other words, if no narrows and no fill nops are used between | |
9808 | here and the frag to align, _even_if_ some of the frags we use | |
9809 | to align targets have already expanded on a previous relaxation | |
9810 | pass. | |
9811 | ||
9812 | Also, count each frag that may be used to help align the target. | |
9813 | ||
9814 | Return 0 if there are no frags left in the chain that need to be | |
9815 | aligned. */ | |
9816 | ||
9817 | static addressT | |
9818 | find_address_of_next_align_frag (fragS **fragPP, | |
9819 | int *wide_nops, | |
9820 | int *narrow_nops, | |
9821 | int *widens, | |
9822 | bool *paddable) | |
9823 | { | |
9824 | fragS *fragP = *fragPP; | |
9825 | addressT address = fragP->fr_address; | |
9826 | ||
9827 | /* Do not reset the counts to 0. */ | |
9828 | ||
9829 | while (fragP) | |
9830 | { | |
9831 | /* Limit this to a small search. */ | |
9832 | if (*widens >= (int) xtensa_fetch_width) | |
9833 | { | |
9834 | *fragPP = fragP; | |
9835 | return 0; | |
9836 | } | |
9837 | address += fragP->fr_fix; | |
9838 | ||
9839 | if (fragP->fr_type == rs_fill) | |
9840 | address += fragP->fr_offset * fragP->fr_var; | |
9841 | else if (fragP->fr_type == rs_machine_dependent) | |
9842 | { | |
9843 | switch (fragP->fr_subtype) | |
9844 | { | |
9845 | case RELAX_UNREACHABLE: | |
9846 | *paddable = true; | |
9847 | break; | |
9848 | ||
9849 | case RELAX_FILL_NOP: | |
9850 | (*wide_nops)++; | |
9851 | if (!fragP->tc_frag_data.is_no_density) | |
9852 | (*narrow_nops)++; | |
9853 | break; | |
9854 | ||
9855 | case RELAX_SLOTS: | |
9856 | if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW) | |
9857 | { | |
9858 | (*widens)++; | |
9859 | break; | |
9860 | } | |
9861 | address += total_frag_text_expansion (fragP); | |
9862 | break; | |
9863 | ||
9864 | case RELAX_IMMED: | |
9865 | address += fragP->tc_frag_data.text_expansion[0]; | |
9866 | break; | |
9867 | ||
9868 | case RELAX_ALIGN_NEXT_OPCODE: | |
9869 | case RELAX_DESIRE_ALIGN: | |
9870 | *fragPP = fragP; | |
9871 | return address; | |
9872 | ||
9873 | case RELAX_MAYBE_UNREACHABLE: | |
9874 | case RELAX_MAYBE_DESIRE_ALIGN: | |
9875 | /* Do nothing. */ | |
9876 | break; | |
9877 | ||
9878 | default: | |
9879 | /* Just punt if we don't know the type. */ | |
9880 | *fragPP = fragP; | |
9881 | return 0; | |
9882 | } | |
9883 | } | |
9884 | else | |
9885 | { | |
9886 | /* Just punt if we don't know the type. */ | |
9887 | *fragPP = fragP; | |
9888 | return 0; | |
9889 | } | |
9890 | fragP = fragP->fr_next; | |
9891 | } | |
9892 | ||
9893 | *fragPP = fragP; | |
9894 | return 0; | |
9895 | } | |
9896 | ||
9897 | ||
9898 | static long bytes_to_stretch (fragS *, int, int, int, int); | |
9899 | ||
9900 | static long | |
9901 | future_alignment_required (fragS *fragP, long stretch ATTRIBUTE_UNUSED) | |
9902 | { | |
9903 | fragS *this_frag = fragP; | |
9904 | long address; | |
9905 | int num_widens = 0; | |
9906 | int wide_nops = 0; | |
9907 | int narrow_nops = 0; | |
9908 | bool paddable = false; | |
9909 | offsetT local_opt_diff; | |
9910 | offsetT opt_diff; | |
9911 | offsetT max_diff; | |
9912 | int stretch_amount = 0; | |
9913 | int local_stretch_amount; | |
9914 | int global_stretch_amount; | |
9915 | ||
9916 | address = find_address_of_next_align_frag | |
9917 | (&fragP, &wide_nops, &narrow_nops, &num_widens, &paddable); | |
9918 | ||
9919 | if (!address) | |
9920 | { | |
9921 | if (this_frag->tc_frag_data.is_aligning_branch) | |
9922 | this_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED; | |
9923 | else | |
9924 | frag_wane (this_frag); | |
9925 | } | |
9926 | else | |
9927 | { | |
9928 | local_opt_diff = get_aligned_diff (fragP, address, &max_diff); | |
9929 | opt_diff = local_opt_diff; | |
9930 | gas_assert (opt_diff >= 0); | |
9931 | gas_assert (max_diff >= opt_diff); | |
9932 | if (max_diff == 0) | |
9933 | return 0; | |
9934 | ||
9935 | if (fragP) | |
9936 | fragP = fragP->fr_next; | |
9937 | ||
9938 | while (fragP && opt_diff < max_diff && address) | |
9939 | { | |
9940 | /* We only use these to determine if we can exit early | |
9941 | because there will be plenty of ways to align future | |
9942 | align frags. */ | |
9943 | int glob_widens = 0; | |
9944 | int dnn = 0; | |
9945 | int dw = 0; | |
9946 | bool glob_pad = 0; | |
9947 | address = find_address_of_next_align_frag | |
9948 | (&fragP, &glob_widens, &dnn, &dw, &glob_pad); | |
9949 | /* If there is a padable portion, then skip. */ | |
9950 | if (glob_pad || glob_widens >= (1 << branch_align_power (now_seg))) | |
9951 | address = 0; | |
9952 | ||
9953 | if (address) | |
9954 | { | |
9955 | offsetT next_m_diff; | |
9956 | offsetT next_o_diff; | |
9957 | ||
9958 | /* Downrange frags haven't had stretch added to them yet. */ | |
9959 | address += stretch; | |
9960 | ||
9961 | /* The address also includes any text expansion from this | |
9962 | frag in a previous pass, but we don't want that. */ | |
9963 | address -= this_frag->tc_frag_data.text_expansion[0]; | |
9964 | ||
9965 | /* Assume we are going to move at least opt_diff. In | |
9966 | reality, we might not be able to, but assuming that | |
9967 | we will helps catch cases where moving opt_diff pushes | |
9968 | the next target from aligned to unaligned. */ | |
9969 | address += opt_diff; | |
9970 | ||
9971 | next_o_diff = get_aligned_diff (fragP, address, &next_m_diff); | |
9972 | ||
9973 | /* Now cleanup for the adjustments to address. */ | |
9974 | next_o_diff += opt_diff; | |
9975 | next_m_diff += opt_diff; | |
9976 | if (next_o_diff <= max_diff && next_o_diff > opt_diff) | |
9977 | opt_diff = next_o_diff; | |
9978 | if (next_m_diff < max_diff) | |
9979 | max_diff = next_m_diff; | |
9980 | fragP = fragP->fr_next; | |
9981 | } | |
9982 | } | |
9983 | ||
9984 | /* If there are enough wideners in between, do it. */ | |
9985 | if (paddable) | |
9986 | { | |
9987 | if (this_frag->fr_subtype == RELAX_UNREACHABLE) | |
9988 | { | |
9989 | gas_assert (opt_diff <= (signed) xtensa_fetch_width); | |
9990 | return opt_diff; | |
9991 | } | |
9992 | return 0; | |
9993 | } | |
9994 | local_stretch_amount | |
9995 | = bytes_to_stretch (this_frag, wide_nops, narrow_nops, | |
9996 | num_widens, local_opt_diff); | |
9997 | global_stretch_amount | |
9998 | = bytes_to_stretch (this_frag, wide_nops, narrow_nops, | |
9999 | num_widens, opt_diff); | |
10000 | /* If the condition below is true, then the frag couldn't | |
10001 | stretch the correct amount for the global case, so we just | |
10002 | optimize locally. We'll rely on the subsequent frags to get | |
10003 | the correct alignment in the global case. */ | |
10004 | if (global_stretch_amount < local_stretch_amount) | |
10005 | stretch_amount = local_stretch_amount; | |
10006 | else | |
10007 | stretch_amount = global_stretch_amount; | |
10008 | ||
10009 | if (this_frag->fr_subtype == RELAX_SLOTS | |
10010 | && this_frag->tc_frag_data.slot_subtypes[0] == RELAX_NARROW) | |
10011 | gas_assert (stretch_amount <= 1); | |
10012 | else if (this_frag->fr_subtype == RELAX_FILL_NOP) | |
10013 | { | |
10014 | if (this_frag->tc_frag_data.is_no_density) | |
10015 | gas_assert (stretch_amount == 3 || stretch_amount == 0); | |
10016 | else | |
10017 | gas_assert (stretch_amount <= 3); | |
10018 | } | |
10019 | } | |
10020 | return stretch_amount; | |
10021 | } | |
10022 | ||
10023 | ||
10024 | /* The idea: widen everything you can to get a target or loop aligned, | |
10025 | then start using NOPs. | |
10026 | ||
10027 | wide_nops = the number of wide NOPs available for aligning | |
10028 | narrow_nops = the number of narrow NOPs available for aligning | |
10029 | (a subset of wide_nops) | |
10030 | widens = the number of narrow instructions that should be widened | |
10031 | ||
10032 | */ | |
10033 | ||
10034 | static long | |
10035 | bytes_to_stretch (fragS *this_frag, | |
10036 | int wide_nops, | |
10037 | int narrow_nops, | |
10038 | int num_widens, | |
10039 | int desired_diff) | |
10040 | { | |
10041 | int nops_needed; | |
10042 | int nop_bytes; | |
10043 | int extra_bytes; | |
10044 | int bytes_short = desired_diff - num_widens; | |
10045 | ||
10046 | gas_assert (desired_diff >= 0 | |
10047 | && desired_diff < (signed) xtensa_fetch_width); | |
10048 | if (desired_diff == 0) | |
10049 | return 0; | |
10050 | ||
10051 | gas_assert (wide_nops > 0 || num_widens > 0); | |
10052 | ||
10053 | /* Always prefer widening to NOP-filling. */ | |
10054 | if (bytes_short < 0) | |
10055 | { | |
10056 | /* There are enough RELAX_NARROW frags after this one | |
10057 | to align the target without widening this frag in any way. */ | |
10058 | return 0; | |
10059 | } | |
10060 | ||
10061 | if (bytes_short == 0) | |
10062 | { | |
10063 | /* Widen every narrow between here and the align target | |
10064 | and the align target will be properly aligned. */ | |
10065 | if (this_frag->fr_subtype == RELAX_FILL_NOP) | |
10066 | return 0; | |
10067 | else | |
10068 | return 1; | |
10069 | } | |
10070 | ||
10071 | /* From here we will need at least one NOP to get an alignment. | |
10072 | However, we may not be able to align at all, in which case, | |
10073 | don't widen. */ | |
10074 | nops_needed = desired_diff / 3; | |
10075 | ||
10076 | /* If there aren't enough nops, don't widen. */ | |
10077 | if (nops_needed > wide_nops) | |
10078 | return 0; | |
10079 | ||
10080 | /* First try it with all wide nops. */ | |
10081 | nop_bytes = nops_needed * 3; | |
10082 | extra_bytes = desired_diff - nop_bytes; | |
10083 | ||
10084 | if (nop_bytes + num_widens >= desired_diff) | |
10085 | { | |
10086 | if (this_frag->fr_subtype == RELAX_FILL_NOP) | |
10087 | return 3; | |
10088 | else if (num_widens == extra_bytes) | |
10089 | return 1; | |
10090 | return 0; | |
10091 | } | |
10092 | ||
10093 | /* Add a narrow nop. */ | |
10094 | nops_needed++; | |
10095 | nop_bytes += 2; | |
10096 | extra_bytes -= 2; | |
10097 | if (narrow_nops == 0 || nops_needed > wide_nops) | |
10098 | return 0; | |
10099 | ||
10100 | if (nop_bytes + num_widens >= desired_diff && extra_bytes >= 0) | |
10101 | { | |
10102 | if (this_frag->fr_subtype == RELAX_FILL_NOP) | |
10103 | return !this_frag->tc_frag_data.is_no_density ? 2 : 3; | |
10104 | else if (num_widens == extra_bytes) | |
10105 | return 1; | |
10106 | return 0; | |
10107 | } | |
10108 | ||
10109 | /* Replace a wide nop with a narrow nop--we can get here if | |
10110 | extra_bytes was negative in the previous conditional. */ | |
10111 | if (narrow_nops == 1) | |
10112 | return 0; | |
10113 | nop_bytes--; | |
10114 | extra_bytes++; | |
10115 | if (nop_bytes + num_widens >= desired_diff) | |
10116 | { | |
10117 | if (this_frag->fr_subtype == RELAX_FILL_NOP) | |
10118 | return !this_frag->tc_frag_data.is_no_density ? 2 : 3; | |
10119 | else if (num_widens == extra_bytes) | |
10120 | return 1; | |
10121 | return 0; | |
10122 | } | |
10123 | ||
10124 | /* If we can't satisfy any of the above cases, then we can't align | |
10125 | using padding or fill nops. */ | |
10126 | return 0; | |
10127 | } | |
10128 | ||
10129 | ||
10130 | static fragS * | |
10131 | xg_find_best_trampoline_for_tinsn (TInsn *tinsn, fragS *fragP) | |
10132 | { | |
10133 | symbolS *sym = tinsn->tok[0].X_add_symbol; | |
10134 | addressT source = fragP->fr_address; | |
10135 | addressT target = S_GET_VALUE (sym) + tinsn->tok[0].X_add_number; | |
10136 | struct trampoline_seg *ts = find_trampoline_seg (now_seg); | |
10137 | size_t i; | |
10138 | ||
10139 | if (!ts || !ts->index.n_entries) | |
10140 | return NULL; | |
10141 | ||
10142 | i = xg_find_best_trampoline (&ts->index, source, target); | |
10143 | ||
10144 | return ts->index.entry[i]; | |
10145 | } | |
10146 | ||
10147 | ||
10148 | /* Append jump to sym + offset to the end of the trampoline frag fragP. | |
10149 | Adjust fragP's jump around if it's present. Adjust fragP's fr_fix/fr_var | |
10150 | and finish the frag if it's full (but don't remove it from the trampoline | |
10151 | frag index). Return fixup for the newly created jump. */ | |
10152 | static fixS *xg_append_jump (fragS *fragP, symbolS *sym, offsetT offset) | |
10153 | { | |
10154 | fixS *fixP; | |
10155 | TInsn insn; | |
10156 | xtensa_format fmt; | |
10157 | xtensa_isa isa = xtensa_default_isa; | |
10158 | ||
10159 | gas_assert (fragP->fr_var >= 3); | |
10160 | tinsn_init (&insn); | |
10161 | insn.insn_type = ITYPE_INSN; | |
10162 | insn.opcode = xtensa_j_opcode; | |
10163 | insn.ntok = 1; | |
10164 | set_expr_symbol_offset (&insn.tok[0], sym, offset); | |
10165 | fmt = xg_get_single_format (xtensa_j_opcode); | |
10166 | tinsn_to_slotbuf (fmt, 0, &insn, trampoline_slotbuf); | |
10167 | xtensa_format_set_slot (isa, fmt, 0, trampoline_buf, trampoline_slotbuf); | |
10168 | xtensa_insnbuf_to_chars (isa, trampoline_buf, | |
10169 | (unsigned char *)fragP->fr_literal + fragP->fr_fix, 3); | |
10170 | fixP = fix_new (fragP, fragP->fr_fix, 3, sym, offset, true, | |
10171 | BFD_RELOC_XTENSA_SLOT0_OP); | |
10172 | fixP->tc_fix_data.slot = 0; | |
10173 | ||
10174 | fragP->fr_fix += 3; | |
10175 | fragP->fr_var -= 3; | |
10176 | ||
10177 | /* Adjust the jump around this trampoline (if present). */ | |
10178 | if (fragP->tc_frag_data.jump_around_fix) | |
10179 | fragP->tc_frag_data.jump_around_fix->fx_offset += 3; | |
10180 | ||
10181 | /* Do we have room for more? */ | |
10182 | if (xg_is_trampoline_frag_full (fragP)) | |
10183 | { | |
10184 | frag_wane (fragP); | |
10185 | fragP->fr_subtype = 0; | |
10186 | } | |
10187 | ||
10188 | return fixP; | |
10189 | } | |
10190 | ||
10191 | ||
10192 | static int | |
10193 | init_trampoline_frag (fragS *fp) | |
10194 | { | |
10195 | int growth = 0; | |
10196 | ||
10197 | if (fp->fr_fix == 0) | |
10198 | { | |
10199 | symbolS *lsym; | |
10200 | char label[10 + 2 * sizeof(fp)]; | |
10201 | ||
10202 | sprintf (label, ".L0_TR_%p", fp); | |
10203 | lsym = (symbolS *) local_symbol_make (label, now_seg, fp, 0); | |
10204 | fp->fr_symbol = lsym; | |
10205 | if (fp->tc_frag_data.needs_jump_around) | |
10206 | { | |
10207 | fp->tc_frag_data.jump_around_fix = xg_append_jump (fp, lsym, 3); | |
10208 | growth = 3; | |
10209 | } | |
10210 | } | |
10211 | return growth; | |
10212 | } | |
10213 | ||
10214 | static int | |
10215 | xg_get_single_symbol_slot (fragS *fragP) | |
10216 | { | |
10217 | int i; | |
10218 | int slot = -1; | |
10219 | ||
10220 | for (i = 0; i < MAX_SLOTS; ++i) | |
10221 | if (fragP->tc_frag_data.slot_symbols[i]) | |
10222 | { | |
10223 | gas_assert (slot == -1); | |
10224 | slot = i; | |
10225 | } | |
10226 | ||
10227 | gas_assert (slot >= 0 && slot < MAX_SLOTS); | |
10228 | ||
10229 | return slot; | |
10230 | } | |
10231 | ||
10232 | static fixS * | |
10233 | add_jump_to_trampoline (fragS *tramp, fragS *origfrag) | |
10234 | { | |
10235 | int slot = xg_get_single_symbol_slot (origfrag); | |
10236 | fixS *fixP; | |
10237 | ||
10238 | /* Assemble a jump to the target label in the trampoline frag. */ | |
10239 | fixP = xg_append_jump (tramp, | |
10240 | origfrag->tc_frag_data.slot_symbols[slot], | |
10241 | origfrag->tc_frag_data.slot_offsets[slot]); | |
10242 | ||
10243 | /* Modify the original j to point here. */ | |
10244 | origfrag->tc_frag_data.slot_symbols[slot] = tramp->fr_symbol; | |
10245 | origfrag->tc_frag_data.slot_offsets[slot] = tramp->fr_fix - 3; | |
10246 | ||
10247 | /* If trampoline is full, remove it from the list. */ | |
10248 | if (xg_is_trampoline_frag_full (tramp)) | |
10249 | { | |
10250 | struct trampoline_seg *ts = find_trampoline_seg (now_seg); | |
10251 | size_t tr = xg_find_trampoline (&ts->index, tramp->fr_address); | |
10252 | ||
10253 | gas_assert (ts->index.entry[tr] == tramp); | |
10254 | xg_remove_trampoline_from_index (&ts->index, tr); | |
10255 | } | |
10256 | ||
10257 | return fixP; | |
10258 | } | |
10259 | ||
10260 | ||
10261 | static long | |
10262 | relax_frag_immed (segT segP, | |
10263 | fragS *fragP, | |
10264 | long stretch, | |
10265 | int min_steps, | |
10266 | xtensa_format fmt, | |
10267 | int slot, | |
10268 | int *stretched_p, | |
10269 | bool estimate_only) | |
10270 | { | |
10271 | TInsn tinsn; | |
10272 | int old_size; | |
10273 | bool negatable_branch = false; | |
10274 | bool branch_jmp_to_next = false; | |
10275 | bool from_wide_insn = false; | |
10276 | xtensa_isa isa = xtensa_default_isa; | |
10277 | IStack istack; | |
10278 | offsetT frag_offset; | |
10279 | int num_steps; | |
10280 | int num_text_bytes, num_literal_bytes; | |
10281 | int literal_diff, total_text_diff, this_text_diff; | |
10282 | ||
10283 | gas_assert (fragP->fr_opcode != NULL); | |
10284 | ||
10285 | xg_clear_vinsn (&cur_vinsn); | |
10286 | vinsn_from_chars (&cur_vinsn, fragP->fr_opcode); | |
10287 | if (cur_vinsn.num_slots > 1) | |
10288 | from_wide_insn = true; | |
10289 | ||
10290 | tinsn = cur_vinsn.slots[slot]; | |
10291 | tinsn_immed_from_frag (&tinsn, fragP, slot); | |
10292 | ||
10293 | if (estimate_only && xtensa_opcode_is_loop (isa, tinsn.opcode) == 1) | |
10294 | return 0; | |
10295 | ||
10296 | if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform) | |
10297 | branch_jmp_to_next = is_branch_jmp_to_next (&tinsn, fragP); | |
10298 | ||
10299 | negatable_branch = (xtensa_opcode_is_branch (isa, tinsn.opcode) == 1); | |
10300 | ||
10301 | old_size = xtensa_format_length (isa, fmt); | |
10302 | ||
10303 | /* Special case: replace a branch to the next instruction with a NOP. | |
10304 | This is required to work around a hardware bug in T1040.0 and also | |
10305 | serves as an optimization. */ | |
10306 | ||
10307 | if (branch_jmp_to_next | |
10308 | && ((old_size == 2) || (old_size == 3)) | |
10309 | && !next_frag_is_loop_target (fragP)) | |
10310 | return 0; | |
10311 | ||
10312 | /* Here is the fun stuff: Get the immediate field from this | |
10313 | instruction. If it fits, we are done. If not, find the next | |
10314 | instruction sequence that fits. */ | |
10315 | ||
10316 | frag_offset = fragP->fr_opcode - fragP->fr_literal; | |
10317 | istack_init (&istack); | |
10318 | num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP, frag_offset, | |
10319 | min_steps, stretch); | |
10320 | gas_assert (num_steps >= min_steps && num_steps <= RELAX_IMMED_MAXSTEPS); | |
10321 | ||
10322 | fragP->tc_frag_data.slot_subtypes[slot] = (int) RELAX_IMMED + num_steps; | |
10323 | ||
10324 | /* Figure out the number of bytes needed. */ | |
10325 | num_literal_bytes = get_num_stack_literal_bytes (&istack); | |
10326 | literal_diff | |
10327 | = num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot]; | |
10328 | num_text_bytes = get_num_stack_text_bytes (&istack); | |
10329 | ||
10330 | if (from_wide_insn) | |
10331 | { | |
10332 | int first = 0; | |
10333 | while (istack.insn[first].opcode == XTENSA_UNDEFINED) | |
10334 | first++; | |
10335 | ||
10336 | num_text_bytes += old_size; | |
10337 | if (opcode_fits_format_slot (istack.insn[first].opcode, fmt, slot)) | |
10338 | num_text_bytes -= xg_get_single_size (istack.insn[first].opcode); | |
10339 | else | |
10340 | { | |
10341 | /* The first instruction in the relaxed sequence will go after | |
10342 | the current wide instruction, and thus its symbolic immediates | |
10343 | might not fit. */ | |
10344 | ||
10345 | istack_init (&istack); | |
10346 | num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP, | |
10347 | frag_offset + old_size, | |
10348 | min_steps, stretch + old_size); | |
10349 | gas_assert (num_steps >= min_steps && num_steps <= RELAX_IMMED_MAXSTEPS); | |
10350 | ||
10351 | fragP->tc_frag_data.slot_subtypes[slot] | |
10352 | = (int) RELAX_IMMED + num_steps; | |
10353 | ||
10354 | num_literal_bytes = get_num_stack_literal_bytes (&istack); | |
10355 | literal_diff | |
10356 | = num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot]; | |
10357 | ||
10358 | num_text_bytes = get_num_stack_text_bytes (&istack) + old_size; | |
10359 | } | |
10360 | } | |
10361 | ||
10362 | total_text_diff = num_text_bytes - old_size; | |
10363 | this_text_diff = total_text_diff - fragP->tc_frag_data.text_expansion[slot]; | |
10364 | ||
10365 | /* It MUST get larger. If not, we could get an infinite loop. */ | |
10366 | gas_assert (num_text_bytes >= 0); | |
10367 | gas_assert (literal_diff >= 0); | |
10368 | gas_assert (total_text_diff >= 0); | |
10369 | ||
10370 | fragP->tc_frag_data.text_expansion[slot] = total_text_diff; | |
10371 | fragP->tc_frag_data.literal_expansion[slot] = num_literal_bytes; | |
10372 | gas_assert (fragP->tc_frag_data.text_expansion[slot] >= 0); | |
10373 | gas_assert (fragP->tc_frag_data.literal_expansion[slot] >= 0); | |
10374 | ||
10375 | /* Find the associated expandable literal for this. */ | |
10376 | if (literal_diff != 0) | |
10377 | { | |
10378 | fragS *lit_fragP = fragP->tc_frag_data.literal_frags[slot]; | |
10379 | if (lit_fragP) | |
10380 | { | |
10381 | gas_assert (literal_diff == 4); | |
10382 | lit_fragP->tc_frag_data.unreported_expansion += literal_diff; | |
10383 | ||
10384 | /* We expect that the literal section state has NOT been | |
10385 | modified yet. */ | |
10386 | gas_assert (lit_fragP->fr_type == rs_machine_dependent | |
10387 | && lit_fragP->fr_subtype == RELAX_LITERAL); | |
10388 | lit_fragP->fr_subtype = RELAX_LITERAL_NR; | |
10389 | ||
10390 | /* We need to mark this section for another iteration | |
10391 | of relaxation. */ | |
10392 | (*stretched_p)++; | |
10393 | } | |
10394 | } | |
10395 | ||
10396 | if (negatable_branch && istack.ninsn > 1) | |
10397 | update_next_frag_state (fragP); | |
10398 | ||
10399 | /* If last insn is a jump, and it cannot reach its target, try to find a trampoline. */ | |
10400 | if (istack.ninsn > 2 && | |
10401 | istack.insn[istack.ninsn - 1].insn_type == ITYPE_LABEL && | |
10402 | istack.insn[istack.ninsn - 2].insn_type == ITYPE_INSN && | |
10403 | istack.insn[istack.ninsn - 2].opcode == xtensa_j_opcode) | |
10404 | { | |
10405 | TInsn *jinsn = &istack.insn[istack.ninsn - 2]; | |
10406 | struct trampoline_seg *ts = find_trampoline_seg (segP); | |
10407 | struct trampoline_chain *tc = NULL; | |
10408 | ||
10409 | if (ts && | |
10410 | !xg_symbolic_immeds_fit (jinsn, segP, fragP, fragP->fr_offset, | |
10411 | total_text_diff)) | |
10412 | { | |
10413 | int s = xg_get_single_symbol_slot (fragP); | |
10414 | addressT offset = fragP->tc_frag_data.slot_offsets[s]; | |
10415 | ||
10416 | tc = xg_find_best_eq_target (ts, fragP->fr_address, | |
10417 | &fragP->tc_frag_data.slot_symbols[s], | |
10418 | &offset); | |
10419 | ||
10420 | if (!tc) | |
10421 | tc = xg_create_trampoline_chain (ts, | |
10422 | fragP->tc_frag_data.slot_symbols[s], | |
10423 | offset); | |
10424 | fragP->tc_frag_data.slot_offsets[s] = offset; | |
10425 | tinsn_immed_from_frag (jinsn, fragP, s); | |
10426 | } | |
10427 | ||
10428 | if (!xg_symbolic_immeds_fit (jinsn, segP, fragP, fragP->fr_offset, | |
10429 | total_text_diff)) | |
10430 | { | |
10431 | fragS *tf = xg_find_best_trampoline_for_tinsn (jinsn, fragP); | |
10432 | ||
10433 | if (tf) | |
10434 | { | |
10435 | fixS *fixP; | |
10436 | ||
10437 | this_text_diff += init_trampoline_frag (tf) + 3; | |
10438 | fixP = add_jump_to_trampoline (tf, fragP); | |
10439 | xg_add_location_to_chain (tc, fixP->fx_frag->fr_symbol, | |
10440 | fixP->fx_where); | |
10441 | fragP->tc_frag_data.relax_seen = false; | |
10442 | } | |
10443 | else | |
10444 | { | |
10445 | /* If target symbol is undefined, assume it will reach once linked. */ | |
10446 | expressionS *exp = &istack.insn[istack.ninsn - 2].tok[0]; | |
10447 | ||
10448 | if (exp->X_op == O_symbol && S_IS_DEFINED (exp->X_add_symbol)) | |
10449 | { | |
10450 | as_bad_where (fragP->fr_file, fragP->fr_line, | |
10451 | _("jump target out of range; no usable trampoline found")); | |
10452 | } | |
10453 | } | |
10454 | } | |
10455 | } | |
10456 | ||
10457 | return this_text_diff; | |
10458 | } | |
10459 | ||
10460 | \f | |
10461 | /* md_convert_frag Hook and Helper Functions. */ | |
10462 | ||
10463 | static void convert_frag_align_next_opcode (fragS *); | |
10464 | static void convert_frag_narrow (segT, fragS *, xtensa_format, int); | |
10465 | static void convert_frag_fill_nop (fragS *); | |
10466 | static void convert_frag_immed (segT, fragS *, int, xtensa_format, int); | |
10467 | ||
10468 | void | |
10469 | md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec, fragS *fragp) | |
10470 | { | |
10471 | static xtensa_insnbuf vbuf = NULL; | |
10472 | xtensa_isa isa = xtensa_default_isa; | |
10473 | int slot; | |
10474 | int num_slots; | |
10475 | xtensa_format fmt; | |
10476 | const char *file_name; | |
10477 | unsigned line; | |
10478 | ||
10479 | file_name = as_where (&line); | |
10480 | new_logical_line (fragp->fr_file, fragp->fr_line); | |
10481 | ||
10482 | switch (fragp->fr_subtype) | |
10483 | { | |
10484 | case RELAX_ALIGN_NEXT_OPCODE: | |
10485 | /* Always convert. */ | |
10486 | convert_frag_align_next_opcode (fragp); | |
10487 | break; | |
10488 | ||
10489 | case RELAX_DESIRE_ALIGN: | |
10490 | /* Do nothing. If not aligned already, too bad. */ | |
10491 | break; | |
10492 | ||
10493 | case RELAX_LITERAL: | |
10494 | case RELAX_LITERAL_FINAL: | |
10495 | break; | |
10496 | ||
10497 | case RELAX_SLOTS: | |
10498 | if (vbuf == NULL) | |
10499 | vbuf = xtensa_insnbuf_alloc (isa); | |
10500 | ||
10501 | xtensa_insnbuf_from_chars | |
10502 | (isa, vbuf, (unsigned char *) fragp->fr_opcode, 0); | |
10503 | fmt = xtensa_format_decode (isa, vbuf); | |
10504 | num_slots = xtensa_format_num_slots (isa, fmt); | |
10505 | ||
10506 | for (slot = 0; slot < num_slots; slot++) | |
10507 | { | |
10508 | switch (fragp->tc_frag_data.slot_subtypes[slot]) | |
10509 | { | |
10510 | case RELAX_NARROW: | |
10511 | convert_frag_narrow (sec, fragp, fmt, slot); | |
10512 | break; | |
10513 | ||
10514 | case RELAX_IMMED: | |
10515 | case RELAX_IMMED_STEP1: | |
10516 | case RELAX_IMMED_STEP2: | |
10517 | case RELAX_IMMED_STEP3: | |
10518 | /* Place the immediate. */ | |
10519 | convert_frag_immed | |
10520 | (sec, fragp, | |
10521 | fragp->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED, | |
10522 | fmt, slot); | |
10523 | break; | |
10524 | ||
10525 | default: | |
10526 | /* This is OK because some slots could have | |
10527 | relaxations and others have none. */ | |
10528 | break; | |
10529 | } | |
10530 | } | |
10531 | break; | |
10532 | ||
10533 | case RELAX_UNREACHABLE: | |
10534 | memset (&fragp->fr_literal[fragp->fr_fix], 0, fragp->fr_var); | |
10535 | fragp->fr_fix += fragp->tc_frag_data.text_expansion[0]; | |
10536 | fragp->fr_var -= fragp->tc_frag_data.text_expansion[0]; | |
10537 | frag_wane (fragp); | |
10538 | break; | |
10539 | ||
10540 | case RELAX_MAYBE_UNREACHABLE: | |
10541 | case RELAX_MAYBE_DESIRE_ALIGN: | |
10542 | frag_wane (fragp); | |
10543 | break; | |
10544 | ||
10545 | case RELAX_FILL_NOP: | |
10546 | convert_frag_fill_nop (fragp); | |
10547 | break; | |
10548 | ||
10549 | case RELAX_LITERAL_NR: | |
10550 | if (use_literal_section) | |
10551 | { | |
10552 | /* This should have been handled during relaxation. When | |
10553 | relaxing a code segment, literals sometimes need to be | |
10554 | added to the corresponding literal segment. If that | |
10555 | literal segment has already been relaxed, then we end up | |
10556 | in this situation. Marking the literal segments as data | |
10557 | would make this happen less often (since GAS always relaxes | |
10558 | code before data), but we could still get into trouble if | |
10559 | there are instructions in a segment that is not marked as | |
10560 | containing code. Until we can implement a better solution, | |
10561 | cheat and adjust the addresses of all the following frags. | |
10562 | This could break subsequent alignments, but the linker's | |
10563 | literal coalescing will do that anyway. */ | |
10564 | ||
10565 | fragS *f; | |
10566 | fragp->fr_subtype = RELAX_LITERAL_FINAL; | |
10567 | gas_assert (fragp->tc_frag_data.unreported_expansion == 4); | |
10568 | memset (&fragp->fr_literal[fragp->fr_fix], 0, 4); | |
10569 | fragp->fr_var -= 4; | |
10570 | fragp->fr_fix += 4; | |
10571 | for (f = fragp->fr_next; f; f = f->fr_next) | |
10572 | f->fr_address += 4; | |
10573 | } | |
10574 | else | |
10575 | as_bad (_("invalid relaxation fragment result")); | |
10576 | break; | |
10577 | ||
10578 | case RELAX_TRAMPOLINE: | |
10579 | break; | |
10580 | } | |
10581 | ||
10582 | fragp->fr_var = 0; | |
10583 | new_logical_line (file_name, line); | |
10584 | } | |
10585 | ||
10586 | ||
10587 | static void | |
10588 | convert_frag_align_next_opcode (fragS *fragp) | |
10589 | { | |
10590 | char *nop_buf; /* Location for Writing. */ | |
10591 | bool use_no_density = fragp->tc_frag_data.is_no_density; | |
10592 | addressT aligned_address; | |
10593 | offsetT fill_size; | |
10594 | int nop, nop_count; | |
10595 | ||
10596 | aligned_address = get_noop_aligned_address (fragp, fragp->fr_address + | |
10597 | fragp->fr_fix); | |
10598 | fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix); | |
10599 | nop_count = get_text_align_nop_count (fill_size, use_no_density); | |
10600 | nop_buf = fragp->fr_literal + fragp->fr_fix; | |
10601 | ||
10602 | for (nop = 0; nop < nop_count; nop++) | |
10603 | { | |
10604 | int nop_size; | |
10605 | nop_size = get_text_align_nth_nop_size (fill_size, nop, use_no_density); | |
10606 | ||
10607 | assemble_nop (nop_size, nop_buf); | |
10608 | nop_buf += nop_size; | |
10609 | } | |
10610 | ||
10611 | fragp->fr_fix += fill_size; | |
10612 | fragp->fr_var -= fill_size; | |
10613 | } | |
10614 | ||
10615 | ||
10616 | static void | |
10617 | convert_frag_narrow (segT segP, fragS *fragP, xtensa_format fmt, int slot) | |
10618 | { | |
10619 | TInsn tinsn, single_target; | |
10620 | int size, old_size, diff; | |
10621 | offsetT frag_offset; | |
10622 | ||
10623 | gas_assert (slot == 0); | |
10624 | tinsn_from_chars (&tinsn, fragP->fr_opcode, 0); | |
10625 | ||
10626 | if (fragP->tc_frag_data.is_aligning_branch == 1) | |
10627 | { | |
10628 | gas_assert (fragP->tc_frag_data.text_expansion[0] == 1 | |
10629 | || fragP->tc_frag_data.text_expansion[0] == 0); | |
10630 | convert_frag_immed (segP, fragP, fragP->tc_frag_data.text_expansion[0], | |
10631 | fmt, slot); | |
10632 | return; | |
10633 | } | |
10634 | ||
10635 | if (fragP->tc_frag_data.text_expansion[0] == 0) | |
10636 | { | |
10637 | /* No conversion. */ | |
10638 | fragP->fr_var = 0; | |
10639 | return; | |
10640 | } | |
10641 | ||
10642 | gas_assert (fragP->fr_opcode != NULL); | |
10643 | ||
10644 | /* Frags in this relaxation state should only contain | |
10645 | single instruction bundles. */ | |
10646 | tinsn_immed_from_frag (&tinsn, fragP, 0); | |
10647 | ||
10648 | /* Just convert it to a wide form.... */ | |
10649 | size = 0; | |
10650 | old_size = xg_get_single_size (tinsn.opcode); | |
10651 | ||
10652 | tinsn_init (&single_target); | |
10653 | frag_offset = fragP->fr_opcode - fragP->fr_literal; | |
10654 | ||
10655 | if (! xg_is_single_relaxable_insn (&tinsn, &single_target, false)) | |
10656 | { | |
10657 | as_bad (_("unable to widen instruction")); | |
10658 | return; | |
10659 | } | |
10660 | ||
10661 | size = xg_get_single_size (single_target.opcode); | |
10662 | xg_emit_insn_to_buf (&single_target, fragP->fr_opcode, fragP, | |
10663 | frag_offset, true); | |
10664 | ||
10665 | diff = size - old_size; | |
10666 | gas_assert (diff >= 0); | |
10667 | gas_assert (diff <= fragP->fr_var); | |
10668 | fragP->fr_var -= diff; | |
10669 | fragP->fr_fix += diff; | |
10670 | ||
10671 | /* clean it up */ | |
10672 | fragP->fr_var = 0; | |
10673 | } | |
10674 | ||
10675 | ||
10676 | static void | |
10677 | convert_frag_fill_nop (fragS *fragP) | |
10678 | { | |
10679 | char *loc = &fragP->fr_literal[fragP->fr_fix]; | |
10680 | int size = fragP->tc_frag_data.text_expansion[0]; | |
10681 | gas_assert ((unsigned) size == (fragP->fr_next->fr_address | |
10682 | - fragP->fr_address - fragP->fr_fix)); | |
10683 | if (size == 0) | |
10684 | { | |
10685 | /* No conversion. */ | |
10686 | fragP->fr_var = 0; | |
10687 | return; | |
10688 | } | |
10689 | assemble_nop (size, loc); | |
10690 | fragP->tc_frag_data.is_insn = true; | |
10691 | fragP->fr_var -= size; | |
10692 | fragP->fr_fix += size; | |
10693 | frag_wane (fragP); | |
10694 | } | |
10695 | ||
10696 | ||
10697 | static fixS *fix_new_exp_in_seg | |
10698 | (segT, subsegT, fragS *, int, int, expressionS *, int, | |
10699 | bfd_reloc_code_real_type); | |
10700 | ||
10701 | static void | |
10702 | convert_frag_immed (segT segP, | |
10703 | fragS *fragP, | |
10704 | int min_steps, | |
10705 | xtensa_format fmt, | |
10706 | int slot) | |
10707 | { | |
10708 | char *immed_instr = fragP->fr_opcode; | |
10709 | TInsn orig_tinsn; | |
10710 | bool expanded = false; | |
10711 | bool branch_jmp_to_next = false; | |
10712 | char *fr_opcode = fragP->fr_opcode; | |
10713 | xtensa_isa isa = xtensa_default_isa; | |
10714 | bool from_wide_insn = false; | |
10715 | int bytes; | |
10716 | bool is_loop; | |
10717 | ||
10718 | gas_assert (fr_opcode != NULL); | |
10719 | ||
10720 | xg_clear_vinsn (&cur_vinsn); | |
10721 | ||
10722 | vinsn_from_chars (&cur_vinsn, fr_opcode); | |
10723 | if (cur_vinsn.num_slots > 1) | |
10724 | from_wide_insn = true; | |
10725 | ||
10726 | orig_tinsn = cur_vinsn.slots[slot]; | |
10727 | tinsn_immed_from_frag (&orig_tinsn, fragP, slot); | |
10728 | ||
10729 | is_loop = xtensa_opcode_is_loop (xtensa_default_isa, orig_tinsn.opcode) == 1; | |
10730 | ||
10731 | if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform) | |
10732 | branch_jmp_to_next = is_branch_jmp_to_next (&orig_tinsn, fragP); | |
10733 | ||
10734 | if (branch_jmp_to_next && !next_frag_is_loop_target (fragP)) | |
10735 | { | |
10736 | /* Conversion just inserts a NOP and marks the fix as completed. */ | |
10737 | bytes = xtensa_format_length (isa, fmt); | |
10738 | if (bytes >= 4) | |
10739 | { | |
10740 | cur_vinsn.slots[slot].opcode = | |
10741 | xtensa_format_slot_nop_opcode (isa, cur_vinsn.format, slot); | |
10742 | cur_vinsn.slots[slot].ntok = 0; | |
10743 | } | |
10744 | else | |
10745 | { | |
10746 | bytes += fragP->tc_frag_data.text_expansion[0]; | |
10747 | gas_assert (bytes == 2 || bytes == 3); | |
10748 | build_nop (&cur_vinsn.slots[0], bytes); | |
10749 | fragP->fr_fix += fragP->tc_frag_data.text_expansion[0]; | |
10750 | } | |
10751 | vinsn_to_insnbuf (&cur_vinsn, fr_opcode, frag_now, true); | |
10752 | xtensa_insnbuf_to_chars | |
10753 | (isa, cur_vinsn.insnbuf, (unsigned char *) fr_opcode, 0); | |
10754 | fragP->fr_var = 0; | |
10755 | } | |
10756 | else | |
10757 | { | |
10758 | /* Here is the fun stuff: Get the immediate field from this | |
10759 | instruction. If it fits, we're done. If not, find the next | |
10760 | instruction sequence that fits. */ | |
10761 | ||
10762 | IStack istack; | |
10763 | int i; | |
10764 | symbolS *lit_sym = NULL; | |
10765 | int total_size = 0; | |
10766 | int target_offset = 0; | |
10767 | int old_size; | |
10768 | int diff; | |
10769 | symbolS *gen_label = NULL; | |
10770 | offsetT frag_offset; | |
10771 | bool first = true; | |
10772 | ||
10773 | /* It does not fit. Find something that does and | |
10774 | convert immediately. */ | |
10775 | frag_offset = fr_opcode - fragP->fr_literal; | |
10776 | istack_init (&istack); | |
10777 | xg_assembly_relax (&istack, &orig_tinsn, | |
10778 | segP, fragP, frag_offset, min_steps, 0); | |
10779 | ||
10780 | old_size = xtensa_format_length (isa, fmt); | |
10781 | ||
10782 | /* Assemble this right inline. */ | |
10783 | ||
10784 | /* First, create the mapping from a label name to the REAL label. */ | |
10785 | target_offset = 0; | |
10786 | for (i = 0; i < istack.ninsn; i++) | |
10787 | { | |
10788 | TInsn *tinsn = &istack.insn[i]; | |
10789 | fragS *lit_frag; | |
10790 | ||
10791 | switch (tinsn->insn_type) | |
10792 | { | |
10793 | case ITYPE_LITERAL: | |
10794 | if (lit_sym != NULL) | |
10795 | as_bad (_("multiple literals in expansion")); | |
10796 | /* First find the appropriate space in the literal pool. */ | |
10797 | lit_frag = fragP->tc_frag_data.literal_frags[slot]; | |
10798 | if (lit_frag == NULL) | |
10799 | as_bad (_("no registered fragment for literal")); | |
10800 | if (tinsn->ntok != 1) | |
10801 | as_bad (_("number of literal tokens != 1")); | |
10802 | ||
10803 | /* Set the literal symbol and add a fixup. */ | |
10804 | lit_sym = lit_frag->fr_symbol; | |
10805 | break; | |
10806 | ||
10807 | case ITYPE_LABEL: | |
10808 | if (align_targets && !is_loop) | |
10809 | { | |
10810 | fragS *unreach = fragP->fr_next; | |
10811 | while (!(unreach->fr_type == rs_machine_dependent | |
10812 | && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE | |
10813 | || unreach->fr_subtype == RELAX_UNREACHABLE))) | |
10814 | { | |
10815 | unreach = unreach->fr_next; | |
10816 | } | |
10817 | ||
10818 | gas_assert (unreach->fr_type == rs_machine_dependent | |
10819 | && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE | |
10820 | || unreach->fr_subtype == RELAX_UNREACHABLE)); | |
10821 | ||
10822 | target_offset += unreach->tc_frag_data.text_expansion[0]; | |
10823 | } | |
10824 | gas_assert (gen_label == NULL); | |
10825 | gen_label = symbol_new (FAKE_LABEL_NAME, now_seg, fragP, | |
10826 | fr_opcode - fragP->fr_literal | |
10827 | + target_offset); | |
10828 | break; | |
10829 | ||
10830 | case ITYPE_INSN: | |
10831 | if (first && from_wide_insn) | |
10832 | { | |
10833 | target_offset += xtensa_format_length (isa, fmt); | |
10834 | first = false; | |
10835 | if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot)) | |
10836 | target_offset += xg_get_single_size (tinsn->opcode); | |
10837 | } | |
10838 | else | |
10839 | target_offset += xg_get_single_size (tinsn->opcode); | |
10840 | break; | |
10841 | } | |
10842 | } | |
10843 | ||
10844 | total_size = 0; | |
10845 | first = true; | |
10846 | for (i = 0; i < istack.ninsn; i++) | |
10847 | { | |
10848 | TInsn *tinsn = &istack.insn[i]; | |
10849 | fragS *lit_frag; | |
10850 | int size; | |
10851 | segT target_seg; | |
10852 | bfd_reloc_code_real_type reloc_type; | |
10853 | ||
10854 | switch (tinsn->insn_type) | |
10855 | { | |
10856 | case ITYPE_LITERAL: | |
10857 | lit_frag = fragP->tc_frag_data.literal_frags[slot]; | |
10858 | /* Already checked. */ | |
10859 | gas_assert (lit_frag != NULL); | |
10860 | gas_assert (lit_sym != NULL); | |
10861 | gas_assert (tinsn->ntok == 1); | |
10862 | /* Add a fixup. */ | |
10863 | target_seg = S_GET_SEGMENT (lit_sym); | |
10864 | gas_assert (target_seg); | |
10865 | reloc_type = map_operator_to_reloc (tinsn->tok[0].X_op, true); | |
10866 | fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4, | |
10867 | &tinsn->tok[0], false, reloc_type); | |
10868 | break; | |
10869 | ||
10870 | case ITYPE_LABEL: | |
10871 | break; | |
10872 | ||
10873 | case ITYPE_INSN: | |
10874 | xg_resolve_labels (tinsn, gen_label); | |
10875 | xg_resolve_literals (tinsn, lit_sym); | |
10876 | if (from_wide_insn && first) | |
10877 | { | |
10878 | first = false; | |
10879 | if (opcode_fits_format_slot (tinsn->opcode, fmt, slot)) | |
10880 | { | |
10881 | cur_vinsn.slots[slot] = *tinsn; | |
10882 | } | |
10883 | else | |
10884 | { | |
10885 | cur_vinsn.slots[slot].opcode = | |
10886 | xtensa_format_slot_nop_opcode (isa, fmt, slot); | |
10887 | cur_vinsn.slots[slot].ntok = 0; | |
10888 | } | |
10889 | vinsn_to_insnbuf (&cur_vinsn, immed_instr, fragP, true); | |
10890 | xtensa_insnbuf_to_chars (isa, cur_vinsn.insnbuf, | |
10891 | (unsigned char *) immed_instr, 0); | |
10892 | fragP->tc_frag_data.is_insn = true; | |
10893 | size = xtensa_format_length (isa, fmt); | |
10894 | if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot)) | |
10895 | { | |
10896 | xg_emit_insn_to_buf | |
10897 | (tinsn, immed_instr + size, fragP, | |
10898 | immed_instr - fragP->fr_literal + size, true); | |
10899 | size += xg_get_single_size (tinsn->opcode); | |
10900 | } | |
10901 | } | |
10902 | else | |
10903 | { | |
10904 | size = xg_get_single_size (tinsn->opcode); | |
10905 | xg_emit_insn_to_buf (tinsn, immed_instr, fragP, | |
10906 | immed_instr - fragP->fr_literal, true); | |
10907 | } | |
10908 | immed_instr += size; | |
10909 | total_size += size; | |
10910 | break; | |
10911 | } | |
10912 | } | |
10913 | ||
10914 | diff = total_size - old_size; | |
10915 | gas_assert (diff >= 0); | |
10916 | if (diff != 0) | |
10917 | expanded = true; | |
10918 | gas_assert (diff <= fragP->fr_var); | |
10919 | fragP->fr_var -= diff; | |
10920 | fragP->fr_fix += diff; | |
10921 | } | |
10922 | ||
10923 | /* Check for undefined immediates in LOOP instructions. */ | |
10924 | if (is_loop) | |
10925 | { | |
10926 | symbolS *sym; | |
10927 | sym = orig_tinsn.tok[1].X_add_symbol; | |
10928 | if (sym != NULL && !S_IS_DEFINED (sym)) | |
10929 | { | |
10930 | as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym)); | |
10931 | return; | |
10932 | } | |
10933 | sym = orig_tinsn.tok[1].X_op_symbol; | |
10934 | if (sym != NULL && !S_IS_DEFINED (sym)) | |
10935 | { | |
10936 | as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym)); | |
10937 | return; | |
10938 | } | |
10939 | } | |
10940 | ||
10941 | if (expanded && is_direct_call_opcode (orig_tinsn.opcode)) | |
10942 | { | |
10943 | /* Add an expansion note on the expanded instruction. */ | |
10944 | fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4, | |
10945 | &orig_tinsn.tok[0], true, | |
10946 | BFD_RELOC_XTENSA_ASM_EXPAND); | |
10947 | } | |
10948 | } | |
10949 | ||
10950 | ||
10951 | /* Add a new fix expression into the desired segment. We have to | |
10952 | switch to that segment to do this. */ | |
10953 | ||
10954 | static fixS * | |
10955 | fix_new_exp_in_seg (segT new_seg, | |
10956 | subsegT new_subseg, | |
10957 | fragS *frag, | |
10958 | int where, | |
10959 | int size, | |
10960 | expressionS *exp, | |
10961 | int pcrel, | |
10962 | bfd_reloc_code_real_type r_type) | |
10963 | { | |
10964 | fixS *new_fix; | |
10965 | segT seg = now_seg; | |
10966 | subsegT subseg = now_subseg; | |
10967 | ||
10968 | gas_assert (new_seg != 0); | |
10969 | subseg_set (new_seg, new_subseg); | |
10970 | ||
10971 | new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type); | |
10972 | subseg_set (seg, subseg); | |
10973 | return new_fix; | |
10974 | } | |
10975 | ||
10976 | ||
10977 | \f | |
10978 | /* A map that keeps information on a per-subsegment basis. This is | |
10979 | maintained during initial assembly, but is invalid once the | |
10980 | subsegments are smashed together. I.E., it cannot be used during | |
10981 | the relaxation. */ | |
10982 | ||
10983 | typedef struct subseg_map_struct | |
10984 | { | |
10985 | /* the key */ | |
10986 | segT seg; | |
10987 | subsegT subseg; | |
10988 | ||
10989 | /* the data */ | |
10990 | unsigned flags; | |
10991 | float total_freq; /* fall-through + branch target frequency */ | |
10992 | float target_freq; /* branch target frequency alone */ | |
10993 | ||
10994 | struct subseg_map_struct *next; | |
10995 | } subseg_map; | |
10996 | ||
10997 | ||
10998 | static subseg_map *sseg_map = NULL; | |
10999 | ||
11000 | static subseg_map * | |
11001 | get_subseg_info (segT seg, subsegT subseg) | |
11002 | { | |
11003 | subseg_map *subseg_e; | |
11004 | ||
11005 | for (subseg_e = sseg_map; subseg_e; subseg_e = subseg_e->next) | |
11006 | { | |
11007 | if (seg == subseg_e->seg && subseg == subseg_e->subseg) | |
11008 | break; | |
11009 | } | |
11010 | return subseg_e; | |
11011 | } | |
11012 | ||
11013 | ||
11014 | static subseg_map * | |
11015 | add_subseg_info (segT seg, subsegT subseg) | |
11016 | { | |
11017 | subseg_map *subseg_e = XNEW (subseg_map); | |
11018 | memset (subseg_e, 0, sizeof (subseg_map)); | |
11019 | subseg_e->seg = seg; | |
11020 | subseg_e->subseg = subseg; | |
11021 | subseg_e->flags = 0; | |
11022 | /* Start off considering every branch target very important. */ | |
11023 | subseg_e->target_freq = 1.0; | |
11024 | subseg_e->total_freq = 1.0; | |
11025 | subseg_e->next = sseg_map; | |
11026 | sseg_map = subseg_e; | |
11027 | return subseg_e; | |
11028 | } | |
11029 | ||
11030 | ||
11031 | static unsigned | |
11032 | get_last_insn_flags (segT seg, subsegT subseg) | |
11033 | { | |
11034 | subseg_map *subseg_e = get_subseg_info (seg, subseg); | |
11035 | if (subseg_e) | |
11036 | return subseg_e->flags; | |
11037 | return 0; | |
11038 | } | |
11039 | ||
11040 | ||
11041 | static void | |
11042 | set_last_insn_flags (segT seg, | |
11043 | subsegT subseg, | |
11044 | unsigned fl, | |
11045 | bool val) | |
11046 | { | |
11047 | subseg_map *subseg_e = get_subseg_info (seg, subseg); | |
11048 | if (! subseg_e) | |
11049 | subseg_e = add_subseg_info (seg, subseg); | |
11050 | if (val) | |
11051 | subseg_e->flags |= fl; | |
11052 | else | |
11053 | subseg_e->flags &= ~fl; | |
11054 | } | |
11055 | ||
11056 | ||
11057 | static float | |
11058 | get_subseg_total_freq (segT seg, subsegT subseg) | |
11059 | { | |
11060 | subseg_map *subseg_e = get_subseg_info (seg, subseg); | |
11061 | if (subseg_e) | |
11062 | return subseg_e->total_freq; | |
11063 | return 1.0; | |
11064 | } | |
11065 | ||
11066 | ||
11067 | static float | |
11068 | get_subseg_target_freq (segT seg, subsegT subseg) | |
11069 | { | |
11070 | subseg_map *subseg_e = get_subseg_info (seg, subseg); | |
11071 | if (subseg_e) | |
11072 | return subseg_e->target_freq; | |
11073 | return 1.0; | |
11074 | } | |
11075 | ||
11076 | ||
11077 | static void | |
11078 | set_subseg_freq (segT seg, subsegT subseg, float total_f, float target_f) | |
11079 | { | |
11080 | subseg_map *subseg_e = get_subseg_info (seg, subseg); | |
11081 | if (! subseg_e) | |
11082 | subseg_e = add_subseg_info (seg, subseg); | |
11083 | subseg_e->total_freq = total_f; | |
11084 | subseg_e->target_freq = target_f; | |
11085 | } | |
11086 | ||
11087 | \f | |
11088 | /* Segment Lists and emit_state Stuff. */ | |
11089 | ||
11090 | static void | |
11091 | xtensa_move_seg_list_to_beginning (seg_list *head) | |
11092 | { | |
11093 | head = head->next; | |
11094 | while (head) | |
11095 | { | |
11096 | segT literal_section = head->seg; | |
11097 | ||
11098 | /* Move the literal section to the front of the section list. */ | |
11099 | gas_assert (literal_section); | |
11100 | if (literal_section != stdoutput->sections) | |
11101 | { | |
11102 | bfd_section_list_remove (stdoutput, literal_section); | |
11103 | bfd_section_list_prepend (stdoutput, literal_section); | |
11104 | } | |
11105 | head = head->next; | |
11106 | } | |
11107 | } | |
11108 | ||
11109 | ||
11110 | static void mark_literal_frags (seg_list *); | |
11111 | ||
11112 | static void | |
11113 | xg_promote_candidate_litpool (struct litpool_seg *lps, | |
11114 | struct litpool_frag *lp) | |
11115 | { | |
11116 | fragS *poolbeg; | |
11117 | fragS *poolend; | |
11118 | symbolS *lsym; | |
11119 | char label[10 + 2 * sizeof (fragS *)]; | |
11120 | ||
11121 | poolbeg = lp->fragP; | |
11122 | lp->priority = 1; | |
11123 | poolbeg->fr_subtype = RELAX_LITERAL_POOL_BEGIN; | |
11124 | poolend = poolbeg->fr_next; | |
11125 | gas_assert (poolend->fr_type == rs_machine_dependent && | |
11126 | poolend->fr_subtype == RELAX_LITERAL_POOL_END); | |
11127 | /* Create a local symbol pointing to the | |
11128 | end of the pool. */ | |
11129 | sprintf (label, ".L0_LT_%p", poolbeg); | |
11130 | lsym = (symbolS *) local_symbol_make (label, lps->seg, poolend, 0); | |
11131 | poolbeg->fr_symbol = lsym; | |
11132 | /* Rest is done in xtensa_relax_frag. */ | |
11133 | } | |
11134 | ||
11135 | static struct litpool_frag *xg_find_litpool (struct litpool_seg *lps, | |
11136 | struct litpool_frag *lpf, | |
11137 | addressT addr) | |
11138 | { | |
11139 | struct litpool_frag *lp = lpf->prev; | |
11140 | ||
11141 | gas_assert (lp->fragP); | |
11142 | ||
11143 | while (lp->fragP->fr_subtype == RELAX_LITERAL_POOL_CANDIDATE_BEGIN) | |
11144 | { | |
11145 | lp = lp->prev; | |
11146 | if (lp->fragP == NULL) | |
11147 | { | |
11148 | /* End of list; have to bite the bullet. | |
11149 | Take the nearest. */ | |
11150 | lp = lpf->prev; | |
11151 | break; | |
11152 | } | |
11153 | /* Does it (conservatively) reach? */ | |
11154 | if (addr - lp->addr <= 128 * 1024) | |
11155 | { | |
11156 | if (lp->fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN && | |
11157 | lp->literal_count < MAX_POOL_LITERALS) | |
11158 | { | |
11159 | /* Found a good one. */ | |
11160 | break; | |
11161 | } | |
11162 | else if (lp->prev->fragP && | |
11163 | addr - lp->prev->addr > 128 * 1024 && | |
11164 | lp->prev->literal_count < MAX_POOL_LITERALS) | |
11165 | { | |
11166 | /* This is still a "candidate" but the next one | |
11167 | will be too far away, so revert to the nearest | |
11168 | one, convert it and add the jump around. */ | |
11169 | lp = lpf->prev; | |
11170 | break; | |
11171 | } | |
11172 | } | |
11173 | } | |
11174 | ||
11175 | if (lp->literal_count >= MAX_POOL_LITERALS) | |
11176 | { | |
11177 | lp = lpf->prev; | |
11178 | while (lp && lp->fragP && lp->literal_count >= MAX_POOL_LITERALS) | |
11179 | { | |
11180 | lp = lp->prev; | |
11181 | } | |
11182 | gas_assert (lp); | |
11183 | } | |
11184 | ||
11185 | gas_assert (lp && lp->fragP && lp->literal_count < MAX_POOL_LITERALS); | |
11186 | ++lp->literal_count; | |
11187 | ||
11188 | /* Convert candidate and add the jump around. */ | |
11189 | if (lp->fragP->fr_subtype == RELAX_LITERAL_POOL_CANDIDATE_BEGIN) | |
11190 | xg_promote_candidate_litpool (lps, lp); | |
11191 | ||
11192 | return lp; | |
11193 | } | |
11194 | ||
11195 | static bool xtensa_is_init_fini (segT seg) | |
11196 | { | |
11197 | if (!seg) | |
11198 | return 0; | |
11199 | return strcmp (segment_name (seg), INIT_SECTION_NAME) == 0 | |
11200 | || strcmp (segment_name (seg), FINI_SECTION_NAME) == 0; | |
11201 | } | |
11202 | ||
11203 | static void | |
11204 | xtensa_assign_litpool_addresses (void) | |
11205 | { | |
11206 | struct litpool_seg *lps; | |
11207 | ||
11208 | for (lps = litpool_seg_list.next; lps; lps = lps->next) | |
11209 | { | |
11210 | frchainS *frchP = seg_info (lps->seg)->frchainP; | |
11211 | struct litpool_frag *lpf = lps->frag_list.next; | |
11212 | addressT addr = 0; | |
11213 | ||
11214 | if (xtensa_is_init_fini (lps->seg)) | |
11215 | continue; | |
11216 | ||
11217 | for ( ; frchP; frchP = frchP->frch_next) | |
11218 | { | |
11219 | fragS *fragP; | |
11220 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
11221 | { | |
11222 | if (lpf && fragP == lpf->fragP) | |
11223 | { | |
11224 | gas_assert(fragP->fr_type == rs_machine_dependent && | |
11225 | (fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN || | |
11226 | fragP->fr_subtype == RELAX_LITERAL_POOL_CANDIDATE_BEGIN)); | |
11227 | /* Found a litpool location. */ | |
11228 | lpf->addr = addr; | |
11229 | lpf = lpf->next; | |
11230 | } | |
11231 | if (fragP->fr_type == rs_machine_dependent && | |
11232 | fragP->fr_subtype == RELAX_SLOTS) | |
11233 | { | |
11234 | int slot; | |
11235 | for (slot = 0; slot < MAX_SLOTS; slot++) | |
11236 | { | |
11237 | fragS *litfrag = fragP->tc_frag_data.literal_frags[slot]; | |
11238 | ||
11239 | if (litfrag | |
11240 | && litfrag->tc_frag_data.is_literal | |
11241 | && !litfrag->tc_frag_data.literal_frag) | |
11242 | { | |
11243 | /* L32R referring .literal or generated as a result | |
11244 | of relaxation. Point its literal to the nearest | |
11245 | litpool preferring non-"candidate" positions to | |
11246 | avoid the jump-around. */ | |
11247 | ||
11248 | struct litpool_frag *lp; | |
11249 | ||
11250 | lp = xg_find_litpool (lps, lpf, addr); | |
11251 | /* Take earliest use of this literal to avoid | |
11252 | forward refs. */ | |
11253 | litfrag->tc_frag_data.literal_frag = lp->fragP; | |
11254 | } | |
11255 | } | |
11256 | } | |
11257 | addr += fragP->fr_fix; | |
11258 | if (fragP->fr_type == rs_fill) | |
11259 | addr += fragP->fr_offset; | |
11260 | } | |
11261 | } | |
11262 | } | |
11263 | } | |
11264 | ||
11265 | static void | |
11266 | xtensa_move_literals (void) | |
11267 | { | |
11268 | seg_list *segment; | |
11269 | frchainS *frchain_from, *frchain_to; | |
11270 | fragS *search_frag, *next_frag, *literal_pool, *insert_after; | |
11271 | fragS **frag_splice; | |
11272 | emit_state state; | |
11273 | segT dest_seg; | |
11274 | fixS *fix, *next_fix, **fix_splice; | |
11275 | sym_list *lit; | |
11276 | const char *init_name = INIT_SECTION_NAME; | |
11277 | const char *fini_name = FINI_SECTION_NAME; | |
11278 | int init_name_len = strlen(init_name); | |
11279 | int fini_name_len = strlen(fini_name); | |
11280 | ||
11281 | mark_literal_frags (literal_head->next); | |
11282 | ||
11283 | if (use_literal_section) | |
11284 | return; | |
11285 | ||
11286 | /* Assign addresses (rough estimates) to the potential literal pool locations | |
11287 | and create new ones if the gaps are too large. */ | |
11288 | ||
11289 | xtensa_assign_litpool_addresses (); | |
11290 | ||
11291 | /* Walk through the literal segments. */ | |
11292 | for (segment = literal_head->next; segment; segment = segment->next) | |
11293 | { | |
11294 | const char *seg_name = segment_name (segment->seg); | |
11295 | ||
11296 | /* Keep the literals for .init and .fini in separate sections. */ | |
11297 | if ((!memcmp (seg_name, init_name, init_name_len) && | |
11298 | !strcmp (seg_name + init_name_len, ".literal")) || | |
11299 | (!memcmp (seg_name, fini_name, fini_name_len) && | |
11300 | !strcmp (seg_name + fini_name_len, ".literal"))) | |
11301 | continue; | |
11302 | ||
11303 | frchain_from = seg_info (segment->seg)->frchainP; | |
11304 | search_frag = frchain_from->frch_root; | |
11305 | literal_pool = NULL; | |
11306 | frchain_to = NULL; | |
11307 | frag_splice = &(frchain_from->frch_root); | |
11308 | ||
11309 | while (search_frag && !search_frag->tc_frag_data.literal_frag) | |
11310 | { | |
11311 | gas_assert (search_frag->fr_fix == 0 | |
11312 | || search_frag->fr_type == rs_align); | |
11313 | search_frag = search_frag->fr_next; | |
11314 | } | |
11315 | ||
11316 | if (!search_frag) | |
11317 | continue; | |
11318 | ||
11319 | gas_assert (search_frag->tc_frag_data.literal_frag->fr_subtype | |
11320 | == RELAX_LITERAL_POOL_BEGIN); | |
11321 | xtensa_switch_section_emit_state (&state, segment->seg, 0); | |
11322 | ||
11323 | /* Make sure that all the frags in this series are closed, and | |
11324 | that there is at least one left over of zero-size. This | |
11325 | prevents us from making a segment with an frchain without any | |
11326 | frags in it. */ | |
11327 | frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
11328 | xtensa_set_frag_assembly_state (frag_now); | |
11329 | frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
11330 | xtensa_set_frag_assembly_state (frag_now); | |
11331 | ||
11332 | while (search_frag != frag_now) | |
11333 | { | |
11334 | next_frag = search_frag->fr_next; | |
11335 | if (search_frag->tc_frag_data.literal_frag) | |
11336 | { | |
11337 | literal_pool = search_frag->tc_frag_data.literal_frag; | |
11338 | gas_assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN); | |
11339 | frchain_to = literal_pool->tc_frag_data.lit_frchain; | |
11340 | gas_assert (frchain_to); | |
11341 | } | |
11342 | ||
11343 | if (search_frag->fr_type == rs_fill && search_frag->fr_fix == 0) | |
11344 | { | |
11345 | /* Skip empty fill frags. */ | |
11346 | *frag_splice = next_frag; | |
11347 | search_frag = next_frag; | |
11348 | continue; | |
11349 | } | |
11350 | ||
11351 | if (search_frag->fr_type == rs_align) | |
11352 | { | |
11353 | /* Skip alignment frags, because the pool as a whole will be | |
11354 | aligned if used, and we don't want to force alignment if the | |
11355 | pool is unused. */ | |
11356 | *frag_splice = next_frag; | |
11357 | search_frag = next_frag; | |
11358 | continue; | |
11359 | } | |
11360 | ||
11361 | /* First, move the frag out of the literal section and | |
11362 | to the appropriate place. */ | |
11363 | ||
11364 | /* Insert an alignment frag at start of pool. */ | |
11365 | if (literal_pool->fr_next->fr_type == rs_machine_dependent && | |
11366 | literal_pool->fr_next->fr_subtype == RELAX_LITERAL_POOL_END) | |
11367 | { | |
11368 | segT pool_seg = literal_pool->fr_next->tc_frag_data.lit_seg; | |
11369 | emit_state prev_state; | |
11370 | fragS *prev_frag; | |
11371 | fragS *align_frag; | |
11372 | xtensa_switch_section_emit_state (&prev_state, pool_seg, 0); | |
11373 | prev_frag = frag_now; | |
11374 | frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
11375 | align_frag = frag_now; | |
11376 | frag_align (2, 0, 0); | |
11377 | /* Splice it into the right place. */ | |
11378 | prev_frag->fr_next = align_frag->fr_next; | |
11379 | align_frag->fr_next = literal_pool->fr_next; | |
11380 | literal_pool->fr_next = align_frag; | |
11381 | /* Insert after this one. */ | |
11382 | literal_pool->tc_frag_data.literal_frag = align_frag; | |
11383 | xtensa_restore_emit_state (&prev_state); | |
11384 | } | |
11385 | insert_after = literal_pool->tc_frag_data.literal_frag; | |
11386 | dest_seg = insert_after->fr_next->tc_frag_data.lit_seg; | |
11387 | /* Skip align frag. */ | |
11388 | if (insert_after->fr_next->fr_type == rs_align) | |
11389 | { | |
11390 | insert_after = insert_after->fr_next; | |
11391 | } | |
11392 | ||
11393 | *frag_splice = next_frag; | |
11394 | search_frag->fr_next = insert_after->fr_next; | |
11395 | insert_after->fr_next = search_frag; | |
11396 | search_frag->tc_frag_data.lit_seg = dest_seg; | |
11397 | literal_pool->tc_frag_data.literal_frag = search_frag; | |
11398 | ||
11399 | /* Now move any fixups associated with this frag to the | |
11400 | right section. */ | |
11401 | fix = frchain_from->fix_root; | |
11402 | fix_splice = &(frchain_from->fix_root); | |
11403 | while (fix) | |
11404 | { | |
11405 | next_fix = fix->fx_next; | |
11406 | if (fix->fx_frag == search_frag) | |
11407 | { | |
11408 | *fix_splice = next_fix; | |
11409 | fix->fx_next = frchain_to->fix_root; | |
11410 | frchain_to->fix_root = fix; | |
11411 | if (frchain_to->fix_tail == NULL) | |
11412 | frchain_to->fix_tail = fix; | |
11413 | } | |
11414 | else | |
11415 | fix_splice = &(fix->fx_next); | |
11416 | fix = next_fix; | |
11417 | } | |
11418 | search_frag = next_frag; | |
11419 | } | |
11420 | ||
11421 | if (frchain_from->fix_root != NULL) | |
11422 | { | |
11423 | frchain_from = seg_info (segment->seg)->frchainP; | |
11424 | as_warn (_("fixes not all moved from %s"), segment->seg->name); | |
11425 | ||
11426 | gas_assert (frchain_from->fix_root == NULL); | |
11427 | } | |
11428 | frchain_from->fix_tail = NULL; | |
11429 | xtensa_restore_emit_state (&state); | |
11430 | } | |
11431 | ||
11432 | /* Now fix up the SEGMENT value for all the literal symbols. */ | |
11433 | for (lit = literal_syms; lit; lit = lit->next) | |
11434 | { | |
11435 | symbolS *lit_sym = lit->sym; | |
11436 | segT dseg = symbol_get_frag (lit_sym)->tc_frag_data.lit_seg; | |
11437 | if (dseg) | |
11438 | S_SET_SEGMENT (lit_sym, dseg); | |
11439 | } | |
11440 | } | |
11441 | ||
11442 | ||
11443 | /* Walk over all the frags for segments in a list and mark them as | |
11444 | containing literals. As clunky as this is, we can't rely on frag_var | |
11445 | and frag_variant to get called in all situations. */ | |
11446 | ||
11447 | static void | |
11448 | mark_literal_frags (seg_list *segment) | |
11449 | { | |
11450 | frchainS *frchain_from; | |
11451 | fragS *search_frag; | |
11452 | ||
11453 | while (segment) | |
11454 | { | |
11455 | frchain_from = seg_info (segment->seg)->frchainP; | |
11456 | search_frag = frchain_from->frch_root; | |
11457 | while (search_frag) | |
11458 | { | |
11459 | search_frag->tc_frag_data.is_literal = true; | |
11460 | search_frag = search_frag->fr_next; | |
11461 | } | |
11462 | segment = segment->next; | |
11463 | } | |
11464 | } | |
11465 | ||
11466 | ||
11467 | static void | |
11468 | xtensa_reorder_seg_list (seg_list *head, segT after) | |
11469 | { | |
11470 | /* Move all of the sections in the section list to come | |
11471 | after "after" in the gnu segment list. */ | |
11472 | ||
11473 | head = head->next; | |
11474 | while (head) | |
11475 | { | |
11476 | segT literal_section = head->seg; | |
11477 | ||
11478 | /* Move the literal section after "after". */ | |
11479 | gas_assert (literal_section); | |
11480 | if (literal_section != after) | |
11481 | { | |
11482 | bfd_section_list_remove (stdoutput, literal_section); | |
11483 | bfd_section_list_insert_after (stdoutput, after, literal_section); | |
11484 | } | |
11485 | ||
11486 | head = head->next; | |
11487 | } | |
11488 | } | |
11489 | ||
11490 | ||
11491 | /* Push all the literal segments to the end of the gnu list. */ | |
11492 | ||
11493 | static void | |
11494 | xtensa_reorder_segments (void) | |
11495 | { | |
11496 | segT sec; | |
11497 | segT last_sec = 0; | |
11498 | int old_count = 0; | |
11499 | int new_count = 0; | |
11500 | ||
11501 | for (sec = stdoutput->sections; sec != NULL; sec = sec->next) | |
11502 | { | |
11503 | last_sec = sec; | |
11504 | old_count++; | |
11505 | } | |
11506 | ||
11507 | /* Now that we have the last section, push all the literal | |
11508 | sections to the end. */ | |
11509 | xtensa_reorder_seg_list (literal_head, last_sec); | |
11510 | ||
11511 | /* Now perform the final error check. */ | |
11512 | for (sec = stdoutput->sections; sec != NULL; sec = sec->next) | |
11513 | new_count++; | |
11514 | gas_assert (new_count == old_count); | |
11515 | } | |
11516 | ||
11517 | ||
11518 | /* Change the emit state (seg, subseg, and frag related stuff) to the | |
11519 | correct location. Return a emit_state which can be passed to | |
11520 | xtensa_restore_emit_state to return to current fragment. */ | |
11521 | ||
11522 | static void | |
11523 | xtensa_switch_to_literal_fragment (emit_state *result) | |
11524 | { | |
11525 | if (directive_state[directive_absolute_literals]) | |
11526 | { | |
11527 | segT lit4_seg = cache_literal_section (true); | |
11528 | xtensa_switch_section_emit_state (result, lit4_seg, 0); | |
11529 | } | |
11530 | else | |
11531 | xtensa_switch_to_non_abs_literal_fragment (result); | |
11532 | ||
11533 | /* Do a 4-byte align here. */ | |
11534 | frag_align (2, 0, 0); | |
11535 | record_alignment (now_seg, 2); | |
11536 | } | |
11537 | ||
11538 | ||
11539 | static void | |
11540 | xtensa_switch_to_non_abs_literal_fragment (emit_state *result) | |
11541 | { | |
11542 | fragS *pool_location = get_literal_pool_location (now_seg); | |
11543 | segT lit_seg; | |
11544 | bool is_init_fini = xtensa_is_init_fini (now_seg); | |
11545 | ||
11546 | if (pool_location == NULL | |
11547 | && !use_literal_section | |
11548 | && !is_init_fini) | |
11549 | { | |
11550 | if (!auto_litpools) | |
11551 | { | |
11552 | as_bad (_("literal pool location required for text-section-literals; specify with .literal_position")); | |
11553 | } | |
11554 | xtensa_maybe_create_literal_pool_frag (true, true); | |
11555 | pool_location = get_literal_pool_location (now_seg); | |
11556 | } | |
11557 | ||
11558 | lit_seg = cache_literal_section (false); | |
11559 | xtensa_switch_section_emit_state (result, lit_seg, 0); | |
11560 | ||
11561 | if (!use_literal_section | |
11562 | && !is_init_fini | |
11563 | && get_literal_pool_location (now_seg) != pool_location) | |
11564 | { | |
11565 | /* Close whatever frag is there. */ | |
11566 | frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
11567 | xtensa_set_frag_assembly_state (frag_now); | |
11568 | frag_now->tc_frag_data.literal_frag = pool_location; | |
11569 | frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
11570 | xtensa_set_frag_assembly_state (frag_now); | |
11571 | } | |
11572 | } | |
11573 | ||
11574 | ||
11575 | /* Call this function before emitting data into the literal section. | |
11576 | This is a helper function for xtensa_switch_to_literal_fragment. | |
11577 | This is similar to a .section new_now_seg subseg. */ | |
11578 | ||
11579 | static void | |
11580 | xtensa_switch_section_emit_state (emit_state *state, | |
11581 | segT new_now_seg, | |
11582 | subsegT new_now_subseg) | |
11583 | { | |
11584 | state->name = now_seg->name; | |
11585 | state->now_seg = now_seg; | |
11586 | state->now_subseg = now_subseg; | |
11587 | state->generating_literals = generating_literals; | |
11588 | generating_literals++; | |
11589 | subseg_set (new_now_seg, new_now_subseg); | |
11590 | } | |
11591 | ||
11592 | ||
11593 | /* Use to restore the emitting into the normal place. */ | |
11594 | ||
11595 | static void | |
11596 | xtensa_restore_emit_state (emit_state *state) | |
11597 | { | |
11598 | generating_literals = state->generating_literals; | |
11599 | subseg_set (state->now_seg, state->now_subseg); | |
11600 | } | |
11601 | ||
11602 | ||
11603 | /* Predicate function used to look up a section in a particular group. */ | |
11604 | ||
11605 | static bool | |
11606 | match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf) | |
11607 | { | |
11608 | const char *gname = inf; | |
11609 | const char *group_name = elf_group_name (sec); | |
11610 | ||
11611 | return (group_name == gname | |
11612 | || (group_name != NULL | |
11613 | && gname != NULL | |
11614 | && strcmp (group_name, gname) == 0)); | |
11615 | } | |
11616 | ||
11617 | ||
11618 | /* Get the literal section to be used for the current text section. | |
11619 | The result may be cached in the default_lit_sections structure. */ | |
11620 | ||
11621 | static segT | |
11622 | cache_literal_section (bool use_abs_literals) | |
11623 | { | |
11624 | const char *text_name, *group_name = 0; | |
11625 | const char *base_name, *suffix; | |
11626 | char *name; | |
11627 | segT *pcached; | |
11628 | segT seg, current_section; | |
11629 | int current_subsec; | |
11630 | bool linkonce = false; | |
11631 | ||
11632 | /* Save the current section/subsection. */ | |
11633 | current_section = now_seg; | |
11634 | current_subsec = now_subseg; | |
11635 | ||
11636 | /* Clear the cached values if they are no longer valid. */ | |
11637 | if (now_seg != default_lit_sections.current_text_seg) | |
11638 | { | |
11639 | default_lit_sections.current_text_seg = now_seg; | |
11640 | default_lit_sections.lit_seg = NULL; | |
11641 | default_lit_sections.lit4_seg = NULL; | |
11642 | } | |
11643 | ||
11644 | /* Check if the literal section is already cached. */ | |
11645 | if (use_abs_literals) | |
11646 | pcached = &default_lit_sections.lit4_seg; | |
11647 | else | |
11648 | pcached = &default_lit_sections.lit_seg; | |
11649 | ||
11650 | if (*pcached) | |
11651 | return *pcached; | |
11652 | ||
11653 | text_name = default_lit_sections.lit_prefix; | |
11654 | if (! text_name || ! *text_name) | |
11655 | { | |
11656 | text_name = segment_name (current_section); | |
11657 | group_name = elf_group_name (current_section); | |
11658 | linkonce = (current_section->flags & SEC_LINK_ONCE) != 0; | |
11659 | } | |
11660 | ||
11661 | base_name = use_abs_literals ? ".lit4" : ".literal"; | |
11662 | if (group_name) | |
11663 | { | |
11664 | name = concat (base_name, ".", group_name, (char *) NULL); | |
11665 | } | |
11666 | else if (strncmp (text_name, ".gnu.linkonce.", linkonce_len) == 0) | |
11667 | { | |
11668 | suffix = strchr (text_name + linkonce_len, '.'); | |
11669 | ||
11670 | name = concat (".gnu.linkonce", base_name, suffix ? suffix : "", | |
11671 | (char *) NULL); | |
11672 | linkonce = true; | |
11673 | } | |
11674 | else | |
11675 | { | |
11676 | /* If the section name begins or ends with ".text", then replace | |
11677 | that portion instead of appending an additional suffix. */ | |
11678 | size_t len = strlen (text_name); | |
11679 | if (len >= 5 | |
11680 | && (strcmp (text_name + len - 5, ".text") == 0 | |
11681 | || startswith (text_name, ".text"))) | |
11682 | len -= 5; | |
11683 | ||
11684 | name = XNEWVEC (char, len + strlen (base_name) + 1); | |
11685 | if (startswith (text_name, ".text")) | |
11686 | { | |
11687 | strcpy (name, base_name); | |
11688 | strcat (name, text_name + 5); | |
11689 | } | |
11690 | else | |
11691 | { | |
11692 | strcpy (name, text_name); | |
11693 | strcpy (name + len, base_name); | |
11694 | } | |
11695 | } | |
11696 | ||
11697 | /* Canonicalize section names to allow renaming literal sections. | |
11698 | The group name, if any, came from the current text section and | |
11699 | has already been canonicalized. */ | |
11700 | name = tc_canonicalize_symbol_name (name); | |
11701 | ||
11702 | seg = bfd_get_section_by_name_if (stdoutput, name, match_section_group, | |
11703 | (void *) group_name); | |
11704 | if (! seg) | |
11705 | { | |
11706 | flagword flags; | |
11707 | ||
11708 | seg = subseg_force_new (name, 0); | |
11709 | ||
11710 | if (! use_abs_literals) | |
11711 | { | |
11712 | /* Add the newly created literal segment to the list. */ | |
11713 | seg_list *n = XNEW (seg_list); | |
11714 | n->seg = seg; | |
11715 | n->next = literal_head->next; | |
11716 | literal_head->next = n; | |
11717 | } | |
11718 | ||
11719 | flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC | SEC_LOAD | |
11720 | | (linkonce ? (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD) : 0) | |
11721 | | (use_abs_literals ? SEC_DATA : SEC_CODE)); | |
11722 | ||
11723 | elf_group_name (seg) = group_name; | |
11724 | ||
11725 | bfd_set_section_flags (seg, flags); | |
11726 | bfd_set_section_alignment (seg, 2); | |
11727 | } | |
11728 | ||
11729 | *pcached = seg; | |
11730 | subseg_set (current_section, current_subsec); | |
11731 | return seg; | |
11732 | } | |
11733 | ||
11734 | \f | |
11735 | /* Property Tables Stuff. */ | |
11736 | ||
11737 | #define XTENSA_INSN_SEC_NAME ".xt.insn" | |
11738 | #define XTENSA_LIT_SEC_NAME ".xt.lit" | |
11739 | #define XTENSA_PROP_SEC_NAME ".xt.prop" | |
11740 | ||
11741 | typedef bool (*frag_predicate) (const fragS *); | |
11742 | typedef void (*frag_flags_fn) (const fragS *, frag_flags *); | |
11743 | ||
11744 | static bool get_frag_is_literal (const fragS *); | |
11745 | static void xtensa_create_property_segments | |
11746 | (frag_predicate, frag_predicate, const char *, xt_section_type); | |
11747 | static void xtensa_create_xproperty_segments | |
11748 | (frag_flags_fn, const char *, xt_section_type); | |
11749 | static bool exclude_section_from_property_tables (segT); | |
11750 | static bool section_has_property (segT, frag_predicate); | |
11751 | static bool section_has_xproperty (segT, frag_flags_fn); | |
11752 | static void add_xt_block_frags | |
11753 | (segT, xtensa_block_info **, frag_predicate, frag_predicate); | |
11754 | static bool xtensa_frag_flags_is_empty (const frag_flags *); | |
11755 | static void xtensa_frag_flags_init (frag_flags *); | |
11756 | static void get_frag_property_flags (const fragS *, frag_flags *); | |
11757 | static flagword frag_flags_to_number (const frag_flags *); | |
11758 | static void add_xt_prop_frags (segT, xtensa_block_info **, frag_flags_fn); | |
11759 | ||
11760 | /* Set up property tables after relaxation. */ | |
11761 | ||
11762 | void | |
11763 | xtensa_post_relax_hook (void) | |
11764 | { | |
11765 | xtensa_move_seg_list_to_beginning (literal_head); | |
11766 | ||
11767 | xtensa_find_unmarked_state_frags (); | |
11768 | xtensa_mark_frags_for_org (); | |
11769 | xtensa_mark_difference_of_two_symbols (); | |
11770 | ||
11771 | xtensa_create_property_segments (get_frag_is_literal, | |
11772 | NULL, | |
11773 | XTENSA_LIT_SEC_NAME, | |
11774 | xt_literal_sec); | |
11775 | xtensa_create_xproperty_segments (get_frag_property_flags, | |
11776 | XTENSA_PROP_SEC_NAME, | |
11777 | xt_prop_sec); | |
11778 | ||
11779 | if (warn_unaligned_branch_targets) | |
11780 | bfd_map_over_sections (stdoutput, xtensa_find_unaligned_branch_targets, 0); | |
11781 | bfd_map_over_sections (stdoutput, xtensa_find_unaligned_loops, 0); | |
11782 | } | |
11783 | ||
11784 | ||
11785 | /* This function is only meaningful after xtensa_move_literals. */ | |
11786 | ||
11787 | static bool | |
11788 | get_frag_is_literal (const fragS *fragP) | |
11789 | { | |
11790 | gas_assert (fragP != NULL); | |
11791 | return fragP->tc_frag_data.is_literal; | |
11792 | } | |
11793 | ||
11794 | ||
11795 | static void | |
11796 | xtensa_create_property_segments (frag_predicate property_function, | |
11797 | frag_predicate end_property_function, | |
11798 | const char *section_name_base, | |
11799 | xt_section_type sec_type) | |
11800 | { | |
11801 | segT *seclist; | |
11802 | ||
11803 | /* Walk over all of the current segments. | |
11804 | Walk over each fragment | |
11805 | For each non-empty fragment, | |
11806 | Build a property record (append where possible). */ | |
11807 | ||
11808 | for (seclist = &stdoutput->sections; | |
11809 | seclist && *seclist; | |
11810 | seclist = &(*seclist)->next) | |
11811 | { | |
11812 | segT sec = *seclist; | |
11813 | ||
11814 | if (exclude_section_from_property_tables (sec)) | |
11815 | continue; | |
11816 | ||
11817 | if (section_has_property (sec, property_function)) | |
11818 | { | |
11819 | segment_info_type *xt_seg_info; | |
11820 | xtensa_block_info **xt_blocks; | |
11821 | segT prop_sec = xtensa_make_property_section (sec, section_name_base); | |
11822 | ||
11823 | prop_sec->output_section = prop_sec; | |
11824 | subseg_set (prop_sec, 0); | |
11825 | xt_seg_info = seg_info (prop_sec); | |
11826 | xt_blocks = &xt_seg_info->tc_segment_info_data.blocks[sec_type]; | |
11827 | ||
11828 | /* Walk over all of the frchains here and add new sections. */ | |
11829 | add_xt_block_frags (sec, xt_blocks, property_function, | |
11830 | end_property_function); | |
11831 | } | |
11832 | } | |
11833 | ||
11834 | /* Now we fill them out.... */ | |
11835 | ||
11836 | for (seclist = &stdoutput->sections; | |
11837 | seclist && *seclist; | |
11838 | seclist = &(*seclist)->next) | |
11839 | { | |
11840 | segment_info_type *seginfo; | |
11841 | xtensa_block_info *block; | |
11842 | segT sec = *seclist; | |
11843 | ||
11844 | seginfo = seg_info (sec); | |
11845 | block = seginfo->tc_segment_info_data.blocks[sec_type]; | |
11846 | ||
11847 | if (block) | |
11848 | { | |
11849 | xtensa_block_info *cur_block; | |
11850 | int num_recs = 0; | |
11851 | bfd_size_type rec_size; | |
11852 | ||
11853 | for (cur_block = block; cur_block; cur_block = cur_block->next) | |
11854 | num_recs++; | |
11855 | ||
11856 | rec_size = num_recs * 8; | |
11857 | bfd_set_section_size (sec, rec_size); | |
11858 | ||
11859 | if (num_recs) | |
11860 | { | |
11861 | char *frag_data; | |
11862 | int i; | |
11863 | ||
11864 | subseg_set (sec, 0); | |
11865 | frag_data = frag_more (rec_size); | |
11866 | cur_block = block; | |
11867 | for (i = 0; i < num_recs; i++) | |
11868 | { | |
11869 | fixS *fix; | |
11870 | ||
11871 | /* Write the fixup. */ | |
11872 | gas_assert (cur_block); | |
11873 | fix = fix_new (frag_now, i * 8, 4, | |
11874 | section_symbol (cur_block->sec), | |
11875 | cur_block->offset, | |
11876 | false, BFD_RELOC_32); | |
11877 | fix->fx_file = "<internal>"; | |
11878 | fix->fx_line = 0; | |
11879 | ||
11880 | /* Write the length. */ | |
11881 | md_number_to_chars (&frag_data[4 + i * 8], | |
11882 | cur_block->size, 4); | |
11883 | cur_block = cur_block->next; | |
11884 | } | |
11885 | frag_wane (frag_now); | |
11886 | frag_new (0); | |
11887 | frag_wane (frag_now); | |
11888 | } | |
11889 | } | |
11890 | } | |
11891 | } | |
11892 | ||
11893 | ||
11894 | static void | |
11895 | xtensa_create_xproperty_segments (frag_flags_fn flag_fn, | |
11896 | const char *section_name_base, | |
11897 | xt_section_type sec_type) | |
11898 | { | |
11899 | segT *seclist; | |
11900 | ||
11901 | /* Walk over all of the current segments. | |
11902 | Walk over each fragment. | |
11903 | For each fragment that has instructions, | |
11904 | build an instruction record (append where possible). */ | |
11905 | ||
11906 | for (seclist = &stdoutput->sections; | |
11907 | seclist && *seclist; | |
11908 | seclist = &(*seclist)->next) | |
11909 | { | |
11910 | segT sec = *seclist; | |
11911 | ||
11912 | if (exclude_section_from_property_tables (sec)) | |
11913 | continue; | |
11914 | ||
11915 | if (section_has_xproperty (sec, flag_fn)) | |
11916 | { | |
11917 | segment_info_type *xt_seg_info; | |
11918 | xtensa_block_info **xt_blocks; | |
11919 | segT prop_sec = xtensa_make_property_section (sec, section_name_base); | |
11920 | ||
11921 | prop_sec->output_section = prop_sec; | |
11922 | subseg_set (prop_sec, 0); | |
11923 | xt_seg_info = seg_info (prop_sec); | |
11924 | xt_blocks = &xt_seg_info->tc_segment_info_data.blocks[sec_type]; | |
11925 | ||
11926 | /* Walk over all of the frchains here and add new sections. */ | |
11927 | add_xt_prop_frags (sec, xt_blocks, flag_fn); | |
11928 | } | |
11929 | } | |
11930 | ||
11931 | /* Now we fill them out.... */ | |
11932 | ||
11933 | for (seclist = &stdoutput->sections; | |
11934 | seclist && *seclist; | |
11935 | seclist = &(*seclist)->next) | |
11936 | { | |
11937 | segment_info_type *seginfo; | |
11938 | xtensa_block_info *block; | |
11939 | segT sec = *seclist; | |
11940 | ||
11941 | seginfo = seg_info (sec); | |
11942 | block = seginfo->tc_segment_info_data.blocks[sec_type]; | |
11943 | ||
11944 | if (block) | |
11945 | { | |
11946 | xtensa_block_info *cur_block; | |
11947 | int num_recs = 0; | |
11948 | bfd_size_type rec_size; | |
11949 | ||
11950 | for (cur_block = block; cur_block; cur_block = cur_block->next) | |
11951 | num_recs++; | |
11952 | ||
11953 | rec_size = num_recs * (8 + 4); | |
11954 | bfd_set_section_size (sec, rec_size); | |
11955 | /* elf_section_data (sec)->this_hdr.sh_entsize = 12; */ | |
11956 | ||
11957 | if (num_recs) | |
11958 | { | |
11959 | char *frag_data; | |
11960 | int i; | |
11961 | ||
11962 | subseg_set (sec, 0); | |
11963 | frag_data = frag_more (rec_size); | |
11964 | cur_block = block; | |
11965 | for (i = 0; i < num_recs; i++) | |
11966 | { | |
11967 | fixS *fix; | |
11968 | ||
11969 | /* Write the fixup. */ | |
11970 | gas_assert (cur_block); | |
11971 | fix = fix_new (frag_now, i * 12, 4, | |
11972 | section_symbol (cur_block->sec), | |
11973 | cur_block->offset, | |
11974 | false, BFD_RELOC_32); | |
11975 | fix->fx_file = "<internal>"; | |
11976 | fix->fx_line = 0; | |
11977 | ||
11978 | /* Write the length. */ | |
11979 | md_number_to_chars (&frag_data[4 + i * 12], | |
11980 | cur_block->size, 4); | |
11981 | md_number_to_chars (&frag_data[8 + i * 12], | |
11982 | frag_flags_to_number (&cur_block->flags), | |
11983 | sizeof (flagword)); | |
11984 | cur_block = cur_block->next; | |
11985 | } | |
11986 | frag_wane (frag_now); | |
11987 | frag_new (0); | |
11988 | frag_wane (frag_now); | |
11989 | } | |
11990 | } | |
11991 | } | |
11992 | } | |
11993 | ||
11994 | ||
11995 | static bool | |
11996 | exclude_section_from_property_tables (segT sec) | |
11997 | { | |
11998 | flagword flags = bfd_section_flags (sec); | |
11999 | ||
12000 | /* Sections that don't contribute to the memory footprint are excluded. */ | |
12001 | if ((flags & SEC_DEBUGGING) | |
12002 | || !(flags & SEC_ALLOC) | |
12003 | || (flags & SEC_MERGE)) | |
12004 | return true; | |
12005 | ||
12006 | /* Linker cie and fde optimizations mess up property entries for | |
12007 | eh_frame sections, but there is nothing inside them relevant to | |
12008 | property tables anyway. */ | |
12009 | if (strcmp (sec->name, ".eh_frame") == 0) | |
12010 | return true; | |
12011 | ||
12012 | return false; | |
12013 | } | |
12014 | ||
12015 | ||
12016 | static bool | |
12017 | section_has_property (segT sec, frag_predicate property_function) | |
12018 | { | |
12019 | segment_info_type *seginfo = seg_info (sec); | |
12020 | fragS *fragP; | |
12021 | ||
12022 | if (seginfo && seginfo->frchainP) | |
12023 | { | |
12024 | for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next) | |
12025 | { | |
12026 | if (property_function (fragP) | |
12027 | && (fragP->fr_type != rs_fill || fragP->fr_fix != 0)) | |
12028 | return true; | |
12029 | } | |
12030 | } | |
12031 | return false; | |
12032 | } | |
12033 | ||
12034 | ||
12035 | static bool | |
12036 | section_has_xproperty (segT sec, frag_flags_fn property_function) | |
12037 | { | |
12038 | segment_info_type *seginfo = seg_info (sec); | |
12039 | fragS *fragP; | |
12040 | ||
12041 | if (seginfo && seginfo->frchainP) | |
12042 | { | |
12043 | for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next) | |
12044 | { | |
12045 | frag_flags prop_flags; | |
12046 | property_function (fragP, &prop_flags); | |
12047 | if (!xtensa_frag_flags_is_empty (&prop_flags)) | |
12048 | return true; | |
12049 | } | |
12050 | } | |
12051 | return false; | |
12052 | } | |
12053 | ||
12054 | ||
12055 | /* Two types of block sections exist right now: literal and insns. */ | |
12056 | ||
12057 | static void | |
12058 | add_xt_block_frags (segT sec, | |
12059 | xtensa_block_info **xt_block, | |
12060 | frag_predicate property_function, | |
12061 | frag_predicate end_property_function) | |
12062 | { | |
12063 | fragS *fragP; | |
12064 | ||
12065 | /* Build it if needed. */ | |
12066 | while (*xt_block != NULL) | |
12067 | xt_block = &(*xt_block)->next; | |
12068 | /* We are either at NULL at the beginning or at the end. */ | |
12069 | ||
12070 | /* Walk through the frags. */ | |
12071 | if (seg_info (sec)->frchainP) | |
12072 | { | |
12073 | for (fragP = seg_info (sec)->frchainP->frch_root; | |
12074 | fragP; | |
12075 | fragP = fragP->fr_next) | |
12076 | { | |
12077 | if (property_function (fragP) | |
12078 | && (fragP->fr_type != rs_fill || fragP->fr_fix != 0)) | |
12079 | { | |
12080 | if (*xt_block != NULL) | |
12081 | { | |
12082 | if ((*xt_block)->offset + (*xt_block)->size | |
12083 | == fragP->fr_address) | |
12084 | (*xt_block)->size += fragP->fr_fix; | |
12085 | else | |
12086 | xt_block = &((*xt_block)->next); | |
12087 | } | |
12088 | if (*xt_block == NULL) | |
12089 | { | |
12090 | xtensa_block_info *new_block = XNEW (xtensa_block_info); | |
12091 | new_block->sec = sec; | |
12092 | new_block->offset = fragP->fr_address; | |
12093 | new_block->size = fragP->fr_fix; | |
12094 | new_block->next = NULL; | |
12095 | xtensa_frag_flags_init (&new_block->flags); | |
12096 | *xt_block = new_block; | |
12097 | } | |
12098 | if (end_property_function | |
12099 | && end_property_function (fragP)) | |
12100 | { | |
12101 | xt_block = &((*xt_block)->next); | |
12102 | } | |
12103 | } | |
12104 | } | |
12105 | } | |
12106 | } | |
12107 | ||
12108 | ||
12109 | /* Break the encapsulation of add_xt_prop_frags here. */ | |
12110 | ||
12111 | static bool | |
12112 | xtensa_frag_flags_is_empty (const frag_flags *prop_flags) | |
12113 | { | |
12114 | if (prop_flags->is_literal | |
12115 | || prop_flags->is_insn | |
12116 | || prop_flags->is_data | |
12117 | || prop_flags->is_unreachable) | |
12118 | return false; | |
12119 | return true; | |
12120 | } | |
12121 | ||
12122 | ||
12123 | static void | |
12124 | xtensa_frag_flags_init (frag_flags *prop_flags) | |
12125 | { | |
12126 | memset (prop_flags, 0, sizeof (frag_flags)); | |
12127 | } | |
12128 | ||
12129 | ||
12130 | static void | |
12131 | get_frag_property_flags (const fragS *fragP, frag_flags *prop_flags) | |
12132 | { | |
12133 | xtensa_frag_flags_init (prop_flags); | |
12134 | if (fragP->tc_frag_data.is_literal) | |
12135 | prop_flags->is_literal = true; | |
12136 | if (fragP->tc_frag_data.is_specific_opcode | |
12137 | || fragP->tc_frag_data.is_no_transform) | |
12138 | { | |
12139 | prop_flags->is_no_transform = true; | |
12140 | if (xtensa_frag_flags_is_empty (prop_flags)) | |
12141 | prop_flags->is_data = true; | |
12142 | } | |
12143 | if (fragP->tc_frag_data.is_unreachable) | |
12144 | prop_flags->is_unreachable = true; | |
12145 | else if (fragP->tc_frag_data.is_insn) | |
12146 | { | |
12147 | prop_flags->is_insn = true; | |
12148 | if (fragP->tc_frag_data.is_loop_target) | |
12149 | prop_flags->insn.is_loop_target = true; | |
12150 | if (fragP->tc_frag_data.is_branch_target) | |
12151 | prop_flags->insn.is_branch_target = true; | |
12152 | if (fragP->tc_frag_data.is_no_density) | |
12153 | prop_flags->insn.is_no_density = true; | |
12154 | if (fragP->tc_frag_data.use_absolute_literals) | |
12155 | prop_flags->insn.is_abslit = true; | |
12156 | } | |
12157 | if (fragP->tc_frag_data.is_align) | |
12158 | { | |
12159 | prop_flags->is_align = true; | |
12160 | prop_flags->alignment = fragP->tc_frag_data.alignment; | |
12161 | if (xtensa_frag_flags_is_empty (prop_flags)) | |
12162 | prop_flags->is_data = true; | |
12163 | } | |
12164 | } | |
12165 | ||
12166 | ||
12167 | static flagword | |
12168 | frag_flags_to_number (const frag_flags *prop_flags) | |
12169 | { | |
12170 | flagword num = 0; | |
12171 | if (prop_flags->is_literal) | |
12172 | num |= XTENSA_PROP_LITERAL; | |
12173 | if (prop_flags->is_insn) | |
12174 | num |= XTENSA_PROP_INSN; | |
12175 | if (prop_flags->is_data) | |
12176 | num |= XTENSA_PROP_DATA; | |
12177 | if (prop_flags->is_unreachable) | |
12178 | num |= XTENSA_PROP_UNREACHABLE; | |
12179 | if (prop_flags->insn.is_loop_target) | |
12180 | num |= XTENSA_PROP_INSN_LOOP_TARGET; | |
12181 | if (prop_flags->insn.is_branch_target) | |
12182 | { | |
12183 | num |= XTENSA_PROP_INSN_BRANCH_TARGET; | |
12184 | num = SET_XTENSA_PROP_BT_ALIGN (num, prop_flags->insn.bt_align_priority); | |
12185 | } | |
12186 | ||
12187 | if (prop_flags->insn.is_no_density) | |
12188 | num |= XTENSA_PROP_INSN_NO_DENSITY; | |
12189 | if (prop_flags->is_no_transform) | |
12190 | num |= XTENSA_PROP_NO_TRANSFORM; | |
12191 | if (prop_flags->insn.is_no_reorder) | |
12192 | num |= XTENSA_PROP_INSN_NO_REORDER; | |
12193 | if (prop_flags->insn.is_abslit) | |
12194 | num |= XTENSA_PROP_INSN_ABSLIT; | |
12195 | ||
12196 | if (prop_flags->is_align) | |
12197 | { | |
12198 | num |= XTENSA_PROP_ALIGN; | |
12199 | num = SET_XTENSA_PROP_ALIGNMENT (num, prop_flags->alignment); | |
12200 | } | |
12201 | ||
12202 | return num; | |
12203 | } | |
12204 | ||
12205 | ||
12206 | static bool | |
12207 | xtensa_frag_flags_combinable (const frag_flags *prop_flags_1, | |
12208 | const frag_flags *prop_flags_2) | |
12209 | { | |
12210 | /* Cannot combine with an end marker. */ | |
12211 | ||
12212 | if (prop_flags_1->is_literal != prop_flags_2->is_literal) | |
12213 | return false; | |
12214 | if (prop_flags_1->is_insn != prop_flags_2->is_insn) | |
12215 | return false; | |
12216 | if (prop_flags_1->is_data != prop_flags_2->is_data) | |
12217 | return false; | |
12218 | ||
12219 | if (prop_flags_1->is_insn) | |
12220 | { | |
12221 | /* Properties of the beginning of the frag. */ | |
12222 | if (prop_flags_2->insn.is_loop_target) | |
12223 | return false; | |
12224 | if (prop_flags_2->insn.is_branch_target) | |
12225 | return false; | |
12226 | if (prop_flags_1->insn.is_no_density != | |
12227 | prop_flags_2->insn.is_no_density) | |
12228 | return false; | |
12229 | if (prop_flags_1->is_no_transform != | |
12230 | prop_flags_2->is_no_transform) | |
12231 | return false; | |
12232 | if (prop_flags_1->insn.is_no_reorder != | |
12233 | prop_flags_2->insn.is_no_reorder) | |
12234 | return false; | |
12235 | if (prop_flags_1->insn.is_abslit != | |
12236 | prop_flags_2->insn.is_abslit) | |
12237 | return false; | |
12238 | } | |
12239 | ||
12240 | if (prop_flags_1->is_align) | |
12241 | return false; | |
12242 | ||
12243 | return true; | |
12244 | } | |
12245 | ||
12246 | ||
12247 | static bfd_vma | |
12248 | xt_block_aligned_size (const xtensa_block_info *xt_block) | |
12249 | { | |
12250 | bfd_vma end_addr; | |
12251 | unsigned align_bits; | |
12252 | ||
12253 | if (!xt_block->flags.is_align) | |
12254 | return xt_block->size; | |
12255 | ||
12256 | end_addr = xt_block->offset + xt_block->size; | |
12257 | align_bits = xt_block->flags.alignment; | |
12258 | end_addr = ((end_addr + ((1 << align_bits) -1)) >> align_bits) << align_bits; | |
12259 | return end_addr - xt_block->offset; | |
12260 | } | |
12261 | ||
12262 | ||
12263 | static bool | |
12264 | xtensa_xt_block_combine (xtensa_block_info *xt_block, | |
12265 | const xtensa_block_info *xt_block_2) | |
12266 | { | |
12267 | if (xt_block->sec != xt_block_2->sec) | |
12268 | return false; | |
12269 | if (xt_block->offset + xt_block_aligned_size (xt_block) | |
12270 | != xt_block_2->offset) | |
12271 | return false; | |
12272 | ||
12273 | if (xt_block_2->size == 0 | |
12274 | && (!xt_block_2->flags.is_unreachable | |
12275 | || xt_block->flags.is_unreachable)) | |
12276 | { | |
12277 | if (xt_block_2->flags.is_align | |
12278 | && xt_block->flags.is_align) | |
12279 | { | |
12280 | /* Nothing needed. */ | |
12281 | if (xt_block->flags.alignment >= xt_block_2->flags.alignment) | |
12282 | return true; | |
12283 | } | |
12284 | else | |
12285 | { | |
12286 | if (xt_block_2->flags.is_align) | |
12287 | { | |
12288 | /* Push alignment to previous entry. */ | |
12289 | xt_block->flags.is_align = xt_block_2->flags.is_align; | |
12290 | xt_block->flags.alignment = xt_block_2->flags.alignment; | |
12291 | } | |
12292 | return true; | |
12293 | } | |
12294 | } | |
12295 | if (!xtensa_frag_flags_combinable (&xt_block->flags, | |
12296 | &xt_block_2->flags)) | |
12297 | return false; | |
12298 | ||
12299 | xt_block->size += xt_block_2->size; | |
12300 | ||
12301 | if (xt_block_2->flags.is_align) | |
12302 | { | |
12303 | xt_block->flags.is_align = true; | |
12304 | xt_block->flags.alignment = xt_block_2->flags.alignment; | |
12305 | } | |
12306 | ||
12307 | return true; | |
12308 | } | |
12309 | ||
12310 | ||
12311 | static void | |
12312 | add_xt_prop_frags (segT sec, | |
12313 | xtensa_block_info **xt_block, | |
12314 | frag_flags_fn property_function) | |
12315 | { | |
12316 | fragS *fragP; | |
12317 | ||
12318 | /* Build it if needed. */ | |
12319 | while (*xt_block != NULL) | |
12320 | { | |
12321 | xt_block = &(*xt_block)->next; | |
12322 | } | |
12323 | /* We are either at NULL at the beginning or at the end. */ | |
12324 | ||
12325 | /* Walk through the frags. */ | |
12326 | if (seg_info (sec)->frchainP) | |
12327 | { | |
12328 | for (fragP = seg_info (sec)->frchainP->frch_root; fragP; | |
12329 | fragP = fragP->fr_next) | |
12330 | { | |
12331 | xtensa_block_info tmp_block; | |
12332 | tmp_block.sec = sec; | |
12333 | tmp_block.offset = fragP->fr_address; | |
12334 | tmp_block.size = fragP->fr_fix; | |
12335 | tmp_block.next = NULL; | |
12336 | property_function (fragP, &tmp_block.flags); | |
12337 | ||
12338 | if (!xtensa_frag_flags_is_empty (&tmp_block.flags)) | |
12339 | /* && fragP->fr_fix != 0) */ | |
12340 | { | |
12341 | if ((*xt_block) == NULL | |
12342 | || !xtensa_xt_block_combine (*xt_block, &tmp_block)) | |
12343 | { | |
12344 | xtensa_block_info *new_block; | |
12345 | if ((*xt_block) != NULL) | |
12346 | xt_block = &(*xt_block)->next; | |
12347 | new_block = XNEW (xtensa_block_info); | |
12348 | *new_block = tmp_block; | |
12349 | *xt_block = new_block; | |
12350 | } | |
12351 | } | |
12352 | } | |
12353 | } | |
12354 | } | |
12355 | ||
12356 | \f | |
12357 | /* op_placement_info_table */ | |
12358 | ||
12359 | /* op_placement_info makes it easier to determine which | |
12360 | ops can go in which slots. */ | |
12361 | ||
12362 | static void | |
12363 | init_op_placement_info_table (void) | |
12364 | { | |
12365 | xtensa_isa isa = xtensa_default_isa; | |
12366 | xtensa_insnbuf ibuf = xtensa_insnbuf_alloc (isa); | |
12367 | xtensa_opcode opcode; | |
12368 | xtensa_format fmt; | |
12369 | int slot; | |
12370 | int num_opcodes = xtensa_isa_num_opcodes (isa); | |
12371 | ||
12372 | op_placement_table = XNEWVEC (op_placement_info, num_opcodes); | |
12373 | gas_assert (xtensa_isa_num_formats (isa) < MAX_FORMATS); | |
12374 | ||
12375 | for (opcode = 0; opcode < num_opcodes; opcode++) | |
12376 | { | |
12377 | op_placement_info *opi = &op_placement_table[opcode]; | |
12378 | /* FIXME: Make tinsn allocation dynamic. */ | |
12379 | if (xtensa_opcode_num_operands (isa, opcode) > MAX_INSN_ARGS) | |
12380 | as_fatal (_("too many operands in instruction")); | |
12381 | opi->narrowest = XTENSA_UNDEFINED; | |
12382 | opi->narrowest_size = 0x7F; | |
12383 | opi->narrowest_slot = 0; | |
12384 | opi->formats = 0; | |
12385 | opi->num_formats = 0; | |
12386 | opi->issuef = 0; | |
12387 | for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++) | |
12388 | { | |
12389 | opi->slots[fmt] = 0; | |
12390 | for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++) | |
12391 | { | |
12392 | if (xtensa_opcode_encode (isa, fmt, slot, ibuf, opcode) == 0) | |
12393 | { | |
12394 | int fmt_length = xtensa_format_length (isa, fmt); | |
12395 | opi->issuef++; | |
12396 | set_bit (fmt, opi->formats); | |
12397 | set_bit (slot, opi->slots[fmt]); | |
12398 | if (fmt_length < opi->narrowest_size | |
12399 | || (fmt_length == opi->narrowest_size | |
12400 | && (xtensa_format_num_slots (isa, fmt) | |
12401 | < xtensa_format_num_slots (isa, | |
12402 | opi->narrowest)))) | |
12403 | { | |
12404 | opi->narrowest = fmt; | |
12405 | opi->narrowest_size = fmt_length; | |
12406 | opi->narrowest_slot = slot; | |
12407 | } | |
12408 | } | |
12409 | } | |
12410 | if (opi->formats) | |
12411 | opi->num_formats++; | |
12412 | } | |
12413 | } | |
12414 | xtensa_insnbuf_free (isa, ibuf); | |
12415 | } | |
12416 | ||
12417 | ||
12418 | bool | |
12419 | opcode_fits_format_slot (xtensa_opcode opcode, xtensa_format fmt, int slot) | |
12420 | { | |
12421 | return bit_is_set (slot, op_placement_table[opcode].slots[fmt]); | |
12422 | } | |
12423 | ||
12424 | ||
12425 | /* If the opcode is available in a single slot format, return its size. */ | |
12426 | ||
12427 | static int | |
12428 | xg_get_single_size (xtensa_opcode opcode) | |
12429 | { | |
12430 | return op_placement_table[opcode].narrowest_size; | |
12431 | } | |
12432 | ||
12433 | ||
12434 | static xtensa_format | |
12435 | xg_get_single_format (xtensa_opcode opcode) | |
12436 | { | |
12437 | return op_placement_table[opcode].narrowest; | |
12438 | } | |
12439 | ||
12440 | ||
12441 | static int | |
12442 | xg_get_single_slot (xtensa_opcode opcode) | |
12443 | { | |
12444 | return op_placement_table[opcode].narrowest_slot; | |
12445 | } | |
12446 | ||
12447 | \f | |
12448 | /* Instruction Stack Functions (from "xtensa-istack.h"). */ | |
12449 | ||
12450 | void | |
12451 | istack_init (IStack *stack) | |
12452 | { | |
12453 | stack->ninsn = 0; | |
12454 | } | |
12455 | ||
12456 | ||
12457 | bool | |
12458 | istack_empty (IStack *stack) | |
12459 | { | |
12460 | return (stack->ninsn == 0); | |
12461 | } | |
12462 | ||
12463 | ||
12464 | bool | |
12465 | istack_full (IStack *stack) | |
12466 | { | |
12467 | return (stack->ninsn == MAX_ISTACK); | |
12468 | } | |
12469 | ||
12470 | ||
12471 | /* Return a pointer to the top IStack entry. | |
12472 | It is an error to call this if istack_empty () is TRUE. */ | |
12473 | ||
12474 | TInsn * | |
12475 | istack_top (IStack *stack) | |
12476 | { | |
12477 | int rec = stack->ninsn - 1; | |
12478 | gas_assert (!istack_empty (stack)); | |
12479 | return &stack->insn[rec]; | |
12480 | } | |
12481 | ||
12482 | ||
12483 | /* Add a new TInsn to an IStack. | |
12484 | It is an error to call this if istack_full () is TRUE. */ | |
12485 | ||
12486 | void | |
12487 | istack_push (IStack *stack, TInsn *insn) | |
12488 | { | |
12489 | int rec = stack->ninsn; | |
12490 | gas_assert (!istack_full (stack)); | |
12491 | stack->insn[rec] = *insn; | |
12492 | stack->ninsn++; | |
12493 | } | |
12494 | ||
12495 | ||
12496 | /* Clear space for the next TInsn on the IStack and return a pointer | |
12497 | to it. It is an error to call this if istack_full () is TRUE. */ | |
12498 | ||
12499 | TInsn * | |
12500 | istack_push_space (IStack *stack) | |
12501 | { | |
12502 | int rec = stack->ninsn; | |
12503 | TInsn *insn; | |
12504 | gas_assert (!istack_full (stack)); | |
12505 | insn = &stack->insn[rec]; | |
12506 | tinsn_init (insn); | |
12507 | stack->ninsn++; | |
12508 | return insn; | |
12509 | } | |
12510 | ||
12511 | ||
12512 | /* Remove the last pushed instruction. It is an error to call this if | |
12513 | istack_empty () returns TRUE. */ | |
12514 | ||
12515 | void | |
12516 | istack_pop (IStack *stack) | |
12517 | { | |
12518 | int rec = stack->ninsn - 1; | |
12519 | gas_assert (!istack_empty (stack)); | |
12520 | stack->ninsn--; | |
12521 | tinsn_init (&stack->insn[rec]); | |
12522 | } | |
12523 | ||
12524 | \f | |
12525 | /* TInsn functions. */ | |
12526 | ||
12527 | void | |
12528 | tinsn_init (TInsn *dst) | |
12529 | { | |
12530 | memset (dst, 0, sizeof (TInsn)); | |
12531 | } | |
12532 | ||
12533 | ||
12534 | /* Return TRUE if ANY of the operands in the insn are symbolic. */ | |
12535 | ||
12536 | static bool | |
12537 | tinsn_has_symbolic_operands (const TInsn *insn) | |
12538 | { | |
12539 | int i; | |
12540 | int n = insn->ntok; | |
12541 | ||
12542 | gas_assert (insn->insn_type == ITYPE_INSN); | |
12543 | ||
12544 | for (i = 0; i < n; ++i) | |
12545 | { | |
12546 | switch (insn->tok[i].X_op) | |
12547 | { | |
12548 | case O_register: | |
12549 | case O_constant: | |
12550 | break; | |
12551 | default: | |
12552 | return true; | |
12553 | } | |
12554 | } | |
12555 | return false; | |
12556 | } | |
12557 | ||
12558 | ||
12559 | bool | |
12560 | tinsn_has_invalid_symbolic_operands (const TInsn *insn) | |
12561 | { | |
12562 | xtensa_isa isa = xtensa_default_isa; | |
12563 | int i; | |
12564 | int n = insn->ntok; | |
12565 | ||
12566 | gas_assert (insn->insn_type == ITYPE_INSN); | |
12567 | ||
12568 | for (i = 0; i < n; ++i) | |
12569 | { | |
12570 | switch (insn->tok[i].X_op) | |
12571 | { | |
12572 | case O_register: | |
12573 | case O_constant: | |
12574 | break; | |
12575 | case O_big: | |
12576 | case O_illegal: | |
12577 | case O_absent: | |
12578 | /* Errors for these types are caught later. */ | |
12579 | break; | |
12580 | case O_hi16: | |
12581 | case O_lo16: | |
12582 | default: | |
12583 | /* Symbolic immediates are only allowed on the last immediate | |
12584 | operand. At this time, CONST16 is the only opcode where we | |
12585 | support non-PC-relative relocations. */ | |
12586 | if (i != get_relaxable_immed (insn->opcode) | |
12587 | || (xtensa_operand_is_PCrelative (isa, insn->opcode, i) != 1 | |
12588 | && insn->opcode != xtensa_const16_opcode)) | |
12589 | { | |
12590 | as_bad (_("invalid symbolic operand")); | |
12591 | return true; | |
12592 | } | |
12593 | } | |
12594 | } | |
12595 | return false; | |
12596 | } | |
12597 | ||
12598 | ||
12599 | /* For assembly code with complex expressions (e.g. subtraction), | |
12600 | we have to build them in the literal pool so that | |
12601 | their results are calculated correctly after relaxation. | |
12602 | The relaxation only handles expressions that | |
12603 | boil down to SYMBOL + OFFSET. */ | |
12604 | ||
12605 | static bool | |
12606 | tinsn_has_complex_operands (const TInsn *insn) | |
12607 | { | |
12608 | int i; | |
12609 | int n = insn->ntok; | |
12610 | gas_assert (insn->insn_type == ITYPE_INSN); | |
12611 | for (i = 0; i < n; ++i) | |
12612 | { | |
12613 | switch (insn->tok[i].X_op) | |
12614 | { | |
12615 | case O_register: | |
12616 | case O_constant: | |
12617 | case O_symbol: | |
12618 | case O_lo16: | |
12619 | case O_hi16: | |
12620 | break; | |
12621 | default: | |
12622 | return true; | |
12623 | } | |
12624 | } | |
12625 | return false; | |
12626 | } | |
12627 | ||
12628 | ||
12629 | /* Encode a TInsn opcode and its constant operands into slotbuf. | |
12630 | Return TRUE if there is a symbol in the immediate field. This | |
12631 | function assumes that: | |
12632 | 1) The number of operands are correct. | |
12633 | 2) The insn_type is ITYPE_INSN. | |
12634 | 3) The opcode can be encoded in the specified format and slot. | |
12635 | 4) Operands are either O_constant or O_symbol, and all constants fit. */ | |
12636 | ||
12637 | static bool | |
12638 | tinsn_to_slotbuf (xtensa_format fmt, | |
12639 | int slot, | |
12640 | TInsn *tinsn, | |
12641 | xtensa_insnbuf slotbuf) | |
12642 | { | |
12643 | xtensa_isa isa = xtensa_default_isa; | |
12644 | xtensa_opcode opcode = tinsn->opcode; | |
12645 | bool has_fixup = false; | |
12646 | int noperands = xtensa_opcode_num_operands (isa, opcode); | |
12647 | int i; | |
12648 | ||
12649 | gas_assert (tinsn->insn_type == ITYPE_INSN); | |
12650 | if (noperands != tinsn->ntok) | |
12651 | as_fatal (_("operand number mismatch")); | |
12652 | ||
12653 | if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opcode)) | |
12654 | { | |
12655 | as_bad (_("cannot encode opcode \"%s\" in the given format \"%s\""), | |
12656 | xtensa_opcode_name (isa, opcode), xtensa_format_name (isa, fmt)); | |
12657 | return false; | |
12658 | } | |
12659 | ||
12660 | for (i = 0; i < noperands; i++) | |
12661 | { | |
12662 | expressionS *exp = &tinsn->tok[i]; | |
12663 | int rc; | |
12664 | unsigned line; | |
12665 | const char *file_name; | |
12666 | uint32 opnd_value; | |
12667 | ||
12668 | switch (exp->X_op) | |
12669 | { | |
12670 | case O_register: | |
12671 | if (xtensa_operand_is_visible (isa, opcode, i) == 0) | |
12672 | break; | |
12673 | /* The register number has already been checked in | |
12674 | expression_maybe_register, so we don't need to check here. */ | |
12675 | opnd_value = exp->X_add_number; | |
12676 | (void) xtensa_operand_encode (isa, opcode, i, &opnd_value); | |
12677 | rc = xtensa_operand_set_field (isa, opcode, i, fmt, slot, slotbuf, | |
12678 | opnd_value); | |
12679 | if (rc != 0) | |
12680 | as_warn (_("xtensa-isa failure: %s"), xtensa_isa_error_msg (isa)); | |
12681 | break; | |
12682 | ||
12683 | case O_constant: | |
12684 | if (xtensa_operand_is_visible (isa, opcode, i) == 0) | |
12685 | break; | |
12686 | file_name = as_where (&line); | |
12687 | /* It is a constant and we called this function | |
12688 | then we have to try to fit it. */ | |
12689 | xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode, i, | |
12690 | exp->X_add_number, file_name, line); | |
12691 | break; | |
12692 | ||
12693 | default: | |
12694 | has_fixup = true; | |
12695 | break; | |
12696 | } | |
12697 | } | |
12698 | ||
12699 | return has_fixup; | |
12700 | } | |
12701 | ||
12702 | ||
12703 | /* Encode a single TInsn into an insnbuf. If the opcode can only be encoded | |
12704 | into a multi-slot instruction, fill the other slots with NOPs. | |
12705 | Return TRUE if there is a symbol in the immediate field. See also the | |
12706 | assumptions listed for tinsn_to_slotbuf. */ | |
12707 | ||
12708 | static bool | |
12709 | tinsn_to_insnbuf (TInsn *tinsn, xtensa_insnbuf insnbuf) | |
12710 | { | |
12711 | static xtensa_insnbuf slotbuf = 0; | |
12712 | static vliw_insn vinsn; | |
12713 | xtensa_isa isa = xtensa_default_isa; | |
12714 | bool has_fixup = false; | |
12715 | int i; | |
12716 | ||
12717 | if (!slotbuf) | |
12718 | { | |
12719 | slotbuf = xtensa_insnbuf_alloc (isa); | |
12720 | xg_init_vinsn (&vinsn); | |
12721 | } | |
12722 | ||
12723 | xg_clear_vinsn (&vinsn); | |
12724 | ||
12725 | bundle_tinsn (tinsn, &vinsn); | |
12726 | ||
12727 | xtensa_format_encode (isa, vinsn.format, insnbuf); | |
12728 | ||
12729 | for (i = 0; i < vinsn.num_slots; i++) | |
12730 | { | |
12731 | /* Only one slot may have a fix-up because the rest contains NOPs. */ | |
12732 | has_fixup |= | |
12733 | tinsn_to_slotbuf (vinsn.format, i, &vinsn.slots[i], vinsn.slotbuf[i]); | |
12734 | xtensa_format_set_slot (isa, vinsn.format, i, insnbuf, vinsn.slotbuf[i]); | |
12735 | } | |
12736 | ||
12737 | return has_fixup; | |
12738 | } | |
12739 | ||
12740 | ||
12741 | /* Check the instruction arguments. Return TRUE on failure. */ | |
12742 | ||
12743 | static bool | |
12744 | tinsn_check_arguments (const TInsn *insn) | |
12745 | { | |
12746 | xtensa_isa isa = xtensa_default_isa; | |
12747 | xtensa_opcode opcode = insn->opcode; | |
12748 | xtensa_regfile t1_regfile, t2_regfile; | |
12749 | int t1_reg, t2_reg; | |
12750 | int t1_base_reg, t1_last_reg; | |
12751 | int t2_base_reg, t2_last_reg; | |
12752 | char t1_inout, t2_inout; | |
12753 | int i, j; | |
12754 | ||
12755 | if (opcode == XTENSA_UNDEFINED) | |
12756 | { | |
12757 | as_bad (_("invalid opcode")); | |
12758 | return true; | |
12759 | } | |
12760 | ||
12761 | if (xtensa_opcode_num_operands (isa, opcode) > insn->ntok) | |
12762 | { | |
12763 | as_bad (_("too few operands")); | |
12764 | return true; | |
12765 | } | |
12766 | ||
12767 | if (xtensa_opcode_num_operands (isa, opcode) < insn->ntok) | |
12768 | { | |
12769 | as_bad (_("too many operands")); | |
12770 | return true; | |
12771 | } | |
12772 | ||
12773 | /* Check registers. */ | |
12774 | for (j = 0; j < insn->ntok; j++) | |
12775 | { | |
12776 | if (xtensa_operand_is_register (isa, insn->opcode, j) != 1) | |
12777 | continue; | |
12778 | ||
12779 | t2_regfile = xtensa_operand_regfile (isa, insn->opcode, j); | |
12780 | t2_base_reg = insn->tok[j].X_add_number; | |
12781 | t2_last_reg | |
12782 | = t2_base_reg + xtensa_operand_num_regs (isa, insn->opcode, j); | |
12783 | ||
12784 | for (i = 0; i < insn->ntok; i++) | |
12785 | { | |
12786 | if (i == j) | |
12787 | continue; | |
12788 | ||
12789 | if (xtensa_operand_is_register (isa, insn->opcode, i) != 1) | |
12790 | continue; | |
12791 | ||
12792 | t1_regfile = xtensa_operand_regfile (isa, insn->opcode, i); | |
12793 | ||
12794 | if (t1_regfile != t2_regfile) | |
12795 | continue; | |
12796 | ||
12797 | t1_inout = xtensa_operand_inout (isa, insn->opcode, i); | |
12798 | t2_inout = xtensa_operand_inout (isa, insn->opcode, j); | |
12799 | ||
12800 | t1_base_reg = insn->tok[i].X_add_number; | |
12801 | t1_last_reg = (t1_base_reg | |
12802 | + xtensa_operand_num_regs (isa, insn->opcode, i)); | |
12803 | ||
12804 | for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++) | |
12805 | { | |
12806 | for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++) | |
12807 | { | |
12808 | if (t1_reg != t2_reg) | |
12809 | continue; | |
12810 | ||
12811 | if (t1_inout != 'i' && t2_inout != 'i') | |
12812 | { | |
12813 | as_bad (_("multiple writes to the same register")); | |
12814 | return true; | |
12815 | } | |
12816 | } | |
12817 | } | |
12818 | } | |
12819 | } | |
12820 | return false; | |
12821 | } | |
12822 | ||
12823 | ||
12824 | /* Load an instruction from its encoded form. */ | |
12825 | ||
12826 | static void | |
12827 | tinsn_from_chars (TInsn *tinsn, char *f, int slot) | |
12828 | { | |
12829 | vliw_insn vinsn; | |
12830 | ||
12831 | xg_init_vinsn (&vinsn); | |
12832 | vinsn_from_chars (&vinsn, f); | |
12833 | ||
12834 | *tinsn = vinsn.slots[slot]; | |
12835 | xg_free_vinsn (&vinsn); | |
12836 | } | |
12837 | ||
12838 | ||
12839 | static void | |
12840 | tinsn_from_insnbuf (TInsn *tinsn, | |
12841 | xtensa_insnbuf slotbuf, | |
12842 | xtensa_format fmt, | |
12843 | int slot) | |
12844 | { | |
12845 | int i; | |
12846 | xtensa_isa isa = xtensa_default_isa; | |
12847 | ||
12848 | /* Find the immed. */ | |
12849 | tinsn_init (tinsn); | |
12850 | tinsn->insn_type = ITYPE_INSN; | |
12851 | tinsn->is_specific_opcode = false; /* must not be specific */ | |
12852 | tinsn->opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf); | |
12853 | tinsn->ntok = xtensa_opcode_num_operands (isa, tinsn->opcode); | |
12854 | for (i = 0; i < tinsn->ntok; i++) | |
12855 | { | |
12856 | set_expr_const (&tinsn->tok[i], | |
12857 | xtensa_insnbuf_get_operand (slotbuf, fmt, slot, | |
12858 | tinsn->opcode, i)); | |
12859 | } | |
12860 | } | |
12861 | ||
12862 | ||
12863 | /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */ | |
12864 | ||
12865 | static void | |
12866 | tinsn_immed_from_frag (TInsn *tinsn, fragS *fragP, int slot) | |
12867 | { | |
12868 | xtensa_opcode opcode = tinsn->opcode; | |
12869 | int opnum; | |
12870 | ||
12871 | if (fragP->tc_frag_data.slot_symbols[slot]) | |
12872 | { | |
12873 | opnum = get_relaxable_immed (opcode); | |
12874 | gas_assert (opnum >= 0); | |
12875 | set_expr_symbol_offset (&tinsn->tok[opnum], | |
12876 | fragP->tc_frag_data.slot_symbols[slot], | |
12877 | fragP->tc_frag_data.slot_offsets[slot]); | |
12878 | } | |
12879 | tinsn->extra_arg = fragP->tc_frag_data.free_reg[slot]; | |
12880 | } | |
12881 | ||
12882 | ||
12883 | static int | |
12884 | get_num_stack_text_bytes (IStack *istack) | |
12885 | { | |
12886 | int i; | |
12887 | int text_bytes = 0; | |
12888 | ||
12889 | for (i = 0; i < istack->ninsn; i++) | |
12890 | { | |
12891 | TInsn *tinsn = &istack->insn[i]; | |
12892 | if (tinsn->insn_type == ITYPE_INSN) | |
12893 | text_bytes += xg_get_single_size (tinsn->opcode); | |
12894 | } | |
12895 | return text_bytes; | |
12896 | } | |
12897 | ||
12898 | ||
12899 | static int | |
12900 | get_num_stack_literal_bytes (IStack *istack) | |
12901 | { | |
12902 | int i; | |
12903 | int lit_bytes = 0; | |
12904 | ||
12905 | for (i = 0; i < istack->ninsn; i++) | |
12906 | { | |
12907 | TInsn *tinsn = &istack->insn[i]; | |
12908 | if (tinsn->insn_type == ITYPE_LITERAL && tinsn->ntok == 1) | |
12909 | lit_bytes += 4; | |
12910 | } | |
12911 | return lit_bytes; | |
12912 | } | |
12913 | ||
12914 | \f | |
12915 | /* vliw_insn functions. */ | |
12916 | ||
12917 | static void | |
12918 | xg_init_vinsn (vliw_insn *v) | |
12919 | { | |
12920 | int i; | |
12921 | xtensa_isa isa = xtensa_default_isa; | |
12922 | ||
12923 | xg_clear_vinsn (v); | |
12924 | ||
12925 | v->insnbuf = xtensa_insnbuf_alloc (isa); | |
12926 | if (v->insnbuf == NULL) | |
12927 | as_fatal (_("out of memory")); | |
12928 | ||
12929 | for (i = 0; i < config_max_slots; i++) | |
12930 | { | |
12931 | v->slotbuf[i] = xtensa_insnbuf_alloc (isa); | |
12932 | if (v->slotbuf[i] == NULL) | |
12933 | as_fatal (_("out of memory")); | |
12934 | } | |
12935 | } | |
12936 | ||
12937 | ||
12938 | static void | |
12939 | xg_clear_vinsn (vliw_insn *v) | |
12940 | { | |
12941 | int i; | |
12942 | ||
12943 | memset (v, 0, offsetof (vliw_insn, slots) | |
12944 | + sizeof(TInsn) * config_max_slots); | |
12945 | ||
12946 | v->format = XTENSA_UNDEFINED; | |
12947 | v->num_slots = 0; | |
12948 | v->inside_bundle = false; | |
12949 | ||
12950 | if (xt_saved_debug_type != DEBUG_NONE) | |
12951 | debug_type = xt_saved_debug_type; | |
12952 | ||
12953 | for (i = 0; i < config_max_slots; i++) | |
12954 | v->slots[i].opcode = XTENSA_UNDEFINED; | |
12955 | } | |
12956 | ||
12957 | ||
12958 | static void | |
12959 | xg_copy_vinsn (vliw_insn *dst, vliw_insn *src) | |
12960 | { | |
12961 | memcpy (dst, src, | |
12962 | offsetof(vliw_insn, slots) + src->num_slots * sizeof(TInsn)); | |
12963 | dst->insnbuf = src->insnbuf; | |
12964 | memcpy (dst->slotbuf, src->slotbuf, src->num_slots * sizeof(xtensa_insnbuf)); | |
12965 | } | |
12966 | ||
12967 | ||
12968 | static bool | |
12969 | vinsn_has_specific_opcodes (vliw_insn *v) | |
12970 | { | |
12971 | int i; | |
12972 | ||
12973 | for (i = 0; i < v->num_slots; i++) | |
12974 | { | |
12975 | if (v->slots[i].is_specific_opcode) | |
12976 | return true; | |
12977 | } | |
12978 | return false; | |
12979 | } | |
12980 | ||
12981 | ||
12982 | static void | |
12983 | xg_free_vinsn (vliw_insn *v) | |
12984 | { | |
12985 | int i; | |
12986 | xtensa_insnbuf_free (xtensa_default_isa, v->insnbuf); | |
12987 | for (i = 0; i < config_max_slots; i++) | |
12988 | xtensa_insnbuf_free (xtensa_default_isa, v->slotbuf[i]); | |
12989 | } | |
12990 | ||
12991 | ||
12992 | /* Encode a vliw_insn into an insnbuf. Return TRUE if there are any symbolic | |
12993 | operands. See also the assumptions listed for tinsn_to_slotbuf. */ | |
12994 | ||
12995 | static bool | |
12996 | vinsn_to_insnbuf (vliw_insn *vinsn, | |
12997 | char *frag_offset, | |
12998 | fragS *fragP, | |
12999 | bool record_fixup) | |
13000 | { | |
13001 | xtensa_isa isa = xtensa_default_isa; | |
13002 | xtensa_format fmt = vinsn->format; | |
13003 | xtensa_insnbuf insnbuf = vinsn->insnbuf; | |
13004 | int slot; | |
13005 | bool has_fixup = false; | |
13006 | ||
13007 | xtensa_format_encode (isa, fmt, insnbuf); | |
13008 | ||
13009 | for (slot = 0; slot < vinsn->num_slots; slot++) | |
13010 | { | |
13011 | TInsn *tinsn = &vinsn->slots[slot]; | |
13012 | expressionS *extra_arg = &tinsn->extra_arg; | |
13013 | bool tinsn_has_fixup = | |
13014 | tinsn_to_slotbuf (vinsn->format, slot, tinsn, | |
13015 | vinsn->slotbuf[slot]); | |
13016 | ||
13017 | xtensa_format_set_slot (isa, fmt, slot, | |
13018 | insnbuf, vinsn->slotbuf[slot]); | |
13019 | if (extra_arg->X_op != O_illegal && extra_arg->X_op != O_register) | |
13020 | { | |
13021 | if (vinsn->num_slots != 1) | |
13022 | as_bad (_("TLS relocation not allowed in FLIX bundle")); | |
13023 | else if (record_fixup) | |
13024 | /* Instructions that generate TLS relocations should always be | |
13025 | relaxed in the front-end. If "record_fixup" is set, then this | |
13026 | function is being called during back-end relaxation, so flag | |
13027 | the unexpected behavior as an error. */ | |
13028 | as_bad (_("unexpected TLS relocation")); | |
13029 | else | |
13030 | fix_new (fragP, frag_offset - fragP->fr_literal, | |
13031 | xtensa_format_length (isa, fmt), | |
13032 | extra_arg->X_add_symbol, extra_arg->X_add_number, | |
13033 | false, map_operator_to_reloc (extra_arg->X_op, false)); | |
13034 | } | |
13035 | if (tinsn_has_fixup) | |
13036 | { | |
13037 | int i; | |
13038 | xtensa_opcode opcode = tinsn->opcode; | |
13039 | int noperands = xtensa_opcode_num_operands (isa, opcode); | |
13040 | has_fixup = true; | |
13041 | ||
13042 | for (i = 0; i < noperands; i++) | |
13043 | { | |
13044 | expressionS* exp = &tinsn->tok[i]; | |
13045 | switch (exp->X_op) | |
13046 | { | |
13047 | case O_symbol: | |
13048 | case O_lo16: | |
13049 | case O_hi16: | |
13050 | if (get_relaxable_immed (opcode) == i) | |
13051 | { | |
13052 | /* Add a fix record for the instruction, except if this | |
13053 | function is being called prior to relaxation, i.e., | |
13054 | if record_fixup is false, and the instruction might | |
13055 | be relaxed later. */ | |
13056 | if (record_fixup | |
13057 | || tinsn->is_specific_opcode | |
13058 | || !xg_is_relaxable_insn (tinsn, 0)) | |
13059 | { | |
13060 | xg_add_opcode_fix (tinsn, i, fmt, slot, exp, fragP, | |
13061 | frag_offset - fragP->fr_literal); | |
13062 | } | |
13063 | else | |
13064 | { | |
13065 | if (exp->X_op != O_symbol) | |
13066 | as_bad (_("invalid operand")); | |
13067 | tinsn->symbol = exp->X_add_symbol; | |
13068 | tinsn->offset = exp->X_add_number; | |
13069 | } | |
13070 | } | |
13071 | else | |
13072 | as_bad (_("symbolic operand not allowed")); | |
13073 | break; | |
13074 | ||
13075 | case O_constant: | |
13076 | case O_register: | |
13077 | break; | |
13078 | ||
13079 | default: | |
13080 | as_bad (_("expression too complex")); | |
13081 | break; | |
13082 | } | |
13083 | } | |
13084 | } | |
13085 | } | |
13086 | ||
13087 | return has_fixup; | |
13088 | } | |
13089 | ||
13090 | ||
13091 | static void | |
13092 | vinsn_from_chars (vliw_insn *vinsn, char *f) | |
13093 | { | |
13094 | static xtensa_insnbuf insnbuf = NULL; | |
13095 | static xtensa_insnbuf slotbuf = NULL; | |
13096 | int i; | |
13097 | xtensa_format fmt; | |
13098 | xtensa_isa isa = xtensa_default_isa; | |
13099 | ||
13100 | if (!insnbuf) | |
13101 | { | |
13102 | insnbuf = xtensa_insnbuf_alloc (isa); | |
13103 | slotbuf = xtensa_insnbuf_alloc (isa); | |
13104 | } | |
13105 | ||
13106 | xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) f, 0); | |
13107 | fmt = xtensa_format_decode (isa, insnbuf); | |
13108 | if (fmt == XTENSA_UNDEFINED) | |
13109 | as_fatal (_("cannot decode instruction format")); | |
13110 | vinsn->format = fmt; | |
13111 | vinsn->num_slots = xtensa_format_num_slots (isa, fmt); | |
13112 | ||
13113 | for (i = 0; i < vinsn->num_slots; i++) | |
13114 | { | |
13115 | TInsn *tinsn = &vinsn->slots[i]; | |
13116 | xtensa_format_get_slot (isa, fmt, i, insnbuf, slotbuf); | |
13117 | tinsn_from_insnbuf (tinsn, slotbuf, fmt, i); | |
13118 | } | |
13119 | } | |
13120 | ||
13121 | \f | |
13122 | /* Expression utilities. */ | |
13123 | ||
13124 | /* Return TRUE if the expression is an integer constant. */ | |
13125 | ||
13126 | bool | |
13127 | expr_is_const (const expressionS *s) | |
13128 | { | |
13129 | return (s->X_op == O_constant); | |
13130 | } | |
13131 | ||
13132 | ||
13133 | /* Get the expression constant. | |
13134 | Calling this is illegal if expr_is_const () returns TRUE. */ | |
13135 | ||
13136 | offsetT | |
13137 | get_expr_const (const expressionS *s) | |
13138 | { | |
13139 | gas_assert (expr_is_const (s)); | |
13140 | return s->X_add_number; | |
13141 | } | |
13142 | ||
13143 | ||
13144 | /* Set the expression to a constant value. */ | |
13145 | ||
13146 | void | |
13147 | set_expr_const (expressionS *s, offsetT val) | |
13148 | { | |
13149 | s->X_op = O_constant; | |
13150 | s->X_add_number = val; | |
13151 | s->X_add_symbol = NULL; | |
13152 | s->X_op_symbol = NULL; | |
13153 | } | |
13154 | ||
13155 | ||
13156 | bool | |
13157 | expr_is_register (const expressionS *s) | |
13158 | { | |
13159 | return (s->X_op == O_register); | |
13160 | } | |
13161 | ||
13162 | ||
13163 | /* Get the expression constant. | |
13164 | Calling this is illegal if expr_is_const () returns TRUE. */ | |
13165 | ||
13166 | offsetT | |
13167 | get_expr_register (const expressionS *s) | |
13168 | { | |
13169 | gas_assert (expr_is_register (s)); | |
13170 | return s->X_add_number; | |
13171 | } | |
13172 | ||
13173 | ||
13174 | /* Set the expression to a symbol + constant offset. */ | |
13175 | ||
13176 | void | |
13177 | set_expr_symbol_offset (expressionS *s, symbolS *sym, offsetT offset) | |
13178 | { | |
13179 | s->X_op = O_symbol; | |
13180 | s->X_add_symbol = sym; | |
13181 | s->X_op_symbol = NULL; /* unused */ | |
13182 | s->X_add_number = offset; | |
13183 | } | |
13184 | ||
13185 | ||
13186 | /* Return TRUE if the two expressions are equal. */ | |
13187 | ||
13188 | bool | |
13189 | expr_is_equal (expressionS *s1, expressionS *s2) | |
13190 | { | |
13191 | if (s1->X_op != s2->X_op) | |
13192 | return false; | |
13193 | if (s1->X_add_symbol != s2->X_add_symbol) | |
13194 | return false; | |
13195 | if (s1->X_op_symbol != s2->X_op_symbol) | |
13196 | return false; | |
13197 | if (s1->X_add_number != s2->X_add_number) | |
13198 | return false; | |
13199 | return true; | |
13200 | } | |
13201 | ||
13202 | ||
13203 | static void | |
13204 | copy_expr (expressionS *dst, const expressionS *src) | |
13205 | { | |
13206 | memcpy (dst, src, sizeof (expressionS)); | |
13207 | } | |
13208 | ||
13209 | \f | |
13210 | /* Support for the "--rename-section" option. */ | |
13211 | ||
13212 | struct rename_section_struct | |
13213 | { | |
13214 | const char *old_name; | |
13215 | char *new_name; | |
13216 | struct rename_section_struct *next; | |
13217 | }; | |
13218 | ||
13219 | static struct rename_section_struct *section_rename; | |
13220 | ||
13221 | ||
13222 | /* Parse the string "oldname=new_name(:oldname2=new_name2)*" and add | |
13223 | entries to the section_rename list. Note: Specifying multiple | |
13224 | renamings separated by colons is not documented and is retained only | |
13225 | for backward compatibility. */ | |
13226 | ||
13227 | static void | |
13228 | build_section_rename (const char *arg) | |
13229 | { | |
13230 | struct rename_section_struct *r; | |
13231 | char *this_arg = NULL; | |
13232 | char *next_arg = NULL; | |
13233 | ||
13234 | for (this_arg = xstrdup (arg); this_arg != NULL; this_arg = next_arg) | |
13235 | { | |
13236 | char *old_name, *new_name; | |
13237 | ||
13238 | if (this_arg) | |
13239 | { | |
13240 | next_arg = strchr (this_arg, ':'); | |
13241 | if (next_arg) | |
13242 | { | |
13243 | *next_arg = '\0'; | |
13244 | next_arg++; | |
13245 | } | |
13246 | } | |
13247 | ||
13248 | old_name = this_arg; | |
13249 | new_name = strchr (this_arg, '='); | |
13250 | ||
13251 | if (*old_name == '\0') | |
13252 | { | |
13253 | as_warn (_("ignoring extra '-rename-section' delimiter ':'")); | |
13254 | continue; | |
13255 | } | |
13256 | if (!new_name || new_name[1] == '\0') | |
13257 | { | |
13258 | as_warn (_("ignoring invalid '-rename-section' specification: '%s'"), | |
13259 | old_name); | |
13260 | continue; | |
13261 | } | |
13262 | *new_name = '\0'; | |
13263 | new_name++; | |
13264 | ||
13265 | /* Check for invalid section renaming. */ | |
13266 | for (r = section_rename; r != NULL; r = r->next) | |
13267 | { | |
13268 | if (strcmp (r->old_name, old_name) == 0) | |
13269 | as_bad (_("section %s renamed multiple times"), old_name); | |
13270 | if (strcmp (r->new_name, new_name) == 0) | |
13271 | as_bad (_("multiple sections remapped to output section %s"), | |
13272 | new_name); | |
13273 | } | |
13274 | ||
13275 | /* Now add it. */ | |
13276 | r = XNEW (struct rename_section_struct); | |
13277 | r->old_name = xstrdup (old_name); | |
13278 | r->new_name = xstrdup (new_name); | |
13279 | r->next = section_rename; | |
13280 | section_rename = r; | |
13281 | } | |
13282 | } | |
13283 | ||
13284 | ||
13285 | char * | |
13286 | xtensa_section_rename (const char *name) | |
13287 | { | |
13288 | struct rename_section_struct *r = section_rename; | |
13289 | ||
13290 | for (r = section_rename; r != NULL; r = r->next) | |
13291 | { | |
13292 | if (strcmp (r->old_name, name) == 0) | |
13293 | return r->new_name; | |
13294 | } | |
13295 | ||
13296 | return (char *) name; | |
13297 | } |