]>
Commit | Line | Data |
---|---|---|
e0001a05 | 1 | /* tc-xtensa.c -- Assemble Xtensa instructions. |
d47d412e | 2 | Copyright 2003, 2004, 2005, 2006 Free Software Foundation, Inc. |
e0001a05 NC |
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 2, 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 | |
4b4da160 NC |
18 | the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, |
19 | MA 02110-1301, USA. */ | |
e0001a05 | 20 | |
43cd72b9 | 21 | #include <limits.h> |
e0001a05 NC |
22 | #include "as.h" |
23 | #include "sb.h" | |
24 | #include "safe-ctype.h" | |
25 | #include "tc-xtensa.h" | |
e0001a05 NC |
26 | #include "subsegs.h" |
27 | #include "xtensa-relax.h" | |
28 | #include "xtensa-istack.h" | |
cda2eb9e | 29 | #include "dwarf2dbg.h" |
e0001a05 NC |
30 | #include "struc-symbol.h" |
31 | #include "xtensa-config.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 | ||
e0001a05 NC |
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 | ||
43cd72b9 BW |
66 | /* Flags to indicate whether the hardware supports the density and |
67 | absolute literals options. */ | |
e0001a05 | 68 | |
e0001a05 | 69 | bfd_boolean density_supported = XCHAL_HAVE_DENSITY; |
43cd72b9 BW |
70 | bfd_boolean absolute_literals_supported = XSHAL_USE_ABSOLUTE_LITERALS; |
71 | ||
72 | /* Maximum width we would pad an unreachable frag to get alignment. */ | |
73 | #define UNREACHABLE_MAX_WIDTH 8 | |
e0001a05 | 74 | |
43cd72b9 BW |
75 | static vliw_insn cur_vinsn; |
76 | ||
d77b99c9 | 77 | unsigned xtensa_fetch_width = XCHAL_INST_FETCH_WIDTH; |
43cd72b9 BW |
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 | |
c138bc38 | 82 | allows us to assert that we haven't crossed over into the |
43cd72b9 BW |
83 | back end. */ |
84 | static bfd_boolean past_xtensa_end = FALSE; | |
e0001a05 NC |
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") | |
43cd72b9 | 97 | #define LIT4_SECTION_NAME xtensa_section_rename (".lit4") |
e0001a05 | 98 | #define INIT_SECTION_NAME xtensa_section_rename (".init") |
74869ac7 | 99 | #define FINI_SECTION_NAME xtensa_section_rename (".fini") |
e0001a05 NC |
100 | |
101 | ||
43cd72b9 | 102 | /* This type is used for the directive_stack to keep track of the |
74869ac7 BW |
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. */ | |
e0001a05 NC |
109 | |
110 | typedef struct lit_state_struct | |
111 | { | |
74869ac7 BW |
112 | char *lit_prefix; |
113 | segT current_text_seg; | |
e0001a05 | 114 | segT lit_seg; |
43cd72b9 | 115 | segT lit4_seg; |
e0001a05 NC |
116 | } lit_state; |
117 | ||
118 | static lit_state default_lit_sections; | |
119 | ||
120 | ||
74869ac7 BW |
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. */ | |
e0001a05 NC |
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; | |
e0001a05 NC |
133 | |
134 | ||
82e7541d BW |
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 | ||
43cd72b9 BW |
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 | ||
e0001a05 NC |
159 | /* Global flag to indicate when we are emitting literals. */ |
160 | int generating_literals = 0; | |
161 | ||
43cd72b9 BW |
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 | #define XTENSA_PROP_INSN_NO_TRANSFORM 0x00000100 | |
186 | ||
187 | /* Branch target alignment information. This transmits information | |
188 | to the linker optimization about the priority of aligning a | |
189 | particular block for branch target alignment: None, low priority, | |
190 | high priority, or required. These only need to be checked in | |
191 | instruction blocks marked as XTENSA_PROP_INSN_BRANCH_TARGET. | |
192 | Common usage is | |
193 | ||
194 | switch (GET_XTENSA_PROP_BT_ALIGN (flags)) | |
195 | case XTENSA_PROP_BT_ALIGN_NONE: | |
196 | case XTENSA_PROP_BT_ALIGN_LOW: | |
197 | case XTENSA_PROP_BT_ALIGN_HIGH: | |
198 | case XTENSA_PROP_BT_ALIGN_REQUIRE: | |
199 | */ | |
200 | #define XTENSA_PROP_BT_ALIGN_MASK 0x00000600 | |
201 | ||
202 | /* No branch target alignment. */ | |
203 | #define XTENSA_PROP_BT_ALIGN_NONE 0x0 | |
204 | /* Low priority branch target alignment. */ | |
205 | #define XTENSA_PROP_BT_ALIGN_LOW 0x1 | |
206 | /* High priority branch target alignment. */ | |
207 | #define XTENSA_PROP_BT_ALIGN_HIGH 0x2 | |
208 | /* Required branch target alignment. */ | |
209 | #define XTENSA_PROP_BT_ALIGN_REQUIRE 0x3 | |
210 | ||
211 | #define GET_XTENSA_PROP_BT_ALIGN(flag) \ | |
212 | (((unsigned) ((flag) & (XTENSA_PROP_BT_ALIGN_MASK))) >> 9) | |
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 GET_XTENSA_PROP_ALIGNMENT(flag) \ | |
234 | (((unsigned) ((flag) & (XTENSA_PROP_ALIGNMENT_MASK))) >> 12) | |
235 | #define SET_XTENSA_PROP_ALIGNMENT(flag, align) \ | |
236 | (((flag) & (~XTENSA_PROP_ALIGNMENT_MASK)) | \ | |
237 | (((align) << 12) & XTENSA_PROP_ALIGNMENT_MASK)) | |
238 | ||
239 | #define XTENSA_PROP_INSN_ABSLIT 0x00020000 | |
240 | ||
241 | ||
242 | /* Structure for saving instruction and alignment per-fragment data | |
243 | that will be written to the object file. This structure is | |
244 | equivalent to the actual data that will be written out to the file | |
245 | but is easier to use. We provide a conversion to file flags | |
246 | in frag_flags_to_number. */ | |
247 | ||
248 | typedef struct frag_flags_struct frag_flags; | |
249 | ||
250 | struct frag_flags_struct | |
251 | { | |
252 | /* is_literal should only be used after xtensa_move_literals. | |
253 | If you need to check if you are generating a literal fragment, | |
254 | then use the generating_literals global. */ | |
255 | ||
256 | unsigned is_literal : 1; | |
257 | unsigned is_insn : 1; | |
258 | unsigned is_data : 1; | |
259 | unsigned is_unreachable : 1; | |
260 | ||
261 | struct | |
262 | { | |
263 | unsigned is_loop_target : 1; | |
264 | unsigned is_branch_target : 1; /* Branch targets have a priority. */ | |
265 | unsigned bt_align_priority : 2; | |
266 | ||
267 | unsigned is_no_density : 1; | |
268 | /* no_longcalls flag does not need to be placed in the object file. */ | |
269 | /* is_specific_opcode implies no_transform. */ | |
270 | unsigned is_no_transform : 1; | |
271 | ||
272 | unsigned is_no_reorder : 1; | |
273 | ||
274 | /* Uses absolute literal addressing for l32r. */ | |
275 | unsigned is_abslit : 1; | |
276 | } insn; | |
277 | unsigned is_align : 1; | |
278 | unsigned alignment : 5; | |
279 | }; | |
280 | ||
281 | ||
282 | /* Structure for saving information about a block of property data | |
283 | for frags that have the same flags. */ | |
284 | struct xtensa_block_info_struct | |
285 | { | |
286 | segT sec; | |
287 | bfd_vma offset; | |
288 | size_t size; | |
289 | frag_flags flags; | |
290 | struct xtensa_block_info_struct *next; | |
291 | }; | |
292 | ||
e0001a05 NC |
293 | |
294 | /* Structure for saving the current state before emitting literals. */ | |
295 | typedef struct emit_state_struct | |
296 | { | |
297 | const char *name; | |
298 | segT now_seg; | |
299 | subsegT now_subseg; | |
300 | int generating_literals; | |
301 | } emit_state; | |
302 | ||
303 | ||
43cd72b9 BW |
304 | /* Opcode placement information */ |
305 | ||
306 | typedef unsigned long long bitfield; | |
307 | #define bit_is_set(bit, bf) ((bf) & (0x01ll << (bit))) | |
308 | #define set_bit(bit, bf) ((bf) |= (0x01ll << (bit))) | |
309 | #define clear_bit(bit, bf) ((bf) &= ~(0x01ll << (bit))) | |
310 | ||
311 | #define MAX_FORMATS 32 | |
312 | ||
313 | typedef struct op_placement_info_struct | |
314 | { | |
315 | int num_formats; | |
316 | /* A number describing how restrictive the issue is for this | |
317 | opcode. For example, an opcode that fits lots of different | |
c138bc38 | 318 | formats has a high freedom, as does an opcode that fits |
43cd72b9 | 319 | only one format but many slots in that format. The most |
c138bc38 | 320 | restrictive is the opcode that fits only one slot in one |
43cd72b9 BW |
321 | format. */ |
322 | int issuef; | |
43cd72b9 | 323 | xtensa_format narrowest; |
43cd72b9 | 324 | char narrowest_size; |
b2d179be | 325 | char narrowest_slot; |
43cd72b9 BW |
326 | |
327 | /* formats is a bitfield with the Nth bit set | |
328 | if the opcode fits in the Nth xtensa_format. */ | |
329 | bitfield formats; | |
330 | ||
331 | /* slots[N]'s Mth bit is set if the op fits in the | |
332 | Mth slot of the Nth xtensa_format. */ | |
333 | bitfield slots[MAX_FORMATS]; | |
334 | ||
335 | /* A count of the number of slots in a given format | |
336 | an op can fit (i.e., the bitcount of the slot field above). */ | |
337 | char slots_in_format[MAX_FORMATS]; | |
338 | ||
339 | } op_placement_info, *op_placement_info_table; | |
340 | ||
341 | op_placement_info_table op_placement_table; | |
342 | ||
343 | ||
344 | /* Extra expression types. */ | |
345 | ||
346 | #define O_pltrel O_md1 /* like O_symbol but use a PLT reloc */ | |
347 | #define O_hi16 O_md2 /* use high 16 bits of symbolic value */ | |
348 | #define O_lo16 O_md3 /* use low 16 bits of symbolic value */ | |
349 | ||
350 | ||
e0001a05 NC |
351 | /* Directives. */ |
352 | ||
353 | typedef enum | |
354 | { | |
355 | directive_none = 0, | |
356 | directive_literal, | |
357 | directive_density, | |
43cd72b9 | 358 | directive_transform, |
e0001a05 NC |
359 | directive_freeregs, |
360 | directive_longcalls, | |
43cd72b9 BW |
361 | directive_literal_prefix, |
362 | directive_schedule, | |
363 | directive_absolute_literals, | |
364 | directive_last_directive | |
e0001a05 NC |
365 | } directiveE; |
366 | ||
367 | typedef struct | |
368 | { | |
369 | const char *name; | |
370 | bfd_boolean can_be_negated; | |
371 | } directive_infoS; | |
372 | ||
373 | const directive_infoS directive_info[] = | |
374 | { | |
43cd72b9 BW |
375 | { "none", FALSE }, |
376 | { "literal", FALSE }, | |
377 | { "density", TRUE }, | |
378 | { "transform", TRUE }, | |
379 | { "freeregs", FALSE }, | |
380 | { "longcalls", TRUE }, | |
381 | { "literal_prefix", FALSE }, | |
382 | { "schedule", TRUE }, | |
383 | { "absolute-literals", TRUE } | |
e0001a05 NC |
384 | }; |
385 | ||
386 | bfd_boolean directive_state[] = | |
387 | { | |
388 | FALSE, /* none */ | |
389 | FALSE, /* literal */ | |
43cd72b9 | 390 | #if !XCHAL_HAVE_DENSITY |
e0001a05 NC |
391 | FALSE, /* density */ |
392 | #else | |
393 | TRUE, /* density */ | |
394 | #endif | |
43cd72b9 | 395 | TRUE, /* transform */ |
e0001a05 NC |
396 | FALSE, /* freeregs */ |
397 | FALSE, /* longcalls */ | |
43cd72b9 BW |
398 | FALSE, /* literal_prefix */ |
399 | TRUE, /* schedule */ | |
400 | #if XSHAL_USE_ABSOLUTE_LITERALS | |
401 | TRUE /* absolute_literals */ | |
402 | #else | |
403 | FALSE /* absolute_literals */ | |
404 | #endif | |
e0001a05 NC |
405 | }; |
406 | ||
e0001a05 NC |
407 | |
408 | /* Directive functions. */ | |
409 | ||
7fa3d080 BW |
410 | static void xtensa_begin_directive (int); |
411 | static void xtensa_end_directive (int); | |
74869ac7 | 412 | static void xtensa_literal_prefix (void); |
7fa3d080 BW |
413 | static void xtensa_literal_position (int); |
414 | static void xtensa_literal_pseudo (int); | |
415 | static void xtensa_frequency_pseudo (int); | |
416 | static void xtensa_elf_cons (int); | |
e0001a05 | 417 | |
7fa3d080 | 418 | /* Parsing and Idiom Translation. */ |
e0001a05 | 419 | |
7fa3d080 | 420 | static bfd_reloc_code_real_type xtensa_elf_suffix (char **, expressionS *); |
e0001a05 | 421 | |
e0001a05 NC |
422 | /* Various Other Internal Functions. */ |
423 | ||
84b08ed9 BW |
424 | extern bfd_boolean xg_is_single_relaxable_insn (TInsn *, TInsn *, bfd_boolean); |
425 | static bfd_boolean xg_build_to_insn (TInsn *, TInsn *, BuildInstr *); | |
7fa3d080 BW |
426 | static void xtensa_mark_literal_pool_location (void); |
427 | static addressT get_expanded_loop_offset (xtensa_opcode); | |
428 | static fragS *get_literal_pool_location (segT); | |
429 | static void set_literal_pool_location (segT, fragS *); | |
430 | static void xtensa_set_frag_assembly_state (fragS *); | |
431 | static void finish_vinsn (vliw_insn *); | |
432 | static bfd_boolean emit_single_op (TInsn *); | |
34e41783 | 433 | static int total_frag_text_expansion (fragS *); |
e0001a05 NC |
434 | |
435 | /* Alignment Functions. */ | |
436 | ||
d77b99c9 BW |
437 | static int get_text_align_power (unsigned); |
438 | static int get_text_align_max_fill_size (int, bfd_boolean, bfd_boolean); | |
664df4e4 | 439 | static int branch_align_power (segT); |
e0001a05 NC |
440 | |
441 | /* Helpers for xtensa_relax_frag(). */ | |
442 | ||
7fa3d080 | 443 | static long relax_frag_add_nop (fragS *); |
e0001a05 | 444 | |
b08b5071 | 445 | /* Accessors for additional per-subsegment information. */ |
e0001a05 | 446 | |
7fa3d080 BW |
447 | static unsigned get_last_insn_flags (segT, subsegT); |
448 | static void set_last_insn_flags (segT, subsegT, unsigned, bfd_boolean); | |
b08b5071 BW |
449 | static float get_subseg_total_freq (segT, subsegT); |
450 | static float get_subseg_target_freq (segT, subsegT); | |
451 | static void set_subseg_freq (segT, subsegT, float, float); | |
e0001a05 NC |
452 | |
453 | /* Segment list functions. */ | |
454 | ||
7fa3d080 BW |
455 | static void xtensa_move_literals (void); |
456 | static void xtensa_reorder_segments (void); | |
457 | static void xtensa_switch_to_literal_fragment (emit_state *); | |
458 | static void xtensa_switch_to_non_abs_literal_fragment (emit_state *); | |
459 | static void xtensa_switch_section_emit_state (emit_state *, segT, subsegT); | |
460 | static void xtensa_restore_emit_state (emit_state *); | |
74869ac7 | 461 | static segT cache_literal_section (bfd_boolean); |
e0001a05 | 462 | |
e0001a05 | 463 | /* Import from elf32-xtensa.c in BFD library. */ |
43cd72b9 | 464 | |
74869ac7 | 465 | extern asection *xtensa_get_property_section (asection *, const char *); |
e0001a05 | 466 | |
43cd72b9 BW |
467 | /* op_placement_info functions. */ |
468 | ||
7fa3d080 BW |
469 | static void init_op_placement_info_table (void); |
470 | extern bfd_boolean opcode_fits_format_slot (xtensa_opcode, xtensa_format, int); | |
471 | static int xg_get_single_size (xtensa_opcode); | |
472 | static xtensa_format xg_get_single_format (xtensa_opcode); | |
b2d179be | 473 | static int xg_get_single_slot (xtensa_opcode); |
43cd72b9 | 474 | |
e0001a05 | 475 | /* TInsn and IStack functions. */ |
43cd72b9 | 476 | |
7fa3d080 BW |
477 | static bfd_boolean tinsn_has_symbolic_operands (const TInsn *); |
478 | static bfd_boolean tinsn_has_invalid_symbolic_operands (const TInsn *); | |
479 | static bfd_boolean tinsn_has_complex_operands (const TInsn *); | |
480 | static bfd_boolean tinsn_to_insnbuf (TInsn *, xtensa_insnbuf); | |
481 | static bfd_boolean tinsn_check_arguments (const TInsn *); | |
482 | static void tinsn_from_chars (TInsn *, char *, int); | |
483 | static void tinsn_immed_from_frag (TInsn *, fragS *, int); | |
484 | static int get_num_stack_text_bytes (IStack *); | |
485 | static int get_num_stack_literal_bytes (IStack *); | |
e0001a05 | 486 | |
43cd72b9 BW |
487 | /* vliw_insn functions. */ |
488 | ||
7fa3d080 BW |
489 | static void xg_init_vinsn (vliw_insn *); |
490 | static void xg_clear_vinsn (vliw_insn *); | |
491 | static bfd_boolean vinsn_has_specific_opcodes (vliw_insn *); | |
492 | static void xg_free_vinsn (vliw_insn *); | |
43cd72b9 | 493 | static bfd_boolean vinsn_to_insnbuf |
7fa3d080 BW |
494 | (vliw_insn *, char *, fragS *, bfd_boolean); |
495 | static void vinsn_from_chars (vliw_insn *, char *); | |
43cd72b9 | 496 | |
e0001a05 | 497 | /* Expression Utilities. */ |
43cd72b9 | 498 | |
7fa3d080 BW |
499 | bfd_boolean expr_is_const (const expressionS *); |
500 | offsetT get_expr_const (const expressionS *); | |
501 | void set_expr_const (expressionS *, offsetT); | |
502 | bfd_boolean expr_is_register (const expressionS *); | |
503 | offsetT get_expr_register (const expressionS *); | |
504 | void set_expr_symbol_offset (expressionS *, symbolS *, offsetT); | |
7fa3d080 BW |
505 | bfd_boolean expr_is_equal (expressionS *, expressionS *); |
506 | static void copy_expr (expressionS *, const expressionS *); | |
e0001a05 | 507 | |
9456465c BW |
508 | /* Section renaming. */ |
509 | ||
7fa3d080 | 510 | static void build_section_rename (const char *); |
e0001a05 | 511 | |
e0001a05 NC |
512 | |
513 | /* ISA imported from bfd. */ | |
514 | extern xtensa_isa xtensa_default_isa; | |
515 | ||
516 | extern int target_big_endian; | |
517 | ||
518 | static xtensa_opcode xtensa_addi_opcode; | |
519 | static xtensa_opcode xtensa_addmi_opcode; | |
520 | static xtensa_opcode xtensa_call0_opcode; | |
521 | static xtensa_opcode xtensa_call4_opcode; | |
522 | static xtensa_opcode xtensa_call8_opcode; | |
523 | static xtensa_opcode xtensa_call12_opcode; | |
524 | static xtensa_opcode xtensa_callx0_opcode; | |
525 | static xtensa_opcode xtensa_callx4_opcode; | |
526 | static xtensa_opcode xtensa_callx8_opcode; | |
527 | static xtensa_opcode xtensa_callx12_opcode; | |
43cd72b9 | 528 | static xtensa_opcode xtensa_const16_opcode; |
e0001a05 | 529 | static xtensa_opcode xtensa_entry_opcode; |
43cd72b9 BW |
530 | static xtensa_opcode xtensa_movi_opcode; |
531 | static xtensa_opcode xtensa_movi_n_opcode; | |
e0001a05 | 532 | static xtensa_opcode xtensa_isync_opcode; |
e0001a05 | 533 | static xtensa_opcode xtensa_jx_opcode; |
43cd72b9 | 534 | static xtensa_opcode xtensa_l32r_opcode; |
e0001a05 NC |
535 | static xtensa_opcode xtensa_loop_opcode; |
536 | static xtensa_opcode xtensa_loopnez_opcode; | |
537 | static xtensa_opcode xtensa_loopgtz_opcode; | |
43cd72b9 | 538 | static xtensa_opcode xtensa_nop_opcode; |
e0001a05 NC |
539 | static xtensa_opcode xtensa_nop_n_opcode; |
540 | static xtensa_opcode xtensa_or_opcode; | |
541 | static xtensa_opcode xtensa_ret_opcode; | |
542 | static xtensa_opcode xtensa_ret_n_opcode; | |
543 | static xtensa_opcode xtensa_retw_opcode; | |
544 | static xtensa_opcode xtensa_retw_n_opcode; | |
43cd72b9 | 545 | static xtensa_opcode xtensa_rsr_lcount_opcode; |
e0001a05 NC |
546 | static xtensa_opcode xtensa_waiti_opcode; |
547 | ||
548 | \f | |
549 | /* Command-line Options. */ | |
550 | ||
551 | bfd_boolean use_literal_section = TRUE; | |
552 | static bfd_boolean align_targets = TRUE; | |
43cd72b9 | 553 | static bfd_boolean warn_unaligned_branch_targets = FALSE; |
e0001a05 | 554 | static bfd_boolean has_a0_b_retw = FALSE; |
43cd72b9 BW |
555 | static bfd_boolean workaround_a0_b_retw = FALSE; |
556 | static bfd_boolean workaround_b_j_loop_end = FALSE; | |
557 | static bfd_boolean workaround_short_loop = FALSE; | |
e0001a05 | 558 | static bfd_boolean maybe_has_short_loop = FALSE; |
43cd72b9 | 559 | static bfd_boolean workaround_close_loop_end = FALSE; |
e0001a05 | 560 | static bfd_boolean maybe_has_close_loop_end = FALSE; |
03aaa593 | 561 | static bfd_boolean enforce_three_byte_loop_align = FALSE; |
e0001a05 | 562 | |
43cd72b9 BW |
563 | /* When workaround_short_loops is TRUE, all loops with early exits must |
564 | have at least 3 instructions. workaround_all_short_loops is a modifier | |
565 | to the workaround_short_loop flag. In addition to the | |
566 | workaround_short_loop actions, all straightline loopgtz and loopnez | |
567 | must have at least 3 instructions. */ | |
e0001a05 | 568 | |
43cd72b9 | 569 | static bfd_boolean workaround_all_short_loops = FALSE; |
e0001a05 | 570 | |
7fa3d080 BW |
571 | |
572 | static void | |
573 | xtensa_setup_hw_workarounds (int earliest, int latest) | |
574 | { | |
575 | if (earliest > latest) | |
576 | as_fatal (_("illegal range of target hardware versions")); | |
577 | ||
578 | /* Enable all workarounds for pre-T1050.0 hardware. */ | |
579 | if (earliest < 105000 || latest < 105000) | |
580 | { | |
581 | workaround_a0_b_retw |= TRUE; | |
582 | workaround_b_j_loop_end |= TRUE; | |
583 | workaround_short_loop |= TRUE; | |
584 | workaround_close_loop_end |= TRUE; | |
585 | workaround_all_short_loops |= TRUE; | |
03aaa593 | 586 | enforce_three_byte_loop_align = TRUE; |
7fa3d080 BW |
587 | } |
588 | } | |
589 | ||
590 | ||
e0001a05 NC |
591 | enum |
592 | { | |
593 | option_density = OPTION_MD_BASE, | |
594 | option_no_density, | |
595 | ||
596 | option_relax, | |
597 | option_no_relax, | |
598 | ||
43cd72b9 BW |
599 | option_link_relax, |
600 | option_no_link_relax, | |
601 | ||
e0001a05 NC |
602 | option_generics, |
603 | option_no_generics, | |
604 | ||
43cd72b9 BW |
605 | option_transform, |
606 | option_no_transform, | |
607 | ||
e0001a05 NC |
608 | option_text_section_literals, |
609 | option_no_text_section_literals, | |
610 | ||
43cd72b9 BW |
611 | option_absolute_literals, |
612 | option_no_absolute_literals, | |
613 | ||
e0001a05 NC |
614 | option_align_targets, |
615 | option_no_align_targets, | |
616 | ||
43cd72b9 | 617 | option_warn_unaligned_targets, |
e0001a05 NC |
618 | |
619 | option_longcalls, | |
620 | option_no_longcalls, | |
621 | ||
622 | option_workaround_a0_b_retw, | |
623 | option_no_workaround_a0_b_retw, | |
624 | ||
625 | option_workaround_b_j_loop_end, | |
626 | option_no_workaround_b_j_loop_end, | |
627 | ||
628 | option_workaround_short_loop, | |
629 | option_no_workaround_short_loop, | |
630 | ||
631 | option_workaround_all_short_loops, | |
632 | option_no_workaround_all_short_loops, | |
633 | ||
634 | option_workaround_close_loop_end, | |
635 | option_no_workaround_close_loop_end, | |
636 | ||
637 | option_no_workarounds, | |
638 | ||
e0001a05 | 639 | option_rename_section_name, |
e0001a05 | 640 | |
43cd72b9 BW |
641 | option_prefer_l32r, |
642 | option_prefer_const16, | |
643 | ||
644 | option_target_hardware | |
e0001a05 NC |
645 | }; |
646 | ||
647 | const char *md_shortopts = ""; | |
648 | ||
649 | struct option md_longopts[] = | |
650 | { | |
43cd72b9 BW |
651 | { "density", no_argument, NULL, option_density }, |
652 | { "no-density", no_argument, NULL, option_no_density }, | |
653 | ||
654 | /* Both "relax" and "generics" are deprecated and treated as equivalent | |
655 | to the "transform" option. */ | |
656 | { "relax", no_argument, NULL, option_relax }, | |
657 | { "no-relax", no_argument, NULL, option_no_relax }, | |
658 | { "generics", no_argument, NULL, option_generics }, | |
659 | { "no-generics", no_argument, NULL, option_no_generics }, | |
660 | ||
661 | { "transform", no_argument, NULL, option_transform }, | |
662 | { "no-transform", no_argument, NULL, option_no_transform }, | |
663 | { "text-section-literals", no_argument, NULL, option_text_section_literals }, | |
664 | { "no-text-section-literals", no_argument, NULL, | |
665 | option_no_text_section_literals }, | |
666 | { "absolute-literals", no_argument, NULL, option_absolute_literals }, | |
667 | { "no-absolute-literals", no_argument, NULL, option_no_absolute_literals }, | |
e0001a05 NC |
668 | /* This option was changed from -align-target to -target-align |
669 | because it conflicted with the "-al" option. */ | |
43cd72b9 | 670 | { "target-align", no_argument, NULL, option_align_targets }, |
7fa3d080 BW |
671 | { "no-target-align", no_argument, NULL, option_no_align_targets }, |
672 | { "warn-unaligned-targets", no_argument, NULL, | |
673 | option_warn_unaligned_targets }, | |
43cd72b9 BW |
674 | { "longcalls", no_argument, NULL, option_longcalls }, |
675 | { "no-longcalls", no_argument, NULL, option_no_longcalls }, | |
676 | ||
677 | { "no-workaround-a0-b-retw", no_argument, NULL, | |
678 | option_no_workaround_a0_b_retw }, | |
679 | { "workaround-a0-b-retw", no_argument, NULL, option_workaround_a0_b_retw }, | |
e0001a05 | 680 | |
43cd72b9 BW |
681 | { "no-workaround-b-j-loop-end", no_argument, NULL, |
682 | option_no_workaround_b_j_loop_end }, | |
683 | { "workaround-b-j-loop-end", no_argument, NULL, | |
684 | option_workaround_b_j_loop_end }, | |
e0001a05 | 685 | |
43cd72b9 BW |
686 | { "no-workaround-short-loops", no_argument, NULL, |
687 | option_no_workaround_short_loop }, | |
7fa3d080 BW |
688 | { "workaround-short-loops", no_argument, NULL, |
689 | option_workaround_short_loop }, | |
e0001a05 | 690 | |
43cd72b9 BW |
691 | { "no-workaround-all-short-loops", no_argument, NULL, |
692 | option_no_workaround_all_short_loops }, | |
693 | { "workaround-all-short-loop", no_argument, NULL, | |
694 | option_workaround_all_short_loops }, | |
695 | ||
696 | { "prefer-l32r", no_argument, NULL, option_prefer_l32r }, | |
697 | { "prefer-const16", no_argument, NULL, option_prefer_const16 }, | |
698 | ||
699 | { "no-workarounds", no_argument, NULL, option_no_workarounds }, | |
700 | ||
701 | { "no-workaround-close-loop-end", no_argument, NULL, | |
702 | option_no_workaround_close_loop_end }, | |
703 | { "workaround-close-loop-end", no_argument, NULL, | |
704 | option_workaround_close_loop_end }, | |
e0001a05 | 705 | |
7fa3d080 | 706 | { "rename-section", required_argument, NULL, option_rename_section_name }, |
e0001a05 | 707 | |
43cd72b9 BW |
708 | { "link-relax", no_argument, NULL, option_link_relax }, |
709 | { "no-link-relax", no_argument, NULL, option_no_link_relax }, | |
710 | ||
711 | { "target-hardware", required_argument, NULL, option_target_hardware }, | |
712 | ||
713 | { NULL, no_argument, NULL, 0 } | |
e0001a05 NC |
714 | }; |
715 | ||
716 | size_t md_longopts_size = sizeof md_longopts; | |
717 | ||
718 | ||
719 | int | |
7fa3d080 | 720 | md_parse_option (int c, char *arg) |
e0001a05 NC |
721 | { |
722 | switch (c) | |
723 | { | |
724 | case option_density: | |
43cd72b9 | 725 | as_warn (_("--density option is ignored")); |
e0001a05 NC |
726 | return 1; |
727 | case option_no_density: | |
43cd72b9 | 728 | as_warn (_("--no-density option is ignored")); |
e0001a05 | 729 | return 1; |
43cd72b9 BW |
730 | case option_link_relax: |
731 | linkrelax = 1; | |
e0001a05 | 732 | return 1; |
43cd72b9 BW |
733 | case option_no_link_relax: |
734 | linkrelax = 0; | |
e0001a05 | 735 | return 1; |
43cd72b9 BW |
736 | case option_generics: |
737 | as_warn (_("--generics is deprecated; use --transform instead")); | |
738 | return md_parse_option (option_transform, arg); | |
739 | case option_no_generics: | |
740 | as_warn (_("--no-generics is deprecated; use --no-transform instead")); | |
741 | return md_parse_option (option_no_transform, arg); | |
742 | case option_relax: | |
743 | as_warn (_("--relax is deprecated; use --transform instead")); | |
744 | return md_parse_option (option_transform, arg); | |
745 | case option_no_relax: | |
746 | as_warn (_("--no-relax is deprecated; use --no-transform instead")); | |
747 | return md_parse_option (option_no_transform, arg); | |
e0001a05 NC |
748 | case option_longcalls: |
749 | directive_state[directive_longcalls] = TRUE; | |
750 | return 1; | |
751 | case option_no_longcalls: | |
752 | directive_state[directive_longcalls] = FALSE; | |
753 | return 1; | |
754 | case option_text_section_literals: | |
755 | use_literal_section = FALSE; | |
756 | return 1; | |
757 | case option_no_text_section_literals: | |
758 | use_literal_section = TRUE; | |
759 | return 1; | |
43cd72b9 BW |
760 | case option_absolute_literals: |
761 | if (!absolute_literals_supported) | |
762 | { | |
763 | as_fatal (_("--absolute-literals option not supported in this Xtensa configuration")); | |
764 | return 0; | |
765 | } | |
766 | directive_state[directive_absolute_literals] = TRUE; | |
767 | return 1; | |
768 | case option_no_absolute_literals: | |
769 | directive_state[directive_absolute_literals] = FALSE; | |
770 | return 1; | |
771 | ||
e0001a05 NC |
772 | case option_workaround_a0_b_retw: |
773 | workaround_a0_b_retw = TRUE; | |
e0001a05 NC |
774 | return 1; |
775 | case option_no_workaround_a0_b_retw: | |
776 | workaround_a0_b_retw = FALSE; | |
e0001a05 NC |
777 | return 1; |
778 | case option_workaround_b_j_loop_end: | |
779 | workaround_b_j_loop_end = TRUE; | |
e0001a05 NC |
780 | return 1; |
781 | case option_no_workaround_b_j_loop_end: | |
782 | workaround_b_j_loop_end = FALSE; | |
e0001a05 NC |
783 | return 1; |
784 | ||
785 | case option_workaround_short_loop: | |
786 | workaround_short_loop = TRUE; | |
e0001a05 NC |
787 | return 1; |
788 | case option_no_workaround_short_loop: | |
789 | workaround_short_loop = FALSE; | |
e0001a05 NC |
790 | return 1; |
791 | ||
792 | case option_workaround_all_short_loops: | |
793 | workaround_all_short_loops = TRUE; | |
e0001a05 NC |
794 | return 1; |
795 | case option_no_workaround_all_short_loops: | |
796 | workaround_all_short_loops = FALSE; | |
e0001a05 NC |
797 | return 1; |
798 | ||
799 | case option_workaround_close_loop_end: | |
800 | workaround_close_loop_end = TRUE; | |
e0001a05 NC |
801 | return 1; |
802 | case option_no_workaround_close_loop_end: | |
803 | workaround_close_loop_end = FALSE; | |
e0001a05 NC |
804 | return 1; |
805 | ||
806 | case option_no_workarounds: | |
807 | workaround_a0_b_retw = FALSE; | |
e0001a05 | 808 | workaround_b_j_loop_end = FALSE; |
e0001a05 | 809 | workaround_short_loop = FALSE; |
e0001a05 | 810 | workaround_all_short_loops = FALSE; |
e0001a05 | 811 | workaround_close_loop_end = FALSE; |
e0001a05 | 812 | return 1; |
43cd72b9 | 813 | |
e0001a05 NC |
814 | case option_align_targets: |
815 | align_targets = TRUE; | |
816 | return 1; | |
817 | case option_no_align_targets: | |
818 | align_targets = FALSE; | |
819 | return 1; | |
820 | ||
43cd72b9 BW |
821 | case option_warn_unaligned_targets: |
822 | warn_unaligned_branch_targets = TRUE; | |
e0001a05 NC |
823 | return 1; |
824 | ||
e0001a05 NC |
825 | case option_rename_section_name: |
826 | build_section_rename (arg); | |
827 | return 1; | |
e0001a05 NC |
828 | |
829 | case 'Q': | |
830 | /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section | |
831 | should be emitted or not. FIXME: Not implemented. */ | |
832 | return 1; | |
c138bc38 | 833 | |
43cd72b9 BW |
834 | case option_prefer_l32r: |
835 | if (prefer_const16) | |
836 | as_fatal (_("prefer-l32r conflicts with prefer-const16")); | |
837 | prefer_l32r = 1; | |
838 | return 1; | |
839 | ||
840 | case option_prefer_const16: | |
841 | if (prefer_l32r) | |
842 | as_fatal (_("prefer-const16 conflicts with prefer-l32r")); | |
843 | prefer_const16 = 1; | |
844 | return 1; | |
845 | ||
c138bc38 | 846 | case option_target_hardware: |
43cd72b9 BW |
847 | { |
848 | int earliest, latest = 0; | |
849 | if (*arg == 0 || *arg == '-') | |
850 | as_fatal (_("invalid target hardware version")); | |
851 | ||
852 | earliest = strtol (arg, &arg, 0); | |
853 | ||
854 | if (*arg == 0) | |
855 | latest = earliest; | |
856 | else if (*arg == '-') | |
857 | { | |
858 | if (*++arg == 0) | |
859 | as_fatal (_("invalid target hardware version")); | |
860 | latest = strtol (arg, &arg, 0); | |
861 | } | |
862 | if (*arg != 0) | |
863 | as_fatal (_("invalid target hardware version")); | |
864 | ||
865 | xtensa_setup_hw_workarounds (earliest, latest); | |
866 | return 1; | |
867 | } | |
868 | ||
869 | case option_transform: | |
870 | /* This option has no affect other than to use the defaults, | |
871 | which are already set. */ | |
872 | return 1; | |
873 | ||
874 | case option_no_transform: | |
875 | /* This option turns off all transformations of any kind. | |
876 | However, because we want to preserve the state of other | |
877 | directives, we only change its own field. Thus, before | |
878 | you perform any transformation, always check if transform | |
879 | is available. If you use the functions we provide for this | |
880 | purpose, you will be ok. */ | |
881 | directive_state[directive_transform] = FALSE; | |
882 | return 1; | |
883 | ||
e0001a05 NC |
884 | default: |
885 | return 0; | |
886 | } | |
887 | } | |
888 | ||
889 | ||
890 | void | |
7fa3d080 | 891 | md_show_usage (FILE *stream) |
e0001a05 | 892 | { |
43cd72b9 BW |
893 | fputs ("\n\ |
894 | Xtensa options:\n\ | |
9456465c BW |
895 | --[no-]text-section-literals\n\ |
896 | [Do not] put literals in the text section\n\ | |
897 | --[no-]absolute-literals\n\ | |
898 | [Do not] default to use non-PC-relative literals\n\ | |
899 | --[no-]target-align [Do not] try to align branch targets\n\ | |
900 | --[no-]longcalls [Do not] emit 32-bit call sequences\n\ | |
901 | --[no-]transform [Do not] transform instructions\n\ | |
902 | --rename-section old=new Rename section 'old' to 'new'\n", stream); | |
e0001a05 NC |
903 | } |
904 | ||
7fa3d080 BW |
905 | \f |
906 | /* Functions related to the list of current label symbols. */ | |
43cd72b9 BW |
907 | |
908 | static void | |
7fa3d080 | 909 | xtensa_add_insn_label (symbolS *sym) |
43cd72b9 | 910 | { |
7fa3d080 | 911 | sym_list *l; |
43cd72b9 | 912 | |
7fa3d080 BW |
913 | if (!free_insn_labels) |
914 | l = (sym_list *) xmalloc (sizeof (sym_list)); | |
915 | else | |
43cd72b9 | 916 | { |
7fa3d080 BW |
917 | l = free_insn_labels; |
918 | free_insn_labels = l->next; | |
919 | } | |
920 | ||
921 | l->sym = sym; | |
922 | l->next = insn_labels; | |
923 | insn_labels = l; | |
924 | } | |
925 | ||
926 | ||
927 | static void | |
928 | xtensa_clear_insn_labels (void) | |
929 | { | |
930 | sym_list **pl; | |
931 | ||
932 | for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next) | |
933 | ; | |
934 | *pl = insn_labels; | |
935 | insn_labels = NULL; | |
936 | } | |
937 | ||
938 | ||
c138bc38 BW |
939 | /* The "loops_ok" argument is provided to allow ignoring labels that |
940 | define loop ends. This fixes a bug where the NOPs to align a | |
7fa3d080 BW |
941 | loop opcode were included in a previous zero-cost loop: |
942 | ||
943 | loop a0, loopend | |
944 | <loop1 body> | |
945 | loopend: | |
946 | ||
947 | loop a2, loopend2 | |
948 | <loop2 body> | |
949 | ||
950 | would become: | |
951 | ||
952 | loop a0, loopend | |
953 | <loop1 body> | |
954 | nop.n <===== bad! | |
955 | loopend: | |
956 | ||
957 | loop a2, loopend2 | |
958 | <loop2 body> | |
959 | ||
960 | This argument is used to prevent moving the NOP to before the | |
961 | loop-end label, which is what you want in this special case. */ | |
962 | ||
963 | static void | |
964 | xtensa_move_labels (fragS *new_frag, valueT new_offset, bfd_boolean loops_ok) | |
965 | { | |
966 | sym_list *lit; | |
967 | ||
968 | for (lit = insn_labels; lit; lit = lit->next) | |
969 | { | |
970 | symbolS *lit_sym = lit->sym; | |
971 | if (loops_ok || ! symbol_get_tc (lit_sym)->is_loop_target) | |
972 | { | |
973 | S_SET_VALUE (lit_sym, new_offset); | |
974 | symbol_set_frag (lit_sym, new_frag); | |
975 | } | |
43cd72b9 BW |
976 | } |
977 | } | |
978 | ||
e0001a05 NC |
979 | \f |
980 | /* Directive data and functions. */ | |
981 | ||
982 | typedef struct state_stackS_struct | |
983 | { | |
984 | directiveE directive; | |
985 | bfd_boolean negated; | |
986 | bfd_boolean old_state; | |
987 | const char *file; | |
988 | unsigned int line; | |
989 | const void *datum; | |
990 | struct state_stackS_struct *prev; | |
991 | } state_stackS; | |
992 | ||
993 | state_stackS *directive_state_stack; | |
994 | ||
995 | const pseudo_typeS md_pseudo_table[] = | |
996 | { | |
43cd72b9 BW |
997 | { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0). */ |
998 | { "literal_position", xtensa_literal_position, 0 }, | |
999 | { "frame", s_ignore, 0 }, /* Formerly used for STABS debugging. */ | |
1000 | { "long", xtensa_elf_cons, 4 }, | |
1001 | { "word", xtensa_elf_cons, 4 }, | |
1002 | { "short", xtensa_elf_cons, 2 }, | |
1003 | { "begin", xtensa_begin_directive, 0 }, | |
1004 | { "end", xtensa_end_directive, 0 }, | |
43cd72b9 BW |
1005 | { "literal", xtensa_literal_pseudo, 0 }, |
1006 | { "frequency", xtensa_frequency_pseudo, 0 }, | |
1007 | { NULL, 0, 0 }, | |
e0001a05 NC |
1008 | }; |
1009 | ||
1010 | ||
7fa3d080 BW |
1011 | static bfd_boolean |
1012 | use_transform (void) | |
e0001a05 | 1013 | { |
43cd72b9 BW |
1014 | /* After md_end, you should be checking frag by frag, rather |
1015 | than state directives. */ | |
1016 | assert (!past_xtensa_end); | |
1017 | return directive_state[directive_transform]; | |
e0001a05 NC |
1018 | } |
1019 | ||
1020 | ||
7fa3d080 BW |
1021 | static bfd_boolean |
1022 | do_align_targets (void) | |
e0001a05 | 1023 | { |
7b1cc377 BW |
1024 | /* Do not use this function after md_end; just look at align_targets |
1025 | instead. There is no target-align directive, so alignment is either | |
1026 | enabled for all frags or not done at all. */ | |
43cd72b9 BW |
1027 | assert (!past_xtensa_end); |
1028 | return align_targets && use_transform (); | |
e0001a05 NC |
1029 | } |
1030 | ||
1031 | ||
1032 | static void | |
7fa3d080 | 1033 | directive_push (directiveE directive, bfd_boolean negated, const void *datum) |
e0001a05 NC |
1034 | { |
1035 | char *file; | |
1036 | unsigned int line; | |
1037 | state_stackS *stack = (state_stackS *) xmalloc (sizeof (state_stackS)); | |
1038 | ||
1039 | as_where (&file, &line); | |
1040 | ||
1041 | stack->directive = directive; | |
1042 | stack->negated = negated; | |
1043 | stack->old_state = directive_state[directive]; | |
1044 | stack->file = file; | |
1045 | stack->line = line; | |
1046 | stack->datum = datum; | |
1047 | stack->prev = directive_state_stack; | |
1048 | directive_state_stack = stack; | |
1049 | ||
1050 | directive_state[directive] = !negated; | |
1051 | } | |
1052 | ||
7fa3d080 | 1053 | |
e0001a05 | 1054 | static void |
7fa3d080 BW |
1055 | directive_pop (directiveE *directive, |
1056 | bfd_boolean *negated, | |
1057 | const char **file, | |
1058 | unsigned int *line, | |
1059 | const void **datum) | |
e0001a05 NC |
1060 | { |
1061 | state_stackS *top = directive_state_stack; | |
1062 | ||
1063 | if (!directive_state_stack) | |
1064 | { | |
1065 | as_bad (_("unmatched end directive")); | |
1066 | *directive = directive_none; | |
1067 | return; | |
1068 | } | |
1069 | ||
1070 | directive_state[directive_state_stack->directive] = top->old_state; | |
1071 | *directive = top->directive; | |
1072 | *negated = top->negated; | |
1073 | *file = top->file; | |
1074 | *line = top->line; | |
1075 | *datum = top->datum; | |
1076 | directive_state_stack = top->prev; | |
1077 | free (top); | |
1078 | } | |
1079 | ||
1080 | ||
1081 | static void | |
7fa3d080 | 1082 | directive_balance (void) |
e0001a05 NC |
1083 | { |
1084 | while (directive_state_stack) | |
1085 | { | |
1086 | directiveE directive; | |
1087 | bfd_boolean negated; | |
1088 | const char *file; | |
1089 | unsigned int line; | |
1090 | const void *datum; | |
1091 | ||
1092 | directive_pop (&directive, &negated, &file, &line, &datum); | |
1093 | as_warn_where ((char *) file, line, | |
1094 | _(".begin directive with no matching .end directive")); | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | ||
1099 | static bfd_boolean | |
7fa3d080 | 1100 | inside_directive (directiveE dir) |
e0001a05 NC |
1101 | { |
1102 | state_stackS *top = directive_state_stack; | |
1103 | ||
1104 | while (top && top->directive != dir) | |
1105 | top = top->prev; | |
1106 | ||
1107 | return (top != NULL); | |
1108 | } | |
1109 | ||
1110 | ||
1111 | static void | |
7fa3d080 | 1112 | get_directive (directiveE *directive, bfd_boolean *negated) |
e0001a05 NC |
1113 | { |
1114 | int len; | |
1115 | unsigned i; | |
43cd72b9 | 1116 | char *directive_string; |
e0001a05 NC |
1117 | |
1118 | if (strncmp (input_line_pointer, "no-", 3) != 0) | |
1119 | *negated = FALSE; | |
1120 | else | |
1121 | { | |
1122 | *negated = TRUE; | |
1123 | input_line_pointer += 3; | |
1124 | } | |
1125 | ||
1126 | len = strspn (input_line_pointer, | |
43cd72b9 BW |
1127 | "abcdefghijklmnopqrstuvwxyz_-/0123456789."); |
1128 | ||
1129 | /* This code is a hack to make .begin [no-][generics|relax] exactly | |
1130 | equivalent to .begin [no-]transform. We should remove it when | |
1131 | we stop accepting those options. */ | |
c138bc38 | 1132 | |
43cd72b9 BW |
1133 | if (strncmp (input_line_pointer, "generics", strlen ("generics")) == 0) |
1134 | { | |
1135 | as_warn (_("[no-]generics is deprecated; use [no-]transform instead")); | |
1136 | directive_string = "transform"; | |
1137 | } | |
1138 | else if (strncmp (input_line_pointer, "relax", strlen ("relax")) == 0) | |
1139 | { | |
1140 | as_warn (_("[no-]relax is deprecated; use [no-]transform instead")); | |
1141 | directive_string = "transform"; | |
c138bc38 | 1142 | } |
43cd72b9 BW |
1143 | else |
1144 | directive_string = input_line_pointer; | |
e0001a05 NC |
1145 | |
1146 | for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i) | |
1147 | { | |
43cd72b9 | 1148 | if (strncmp (directive_string, directive_info[i].name, len) == 0) |
e0001a05 NC |
1149 | { |
1150 | input_line_pointer += len; | |
1151 | *directive = (directiveE) i; | |
1152 | if (*negated && !directive_info[i].can_be_negated) | |
43cd72b9 | 1153 | as_bad (_("directive %s cannot be negated"), |
e0001a05 NC |
1154 | directive_info[i].name); |
1155 | return; | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | as_bad (_("unknown directive")); | |
1160 | *directive = (directiveE) XTENSA_UNDEFINED; | |
1161 | } | |
1162 | ||
1163 | ||
1164 | static void | |
7fa3d080 | 1165 | xtensa_begin_directive (int ignore ATTRIBUTE_UNUSED) |
e0001a05 NC |
1166 | { |
1167 | directiveE directive; | |
1168 | bfd_boolean negated; | |
1169 | emit_state *state; | |
e0001a05 NC |
1170 | lit_state *ls; |
1171 | ||
1172 | get_directive (&directive, &negated); | |
1173 | if (directive == (directiveE) XTENSA_UNDEFINED) | |
1174 | { | |
1175 | discard_rest_of_line (); | |
1176 | return; | |
1177 | } | |
1178 | ||
43cd72b9 BW |
1179 | if (cur_vinsn.inside_bundle) |
1180 | as_bad (_("directives are not valid inside bundles")); | |
1181 | ||
e0001a05 NC |
1182 | switch (directive) |
1183 | { | |
1184 | case directive_literal: | |
82e7541d BW |
1185 | if (!inside_directive (directive_literal)) |
1186 | { | |
1187 | /* Previous labels go with whatever follows this directive, not with | |
1188 | the literal, so save them now. */ | |
1189 | saved_insn_labels = insn_labels; | |
1190 | insn_labels = NULL; | |
1191 | } | |
43cd72b9 | 1192 | as_warn (_(".begin literal is deprecated; use .literal instead")); |
e0001a05 NC |
1193 | state = (emit_state *) xmalloc (sizeof (emit_state)); |
1194 | xtensa_switch_to_literal_fragment (state); | |
1195 | directive_push (directive_literal, negated, state); | |
1196 | break; | |
1197 | ||
1198 | case directive_literal_prefix: | |
c138bc38 | 1199 | /* Have to flush pending output because a movi relaxed to an l32r |
43cd72b9 BW |
1200 | might produce a literal. */ |
1201 | md_flush_pending_output (); | |
e0001a05 NC |
1202 | /* Check to see if the current fragment is a literal |
1203 | fragment. If it is, then this operation is not allowed. */ | |
43cd72b9 | 1204 | if (generating_literals) |
e0001a05 NC |
1205 | { |
1206 | as_bad (_("cannot set literal_prefix inside literal fragment")); | |
1207 | return; | |
1208 | } | |
1209 | ||
1210 | /* Allocate the literal state for this section and push | |
1211 | onto the directive stack. */ | |
1212 | ls = xmalloc (sizeof (lit_state)); | |
1213 | assert (ls); | |
1214 | ||
1215 | *ls = default_lit_sections; | |
e0001a05 NC |
1216 | directive_push (directive_literal_prefix, negated, ls); |
1217 | ||
e0001a05 | 1218 | /* Process the new prefix. */ |
74869ac7 | 1219 | xtensa_literal_prefix (); |
e0001a05 NC |
1220 | break; |
1221 | ||
1222 | case directive_freeregs: | |
1223 | /* This information is currently unused, but we'll accept the statement | |
1224 | and just discard the rest of the line. This won't check the syntax, | |
1225 | but it will accept every correct freeregs directive. */ | |
1226 | input_line_pointer += strcspn (input_line_pointer, "\n"); | |
1227 | directive_push (directive_freeregs, negated, 0); | |
1228 | break; | |
1229 | ||
43cd72b9 BW |
1230 | case directive_schedule: |
1231 | md_flush_pending_output (); | |
1232 | frag_var (rs_fill, 0, 0, frag_now->fr_subtype, | |
1233 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
1234 | directive_push (directive_schedule, negated, 0); | |
1235 | xtensa_set_frag_assembly_state (frag_now); | |
1236 | break; | |
1237 | ||
e0001a05 | 1238 | case directive_density: |
43cd72b9 BW |
1239 | as_warn (_(".begin [no-]density is ignored")); |
1240 | break; | |
1241 | ||
1242 | case directive_absolute_literals: | |
1243 | md_flush_pending_output (); | |
1244 | if (!absolute_literals_supported && !negated) | |
e0001a05 | 1245 | { |
43cd72b9 | 1246 | as_warn (_("Xtensa absolute literals option not supported; ignored")); |
e0001a05 NC |
1247 | break; |
1248 | } | |
43cd72b9 BW |
1249 | xtensa_set_frag_assembly_state (frag_now); |
1250 | directive_push (directive, negated, 0); | |
1251 | break; | |
e0001a05 NC |
1252 | |
1253 | default: | |
43cd72b9 BW |
1254 | md_flush_pending_output (); |
1255 | xtensa_set_frag_assembly_state (frag_now); | |
e0001a05 NC |
1256 | directive_push (directive, negated, 0); |
1257 | break; | |
1258 | } | |
1259 | ||
1260 | demand_empty_rest_of_line (); | |
1261 | } | |
1262 | ||
1263 | ||
1264 | static void | |
7fa3d080 | 1265 | xtensa_end_directive (int ignore ATTRIBUTE_UNUSED) |
e0001a05 NC |
1266 | { |
1267 | directiveE begin_directive, end_directive; | |
1268 | bfd_boolean begin_negated, end_negated; | |
1269 | const char *file; | |
1270 | unsigned int line; | |
1271 | emit_state *state; | |
43cd72b9 | 1272 | emit_state **state_ptr; |
e0001a05 NC |
1273 | lit_state *s; |
1274 | ||
43cd72b9 BW |
1275 | if (cur_vinsn.inside_bundle) |
1276 | as_bad (_("directives are not valid inside bundles")); | |
82e7541d | 1277 | |
e0001a05 | 1278 | get_directive (&end_directive, &end_negated); |
43cd72b9 BW |
1279 | |
1280 | md_flush_pending_output (); | |
1281 | ||
1282 | switch (end_directive) | |
e0001a05 | 1283 | { |
43cd72b9 | 1284 | case (directiveE) XTENSA_UNDEFINED: |
e0001a05 NC |
1285 | discard_rest_of_line (); |
1286 | return; | |
e0001a05 | 1287 | |
43cd72b9 BW |
1288 | case directive_density: |
1289 | as_warn (_(".end [no-]density is ignored")); | |
e0001a05 | 1290 | demand_empty_rest_of_line (); |
43cd72b9 BW |
1291 | break; |
1292 | ||
1293 | case directive_absolute_literals: | |
1294 | if (!absolute_literals_supported && !end_negated) | |
1295 | { | |
1296 | as_warn (_("Xtensa absolute literals option not supported; ignored")); | |
1297 | demand_empty_rest_of_line (); | |
1298 | return; | |
1299 | } | |
1300 | break; | |
1301 | ||
1302 | default: | |
1303 | break; | |
e0001a05 NC |
1304 | } |
1305 | ||
43cd72b9 | 1306 | state_ptr = &state; /* use state_ptr to avoid type-punning warning */ |
e0001a05 | 1307 | directive_pop (&begin_directive, &begin_negated, &file, &line, |
43cd72b9 | 1308 | (const void **) state_ptr); |
e0001a05 NC |
1309 | |
1310 | if (begin_directive != directive_none) | |
1311 | { | |
1312 | if (begin_directive != end_directive || begin_negated != end_negated) | |
1313 | { | |
1314 | as_bad (_("does not match begin %s%s at %s:%d"), | |
1315 | begin_negated ? "no-" : "", | |
1316 | directive_info[begin_directive].name, file, line); | |
1317 | } | |
1318 | else | |
1319 | { | |
1320 | switch (end_directive) | |
1321 | { | |
1322 | case directive_literal: | |
1323 | frag_var (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
1324 | xtensa_restore_emit_state (state); | |
43cd72b9 | 1325 | xtensa_set_frag_assembly_state (frag_now); |
e0001a05 | 1326 | free (state); |
82e7541d BW |
1327 | if (!inside_directive (directive_literal)) |
1328 | { | |
1329 | /* Restore the list of current labels. */ | |
1330 | xtensa_clear_insn_labels (); | |
1331 | insn_labels = saved_insn_labels; | |
1332 | } | |
e0001a05 NC |
1333 | break; |
1334 | ||
e0001a05 NC |
1335 | case directive_literal_prefix: |
1336 | /* Restore the default collection sections from saved state. */ | |
1337 | s = (lit_state *) state; | |
1338 | assert (s); | |
e8247da7 | 1339 | default_lit_sections = *s; |
e0001a05 | 1340 | |
74869ac7 BW |
1341 | /* Free the state storage. */ |
1342 | free (s->lit_prefix); | |
e0001a05 NC |
1343 | free (s); |
1344 | break; | |
1345 | ||
43cd72b9 BW |
1346 | case directive_schedule: |
1347 | case directive_freeregs: | |
1348 | break; | |
1349 | ||
e0001a05 | 1350 | default: |
43cd72b9 | 1351 | xtensa_set_frag_assembly_state (frag_now); |
e0001a05 NC |
1352 | break; |
1353 | } | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | demand_empty_rest_of_line (); | |
1358 | } | |
1359 | ||
1360 | ||
1361 | /* Place an aligned literal fragment at the current location. */ | |
1362 | ||
1363 | static void | |
7fa3d080 | 1364 | xtensa_literal_position (int ignore ATTRIBUTE_UNUSED) |
e0001a05 | 1365 | { |
43cd72b9 BW |
1366 | md_flush_pending_output (); |
1367 | ||
e0001a05 NC |
1368 | if (inside_directive (directive_literal)) |
1369 | as_warn (_(".literal_position inside literal directive; ignoring")); | |
43cd72b9 | 1370 | xtensa_mark_literal_pool_location (); |
e0001a05 NC |
1371 | |
1372 | demand_empty_rest_of_line (); | |
82e7541d | 1373 | xtensa_clear_insn_labels (); |
e0001a05 NC |
1374 | } |
1375 | ||
1376 | ||
43cd72b9 | 1377 | /* Support .literal label, expr, ... */ |
e0001a05 NC |
1378 | |
1379 | static void | |
7fa3d080 | 1380 | xtensa_literal_pseudo (int ignored ATTRIBUTE_UNUSED) |
e0001a05 NC |
1381 | { |
1382 | emit_state state; | |
1745fcba | 1383 | char *p, *base_name; |
e0001a05 | 1384 | char c; |
e0001a05 NC |
1385 | segT dest_seg; |
1386 | ||
82e7541d BW |
1387 | if (inside_directive (directive_literal)) |
1388 | { | |
1389 | as_bad (_(".literal not allowed inside .begin literal region")); | |
1390 | ignore_rest_of_line (); | |
1391 | return; | |
1392 | } | |
1393 | ||
43cd72b9 BW |
1394 | md_flush_pending_output (); |
1395 | ||
82e7541d BW |
1396 | /* Previous labels go with whatever follows this directive, not with |
1397 | the literal, so save them now. */ | |
1398 | saved_insn_labels = insn_labels; | |
1399 | insn_labels = NULL; | |
1400 | ||
e0001a05 NC |
1401 | /* If we are using text-section literals, then this is the right value... */ |
1402 | dest_seg = now_seg; | |
1403 | ||
1404 | base_name = input_line_pointer; | |
1405 | ||
1406 | xtensa_switch_to_literal_fragment (&state); | |
1407 | ||
43cd72b9 | 1408 | /* ...but if we aren't using text-section-literals, then we |
e0001a05 | 1409 | need to put them in the section we just switched to. */ |
43cd72b9 | 1410 | if (use_literal_section || directive_state[directive_absolute_literals]) |
e0001a05 NC |
1411 | dest_seg = now_seg; |
1412 | ||
43cd72b9 BW |
1413 | /* All literals are aligned to four-byte boundaries. */ |
1414 | frag_align (2, 0, 0); | |
1415 | record_alignment (now_seg, 2); | |
e0001a05 NC |
1416 | |
1417 | c = get_symbol_end (); | |
1418 | /* Just after name is now '\0'. */ | |
1419 | p = input_line_pointer; | |
1420 | *p = c; | |
1421 | SKIP_WHITESPACE (); | |
1422 | ||
1423 | if (*input_line_pointer != ',' && *input_line_pointer != ':') | |
1424 | { | |
1425 | as_bad (_("expected comma or colon after symbol name; " | |
1426 | "rest of line ignored")); | |
1427 | ignore_rest_of_line (); | |
1428 | xtensa_restore_emit_state (&state); | |
1429 | return; | |
1430 | } | |
1431 | *p = 0; | |
1432 | ||
e0001a05 | 1433 | colon (base_name); |
e0001a05 | 1434 | |
e0001a05 | 1435 | *p = c; |
43cd72b9 | 1436 | input_line_pointer++; /* skip ',' or ':' */ |
e0001a05 | 1437 | |
43cd72b9 | 1438 | xtensa_elf_cons (4); |
e0001a05 NC |
1439 | |
1440 | xtensa_restore_emit_state (&state); | |
82e7541d BW |
1441 | |
1442 | /* Restore the list of current labels. */ | |
1443 | xtensa_clear_insn_labels (); | |
1444 | insn_labels = saved_insn_labels; | |
e0001a05 NC |
1445 | } |
1446 | ||
1447 | ||
1448 | static void | |
74869ac7 | 1449 | xtensa_literal_prefix (void) |
e0001a05 | 1450 | { |
74869ac7 BW |
1451 | char *name; |
1452 | int len; | |
1453 | ||
1454 | /* Parse the new prefix from the input_line_pointer. */ | |
1455 | SKIP_WHITESPACE (); | |
1456 | len = strspn (input_line_pointer, | |
1457 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
1458 | "abcdefghijklmnopqrstuvwxyz_/0123456789.$"); | |
e0001a05 NC |
1459 | |
1460 | /* Get a null-terminated copy of the name. */ | |
1461 | name = xmalloc (len + 1); | |
1462 | assert (name); | |
74869ac7 | 1463 | strncpy (name, input_line_pointer, len); |
e0001a05 NC |
1464 | name[len] = 0; |
1465 | ||
74869ac7 BW |
1466 | /* Skip the name in the input line. */ |
1467 | input_line_pointer += len; | |
43cd72b9 | 1468 | |
74869ac7 | 1469 | default_lit_sections.lit_prefix = name; |
43cd72b9 | 1470 | |
74869ac7 | 1471 | /* Clear cached literal sections, since the prefix has changed. */ |
43cd72b9 BW |
1472 | default_lit_sections.lit_seg = NULL; |
1473 | default_lit_sections.lit4_seg = NULL; | |
43cd72b9 BW |
1474 | } |
1475 | ||
1476 | ||
1477 | /* Support ".frequency branch_target_frequency fall_through_frequency". */ | |
1478 | ||
1479 | static void | |
7fa3d080 | 1480 | xtensa_frequency_pseudo (int ignored ATTRIBUTE_UNUSED) |
43cd72b9 BW |
1481 | { |
1482 | float fall_through_f, target_f; | |
43cd72b9 BW |
1483 | |
1484 | fall_through_f = (float) strtod (input_line_pointer, &input_line_pointer); | |
1485 | if (fall_through_f < 0) | |
1486 | { | |
1487 | as_bad (_("fall through frequency must be greater than 0")); | |
1488 | ignore_rest_of_line (); | |
1489 | return; | |
1490 | } | |
1491 | ||
1492 | target_f = (float) strtod (input_line_pointer, &input_line_pointer); | |
1493 | if (target_f < 0) | |
1494 | { | |
1495 | as_bad (_("branch target frequency must be greater than 0")); | |
1496 | ignore_rest_of_line (); | |
1497 | return; | |
1498 | } | |
1499 | ||
b08b5071 | 1500 | set_subseg_freq (now_seg, now_subseg, target_f + fall_through_f, target_f); |
43cd72b9 BW |
1501 | |
1502 | demand_empty_rest_of_line (); | |
1503 | } | |
1504 | ||
1505 | ||
1506 | /* Like normal .long/.short/.word, except support @plt, etc. | |
1507 | Clobbers input_line_pointer, checks end-of-line. */ | |
1508 | ||
1509 | static void | |
7fa3d080 | 1510 | xtensa_elf_cons (int nbytes) |
43cd72b9 BW |
1511 | { |
1512 | expressionS exp; | |
1513 | bfd_reloc_code_real_type reloc; | |
1514 | ||
1515 | md_flush_pending_output (); | |
1516 | ||
1517 | if (cur_vinsn.inside_bundle) | |
1518 | as_bad (_("directives are not valid inside bundles")); | |
1519 | ||
1520 | if (is_it_end_of_statement ()) | |
1521 | { | |
1522 | demand_empty_rest_of_line (); | |
1523 | return; | |
1524 | } | |
1525 | ||
1526 | do | |
1527 | { | |
1528 | expression (&exp); | |
1529 | if (exp.X_op == O_symbol | |
1530 | && *input_line_pointer == '@' | |
1531 | && ((reloc = xtensa_elf_suffix (&input_line_pointer, &exp)) | |
1532 | != BFD_RELOC_NONE)) | |
1533 | { | |
1534 | reloc_howto_type *reloc_howto = | |
1535 | bfd_reloc_type_lookup (stdoutput, reloc); | |
1536 | ||
1537 | if (reloc == BFD_RELOC_UNUSED || !reloc_howto) | |
1538 | as_bad (_("unsupported relocation")); | |
1539 | else if ((reloc >= BFD_RELOC_XTENSA_SLOT0_OP | |
1540 | && reloc <= BFD_RELOC_XTENSA_SLOT14_OP) | |
1541 | || (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT | |
1542 | && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT)) | |
1543 | as_bad (_("opcode-specific %s relocation used outside " | |
1544 | "an instruction"), reloc_howto->name); | |
1545 | else if (nbytes != (int) bfd_get_reloc_size (reloc_howto)) | |
1546 | as_bad (_("%s relocations do not fit in %d bytes"), | |
1547 | reloc_howto->name, nbytes); | |
1548 | else | |
1549 | { | |
1550 | char *p = frag_more ((int) nbytes); | |
1551 | xtensa_set_frag_assembly_state (frag_now); | |
1552 | fix_new_exp (frag_now, p - frag_now->fr_literal, | |
1553 | nbytes, &exp, 0, reloc); | |
1554 | } | |
1555 | } | |
1556 | else | |
1557 | emit_expr (&exp, (unsigned int) nbytes); | |
1558 | } | |
1559 | while (*input_line_pointer++ == ','); | |
1560 | ||
1561 | input_line_pointer--; /* Put terminator back into stream. */ | |
1562 | demand_empty_rest_of_line (); | |
1563 | } | |
1564 | ||
7fa3d080 BW |
1565 | \f |
1566 | /* Parsing and Idiom Translation. */ | |
43cd72b9 BW |
1567 | |
1568 | /* Parse @plt, etc. and return the desired relocation. */ | |
1569 | static bfd_reloc_code_real_type | |
7fa3d080 | 1570 | xtensa_elf_suffix (char **str_p, expressionS *exp_p) |
43cd72b9 BW |
1571 | { |
1572 | struct map_bfd | |
1573 | { | |
1574 | char *string; | |
1575 | int length; | |
1576 | bfd_reloc_code_real_type reloc; | |
1577 | }; | |
1578 | ||
1579 | char ident[20]; | |
1580 | char *str = *str_p; | |
1581 | char *str2; | |
1582 | int ch; | |
1583 | int len; | |
1584 | struct map_bfd *ptr; | |
1585 | ||
1586 | #define MAP(str,reloc) { str, sizeof (str) - 1, reloc } | |
e0001a05 | 1587 | |
43cd72b9 BW |
1588 | static struct map_bfd mapping[] = |
1589 | { | |
1590 | MAP ("l", BFD_RELOC_LO16), | |
1591 | MAP ("h", BFD_RELOC_HI16), | |
1592 | MAP ("plt", BFD_RELOC_XTENSA_PLT), | |
1593 | { (char *) 0, 0, BFD_RELOC_UNUSED } | |
1594 | }; | |
1595 | ||
1596 | if (*str++ != '@') | |
1597 | return BFD_RELOC_NONE; | |
1598 | ||
1599 | for (ch = *str, str2 = ident; | |
1600 | (str2 < ident + sizeof (ident) - 1 | |
1601 | && (ISALNUM (ch) || ch == '@')); | |
1602 | ch = *++str) | |
1603 | { | |
1604 | *str2++ = (ISLOWER (ch)) ? ch : TOLOWER (ch); | |
1605 | } | |
1606 | ||
1607 | *str2 = '\0'; | |
1608 | len = str2 - ident; | |
1609 | ||
1610 | ch = ident[0]; | |
1611 | for (ptr = &mapping[0]; ptr->length > 0; ptr++) | |
1612 | if (ch == ptr->string[0] | |
1613 | && len == ptr->length | |
1614 | && memcmp (ident, ptr->string, ptr->length) == 0) | |
1615 | { | |
1616 | /* Now check for "identifier@suffix+constant". */ | |
1617 | if (*str == '-' || *str == '+') | |
1618 | { | |
1619 | char *orig_line = input_line_pointer; | |
1620 | expressionS new_exp; | |
1621 | ||
1622 | input_line_pointer = str; | |
1623 | expression (&new_exp); | |
1624 | if (new_exp.X_op == O_constant) | |
1625 | { | |
1626 | exp_p->X_add_number += new_exp.X_add_number; | |
1627 | str = input_line_pointer; | |
1628 | } | |
1629 | ||
1630 | if (&input_line_pointer != str_p) | |
1631 | input_line_pointer = orig_line; | |
1632 | } | |
1633 | ||
1634 | *str_p = str; | |
1635 | return ptr->reloc; | |
1636 | } | |
1637 | ||
1638 | return BFD_RELOC_UNUSED; | |
e0001a05 NC |
1639 | } |
1640 | ||
e0001a05 NC |
1641 | |
1642 | static const char * | |
7fa3d080 | 1643 | expression_end (const char *name) |
e0001a05 NC |
1644 | { |
1645 | while (1) | |
1646 | { | |
1647 | switch (*name) | |
1648 | { | |
43cd72b9 | 1649 | case '}': |
e0001a05 NC |
1650 | case ';': |
1651 | case '\0': | |
1652 | case ',': | |
43cd72b9 | 1653 | case ':': |
e0001a05 NC |
1654 | return name; |
1655 | case ' ': | |
1656 | case '\t': | |
1657 | ++name; | |
1658 | continue; | |
1659 | default: | |
1660 | return 0; | |
1661 | } | |
1662 | } | |
1663 | } | |
1664 | ||
1665 | ||
1666 | #define ERROR_REG_NUM ((unsigned) -1) | |
1667 | ||
1668 | static unsigned | |
7fa3d080 | 1669 | tc_get_register (const char *prefix) |
e0001a05 NC |
1670 | { |
1671 | unsigned reg; | |
1672 | const char *next_expr; | |
1673 | const char *old_line_pointer; | |
1674 | ||
1675 | SKIP_WHITESPACE (); | |
1676 | old_line_pointer = input_line_pointer; | |
1677 | ||
1678 | if (*input_line_pointer == '$') | |
1679 | ++input_line_pointer; | |
1680 | ||
1681 | /* Accept "sp" as a synonym for "a1". */ | |
1682 | if (input_line_pointer[0] == 's' && input_line_pointer[1] == 'p' | |
1683 | && expression_end (input_line_pointer + 2)) | |
1684 | { | |
1685 | input_line_pointer += 2; | |
1686 | return 1; /* AR[1] */ | |
1687 | } | |
1688 | ||
1689 | while (*input_line_pointer++ == *prefix++) | |
1690 | ; | |
1691 | --input_line_pointer; | |
1692 | --prefix; | |
1693 | ||
1694 | if (*prefix) | |
1695 | { | |
1696 | as_bad (_("bad register name: %s"), old_line_pointer); | |
1697 | return ERROR_REG_NUM; | |
1698 | } | |
1699 | ||
1700 | if (!ISDIGIT ((unsigned char) *input_line_pointer)) | |
1701 | { | |
1702 | as_bad (_("bad register number: %s"), input_line_pointer); | |
1703 | return ERROR_REG_NUM; | |
1704 | } | |
1705 | ||
1706 | reg = 0; | |
1707 | ||
1708 | while (ISDIGIT ((int) *input_line_pointer)) | |
1709 | reg = reg * 10 + *input_line_pointer++ - '0'; | |
1710 | ||
1711 | if (!(next_expr = expression_end (input_line_pointer))) | |
1712 | { | |
1713 | as_bad (_("bad register name: %s"), old_line_pointer); | |
1714 | return ERROR_REG_NUM; | |
1715 | } | |
1716 | ||
1717 | input_line_pointer = (char *) next_expr; | |
1718 | ||
1719 | return reg; | |
1720 | } | |
1721 | ||
1722 | ||
e0001a05 | 1723 | static void |
7fa3d080 | 1724 | expression_maybe_register (xtensa_opcode opc, int opnd, expressionS *tok) |
e0001a05 | 1725 | { |
43cd72b9 | 1726 | xtensa_isa isa = xtensa_default_isa; |
e0001a05 | 1727 | |
43cd72b9 BW |
1728 | /* Check if this is an immediate operand. */ |
1729 | if (xtensa_operand_is_register (isa, opc, opnd) == 0) | |
e0001a05 | 1730 | { |
43cd72b9 | 1731 | bfd_reloc_code_real_type reloc; |
e0001a05 | 1732 | segT t = expression (tok); |
43cd72b9 BW |
1733 | if (t == absolute_section |
1734 | && xtensa_operand_is_PCrelative (isa, opc, opnd) == 1) | |
e0001a05 NC |
1735 | { |
1736 | assert (tok->X_op == O_constant); | |
1737 | tok->X_op = O_symbol; | |
1738 | tok->X_add_symbol = &abs_symbol; | |
1739 | } | |
43cd72b9 BW |
1740 | |
1741 | if ((tok->X_op == O_constant || tok->X_op == O_symbol) | |
1742 | && (reloc = xtensa_elf_suffix (&input_line_pointer, tok)) | |
1743 | && (reloc != BFD_RELOC_NONE)) | |
e0001a05 | 1744 | { |
43cd72b9 BW |
1745 | switch (reloc) |
1746 | { | |
1747 | default: | |
1748 | case BFD_RELOC_UNUSED: | |
1749 | as_bad (_("unsupported relocation")); | |
1750 | break; | |
1751 | ||
1752 | case BFD_RELOC_XTENSA_PLT: | |
1753 | tok->X_op = O_pltrel; | |
1754 | break; | |
1755 | ||
1756 | case BFD_RELOC_LO16: | |
1757 | if (tok->X_op == O_constant) | |
1758 | tok->X_add_number &= 0xffff; | |
1759 | else | |
1760 | tok->X_op = O_lo16; | |
1761 | break; | |
1762 | ||
1763 | case BFD_RELOC_HI16: | |
1764 | if (tok->X_op == O_constant) | |
1765 | tok->X_add_number = ((unsigned) tok->X_add_number) >> 16; | |
1766 | else | |
1767 | tok->X_op = O_hi16; | |
1768 | break; | |
1769 | } | |
e0001a05 | 1770 | } |
e0001a05 NC |
1771 | } |
1772 | else | |
1773 | { | |
43cd72b9 BW |
1774 | xtensa_regfile opnd_rf = xtensa_operand_regfile (isa, opc, opnd); |
1775 | unsigned reg = tc_get_register (xtensa_regfile_shortname (isa, opnd_rf)); | |
e0001a05 NC |
1776 | |
1777 | if (reg != ERROR_REG_NUM) /* Already errored */ | |
1778 | { | |
1779 | uint32 buf = reg; | |
43cd72b9 | 1780 | if (xtensa_operand_encode (isa, opc, opnd, &buf)) |
e0001a05 NC |
1781 | as_bad (_("register number out of range")); |
1782 | } | |
1783 | ||
1784 | tok->X_op = O_register; | |
1785 | tok->X_add_symbol = 0; | |
1786 | tok->X_add_number = reg; | |
1787 | } | |
1788 | } | |
1789 | ||
1790 | ||
1791 | /* Split up the arguments for an opcode or pseudo-op. */ | |
1792 | ||
1793 | static int | |
7fa3d080 | 1794 | tokenize_arguments (char **args, char *str) |
e0001a05 NC |
1795 | { |
1796 | char *old_input_line_pointer; | |
1797 | bfd_boolean saw_comma = FALSE; | |
1798 | bfd_boolean saw_arg = FALSE; | |
43cd72b9 | 1799 | bfd_boolean saw_colon = FALSE; |
e0001a05 NC |
1800 | int num_args = 0; |
1801 | char *arg_end, *arg; | |
1802 | int arg_len; | |
43cd72b9 BW |
1803 | |
1804 | /* Save and restore input_line_pointer around this function. */ | |
e0001a05 NC |
1805 | old_input_line_pointer = input_line_pointer; |
1806 | input_line_pointer = str; | |
1807 | ||
1808 | while (*input_line_pointer) | |
1809 | { | |
1810 | SKIP_WHITESPACE (); | |
1811 | switch (*input_line_pointer) | |
1812 | { | |
1813 | case '\0': | |
43cd72b9 | 1814 | case '}': |
e0001a05 NC |
1815 | goto fini; |
1816 | ||
43cd72b9 BW |
1817 | case ':': |
1818 | input_line_pointer++; | |
1819 | if (saw_comma || saw_colon || !saw_arg) | |
1820 | goto err; | |
1821 | saw_colon = TRUE; | |
1822 | break; | |
1823 | ||
e0001a05 NC |
1824 | case ',': |
1825 | input_line_pointer++; | |
43cd72b9 | 1826 | if (saw_comma || saw_colon || !saw_arg) |
e0001a05 NC |
1827 | goto err; |
1828 | saw_comma = TRUE; | |
1829 | break; | |
1830 | ||
1831 | default: | |
43cd72b9 | 1832 | if (!saw_comma && !saw_colon && saw_arg) |
e0001a05 NC |
1833 | goto err; |
1834 | ||
1835 | arg_end = input_line_pointer + 1; | |
1836 | while (!expression_end (arg_end)) | |
1837 | arg_end += 1; | |
43cd72b9 | 1838 | |
e0001a05 | 1839 | arg_len = arg_end - input_line_pointer; |
43cd72b9 | 1840 | arg = (char *) xmalloc ((saw_colon ? 1 : 0) + arg_len + 1); |
e0001a05 NC |
1841 | args[num_args] = arg; |
1842 | ||
43cd72b9 BW |
1843 | if (saw_colon) |
1844 | *arg++ = ':'; | |
e0001a05 NC |
1845 | strncpy (arg, input_line_pointer, arg_len); |
1846 | arg[arg_len] = '\0'; | |
43cd72b9 | 1847 | |
e0001a05 NC |
1848 | input_line_pointer = arg_end; |
1849 | num_args += 1; | |
c138bc38 | 1850 | saw_comma = FALSE; |
43cd72b9 | 1851 | saw_colon = FALSE; |
c138bc38 | 1852 | saw_arg = TRUE; |
e0001a05 NC |
1853 | break; |
1854 | } | |
1855 | } | |
1856 | ||
1857 | fini: | |
43cd72b9 | 1858 | if (saw_comma || saw_colon) |
e0001a05 NC |
1859 | goto err; |
1860 | input_line_pointer = old_input_line_pointer; | |
1861 | return num_args; | |
1862 | ||
1863 | err: | |
43cd72b9 BW |
1864 | if (saw_comma) |
1865 | as_bad (_("extra comma")); | |
1866 | else if (saw_colon) | |
1867 | as_bad (_("extra colon")); | |
1868 | else if (!saw_arg) | |
c138bc38 | 1869 | as_bad (_("missing argument")); |
43cd72b9 BW |
1870 | else |
1871 | as_bad (_("missing comma or colon")); | |
e0001a05 NC |
1872 | input_line_pointer = old_input_line_pointer; |
1873 | return -1; | |
1874 | } | |
1875 | ||
1876 | ||
43cd72b9 | 1877 | /* Parse the arguments to an opcode. Return TRUE on error. */ |
e0001a05 NC |
1878 | |
1879 | static bfd_boolean | |
7fa3d080 | 1880 | parse_arguments (TInsn *insn, int num_args, char **arg_strings) |
e0001a05 | 1881 | { |
43cd72b9 | 1882 | expressionS *tok, *last_tok; |
e0001a05 NC |
1883 | xtensa_opcode opcode = insn->opcode; |
1884 | bfd_boolean had_error = TRUE; | |
43cd72b9 BW |
1885 | xtensa_isa isa = xtensa_default_isa; |
1886 | int n, num_regs = 0; | |
e0001a05 | 1887 | int opcode_operand_count; |
43cd72b9 BW |
1888 | int opnd_cnt, last_opnd_cnt; |
1889 | unsigned int next_reg = 0; | |
e0001a05 NC |
1890 | char *old_input_line_pointer; |
1891 | ||
1892 | if (insn->insn_type == ITYPE_LITERAL) | |
1893 | opcode_operand_count = 1; | |
1894 | else | |
43cd72b9 | 1895 | opcode_operand_count = xtensa_opcode_num_operands (isa, opcode); |
e0001a05 | 1896 | |
43cd72b9 | 1897 | tok = insn->tok; |
e0001a05 NC |
1898 | memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS); |
1899 | ||
1900 | /* Save and restore input_line_pointer around this function. */ | |
43cd72b9 BW |
1901 | old_input_line_pointer = input_line_pointer; |
1902 | ||
1903 | last_tok = 0; | |
1904 | last_opnd_cnt = -1; | |
1905 | opnd_cnt = 0; | |
1906 | ||
1907 | /* Skip invisible operands. */ | |
1908 | while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0) | |
1909 | { | |
1910 | opnd_cnt += 1; | |
1911 | tok++; | |
1912 | } | |
e0001a05 NC |
1913 | |
1914 | for (n = 0; n < num_args; n++) | |
43cd72b9 | 1915 | { |
e0001a05 | 1916 | input_line_pointer = arg_strings[n]; |
43cd72b9 BW |
1917 | if (*input_line_pointer == ':') |
1918 | { | |
1919 | xtensa_regfile opnd_rf; | |
1920 | input_line_pointer++; | |
1921 | if (num_regs == 0) | |
1922 | goto err; | |
1923 | assert (opnd_cnt > 0); | |
1924 | num_regs--; | |
1925 | opnd_rf = xtensa_operand_regfile (isa, opcode, last_opnd_cnt); | |
1926 | if (next_reg | |
1927 | != tc_get_register (xtensa_regfile_shortname (isa, opnd_rf))) | |
1928 | as_warn (_("incorrect register number, ignoring")); | |
1929 | next_reg++; | |
1930 | } | |
1931 | else | |
1932 | { | |
1933 | if (opnd_cnt >= opcode_operand_count) | |
1934 | { | |
1935 | as_warn (_("too many arguments")); | |
1936 | goto err; | |
1937 | } | |
1938 | assert (opnd_cnt < MAX_INSN_ARGS); | |
1939 | ||
1940 | expression_maybe_register (opcode, opnd_cnt, tok); | |
1941 | next_reg = tok->X_add_number + 1; | |
1942 | ||
1943 | if (tok->X_op == O_illegal || tok->X_op == O_absent) | |
1944 | goto err; | |
1945 | if (xtensa_operand_is_register (isa, opcode, opnd_cnt) == 1) | |
1946 | { | |
1947 | num_regs = xtensa_operand_num_regs (isa, opcode, opnd_cnt) - 1; | |
1948 | /* minus 1 because we are seeing one right now */ | |
1949 | } | |
1950 | else | |
1951 | num_regs = 0; | |
e0001a05 | 1952 | |
43cd72b9 BW |
1953 | last_tok = tok; |
1954 | last_opnd_cnt = opnd_cnt; | |
e0001a05 | 1955 | |
43cd72b9 BW |
1956 | do |
1957 | { | |
1958 | opnd_cnt += 1; | |
1959 | tok++; | |
1960 | } | |
1961 | while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0); | |
1962 | } | |
1963 | } | |
e0001a05 | 1964 | |
43cd72b9 BW |
1965 | if (num_regs > 0 && ((int) next_reg != last_tok->X_add_number + 1)) |
1966 | goto err; | |
e0001a05 NC |
1967 | |
1968 | insn->ntok = tok - insn->tok; | |
c138bc38 | 1969 | had_error = FALSE; |
e0001a05 NC |
1970 | |
1971 | err: | |
43cd72b9 | 1972 | input_line_pointer = old_input_line_pointer; |
e0001a05 NC |
1973 | return had_error; |
1974 | } | |
1975 | ||
1976 | ||
43cd72b9 | 1977 | static int |
7fa3d080 | 1978 | get_invisible_operands (TInsn *insn) |
43cd72b9 BW |
1979 | { |
1980 | xtensa_isa isa = xtensa_default_isa; | |
1981 | static xtensa_insnbuf slotbuf = NULL; | |
1982 | xtensa_format fmt; | |
1983 | xtensa_opcode opc = insn->opcode; | |
1984 | int slot, opnd, fmt_found; | |
1985 | unsigned val; | |
1986 | ||
1987 | if (!slotbuf) | |
1988 | slotbuf = xtensa_insnbuf_alloc (isa); | |
1989 | ||
1990 | /* Find format/slot where this can be encoded. */ | |
1991 | fmt_found = 0; | |
1992 | slot = 0; | |
1993 | for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++) | |
1994 | { | |
1995 | for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++) | |
1996 | { | |
1997 | if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opc) == 0) | |
1998 | { | |
1999 | fmt_found = 1; | |
2000 | break; | |
2001 | } | |
2002 | } | |
2003 | if (fmt_found) break; | |
2004 | } | |
2005 | ||
2006 | if (!fmt_found) | |
2007 | { | |
2008 | as_bad (_("cannot encode opcode \"%s\""), xtensa_opcode_name (isa, opc)); | |
2009 | return -1; | |
2010 | } | |
2011 | ||
2012 | /* First encode all the visible operands | |
2013 | (to deal with shared field operands). */ | |
2014 | for (opnd = 0; opnd < insn->ntok; opnd++) | |
2015 | { | |
2016 | if (xtensa_operand_is_visible (isa, opc, opnd) == 1 | |
2017 | && (insn->tok[opnd].X_op == O_register | |
2018 | || insn->tok[opnd].X_op == O_constant)) | |
2019 | { | |
2020 | val = insn->tok[opnd].X_add_number; | |
2021 | xtensa_operand_encode (isa, opc, opnd, &val); | |
2022 | xtensa_operand_set_field (isa, opc, opnd, fmt, slot, slotbuf, val); | |
2023 | } | |
2024 | } | |
2025 | ||
2026 | /* Then pull out the values for the invisible ones. */ | |
2027 | for (opnd = 0; opnd < insn->ntok; opnd++) | |
2028 | { | |
2029 | if (xtensa_operand_is_visible (isa, opc, opnd) == 0) | |
2030 | { | |
2031 | xtensa_operand_get_field (isa, opc, opnd, fmt, slot, slotbuf, &val); | |
2032 | xtensa_operand_decode (isa, opc, opnd, &val); | |
2033 | insn->tok[opnd].X_add_number = val; | |
2034 | if (xtensa_operand_is_register (isa, opc, opnd) == 1) | |
2035 | insn->tok[opnd].X_op = O_register; | |
2036 | else | |
2037 | insn->tok[opnd].X_op = O_constant; | |
2038 | } | |
2039 | } | |
2040 | ||
2041 | return 0; | |
2042 | } | |
2043 | ||
2044 | ||
e0001a05 | 2045 | static void |
7fa3d080 | 2046 | xg_reverse_shift_count (char **cnt_argp) |
e0001a05 NC |
2047 | { |
2048 | char *cnt_arg, *new_arg; | |
2049 | cnt_arg = *cnt_argp; | |
2050 | ||
2051 | /* replace the argument with "31-(argument)" */ | |
2052 | new_arg = (char *) xmalloc (strlen (cnt_arg) + 6); | |
2053 | sprintf (new_arg, "31-(%s)", cnt_arg); | |
2054 | ||
2055 | free (cnt_arg); | |
2056 | *cnt_argp = new_arg; | |
2057 | } | |
2058 | ||
2059 | ||
2060 | /* If "arg" is a constant expression, return non-zero with the value | |
2061 | in *valp. */ | |
2062 | ||
2063 | static int | |
7fa3d080 | 2064 | xg_arg_is_constant (char *arg, offsetT *valp) |
e0001a05 NC |
2065 | { |
2066 | expressionS exp; | |
2067 | char *save_ptr = input_line_pointer; | |
2068 | ||
2069 | input_line_pointer = arg; | |
2070 | expression (&exp); | |
2071 | input_line_pointer = save_ptr; | |
2072 | ||
2073 | if (exp.X_op == O_constant) | |
2074 | { | |
2075 | *valp = exp.X_add_number; | |
2076 | return 1; | |
2077 | } | |
2078 | ||
2079 | return 0; | |
2080 | } | |
2081 | ||
2082 | ||
2083 | static void | |
7fa3d080 | 2084 | xg_replace_opname (char **popname, char *newop) |
e0001a05 NC |
2085 | { |
2086 | free (*popname); | |
2087 | *popname = (char *) xmalloc (strlen (newop) + 1); | |
2088 | strcpy (*popname, newop); | |
2089 | } | |
2090 | ||
2091 | ||
2092 | static int | |
7fa3d080 BW |
2093 | xg_check_num_args (int *pnum_args, |
2094 | int expected_num, | |
2095 | char *opname, | |
2096 | char **arg_strings) | |
e0001a05 NC |
2097 | { |
2098 | int num_args = *pnum_args; | |
2099 | ||
43cd72b9 | 2100 | if (num_args < expected_num) |
e0001a05 NC |
2101 | { |
2102 | as_bad (_("not enough operands (%d) for '%s'; expected %d"), | |
2103 | num_args, opname, expected_num); | |
2104 | return -1; | |
2105 | } | |
2106 | ||
2107 | if (num_args > expected_num) | |
2108 | { | |
2109 | as_warn (_("too many operands (%d) for '%s'; expected %d"), | |
2110 | num_args, opname, expected_num); | |
2111 | while (num_args-- > expected_num) | |
2112 | { | |
2113 | free (arg_strings[num_args]); | |
2114 | arg_strings[num_args] = 0; | |
2115 | } | |
2116 | *pnum_args = expected_num; | |
2117 | return -1; | |
2118 | } | |
2119 | ||
2120 | return 0; | |
2121 | } | |
2122 | ||
2123 | ||
43cd72b9 BW |
2124 | /* If the register is not specified as part of the opcode, |
2125 | then get it from the operand and move it to the opcode. */ | |
2126 | ||
e0001a05 | 2127 | static int |
7fa3d080 | 2128 | xg_translate_sysreg_op (char **popname, int *pnum_args, char **arg_strings) |
e0001a05 | 2129 | { |
43cd72b9 BW |
2130 | xtensa_isa isa = xtensa_default_isa; |
2131 | xtensa_sysreg sr; | |
e0001a05 | 2132 | char *opname, *new_opname; |
43cd72b9 BW |
2133 | const char *sr_name; |
2134 | int is_user, is_write; | |
e0001a05 NC |
2135 | |
2136 | opname = *popname; | |
2137 | if (*opname == '_') | |
80ca4e2c | 2138 | opname += 1; |
43cd72b9 BW |
2139 | is_user = (opname[1] == 'u'); |
2140 | is_write = (opname[0] == 'w'); | |
e0001a05 | 2141 | |
43cd72b9 | 2142 | /* Opname == [rw]ur or [rwx]sr... */ |
e0001a05 | 2143 | |
43cd72b9 BW |
2144 | if (xg_check_num_args (pnum_args, 2, opname, arg_strings)) |
2145 | return -1; | |
e0001a05 | 2146 | |
43cd72b9 BW |
2147 | /* Check if the argument is a symbolic register name. */ |
2148 | sr = xtensa_sysreg_lookup_name (isa, arg_strings[1]); | |
2149 | /* Handle WSR to "INTSET" as a special case. */ | |
2150 | if (sr == XTENSA_UNDEFINED && is_write && !is_user | |
2151 | && !strcasecmp (arg_strings[1], "intset")) | |
2152 | sr = xtensa_sysreg_lookup_name (isa, "interrupt"); | |
2153 | if (sr == XTENSA_UNDEFINED | |
2154 | || (xtensa_sysreg_is_user (isa, sr) == 1) != is_user) | |
2155 | { | |
2156 | /* Maybe it's a register number.... */ | |
2157 | offsetT val; | |
e0001a05 NC |
2158 | if (!xg_arg_is_constant (arg_strings[1], &val)) |
2159 | { | |
43cd72b9 BW |
2160 | as_bad (_("invalid register '%s' for '%s' instruction"), |
2161 | arg_strings[1], opname); | |
e0001a05 NC |
2162 | return -1; |
2163 | } | |
43cd72b9 BW |
2164 | sr = xtensa_sysreg_lookup (isa, val, is_user); |
2165 | if (sr == XTENSA_UNDEFINED) | |
e0001a05 | 2166 | { |
43cd72b9 | 2167 | as_bad (_("invalid register number (%ld) for '%s' instruction"), |
dd49a749 | 2168 | (long) val, opname); |
e0001a05 NC |
2169 | return -1; |
2170 | } | |
43cd72b9 | 2171 | } |
e0001a05 | 2172 | |
43cd72b9 BW |
2173 | /* Remove the last argument, which is now part of the opcode. */ |
2174 | free (arg_strings[1]); | |
2175 | arg_strings[1] = 0; | |
2176 | *pnum_args = 1; | |
2177 | ||
2178 | /* Translate the opcode. */ | |
2179 | sr_name = xtensa_sysreg_name (isa, sr); | |
2180 | /* Another special case for "WSR.INTSET".... */ | |
2181 | if (is_write && !is_user && !strcasecmp ("interrupt", sr_name)) | |
2182 | sr_name = "intset"; | |
2183 | new_opname = (char *) xmalloc (strlen (sr_name) + 6); | |
80ca4e2c | 2184 | sprintf (new_opname, "%s.%s", *popname, sr_name); |
43cd72b9 BW |
2185 | free (*popname); |
2186 | *popname = new_opname; | |
2187 | ||
2188 | return 0; | |
2189 | } | |
2190 | ||
2191 | ||
2192 | static int | |
7fa3d080 | 2193 | xtensa_translate_old_userreg_ops (char **popname) |
43cd72b9 BW |
2194 | { |
2195 | xtensa_isa isa = xtensa_default_isa; | |
2196 | xtensa_sysreg sr; | |
2197 | char *opname, *new_opname; | |
2198 | const char *sr_name; | |
2199 | bfd_boolean has_underbar = FALSE; | |
2200 | ||
2201 | opname = *popname; | |
2202 | if (opname[0] == '_') | |
2203 | { | |
2204 | has_underbar = TRUE; | |
2205 | opname += 1; | |
2206 | } | |
2207 | ||
2208 | sr = xtensa_sysreg_lookup_name (isa, opname + 1); | |
2209 | if (sr != XTENSA_UNDEFINED) | |
2210 | { | |
2211 | /* The new default name ("nnn") is different from the old default | |
2212 | name ("URnnn"). The old default is handled below, and we don't | |
2213 | want to recognize [RW]nnn, so do nothing if the name is the (new) | |
2214 | default. */ | |
2215 | static char namebuf[10]; | |
2216 | sprintf (namebuf, "%d", xtensa_sysreg_number (isa, sr)); | |
2217 | if (strcmp (namebuf, opname + 1) == 0) | |
2218 | return 0; | |
2219 | } | |
2220 | else | |
2221 | { | |
2222 | offsetT val; | |
2223 | char *end; | |
2224 | ||
2225 | /* Only continue if the reg name is "URnnn". */ | |
2226 | if (opname[1] != 'u' || opname[2] != 'r') | |
2227 | return 0; | |
2228 | val = strtoul (opname + 3, &end, 10); | |
2229 | if (*end != '\0') | |
2230 | return 0; | |
2231 | ||
2232 | sr = xtensa_sysreg_lookup (isa, val, 1); | |
2233 | if (sr == XTENSA_UNDEFINED) | |
2234 | { | |
2235 | as_bad (_("invalid register number (%ld) for '%s'"), | |
dd49a749 | 2236 | (long) val, opname); |
43cd72b9 BW |
2237 | return -1; |
2238 | } | |
2239 | } | |
2240 | ||
2241 | /* Translate the opcode. */ | |
2242 | sr_name = xtensa_sysreg_name (isa, sr); | |
2243 | new_opname = (char *) xmalloc (strlen (sr_name) + 6); | |
2244 | sprintf (new_opname, "%s%cur.%s", (has_underbar ? "_" : ""), | |
2245 | opname[0], sr_name); | |
2246 | free (*popname); | |
2247 | *popname = new_opname; | |
2248 | ||
2249 | return 0; | |
2250 | } | |
2251 | ||
2252 | ||
2253 | static int | |
7fa3d080 BW |
2254 | xtensa_translate_zero_immed (char *old_op, |
2255 | char *new_op, | |
2256 | char **popname, | |
2257 | int *pnum_args, | |
2258 | char **arg_strings) | |
43cd72b9 BW |
2259 | { |
2260 | char *opname; | |
2261 | offsetT val; | |
2262 | ||
2263 | opname = *popname; | |
2264 | assert (opname[0] != '_'); | |
2265 | ||
2266 | if (strcmp (opname, old_op) != 0) | |
2267 | return 0; | |
e0001a05 | 2268 | |
43cd72b9 BW |
2269 | if (xg_check_num_args (pnum_args, 3, opname, arg_strings)) |
2270 | return -1; | |
2271 | if (xg_arg_is_constant (arg_strings[1], &val) && val == 0) | |
2272 | { | |
2273 | xg_replace_opname (popname, new_op); | |
2274 | free (arg_strings[1]); | |
2275 | arg_strings[1] = arg_strings[2]; | |
2276 | arg_strings[2] = 0; | |
2277 | *pnum_args = 2; | |
e0001a05 NC |
2278 | } |
2279 | ||
2280 | return 0; | |
2281 | } | |
2282 | ||
2283 | ||
2284 | /* If the instruction is an idiom (i.e., a built-in macro), translate it. | |
2285 | Returns non-zero if an error was found. */ | |
2286 | ||
2287 | static int | |
7fa3d080 | 2288 | xg_translate_idioms (char **popname, int *pnum_args, char **arg_strings) |
e0001a05 NC |
2289 | { |
2290 | char *opname = *popname; | |
2291 | bfd_boolean has_underbar = FALSE; | |
2292 | ||
43cd72b9 BW |
2293 | if (cur_vinsn.inside_bundle) |
2294 | return 0; | |
2295 | ||
e0001a05 NC |
2296 | if (*opname == '_') |
2297 | { | |
2298 | has_underbar = TRUE; | |
2299 | opname += 1; | |
2300 | } | |
2301 | ||
2302 | if (strcmp (opname, "mov") == 0) | |
2303 | { | |
43cd72b9 | 2304 | if (use_transform () && !has_underbar && density_supported) |
e0001a05 NC |
2305 | xg_replace_opname (popname, "mov.n"); |
2306 | else | |
2307 | { | |
2308 | if (xg_check_num_args (pnum_args, 2, opname, arg_strings)) | |
2309 | return -1; | |
2310 | xg_replace_opname (popname, (has_underbar ? "_or" : "or")); | |
2311 | arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1); | |
2312 | strcpy (arg_strings[2], arg_strings[1]); | |
2313 | *pnum_args = 3; | |
2314 | } | |
2315 | return 0; | |
2316 | } | |
2317 | ||
2318 | if (strcmp (opname, "bbsi.l") == 0) | |
2319 | { | |
2320 | if (xg_check_num_args (pnum_args, 3, opname, arg_strings)) | |
2321 | return -1; | |
2322 | xg_replace_opname (popname, (has_underbar ? "_bbsi" : "bbsi")); | |
2323 | if (target_big_endian) | |
2324 | xg_reverse_shift_count (&arg_strings[1]); | |
2325 | return 0; | |
2326 | } | |
2327 | ||
2328 | if (strcmp (opname, "bbci.l") == 0) | |
2329 | { | |
2330 | if (xg_check_num_args (pnum_args, 3, opname, arg_strings)) | |
2331 | return -1; | |
2332 | xg_replace_opname (popname, (has_underbar ? "_bbci" : "bbci")); | |
2333 | if (target_big_endian) | |
2334 | xg_reverse_shift_count (&arg_strings[1]); | |
2335 | return 0; | |
2336 | } | |
2337 | ||
43cd72b9 BW |
2338 | if (xtensa_nop_opcode == XTENSA_UNDEFINED |
2339 | && strcmp (opname, "nop") == 0) | |
e0001a05 | 2340 | { |
43cd72b9 | 2341 | if (use_transform () && !has_underbar && density_supported) |
e0001a05 NC |
2342 | xg_replace_opname (popname, "nop.n"); |
2343 | else | |
2344 | { | |
2345 | if (xg_check_num_args (pnum_args, 0, opname, arg_strings)) | |
2346 | return -1; | |
2347 | xg_replace_opname (popname, (has_underbar ? "_or" : "or")); | |
2348 | arg_strings[0] = (char *) xmalloc (3); | |
2349 | arg_strings[1] = (char *) xmalloc (3); | |
2350 | arg_strings[2] = (char *) xmalloc (3); | |
2351 | strcpy (arg_strings[0], "a1"); | |
2352 | strcpy (arg_strings[1], "a1"); | |
2353 | strcpy (arg_strings[2], "a1"); | |
2354 | *pnum_args = 3; | |
2355 | } | |
2356 | return 0; | |
2357 | } | |
2358 | ||
43cd72b9 BW |
2359 | /* Recognize [RW]UR and [RWX]SR. */ |
2360 | if ((((opname[0] == 'r' || opname[0] == 'w') | |
2361 | && (opname[1] == 'u' || opname[1] == 's')) | |
2362 | || (opname[0] == 'x' && opname[1] == 's')) | |
2363 | && opname[2] == 'r' | |
2364 | && opname[3] == '\0') | |
e0001a05 NC |
2365 | return xg_translate_sysreg_op (popname, pnum_args, arg_strings); |
2366 | ||
43cd72b9 BW |
2367 | /* Backward compatibility for RUR and WUR: Recognize [RW]UR<nnn> and |
2368 | [RW]<name> if <name> is the non-default name of a user register. */ | |
2369 | if ((opname[0] == 'r' || opname[0] == 'w') | |
2370 | && xtensa_opcode_lookup (xtensa_default_isa, opname) == XTENSA_UNDEFINED) | |
2371 | return xtensa_translate_old_userreg_ops (popname); | |
e0001a05 | 2372 | |
43cd72b9 BW |
2373 | /* Relax branches that don't allow comparisons against an immediate value |
2374 | of zero to the corresponding branches with implicit zero immediates. */ | |
2375 | if (!has_underbar && use_transform ()) | |
2376 | { | |
2377 | if (xtensa_translate_zero_immed ("bnei", "bnez", popname, | |
2378 | pnum_args, arg_strings)) | |
2379 | return -1; | |
e0001a05 | 2380 | |
43cd72b9 BW |
2381 | if (xtensa_translate_zero_immed ("beqi", "beqz", popname, |
2382 | pnum_args, arg_strings)) | |
2383 | return -1; | |
e0001a05 | 2384 | |
43cd72b9 BW |
2385 | if (xtensa_translate_zero_immed ("bgei", "bgez", popname, |
2386 | pnum_args, arg_strings)) | |
2387 | return -1; | |
e0001a05 | 2388 | |
43cd72b9 BW |
2389 | if (xtensa_translate_zero_immed ("blti", "bltz", popname, |
2390 | pnum_args, arg_strings)) | |
2391 | return -1; | |
2392 | } | |
e0001a05 | 2393 | |
43cd72b9 BW |
2394 | return 0; |
2395 | } | |
e0001a05 | 2396 | |
43cd72b9 BW |
2397 | \f |
2398 | /* Functions for dealing with the Xtensa ISA. */ | |
e0001a05 | 2399 | |
43cd72b9 BW |
2400 | /* Currently the assembler only allows us to use a single target per |
2401 | fragment. Because of this, only one operand for a given | |
2402 | instruction may be symbolic. If there is a PC-relative operand, | |
2403 | the last one is chosen. Otherwise, the result is the number of the | |
2404 | last immediate operand, and if there are none of those, we fail and | |
2405 | return -1. */ | |
e0001a05 | 2406 | |
7fa3d080 BW |
2407 | static int |
2408 | get_relaxable_immed (xtensa_opcode opcode) | |
43cd72b9 BW |
2409 | { |
2410 | int last_immed = -1; | |
2411 | int noperands, opi; | |
e0001a05 | 2412 | |
43cd72b9 BW |
2413 | if (opcode == XTENSA_UNDEFINED) |
2414 | return -1; | |
e0001a05 | 2415 | |
43cd72b9 BW |
2416 | noperands = xtensa_opcode_num_operands (xtensa_default_isa, opcode); |
2417 | for (opi = noperands - 1; opi >= 0; opi--) | |
2418 | { | |
2419 | if (xtensa_operand_is_visible (xtensa_default_isa, opcode, opi) == 0) | |
2420 | continue; | |
2421 | if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, opi) == 1) | |
2422 | return opi; | |
2423 | if (last_immed == -1 | |
2424 | && xtensa_operand_is_register (xtensa_default_isa, opcode, opi) == 0) | |
2425 | last_immed = opi; | |
e0001a05 | 2426 | } |
43cd72b9 | 2427 | return last_immed; |
e0001a05 NC |
2428 | } |
2429 | ||
e0001a05 | 2430 | |
43cd72b9 | 2431 | static xtensa_opcode |
7fa3d080 | 2432 | get_opcode_from_buf (const char *buf, int slot) |
e0001a05 | 2433 | { |
43cd72b9 BW |
2434 | static xtensa_insnbuf insnbuf = NULL; |
2435 | static xtensa_insnbuf slotbuf = NULL; | |
2436 | xtensa_isa isa = xtensa_default_isa; | |
2437 | xtensa_format fmt; | |
2438 | ||
2439 | if (!insnbuf) | |
e0001a05 | 2440 | { |
43cd72b9 BW |
2441 | insnbuf = xtensa_insnbuf_alloc (isa); |
2442 | slotbuf = xtensa_insnbuf_alloc (isa); | |
e0001a05 | 2443 | } |
e0001a05 | 2444 | |
d77b99c9 | 2445 | xtensa_insnbuf_from_chars (isa, insnbuf, (const unsigned char *) buf, 0); |
43cd72b9 BW |
2446 | fmt = xtensa_format_decode (isa, insnbuf); |
2447 | if (fmt == XTENSA_UNDEFINED) | |
2448 | return XTENSA_UNDEFINED; | |
e0001a05 | 2449 | |
43cd72b9 BW |
2450 | if (slot >= xtensa_format_num_slots (isa, fmt)) |
2451 | return XTENSA_UNDEFINED; | |
e0001a05 | 2452 | |
43cd72b9 BW |
2453 | xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf); |
2454 | return xtensa_opcode_decode (isa, fmt, slot, slotbuf); | |
e0001a05 NC |
2455 | } |
2456 | ||
2457 | ||
43cd72b9 | 2458 | #ifdef TENSILICA_DEBUG |
e0001a05 | 2459 | |
43cd72b9 | 2460 | /* For debugging, print out the mapping of opcode numbers to opcodes. */ |
e0001a05 | 2461 | |
7fa3d080 BW |
2462 | static void |
2463 | xtensa_print_insn_table (void) | |
43cd72b9 BW |
2464 | { |
2465 | int num_opcodes, num_operands; | |
2466 | xtensa_opcode opcode; | |
2467 | xtensa_isa isa = xtensa_default_isa; | |
e0001a05 | 2468 | |
43cd72b9 BW |
2469 | num_opcodes = xtensa_isa_num_opcodes (xtensa_default_isa); |
2470 | for (opcode = 0; opcode < num_opcodes; opcode++) | |
e0001a05 | 2471 | { |
43cd72b9 BW |
2472 | int opn; |
2473 | fprintf (stderr, "%d: %s: ", opcode, xtensa_opcode_name (isa, opcode)); | |
2474 | num_operands = xtensa_opcode_num_operands (isa, opcode); | |
2475 | for (opn = 0; opn < num_operands; opn++) | |
2476 | { | |
2477 | if (xtensa_operand_is_visible (isa, opcode, opn) == 0) | |
2478 | continue; | |
2479 | if (xtensa_operand_is_register (isa, opcode, opn) == 1) | |
2480 | { | |
2481 | xtensa_regfile opnd_rf = | |
2482 | xtensa_operand_regfile (isa, opcode, opn); | |
2483 | fprintf (stderr, "%s ", xtensa_regfile_shortname (isa, opnd_rf)); | |
2484 | } | |
2485 | else if (xtensa_operand_is_PCrelative (isa, opcode, opn) == 1) | |
2486 | fputs ("[lLr] ", stderr); | |
2487 | else | |
2488 | fputs ("i ", stderr); | |
2489 | } | |
2490 | fprintf (stderr, "\n"); | |
e0001a05 | 2491 | } |
e0001a05 NC |
2492 | } |
2493 | ||
2494 | ||
43cd72b9 | 2495 | static void |
7fa3d080 | 2496 | print_vliw_insn (xtensa_insnbuf vbuf) |
e0001a05 | 2497 | { |
e0001a05 | 2498 | xtensa_isa isa = xtensa_default_isa; |
43cd72b9 BW |
2499 | xtensa_format f = xtensa_format_decode (isa, vbuf); |
2500 | xtensa_insnbuf sbuf = xtensa_insnbuf_alloc (isa); | |
2501 | int op; | |
e0001a05 | 2502 | |
43cd72b9 | 2503 | fprintf (stderr, "format = %d\n", f); |
e0001a05 | 2504 | |
43cd72b9 BW |
2505 | for (op = 0; op < xtensa_format_num_slots (isa, f); op++) |
2506 | { | |
2507 | xtensa_opcode opcode; | |
2508 | const char *opname; | |
2509 | int operands; | |
2510 | ||
2511 | xtensa_format_get_slot (isa, f, op, vbuf, sbuf); | |
2512 | opcode = xtensa_opcode_decode (isa, f, op, sbuf); | |
2513 | opname = xtensa_opcode_name (isa, opcode); | |
2514 | ||
2515 | fprintf (stderr, "op in slot %i is %s;\n", op, opname); | |
2516 | fprintf (stderr, " operands = "); | |
2517 | for (operands = 0; | |
2518 | operands < xtensa_opcode_num_operands (isa, opcode); | |
2519 | operands++) | |
2520 | { | |
2521 | unsigned int val; | |
2522 | if (xtensa_operand_is_visible (isa, opcode, operands) == 0) | |
2523 | continue; | |
2524 | xtensa_operand_get_field (isa, opcode, operands, f, op, sbuf, &val); | |
2525 | xtensa_operand_decode (isa, opcode, operands, &val); | |
2526 | fprintf (stderr, "%d ", val); | |
2527 | } | |
2528 | fprintf (stderr, "\n"); | |
2529 | } | |
2530 | xtensa_insnbuf_free (isa, sbuf); | |
e0001a05 NC |
2531 | } |
2532 | ||
43cd72b9 BW |
2533 | #endif /* TENSILICA_DEBUG */ |
2534 | ||
e0001a05 NC |
2535 | |
2536 | static bfd_boolean | |
7fa3d080 | 2537 | is_direct_call_opcode (xtensa_opcode opcode) |
e0001a05 | 2538 | { |
43cd72b9 BW |
2539 | xtensa_isa isa = xtensa_default_isa; |
2540 | int n, num_operands; | |
e0001a05 | 2541 | |
64b607e6 | 2542 | if (xtensa_opcode_is_call (isa, opcode) != 1) |
e0001a05 NC |
2543 | return FALSE; |
2544 | ||
43cd72b9 BW |
2545 | num_operands = xtensa_opcode_num_operands (isa, opcode); |
2546 | for (n = 0; n < num_operands; n++) | |
2547 | { | |
2548 | if (xtensa_operand_is_register (isa, opcode, n) == 0 | |
2549 | && xtensa_operand_is_PCrelative (isa, opcode, n) == 1) | |
2550 | return TRUE; | |
2551 | } | |
2552 | return FALSE; | |
e0001a05 NC |
2553 | } |
2554 | ||
2555 | ||
43cd72b9 BW |
2556 | /* Convert from BFD relocation type code to slot and operand number. |
2557 | Returns non-zero on failure. */ | |
e0001a05 | 2558 | |
43cd72b9 | 2559 | static int |
7fa3d080 | 2560 | decode_reloc (bfd_reloc_code_real_type reloc, int *slot, bfd_boolean *is_alt) |
e0001a05 | 2561 | { |
43cd72b9 BW |
2562 | if (reloc >= BFD_RELOC_XTENSA_SLOT0_OP |
2563 | && reloc <= BFD_RELOC_XTENSA_SLOT14_OP) | |
e0001a05 | 2564 | { |
43cd72b9 BW |
2565 | *slot = reloc - BFD_RELOC_XTENSA_SLOT0_OP; |
2566 | *is_alt = FALSE; | |
e0001a05 | 2567 | } |
43cd72b9 BW |
2568 | else if (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT |
2569 | && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT) | |
e0001a05 | 2570 | { |
43cd72b9 BW |
2571 | *slot = reloc - BFD_RELOC_XTENSA_SLOT0_ALT; |
2572 | *is_alt = TRUE; | |
e0001a05 | 2573 | } |
43cd72b9 BW |
2574 | else |
2575 | return -1; | |
2576 | ||
2577 | return 0; | |
e0001a05 NC |
2578 | } |
2579 | ||
2580 | ||
43cd72b9 BW |
2581 | /* Convert from slot number to BFD relocation type code for the |
2582 | standard PC-relative relocations. Return BFD_RELOC_NONE on | |
2583 | failure. */ | |
e0001a05 | 2584 | |
43cd72b9 | 2585 | static bfd_reloc_code_real_type |
7fa3d080 | 2586 | encode_reloc (int slot) |
e0001a05 | 2587 | { |
43cd72b9 BW |
2588 | if (slot < 0 || slot > 14) |
2589 | return BFD_RELOC_NONE; | |
2590 | ||
2591 | return BFD_RELOC_XTENSA_SLOT0_OP + slot; | |
e0001a05 NC |
2592 | } |
2593 | ||
2594 | ||
43cd72b9 BW |
2595 | /* Convert from slot numbers to BFD relocation type code for the |
2596 | "alternate" relocations. Return BFD_RELOC_NONE on failure. */ | |
e0001a05 | 2597 | |
43cd72b9 | 2598 | static bfd_reloc_code_real_type |
7fa3d080 | 2599 | encode_alt_reloc (int slot) |
e0001a05 | 2600 | { |
43cd72b9 BW |
2601 | if (slot < 0 || slot > 14) |
2602 | return BFD_RELOC_NONE; | |
2603 | ||
2604 | return BFD_RELOC_XTENSA_SLOT0_ALT + slot; | |
e0001a05 NC |
2605 | } |
2606 | ||
2607 | ||
2608 | static void | |
7fa3d080 BW |
2609 | xtensa_insnbuf_set_operand (xtensa_insnbuf slotbuf, |
2610 | xtensa_format fmt, | |
2611 | int slot, | |
2612 | xtensa_opcode opcode, | |
2613 | int operand, | |
2614 | uint32 value, | |
2615 | const char *file, | |
2616 | unsigned int line) | |
e0001a05 | 2617 | { |
e0001a05 NC |
2618 | uint32 valbuf = value; |
2619 | ||
43cd72b9 | 2620 | if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf)) |
e0001a05 | 2621 | { |
43cd72b9 BW |
2622 | if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, operand) |
2623 | == 1) | |
2624 | as_bad_where ((char *) file, line, | |
d7c531cd BW |
2625 | _("operand %d of '%s' has out of range value '%u'"), |
2626 | operand + 1, | |
2627 | xtensa_opcode_name (xtensa_default_isa, opcode), | |
2628 | value); | |
43cd72b9 BW |
2629 | else |
2630 | as_bad_where ((char *) file, line, | |
d7c531cd BW |
2631 | _("operand %d of '%s' has invalid value '%u'"), |
2632 | operand + 1, | |
2633 | xtensa_opcode_name (xtensa_default_isa, opcode), | |
2634 | value); | |
43cd72b9 | 2635 | return; |
e0001a05 NC |
2636 | } |
2637 | ||
43cd72b9 BW |
2638 | xtensa_operand_set_field (xtensa_default_isa, opcode, operand, fmt, slot, |
2639 | slotbuf, valbuf); | |
e0001a05 NC |
2640 | } |
2641 | ||
2642 | ||
2643 | static uint32 | |
7fa3d080 BW |
2644 | xtensa_insnbuf_get_operand (xtensa_insnbuf slotbuf, |
2645 | xtensa_format fmt, | |
2646 | int slot, | |
2647 | xtensa_opcode opcode, | |
2648 | int opnum) | |
e0001a05 | 2649 | { |
43cd72b9 BW |
2650 | uint32 val = 0; |
2651 | (void) xtensa_operand_get_field (xtensa_default_isa, opcode, opnum, | |
2652 | fmt, slot, slotbuf, &val); | |
2653 | (void) xtensa_operand_decode (xtensa_default_isa, opcode, opnum, &val); | |
2654 | return val; | |
e0001a05 NC |
2655 | } |
2656 | ||
e0001a05 | 2657 | \f |
7fa3d080 | 2658 | /* Checks for rules from xtensa-relax tables. */ |
e0001a05 | 2659 | |
7fa3d080 BW |
2660 | /* The routine xg_instruction_matches_option_term must return TRUE |
2661 | when a given option term is true. The meaning of all of the option | |
2662 | terms is given interpretation by this function. This is needed when | |
2663 | an option depends on the state of a directive, but there are no such | |
2664 | options in use right now. */ | |
e0001a05 | 2665 | |
7fa3d080 BW |
2666 | static bfd_boolean |
2667 | xg_instruction_matches_option_term (TInsn *insn ATTRIBUTE_UNUSED, | |
2668 | const ReqOrOption *option) | |
e0001a05 | 2669 | { |
7fa3d080 BW |
2670 | if (strcmp (option->option_name, "realnop") == 0 |
2671 | || strncmp (option->option_name, "IsaUse", 6) == 0) | |
2672 | { | |
2673 | /* These conditions were evaluated statically when building the | |
2674 | relaxation table. There's no need to reevaluate them now. */ | |
2675 | return TRUE; | |
2676 | } | |
2677 | else | |
2678 | { | |
2679 | as_fatal (_("internal error: unknown option name '%s'"), | |
2680 | option->option_name); | |
2681 | } | |
e0001a05 NC |
2682 | } |
2683 | ||
2684 | ||
7fa3d080 BW |
2685 | static bfd_boolean |
2686 | xg_instruction_matches_or_options (TInsn *insn, | |
2687 | const ReqOrOptionList *or_option) | |
e0001a05 | 2688 | { |
7fa3d080 BW |
2689 | const ReqOrOption *option; |
2690 | /* Must match each of the AND terms. */ | |
2691 | for (option = or_option; option != NULL; option = option->next) | |
e0001a05 | 2692 | { |
7fa3d080 BW |
2693 | if (xg_instruction_matches_option_term (insn, option)) |
2694 | return TRUE; | |
e0001a05 | 2695 | } |
7fa3d080 | 2696 | return FALSE; |
e0001a05 NC |
2697 | } |
2698 | ||
2699 | ||
7fa3d080 BW |
2700 | static bfd_boolean |
2701 | xg_instruction_matches_options (TInsn *insn, const ReqOptionList *options) | |
e0001a05 | 2702 | { |
7fa3d080 BW |
2703 | const ReqOption *req_options; |
2704 | /* Must match each of the AND terms. */ | |
2705 | for (req_options = options; | |
2706 | req_options != NULL; | |
2707 | req_options = req_options->next) | |
e0001a05 | 2708 | { |
7fa3d080 BW |
2709 | /* Must match one of the OR clauses. */ |
2710 | if (!xg_instruction_matches_or_options (insn, | |
2711 | req_options->or_option_terms)) | |
2712 | return FALSE; | |
e0001a05 | 2713 | } |
7fa3d080 | 2714 | return TRUE; |
e0001a05 NC |
2715 | } |
2716 | ||
2717 | ||
7fa3d080 | 2718 | /* Return the transition rule that matches or NULL if none matches. */ |
e0001a05 | 2719 | |
7fa3d080 BW |
2720 | static bfd_boolean |
2721 | xg_instruction_matches_rule (TInsn *insn, TransitionRule *rule) | |
e0001a05 | 2722 | { |
7fa3d080 | 2723 | PreconditionList *condition_l; |
e0001a05 | 2724 | |
7fa3d080 BW |
2725 | if (rule->opcode != insn->opcode) |
2726 | return FALSE; | |
e0001a05 | 2727 | |
7fa3d080 BW |
2728 | for (condition_l = rule->conditions; |
2729 | condition_l != NULL; | |
2730 | condition_l = condition_l->next) | |
e0001a05 | 2731 | { |
7fa3d080 BW |
2732 | expressionS *exp1; |
2733 | expressionS *exp2; | |
2734 | Precondition *cond = condition_l->precond; | |
e0001a05 | 2735 | |
7fa3d080 | 2736 | switch (cond->typ) |
e0001a05 | 2737 | { |
7fa3d080 BW |
2738 | case OP_CONSTANT: |
2739 | /* The expression must be the constant. */ | |
2740 | assert (cond->op_num < insn->ntok); | |
2741 | exp1 = &insn->tok[cond->op_num]; | |
2742 | if (expr_is_const (exp1)) | |
2743 | { | |
2744 | switch (cond->cmp) | |
2745 | { | |
2746 | case OP_EQUAL: | |
2747 | if (get_expr_const (exp1) != cond->op_data) | |
2748 | return FALSE; | |
2749 | break; | |
2750 | case OP_NOTEQUAL: | |
2751 | if (get_expr_const (exp1) == cond->op_data) | |
2752 | return FALSE; | |
2753 | break; | |
2754 | default: | |
2755 | return FALSE; | |
2756 | } | |
2757 | } | |
2758 | else if (expr_is_register (exp1)) | |
2759 | { | |
2760 | switch (cond->cmp) | |
2761 | { | |
2762 | case OP_EQUAL: | |
2763 | if (get_expr_register (exp1) != cond->op_data) | |
2764 | return FALSE; | |
2765 | break; | |
2766 | case OP_NOTEQUAL: | |
2767 | if (get_expr_register (exp1) == cond->op_data) | |
2768 | return FALSE; | |
2769 | break; | |
2770 | default: | |
2771 | return FALSE; | |
2772 | } | |
2773 | } | |
2774 | else | |
2775 | return FALSE; | |
2776 | break; | |
2777 | ||
2778 | case OP_OPERAND: | |
2779 | assert (cond->op_num < insn->ntok); | |
2780 | assert (cond->op_data < insn->ntok); | |
2781 | exp1 = &insn->tok[cond->op_num]; | |
2782 | exp2 = &insn->tok[cond->op_data]; | |
2783 | ||
2784 | switch (cond->cmp) | |
2785 | { | |
2786 | case OP_EQUAL: | |
2787 | if (!expr_is_equal (exp1, exp2)) | |
2788 | return FALSE; | |
2789 | break; | |
2790 | case OP_NOTEQUAL: | |
2791 | if (expr_is_equal (exp1, exp2)) | |
2792 | return FALSE; | |
2793 | break; | |
2794 | } | |
2795 | break; | |
2796 | ||
2797 | case OP_LITERAL: | |
2798 | case OP_LABEL: | |
2799 | default: | |
2800 | return FALSE; | |
2801 | } | |
2802 | } | |
2803 | if (!xg_instruction_matches_options (insn, rule->options)) | |
2804 | return FALSE; | |
2805 | ||
2806 | return TRUE; | |
2807 | } | |
2808 | ||
2809 | ||
2810 | static int | |
2811 | transition_rule_cmp (const TransitionRule *a, const TransitionRule *b) | |
2812 | { | |
2813 | bfd_boolean a_greater = FALSE; | |
2814 | bfd_boolean b_greater = FALSE; | |
2815 | ||
2816 | ReqOptionList *l_a = a->options; | |
2817 | ReqOptionList *l_b = b->options; | |
2818 | ||
2819 | /* We only care if they both are the same except for | |
2820 | a const16 vs. an l32r. */ | |
2821 | ||
2822 | while (l_a && l_b && ((l_a->next == NULL) == (l_b->next == NULL))) | |
2823 | { | |
2824 | ReqOrOptionList *l_or_a = l_a->or_option_terms; | |
2825 | ReqOrOptionList *l_or_b = l_b->or_option_terms; | |
2826 | while (l_or_a && l_or_b && ((l_a->next == NULL) == (l_b->next == NULL))) | |
2827 | { | |
2828 | if (l_or_a->is_true != l_or_b->is_true) | |
2829 | return 0; | |
2830 | if (strcmp (l_or_a->option_name, l_or_b->option_name) != 0) | |
2831 | { | |
2832 | /* This is the case we care about. */ | |
2833 | if (strcmp (l_or_a->option_name, "IsaUseConst16") == 0 | |
2834 | && strcmp (l_or_b->option_name, "IsaUseL32R") == 0) | |
2835 | { | |
2836 | if (prefer_const16) | |
2837 | a_greater = TRUE; | |
2838 | else | |
2839 | b_greater = TRUE; | |
2840 | } | |
2841 | else if (strcmp (l_or_a->option_name, "IsaUseL32R") == 0 | |
2842 | && strcmp (l_or_b->option_name, "IsaUseConst16") == 0) | |
2843 | { | |
2844 | if (prefer_const16) | |
2845 | b_greater = TRUE; | |
2846 | else | |
2847 | a_greater = TRUE; | |
2848 | } | |
2849 | else | |
2850 | return 0; | |
2851 | } | |
2852 | l_or_a = l_or_a->next; | |
2853 | l_or_b = l_or_b->next; | |
2854 | } | |
2855 | if (l_or_a || l_or_b) | |
2856 | return 0; | |
2857 | ||
2858 | l_a = l_a->next; | |
2859 | l_b = l_b->next; | |
2860 | } | |
2861 | if (l_a || l_b) | |
2862 | return 0; | |
2863 | ||
2864 | /* Incomparable if the substitution was used differently in two cases. */ | |
2865 | if (a_greater && b_greater) | |
2866 | return 0; | |
2867 | ||
2868 | if (b_greater) | |
2869 | return 1; | |
2870 | if (a_greater) | |
2871 | return -1; | |
2872 | ||
2873 | return 0; | |
2874 | } | |
2875 | ||
2876 | ||
2877 | static TransitionRule * | |
2878 | xg_instruction_match (TInsn *insn) | |
2879 | { | |
2880 | TransitionTable *table = xg_build_simplify_table (&transition_rule_cmp); | |
2881 | TransitionList *l; | |
2882 | assert (insn->opcode < table->num_opcodes); | |
2883 | ||
2884 | /* Walk through all of the possible transitions. */ | |
2885 | for (l = table->table[insn->opcode]; l != NULL; l = l->next) | |
2886 | { | |
2887 | TransitionRule *rule = l->rule; | |
2888 | if (xg_instruction_matches_rule (insn, rule)) | |
2889 | return rule; | |
2890 | } | |
2891 | return NULL; | |
2892 | } | |
2893 | ||
2894 | \f | |
2895 | /* Various Other Internal Functions. */ | |
2896 | ||
2897 | static bfd_boolean | |
2898 | is_unique_insn_expansion (TransitionRule *r) | |
2899 | { | |
2900 | if (!r->to_instr || r->to_instr->next != NULL) | |
2901 | return FALSE; | |
2902 | if (r->to_instr->typ != INSTR_INSTR) | |
2903 | return FALSE; | |
2904 | return TRUE; | |
2905 | } | |
2906 | ||
2907 | ||
84b08ed9 BW |
2908 | /* Check if there is exactly one relaxation for INSN that converts it to |
2909 | another instruction of equal or larger size. If so, and if TARG is | |
2910 | non-null, go ahead and generate the relaxed instruction into TARG. If | |
2911 | NARROW_ONLY is true, then only consider relaxations that widen a narrow | |
2912 | instruction, i.e., ignore relaxations that convert to an instruction of | |
2913 | equal size. In some contexts where this function is used, only | |
c138bc38 | 2914 | a single widening is allowed and the NARROW_ONLY argument is used to |
84b08ed9 BW |
2915 | exclude cases like ADDI being "widened" to an ADDMI, which may |
2916 | later be relaxed to an ADDMI/ADDI pair. */ | |
7fa3d080 | 2917 | |
84b08ed9 BW |
2918 | bfd_boolean |
2919 | xg_is_single_relaxable_insn (TInsn *insn, TInsn *targ, bfd_boolean narrow_only) | |
7fa3d080 BW |
2920 | { |
2921 | TransitionTable *table = xg_build_widen_table (&transition_rule_cmp); | |
2922 | TransitionList *l; | |
84b08ed9 | 2923 | TransitionRule *match = 0; |
7fa3d080 | 2924 | |
7fa3d080 BW |
2925 | assert (insn->insn_type == ITYPE_INSN); |
2926 | assert (insn->opcode < table->num_opcodes); | |
2927 | ||
2928 | for (l = table->table[insn->opcode]; l != NULL; l = l->next) | |
2929 | { | |
2930 | TransitionRule *rule = l->rule; | |
2931 | ||
2932 | if (xg_instruction_matches_rule (insn, rule) | |
84b08ed9 BW |
2933 | && is_unique_insn_expansion (rule) |
2934 | && (xg_get_single_size (insn->opcode) + (narrow_only ? 1 : 0) | |
2935 | <= xg_get_single_size (rule->to_instr->opcode))) | |
7fa3d080 | 2936 | { |
84b08ed9 BW |
2937 | if (match) |
2938 | return FALSE; | |
2939 | match = rule; | |
7fa3d080 BW |
2940 | } |
2941 | } | |
84b08ed9 BW |
2942 | if (!match) |
2943 | return FALSE; | |
2944 | ||
2945 | if (targ) | |
2946 | xg_build_to_insn (targ, insn, match->to_instr); | |
2947 | return TRUE; | |
7fa3d080 BW |
2948 | } |
2949 | ||
2950 | ||
2951 | /* Return the maximum number of bytes this opcode can expand to. */ | |
2952 | ||
2953 | static int | |
2954 | xg_get_max_insn_widen_size (xtensa_opcode opcode) | |
2955 | { | |
2956 | TransitionTable *table = xg_build_widen_table (&transition_rule_cmp); | |
2957 | TransitionList *l; | |
2958 | int max_size = xg_get_single_size (opcode); | |
2959 | ||
2960 | assert (opcode < table->num_opcodes); | |
2961 | ||
2962 | for (l = table->table[opcode]; l != NULL; l = l->next) | |
2963 | { | |
2964 | TransitionRule *rule = l->rule; | |
2965 | BuildInstr *build_list; | |
2966 | int this_size = 0; | |
2967 | ||
2968 | if (!rule) | |
2969 | continue; | |
2970 | build_list = rule->to_instr; | |
2971 | if (is_unique_insn_expansion (rule)) | |
2972 | { | |
2973 | assert (build_list->typ == INSTR_INSTR); | |
2974 | this_size = xg_get_max_insn_widen_size (build_list->opcode); | |
2975 | } | |
2976 | else | |
2977 | for (; build_list != NULL; build_list = build_list->next) | |
2978 | { | |
2979 | switch (build_list->typ) | |
2980 | { | |
2981 | case INSTR_INSTR: | |
2982 | this_size += xg_get_single_size (build_list->opcode); | |
2983 | break; | |
2984 | case INSTR_LITERAL_DEF: | |
2985 | case INSTR_LABEL_DEF: | |
e0001a05 NC |
2986 | default: |
2987 | break; | |
2988 | } | |
2989 | } | |
2990 | if (this_size > max_size) | |
2991 | max_size = this_size; | |
2992 | } | |
2993 | return max_size; | |
2994 | } | |
2995 | ||
2996 | ||
2997 | /* Return the maximum number of literal bytes this opcode can generate. */ | |
2998 | ||
7fa3d080 BW |
2999 | static int |
3000 | xg_get_max_insn_widen_literal_size (xtensa_opcode opcode) | |
e0001a05 | 3001 | { |
43cd72b9 | 3002 | TransitionTable *table = xg_build_widen_table (&transition_rule_cmp); |
e0001a05 NC |
3003 | TransitionList *l; |
3004 | int max_size = 0; | |
3005 | ||
3006 | assert (opcode < table->num_opcodes); | |
3007 | ||
3008 | for (l = table->table[opcode]; l != NULL; l = l->next) | |
3009 | { | |
3010 | TransitionRule *rule = l->rule; | |
3011 | BuildInstr *build_list; | |
3012 | int this_size = 0; | |
3013 | ||
3014 | if (!rule) | |
3015 | continue; | |
3016 | build_list = rule->to_instr; | |
3017 | if (is_unique_insn_expansion (rule)) | |
3018 | { | |
3019 | assert (build_list->typ == INSTR_INSTR); | |
3020 | this_size = xg_get_max_insn_widen_literal_size (build_list->opcode); | |
3021 | } | |
3022 | else | |
3023 | for (; build_list != NULL; build_list = build_list->next) | |
3024 | { | |
3025 | switch (build_list->typ) | |
3026 | { | |
3027 | case INSTR_LITERAL_DEF: | |
43cd72b9 | 3028 | /* Hard-coded 4-byte literal. */ |
e0001a05 NC |
3029 | this_size += 4; |
3030 | break; | |
3031 | case INSTR_INSTR: | |
3032 | case INSTR_LABEL_DEF: | |
3033 | default: | |
3034 | break; | |
3035 | } | |
3036 | } | |
3037 | if (this_size > max_size) | |
3038 | max_size = this_size; | |
3039 | } | |
3040 | return max_size; | |
3041 | } | |
3042 | ||
3043 | ||
7fa3d080 BW |
3044 | static bfd_boolean |
3045 | xg_is_relaxable_insn (TInsn *insn, int lateral_steps) | |
3046 | { | |
3047 | int steps_taken = 0; | |
3048 | TransitionTable *table = xg_build_widen_table (&transition_rule_cmp); | |
3049 | TransitionList *l; | |
3050 | ||
3051 | assert (insn->insn_type == ITYPE_INSN); | |
3052 | assert (insn->opcode < table->num_opcodes); | |
3053 | ||
3054 | for (l = table->table[insn->opcode]; l != NULL; l = l->next) | |
3055 | { | |
3056 | TransitionRule *rule = l->rule; | |
3057 | ||
3058 | if (xg_instruction_matches_rule (insn, rule)) | |
3059 | { | |
3060 | if (steps_taken == lateral_steps) | |
3061 | return TRUE; | |
3062 | steps_taken++; | |
3063 | } | |
3064 | } | |
3065 | return FALSE; | |
3066 | } | |
3067 | ||
3068 | ||
3069 | static symbolS * | |
3070 | get_special_literal_symbol (void) | |
3071 | { | |
3072 | static symbolS *sym = NULL; | |
3073 | ||
3074 | if (sym == NULL) | |
3075 | sym = symbol_find_or_make ("SPECIAL_LITERAL0\001"); | |
3076 | return sym; | |
3077 | } | |
3078 | ||
3079 | ||
3080 | static symbolS * | |
3081 | get_special_label_symbol (void) | |
3082 | { | |
3083 | static symbolS *sym = NULL; | |
3084 | ||
3085 | if (sym == NULL) | |
3086 | sym = symbol_find_or_make ("SPECIAL_LABEL0\001"); | |
3087 | return sym; | |
3088 | } | |
3089 | ||
3090 | ||
3091 | static bfd_boolean | |
3092 | xg_valid_literal_expression (const expressionS *exp) | |
3093 | { | |
3094 | switch (exp->X_op) | |
3095 | { | |
3096 | case O_constant: | |
3097 | case O_symbol: | |
3098 | case O_big: | |
3099 | case O_uminus: | |
3100 | case O_subtract: | |
3101 | case O_pltrel: | |
3102 | return TRUE; | |
3103 | default: | |
3104 | return FALSE; | |
3105 | } | |
3106 | } | |
3107 | ||
3108 | ||
3109 | /* This will check to see if the value can be converted into the | |
3110 | operand type. It will return TRUE if it does not fit. */ | |
3111 | ||
3112 | static bfd_boolean | |
3113 | xg_check_operand (int32 value, xtensa_opcode opcode, int operand) | |
3114 | { | |
3115 | uint32 valbuf = value; | |
3116 | if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf)) | |
3117 | return TRUE; | |
3118 | return FALSE; | |
3119 | } | |
3120 | ||
3121 | ||
3122 | /* Assumes: All immeds are constants. Check that all constants fit | |
3123 | into their immeds; return FALSE if not. */ | |
3124 | ||
3125 | static bfd_boolean | |
3126 | xg_immeds_fit (const TInsn *insn) | |
3127 | { | |
3128 | xtensa_isa isa = xtensa_default_isa; | |
3129 | int i; | |
3130 | ||
3131 | int n = insn->ntok; | |
3132 | assert (insn->insn_type == ITYPE_INSN); | |
3133 | for (i = 0; i < n; ++i) | |
3134 | { | |
3135 | const expressionS *expr = &insn->tok[i]; | |
3136 | if (xtensa_operand_is_register (isa, insn->opcode, i) == 1) | |
3137 | continue; | |
3138 | ||
3139 | switch (expr->X_op) | |
3140 | { | |
3141 | case O_register: | |
3142 | case O_constant: | |
3143 | if (xg_check_operand (expr->X_add_number, insn->opcode, i)) | |
3144 | return FALSE; | |
3145 | break; | |
3146 | ||
3147 | default: | |
3148 | /* The symbol should have a fixup associated with it. */ | |
3149 | assert (FALSE); | |
3150 | break; | |
3151 | } | |
3152 | } | |
3153 | return TRUE; | |
3154 | } | |
3155 | ||
3156 | ||
3157 | /* This should only be called after we have an initial | |
3158 | estimate of the addresses. */ | |
3159 | ||
3160 | static bfd_boolean | |
3161 | xg_symbolic_immeds_fit (const TInsn *insn, | |
3162 | segT pc_seg, | |
3163 | fragS *pc_frag, | |
3164 | offsetT pc_offset, | |
3165 | long stretch) | |
e0001a05 | 3166 | { |
7fa3d080 BW |
3167 | xtensa_isa isa = xtensa_default_isa; |
3168 | symbolS *symbolP; | |
3169 | fragS *sym_frag; | |
3170 | offsetT target, pc; | |
3171 | uint32 new_offset; | |
3172 | int i; | |
3173 | int n = insn->ntok; | |
e0001a05 NC |
3174 | |
3175 | assert (insn->insn_type == ITYPE_INSN); | |
e0001a05 | 3176 | |
7fa3d080 | 3177 | for (i = 0; i < n; ++i) |
e0001a05 | 3178 | { |
7fa3d080 BW |
3179 | const expressionS *expr = &insn->tok[i]; |
3180 | if (xtensa_operand_is_register (isa, insn->opcode, i) == 1) | |
3181 | continue; | |
e0001a05 | 3182 | |
7fa3d080 | 3183 | switch (expr->X_op) |
e0001a05 | 3184 | { |
7fa3d080 BW |
3185 | case O_register: |
3186 | case O_constant: | |
3187 | if (xg_check_operand (expr->X_add_number, insn->opcode, i)) | |
3188 | return FALSE; | |
3189 | break; | |
e0001a05 | 3190 | |
7fa3d080 BW |
3191 | case O_lo16: |
3192 | case O_hi16: | |
3193 | /* Check for the worst case. */ | |
3194 | if (xg_check_operand (0xffff, insn->opcode, i)) | |
3195 | return FALSE; | |
3196 | break; | |
e0001a05 | 3197 | |
7fa3d080 | 3198 | case O_symbol: |
7c834684 | 3199 | /* We only allow symbols for PC-relative references. |
7fa3d080 | 3200 | If pc_frag == 0, then we don't have frag locations yet. */ |
7c834684 BW |
3201 | if (pc_frag == 0 |
3202 | || xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 0) | |
7fa3d080 | 3203 | return FALSE; |
e0001a05 | 3204 | |
7c834684 BW |
3205 | /* If it is a weak symbol, then assume it won't reach. */ |
3206 | if (S_IS_WEAK (expr->X_add_symbol)) | |
7fa3d080 | 3207 | return FALSE; |
e0001a05 | 3208 | |
7c834684 BW |
3209 | if (is_direct_call_opcode (insn->opcode) |
3210 | && ! pc_frag->tc_frag_data.use_longcalls) | |
3211 | { | |
3212 | /* If callee is undefined or in a different segment, be | |
3213 | optimistic and assume it will be in range. */ | |
3214 | if (S_GET_SEGMENT (expr->X_add_symbol) != pc_seg) | |
3215 | return TRUE; | |
3216 | } | |
3217 | ||
3218 | /* Only references within a segment can be known to fit in the | |
3219 | operands at assembly time. */ | |
3220 | if (S_GET_SEGMENT (expr->X_add_symbol) != pc_seg) | |
7fa3d080 | 3221 | return FALSE; |
e0001a05 | 3222 | |
7fa3d080 BW |
3223 | symbolP = expr->X_add_symbol; |
3224 | sym_frag = symbol_get_frag (symbolP); | |
3225 | target = S_GET_VALUE (symbolP) + expr->X_add_number; | |
3226 | pc = pc_frag->fr_address + pc_offset; | |
e0001a05 | 3227 | |
7fa3d080 BW |
3228 | /* If frag has yet to be reached on this pass, assume it |
3229 | will move by STRETCH just as we did. If this is not so, | |
3230 | it will be because some frag between grows, and that will | |
3231 | force another pass. Beware zero-length frags. There | |
3232 | should be a faster way to do this. */ | |
3233 | ||
3234 | if (stretch != 0 | |
3235 | && sym_frag->relax_marker != pc_frag->relax_marker | |
3236 | && S_GET_SEGMENT (symbolP) == pc_seg) | |
3237 | { | |
3238 | target += stretch; | |
3239 | } | |
c138bc38 | 3240 | |
7fa3d080 BW |
3241 | new_offset = target; |
3242 | xtensa_operand_do_reloc (isa, insn->opcode, i, &new_offset, pc); | |
3243 | if (xg_check_operand (new_offset, insn->opcode, i)) | |
3244 | return FALSE; | |
3245 | break; | |
3246 | ||
3247 | default: | |
3248 | /* The symbol should have a fixup associated with it. */ | |
3249 | return FALSE; | |
3250 | } | |
3251 | } | |
3252 | ||
3253 | return TRUE; | |
e0001a05 NC |
3254 | } |
3255 | ||
3256 | ||
43cd72b9 | 3257 | /* Return TRUE on success. */ |
e0001a05 | 3258 | |
7fa3d080 BW |
3259 | static bfd_boolean |
3260 | xg_build_to_insn (TInsn *targ, TInsn *insn, BuildInstr *bi) | |
e0001a05 NC |
3261 | { |
3262 | BuildOp *op; | |
3263 | symbolS *sym; | |
3264 | ||
3265 | memset (targ, 0, sizeof (TInsn)); | |
7c430684 | 3266 | targ->linenum = insn->linenum; |
e0001a05 NC |
3267 | switch (bi->typ) |
3268 | { | |
3269 | case INSTR_INSTR: | |
3270 | op = bi->ops; | |
3271 | targ->opcode = bi->opcode; | |
3272 | targ->insn_type = ITYPE_INSN; | |
3273 | targ->is_specific_opcode = FALSE; | |
3274 | ||
3275 | for (; op != NULL; op = op->next) | |
3276 | { | |
3277 | int op_num = op->op_num; | |
3278 | int op_data = op->op_data; | |
3279 | ||
3280 | assert (op->op_num < MAX_INSN_ARGS); | |
3281 | ||
3282 | if (targ->ntok <= op_num) | |
3283 | targ->ntok = op_num + 1; | |
3284 | ||
3285 | switch (op->typ) | |
3286 | { | |
3287 | case OP_CONSTANT: | |
3288 | set_expr_const (&targ->tok[op_num], op_data); | |
3289 | break; | |
3290 | case OP_OPERAND: | |
3291 | assert (op_data < insn->ntok); | |
3292 | copy_expr (&targ->tok[op_num], &insn->tok[op_data]); | |
3293 | break; | |
3294 | case OP_LITERAL: | |
3295 | sym = get_special_literal_symbol (); | |
3296 | set_expr_symbol_offset (&targ->tok[op_num], sym, 0); | |
3297 | break; | |
3298 | case OP_LABEL: | |
3299 | sym = get_special_label_symbol (); | |
3300 | set_expr_symbol_offset (&targ->tok[op_num], sym, 0); | |
3301 | break; | |
43cd72b9 BW |
3302 | case OP_OPERAND_HI16U: |
3303 | case OP_OPERAND_LOW16U: | |
3304 | assert (op_data < insn->ntok); | |
3305 | if (expr_is_const (&insn->tok[op_data])) | |
3306 | { | |
3307 | long val; | |
3308 | copy_expr (&targ->tok[op_num], &insn->tok[op_data]); | |
3309 | val = xg_apply_userdef_op_fn (op->typ, | |
3310 | targ->tok[op_num]. | |
3311 | X_add_number); | |
3312 | targ->tok[op_num].X_add_number = val; | |
3313 | } | |
3314 | else | |
3315 | { | |
3316 | /* For const16 we can create relocations for these. */ | |
3317 | if (targ->opcode == XTENSA_UNDEFINED | |
3318 | || (targ->opcode != xtensa_const16_opcode)) | |
3319 | return FALSE; | |
3320 | assert (op_data < insn->ntok); | |
3321 | /* Need to build a O_lo16 or O_hi16. */ | |
3322 | copy_expr (&targ->tok[op_num], &insn->tok[op_data]); | |
3323 | if (targ->tok[op_num].X_op == O_symbol) | |
3324 | { | |
3325 | if (op->typ == OP_OPERAND_HI16U) | |
3326 | targ->tok[op_num].X_op = O_hi16; | |
3327 | else if (op->typ == OP_OPERAND_LOW16U) | |
3328 | targ->tok[op_num].X_op = O_lo16; | |
3329 | else | |
3330 | return FALSE; | |
3331 | } | |
3332 | } | |
3333 | break; | |
e0001a05 NC |
3334 | default: |
3335 | /* currently handles: | |
3336 | OP_OPERAND_LOW8 | |
3337 | OP_OPERAND_HI24S | |
3338 | OP_OPERAND_F32MINUS */ | |
3339 | if (xg_has_userdef_op_fn (op->typ)) | |
3340 | { | |
3341 | assert (op_data < insn->ntok); | |
3342 | if (expr_is_const (&insn->tok[op_data])) | |
3343 | { | |
3344 | long val; | |
3345 | copy_expr (&targ->tok[op_num], &insn->tok[op_data]); | |
3346 | val = xg_apply_userdef_op_fn (op->typ, | |
3347 | targ->tok[op_num]. | |
3348 | X_add_number); | |
3349 | targ->tok[op_num].X_add_number = val; | |
3350 | } | |
3351 | else | |
3352 | return FALSE; /* We cannot use a relocation for this. */ | |
3353 | break; | |
3354 | } | |
3355 | assert (0); | |
3356 | break; | |
3357 | } | |
3358 | } | |
3359 | break; | |
3360 | ||
3361 | case INSTR_LITERAL_DEF: | |
3362 | op = bi->ops; | |
3363 | targ->opcode = XTENSA_UNDEFINED; | |
3364 | targ->insn_type = ITYPE_LITERAL; | |
3365 | targ->is_specific_opcode = FALSE; | |
3366 | for (; op != NULL; op = op->next) | |
3367 | { | |
3368 | int op_num = op->op_num; | |
3369 | int op_data = op->op_data; | |
3370 | assert (op->op_num < MAX_INSN_ARGS); | |
3371 | ||
3372 | if (targ->ntok <= op_num) | |
3373 | targ->ntok = op_num + 1; | |
3374 | ||
3375 | switch (op->typ) | |
3376 | { | |
3377 | case OP_OPERAND: | |
3378 | assert (op_data < insn->ntok); | |
43cd72b9 BW |
3379 | /* We can only pass resolvable literals through. */ |
3380 | if (!xg_valid_literal_expression (&insn->tok[op_data])) | |
3381 | return FALSE; | |
e0001a05 NC |
3382 | copy_expr (&targ->tok[op_num], &insn->tok[op_data]); |
3383 | break; | |
3384 | case OP_LITERAL: | |
3385 | case OP_CONSTANT: | |
3386 | case OP_LABEL: | |
3387 | default: | |
3388 | assert (0); | |
3389 | break; | |
3390 | } | |
3391 | } | |
3392 | break; | |
3393 | ||
3394 | case INSTR_LABEL_DEF: | |
3395 | op = bi->ops; | |
3396 | targ->opcode = XTENSA_UNDEFINED; | |
3397 | targ->insn_type = ITYPE_LABEL; | |
3398 | targ->is_specific_opcode = FALSE; | |
43cd72b9 | 3399 | /* Literal with no ops is a label? */ |
e0001a05 NC |
3400 | assert (op == NULL); |
3401 | break; | |
3402 | ||
3403 | default: | |
3404 | assert (0); | |
3405 | } | |
3406 | ||
3407 | return TRUE; | |
3408 | } | |
3409 | ||
3410 | ||
43cd72b9 | 3411 | /* Return TRUE on success. */ |
e0001a05 | 3412 | |
7fa3d080 BW |
3413 | static bfd_boolean |
3414 | xg_build_to_stack (IStack *istack, TInsn *insn, BuildInstr *bi) | |
e0001a05 NC |
3415 | { |
3416 | for (; bi != NULL; bi = bi->next) | |
3417 | { | |
3418 | TInsn *next_insn = istack_push_space (istack); | |
3419 | ||
3420 | if (!xg_build_to_insn (next_insn, insn, bi)) | |
3421 | return FALSE; | |
3422 | } | |
3423 | return TRUE; | |
3424 | } | |
3425 | ||
3426 | ||
43cd72b9 | 3427 | /* Return TRUE on valid expansion. */ |
e0001a05 | 3428 | |
7fa3d080 BW |
3429 | static bfd_boolean |
3430 | xg_expand_to_stack (IStack *istack, TInsn *insn, int lateral_steps) | |
e0001a05 NC |
3431 | { |
3432 | int stack_size = istack->ninsn; | |
3433 | int steps_taken = 0; | |
43cd72b9 | 3434 | TransitionTable *table = xg_build_widen_table (&transition_rule_cmp); |
e0001a05 NC |
3435 | TransitionList *l; |
3436 | ||
3437 | assert (insn->insn_type == ITYPE_INSN); | |
3438 | assert (insn->opcode < table->num_opcodes); | |
3439 | ||
3440 | for (l = table->table[insn->opcode]; l != NULL; l = l->next) | |
3441 | { | |
3442 | TransitionRule *rule = l->rule; | |
3443 | ||
3444 | if (xg_instruction_matches_rule (insn, rule)) | |
3445 | { | |
3446 | if (lateral_steps == steps_taken) | |
3447 | { | |
3448 | int i; | |
3449 | ||
3450 | /* This is it. Expand the rule to the stack. */ | |
3451 | if (!xg_build_to_stack (istack, insn, rule->to_instr)) | |
3452 | return FALSE; | |
3453 | ||
3454 | /* Check to see if it fits. */ | |
3455 | for (i = stack_size; i < istack->ninsn; i++) | |
3456 | { | |
3457 | TInsn *insn = &istack->insn[i]; | |
3458 | ||
3459 | if (insn->insn_type == ITYPE_INSN | |
3460 | && !tinsn_has_symbolic_operands (insn) | |
3461 | && !xg_immeds_fit (insn)) | |
3462 | { | |
3463 | istack->ninsn = stack_size; | |
3464 | return FALSE; | |
3465 | } | |
3466 | } | |
3467 | return TRUE; | |
3468 | } | |
3469 | steps_taken++; | |
3470 | } | |
3471 | } | |
3472 | return FALSE; | |
3473 | } | |
3474 | ||
43cd72b9 | 3475 | \f |
43cd72b9 BW |
3476 | /* Relax the assembly instruction at least "min_steps". |
3477 | Return the number of steps taken. */ | |
e0001a05 | 3478 | |
7fa3d080 BW |
3479 | static int |
3480 | xg_assembly_relax (IStack *istack, | |
3481 | TInsn *insn, | |
3482 | segT pc_seg, | |
3483 | fragS *pc_frag, /* if pc_frag == 0, not pc-relative */ | |
3484 | offsetT pc_offset, /* offset in fragment */ | |
3485 | int min_steps, /* minimum conversion steps */ | |
3486 | long stretch) /* number of bytes stretched so far */ | |
e0001a05 NC |
3487 | { |
3488 | int steps_taken = 0; | |
3489 | ||
3490 | /* assert (has no symbolic operands) | |
3491 | Some of its immeds don't fit. | |
3492 | Try to build a relaxed version. | |
3493 | This may go through a couple of stages | |
3494 | of single instruction transformations before | |
3495 | we get there. */ | |
3496 | ||
3497 | TInsn single_target; | |
3498 | TInsn current_insn; | |
3499 | int lateral_steps = 0; | |
3500 | int istack_size = istack->ninsn; | |
3501 | ||
3502 | if (xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch) | |
3503 | && steps_taken >= min_steps) | |
3504 | { | |
3505 | istack_push (istack, insn); | |
3506 | return steps_taken; | |
3507 | } | |
43cd72b9 | 3508 | current_insn = *insn; |
e0001a05 | 3509 | |
7c834684 | 3510 | /* Walk through all of the single instruction expansions. */ |
84b08ed9 | 3511 | while (xg_is_single_relaxable_insn (¤t_insn, &single_target, FALSE)) |
e0001a05 | 3512 | { |
21af2bbd | 3513 | steps_taken++; |
e0001a05 NC |
3514 | if (xg_symbolic_immeds_fit (&single_target, pc_seg, pc_frag, pc_offset, |
3515 | stretch)) | |
3516 | { | |
e0001a05 NC |
3517 | if (steps_taken >= min_steps) |
3518 | { | |
3519 | istack_push (istack, &single_target); | |
3520 | return steps_taken; | |
3521 | } | |
3522 | } | |
43cd72b9 | 3523 | current_insn = single_target; |
e0001a05 NC |
3524 | } |
3525 | ||
3526 | /* Now check for a multi-instruction expansion. */ | |
3527 | while (xg_is_relaxable_insn (¤t_insn, lateral_steps)) | |
3528 | { | |
3529 | if (xg_symbolic_immeds_fit (¤t_insn, pc_seg, pc_frag, pc_offset, | |
3530 | stretch)) | |
3531 | { | |
3532 | if (steps_taken >= min_steps) | |
3533 | { | |
3534 | istack_push (istack, ¤t_insn); | |
3535 | return steps_taken; | |
3536 | } | |
3537 | } | |
3538 | steps_taken++; | |
3539 | if (xg_expand_to_stack (istack, ¤t_insn, lateral_steps)) | |
3540 | { | |
3541 | if (steps_taken >= min_steps) | |
3542 | return steps_taken; | |
3543 | } | |
3544 | lateral_steps++; | |
3545 | istack->ninsn = istack_size; | |
3546 | } | |
3547 | ||
3548 | /* It's not going to work -- use the original. */ | |
3549 | istack_push (istack, insn); | |
3550 | return steps_taken; | |
3551 | } | |
3552 | ||
3553 | ||
3554 | static void | |
7fa3d080 | 3555 | xg_force_frag_space (int size) |
e0001a05 NC |
3556 | { |
3557 | /* This may have the side effect of creating a new fragment for the | |
3558 | space to go into. I just do not like the name of the "frag" | |
3559 | functions. */ | |
3560 | frag_grow (size); | |
3561 | } | |
3562 | ||
3563 | ||
7fa3d080 BW |
3564 | static void |
3565 | xg_finish_frag (char *last_insn, | |
3566 | enum xtensa_relax_statesE frag_state, | |
3567 | enum xtensa_relax_statesE slot0_state, | |
3568 | int max_growth, | |
3569 | bfd_boolean is_insn) | |
e0001a05 NC |
3570 | { |
3571 | /* Finish off this fragment so that it has at LEAST the desired | |
3572 | max_growth. If it doesn't fit in this fragment, close this one | |
3573 | and start a new one. In either case, return a pointer to the | |
3574 | beginning of the growth area. */ | |
3575 | ||
3576 | fragS *old_frag; | |
43cd72b9 | 3577 | |
e0001a05 NC |
3578 | xg_force_frag_space (max_growth); |
3579 | ||
3580 | old_frag = frag_now; | |
3581 | ||
3582 | frag_now->fr_opcode = last_insn; | |
3583 | if (is_insn) | |
3584 | frag_now->tc_frag_data.is_insn = TRUE; | |
3585 | ||
3586 | frag_var (rs_machine_dependent, max_growth, max_growth, | |
43cd72b9 BW |
3587 | frag_state, frag_now->fr_symbol, frag_now->fr_offset, last_insn); |
3588 | ||
3589 | old_frag->tc_frag_data.slot_subtypes[0] = slot0_state; | |
3590 | xtensa_set_frag_assembly_state (frag_now); | |
e0001a05 NC |
3591 | |
3592 | /* Just to make sure that we did not split it up. */ | |
3593 | assert (old_frag->fr_next == frag_now); | |
3594 | } | |
3595 | ||
3596 | ||
7fa3d080 BW |
3597 | /* Return TRUE if the target frag is one of the next non-empty frags. */ |
3598 | ||
3599 | static bfd_boolean | |
3600 | is_next_frag_target (const fragS *fragP, const fragS *target) | |
3601 | { | |
3602 | if (fragP == NULL) | |
3603 | return FALSE; | |
3604 | ||
3605 | for (; fragP; fragP = fragP->fr_next) | |
3606 | { | |
3607 | if (fragP == target) | |
3608 | return TRUE; | |
3609 | if (fragP->fr_fix != 0) | |
3610 | return FALSE; | |
3611 | if (fragP->fr_type == rs_fill && fragP->fr_offset != 0) | |
3612 | return FALSE; | |
3613 | if ((fragP->fr_type == rs_align || fragP->fr_type == rs_align_code) | |
3614 | && ((fragP->fr_address % (1 << fragP->fr_offset)) != 0)) | |
3615 | return FALSE; | |
3616 | if (fragP->fr_type == rs_space) | |
3617 | return FALSE; | |
3618 | } | |
3619 | return FALSE; | |
3620 | } | |
3621 | ||
3622 | ||
e0001a05 | 3623 | static bfd_boolean |
7fa3d080 | 3624 | is_branch_jmp_to_next (TInsn *insn, fragS *fragP) |
e0001a05 NC |
3625 | { |
3626 | xtensa_isa isa = xtensa_default_isa; | |
3627 | int i; | |
43cd72b9 | 3628 | int num_ops = xtensa_opcode_num_operands (isa, insn->opcode); |
e0001a05 NC |
3629 | int target_op = -1; |
3630 | symbolS *sym; | |
3631 | fragS *target_frag; | |
3632 | ||
64b607e6 BW |
3633 | if (xtensa_opcode_is_branch (isa, insn->opcode) != 1 |
3634 | && xtensa_opcode_is_jump (isa, insn->opcode) != 1) | |
e0001a05 NC |
3635 | return FALSE; |
3636 | ||
3637 | for (i = 0; i < num_ops; i++) | |
3638 | { | |
43cd72b9 | 3639 | if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1) |
e0001a05 NC |
3640 | { |
3641 | target_op = i; | |
3642 | break; | |
3643 | } | |
3644 | } | |
3645 | if (target_op == -1) | |
3646 | return FALSE; | |
3647 | ||
3648 | if (insn->ntok <= target_op) | |
3649 | return FALSE; | |
3650 | ||
3651 | if (insn->tok[target_op].X_op != O_symbol) | |
3652 | return FALSE; | |
3653 | ||
3654 | sym = insn->tok[target_op].X_add_symbol; | |
3655 | if (sym == NULL) | |
3656 | return FALSE; | |
3657 | ||
3658 | if (insn->tok[target_op].X_add_number != 0) | |
3659 | return FALSE; | |
3660 | ||
3661 | target_frag = symbol_get_frag (sym); | |
3662 | if (target_frag == NULL) | |
3663 | return FALSE; | |
3664 | ||
c138bc38 | 3665 | if (is_next_frag_target (fragP->fr_next, target_frag) |
e0001a05 NC |
3666 | && S_GET_VALUE (sym) == target_frag->fr_address) |
3667 | return TRUE; | |
3668 | ||
3669 | return FALSE; | |
3670 | } | |
3671 | ||
3672 | ||
3673 | static void | |
7fa3d080 | 3674 | xg_add_branch_and_loop_targets (TInsn *insn) |
e0001a05 NC |
3675 | { |
3676 | xtensa_isa isa = xtensa_default_isa; | |
7fa3d080 | 3677 | int num_ops = xtensa_opcode_num_operands (isa, insn->opcode); |
43cd72b9 | 3678 | |
7fa3d080 BW |
3679 | if (xtensa_opcode_is_loop (isa, insn->opcode) == 1) |
3680 | { | |
3681 | int i = 1; | |
3682 | if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1 | |
3683 | && insn->tok[i].X_op == O_symbol) | |
3684 | symbol_get_tc (insn->tok[i].X_add_symbol)->is_loop_target = TRUE; | |
3685 | return; | |
3686 | } | |
e0001a05 | 3687 | |
7fa3d080 BW |
3688 | if (xtensa_opcode_is_branch (isa, insn->opcode) == 1 |
3689 | || xtensa_opcode_is_loop (isa, insn->opcode) == 1) | |
e0001a05 | 3690 | { |
7fa3d080 BW |
3691 | int i; |
3692 | ||
3693 | for (i = 0; i < insn->ntok && i < num_ops; i++) | |
3694 | { | |
3695 | if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1 | |
3696 | && insn->tok[i].X_op == O_symbol) | |
3697 | { | |
3698 | symbolS *sym = insn->tok[i].X_add_symbol; | |
3699 | symbol_get_tc (sym)->is_branch_target = TRUE; | |
3700 | if (S_IS_DEFINED (sym)) | |
3701 | symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE; | |
3702 | } | |
3703 | } | |
e0001a05 | 3704 | } |
e0001a05 NC |
3705 | } |
3706 | ||
3707 | ||
43cd72b9 | 3708 | /* Return FALSE if no error. */ |
e0001a05 | 3709 | |
7fa3d080 BW |
3710 | static bfd_boolean |
3711 | xg_build_token_insn (BuildInstr *instr_spec, TInsn *old_insn, TInsn *new_insn) | |
e0001a05 NC |
3712 | { |
3713 | int num_ops = 0; | |
3714 | BuildOp *b_op; | |
3715 | ||
3716 | switch (instr_spec->typ) | |
3717 | { | |
3718 | case INSTR_INSTR: | |
3719 | new_insn->insn_type = ITYPE_INSN; | |
3720 | new_insn->opcode = instr_spec->opcode; | |
3721 | new_insn->is_specific_opcode = FALSE; | |
7c430684 | 3722 | new_insn->linenum = old_insn->linenum; |
e0001a05 NC |
3723 | break; |
3724 | case INSTR_LITERAL_DEF: | |
3725 | new_insn->insn_type = ITYPE_LITERAL; | |
3726 | new_insn->opcode = XTENSA_UNDEFINED; | |
3727 | new_insn->is_specific_opcode = FALSE; | |
7c430684 | 3728 | new_insn->linenum = old_insn->linenum; |
e0001a05 NC |
3729 | break; |
3730 | case INSTR_LABEL_DEF: | |
3731 | as_bad (_("INSTR_LABEL_DEF not supported yet")); | |
3732 | break; | |
3733 | } | |
3734 | ||
3735 | for (b_op = instr_spec->ops; b_op != NULL; b_op = b_op->next) | |
3736 | { | |
3737 | expressionS *exp; | |
3738 | const expressionS *src_exp; | |
3739 | ||
3740 | num_ops++; | |
3741 | switch (b_op->typ) | |
3742 | { | |
3743 | case OP_CONSTANT: | |
3744 | /* The expression must be the constant. */ | |
3745 | assert (b_op->op_num < MAX_INSN_ARGS); | |
3746 | exp = &new_insn->tok[b_op->op_num]; | |
3747 | set_expr_const (exp, b_op->op_data); | |
3748 | break; | |
3749 | ||
3750 | case OP_OPERAND: | |
3751 | assert (b_op->op_num < MAX_INSN_ARGS); | |
3752 | assert (b_op->op_data < (unsigned) old_insn->ntok); | |
3753 | src_exp = &old_insn->tok[b_op->op_data]; | |
3754 | exp = &new_insn->tok[b_op->op_num]; | |
3755 | copy_expr (exp, src_exp); | |
3756 | break; | |
3757 | ||
3758 | case OP_LITERAL: | |
3759 | case OP_LABEL: | |
3760 | as_bad (_("can't handle generation of literal/labels yet")); | |
3761 | assert (0); | |
3762 | ||
3763 | default: | |
3764 | as_bad (_("can't handle undefined OP TYPE")); | |
3765 | assert (0); | |
3766 | } | |
3767 | } | |
3768 | ||
3769 | new_insn->ntok = num_ops; | |
3770 | return FALSE; | |
3771 | } | |
3772 | ||
3773 | ||
43cd72b9 | 3774 | /* Return TRUE if it was simplified. */ |
e0001a05 | 3775 | |
7fa3d080 BW |
3776 | static bfd_boolean |
3777 | xg_simplify_insn (TInsn *old_insn, TInsn *new_insn) | |
e0001a05 | 3778 | { |
43cd72b9 | 3779 | TransitionRule *rule; |
e0001a05 | 3780 | BuildInstr *insn_spec; |
43cd72b9 BW |
3781 | |
3782 | if (old_insn->is_specific_opcode || !density_supported) | |
3783 | return FALSE; | |
3784 | ||
3785 | rule = xg_instruction_match (old_insn); | |
e0001a05 NC |
3786 | if (rule == NULL) |
3787 | return FALSE; | |
3788 | ||
3789 | insn_spec = rule->to_instr; | |
3790 | /* There should only be one. */ | |
3791 | assert (insn_spec != NULL); | |
3792 | assert (insn_spec->next == NULL); | |
3793 | if (insn_spec->next != NULL) | |
3794 | return FALSE; | |
3795 | ||
3796 | xg_build_token_insn (insn_spec, old_insn, new_insn); | |
3797 | ||
3798 | return TRUE; | |
3799 | } | |
3800 | ||
3801 | ||
3802 | /* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i -> | |
3803 | l32i.n. (2) Check the number of operands. (3) Place the instruction | |
7c834684 BW |
3804 | tokens into the stack or relax it and place multiple |
3805 | instructions/literals onto the stack. Return FALSE if no error. */ | |
e0001a05 NC |
3806 | |
3807 | static bfd_boolean | |
7fa3d080 | 3808 | xg_expand_assembly_insn (IStack *istack, TInsn *orig_insn) |
e0001a05 NC |
3809 | { |
3810 | int noperands; | |
3811 | TInsn new_insn; | |
7c834684 BW |
3812 | bfd_boolean do_expand; |
3813 | ||
e0001a05 NC |
3814 | memset (&new_insn, 0, sizeof (TInsn)); |
3815 | ||
43cd72b9 BW |
3816 | /* Narrow it if we can. xg_simplify_insn now does all the |
3817 | appropriate checking (e.g., for the density option). */ | |
3818 | if (xg_simplify_insn (orig_insn, &new_insn)) | |
3819 | orig_insn = &new_insn; | |
e0001a05 | 3820 | |
43cd72b9 BW |
3821 | noperands = xtensa_opcode_num_operands (xtensa_default_isa, |
3822 | orig_insn->opcode); | |
e0001a05 NC |
3823 | if (orig_insn->ntok < noperands) |
3824 | { | |
3825 | as_bad (_("found %d operands for '%s': Expected %d"), | |
3826 | orig_insn->ntok, | |
3827 | xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode), | |
3828 | noperands); | |
3829 | return TRUE; | |
3830 | } | |
3831 | if (orig_insn->ntok > noperands) | |
3832 | as_warn (_("found too many (%d) operands for '%s': Expected %d"), | |
3833 | orig_insn->ntok, | |
3834 | xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode), | |
3835 | noperands); | |
3836 | ||
43cd72b9 | 3837 | /* If there are not enough operands, we will assert above. If there |
e0001a05 | 3838 | are too many, just cut out the extras here. */ |
e0001a05 NC |
3839 | orig_insn->ntok = noperands; |
3840 | ||
e0001a05 NC |
3841 | if (tinsn_has_invalid_symbolic_operands (orig_insn)) |
3842 | return TRUE; | |
3843 | ||
7c834684 BW |
3844 | /* If the instruction will definitely need to be relaxed, it is better |
3845 | to expand it now for better scheduling. Decide whether to expand | |
3846 | now.... */ | |
3847 | do_expand = (!orig_insn->is_specific_opcode && use_transform ()); | |
3848 | ||
3849 | /* Calls should be expanded to longcalls only in the backend relaxation | |
3850 | so that the assembly scheduler will keep the L32R/CALLX instructions | |
3851 | adjacent. */ | |
3852 | if (is_direct_call_opcode (orig_insn->opcode)) | |
3853 | do_expand = FALSE; | |
e0001a05 NC |
3854 | |
3855 | if (tinsn_has_symbolic_operands (orig_insn)) | |
3856 | { | |
7c834684 BW |
3857 | /* The values of symbolic operands are not known yet, so only expand |
3858 | now if an operand is "complex" (e.g., difference of symbols) and | |
3859 | will have to be stored as a literal regardless of the value. */ | |
3860 | if (!tinsn_has_complex_operands (orig_insn)) | |
3861 | do_expand = FALSE; | |
e0001a05 | 3862 | } |
7c834684 BW |
3863 | else if (xg_immeds_fit (orig_insn)) |
3864 | do_expand = FALSE; | |
3865 | ||
3866 | if (do_expand) | |
3867 | xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0); | |
e0001a05 | 3868 | else |
7c834684 | 3869 | istack_push (istack, orig_insn); |
e0001a05 | 3870 | |
e0001a05 NC |
3871 | return FALSE; |
3872 | } | |
3873 | ||
3874 | ||
7fa3d080 | 3875 | /* Return TRUE if the section flags are marked linkonce |
74869ac7 BW |
3876 | or the name is .gnu.linkonce.*. */ |
3877 | ||
3878 | static int linkonce_len = sizeof (".gnu.linkonce.") - 1; | |
7fa3d080 BW |
3879 | |
3880 | static bfd_boolean | |
3881 | get_is_linkonce_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec) | |
3882 | { | |
3883 | flagword flags, link_once_flags; | |
3884 | ||
3885 | flags = bfd_get_section_flags (abfd, sec); | |
3886 | link_once_flags = (flags & SEC_LINK_ONCE); | |
3887 | ||
3888 | /* Flags might not be set yet. */ | |
74869ac7 BW |
3889 | if (!link_once_flags |
3890 | && strncmp (segment_name (sec), ".gnu.linkonce.", linkonce_len) == 0) | |
3891 | link_once_flags = SEC_LINK_ONCE; | |
7fa3d080 | 3892 | |
7fa3d080 BW |
3893 | return (link_once_flags != 0); |
3894 | } | |
3895 | ||
3896 | ||
3897 | static void | |
3898 | xtensa_add_literal_sym (symbolS *sym) | |
3899 | { | |
3900 | sym_list *l; | |
3901 | ||
3902 | l = (sym_list *) xmalloc (sizeof (sym_list)); | |
3903 | l->sym = sym; | |
3904 | l->next = literal_syms; | |
3905 | literal_syms = l; | |
3906 | } | |
3907 | ||
3908 | ||
3909 | static symbolS * | |
3910 | xtensa_create_literal_symbol (segT sec, fragS *frag) | |
3911 | { | |
3912 | static int lit_num = 0; | |
3913 | static char name[256]; | |
3914 | symbolS *symbolP; | |
3915 | ||
3916 | sprintf (name, ".L_lit_sym%d", lit_num); | |
3917 | ||
3918 | /* Create a local symbol. If it is in a linkonce section, we have to | |
3919 | be careful to make sure that if it is used in a relocation that the | |
3920 | symbol will be in the output file. */ | |
3921 | if (get_is_linkonce_section (stdoutput, sec)) | |
3922 | { | |
3923 | symbolP = symbol_new (name, sec, 0, frag); | |
3924 | S_CLEAR_EXTERNAL (symbolP); | |
3925 | /* symbolP->local = 1; */ | |
3926 | } | |
3927 | else | |
3928 | symbolP = symbol_new (name, sec, 0, frag); | |
3929 | ||
3930 | xtensa_add_literal_sym (symbolP); | |
3931 | ||
7fa3d080 BW |
3932 | lit_num++; |
3933 | return symbolP; | |
3934 | } | |
3935 | ||
3936 | ||
e0001a05 NC |
3937 | /* Currently all literals that are generated here are 32-bit L32R targets. */ |
3938 | ||
7fa3d080 BW |
3939 | static symbolS * |
3940 | xg_assemble_literal (/* const */ TInsn *insn) | |
e0001a05 NC |
3941 | { |
3942 | emit_state state; | |
3943 | symbolS *lit_sym = NULL; | |
3944 | ||
3945 | /* size = 4 for L32R. It could easily be larger when we move to | |
3946 | larger constants. Add a parameter later. */ | |
3947 | offsetT litsize = 4; | |
3948 | offsetT litalign = 2; /* 2^2 = 4 */ | |
3949 | expressionS saved_loc; | |
43cd72b9 BW |
3950 | expressionS * emit_val; |
3951 | ||
e0001a05 NC |
3952 | set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ()); |
3953 | ||
3954 | assert (insn->insn_type == ITYPE_LITERAL); | |
77cd6497 | 3955 | assert (insn->ntok == 1); /* must be only one token here */ |
e0001a05 NC |
3956 | |
3957 | xtensa_switch_to_literal_fragment (&state); | |
3958 | ||
43cd72b9 BW |
3959 | emit_val = &insn->tok[0]; |
3960 | if (emit_val->X_op == O_big) | |
3961 | { | |
3962 | int size = emit_val->X_add_number * CHARS_PER_LITTLENUM; | |
3963 | if (size > litsize) | |
3964 | { | |
3965 | /* This happens when someone writes a "movi a2, big_number". */ | |
c138bc38 | 3966 | as_bad_where (frag_now->fr_file, frag_now->fr_line, |
43cd72b9 BW |
3967 | _("invalid immediate")); |
3968 | xtensa_restore_emit_state (&state); | |
3969 | return NULL; | |
3970 | } | |
3971 | } | |
3972 | ||
e0001a05 NC |
3973 | /* Force a 4-byte align here. Note that this opens a new frag, so all |
3974 | literals done with this function have a frag to themselves. That's | |
3975 | important for the way text section literals work. */ | |
3976 | frag_align (litalign, 0, 0); | |
43cd72b9 | 3977 | record_alignment (now_seg, litalign); |
e0001a05 | 3978 | |
43cd72b9 BW |
3979 | if (emit_val->X_op == O_pltrel) |
3980 | { | |
3981 | char *p = frag_more (litsize); | |
3982 | xtensa_set_frag_assembly_state (frag_now); | |
3983 | if (emit_val->X_add_symbol) | |
3984 | emit_val->X_op = O_symbol; | |
3985 | else | |
3986 | emit_val->X_op = O_constant; | |
3987 | fix_new_exp (frag_now, p - frag_now->fr_literal, | |
3988 | litsize, emit_val, 0, BFD_RELOC_XTENSA_PLT); | |
3989 | } | |
3990 | else | |
3991 | emit_expr (emit_val, litsize); | |
e0001a05 NC |
3992 | |
3993 | assert (frag_now->tc_frag_data.literal_frag == NULL); | |
3994 | frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg); | |
3995 | frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now); | |
3996 | lit_sym = frag_now->fr_symbol; | |
e0001a05 NC |
3997 | |
3998 | /* Go back. */ | |
3999 | xtensa_restore_emit_state (&state); | |
4000 | return lit_sym; | |
4001 | } | |
4002 | ||
4003 | ||
4004 | static void | |
7fa3d080 | 4005 | xg_assemble_literal_space (/* const */ int size, int slot) |
e0001a05 NC |
4006 | { |
4007 | emit_state state; | |
43cd72b9 | 4008 | /* We might have to do something about this alignment. It only |
e0001a05 NC |
4009 | takes effect if something is placed here. */ |
4010 | offsetT litalign = 2; /* 2^2 = 4 */ | |
4011 | fragS *lit_saved_frag; | |
4012 | ||
e0001a05 | 4013 | assert (size % 4 == 0); |
e0001a05 NC |
4014 | |
4015 | xtensa_switch_to_literal_fragment (&state); | |
4016 | ||
4017 | /* Force a 4-byte align here. */ | |
4018 | frag_align (litalign, 0, 0); | |
43cd72b9 | 4019 | record_alignment (now_seg, litalign); |
e0001a05 NC |
4020 | |
4021 | xg_force_frag_space (size); | |
4022 | ||
4023 | lit_saved_frag = frag_now; | |
4024 | frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg); | |
e0001a05 | 4025 | frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now); |
43cd72b9 | 4026 | xg_finish_frag (0, RELAX_LITERAL, 0, size, FALSE); |
e0001a05 NC |
4027 | |
4028 | /* Go back. */ | |
4029 | xtensa_restore_emit_state (&state); | |
43cd72b9 | 4030 | frag_now->tc_frag_data.literal_frags[slot] = lit_saved_frag; |
e0001a05 NC |
4031 | } |
4032 | ||
4033 | ||
e0001a05 | 4034 | /* Put in a fixup record based on the opcode. |
43cd72b9 | 4035 | Return TRUE on success. */ |
e0001a05 | 4036 | |
7fa3d080 BW |
4037 | static bfd_boolean |
4038 | xg_add_opcode_fix (TInsn *tinsn, | |
4039 | int opnum, | |
4040 | xtensa_format fmt, | |
4041 | int slot, | |
4042 | expressionS *expr, | |
4043 | fragS *fragP, | |
4044 | offsetT offset) | |
43cd72b9 BW |
4045 | { |
4046 | xtensa_opcode opcode = tinsn->opcode; | |
4047 | bfd_reloc_code_real_type reloc; | |
4048 | reloc_howto_type *howto; | |
4049 | int fmt_length; | |
e0001a05 NC |
4050 | fixS *the_fix; |
4051 | ||
43cd72b9 BW |
4052 | reloc = BFD_RELOC_NONE; |
4053 | ||
4054 | /* First try the special cases for "alternate" relocs. */ | |
4055 | if (opcode == xtensa_l32r_opcode) | |
4056 | { | |
4057 | if (fragP->tc_frag_data.use_absolute_literals) | |
4058 | reloc = encode_alt_reloc (slot); | |
4059 | } | |
4060 | else if (opcode == xtensa_const16_opcode) | |
4061 | { | |
4062 | if (expr->X_op == O_lo16) | |
4063 | { | |
4064 | reloc = encode_reloc (slot); | |
4065 | expr->X_op = O_symbol; | |
4066 | } | |
4067 | else if (expr->X_op == O_hi16) | |
4068 | { | |
4069 | reloc = encode_alt_reloc (slot); | |
4070 | expr->X_op = O_symbol; | |
4071 | } | |
4072 | } | |
4073 | ||
4074 | if (opnum != get_relaxable_immed (opcode)) | |
e0001a05 | 4075 | { |
43cd72b9 | 4076 | as_bad (_("invalid relocation for operand %i of '%s'"), |
431ad2d0 | 4077 | opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode)); |
e0001a05 NC |
4078 | return FALSE; |
4079 | } | |
4080 | ||
43cd72b9 BW |
4081 | /* Handle erroneous "@h" and "@l" expressions here before they propagate |
4082 | into the symbol table where the generic portions of the assembler | |
4083 | won't know what to do with them. */ | |
4084 | if (expr->X_op == O_lo16 || expr->X_op == O_hi16) | |
4085 | { | |
4086 | as_bad (_("invalid expression for operand %i of '%s'"), | |
431ad2d0 | 4087 | opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode)); |
43cd72b9 BW |
4088 | return FALSE; |
4089 | } | |
4090 | ||
4091 | /* Next try the generic relocs. */ | |
4092 | if (reloc == BFD_RELOC_NONE) | |
4093 | reloc = encode_reloc (slot); | |
4094 | if (reloc == BFD_RELOC_NONE) | |
4095 | { | |
4096 | as_bad (_("invalid relocation in instruction slot %i"), slot); | |
4097 | return FALSE; | |
4098 | } | |
e0001a05 | 4099 | |
43cd72b9 | 4100 | howto = bfd_reloc_type_lookup (stdoutput, reloc); |
e0001a05 NC |
4101 | if (!howto) |
4102 | { | |
43cd72b9 | 4103 | as_bad (_("undefined symbol for opcode \"%s\""), |
e0001a05 NC |
4104 | xtensa_opcode_name (xtensa_default_isa, opcode)); |
4105 | return FALSE; | |
4106 | } | |
4107 | ||
43cd72b9 BW |
4108 | fmt_length = xtensa_format_length (xtensa_default_isa, fmt); |
4109 | the_fix = fix_new_exp (fragP, offset, fmt_length, expr, | |
e0001a05 | 4110 | howto->pc_relative, reloc); |
d9740523 | 4111 | the_fix->fx_no_overflow = 1; |
e0001a05 | 4112 | |
7fa3d080 BW |
4113 | if (expr->X_add_symbol |
4114 | && (S_IS_EXTERNAL (expr->X_add_symbol) | |
4115 | || S_IS_WEAK (expr->X_add_symbol))) | |
4116 | the_fix->fx_plt = TRUE; | |
4117 | ||
4118 | the_fix->tc_fix_data.X_add_symbol = expr->X_add_symbol; | |
4119 | the_fix->tc_fix_data.X_add_number = expr->X_add_number; | |
4120 | the_fix->tc_fix_data.slot = slot; | |
c138bc38 | 4121 | |
7fa3d080 BW |
4122 | return TRUE; |
4123 | } | |
4124 | ||
4125 | ||
4126 | static bfd_boolean | |
4127 | xg_emit_insn_to_buf (TInsn *tinsn, | |
7fa3d080 BW |
4128 | char *buf, |
4129 | fragS *fragP, | |
4130 | offsetT offset, | |
4131 | bfd_boolean build_fix) | |
4132 | { | |
4133 | static xtensa_insnbuf insnbuf = NULL; | |
4134 | bfd_boolean has_symbolic_immed = FALSE; | |
4135 | bfd_boolean ok = TRUE; | |
b2d179be | 4136 | |
7fa3d080 BW |
4137 | if (!insnbuf) |
4138 | insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa); | |
4139 | ||
4140 | has_symbolic_immed = tinsn_to_insnbuf (tinsn, insnbuf); | |
4141 | if (has_symbolic_immed && build_fix) | |
4142 | { | |
4143 | /* Add a fixup. */ | |
b2d179be BW |
4144 | xtensa_format fmt = xg_get_single_format (tinsn->opcode); |
4145 | int slot = xg_get_single_slot (tinsn->opcode); | |
7fa3d080 BW |
4146 | int opnum = get_relaxable_immed (tinsn->opcode); |
4147 | expressionS *exp = &tinsn->tok[opnum]; | |
43cd72b9 | 4148 | |
b2d179be | 4149 | if (!xg_add_opcode_fix (tinsn, opnum, fmt, slot, exp, fragP, offset)) |
7fa3d080 BW |
4150 | ok = FALSE; |
4151 | } | |
4152 | fragP->tc_frag_data.is_insn = TRUE; | |
d77b99c9 BW |
4153 | xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, |
4154 | (unsigned char *) buf, 0); | |
7fa3d080 | 4155 | return ok; |
e0001a05 NC |
4156 | } |
4157 | ||
4158 | ||
7fa3d080 BW |
4159 | static void |
4160 | xg_resolve_literals (TInsn *insn, symbolS *lit_sym) | |
e0001a05 NC |
4161 | { |
4162 | symbolS *sym = get_special_literal_symbol (); | |
4163 | int i; | |
4164 | if (lit_sym == 0) | |
4165 | return; | |
4166 | assert (insn->insn_type == ITYPE_INSN); | |
4167 | for (i = 0; i < insn->ntok; i++) | |
4168 | if (insn->tok[i].X_add_symbol == sym) | |
4169 | insn->tok[i].X_add_symbol = lit_sym; | |
4170 | ||
4171 | } | |
4172 | ||
4173 | ||
7fa3d080 BW |
4174 | static void |
4175 | xg_resolve_labels (TInsn *insn, symbolS *label_sym) | |
e0001a05 NC |
4176 | { |
4177 | symbolS *sym = get_special_label_symbol (); | |
4178 | int i; | |
e0001a05 NC |
4179 | for (i = 0; i < insn->ntok; i++) |
4180 | if (insn->tok[i].X_add_symbol == sym) | |
4181 | insn->tok[i].X_add_symbol = label_sym; | |
4182 | ||
4183 | } | |
4184 | ||
4185 | ||
43cd72b9 | 4186 | /* Return TRUE if the instruction can write to the specified |
e0001a05 NC |
4187 | integer register. */ |
4188 | ||
4189 | static bfd_boolean | |
7fa3d080 | 4190 | is_register_writer (const TInsn *insn, const char *regset, int regnum) |
e0001a05 NC |
4191 | { |
4192 | int i; | |
4193 | int num_ops; | |
4194 | xtensa_isa isa = xtensa_default_isa; | |
4195 | ||
43cd72b9 | 4196 | num_ops = xtensa_opcode_num_operands (isa, insn->opcode); |
e0001a05 NC |
4197 | |
4198 | for (i = 0; i < num_ops; i++) | |
4199 | { | |
43cd72b9 BW |
4200 | char inout; |
4201 | inout = xtensa_operand_inout (isa, insn->opcode, i); | |
4202 | if ((inout == 'o' || inout == 'm') | |
4203 | && xtensa_operand_is_register (isa, insn->opcode, i) == 1) | |
e0001a05 | 4204 | { |
43cd72b9 BW |
4205 | xtensa_regfile opnd_rf = |
4206 | xtensa_operand_regfile (isa, insn->opcode, i); | |
4207 | if (!strcmp (xtensa_regfile_shortname (isa, opnd_rf), regset)) | |
e0001a05 NC |
4208 | { |
4209 | if ((insn->tok[i].X_op == O_register) | |
4210 | && (insn->tok[i].X_add_number == regnum)) | |
4211 | return TRUE; | |
4212 | } | |
4213 | } | |
4214 | } | |
4215 | return FALSE; | |
4216 | } | |
4217 | ||
4218 | ||
4219 | static bfd_boolean | |
7fa3d080 | 4220 | is_bad_loopend_opcode (const TInsn *tinsn) |
e0001a05 NC |
4221 | { |
4222 | xtensa_opcode opcode = tinsn->opcode; | |
4223 | ||
4224 | if (opcode == XTENSA_UNDEFINED) | |
4225 | return FALSE; | |
4226 | ||
4227 | if (opcode == xtensa_call0_opcode | |
4228 | || opcode == xtensa_callx0_opcode | |
4229 | || opcode == xtensa_call4_opcode | |
4230 | || opcode == xtensa_callx4_opcode | |
4231 | || opcode == xtensa_call8_opcode | |
4232 | || opcode == xtensa_callx8_opcode | |
4233 | || opcode == xtensa_call12_opcode | |
4234 | || opcode == xtensa_callx12_opcode | |
4235 | || opcode == xtensa_isync_opcode | |
4236 | || opcode == xtensa_ret_opcode | |
4237 | || opcode == xtensa_ret_n_opcode | |
4238 | || opcode == xtensa_retw_opcode | |
4239 | || opcode == xtensa_retw_n_opcode | |
43cd72b9 BW |
4240 | || opcode == xtensa_waiti_opcode |
4241 | || opcode == xtensa_rsr_lcount_opcode) | |
e0001a05 | 4242 | return TRUE; |
c138bc38 | 4243 | |
e0001a05 NC |
4244 | return FALSE; |
4245 | } | |
4246 | ||
4247 | ||
4248 | /* Labels that begin with ".Ln" or ".LM" are unaligned. | |
4249 | This allows the debugger to add unaligned labels. | |
4250 | Also, the assembler generates stabs labels that need | |
4251 | not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */ | |
4252 | ||
7fa3d080 BW |
4253 | static bfd_boolean |
4254 | is_unaligned_label (symbolS *sym) | |
e0001a05 NC |
4255 | { |
4256 | const char *name = S_GET_NAME (sym); | |
4257 | static size_t fake_size = 0; | |
4258 | ||
4259 | if (name | |
4260 | && name[0] == '.' | |
4261 | && name[1] == 'L' && (name[2] == 'n' || name[2] == 'M')) | |
4262 | return TRUE; | |
4263 | ||
4264 | /* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */ | |
4265 | if (fake_size == 0) | |
4266 | fake_size = strlen (FAKE_LABEL_NAME); | |
4267 | ||
43cd72b9 | 4268 | if (name |
e0001a05 NC |
4269 | && strncmp (FAKE_LABEL_NAME, name, fake_size) == 0 |
4270 | && (name[fake_size] == 'F' | |
4271 | || name[fake_size] == 'L' | |
4272 | || (name[fake_size] == 'e' | |
4273 | && strncmp ("endfunc", name+fake_size, 7) == 0))) | |
4274 | return TRUE; | |
4275 | ||
4276 | return FALSE; | |
4277 | } | |
4278 | ||
4279 | ||
7fa3d080 BW |
4280 | static fragS * |
4281 | next_non_empty_frag (const fragS *fragP) | |
e0001a05 NC |
4282 | { |
4283 | fragS *next_fragP = fragP->fr_next; | |
4284 | ||
c138bc38 | 4285 | /* Sometimes an empty will end up here due storage allocation issues. |
e0001a05 NC |
4286 | So we have to skip until we find something legit. */ |
4287 | while (next_fragP && next_fragP->fr_fix == 0) | |
4288 | next_fragP = next_fragP->fr_next; | |
4289 | ||
4290 | if (next_fragP == NULL || next_fragP->fr_fix == 0) | |
4291 | return NULL; | |
4292 | ||
4293 | return next_fragP; | |
4294 | } | |
4295 | ||
4296 | ||
43cd72b9 | 4297 | static bfd_boolean |
7fa3d080 | 4298 | next_frag_opcode_is_loop (const fragS *fragP, xtensa_opcode *opcode) |
43cd72b9 BW |
4299 | { |
4300 | xtensa_opcode out_opcode; | |
4301 | const fragS *next_fragP = next_non_empty_frag (fragP); | |
4302 | ||
4303 | if (next_fragP == NULL) | |
4304 | return FALSE; | |
4305 | ||
4306 | out_opcode = get_opcode_from_buf (next_fragP->fr_literal, 0); | |
4307 | if (xtensa_opcode_is_loop (xtensa_default_isa, out_opcode) == 1) | |
4308 | { | |
4309 | *opcode = out_opcode; | |
4310 | return TRUE; | |
4311 | } | |
4312 | return FALSE; | |
4313 | } | |
4314 | ||
4315 | ||
4316 | static int | |
7fa3d080 | 4317 | frag_format_size (const fragS *fragP) |
43cd72b9 | 4318 | { |
e0001a05 NC |
4319 | static xtensa_insnbuf insnbuf = NULL; |
4320 | xtensa_isa isa = xtensa_default_isa; | |
43cd72b9 | 4321 | xtensa_format fmt; |
c138bc38 | 4322 | int fmt_size; |
e0001a05 NC |
4323 | |
4324 | if (!insnbuf) | |
4325 | insnbuf = xtensa_insnbuf_alloc (isa); | |
4326 | ||
43cd72b9 BW |
4327 | if (fragP == NULL) |
4328 | return XTENSA_UNDEFINED; | |
4329 | ||
d77b99c9 BW |
4330 | xtensa_insnbuf_from_chars (isa, insnbuf, |
4331 | (unsigned char *) fragP->fr_literal, 0); | |
43cd72b9 BW |
4332 | |
4333 | fmt = xtensa_format_decode (isa, insnbuf); | |
4334 | if (fmt == XTENSA_UNDEFINED) | |
e0001a05 | 4335 | return XTENSA_UNDEFINED; |
43cd72b9 BW |
4336 | fmt_size = xtensa_format_length (isa, fmt); |
4337 | ||
4338 | /* If the next format won't be changing due to relaxation, just | |
4339 | return the length of the first format. */ | |
4340 | if (fragP->fr_opcode != fragP->fr_literal) | |
4341 | return fmt_size; | |
4342 | ||
c138bc38 | 4343 | /* If during relaxation we have to pull an instruction out of a |
43cd72b9 BW |
4344 | multi-slot instruction, we will return the more conservative |
4345 | number. This works because alignment on bigger instructions | |
4346 | is more restrictive than alignment on smaller instructions. | |
4347 | This is more conservative than we would like, but it happens | |
4348 | infrequently. */ | |
4349 | ||
4350 | if (xtensa_format_num_slots (xtensa_default_isa, fmt) > 1) | |
4351 | return fmt_size; | |
4352 | ||
4353 | /* If we aren't doing one of our own relaxations or it isn't | |
4354 | slot-based, then the insn size won't change. */ | |
4355 | if (fragP->fr_type != rs_machine_dependent) | |
4356 | return fmt_size; | |
4357 | if (fragP->fr_subtype != RELAX_SLOTS) | |
4358 | return fmt_size; | |
4359 | ||
4360 | /* If an instruction is about to grow, return the longer size. */ | |
4361 | if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP1 | |
4362 | || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP2) | |
4363 | return 3; | |
c138bc38 | 4364 | |
43cd72b9 BW |
4365 | if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW) |
4366 | return 2 + fragP->tc_frag_data.text_expansion[0]; | |
e0001a05 | 4367 | |
43cd72b9 | 4368 | return fmt_size; |
e0001a05 NC |
4369 | } |
4370 | ||
4371 | ||
7fa3d080 BW |
4372 | static int |
4373 | next_frag_format_size (const fragS *fragP) | |
e0001a05 | 4374 | { |
7fa3d080 BW |
4375 | const fragS *next_fragP = next_non_empty_frag (fragP); |
4376 | return frag_format_size (next_fragP); | |
e0001a05 NC |
4377 | } |
4378 | ||
4379 | ||
03aaa593 BW |
4380 | /* In early Xtensa Processors, for reasons that are unclear, the ISA |
4381 | required two-byte instructions to be treated as three-byte instructions | |
4382 | for loop instruction alignment. This restriction was removed beginning | |
4383 | with Xtensa LX. Now the only requirement on loop instruction alignment | |
4384 | is that the first instruction of the loop must appear at an address that | |
4385 | does not cross a fetch boundary. */ | |
4386 | ||
4387 | static int | |
4388 | get_loop_align_size (int insn_size) | |
4389 | { | |
4390 | if (insn_size == XTENSA_UNDEFINED) | |
4391 | return xtensa_fetch_width; | |
4392 | ||
4393 | if (enforce_three_byte_loop_align && insn_size == 2) | |
4394 | return 3; | |
4395 | ||
4396 | return insn_size; | |
4397 | } | |
4398 | ||
4399 | ||
e0001a05 NC |
4400 | /* If the next legit fragment is an end-of-loop marker, |
4401 | switch its state so it will instantiate a NOP. */ | |
4402 | ||
4403 | static void | |
1d19a770 | 4404 | update_next_frag_state (fragS *fragP) |
e0001a05 NC |
4405 | { |
4406 | fragS *next_fragP = fragP->fr_next; | |
43cd72b9 | 4407 | fragS *new_target = NULL; |
e0001a05 | 4408 | |
7b1cc377 | 4409 | if (align_targets) |
43cd72b9 BW |
4410 | { |
4411 | /* We are guaranteed there will be one of these... */ | |
4412 | while (!(next_fragP->fr_type == rs_machine_dependent | |
4413 | && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE | |
4414 | || next_fragP->fr_subtype == RELAX_UNREACHABLE))) | |
4415 | next_fragP = next_fragP->fr_next; | |
4416 | ||
4417 | assert (next_fragP->fr_type == rs_machine_dependent | |
4418 | && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE | |
4419 | || next_fragP->fr_subtype == RELAX_UNREACHABLE)); | |
4420 | ||
4421 | /* ...and one of these. */ | |
4422 | new_target = next_fragP->fr_next; | |
4423 | while (!(new_target->fr_type == rs_machine_dependent | |
4424 | && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN | |
4425 | || new_target->fr_subtype == RELAX_DESIRE_ALIGN))) | |
4426 | new_target = new_target->fr_next; | |
4427 | ||
4428 | assert (new_target->fr_type == rs_machine_dependent | |
4429 | && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN | |
4430 | || new_target->fr_subtype == RELAX_DESIRE_ALIGN)); | |
4431 | } | |
43cd72b9 | 4432 | |
1d19a770 | 4433 | while (next_fragP && next_fragP->fr_fix == 0) |
43cd72b9 | 4434 | { |
1d19a770 BW |
4435 | if (next_fragP->fr_type == rs_machine_dependent |
4436 | && next_fragP->fr_subtype == RELAX_LOOP_END) | |
43cd72b9 | 4437 | { |
1d19a770 BW |
4438 | next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP; |
4439 | return; | |
e0001a05 | 4440 | } |
1d19a770 BW |
4441 | |
4442 | next_fragP = next_fragP->fr_next; | |
e0001a05 NC |
4443 | } |
4444 | } | |
4445 | ||
4446 | ||
4447 | static bfd_boolean | |
7fa3d080 | 4448 | next_frag_is_branch_target (const fragS *fragP) |
e0001a05 | 4449 | { |
43cd72b9 | 4450 | /* Sometimes an empty will end up here due to storage allocation issues, |
e0001a05 NC |
4451 | so we have to skip until we find something legit. */ |
4452 | for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next) | |
4453 | { | |
4454 | if (fragP->tc_frag_data.is_branch_target) | |
4455 | return TRUE; | |
4456 | if (fragP->fr_fix != 0) | |
4457 | break; | |
4458 | } | |
4459 | return FALSE; | |
4460 | } | |
4461 | ||
4462 | ||
4463 | static bfd_boolean | |
7fa3d080 | 4464 | next_frag_is_loop_target (const fragS *fragP) |
e0001a05 | 4465 | { |
c138bc38 | 4466 | /* Sometimes an empty will end up here due storage allocation issues. |
e0001a05 NC |
4467 | So we have to skip until we find something legit. */ |
4468 | for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next) | |
4469 | { | |
4470 | if (fragP->tc_frag_data.is_loop_target) | |
4471 | return TRUE; | |
4472 | if (fragP->fr_fix != 0) | |
4473 | break; | |
4474 | } | |
4475 | return FALSE; | |
4476 | } | |
4477 | ||
4478 | ||
4479 | static addressT | |
7fa3d080 | 4480 | next_frag_pre_opcode_bytes (const fragS *fragp) |
e0001a05 NC |
4481 | { |
4482 | const fragS *next_fragp = fragp->fr_next; | |
43cd72b9 | 4483 | xtensa_opcode next_opcode; |
e0001a05 | 4484 | |
43cd72b9 | 4485 | if (!next_frag_opcode_is_loop (fragp, &next_opcode)) |
e0001a05 NC |
4486 | return 0; |
4487 | ||
43cd72b9 BW |
4488 | /* Sometimes an empty will end up here due to storage allocation issues, |
4489 | so we have to skip until we find something legit. */ | |
e0001a05 NC |
4490 | while (next_fragp->fr_fix == 0) |
4491 | next_fragp = next_fragp->fr_next; | |
4492 | ||
4493 | if (next_fragp->fr_type != rs_machine_dependent) | |
4494 | return 0; | |
4495 | ||
4496 | /* There is some implicit knowledge encoded in here. | |
4497 | The LOOP instructions that are NOT RELAX_IMMED have | |
43cd72b9 BW |
4498 | been relaxed. Note that we can assume that the LOOP |
4499 | instruction is in slot 0 because loops aren't bundleable. */ | |
4500 | if (next_fragp->tc_frag_data.slot_subtypes[0] > RELAX_IMMED) | |
e0001a05 NC |
4501 | return get_expanded_loop_offset (next_opcode); |
4502 | ||
4503 | return 0; | |
4504 | } | |
4505 | ||
4506 | ||
4507 | /* Mark a location where we can later insert literal frags. Update | |
4508 | the section's literal_pool_loc, so subsequent literals can be | |
4509 | placed nearest to their use. */ | |
4510 | ||
4511 | static void | |
7fa3d080 | 4512 | xtensa_mark_literal_pool_location (void) |
e0001a05 NC |
4513 | { |
4514 | /* Any labels pointing to the current location need | |
4515 | to be adjusted to after the literal pool. */ | |
4516 | emit_state s; | |
e0001a05 | 4517 | fragS *pool_location; |
e0001a05 | 4518 | |
1f2a7e38 | 4519 | if (use_literal_section) |
43cd72b9 BW |
4520 | return; |
4521 | ||
dd49a749 BW |
4522 | /* We stash info in these frags so we can later move the literal's |
4523 | fixes into this frchain's fix list. */ | |
e0001a05 | 4524 | pool_location = frag_now; |
dd49a749 BW |
4525 | frag_now->tc_frag_data.lit_frchain = frchain_now; |
4526 | frag_variant (rs_machine_dependent, 0, 0, | |
e0001a05 | 4527 | RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL); |
43cd72b9 | 4528 | xtensa_set_frag_assembly_state (frag_now); |
dd49a749 BW |
4529 | frag_now->tc_frag_data.lit_seg = now_seg; |
4530 | frag_variant (rs_machine_dependent, 0, 0, | |
e0001a05 | 4531 | RELAX_LITERAL_POOL_END, NULL, 0, NULL); |
43cd72b9 | 4532 | xtensa_set_frag_assembly_state (frag_now); |
e0001a05 NC |
4533 | |
4534 | /* Now put a frag into the literal pool that points to this location. */ | |
4535 | set_literal_pool_location (now_seg, pool_location); | |
43cd72b9 BW |
4536 | xtensa_switch_to_non_abs_literal_fragment (&s); |
4537 | frag_align (2, 0, 0); | |
4538 | record_alignment (now_seg, 2); | |
e0001a05 NC |
4539 | |
4540 | /* Close whatever frag is there. */ | |
4541 | frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
43cd72b9 | 4542 | xtensa_set_frag_assembly_state (frag_now); |
e0001a05 NC |
4543 | frag_now->tc_frag_data.literal_frag = pool_location; |
4544 | frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
4545 | xtensa_restore_emit_state (&s); | |
43cd72b9 | 4546 | xtensa_set_frag_assembly_state (frag_now); |
e0001a05 NC |
4547 | } |
4548 | ||
4549 | ||
43cd72b9 BW |
4550 | /* Build a nop of the correct size into tinsn. */ |
4551 | ||
4552 | static void | |
7fa3d080 | 4553 | build_nop (TInsn *tinsn, int size) |
43cd72b9 BW |
4554 | { |
4555 | tinsn_init (tinsn); | |
4556 | switch (size) | |
4557 | { | |
4558 | case 2: | |
4559 | tinsn->opcode = xtensa_nop_n_opcode; | |
4560 | tinsn->ntok = 0; | |
4561 | if (tinsn->opcode == XTENSA_UNDEFINED) | |
4562 | as_fatal (_("opcode 'NOP.N' unavailable in this configuration")); | |
4563 | break; | |
4564 | ||
4565 | case 3: | |
4566 | if (xtensa_nop_opcode == XTENSA_UNDEFINED) | |
4567 | { | |
4568 | tinsn->opcode = xtensa_or_opcode; | |
4569 | set_expr_const (&tinsn->tok[0], 1); | |
4570 | set_expr_const (&tinsn->tok[1], 1); | |
4571 | set_expr_const (&tinsn->tok[2], 1); | |
4572 | tinsn->ntok = 3; | |
4573 | } | |
4574 | else | |
4575 | tinsn->opcode = xtensa_nop_opcode; | |
4576 | ||
4577 | assert (tinsn->opcode != XTENSA_UNDEFINED); | |
4578 | } | |
4579 | } | |
4580 | ||
4581 | ||
e0001a05 NC |
4582 | /* Assemble a NOP of the requested size in the buffer. User must have |
4583 | allocated "buf" with at least "size" bytes. */ | |
4584 | ||
7fa3d080 | 4585 | static void |
d77b99c9 | 4586 | assemble_nop (int size, char *buf) |
e0001a05 NC |
4587 | { |
4588 | static xtensa_insnbuf insnbuf = NULL; | |
43cd72b9 | 4589 | TInsn tinsn; |
e0001a05 | 4590 | |
43cd72b9 | 4591 | build_nop (&tinsn, size); |
e0001a05 | 4592 | |
43cd72b9 BW |
4593 | if (!insnbuf) |
4594 | insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa); | |
e0001a05 | 4595 | |
43cd72b9 | 4596 | tinsn_to_insnbuf (&tinsn, insnbuf); |
d77b99c9 BW |
4597 | xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, |
4598 | (unsigned char *) buf, 0); | |
e0001a05 NC |
4599 | } |
4600 | ||
4601 | ||
4602 | /* Return the number of bytes for the offset of the expanded loop | |
4603 | instruction. This should be incorporated into the relaxation | |
4604 | specification but is hard-coded here. This is used to auto-align | |
4605 | the loop instruction. It is invalid to call this function if the | |
4606 | configuration does not have loops or if the opcode is not a loop | |
4607 | opcode. */ | |
4608 | ||
4609 | static addressT | |
7fa3d080 | 4610 | get_expanded_loop_offset (xtensa_opcode opcode) |
e0001a05 NC |
4611 | { |
4612 | /* This is the OFFSET of the loop instruction in the expanded loop. | |
4613 | This MUST correspond directly to the specification of the loop | |
4614 | expansion. It will be validated on fragment conversion. */ | |
43cd72b9 | 4615 | assert (opcode != XTENSA_UNDEFINED); |
e0001a05 NC |
4616 | if (opcode == xtensa_loop_opcode) |
4617 | return 0; | |
4618 | if (opcode == xtensa_loopnez_opcode) | |
4619 | return 3; | |
4620 | if (opcode == xtensa_loopgtz_opcode) | |
4621 | return 6; | |
4622 | as_fatal (_("get_expanded_loop_offset: invalid opcode")); | |
4623 | return 0; | |
4624 | } | |
4625 | ||
4626 | ||
7fa3d080 BW |
4627 | static fragS * |
4628 | get_literal_pool_location (segT seg) | |
e0001a05 NC |
4629 | { |
4630 | return seg_info (seg)->tc_segment_info_data.literal_pool_loc; | |
4631 | } | |
4632 | ||
4633 | ||
4634 | static void | |
7fa3d080 | 4635 | set_literal_pool_location (segT seg, fragS *literal_pool_loc) |
e0001a05 NC |
4636 | { |
4637 | seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc; | |
4638 | } | |
4639 | ||
43cd72b9 BW |
4640 | |
4641 | /* Set frag assembly state should be called when a new frag is | |
4642 | opened and after a frag has been closed. */ | |
4643 | ||
7fa3d080 BW |
4644 | static void |
4645 | xtensa_set_frag_assembly_state (fragS *fragP) | |
43cd72b9 BW |
4646 | { |
4647 | if (!density_supported) | |
4648 | fragP->tc_frag_data.is_no_density = TRUE; | |
4649 | ||
4650 | /* This function is called from subsegs_finish, which is called | |
c138bc38 | 4651 | after xtensa_end, so we can't use "use_transform" or |
43cd72b9 BW |
4652 | "use_schedule" here. */ |
4653 | if (!directive_state[directive_transform]) | |
4654 | fragP->tc_frag_data.is_no_transform = TRUE; | |
7c834684 BW |
4655 | if (directive_state[directive_longcalls]) |
4656 | fragP->tc_frag_data.use_longcalls = TRUE; | |
43cd72b9 BW |
4657 | fragP->tc_frag_data.use_absolute_literals = |
4658 | directive_state[directive_absolute_literals]; | |
4659 | fragP->tc_frag_data.is_assembly_state_set = TRUE; | |
4660 | } | |
4661 | ||
4662 | ||
7fa3d080 BW |
4663 | static bfd_boolean |
4664 | relaxable_section (asection *sec) | |
43cd72b9 BW |
4665 | { |
4666 | return (sec->flags & SEC_DEBUGGING) == 0; | |
4667 | } | |
4668 | ||
4669 | ||
4670 | static void | |
7fa3d080 | 4671 | xtensa_find_unmarked_state_frags (void) |
43cd72b9 BW |
4672 | { |
4673 | segT *seclist; | |
4674 | ||
4675 | /* Walk over each fragment of all of the current segments. For each | |
4676 | unmarked fragment, mark it with the same info as the previous | |
4677 | fragment. */ | |
4678 | for (seclist = &stdoutput->sections; | |
4679 | seclist && *seclist; | |
4680 | seclist = &(*seclist)->next) | |
4681 | { | |
4682 | segT sec = *seclist; | |
4683 | segment_info_type *seginfo; | |
4684 | fragS *fragP; | |
4685 | flagword flags; | |
4686 | flags = bfd_get_section_flags (stdoutput, sec); | |
4687 | if (flags & SEC_DEBUGGING) | |
4688 | continue; | |
4689 | if (!(flags & SEC_ALLOC)) | |
4690 | continue; | |
4691 | ||
4692 | seginfo = seg_info (sec); | |
4693 | if (seginfo && seginfo->frchainP) | |
4694 | { | |
4695 | fragS *last_fragP = 0; | |
4696 | for (fragP = seginfo->frchainP->frch_root; fragP; | |
4697 | fragP = fragP->fr_next) | |
4698 | { | |
4699 | if (fragP->fr_fix != 0 | |
4700 | && !fragP->tc_frag_data.is_assembly_state_set) | |
4701 | { | |
4702 | if (last_fragP == 0) | |
4703 | { | |
4704 | as_warn_where (fragP->fr_file, fragP->fr_line, | |
4705 | _("assembly state not set for first frag in section %s"), | |
4706 | sec->name); | |
4707 | } | |
4708 | else | |
4709 | { | |
4710 | fragP->tc_frag_data.is_assembly_state_set = TRUE; | |
4711 | fragP->tc_frag_data.is_no_density = | |
4712 | last_fragP->tc_frag_data.is_no_density; | |
4713 | fragP->tc_frag_data.is_no_transform = | |
4714 | last_fragP->tc_frag_data.is_no_transform; | |
7c834684 BW |
4715 | fragP->tc_frag_data.use_longcalls = |
4716 | last_fragP->tc_frag_data.use_longcalls; | |
43cd72b9 BW |
4717 | fragP->tc_frag_data.use_absolute_literals = |
4718 | last_fragP->tc_frag_data.use_absolute_literals; | |
4719 | } | |
4720 | } | |
4721 | if (fragP->tc_frag_data.is_assembly_state_set) | |
4722 | last_fragP = fragP; | |
4723 | } | |
4724 | } | |
4725 | } | |
4726 | } | |
4727 | ||
4728 | ||
4729 | static void | |
7fa3d080 BW |
4730 | xtensa_find_unaligned_branch_targets (bfd *abfd ATTRIBUTE_UNUSED, |
4731 | asection *sec, | |
4732 | void *unused ATTRIBUTE_UNUSED) | |
43cd72b9 BW |
4733 | { |
4734 | flagword flags = bfd_get_section_flags (abfd, sec); | |
4735 | segment_info_type *seginfo = seg_info (sec); | |
4736 | fragS *frag = seginfo->frchainP->frch_root; | |
c138bc38 | 4737 | |
43cd72b9 | 4738 | if (flags & SEC_CODE) |
c138bc38 | 4739 | { |
43cd72b9 BW |
4740 | xtensa_isa isa = xtensa_default_isa; |
4741 | xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa); | |
4742 | while (frag != NULL) | |
4743 | { | |
4744 | if (frag->tc_frag_data.is_branch_target) | |
4745 | { | |
4746 | int op_size; | |
664df4e4 | 4747 | addressT branch_align, frag_addr; |
43cd72b9 BW |
4748 | xtensa_format fmt; |
4749 | ||
d77b99c9 BW |
4750 | xtensa_insnbuf_from_chars |
4751 | (isa, insnbuf, (unsigned char *) frag->fr_literal, 0); | |
43cd72b9 BW |
4752 | fmt = xtensa_format_decode (isa, insnbuf); |
4753 | op_size = xtensa_format_length (isa, fmt); | |
664df4e4 BW |
4754 | branch_align = 1 << branch_align_power (sec); |
4755 | frag_addr = frag->fr_address % branch_align; | |
4756 | if (frag_addr + op_size > branch_align) | |
43cd72b9 BW |
4757 | as_warn_where (frag->fr_file, frag->fr_line, |
4758 | _("unaligned branch target: %d bytes at 0x%lx"), | |
dd49a749 | 4759 | op_size, (long) frag->fr_address); |
43cd72b9 BW |
4760 | } |
4761 | frag = frag->fr_next; | |
4762 | } | |
4763 | xtensa_insnbuf_free (isa, insnbuf); | |
4764 | } | |
4765 | } | |
4766 | ||
4767 | ||
4768 | static void | |
7fa3d080 BW |
4769 | xtensa_find_unaligned_loops (bfd *abfd ATTRIBUTE_UNUSED, |
4770 | asection *sec, | |
4771 | void *unused ATTRIBUTE_UNUSED) | |
43cd72b9 BW |
4772 | { |
4773 | flagword flags = bfd_get_section_flags (abfd, sec); | |
4774 | segment_info_type *seginfo = seg_info (sec); | |
4775 | fragS *frag = seginfo->frchainP->frch_root; | |
4776 | xtensa_isa isa = xtensa_default_isa; | |
c138bc38 | 4777 | |
43cd72b9 | 4778 | if (flags & SEC_CODE) |
c138bc38 | 4779 | { |
43cd72b9 BW |
4780 | xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa); |
4781 | while (frag != NULL) | |
4782 | { | |
4783 | if (frag->tc_frag_data.is_first_loop_insn) | |
4784 | { | |
4785 | int op_size; | |
d77b99c9 | 4786 | addressT frag_addr; |
43cd72b9 BW |
4787 | xtensa_format fmt; |
4788 | ||
d77b99c9 BW |
4789 | xtensa_insnbuf_from_chars |
4790 | (isa, insnbuf, (unsigned char *) frag->fr_literal, 0); | |
43cd72b9 BW |
4791 | fmt = xtensa_format_decode (isa, insnbuf); |
4792 | op_size = xtensa_format_length (isa, fmt); | |
4793 | frag_addr = frag->fr_address % xtensa_fetch_width; | |
4794 | ||
d77b99c9 | 4795 | if (frag_addr + op_size > xtensa_fetch_width) |
43cd72b9 BW |
4796 | as_warn_where (frag->fr_file, frag->fr_line, |
4797 | _("unaligned loop: %d bytes at 0x%lx"), | |
dd49a749 | 4798 | op_size, (long) frag->fr_address); |
43cd72b9 BW |
4799 | } |
4800 | frag = frag->fr_next; | |
4801 | } | |
4802 | xtensa_insnbuf_free (isa, insnbuf); | |
4803 | } | |
4804 | } | |
4805 | ||
4806 | ||
30f725a1 BW |
4807 | static int |
4808 | xg_apply_fix_value (fixS *fixP, valueT val) | |
43cd72b9 BW |
4809 | { |
4810 | xtensa_isa isa = xtensa_default_isa; | |
4811 | static xtensa_insnbuf insnbuf = NULL; | |
4812 | static xtensa_insnbuf slotbuf = NULL; | |
4813 | xtensa_format fmt; | |
4814 | int slot; | |
4815 | bfd_boolean alt_reloc; | |
4816 | xtensa_opcode opcode; | |
4817 | char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where; | |
4818 | ||
4819 | (void) decode_reloc (fixP->fx_r_type, &slot, &alt_reloc); | |
4820 | if (alt_reloc) | |
4821 | as_fatal (_("unexpected fix")); | |
4822 | ||
4823 | if (!insnbuf) | |
4824 | { | |
4825 | insnbuf = xtensa_insnbuf_alloc (isa); | |
4826 | slotbuf = xtensa_insnbuf_alloc (isa); | |
4827 | } | |
4828 | ||
d77b99c9 | 4829 | xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) fixpos, 0); |
43cd72b9 BW |
4830 | fmt = xtensa_format_decode (isa, insnbuf); |
4831 | if (fmt == XTENSA_UNDEFINED) | |
4832 | as_fatal (_("undecodable fix")); | |
4833 | xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf); | |
4834 | opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf); | |
4835 | if (opcode == XTENSA_UNDEFINED) | |
4836 | as_fatal (_("undecodable fix")); | |
4837 | ||
4838 | /* CONST16 immediates are not PC-relative, despite the fact that we | |
4839 | reuse the normal PC-relative operand relocations for the low part | |
30f725a1 | 4840 | of a CONST16 operand. */ |
43cd72b9 | 4841 | if (opcode == xtensa_const16_opcode) |
30f725a1 | 4842 | return 0; |
43cd72b9 BW |
4843 | |
4844 | xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode, | |
4845 | get_relaxable_immed (opcode), val, | |
4846 | fixP->fx_file, fixP->fx_line); | |
4847 | ||
4848 | xtensa_format_set_slot (isa, fmt, slot, insnbuf, slotbuf); | |
d77b99c9 | 4849 | xtensa_insnbuf_to_chars (isa, insnbuf, (unsigned char *) fixpos, 0); |
30f725a1 BW |
4850 | |
4851 | return 1; | |
43cd72b9 BW |
4852 | } |
4853 | ||
e0001a05 NC |
4854 | \f |
4855 | /* External Functions and Other GAS Hooks. */ | |
4856 | ||
4857 | const char * | |
7fa3d080 | 4858 | xtensa_target_format (void) |
e0001a05 NC |
4859 | { |
4860 | return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le"); | |
4861 | } | |
4862 | ||
4863 | ||
4864 | void | |
7fa3d080 | 4865 | xtensa_file_arch_init (bfd *abfd) |
e0001a05 NC |
4866 | { |
4867 | bfd_set_private_flags (abfd, 0x100 | 0x200); | |
4868 | } | |
4869 | ||
4870 | ||
4871 | void | |
7fa3d080 | 4872 | md_number_to_chars (char *buf, valueT val, int n) |
e0001a05 NC |
4873 | { |
4874 | if (target_big_endian) | |
4875 | number_to_chars_bigendian (buf, val, n); | |
4876 | else | |
4877 | number_to_chars_littleendian (buf, val, n); | |
4878 | } | |
4879 | ||
4880 | ||
4881 | /* This function is called once, at assembler startup time. It should | |
4882 | set up all the tables, etc. that the MD part of the assembler will | |
4883 | need. */ | |
4884 | ||
4885 | void | |
7fa3d080 | 4886 | md_begin (void) |
e0001a05 NC |
4887 | { |
4888 | segT current_section = now_seg; | |
4889 | int current_subsec = now_subseg; | |
4890 | xtensa_isa isa; | |
4891 | ||
43cd72b9 | 4892 | xtensa_default_isa = xtensa_isa_init (0, 0); |
e0001a05 | 4893 | isa = xtensa_default_isa; |
e0001a05 | 4894 | |
43cd72b9 BW |
4895 | linkrelax = 1; |
4896 | ||
74869ac7 | 4897 | /* Set up the literal sections. */ |
e0001a05 | 4898 | memset (&default_lit_sections, 0, sizeof (default_lit_sections)); |
e0001a05 NC |
4899 | |
4900 | subseg_set (current_section, current_subsec); | |
4901 | ||
43cd72b9 BW |
4902 | xg_init_vinsn (&cur_vinsn); |
4903 | ||
e0001a05 NC |
4904 | xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi"); |
4905 | xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi"); | |
4906 | xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0"); | |
4907 | xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4"); | |
4908 | xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8"); | |
4909 | xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12"); | |
4910 | xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0"); | |
4911 | xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4"); | |
4912 | xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8"); | |
4913 | xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12"); | |
43cd72b9 | 4914 | xtensa_const16_opcode = xtensa_opcode_lookup (isa, "const16"); |
e0001a05 | 4915 | xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry"); |
43cd72b9 BW |
4916 | xtensa_movi_opcode = xtensa_opcode_lookup (isa, "movi"); |
4917 | xtensa_movi_n_opcode = xtensa_opcode_lookup (isa, "movi.n"); | |
e0001a05 | 4918 | xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync"); |
e0001a05 | 4919 | xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx"); |
43cd72b9 | 4920 | xtensa_l32r_opcode = xtensa_opcode_lookup (isa, "l32r"); |
e0001a05 NC |
4921 | xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop"); |
4922 | xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez"); | |
4923 | xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz"); | |
43cd72b9 | 4924 | xtensa_nop_opcode = xtensa_opcode_lookup (isa, "nop"); |
e0001a05 NC |
4925 | xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n"); |
4926 | xtensa_or_opcode = xtensa_opcode_lookup (isa, "or"); | |
4927 | xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret"); | |
4928 | xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n"); | |
4929 | xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw"); | |
4930 | xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n"); | |
43cd72b9 | 4931 | xtensa_rsr_lcount_opcode = xtensa_opcode_lookup (isa, "rsr.lcount"); |
e0001a05 | 4932 | xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti"); |
43cd72b9 BW |
4933 | |
4934 | init_op_placement_info_table (); | |
4935 | ||
4936 | /* Set up the assembly state. */ | |
4937 | if (!frag_now->tc_frag_data.is_assembly_state_set) | |
4938 | xtensa_set_frag_assembly_state (frag_now); | |
4939 | } | |
4940 | ||
4941 | ||
4942 | /* TC_INIT_FIX_DATA hook */ | |
4943 | ||
4944 | void | |
7fa3d080 | 4945 | xtensa_init_fix_data (fixS *x) |
43cd72b9 BW |
4946 | { |
4947 | x->tc_fix_data.slot = 0; | |
4948 | x->tc_fix_data.X_add_symbol = NULL; | |
4949 | x->tc_fix_data.X_add_number = 0; | |
e0001a05 NC |
4950 | } |
4951 | ||
4952 | ||
4953 | /* tc_frob_label hook */ | |
4954 | ||
4955 | void | |
7fa3d080 | 4956 | xtensa_frob_label (symbolS *sym) |
e0001a05 | 4957 | { |
3ea38ac2 BW |
4958 | float freq; |
4959 | ||
4960 | if (cur_vinsn.inside_bundle) | |
4961 | { | |
4962 | as_bad (_("labels are not valid inside bundles")); | |
4963 | return; | |
4964 | } | |
4965 | ||
4966 | freq = get_subseg_target_freq (now_seg, now_subseg); | |
7b1cc377 | 4967 | |
43cd72b9 BW |
4968 | /* Since the label was already attached to a frag associated with the |
4969 | previous basic block, it now needs to be reset to the current frag. */ | |
4970 | symbol_set_frag (sym, frag_now); | |
4971 | S_SET_VALUE (sym, (valueT) frag_now_fix ()); | |
4972 | ||
82e7541d BW |
4973 | if (generating_literals) |
4974 | xtensa_add_literal_sym (sym); | |
4975 | else | |
4976 | xtensa_add_insn_label (sym); | |
4977 | ||
7b1cc377 BW |
4978 | if (symbol_get_tc (sym)->is_loop_target) |
4979 | { | |
4980 | if ((get_last_insn_flags (now_seg, now_subseg) | |
e0001a05 | 4981 | & FLAG_IS_BAD_LOOPEND) != 0) |
7b1cc377 BW |
4982 | as_bad (_("invalid last instruction for a zero-overhead loop")); |
4983 | ||
4984 | xtensa_set_frag_assembly_state (frag_now); | |
4985 | frag_var (rs_machine_dependent, 4, 4, RELAX_LOOP_END, | |
4986 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
4987 | ||
4988 | xtensa_set_frag_assembly_state (frag_now); | |
4989 | xtensa_move_labels (frag_now, 0, TRUE); | |
07a53e5c | 4990 | } |
e0001a05 NC |
4991 | |
4992 | /* No target aligning in the absolute section. */ | |
61846f28 | 4993 | if (now_seg != absolute_section |
43cd72b9 | 4994 | && do_align_targets () |
61846f28 | 4995 | && !is_unaligned_label (sym) |
43cd72b9 BW |
4996 | && !generating_literals) |
4997 | { | |
43cd72b9 BW |
4998 | xtensa_set_frag_assembly_state (frag_now); |
4999 | ||
43cd72b9 | 5000 | frag_var (rs_machine_dependent, |
7b1cc377 | 5001 | 0, (int) freq, |
e0001a05 NC |
5002 | RELAX_DESIRE_ALIGN_IF_TARGET, |
5003 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
43cd72b9 | 5004 | xtensa_set_frag_assembly_state (frag_now); |
82e7541d | 5005 | xtensa_move_labels (frag_now, 0, TRUE); |
43cd72b9 BW |
5006 | } |
5007 | ||
5008 | /* We need to mark the following properties even if we aren't aligning. */ | |
5009 | ||
5010 | /* If the label is already known to be a branch target, i.e., a | |
5011 | forward branch, mark the frag accordingly. Backward branches | |
5012 | are handled by xg_add_branch_and_loop_targets. */ | |
5013 | if (symbol_get_tc (sym)->is_branch_target) | |
5014 | symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE; | |
5015 | ||
5016 | /* Loops only go forward, so they can be identified here. */ | |
5017 | if (symbol_get_tc (sym)->is_loop_target) | |
5018 | symbol_get_frag (sym)->tc_frag_data.is_loop_target = TRUE; | |
07a53e5c RH |
5019 | |
5020 | dwarf2_emit_label (sym); | |
43cd72b9 BW |
5021 | } |
5022 | ||
5023 | ||
5024 | /* tc_unrecognized_line hook */ | |
5025 | ||
5026 | int | |
7fa3d080 | 5027 | xtensa_unrecognized_line (int ch) |
43cd72b9 BW |
5028 | { |
5029 | switch (ch) | |
5030 | { | |
5031 | case '{' : | |
5032 | if (cur_vinsn.inside_bundle == 0) | |
5033 | { | |
5034 | /* PR8110: Cannot emit line number info inside a FLIX bundle | |
5035 | when using --gstabs. Temporarily disable debug info. */ | |
5036 | generate_lineno_debug (); | |
5037 | if (debug_type == DEBUG_STABS) | |
5038 | { | |
5039 | xt_saved_debug_type = debug_type; | |
5040 | debug_type = DEBUG_NONE; | |
5041 | } | |
82e7541d | 5042 | |
43cd72b9 BW |
5043 | cur_vinsn.inside_bundle = 1; |
5044 | } | |
5045 | else | |
5046 | { | |
5047 | as_bad (_("extra opening brace")); | |
5048 | return 0; | |
5049 | } | |
5050 | break; | |
82e7541d | 5051 | |
43cd72b9 BW |
5052 | case '}' : |
5053 | if (cur_vinsn.inside_bundle) | |
5054 | finish_vinsn (&cur_vinsn); | |
5055 | else | |
5056 | { | |
5057 | as_bad (_("extra closing brace")); | |
5058 | return 0; | |
5059 | } | |
5060 | break; | |
5061 | default: | |
5062 | as_bad (_("syntax error")); | |
5063 | return 0; | |
e0001a05 | 5064 | } |
43cd72b9 | 5065 | return 1; |
e0001a05 NC |
5066 | } |
5067 | ||
5068 | ||
5069 | /* md_flush_pending_output hook */ | |
5070 | ||
5071 | void | |
7fa3d080 | 5072 | xtensa_flush_pending_output (void) |
e0001a05 | 5073 | { |
43cd72b9 BW |
5074 | if (cur_vinsn.inside_bundle) |
5075 | as_bad (_("missing closing brace")); | |
5076 | ||
e0001a05 NC |
5077 | /* If there is a non-zero instruction fragment, close it. */ |
5078 | if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn) | |
5079 | { | |
5080 | frag_wane (frag_now); | |
5081 | frag_new (0); | |
43cd72b9 | 5082 | xtensa_set_frag_assembly_state (frag_now); |
e0001a05 NC |
5083 | } |
5084 | frag_now->tc_frag_data.is_insn = FALSE; | |
82e7541d BW |
5085 | |
5086 | xtensa_clear_insn_labels (); | |
e0001a05 NC |
5087 | } |
5088 | ||
5089 | ||
43cd72b9 BW |
5090 | /* We had an error while parsing an instruction. The string might look |
5091 | like this: "insn arg1, arg2 }". If so, we need to see the closing | |
5092 | brace and reset some fields. Otherwise, the vinsn never gets closed | |
5093 | and the num_slots field will grow past the end of the array of slots, | |
5094 | and bad things happen. */ | |
5095 | ||
5096 | static void | |
7fa3d080 | 5097 | error_reset_cur_vinsn (void) |
43cd72b9 BW |
5098 | { |
5099 | if (cur_vinsn.inside_bundle) | |
5100 | { | |
5101 | if (*input_line_pointer == '}' | |
5102 | || *(input_line_pointer - 1) == '}' | |
5103 | || *(input_line_pointer - 2) == '}') | |
5104 | xg_clear_vinsn (&cur_vinsn); | |
5105 | } | |
5106 | } | |
5107 | ||
5108 | ||
e0001a05 | 5109 | void |
7fa3d080 | 5110 | md_assemble (char *str) |
e0001a05 NC |
5111 | { |
5112 | xtensa_isa isa = xtensa_default_isa; | |
7c430684 | 5113 | char *opname, *file_name; |
e0001a05 NC |
5114 | unsigned opnamelen; |
5115 | bfd_boolean has_underbar = FALSE; | |
43cd72b9 | 5116 | char *arg_strings[MAX_INSN_ARGS]; |
e0001a05 | 5117 | int num_args; |
e0001a05 | 5118 | TInsn orig_insn; /* Original instruction from the input. */ |
e0001a05 | 5119 | |
e0001a05 NC |
5120 | tinsn_init (&orig_insn); |
5121 | ||
5122 | /* Split off the opcode. */ | |
5123 | opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789."); | |
5124 | opname = xmalloc (opnamelen + 1); | |
5125 | memcpy (opname, str, opnamelen); | |
5126 | opname[opnamelen] = '\0'; | |
5127 | ||
5128 | num_args = tokenize_arguments (arg_strings, str + opnamelen); | |
5129 | if (num_args == -1) | |
5130 | { | |
5131 | as_bad (_("syntax error")); | |
5132 | return; | |
5133 | } | |
5134 | ||
5135 | if (xg_translate_idioms (&opname, &num_args, arg_strings)) | |
5136 | return; | |
5137 | ||
5138 | /* Check for an underbar prefix. */ | |
5139 | if (*opname == '_') | |
5140 | { | |
5141 | has_underbar = TRUE; | |
5142 | opname += 1; | |
5143 | } | |
5144 | ||
5145 | orig_insn.insn_type = ITYPE_INSN; | |
5146 | orig_insn.ntok = 0; | |
43cd72b9 | 5147 | orig_insn.is_specific_opcode = (has_underbar || !use_transform ()); |
e0001a05 NC |
5148 | |
5149 | orig_insn.opcode = xtensa_opcode_lookup (isa, opname); | |
5150 | if (orig_insn.opcode == XTENSA_UNDEFINED) | |
5151 | { | |
43cd72b9 BW |
5152 | xtensa_format fmt = xtensa_format_lookup (isa, opname); |
5153 | if (fmt == XTENSA_UNDEFINED) | |
5154 | { | |
5155 | as_bad (_("unknown opcode or format name '%s'"), opname); | |
5156 | error_reset_cur_vinsn (); | |
5157 | return; | |
5158 | } | |
5159 | if (!cur_vinsn.inside_bundle) | |
5160 | { | |
5161 | as_bad (_("format names only valid inside bundles")); | |
5162 | error_reset_cur_vinsn (); | |
5163 | return; | |
5164 | } | |
5165 | if (cur_vinsn.format != XTENSA_UNDEFINED) | |
5166 | as_warn (_("multiple formats specified for one bundle; using '%s'"), | |
5167 | opname); | |
5168 | cur_vinsn.format = fmt; | |
5169 | free (has_underbar ? opname - 1 : opname); | |
5170 | error_reset_cur_vinsn (); | |
e0001a05 NC |
5171 | return; |
5172 | } | |
5173 | ||
e0001a05 NC |
5174 | /* Parse the arguments. */ |
5175 | if (parse_arguments (&orig_insn, num_args, arg_strings)) | |
5176 | { | |
5177 | as_bad (_("syntax error")); | |
43cd72b9 | 5178 | error_reset_cur_vinsn (); |
e0001a05 NC |
5179 | return; |
5180 | } | |
5181 | ||
5182 | /* Free the opcode and argument strings, now that they've been parsed. */ | |
5183 | free (has_underbar ? opname - 1 : opname); | |
5184 | opname = 0; | |
5185 | while (num_args-- > 0) | |
5186 | free (arg_strings[num_args]); | |
5187 | ||
43cd72b9 BW |
5188 | /* Get expressions for invisible operands. */ |
5189 | if (get_invisible_operands (&orig_insn)) | |
5190 | { | |
5191 | error_reset_cur_vinsn (); | |
5192 | return; | |
5193 | } | |
5194 | ||
e0001a05 NC |
5195 | /* Check for the right number and type of arguments. */ |
5196 | if (tinsn_check_arguments (&orig_insn)) | |
e0001a05 | 5197 | { |
43cd72b9 BW |
5198 | error_reset_cur_vinsn (); |
5199 | return; | |
e0001a05 NC |
5200 | } |
5201 | ||
7c430684 BW |
5202 | /* A FLIX bundle may be spread across multiple input lines. We want to |
5203 | report the first such line in the debug information. Record the line | |
5204 | number for each TInsn (assume the file name doesn't change), so the | |
5205 | first line can be found later. */ | |
5206 | as_where (&file_name, &orig_insn.linenum); | |
c138bc38 | 5207 | |
43cd72b9 BW |
5208 | xg_add_branch_and_loop_targets (&orig_insn); |
5209 | ||
431ad2d0 BW |
5210 | /* Check that immediate value for ENTRY is >= 16. */ |
5211 | if (orig_insn.opcode == xtensa_entry_opcode && orig_insn.ntok >= 3) | |
e0001a05 | 5212 | { |
431ad2d0 BW |
5213 | expressionS *exp = &orig_insn.tok[2]; |
5214 | if (exp->X_op == O_constant && exp->X_add_number < 16) | |
5215 | as_warn (_("entry instruction with stack decrement < 16")); | |
e0001a05 NC |
5216 | } |
5217 | ||
e0001a05 | 5218 | /* Finish it off: |
43cd72b9 BW |
5219 | assemble_tokens (opcode, tok, ntok); |
5220 | expand the tokens from the orig_insn into the | |
5221 | stack of instructions that will not expand | |
e0001a05 | 5222 | unless required at relaxation time. */ |
e0001a05 | 5223 | |
43cd72b9 BW |
5224 | if (!cur_vinsn.inside_bundle) |
5225 | emit_single_op (&orig_insn); | |
5226 | else /* We are inside a bundle. */ | |
e0001a05 | 5227 | { |
43cd72b9 BW |
5228 | cur_vinsn.slots[cur_vinsn.num_slots] = orig_insn; |
5229 | cur_vinsn.num_slots++; | |
5230 | if (*input_line_pointer == '}' | |
5231 | || *(input_line_pointer - 1) == '}' | |
5232 | || *(input_line_pointer - 2) == '}') | |
5233 | finish_vinsn (&cur_vinsn); | |
e0001a05 NC |
5234 | } |
5235 | ||
43cd72b9 BW |
5236 | /* We've just emitted a new instruction so clear the list of labels. */ |
5237 | xtensa_clear_insn_labels (); | |
e0001a05 NC |
5238 | } |
5239 | ||
5240 | ||
43cd72b9 | 5241 | /* HANDLE_ALIGN hook */ |
e0001a05 | 5242 | |
43cd72b9 BW |
5243 | /* For a .align directive, we mark the previous block with the alignment |
5244 | information. This will be placed in the object file in the | |
5245 | property section corresponding to this section. */ | |
e0001a05 | 5246 | |
43cd72b9 | 5247 | void |
7fa3d080 | 5248 | xtensa_handle_align (fragS *fragP) |
43cd72b9 BW |
5249 | { |
5250 | if (linkrelax | |
b08b5071 | 5251 | && ! fragP->tc_frag_data.is_literal |
43cd72b9 BW |
5252 | && (fragP->fr_type == rs_align |
5253 | || fragP->fr_type == rs_align_code) | |
5254 | && fragP->fr_address + fragP->fr_fix > 0 | |
5255 | && fragP->fr_offset > 0 | |
5256 | && now_seg != bss_section) | |
e0001a05 | 5257 | { |
43cd72b9 BW |
5258 | fragP->tc_frag_data.is_align = TRUE; |
5259 | fragP->tc_frag_data.alignment = fragP->fr_offset; | |
e0001a05 NC |
5260 | } |
5261 | ||
43cd72b9 | 5262 | if (fragP->fr_type == rs_align_test) |
e0001a05 | 5263 | { |
43cd72b9 BW |
5264 | int count; |
5265 | count = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix; | |
5266 | if (count != 0) | |
c138bc38 | 5267 | as_bad_where (fragP->fr_file, fragP->fr_line, |
43cd72b9 | 5268 | _("unaligned entry instruction")); |
e0001a05 | 5269 | } |
e0001a05 | 5270 | } |
43cd72b9 | 5271 | |
e0001a05 NC |
5272 | |
5273 | /* TC_FRAG_INIT hook */ | |
5274 | ||
5275 | void | |
7fa3d080 | 5276 | xtensa_frag_init (fragS *frag) |
e0001a05 | 5277 | { |
43cd72b9 | 5278 | xtensa_set_frag_assembly_state (frag); |
e0001a05 NC |
5279 | } |
5280 | ||
5281 | ||
5282 | symbolS * | |
7fa3d080 | 5283 | md_undefined_symbol (char *name ATTRIBUTE_UNUSED) |
e0001a05 NC |
5284 | { |
5285 | return NULL; | |
5286 | } | |
5287 | ||
5288 | ||
5289 | /* Round up a section size to the appropriate boundary. */ | |
5290 | ||
5291 | valueT | |
7fa3d080 | 5292 | md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size) |
e0001a05 NC |
5293 | { |
5294 | return size; /* Byte alignment is fine. */ | |
5295 | } | |
5296 | ||
5297 | ||
5298 | long | |
7fa3d080 | 5299 | md_pcrel_from (fixS *fixP) |
e0001a05 NC |
5300 | { |
5301 | char *insn_p; | |
5302 | static xtensa_insnbuf insnbuf = NULL; | |
43cd72b9 | 5303 | static xtensa_insnbuf slotbuf = NULL; |
e0001a05 | 5304 | int opnum; |
43cd72b9 | 5305 | uint32 opnd_value; |
e0001a05 | 5306 | xtensa_opcode opcode; |
43cd72b9 BW |
5307 | xtensa_format fmt; |
5308 | int slot; | |
e0001a05 NC |
5309 | xtensa_isa isa = xtensa_default_isa; |
5310 | valueT addr = fixP->fx_where + fixP->fx_frag->fr_address; | |
43cd72b9 | 5311 | bfd_boolean alt_reloc; |
e0001a05 | 5312 | |
e0001a05 | 5313 | if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND) |
30f725a1 | 5314 | return 0; |
e0001a05 NC |
5315 | |
5316 | if (!insnbuf) | |
43cd72b9 BW |
5317 | { |
5318 | insnbuf = xtensa_insnbuf_alloc (isa); | |
5319 | slotbuf = xtensa_insnbuf_alloc (isa); | |
5320 | } | |
e0001a05 NC |
5321 | |
5322 | insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where]; | |
d77b99c9 | 5323 | xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) insn_p, 0); |
43cd72b9 BW |
5324 | fmt = xtensa_format_decode (isa, insnbuf); |
5325 | ||
5326 | if (fmt == XTENSA_UNDEFINED) | |
5327 | as_fatal (_("bad instruction format")); | |
5328 | ||
5329 | if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc) != 0) | |
5330 | as_fatal (_("invalid relocation")); | |
5331 | ||
5332 | xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf); | |
5333 | opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf); | |
5334 | ||
30f725a1 BW |
5335 | /* Check for "alternate" relocations (operand not specified). None |
5336 | of the current uses for these are really PC-relative. */ | |
43cd72b9 BW |
5337 | if (alt_reloc || opcode == xtensa_const16_opcode) |
5338 | { | |
5339 | if (opcode != xtensa_l32r_opcode | |
5340 | && opcode != xtensa_const16_opcode) | |
5341 | as_fatal (_("invalid relocation for '%s' instruction"), | |
5342 | xtensa_opcode_name (isa, opcode)); | |
30f725a1 | 5343 | return 0; |
e0001a05 NC |
5344 | } |
5345 | ||
43cd72b9 BW |
5346 | opnum = get_relaxable_immed (opcode); |
5347 | opnd_value = 0; | |
5348 | if (xtensa_operand_is_PCrelative (isa, opcode, opnum) != 1 | |
5349 | || xtensa_operand_do_reloc (isa, opcode, opnum, &opnd_value, addr)) | |
e0001a05 NC |
5350 | { |
5351 | as_bad_where (fixP->fx_file, | |
5352 | fixP->fx_line, | |
5353 | _("invalid relocation for operand %d of '%s'"), | |
5354 | opnum, xtensa_opcode_name (isa, opcode)); | |
30f725a1 | 5355 | return 0; |
e0001a05 | 5356 | } |
43cd72b9 BW |
5357 | return 0 - opnd_value; |
5358 | } | |
5359 | ||
5360 | ||
5361 | /* TC_FORCE_RELOCATION hook */ | |
5362 | ||
5363 | int | |
7fa3d080 | 5364 | xtensa_force_relocation (fixS *fix) |
43cd72b9 BW |
5365 | { |
5366 | switch (fix->fx_r_type) | |
30f725a1 BW |
5367 | { |
5368 | case BFD_RELOC_XTENSA_ASM_EXPAND: | |
43cd72b9 BW |
5369 | case BFD_RELOC_XTENSA_SLOT0_ALT: |
5370 | case BFD_RELOC_XTENSA_SLOT1_ALT: | |
5371 | case BFD_RELOC_XTENSA_SLOT2_ALT: | |
5372 | case BFD_RELOC_XTENSA_SLOT3_ALT: | |
5373 | case BFD_RELOC_XTENSA_SLOT4_ALT: | |
5374 | case BFD_RELOC_XTENSA_SLOT5_ALT: | |
5375 | case BFD_RELOC_XTENSA_SLOT6_ALT: | |
5376 | case BFD_RELOC_XTENSA_SLOT7_ALT: | |
5377 | case BFD_RELOC_XTENSA_SLOT8_ALT: | |
5378 | case BFD_RELOC_XTENSA_SLOT9_ALT: | |
5379 | case BFD_RELOC_XTENSA_SLOT10_ALT: | |
5380 | case BFD_RELOC_XTENSA_SLOT11_ALT: | |
5381 | case BFD_RELOC_XTENSA_SLOT12_ALT: | |
5382 | case BFD_RELOC_XTENSA_SLOT13_ALT: | |
5383 | case BFD_RELOC_XTENSA_SLOT14_ALT: | |
43cd72b9 BW |
5384 | return 1; |
5385 | default: | |
5386 | break; | |
e0001a05 NC |
5387 | } |
5388 | ||
43cd72b9 BW |
5389 | if (linkrelax && fix->fx_addsy |
5390 | && relaxable_section (S_GET_SEGMENT (fix->fx_addsy))) | |
5391 | return 1; | |
5392 | ||
5393 | return generic_force_reloc (fix); | |
5394 | } | |
5395 | ||
5396 | ||
30f725a1 BW |
5397 | /* TC_VALIDATE_FIX_SUB hook */ |
5398 | ||
5399 | int | |
5400 | xtensa_validate_fix_sub (fixS *fix) | |
5401 | { | |
5402 | segT add_symbol_segment, sub_symbol_segment; | |
5403 | ||
5404 | /* The difference of two symbols should be resolved by the assembler when | |
5405 | linkrelax is not set. If the linker may relax the section containing | |
5406 | the symbols, then an Xtensa DIFF relocation must be generated so that | |
5407 | the linker knows to adjust the difference value. */ | |
5408 | if (!linkrelax || fix->fx_addsy == NULL) | |
5409 | return 0; | |
5410 | ||
5411 | /* Make sure both symbols are in the same segment, and that segment is | |
5412 | "normal" and relaxable. If the segment is not "normal", then the | |
5413 | fix is not valid. If the segment is not "relaxable", then the fix | |
5414 | should have been handled earlier. */ | |
5415 | add_symbol_segment = S_GET_SEGMENT (fix->fx_addsy); | |
5416 | if (! SEG_NORMAL (add_symbol_segment) || | |
5417 | ! relaxable_section (add_symbol_segment)) | |
5418 | return 0; | |
5419 | sub_symbol_segment = S_GET_SEGMENT (fix->fx_subsy); | |
5420 | return (sub_symbol_segment == add_symbol_segment); | |
5421 | } | |
5422 | ||
5423 | ||
43cd72b9 BW |
5424 | /* NO_PSEUDO_DOT hook */ |
5425 | ||
5426 | /* This function has nothing to do with pseudo dots, but this is the | |
5427 | nearest macro to where the check needs to take place. FIXME: This | |
5428 | seems wrong. */ | |
5429 | ||
5430 | bfd_boolean | |
7fa3d080 | 5431 | xtensa_check_inside_bundle (void) |
43cd72b9 BW |
5432 | { |
5433 | if (cur_vinsn.inside_bundle && input_line_pointer[-1] == '.') | |
5434 | as_bad (_("directives are not valid inside bundles")); | |
5435 | ||
5436 | /* This function must always return FALSE because it is called via a | |
5437 | macro that has nothing to do with bundling. */ | |
5438 | return FALSE; | |
e0001a05 NC |
5439 | } |
5440 | ||
5441 | ||
43cd72b9 | 5442 | /* md_elf_section_change_hook */ |
e0001a05 NC |
5443 | |
5444 | void | |
7fa3d080 | 5445 | xtensa_elf_section_change_hook (void) |
e0001a05 | 5446 | { |
43cd72b9 BW |
5447 | /* Set up the assembly state. */ |
5448 | if (!frag_now->tc_frag_data.is_assembly_state_set) | |
5449 | xtensa_set_frag_assembly_state (frag_now); | |
e0001a05 NC |
5450 | } |
5451 | ||
5452 | ||
5453 | /* tc_fix_adjustable hook */ | |
5454 | ||
5455 | bfd_boolean | |
7fa3d080 | 5456 | xtensa_fix_adjustable (fixS *fixP) |
e0001a05 | 5457 | { |
43cd72b9 BW |
5458 | /* An offset is not allowed in combination with the difference of two |
5459 | symbols, but that cannot be easily detected after a local symbol | |
5460 | has been adjusted to a (section+offset) form. Return 0 so that such | |
5461 | an fix will not be adjusted. */ | |
5462 | if (fixP->fx_subsy && fixP->fx_addsy && fixP->fx_offset | |
5463 | && relaxable_section (S_GET_SEGMENT (fixP->fx_subsy))) | |
5464 | return 0; | |
5465 | ||
e0001a05 NC |
5466 | /* We need the symbol name for the VTABLE entries. */ |
5467 | if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT | |
5468 | || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY) | |
5469 | return 0; | |
5470 | ||
5471 | return 1; | |
5472 | } | |
5473 | ||
5474 | ||
5475 | void | |
55cf6793 | 5476 | md_apply_fix (fixS *fixP, valueT *valP, segT seg) |
e0001a05 | 5477 | { |
30f725a1 | 5478 | char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where; |
d47d412e | 5479 | valueT val = 0; |
30f725a1 | 5480 | |
e7da6241 BW |
5481 | /* Subtracted symbols are only allowed for a few relocation types, and |
5482 | unless linkrelax is enabled, they should not make it to this point. */ | |
5483 | if (fixP->fx_subsy && !(linkrelax && (fixP->fx_r_type == BFD_RELOC_32 | |
5484 | || fixP->fx_r_type == BFD_RELOC_16 | |
5485 | || fixP->fx_r_type == BFD_RELOC_8))) | |
5486 | as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex")); | |
5487 | ||
30f725a1 | 5488 | switch (fixP->fx_r_type) |
e0001a05 | 5489 | { |
30f725a1 BW |
5490 | case BFD_RELOC_32: |
5491 | case BFD_RELOC_16: | |
5492 | case BFD_RELOC_8: | |
e7da6241 | 5493 | if (fixP->fx_subsy) |
30f725a1 BW |
5494 | { |
5495 | switch (fixP->fx_r_type) | |
5496 | { | |
5497 | case BFD_RELOC_8: | |
5498 | fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF8; | |
5499 | break; | |
5500 | case BFD_RELOC_16: | |
5501 | fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF16; | |
5502 | break; | |
5503 | case BFD_RELOC_32: | |
5504 | fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF32; | |
5505 | break; | |
5506 | default: | |
5507 | break; | |
5508 | } | |
e0001a05 | 5509 | |
30f725a1 BW |
5510 | /* An offset is only allowed when it results from adjusting a |
5511 | local symbol into a section-relative offset. If the offset | |
5512 | came from the original expression, tc_fix_adjustable will have | |
5513 | prevented the fix from being converted to a section-relative | |
5514 | form so that we can flag the error here. */ | |
5515 | if (fixP->fx_offset != 0 && !symbol_section_p (fixP->fx_addsy)) | |
5516 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
5517 | _("cannot represent subtraction with an offset")); | |
5518 | ||
5519 | val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset | |
5520 | - S_GET_VALUE (fixP->fx_subsy)); | |
5521 | ||
5522 | /* The difference value gets written out, and the DIFF reloc | |
5523 | identifies the address of the subtracted symbol (i.e., the one | |
5524 | with the lowest address). */ | |
5525 | *valP = val; | |
5526 | fixP->fx_offset -= val; | |
5527 | fixP->fx_subsy = NULL; | |
5528 | } | |
5529 | else if (! fixP->fx_addsy) | |
e0001a05 | 5530 | { |
30f725a1 | 5531 | val = *valP; |
e0001a05 | 5532 | fixP->fx_done = 1; |
30f725a1 | 5533 | } |
d47d412e BW |
5534 | /* fall through */ |
5535 | ||
5536 | case BFD_RELOC_XTENSA_PLT: | |
30f725a1 BW |
5537 | md_number_to_chars (fixpos, val, fixP->fx_size); |
5538 | fixP->fx_no_overflow = 0; /* Use the standard overflow check. */ | |
5539 | break; | |
e0001a05 | 5540 | |
30f725a1 BW |
5541 | case BFD_RELOC_XTENSA_SLOT0_OP: |
5542 | case BFD_RELOC_XTENSA_SLOT1_OP: | |
5543 | case BFD_RELOC_XTENSA_SLOT2_OP: | |
5544 | case BFD_RELOC_XTENSA_SLOT3_OP: | |
5545 | case BFD_RELOC_XTENSA_SLOT4_OP: | |
5546 | case BFD_RELOC_XTENSA_SLOT5_OP: | |
5547 | case BFD_RELOC_XTENSA_SLOT6_OP: | |
5548 | case BFD_RELOC_XTENSA_SLOT7_OP: | |
5549 | case BFD_RELOC_XTENSA_SLOT8_OP: | |
5550 | case BFD_RELOC_XTENSA_SLOT9_OP: | |
5551 | case BFD_RELOC_XTENSA_SLOT10_OP: | |
5552 | case BFD_RELOC_XTENSA_SLOT11_OP: | |
5553 | case BFD_RELOC_XTENSA_SLOT12_OP: | |
5554 | case BFD_RELOC_XTENSA_SLOT13_OP: | |
5555 | case BFD_RELOC_XTENSA_SLOT14_OP: | |
5556 | if (linkrelax) | |
5557 | { | |
5558 | /* Write the tentative value of a PC-relative relocation to a | |
5559 | local symbol into the instruction. The value will be ignored | |
5560 | by the linker, and it makes the object file disassembly | |
5561 | readable when all branch targets are encoded in relocations. */ | |
5562 | ||
5563 | assert (fixP->fx_addsy); | |
5564 | if (S_GET_SEGMENT (fixP->fx_addsy) == seg && !fixP->fx_plt | |
5565 | && !S_FORCE_RELOC (fixP->fx_addsy, 1)) | |
5566 | { | |
5567 | val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset | |
5568 | - md_pcrel_from (fixP)); | |
5569 | (void) xg_apply_fix_value (fixP, val); | |
5570 | } | |
5571 | } | |
5572 | else if (! fixP->fx_addsy) | |
5573 | { | |
5574 | val = *valP; | |
5575 | if (xg_apply_fix_value (fixP, val)) | |
5576 | fixP->fx_done = 1; | |
5577 | } | |
5578 | break; | |
e0001a05 | 5579 | |
30f725a1 BW |
5580 | case BFD_RELOC_XTENSA_ASM_EXPAND: |
5581 | case BFD_RELOC_XTENSA_SLOT0_ALT: | |
5582 | case BFD_RELOC_XTENSA_SLOT1_ALT: | |
5583 | case BFD_RELOC_XTENSA_SLOT2_ALT: | |
5584 | case BFD_RELOC_XTENSA_SLOT3_ALT: | |
5585 | case BFD_RELOC_XTENSA_SLOT4_ALT: | |
5586 | case BFD_RELOC_XTENSA_SLOT5_ALT: | |
5587 | case BFD_RELOC_XTENSA_SLOT6_ALT: | |
5588 | case BFD_RELOC_XTENSA_SLOT7_ALT: | |
5589 | case BFD_RELOC_XTENSA_SLOT8_ALT: | |
5590 | case BFD_RELOC_XTENSA_SLOT9_ALT: | |
5591 | case BFD_RELOC_XTENSA_SLOT10_ALT: | |
5592 | case BFD_RELOC_XTENSA_SLOT11_ALT: | |
5593 | case BFD_RELOC_XTENSA_SLOT12_ALT: | |
5594 | case BFD_RELOC_XTENSA_SLOT13_ALT: | |
5595 | case BFD_RELOC_XTENSA_SLOT14_ALT: | |
5596 | /* These all need to be resolved at link-time. Do nothing now. */ | |
5597 | break; | |
e0001a05 | 5598 | |
30f725a1 BW |
5599 | case BFD_RELOC_VTABLE_INHERIT: |
5600 | case BFD_RELOC_VTABLE_ENTRY: | |
5601 | fixP->fx_done = 0; | |
5602 | break; | |
e0001a05 | 5603 | |
30f725a1 BW |
5604 | default: |
5605 | as_bad (_("unhandled local relocation fix %s"), | |
5606 | bfd_get_reloc_code_name (fixP->fx_r_type)); | |
e0001a05 NC |
5607 | } |
5608 | } | |
5609 | ||
5610 | ||
5611 | char * | |
7fa3d080 | 5612 | md_atof (int type, char *litP, int *sizeP) |
e0001a05 NC |
5613 | { |
5614 | int prec; | |
5615 | LITTLENUM_TYPE words[4]; | |
5616 | char *t; | |
5617 | int i; | |
5618 | ||
5619 | switch (type) | |
5620 | { | |
5621 | case 'f': | |
5622 | prec = 2; | |
5623 | break; | |
5624 | ||
5625 | case 'd': | |
5626 | prec = 4; | |
5627 | break; | |
5628 | ||
5629 | default: | |
5630 | *sizeP = 0; | |
5631 | return "bad call to md_atof"; | |
5632 | } | |
5633 | ||
5634 | t = atof_ieee (input_line_pointer, type, words); | |
5635 | if (t) | |
5636 | input_line_pointer = t; | |
5637 | ||
5638 | *sizeP = prec * 2; | |
5639 | ||
5640 | for (i = prec - 1; i >= 0; i--) | |
5641 | { | |
5642 | int idx = i; | |
5643 | if (target_big_endian) | |
5644 | idx = (prec - 1 - i); | |
5645 | ||
5646 | md_number_to_chars (litP, (valueT) words[idx], 2); | |
5647 | litP += 2; | |
5648 | } | |
5649 | ||
5650 | return NULL; | |
5651 | } | |
5652 | ||
5653 | ||
5654 | int | |
7fa3d080 | 5655 | md_estimate_size_before_relax (fragS *fragP, segT seg ATTRIBUTE_UNUSED) |
e0001a05 | 5656 | { |
34e41783 | 5657 | return total_frag_text_expansion (fragP); |
e0001a05 NC |
5658 | } |
5659 | ||
5660 | ||
5661 | /* Translate internal representation of relocation info to BFD target | |
5662 | format. */ | |
5663 | ||
5664 | arelent * | |
30f725a1 | 5665 | tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp) |
e0001a05 NC |
5666 | { |
5667 | arelent *reloc; | |
5668 | ||
5669 | reloc = (arelent *) xmalloc (sizeof (arelent)); | |
5670 | reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *)); | |
5671 | *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy); | |
5672 | reloc->address = fixp->fx_frag->fr_address + fixp->fx_where; | |
5673 | ||
5674 | /* Make sure none of our internal relocations make it this far. | |
5675 | They'd better have been fully resolved by this point. */ | |
5676 | assert ((int) fixp->fx_r_type > 0); | |
5677 | ||
30f725a1 | 5678 | reloc->addend = fixp->fx_offset; |
43cd72b9 | 5679 | |
e0001a05 NC |
5680 | reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type); |
5681 | if (reloc->howto == NULL) | |
5682 | { | |
5683 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
5684 | _("cannot represent `%s' relocation in object file"), | |
5685 | bfd_get_reloc_code_name (fixp->fx_r_type)); | |
43cd72b9 BW |
5686 | free (reloc->sym_ptr_ptr); |
5687 | free (reloc); | |
e0001a05 NC |
5688 | return NULL; |
5689 | } | |
5690 | ||
5691 | if (!fixp->fx_pcrel != !reloc->howto->pc_relative) | |
43cd72b9 BW |
5692 | as_fatal (_("internal error? cannot generate `%s' relocation"), |
5693 | bfd_get_reloc_code_name (fixp->fx_r_type)); | |
e0001a05 | 5694 | |
e0001a05 NC |
5695 | return reloc; |
5696 | } | |
5697 | ||
7fa3d080 BW |
5698 | \f |
5699 | /* Checks for resource conflicts between instructions. */ | |
5700 | ||
c138bc38 BW |
5701 | /* The func unit stuff could be implemented as bit-vectors rather |
5702 | than the iterative approach here. If it ends up being too | |
7fa3d080 BW |
5703 | slow, we will switch it. */ |
5704 | ||
c138bc38 | 5705 | resource_table * |
7fa3d080 BW |
5706 | new_resource_table (void *data, |
5707 | int cycles, | |
5708 | int nu, | |
5709 | unit_num_copies_func uncf, | |
5710 | opcode_num_units_func onuf, | |
5711 | opcode_funcUnit_use_unit_func ouuf, | |
5712 | opcode_funcUnit_use_stage_func ousf) | |
5713 | { | |
5714 | int i; | |
5715 | resource_table *rt = (resource_table *) xmalloc (sizeof (resource_table)); | |
5716 | rt->data = data; | |
5717 | rt->cycles = cycles; | |
5718 | rt->allocated_cycles = cycles; | |
5719 | rt->num_units = nu; | |
5720 | rt->unit_num_copies = uncf; | |
5721 | rt->opcode_num_units = onuf; | |
5722 | rt->opcode_unit_use = ouuf; | |
5723 | rt->opcode_unit_stage = ousf; | |
5724 | ||
0bf60745 | 5725 | rt->units = (unsigned char **) xcalloc (cycles, sizeof (unsigned char *)); |
7fa3d080 | 5726 | for (i = 0; i < cycles; i++) |
0bf60745 | 5727 | rt->units[i] = (unsigned char *) xcalloc (nu, sizeof (unsigned char)); |
7fa3d080 BW |
5728 | |
5729 | return rt; | |
5730 | } | |
5731 | ||
5732 | ||
c138bc38 | 5733 | void |
7fa3d080 BW |
5734 | clear_resource_table (resource_table *rt) |
5735 | { | |
5736 | int i, j; | |
5737 | for (i = 0; i < rt->allocated_cycles; i++) | |
5738 | for (j = 0; j < rt->num_units; j++) | |
5739 | rt->units[i][j] = 0; | |
5740 | } | |
5741 | ||
5742 | ||
5743 | /* We never shrink it, just fake it into thinking so. */ | |
5744 | ||
c138bc38 | 5745 | void |
7fa3d080 BW |
5746 | resize_resource_table (resource_table *rt, int cycles) |
5747 | { | |
5748 | int i, old_cycles; | |
5749 | ||
5750 | rt->cycles = cycles; | |
5751 | if (cycles <= rt->allocated_cycles) | |
5752 | return; | |
5753 | ||
5754 | old_cycles = rt->allocated_cycles; | |
5755 | rt->allocated_cycles = cycles; | |
5756 | ||
0bf60745 BW |
5757 | rt->units = xrealloc (rt->units, |
5758 | rt->allocated_cycles * sizeof (unsigned char *)); | |
7fa3d080 | 5759 | for (i = 0; i < old_cycles; i++) |
0bf60745 BW |
5760 | rt->units[i] = xrealloc (rt->units[i], |
5761 | rt->num_units * sizeof (unsigned char)); | |
7fa3d080 | 5762 | for (i = old_cycles; i < cycles; i++) |
0bf60745 | 5763 | rt->units[i] = xcalloc (rt->num_units, sizeof (unsigned char)); |
7fa3d080 BW |
5764 | } |
5765 | ||
5766 | ||
c138bc38 | 5767 | bfd_boolean |
7fa3d080 BW |
5768 | resources_available (resource_table *rt, xtensa_opcode opcode, int cycle) |
5769 | { | |
5770 | int i; | |
5771 | int uses = (rt->opcode_num_units) (rt->data, opcode); | |
5772 | ||
c138bc38 | 5773 | for (i = 0; i < uses; i++) |
7fa3d080 BW |
5774 | { |
5775 | xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i); | |
5776 | int stage = (rt->opcode_unit_stage) (rt->data, opcode, i); | |
5777 | int copies_in_use = rt->units[stage + cycle][unit]; | |
5778 | int copies = (rt->unit_num_copies) (rt->data, unit); | |
5779 | if (copies_in_use >= copies) | |
5780 | return FALSE; | |
5781 | } | |
5782 | return TRUE; | |
5783 | } | |
7fa3d080 | 5784 | |
c138bc38 BW |
5785 | |
5786 | void | |
7fa3d080 BW |
5787 | reserve_resources (resource_table *rt, xtensa_opcode opcode, int cycle) |
5788 | { | |
5789 | int i; | |
5790 | int uses = (rt->opcode_num_units) (rt->data, opcode); | |
5791 | ||
c138bc38 | 5792 | for (i = 0; i < uses; i++) |
7fa3d080 BW |
5793 | { |
5794 | xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i); | |
5795 | int stage = (rt->opcode_unit_stage) (rt->data, opcode, i); | |
c138bc38 BW |
5796 | /* Note that this allows resources to be oversubscribed. That's |
5797 | essential to the way the optional scheduler works. | |
7fa3d080 BW |
5798 | resources_available reports when a resource is over-subscribed, |
5799 | so it's easy to tell. */ | |
5800 | rt->units[stage + cycle][unit]++; | |
5801 | } | |
5802 | } | |
5803 | ||
5804 | ||
c138bc38 | 5805 | void |
7fa3d080 BW |
5806 | release_resources (resource_table *rt, xtensa_opcode opcode, int cycle) |
5807 | { | |
5808 | int i; | |
5809 | int uses = (rt->opcode_num_units) (rt->data, opcode); | |
5810 | ||
c138bc38 | 5811 | for (i = 0; i < uses; i++) |
7fa3d080 BW |
5812 | { |
5813 | xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i); | |
5814 | int stage = (rt->opcode_unit_stage) (rt->data, opcode, i); | |
0bf60745 | 5815 | assert (rt->units[stage + cycle][unit] > 0); |
7fa3d080 | 5816 | rt->units[stage + cycle][unit]--; |
7fa3d080 BW |
5817 | } |
5818 | } | |
c138bc38 | 5819 | |
7fa3d080 BW |
5820 | |
5821 | /* Wrapper functions make parameterized resource reservation | |
5822 | more convenient. */ | |
5823 | ||
c138bc38 | 5824 | int |
7fa3d080 BW |
5825 | opcode_funcUnit_use_unit (void *data, xtensa_opcode opcode, int idx) |
5826 | { | |
5827 | xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx); | |
c138bc38 | 5828 | return use->unit; |
7fa3d080 BW |
5829 | } |
5830 | ||
5831 | ||
c138bc38 | 5832 | int |
7fa3d080 BW |
5833 | opcode_funcUnit_use_stage (void *data, xtensa_opcode opcode, int idx) |
5834 | { | |
5835 | xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx); | |
5836 | return use->stage; | |
5837 | } | |
5838 | ||
5839 | ||
5840 | /* Note that this function does not check issue constraints, but | |
5841 | solely whether the hardware is available to execute the given | |
c138bc38 | 5842 | instructions together. It also doesn't check if the tinsns |
7fa3d080 | 5843 | write the same state, or access the same tieports. That is |
a1ace8d8 | 5844 | checked by check_t1_t2_reads_and_writes. */ |
7fa3d080 BW |
5845 | |
5846 | static bfd_boolean | |
5847 | resources_conflict (vliw_insn *vinsn) | |
5848 | { | |
5849 | int i; | |
5850 | static resource_table *rt = NULL; | |
5851 | ||
5852 | /* This is the most common case by far. Optimize it. */ | |
5853 | if (vinsn->num_slots == 1) | |
5854 | return FALSE; | |
43cd72b9 | 5855 | |
c138bc38 | 5856 | if (rt == NULL) |
7fa3d080 BW |
5857 | { |
5858 | xtensa_isa isa = xtensa_default_isa; | |
5859 | rt = new_resource_table | |
5860 | (isa, xtensa_isa_num_pipe_stages (isa), | |
5861 | xtensa_isa_num_funcUnits (isa), | |
5862 | (unit_num_copies_func) xtensa_funcUnit_num_copies, | |
5863 | (opcode_num_units_func) xtensa_opcode_num_funcUnit_uses, | |
5864 | opcode_funcUnit_use_unit, | |
5865 | opcode_funcUnit_use_stage); | |
5866 | } | |
43cd72b9 | 5867 | |
7fa3d080 | 5868 | clear_resource_table (rt); |
43cd72b9 | 5869 | |
7fa3d080 BW |
5870 | for (i = 0; i < vinsn->num_slots; i++) |
5871 | { | |
5872 | if (!resources_available (rt, vinsn->slots[i].opcode, 0)) | |
5873 | return TRUE; | |
5874 | reserve_resources (rt, vinsn->slots[i].opcode, 0); | |
5875 | } | |
e0001a05 | 5876 | |
7fa3d080 BW |
5877 | return FALSE; |
5878 | } | |
e0001a05 | 5879 | |
7fa3d080 BW |
5880 | \f |
5881 | /* finish_vinsn, emit_single_op and helper functions. */ | |
e0001a05 | 5882 | |
7fa3d080 BW |
5883 | static bfd_boolean find_vinsn_conflicts (vliw_insn *); |
5884 | static xtensa_format xg_find_narrowest_format (vliw_insn *); | |
7fa3d080 | 5885 | static void xg_assemble_vliw_tokens (vliw_insn *); |
e0001a05 NC |
5886 | |
5887 | ||
43cd72b9 BW |
5888 | /* We have reached the end of a bundle; emit into the frag. */ |
5889 | ||
e0001a05 | 5890 | static void |
7fa3d080 | 5891 | finish_vinsn (vliw_insn *vinsn) |
e0001a05 | 5892 | { |
43cd72b9 BW |
5893 | IStack slotstack; |
5894 | int i; | |
5895 | char *file_name; | |
d77b99c9 | 5896 | unsigned line; |
e0001a05 | 5897 | |
43cd72b9 | 5898 | if (find_vinsn_conflicts (vinsn)) |
a1ace8d8 BW |
5899 | { |
5900 | xg_clear_vinsn (vinsn); | |
5901 | return; | |
5902 | } | |
43cd72b9 BW |
5903 | |
5904 | /* First, find a format that works. */ | |
5905 | if (vinsn->format == XTENSA_UNDEFINED) | |
5906 | vinsn->format = xg_find_narrowest_format (vinsn); | |
5907 | ||
5908 | if (vinsn->format == XTENSA_UNDEFINED) | |
5909 | { | |
5910 | as_where (&file_name, &line); | |
5911 | as_bad_where (file_name, line, | |
5912 | _("couldn't find a valid instruction format")); | |
5913 | fprintf (stderr, _(" ops were: ")); | |
5914 | for (i = 0; i < vinsn->num_slots; i++) | |
5915 | fprintf (stderr, _(" %s;"), | |
5916 | xtensa_opcode_name (xtensa_default_isa, | |
5917 | vinsn->slots[i].opcode)); | |
5918 | fprintf (stderr, _("\n")); | |
5919 | xg_clear_vinsn (vinsn); | |
5920 | return; | |
5921 | } | |
5922 | ||
5923 | if (vinsn->num_slots | |
5924 | != xtensa_format_num_slots (xtensa_default_isa, vinsn->format)) | |
e0001a05 | 5925 | { |
43cd72b9 BW |
5926 | as_bad (_("format '%s' allows %d slots, but there are %d opcodes"), |
5927 | xtensa_format_name (xtensa_default_isa, vinsn->format), | |
5928 | xtensa_format_num_slots (xtensa_default_isa, vinsn->format), | |
5929 | vinsn->num_slots); | |
5930 | xg_clear_vinsn (vinsn); | |
5931 | return; | |
5932 | } | |
e0001a05 | 5933 | |
c138bc38 | 5934 | if (resources_conflict (vinsn)) |
43cd72b9 BW |
5935 | { |
5936 | as_where (&file_name, &line); | |
5937 | as_bad_where (file_name, line, _("illegal resource usage in bundle")); | |
5938 | fprintf (stderr, " ops were: "); | |
5939 | for (i = 0; i < vinsn->num_slots; i++) | |
5940 | fprintf (stderr, " %s;", | |
5941 | xtensa_opcode_name (xtensa_default_isa, | |
5942 | vinsn->slots[i].opcode)); | |
5943 | fprintf (stderr, "\n"); | |
5944 | xg_clear_vinsn (vinsn); | |
5945 | return; | |
5946 | } | |
5947 | ||
5948 | for (i = 0; i < vinsn->num_slots; i++) | |
5949 | { | |
5950 | if (vinsn->slots[i].opcode != XTENSA_UNDEFINED) | |
e0001a05 | 5951 | { |
43cd72b9 BW |
5952 | symbolS *lit_sym = NULL; |
5953 | int j; | |
5954 | bfd_boolean e = FALSE; | |
5955 | bfd_boolean saved_density = density_supported; | |
5956 | ||
5957 | /* We don't want to narrow ops inside multi-slot bundles. */ | |
5958 | if (vinsn->num_slots > 1) | |
5959 | density_supported = FALSE; | |
5960 | ||
5961 | istack_init (&slotstack); | |
5962 | if (vinsn->slots[i].opcode == xtensa_nop_opcode) | |
e0001a05 | 5963 | { |
43cd72b9 BW |
5964 | vinsn->slots[i].opcode = |
5965 | xtensa_format_slot_nop_opcode (xtensa_default_isa, | |
5966 | vinsn->format, i); | |
5967 | vinsn->slots[i].ntok = 0; | |
5968 | } | |
e0001a05 | 5969 | |
43cd72b9 BW |
5970 | if (xg_expand_assembly_insn (&slotstack, &vinsn->slots[i])) |
5971 | { | |
5972 | e = TRUE; | |
5973 | continue; | |
e0001a05 | 5974 | } |
e0001a05 | 5975 | |
43cd72b9 | 5976 | density_supported = saved_density; |
e0001a05 | 5977 | |
43cd72b9 BW |
5978 | if (e) |
5979 | { | |
5980 | xg_clear_vinsn (vinsn); | |
5981 | return; | |
5982 | } | |
e0001a05 | 5983 | |
0fa77c95 | 5984 | for (j = 0; j < slotstack.ninsn; j++) |
43cd72b9 BW |
5985 | { |
5986 | TInsn *insn = &slotstack.insn[j]; | |
5987 | if (insn->insn_type == ITYPE_LITERAL) | |
5988 | { | |
5989 | assert (lit_sym == NULL); | |
5990 | lit_sym = xg_assemble_literal (insn); | |
5991 | } | |
5992 | else | |
5993 | { | |
0fa77c95 | 5994 | assert (insn->insn_type == ITYPE_INSN); |
43cd72b9 BW |
5995 | if (lit_sym) |
5996 | xg_resolve_literals (insn, lit_sym); | |
0fa77c95 BW |
5997 | if (j != slotstack.ninsn - 1) |
5998 | emit_single_op (insn); | |
43cd72b9 BW |
5999 | } |
6000 | } | |
6001 | ||
6002 | if (vinsn->num_slots > 1) | |
6003 | { | |
6004 | if (opcode_fits_format_slot | |
6005 | (slotstack.insn[slotstack.ninsn - 1].opcode, | |
6006 | vinsn->format, i)) | |
6007 | { | |
6008 | vinsn->slots[i] = slotstack.insn[slotstack.ninsn - 1]; | |
6009 | } | |
6010 | else | |
6011 | { | |
b2d179be | 6012 | emit_single_op (&slotstack.insn[slotstack.ninsn - 1]); |
43cd72b9 BW |
6013 | if (vinsn->format == XTENSA_UNDEFINED) |
6014 | vinsn->slots[i].opcode = xtensa_nop_opcode; | |
6015 | else | |
c138bc38 | 6016 | vinsn->slots[i].opcode |
43cd72b9 BW |
6017 | = xtensa_format_slot_nop_opcode (xtensa_default_isa, |
6018 | vinsn->format, i); | |
6019 | ||
6020 | vinsn->slots[i].ntok = 0; | |
6021 | } | |
6022 | } | |
6023 | else | |
6024 | { | |
6025 | vinsn->slots[0] = slotstack.insn[slotstack.ninsn - 1]; | |
6026 | vinsn->format = XTENSA_UNDEFINED; | |
6027 | } | |
6028 | } | |
6029 | } | |
6030 | ||
6031 | /* Now check resource conflicts on the modified bundle. */ | |
c138bc38 | 6032 | if (resources_conflict (vinsn)) |
43cd72b9 BW |
6033 | { |
6034 | as_where (&file_name, &line); | |
6035 | as_bad_where (file_name, line, _("illegal resource usage in bundle")); | |
6036 | fprintf (stderr, " ops were: "); | |
6037 | for (i = 0; i < vinsn->num_slots; i++) | |
6038 | fprintf (stderr, " %s;", | |
6039 | xtensa_opcode_name (xtensa_default_isa, | |
6040 | vinsn->slots[i].opcode)); | |
6041 | fprintf (stderr, "\n"); | |
6042 | xg_clear_vinsn (vinsn); | |
6043 | return; | |
6044 | } | |
6045 | ||
6046 | /* First, find a format that works. */ | |
6047 | if (vinsn->format == XTENSA_UNDEFINED) | |
6048 | vinsn->format = xg_find_narrowest_format (vinsn); | |
6049 | ||
6050 | xg_assemble_vliw_tokens (vinsn); | |
6051 | ||
6052 | xg_clear_vinsn (vinsn); | |
6053 | } | |
6054 | ||
6055 | ||
6056 | /* Given an vliw instruction, what conflicts are there in register | |
6057 | usage and in writes to states and queues? | |
6058 | ||
6059 | This function does two things: | |
6060 | 1. Reports an error when a vinsn contains illegal combinations | |
6061 | of writes to registers states or queues. | |
6062 | 2. Marks individual tinsns as not relaxable if the combination | |
6063 | contains antidependencies. | |
6064 | ||
6065 | Job 2 handles things like swap semantics in instructions that need | |
6066 | to be relaxed. For example, | |
6067 | ||
6068 | addi a0, a1, 100000 | |
6069 | ||
6070 | normally would be relaxed to | |
6071 | ||
6072 | l32r a0, some_label | |
6073 | add a0, a1, a0 | |
6074 | ||
6075 | _but_, if the above instruction is bundled with an a0 reader, e.g., | |
6076 | ||
6077 | { addi a0, a1, 10000 ; add a2, a0, a4 ; } | |
6078 | ||
6079 | then we can't relax it into | |
6080 | ||
6081 | l32r a0, some_label | |
6082 | { add a0, a1, a0 ; add a2, a0, a4 ; } | |
6083 | ||
6084 | because the value of a0 is trashed before the second add can read it. */ | |
6085 | ||
7fa3d080 BW |
6086 | static char check_t1_t2_reads_and_writes (TInsn *, TInsn *); |
6087 | ||
43cd72b9 | 6088 | static bfd_boolean |
7fa3d080 | 6089 | find_vinsn_conflicts (vliw_insn *vinsn) |
43cd72b9 BW |
6090 | { |
6091 | int i, j; | |
6092 | int branches = 0; | |
6093 | xtensa_isa isa = xtensa_default_isa; | |
6094 | ||
6095 | assert (!past_xtensa_end); | |
6096 | ||
6097 | for (i = 0 ; i < vinsn->num_slots; i++) | |
6098 | { | |
6099 | TInsn *op1 = &vinsn->slots[i]; | |
6100 | if (op1->is_specific_opcode) | |
6101 | op1->keep_wide = TRUE; | |
6102 | else | |
6103 | op1->keep_wide = FALSE; | |
6104 | } | |
6105 | ||
6106 | for (i = 0 ; i < vinsn->num_slots; i++) | |
6107 | { | |
6108 | TInsn *op1 = &vinsn->slots[i]; | |
6109 | ||
6110 | if (xtensa_opcode_is_branch (isa, op1->opcode) == 1) | |
6111 | branches++; | |
6112 | ||
6113 | for (j = 0; j < vinsn->num_slots; j++) | |
6114 | { | |
6115 | if (i != j) | |
6116 | { | |
6117 | TInsn *op2 = &vinsn->slots[j]; | |
6118 | char conflict_type = check_t1_t2_reads_and_writes (op1, op2); | |
6119 | switch (conflict_type) | |
6120 | { | |
6121 | case 'c': | |
6122 | as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same register"), | |
6123 | xtensa_opcode_name (isa, op1->opcode), i, | |
6124 | xtensa_opcode_name (isa, op2->opcode), j); | |
6125 | return TRUE; | |
6126 | case 'd': | |
6127 | as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same state"), | |
6128 | xtensa_opcode_name (isa, op1->opcode), i, | |
6129 | xtensa_opcode_name (isa, op2->opcode), j); | |
6130 | return TRUE; | |
6131 | case 'e': | |
53dfbcc7 | 6132 | as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same port"), |
43cd72b9 BW |
6133 | xtensa_opcode_name (isa, op1->opcode), i, |
6134 | xtensa_opcode_name (isa, op2->opcode), j); | |
6135 | return TRUE; | |
6136 | case 'f': | |
53dfbcc7 | 6137 | as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) both have volatile port accesses"), |
43cd72b9 BW |
6138 | xtensa_opcode_name (isa, op1->opcode), i, |
6139 | xtensa_opcode_name (isa, op2->opcode), j); | |
6140 | return TRUE; | |
6141 | default: | |
6142 | /* Everything is OK. */ | |
6143 | break; | |
6144 | } | |
6145 | op2->is_specific_opcode = (op2->is_specific_opcode | |
6146 | || conflict_type == 'a'); | |
6147 | } | |
6148 | } | |
6149 | } | |
6150 | ||
6151 | if (branches > 1) | |
6152 | { | |
6153 | as_bad (_("multiple branches or jumps in the same bundle")); | |
6154 | return TRUE; | |
6155 | } | |
6156 | ||
6157 | return FALSE; | |
6158 | } | |
6159 | ||
6160 | ||
a1ace8d8 | 6161 | /* Check how the state used by t1 and t2 relate. |
43cd72b9 BW |
6162 | Cases found are: |
6163 | ||
6164 | case A: t1 reads a register t2 writes (an antidependency within a bundle) | |
6165 | case B: no relationship between what is read and written (both could | |
6166 | read the same reg though) | |
c138bc38 | 6167 | case C: t1 writes a register t2 writes (a register conflict within a |
43cd72b9 BW |
6168 | bundle) |
6169 | case D: t1 writes a state that t2 also writes | |
6170 | case E: t1 writes a tie queue that t2 also writes | |
a1ace8d8 | 6171 | case F: two volatile queue accesses |
43cd72b9 BW |
6172 | */ |
6173 | ||
6174 | static char | |
7fa3d080 | 6175 | check_t1_t2_reads_and_writes (TInsn *t1, TInsn *t2) |
43cd72b9 BW |
6176 | { |
6177 | xtensa_isa isa = xtensa_default_isa; | |
6178 | xtensa_regfile t1_regfile, t2_regfile; | |
6179 | int t1_reg, t2_reg; | |
6180 | int t1_base_reg, t1_last_reg; | |
6181 | int t2_base_reg, t2_last_reg; | |
6182 | char t1_inout, t2_inout; | |
6183 | int i, j; | |
6184 | char conflict = 'b'; | |
6185 | int t1_states; | |
6186 | int t2_states; | |
6187 | int t1_interfaces; | |
6188 | int t2_interfaces; | |
6189 | bfd_boolean t1_volatile = FALSE; | |
6190 | bfd_boolean t2_volatile = FALSE; | |
6191 | ||
6192 | /* Check registers. */ | |
6193 | for (j = 0; j < t2->ntok; j++) | |
6194 | { | |
6195 | if (xtensa_operand_is_register (isa, t2->opcode, j) != 1) | |
6196 | continue; | |
6197 | ||
6198 | t2_regfile = xtensa_operand_regfile (isa, t2->opcode, j); | |
6199 | t2_base_reg = t2->tok[j].X_add_number; | |
6200 | t2_last_reg = t2_base_reg + xtensa_operand_num_regs (isa, t2->opcode, j); | |
6201 | ||
6202 | for (i = 0; i < t1->ntok; i++) | |
6203 | { | |
6204 | if (xtensa_operand_is_register (isa, t1->opcode, i) != 1) | |
6205 | continue; | |
6206 | ||
6207 | t1_regfile = xtensa_operand_regfile (isa, t1->opcode, i); | |
6208 | ||
6209 | if (t1_regfile != t2_regfile) | |
6210 | continue; | |
6211 | ||
6212 | t1_inout = xtensa_operand_inout (isa, t1->opcode, i); | |
6213 | t2_inout = xtensa_operand_inout (isa, t2->opcode, j); | |
6214 | ||
6215 | if (xtensa_operand_is_known_reg (isa, t1->opcode, i) == 0 | |
6216 | || xtensa_operand_is_known_reg (isa, t2->opcode, j) == 0) | |
6217 | { | |
6218 | if (t1_inout == 'm' || t1_inout == 'o' | |
6219 | || t2_inout == 'm' || t2_inout == 'o') | |
6220 | { | |
6221 | conflict = 'a'; | |
6222 | continue; | |
6223 | } | |
6224 | } | |
6225 | ||
6226 | t1_base_reg = t1->tok[i].X_add_number; | |
6227 | t1_last_reg = (t1_base_reg | |
6228 | + xtensa_operand_num_regs (isa, t1->opcode, i)); | |
6229 | ||
6230 | for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++) | |
6231 | { | |
6232 | for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++) | |
6233 | { | |
6234 | if (t1_reg != t2_reg) | |
6235 | continue; | |
6236 | ||
6237 | if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o')) | |
7fa3d080 BW |
6238 | { |
6239 | conflict = 'a'; | |
6240 | continue; | |
6241 | } | |
43cd72b9 | 6242 | |
7fa3d080 BW |
6243 | if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o')) |
6244 | { | |
6245 | conflict = 'a'; | |
6246 | continue; | |
6247 | } | |
43cd72b9 | 6248 | |
7fa3d080 BW |
6249 | if (t1_inout != 'i' && t2_inout != 'i') |
6250 | return 'c'; | |
6251 | } | |
6252 | } | |
6253 | } | |
6254 | } | |
43cd72b9 | 6255 | |
7fa3d080 BW |
6256 | /* Check states. */ |
6257 | t1_states = xtensa_opcode_num_stateOperands (isa, t1->opcode); | |
6258 | t2_states = xtensa_opcode_num_stateOperands (isa, t2->opcode); | |
6259 | for (j = 0; j < t2_states; j++) | |
43cd72b9 | 6260 | { |
7fa3d080 BW |
6261 | xtensa_state t2_so = xtensa_stateOperand_state (isa, t2->opcode, j); |
6262 | t2_inout = xtensa_stateOperand_inout (isa, t2->opcode, j); | |
6263 | for (i = 0; i < t1_states; i++) | |
6264 | { | |
6265 | xtensa_state t1_so = xtensa_stateOperand_state (isa, t1->opcode, i); | |
6266 | t1_inout = xtensa_stateOperand_inout (isa, t1->opcode, i); | |
c138bc38 | 6267 | if (t1_so != t2_so) |
7fa3d080 | 6268 | continue; |
43cd72b9 | 6269 | |
7fa3d080 BW |
6270 | if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o')) |
6271 | { | |
6272 | conflict = 'a'; | |
6273 | continue; | |
6274 | } | |
c138bc38 | 6275 | |
7fa3d080 BW |
6276 | if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o')) |
6277 | { | |
6278 | conflict = 'a'; | |
6279 | continue; | |
6280 | } | |
c138bc38 | 6281 | |
7fa3d080 BW |
6282 | if (t1_inout != 'i' && t2_inout != 'i') |
6283 | return 'd'; | |
c138bc38 | 6284 | } |
7fa3d080 | 6285 | } |
43cd72b9 | 6286 | |
7fa3d080 BW |
6287 | /* Check tieports. */ |
6288 | t1_interfaces = xtensa_opcode_num_interfaceOperands (isa, t1->opcode); | |
6289 | t2_interfaces = xtensa_opcode_num_interfaceOperands (isa, t2->opcode); | |
c138bc38 | 6290 | for (j = 0; j < t2_interfaces; j++) |
43cd72b9 | 6291 | { |
7fa3d080 BW |
6292 | xtensa_interface t2_int |
6293 | = xtensa_interfaceOperand_interface (isa, t2->opcode, j); | |
a1ace8d8 BW |
6294 | int t2_class = xtensa_interface_class_id (isa, t2_int); |
6295 | ||
53dfbcc7 | 6296 | t2_inout = xtensa_interface_inout (isa, t2_int); |
a1ace8d8 | 6297 | if (xtensa_interface_has_side_effect (isa, t2_int) == 1) |
7fa3d080 | 6298 | t2_volatile = TRUE; |
a1ace8d8 | 6299 | |
7fa3d080 BW |
6300 | for (i = 0; i < t1_interfaces; i++) |
6301 | { | |
6302 | xtensa_interface t1_int | |
6303 | = xtensa_interfaceOperand_interface (isa, t1->opcode, j); | |
2eccd1b4 | 6304 | int t1_class = xtensa_interface_class_id (isa, t1_int); |
a1ace8d8 | 6305 | |
53dfbcc7 | 6306 | t1_inout = xtensa_interface_inout (isa, t1_int); |
a1ace8d8 | 6307 | if (xtensa_interface_has_side_effect (isa, t1_int) == 1) |
7fa3d080 | 6308 | t1_volatile = TRUE; |
a1ace8d8 BW |
6309 | |
6310 | if (t1_volatile && t2_volatile && (t1_class == t2_class)) | |
6311 | return 'f'; | |
c138bc38 | 6312 | |
7fa3d080 BW |
6313 | if (t1_int != t2_int) |
6314 | continue; | |
c138bc38 | 6315 | |
7fa3d080 BW |
6316 | if (t2_inout == 'i' && t1_inout == 'o') |
6317 | { | |
6318 | conflict = 'a'; | |
6319 | continue; | |
6320 | } | |
c138bc38 | 6321 | |
7fa3d080 BW |
6322 | if (t1_inout == 'i' && t2_inout == 'o') |
6323 | { | |
6324 | conflict = 'a'; | |
6325 | continue; | |
6326 | } | |
c138bc38 | 6327 | |
7fa3d080 BW |
6328 | if (t1_inout != 'i' && t2_inout != 'i') |
6329 | return 'e'; | |
6330 | } | |
43cd72b9 | 6331 | } |
c138bc38 | 6332 | |
7fa3d080 | 6333 | return conflict; |
43cd72b9 BW |
6334 | } |
6335 | ||
6336 | ||
6337 | static xtensa_format | |
7fa3d080 | 6338 | xg_find_narrowest_format (vliw_insn *vinsn) |
43cd72b9 BW |
6339 | { |
6340 | /* Right now we assume that the ops within the vinsn are properly | |
6341 | ordered for the slots that the programmer wanted them in. In | |
6342 | other words, we don't rearrange the ops in hopes of finding a | |
6343 | better format. The scheduler handles that. */ | |
6344 | ||
6345 | xtensa_isa isa = xtensa_default_isa; | |
6346 | xtensa_format format; | |
6347 | vliw_insn v_copy = *vinsn; | |
6348 | xtensa_opcode nop_opcode = xtensa_nop_opcode; | |
6349 | ||
65738a7d BW |
6350 | if (vinsn->num_slots == 1) |
6351 | return xg_get_single_format (vinsn->slots[0].opcode); | |
6352 | ||
43cd72b9 BW |
6353 | for (format = 0; format < xtensa_isa_num_formats (isa); format++) |
6354 | { | |
6355 | v_copy = *vinsn; | |
6356 | if (xtensa_format_num_slots (isa, format) == v_copy.num_slots) | |
6357 | { | |
6358 | int slot; | |
6359 | int fit = 0; | |
6360 | for (slot = 0; slot < v_copy.num_slots; slot++) | |
6361 | { | |
6362 | if (v_copy.slots[slot].opcode == nop_opcode) | |
6363 | { | |
6364 | v_copy.slots[slot].opcode = | |
6365 | xtensa_format_slot_nop_opcode (isa, format, slot); | |
6366 | v_copy.slots[slot].ntok = 0; | |
6367 | } | |
6368 | ||
6369 | if (opcode_fits_format_slot (v_copy.slots[slot].opcode, | |
6370 | format, slot)) | |
6371 | fit++; | |
7fa3d080 | 6372 | else if (v_copy.num_slots > 1) |
43cd72b9 | 6373 | { |
7fa3d080 BW |
6374 | TInsn widened; |
6375 | /* Try the widened version. */ | |
6376 | if (!v_copy.slots[slot].keep_wide | |
6377 | && !v_copy.slots[slot].is_specific_opcode | |
84b08ed9 BW |
6378 | && xg_is_single_relaxable_insn (&v_copy.slots[slot], |
6379 | &widened, TRUE) | |
7fa3d080 BW |
6380 | && opcode_fits_format_slot (widened.opcode, |
6381 | format, slot)) | |
43cd72b9 | 6382 | { |
7fa3d080 BW |
6383 | v_copy.slots[slot] = widened; |
6384 | fit++; | |
43cd72b9 BW |
6385 | } |
6386 | } | |
6387 | } | |
6388 | if (fit == v_copy.num_slots) | |
6389 | { | |
6390 | *vinsn = v_copy; | |
6391 | xtensa_format_encode (isa, format, vinsn->insnbuf); | |
6392 | vinsn->format = format; | |
6393 | break; | |
6394 | } | |
6395 | } | |
6396 | } | |
6397 | ||
6398 | if (format == xtensa_isa_num_formats (isa)) | |
6399 | return XTENSA_UNDEFINED; | |
6400 | ||
6401 | return format; | |
6402 | } | |
6403 | ||
6404 | ||
6405 | /* Return the additional space needed in a frag | |
6406 | for possible relaxations of any ops in a VLIW insn. | |
6407 | Also fill out the relaxations that might be required of | |
6408 | each tinsn in the vinsn. */ | |
6409 | ||
6410 | static int | |
e7da6241 | 6411 | relaxation_requirements (vliw_insn *vinsn, bfd_boolean *pfinish_frag) |
43cd72b9 | 6412 | { |
e7da6241 | 6413 | bfd_boolean finish_frag = FALSE; |
43cd72b9 BW |
6414 | int extra_space = 0; |
6415 | int slot; | |
6416 | ||
6417 | for (slot = 0; slot < vinsn->num_slots; slot++) | |
6418 | { | |
6419 | TInsn *tinsn = &vinsn->slots[slot]; | |
6420 | if (!tinsn_has_symbolic_operands (tinsn)) | |
6421 | { | |
6422 | /* A narrow instruction could be widened later to help | |
6423 | alignment issues. */ | |
84b08ed9 | 6424 | if (xg_is_single_relaxable_insn (tinsn, 0, TRUE) |
43cd72b9 BW |
6425 | && !tinsn->is_specific_opcode |
6426 | && vinsn->num_slots == 1) | |
6427 | { | |
6428 | /* Difference in bytes between narrow and wide insns... */ | |
6429 | extra_space += 1; | |
6430 | tinsn->subtype = RELAX_NARROW; | |
43cd72b9 BW |
6431 | } |
6432 | } | |
6433 | else | |
6434 | { | |
b08b5071 BW |
6435 | if (workaround_b_j_loop_end |
6436 | && tinsn->opcode == xtensa_jx_opcode | |
43cd72b9 BW |
6437 | && use_transform ()) |
6438 | { | |
6439 | /* Add 2 of these. */ | |
6440 | extra_space += 3; /* for the nop size */ | |
6441 | tinsn->subtype = RELAX_ADD_NOP_IF_PRE_LOOP_END; | |
6442 | } | |
c138bc38 | 6443 | |
43cd72b9 BW |
6444 | /* Need to assemble it with space for the relocation. */ |
6445 | if (xg_is_relaxable_insn (tinsn, 0) | |
6446 | && !tinsn->is_specific_opcode) | |
6447 | { | |
6448 | int max_size = xg_get_max_insn_widen_size (tinsn->opcode); | |
6449 | int max_literal_size = | |
6450 | xg_get_max_insn_widen_literal_size (tinsn->opcode); | |
c138bc38 | 6451 | |
43cd72b9 | 6452 | tinsn->literal_space = max_literal_size; |
c138bc38 | 6453 | |
43cd72b9 | 6454 | tinsn->subtype = RELAX_IMMED; |
43cd72b9 BW |
6455 | extra_space += max_size; |
6456 | } | |
6457 | else | |
6458 | { | |
e7da6241 BW |
6459 | /* A fix record will be added for this instruction prior |
6460 | to relaxation, so make it end the frag. */ | |
6461 | finish_frag = TRUE; | |
43cd72b9 BW |
6462 | } |
6463 | } | |
6464 | } | |
e7da6241 | 6465 | *pfinish_frag = finish_frag; |
43cd72b9 BW |
6466 | return extra_space; |
6467 | } | |
6468 | ||
6469 | ||
6470 | static void | |
b2d179be | 6471 | bundle_tinsn (TInsn *tinsn, vliw_insn *vinsn) |
43cd72b9 BW |
6472 | { |
6473 | xtensa_isa isa = xtensa_default_isa; | |
b2d179be | 6474 | int slot, chosen_slot; |
43cd72b9 | 6475 | |
b2d179be BW |
6476 | vinsn->format = xg_get_single_format (tinsn->opcode); |
6477 | assert (vinsn->format != XTENSA_UNDEFINED); | |
6478 | vinsn->num_slots = xtensa_format_num_slots (isa, vinsn->format); | |
43cd72b9 | 6479 | |
b2d179be BW |
6480 | chosen_slot = xg_get_single_slot (tinsn->opcode); |
6481 | for (slot = 0; slot < vinsn->num_slots; slot++) | |
43cd72b9 | 6482 | { |
b2d179be BW |
6483 | if (slot == chosen_slot) |
6484 | vinsn->slots[slot] = *tinsn; | |
6485 | else | |
6486 | { | |
6487 | vinsn->slots[slot].opcode = | |
6488 | xtensa_format_slot_nop_opcode (isa, vinsn->format, slot); | |
6489 | vinsn->slots[slot].ntok = 0; | |
6490 | vinsn->slots[slot].insn_type = ITYPE_INSN; | |
6491 | } | |
43cd72b9 | 6492 | } |
43cd72b9 BW |
6493 | } |
6494 | ||
6495 | ||
6496 | static bfd_boolean | |
7fa3d080 | 6497 | emit_single_op (TInsn *orig_insn) |
43cd72b9 BW |
6498 | { |
6499 | int i; | |
6500 | IStack istack; /* put instructions into here */ | |
6501 | symbolS *lit_sym = NULL; | |
6502 | symbolS *label_sym = NULL; | |
6503 | ||
6504 | istack_init (&istack); | |
6505 | ||
6506 | /* Special-case for "movi aX, foo" which is guaranteed to need relaxing. | |
c138bc38 BW |
6507 | Because the scheduling and bundling characteristics of movi and |
6508 | l32r or const16 are so different, we can do much better if we relax | |
43cd72b9 | 6509 | it prior to scheduling and bundling, rather than after. */ |
c138bc38 | 6510 | if ((orig_insn->opcode == xtensa_movi_opcode |
b08b5071 BW |
6511 | || orig_insn->opcode == xtensa_movi_n_opcode) |
6512 | && !cur_vinsn.inside_bundle | |
43cd72b9 | 6513 | && (orig_insn->tok[1].X_op == O_symbol |
482fd9f9 BW |
6514 | || orig_insn->tok[1].X_op == O_pltrel) |
6515 | && !orig_insn->is_specific_opcode && use_transform ()) | |
43cd72b9 BW |
6516 | xg_assembly_relax (&istack, orig_insn, now_seg, frag_now, 0, 1, 0); |
6517 | else | |
6518 | if (xg_expand_assembly_insn (&istack, orig_insn)) | |
6519 | return TRUE; | |
6520 | ||
6521 | for (i = 0; i < istack.ninsn; i++) | |
6522 | { | |
6523 | TInsn *insn = &istack.insn[i]; | |
c138bc38 | 6524 | switch (insn->insn_type) |
43cd72b9 BW |
6525 | { |
6526 | case ITYPE_LITERAL: | |
6527 | assert (lit_sym == NULL); | |
6528 | lit_sym = xg_assemble_literal (insn); | |
6529 | break; | |
6530 | case ITYPE_LABEL: | |
6531 | { | |
6532 | static int relaxed_sym_idx = 0; | |
6533 | char *label = xmalloc (strlen (FAKE_LABEL_NAME) + 12); | |
6534 | sprintf (label, "%s_rl_%x", FAKE_LABEL_NAME, relaxed_sym_idx++); | |
6535 | colon (label); | |
6536 | assert (label_sym == NULL); | |
6537 | label_sym = symbol_find_or_make (label); | |
6538 | assert (label_sym); | |
6539 | free (label); | |
6540 | } | |
6541 | break; | |
6542 | case ITYPE_INSN: | |
b2d179be BW |
6543 | { |
6544 | vliw_insn v; | |
6545 | if (lit_sym) | |
6546 | xg_resolve_literals (insn, lit_sym); | |
6547 | if (label_sym) | |
6548 | xg_resolve_labels (insn, label_sym); | |
6549 | xg_init_vinsn (&v); | |
6550 | bundle_tinsn (insn, &v); | |
6551 | finish_vinsn (&v); | |
6552 | xg_free_vinsn (&v); | |
6553 | } | |
43cd72b9 BW |
6554 | break; |
6555 | default: | |
6556 | assert (0); | |
6557 | break; | |
6558 | } | |
6559 | } | |
6560 | return FALSE; | |
6561 | } | |
6562 | ||
6563 | ||
34e41783 BW |
6564 | static int |
6565 | total_frag_text_expansion (fragS *fragP) | |
6566 | { | |
6567 | int slot; | |
6568 | int total_expansion = 0; | |
6569 | ||
6570 | for (slot = 0; slot < MAX_SLOTS; slot++) | |
6571 | total_expansion += fragP->tc_frag_data.text_expansion[slot]; | |
6572 | ||
6573 | return total_expansion; | |
6574 | } | |
6575 | ||
6576 | ||
43cd72b9 BW |
6577 | /* Emit a vliw instruction to the current fragment. */ |
6578 | ||
7fa3d080 BW |
6579 | static void |
6580 | xg_assemble_vliw_tokens (vliw_insn *vinsn) | |
43cd72b9 | 6581 | { |
e7da6241 | 6582 | bfd_boolean finish_frag; |
43cd72b9 BW |
6583 | bfd_boolean is_jump = FALSE; |
6584 | bfd_boolean is_branch = FALSE; | |
6585 | xtensa_isa isa = xtensa_default_isa; | |
6586 | int i; | |
6587 | int insn_size; | |
6588 | int extra_space; | |
6589 | char *f = NULL; | |
6590 | int slot; | |
7c430684 BW |
6591 | unsigned current_line, best_linenum; |
6592 | char *current_file; | |
43cd72b9 | 6593 | |
7c430684 | 6594 | best_linenum = UINT_MAX; |
43cd72b9 BW |
6595 | |
6596 | if (generating_literals) | |
6597 | { | |
6598 | static int reported = 0; | |
6599 | if (reported < 4) | |
6600 | as_bad_where (frag_now->fr_file, frag_now->fr_line, | |
6601 | _("cannot assemble into a literal fragment")); | |
6602 | if (reported == 3) | |
6603 | as_bad (_("...")); | |
6604 | reported++; | |
6605 | return; | |
6606 | } | |
6607 | ||
6608 | if (frag_now_fix () != 0 | |
b08b5071 | 6609 | && (! frag_now->tc_frag_data.is_insn |
43cd72b9 | 6610 | || (vinsn_has_specific_opcodes (vinsn) && use_transform ()) |
b08b5071 | 6611 | || !use_transform () != frag_now->tc_frag_data.is_no_transform |
7c834684 BW |
6612 | || (directive_state[directive_longcalls] |
6613 | != frag_now->tc_frag_data.use_longcalls) | |
43cd72b9 BW |
6614 | || (directive_state[directive_absolute_literals] |
6615 | != frag_now->tc_frag_data.use_absolute_literals))) | |
6616 | { | |
6617 | frag_wane (frag_now); | |
6618 | frag_new (0); | |
6619 | xtensa_set_frag_assembly_state (frag_now); | |
6620 | } | |
6621 | ||
6622 | if (workaround_a0_b_retw | |
6623 | && vinsn->num_slots == 1 | |
6624 | && (get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0 | |
6625 | && xtensa_opcode_is_branch (isa, vinsn->slots[0].opcode) == 1 | |
6626 | && use_transform ()) | |
6627 | { | |
6628 | has_a0_b_retw = TRUE; | |
6629 | ||
6630 | /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW. | |
6631 | After the first assembly pass we will check all of them and | |
6632 | add a nop if needed. */ | |
6633 | frag_now->tc_frag_data.is_insn = TRUE; | |
6634 | frag_var (rs_machine_dependent, 4, 4, | |
6635 | RELAX_ADD_NOP_IF_A0_B_RETW, | |
6636 | frag_now->fr_symbol, | |
6637 | frag_now->fr_offset, | |
6638 | NULL); | |
6639 | xtensa_set_frag_assembly_state (frag_now); | |
6640 | frag_now->tc_frag_data.is_insn = TRUE; | |
6641 | frag_var (rs_machine_dependent, 4, 4, | |
6642 | RELAX_ADD_NOP_IF_A0_B_RETW, | |
6643 | frag_now->fr_symbol, | |
6644 | frag_now->fr_offset, | |
6645 | NULL); | |
6646 | xtensa_set_frag_assembly_state (frag_now); | |
6647 | } | |
6648 | ||
6649 | for (i = 0; i < vinsn->num_slots; i++) | |
6650 | { | |
6651 | /* See if the instruction implies an aligned section. */ | |
6652 | if (xtensa_opcode_is_loop (isa, vinsn->slots[i].opcode) == 1) | |
6653 | record_alignment (now_seg, 2); | |
c138bc38 | 6654 | |
43cd72b9 | 6655 | /* Also determine the best line number for debug info. */ |
7c430684 BW |
6656 | best_linenum = vinsn->slots[i].linenum < best_linenum |
6657 | ? vinsn->slots[i].linenum : best_linenum; | |
43cd72b9 BW |
6658 | } |
6659 | ||
6660 | /* Special cases for instructions that force an alignment... */ | |
6661 | /* None of these opcodes are bundle-able. */ | |
6662 | if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1) | |
6663 | { | |
d77b99c9 | 6664 | int max_fill; |
c138bc38 | 6665 | |
05d58145 BW |
6666 | /* Remember the symbol that marks the end of the loop in the frag |
6667 | that marks the start of the loop. This way we can easily find | |
6668 | the end of the loop at the beginning, without adding special code | |
6669 | to mark the loop instructions themselves. */ | |
6670 | symbolS *target_sym = NULL; | |
6671 | if (vinsn->slots[0].tok[1].X_op == O_symbol) | |
6672 | target_sym = vinsn->slots[0].tok[1].X_add_symbol; | |
6673 | ||
43cd72b9 BW |
6674 | xtensa_set_frag_assembly_state (frag_now); |
6675 | frag_now->tc_frag_data.is_insn = TRUE; | |
c138bc38 | 6676 | |
43cd72b9 BW |
6677 | max_fill = get_text_align_max_fill_size |
6678 | (get_text_align_power (xtensa_fetch_width), | |
6679 | TRUE, frag_now->tc_frag_data.is_no_density); | |
6680 | ||
6681 | if (use_transform ()) | |
6682 | frag_var (rs_machine_dependent, max_fill, max_fill, | |
05d58145 | 6683 | RELAX_ALIGN_NEXT_OPCODE, target_sym, 0, NULL); |
43cd72b9 | 6684 | else |
c138bc38 | 6685 | frag_var (rs_machine_dependent, 0, 0, |
05d58145 | 6686 | RELAX_CHECK_ALIGN_NEXT_OPCODE, target_sym, 0, NULL); |
43cd72b9 | 6687 | xtensa_set_frag_assembly_state (frag_now); |
c138bc38 | 6688 | |
43cd72b9 BW |
6689 | xtensa_move_labels (frag_now, 0, FALSE); |
6690 | } | |
6691 | ||
b08b5071 | 6692 | if (vinsn->slots[0].opcode == xtensa_entry_opcode |
43cd72b9 BW |
6693 | && !vinsn->slots[0].is_specific_opcode) |
6694 | { | |
6695 | xtensa_mark_literal_pool_location (); | |
6696 | xtensa_move_labels (frag_now, 0, TRUE); | |
6697 | frag_var (rs_align_test, 1, 1, 0, NULL, 2, NULL); | |
6698 | } | |
6699 | ||
6700 | if (vinsn->num_slots == 1) | |
6701 | { | |
6702 | if (workaround_a0_b_retw && use_transform ()) | |
6703 | set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER, | |
6704 | is_register_writer (&vinsn->slots[0], "a", 0)); | |
6705 | ||
6706 | set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND, | |
6707 | is_bad_loopend_opcode (&vinsn->slots[0])); | |
6708 | } | |
6709 | else | |
6710 | set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND, FALSE); | |
6711 | ||
6712 | insn_size = xtensa_format_length (isa, vinsn->format); | |
6713 | ||
e7da6241 | 6714 | extra_space = relaxation_requirements (vinsn, &finish_frag); |
43cd72b9 BW |
6715 | |
6716 | /* vinsn_to_insnbuf will produce the error. */ | |
6717 | if (vinsn->format != XTENSA_UNDEFINED) | |
6718 | { | |
d77b99c9 | 6719 | f = frag_more (insn_size + extra_space); |
43cd72b9 BW |
6720 | xtensa_set_frag_assembly_state (frag_now); |
6721 | frag_now->tc_frag_data.is_insn = TRUE; | |
6722 | } | |
6723 | ||
e7da6241 | 6724 | vinsn_to_insnbuf (vinsn, f, frag_now, FALSE); |
43cd72b9 BW |
6725 | if (vinsn->format == XTENSA_UNDEFINED) |
6726 | return; | |
6727 | ||
d77b99c9 | 6728 | xtensa_insnbuf_to_chars (isa, vinsn->insnbuf, (unsigned char *) f, 0); |
c138bc38 | 6729 | |
7c430684 BW |
6730 | /* Temporarily set the logical line number to the one we want to appear |
6731 | in the debug information. */ | |
6732 | as_where (¤t_file, ¤t_line); | |
6733 | new_logical_line (current_file, best_linenum); | |
6734 | dwarf2_emit_insn (insn_size + extra_space); | |
6735 | new_logical_line (current_file, current_line); | |
43cd72b9 BW |
6736 | |
6737 | for (slot = 0; slot < vinsn->num_slots; slot++) | |
6738 | { | |
6739 | TInsn *tinsn = &vinsn->slots[slot]; | |
6740 | frag_now->tc_frag_data.slot_subtypes[slot] = tinsn->subtype; | |
7c834684 | 6741 | frag_now->tc_frag_data.slot_symbols[slot] = tinsn->symbol; |
7c834684 | 6742 | frag_now->tc_frag_data.slot_offsets[slot] = tinsn->offset; |
43cd72b9 BW |
6743 | frag_now->tc_frag_data.literal_frags[slot] = tinsn->literal_frag; |
6744 | if (tinsn->literal_space != 0) | |
6745 | xg_assemble_literal_space (tinsn->literal_space, slot); | |
6746 | ||
6747 | if (tinsn->subtype == RELAX_NARROW) | |
6748 | assert (vinsn->num_slots == 1); | |
6749 | if (xtensa_opcode_is_jump (isa, tinsn->opcode) == 1) | |
6750 | is_jump = TRUE; | |
6751 | if (xtensa_opcode_is_branch (isa, tinsn->opcode) == 1) | |
6752 | is_branch = TRUE; | |
6753 | ||
e7da6241 BW |
6754 | if (tinsn->subtype || tinsn->symbol || tinsn->offset |
6755 | || tinsn->literal_frag || is_jump || is_branch) | |
43cd72b9 BW |
6756 | finish_frag = TRUE; |
6757 | } | |
6758 | ||
6759 | if (vinsn_has_specific_opcodes (vinsn) && use_transform ()) | |
b08b5071 | 6760 | frag_now->tc_frag_data.is_specific_opcode = TRUE; |
43cd72b9 BW |
6761 | |
6762 | if (finish_frag) | |
6763 | { | |
6764 | frag_variant (rs_machine_dependent, | |
6765 | extra_space, extra_space, RELAX_SLOTS, | |
6766 | frag_now->fr_symbol, frag_now->fr_offset, f); | |
6767 | xtensa_set_frag_assembly_state (frag_now); | |
6768 | } | |
6769 | ||
6770 | /* Special cases for loops: | |
6771 | close_loop_end should be inserted AFTER short_loop. | |
6772 | Make sure that CLOSE loops are processed BEFORE short_loops | |
6773 | when converting them. */ | |
6774 | ||
6775 | /* "short_loop": Add a NOP if the loop is < 4 bytes. */ | |
64b607e6 | 6776 | if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1 |
43cd72b9 BW |
6777 | && !vinsn->slots[0].is_specific_opcode) |
6778 | { | |
6779 | if (workaround_short_loop && use_transform ()) | |
6780 | { | |
6781 | maybe_has_short_loop = TRUE; | |
6782 | frag_now->tc_frag_data.is_insn = TRUE; | |
6783 | frag_var (rs_machine_dependent, 4, 4, | |
6784 | RELAX_ADD_NOP_IF_SHORT_LOOP, | |
6785 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
6786 | frag_now->tc_frag_data.is_insn = TRUE; | |
6787 | frag_var (rs_machine_dependent, 4, 4, | |
6788 | RELAX_ADD_NOP_IF_SHORT_LOOP, | |
6789 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
6790 | } | |
6791 | ||
6792 | /* "close_loop_end": Add up to 12 bytes of NOPs to keep a | |
6793 | loop at least 12 bytes away from another loop's end. */ | |
6794 | if (workaround_close_loop_end && use_transform ()) | |
6795 | { | |
6796 | maybe_has_close_loop_end = TRUE; | |
6797 | frag_now->tc_frag_data.is_insn = TRUE; | |
6798 | frag_var (rs_machine_dependent, 12, 12, | |
6799 | RELAX_ADD_NOP_IF_CLOSE_LOOP_END, | |
6800 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
6801 | } | |
6802 | } | |
6803 | ||
6804 | if (use_transform ()) | |
6805 | { | |
6806 | if (is_jump) | |
6807 | { | |
6808 | assert (finish_frag); | |
6809 | frag_var (rs_machine_dependent, | |
6810 | UNREACHABLE_MAX_WIDTH, UNREACHABLE_MAX_WIDTH, | |
6811 | RELAX_UNREACHABLE, | |
6812 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
6813 | xtensa_set_frag_assembly_state (frag_now); | |
6814 | } | |
7b1cc377 | 6815 | else if (is_branch && do_align_targets ()) |
43cd72b9 BW |
6816 | { |
6817 | assert (finish_frag); | |
6818 | frag_var (rs_machine_dependent, | |
6819 | UNREACHABLE_MAX_WIDTH, UNREACHABLE_MAX_WIDTH, | |
6820 | RELAX_MAYBE_UNREACHABLE, | |
6821 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
6822 | xtensa_set_frag_assembly_state (frag_now); | |
6823 | frag_var (rs_machine_dependent, | |
6824 | 0, 0, | |
6825 | RELAX_MAYBE_DESIRE_ALIGN, | |
6826 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
6827 | xtensa_set_frag_assembly_state (frag_now); | |
6828 | } | |
6829 | } | |
6830 | ||
6831 | /* Now, if the original opcode was a call... */ | |
6832 | if (do_align_targets () | |
6833 | && xtensa_opcode_is_call (isa, vinsn->slots[0].opcode) == 1) | |
6834 | { | |
b08b5071 | 6835 | float freq = get_subseg_total_freq (now_seg, now_subseg); |
43cd72b9 BW |
6836 | frag_now->tc_frag_data.is_insn = TRUE; |
6837 | frag_var (rs_machine_dependent, 4, (int) freq, RELAX_DESIRE_ALIGN, | |
6838 | frag_now->fr_symbol, frag_now->fr_offset, NULL); | |
6839 | xtensa_set_frag_assembly_state (frag_now); | |
6840 | } | |
6841 | ||
6842 | if (vinsn_has_specific_opcodes (vinsn) && use_transform ()) | |
6843 | { | |
6844 | frag_wane (frag_now); | |
6845 | frag_new (0); | |
6846 | xtensa_set_frag_assembly_state (frag_now); | |
6847 | } | |
6848 | } | |
6849 | ||
6850 | \f | |
7fa3d080 BW |
6851 | /* xtensa_end and helper functions. */ |
6852 | ||
6853 | static void xtensa_cleanup_align_frags (void); | |
6854 | static void xtensa_fix_target_frags (void); | |
6855 | static void xtensa_mark_narrow_branches (void); | |
6856 | static void xtensa_mark_zcl_first_insns (void); | |
6857 | static void xtensa_fix_a0_b_retw_frags (void); | |
6858 | static void xtensa_fix_b_j_loop_end_frags (void); | |
6859 | static void xtensa_fix_close_loop_end_frags (void); | |
6860 | static void xtensa_fix_short_loop_frags (void); | |
6861 | static void xtensa_sanity_check (void); | |
6862 | ||
43cd72b9 | 6863 | void |
7fa3d080 | 6864 | xtensa_end (void) |
43cd72b9 BW |
6865 | { |
6866 | directive_balance (); | |
6867 | xtensa_flush_pending_output (); | |
6868 | ||
6869 | past_xtensa_end = TRUE; | |
6870 | ||
6871 | xtensa_move_literals (); | |
6872 | ||
6873 | xtensa_reorder_segments (); | |
6874 | xtensa_cleanup_align_frags (); | |
6875 | xtensa_fix_target_frags (); | |
6876 | if (workaround_a0_b_retw && has_a0_b_retw) | |
6877 | xtensa_fix_a0_b_retw_frags (); | |
6878 | if (workaround_b_j_loop_end) | |
6879 | xtensa_fix_b_j_loop_end_frags (); | |
6880 | ||
6881 | /* "close_loop_end" should be processed BEFORE "short_loop". */ | |
6882 | if (workaround_close_loop_end && maybe_has_close_loop_end) | |
6883 | xtensa_fix_close_loop_end_frags (); | |
6884 | ||
6885 | if (workaround_short_loop && maybe_has_short_loop) | |
6886 | xtensa_fix_short_loop_frags (); | |
03aaa593 BW |
6887 | if (align_targets) |
6888 | xtensa_mark_narrow_branches (); | |
43cd72b9 BW |
6889 | xtensa_mark_zcl_first_insns (); |
6890 | ||
6891 | xtensa_sanity_check (); | |
6892 | } | |
6893 | ||
6894 | ||
6895 | static void | |
7fa3d080 | 6896 | xtensa_cleanup_align_frags (void) |
43cd72b9 BW |
6897 | { |
6898 | frchainS *frchP; | |
c9049d30 | 6899 | asection *s; |
43cd72b9 | 6900 | |
c9049d30 AM |
6901 | for (s = stdoutput->sections; s; s = s->next) |
6902 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
6903 | { | |
6904 | fragS *fragP; | |
6905 | /* Walk over all of the fragments in a subsection. */ | |
6906 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
6907 | { | |
6908 | if ((fragP->fr_type == rs_align | |
6909 | || fragP->fr_type == rs_align_code | |
6910 | || (fragP->fr_type == rs_machine_dependent | |
6911 | && (fragP->fr_subtype == RELAX_DESIRE_ALIGN | |
6912 | || fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET))) | |
6913 | && fragP->fr_fix == 0) | |
6914 | { | |
6915 | fragS *next = fragP->fr_next; | |
6916 | ||
6917 | while (next | |
6918 | && next->fr_fix == 0 | |
6919 | && next->fr_type == rs_machine_dependent | |
6920 | && next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET) | |
6921 | { | |
6922 | frag_wane (next); | |
6923 | next = next->fr_next; | |
6924 | } | |
6925 | } | |
6926 | /* If we don't widen branch targets, then they | |
6927 | will be easier to align. */ | |
6928 | if (fragP->tc_frag_data.is_branch_target | |
6929 | && fragP->fr_opcode == fragP->fr_literal | |
6930 | && fragP->fr_type == rs_machine_dependent | |
6931 | && fragP->fr_subtype == RELAX_SLOTS | |
6932 | && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW) | |
6933 | frag_wane (fragP); | |
6934 | if (fragP->fr_type == rs_machine_dependent | |
6935 | && fragP->fr_subtype == RELAX_UNREACHABLE) | |
6936 | fragP->tc_frag_data.is_unreachable = TRUE; | |
6937 | } | |
6938 | } | |
43cd72b9 BW |
6939 | } |
6940 | ||
6941 | ||
6942 | /* Re-process all of the fragments looking to convert all of the | |
6943 | RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch | |
6944 | target in the next fragment, convert this to RELAX_DESIRE_ALIGN. | |
7b1cc377 | 6945 | Otherwise, convert to a .fill 0. */ |
7fa3d080 | 6946 | |
43cd72b9 | 6947 | static void |
7fa3d080 | 6948 | xtensa_fix_target_frags (void) |
e0001a05 NC |
6949 | { |
6950 | frchainS *frchP; | |
c9049d30 | 6951 | asection *s; |
e0001a05 NC |
6952 | |
6953 | /* When this routine is called, all of the subsections are still intact | |
6954 | so we walk over subsections instead of sections. */ | |
c9049d30 AM |
6955 | for (s = stdoutput->sections; s; s = s->next) |
6956 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
6957 | { | |
6958 | fragS *fragP; | |
e0001a05 | 6959 | |
c9049d30 AM |
6960 | /* Walk over all of the fragments in a subsection. */ |
6961 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
6962 | { | |
6963 | if (fragP->fr_type == rs_machine_dependent | |
6964 | && fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET) | |
6965 | { | |
6966 | if (next_frag_is_branch_target (fragP)) | |
6967 | fragP->fr_subtype = RELAX_DESIRE_ALIGN; | |
6968 | else | |
6969 | frag_wane (fragP); | |
6970 | } | |
6971 | } | |
6972 | } | |
e0001a05 NC |
6973 | } |
6974 | ||
6975 | ||
7fa3d080 BW |
6976 | static bfd_boolean is_narrow_branch_guaranteed_in_range (fragS *, TInsn *); |
6977 | ||
43cd72b9 | 6978 | static void |
7fa3d080 | 6979 | xtensa_mark_narrow_branches (void) |
43cd72b9 BW |
6980 | { |
6981 | frchainS *frchP; | |
c9049d30 | 6982 | asection *s; |
43cd72b9 | 6983 | |
c9049d30 AM |
6984 | for (s = stdoutput->sections; s; s = s->next) |
6985 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
6986 | { | |
6987 | fragS *fragP; | |
6988 | /* Walk over all of the fragments in a subsection. */ | |
6989 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
6990 | { | |
6991 | if (fragP->fr_type == rs_machine_dependent | |
6992 | && fragP->fr_subtype == RELAX_SLOTS | |
6993 | && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED) | |
6994 | { | |
6995 | vliw_insn vinsn; | |
6996 | ||
6997 | vinsn_from_chars (&vinsn, fragP->fr_opcode); | |
6998 | tinsn_immed_from_frag (&vinsn.slots[0], fragP, 0); | |
6999 | ||
7000 | if (vinsn.num_slots == 1 | |
7001 | && xtensa_opcode_is_branch (xtensa_default_isa, | |
64b607e6 | 7002 | vinsn.slots[0].opcode) == 1 |
c9049d30 AM |
7003 | && xg_get_single_size (vinsn.slots[0].opcode) == 2 |
7004 | && is_narrow_branch_guaranteed_in_range (fragP, | |
7005 | &vinsn.slots[0])) | |
7006 | { | |
7007 | fragP->fr_subtype = RELAX_SLOTS; | |
7008 | fragP->tc_frag_data.slot_subtypes[0] = RELAX_NARROW; | |
7009 | fragP->tc_frag_data.is_aligning_branch = 1; | |
7010 | } | |
7011 | } | |
7012 | } | |
7013 | } | |
43cd72b9 BW |
7014 | } |
7015 | ||
7016 | ||
7017 | /* A branch is typically widened only when its target is out of | |
7018 | range. However, we would like to widen them to align a subsequent | |
7019 | branch target when possible. | |
7020 | ||
7021 | Because the branch relaxation code is so convoluted, the optimal solution | |
7022 | (combining the two cases) is difficult to get right in all circumstances. | |
7023 | We therefore go with an "almost as good" solution, where we only | |
7024 | use for alignment narrow branches that definitely will not expand to a | |
7025 | jump and a branch. These functions find and mark these cases. */ | |
7026 | ||
a67517f4 BW |
7027 | /* The range in bytes of BNEZ.N and BEQZ.N. The target operand is encoded |
7028 | as PC + 4 + imm6, where imm6 is a 6-bit immediate ranging from 0 to 63. | |
7029 | We start counting beginning with the frag after the 2-byte branch, so the | |
7030 | maximum offset is (4 - 2) + 63 = 65. */ | |
7031 | #define MAX_IMMED6 65 | |
43cd72b9 | 7032 | |
d77b99c9 | 7033 | static offsetT unrelaxed_frag_max_size (fragS *); |
7fa3d080 | 7034 | |
43cd72b9 | 7035 | static bfd_boolean |
7fa3d080 | 7036 | is_narrow_branch_guaranteed_in_range (fragS *fragP, TInsn *tinsn) |
43cd72b9 BW |
7037 | { |
7038 | const expressionS *expr = &tinsn->tok[1]; | |
7039 | symbolS *symbolP = expr->X_add_symbol; | |
d77b99c9 | 7040 | offsetT max_distance = expr->X_add_number; |
e7da6241 BW |
7041 | fragS *target_frag; |
7042 | ||
7043 | if (expr->X_op != O_symbol) | |
7044 | return FALSE; | |
7045 | ||
7046 | target_frag = symbol_get_frag (symbolP); | |
7047 | ||
43cd72b9 BW |
7048 | max_distance += (S_GET_VALUE (symbolP) - target_frag->fr_address); |
7049 | if (is_branch_jmp_to_next (tinsn, fragP)) | |
7050 | return FALSE; | |
7051 | ||
7052 | /* The branch doesn't branch over it's own frag, | |
7053 | but over the subsequent ones. */ | |
7054 | fragP = fragP->fr_next; | |
7055 | while (fragP != NULL && fragP != target_frag && max_distance <= MAX_IMMED6) | |
7056 | { | |
7057 | max_distance += unrelaxed_frag_max_size (fragP); | |
7058 | fragP = fragP->fr_next; | |
7059 | } | |
7060 | if (max_distance <= MAX_IMMED6 && fragP == target_frag) | |
7061 | return TRUE; | |
e0001a05 NC |
7062 | return FALSE; |
7063 | } | |
7064 | ||
7065 | ||
43cd72b9 | 7066 | static void |
7fa3d080 | 7067 | xtensa_mark_zcl_first_insns (void) |
43cd72b9 BW |
7068 | { |
7069 | frchainS *frchP; | |
c9049d30 | 7070 | asection *s; |
43cd72b9 | 7071 | |
c9049d30 AM |
7072 | for (s = stdoutput->sections; s; s = s->next) |
7073 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
7074 | { | |
7075 | fragS *fragP; | |
7076 | /* Walk over all of the fragments in a subsection. */ | |
7077 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
7078 | { | |
7079 | if (fragP->fr_type == rs_machine_dependent | |
7080 | && (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE | |
7081 | || fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)) | |
7082 | { | |
7083 | /* Find the loop frag. */ | |
7084 | fragS *targ_frag = next_non_empty_frag (fragP); | |
7085 | /* Find the first insn frag. */ | |
7086 | targ_frag = next_non_empty_frag (targ_frag); | |
7087 | ||
7088 | /* Of course, sometimes (mostly for toy test cases) a | |
7089 | zero-cost loop instruction is the last in a section. */ | |
7090 | if (targ_frag) | |
7091 | { | |
7092 | targ_frag->tc_frag_data.is_first_loop_insn = TRUE; | |
7093 | /* Do not widen a frag that is the first instruction of a | |
7094 | zero-cost loop. It makes that loop harder to align. */ | |
7095 | if (targ_frag->fr_type == rs_machine_dependent | |
7096 | && targ_frag->fr_subtype == RELAX_SLOTS | |
7097 | && (targ_frag->tc_frag_data.slot_subtypes[0] | |
7098 | == RELAX_NARROW)) | |
7099 | { | |
7100 | if (targ_frag->tc_frag_data.is_aligning_branch) | |
7101 | targ_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED; | |
7102 | else | |
7103 | { | |
7104 | frag_wane (targ_frag); | |
7105 | targ_frag->tc_frag_data.slot_subtypes[0] = 0; | |
7106 | } | |
7107 | } | |
7108 | } | |
7109 | if (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE) | |
7110 | frag_wane (fragP); | |
7111 | } | |
7112 | } | |
7113 | } | |
43cd72b9 BW |
7114 | } |
7115 | ||
7116 | ||
e0001a05 NC |
7117 | /* Re-process all of the fragments looking to convert all of the |
7118 | RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a | |
7119 | conditional branch or a retw/retw.n, convert this frag to one that | |
7120 | will generate a NOP. In any case close it off with a .fill 0. */ | |
7121 | ||
7fa3d080 BW |
7122 | static bfd_boolean next_instrs_are_b_retw (fragS *); |
7123 | ||
e0001a05 | 7124 | static void |
7fa3d080 | 7125 | xtensa_fix_a0_b_retw_frags (void) |
e0001a05 NC |
7126 | { |
7127 | frchainS *frchP; | |
c9049d30 | 7128 | asection *s; |
e0001a05 NC |
7129 | |
7130 | /* When this routine is called, all of the subsections are still intact | |
7131 | so we walk over subsections instead of sections. */ | |
c9049d30 AM |
7132 | for (s = stdoutput->sections; s; s = s->next) |
7133 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
7134 | { | |
7135 | fragS *fragP; | |
e0001a05 | 7136 | |
c9049d30 AM |
7137 | /* Walk over all of the fragments in a subsection. */ |
7138 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
7139 | { | |
7140 | if (fragP->fr_type == rs_machine_dependent | |
7141 | && fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW) | |
7142 | { | |
7143 | if (next_instrs_are_b_retw (fragP)) | |
7144 | { | |
7145 | if (fragP->tc_frag_data.is_no_transform) | |
7146 | as_bad (_("instruction sequence (write a0, branch, retw) may trigger hardware errata")); | |
7147 | else | |
7148 | relax_frag_add_nop (fragP); | |
7149 | } | |
7150 | frag_wane (fragP); | |
7151 | } | |
7152 | } | |
7153 | } | |
e0001a05 NC |
7154 | } |
7155 | ||
7156 | ||
7fa3d080 BW |
7157 | static bfd_boolean |
7158 | next_instrs_are_b_retw (fragS *fragP) | |
e0001a05 NC |
7159 | { |
7160 | xtensa_opcode opcode; | |
43cd72b9 | 7161 | xtensa_format fmt; |
e0001a05 NC |
7162 | const fragS *next_fragP = next_non_empty_frag (fragP); |
7163 | static xtensa_insnbuf insnbuf = NULL; | |
43cd72b9 | 7164 | static xtensa_insnbuf slotbuf = NULL; |
e0001a05 NC |
7165 | xtensa_isa isa = xtensa_default_isa; |
7166 | int offset = 0; | |
43cd72b9 BW |
7167 | int slot; |
7168 | bfd_boolean branch_seen = FALSE; | |
e0001a05 NC |
7169 | |
7170 | if (!insnbuf) | |
43cd72b9 BW |
7171 | { |
7172 | insnbuf = xtensa_insnbuf_alloc (isa); | |
7173 | slotbuf = xtensa_insnbuf_alloc (isa); | |
7174 | } | |
e0001a05 NC |
7175 | |
7176 | if (next_fragP == NULL) | |
7177 | return FALSE; | |
7178 | ||
7179 | /* Check for the conditional branch. */ | |
d77b99c9 BW |
7180 | xtensa_insnbuf_from_chars |
7181 | (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0); | |
43cd72b9 BW |
7182 | fmt = xtensa_format_decode (isa, insnbuf); |
7183 | if (fmt == XTENSA_UNDEFINED) | |
7184 | return FALSE; | |
7185 | ||
7186 | for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++) | |
7187 | { | |
7188 | xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf); | |
7189 | opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf); | |
7190 | ||
7191 | branch_seen = (branch_seen | |
7192 | || xtensa_opcode_is_branch (isa, opcode) == 1); | |
7193 | } | |
e0001a05 | 7194 | |
43cd72b9 | 7195 | if (!branch_seen) |
e0001a05 NC |
7196 | return FALSE; |
7197 | ||
43cd72b9 | 7198 | offset += xtensa_format_length (isa, fmt); |
e0001a05 NC |
7199 | if (offset == next_fragP->fr_fix) |
7200 | { | |
7201 | next_fragP = next_non_empty_frag (next_fragP); | |
7202 | offset = 0; | |
7203 | } | |
43cd72b9 | 7204 | |
e0001a05 NC |
7205 | if (next_fragP == NULL) |
7206 | return FALSE; | |
7207 | ||
7208 | /* Check for the retw/retw.n. */ | |
d77b99c9 BW |
7209 | xtensa_insnbuf_from_chars |
7210 | (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0); | |
43cd72b9 BW |
7211 | fmt = xtensa_format_decode (isa, insnbuf); |
7212 | ||
7213 | /* Because RETW[.N] is not bundleable, a VLIW bundle here means that we | |
7214 | have no problems. */ | |
7215 | if (fmt == XTENSA_UNDEFINED | |
7216 | || xtensa_format_num_slots (isa, fmt) != 1) | |
7217 | return FALSE; | |
7218 | ||
7219 | xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf); | |
7220 | opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf); | |
e0001a05 | 7221 | |
b08b5071 | 7222 | if (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode) |
e0001a05 | 7223 | return TRUE; |
43cd72b9 | 7224 | |
e0001a05 NC |
7225 | return FALSE; |
7226 | } | |
7227 | ||
7228 | ||
7229 | /* Re-process all of the fragments looking to convert all of the | |
7230 | RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a | |
7231 | loop end label, convert this frag to one that will generate a NOP. | |
7232 | In any case close it off with a .fill 0. */ | |
7233 | ||
7fa3d080 BW |
7234 | static bfd_boolean next_instr_is_loop_end (fragS *); |
7235 | ||
e0001a05 | 7236 | static void |
7fa3d080 | 7237 | xtensa_fix_b_j_loop_end_frags (void) |
e0001a05 NC |
7238 | { |
7239 | frchainS *frchP; | |
c9049d30 | 7240 | asection *s; |
e0001a05 NC |
7241 | |
7242 | /* When this routine is called, all of the subsections are still intact | |
7243 | so we walk over subsections instead of sections. */ | |
c9049d30 AM |
7244 | for (s = stdoutput->sections; s; s = s->next) |
7245 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
7246 | { | |
7247 | fragS *fragP; | |
e0001a05 | 7248 | |
c9049d30 AM |
7249 | /* Walk over all of the fragments in a subsection. */ |
7250 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
7251 | { | |
7252 | if (fragP->fr_type == rs_machine_dependent | |
7253 | && fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END) | |
7254 | { | |
7255 | if (next_instr_is_loop_end (fragP)) | |
7256 | { | |
7257 | if (fragP->tc_frag_data.is_no_transform) | |
7258 | as_bad (_("branching or jumping to a loop end may trigger hardware errata")); | |
7259 | else | |
7260 | relax_frag_add_nop (fragP); | |
7261 | } | |
7262 | frag_wane (fragP); | |
7263 | } | |
7264 | } | |
7265 | } | |
e0001a05 NC |
7266 | } |
7267 | ||
7268 | ||
7fa3d080 BW |
7269 | static bfd_boolean |
7270 | next_instr_is_loop_end (fragS *fragP) | |
e0001a05 NC |
7271 | { |
7272 | const fragS *next_fragP; | |
7273 | ||
7274 | if (next_frag_is_loop_target (fragP)) | |
7275 | return FALSE; | |
7276 | ||
7277 | next_fragP = next_non_empty_frag (fragP); | |
7278 | if (next_fragP == NULL) | |
7279 | return FALSE; | |
7280 | ||
7281 | if (!next_frag_is_loop_target (next_fragP)) | |
7282 | return FALSE; | |
7283 | ||
7284 | /* If the size is >= 3 then there is more than one instruction here. | |
7285 | The hardware bug will not fire. */ | |
7286 | if (next_fragP->fr_fix > 3) | |
7287 | return FALSE; | |
7288 | ||
7289 | return TRUE; | |
7290 | } | |
7291 | ||
7292 | ||
7293 | /* Re-process all of the fragments looking to convert all of the | |
7294 | RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is | |
7295 | not MY loop's loop end within 12 bytes, add enough nops here to | |
7296 | make it at least 12 bytes away. In any case close it off with a | |
7297 | .fill 0. */ | |
7298 | ||
d77b99c9 | 7299 | static offsetT min_bytes_to_other_loop_end |
05d58145 | 7300 | (fragS *, fragS *, offsetT); |
7fa3d080 | 7301 | |
e0001a05 | 7302 | static void |
7fa3d080 | 7303 | xtensa_fix_close_loop_end_frags (void) |
e0001a05 NC |
7304 | { |
7305 | frchainS *frchP; | |
c9049d30 | 7306 | asection *s; |
e0001a05 NC |
7307 | |
7308 | /* When this routine is called, all of the subsections are still intact | |
7309 | so we walk over subsections instead of sections. */ | |
c9049d30 AM |
7310 | for (s = stdoutput->sections; s; s = s->next) |
7311 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
7312 | { | |
7313 | fragS *fragP; | |
e0001a05 | 7314 | |
c9049d30 | 7315 | fragS *current_target = NULL; |
e0001a05 | 7316 | |
c9049d30 AM |
7317 | /* Walk over all of the fragments in a subsection. */ |
7318 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
7319 | { | |
7320 | if (fragP->fr_type == rs_machine_dependent | |
7321 | && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE) | |
7322 | || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE))) | |
05d58145 | 7323 | current_target = symbol_get_frag (fragP->fr_symbol); |
e0001a05 | 7324 | |
c9049d30 AM |
7325 | if (current_target |
7326 | && fragP->fr_type == rs_machine_dependent | |
7327 | && fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END) | |
7328 | { | |
7329 | offsetT min_bytes; | |
7330 | int bytes_added = 0; | |
e0001a05 NC |
7331 | |
7332 | #define REQUIRED_LOOP_DIVIDING_BYTES 12 | |
c9049d30 AM |
7333 | /* Max out at 12. */ |
7334 | min_bytes = min_bytes_to_other_loop_end | |
7335 | (fragP->fr_next, current_target, REQUIRED_LOOP_DIVIDING_BYTES); | |
7336 | ||
7337 | if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES) | |
7338 | { | |
7339 | if (fragP->tc_frag_data.is_no_transform) | |
7340 | as_bad (_("loop end too close to another loop end may trigger hardware errata")); | |
7341 | else | |
7342 | { | |
7343 | while (min_bytes + bytes_added | |
7344 | < REQUIRED_LOOP_DIVIDING_BYTES) | |
7345 | { | |
7346 | int length = 3; | |
7347 | ||
7348 | if (fragP->fr_var < length) | |
7349 | as_fatal (_("fr_var %lu < length %d"), | |
7350 | (long) fragP->fr_var, length); | |
7351 | else | |
7352 | { | |
7353 | assemble_nop (length, | |
7354 | fragP->fr_literal + fragP->fr_fix); | |
7355 | fragP->fr_fix += length; | |
7356 | fragP->fr_var -= length; | |
7357 | } | |
7358 | bytes_added += length; | |
7359 | } | |
7360 | } | |
7361 | } | |
7362 | frag_wane (fragP); | |
7363 | } | |
7364 | assert (fragP->fr_type != rs_machine_dependent | |
7365 | || fragP->fr_subtype != RELAX_ADD_NOP_IF_CLOSE_LOOP_END); | |
7366 | } | |
7367 | } | |
e0001a05 NC |
7368 | } |
7369 | ||
7370 | ||
d77b99c9 | 7371 | static offsetT unrelaxed_frag_min_size (fragS *); |
7fa3d080 | 7372 | |
d77b99c9 | 7373 | static offsetT |
7fa3d080 BW |
7374 | min_bytes_to_other_loop_end (fragS *fragP, |
7375 | fragS *current_target, | |
d77b99c9 | 7376 | offsetT max_size) |
e0001a05 | 7377 | { |
d77b99c9 | 7378 | offsetT offset = 0; |
e0001a05 NC |
7379 | fragS *current_fragP; |
7380 | ||
7381 | for (current_fragP = fragP; | |
7382 | current_fragP; | |
7383 | current_fragP = current_fragP->fr_next) | |
7384 | { | |
7385 | if (current_fragP->tc_frag_data.is_loop_target | |
7386 | && current_fragP != current_target) | |
05d58145 | 7387 | return offset; |
e0001a05 NC |
7388 | |
7389 | offset += unrelaxed_frag_min_size (current_fragP); | |
7390 | ||
05d58145 | 7391 | if (offset >= max_size) |
e0001a05 NC |
7392 | return max_size; |
7393 | } | |
7394 | return max_size; | |
7395 | } | |
7396 | ||
7397 | ||
d77b99c9 | 7398 | static offsetT |
7fa3d080 | 7399 | unrelaxed_frag_min_size (fragS *fragP) |
e0001a05 | 7400 | { |
d77b99c9 | 7401 | offsetT size = fragP->fr_fix; |
e0001a05 | 7402 | |
d77b99c9 | 7403 | /* Add fill size. */ |
e0001a05 NC |
7404 | if (fragP->fr_type == rs_fill) |
7405 | size += fragP->fr_offset; | |
7406 | ||
7407 | return size; | |
7408 | } | |
7409 | ||
7410 | ||
d77b99c9 | 7411 | static offsetT |
7fa3d080 | 7412 | unrelaxed_frag_max_size (fragS *fragP) |
43cd72b9 | 7413 | { |
d77b99c9 | 7414 | offsetT size = fragP->fr_fix; |
43cd72b9 BW |
7415 | switch (fragP->fr_type) |
7416 | { | |
7417 | case 0: | |
c138bc38 | 7418 | /* Empty frags created by the obstack allocation scheme |
43cd72b9 BW |
7419 | end up with type 0. */ |
7420 | break; | |
7421 | case rs_fill: | |
7422 | case rs_org: | |
7423 | case rs_space: | |
7424 | size += fragP->fr_offset; | |
7425 | break; | |
7426 | case rs_align: | |
7427 | case rs_align_code: | |
7428 | case rs_align_test: | |
7429 | case rs_leb128: | |
7430 | case rs_cfa: | |
7431 | case rs_dwarf2dbg: | |
7432 | /* No further adjustments needed. */ | |
7433 | break; | |
7434 | case rs_machine_dependent: | |
7435 | if (fragP->fr_subtype != RELAX_DESIRE_ALIGN) | |
7436 | size += fragP->fr_var; | |
7437 | break; | |
7438 | default: | |
7439 | /* We had darn well better know how big it is. */ | |
7440 | assert (0); | |
7441 | break; | |
7442 | } | |
7443 | ||
7444 | return size; | |
7445 | } | |
7446 | ||
7447 | ||
e0001a05 NC |
7448 | /* Re-process all of the fragments looking to convert all |
7449 | of the RELAX_ADD_NOP_IF_SHORT_LOOP. If: | |
7450 | ||
7451 | A) | |
7452 | 1) the instruction size count to the loop end label | |
7453 | is too short (<= 2 instructions), | |
7454 | 2) loop has a jump or branch in it | |
7455 | ||
7456 | or B) | |
43cd72b9 | 7457 | 1) workaround_all_short_loops is TRUE |
e0001a05 NC |
7458 | 2) The generating loop was a 'loopgtz' or 'loopnez' |
7459 | 3) the instruction size count to the loop end label is too short | |
7460 | (<= 2 instructions) | |
7461 | then convert this frag (and maybe the next one) to generate a NOP. | |
7462 | In any case close it off with a .fill 0. */ | |
7463 | ||
d77b99c9 | 7464 | static int count_insns_to_loop_end (fragS *, bfd_boolean, int); |
7fa3d080 BW |
7465 | static bfd_boolean branch_before_loop_end (fragS *); |
7466 | ||
e0001a05 | 7467 | static void |
7fa3d080 | 7468 | xtensa_fix_short_loop_frags (void) |
e0001a05 NC |
7469 | { |
7470 | frchainS *frchP; | |
c9049d30 | 7471 | asection *s; |
e0001a05 NC |
7472 | |
7473 | /* When this routine is called, all of the subsections are still intact | |
7474 | so we walk over subsections instead of sections. */ | |
c9049d30 AM |
7475 | for (s = stdoutput->sections; s; s = s->next) |
7476 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
7477 | { | |
7478 | fragS *fragP; | |
7479 | fragS *current_target = NULL; | |
7480 | xtensa_opcode current_opcode = XTENSA_UNDEFINED; | |
e0001a05 | 7481 | |
c9049d30 AM |
7482 | /* Walk over all of the fragments in a subsection. */ |
7483 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
7484 | { | |
7485 | if (fragP->fr_type == rs_machine_dependent | |
7486 | && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE) | |
7487 | || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE))) | |
7488 | { | |
7489 | TInsn t_insn; | |
7490 | fragS *loop_frag = next_non_empty_frag (fragP); | |
7491 | tinsn_from_chars (&t_insn, loop_frag->fr_opcode, 0); | |
7492 | current_target = symbol_get_frag (fragP->fr_symbol); | |
7493 | current_opcode = t_insn.opcode; | |
7494 | assert (xtensa_opcode_is_loop (xtensa_default_isa, | |
64b607e6 | 7495 | current_opcode) == 1); |
c9049d30 | 7496 | } |
e0001a05 | 7497 | |
c9049d30 AM |
7498 | if (fragP->fr_type == rs_machine_dependent |
7499 | && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP) | |
7500 | { | |
7501 | if (count_insns_to_loop_end (fragP->fr_next, TRUE, 3) < 3 | |
7502 | && (branch_before_loop_end (fragP->fr_next) | |
7503 | || (workaround_all_short_loops | |
7504 | && current_opcode != XTENSA_UNDEFINED | |
7505 | && current_opcode != xtensa_loop_opcode))) | |
7506 | { | |
7507 | if (fragP->tc_frag_data.is_no_transform) | |
7508 | as_bad (_("loop containing less than three instructions may trigger hardware errata")); | |
7509 | else | |
7510 | relax_frag_add_nop (fragP); | |
7511 | } | |
7512 | frag_wane (fragP); | |
7513 | } | |
7514 | } | |
7515 | } | |
e0001a05 NC |
7516 | } |
7517 | ||
7518 | ||
d77b99c9 | 7519 | static int unrelaxed_frag_min_insn_count (fragS *); |
7fa3d080 | 7520 | |
d77b99c9 | 7521 | static int |
7fa3d080 BW |
7522 | count_insns_to_loop_end (fragS *base_fragP, |
7523 | bfd_boolean count_relax_add, | |
d77b99c9 | 7524 | int max_count) |
e0001a05 NC |
7525 | { |
7526 | fragS *fragP = NULL; | |
d77b99c9 | 7527 | int insn_count = 0; |
e0001a05 NC |
7528 | |
7529 | fragP = base_fragP; | |
7530 | ||
7531 | for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next) | |
7532 | { | |
7533 | insn_count += unrelaxed_frag_min_insn_count (fragP); | |
7534 | if (insn_count >= max_count) | |
7535 | return max_count; | |
7536 | ||
7537 | if (count_relax_add) | |
7538 | { | |
7539 | if (fragP->fr_type == rs_machine_dependent | |
7540 | && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP) | |
7541 | { | |
7542 | /* In order to add the appropriate number of | |
7543 | NOPs, we count an instruction for downstream | |
7544 | occurrences. */ | |
7545 | insn_count++; | |
7546 | if (insn_count >= max_count) | |
7547 | return max_count; | |
7548 | } | |
7549 | } | |
7550 | } | |
7551 | return insn_count; | |
7552 | } | |
7553 | ||
7554 | ||
d77b99c9 | 7555 | static int |
7fa3d080 | 7556 | unrelaxed_frag_min_insn_count (fragS *fragP) |
e0001a05 | 7557 | { |
43cd72b9 BW |
7558 | xtensa_isa isa = xtensa_default_isa; |
7559 | static xtensa_insnbuf insnbuf = NULL; | |
d77b99c9 | 7560 | int insn_count = 0; |
e0001a05 NC |
7561 | int offset = 0; |
7562 | ||
7563 | if (!fragP->tc_frag_data.is_insn) | |
7564 | return insn_count; | |
7565 | ||
43cd72b9 BW |
7566 | if (!insnbuf) |
7567 | insnbuf = xtensa_insnbuf_alloc (isa); | |
7568 | ||
e0001a05 NC |
7569 | /* Decode the fixed instructions. */ |
7570 | while (offset < fragP->fr_fix) | |
7571 | { | |
43cd72b9 BW |
7572 | xtensa_format fmt; |
7573 | ||
d77b99c9 BW |
7574 | xtensa_insnbuf_from_chars |
7575 | (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0); | |
43cd72b9 BW |
7576 | fmt = xtensa_format_decode (isa, insnbuf); |
7577 | ||
7578 | if (fmt == XTENSA_UNDEFINED) | |
e0001a05 NC |
7579 | { |
7580 | as_fatal (_("undecodable instruction in instruction frag")); | |
7581 | return insn_count; | |
7582 | } | |
43cd72b9 | 7583 | offset += xtensa_format_length (isa, fmt); |
e0001a05 NC |
7584 | insn_count++; |
7585 | } | |
7586 | ||
7587 | return insn_count; | |
7588 | } | |
7589 | ||
7590 | ||
7fa3d080 BW |
7591 | static bfd_boolean unrelaxed_frag_has_b_j (fragS *); |
7592 | ||
43cd72b9 | 7593 | static bfd_boolean |
7fa3d080 | 7594 | branch_before_loop_end (fragS *base_fragP) |
e0001a05 NC |
7595 | { |
7596 | fragS *fragP; | |
7597 | ||
7598 | for (fragP = base_fragP; | |
7599 | fragP && !fragP->tc_frag_data.is_loop_target; | |
7600 | fragP = fragP->fr_next) | |
7601 | { | |
7602 | if (unrelaxed_frag_has_b_j (fragP)) | |
7603 | return TRUE; | |
7604 | } | |
7605 | return FALSE; | |
7606 | } | |
7607 | ||
7608 | ||
43cd72b9 | 7609 | static bfd_boolean |
7fa3d080 | 7610 | unrelaxed_frag_has_b_j (fragS *fragP) |
e0001a05 | 7611 | { |
43cd72b9 BW |
7612 | static xtensa_insnbuf insnbuf = NULL; |
7613 | xtensa_isa isa = xtensa_default_isa; | |
e0001a05 NC |
7614 | int offset = 0; |
7615 | ||
7616 | if (!fragP->tc_frag_data.is_insn) | |
7617 | return FALSE; | |
7618 | ||
43cd72b9 BW |
7619 | if (!insnbuf) |
7620 | insnbuf = xtensa_insnbuf_alloc (isa); | |
7621 | ||
e0001a05 NC |
7622 | /* Decode the fixed instructions. */ |
7623 | while (offset < fragP->fr_fix) | |
7624 | { | |
43cd72b9 BW |
7625 | xtensa_format fmt; |
7626 | int slot; | |
7627 | ||
d77b99c9 BW |
7628 | xtensa_insnbuf_from_chars |
7629 | (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0); | |
43cd72b9 BW |
7630 | fmt = xtensa_format_decode (isa, insnbuf); |
7631 | if (fmt == XTENSA_UNDEFINED) | |
7632 | return FALSE; | |
7633 | ||
7634 | for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++) | |
e0001a05 | 7635 | { |
43cd72b9 BW |
7636 | xtensa_opcode opcode = |
7637 | get_opcode_from_buf (fragP->fr_literal + offset, slot); | |
7638 | if (xtensa_opcode_is_branch (isa, opcode) == 1 | |
7639 | || xtensa_opcode_is_jump (isa, opcode) == 1) | |
7640 | return TRUE; | |
e0001a05 | 7641 | } |
43cd72b9 | 7642 | offset += xtensa_format_length (isa, fmt); |
e0001a05 NC |
7643 | } |
7644 | return FALSE; | |
7645 | } | |
7646 | ||
7647 | ||
7648 | /* Checks to be made after initial assembly but before relaxation. */ | |
7649 | ||
7fa3d080 BW |
7650 | static bfd_boolean is_empty_loop (const TInsn *, fragS *); |
7651 | static bfd_boolean is_local_forward_loop (const TInsn *, fragS *); | |
7652 | ||
e0001a05 | 7653 | static void |
7fa3d080 | 7654 | xtensa_sanity_check (void) |
e0001a05 NC |
7655 | { |
7656 | char *file_name; | |
d77b99c9 | 7657 | unsigned line; |
e0001a05 | 7658 | frchainS *frchP; |
c9049d30 | 7659 | asection *s; |
e0001a05 NC |
7660 | |
7661 | as_where (&file_name, &line); | |
c9049d30 AM |
7662 | for (s = stdoutput->sections; s; s = s->next) |
7663 | for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next) | |
7664 | { | |
7665 | fragS *fragP; | |
e0001a05 | 7666 | |
c9049d30 AM |
7667 | /* Walk over all of the fragments in a subsection. */ |
7668 | for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next) | |
7669 | { | |
7670 | /* Currently we only check for empty loops here. */ | |
7671 | if (fragP->fr_type == rs_machine_dependent | |
7672 | && fragP->fr_subtype == RELAX_IMMED) | |
7673 | { | |
7674 | static xtensa_insnbuf insnbuf = NULL; | |
7675 | TInsn t_insn; | |
7676 | ||
7677 | if (fragP->fr_opcode != NULL) | |
7678 | { | |
7679 | if (!insnbuf) | |
7680 | insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa); | |
7681 | tinsn_from_chars (&t_insn, fragP->fr_opcode, 0); | |
7682 | tinsn_immed_from_frag (&t_insn, fragP, 0); | |
7683 | ||
7684 | if (xtensa_opcode_is_loop (xtensa_default_isa, | |
7685 | t_insn.opcode) == 1) | |
7686 | { | |
7687 | if (is_empty_loop (&t_insn, fragP)) | |
7688 | { | |
7689 | new_logical_line (fragP->fr_file, fragP->fr_line); | |
7690 | as_bad (_("invalid empty loop")); | |
7691 | } | |
7692 | if (!is_local_forward_loop (&t_insn, fragP)) | |
7693 | { | |
7694 | new_logical_line (fragP->fr_file, fragP->fr_line); | |
7695 | as_bad (_("loop target does not follow " | |
7696 | "loop instruction in section")); | |
7697 | } | |
7698 | } | |
7699 | } | |
7700 | } | |
7701 | } | |
7702 | } | |
e0001a05 NC |
7703 | new_logical_line (file_name, line); |
7704 | } | |
7705 | ||
7706 | ||
7707 | #define LOOP_IMMED_OPN 1 | |
7708 | ||
43cd72b9 | 7709 | /* Return TRUE if the loop target is the next non-zero fragment. */ |
e0001a05 | 7710 | |
7fa3d080 BW |
7711 | static bfd_boolean |
7712 | is_empty_loop (const TInsn *insn, fragS *fragP) | |
e0001a05 NC |
7713 | { |
7714 | const expressionS *expr; | |
7715 | symbolS *symbolP; | |
7716 | fragS *next_fragP; | |
7717 | ||
7718 | if (insn->insn_type != ITYPE_INSN) | |
7719 | return FALSE; | |
7720 | ||
43cd72b9 | 7721 | if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1) |
e0001a05 NC |
7722 | return FALSE; |
7723 | ||
7724 | if (insn->ntok <= LOOP_IMMED_OPN) | |
7725 | return FALSE; | |
7726 | ||
7727 | expr = &insn->tok[LOOP_IMMED_OPN]; | |
7728 | ||
7729 | if (expr->X_op != O_symbol) | |
7730 | return FALSE; | |
7731 | ||
7732 | symbolP = expr->X_add_symbol; | |
7733 | if (!symbolP) | |
7734 | return FALSE; | |
7735 | ||
7736 | if (symbol_get_frag (symbolP) == NULL) | |
7737 | return FALSE; | |
7738 | ||
7739 | if (S_GET_VALUE (symbolP) != 0) | |
7740 | return FALSE; | |
7741 | ||
7742 | /* Walk through the zero-size fragments from this one. If we find | |
7743 | the target fragment, then this is a zero-size loop. */ | |
43cd72b9 | 7744 | |
e0001a05 NC |
7745 | for (next_fragP = fragP->fr_next; |
7746 | next_fragP != NULL; | |
7747 | next_fragP = next_fragP->fr_next) | |
7748 | { | |
7749 | if (next_fragP == symbol_get_frag (symbolP)) | |
7750 | return TRUE; | |
7751 | if (next_fragP->fr_fix != 0) | |
7752 | return FALSE; | |
7753 | } | |
7754 | return FALSE; | |
7755 | } | |
7756 | ||
7757 | ||
7fa3d080 BW |
7758 | static bfd_boolean |
7759 | is_local_forward_loop (const TInsn *insn, fragS *fragP) | |
e0001a05 NC |
7760 | { |
7761 | const expressionS *expr; | |
7762 | symbolS *symbolP; | |
7763 | fragS *next_fragP; | |
7764 | ||
7765 | if (insn->insn_type != ITYPE_INSN) | |
7766 | return FALSE; | |
7767 | ||
64b607e6 | 7768 | if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1) |
e0001a05 NC |
7769 | return FALSE; |
7770 | ||
7771 | if (insn->ntok <= LOOP_IMMED_OPN) | |
7772 | return FALSE; | |
7773 | ||
7774 | expr = &insn->tok[LOOP_IMMED_OPN]; | |
7775 | ||
7776 | if (expr->X_op != O_symbol) | |
7777 | return FALSE; | |
7778 | ||
7779 | symbolP = expr->X_add_symbol; | |
7780 | if (!symbolP) | |
7781 | return FALSE; | |
7782 | ||
7783 | if (symbol_get_frag (symbolP) == NULL) | |
7784 | return FALSE; | |
7785 | ||
7786 | /* Walk through fragments until we find the target. | |
7787 | If we do not find the target, then this is an invalid loop. */ | |
43cd72b9 | 7788 | |
e0001a05 NC |
7789 | for (next_fragP = fragP->fr_next; |
7790 | next_fragP != NULL; | |
7791 | next_fragP = next_fragP->fr_next) | |
43cd72b9 BW |
7792 | { |
7793 | if (next_fragP == symbol_get_frag (symbolP)) | |
7794 | return TRUE; | |
7795 | } | |
e0001a05 NC |
7796 | |
7797 | return FALSE; | |
7798 | } | |
7799 | ||
7800 | \f | |
7801 | /* Alignment Functions. */ | |
7802 | ||
d77b99c9 BW |
7803 | static int |
7804 | get_text_align_power (unsigned target_size) | |
e0001a05 | 7805 | { |
03aaa593 BW |
7806 | if (target_size <= 4) |
7807 | return 2; | |
7808 | assert (target_size == 8); | |
7809 | return 3; | |
e0001a05 NC |
7810 | } |
7811 | ||
7812 | ||
d77b99c9 | 7813 | static int |
7fa3d080 BW |
7814 | get_text_align_max_fill_size (int align_pow, |
7815 | bfd_boolean use_nops, | |
7816 | bfd_boolean use_no_density) | |
e0001a05 NC |
7817 | { |
7818 | if (!use_nops) | |
7819 | return (1 << align_pow); | |
7820 | if (use_no_density) | |
7821 | return 3 * (1 << align_pow); | |
7822 | ||
7823 | return 1 + (1 << align_pow); | |
7824 | } | |
7825 | ||
7826 | ||
d77b99c9 BW |
7827 | /* Calculate the minimum bytes of fill needed at "address" to align a |
7828 | target instruction of size "target_size" so that it does not cross a | |
7829 | power-of-two boundary specified by "align_pow". If "use_nops" is FALSE, | |
7830 | the fill can be an arbitrary number of bytes. Otherwise, the space must | |
7831 | be filled by NOP instructions. */ | |
e0001a05 | 7832 | |
d77b99c9 | 7833 | static int |
7fa3d080 BW |
7834 | get_text_align_fill_size (addressT address, |
7835 | int align_pow, | |
7836 | int target_size, | |
7837 | bfd_boolean use_nops, | |
7838 | bfd_boolean use_no_density) | |
e0001a05 | 7839 | { |
d77b99c9 BW |
7840 | addressT alignment, fill, fill_limit, fill_step; |
7841 | bfd_boolean skip_one = FALSE; | |
e0001a05 | 7842 | |
d77b99c9 BW |
7843 | alignment = (1 << align_pow); |
7844 | assert (target_size > 0 && alignment >= (addressT) target_size); | |
c138bc38 | 7845 | |
e0001a05 NC |
7846 | if (!use_nops) |
7847 | { | |
d77b99c9 BW |
7848 | fill_limit = alignment; |
7849 | fill_step = 1; | |
e0001a05 | 7850 | } |
d77b99c9 | 7851 | else if (!use_no_density) |
e0001a05 | 7852 | { |
d77b99c9 BW |
7853 | /* Combine 2- and 3-byte NOPs to fill anything larger than one. */ |
7854 | fill_limit = alignment * 2; | |
7855 | fill_step = 1; | |
7856 | skip_one = TRUE; | |
e0001a05 NC |
7857 | } |
7858 | else | |
7859 | { | |
d77b99c9 BW |
7860 | /* Fill with 3-byte NOPs -- can only fill multiples of 3. */ |
7861 | fill_limit = alignment * 3; | |
7862 | fill_step = 3; | |
7863 | } | |
e0001a05 | 7864 | |
d77b99c9 BW |
7865 | /* Try all fill sizes until finding one that works. */ |
7866 | for (fill = 0; fill < fill_limit; fill += fill_step) | |
7867 | { | |
7868 | if (skip_one && fill == 1) | |
7869 | continue; | |
7870 | if ((address + fill) >> align_pow | |
7871 | == (address + fill + target_size - 1) >> align_pow) | |
7872 | return fill; | |
e0001a05 NC |
7873 | } |
7874 | assert (0); | |
7875 | return 0; | |
7876 | } | |
7877 | ||
7878 | ||
664df4e4 BW |
7879 | static int |
7880 | branch_align_power (segT sec) | |
7881 | { | |
7882 | /* If the Xtensa processor has a fetch width of 8 bytes, and the section | |
7883 | is aligned to at least an 8-byte boundary, then a branch target need | |
7884 | only fit within an 8-byte aligned block of memory to avoid a stall. | |
7885 | Otherwise, try to fit branch targets within 4-byte aligned blocks | |
7886 | (which may be insufficient, e.g., if the section has no alignment, but | |
7887 | it's good enough). */ | |
7888 | if (xtensa_fetch_width == 8) | |
7889 | { | |
7890 | if (get_recorded_alignment (sec) >= 3) | |
7891 | return 3; | |
7892 | } | |
7893 | else | |
7894 | assert (xtensa_fetch_width == 4); | |
7895 | ||
7896 | return 2; | |
7897 | } | |
7898 | ||
7899 | ||
e0001a05 NC |
7900 | /* This will assert if it is not possible. */ |
7901 | ||
d77b99c9 BW |
7902 | static int |
7903 | get_text_align_nop_count (offsetT fill_size, bfd_boolean use_no_density) | |
e0001a05 | 7904 | { |
d77b99c9 BW |
7905 | int count = 0; |
7906 | ||
e0001a05 NC |
7907 | if (use_no_density) |
7908 | { | |
7909 | assert (fill_size % 3 == 0); | |
7910 | return (fill_size / 3); | |
7911 | } | |
7912 | ||
7913 | assert (fill_size != 1); /* Bad argument. */ | |
7914 | ||
7915 | while (fill_size > 1) | |
7916 | { | |
d77b99c9 | 7917 | int insn_size = 3; |
e0001a05 NC |
7918 | if (fill_size == 2 || fill_size == 4) |
7919 | insn_size = 2; | |
7920 | fill_size -= insn_size; | |
7921 | count++; | |
7922 | } | |
7923 | assert (fill_size != 1); /* Bad algorithm. */ | |
7924 | return count; | |
7925 | } | |
7926 | ||
7927 | ||
d77b99c9 BW |
7928 | static int |
7929 | get_text_align_nth_nop_size (offsetT fill_size, | |
7930 | int n, | |
7fa3d080 | 7931 | bfd_boolean use_no_density) |
e0001a05 | 7932 | { |
d77b99c9 | 7933 | int count = 0; |
e0001a05 NC |
7934 | |
7935 | if (use_no_density) | |
7936 | return 3; | |
7937 | ||
d77b99c9 BW |
7938 | assert (fill_size != 1); /* Bad argument. */ |
7939 | ||
e0001a05 NC |
7940 | while (fill_size > 1) |
7941 | { | |
d77b99c9 | 7942 | int insn_size = 3; |
e0001a05 NC |
7943 | if (fill_size == 2 || fill_size == 4) |
7944 | insn_size = 2; | |
7945 | fill_size -= insn_size; | |
7946 | count++; | |
7947 | if (n + 1 == count) | |
7948 | return insn_size; | |
7949 | } | |
7950 | assert (0); | |
7951 | return 0; | |
7952 | } | |
7953 | ||
7954 | ||
7955 | /* For the given fragment, find the appropriate address | |
7956 | for it to begin at if we are using NOPs to align it. */ | |
7957 | ||
7958 | static addressT | |
7fa3d080 | 7959 | get_noop_aligned_address (fragS *fragP, addressT address) |
e0001a05 | 7960 | { |
43cd72b9 BW |
7961 | /* The rule is: get next fragment's FIRST instruction. Find |
7962 | the smallest number of bytes that need to be added to | |
7963 | ensure that the next fragment's FIRST instruction will fit | |
7964 | in a single word. | |
c138bc38 | 7965 | |
43cd72b9 BW |
7966 | E.G., 2 bytes : 0, 1, 2 mod 4 |
7967 | 3 bytes: 0, 1 mod 4 | |
c138bc38 | 7968 | |
43cd72b9 BW |
7969 | If the FIRST instruction MIGHT be relaxed, |
7970 | assume that it will become a 3-byte instruction. | |
c138bc38 | 7971 | |
43cd72b9 BW |
7972 | Note again here that LOOP instructions are not bundleable, |
7973 | and this relaxation only applies to LOOP opcodes. */ | |
c138bc38 | 7974 | |
d77b99c9 | 7975 | int fill_size = 0; |
43cd72b9 BW |
7976 | int first_insn_size; |
7977 | int loop_insn_size; | |
7978 | addressT pre_opcode_bytes; | |
d77b99c9 | 7979 | int align_power; |
43cd72b9 BW |
7980 | fragS *first_insn; |
7981 | xtensa_opcode opcode; | |
7982 | bfd_boolean is_loop; | |
e0001a05 | 7983 | |
43cd72b9 BW |
7984 | assert (fragP->fr_type == rs_machine_dependent); |
7985 | assert (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE); | |
e0001a05 | 7986 | |
43cd72b9 BW |
7987 | /* Find the loop frag. */ |
7988 | first_insn = next_non_empty_frag (fragP); | |
7989 | /* Now find the first insn frag. */ | |
7990 | first_insn = next_non_empty_frag (first_insn); | |
e0001a05 | 7991 | |
43cd72b9 BW |
7992 | is_loop = next_frag_opcode_is_loop (fragP, &opcode); |
7993 | assert (is_loop); | |
7994 | loop_insn_size = xg_get_single_size (opcode); | |
e0001a05 | 7995 | |
43cd72b9 BW |
7996 | pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP); |
7997 | pre_opcode_bytes += loop_insn_size; | |
e0001a05 | 7998 | |
43cd72b9 BW |
7999 | /* For loops, the alignment depends on the size of the |
8000 | instruction following the loop, not the LOOP instruction. */ | |
e0001a05 | 8001 | |
43cd72b9 | 8002 | if (first_insn == NULL) |
03aaa593 BW |
8003 | first_insn_size = xtensa_fetch_width; |
8004 | else | |
8005 | first_insn_size = get_loop_align_size (frag_format_size (first_insn)); | |
e0001a05 | 8006 | |
43cd72b9 | 8007 | /* If it was 8, then we'll need a larger alignment for the section. */ |
d77b99c9 BW |
8008 | align_power = get_text_align_power (first_insn_size); |
8009 | record_alignment (now_seg, align_power); | |
c138bc38 | 8010 | |
43cd72b9 | 8011 | fill_size = get_text_align_fill_size |
d77b99c9 BW |
8012 | (address + pre_opcode_bytes, align_power, first_insn_size, TRUE, |
8013 | fragP->tc_frag_data.is_no_density); | |
e0001a05 NC |
8014 | |
8015 | return address + fill_size; | |
8016 | } | |
8017 | ||
8018 | ||
43cd72b9 BW |
8019 | /* 3 mechanisms for relaxing an alignment: |
8020 | ||
8021 | Align to a power of 2. | |
8022 | Align so the next fragment's instruction does not cross a word boundary. | |
8023 | Align the current instruction so that if the next instruction | |
8024 | were 3 bytes, it would not cross a word boundary. | |
8025 | ||
e0001a05 NC |
8026 | We can align with: |
8027 | ||
43cd72b9 BW |
8028 | zeros - This is easy; always insert zeros. |
8029 | nops - 3-byte and 2-byte instructions | |
8030 | 2 - 2-byte nop | |
8031 | 3 - 3-byte nop | |
8032 | 4 - 2 2-byte nops | |
8033 | >=5 : 3-byte instruction + fn (n-3) | |
e0001a05 NC |
8034 | widening - widen previous instructions. */ |
8035 | ||
d77b99c9 BW |
8036 | static offsetT |
8037 | get_aligned_diff (fragS *fragP, addressT address, offsetT *max_diff) | |
e0001a05 | 8038 | { |
43cd72b9 BW |
8039 | addressT target_address, loop_insn_offset; |
8040 | int target_size; | |
8041 | xtensa_opcode loop_opcode; | |
8042 | bfd_boolean is_loop; | |
d77b99c9 BW |
8043 | int align_power; |
8044 | offsetT opt_diff; | |
5f9084e9 | 8045 | offsetT branch_align; |
e0001a05 | 8046 | |
43cd72b9 BW |
8047 | assert (fragP->fr_type == rs_machine_dependent); |
8048 | switch (fragP->fr_subtype) | |
e0001a05 | 8049 | { |
43cd72b9 BW |
8050 | case RELAX_DESIRE_ALIGN: |
8051 | target_size = next_frag_format_size (fragP); | |
8052 | if (target_size == XTENSA_UNDEFINED) | |
8053 | target_size = 3; | |
664df4e4 BW |
8054 | align_power = branch_align_power (now_seg); |
8055 | branch_align = 1 << align_power; | |
0e5cd789 BW |
8056 | /* Don't count on the section alignment being as large as the target. */ |
8057 | if (target_size > branch_align) | |
8058 | target_size = branch_align; | |
d77b99c9 | 8059 | opt_diff = get_text_align_fill_size (address, align_power, |
43cd72b9 BW |
8060 | target_size, FALSE, FALSE); |
8061 | ||
664df4e4 BW |
8062 | *max_diff = (opt_diff + branch_align |
8063 | - (target_size + ((address + opt_diff) % branch_align))); | |
43cd72b9 BW |
8064 | assert (*max_diff >= opt_diff); |
8065 | return opt_diff; | |
e0001a05 | 8066 | |
43cd72b9 | 8067 | case RELAX_ALIGN_NEXT_OPCODE: |
03aaa593 | 8068 | target_size = get_loop_align_size (next_frag_format_size (fragP)); |
43cd72b9 BW |
8069 | loop_insn_offset = 0; |
8070 | is_loop = next_frag_opcode_is_loop (fragP, &loop_opcode); | |
8071 | assert (is_loop); | |
8072 | ||
8073 | /* If the loop has been expanded then the LOOP instruction | |
8074 | could be at an offset from this fragment. */ | |
8075 | if (next_non_empty_frag(fragP)->tc_frag_data.slot_subtypes[0] | |
8076 | != RELAX_IMMED) | |
8077 | loop_insn_offset = get_expanded_loop_offset (loop_opcode); | |
8078 | ||
43cd72b9 BW |
8079 | /* In an ideal world, which is what we are shooting for here, |
8080 | we wouldn't need to use any NOPs immediately prior to the | |
8081 | LOOP instruction. If this approach fails, relax_frag_loop_align | |
8082 | will call get_noop_aligned_address. */ | |
8083 | target_address = | |
8084 | address + loop_insn_offset + xg_get_single_size (loop_opcode); | |
d77b99c9 BW |
8085 | align_power = get_text_align_power (target_size), |
8086 | opt_diff = get_text_align_fill_size (target_address, align_power, | |
43cd72b9 BW |
8087 | target_size, FALSE, FALSE); |
8088 | ||
8089 | *max_diff = xtensa_fetch_width | |
8090 | - ((target_address + opt_diff) % xtensa_fetch_width) | |
8091 | - target_size + opt_diff; | |
8092 | assert (*max_diff >= opt_diff); | |
8093 | return opt_diff; | |
e0001a05 | 8094 | |
43cd72b9 BW |
8095 | default: |
8096 | break; | |
e0001a05 | 8097 | } |
43cd72b9 BW |
8098 | assert (0); |
8099 | return 0; | |
e0001a05 NC |
8100 | } |
8101 | ||
8102 | \f | |
8103 | /* md_relax_frag Hook and Helper Functions. */ | |
8104 | ||
7fa3d080 BW |
8105 | static long relax_frag_loop_align (fragS *, long); |
8106 | static long relax_frag_for_align (fragS *, long); | |
8107 | static long relax_frag_immed | |
8108 | (segT, fragS *, long, int, xtensa_format, int, int *, bfd_boolean); | |
8109 | ||
8110 | ||
e0001a05 NC |
8111 | /* Return the number of bytes added to this fragment, given that the |
8112 | input has been stretched already by "stretch". */ | |
8113 | ||
8114 | long | |
7fa3d080 | 8115 | xtensa_relax_frag (fragS *fragP, long stretch, int *stretched_p) |
e0001a05 | 8116 | { |
43cd72b9 | 8117 | xtensa_isa isa = xtensa_default_isa; |
e0001a05 NC |
8118 | int unreported = fragP->tc_frag_data.unreported_expansion; |
8119 | long new_stretch = 0; | |
8120 | char *file_name; | |
d77b99c9 BW |
8121 | unsigned line; |
8122 | int lit_size; | |
43cd72b9 BW |
8123 | static xtensa_insnbuf vbuf = NULL; |
8124 | int slot, num_slots; | |
8125 | xtensa_format fmt; | |
e0001a05 NC |
8126 | |
8127 | as_where (&file_name, &line); | |
8128 | new_logical_line (fragP->fr_file, fragP->fr_line); | |
8129 | ||
8130 | fragP->tc_frag_data.unreported_expansion = 0; | |
8131 | ||
8132 | switch (fragP->fr_subtype) | |
8133 | { | |
8134 | case RELAX_ALIGN_NEXT_OPCODE: | |
8135 | /* Always convert. */ | |
43cd72b9 BW |
8136 | if (fragP->tc_frag_data.relax_seen) |
8137 | new_stretch = relax_frag_loop_align (fragP, stretch); | |
e0001a05 NC |
8138 | break; |
8139 | ||
8140 | case RELAX_LOOP_END: | |
8141 | /* Do nothing. */ | |
8142 | break; | |
8143 | ||
8144 | case RELAX_LOOP_END_ADD_NOP: | |
8145 | /* Add a NOP and switch to .fill 0. */ | |
8146 | new_stretch = relax_frag_add_nop (fragP); | |
43cd72b9 | 8147 | frag_wane (fragP); |
e0001a05 NC |
8148 | break; |
8149 | ||
8150 | case RELAX_DESIRE_ALIGN: | |
43cd72b9 | 8151 | /* Do nothing. The narrowing before this frag will either align |
e0001a05 NC |
8152 | it or not. */ |
8153 | break; | |
8154 | ||
8155 | case RELAX_LITERAL: | |
8156 | case RELAX_LITERAL_FINAL: | |
8157 | return 0; | |
8158 | ||
8159 | case RELAX_LITERAL_NR: | |
8160 | lit_size = 4; | |
8161 | fragP->fr_subtype = RELAX_LITERAL_FINAL; | |
8162 | assert (unreported == lit_size); | |
8163 | memset (&fragP->fr_literal[fragP->fr_fix], 0, 4); | |
8164 | fragP->fr_var -= lit_size; | |
8165 | fragP->fr_fix += lit_size; | |
8166 | new_stretch = 4; | |
8167 | break; | |
8168 | ||
43cd72b9 BW |
8169 | case RELAX_SLOTS: |
8170 | if (vbuf == NULL) | |
8171 | vbuf = xtensa_insnbuf_alloc (isa); | |
8172 | ||
d77b99c9 BW |
8173 | xtensa_insnbuf_from_chars |
8174 | (isa, vbuf, (unsigned char *) fragP->fr_opcode, 0); | |
43cd72b9 BW |
8175 | fmt = xtensa_format_decode (isa, vbuf); |
8176 | num_slots = xtensa_format_num_slots (isa, fmt); | |
e0001a05 | 8177 | |
43cd72b9 BW |
8178 | for (slot = 0; slot < num_slots; slot++) |
8179 | { | |
8180 | switch (fragP->tc_frag_data.slot_subtypes[slot]) | |
8181 | { | |
8182 | case RELAX_NARROW: | |
8183 | if (fragP->tc_frag_data.relax_seen) | |
8184 | new_stretch += relax_frag_for_align (fragP, stretch); | |
8185 | break; | |
8186 | ||
8187 | case RELAX_IMMED: | |
8188 | case RELAX_IMMED_STEP1: | |
8189 | case RELAX_IMMED_STEP2: | |
8190 | /* Place the immediate. */ | |
8191 | new_stretch += relax_frag_immed | |
8192 | (now_seg, fragP, stretch, | |
8193 | fragP->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED, | |
8194 | fmt, slot, stretched_p, FALSE); | |
8195 | break; | |
8196 | ||
8197 | default: | |
8198 | /* This is OK; see the note in xg_assemble_vliw_tokens. */ | |
8199 | break; | |
8200 | } | |
8201 | } | |
e0001a05 NC |
8202 | break; |
8203 | ||
8204 | case RELAX_LITERAL_POOL_BEGIN: | |
8205 | case RELAX_LITERAL_POOL_END: | |
43cd72b9 BW |
8206 | case RELAX_MAYBE_UNREACHABLE: |
8207 | case RELAX_MAYBE_DESIRE_ALIGN: | |
e0001a05 NC |
8208 | /* No relaxation required. */ |
8209 | break; | |
8210 | ||
43cd72b9 BW |
8211 | case RELAX_FILL_NOP: |
8212 | case RELAX_UNREACHABLE: | |
8213 | if (fragP->tc_frag_data.relax_seen) | |
8214 | new_stretch += relax_frag_for_align (fragP, stretch); | |
8215 | break; | |
8216 | ||
e0001a05 NC |
8217 | default: |
8218 | as_bad (_("bad relaxation state")); | |
8219 | } | |
8220 | ||
43cd72b9 | 8221 | /* Tell gas we need another relaxation pass. */ |
c138bc38 | 8222 | if (! fragP->tc_frag_data.relax_seen) |
43cd72b9 BW |
8223 | { |
8224 | fragP->tc_frag_data.relax_seen = TRUE; | |
8225 | *stretched_p = 1; | |
8226 | } | |
8227 | ||
e0001a05 NC |
8228 | new_logical_line (file_name, line); |
8229 | return new_stretch; | |
8230 | } | |
8231 | ||
8232 | ||
8233 | static long | |
7fa3d080 | 8234 | relax_frag_loop_align (fragS *fragP, long stretch) |
e0001a05 NC |
8235 | { |
8236 | addressT old_address, old_next_address, old_size; | |
8237 | addressT new_address, new_next_address, new_size; | |
8238 | addressT growth; | |
8239 | ||
43cd72b9 BW |
8240 | /* All the frags with relax_frag_for_alignment prior to this one in the |
8241 | section have been done, hopefully eliminating the need for a NOP here. | |
8242 | But, this will put it in if necessary. */ | |
e0001a05 NC |
8243 | |
8244 | /* Calculate the old address of this fragment and the next fragment. */ | |
8245 | old_address = fragP->fr_address - stretch; | |
8246 | old_next_address = (fragP->fr_address - stretch + fragP->fr_fix + | |
43cd72b9 | 8247 | fragP->tc_frag_data.text_expansion[0]); |
e0001a05 NC |
8248 | old_size = old_next_address - old_address; |
8249 | ||
8250 | /* Calculate the new address of this fragment and the next fragment. */ | |
8251 | new_address = fragP->fr_address; | |
8252 | new_next_address = | |
8253 | get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix); | |
8254 | new_size = new_next_address - new_address; | |
8255 | ||
8256 | growth = new_size - old_size; | |
8257 | ||
8258 | /* Fix up the text_expansion field and return the new growth. */ | |
43cd72b9 | 8259 | fragP->tc_frag_data.text_expansion[0] += growth; |
e0001a05 NC |
8260 | return growth; |
8261 | } | |
8262 | ||
8263 | ||
43cd72b9 | 8264 | /* Add a NOP instruction. */ |
e0001a05 NC |
8265 | |
8266 | static long | |
7fa3d080 | 8267 | relax_frag_add_nop (fragS *fragP) |
e0001a05 | 8268 | { |
e0001a05 | 8269 | char *nop_buf = fragP->fr_literal + fragP->fr_fix; |
43cd72b9 BW |
8270 | int length = fragP->tc_frag_data.is_no_density ? 3 : 2; |
8271 | assemble_nop (length, nop_buf); | |
e0001a05 | 8272 | fragP->tc_frag_data.is_insn = TRUE; |
e0001a05 | 8273 | |
e0001a05 NC |
8274 | if (fragP->fr_var < length) |
8275 | { | |
dd49a749 | 8276 | as_fatal (_("fr_var (%ld) < length (%d)"), (long) fragP->fr_var, length); |
e0001a05 NC |
8277 | return 0; |
8278 | } | |
8279 | ||
8280 | fragP->fr_fix += length; | |
8281 | fragP->fr_var -= length; | |
e0001a05 NC |
8282 | return length; |
8283 | } | |
8284 | ||
8285 | ||
7fa3d080 BW |
8286 | static long future_alignment_required (fragS *, long); |
8287 | ||
e0001a05 | 8288 | static long |
7fa3d080 | 8289 | relax_frag_for_align (fragS *fragP, long stretch) |
e0001a05 | 8290 | { |
43cd72b9 BW |
8291 | /* Overview of the relaxation procedure for alignment: |
8292 | We can widen with NOPs or by widening instructions or by filling | |
8293 | bytes after jump instructions. Find the opportune places and widen | |
8294 | them if necessary. */ | |
8295 | ||
8296 | long stretch_me; | |
8297 | long diff; | |
e0001a05 | 8298 | |
43cd72b9 BW |
8299 | assert (fragP->fr_subtype == RELAX_FILL_NOP |
8300 | || fragP->fr_subtype == RELAX_UNREACHABLE | |
8301 | || (fragP->fr_subtype == RELAX_SLOTS | |
8302 | && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)); | |
8303 | ||
8304 | stretch_me = future_alignment_required (fragP, stretch); | |
8305 | diff = stretch_me - fragP->tc_frag_data.text_expansion[0]; | |
8306 | if (diff == 0) | |
8307 | return 0; | |
e0001a05 | 8308 | |
43cd72b9 | 8309 | if (diff < 0) |
e0001a05 | 8310 | { |
43cd72b9 BW |
8311 | /* We expanded on a previous pass. Can we shrink now? */ |
8312 | long shrink = fragP->tc_frag_data.text_expansion[0] - stretch_me; | |
8313 | if (shrink <= stretch && stretch > 0) | |
e0001a05 | 8314 | { |
43cd72b9 BW |
8315 | fragP->tc_frag_data.text_expansion[0] = stretch_me; |
8316 | return -shrink; | |
e0001a05 NC |
8317 | } |
8318 | return 0; | |
8319 | } | |
8320 | ||
43cd72b9 BW |
8321 | /* Below here, diff > 0. */ |
8322 | fragP->tc_frag_data.text_expansion[0] = stretch_me; | |
e0001a05 | 8323 | |
43cd72b9 | 8324 | return diff; |
e0001a05 NC |
8325 | } |
8326 | ||
8327 | ||
43cd72b9 BW |
8328 | /* Return the address of the next frag that should be aligned. |
8329 | ||
8330 | By "address" we mean the address it _would_ be at if there | |
8331 | is no action taken to align it between here and the target frag. | |
8332 | In other words, if no narrows and no fill nops are used between | |
8333 | here and the frag to align, _even_if_ some of the frags we use | |
8334 | to align targets have already expanded on a previous relaxation | |
8335 | pass. | |
8336 | ||
8337 | Also, count each frag that may be used to help align the target. | |
8338 | ||
8339 | Return 0 if there are no frags left in the chain that need to be | |
8340 | aligned. */ | |
8341 | ||
8342 | static addressT | |
7fa3d080 BW |
8343 | find_address_of_next_align_frag (fragS **fragPP, |
8344 | int *wide_nops, | |
8345 | int *narrow_nops, | |
8346 | int *widens, | |
8347 | bfd_boolean *paddable) | |
e0001a05 | 8348 | { |
43cd72b9 BW |
8349 | fragS *fragP = *fragPP; |
8350 | addressT address = fragP->fr_address; | |
8351 | ||
8352 | /* Do not reset the counts to 0. */ | |
e0001a05 NC |
8353 | |
8354 | while (fragP) | |
8355 | { | |
8356 | /* Limit this to a small search. */ | |
b5e4a23d | 8357 | if (*widens >= (int) xtensa_fetch_width) |
43cd72b9 BW |
8358 | { |
8359 | *fragPP = fragP; | |
8360 | return 0; | |
8361 | } | |
e0001a05 NC |
8362 | address += fragP->fr_fix; |
8363 | ||
43cd72b9 BW |
8364 | if (fragP->fr_type == rs_fill) |
8365 | address += fragP->fr_offset * fragP->fr_var; | |
8366 | else if (fragP->fr_type == rs_machine_dependent) | |
e0001a05 | 8367 | { |
e0001a05 NC |
8368 | switch (fragP->fr_subtype) |
8369 | { | |
43cd72b9 BW |
8370 | case RELAX_UNREACHABLE: |
8371 | *paddable = TRUE; | |
8372 | break; | |
8373 | ||
8374 | case RELAX_FILL_NOP: | |
8375 | (*wide_nops)++; | |
8376 | if (!fragP->tc_frag_data.is_no_density) | |
8377 | (*narrow_nops)++; | |
8378 | break; | |
8379 | ||
8380 | case RELAX_SLOTS: | |
8381 | if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW) | |
8382 | { | |
8383 | (*widens)++; | |
8384 | break; | |
8385 | } | |
34e41783 | 8386 | address += total_frag_text_expansion (fragP);; |
e0001a05 NC |
8387 | break; |
8388 | ||
8389 | case RELAX_IMMED: | |
43cd72b9 | 8390 | address += fragP->tc_frag_data.text_expansion[0]; |
e0001a05 NC |
8391 | break; |
8392 | ||
8393 | case RELAX_ALIGN_NEXT_OPCODE: | |
8394 | case RELAX_DESIRE_ALIGN: | |
43cd72b9 BW |
8395 | *fragPP = fragP; |
8396 | return address; | |
8397 | ||
8398 | case RELAX_MAYBE_UNREACHABLE: | |
8399 | case RELAX_MAYBE_DESIRE_ALIGN: | |
8400 | /* Do nothing. */ | |
e0001a05 NC |
8401 | break; |
8402 | ||
8403 | default: | |
43cd72b9 BW |
8404 | /* Just punt if we don't know the type. */ |
8405 | *fragPP = fragP; | |
8406 | return 0; | |
e0001a05 | 8407 | } |
43cd72b9 | 8408 | } |
c138bc38 | 8409 | else |
43cd72b9 BW |
8410 | { |
8411 | /* Just punt if we don't know the type. */ | |
8412 | *fragPP = fragP; | |
8413 | return 0; | |
8414 | } | |
8415 | fragP = fragP->fr_next; | |
8416 | } | |
8417 | ||
8418 | *fragPP = fragP; | |
8419 | return 0; | |
8420 | } | |
8421 | ||
8422 | ||
7fa3d080 BW |
8423 | static long bytes_to_stretch (fragS *, int, int, int, int); |
8424 | ||
43cd72b9 | 8425 | static long |
7fa3d080 | 8426 | future_alignment_required (fragS *fragP, long stretch ATTRIBUTE_UNUSED) |
43cd72b9 BW |
8427 | { |
8428 | fragS *this_frag = fragP; | |
8429 | long address; | |
8430 | int num_widens = 0; | |
8431 | int wide_nops = 0; | |
8432 | int narrow_nops = 0; | |
8433 | bfd_boolean paddable = FALSE; | |
8434 | offsetT local_opt_diff; | |
8435 | offsetT opt_diff; | |
8436 | offsetT max_diff; | |
8437 | int stretch_amount = 0; | |
8438 | int local_stretch_amount; | |
8439 | int global_stretch_amount; | |
8440 | ||
7fa3d080 BW |
8441 | address = find_address_of_next_align_frag |
8442 | (&fragP, &wide_nops, &narrow_nops, &num_widens, &paddable); | |
43cd72b9 | 8443 | |
b5e4a23d BW |
8444 | if (!address) |
8445 | { | |
8446 | if (this_frag->tc_frag_data.is_aligning_branch) | |
8447 | this_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED; | |
8448 | else | |
8449 | frag_wane (this_frag); | |
8450 | } | |
8451 | else | |
43cd72b9 BW |
8452 | { |
8453 | local_opt_diff = get_aligned_diff (fragP, address, &max_diff); | |
8454 | opt_diff = local_opt_diff; | |
8455 | assert (opt_diff >= 0); | |
8456 | assert (max_diff >= opt_diff); | |
c138bc38 | 8457 | if (max_diff == 0) |
43cd72b9 | 8458 | return 0; |
d2a033cd | 8459 | |
43cd72b9 BW |
8460 | if (fragP) |
8461 | fragP = fragP->fr_next; | |
8462 | ||
8463 | while (fragP && opt_diff < max_diff && address) | |
8464 | { | |
8465 | /* We only use these to determine if we can exit early | |
c138bc38 | 8466 | because there will be plenty of ways to align future |
43cd72b9 | 8467 | align frags. */ |
d77b99c9 | 8468 | int glob_widens = 0; |
43cd72b9 BW |
8469 | int dnn = 0; |
8470 | int dw = 0; | |
8471 | bfd_boolean glob_pad = 0; | |
7fa3d080 BW |
8472 | address = find_address_of_next_align_frag |
8473 | (&fragP, &glob_widens, &dnn, &dw, &glob_pad); | |
43cd72b9 | 8474 | /* If there is a padable portion, then skip. */ |
664df4e4 | 8475 | if (glob_pad || glob_widens >= (1 << branch_align_power (now_seg))) |
b5e4a23d | 8476 | address = 0; |
43cd72b9 | 8477 | |
c138bc38 | 8478 | if (address) |
43cd72b9 BW |
8479 | { |
8480 | offsetT next_m_diff; | |
8481 | offsetT next_o_diff; | |
8482 | ||
8483 | /* Downrange frags haven't had stretch added to them yet. */ | |
8484 | address += stretch; | |
8485 | ||
8486 | /* The address also includes any text expansion from this | |
8487 | frag in a previous pass, but we don't want that. */ | |
8488 | address -= this_frag->tc_frag_data.text_expansion[0]; | |
8489 | ||
8490 | /* Assume we are going to move at least opt_diff. In | |
8491 | reality, we might not be able to, but assuming that | |
8492 | we will helps catch cases where moving opt_diff pushes | |
8493 | the next target from aligned to unaligned. */ | |
8494 | address += opt_diff; | |
8495 | ||
8496 | next_o_diff = get_aligned_diff (fragP, address, &next_m_diff); | |
8497 | ||
8498 | /* Now cleanup for the adjustments to address. */ | |
8499 | next_o_diff += opt_diff; | |
8500 | next_m_diff += opt_diff; | |
8501 | if (next_o_diff <= max_diff && next_o_diff > opt_diff) | |
8502 | opt_diff = next_o_diff; | |
8503 | if (next_m_diff < max_diff) | |
8504 | max_diff = next_m_diff; | |
8505 | fragP = fragP->fr_next; | |
8506 | } | |
8507 | } | |
d2a033cd | 8508 | |
43cd72b9 BW |
8509 | /* If there are enough wideners in between, do it. */ |
8510 | if (paddable) | |
8511 | { | |
8512 | if (this_frag->fr_subtype == RELAX_UNREACHABLE) | |
8513 | { | |
8514 | assert (opt_diff <= UNREACHABLE_MAX_WIDTH); | |
8515 | return opt_diff; | |
8516 | } | |
8517 | return 0; | |
8518 | } | |
c138bc38 | 8519 | local_stretch_amount |
43cd72b9 BW |
8520 | = bytes_to_stretch (this_frag, wide_nops, narrow_nops, |
8521 | num_widens, local_opt_diff); | |
c138bc38 BW |
8522 | global_stretch_amount |
8523 | = bytes_to_stretch (this_frag, wide_nops, narrow_nops, | |
43cd72b9 | 8524 | num_widens, opt_diff); |
c138bc38 BW |
8525 | /* If the condition below is true, then the frag couldn't |
8526 | stretch the correct amount for the global case, so we just | |
8527 | optimize locally. We'll rely on the subsequent frags to get | |
43cd72b9 BW |
8528 | the correct alignment in the global case. */ |
8529 | if (global_stretch_amount < local_stretch_amount) | |
8530 | stretch_amount = local_stretch_amount; | |
8531 | else | |
8532 | stretch_amount = global_stretch_amount; | |
d2a033cd | 8533 | |
43cd72b9 BW |
8534 | if (this_frag->fr_subtype == RELAX_SLOTS |
8535 | && this_frag->tc_frag_data.slot_subtypes[0] == RELAX_NARROW) | |
8536 | assert (stretch_amount <= 1); | |
8537 | else if (this_frag->fr_subtype == RELAX_FILL_NOP) | |
8538 | { | |
8539 | if (this_frag->tc_frag_data.is_no_density) | |
8540 | assert (stretch_amount == 3 || stretch_amount == 0); | |
8541 | else | |
8542 | assert (stretch_amount <= 3); | |
8543 | } | |
8544 | } | |
8545 | return stretch_amount; | |
8546 | } | |
8547 | ||
8548 | ||
8549 | /* The idea: widen everything you can to get a target or loop aligned, | |
8550 | then start using NOPs. | |
8551 | ||
8552 | When we must have a NOP, here is a table of how we decide | |
8553 | (so you don't have to fight through the control flow below): | |
8554 | ||
8555 | wide_nops = the number of wide NOPs available for aligning | |
8556 | narrow_nops = the number of narrow NOPs available for aligning | |
8557 | (a subset of wide_nops) | |
8558 | widens = the number of narrow instructions that should be widened | |
8559 | ||
8560 | Desired wide narrow | |
8561 | Diff nop nop widens | |
8562 | 1 0 0 1 | |
8563 | 2 0 1 0 | |
8564 | 3a 1 0 0 | |
8565 | b 0 1 1 (case 3a makes this case unnecessary) | |
8566 | 4a 1 0 1 | |
8567 | b 0 2 0 | |
8568 | c 0 1 2 (case 4a makes this case unnecessary) | |
8569 | 5a 1 0 2 | |
8570 | b 1 1 0 | |
8571 | c 0 2 1 (case 5b makes this case unnecessary) | |
8572 | 6a 2 0 0 | |
8573 | b 1 0 3 | |
708587a4 | 8574 | c 0 1 4 (case 6b makes this case unnecessary) |
43cd72b9 BW |
8575 | d 1 1 1 (case 6a makes this case unnecessary) |
8576 | e 0 2 2 (case 6a makes this case unnecessary) | |
8577 | f 0 3 0 (case 6a makes this case unnecessary) | |
8578 | 7a 1 0 4 | |
8579 | b 2 0 1 | |
8580 | c 1 1 2 (case 7b makes this case unnecessary) | |
8581 | d 0 1 5 (case 7a makes this case unnecessary) | |
8582 | e 0 2 3 (case 7b makes this case unnecessary) | |
8583 | f 0 3 1 (case 7b makes this case unnecessary) | |
8584 | g 1 2 1 (case 7b makes this case unnecessary) | |
8585 | */ | |
8586 | ||
8587 | static long | |
7fa3d080 BW |
8588 | bytes_to_stretch (fragS *this_frag, |
8589 | int wide_nops, | |
8590 | int narrow_nops, | |
8591 | int num_widens, | |
8592 | int desired_diff) | |
43cd72b9 BW |
8593 | { |
8594 | int bytes_short = desired_diff - num_widens; | |
8595 | ||
8596 | assert (desired_diff >= 0 && desired_diff < 8); | |
8597 | if (desired_diff == 0) | |
8598 | return 0; | |
c138bc38 | 8599 | |
43cd72b9 | 8600 | assert (wide_nops > 0 || num_widens > 0); |
e0001a05 | 8601 | |
43cd72b9 BW |
8602 | /* Always prefer widening to NOP-filling. */ |
8603 | if (bytes_short < 0) | |
8604 | { | |
8605 | /* There are enough RELAX_NARROW frags after this one | |
8606 | to align the target without widening this frag in any way. */ | |
8607 | return 0; | |
8608 | } | |
c138bc38 | 8609 | |
43cd72b9 BW |
8610 | if (bytes_short == 0) |
8611 | { | |
8612 | /* Widen every narrow between here and the align target | |
8613 | and the align target will be properly aligned. */ | |
8614 | if (this_frag->fr_subtype == RELAX_FILL_NOP) | |
8615 | return 0; | |
8616 | else | |
8617 | return 1; | |
8618 | } | |
c138bc38 | 8619 | |
43cd72b9 BW |
8620 | /* From here we will need at least one NOP to get an alignment. |
8621 | However, we may not be able to align at all, in which case, | |
8622 | don't widen. */ | |
8623 | if (this_frag->fr_subtype == RELAX_FILL_NOP) | |
8624 | { | |
8625 | switch (desired_diff) | |
8626 | { | |
8627 | case 1: | |
8628 | return 0; | |
8629 | case 2: | |
8630 | if (!this_frag->tc_frag_data.is_no_density && narrow_nops == 1) | |
8631 | return 2; /* case 2 */ | |
8632 | return 0; | |
c138bc38 | 8633 | case 3: |
43cd72b9 BW |
8634 | if (wide_nops > 1) |
8635 | return 0; | |
8636 | else | |
8637 | return 3; /* case 3a */ | |
8638 | case 4: | |
8639 | if (num_widens >= 1 && wide_nops == 1) | |
8640 | return 3; /* case 4a */ | |
8641 | if (!this_frag->tc_frag_data.is_no_density && narrow_nops == 2) | |
8642 | return 2; /* case 4b */ | |
8643 | return 0; | |
8644 | case 5: | |
8645 | if (num_widens >= 2 && wide_nops == 1) | |
8646 | return 3; /* case 5a */ | |
c138bc38 | 8647 | /* We will need two nops. Are there enough nops |
43cd72b9 BW |
8648 | between here and the align target? */ |
8649 | if (wide_nops < 2 || narrow_nops == 0) | |
8650 | return 0; | |
8651 | /* Are there other nops closer that can serve instead? */ | |
8652 | if (wide_nops > 2 && narrow_nops > 1) | |
8653 | return 0; | |
8654 | /* Take the density one first, because there might not be | |
8655 | another density one available. */ | |
8656 | if (!this_frag->tc_frag_data.is_no_density) | |
8657 | return 2; /* case 5b narrow */ | |
8658 | else | |
8659 | return 3; /* case 5b wide */ | |
8660 | return 0; | |
8661 | case 6: | |
8662 | if (wide_nops == 2) | |
8663 | return 3; /* case 6a */ | |
8664 | else if (num_widens >= 3 && wide_nops == 1) | |
8665 | return 3; /* case 6b */ | |
8666 | return 0; | |
8667 | case 7: | |
8668 | if (wide_nops == 1 && num_widens >= 4) | |
8669 | return 3; /* case 7a */ | |
8670 | else if (wide_nops == 2 && num_widens >= 1) | |
8671 | return 3; /* case 7b */ | |
8672 | return 0; | |
e0001a05 | 8673 | default: |
43cd72b9 | 8674 | assert (0); |
e0001a05 | 8675 | } |
e0001a05 | 8676 | } |
43cd72b9 BW |
8677 | else |
8678 | { | |
c138bc38 | 8679 | /* We will need a NOP no matter what, but should we widen |
43cd72b9 | 8680 | this instruction to help? |
e0001a05 | 8681 | |
03aaa593 | 8682 | This is a RELAX_NARROW frag. */ |
43cd72b9 BW |
8683 | switch (desired_diff) |
8684 | { | |
8685 | case 1: | |
8686 | assert (0); | |
8687 | return 0; | |
8688 | case 2: | |
8689 | case 3: | |
8690 | return 0; | |
8691 | case 4: | |
8692 | if (wide_nops >= 1 && num_widens == 1) | |
8693 | return 1; /* case 4a */ | |
8694 | return 0; | |
8695 | case 5: | |
8696 | if (wide_nops >= 1 && num_widens == 2) | |
8697 | return 1; /* case 5a */ | |
8698 | return 0; | |
8699 | case 6: | |
8700 | if (wide_nops >= 2) | |
8701 | return 0; /* case 6a */ | |
8702 | else if (wide_nops >= 1 && num_widens == 3) | |
8703 | return 1; /* case 6b */ | |
8704 | return 0; | |
8705 | case 7: | |
8706 | if (wide_nops >= 1 && num_widens == 4) | |
8707 | return 1; /* case 7a */ | |
8708 | else if (wide_nops >= 2 && num_widens == 1) | |
8709 | return 1; /* case 7b */ | |
8710 | return 0; | |
8711 | default: | |
8712 | assert (0); | |
8713 | return 0; | |
8714 | } | |
8715 | } | |
8716 | assert (0); | |
8717 | return 0; | |
e0001a05 NC |
8718 | } |
8719 | ||
8720 | ||
8721 | static long | |
7fa3d080 BW |
8722 | relax_frag_immed (segT segP, |
8723 | fragS *fragP, | |
8724 | long stretch, | |
8725 | int min_steps, | |
8726 | xtensa_format fmt, | |
8727 | int slot, | |
8728 | int *stretched_p, | |
8729 | bfd_boolean estimate_only) | |
e0001a05 | 8730 | { |
43cd72b9 | 8731 | TInsn tinsn; |
e0001a05 NC |
8732 | int old_size; |
8733 | bfd_boolean negatable_branch = FALSE; | |
8734 | bfd_boolean branch_jmp_to_next = FALSE; | |
43cd72b9 BW |
8735 | bfd_boolean wide_insn = FALSE; |
8736 | xtensa_isa isa = xtensa_default_isa; | |
e0001a05 NC |
8737 | IStack istack; |
8738 | offsetT frag_offset; | |
8739 | int num_steps; | |
8740 | fragS *lit_fragP; | |
8741 | int num_text_bytes, num_literal_bytes; | |
43cd72b9 | 8742 | int literal_diff, total_text_diff, this_text_diff, first; |
e0001a05 NC |
8743 | |
8744 | assert (fragP->fr_opcode != NULL); | |
8745 | ||
b5e4a23d BW |
8746 | xg_clear_vinsn (&cur_vinsn); |
8747 | vinsn_from_chars (&cur_vinsn, fragP->fr_opcode); | |
b2d179be | 8748 | if (cur_vinsn.num_slots > 1) |
43cd72b9 BW |
8749 | wide_insn = TRUE; |
8750 | ||
b5e4a23d | 8751 | tinsn = cur_vinsn.slots[slot]; |
43cd72b9 | 8752 | tinsn_immed_from_frag (&tinsn, fragP, slot); |
e0001a05 | 8753 | |
64b607e6 | 8754 | if (estimate_only && xtensa_opcode_is_loop (isa, tinsn.opcode) == 1) |
43cd72b9 | 8755 | return 0; |
e0001a05 | 8756 | |
b08b5071 | 8757 | if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform) |
43cd72b9 | 8758 | branch_jmp_to_next = is_branch_jmp_to_next (&tinsn, fragP); |
e0001a05 | 8759 | |
43cd72b9 | 8760 | negatable_branch = (xtensa_opcode_is_branch (isa, tinsn.opcode) == 1); |
e0001a05 | 8761 | |
43cd72b9 | 8762 | old_size = xtensa_format_length (isa, fmt); |
e0001a05 NC |
8763 | |
8764 | /* Special case: replace a branch to the next instruction with a NOP. | |
8765 | This is required to work around a hardware bug in T1040.0 and also | |
8766 | serves as an optimization. */ | |
8767 | ||
8768 | if (branch_jmp_to_next | |
8769 | && ((old_size == 2) || (old_size == 3)) | |
8770 | && !next_frag_is_loop_target (fragP)) | |
8771 | return 0; | |
8772 | ||
8773 | /* Here is the fun stuff: Get the immediate field from this | |
8774 | instruction. If it fits, we are done. If not, find the next | |
8775 | instruction sequence that fits. */ | |
8776 | ||
8777 | frag_offset = fragP->fr_opcode - fragP->fr_literal; | |
8778 | istack_init (&istack); | |
43cd72b9 | 8779 | num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP, frag_offset, |
e0001a05 NC |
8780 | min_steps, stretch); |
8781 | if (num_steps < min_steps) | |
8782 | { | |
8783 | as_fatal (_("internal error: relaxation failed")); | |
8784 | return 0; | |
8785 | } | |
8786 | ||
8787 | if (num_steps > RELAX_IMMED_MAXSTEPS) | |
8788 | { | |
8789 | as_fatal (_("internal error: relaxation requires too many steps")); | |
8790 | return 0; | |
8791 | } | |
8792 | ||
43cd72b9 | 8793 | fragP->tc_frag_data.slot_subtypes[slot] = (int) RELAX_IMMED + num_steps; |
e0001a05 NC |
8794 | |
8795 | /* Figure out the number of bytes needed. */ | |
8796 | lit_fragP = 0; | |
e0001a05 | 8797 | num_literal_bytes = get_num_stack_literal_bytes (&istack); |
43cd72b9 BW |
8798 | literal_diff = |
8799 | num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot]; | |
8800 | first = 0; | |
8801 | while (istack.insn[first].opcode == XTENSA_UNDEFINED) | |
8802 | first++; | |
8803 | num_text_bytes = get_num_stack_text_bytes (&istack); | |
8804 | if (wide_insn) | |
8805 | { | |
8806 | num_text_bytes += old_size; | |
8807 | if (opcode_fits_format_slot (istack.insn[first].opcode, fmt, slot)) | |
8808 | num_text_bytes -= xg_get_single_size (istack.insn[first].opcode); | |
8809 | } | |
8810 | total_text_diff = num_text_bytes - old_size; | |
8811 | this_text_diff = total_text_diff - fragP->tc_frag_data.text_expansion[slot]; | |
e0001a05 NC |
8812 | |
8813 | /* It MUST get larger. If not, we could get an infinite loop. */ | |
43cd72b9 BW |
8814 | assert (num_text_bytes >= 0); |
8815 | assert (literal_diff >= 0); | |
8816 | assert (total_text_diff >= 0); | |
e0001a05 | 8817 | |
43cd72b9 BW |
8818 | fragP->tc_frag_data.text_expansion[slot] = total_text_diff; |
8819 | fragP->tc_frag_data.literal_expansion[slot] = num_literal_bytes; | |
8820 | assert (fragP->tc_frag_data.text_expansion[slot] >= 0); | |
8821 | assert (fragP->tc_frag_data.literal_expansion[slot] >= 0); | |
e0001a05 NC |
8822 | |
8823 | /* Find the associated expandable literal for this. */ | |
8824 | if (literal_diff != 0) | |
8825 | { | |
43cd72b9 | 8826 | lit_fragP = fragP->tc_frag_data.literal_frags[slot]; |
e0001a05 NC |
8827 | if (lit_fragP) |
8828 | { | |
8829 | assert (literal_diff == 4); | |
8830 | lit_fragP->tc_frag_data.unreported_expansion += literal_diff; | |
8831 | ||
8832 | /* We expect that the literal section state has NOT been | |
8833 | modified yet. */ | |
8834 | assert (lit_fragP->fr_type == rs_machine_dependent | |
8835 | && lit_fragP->fr_subtype == RELAX_LITERAL); | |
8836 | lit_fragP->fr_subtype = RELAX_LITERAL_NR; | |
8837 | ||
8838 | /* We need to mark this section for another iteration | |
8839 | of relaxation. */ | |
8840 | (*stretched_p)++; | |
8841 | } | |
8842 | } | |
8843 | ||
43cd72b9 | 8844 | if (negatable_branch && istack.ninsn > 1) |
1d19a770 | 8845 | update_next_frag_state (fragP); |
e0001a05 | 8846 | |
43cd72b9 | 8847 | return this_text_diff; |
e0001a05 NC |
8848 | } |
8849 | ||
8850 | \f | |
8851 | /* md_convert_frag Hook and Helper Functions. */ | |
8852 | ||
7fa3d080 BW |
8853 | static void convert_frag_align_next_opcode (fragS *); |
8854 | static void convert_frag_narrow (segT, fragS *, xtensa_format, int); | |
8855 | static void convert_frag_fill_nop (fragS *); | |
8856 | static void convert_frag_immed (segT, fragS *, int, xtensa_format, int); | |
8857 | ||
e0001a05 | 8858 | void |
7fa3d080 | 8859 | md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec, fragS *fragp) |
e0001a05 | 8860 | { |
43cd72b9 BW |
8861 | static xtensa_insnbuf vbuf = NULL; |
8862 | xtensa_isa isa = xtensa_default_isa; | |
8863 | int slot; | |
8864 | int num_slots; | |
8865 | xtensa_format fmt; | |
e0001a05 | 8866 | char *file_name; |
d77b99c9 | 8867 | unsigned line; |
e0001a05 NC |
8868 | |
8869 | as_where (&file_name, &line); | |
8870 | new_logical_line (fragp->fr_file, fragp->fr_line); | |
8871 | ||
8872 | switch (fragp->fr_subtype) | |
8873 | { | |
8874 | case RELAX_ALIGN_NEXT_OPCODE: | |
8875 | /* Always convert. */ | |
8876 | convert_frag_align_next_opcode (fragp); | |
8877 | break; | |
8878 | ||
8879 | case RELAX_DESIRE_ALIGN: | |
8880 | /* Do nothing. If not aligned already, too bad. */ | |
8881 | break; | |
8882 | ||
43cd72b9 BW |
8883 | case RELAX_LITERAL: |
8884 | case RELAX_LITERAL_FINAL: | |
8885 | break; | |
8886 | ||
8887 | case RELAX_SLOTS: | |
8888 | if (vbuf == NULL) | |
8889 | vbuf = xtensa_insnbuf_alloc (isa); | |
8890 | ||
d77b99c9 BW |
8891 | xtensa_insnbuf_from_chars |
8892 | (isa, vbuf, (unsigned char *) fragp->fr_opcode, 0); | |
43cd72b9 BW |
8893 | fmt = xtensa_format_decode (isa, vbuf); |
8894 | num_slots = xtensa_format_num_slots (isa, fmt); | |
8895 | ||
8896 | for (slot = 0; slot < num_slots; slot++) | |
8897 | { | |
8898 | switch (fragp->tc_frag_data.slot_subtypes[slot]) | |
8899 | { | |
8900 | case RELAX_NARROW: | |
8901 | convert_frag_narrow (sec, fragp, fmt, slot); | |
8902 | break; | |
8903 | ||
8904 | case RELAX_IMMED: | |
8905 | case RELAX_IMMED_STEP1: | |
8906 | case RELAX_IMMED_STEP2: | |
8907 | /* Place the immediate. */ | |
8908 | convert_frag_immed | |
8909 | (sec, fragp, | |
8910 | fragp->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED, | |
8911 | fmt, slot); | |
8912 | break; | |
8913 | ||
8914 | default: | |
8915 | /* This is OK because some slots could have | |
8916 | relaxations and others have none. */ | |
8917 | break; | |
8918 | } | |
8919 | } | |
8920 | break; | |
8921 | ||
8922 | case RELAX_UNREACHABLE: | |
8923 | memset (&fragp->fr_literal[fragp->fr_fix], 0, fragp->fr_var); | |
8924 | fragp->fr_fix += fragp->tc_frag_data.text_expansion[0]; | |
8925 | fragp->fr_var -= fragp->tc_frag_data.text_expansion[0]; | |
8926 | frag_wane (fragp); | |
e0001a05 NC |
8927 | break; |
8928 | ||
43cd72b9 BW |
8929 | case RELAX_MAYBE_UNREACHABLE: |
8930 | case RELAX_MAYBE_DESIRE_ALIGN: | |
8931 | frag_wane (fragp); | |
e0001a05 NC |
8932 | break; |
8933 | ||
43cd72b9 BW |
8934 | case RELAX_FILL_NOP: |
8935 | convert_frag_fill_nop (fragp); | |
e0001a05 NC |
8936 | break; |
8937 | ||
8938 | case RELAX_LITERAL_NR: | |
8939 | if (use_literal_section) | |
8940 | { | |
8941 | /* This should have been handled during relaxation. When | |
8942 | relaxing a code segment, literals sometimes need to be | |
8943 | added to the corresponding literal segment. If that | |
8944 | literal segment has already been relaxed, then we end up | |
8945 | in this situation. Marking the literal segments as data | |
8946 | would make this happen less often (since GAS always relaxes | |
8947 | code before data), but we could still get into trouble if | |
8948 | there are instructions in a segment that is not marked as | |
8949 | containing code. Until we can implement a better solution, | |
8950 | cheat and adjust the addresses of all the following frags. | |
8951 | This could break subsequent alignments, but the linker's | |
8952 | literal coalescing will do that anyway. */ | |
8953 | ||
8954 | fragS *f; | |
8955 | fragp->fr_subtype = RELAX_LITERAL_FINAL; | |
8956 | assert (fragp->tc_frag_data.unreported_expansion == 4); | |
8957 | memset (&fragp->fr_literal[fragp->fr_fix], 0, 4); | |
8958 | fragp->fr_var -= 4; | |
8959 | fragp->fr_fix += 4; | |
8960 | for (f = fragp->fr_next; f; f = f->fr_next) | |
8961 | f->fr_address += 4; | |
8962 | } | |
8963 | else | |
8964 | as_bad (_("invalid relaxation fragment result")); | |
8965 | break; | |
8966 | } | |
8967 | ||
8968 | fragp->fr_var = 0; | |
8969 | new_logical_line (file_name, line); | |
8970 | } | |
8971 | ||
8972 | ||
7fa3d080 BW |
8973 | static void |
8974 | convert_frag_align_next_opcode (fragS *fragp) | |
e0001a05 NC |
8975 | { |
8976 | char *nop_buf; /* Location for Writing. */ | |
e0001a05 NC |
8977 | bfd_boolean use_no_density = fragp->tc_frag_data.is_no_density; |
8978 | addressT aligned_address; | |
d77b99c9 BW |
8979 | offsetT fill_size; |
8980 | int nop, nop_count; | |
e0001a05 NC |
8981 | |
8982 | aligned_address = get_noop_aligned_address (fragp, fragp->fr_address + | |
8983 | fragp->fr_fix); | |
8984 | fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix); | |
8985 | nop_count = get_text_align_nop_count (fill_size, use_no_density); | |
8986 | nop_buf = fragp->fr_literal + fragp->fr_fix; | |
8987 | ||
d77b99c9 | 8988 | for (nop = 0; nop < nop_count; nop++) |
e0001a05 | 8989 | { |
d77b99c9 BW |
8990 | int nop_size; |
8991 | nop_size = get_text_align_nth_nop_size (fill_size, nop, use_no_density); | |
e0001a05 NC |
8992 | |
8993 | assemble_nop (nop_size, nop_buf); | |
8994 | nop_buf += nop_size; | |
8995 | } | |
8996 | ||
8997 | fragp->fr_fix += fill_size; | |
8998 | fragp->fr_var -= fill_size; | |
8999 | } | |
9000 | ||
9001 | ||
9002 | static void | |
7fa3d080 | 9003 | convert_frag_narrow (segT segP, fragS *fragP, xtensa_format fmt, int slot) |
e0001a05 | 9004 | { |
43cd72b9 | 9005 | TInsn tinsn, single_target; |
84b08ed9 | 9006 | int size, old_size, diff; |
e0001a05 NC |
9007 | offsetT frag_offset; |
9008 | ||
43cd72b9 BW |
9009 | assert (slot == 0); |
9010 | tinsn_from_chars (&tinsn, fragP->fr_opcode, 0); | |
9011 | ||
b5e4a23d | 9012 | if (fragP->tc_frag_data.is_aligning_branch == 1) |
43cd72b9 BW |
9013 | { |
9014 | assert (fragP->tc_frag_data.text_expansion[0] == 1 | |
9015 | || fragP->tc_frag_data.text_expansion[0] == 0); | |
9016 | convert_frag_immed (segP, fragP, fragP->tc_frag_data.text_expansion[0], | |
9017 | fmt, slot); | |
9018 | return; | |
9019 | } | |
9020 | ||
9021 | if (fragP->tc_frag_data.text_expansion[0] == 0) | |
e0001a05 NC |
9022 | { |
9023 | /* No conversion. */ | |
9024 | fragP->fr_var = 0; | |
9025 | return; | |
9026 | } | |
9027 | ||
9028 | assert (fragP->fr_opcode != NULL); | |
9029 | ||
43cd72b9 BW |
9030 | /* Frags in this relaxation state should only contain |
9031 | single instruction bundles. */ | |
9032 | tinsn_immed_from_frag (&tinsn, fragP, 0); | |
e0001a05 NC |
9033 | |
9034 | /* Just convert it to a wide form.... */ | |
9035 | size = 0; | |
43cd72b9 | 9036 | old_size = xg_get_single_size (tinsn.opcode); |
e0001a05 NC |
9037 | |
9038 | tinsn_init (&single_target); | |
9039 | frag_offset = fragP->fr_opcode - fragP->fr_literal; | |
9040 | ||
84b08ed9 | 9041 | if (! xg_is_single_relaxable_insn (&tinsn, &single_target, FALSE)) |
43cd72b9 BW |
9042 | { |
9043 | as_bad (_("unable to widen instruction")); | |
9044 | return; | |
9045 | } | |
9046 | ||
9047 | size = xg_get_single_size (single_target.opcode); | |
b2d179be BW |
9048 | xg_emit_insn_to_buf (&single_target, fragP->fr_opcode, fragP, |
9049 | frag_offset, TRUE); | |
e0001a05 NC |
9050 | |
9051 | diff = size - old_size; | |
9052 | assert (diff >= 0); | |
9053 | assert (diff <= fragP->fr_var); | |
9054 | fragP->fr_var -= diff; | |
9055 | fragP->fr_fix += diff; | |
9056 | ||
9057 | /* clean it up */ | |
9058 | fragP->fr_var = 0; | |
9059 | } | |
9060 | ||
9061 | ||
9062 | static void | |
7fa3d080 | 9063 | convert_frag_fill_nop (fragS *fragP) |
43cd72b9 BW |
9064 | { |
9065 | char *loc = &fragP->fr_literal[fragP->fr_fix]; | |
9066 | int size = fragP->tc_frag_data.text_expansion[0]; | |
9067 | assert ((unsigned) size == (fragP->fr_next->fr_address | |
9068 | - fragP->fr_address - fragP->fr_fix)); | |
9069 | if (size == 0) | |
9070 | { | |
9071 | /* No conversion. */ | |
9072 | fragP->fr_var = 0; | |
9073 | return; | |
9074 | } | |
9075 | assemble_nop (size, loc); | |
9076 | fragP->tc_frag_data.is_insn = TRUE; | |
9077 | fragP->fr_var -= size; | |
9078 | fragP->fr_fix += size; | |
9079 | frag_wane (fragP); | |
9080 | } | |
9081 | ||
9082 | ||
7fa3d080 BW |
9083 | static fixS *fix_new_exp_in_seg |
9084 | (segT, subsegT, fragS *, int, int, expressionS *, int, | |
9085 | bfd_reloc_code_real_type); | |
9086 | static void convert_frag_immed_finish_loop (segT, fragS *, TInsn *); | |
9087 | ||
43cd72b9 | 9088 | static void |
7fa3d080 BW |
9089 | convert_frag_immed (segT segP, |
9090 | fragS *fragP, | |
9091 | int min_steps, | |
9092 | xtensa_format fmt, | |
9093 | int slot) | |
e0001a05 NC |
9094 | { |
9095 | char *immed_instr = fragP->fr_opcode; | |
43cd72b9 | 9096 | TInsn orig_tinsn; |
e0001a05 | 9097 | bfd_boolean expanded = FALSE; |
e0001a05 | 9098 | bfd_boolean branch_jmp_to_next = FALSE; |
43cd72b9 | 9099 | char *fr_opcode = fragP->fr_opcode; |
43cd72b9 BW |
9100 | xtensa_isa isa = xtensa_default_isa; |
9101 | bfd_boolean wide_insn = FALSE; | |
9102 | int bytes; | |
9103 | bfd_boolean is_loop; | |
e0001a05 | 9104 | |
43cd72b9 | 9105 | assert (fr_opcode != NULL); |
e0001a05 | 9106 | |
b5e4a23d | 9107 | xg_clear_vinsn (&cur_vinsn); |
e0001a05 | 9108 | |
b5e4a23d | 9109 | vinsn_from_chars (&cur_vinsn, fr_opcode); |
b2d179be | 9110 | if (cur_vinsn.num_slots > 1) |
43cd72b9 | 9111 | wide_insn = TRUE; |
e0001a05 | 9112 | |
b5e4a23d | 9113 | orig_tinsn = cur_vinsn.slots[slot]; |
43cd72b9 BW |
9114 | tinsn_immed_from_frag (&orig_tinsn, fragP, slot); |
9115 | ||
9116 | is_loop = xtensa_opcode_is_loop (xtensa_default_isa, orig_tinsn.opcode) == 1; | |
e0001a05 | 9117 | |
b08b5071 | 9118 | if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform) |
43cd72b9 | 9119 | branch_jmp_to_next = is_branch_jmp_to_next (&orig_tinsn, fragP); |
e0001a05 NC |
9120 | |
9121 | if (branch_jmp_to_next && !next_frag_is_loop_target (fragP)) | |
9122 | { | |
9123 | /* Conversion just inserts a NOP and marks the fix as completed. */ | |
43cd72b9 BW |
9124 | bytes = xtensa_format_length (isa, fmt); |
9125 | if (bytes >= 4) | |
9126 | { | |
b5e4a23d BW |
9127 | cur_vinsn.slots[slot].opcode = |
9128 | xtensa_format_slot_nop_opcode (isa, cur_vinsn.format, slot); | |
9129 | cur_vinsn.slots[slot].ntok = 0; | |
43cd72b9 BW |
9130 | } |
9131 | else | |
9132 | { | |
9133 | bytes += fragP->tc_frag_data.text_expansion[0]; | |
9134 | assert (bytes == 2 || bytes == 3); | |
b5e4a23d | 9135 | build_nop (&cur_vinsn.slots[0], bytes); |
43cd72b9 BW |
9136 | fragP->fr_fix += fragP->tc_frag_data.text_expansion[0]; |
9137 | } | |
e7da6241 | 9138 | vinsn_to_insnbuf (&cur_vinsn, fr_opcode, frag_now, TRUE); |
d77b99c9 | 9139 | xtensa_insnbuf_to_chars |
b5e4a23d | 9140 | (isa, cur_vinsn.insnbuf, (unsigned char *) fr_opcode, 0); |
e0001a05 NC |
9141 | fragP->fr_var = 0; |
9142 | } | |
7c834684 | 9143 | else |
e0001a05 | 9144 | { |
43cd72b9 BW |
9145 | /* Here is the fun stuff: Get the immediate field from this |
9146 | instruction. If it fits, we're done. If not, find the next | |
9147 | instruction sequence that fits. */ | |
9148 | ||
e0001a05 NC |
9149 | IStack istack; |
9150 | int i; | |
9151 | symbolS *lit_sym = NULL; | |
9152 | int total_size = 0; | |
43cd72b9 | 9153 | int target_offset = 0; |
e0001a05 NC |
9154 | int old_size; |
9155 | int diff; | |
9156 | symbolS *gen_label = NULL; | |
9157 | offsetT frag_offset; | |
43cd72b9 BW |
9158 | bfd_boolean first = TRUE; |
9159 | bfd_boolean last_is_jump; | |
e0001a05 | 9160 | |
43cd72b9 | 9161 | /* It does not fit. Find something that does and |
e0001a05 | 9162 | convert immediately. */ |
43cd72b9 | 9163 | frag_offset = fr_opcode - fragP->fr_literal; |
e0001a05 | 9164 | istack_init (&istack); |
43cd72b9 | 9165 | xg_assembly_relax (&istack, &orig_tinsn, |
e0001a05 NC |
9166 | segP, fragP, frag_offset, min_steps, 0); |
9167 | ||
43cd72b9 | 9168 | old_size = xtensa_format_length (isa, fmt); |
e0001a05 NC |
9169 | |
9170 | /* Assemble this right inline. */ | |
9171 | ||
9172 | /* First, create the mapping from a label name to the REAL label. */ | |
43cd72b9 | 9173 | target_offset = 0; |
e0001a05 NC |
9174 | for (i = 0; i < istack.ninsn; i++) |
9175 | { | |
43cd72b9 | 9176 | TInsn *tinsn = &istack.insn[i]; |
e0001a05 NC |
9177 | fragS *lit_frag; |
9178 | ||
43cd72b9 | 9179 | switch (tinsn->insn_type) |
e0001a05 NC |
9180 | { |
9181 | case ITYPE_LITERAL: | |
9182 | if (lit_sym != NULL) | |
9183 | as_bad (_("multiple literals in expansion")); | |
9184 | /* First find the appropriate space in the literal pool. */ | |
43cd72b9 | 9185 | lit_frag = fragP->tc_frag_data.literal_frags[slot]; |
e0001a05 NC |
9186 | if (lit_frag == NULL) |
9187 | as_bad (_("no registered fragment for literal")); | |
43cd72b9 | 9188 | if (tinsn->ntok != 1) |
e0001a05 NC |
9189 | as_bad (_("number of literal tokens != 1")); |
9190 | ||
9191 | /* Set the literal symbol and add a fixup. */ | |
9192 | lit_sym = lit_frag->fr_symbol; | |
9193 | break; | |
9194 | ||
9195 | case ITYPE_LABEL: | |
43cd72b9 BW |
9196 | if (align_targets && !is_loop) |
9197 | { | |
9198 | fragS *unreach = fragP->fr_next; | |
9199 | while (!(unreach->fr_type == rs_machine_dependent | |
9200 | && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE | |
9201 | || unreach->fr_subtype == RELAX_UNREACHABLE))) | |
9202 | { | |
9203 | unreach = unreach->fr_next; | |
9204 | } | |
9205 | ||
9206 | assert (unreach->fr_type == rs_machine_dependent | |
9207 | && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE | |
9208 | || unreach->fr_subtype == RELAX_UNREACHABLE)); | |
9209 | ||
9210 | target_offset += unreach->tc_frag_data.text_expansion[0]; | |
9211 | } | |
e0001a05 NC |
9212 | assert (gen_label == NULL); |
9213 | gen_label = symbol_new (FAKE_LABEL_NAME, now_seg, | |
43cd72b9 BW |
9214 | fr_opcode - fragP->fr_literal |
9215 | + target_offset, fragP); | |
e0001a05 NC |
9216 | break; |
9217 | ||
9218 | case ITYPE_INSN: | |
43cd72b9 BW |
9219 | if (first && wide_insn) |
9220 | { | |
9221 | target_offset += xtensa_format_length (isa, fmt); | |
9222 | first = FALSE; | |
9223 | if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot)) | |
9224 | target_offset += xg_get_single_size (tinsn->opcode); | |
9225 | } | |
9226 | else | |
9227 | target_offset += xg_get_single_size (tinsn->opcode); | |
e0001a05 NC |
9228 | break; |
9229 | } | |
9230 | } | |
9231 | ||
9232 | total_size = 0; | |
43cd72b9 BW |
9233 | first = TRUE; |
9234 | last_is_jump = FALSE; | |
e0001a05 NC |
9235 | for (i = 0; i < istack.ninsn; i++) |
9236 | { | |
43cd72b9 | 9237 | TInsn *tinsn = &istack.insn[i]; |
e0001a05 NC |
9238 | fragS *lit_frag; |
9239 | int size; | |
9240 | segT target_seg; | |
43cd72b9 | 9241 | bfd_reloc_code_real_type reloc_type; |
e0001a05 | 9242 | |
43cd72b9 | 9243 | switch (tinsn->insn_type) |
e0001a05 NC |
9244 | { |
9245 | case ITYPE_LITERAL: | |
43cd72b9 BW |
9246 | lit_frag = fragP->tc_frag_data.literal_frags[slot]; |
9247 | /* Already checked. */ | |
e0001a05 NC |
9248 | assert (lit_frag != NULL); |
9249 | assert (lit_sym != NULL); | |
43cd72b9 BW |
9250 | assert (tinsn->ntok == 1); |
9251 | /* Add a fixup. */ | |
e0001a05 NC |
9252 | target_seg = S_GET_SEGMENT (lit_sym); |
9253 | assert (target_seg); | |
43cd72b9 BW |
9254 | if (tinsn->tok[0].X_op == O_pltrel) |
9255 | reloc_type = BFD_RELOC_XTENSA_PLT; | |
9256 | else | |
9257 | reloc_type = BFD_RELOC_32; | |
e0001a05 | 9258 | fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4, |
43cd72b9 | 9259 | &tinsn->tok[0], FALSE, reloc_type); |
e0001a05 NC |
9260 | break; |
9261 | ||
9262 | case ITYPE_LABEL: | |
9263 | break; | |
9264 | ||
9265 | case ITYPE_INSN: | |
43cd72b9 BW |
9266 | xg_resolve_labels (tinsn, gen_label); |
9267 | xg_resolve_literals (tinsn, lit_sym); | |
9268 | if (wide_insn && first) | |
9269 | { | |
9270 | first = FALSE; | |
9271 | if (opcode_fits_format_slot (tinsn->opcode, fmt, slot)) | |
9272 | { | |
b5e4a23d | 9273 | cur_vinsn.slots[slot] = *tinsn; |
43cd72b9 BW |
9274 | } |
9275 | else | |
9276 | { | |
b5e4a23d | 9277 | cur_vinsn.slots[slot].opcode = |
43cd72b9 | 9278 | xtensa_format_slot_nop_opcode (isa, fmt, slot); |
b5e4a23d | 9279 | cur_vinsn.slots[slot].ntok = 0; |
43cd72b9 | 9280 | } |
b5e4a23d BW |
9281 | vinsn_to_insnbuf (&cur_vinsn, immed_instr, fragP, TRUE); |
9282 | xtensa_insnbuf_to_chars (isa, cur_vinsn.insnbuf, | |
d77b99c9 | 9283 | (unsigned char *) immed_instr, 0); |
43cd72b9 BW |
9284 | fragP->tc_frag_data.is_insn = TRUE; |
9285 | size = xtensa_format_length (isa, fmt); | |
9286 | if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot)) | |
9287 | { | |
43cd72b9 | 9288 | xg_emit_insn_to_buf |
b2d179be | 9289 | (tinsn, immed_instr + size, fragP, |
43cd72b9 BW |
9290 | immed_instr - fragP->fr_literal + size, TRUE); |
9291 | size += xg_get_single_size (tinsn->opcode); | |
9292 | } | |
9293 | } | |
9294 | else | |
9295 | { | |
43cd72b9 | 9296 | size = xg_get_single_size (tinsn->opcode); |
b2d179be | 9297 | xg_emit_insn_to_buf (tinsn, immed_instr, fragP, |
43cd72b9 | 9298 | immed_instr - fragP->fr_literal, TRUE); |
43cd72b9 | 9299 | } |
e0001a05 | 9300 | immed_instr += size; |
43cd72b9 | 9301 | total_size += size; |
e0001a05 NC |
9302 | break; |
9303 | } | |
9304 | } | |
9305 | ||
9306 | diff = total_size - old_size; | |
9307 | assert (diff >= 0); | |
9308 | if (diff != 0) | |
9309 | expanded = TRUE; | |
9310 | assert (diff <= fragP->fr_var); | |
9311 | fragP->fr_var -= diff; | |
9312 | fragP->fr_fix += diff; | |
9313 | } | |
9314 | ||
e0001a05 | 9315 | /* Check for undefined immediates in LOOP instructions. */ |
43cd72b9 | 9316 | if (is_loop) |
e0001a05 NC |
9317 | { |
9318 | symbolS *sym; | |
43cd72b9 | 9319 | sym = orig_tinsn.tok[1].X_add_symbol; |
e0001a05 NC |
9320 | if (sym != NULL && !S_IS_DEFINED (sym)) |
9321 | { | |
9322 | as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym)); | |
9323 | return; | |
9324 | } | |
43cd72b9 | 9325 | sym = orig_tinsn.tok[1].X_op_symbol; |
e0001a05 NC |
9326 | if (sym != NULL && !S_IS_DEFINED (sym)) |
9327 | { | |
9328 | as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym)); | |
9329 | return; | |
9330 | } | |
9331 | } | |
9332 | ||
43cd72b9 BW |
9333 | if (expanded && xtensa_opcode_is_loop (isa, orig_tinsn.opcode) == 1) |
9334 | convert_frag_immed_finish_loop (segP, fragP, &orig_tinsn); | |
e0001a05 | 9335 | |
43cd72b9 | 9336 | if (expanded && is_direct_call_opcode (orig_tinsn.opcode)) |
e0001a05 NC |
9337 | { |
9338 | /* Add an expansion note on the expanded instruction. */ | |
9339 | fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4, | |
43cd72b9 | 9340 | &orig_tinsn.tok[0], TRUE, |
e0001a05 | 9341 | BFD_RELOC_XTENSA_ASM_EXPAND); |
e0001a05 NC |
9342 | } |
9343 | } | |
9344 | ||
9345 | ||
9346 | /* Add a new fix expression into the desired segment. We have to | |
9347 | switch to that segment to do this. */ | |
9348 | ||
9349 | static fixS * | |
7fa3d080 BW |
9350 | fix_new_exp_in_seg (segT new_seg, |
9351 | subsegT new_subseg, | |
9352 | fragS *frag, | |
9353 | int where, | |
9354 | int size, | |
9355 | expressionS *exp, | |
9356 | int pcrel, | |
9357 | bfd_reloc_code_real_type r_type) | |
e0001a05 NC |
9358 | { |
9359 | fixS *new_fix; | |
9360 | segT seg = now_seg; | |
9361 | subsegT subseg = now_subseg; | |
43cd72b9 | 9362 | |
e0001a05 NC |
9363 | assert (new_seg != 0); |
9364 | subseg_set (new_seg, new_subseg); | |
9365 | ||
e0001a05 NC |
9366 | new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type); |
9367 | subseg_set (seg, subseg); | |
9368 | return new_fix; | |
9369 | } | |
9370 | ||
9371 | ||
43cd72b9 BW |
9372 | /* Relax a loop instruction so that it can span loop >256 bytes. |
9373 | ||
9374 | loop as, .L1 | |
9375 | .L0: | |
9376 | rsr as, LEND | |
9377 | wsr as, LBEG | |
9378 | addi as, as, lo8 (label-.L1) | |
9379 | addmi as, as, mid8 (label-.L1) | |
9380 | wsr as, LEND | |
9381 | isync | |
9382 | rsr as, LCOUNT | |
9383 | addi as, as, 1 | |
9384 | .L1: | |
9385 | <<body>> | |
9386 | label: | |
9387 | */ | |
e0001a05 NC |
9388 | |
9389 | static void | |
7fa3d080 | 9390 | convert_frag_immed_finish_loop (segT segP, fragS *fragP, TInsn *tinsn) |
e0001a05 NC |
9391 | { |
9392 | TInsn loop_insn; | |
9393 | TInsn addi_insn; | |
9394 | TInsn addmi_insn; | |
9395 | unsigned long target; | |
9396 | static xtensa_insnbuf insnbuf = NULL; | |
9397 | unsigned int loop_length, loop_length_hi, loop_length_lo; | |
9398 | xtensa_isa isa = xtensa_default_isa; | |
9399 | addressT loop_offset; | |
9400 | addressT addi_offset = 9; | |
9401 | addressT addmi_offset = 12; | |
43cd72b9 | 9402 | fragS *next_fragP; |
d77b99c9 | 9403 | int target_count; |
e0001a05 NC |
9404 | |
9405 | if (!insnbuf) | |
9406 | insnbuf = xtensa_insnbuf_alloc (isa); | |
9407 | ||
9408 | /* Get the loop offset. */ | |
43cd72b9 | 9409 | loop_offset = get_expanded_loop_offset (tinsn->opcode); |
e0001a05 | 9410 | |
43cd72b9 BW |
9411 | /* Validate that there really is a LOOP at the loop_offset. Because |
9412 | loops are not bundleable, we can assume that the instruction will be | |
9413 | in slot 0. */ | |
9414 | tinsn_from_chars (&loop_insn, fragP->fr_opcode + loop_offset, 0); | |
9415 | tinsn_immed_from_frag (&loop_insn, fragP, 0); | |
9416 | ||
9417 | assert (xtensa_opcode_is_loop (isa, loop_insn.opcode) == 1); | |
e0001a05 NC |
9418 | addi_offset += loop_offset; |
9419 | addmi_offset += loop_offset; | |
9420 | ||
43cd72b9 | 9421 | assert (tinsn->ntok == 2); |
b08b5071 BW |
9422 | if (tinsn->tok[1].X_op == O_constant) |
9423 | target = tinsn->tok[1].X_add_number; | |
9424 | else if (tinsn->tok[1].X_op == O_symbol) | |
9425 | { | |
9426 | /* Find the fragment. */ | |
9427 | symbolS *sym = tinsn->tok[1].X_add_symbol; | |
9428 | assert (S_GET_SEGMENT (sym) == segP | |
9429 | || S_GET_SEGMENT (sym) == absolute_section); | |
9430 | target = (S_GET_VALUE (sym) + tinsn->tok[1].X_add_number); | |
9431 | } | |
9432 | else | |
9433 | { | |
9434 | as_bad (_("invalid expression evaluation type %d"), tinsn->tok[1].X_op); | |
9435 | target = 0; | |
9436 | } | |
e0001a05 NC |
9437 | |
9438 | know (symbolP); | |
9439 | know (symbolP->sy_frag); | |
9440 | know (!(S_GET_SEGMENT (symbolP) == absolute_section) | |
9441 | || symbol_get_frag (symbolP) == &zero_address_frag); | |
9442 | ||
9443 | loop_length = target - (fragP->fr_address + fragP->fr_fix); | |
9444 | loop_length_hi = loop_length & ~0x0ff; | |
9445 | loop_length_lo = loop_length & 0x0ff; | |
9446 | if (loop_length_lo >= 128) | |
9447 | { | |
9448 | loop_length_lo -= 256; | |
9449 | loop_length_hi += 256; | |
9450 | } | |
9451 | ||
43cd72b9 | 9452 | /* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most |
e0001a05 NC |
9453 | 32512. If the loop is larger than that, then we just fail. */ |
9454 | if (loop_length_hi > 32512) | |
9455 | as_bad_where (fragP->fr_file, fragP->fr_line, | |
9456 | _("loop too long for LOOP instruction")); | |
9457 | ||
43cd72b9 | 9458 | tinsn_from_chars (&addi_insn, fragP->fr_opcode + addi_offset, 0); |
e0001a05 NC |
9459 | assert (addi_insn.opcode == xtensa_addi_opcode); |
9460 | ||
43cd72b9 | 9461 | tinsn_from_chars (&addmi_insn, fragP->fr_opcode + addmi_offset, 0); |
e0001a05 NC |
9462 | assert (addmi_insn.opcode == xtensa_addmi_opcode); |
9463 | ||
9464 | set_expr_const (&addi_insn.tok[2], loop_length_lo); | |
9465 | tinsn_to_insnbuf (&addi_insn, insnbuf); | |
43cd72b9 | 9466 | |
e0001a05 | 9467 | fragP->tc_frag_data.is_insn = TRUE; |
d77b99c9 BW |
9468 | xtensa_insnbuf_to_chars |
9469 | (isa, insnbuf, (unsigned char *) fragP->fr_opcode + addi_offset, 0); | |
e0001a05 NC |
9470 | |
9471 | set_expr_const (&addmi_insn.tok[2], loop_length_hi); | |
9472 | tinsn_to_insnbuf (&addmi_insn, insnbuf); | |
d77b99c9 BW |
9473 | xtensa_insnbuf_to_chars |
9474 | (isa, insnbuf, (unsigned char *) fragP->fr_opcode + addmi_offset, 0); | |
43cd72b9 BW |
9475 | |
9476 | /* Walk through all of the frags from here to the loop end | |
9477 | and mark them as no_transform to keep them from being modified | |
9478 | by the linker. If we ever have a relocation for the | |
9479 | addi/addmi of the difference of two symbols we can remove this. */ | |
9480 | ||
9481 | target_count = 0; | |
9482 | for (next_fragP = fragP; next_fragP != NULL; | |
9483 | next_fragP = next_fragP->fr_next) | |
9484 | { | |
b08b5071 | 9485 | next_fragP->tc_frag_data.is_no_transform = TRUE; |
43cd72b9 BW |
9486 | if (next_fragP->tc_frag_data.is_loop_target) |
9487 | target_count++; | |
9488 | if (target_count == 2) | |
9489 | break; | |
9490 | } | |
e0001a05 NC |
9491 | } |
9492 | ||
b08b5071 BW |
9493 | \f |
9494 | /* A map that keeps information on a per-subsegment basis. This is | |
9495 | maintained during initial assembly, but is invalid once the | |
9496 | subsegments are smashed together. I.E., it cannot be used during | |
9497 | the relaxation. */ | |
e0001a05 | 9498 | |
b08b5071 | 9499 | typedef struct subseg_map_struct |
e0001a05 | 9500 | { |
b08b5071 BW |
9501 | /* the key */ |
9502 | segT seg; | |
9503 | subsegT subseg; | |
e0001a05 | 9504 | |
b08b5071 BW |
9505 | /* the data */ |
9506 | unsigned flags; | |
9507 | float total_freq; /* fall-through + branch target frequency */ | |
9508 | float target_freq; /* branch target frequency alone */ | |
9509 | ||
9510 | struct subseg_map_struct *next; | |
9511 | } subseg_map; | |
e0001a05 | 9512 | |
e0001a05 | 9513 | |
e0001a05 NC |
9514 | static subseg_map *sseg_map = NULL; |
9515 | ||
43cd72b9 | 9516 | static subseg_map * |
7fa3d080 | 9517 | get_subseg_info (segT seg, subsegT subseg) |
e0001a05 NC |
9518 | { |
9519 | subseg_map *subseg_e; | |
9520 | ||
9521 | for (subseg_e = sseg_map; subseg_e; subseg_e = subseg_e->next) | |
e0001a05 | 9522 | { |
43cd72b9 | 9523 | if (seg == subseg_e->seg && subseg == subseg_e->subseg) |
b08b5071 | 9524 | break; |
e0001a05 | 9525 | } |
b08b5071 BW |
9526 | return subseg_e; |
9527 | } | |
9528 | ||
9529 | ||
9530 | static subseg_map * | |
9531 | add_subseg_info (segT seg, subsegT subseg) | |
9532 | { | |
9533 | subseg_map *subseg_e = (subseg_map *) xmalloc (sizeof (subseg_map)); | |
43cd72b9 BW |
9534 | memset (subseg_e, 0, sizeof (subseg_map)); |
9535 | subseg_e->seg = seg; | |
9536 | subseg_e->subseg = subseg; | |
9537 | subseg_e->flags = 0; | |
9538 | /* Start off considering every branch target very important. */ | |
b08b5071 BW |
9539 | subseg_e->target_freq = 1.0; |
9540 | subseg_e->total_freq = 1.0; | |
43cd72b9 BW |
9541 | subseg_e->next = sseg_map; |
9542 | sseg_map = subseg_e; | |
43cd72b9 BW |
9543 | return subseg_e; |
9544 | } | |
e0001a05 | 9545 | |
7fa3d080 BW |
9546 | |
9547 | static unsigned | |
9548 | get_last_insn_flags (segT seg, subsegT subseg) | |
9549 | { | |
9550 | subseg_map *subseg_e = get_subseg_info (seg, subseg); | |
b08b5071 BW |
9551 | if (subseg_e) |
9552 | return subseg_e->flags; | |
9553 | return 0; | |
7fa3d080 BW |
9554 | } |
9555 | ||
9556 | ||
43cd72b9 | 9557 | static void |
7fa3d080 BW |
9558 | set_last_insn_flags (segT seg, |
9559 | subsegT subseg, | |
9560 | unsigned fl, | |
9561 | bfd_boolean val) | |
43cd72b9 BW |
9562 | { |
9563 | subseg_map *subseg_e = get_subseg_info (seg, subseg); | |
b08b5071 BW |
9564 | if (! subseg_e) |
9565 | subseg_e = add_subseg_info (seg, subseg); | |
e0001a05 NC |
9566 | if (val) |
9567 | subseg_e->flags |= fl; | |
9568 | else | |
9569 | subseg_e->flags &= ~fl; | |
9570 | } | |
9571 | ||
b08b5071 BW |
9572 | |
9573 | static float | |
9574 | get_subseg_total_freq (segT seg, subsegT subseg) | |
9575 | { | |
9576 | subseg_map *subseg_e = get_subseg_info (seg, subseg); | |
9577 | if (subseg_e) | |
9578 | return subseg_e->total_freq; | |
9579 | return 1.0; | |
9580 | } | |
9581 | ||
9582 | ||
9583 | static float | |
9584 | get_subseg_target_freq (segT seg, subsegT subseg) | |
9585 | { | |
9586 | subseg_map *subseg_e = get_subseg_info (seg, subseg); | |
9587 | if (subseg_e) | |
9588 | return subseg_e->target_freq; | |
9589 | return 1.0; | |
9590 | } | |
9591 | ||
9592 | ||
9593 | static void | |
9594 | set_subseg_freq (segT seg, subsegT subseg, float total_f, float target_f) | |
9595 | { | |
9596 | subseg_map *subseg_e = get_subseg_info (seg, subseg); | |
9597 | if (! subseg_e) | |
9598 | subseg_e = add_subseg_info (seg, subseg); | |
9599 | subseg_e->total_freq = total_f; | |
9600 | subseg_e->target_freq = target_f; | |
9601 | } | |
9602 | ||
e0001a05 NC |
9603 | \f |
9604 | /* Segment Lists and emit_state Stuff. */ | |
9605 | ||
e0001a05 | 9606 | static void |
7fa3d080 | 9607 | xtensa_move_seg_list_to_beginning (seg_list *head) |
e0001a05 NC |
9608 | { |
9609 | head = head->next; | |
9610 | while (head) | |
9611 | { | |
9612 | segT literal_section = head->seg; | |
9613 | ||
9614 | /* Move the literal section to the front of the section list. */ | |
9615 | assert (literal_section); | |
69852798 AM |
9616 | if (literal_section != stdoutput->sections) |
9617 | { | |
9618 | bfd_section_list_remove (stdoutput, literal_section); | |
9619 | bfd_section_list_prepend (stdoutput, literal_section); | |
9620 | } | |
e0001a05 NC |
9621 | head = head->next; |
9622 | } | |
9623 | } | |
9624 | ||
9625 | ||
7fa3d080 BW |
9626 | static void mark_literal_frags (seg_list *); |
9627 | ||
9628 | static void | |
9629 | xtensa_move_literals (void) | |
e0001a05 NC |
9630 | { |
9631 | seg_list *segment; | |
9632 | frchainS *frchain_from, *frchain_to; | |
9633 | fragS *search_frag, *next_frag, *last_frag, *literal_pool, *insert_after; | |
9634 | fragS **frag_splice; | |
9635 | emit_state state; | |
9636 | segT dest_seg; | |
9637 | fixS *fix, *next_fix, **fix_splice; | |
82e7541d | 9638 | sym_list *lit; |
e0001a05 | 9639 | |
a7877748 | 9640 | mark_literal_frags (literal_head->next); |
e0001a05 NC |
9641 | |
9642 | if (use_literal_section) | |
9643 | return; | |
9644 | ||
74869ac7 | 9645 | for (segment = literal_head->next; segment; segment = segment->next) |
e0001a05 | 9646 | { |
74869ac7 BW |
9647 | /* Keep the literals for .init and .fini in separate sections. */ |
9648 | if (!strcmp (segment_name (segment->seg), INIT_SECTION_NAME) | |
9649 | || !strcmp (segment_name (segment->seg), FINI_SECTION_NAME)) | |
9650 | continue; | |
9651 | ||
e0001a05 NC |
9652 | frchain_from = seg_info (segment->seg)->frchainP; |
9653 | search_frag = frchain_from->frch_root; | |
9654 | literal_pool = NULL; | |
9655 | frchain_to = NULL; | |
9656 | frag_splice = &(frchain_from->frch_root); | |
9657 | ||
9658 | while (!search_frag->tc_frag_data.literal_frag) | |
9659 | { | |
9660 | assert (search_frag->fr_fix == 0 | |
9661 | || search_frag->fr_type == rs_align); | |
9662 | search_frag = search_frag->fr_next; | |
9663 | } | |
9664 | ||
9665 | assert (search_frag->tc_frag_data.literal_frag->fr_subtype | |
9666 | == RELAX_LITERAL_POOL_BEGIN); | |
9667 | xtensa_switch_section_emit_state (&state, segment->seg, 0); | |
9668 | ||
9669 | /* Make sure that all the frags in this series are closed, and | |
9670 | that there is at least one left over of zero-size. This | |
9671 | prevents us from making a segment with an frchain without any | |
9672 | frags in it. */ | |
9673 | frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
43cd72b9 | 9674 | xtensa_set_frag_assembly_state (frag_now); |
e0001a05 NC |
9675 | last_frag = frag_now; |
9676 | frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
43cd72b9 | 9677 | xtensa_set_frag_assembly_state (frag_now); |
e0001a05 | 9678 | |
43cd72b9 | 9679 | while (search_frag != frag_now) |
e0001a05 NC |
9680 | { |
9681 | next_frag = search_frag->fr_next; | |
9682 | ||
43cd72b9 | 9683 | /* First, move the frag out of the literal section and |
e0001a05 NC |
9684 | to the appropriate place. */ |
9685 | if (search_frag->tc_frag_data.literal_frag) | |
9686 | { | |
9687 | literal_pool = search_frag->tc_frag_data.literal_frag; | |
9688 | assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN); | |
dd49a749 BW |
9689 | frchain_to = literal_pool->tc_frag_data.lit_frchain; |
9690 | assert (frchain_to); | |
e0001a05 NC |
9691 | } |
9692 | insert_after = literal_pool; | |
43cd72b9 | 9693 | |
e0001a05 NC |
9694 | while (insert_after->fr_next->fr_subtype != RELAX_LITERAL_POOL_END) |
9695 | insert_after = insert_after->fr_next; | |
9696 | ||
dd49a749 | 9697 | dest_seg = insert_after->fr_next->tc_frag_data.lit_seg; |
43cd72b9 | 9698 | |
e0001a05 NC |
9699 | *frag_splice = next_frag; |
9700 | search_frag->fr_next = insert_after->fr_next; | |
9701 | insert_after->fr_next = search_frag; | |
9702 | search_frag->tc_frag_data.lit_seg = dest_seg; | |
9703 | ||
9704 | /* Now move any fixups associated with this frag to the | |
9705 | right section. */ | |
9706 | fix = frchain_from->fix_root; | |
9707 | fix_splice = &(frchain_from->fix_root); | |
9708 | while (fix) | |
9709 | { | |
9710 | next_fix = fix->fx_next; | |
9711 | if (fix->fx_frag == search_frag) | |
9712 | { | |
9713 | *fix_splice = next_fix; | |
9714 | fix->fx_next = frchain_to->fix_root; | |
9715 | frchain_to->fix_root = fix; | |
9716 | if (frchain_to->fix_tail == NULL) | |
9717 | frchain_to->fix_tail = fix; | |
9718 | } | |
9719 | else | |
9720 | fix_splice = &(fix->fx_next); | |
9721 | fix = next_fix; | |
9722 | } | |
9723 | search_frag = next_frag; | |
9724 | } | |
9725 | ||
9726 | if (frchain_from->fix_root != NULL) | |
9727 | { | |
9728 | frchain_from = seg_info (segment->seg)->frchainP; | |
9729 | as_warn (_("fixes not all moved from %s"), segment->seg->name); | |
9730 | ||
9731 | assert (frchain_from->fix_root == NULL); | |
9732 | } | |
9733 | frchain_from->fix_tail = NULL; | |
9734 | xtensa_restore_emit_state (&state); | |
e0001a05 NC |
9735 | } |
9736 | ||
82e7541d BW |
9737 | /* Now fix up the SEGMENT value for all the literal symbols. */ |
9738 | for (lit = literal_syms; lit; lit = lit->next) | |
9739 | { | |
9740 | symbolS *lit_sym = lit->sym; | |
9741 | segT dest_seg = symbol_get_frag (lit_sym)->tc_frag_data.lit_seg; | |
43cd72b9 BW |
9742 | if (dest_seg) |
9743 | S_SET_SEGMENT (lit_sym, dest_seg); | |
82e7541d | 9744 | } |
e0001a05 NC |
9745 | } |
9746 | ||
9747 | ||
a7877748 BW |
9748 | /* Walk over all the frags for segments in a list and mark them as |
9749 | containing literals. As clunky as this is, we can't rely on frag_var | |
9750 | and frag_variant to get called in all situations. */ | |
9751 | ||
9752 | static void | |
7fa3d080 | 9753 | mark_literal_frags (seg_list *segment) |
a7877748 BW |
9754 | { |
9755 | frchainS *frchain_from; | |
9756 | fragS *search_frag; | |
9757 | ||
9758 | while (segment) | |
9759 | { | |
9760 | frchain_from = seg_info (segment->seg)->frchainP; | |
9761 | search_frag = frchain_from->frch_root; | |
c138bc38 | 9762 | while (search_frag) |
a7877748 BW |
9763 | { |
9764 | search_frag->tc_frag_data.is_literal = TRUE; | |
9765 | search_frag = search_frag->fr_next; | |
9766 | } | |
9767 | segment = segment->next; | |
9768 | } | |
9769 | } | |
9770 | ||
9771 | ||
e0001a05 | 9772 | static void |
7fa3d080 | 9773 | xtensa_reorder_seg_list (seg_list *head, segT after) |
e0001a05 NC |
9774 | { |
9775 | /* Move all of the sections in the section list to come | |
9776 | after "after" in the gnu segment list. */ | |
9777 | ||
9778 | head = head->next; | |
9779 | while (head) | |
9780 | { | |
9781 | segT literal_section = head->seg; | |
9782 | ||
9783 | /* Move the literal section after "after". */ | |
9784 | assert (literal_section); | |
9785 | if (literal_section != after) | |
9786 | { | |
69852798 AM |
9787 | bfd_section_list_remove (stdoutput, literal_section); |
9788 | bfd_section_list_insert_after (stdoutput, after, literal_section); | |
e0001a05 NC |
9789 | } |
9790 | ||
9791 | head = head->next; | |
9792 | } | |
9793 | } | |
9794 | ||
9795 | ||
9796 | /* Push all the literal segments to the end of the gnu list. */ | |
9797 | ||
7fa3d080 BW |
9798 | static void |
9799 | xtensa_reorder_segments (void) | |
e0001a05 NC |
9800 | { |
9801 | segT sec; | |
b08b5071 | 9802 | segT last_sec = 0; |
e0001a05 NC |
9803 | int old_count = 0; |
9804 | int new_count = 0; | |
9805 | ||
9806 | for (sec = stdoutput->sections; sec != NULL; sec = sec->next) | |
b08b5071 BW |
9807 | { |
9808 | last_sec = sec; | |
9809 | old_count++; | |
9810 | } | |
e0001a05 NC |
9811 | |
9812 | /* Now that we have the last section, push all the literal | |
9813 | sections to the end. */ | |
e0001a05 | 9814 | xtensa_reorder_seg_list (literal_head, last_sec); |
e0001a05 NC |
9815 | |
9816 | /* Now perform the final error check. */ | |
9817 | for (sec = stdoutput->sections; sec != NULL; sec = sec->next) | |
9818 | new_count++; | |
9819 | assert (new_count == old_count); | |
9820 | } | |
9821 | ||
9822 | ||
e0001a05 NC |
9823 | /* Change the emit state (seg, subseg, and frag related stuff) to the |
9824 | correct location. Return a emit_state which can be passed to | |
9825 | xtensa_restore_emit_state to return to current fragment. */ | |
9826 | ||
7fa3d080 BW |
9827 | static void |
9828 | xtensa_switch_to_literal_fragment (emit_state *result) | |
43cd72b9 BW |
9829 | { |
9830 | if (directive_state[directive_absolute_literals]) | |
9831 | { | |
74869ac7 BW |
9832 | segT lit4_seg = cache_literal_section (TRUE); |
9833 | xtensa_switch_section_emit_state (result, lit4_seg, 0); | |
43cd72b9 BW |
9834 | } |
9835 | else | |
9836 | xtensa_switch_to_non_abs_literal_fragment (result); | |
9837 | ||
9838 | /* Do a 4-byte align here. */ | |
9839 | frag_align (2, 0, 0); | |
9840 | record_alignment (now_seg, 2); | |
9841 | } | |
9842 | ||
9843 | ||
7fa3d080 BW |
9844 | static void |
9845 | xtensa_switch_to_non_abs_literal_fragment (emit_state *result) | |
e0001a05 | 9846 | { |
e0001a05 NC |
9847 | static bfd_boolean recursive = FALSE; |
9848 | fragS *pool_location = get_literal_pool_location (now_seg); | |
74869ac7 | 9849 | segT lit_seg; |
c138bc38 | 9850 | bfd_boolean is_init = |
e0001a05 | 9851 | (now_seg && !strcmp (segment_name (now_seg), INIT_SECTION_NAME)); |
c138bc38 | 9852 | bfd_boolean is_fini = |
e0001a05 | 9853 | (now_seg && !strcmp (segment_name (now_seg), FINI_SECTION_NAME)); |
e0001a05 | 9854 | |
43cd72b9 BW |
9855 | if (pool_location == NULL |
9856 | && !use_literal_section | |
e0001a05 NC |
9857 | && !recursive |
9858 | && !is_init && ! is_fini) | |
9859 | { | |
43cd72b9 | 9860 | as_bad (_("literal pool location required for text-section-literals; specify with .literal_position")); |
74869ac7 BW |
9861 | |
9862 | /* When we mark a literal pool location, we want to put a frag in | |
9863 | the literal pool that points to it. But to do that, we want to | |
9864 | switch_to_literal_fragment. But literal sections don't have | |
9865 | literal pools, so their location is always null, so we would | |
9866 | recurse forever. This is kind of hacky, but it works. */ | |
9867 | ||
e0001a05 | 9868 | recursive = TRUE; |
61846f28 | 9869 | xtensa_mark_literal_pool_location (); |
e0001a05 NC |
9870 | recursive = FALSE; |
9871 | } | |
9872 | ||
74869ac7 BW |
9873 | lit_seg = cache_literal_section (FALSE); |
9874 | xtensa_switch_section_emit_state (result, lit_seg, 0); | |
e0001a05 | 9875 | |
43cd72b9 BW |
9876 | if (!use_literal_section |
9877 | && !is_init && !is_fini | |
9878 | && get_literal_pool_location (now_seg) != pool_location) | |
e0001a05 NC |
9879 | { |
9880 | /* Close whatever frag is there. */ | |
9881 | frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
43cd72b9 | 9882 | xtensa_set_frag_assembly_state (frag_now); |
e0001a05 NC |
9883 | frag_now->tc_frag_data.literal_frag = pool_location; |
9884 | frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL); | |
43cd72b9 | 9885 | xtensa_set_frag_assembly_state (frag_now); |
e0001a05 | 9886 | } |
e0001a05 NC |
9887 | } |
9888 | ||
9889 | ||
9890 | /* Call this function before emitting data into the literal section. | |
9891 | This is a helper function for xtensa_switch_to_literal_fragment. | |
9892 | This is similar to a .section new_now_seg subseg. */ | |
9893 | ||
7fa3d080 BW |
9894 | static void |
9895 | xtensa_switch_section_emit_state (emit_state *state, | |
9896 | segT new_now_seg, | |
9897 | subsegT new_now_subseg) | |
e0001a05 NC |
9898 | { |
9899 | state->name = now_seg->name; | |
9900 | state->now_seg = now_seg; | |
9901 | state->now_subseg = now_subseg; | |
9902 | state->generating_literals = generating_literals; | |
9903 | generating_literals++; | |
2b0210eb | 9904 | subseg_set (new_now_seg, new_now_subseg); |
e0001a05 NC |
9905 | } |
9906 | ||
9907 | ||
9908 | /* Use to restore the emitting into the normal place. */ | |
9909 | ||
7fa3d080 BW |
9910 | static void |
9911 | xtensa_restore_emit_state (emit_state *state) | |
e0001a05 NC |
9912 | { |
9913 | generating_literals = state->generating_literals; | |
2b0210eb | 9914 | subseg_set (state->now_seg, state->now_subseg); |
e0001a05 NC |
9915 | } |
9916 | ||
9917 | ||
74869ac7 | 9918 | /* Predicate function used to look up a section in a particular group. */ |
e0001a05 | 9919 | |
74869ac7 BW |
9920 | static bfd_boolean |
9921 | match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf) | |
e0001a05 | 9922 | { |
74869ac7 BW |
9923 | const char *gname = inf; |
9924 | const char *group_name = elf_group_name (sec); | |
9925 | ||
9926 | return (group_name == gname | |
9927 | || (group_name != NULL | |
9928 | && gname != NULL | |
9929 | && strcmp (group_name, gname) == 0)); | |
9930 | } | |
e0001a05 | 9931 | |
e0001a05 | 9932 | |
74869ac7 BW |
9933 | /* Get the literal section to be used for the current text section. |
9934 | The result may be cached in the default_lit_sections structure. */ | |
9935 | ||
9936 | static segT | |
9937 | cache_literal_section (bfd_boolean use_abs_literals) | |
9938 | { | |
9939 | const char *text_name, *group_name = 0; | |
9940 | char *base_name, *name, *suffix; | |
9941 | segT *pcached; | |
9942 | segT seg, current_section; | |
9943 | int current_subsec; | |
9944 | bfd_boolean linkonce = FALSE; | |
9945 | ||
9946 | /* Save the current section/subsection. */ | |
9947 | current_section = now_seg; | |
9948 | current_subsec = now_subseg; | |
9949 | ||
9950 | /* Clear the cached values if they are no longer valid. */ | |
9951 | if (now_seg != default_lit_sections.current_text_seg) | |
b08b5071 | 9952 | { |
74869ac7 BW |
9953 | default_lit_sections.current_text_seg = now_seg; |
9954 | default_lit_sections.lit_seg = NULL; | |
9955 | default_lit_sections.lit4_seg = NULL; | |
9956 | } | |
9957 | ||
9958 | /* Check if the literal section is already cached. */ | |
9959 | if (use_abs_literals) | |
9960 | pcached = &default_lit_sections.lit4_seg; | |
9961 | else | |
9962 | pcached = &default_lit_sections.lit_seg; | |
9963 | ||
9964 | if (*pcached) | |
9965 | return *pcached; | |
9966 | ||
9967 | text_name = default_lit_sections.lit_prefix; | |
9968 | if (! text_name || ! *text_name) | |
9969 | { | |
9970 | text_name = segment_name (current_section); | |
9971 | group_name = elf_group_name (current_section); | |
9972 | linkonce = (current_section->flags & SEC_LINK_ONCE) != 0; | |
9973 | } | |
9974 | ||
9975 | base_name = use_abs_literals ? ".lit4" : ".literal"; | |
9976 | if (group_name) | |
9977 | { | |
9978 | name = xmalloc (strlen (base_name) + strlen (group_name) + 2); | |
9979 | sprintf (name, "%s.%s", base_name, group_name); | |
9980 | } | |
9981 | else if (strncmp (text_name, ".gnu.linkonce.", linkonce_len) == 0) | |
9982 | { | |
9983 | suffix = strchr (text_name + linkonce_len, '.'); | |
9984 | ||
9985 | name = xmalloc (linkonce_len + strlen (base_name) + 1 | |
9986 | + (suffix ? strlen (suffix) : 0)); | |
9987 | strcpy (name, ".gnu.linkonce"); | |
9988 | strcat (name, base_name); | |
9989 | if (suffix) | |
9990 | strcat (name, suffix); | |
9991 | linkonce = TRUE; | |
9992 | } | |
9993 | else | |
9994 | { | |
9995 | /* If the section name ends with ".text", then replace that suffix | |
9996 | instead of appending an additional suffix. */ | |
9997 | size_t len = strlen (text_name); | |
9998 | if (len >= 5 && strcmp (text_name + len - 5, ".text") == 0) | |
9999 | len -= 5; | |
10000 | ||
10001 | name = xmalloc (len + strlen (base_name) + 1); | |
10002 | strcpy (name, text_name); | |
10003 | strcpy (name + len, base_name); | |
b08b5071 | 10004 | } |
e0001a05 | 10005 | |
74869ac7 BW |
10006 | /* Canonicalize section names to allow renaming literal sections. |
10007 | The group name, if any, came from the current text section and | |
10008 | has already been canonicalized. */ | |
10009 | name = tc_canonicalize_symbol_name (name); | |
10010 | ||
10011 | seg = bfd_get_section_by_name_if (stdoutput, name, match_section_group, | |
10012 | (void *) group_name); | |
10013 | if (! seg) | |
e0001a05 | 10014 | { |
74869ac7 BW |
10015 | flagword flags; |
10016 | ||
10017 | seg = subseg_force_new (name, 0); | |
10018 | ||
10019 | if (! use_abs_literals) | |
b08b5071 | 10020 | { |
74869ac7 | 10021 | /* Add the newly created literal segment to the list. */ |
b08b5071 BW |
10022 | seg_list *n = (seg_list *) xmalloc (sizeof (seg_list)); |
10023 | n->seg = seg; | |
74869ac7 BW |
10024 | n->next = literal_head->next; |
10025 | literal_head->next = n; | |
b08b5071 | 10026 | } |
74869ac7 BW |
10027 | |
10028 | flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC | SEC_LOAD | |
10029 | | (linkonce ? (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD) : 0) | |
10030 | | (use_abs_literals ? SEC_DATA : SEC_CODE)); | |
10031 | ||
10032 | elf_group_name (seg) = group_name; | |
10033 | ||
10034 | bfd_set_section_flags (stdoutput, seg, flags); | |
b08b5071 | 10035 | bfd_set_section_alignment (stdoutput, seg, 2); |
e0001a05 NC |
10036 | } |
10037 | ||
74869ac7 | 10038 | *pcached = seg; |
b08b5071 | 10039 | subseg_set (current_section, current_subsec); |
74869ac7 | 10040 | return seg; |
e0001a05 NC |
10041 | } |
10042 | ||
43cd72b9 BW |
10043 | \f |
10044 | /* Property Tables Stuff. */ | |
10045 | ||
7fa3d080 BW |
10046 | #define XTENSA_INSN_SEC_NAME ".xt.insn" |
10047 | #define XTENSA_LIT_SEC_NAME ".xt.lit" | |
10048 | #define XTENSA_PROP_SEC_NAME ".xt.prop" | |
10049 | ||
10050 | typedef bfd_boolean (*frag_predicate) (const fragS *); | |
10051 | typedef void (*frag_flags_fn) (const fragS *, frag_flags *); | |
10052 | ||
b08b5071 | 10053 | static bfd_boolean get_frag_is_literal (const fragS *); |
7fa3d080 BW |
10054 | static void xtensa_create_property_segments |
10055 | (frag_predicate, frag_predicate, const char *, xt_section_type); | |
10056 | static void xtensa_create_xproperty_segments | |
10057 | (frag_flags_fn, const char *, xt_section_type); | |
10058 | static segment_info_type *retrieve_segment_info (segT); | |
7fa3d080 BW |
10059 | static bfd_boolean section_has_property (segT, frag_predicate); |
10060 | static bfd_boolean section_has_xproperty (segT, frag_flags_fn); | |
10061 | static void add_xt_block_frags | |
10062 | (segT, segT, xtensa_block_info **, frag_predicate, frag_predicate); | |
10063 | static bfd_boolean xtensa_frag_flags_is_empty (const frag_flags *); | |
10064 | static void xtensa_frag_flags_init (frag_flags *); | |
10065 | static void get_frag_property_flags (const fragS *, frag_flags *); | |
10066 | static bfd_vma frag_flags_to_number (const frag_flags *); | |
10067 | static void add_xt_prop_frags | |
10068 | (segT, segT, xtensa_block_info **, frag_flags_fn); | |
10069 | ||
10070 | /* Set up property tables after relaxation. */ | |
10071 | ||
10072 | void | |
10073 | xtensa_post_relax_hook (void) | |
10074 | { | |
10075 | xtensa_move_seg_list_to_beginning (literal_head); | |
7fa3d080 BW |
10076 | |
10077 | xtensa_find_unmarked_state_frags (); | |
10078 | ||
b29757dc BW |
10079 | xtensa_create_property_segments (get_frag_is_literal, |
10080 | NULL, | |
10081 | XTENSA_LIT_SEC_NAME, | |
10082 | xt_literal_sec); | |
7fa3d080 BW |
10083 | xtensa_create_xproperty_segments (get_frag_property_flags, |
10084 | XTENSA_PROP_SEC_NAME, | |
10085 | xt_prop_sec); | |
10086 | ||
10087 | if (warn_unaligned_branch_targets) | |
10088 | bfd_map_over_sections (stdoutput, xtensa_find_unaligned_branch_targets, 0); | |
10089 | bfd_map_over_sections (stdoutput, xtensa_find_unaligned_loops, 0); | |
10090 | } | |
10091 | ||
10092 | ||
43cd72b9 BW |
10093 | /* This function is only meaningful after xtensa_move_literals. */ |
10094 | ||
10095 | static bfd_boolean | |
7fa3d080 | 10096 | get_frag_is_literal (const fragS *fragP) |
43cd72b9 BW |
10097 | { |
10098 | assert (fragP != NULL); | |
10099 | return fragP->tc_frag_data.is_literal; | |
10100 | } | |
10101 | ||
10102 | ||
43cd72b9 | 10103 | static void |
7fa3d080 BW |
10104 | xtensa_create_property_segments (frag_predicate property_function, |
10105 | frag_predicate end_property_function, | |
10106 | const char *section_name_base, | |
10107 | xt_section_type sec_type) | |
43cd72b9 BW |
10108 | { |
10109 | segT *seclist; | |
10110 | ||
10111 | /* Walk over all of the current segments. | |
10112 | Walk over each fragment | |
10113 | For each non-empty fragment, | |
10114 | Build a property record (append where possible). */ | |
10115 | ||
10116 | for (seclist = &stdoutput->sections; | |
10117 | seclist && *seclist; | |
10118 | seclist = &(*seclist)->next) | |
10119 | { | |
10120 | segT sec = *seclist; | |
10121 | flagword flags; | |
10122 | ||
10123 | flags = bfd_get_section_flags (stdoutput, sec); | |
10124 | if (flags & SEC_DEBUGGING) | |
10125 | continue; | |
10126 | if (!(flags & SEC_ALLOC)) | |
10127 | continue; | |
10128 | ||
10129 | if (section_has_property (sec, property_function)) | |
10130 | { | |
74869ac7 BW |
10131 | segT insn_sec = |
10132 | xtensa_get_property_section (sec, section_name_base); | |
43cd72b9 BW |
10133 | segment_info_type *xt_seg_info = retrieve_segment_info (insn_sec); |
10134 | xtensa_block_info **xt_blocks = | |
10135 | &xt_seg_info->tc_segment_info_data.blocks[sec_type]; | |
10136 | /* Walk over all of the frchains here and add new sections. */ | |
10137 | add_xt_block_frags (sec, insn_sec, xt_blocks, property_function, | |
10138 | end_property_function); | |
10139 | } | |
10140 | } | |
10141 | ||
10142 | /* Now we fill them out.... */ | |
10143 | ||
10144 | for (seclist = &stdoutput->sections; | |
10145 | seclist && *seclist; | |
10146 | seclist = &(*seclist)->next) | |
10147 | { | |
10148 | segment_info_type *seginfo; | |
10149 | xtensa_block_info *block; | |
10150 | segT sec = *seclist; | |
10151 | ||
10152 | seginfo = seg_info (sec); | |
10153 | block = seginfo->tc_segment_info_data.blocks[sec_type]; | |
10154 | ||
10155 | if (block) | |
10156 | { | |
10157 | xtensa_block_info *cur_block; | |
10158 | /* This is a section with some data. */ | |
10159 | int num_recs = 0; | |
d77b99c9 | 10160 | bfd_size_type rec_size; |
43cd72b9 BW |
10161 | |
10162 | for (cur_block = block; cur_block; cur_block = cur_block->next) | |
10163 | num_recs++; | |
10164 | ||
10165 | rec_size = num_recs * 8; | |
10166 | bfd_set_section_size (stdoutput, sec, rec_size); | |
10167 | ||
10168 | /* In order to make this work with the assembler, we have to | |
10169 | build some frags and then build the "fixups" for it. It | |
10170 | would be easier to just set the contents then set the | |
10171 | arlents. */ | |
10172 | ||
10173 | if (num_recs) | |
10174 | { | |
10175 | /* Allocate a fragment and leak it. */ | |
10176 | fragS *fragP; | |
d77b99c9 | 10177 | bfd_size_type frag_size; |
43cd72b9 BW |
10178 | fixS *fixes; |
10179 | frchainS *frchainP; | |
10180 | int i; | |
10181 | char *frag_data; | |
10182 | ||
10183 | frag_size = sizeof (fragS) + rec_size; | |
10184 | fragP = (fragS *) xmalloc (frag_size); | |
e0001a05 | 10185 | |
43cd72b9 BW |
10186 | memset (fragP, 0, frag_size); |
10187 | fragP->fr_address = 0; | |
10188 | fragP->fr_next = NULL; | |
10189 | fragP->fr_fix = rec_size; | |
10190 | fragP->fr_var = 0; | |
10191 | fragP->fr_type = rs_fill; | |
10192 | /* The rest are zeros. */ | |
e0001a05 | 10193 | |
43cd72b9 BW |
10194 | frchainP = seginfo->frchainP; |
10195 | frchainP->frch_root = fragP; | |
10196 | frchainP->frch_last = fragP; | |
e0001a05 | 10197 | |
43cd72b9 BW |
10198 | fixes = (fixS *) xmalloc (sizeof (fixS) * num_recs); |
10199 | memset (fixes, 0, sizeof (fixS) * num_recs); | |
e0001a05 | 10200 | |
43cd72b9 BW |
10201 | seginfo->fix_root = fixes; |
10202 | seginfo->fix_tail = &fixes[num_recs - 1]; | |
10203 | cur_block = block; | |
10204 | frag_data = &fragP->fr_literal[0]; | |
10205 | for (i = 0; i < num_recs; i++) | |
10206 | { | |
10207 | fixS *fix = &fixes[i]; | |
10208 | assert (cur_block); | |
e0001a05 | 10209 | |
43cd72b9 BW |
10210 | /* Write the fixup. */ |
10211 | if (i != num_recs - 1) | |
10212 | fix->fx_next = &fixes[i + 1]; | |
10213 | else | |
10214 | fix->fx_next = NULL; | |
10215 | fix->fx_size = 4; | |
10216 | fix->fx_done = 0; | |
10217 | fix->fx_frag = fragP; | |
10218 | fix->fx_where = i * 8; | |
10219 | fix->fx_addsy = section_symbol (cur_block->sec); | |
10220 | fix->fx_offset = cur_block->offset; | |
10221 | fix->fx_r_type = BFD_RELOC_32; | |
10222 | fix->fx_file = "Internal Assembly"; | |
10223 | fix->fx_line = 0; | |
e0001a05 | 10224 | |
43cd72b9 BW |
10225 | /* Write the length. */ |
10226 | md_number_to_chars (&frag_data[4 + 8 * i], | |
10227 | cur_block->size, 4); | |
10228 | cur_block = cur_block->next; | |
10229 | } | |
10230 | } | |
10231 | } | |
10232 | } | |
e0001a05 NC |
10233 | } |
10234 | ||
10235 | ||
7fa3d080 BW |
10236 | static void |
10237 | xtensa_create_xproperty_segments (frag_flags_fn flag_fn, | |
10238 | const char *section_name_base, | |
10239 | xt_section_type sec_type) | |
e0001a05 NC |
10240 | { |
10241 | segT *seclist; | |
10242 | ||
10243 | /* Walk over all of the current segments. | |
43cd72b9 BW |
10244 | Walk over each fragment. |
10245 | For each fragment that has instructions, | |
10246 | build an instruction record (append where possible). */ | |
e0001a05 NC |
10247 | |
10248 | for (seclist = &stdoutput->sections; | |
10249 | seclist && *seclist; | |
10250 | seclist = &(*seclist)->next) | |
10251 | { | |
10252 | segT sec = *seclist; | |
43cd72b9 BW |
10253 | flagword flags; |
10254 | ||
10255 | flags = bfd_get_section_flags (stdoutput, sec); | |
6624cbde BW |
10256 | if ((flags & SEC_DEBUGGING) |
10257 | || !(flags & SEC_ALLOC) | |
10258 | || (flags & SEC_MERGE)) | |
43cd72b9 BW |
10259 | continue; |
10260 | ||
10261 | if (section_has_xproperty (sec, flag_fn)) | |
e0001a05 | 10262 | { |
74869ac7 BW |
10263 | segT insn_sec = |
10264 | xtensa_get_property_section (sec, section_name_base); | |
e0001a05 | 10265 | segment_info_type *xt_seg_info = retrieve_segment_info (insn_sec); |
43cd72b9 | 10266 | xtensa_block_info **xt_blocks = |
e0001a05 NC |
10267 | &xt_seg_info->tc_segment_info_data.blocks[sec_type]; |
10268 | /* Walk over all of the frchains here and add new sections. */ | |
43cd72b9 | 10269 | add_xt_prop_frags (sec, insn_sec, xt_blocks, flag_fn); |
e0001a05 NC |
10270 | } |
10271 | } | |
10272 | ||
10273 | /* Now we fill them out.... */ | |
10274 | ||
10275 | for (seclist = &stdoutput->sections; | |
10276 | seclist && *seclist; | |
10277 | seclist = &(*seclist)->next) | |
10278 | { | |
10279 | segment_info_type *seginfo; | |
10280 | xtensa_block_info *block; | |
10281 | segT sec = *seclist; | |
43cd72b9 | 10282 | |
e0001a05 NC |
10283 | seginfo = seg_info (sec); |
10284 | block = seginfo->tc_segment_info_data.blocks[sec_type]; | |
10285 | ||
10286 | if (block) | |
10287 | { | |
10288 | xtensa_block_info *cur_block; | |
10289 | /* This is a section with some data. */ | |
43cd72b9 | 10290 | int num_recs = 0; |
d77b99c9 | 10291 | bfd_size_type rec_size; |
e0001a05 NC |
10292 | |
10293 | for (cur_block = block; cur_block; cur_block = cur_block->next) | |
10294 | num_recs++; | |
10295 | ||
43cd72b9 | 10296 | rec_size = num_recs * (8 + 4); |
e0001a05 NC |
10297 | bfd_set_section_size (stdoutput, sec, rec_size); |
10298 | ||
43cd72b9 BW |
10299 | /* elf_section_data (sec)->this_hdr.sh_entsize = 12; */ |
10300 | ||
10301 | /* In order to make this work with the assembler, we have to build | |
10302 | some frags then build the "fixups" for it. It would be easier to | |
10303 | just set the contents then set the arlents. */ | |
e0001a05 NC |
10304 | |
10305 | if (num_recs) | |
10306 | { | |
43cd72b9 | 10307 | /* Allocate a fragment and (unfortunately) leak it. */ |
e0001a05 | 10308 | fragS *fragP; |
d77b99c9 | 10309 | bfd_size_type frag_size; |
e0001a05 NC |
10310 | fixS *fixes; |
10311 | frchainS *frchainP; | |
43cd72b9 | 10312 | int i; |
e0001a05 NC |
10313 | char *frag_data; |
10314 | ||
10315 | frag_size = sizeof (fragS) + rec_size; | |
10316 | fragP = (fragS *) xmalloc (frag_size); | |
10317 | ||
10318 | memset (fragP, 0, frag_size); | |
10319 | fragP->fr_address = 0; | |
10320 | fragP->fr_next = NULL; | |
10321 | fragP->fr_fix = rec_size; | |
10322 | fragP->fr_var = 0; | |
10323 | fragP->fr_type = rs_fill; | |
43cd72b9 | 10324 | /* The rest are zeros. */ |
e0001a05 NC |
10325 | |
10326 | frchainP = seginfo->frchainP; | |
10327 | frchainP->frch_root = fragP; | |
10328 | frchainP->frch_last = fragP; | |
10329 | ||
10330 | fixes = (fixS *) xmalloc (sizeof (fixS) * num_recs); | |
10331 | memset (fixes, 0, sizeof (fixS) * num_recs); | |
10332 | ||
10333 | seginfo->fix_root = fixes; | |
10334 | seginfo->fix_tail = &fixes[num_recs - 1]; | |
10335 | cur_block = block; | |
10336 | frag_data = &fragP->fr_literal[0]; | |
10337 | for (i = 0; i < num_recs; i++) | |
10338 | { | |
10339 | fixS *fix = &fixes[i]; | |
10340 | assert (cur_block); | |
10341 | ||
10342 | /* Write the fixup. */ | |
10343 | if (i != num_recs - 1) | |
10344 | fix->fx_next = &fixes[i + 1]; | |
10345 | else | |
10346 | fix->fx_next = NULL; | |
10347 | fix->fx_size = 4; | |
10348 | fix->fx_done = 0; | |
10349 | fix->fx_frag = fragP; | |
43cd72b9 | 10350 | fix->fx_where = i * (8 + 4); |
e0001a05 NC |
10351 | fix->fx_addsy = section_symbol (cur_block->sec); |
10352 | fix->fx_offset = cur_block->offset; | |
10353 | fix->fx_r_type = BFD_RELOC_32; | |
10354 | fix->fx_file = "Internal Assembly"; | |
10355 | fix->fx_line = 0; | |
10356 | ||
10357 | /* Write the length. */ | |
43cd72b9 | 10358 | md_number_to_chars (&frag_data[4 + (8+4) * i], |
e0001a05 | 10359 | cur_block->size, 4); |
43cd72b9 BW |
10360 | md_number_to_chars (&frag_data[8 + (8+4) * i], |
10361 | frag_flags_to_number (&cur_block->flags), | |
10362 | 4); | |
e0001a05 NC |
10363 | cur_block = cur_block->next; |
10364 | } | |
10365 | } | |
10366 | } | |
10367 | } | |
10368 | } | |
10369 | ||
10370 | ||
7fa3d080 BW |
10371 | static segment_info_type * |
10372 | retrieve_segment_info (segT seg) | |
e0001a05 NC |
10373 | { |
10374 | segment_info_type *seginfo; | |
10375 | seginfo = (segment_info_type *) bfd_get_section_userdata (stdoutput, seg); | |
10376 | if (!seginfo) | |
10377 | { | |
10378 | frchainS *frchainP; | |
10379 | ||
10380 | seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo)); | |
7fa3d080 | 10381 | memset ((void *) seginfo, 0, sizeof (*seginfo)); |
e0001a05 NC |
10382 | seginfo->fix_root = NULL; |
10383 | seginfo->fix_tail = NULL; | |
10384 | seginfo->bfd_section = seg; | |
10385 | seginfo->sym = 0; | |
10386 | /* We will not be dealing with these, only our special ones. */ | |
65ec77d2 | 10387 | bfd_set_section_userdata (stdoutput, seg, (void *) seginfo); |
e0001a05 NC |
10388 | |
10389 | frchainP = (frchainS *) xmalloc (sizeof (frchainS)); | |
10390 | frchainP->frch_root = NULL; | |
10391 | frchainP->frch_last = NULL; | |
10392 | frchainP->frch_next = NULL; | |
e0001a05 NC |
10393 | frchainP->frch_subseg = 0; |
10394 | frchainP->fix_root = NULL; | |
10395 | frchainP->fix_tail = NULL; | |
10396 | /* Do not init the objstack. */ | |
10397 | /* obstack_begin (&frchainP->frch_obstack, chunksize); */ | |
10398 | /* frchainP->frch_frag_now = fragP; */ | |
10399 | frchainP->frch_frag_now = NULL; | |
10400 | ||
10401 | seginfo->frchainP = frchainP; | |
10402 | } | |
10403 | ||
10404 | return seginfo; | |
10405 | } | |
10406 | ||
10407 | ||
7fa3d080 BW |
10408 | static bfd_boolean |
10409 | section_has_property (segT sec, frag_predicate property_function) | |
e0001a05 NC |
10410 | { |
10411 | segment_info_type *seginfo = seg_info (sec); | |
10412 | fragS *fragP; | |
10413 | ||
10414 | if (seginfo && seginfo->frchainP) | |
10415 | { | |
10416 | for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next) | |
10417 | { | |
10418 | if (property_function (fragP) | |
10419 | && (fragP->fr_type != rs_fill || fragP->fr_fix != 0)) | |
10420 | return TRUE; | |
10421 | } | |
10422 | } | |
10423 | return FALSE; | |
10424 | } | |
10425 | ||
10426 | ||
7fa3d080 BW |
10427 | static bfd_boolean |
10428 | section_has_xproperty (segT sec, frag_flags_fn property_function) | |
43cd72b9 BW |
10429 | { |
10430 | segment_info_type *seginfo = seg_info (sec); | |
10431 | fragS *fragP; | |
10432 | ||
10433 | if (seginfo && seginfo->frchainP) | |
10434 | { | |
10435 | for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next) | |
10436 | { | |
10437 | frag_flags prop_flags; | |
10438 | property_function (fragP, &prop_flags); | |
10439 | if (!xtensa_frag_flags_is_empty (&prop_flags)) | |
10440 | return TRUE; | |
10441 | } | |
10442 | } | |
10443 | return FALSE; | |
10444 | } | |
10445 | ||
10446 | ||
e0001a05 NC |
10447 | /* Two types of block sections exist right now: literal and insns. */ |
10448 | ||
7fa3d080 BW |
10449 | static void |
10450 | add_xt_block_frags (segT sec, | |
10451 | segT xt_block_sec, | |
10452 | xtensa_block_info **xt_block, | |
10453 | frag_predicate property_function, | |
10454 | frag_predicate end_property_function) | |
e0001a05 NC |
10455 | { |
10456 | segment_info_type *seg_info; | |
10457 | segment_info_type *xt_seg_info; | |
10458 | bfd_vma seg_offset; | |
10459 | fragS *fragP; | |
10460 | ||
10461 | xt_seg_info = retrieve_segment_info (xt_block_sec); | |
10462 | seg_info = retrieve_segment_info (sec); | |
10463 | ||
10464 | /* Build it if needed. */ | |
10465 | while (*xt_block != NULL) | |
10466 | xt_block = &(*xt_block)->next; | |
10467 | /* We are either at NULL at the beginning or at the end. */ | |
10468 | ||
10469 | /* Walk through the frags. */ | |
10470 | seg_offset = 0; | |
10471 | ||
10472 | if (seg_info->frchainP) | |
10473 | { | |
10474 | for (fragP = seg_info->frchainP->frch_root; | |
10475 | fragP; | |
10476 | fragP = fragP->fr_next) | |
10477 | { | |
10478 | if (property_function (fragP) | |
10479 | && (fragP->fr_type != rs_fill || fragP->fr_fix != 0)) | |
10480 | { | |
10481 | if (*xt_block != NULL) | |
10482 | { | |
10483 | if ((*xt_block)->offset + (*xt_block)->size | |
10484 | == fragP->fr_address) | |
10485 | (*xt_block)->size += fragP->fr_fix; | |
10486 | else | |
10487 | xt_block = &((*xt_block)->next); | |
10488 | } | |
10489 | if (*xt_block == NULL) | |
10490 | { | |
43cd72b9 BW |
10491 | xtensa_block_info *new_block = (xtensa_block_info *) |
10492 | xmalloc (sizeof (xtensa_block_info)); | |
10493 | new_block->sec = sec; | |
10494 | new_block->offset = fragP->fr_address; | |
10495 | new_block->size = fragP->fr_fix; | |
10496 | new_block->next = NULL; | |
10497 | xtensa_frag_flags_init (&new_block->flags); | |
10498 | *xt_block = new_block; | |
10499 | } | |
10500 | if (end_property_function | |
10501 | && end_property_function (fragP)) | |
10502 | { | |
10503 | xt_block = &((*xt_block)->next); | |
10504 | } | |
10505 | } | |
10506 | } | |
10507 | } | |
10508 | } | |
10509 | ||
10510 | ||
10511 | /* Break the encapsulation of add_xt_prop_frags here. */ | |
10512 | ||
7fa3d080 BW |
10513 | static bfd_boolean |
10514 | xtensa_frag_flags_is_empty (const frag_flags *prop_flags) | |
43cd72b9 BW |
10515 | { |
10516 | if (prop_flags->is_literal | |
10517 | || prop_flags->is_insn | |
10518 | || prop_flags->is_data | |
10519 | || prop_flags->is_unreachable) | |
10520 | return FALSE; | |
10521 | return TRUE; | |
10522 | } | |
10523 | ||
10524 | ||
7fa3d080 BW |
10525 | static void |
10526 | xtensa_frag_flags_init (frag_flags *prop_flags) | |
43cd72b9 BW |
10527 | { |
10528 | memset (prop_flags, 0, sizeof (frag_flags)); | |
10529 | } | |
10530 | ||
10531 | ||
7fa3d080 BW |
10532 | static void |
10533 | get_frag_property_flags (const fragS *fragP, frag_flags *prop_flags) | |
43cd72b9 BW |
10534 | { |
10535 | xtensa_frag_flags_init (prop_flags); | |
10536 | if (fragP->tc_frag_data.is_literal) | |
10537 | prop_flags->is_literal = TRUE; | |
10538 | if (fragP->tc_frag_data.is_unreachable) | |
7fa3d080 | 10539 | prop_flags->is_unreachable = TRUE; |
43cd72b9 BW |
10540 | else if (fragP->tc_frag_data.is_insn) |
10541 | { | |
10542 | prop_flags->is_insn = TRUE; | |
10543 | if (fragP->tc_frag_data.is_loop_target) | |
10544 | prop_flags->insn.is_loop_target = TRUE; | |
10545 | if (fragP->tc_frag_data.is_branch_target) | |
10546 | prop_flags->insn.is_branch_target = TRUE; | |
10547 | if (fragP->tc_frag_data.is_specific_opcode | |
10548 | || fragP->tc_frag_data.is_no_transform) | |
10549 | prop_flags->insn.is_no_transform = TRUE; | |
10550 | if (fragP->tc_frag_data.is_no_density) | |
10551 | prop_flags->insn.is_no_density = TRUE; | |
10552 | if (fragP->tc_frag_data.use_absolute_literals) | |
10553 | prop_flags->insn.is_abslit = TRUE; | |
10554 | } | |
10555 | if (fragP->tc_frag_data.is_align) | |
10556 | { | |
10557 | prop_flags->is_align = TRUE; | |
10558 | prop_flags->alignment = fragP->tc_frag_data.alignment; | |
10559 | if (xtensa_frag_flags_is_empty (prop_flags)) | |
10560 | prop_flags->is_data = TRUE; | |
10561 | } | |
10562 | } | |
10563 | ||
10564 | ||
7fa3d080 BW |
10565 | static bfd_vma |
10566 | frag_flags_to_number (const frag_flags *prop_flags) | |
43cd72b9 BW |
10567 | { |
10568 | bfd_vma num = 0; | |
10569 | if (prop_flags->is_literal) | |
10570 | num |= XTENSA_PROP_LITERAL; | |
10571 | if (prop_flags->is_insn) | |
10572 | num |= XTENSA_PROP_INSN; | |
10573 | if (prop_flags->is_data) | |
10574 | num |= XTENSA_PROP_DATA; | |
10575 | if (prop_flags->is_unreachable) | |
10576 | num |= XTENSA_PROP_UNREACHABLE; | |
10577 | if (prop_flags->insn.is_loop_target) | |
10578 | num |= XTENSA_PROP_INSN_LOOP_TARGET; | |
10579 | if (prop_flags->insn.is_branch_target) | |
10580 | { | |
10581 | num |= XTENSA_PROP_INSN_BRANCH_TARGET; | |
10582 | num = SET_XTENSA_PROP_BT_ALIGN (num, prop_flags->insn.bt_align_priority); | |
10583 | } | |
10584 | ||
10585 | if (prop_flags->insn.is_no_density) | |
10586 | num |= XTENSA_PROP_INSN_NO_DENSITY; | |
10587 | if (prop_flags->insn.is_no_transform) | |
10588 | num |= XTENSA_PROP_INSN_NO_TRANSFORM; | |
10589 | if (prop_flags->insn.is_no_reorder) | |
10590 | num |= XTENSA_PROP_INSN_NO_REORDER; | |
10591 | if (prop_flags->insn.is_abslit) | |
10592 | num |= XTENSA_PROP_INSN_ABSLIT; | |
10593 | ||
10594 | if (prop_flags->is_align) | |
10595 | { | |
10596 | num |= XTENSA_PROP_ALIGN; | |
10597 | num = SET_XTENSA_PROP_ALIGNMENT (num, prop_flags->alignment); | |
10598 | } | |
10599 | ||
10600 | return num; | |
10601 | } | |
10602 | ||
10603 | ||
10604 | static bfd_boolean | |
7fa3d080 BW |
10605 | xtensa_frag_flags_combinable (const frag_flags *prop_flags_1, |
10606 | const frag_flags *prop_flags_2) | |
43cd72b9 BW |
10607 | { |
10608 | /* Cannot combine with an end marker. */ | |
10609 | ||
10610 | if (prop_flags_1->is_literal != prop_flags_2->is_literal) | |
10611 | return FALSE; | |
10612 | if (prop_flags_1->is_insn != prop_flags_2->is_insn) | |
10613 | return FALSE; | |
10614 | if (prop_flags_1->is_data != prop_flags_2->is_data) | |
10615 | return FALSE; | |
10616 | ||
10617 | if (prop_flags_1->is_insn) | |
10618 | { | |
10619 | /* Properties of the beginning of the frag. */ | |
10620 | if (prop_flags_2->insn.is_loop_target) | |
10621 | return FALSE; | |
10622 | if (prop_flags_2->insn.is_branch_target) | |
10623 | return FALSE; | |
10624 | if (prop_flags_1->insn.is_no_density != | |
10625 | prop_flags_2->insn.is_no_density) | |
10626 | return FALSE; | |
10627 | if (prop_flags_1->insn.is_no_transform != | |
10628 | prop_flags_2->insn.is_no_transform) | |
10629 | return FALSE; | |
10630 | if (prop_flags_1->insn.is_no_reorder != | |
10631 | prop_flags_2->insn.is_no_reorder) | |
10632 | return FALSE; | |
10633 | if (prop_flags_1->insn.is_abslit != | |
10634 | prop_flags_2->insn.is_abslit) | |
10635 | return FALSE; | |
10636 | } | |
10637 | ||
10638 | if (prop_flags_1->is_align) | |
10639 | return FALSE; | |
10640 | ||
10641 | return TRUE; | |
10642 | } | |
10643 | ||
10644 | ||
7fa3d080 BW |
10645 | static bfd_vma |
10646 | xt_block_aligned_size (const xtensa_block_info *xt_block) | |
43cd72b9 BW |
10647 | { |
10648 | bfd_vma end_addr; | |
d77b99c9 | 10649 | unsigned align_bits; |
43cd72b9 BW |
10650 | |
10651 | if (!xt_block->flags.is_align) | |
10652 | return xt_block->size; | |
10653 | ||
10654 | end_addr = xt_block->offset + xt_block->size; | |
10655 | align_bits = xt_block->flags.alignment; | |
10656 | end_addr = ((end_addr + ((1 << align_bits) -1)) >> align_bits) << align_bits; | |
10657 | return end_addr - xt_block->offset; | |
10658 | } | |
10659 | ||
10660 | ||
10661 | static bfd_boolean | |
7fa3d080 BW |
10662 | xtensa_xt_block_combine (xtensa_block_info *xt_block, |
10663 | const xtensa_block_info *xt_block_2) | |
43cd72b9 BW |
10664 | { |
10665 | if (xt_block->sec != xt_block_2->sec) | |
10666 | return FALSE; | |
10667 | if (xt_block->offset + xt_block_aligned_size (xt_block) | |
10668 | != xt_block_2->offset) | |
10669 | return FALSE; | |
10670 | ||
10671 | if (xt_block_2->size == 0 | |
10672 | && (!xt_block_2->flags.is_unreachable | |
10673 | || xt_block->flags.is_unreachable)) | |
10674 | { | |
10675 | if (xt_block_2->flags.is_align | |
10676 | && xt_block->flags.is_align) | |
10677 | { | |
10678 | /* Nothing needed. */ | |
10679 | if (xt_block->flags.alignment >= xt_block_2->flags.alignment) | |
10680 | return TRUE; | |
10681 | } | |
10682 | else | |
10683 | { | |
10684 | if (xt_block_2->flags.is_align) | |
10685 | { | |
10686 | /* Push alignment to previous entry. */ | |
10687 | xt_block->flags.is_align = xt_block_2->flags.is_align; | |
10688 | xt_block->flags.alignment = xt_block_2->flags.alignment; | |
10689 | } | |
10690 | return TRUE; | |
10691 | } | |
10692 | } | |
10693 | if (!xtensa_frag_flags_combinable (&xt_block->flags, | |
10694 | &xt_block_2->flags)) | |
10695 | return FALSE; | |
10696 | ||
10697 | xt_block->size += xt_block_2->size; | |
10698 | ||
10699 | if (xt_block_2->flags.is_align) | |
10700 | { | |
10701 | xt_block->flags.is_align = TRUE; | |
10702 | xt_block->flags.alignment = xt_block_2->flags.alignment; | |
10703 | } | |
10704 | ||
10705 | return TRUE; | |
10706 | } | |
10707 | ||
10708 | ||
7fa3d080 BW |
10709 | static void |
10710 | add_xt_prop_frags (segT sec, | |
10711 | segT xt_block_sec, | |
10712 | xtensa_block_info **xt_block, | |
10713 | frag_flags_fn property_function) | |
43cd72b9 BW |
10714 | { |
10715 | segment_info_type *seg_info; | |
10716 | segment_info_type *xt_seg_info; | |
10717 | bfd_vma seg_offset; | |
10718 | fragS *fragP; | |
10719 | ||
10720 | xt_seg_info = retrieve_segment_info (xt_block_sec); | |
10721 | seg_info = retrieve_segment_info (sec); | |
10722 | /* Build it if needed. */ | |
10723 | while (*xt_block != NULL) | |
10724 | { | |
10725 | xt_block = &(*xt_block)->next; | |
10726 | } | |
10727 | /* We are either at NULL at the beginning or at the end. */ | |
10728 | ||
10729 | /* Walk through the frags. */ | |
10730 | seg_offset = 0; | |
10731 | ||
10732 | if (seg_info->frchainP) | |
10733 | { | |
10734 | for (fragP = seg_info->frchainP->frch_root; fragP; | |
10735 | fragP = fragP->fr_next) | |
10736 | { | |
10737 | xtensa_block_info tmp_block; | |
10738 | tmp_block.sec = sec; | |
10739 | tmp_block.offset = fragP->fr_address; | |
10740 | tmp_block.size = fragP->fr_fix; | |
10741 | tmp_block.next = NULL; | |
10742 | property_function (fragP, &tmp_block.flags); | |
10743 | ||
10744 | if (!xtensa_frag_flags_is_empty (&tmp_block.flags)) | |
10745 | /* && fragP->fr_fix != 0) */ | |
10746 | { | |
10747 | if ((*xt_block) == NULL | |
10748 | || !xtensa_xt_block_combine (*xt_block, &tmp_block)) | |
10749 | { | |
10750 | xtensa_block_info *new_block; | |
10751 | if ((*xt_block) != NULL) | |
10752 | xt_block = &(*xt_block)->next; | |
10753 | new_block = (xtensa_block_info *) | |
10754 | xmalloc (sizeof (xtensa_block_info)); | |
10755 | *new_block = tmp_block; | |
10756 | *xt_block = new_block; | |
10757 | } | |
10758 | } | |
10759 | } | |
10760 | } | |
10761 | } | |
10762 | ||
10763 | \f | |
10764 | /* op_placement_info_table */ | |
10765 | ||
10766 | /* op_placement_info makes it easier to determine which | |
10767 | ops can go in which slots. */ | |
10768 | ||
10769 | static void | |
7fa3d080 | 10770 | init_op_placement_info_table (void) |
43cd72b9 BW |
10771 | { |
10772 | xtensa_isa isa = xtensa_default_isa; | |
10773 | xtensa_insnbuf ibuf = xtensa_insnbuf_alloc (isa); | |
10774 | xtensa_opcode opcode; | |
10775 | xtensa_format fmt; | |
10776 | int slot; | |
10777 | int num_opcodes = xtensa_isa_num_opcodes (isa); | |
10778 | ||
10779 | op_placement_table = (op_placement_info_table) | |
10780 | xmalloc (sizeof (op_placement_info) * num_opcodes); | |
10781 | assert (xtensa_isa_num_formats (isa) < MAX_FORMATS); | |
10782 | ||
10783 | for (opcode = 0; opcode < num_opcodes; opcode++) | |
10784 | { | |
10785 | op_placement_info *opi = &op_placement_table[opcode]; | |
10786 | /* FIXME: Make tinsn allocation dynamic. */ | |
10787 | if (xtensa_opcode_num_operands (isa, opcode) >= MAX_INSN_ARGS) | |
10788 | as_fatal (_("too many operands in instruction")); | |
43cd72b9 BW |
10789 | opi->narrowest = XTENSA_UNDEFINED; |
10790 | opi->narrowest_size = 0x7F; | |
b2d179be | 10791 | opi->narrowest_slot = 0; |
43cd72b9 BW |
10792 | opi->formats = 0; |
10793 | opi->num_formats = 0; | |
10794 | opi->issuef = 0; | |
10795 | for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++) | |
10796 | { | |
10797 | opi->slots[fmt] = 0; | |
10798 | for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++) | |
10799 | { | |
10800 | if (xtensa_opcode_encode (isa, fmt, slot, ibuf, opcode) == 0) | |
10801 | { | |
10802 | int fmt_length = xtensa_format_length (isa, fmt); | |
10803 | opi->issuef++; | |
10804 | set_bit (fmt, opi->formats); | |
10805 | set_bit (slot, opi->slots[fmt]); | |
a02728c8 BW |
10806 | if (fmt_length < opi->narrowest_size |
10807 | || (fmt_length == opi->narrowest_size | |
10808 | && (xtensa_format_num_slots (isa, fmt) | |
10809 | < xtensa_format_num_slots (isa, | |
10810 | opi->narrowest)))) | |
43cd72b9 BW |
10811 | { |
10812 | opi->narrowest = fmt; | |
10813 | opi->narrowest_size = fmt_length; | |
b2d179be | 10814 | opi->narrowest_slot = slot; |
43cd72b9 | 10815 | } |
e0001a05 NC |
10816 | } |
10817 | } | |
43cd72b9 BW |
10818 | if (opi->formats) |
10819 | opi->num_formats++; | |
e0001a05 NC |
10820 | } |
10821 | } | |
43cd72b9 BW |
10822 | xtensa_insnbuf_free (isa, ibuf); |
10823 | } | |
10824 | ||
10825 | ||
10826 | bfd_boolean | |
7fa3d080 | 10827 | opcode_fits_format_slot (xtensa_opcode opcode, xtensa_format fmt, int slot) |
43cd72b9 BW |
10828 | { |
10829 | return bit_is_set (slot, op_placement_table[opcode].slots[fmt]); | |
10830 | } | |
10831 | ||
10832 | ||
10833 | /* If the opcode is available in a single slot format, return its size. */ | |
10834 | ||
7fa3d080 BW |
10835 | static int |
10836 | xg_get_single_size (xtensa_opcode opcode) | |
43cd72b9 | 10837 | { |
b2d179be | 10838 | return op_placement_table[opcode].narrowest_size; |
43cd72b9 BW |
10839 | } |
10840 | ||
10841 | ||
7fa3d080 BW |
10842 | static xtensa_format |
10843 | xg_get_single_format (xtensa_opcode opcode) | |
43cd72b9 | 10844 | { |
b2d179be BW |
10845 | return op_placement_table[opcode].narrowest; |
10846 | } | |
10847 | ||
10848 | ||
10849 | static int | |
10850 | xg_get_single_slot (xtensa_opcode opcode) | |
10851 | { | |
10852 | return op_placement_table[opcode].narrowest_slot; | |
e0001a05 NC |
10853 | } |
10854 | ||
10855 | \f | |
10856 | /* Instruction Stack Functions (from "xtensa-istack.h"). */ | |
10857 | ||
10858 | void | |
7fa3d080 | 10859 | istack_init (IStack *stack) |
e0001a05 NC |
10860 | { |
10861 | memset (stack, 0, sizeof (IStack)); | |
10862 | stack->ninsn = 0; | |
10863 | } | |
10864 | ||
10865 | ||
10866 | bfd_boolean | |
7fa3d080 | 10867 | istack_empty (IStack *stack) |
e0001a05 NC |
10868 | { |
10869 | return (stack->ninsn == 0); | |
10870 | } | |
10871 | ||
10872 | ||
10873 | bfd_boolean | |
7fa3d080 | 10874 | istack_full (IStack *stack) |
e0001a05 NC |
10875 | { |
10876 | return (stack->ninsn == MAX_ISTACK); | |
10877 | } | |
10878 | ||
10879 | ||
10880 | /* Return a pointer to the top IStack entry. | |
43cd72b9 | 10881 | It is an error to call this if istack_empty () is TRUE. */ |
e0001a05 NC |
10882 | |
10883 | TInsn * | |
7fa3d080 | 10884 | istack_top (IStack *stack) |
e0001a05 NC |
10885 | { |
10886 | int rec = stack->ninsn - 1; | |
10887 | assert (!istack_empty (stack)); | |
10888 | return &stack->insn[rec]; | |
10889 | } | |
10890 | ||
10891 | ||
10892 | /* Add a new TInsn to an IStack. | |
43cd72b9 | 10893 | It is an error to call this if istack_full () is TRUE. */ |
e0001a05 NC |
10894 | |
10895 | void | |
7fa3d080 | 10896 | istack_push (IStack *stack, TInsn *insn) |
e0001a05 NC |
10897 | { |
10898 | int rec = stack->ninsn; | |
10899 | assert (!istack_full (stack)); | |
43cd72b9 | 10900 | stack->insn[rec] = *insn; |
e0001a05 NC |
10901 | stack->ninsn++; |
10902 | } | |
10903 | ||
10904 | ||
10905 | /* Clear space for the next TInsn on the IStack and return a pointer | |
43cd72b9 | 10906 | to it. It is an error to call this if istack_full () is TRUE. */ |
e0001a05 NC |
10907 | |
10908 | TInsn * | |
7fa3d080 | 10909 | istack_push_space (IStack *stack) |
e0001a05 NC |
10910 | { |
10911 | int rec = stack->ninsn; | |
10912 | TInsn *insn; | |
10913 | assert (!istack_full (stack)); | |
10914 | insn = &stack->insn[rec]; | |
10915 | memset (insn, 0, sizeof (TInsn)); | |
10916 | stack->ninsn++; | |
10917 | return insn; | |
10918 | } | |
10919 | ||
10920 | ||
10921 | /* Remove the last pushed instruction. It is an error to call this if | |
43cd72b9 | 10922 | istack_empty () returns TRUE. */ |
e0001a05 NC |
10923 | |
10924 | void | |
7fa3d080 | 10925 | istack_pop (IStack *stack) |
e0001a05 NC |
10926 | { |
10927 | int rec = stack->ninsn - 1; | |
10928 | assert (!istack_empty (stack)); | |
10929 | stack->ninsn--; | |
10930 | memset (&stack->insn[rec], 0, sizeof (TInsn)); | |
10931 | } | |
10932 | ||
10933 | \f | |
10934 | /* TInsn functions. */ | |
10935 | ||
10936 | void | |
7fa3d080 | 10937 | tinsn_init (TInsn *dst) |
e0001a05 NC |
10938 | { |
10939 | memset (dst, 0, sizeof (TInsn)); | |
10940 | } | |
10941 | ||
10942 | ||
e0001a05 NC |
10943 | /* Get the ``num''th token of the TInsn. |
10944 | It is illegal to call this if num > insn->ntoks. */ | |
10945 | ||
10946 | expressionS * | |
7fa3d080 | 10947 | tinsn_get_tok (TInsn *insn, int num) |
e0001a05 NC |
10948 | { |
10949 | assert (num < insn->ntok); | |
10950 | return &insn->tok[num]; | |
10951 | } | |
10952 | ||
10953 | ||
43cd72b9 | 10954 | /* Return TRUE if ANY of the operands in the insn are symbolic. */ |
e0001a05 NC |
10955 | |
10956 | static bfd_boolean | |
7fa3d080 | 10957 | tinsn_has_symbolic_operands (const TInsn *insn) |
e0001a05 NC |
10958 | { |
10959 | int i; | |
10960 | int n = insn->ntok; | |
10961 | ||
10962 | assert (insn->insn_type == ITYPE_INSN); | |
10963 | ||
10964 | for (i = 0; i < n; ++i) | |
10965 | { | |
10966 | switch (insn->tok[i].X_op) | |
10967 | { | |
10968 | case O_register: | |
10969 | case O_constant: | |
10970 | break; | |
10971 | default: | |
10972 | return TRUE; | |
10973 | } | |
10974 | } | |
10975 | return FALSE; | |
10976 | } | |
10977 | ||
10978 | ||
10979 | bfd_boolean | |
7fa3d080 | 10980 | tinsn_has_invalid_symbolic_operands (const TInsn *insn) |
e0001a05 | 10981 | { |
43cd72b9 | 10982 | xtensa_isa isa = xtensa_default_isa; |
e0001a05 NC |
10983 | int i; |
10984 | int n = insn->ntok; | |
10985 | ||
10986 | assert (insn->insn_type == ITYPE_INSN); | |
10987 | ||
10988 | for (i = 0; i < n; ++i) | |
10989 | { | |
10990 | switch (insn->tok[i].X_op) | |
10991 | { | |
10992 | case O_register: | |
10993 | case O_constant: | |
10994 | break; | |
43cd72b9 BW |
10995 | case O_big: |
10996 | case O_illegal: | |
10997 | case O_absent: | |
10998 | /* Errors for these types are caught later. */ | |
10999 | break; | |
11000 | case O_hi16: | |
11001 | case O_lo16: | |
e0001a05 | 11002 | default: |
43cd72b9 BW |
11003 | /* Symbolic immediates are only allowed on the last immediate |
11004 | operand. At this time, CONST16 is the only opcode where we | |
e7da6241 | 11005 | support non-PC-relative relocations. */ |
43cd72b9 BW |
11006 | if (i != get_relaxable_immed (insn->opcode) |
11007 | || (xtensa_operand_is_PCrelative (isa, insn->opcode, i) != 1 | |
11008 | && insn->opcode != xtensa_const16_opcode)) | |
11009 | { | |
431ad2d0 | 11010 | as_bad (_("invalid symbolic operand")); |
43cd72b9 BW |
11011 | return TRUE; |
11012 | } | |
e0001a05 NC |
11013 | } |
11014 | } | |
11015 | return FALSE; | |
11016 | } | |
11017 | ||
11018 | ||
11019 | /* For assembly code with complex expressions (e.g. subtraction), | |
11020 | we have to build them in the literal pool so that | |
11021 | their results are calculated correctly after relaxation. | |
11022 | The relaxation only handles expressions that | |
11023 | boil down to SYMBOL + OFFSET. */ | |
11024 | ||
11025 | static bfd_boolean | |
7fa3d080 | 11026 | tinsn_has_complex_operands (const TInsn *insn) |
e0001a05 NC |
11027 | { |
11028 | int i; | |
11029 | int n = insn->ntok; | |
11030 | assert (insn->insn_type == ITYPE_INSN); | |
11031 | for (i = 0; i < n; ++i) | |
11032 | { | |
11033 | switch (insn->tok[i].X_op) | |
11034 | { | |
11035 | case O_register: | |
11036 | case O_constant: | |
11037 | case O_symbol: | |
43cd72b9 BW |
11038 | case O_lo16: |
11039 | case O_hi16: | |
e0001a05 NC |
11040 | break; |
11041 | default: | |
11042 | return TRUE; | |
11043 | } | |
11044 | } | |
11045 | return FALSE; | |
11046 | } | |
11047 | ||
11048 | ||
b2d179be BW |
11049 | /* Encode a TInsn opcode and its constant operands into slotbuf. |
11050 | Return TRUE if there is a symbol in the immediate field. This | |
11051 | function assumes that: | |
11052 | 1) The number of operands are correct. | |
11053 | 2) The insn_type is ITYPE_INSN. | |
11054 | 3) The opcode can be encoded in the specified format and slot. | |
11055 | 4) Operands are either O_constant or O_symbol, and all constants fit. */ | |
43cd72b9 BW |
11056 | |
11057 | static bfd_boolean | |
7fa3d080 BW |
11058 | tinsn_to_slotbuf (xtensa_format fmt, |
11059 | int slot, | |
11060 | TInsn *tinsn, | |
11061 | xtensa_insnbuf slotbuf) | |
43cd72b9 BW |
11062 | { |
11063 | xtensa_isa isa = xtensa_default_isa; | |
11064 | xtensa_opcode opcode = tinsn->opcode; | |
11065 | bfd_boolean has_fixup = FALSE; | |
11066 | int noperands = xtensa_opcode_num_operands (isa, opcode); | |
11067 | int i; | |
11068 | ||
43cd72b9 BW |
11069 | assert (tinsn->insn_type == ITYPE_INSN); |
11070 | if (noperands != tinsn->ntok) | |
11071 | as_fatal (_("operand number mismatch")); | |
11072 | ||
11073 | if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opcode)) | |
11074 | { | |
11075 | as_bad (_("cannot encode opcode \"%s\" in the given format \"%s\""), | |
11076 | xtensa_opcode_name (isa, opcode), xtensa_format_name (isa, fmt)); | |
11077 | return FALSE; | |
11078 | } | |
11079 | ||
11080 | for (i = 0; i < noperands; i++) | |
11081 | { | |
11082 | expressionS *expr = &tinsn->tok[i]; | |
d77b99c9 BW |
11083 | int rc; |
11084 | unsigned line; | |
43cd72b9 BW |
11085 | char *file_name; |
11086 | uint32 opnd_value; | |
11087 | ||
11088 | switch (expr->X_op) | |
11089 | { | |
11090 | case O_register: | |
11091 | if (xtensa_operand_is_visible (isa, opcode, i) == 0) | |
11092 | break; | |
11093 | /* The register number has already been checked in | |
11094 | expression_maybe_register, so we don't need to check here. */ | |
11095 | opnd_value = expr->X_add_number; | |
11096 | (void) xtensa_operand_encode (isa, opcode, i, &opnd_value); | |
11097 | rc = xtensa_operand_set_field (isa, opcode, i, fmt, slot, slotbuf, | |
11098 | opnd_value); | |
11099 | if (rc != 0) | |
11100 | as_warn (_("xtensa-isa failure: %s"), xtensa_isa_error_msg (isa)); | |
11101 | break; | |
11102 | ||
11103 | case O_constant: | |
11104 | if (xtensa_operand_is_visible (isa, opcode, i) == 0) | |
11105 | break; | |
11106 | as_where (&file_name, &line); | |
11107 | /* It is a constant and we called this function | |
11108 | then we have to try to fit it. */ | |
11109 | xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode, i, | |
e0001a05 NC |
11110 | expr->X_add_number, file_name, line); |
11111 | break; | |
11112 | ||
e0001a05 NC |
11113 | default: |
11114 | has_fixup = TRUE; | |
11115 | break; | |
11116 | } | |
11117 | } | |
43cd72b9 | 11118 | |
e0001a05 NC |
11119 | return has_fixup; |
11120 | } | |
11121 | ||
11122 | ||
b2d179be BW |
11123 | /* Encode a single TInsn into an insnbuf. If the opcode can only be encoded |
11124 | into a multi-slot instruction, fill the other slots with NOPs. | |
11125 | Return TRUE if there is a symbol in the immediate field. See also the | |
11126 | assumptions listed for tinsn_to_slotbuf. */ | |
11127 | ||
11128 | static bfd_boolean | |
11129 | tinsn_to_insnbuf (TInsn *tinsn, xtensa_insnbuf insnbuf) | |
11130 | { | |
11131 | static xtensa_insnbuf slotbuf = 0; | |
11132 | static vliw_insn vinsn; | |
11133 | xtensa_isa isa = xtensa_default_isa; | |
11134 | bfd_boolean has_fixup = FALSE; | |
11135 | int i; | |
11136 | ||
11137 | if (!slotbuf) | |
11138 | { | |
11139 | slotbuf = xtensa_insnbuf_alloc (isa); | |
11140 | xg_init_vinsn (&vinsn); | |
11141 | } | |
11142 | ||
11143 | xg_clear_vinsn (&vinsn); | |
11144 | ||
11145 | bundle_tinsn (tinsn, &vinsn); | |
11146 | ||
11147 | xtensa_format_encode (isa, vinsn.format, insnbuf); | |
11148 | ||
11149 | for (i = 0; i < vinsn.num_slots; i++) | |
11150 | { | |
11151 | /* Only one slot may have a fix-up because the rest contains NOPs. */ | |
11152 | has_fixup |= | |
11153 | tinsn_to_slotbuf (vinsn.format, i, &vinsn.slots[i], vinsn.slotbuf[i]); | |
11154 | xtensa_format_set_slot (isa, vinsn.format, i, insnbuf, vinsn.slotbuf[i]); | |
11155 | } | |
11156 | ||
11157 | return has_fixup; | |
11158 | } | |
11159 | ||
11160 | ||
43cd72b9 | 11161 | /* Check the instruction arguments. Return TRUE on failure. */ |
e0001a05 | 11162 | |
7fa3d080 BW |
11163 | static bfd_boolean |
11164 | tinsn_check_arguments (const TInsn *insn) | |
e0001a05 NC |
11165 | { |
11166 | xtensa_isa isa = xtensa_default_isa; | |
11167 | xtensa_opcode opcode = insn->opcode; | |
11168 | ||
11169 | if (opcode == XTENSA_UNDEFINED) | |
11170 | { | |
11171 | as_bad (_("invalid opcode")); | |
11172 | return TRUE; | |
11173 | } | |
11174 | ||
43cd72b9 | 11175 | if (xtensa_opcode_num_operands (isa, opcode) > insn->ntok) |
e0001a05 NC |
11176 | { |
11177 | as_bad (_("too few operands")); | |
11178 | return TRUE; | |
11179 | } | |
11180 | ||
43cd72b9 | 11181 | if (xtensa_opcode_num_operands (isa, opcode) < insn->ntok) |
e0001a05 NC |
11182 | { |
11183 | as_bad (_("too many operands")); | |
11184 | return TRUE; | |
11185 | } | |
11186 | return FALSE; | |
11187 | } | |
11188 | ||
11189 | ||
11190 | /* Load an instruction from its encoded form. */ | |
11191 | ||
11192 | static void | |
7fa3d080 | 11193 | tinsn_from_chars (TInsn *tinsn, char *f, int slot) |
e0001a05 | 11194 | { |
43cd72b9 | 11195 | vliw_insn vinsn; |
e0001a05 | 11196 | |
43cd72b9 BW |
11197 | xg_init_vinsn (&vinsn); |
11198 | vinsn_from_chars (&vinsn, f); | |
11199 | ||
11200 | *tinsn = vinsn.slots[slot]; | |
11201 | xg_free_vinsn (&vinsn); | |
11202 | } | |
e0001a05 | 11203 | |
43cd72b9 BW |
11204 | |
11205 | static void | |
7fa3d080 BW |
11206 | tinsn_from_insnbuf (TInsn *tinsn, |
11207 | xtensa_insnbuf slotbuf, | |
11208 | xtensa_format fmt, | |
11209 | int slot) | |
43cd72b9 BW |
11210 | { |
11211 | int i; | |
11212 | xtensa_isa isa = xtensa_default_isa; | |
e0001a05 NC |
11213 | |
11214 | /* Find the immed. */ | |
43cd72b9 BW |
11215 | tinsn_init (tinsn); |
11216 | tinsn->insn_type = ITYPE_INSN; | |
11217 | tinsn->is_specific_opcode = FALSE; /* must not be specific */ | |
11218 | tinsn->opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf); | |
11219 | tinsn->ntok = xtensa_opcode_num_operands (isa, tinsn->opcode); | |
11220 | for (i = 0; i < tinsn->ntok; i++) | |
e0001a05 | 11221 | { |
43cd72b9 BW |
11222 | set_expr_const (&tinsn->tok[i], |
11223 | xtensa_insnbuf_get_operand (slotbuf, fmt, slot, | |
11224 | tinsn->opcode, i)); | |
e0001a05 NC |
11225 | } |
11226 | } | |
11227 | ||
11228 | ||
11229 | /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */ | |
11230 | ||
11231 | static void | |
7fa3d080 | 11232 | tinsn_immed_from_frag (TInsn *tinsn, fragS *fragP, int slot) |
e0001a05 | 11233 | { |
43cd72b9 | 11234 | xtensa_opcode opcode = tinsn->opcode; |
e0001a05 NC |
11235 | int opnum; |
11236 | ||
43cd72b9 | 11237 | if (fragP->tc_frag_data.slot_symbols[slot]) |
e0001a05 NC |
11238 | { |
11239 | opnum = get_relaxable_immed (opcode); | |
43cd72b9 | 11240 | assert (opnum >= 0); |
e7da6241 BW |
11241 | set_expr_symbol_offset (&tinsn->tok[opnum], |
11242 | fragP->tc_frag_data.slot_symbols[slot], | |
11243 | fragP->tc_frag_data.slot_offsets[slot]); | |
e0001a05 NC |
11244 | } |
11245 | } | |
11246 | ||
11247 | ||
11248 | static int | |
7fa3d080 | 11249 | get_num_stack_text_bytes (IStack *istack) |
e0001a05 NC |
11250 | { |
11251 | int i; | |
11252 | int text_bytes = 0; | |
11253 | ||
11254 | for (i = 0; i < istack->ninsn; i++) | |
11255 | { | |
43cd72b9 BW |
11256 | TInsn *tinsn = &istack->insn[i]; |
11257 | if (tinsn->insn_type == ITYPE_INSN) | |
11258 | text_bytes += xg_get_single_size (tinsn->opcode); | |
e0001a05 NC |
11259 | } |
11260 | return text_bytes; | |
11261 | } | |
11262 | ||
11263 | ||
11264 | static int | |
7fa3d080 | 11265 | get_num_stack_literal_bytes (IStack *istack) |
e0001a05 NC |
11266 | { |
11267 | int i; | |
11268 | int lit_bytes = 0; | |
11269 | ||
11270 | for (i = 0; i < istack->ninsn; i++) | |
11271 | { | |
43cd72b9 BW |
11272 | TInsn *tinsn = &istack->insn[i]; |
11273 | if (tinsn->insn_type == ITYPE_LITERAL && tinsn->ntok == 1) | |
e0001a05 NC |
11274 | lit_bytes += 4; |
11275 | } | |
11276 | return lit_bytes; | |
11277 | } | |
11278 | ||
43cd72b9 BW |
11279 | \f |
11280 | /* vliw_insn functions. */ | |
11281 | ||
7fa3d080 BW |
11282 | static void |
11283 | xg_init_vinsn (vliw_insn *v) | |
43cd72b9 BW |
11284 | { |
11285 | int i; | |
11286 | xtensa_isa isa = xtensa_default_isa; | |
11287 | ||
11288 | xg_clear_vinsn (v); | |
11289 | ||
11290 | v->insnbuf = xtensa_insnbuf_alloc (isa); | |
11291 | if (v->insnbuf == NULL) | |
11292 | as_fatal (_("out of memory")); | |
11293 | ||
11294 | for (i = 0; i < MAX_SLOTS; i++) | |
11295 | { | |
43cd72b9 BW |
11296 | v->slotbuf[i] = xtensa_insnbuf_alloc (isa); |
11297 | if (v->slotbuf[i] == NULL) | |
11298 | as_fatal (_("out of memory")); | |
11299 | } | |
11300 | } | |
11301 | ||
11302 | ||
7fa3d080 BW |
11303 | static void |
11304 | xg_clear_vinsn (vliw_insn *v) | |
43cd72b9 BW |
11305 | { |
11306 | int i; | |
65738a7d BW |
11307 | |
11308 | memset (v, 0, offsetof (vliw_insn, insnbuf)); | |
11309 | ||
43cd72b9 BW |
11310 | v->format = XTENSA_UNDEFINED; |
11311 | v->num_slots = 0; | |
11312 | v->inside_bundle = FALSE; | |
11313 | ||
11314 | if (xt_saved_debug_type != DEBUG_NONE) | |
11315 | debug_type = xt_saved_debug_type; | |
11316 | ||
11317 | for (i = 0; i < MAX_SLOTS; i++) | |
65738a7d | 11318 | v->slots[i].opcode = XTENSA_UNDEFINED; |
43cd72b9 BW |
11319 | } |
11320 | ||
11321 | ||
7fa3d080 BW |
11322 | static bfd_boolean |
11323 | vinsn_has_specific_opcodes (vliw_insn *v) | |
43cd72b9 BW |
11324 | { |
11325 | int i; | |
c138bc38 | 11326 | |
43cd72b9 BW |
11327 | for (i = 0; i < v->num_slots; i++) |
11328 | { | |
11329 | if (v->slots[i].is_specific_opcode) | |
11330 | return TRUE; | |
11331 | } | |
11332 | return FALSE; | |
11333 | } | |
11334 | ||
11335 | ||
7fa3d080 BW |
11336 | static void |
11337 | xg_free_vinsn (vliw_insn *v) | |
43cd72b9 BW |
11338 | { |
11339 | int i; | |
11340 | xtensa_insnbuf_free (xtensa_default_isa, v->insnbuf); | |
11341 | for (i = 0; i < MAX_SLOTS; i++) | |
11342 | xtensa_insnbuf_free (xtensa_default_isa, v->slotbuf[i]); | |
11343 | } | |
11344 | ||
11345 | ||
e7da6241 BW |
11346 | /* Encode a vliw_insn into an insnbuf. Return TRUE if there are any symbolic |
11347 | operands. See also the assumptions listed for tinsn_to_slotbuf. */ | |
43cd72b9 BW |
11348 | |
11349 | static bfd_boolean | |
7fa3d080 BW |
11350 | vinsn_to_insnbuf (vliw_insn *vinsn, |
11351 | char *frag_offset, | |
11352 | fragS *fragP, | |
11353 | bfd_boolean record_fixup) | |
43cd72b9 BW |
11354 | { |
11355 | xtensa_isa isa = xtensa_default_isa; | |
11356 | xtensa_format fmt = vinsn->format; | |
11357 | xtensa_insnbuf insnbuf = vinsn->insnbuf; | |
11358 | int slot; | |
11359 | bfd_boolean has_fixup = FALSE; | |
11360 | ||
11361 | xtensa_format_encode (isa, fmt, insnbuf); | |
11362 | ||
11363 | for (slot = 0; slot < vinsn->num_slots; slot++) | |
11364 | { | |
11365 | TInsn *tinsn = &vinsn->slots[slot]; | |
11366 | bfd_boolean tinsn_has_fixup = | |
11367 | tinsn_to_slotbuf (vinsn->format, slot, tinsn, | |
11368 | vinsn->slotbuf[slot]); | |
11369 | ||
11370 | xtensa_format_set_slot (isa, fmt, slot, | |
11371 | insnbuf, vinsn->slotbuf[slot]); | |
e7da6241 | 11372 | if (tinsn_has_fixup) |
43cd72b9 BW |
11373 | { |
11374 | int i; | |
11375 | xtensa_opcode opcode = tinsn->opcode; | |
11376 | int noperands = xtensa_opcode_num_operands (isa, opcode); | |
11377 | has_fixup = TRUE; | |
11378 | ||
11379 | for (i = 0; i < noperands; i++) | |
11380 | { | |
11381 | expressionS* expr = &tinsn->tok[i]; | |
11382 | switch (expr->X_op) | |
11383 | { | |
11384 | case O_symbol: | |
11385 | case O_lo16: | |
11386 | case O_hi16: | |
11387 | if (get_relaxable_immed (opcode) == i) | |
11388 | { | |
e7da6241 BW |
11389 | /* Add a fix record for the instruction, except if this |
11390 | function is being called prior to relaxation, i.e., | |
11391 | if record_fixup is false, and the instruction might | |
11392 | be relaxed later. */ | |
11393 | if (record_fixup | |
11394 | || tinsn->is_specific_opcode | |
11395 | || !xg_is_relaxable_insn (tinsn, 0)) | |
43cd72b9 | 11396 | { |
e7da6241 BW |
11397 | xg_add_opcode_fix (tinsn, i, fmt, slot, expr, fragP, |
11398 | frag_offset - fragP->fr_literal); | |
43cd72b9 BW |
11399 | } |
11400 | else | |
11401 | { | |
e7da6241 BW |
11402 | if (expr->X_op != O_symbol) |
11403 | as_bad (_("invalid operand")); | |
43cd72b9 BW |
11404 | tinsn->symbol = expr->X_add_symbol; |
11405 | tinsn->offset = expr->X_add_number; | |
11406 | } | |
11407 | } | |
11408 | else | |
e7da6241 | 11409 | as_bad (_("symbolic operand not allowed")); |
43cd72b9 BW |
11410 | break; |
11411 | ||
11412 | case O_constant: | |
11413 | case O_register: | |
11414 | break; | |
11415 | ||
43cd72b9 | 11416 | default: |
e7da6241 | 11417 | as_bad (_("expression too complex")); |
43cd72b9 BW |
11418 | break; |
11419 | } | |
11420 | } | |
11421 | } | |
11422 | } | |
11423 | ||
11424 | return has_fixup; | |
11425 | } | |
11426 | ||
11427 | ||
11428 | static void | |
7fa3d080 | 11429 | vinsn_from_chars (vliw_insn *vinsn, char *f) |
43cd72b9 BW |
11430 | { |
11431 | static xtensa_insnbuf insnbuf = NULL; | |
11432 | static xtensa_insnbuf slotbuf = NULL; | |
11433 | int i; | |
11434 | xtensa_format fmt; | |
11435 | xtensa_isa isa = xtensa_default_isa; | |
11436 | ||
11437 | if (!insnbuf) | |
11438 | { | |
11439 | insnbuf = xtensa_insnbuf_alloc (isa); | |
11440 | slotbuf = xtensa_insnbuf_alloc (isa); | |
11441 | } | |
11442 | ||
d77b99c9 | 11443 | xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) f, 0); |
43cd72b9 BW |
11444 | fmt = xtensa_format_decode (isa, insnbuf); |
11445 | if (fmt == XTENSA_UNDEFINED) | |
11446 | as_fatal (_("cannot decode instruction format")); | |
11447 | vinsn->format = fmt; | |
11448 | vinsn->num_slots = xtensa_format_num_slots (isa, fmt); | |
11449 | ||
11450 | for (i = 0; i < vinsn->num_slots; i++) | |
11451 | { | |
11452 | TInsn *tinsn = &vinsn->slots[i]; | |
11453 | xtensa_format_get_slot (isa, fmt, i, insnbuf, slotbuf); | |
11454 | tinsn_from_insnbuf (tinsn, slotbuf, fmt, i); | |
11455 | } | |
11456 | } | |
11457 | ||
e0001a05 NC |
11458 | \f |
11459 | /* Expression utilities. */ | |
11460 | ||
43cd72b9 | 11461 | /* Return TRUE if the expression is an integer constant. */ |
e0001a05 NC |
11462 | |
11463 | bfd_boolean | |
7fa3d080 | 11464 | expr_is_const (const expressionS *s) |
e0001a05 NC |
11465 | { |
11466 | return (s->X_op == O_constant); | |
11467 | } | |
11468 | ||
11469 | ||
11470 | /* Get the expression constant. | |
43cd72b9 | 11471 | Calling this is illegal if expr_is_const () returns TRUE. */ |
e0001a05 NC |
11472 | |
11473 | offsetT | |
7fa3d080 | 11474 | get_expr_const (const expressionS *s) |
e0001a05 NC |
11475 | { |
11476 | assert (expr_is_const (s)); | |
11477 | return s->X_add_number; | |
11478 | } | |
11479 | ||
11480 | ||
11481 | /* Set the expression to a constant value. */ | |
11482 | ||
11483 | void | |
7fa3d080 | 11484 | set_expr_const (expressionS *s, offsetT val) |
e0001a05 NC |
11485 | { |
11486 | s->X_op = O_constant; | |
11487 | s->X_add_number = val; | |
11488 | s->X_add_symbol = NULL; | |
11489 | s->X_op_symbol = NULL; | |
11490 | } | |
11491 | ||
11492 | ||
43cd72b9 | 11493 | bfd_boolean |
7fa3d080 | 11494 | expr_is_register (const expressionS *s) |
43cd72b9 BW |
11495 | { |
11496 | return (s->X_op == O_register); | |
11497 | } | |
11498 | ||
11499 | ||
11500 | /* Get the expression constant. | |
11501 | Calling this is illegal if expr_is_const () returns TRUE. */ | |
11502 | ||
11503 | offsetT | |
7fa3d080 | 11504 | get_expr_register (const expressionS *s) |
43cd72b9 BW |
11505 | { |
11506 | assert (expr_is_register (s)); | |
11507 | return s->X_add_number; | |
11508 | } | |
11509 | ||
11510 | ||
e0001a05 NC |
11511 | /* Set the expression to a symbol + constant offset. */ |
11512 | ||
11513 | void | |
7fa3d080 | 11514 | set_expr_symbol_offset (expressionS *s, symbolS *sym, offsetT offset) |
e0001a05 NC |
11515 | { |
11516 | s->X_op = O_symbol; | |
11517 | s->X_add_symbol = sym; | |
11518 | s->X_op_symbol = NULL; /* unused */ | |
11519 | s->X_add_number = offset; | |
11520 | } | |
11521 | ||
11522 | ||
43cd72b9 BW |
11523 | /* Return TRUE if the two expressions are equal. */ |
11524 | ||
e0001a05 | 11525 | bfd_boolean |
7fa3d080 | 11526 | expr_is_equal (expressionS *s1, expressionS *s2) |
e0001a05 NC |
11527 | { |
11528 | if (s1->X_op != s2->X_op) | |
11529 | return FALSE; | |
11530 | if (s1->X_add_symbol != s2->X_add_symbol) | |
11531 | return FALSE; | |
11532 | if (s1->X_op_symbol != s2->X_op_symbol) | |
11533 | return FALSE; | |
11534 | if (s1->X_add_number != s2->X_add_number) | |
11535 | return FALSE; | |
11536 | return TRUE; | |
11537 | } | |
11538 | ||
11539 | ||
11540 | static void | |
7fa3d080 | 11541 | copy_expr (expressionS *dst, const expressionS *src) |
e0001a05 NC |
11542 | { |
11543 | memcpy (dst, src, sizeof (expressionS)); | |
11544 | } | |
11545 | ||
11546 | \f | |
9456465c | 11547 | /* Support for the "--rename-section" option. */ |
e0001a05 NC |
11548 | |
11549 | struct rename_section_struct | |
11550 | { | |
11551 | char *old_name; | |
11552 | char *new_name; | |
11553 | struct rename_section_struct *next; | |
11554 | }; | |
11555 | ||
11556 | static struct rename_section_struct *section_rename; | |
11557 | ||
11558 | ||
9456465c BW |
11559 | /* Parse the string "oldname=new_name(:oldname2=new_name2)*" and add |
11560 | entries to the section_rename list. Note: Specifying multiple | |
11561 | renamings separated by colons is not documented and is retained only | |
11562 | for backward compatibility. */ | |
e0001a05 | 11563 | |
7fa3d080 BW |
11564 | static void |
11565 | build_section_rename (const char *arg) | |
e0001a05 | 11566 | { |
9456465c | 11567 | struct rename_section_struct *r; |
e0001a05 NC |
11568 | char *this_arg = NULL; |
11569 | char *next_arg = NULL; | |
11570 | ||
9456465c | 11571 | for (this_arg = xstrdup (arg); this_arg != NULL; this_arg = next_arg) |
e0001a05 | 11572 | { |
9456465c BW |
11573 | char *old_name, *new_name; |
11574 | ||
e0001a05 NC |
11575 | if (this_arg) |
11576 | { | |
11577 | next_arg = strchr (this_arg, ':'); | |
11578 | if (next_arg) | |
11579 | { | |
11580 | *next_arg = '\0'; | |
11581 | next_arg++; | |
11582 | } | |
11583 | } | |
e0001a05 | 11584 | |
9456465c BW |
11585 | old_name = this_arg; |
11586 | new_name = strchr (this_arg, '='); | |
e0001a05 | 11587 | |
9456465c BW |
11588 | if (*old_name == '\0') |
11589 | { | |
11590 | as_warn (_("ignoring extra '-rename-section' delimiter ':'")); | |
11591 | continue; | |
11592 | } | |
11593 | if (!new_name || new_name[1] == '\0') | |
11594 | { | |
11595 | as_warn (_("ignoring invalid '-rename-section' specification: '%s'"), | |
11596 | old_name); | |
11597 | continue; | |
11598 | } | |
11599 | *new_name = '\0'; | |
11600 | new_name++; | |
e0001a05 | 11601 | |
9456465c BW |
11602 | /* Check for invalid section renaming. */ |
11603 | for (r = section_rename; r != NULL; r = r->next) | |
11604 | { | |
11605 | if (strcmp (r->old_name, old_name) == 0) | |
11606 | as_bad (_("section %s renamed multiple times"), old_name); | |
11607 | if (strcmp (r->new_name, new_name) == 0) | |
11608 | as_bad (_("multiple sections remapped to output section %s"), | |
11609 | new_name); | |
11610 | } | |
e0001a05 | 11611 | |
9456465c BW |
11612 | /* Now add it. */ |
11613 | r = (struct rename_section_struct *) | |
11614 | xmalloc (sizeof (struct rename_section_struct)); | |
11615 | r->old_name = xstrdup (old_name); | |
11616 | r->new_name = xstrdup (new_name); | |
11617 | r->next = section_rename; | |
11618 | section_rename = r; | |
e0001a05 | 11619 | } |
e0001a05 NC |
11620 | } |
11621 | ||
11622 | ||
9456465c BW |
11623 | char * |
11624 | xtensa_section_rename (char *name) | |
e0001a05 NC |
11625 | { |
11626 | struct rename_section_struct *r = section_rename; | |
11627 | ||
11628 | for (r = section_rename; r != NULL; r = r->next) | |
43cd72b9 BW |
11629 | { |
11630 | if (strcmp (r->old_name, name) == 0) | |
11631 | return r->new_name; | |
11632 | } | |
e0001a05 NC |
11633 | |
11634 | return name; | |
11635 | } |