]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-xtensa.c
Constify more
[thirdparty/binutils-gdb.git] / gas / config / tc-xtensa.c
1 /* tc-xtensa.c -- Assemble Xtensa instructions.
2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "as.h"
22 #include <limits.h>
23 #include "sb.h"
24 #include "safe-ctype.h"
25 #include "tc-xtensa.h"
26 #include "subsegs.h"
27 #include "xtensa-relax.h"
28 #include "dwarf2dbg.h"
29 #include "xtensa-istack.h"
30 #include "struc-symbol.h"
31 #include "xtensa-config.h"
32
33 /* Provide default values for new configuration settings. */
34 #ifndef XSHAL_ABI
35 #define XSHAL_ABI 0
36 #endif
37
38 #ifndef uint32
39 #define uint32 unsigned int
40 #endif
41 #ifndef int32
42 #define int32 signed int
43 #endif
44
45 /* Notes:
46
47 Naming conventions (used somewhat inconsistently):
48 The xtensa_ functions are exported
49 The xg_ functions are internal
50
51 We also have a couple of different extensibility mechanisms.
52 1) The idiom replacement:
53 This is used when a line is first parsed to
54 replace an instruction pattern with another instruction
55 It is currently limited to replacements of instructions
56 with constant operands.
57 2) The xtensa-relax.c mechanism that has stronger instruction
58 replacement patterns. When an instruction's immediate field
59 does not fit the next instruction sequence is attempted.
60 In addition, "narrow" opcodes are supported this way. */
61
62
63 /* Define characters with special meanings to GAS. */
64 const char comment_chars[] = "#";
65 const char line_comment_chars[] = "#";
66 const char line_separator_chars[] = ";";
67 const char EXP_CHARS[] = "eE";
68 const char FLT_CHARS[] = "rRsSfFdDxXpP";
69
70
71 /* Flags to indicate whether the hardware supports the density and
72 absolute literals options. */
73
74 bfd_boolean density_supported = XCHAL_HAVE_DENSITY;
75 bfd_boolean absolute_literals_supported = XSHAL_USE_ABSOLUTE_LITERALS;
76
77 static vliw_insn cur_vinsn;
78
79 unsigned xtensa_num_pipe_stages;
80 unsigned xtensa_fetch_width = XCHAL_INST_FETCH_WIDTH;
81
82 static enum debug_info_type xt_saved_debug_type = DEBUG_NONE;
83
84 /* Some functions are only valid in the front end. This variable
85 allows us to assert that we haven't crossed over into the
86 back end. */
87 static bfd_boolean past_xtensa_end = FALSE;
88
89 /* Flags for properties of the last instruction in a segment. */
90 #define FLAG_IS_A0_WRITER 0x1
91 #define FLAG_IS_BAD_LOOPEND 0x2
92
93
94 /* We define a special segment names ".literal" to place literals
95 into. The .fini and .init sections are special because they
96 contain code that is moved together by the linker. We give them
97 their own special .fini.literal and .init.literal sections. */
98
99 #define LITERAL_SECTION_NAME xtensa_section_rename (".literal")
100 #define LIT4_SECTION_NAME xtensa_section_rename (".lit4")
101 #define INIT_SECTION_NAME xtensa_section_rename (".init")
102 #define FINI_SECTION_NAME xtensa_section_rename (".fini")
103
104
105 /* This type is used for the directive_stack to keep track of the
106 state of the literal collection pools. If lit_prefix is set, it is
107 used to determine the literal section names; otherwise, the literal
108 sections are determined based on the current text section. The
109 lit_seg and lit4_seg fields cache these literal sections, with the
110 current_text_seg field used a tag to indicate whether the cached
111 values are valid. */
112
113 typedef struct lit_state_struct
114 {
115 char *lit_prefix;
116 segT current_text_seg;
117 segT lit_seg;
118 segT lit4_seg;
119 } lit_state;
120
121 static lit_state default_lit_sections;
122
123
124 /* We keep a list of literal segments. The seg_list type is the node
125 for this list. The literal_head pointer is the head of the list,
126 with the literal_head_h dummy node at the start. */
127
128 typedef struct seg_list_struct
129 {
130 struct seg_list_struct *next;
131 segT seg;
132 } seg_list;
133
134 static seg_list literal_head_h;
135 static seg_list *literal_head = &literal_head_h;
136
137
138 /* Lists of symbols. We keep a list of symbols that label the current
139 instruction, so that we can adjust the symbols when inserting alignment
140 for various instructions. We also keep a list of all the symbols on
141 literals, so that we can fix up those symbols when the literals are
142 later moved into the text sections. */
143
144 typedef struct sym_list_struct
145 {
146 struct sym_list_struct *next;
147 symbolS *sym;
148 } sym_list;
149
150 static sym_list *insn_labels = NULL;
151 static sym_list *free_insn_labels = NULL;
152 static sym_list *saved_insn_labels = NULL;
153
154 static sym_list *literal_syms;
155
156
157 /* Flags to determine whether to prefer const16 or l32r
158 if both options are available. */
159 int prefer_const16 = 0;
160 int prefer_l32r = 0;
161
162 /* Global flag to indicate when we are emitting literals. */
163 int generating_literals = 0;
164
165 /* The following PROPERTY table definitions are copied from
166 <elf/xtensa.h> and must be kept in sync with the code there. */
167
168 /* Flags in the property tables to specify whether blocks of memory
169 are literals, instructions, data, or unreachable. For
170 instructions, blocks that begin loop targets and branch targets are
171 designated. Blocks that do not allow density, instruction
172 reordering or transformation are also specified. Finally, for
173 branch targets, branch target alignment priority is included.
174 Alignment of the next block is specified in the current block
175 and the size of the current block does not include any fill required
176 to align to the next block. */
177
178 #define XTENSA_PROP_LITERAL 0x00000001
179 #define XTENSA_PROP_INSN 0x00000002
180 #define XTENSA_PROP_DATA 0x00000004
181 #define XTENSA_PROP_UNREACHABLE 0x00000008
182 /* Instruction only properties at beginning of code. */
183 #define XTENSA_PROP_INSN_LOOP_TARGET 0x00000010
184 #define XTENSA_PROP_INSN_BRANCH_TARGET 0x00000020
185 /* Instruction only properties about code. */
186 #define XTENSA_PROP_INSN_NO_DENSITY 0x00000040
187 #define XTENSA_PROP_INSN_NO_REORDER 0x00000080
188 /* Historically, NO_TRANSFORM was a property of instructions,
189 but it should apply to literals under certain circumstances. */
190 #define XTENSA_PROP_NO_TRANSFORM 0x00000100
191
192 /* Branch target alignment information. This transmits information
193 to the linker optimization about the priority of aligning a
194 particular block for branch target alignment: None, low priority,
195 high priority, or required. These only need to be checked in
196 instruction blocks marked as XTENSA_PROP_INSN_BRANCH_TARGET.
197 Common usage is
198
199 switch (GET_XTENSA_PROP_BT_ALIGN (flags))
200 case XTENSA_PROP_BT_ALIGN_NONE:
201 case XTENSA_PROP_BT_ALIGN_LOW:
202 case XTENSA_PROP_BT_ALIGN_HIGH:
203 case XTENSA_PROP_BT_ALIGN_REQUIRE:
204 */
205 #define XTENSA_PROP_BT_ALIGN_MASK 0x00000600
206
207 /* No branch target alignment. */
208 #define XTENSA_PROP_BT_ALIGN_NONE 0x0
209 /* Low priority branch target alignment. */
210 #define XTENSA_PROP_BT_ALIGN_LOW 0x1
211 /* High priority branch target alignment. */
212 #define XTENSA_PROP_BT_ALIGN_HIGH 0x2
213 /* Required branch target alignment. */
214 #define XTENSA_PROP_BT_ALIGN_REQUIRE 0x3
215
216 #define GET_XTENSA_PROP_BT_ALIGN(flag) \
217 (((unsigned) ((flag) & (XTENSA_PROP_BT_ALIGN_MASK))) >> 9)
218 #define SET_XTENSA_PROP_BT_ALIGN(flag, align) \
219 (((flag) & (~XTENSA_PROP_BT_ALIGN_MASK)) | \
220 (((align) << 9) & XTENSA_PROP_BT_ALIGN_MASK))
221
222
223 /* Alignment is specified in the block BEFORE the one that needs
224 alignment. Up to 5 bits. Use GET_XTENSA_PROP_ALIGNMENT(flags) to
225 get the required alignment specified as a power of 2. Use
226 SET_XTENSA_PROP_ALIGNMENT(flags, pow2) to set the required
227 alignment. Be careful of side effects since the SET will evaluate
228 flags twice. Also, note that the SIZE of a block in the property
229 table does not include the alignment size, so the alignment fill
230 must be calculated to determine if two blocks are contiguous.
231 TEXT_ALIGN is not currently implemented but is a placeholder for a
232 possible future implementation. */
233
234 #define XTENSA_PROP_ALIGN 0x00000800
235
236 #define XTENSA_PROP_ALIGNMENT_MASK 0x0001f000
237
238 #define GET_XTENSA_PROP_ALIGNMENT(flag) \
239 (((unsigned) ((flag) & (XTENSA_PROP_ALIGNMENT_MASK))) >> 12)
240 #define SET_XTENSA_PROP_ALIGNMENT(flag, align) \
241 (((flag) & (~XTENSA_PROP_ALIGNMENT_MASK)) | \
242 (((align) << 12) & XTENSA_PROP_ALIGNMENT_MASK))
243
244 #define XTENSA_PROP_INSN_ABSLIT 0x00020000
245
246
247 /* Structure for saving instruction and alignment per-fragment data
248 that will be written to the object file. This structure is
249 equivalent to the actual data that will be written out to the file
250 but is easier to use. We provide a conversion to file flags
251 in frag_flags_to_number. */
252
253 typedef struct frag_flags_struct frag_flags;
254
255 struct frag_flags_struct
256 {
257 /* is_literal should only be used after xtensa_move_literals.
258 If you need to check if you are generating a literal fragment,
259 then use the generating_literals global. */
260
261 unsigned is_literal : 1;
262 unsigned is_insn : 1;
263 unsigned is_data : 1;
264 unsigned is_unreachable : 1;
265
266 /* is_specific_opcode implies no_transform. */
267 unsigned is_no_transform : 1;
268
269 struct
270 {
271 unsigned is_loop_target : 1;
272 unsigned is_branch_target : 1; /* Branch targets have a priority. */
273 unsigned bt_align_priority : 2;
274
275 unsigned is_no_density : 1;
276 /* no_longcalls flag does not need to be placed in the object file. */
277
278 unsigned is_no_reorder : 1;
279
280 /* Uses absolute literal addressing for l32r. */
281 unsigned is_abslit : 1;
282 } insn;
283 unsigned is_align : 1;
284 unsigned alignment : 5;
285 };
286
287
288 /* Structure for saving information about a block of property data
289 for frags that have the same flags. */
290 struct xtensa_block_info_struct
291 {
292 segT sec;
293 bfd_vma offset;
294 size_t size;
295 frag_flags flags;
296 struct xtensa_block_info_struct *next;
297 };
298
299
300 /* Structure for saving the current state before emitting literals. */
301 typedef struct emit_state_struct
302 {
303 const char *name;
304 segT now_seg;
305 subsegT now_subseg;
306 int generating_literals;
307 } emit_state;
308
309
310 /* Opcode placement information */
311
312 typedef unsigned long long bitfield;
313 #define bit_is_set(bit, bf) ((bf) & (0x01ll << (bit)))
314 #define set_bit(bit, bf) ((bf) |= (0x01ll << (bit)))
315 #define clear_bit(bit, bf) ((bf) &= ~(0x01ll << (bit)))
316
317 #define MAX_FORMATS 32
318
319 typedef struct op_placement_info_struct
320 {
321 int num_formats;
322 /* A number describing how restrictive the issue is for this
323 opcode. For example, an opcode that fits lots of different
324 formats has a high freedom, as does an opcode that fits
325 only one format but many slots in that format. The most
326 restrictive is the opcode that fits only one slot in one
327 format. */
328 int issuef;
329 xtensa_format narrowest;
330 char narrowest_size;
331 char narrowest_slot;
332
333 /* formats is a bitfield with the Nth bit set
334 if the opcode fits in the Nth xtensa_format. */
335 bitfield formats;
336
337 /* slots[N]'s Mth bit is set if the op fits in the
338 Mth slot of the Nth xtensa_format. */
339 bitfield slots[MAX_FORMATS];
340
341 /* A count of the number of slots in a given format
342 an op can fit (i.e., the bitcount of the slot field above). */
343 char slots_in_format[MAX_FORMATS];
344
345 } op_placement_info, *op_placement_info_table;
346
347 op_placement_info_table op_placement_table;
348
349
350 /* Extra expression types. */
351
352 #define O_pltrel O_md1 /* like O_symbol but use a PLT reloc */
353 #define O_hi16 O_md2 /* use high 16 bits of symbolic value */
354 #define O_lo16 O_md3 /* use low 16 bits of symbolic value */
355 #define O_pcrel O_md4 /* value is a PC-relative offset */
356 #define O_tlsfunc O_md5 /* TLS_FUNC/TLSDESC_FN relocation */
357 #define O_tlsarg O_md6 /* TLS_ARG/TLSDESC_ARG relocation */
358 #define O_tlscall O_md7 /* TLS_CALL relocation */
359 #define O_tpoff O_md8 /* TPOFF relocation */
360 #define O_dtpoff O_md9 /* DTPOFF relocation */
361
362 struct suffix_reloc_map
363 {
364 const char *suffix;
365 int length;
366 bfd_reloc_code_real_type reloc;
367 unsigned char operator;
368 };
369
370 #define SUFFIX_MAP(str, reloc, op) { str, sizeof (str) - 1, reloc, op }
371
372 static struct suffix_reloc_map suffix_relocs[] =
373 {
374 SUFFIX_MAP ("l", BFD_RELOC_LO16, O_lo16),
375 SUFFIX_MAP ("h", BFD_RELOC_HI16, O_hi16),
376 SUFFIX_MAP ("plt", BFD_RELOC_XTENSA_PLT, O_pltrel),
377 SUFFIX_MAP ("pcrel", BFD_RELOC_32_PCREL, O_pcrel),
378 SUFFIX_MAP ("tlsfunc", BFD_RELOC_XTENSA_TLS_FUNC, O_tlsfunc),
379 SUFFIX_MAP ("tlsarg", BFD_RELOC_XTENSA_TLS_ARG, O_tlsarg),
380 SUFFIX_MAP ("tlscall", BFD_RELOC_XTENSA_TLS_CALL, O_tlscall),
381 SUFFIX_MAP ("tpoff", BFD_RELOC_XTENSA_TLS_TPOFF, O_tpoff),
382 SUFFIX_MAP ("dtpoff", BFD_RELOC_XTENSA_TLS_DTPOFF, O_dtpoff),
383 { (char *) 0, 0, BFD_RELOC_UNUSED, 0 }
384 };
385
386
387 /* Directives. */
388
389 typedef enum
390 {
391 directive_none = 0,
392 directive_literal,
393 directive_density,
394 directive_transform,
395 directive_freeregs,
396 directive_longcalls,
397 directive_literal_prefix,
398 directive_schedule,
399 directive_absolute_literals,
400 directive_last_directive
401 } directiveE;
402
403 typedef struct
404 {
405 const char *name;
406 bfd_boolean can_be_negated;
407 } directive_infoS;
408
409 const directive_infoS directive_info[] =
410 {
411 { "none", FALSE },
412 { "literal", FALSE },
413 { "density", TRUE },
414 { "transform", TRUE },
415 { "freeregs", FALSE },
416 { "longcalls", TRUE },
417 { "literal_prefix", FALSE },
418 { "schedule", TRUE },
419 { "absolute-literals", TRUE }
420 };
421
422 bfd_boolean directive_state[] =
423 {
424 FALSE, /* none */
425 FALSE, /* literal */
426 #if !XCHAL_HAVE_DENSITY
427 FALSE, /* density */
428 #else
429 TRUE, /* density */
430 #endif
431 TRUE, /* transform */
432 FALSE, /* freeregs */
433 FALSE, /* longcalls */
434 FALSE, /* literal_prefix */
435 FALSE, /* schedule */
436 #if XSHAL_USE_ABSOLUTE_LITERALS
437 TRUE /* absolute_literals */
438 #else
439 FALSE /* absolute_literals */
440 #endif
441 };
442
443 /* A circular list of all potential and actual literal pool locations
444 in a segment. */
445 struct litpool_frag
446 {
447 struct litpool_frag *next;
448 struct litpool_frag *prev;
449 fragS *fragP;
450 addressT addr;
451 short priority; /* 1, 2, or 3 -- 1 is highest */
452 short original_priority;
453 };
454
455 /* Map a segment to its litpool_frag list. */
456 struct litpool_seg
457 {
458 struct litpool_seg *next;
459 asection *seg;
460 struct litpool_frag frag_list;
461 int frag_count; /* since last litpool location */
462 };
463
464 static struct litpool_seg litpool_seg_list;
465
466
467 /* Directive functions. */
468
469 static void xtensa_begin_directive (int);
470 static void xtensa_end_directive (int);
471 static void xtensa_literal_prefix (void);
472 static void xtensa_literal_position (int);
473 static void xtensa_literal_pseudo (int);
474 static void xtensa_frequency_pseudo (int);
475 static void xtensa_elf_cons (int);
476 static void xtensa_leb128 (int);
477
478 /* Parsing and Idiom Translation. */
479
480 static bfd_reloc_code_real_type xtensa_elf_suffix (char **, expressionS *);
481
482 /* Various Other Internal Functions. */
483
484 extern bfd_boolean xg_is_single_relaxable_insn (TInsn *, TInsn *, bfd_boolean);
485 static bfd_boolean xg_build_to_insn (TInsn *, TInsn *, BuildInstr *);
486 static void xtensa_mark_literal_pool_location (void);
487 static addressT get_expanded_loop_offset (xtensa_opcode);
488 static fragS *get_literal_pool_location (segT);
489 static void set_literal_pool_location (segT, fragS *);
490 static void xtensa_set_frag_assembly_state (fragS *);
491 static void finish_vinsn (vliw_insn *);
492 static bfd_boolean emit_single_op (TInsn *);
493 static int total_frag_text_expansion (fragS *);
494 static bfd_boolean use_trampolines = TRUE;
495 static void xtensa_check_frag_count (void);
496 static void xtensa_create_trampoline_frag (bfd_boolean);
497 static void xtensa_maybe_create_trampoline_frag (void);
498 struct trampoline_frag;
499 static int init_trampoline_frag (struct trampoline_frag *);
500 static void xtensa_maybe_create_literal_pool_frag (bfd_boolean, bfd_boolean);
501 static bfd_boolean auto_litpools = FALSE;
502 static int auto_litpool_limit = 10000;
503
504 /* Alignment Functions. */
505
506 static int get_text_align_power (unsigned);
507 static int get_text_align_max_fill_size (int, bfd_boolean, bfd_boolean);
508 static int branch_align_power (segT);
509
510 /* Helpers for xtensa_relax_frag(). */
511
512 static long relax_frag_add_nop (fragS *);
513
514 /* Accessors for additional per-subsegment information. */
515
516 static unsigned get_last_insn_flags (segT, subsegT);
517 static void set_last_insn_flags (segT, subsegT, unsigned, bfd_boolean);
518 static float get_subseg_total_freq (segT, subsegT);
519 static float get_subseg_target_freq (segT, subsegT);
520 static void set_subseg_freq (segT, subsegT, float, float);
521
522 /* Segment list functions. */
523
524 static void xtensa_move_literals (void);
525 static void xtensa_reorder_segments (void);
526 static void xtensa_switch_to_literal_fragment (emit_state *);
527 static void xtensa_switch_to_non_abs_literal_fragment (emit_state *);
528 static void xtensa_switch_section_emit_state (emit_state *, segT, subsegT);
529 static void xtensa_restore_emit_state (emit_state *);
530 static segT cache_literal_section (bfd_boolean);
531
532 /* Import from elf32-xtensa.c in BFD library. */
533
534 extern asection *xtensa_make_property_section (asection *, const char *);
535
536 /* op_placement_info functions. */
537
538 static void init_op_placement_info_table (void);
539 extern bfd_boolean opcode_fits_format_slot (xtensa_opcode, xtensa_format, int);
540 static int xg_get_single_size (xtensa_opcode);
541 static xtensa_format xg_get_single_format (xtensa_opcode);
542 static int xg_get_single_slot (xtensa_opcode);
543
544 /* TInsn and IStack functions. */
545
546 static bfd_boolean tinsn_has_symbolic_operands (const TInsn *);
547 static bfd_boolean tinsn_has_invalid_symbolic_operands (const TInsn *);
548 static bfd_boolean tinsn_has_complex_operands (const TInsn *);
549 static bfd_boolean tinsn_to_insnbuf (TInsn *, xtensa_insnbuf);
550 static bfd_boolean tinsn_check_arguments (const TInsn *);
551 static void tinsn_from_chars (TInsn *, char *, int);
552 static void tinsn_immed_from_frag (TInsn *, fragS *, int);
553 static int get_num_stack_text_bytes (IStack *);
554 static int get_num_stack_literal_bytes (IStack *);
555 static bfd_boolean tinsn_to_slotbuf (xtensa_format, int, TInsn *, xtensa_insnbuf);
556
557 /* vliw_insn functions. */
558
559 static void xg_init_vinsn (vliw_insn *);
560 static void xg_copy_vinsn (vliw_insn *, vliw_insn *);
561 static void xg_clear_vinsn (vliw_insn *);
562 static bfd_boolean vinsn_has_specific_opcodes (vliw_insn *);
563 static void xg_free_vinsn (vliw_insn *);
564 static bfd_boolean vinsn_to_insnbuf
565 (vliw_insn *, char *, fragS *, bfd_boolean);
566 static void vinsn_from_chars (vliw_insn *, char *);
567
568 /* Expression Utilities. */
569
570 bfd_boolean expr_is_const (const expressionS *);
571 offsetT get_expr_const (const expressionS *);
572 void set_expr_const (expressionS *, offsetT);
573 bfd_boolean expr_is_register (const expressionS *);
574 offsetT get_expr_register (const expressionS *);
575 void set_expr_symbol_offset (expressionS *, symbolS *, offsetT);
576 bfd_boolean expr_is_equal (expressionS *, expressionS *);
577 static void copy_expr (expressionS *, const expressionS *);
578
579 /* Section renaming. */
580
581 static void build_section_rename (const char *);
582
583
584 /* ISA imported from bfd. */
585 extern xtensa_isa xtensa_default_isa;
586
587 extern int target_big_endian;
588
589 static xtensa_opcode xtensa_addi_opcode;
590 static xtensa_opcode xtensa_addmi_opcode;
591 static xtensa_opcode xtensa_call0_opcode;
592 static xtensa_opcode xtensa_call4_opcode;
593 static xtensa_opcode xtensa_call8_opcode;
594 static xtensa_opcode xtensa_call12_opcode;
595 static xtensa_opcode xtensa_callx0_opcode;
596 static xtensa_opcode xtensa_callx4_opcode;
597 static xtensa_opcode xtensa_callx8_opcode;
598 static xtensa_opcode xtensa_callx12_opcode;
599 static xtensa_opcode xtensa_const16_opcode;
600 static xtensa_opcode xtensa_entry_opcode;
601 static xtensa_opcode xtensa_extui_opcode;
602 static xtensa_opcode xtensa_movi_opcode;
603 static xtensa_opcode xtensa_movi_n_opcode;
604 static xtensa_opcode xtensa_isync_opcode;
605 static xtensa_opcode xtensa_j_opcode;
606 static xtensa_opcode xtensa_jx_opcode;
607 static xtensa_opcode xtensa_l32r_opcode;
608 static xtensa_opcode xtensa_loop_opcode;
609 static xtensa_opcode xtensa_loopnez_opcode;
610 static xtensa_opcode xtensa_loopgtz_opcode;
611 static xtensa_opcode xtensa_nop_opcode;
612 static xtensa_opcode xtensa_nop_n_opcode;
613 static xtensa_opcode xtensa_or_opcode;
614 static xtensa_opcode xtensa_ret_opcode;
615 static xtensa_opcode xtensa_ret_n_opcode;
616 static xtensa_opcode xtensa_retw_opcode;
617 static xtensa_opcode xtensa_retw_n_opcode;
618 static xtensa_opcode xtensa_rsr_lcount_opcode;
619 static xtensa_opcode xtensa_waiti_opcode;
620 static int config_max_slots = 0;
621
622 \f
623 /* Command-line Options. */
624
625 bfd_boolean use_literal_section = TRUE;
626 enum flix_level produce_flix = FLIX_ALL;
627 static bfd_boolean align_targets = TRUE;
628 static bfd_boolean warn_unaligned_branch_targets = FALSE;
629 static bfd_boolean has_a0_b_retw = FALSE;
630 static bfd_boolean workaround_a0_b_retw = FALSE;
631 static bfd_boolean workaround_b_j_loop_end = FALSE;
632 static bfd_boolean workaround_short_loop = FALSE;
633 static bfd_boolean maybe_has_short_loop = FALSE;
634 static bfd_boolean workaround_close_loop_end = FALSE;
635 static bfd_boolean maybe_has_close_loop_end = FALSE;
636 static bfd_boolean enforce_three_byte_loop_align = FALSE;
637
638 /* When workaround_short_loops is TRUE, all loops with early exits must
639 have at least 3 instructions. workaround_all_short_loops is a modifier
640 to the workaround_short_loop flag. In addition to the
641 workaround_short_loop actions, all straightline loopgtz and loopnez
642 must have at least 3 instructions. */
643
644 static bfd_boolean workaround_all_short_loops = FALSE;
645
646
647 static void
648 xtensa_setup_hw_workarounds (int earliest, int latest)
649 {
650 if (earliest > latest)
651 as_fatal (_("illegal range of target hardware versions"));
652
653 /* Enable all workarounds for pre-T1050.0 hardware. */
654 if (earliest < 105000 || latest < 105000)
655 {
656 workaround_a0_b_retw |= TRUE;
657 workaround_b_j_loop_end |= TRUE;
658 workaround_short_loop |= TRUE;
659 workaround_close_loop_end |= TRUE;
660 workaround_all_short_loops |= TRUE;
661 enforce_three_byte_loop_align = TRUE;
662 }
663 }
664
665
666 enum
667 {
668 option_density = OPTION_MD_BASE,
669 option_no_density,
670
671 option_flix,
672 option_no_generate_flix,
673 option_no_flix,
674
675 option_relax,
676 option_no_relax,
677
678 option_link_relax,
679 option_no_link_relax,
680
681 option_generics,
682 option_no_generics,
683
684 option_transform,
685 option_no_transform,
686
687 option_text_section_literals,
688 option_no_text_section_literals,
689
690 option_absolute_literals,
691 option_no_absolute_literals,
692
693 option_align_targets,
694 option_no_align_targets,
695
696 option_warn_unaligned_targets,
697
698 option_longcalls,
699 option_no_longcalls,
700
701 option_workaround_a0_b_retw,
702 option_no_workaround_a0_b_retw,
703
704 option_workaround_b_j_loop_end,
705 option_no_workaround_b_j_loop_end,
706
707 option_workaround_short_loop,
708 option_no_workaround_short_loop,
709
710 option_workaround_all_short_loops,
711 option_no_workaround_all_short_loops,
712
713 option_workaround_close_loop_end,
714 option_no_workaround_close_loop_end,
715
716 option_no_workarounds,
717
718 option_rename_section_name,
719
720 option_prefer_l32r,
721 option_prefer_const16,
722
723 option_target_hardware,
724
725 option_trampolines,
726 option_no_trampolines,
727
728 option_auto_litpools,
729 option_no_auto_litpools,
730 option_auto_litpool_limit,
731 };
732
733 const char *md_shortopts = "";
734
735 struct option md_longopts[] =
736 {
737 { "density", no_argument, NULL, option_density },
738 { "no-density", no_argument, NULL, option_no_density },
739
740 { "flix", no_argument, NULL, option_flix },
741 { "no-generate-flix", no_argument, NULL, option_no_generate_flix },
742 { "no-allow-flix", no_argument, NULL, option_no_flix },
743
744 /* Both "relax" and "generics" are deprecated and treated as equivalent
745 to the "transform" option. */
746 { "relax", no_argument, NULL, option_relax },
747 { "no-relax", no_argument, NULL, option_no_relax },
748 { "generics", no_argument, NULL, option_generics },
749 { "no-generics", no_argument, NULL, option_no_generics },
750
751 { "transform", no_argument, NULL, option_transform },
752 { "no-transform", no_argument, NULL, option_no_transform },
753 { "text-section-literals", no_argument, NULL, option_text_section_literals },
754 { "no-text-section-literals", no_argument, NULL,
755 option_no_text_section_literals },
756 { "absolute-literals", no_argument, NULL, option_absolute_literals },
757 { "no-absolute-literals", no_argument, NULL, option_no_absolute_literals },
758 /* This option was changed from -align-target to -target-align
759 because it conflicted with the "-al" option. */
760 { "target-align", no_argument, NULL, option_align_targets },
761 { "no-target-align", no_argument, NULL, option_no_align_targets },
762 { "warn-unaligned-targets", no_argument, NULL,
763 option_warn_unaligned_targets },
764 { "longcalls", no_argument, NULL, option_longcalls },
765 { "no-longcalls", no_argument, NULL, option_no_longcalls },
766
767 { "no-workaround-a0-b-retw", no_argument, NULL,
768 option_no_workaround_a0_b_retw },
769 { "workaround-a0-b-retw", no_argument, NULL, option_workaround_a0_b_retw },
770
771 { "no-workaround-b-j-loop-end", no_argument, NULL,
772 option_no_workaround_b_j_loop_end },
773 { "workaround-b-j-loop-end", no_argument, NULL,
774 option_workaround_b_j_loop_end },
775
776 { "no-workaround-short-loops", no_argument, NULL,
777 option_no_workaround_short_loop },
778 { "workaround-short-loops", no_argument, NULL,
779 option_workaround_short_loop },
780
781 { "no-workaround-all-short-loops", no_argument, NULL,
782 option_no_workaround_all_short_loops },
783 { "workaround-all-short-loop", no_argument, NULL,
784 option_workaround_all_short_loops },
785
786 { "prefer-l32r", no_argument, NULL, option_prefer_l32r },
787 { "prefer-const16", no_argument, NULL, option_prefer_const16 },
788
789 { "no-workarounds", no_argument, NULL, option_no_workarounds },
790
791 { "no-workaround-close-loop-end", no_argument, NULL,
792 option_no_workaround_close_loop_end },
793 { "workaround-close-loop-end", no_argument, NULL,
794 option_workaround_close_loop_end },
795
796 { "rename-section", required_argument, NULL, option_rename_section_name },
797
798 { "link-relax", no_argument, NULL, option_link_relax },
799 { "no-link-relax", no_argument, NULL, option_no_link_relax },
800
801 { "target-hardware", required_argument, NULL, option_target_hardware },
802
803 { "trampolines", no_argument, NULL, option_trampolines },
804 { "no-trampolines", no_argument, NULL, option_no_trampolines },
805
806 { "auto-litpools", no_argument, NULL, option_auto_litpools },
807 { "no-auto-litpools", no_argument, NULL, option_no_auto_litpools },
808 { "auto-litpool-limit", required_argument, NULL, option_auto_litpool_limit },
809
810 { NULL, no_argument, NULL, 0 }
811 };
812
813 size_t md_longopts_size = sizeof md_longopts;
814
815
816 int
817 md_parse_option (int c, const char *arg)
818 {
819 switch (c)
820 {
821 case option_density:
822 as_warn (_("--density option is ignored"));
823 return 1;
824 case option_no_density:
825 as_warn (_("--no-density option is ignored"));
826 return 1;
827 case option_link_relax:
828 linkrelax = 1;
829 return 1;
830 case option_no_link_relax:
831 linkrelax = 0;
832 return 1;
833 case option_flix:
834 produce_flix = FLIX_ALL;
835 return 1;
836 case option_no_generate_flix:
837 produce_flix = FLIX_NO_GENERATE;
838 return 1;
839 case option_no_flix:
840 produce_flix = FLIX_NONE;
841 return 1;
842 case option_generics:
843 as_warn (_("--generics is deprecated; use --transform instead"));
844 return md_parse_option (option_transform, arg);
845 case option_no_generics:
846 as_warn (_("--no-generics is deprecated; use --no-transform instead"));
847 return md_parse_option (option_no_transform, arg);
848 case option_relax:
849 as_warn (_("--relax is deprecated; use --transform instead"));
850 return md_parse_option (option_transform, arg);
851 case option_no_relax:
852 as_warn (_("--no-relax is deprecated; use --no-transform instead"));
853 return md_parse_option (option_no_transform, arg);
854 case option_longcalls:
855 directive_state[directive_longcalls] = TRUE;
856 return 1;
857 case option_no_longcalls:
858 directive_state[directive_longcalls] = FALSE;
859 return 1;
860 case option_text_section_literals:
861 use_literal_section = FALSE;
862 return 1;
863 case option_no_text_section_literals:
864 use_literal_section = TRUE;
865 return 1;
866 case option_absolute_literals:
867 if (!absolute_literals_supported)
868 {
869 as_fatal (_("--absolute-literals option not supported in this Xtensa configuration"));
870 return 0;
871 }
872 directive_state[directive_absolute_literals] = TRUE;
873 return 1;
874 case option_no_absolute_literals:
875 directive_state[directive_absolute_literals] = FALSE;
876 return 1;
877
878 case option_workaround_a0_b_retw:
879 workaround_a0_b_retw = TRUE;
880 return 1;
881 case option_no_workaround_a0_b_retw:
882 workaround_a0_b_retw = FALSE;
883 return 1;
884 case option_workaround_b_j_loop_end:
885 workaround_b_j_loop_end = TRUE;
886 return 1;
887 case option_no_workaround_b_j_loop_end:
888 workaround_b_j_loop_end = FALSE;
889 return 1;
890
891 case option_workaround_short_loop:
892 workaround_short_loop = TRUE;
893 return 1;
894 case option_no_workaround_short_loop:
895 workaround_short_loop = FALSE;
896 return 1;
897
898 case option_workaround_all_short_loops:
899 workaround_all_short_loops = TRUE;
900 return 1;
901 case option_no_workaround_all_short_loops:
902 workaround_all_short_loops = FALSE;
903 return 1;
904
905 case option_workaround_close_loop_end:
906 workaround_close_loop_end = TRUE;
907 return 1;
908 case option_no_workaround_close_loop_end:
909 workaround_close_loop_end = FALSE;
910 return 1;
911
912 case option_no_workarounds:
913 workaround_a0_b_retw = FALSE;
914 workaround_b_j_loop_end = FALSE;
915 workaround_short_loop = FALSE;
916 workaround_all_short_loops = FALSE;
917 workaround_close_loop_end = FALSE;
918 return 1;
919
920 case option_align_targets:
921 align_targets = TRUE;
922 return 1;
923 case option_no_align_targets:
924 align_targets = FALSE;
925 return 1;
926
927 case option_warn_unaligned_targets:
928 warn_unaligned_branch_targets = TRUE;
929 return 1;
930
931 case option_rename_section_name:
932 build_section_rename (arg);
933 return 1;
934
935 case 'Q':
936 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
937 should be emitted or not. FIXME: Not implemented. */
938 return 1;
939
940 case option_prefer_l32r:
941 if (prefer_const16)
942 as_fatal (_("prefer-l32r conflicts with prefer-const16"));
943 prefer_l32r = 1;
944 return 1;
945
946 case option_prefer_const16:
947 if (prefer_l32r)
948 as_fatal (_("prefer-const16 conflicts with prefer-l32r"));
949 prefer_const16 = 1;
950 return 1;
951
952 case option_target_hardware:
953 {
954 int earliest, latest = 0;
955 char *end;
956 if (*arg == 0 || *arg == '-')
957 as_fatal (_("invalid target hardware version"));
958
959 earliest = strtol (arg, &end, 0);
960
961 if (*end == 0)
962 latest = earliest;
963 else if (*end == '-')
964 {
965 if (*++end == 0)
966 as_fatal (_("invalid target hardware version"));
967 latest = strtol (end, &end, 0);
968 }
969 if (*end != 0)
970 as_fatal (_("invalid target hardware version"));
971
972 xtensa_setup_hw_workarounds (earliest, latest);
973 return 1;
974 }
975
976 case option_transform:
977 /* This option has no affect other than to use the defaults,
978 which are already set. */
979 return 1;
980
981 case option_no_transform:
982 /* This option turns off all transformations of any kind.
983 However, because we want to preserve the state of other
984 directives, we only change its own field. Thus, before
985 you perform any transformation, always check if transform
986 is available. If you use the functions we provide for this
987 purpose, you will be ok. */
988 directive_state[directive_transform] = FALSE;
989 return 1;
990
991 case option_trampolines:
992 use_trampolines = TRUE;
993 return 1;
994
995 case option_no_trampolines:
996 use_trampolines = FALSE;
997 return 1;
998
999 case option_auto_litpools:
1000 auto_litpools = TRUE;
1001 use_literal_section = FALSE;
1002 return 1;
1003
1004 case option_no_auto_litpools:
1005 auto_litpools = FALSE;
1006 auto_litpool_limit = -1;
1007 return 1;
1008
1009 case option_auto_litpool_limit:
1010 {
1011 int value = 0;
1012 char *end;
1013 if (auto_litpool_limit < 0)
1014 as_fatal (_("no-auto-litpools is incompatible with auto-litpool-limit"));
1015 if (*arg == 0 || *arg == '-')
1016 as_fatal (_("invalid auto-litpool-limit argument"));
1017 value = strtol (arg, &end, 10);
1018 if (*end != 0)
1019 as_fatal (_("invalid auto-litpool-limit argument"));
1020 if (value < 100 || value > 10000)
1021 as_fatal (_("invalid auto-litpool-limit argument (range is 100-10000)"));
1022 auto_litpool_limit = value;
1023 auto_litpools = TRUE;
1024 use_literal_section = FALSE;
1025 return 1;
1026 }
1027
1028 default:
1029 return 0;
1030 }
1031 }
1032
1033
1034 void
1035 md_show_usage (FILE *stream)
1036 {
1037 fputs ("\n\
1038 Xtensa options:\n\
1039 --[no-]text-section-literals\n\
1040 [Do not] put literals in the text section\n\
1041 --[no-]absolute-literals\n\
1042 [Do not] default to use non-PC-relative literals\n\
1043 --[no-]target-align [Do not] try to align branch targets\n\
1044 --[no-]longcalls [Do not] emit 32-bit call sequences\n\
1045 --[no-]transform [Do not] transform instructions\n\
1046 --flix both allow hand-written and generate flix bundles\n\
1047 --no-generate-flix allow hand-written but do not generate\n\
1048 flix bundles\n\
1049 --no-allow-flix neither allow hand-written nor generate\n\
1050 flix bundles\n\
1051 --rename-section old=new Rename section 'old' to 'new'\n\
1052 --[no-]trampolines [Do not] generate trampolines (jumps to jumps)\n\
1053 when jumps do not reach their targets\n\
1054 --[no-]auto-litpools [Do not] automatically create literal pools\n\
1055 --auto-litpool-limit=<value>\n\
1056 (range 100-10000) Maximum number of blocks of\n\
1057 instructions to emit between literal pool\n\
1058 locations; implies --auto-litpools flag\n", stream);
1059 }
1060
1061 \f
1062 /* Functions related to the list of current label symbols. */
1063
1064 static void
1065 xtensa_add_insn_label (symbolS *sym)
1066 {
1067 sym_list *l;
1068
1069 if (!free_insn_labels)
1070 l = (sym_list *) xmalloc (sizeof (sym_list));
1071 else
1072 {
1073 l = free_insn_labels;
1074 free_insn_labels = l->next;
1075 }
1076
1077 l->sym = sym;
1078 l->next = insn_labels;
1079 insn_labels = l;
1080 }
1081
1082
1083 static void
1084 xtensa_clear_insn_labels (void)
1085 {
1086 sym_list **pl;
1087
1088 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1089 ;
1090 *pl = insn_labels;
1091 insn_labels = NULL;
1092 }
1093
1094
1095 static void
1096 xtensa_move_labels (fragS *new_frag, valueT new_offset)
1097 {
1098 sym_list *lit;
1099
1100 for (lit = insn_labels; lit; lit = lit->next)
1101 {
1102 symbolS *lit_sym = lit->sym;
1103 S_SET_VALUE (lit_sym, new_offset);
1104 symbol_set_frag (lit_sym, new_frag);
1105 }
1106 }
1107
1108 \f
1109 /* Directive data and functions. */
1110
1111 typedef struct state_stackS_struct
1112 {
1113 directiveE directive;
1114 bfd_boolean negated;
1115 bfd_boolean old_state;
1116 const char *file;
1117 unsigned int line;
1118 const void *datum;
1119 struct state_stackS_struct *prev;
1120 } state_stackS;
1121
1122 state_stackS *directive_state_stack;
1123
1124 const pseudo_typeS md_pseudo_table[] =
1125 {
1126 { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0). */
1127 { "literal_position", xtensa_literal_position, 0 },
1128 { "frame", s_ignore, 0 }, /* Formerly used for STABS debugging. */
1129 { "long", xtensa_elf_cons, 4 },
1130 { "word", xtensa_elf_cons, 4 },
1131 { "4byte", xtensa_elf_cons, 4 },
1132 { "short", xtensa_elf_cons, 2 },
1133 { "2byte", xtensa_elf_cons, 2 },
1134 { "sleb128", xtensa_leb128, 1},
1135 { "uleb128", xtensa_leb128, 0},
1136 { "begin", xtensa_begin_directive, 0 },
1137 { "end", xtensa_end_directive, 0 },
1138 { "literal", xtensa_literal_pseudo, 0 },
1139 { "frequency", xtensa_frequency_pseudo, 0 },
1140 { NULL, 0, 0 },
1141 };
1142
1143
1144 static bfd_boolean
1145 use_transform (void)
1146 {
1147 /* After md_end, you should be checking frag by frag, rather
1148 than state directives. */
1149 gas_assert (!past_xtensa_end);
1150 return directive_state[directive_transform];
1151 }
1152
1153
1154 static bfd_boolean
1155 do_align_targets (void)
1156 {
1157 /* Do not use this function after md_end; just look at align_targets
1158 instead. There is no target-align directive, so alignment is either
1159 enabled for all frags or not done at all. */
1160 gas_assert (!past_xtensa_end);
1161 return align_targets && use_transform ();
1162 }
1163
1164
1165 static void
1166 directive_push (directiveE directive, bfd_boolean negated, const void *datum)
1167 {
1168 const char *file;
1169 unsigned int line;
1170 state_stackS *stack = (state_stackS *) xmalloc (sizeof (state_stackS));
1171
1172 file = as_where (&line);
1173
1174 stack->directive = directive;
1175 stack->negated = negated;
1176 stack->old_state = directive_state[directive];
1177 stack->file = file;
1178 stack->line = line;
1179 stack->datum = datum;
1180 stack->prev = directive_state_stack;
1181 directive_state_stack = stack;
1182
1183 directive_state[directive] = !negated;
1184 }
1185
1186
1187 static void
1188 directive_pop (directiveE *directive,
1189 bfd_boolean *negated,
1190 const char **file,
1191 unsigned int *line,
1192 const void **datum)
1193 {
1194 state_stackS *top = directive_state_stack;
1195
1196 if (!directive_state_stack)
1197 {
1198 as_bad (_("unmatched end directive"));
1199 *directive = directive_none;
1200 return;
1201 }
1202
1203 directive_state[directive_state_stack->directive] = top->old_state;
1204 *directive = top->directive;
1205 *negated = top->negated;
1206 *file = top->file;
1207 *line = top->line;
1208 *datum = top->datum;
1209 directive_state_stack = top->prev;
1210 free (top);
1211 }
1212
1213
1214 static void
1215 directive_balance (void)
1216 {
1217 while (directive_state_stack)
1218 {
1219 directiveE directive;
1220 bfd_boolean negated;
1221 const char *file;
1222 unsigned int line;
1223 const void *datum;
1224
1225 directive_pop (&directive, &negated, &file, &line, &datum);
1226 as_warn_where ((char *) file, line,
1227 _(".begin directive with no matching .end directive"));
1228 }
1229 }
1230
1231
1232 static bfd_boolean
1233 inside_directive (directiveE dir)
1234 {
1235 state_stackS *top = directive_state_stack;
1236
1237 while (top && top->directive != dir)
1238 top = top->prev;
1239
1240 return (top != NULL);
1241 }
1242
1243
1244 static void
1245 get_directive (directiveE *directive, bfd_boolean *negated)
1246 {
1247 int len;
1248 unsigned i;
1249 const char *directive_string;
1250
1251 if (strncmp (input_line_pointer, "no-", 3) != 0)
1252 *negated = FALSE;
1253 else
1254 {
1255 *negated = TRUE;
1256 input_line_pointer += 3;
1257 }
1258
1259 len = strspn (input_line_pointer,
1260 "abcdefghijklmnopqrstuvwxyz_-/0123456789.");
1261
1262 /* This code is a hack to make .begin [no-][generics|relax] exactly
1263 equivalent to .begin [no-]transform. We should remove it when
1264 we stop accepting those options. */
1265
1266 if (strncmp (input_line_pointer, "generics", strlen ("generics")) == 0)
1267 {
1268 as_warn (_("[no-]generics is deprecated; use [no-]transform instead"));
1269 directive_string = "transform";
1270 }
1271 else if (strncmp (input_line_pointer, "relax", strlen ("relax")) == 0)
1272 {
1273 as_warn (_("[no-]relax is deprecated; use [no-]transform instead"));
1274 directive_string = "transform";
1275 }
1276 else
1277 directive_string = input_line_pointer;
1278
1279 for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i)
1280 {
1281 if (strncmp (directive_string, directive_info[i].name, len) == 0)
1282 {
1283 input_line_pointer += len;
1284 *directive = (directiveE) i;
1285 if (*negated && !directive_info[i].can_be_negated)
1286 as_bad (_("directive %s cannot be negated"),
1287 directive_info[i].name);
1288 return;
1289 }
1290 }
1291
1292 as_bad (_("unknown directive"));
1293 *directive = (directiveE) XTENSA_UNDEFINED;
1294 }
1295
1296
1297 static void
1298 xtensa_begin_directive (int ignore ATTRIBUTE_UNUSED)
1299 {
1300 directiveE directive;
1301 bfd_boolean negated;
1302 emit_state *state;
1303 lit_state *ls;
1304
1305 get_directive (&directive, &negated);
1306 if (directive == (directiveE) XTENSA_UNDEFINED)
1307 {
1308 discard_rest_of_line ();
1309 return;
1310 }
1311
1312 if (cur_vinsn.inside_bundle)
1313 as_bad (_("directives are not valid inside bundles"));
1314
1315 switch (directive)
1316 {
1317 case directive_literal:
1318 if (!inside_directive (directive_literal))
1319 {
1320 /* Previous labels go with whatever follows this directive, not with
1321 the literal, so save them now. */
1322 saved_insn_labels = insn_labels;
1323 insn_labels = NULL;
1324 }
1325 as_warn (_(".begin literal is deprecated; use .literal instead"));
1326 state = (emit_state *) xmalloc (sizeof (emit_state));
1327 xtensa_switch_to_literal_fragment (state);
1328 directive_push (directive_literal, negated, state);
1329 break;
1330
1331 case directive_literal_prefix:
1332 /* Have to flush pending output because a movi relaxed to an l32r
1333 might produce a literal. */
1334 md_flush_pending_output ();
1335 /* Check to see if the current fragment is a literal
1336 fragment. If it is, then this operation is not allowed. */
1337 if (generating_literals)
1338 {
1339 as_bad (_("cannot set literal_prefix inside literal fragment"));
1340 return;
1341 }
1342
1343 /* Allocate the literal state for this section and push
1344 onto the directive stack. */
1345 ls = xmalloc (sizeof (lit_state));
1346 gas_assert (ls);
1347
1348 *ls = default_lit_sections;
1349 directive_push (directive_literal_prefix, negated, ls);
1350
1351 /* Process the new prefix. */
1352 xtensa_literal_prefix ();
1353 break;
1354
1355 case directive_freeregs:
1356 /* This information is currently unused, but we'll accept the statement
1357 and just discard the rest of the line. This won't check the syntax,
1358 but it will accept every correct freeregs directive. */
1359 input_line_pointer += strcspn (input_line_pointer, "\n");
1360 directive_push (directive_freeregs, negated, 0);
1361 break;
1362
1363 case directive_schedule:
1364 md_flush_pending_output ();
1365 frag_var (rs_fill, 0, 0, frag_now->fr_subtype,
1366 frag_now->fr_symbol, frag_now->fr_offset, NULL);
1367 directive_push (directive_schedule, negated, 0);
1368 xtensa_set_frag_assembly_state (frag_now);
1369 break;
1370
1371 case directive_density:
1372 as_warn (_(".begin [no-]density is ignored"));
1373 break;
1374
1375 case directive_absolute_literals:
1376 md_flush_pending_output ();
1377 if (!absolute_literals_supported && !negated)
1378 {
1379 as_warn (_("Xtensa absolute literals option not supported; ignored"));
1380 break;
1381 }
1382 xtensa_set_frag_assembly_state (frag_now);
1383 directive_push (directive, negated, 0);
1384 break;
1385
1386 default:
1387 md_flush_pending_output ();
1388 xtensa_set_frag_assembly_state (frag_now);
1389 directive_push (directive, negated, 0);
1390 break;
1391 }
1392
1393 demand_empty_rest_of_line ();
1394 }
1395
1396
1397 static void
1398 xtensa_end_directive (int ignore ATTRIBUTE_UNUSED)
1399 {
1400 directiveE begin_directive, end_directive;
1401 bfd_boolean begin_negated, end_negated;
1402 const char *file;
1403 unsigned int line;
1404 emit_state *state;
1405 emit_state **state_ptr;
1406 lit_state *s;
1407
1408 if (cur_vinsn.inside_bundle)
1409 as_bad (_("directives are not valid inside bundles"));
1410
1411 get_directive (&end_directive, &end_negated);
1412
1413 md_flush_pending_output ();
1414
1415 switch ((int) end_directive)
1416 {
1417 case XTENSA_UNDEFINED:
1418 discard_rest_of_line ();
1419 return;
1420
1421 case (int) directive_density:
1422 as_warn (_(".end [no-]density is ignored"));
1423 demand_empty_rest_of_line ();
1424 break;
1425
1426 case (int) directive_absolute_literals:
1427 if (!absolute_literals_supported && !end_negated)
1428 {
1429 as_warn (_("Xtensa absolute literals option not supported; ignored"));
1430 demand_empty_rest_of_line ();
1431 return;
1432 }
1433 break;
1434
1435 default:
1436 break;
1437 }
1438
1439 state_ptr = &state; /* use state_ptr to avoid type-punning warning */
1440 directive_pop (&begin_directive, &begin_negated, &file, &line,
1441 (const void **) state_ptr);
1442
1443 if (begin_directive != directive_none)
1444 {
1445 if (begin_directive != end_directive || begin_negated != end_negated)
1446 {
1447 as_bad (_("does not match begin %s%s at %s:%d"),
1448 begin_negated ? "no-" : "",
1449 directive_info[begin_directive].name, file, line);
1450 }
1451 else
1452 {
1453 switch (end_directive)
1454 {
1455 case directive_literal:
1456 frag_var (rs_fill, 0, 0, 0, NULL, 0, NULL);
1457 xtensa_restore_emit_state (state);
1458 xtensa_set_frag_assembly_state (frag_now);
1459 free (state);
1460 if (!inside_directive (directive_literal))
1461 {
1462 /* Restore the list of current labels. */
1463 xtensa_clear_insn_labels ();
1464 insn_labels = saved_insn_labels;
1465 }
1466 break;
1467
1468 case directive_literal_prefix:
1469 /* Restore the default collection sections from saved state. */
1470 s = (lit_state *) state;
1471 gas_assert (s);
1472 default_lit_sections = *s;
1473
1474 /* Free the state storage. */
1475 free (s->lit_prefix);
1476 free (s);
1477 break;
1478
1479 case directive_schedule:
1480 case directive_freeregs:
1481 break;
1482
1483 default:
1484 xtensa_set_frag_assembly_state (frag_now);
1485 break;
1486 }
1487 }
1488 }
1489
1490 demand_empty_rest_of_line ();
1491 }
1492
1493
1494 /* Place an aligned literal fragment at the current location. */
1495
1496 static void
1497 xtensa_literal_position (int ignore ATTRIBUTE_UNUSED)
1498 {
1499 md_flush_pending_output ();
1500
1501 if (inside_directive (directive_literal))
1502 as_warn (_(".literal_position inside literal directive; ignoring"));
1503 xtensa_mark_literal_pool_location ();
1504
1505 demand_empty_rest_of_line ();
1506 xtensa_clear_insn_labels ();
1507 }
1508
1509
1510 /* Support .literal label, expr, ... */
1511
1512 static void
1513 xtensa_literal_pseudo (int ignored ATTRIBUTE_UNUSED)
1514 {
1515 emit_state state;
1516 char *p, *base_name;
1517 char c;
1518 segT dest_seg;
1519
1520 if (inside_directive (directive_literal))
1521 {
1522 as_bad (_(".literal not allowed inside .begin literal region"));
1523 ignore_rest_of_line ();
1524 return;
1525 }
1526
1527 md_flush_pending_output ();
1528
1529 /* Previous labels go with whatever follows this directive, not with
1530 the literal, so save them now. */
1531 saved_insn_labels = insn_labels;
1532 insn_labels = NULL;
1533
1534 /* If we are using text-section literals, then this is the right value... */
1535 dest_seg = now_seg;
1536
1537 base_name = input_line_pointer;
1538
1539 xtensa_switch_to_literal_fragment (&state);
1540
1541 /* ...but if we aren't using text-section-literals, then we
1542 need to put them in the section we just switched to. */
1543 if (use_literal_section || directive_state[directive_absolute_literals])
1544 dest_seg = now_seg;
1545
1546 /* FIXME, despite the previous comments, dest_seg is unused... */
1547 (void) dest_seg;
1548
1549 /* All literals are aligned to four-byte boundaries. */
1550 frag_align (2, 0, 0);
1551 record_alignment (now_seg, 2);
1552
1553 c = get_symbol_name (&base_name);
1554 /* Just after name is now '\0'. */
1555 p = input_line_pointer;
1556 *p = c;
1557 SKIP_WHITESPACE_AFTER_NAME ();
1558
1559 if (*input_line_pointer != ',' && *input_line_pointer != ':')
1560 {
1561 as_bad (_("expected comma or colon after symbol name; "
1562 "rest of line ignored"));
1563 ignore_rest_of_line ();
1564 xtensa_restore_emit_state (&state);
1565 return;
1566 }
1567
1568 *p = 0;
1569 colon (base_name);
1570 *p = c;
1571
1572 input_line_pointer++; /* skip ',' or ':' */
1573
1574 xtensa_elf_cons (4);
1575
1576 xtensa_restore_emit_state (&state);
1577
1578 /* Restore the list of current labels. */
1579 xtensa_clear_insn_labels ();
1580 insn_labels = saved_insn_labels;
1581 }
1582
1583
1584 static void
1585 xtensa_literal_prefix (void)
1586 {
1587 char *name;
1588 int len;
1589
1590 /* Parse the new prefix from the input_line_pointer. */
1591 SKIP_WHITESPACE ();
1592 len = strspn (input_line_pointer,
1593 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1594 "abcdefghijklmnopqrstuvwxyz_/0123456789.$");
1595
1596 /* Get a null-terminated copy of the name. */
1597 name = xmalloc (len + 1);
1598 gas_assert (name);
1599 strncpy (name, input_line_pointer, len);
1600 name[len] = 0;
1601
1602 /* Skip the name in the input line. */
1603 input_line_pointer += len;
1604
1605 default_lit_sections.lit_prefix = name;
1606
1607 /* Clear cached literal sections, since the prefix has changed. */
1608 default_lit_sections.lit_seg = NULL;
1609 default_lit_sections.lit4_seg = NULL;
1610 }
1611
1612
1613 /* Support ".frequency branch_target_frequency fall_through_frequency". */
1614
1615 static void
1616 xtensa_frequency_pseudo (int ignored ATTRIBUTE_UNUSED)
1617 {
1618 float fall_through_f, target_f;
1619
1620 fall_through_f = (float) strtod (input_line_pointer, &input_line_pointer);
1621 if (fall_through_f < 0)
1622 {
1623 as_bad (_("fall through frequency must be greater than 0"));
1624 ignore_rest_of_line ();
1625 return;
1626 }
1627
1628 target_f = (float) strtod (input_line_pointer, &input_line_pointer);
1629 if (target_f < 0)
1630 {
1631 as_bad (_("branch target frequency must be greater than 0"));
1632 ignore_rest_of_line ();
1633 return;
1634 }
1635
1636 set_subseg_freq (now_seg, now_subseg, target_f + fall_through_f, target_f);
1637
1638 demand_empty_rest_of_line ();
1639 }
1640
1641
1642 /* Like normal .long/.short/.word, except support @plt, etc.
1643 Clobbers input_line_pointer, checks end-of-line. */
1644
1645 static void
1646 xtensa_elf_cons (int nbytes)
1647 {
1648 expressionS exp;
1649 bfd_reloc_code_real_type reloc;
1650
1651 md_flush_pending_output ();
1652
1653 if (cur_vinsn.inside_bundle)
1654 as_bad (_("directives are not valid inside bundles"));
1655
1656 if (is_it_end_of_statement ())
1657 {
1658 demand_empty_rest_of_line ();
1659 return;
1660 }
1661
1662 do
1663 {
1664 expression (&exp);
1665 if (exp.X_op == O_symbol
1666 && *input_line_pointer == '@'
1667 && ((reloc = xtensa_elf_suffix (&input_line_pointer, &exp))
1668 != BFD_RELOC_NONE))
1669 {
1670 reloc_howto_type *reloc_howto =
1671 bfd_reloc_type_lookup (stdoutput, reloc);
1672
1673 if (reloc == BFD_RELOC_UNUSED || !reloc_howto)
1674 as_bad (_("unsupported relocation"));
1675 else if ((reloc >= BFD_RELOC_XTENSA_SLOT0_OP
1676 && reloc <= BFD_RELOC_XTENSA_SLOT14_OP)
1677 || (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT
1678 && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT))
1679 as_bad (_("opcode-specific %s relocation used outside "
1680 "an instruction"), reloc_howto->name);
1681 else if (nbytes != (int) bfd_get_reloc_size (reloc_howto))
1682 as_bad (_("%s relocations do not fit in %d bytes"),
1683 reloc_howto->name, nbytes);
1684 else if (reloc == BFD_RELOC_XTENSA_TLS_FUNC
1685 || reloc == BFD_RELOC_XTENSA_TLS_ARG
1686 || reloc == BFD_RELOC_XTENSA_TLS_CALL)
1687 as_bad (_("invalid use of %s relocation"), reloc_howto->name);
1688 else
1689 {
1690 char *p = frag_more ((int) nbytes);
1691 xtensa_set_frag_assembly_state (frag_now);
1692 fix_new_exp (frag_now, p - frag_now->fr_literal,
1693 nbytes, &exp, reloc_howto->pc_relative, reloc);
1694 }
1695 }
1696 else
1697 {
1698 xtensa_set_frag_assembly_state (frag_now);
1699 emit_expr (&exp, (unsigned int) nbytes);
1700 }
1701 }
1702 while (*input_line_pointer++ == ',');
1703
1704 input_line_pointer--; /* Put terminator back into stream. */
1705 demand_empty_rest_of_line ();
1706 }
1707
1708 static bfd_boolean is_leb128_expr;
1709
1710 static void
1711 xtensa_leb128 (int sign)
1712 {
1713 is_leb128_expr = TRUE;
1714 s_leb128 (sign);
1715 is_leb128_expr = FALSE;
1716 }
1717
1718 \f
1719 /* Parsing and Idiom Translation. */
1720
1721 /* Parse @plt, etc. and return the desired relocation. */
1722 static bfd_reloc_code_real_type
1723 xtensa_elf_suffix (char **str_p, expressionS *exp_p)
1724 {
1725 char ident[20];
1726 char *str = *str_p;
1727 char *str2;
1728 int ch;
1729 int len;
1730 struct suffix_reloc_map *ptr;
1731
1732 if (*str++ != '@')
1733 return BFD_RELOC_NONE;
1734
1735 for (ch = *str, str2 = ident;
1736 (str2 < ident + sizeof (ident) - 1
1737 && (ISALNUM (ch) || ch == '@'));
1738 ch = *++str)
1739 {
1740 *str2++ = (ISLOWER (ch)) ? ch : TOLOWER (ch);
1741 }
1742
1743 *str2 = '\0';
1744 len = str2 - ident;
1745
1746 ch = ident[0];
1747 for (ptr = &suffix_relocs[0]; ptr->length > 0; ptr++)
1748 if (ch == ptr->suffix[0]
1749 && len == ptr->length
1750 && memcmp (ident, ptr->suffix, ptr->length) == 0)
1751 {
1752 /* Now check for "identifier@suffix+constant". */
1753 if (*str == '-' || *str == '+')
1754 {
1755 char *orig_line = input_line_pointer;
1756 expressionS new_exp;
1757
1758 input_line_pointer = str;
1759 expression (&new_exp);
1760 if (new_exp.X_op == O_constant)
1761 {
1762 exp_p->X_add_number += new_exp.X_add_number;
1763 str = input_line_pointer;
1764 }
1765
1766 if (&input_line_pointer != str_p)
1767 input_line_pointer = orig_line;
1768 }
1769
1770 *str_p = str;
1771 return ptr->reloc;
1772 }
1773
1774 return BFD_RELOC_UNUSED;
1775 }
1776
1777
1778 /* Find the matching operator type. */
1779 static unsigned char
1780 map_suffix_reloc_to_operator (bfd_reloc_code_real_type reloc)
1781 {
1782 struct suffix_reloc_map *sfx;
1783 unsigned char operator = (unsigned char) -1;
1784
1785 for (sfx = &suffix_relocs[0]; sfx->suffix; sfx++)
1786 {
1787 if (sfx->reloc == reloc)
1788 {
1789 operator = sfx->operator;
1790 break;
1791 }
1792 }
1793 gas_assert (operator != (unsigned char) -1);
1794 return operator;
1795 }
1796
1797
1798 /* Find the matching reloc type. */
1799 static bfd_reloc_code_real_type
1800 map_operator_to_reloc (unsigned char operator, bfd_boolean is_literal)
1801 {
1802 struct suffix_reloc_map *sfx;
1803 bfd_reloc_code_real_type reloc = BFD_RELOC_UNUSED;
1804
1805 for (sfx = &suffix_relocs[0]; sfx->suffix; sfx++)
1806 {
1807 if (sfx->operator == operator)
1808 {
1809 reloc = sfx->reloc;
1810 break;
1811 }
1812 }
1813
1814 if (is_literal)
1815 {
1816 if (reloc == BFD_RELOC_XTENSA_TLS_FUNC)
1817 return BFD_RELOC_XTENSA_TLSDESC_FN;
1818 else if (reloc == BFD_RELOC_XTENSA_TLS_ARG)
1819 return BFD_RELOC_XTENSA_TLSDESC_ARG;
1820 }
1821
1822 if (reloc == BFD_RELOC_UNUSED)
1823 return BFD_RELOC_32;
1824
1825 return reloc;
1826 }
1827
1828
1829 static const char *
1830 expression_end (const char *name)
1831 {
1832 while (1)
1833 {
1834 switch (*name)
1835 {
1836 case '}':
1837 case ';':
1838 case '\0':
1839 case ',':
1840 case ':':
1841 return name;
1842 case ' ':
1843 case '\t':
1844 ++name;
1845 continue;
1846 default:
1847 return 0;
1848 }
1849 }
1850 }
1851
1852
1853 #define ERROR_REG_NUM ((unsigned) -1)
1854
1855 static unsigned
1856 tc_get_register (const char *prefix)
1857 {
1858 unsigned reg;
1859 const char *next_expr;
1860 const char *old_line_pointer;
1861
1862 SKIP_WHITESPACE ();
1863 old_line_pointer = input_line_pointer;
1864
1865 if (*input_line_pointer == '$')
1866 ++input_line_pointer;
1867
1868 /* Accept "sp" as a synonym for "a1". */
1869 if (input_line_pointer[0] == 's' && input_line_pointer[1] == 'p'
1870 && expression_end (input_line_pointer + 2))
1871 {
1872 input_line_pointer += 2;
1873 return 1; /* AR[1] */
1874 }
1875
1876 while (*input_line_pointer++ == *prefix++)
1877 ;
1878 --input_line_pointer;
1879 --prefix;
1880
1881 if (*prefix)
1882 {
1883 as_bad (_("bad register name: %s"), old_line_pointer);
1884 return ERROR_REG_NUM;
1885 }
1886
1887 if (!ISDIGIT ((unsigned char) *input_line_pointer))
1888 {
1889 as_bad (_("bad register number: %s"), input_line_pointer);
1890 return ERROR_REG_NUM;
1891 }
1892
1893 reg = 0;
1894
1895 while (ISDIGIT ((int) *input_line_pointer))
1896 reg = reg * 10 + *input_line_pointer++ - '0';
1897
1898 if (!(next_expr = expression_end (input_line_pointer)))
1899 {
1900 as_bad (_("bad register name: %s"), old_line_pointer);
1901 return ERROR_REG_NUM;
1902 }
1903
1904 input_line_pointer = (char *) next_expr;
1905
1906 return reg;
1907 }
1908
1909
1910 static void
1911 expression_maybe_register (xtensa_opcode opc, int opnd, expressionS *tok)
1912 {
1913 xtensa_isa isa = xtensa_default_isa;
1914
1915 /* Check if this is an immediate operand. */
1916 if (xtensa_operand_is_register (isa, opc, opnd) == 0)
1917 {
1918 bfd_reloc_code_real_type reloc;
1919 segT t = expression (tok);
1920
1921 if (t == absolute_section
1922 && xtensa_operand_is_PCrelative (isa, opc, opnd) == 1)
1923 {
1924 gas_assert (tok->X_op == O_constant);
1925 tok->X_op = O_symbol;
1926 tok->X_add_symbol = &abs_symbol;
1927 }
1928
1929 if ((tok->X_op == O_constant || tok->X_op == O_symbol)
1930 && ((reloc = xtensa_elf_suffix (&input_line_pointer, tok))
1931 != BFD_RELOC_NONE))
1932 {
1933 switch (reloc)
1934 {
1935 case BFD_RELOC_LO16:
1936 if (tok->X_op == O_constant)
1937 {
1938 tok->X_add_number &= 0xffff;
1939 return;
1940 }
1941 break;
1942 case BFD_RELOC_HI16:
1943 if (tok->X_op == O_constant)
1944 {
1945 tok->X_add_number = ((unsigned) tok->X_add_number) >> 16;
1946 return;
1947 }
1948 break;
1949 case BFD_RELOC_UNUSED:
1950 as_bad (_("unsupported relocation"));
1951 return;
1952 case BFD_RELOC_32_PCREL:
1953 as_bad (_("pcrel relocation not allowed in an instruction"));
1954 return;
1955 default:
1956 break;
1957 }
1958 tok->X_op = map_suffix_reloc_to_operator (reloc);
1959 }
1960 }
1961 else
1962 {
1963 xtensa_regfile opnd_rf = xtensa_operand_regfile (isa, opc, opnd);
1964 unsigned reg = tc_get_register (xtensa_regfile_shortname (isa, opnd_rf));
1965
1966 if (reg != ERROR_REG_NUM) /* Already errored */
1967 {
1968 uint32 buf = reg;
1969 if (xtensa_operand_encode (isa, opc, opnd, &buf))
1970 as_bad (_("register number out of range"));
1971 }
1972
1973 tok->X_op = O_register;
1974 tok->X_add_symbol = 0;
1975 tok->X_add_number = reg;
1976 }
1977 }
1978
1979
1980 /* Split up the arguments for an opcode or pseudo-op. */
1981
1982 static int
1983 tokenize_arguments (char **args, char *str)
1984 {
1985 char *old_input_line_pointer;
1986 bfd_boolean saw_comma = FALSE;
1987 bfd_boolean saw_arg = FALSE;
1988 bfd_boolean saw_colon = FALSE;
1989 int num_args = 0;
1990 char *arg_end, *arg;
1991 int arg_len;
1992
1993 /* Save and restore input_line_pointer around this function. */
1994 old_input_line_pointer = input_line_pointer;
1995 input_line_pointer = str;
1996
1997 while (*input_line_pointer)
1998 {
1999 SKIP_WHITESPACE ();
2000 switch (*input_line_pointer)
2001 {
2002 case '\0':
2003 case '}':
2004 goto fini;
2005
2006 case ':':
2007 input_line_pointer++;
2008 if (saw_comma || saw_colon || !saw_arg)
2009 goto err;
2010 saw_colon = TRUE;
2011 break;
2012
2013 case ',':
2014 input_line_pointer++;
2015 if (saw_comma || saw_colon || !saw_arg)
2016 goto err;
2017 saw_comma = TRUE;
2018 break;
2019
2020 default:
2021 if (!saw_comma && !saw_colon && saw_arg)
2022 goto err;
2023
2024 arg_end = input_line_pointer + 1;
2025 while (!expression_end (arg_end))
2026 arg_end += 1;
2027
2028 arg_len = arg_end - input_line_pointer;
2029 arg = (char *) xmalloc ((saw_colon ? 1 : 0) + arg_len + 1);
2030 args[num_args] = arg;
2031
2032 if (saw_colon)
2033 *arg++ = ':';
2034 strncpy (arg, input_line_pointer, arg_len);
2035 arg[arg_len] = '\0';
2036
2037 input_line_pointer = arg_end;
2038 num_args += 1;
2039 saw_comma = FALSE;
2040 saw_colon = FALSE;
2041 saw_arg = TRUE;
2042 break;
2043 }
2044 }
2045
2046 fini:
2047 if (saw_comma || saw_colon)
2048 goto err;
2049 input_line_pointer = old_input_line_pointer;
2050 return num_args;
2051
2052 err:
2053 if (saw_comma)
2054 as_bad (_("extra comma"));
2055 else if (saw_colon)
2056 as_bad (_("extra colon"));
2057 else if (!saw_arg)
2058 as_bad (_("missing argument"));
2059 else
2060 as_bad (_("missing comma or colon"));
2061 input_line_pointer = old_input_line_pointer;
2062 return -1;
2063 }
2064
2065
2066 /* Parse the arguments to an opcode. Return TRUE on error. */
2067
2068 static bfd_boolean
2069 parse_arguments (TInsn *insn, int num_args, char **arg_strings)
2070 {
2071 expressionS *tok, *last_tok;
2072 xtensa_opcode opcode = insn->opcode;
2073 bfd_boolean had_error = TRUE;
2074 xtensa_isa isa = xtensa_default_isa;
2075 int n, num_regs = 0;
2076 int opcode_operand_count;
2077 int opnd_cnt, last_opnd_cnt;
2078 unsigned int next_reg = 0;
2079 char *old_input_line_pointer;
2080
2081 if (insn->insn_type == ITYPE_LITERAL)
2082 opcode_operand_count = 1;
2083 else
2084 opcode_operand_count = xtensa_opcode_num_operands (isa, opcode);
2085
2086 tok = insn->tok;
2087 memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS);
2088
2089 /* Save and restore input_line_pointer around this function. */
2090 old_input_line_pointer = input_line_pointer;
2091
2092 last_tok = 0;
2093 last_opnd_cnt = -1;
2094 opnd_cnt = 0;
2095
2096 /* Skip invisible operands. */
2097 while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0)
2098 {
2099 opnd_cnt += 1;
2100 tok++;
2101 }
2102
2103 for (n = 0; n < num_args; n++)
2104 {
2105 input_line_pointer = arg_strings[n];
2106 if (*input_line_pointer == ':')
2107 {
2108 xtensa_regfile opnd_rf;
2109 input_line_pointer++;
2110 if (num_regs == 0)
2111 goto err;
2112 gas_assert (opnd_cnt > 0);
2113 num_regs--;
2114 opnd_rf = xtensa_operand_regfile (isa, opcode, last_opnd_cnt);
2115 if (next_reg
2116 != tc_get_register (xtensa_regfile_shortname (isa, opnd_rf)))
2117 as_warn (_("incorrect register number, ignoring"));
2118 next_reg++;
2119 }
2120 else
2121 {
2122 if (opnd_cnt >= opcode_operand_count)
2123 {
2124 as_warn (_("too many arguments"));
2125 goto err;
2126 }
2127 gas_assert (opnd_cnt < MAX_INSN_ARGS);
2128
2129 expression_maybe_register (opcode, opnd_cnt, tok);
2130 next_reg = tok->X_add_number + 1;
2131
2132 if (tok->X_op == O_illegal || tok->X_op == O_absent)
2133 goto err;
2134 if (xtensa_operand_is_register (isa, opcode, opnd_cnt) == 1)
2135 {
2136 num_regs = xtensa_operand_num_regs (isa, opcode, opnd_cnt) - 1;
2137 /* minus 1 because we are seeing one right now */
2138 }
2139 else
2140 num_regs = 0;
2141
2142 last_tok = tok;
2143 last_opnd_cnt = opnd_cnt;
2144 demand_empty_rest_of_line ();
2145
2146 do
2147 {
2148 opnd_cnt += 1;
2149 tok++;
2150 }
2151 while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0);
2152 }
2153 }
2154
2155 if (num_regs > 0 && ((int) next_reg != last_tok->X_add_number + 1))
2156 goto err;
2157
2158 insn->ntok = tok - insn->tok;
2159 had_error = FALSE;
2160
2161 err:
2162 input_line_pointer = old_input_line_pointer;
2163 return had_error;
2164 }
2165
2166
2167 static int
2168 get_invisible_operands (TInsn *insn)
2169 {
2170 xtensa_isa isa = xtensa_default_isa;
2171 static xtensa_insnbuf slotbuf = NULL;
2172 xtensa_format fmt;
2173 xtensa_opcode opc = insn->opcode;
2174 int slot, opnd, fmt_found;
2175 unsigned val;
2176
2177 if (!slotbuf)
2178 slotbuf = xtensa_insnbuf_alloc (isa);
2179
2180 /* Find format/slot where this can be encoded. */
2181 fmt_found = 0;
2182 slot = 0;
2183 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
2184 {
2185 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
2186 {
2187 if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opc) == 0)
2188 {
2189 fmt_found = 1;
2190 break;
2191 }
2192 }
2193 if (fmt_found) break;
2194 }
2195
2196 if (!fmt_found)
2197 {
2198 as_bad (_("cannot encode opcode \"%s\""), xtensa_opcode_name (isa, opc));
2199 return -1;
2200 }
2201
2202 /* First encode all the visible operands
2203 (to deal with shared field operands). */
2204 for (opnd = 0; opnd < insn->ntok; opnd++)
2205 {
2206 if (xtensa_operand_is_visible (isa, opc, opnd) == 1
2207 && (insn->tok[opnd].X_op == O_register
2208 || insn->tok[opnd].X_op == O_constant))
2209 {
2210 val = insn->tok[opnd].X_add_number;
2211 xtensa_operand_encode (isa, opc, opnd, &val);
2212 xtensa_operand_set_field (isa, opc, opnd, fmt, slot, slotbuf, val);
2213 }
2214 }
2215
2216 /* Then pull out the values for the invisible ones. */
2217 for (opnd = 0; opnd < insn->ntok; opnd++)
2218 {
2219 if (xtensa_operand_is_visible (isa, opc, opnd) == 0)
2220 {
2221 xtensa_operand_get_field (isa, opc, opnd, fmt, slot, slotbuf, &val);
2222 xtensa_operand_decode (isa, opc, opnd, &val);
2223 insn->tok[opnd].X_add_number = val;
2224 if (xtensa_operand_is_register (isa, opc, opnd) == 1)
2225 insn->tok[opnd].X_op = O_register;
2226 else
2227 insn->tok[opnd].X_op = O_constant;
2228 }
2229 }
2230
2231 return 0;
2232 }
2233
2234
2235 static void
2236 xg_reverse_shift_count (char **cnt_argp)
2237 {
2238 char *cnt_arg, *new_arg;
2239 cnt_arg = *cnt_argp;
2240
2241 /* replace the argument with "31-(argument)" */
2242 new_arg = (char *) xmalloc (strlen (cnt_arg) + 6);
2243 sprintf (new_arg, "31-(%s)", cnt_arg);
2244
2245 free (cnt_arg);
2246 *cnt_argp = new_arg;
2247 }
2248
2249
2250 /* If "arg" is a constant expression, return non-zero with the value
2251 in *valp. */
2252
2253 static int
2254 xg_arg_is_constant (char *arg, offsetT *valp)
2255 {
2256 expressionS exp;
2257 char *save_ptr = input_line_pointer;
2258
2259 input_line_pointer = arg;
2260 expression (&exp);
2261 input_line_pointer = save_ptr;
2262
2263 if (exp.X_op == O_constant)
2264 {
2265 *valp = exp.X_add_number;
2266 return 1;
2267 }
2268
2269 return 0;
2270 }
2271
2272
2273 static void
2274 xg_replace_opname (char **popname, const char *newop)
2275 {
2276 free (*popname);
2277 *popname = xstrdup (newop);
2278 }
2279
2280
2281 static int
2282 xg_check_num_args (int *pnum_args,
2283 int expected_num,
2284 char *opname,
2285 char **arg_strings)
2286 {
2287 int num_args = *pnum_args;
2288
2289 if (num_args < expected_num)
2290 {
2291 as_bad (_("not enough operands (%d) for '%s'; expected %d"),
2292 num_args, opname, expected_num);
2293 return -1;
2294 }
2295
2296 if (num_args > expected_num)
2297 {
2298 as_warn (_("too many operands (%d) for '%s'; expected %d"),
2299 num_args, opname, expected_num);
2300 while (num_args-- > expected_num)
2301 {
2302 free (arg_strings[num_args]);
2303 arg_strings[num_args] = 0;
2304 }
2305 *pnum_args = expected_num;
2306 return -1;
2307 }
2308
2309 return 0;
2310 }
2311
2312
2313 /* If the register is not specified as part of the opcode,
2314 then get it from the operand and move it to the opcode. */
2315
2316 static int
2317 xg_translate_sysreg_op (char **popname, int *pnum_args, char **arg_strings)
2318 {
2319 xtensa_isa isa = xtensa_default_isa;
2320 xtensa_sysreg sr;
2321 char *opname, *new_opname;
2322 const char *sr_name;
2323 int is_user, is_write;
2324
2325 opname = *popname;
2326 if (*opname == '_')
2327 opname += 1;
2328 is_user = (opname[1] == 'u');
2329 is_write = (opname[0] == 'w');
2330
2331 /* Opname == [rw]ur or [rwx]sr... */
2332
2333 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2334 return -1;
2335
2336 /* Check if the argument is a symbolic register name. */
2337 sr = xtensa_sysreg_lookup_name (isa, arg_strings[1]);
2338 /* Handle WSR to "INTSET" as a special case. */
2339 if (sr == XTENSA_UNDEFINED && is_write && !is_user
2340 && !strcasecmp (arg_strings[1], "intset"))
2341 sr = xtensa_sysreg_lookup_name (isa, "interrupt");
2342 if (sr == XTENSA_UNDEFINED
2343 || (xtensa_sysreg_is_user (isa, sr) == 1) != is_user)
2344 {
2345 /* Maybe it's a register number.... */
2346 offsetT val;
2347 if (!xg_arg_is_constant (arg_strings[1], &val))
2348 {
2349 as_bad (_("invalid register '%s' for '%s' instruction"),
2350 arg_strings[1], opname);
2351 return -1;
2352 }
2353 sr = xtensa_sysreg_lookup (isa, val, is_user);
2354 if (sr == XTENSA_UNDEFINED)
2355 {
2356 as_bad (_("invalid register number (%ld) for '%s' instruction"),
2357 (long) val, opname);
2358 return -1;
2359 }
2360 }
2361
2362 /* Remove the last argument, which is now part of the opcode. */
2363 free (arg_strings[1]);
2364 arg_strings[1] = 0;
2365 *pnum_args = 1;
2366
2367 /* Translate the opcode. */
2368 sr_name = xtensa_sysreg_name (isa, sr);
2369 /* Another special case for "WSR.INTSET".... */
2370 if (is_write && !is_user && !strcasecmp ("interrupt", sr_name))
2371 sr_name = "intset";
2372 new_opname = concat (*popname, ".", sr_name, (char *) NULL);
2373 free (*popname);
2374 *popname = new_opname;
2375
2376 return 0;
2377 }
2378
2379
2380 static int
2381 xtensa_translate_old_userreg_ops (char **popname)
2382 {
2383 xtensa_isa isa = xtensa_default_isa;
2384 xtensa_sysreg sr;
2385 char *opname, *new_opname;
2386 const char *sr_name;
2387 bfd_boolean has_underbar = FALSE;
2388
2389 opname = *popname;
2390 if (opname[0] == '_')
2391 {
2392 has_underbar = TRUE;
2393 opname += 1;
2394 }
2395
2396 sr = xtensa_sysreg_lookup_name (isa, opname + 1);
2397 if (sr != XTENSA_UNDEFINED)
2398 {
2399 /* The new default name ("nnn") is different from the old default
2400 name ("URnnn"). The old default is handled below, and we don't
2401 want to recognize [RW]nnn, so do nothing if the name is the (new)
2402 default. */
2403 static char namebuf[10];
2404 sprintf (namebuf, "%d", xtensa_sysreg_number (isa, sr));
2405 if (strcmp (namebuf, opname + 1) == 0)
2406 return 0;
2407 }
2408 else
2409 {
2410 offsetT val;
2411 char *end;
2412
2413 /* Only continue if the reg name is "URnnn". */
2414 if (opname[1] != 'u' || opname[2] != 'r')
2415 return 0;
2416 val = strtoul (opname + 3, &end, 10);
2417 if (*end != '\0')
2418 return 0;
2419
2420 sr = xtensa_sysreg_lookup (isa, val, 1);
2421 if (sr == XTENSA_UNDEFINED)
2422 {
2423 as_bad (_("invalid register number (%ld) for '%s'"),
2424 (long) val, opname);
2425 return -1;
2426 }
2427 }
2428
2429 /* Translate the opcode. */
2430 sr_name = xtensa_sysreg_name (isa, sr);
2431 new_opname = (char *) xmalloc (strlen (sr_name) + 6);
2432 sprintf (new_opname, "%s%cur.%s", (has_underbar ? "_" : ""),
2433 opname[0], sr_name);
2434 free (*popname);
2435 *popname = new_opname;
2436
2437 return 0;
2438 }
2439
2440
2441 static int
2442 xtensa_translate_zero_immed (const char *old_op,
2443 const char *new_op,
2444 char **popname,
2445 int *pnum_args,
2446 char **arg_strings)
2447 {
2448 char *opname;
2449 offsetT val;
2450
2451 opname = *popname;
2452 gas_assert (opname[0] != '_');
2453
2454 if (strcmp (opname, old_op) != 0)
2455 return 0;
2456
2457 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2458 return -1;
2459 if (xg_arg_is_constant (arg_strings[1], &val) && val == 0)
2460 {
2461 xg_replace_opname (popname, new_op);
2462 free (arg_strings[1]);
2463 arg_strings[1] = arg_strings[2];
2464 arg_strings[2] = 0;
2465 *pnum_args = 2;
2466 }
2467
2468 return 0;
2469 }
2470
2471
2472 /* If the instruction is an idiom (i.e., a built-in macro), translate it.
2473 Returns non-zero if an error was found. */
2474
2475 static int
2476 xg_translate_idioms (char **popname, int *pnum_args, char **arg_strings)
2477 {
2478 char *opname = *popname;
2479 bfd_boolean has_underbar = FALSE;
2480
2481 if (*opname == '_')
2482 {
2483 has_underbar = TRUE;
2484 opname += 1;
2485 }
2486
2487 if (strcmp (opname, "mov") == 0)
2488 {
2489 if (use_transform () && !has_underbar && density_supported)
2490 xg_replace_opname (popname, "mov.n");
2491 else
2492 {
2493 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2494 return -1;
2495 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2496 arg_strings[2] = xstrdup (arg_strings[1]);
2497 *pnum_args = 3;
2498 }
2499 return 0;
2500 }
2501
2502 if (strcmp (opname, "bbsi.l") == 0)
2503 {
2504 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2505 return -1;
2506 xg_replace_opname (popname, (has_underbar ? "_bbsi" : "bbsi"));
2507 if (target_big_endian)
2508 xg_reverse_shift_count (&arg_strings[1]);
2509 return 0;
2510 }
2511
2512 if (strcmp (opname, "bbci.l") == 0)
2513 {
2514 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
2515 return -1;
2516 xg_replace_opname (popname, (has_underbar ? "_bbci" : "bbci"));
2517 if (target_big_endian)
2518 xg_reverse_shift_count (&arg_strings[1]);
2519 return 0;
2520 }
2521
2522 /* Don't do anything special with NOPs inside FLIX instructions. They
2523 are handled elsewhere. Real NOP instructions are always available
2524 in configurations with FLIX, so this should never be an issue but
2525 check for it anyway. */
2526 if (!cur_vinsn.inside_bundle && xtensa_nop_opcode == XTENSA_UNDEFINED
2527 && strcmp (opname, "nop") == 0)
2528 {
2529 if (use_transform () && !has_underbar && density_supported)
2530 xg_replace_opname (popname, "nop.n");
2531 else
2532 {
2533 if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
2534 return -1;
2535 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2536 arg_strings[0] = xstrdup ("a1");
2537 arg_strings[1] = xstrdup ("a1");
2538 arg_strings[2] = xstrdup ("a1");
2539 *pnum_args = 3;
2540 }
2541 return 0;
2542 }
2543
2544 /* Recognize [RW]UR and [RWX]SR. */
2545 if ((((opname[0] == 'r' || opname[0] == 'w')
2546 && (opname[1] == 'u' || opname[1] == 's'))
2547 || (opname[0] == 'x' && opname[1] == 's'))
2548 && opname[2] == 'r'
2549 && opname[3] == '\0')
2550 return xg_translate_sysreg_op (popname, pnum_args, arg_strings);
2551
2552 /* Backward compatibility for RUR and WUR: Recognize [RW]UR<nnn> and
2553 [RW]<name> if <name> is the non-default name of a user register. */
2554 if ((opname[0] == 'r' || opname[0] == 'w')
2555 && xtensa_opcode_lookup (xtensa_default_isa, opname) == XTENSA_UNDEFINED)
2556 return xtensa_translate_old_userreg_ops (popname);
2557
2558 /* Relax branches that don't allow comparisons against an immediate value
2559 of zero to the corresponding branches with implicit zero immediates. */
2560 if (!has_underbar && use_transform ())
2561 {
2562 if (xtensa_translate_zero_immed ("bnei", "bnez", popname,
2563 pnum_args, arg_strings))
2564 return -1;
2565
2566 if (xtensa_translate_zero_immed ("beqi", "beqz", popname,
2567 pnum_args, arg_strings))
2568 return -1;
2569
2570 if (xtensa_translate_zero_immed ("bgei", "bgez", popname,
2571 pnum_args, arg_strings))
2572 return -1;
2573
2574 if (xtensa_translate_zero_immed ("blti", "bltz", popname,
2575 pnum_args, arg_strings))
2576 return -1;
2577 }
2578
2579 return 0;
2580 }
2581
2582 \f
2583 /* Functions for dealing with the Xtensa ISA. */
2584
2585 /* Currently the assembler only allows us to use a single target per
2586 fragment. Because of this, only one operand for a given
2587 instruction may be symbolic. If there is a PC-relative operand,
2588 the last one is chosen. Otherwise, the result is the number of the
2589 last immediate operand, and if there are none of those, we fail and
2590 return -1. */
2591
2592 static int
2593 get_relaxable_immed (xtensa_opcode opcode)
2594 {
2595 int last_immed = -1;
2596 int noperands, opi;
2597
2598 if (opcode == XTENSA_UNDEFINED)
2599 return -1;
2600
2601 noperands = xtensa_opcode_num_operands (xtensa_default_isa, opcode);
2602 for (opi = noperands - 1; opi >= 0; opi--)
2603 {
2604 if (xtensa_operand_is_visible (xtensa_default_isa, opcode, opi) == 0)
2605 continue;
2606 if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, opi) == 1)
2607 return opi;
2608 if (last_immed == -1
2609 && xtensa_operand_is_register (xtensa_default_isa, opcode, opi) == 0)
2610 last_immed = opi;
2611 }
2612 return last_immed;
2613 }
2614
2615
2616 static xtensa_opcode
2617 get_opcode_from_buf (const char *buf, int slot)
2618 {
2619 static xtensa_insnbuf insnbuf = NULL;
2620 static xtensa_insnbuf slotbuf = NULL;
2621 xtensa_isa isa = xtensa_default_isa;
2622 xtensa_format fmt;
2623
2624 if (!insnbuf)
2625 {
2626 insnbuf = xtensa_insnbuf_alloc (isa);
2627 slotbuf = xtensa_insnbuf_alloc (isa);
2628 }
2629
2630 xtensa_insnbuf_from_chars (isa, insnbuf, (const unsigned char *) buf, 0);
2631 fmt = xtensa_format_decode (isa, insnbuf);
2632 if (fmt == XTENSA_UNDEFINED)
2633 return XTENSA_UNDEFINED;
2634
2635 if (slot >= xtensa_format_num_slots (isa, fmt))
2636 return XTENSA_UNDEFINED;
2637
2638 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
2639 return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
2640 }
2641
2642
2643 #ifdef TENSILICA_DEBUG
2644
2645 /* For debugging, print out the mapping of opcode numbers to opcodes. */
2646
2647 static void
2648 xtensa_print_insn_table (void)
2649 {
2650 int num_opcodes, num_operands;
2651 xtensa_opcode opcode;
2652 xtensa_isa isa = xtensa_default_isa;
2653
2654 num_opcodes = xtensa_isa_num_opcodes (xtensa_default_isa);
2655 for (opcode = 0; opcode < num_opcodes; opcode++)
2656 {
2657 int opn;
2658 fprintf (stderr, "%d: %s: ", opcode, xtensa_opcode_name (isa, opcode));
2659 num_operands = xtensa_opcode_num_operands (isa, opcode);
2660 for (opn = 0; opn < num_operands; opn++)
2661 {
2662 if (xtensa_operand_is_visible (isa, opcode, opn) == 0)
2663 continue;
2664 if (xtensa_operand_is_register (isa, opcode, opn) == 1)
2665 {
2666 xtensa_regfile opnd_rf =
2667 xtensa_operand_regfile (isa, opcode, opn);
2668 fprintf (stderr, "%s ", xtensa_regfile_shortname (isa, opnd_rf));
2669 }
2670 else if (xtensa_operand_is_PCrelative (isa, opcode, opn) == 1)
2671 fputs ("[lLr] ", stderr);
2672 else
2673 fputs ("i ", stderr);
2674 }
2675 fprintf (stderr, "\n");
2676 }
2677 }
2678
2679
2680 static void
2681 print_vliw_insn (xtensa_insnbuf vbuf)
2682 {
2683 xtensa_isa isa = xtensa_default_isa;
2684 xtensa_format f = xtensa_format_decode (isa, vbuf);
2685 xtensa_insnbuf sbuf = xtensa_insnbuf_alloc (isa);
2686 int op;
2687
2688 fprintf (stderr, "format = %d\n", f);
2689
2690 for (op = 0; op < xtensa_format_num_slots (isa, f); op++)
2691 {
2692 xtensa_opcode opcode;
2693 const char *opname;
2694 int operands;
2695
2696 xtensa_format_get_slot (isa, f, op, vbuf, sbuf);
2697 opcode = xtensa_opcode_decode (isa, f, op, sbuf);
2698 opname = xtensa_opcode_name (isa, opcode);
2699
2700 fprintf (stderr, "op in slot %i is %s;\n", op, opname);
2701 fprintf (stderr, " operands = ");
2702 for (operands = 0;
2703 operands < xtensa_opcode_num_operands (isa, opcode);
2704 operands++)
2705 {
2706 unsigned int val;
2707 if (xtensa_operand_is_visible (isa, opcode, operands) == 0)
2708 continue;
2709 xtensa_operand_get_field (isa, opcode, operands, f, op, sbuf, &val);
2710 xtensa_operand_decode (isa, opcode, operands, &val);
2711 fprintf (stderr, "%d ", val);
2712 }
2713 fprintf (stderr, "\n");
2714 }
2715 xtensa_insnbuf_free (isa, sbuf);
2716 }
2717
2718 #endif /* TENSILICA_DEBUG */
2719
2720
2721 static bfd_boolean
2722 is_direct_call_opcode (xtensa_opcode opcode)
2723 {
2724 xtensa_isa isa = xtensa_default_isa;
2725 int n, num_operands;
2726
2727 if (xtensa_opcode_is_call (isa, opcode) != 1)
2728 return FALSE;
2729
2730 num_operands = xtensa_opcode_num_operands (isa, opcode);
2731 for (n = 0; n < num_operands; n++)
2732 {
2733 if (xtensa_operand_is_register (isa, opcode, n) == 0
2734 && xtensa_operand_is_PCrelative (isa, opcode, n) == 1)
2735 return TRUE;
2736 }
2737 return FALSE;
2738 }
2739
2740
2741 /* Convert from BFD relocation type code to slot and operand number.
2742 Returns non-zero on failure. */
2743
2744 static int
2745 decode_reloc (bfd_reloc_code_real_type reloc, int *slot, bfd_boolean *is_alt)
2746 {
2747 if (reloc >= BFD_RELOC_XTENSA_SLOT0_OP
2748 && reloc <= BFD_RELOC_XTENSA_SLOT14_OP)
2749 {
2750 *slot = reloc - BFD_RELOC_XTENSA_SLOT0_OP;
2751 *is_alt = FALSE;
2752 }
2753 else if (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT
2754 && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT)
2755 {
2756 *slot = reloc - BFD_RELOC_XTENSA_SLOT0_ALT;
2757 *is_alt = TRUE;
2758 }
2759 else
2760 return -1;
2761
2762 return 0;
2763 }
2764
2765
2766 /* Convert from slot number to BFD relocation type code for the
2767 standard PC-relative relocations. Return BFD_RELOC_NONE on
2768 failure. */
2769
2770 static bfd_reloc_code_real_type
2771 encode_reloc (int slot)
2772 {
2773 if (slot < 0 || slot > 14)
2774 return BFD_RELOC_NONE;
2775
2776 return BFD_RELOC_XTENSA_SLOT0_OP + slot;
2777 }
2778
2779
2780 /* Convert from slot numbers to BFD relocation type code for the
2781 "alternate" relocations. Return BFD_RELOC_NONE on failure. */
2782
2783 static bfd_reloc_code_real_type
2784 encode_alt_reloc (int slot)
2785 {
2786 if (slot < 0 || slot > 14)
2787 return BFD_RELOC_NONE;
2788
2789 return BFD_RELOC_XTENSA_SLOT0_ALT + slot;
2790 }
2791
2792
2793 static void
2794 xtensa_insnbuf_set_operand (xtensa_insnbuf slotbuf,
2795 xtensa_format fmt,
2796 int slot,
2797 xtensa_opcode opcode,
2798 int operand,
2799 uint32 value,
2800 const char *file,
2801 unsigned int line)
2802 {
2803 uint32 valbuf = value;
2804
2805 if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf))
2806 {
2807 if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, operand)
2808 == 1)
2809 as_bad_where ((char *) file, line,
2810 _("operand %d of '%s' has out of range value '%u'"),
2811 operand + 1,
2812 xtensa_opcode_name (xtensa_default_isa, opcode),
2813 value);
2814 else
2815 as_bad_where ((char *) file, line,
2816 _("operand %d of '%s' has invalid value '%u'"),
2817 operand + 1,
2818 xtensa_opcode_name (xtensa_default_isa, opcode),
2819 value);
2820 return;
2821 }
2822
2823 xtensa_operand_set_field (xtensa_default_isa, opcode, operand, fmt, slot,
2824 slotbuf, valbuf);
2825 }
2826
2827
2828 static uint32
2829 xtensa_insnbuf_get_operand (xtensa_insnbuf slotbuf,
2830 xtensa_format fmt,
2831 int slot,
2832 xtensa_opcode opcode,
2833 int opnum)
2834 {
2835 uint32 val = 0;
2836 (void) xtensa_operand_get_field (xtensa_default_isa, opcode, opnum,
2837 fmt, slot, slotbuf, &val);
2838 (void) xtensa_operand_decode (xtensa_default_isa, opcode, opnum, &val);
2839 return val;
2840 }
2841
2842 \f
2843 /* Checks for rules from xtensa-relax tables. */
2844
2845 /* The routine xg_instruction_matches_option_term must return TRUE
2846 when a given option term is true. The meaning of all of the option
2847 terms is given interpretation by this function. */
2848
2849 static bfd_boolean
2850 xg_instruction_matches_option_term (TInsn *insn, const ReqOrOption *option)
2851 {
2852 if (strcmp (option->option_name, "realnop") == 0
2853 || strncmp (option->option_name, "IsaUse", 6) == 0)
2854 {
2855 /* These conditions were evaluated statically when building the
2856 relaxation table. There's no need to reevaluate them now. */
2857 return TRUE;
2858 }
2859 else if (strcmp (option->option_name, "FREEREG") == 0)
2860 return insn->extra_arg.X_op == O_register;
2861 else
2862 {
2863 as_fatal (_("internal error: unknown option name '%s'"),
2864 option->option_name);
2865 }
2866 }
2867
2868
2869 static bfd_boolean
2870 xg_instruction_matches_or_options (TInsn *insn,
2871 const ReqOrOptionList *or_option)
2872 {
2873 const ReqOrOption *option;
2874 /* Must match each of the AND terms. */
2875 for (option = or_option; option != NULL; option = option->next)
2876 {
2877 if (xg_instruction_matches_option_term (insn, option))
2878 return TRUE;
2879 }
2880 return FALSE;
2881 }
2882
2883
2884 static bfd_boolean
2885 xg_instruction_matches_options (TInsn *insn, const ReqOptionList *options)
2886 {
2887 const ReqOption *req_options;
2888 /* Must match each of the AND terms. */
2889 for (req_options = options;
2890 req_options != NULL;
2891 req_options = req_options->next)
2892 {
2893 /* Must match one of the OR clauses. */
2894 if (!xg_instruction_matches_or_options (insn,
2895 req_options->or_option_terms))
2896 return FALSE;
2897 }
2898 return TRUE;
2899 }
2900
2901
2902 /* Return the transition rule that matches or NULL if none matches. */
2903
2904 static bfd_boolean
2905 xg_instruction_matches_rule (TInsn *insn, TransitionRule *rule)
2906 {
2907 PreconditionList *condition_l;
2908
2909 if (rule->opcode != insn->opcode)
2910 return FALSE;
2911
2912 for (condition_l = rule->conditions;
2913 condition_l != NULL;
2914 condition_l = condition_l->next)
2915 {
2916 expressionS *exp1;
2917 expressionS *exp2;
2918 Precondition *cond = condition_l->precond;
2919
2920 switch (cond->typ)
2921 {
2922 case OP_CONSTANT:
2923 /* The expression must be the constant. */
2924 gas_assert (cond->op_num < insn->ntok);
2925 exp1 = &insn->tok[cond->op_num];
2926 if (expr_is_const (exp1))
2927 {
2928 switch (cond->cmp)
2929 {
2930 case OP_EQUAL:
2931 if (get_expr_const (exp1) != cond->op_data)
2932 return FALSE;
2933 break;
2934 case OP_NOTEQUAL:
2935 if (get_expr_const (exp1) == cond->op_data)
2936 return FALSE;
2937 break;
2938 default:
2939 return FALSE;
2940 }
2941 }
2942 else if (expr_is_register (exp1))
2943 {
2944 switch (cond->cmp)
2945 {
2946 case OP_EQUAL:
2947 if (get_expr_register (exp1) != cond->op_data)
2948 return FALSE;
2949 break;
2950 case OP_NOTEQUAL:
2951 if (get_expr_register (exp1) == cond->op_data)
2952 return FALSE;
2953 break;
2954 default:
2955 return FALSE;
2956 }
2957 }
2958 else
2959 return FALSE;
2960 break;
2961
2962 case OP_OPERAND:
2963 gas_assert (cond->op_num < insn->ntok);
2964 gas_assert (cond->op_data < insn->ntok);
2965 exp1 = &insn->tok[cond->op_num];
2966 exp2 = &insn->tok[cond->op_data];
2967
2968 switch (cond->cmp)
2969 {
2970 case OP_EQUAL:
2971 if (!expr_is_equal (exp1, exp2))
2972 return FALSE;
2973 break;
2974 case OP_NOTEQUAL:
2975 if (expr_is_equal (exp1, exp2))
2976 return FALSE;
2977 break;
2978 }
2979 break;
2980
2981 case OP_LITERAL:
2982 case OP_LABEL:
2983 default:
2984 return FALSE;
2985 }
2986 }
2987 if (!xg_instruction_matches_options (insn, rule->options))
2988 return FALSE;
2989
2990 return TRUE;
2991 }
2992
2993
2994 static int
2995 transition_rule_cmp (const TransitionRule *a, const TransitionRule *b)
2996 {
2997 bfd_boolean a_greater = FALSE;
2998 bfd_boolean b_greater = FALSE;
2999
3000 ReqOptionList *l_a = a->options;
3001 ReqOptionList *l_b = b->options;
3002
3003 /* We only care if they both are the same except for
3004 a const16 vs. an l32r. */
3005
3006 while (l_a && l_b && ((l_a->next == NULL) == (l_b->next == NULL)))
3007 {
3008 ReqOrOptionList *l_or_a = l_a->or_option_terms;
3009 ReqOrOptionList *l_or_b = l_b->or_option_terms;
3010 while (l_or_a && l_or_b && ((l_a->next == NULL) == (l_b->next == NULL)))
3011 {
3012 if (l_or_a->is_true != l_or_b->is_true)
3013 return 0;
3014 if (strcmp (l_or_a->option_name, l_or_b->option_name) != 0)
3015 {
3016 /* This is the case we care about. */
3017 if (strcmp (l_or_a->option_name, "IsaUseConst16") == 0
3018 && strcmp (l_or_b->option_name, "IsaUseL32R") == 0)
3019 {
3020 if (prefer_const16)
3021 a_greater = TRUE;
3022 else
3023 b_greater = TRUE;
3024 }
3025 else if (strcmp (l_or_a->option_name, "IsaUseL32R") == 0
3026 && strcmp (l_or_b->option_name, "IsaUseConst16") == 0)
3027 {
3028 if (prefer_const16)
3029 b_greater = TRUE;
3030 else
3031 a_greater = TRUE;
3032 }
3033 else
3034 return 0;
3035 }
3036 l_or_a = l_or_a->next;
3037 l_or_b = l_or_b->next;
3038 }
3039 if (l_or_a || l_or_b)
3040 return 0;
3041
3042 l_a = l_a->next;
3043 l_b = l_b->next;
3044 }
3045 if (l_a || l_b)
3046 return 0;
3047
3048 /* Incomparable if the substitution was used differently in two cases. */
3049 if (a_greater && b_greater)
3050 return 0;
3051
3052 if (b_greater)
3053 return 1;
3054 if (a_greater)
3055 return -1;
3056
3057 return 0;
3058 }
3059
3060
3061 static TransitionRule *
3062 xg_instruction_match (TInsn *insn)
3063 {
3064 TransitionTable *table = xg_build_simplify_table (&transition_rule_cmp);
3065 TransitionList *l;
3066 gas_assert (insn->opcode < table->num_opcodes);
3067
3068 /* Walk through all of the possible transitions. */
3069 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3070 {
3071 TransitionRule *rule = l->rule;
3072 if (xg_instruction_matches_rule (insn, rule))
3073 return rule;
3074 }
3075 return NULL;
3076 }
3077
3078 \f
3079 /* Various Other Internal Functions. */
3080
3081 static bfd_boolean
3082 is_unique_insn_expansion (TransitionRule *r)
3083 {
3084 if (!r->to_instr || r->to_instr->next != NULL)
3085 return FALSE;
3086 if (r->to_instr->typ != INSTR_INSTR)
3087 return FALSE;
3088 return TRUE;
3089 }
3090
3091
3092 /* Check if there is exactly one relaxation for INSN that converts it to
3093 another instruction of equal or larger size. If so, and if TARG is
3094 non-null, go ahead and generate the relaxed instruction into TARG. If
3095 NARROW_ONLY is true, then only consider relaxations that widen a narrow
3096 instruction, i.e., ignore relaxations that convert to an instruction of
3097 equal size. In some contexts where this function is used, only
3098 a single widening is allowed and the NARROW_ONLY argument is used to
3099 exclude cases like ADDI being "widened" to an ADDMI, which may
3100 later be relaxed to an ADDMI/ADDI pair. */
3101
3102 bfd_boolean
3103 xg_is_single_relaxable_insn (TInsn *insn, TInsn *targ, bfd_boolean narrow_only)
3104 {
3105 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3106 TransitionList *l;
3107 TransitionRule *match = 0;
3108
3109 gas_assert (insn->insn_type == ITYPE_INSN);
3110 gas_assert (insn->opcode < table->num_opcodes);
3111
3112 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3113 {
3114 TransitionRule *rule = l->rule;
3115
3116 if (xg_instruction_matches_rule (insn, rule)
3117 && is_unique_insn_expansion (rule)
3118 && (xg_get_single_size (insn->opcode) + (narrow_only ? 1 : 0)
3119 <= xg_get_single_size (rule->to_instr->opcode)))
3120 {
3121 if (match)
3122 return FALSE;
3123 match = rule;
3124 }
3125 }
3126 if (!match)
3127 return FALSE;
3128
3129 if (targ)
3130 xg_build_to_insn (targ, insn, match->to_instr);
3131 return TRUE;
3132 }
3133
3134
3135 /* Return the maximum number of bytes this opcode can expand to. */
3136
3137 static int
3138 xg_get_max_insn_widen_size (xtensa_opcode opcode)
3139 {
3140 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3141 TransitionList *l;
3142 int max_size = xg_get_single_size (opcode);
3143
3144 gas_assert (opcode < table->num_opcodes);
3145
3146 for (l = table->table[opcode]; l != NULL; l = l->next)
3147 {
3148 TransitionRule *rule = l->rule;
3149 BuildInstr *build_list;
3150 int this_size = 0;
3151
3152 if (!rule)
3153 continue;
3154 build_list = rule->to_instr;
3155 if (is_unique_insn_expansion (rule))
3156 {
3157 gas_assert (build_list->typ == INSTR_INSTR);
3158 this_size = xg_get_max_insn_widen_size (build_list->opcode);
3159 }
3160 else
3161 for (; build_list != NULL; build_list = build_list->next)
3162 {
3163 switch (build_list->typ)
3164 {
3165 case INSTR_INSTR:
3166 this_size += xg_get_single_size (build_list->opcode);
3167 break;
3168 case INSTR_LITERAL_DEF:
3169 case INSTR_LABEL_DEF:
3170 default:
3171 break;
3172 }
3173 }
3174 if (this_size > max_size)
3175 max_size = this_size;
3176 }
3177 return max_size;
3178 }
3179
3180
3181 /* Return the maximum number of literal bytes this opcode can generate. */
3182
3183 static int
3184 xg_get_max_insn_widen_literal_size (xtensa_opcode opcode)
3185 {
3186 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3187 TransitionList *l;
3188 int max_size = 0;
3189
3190 gas_assert (opcode < table->num_opcodes);
3191
3192 for (l = table->table[opcode]; l != NULL; l = l->next)
3193 {
3194 TransitionRule *rule = l->rule;
3195 BuildInstr *build_list;
3196 int this_size = 0;
3197
3198 if (!rule)
3199 continue;
3200 build_list = rule->to_instr;
3201 if (is_unique_insn_expansion (rule))
3202 {
3203 gas_assert (build_list->typ == INSTR_INSTR);
3204 this_size = xg_get_max_insn_widen_literal_size (build_list->opcode);
3205 }
3206 else
3207 for (; build_list != NULL; build_list = build_list->next)
3208 {
3209 switch (build_list->typ)
3210 {
3211 case INSTR_LITERAL_DEF:
3212 /* Hard-coded 4-byte literal. */
3213 this_size += 4;
3214 break;
3215 case INSTR_INSTR:
3216 case INSTR_LABEL_DEF:
3217 default:
3218 break;
3219 }
3220 }
3221 if (this_size > max_size)
3222 max_size = this_size;
3223 }
3224 return max_size;
3225 }
3226
3227
3228 static bfd_boolean
3229 xg_is_relaxable_insn (TInsn *insn, int lateral_steps)
3230 {
3231 int steps_taken = 0;
3232 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3233 TransitionList *l;
3234
3235 gas_assert (insn->insn_type == ITYPE_INSN);
3236 gas_assert (insn->opcode < table->num_opcodes);
3237
3238 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3239 {
3240 TransitionRule *rule = l->rule;
3241
3242 if (xg_instruction_matches_rule (insn, rule))
3243 {
3244 if (steps_taken == lateral_steps)
3245 return TRUE;
3246 steps_taken++;
3247 }
3248 }
3249 return FALSE;
3250 }
3251
3252
3253 static symbolS *
3254 get_special_literal_symbol (void)
3255 {
3256 static symbolS *sym = NULL;
3257
3258 if (sym == NULL)
3259 sym = symbol_find_or_make ("SPECIAL_LITERAL0\001");
3260 return sym;
3261 }
3262
3263
3264 static symbolS *
3265 get_special_label_symbol (void)
3266 {
3267 static symbolS *sym = NULL;
3268
3269 if (sym == NULL)
3270 sym = symbol_find_or_make ("SPECIAL_LABEL0\001");
3271 return sym;
3272 }
3273
3274
3275 static bfd_boolean
3276 xg_valid_literal_expression (const expressionS *exp)
3277 {
3278 switch (exp->X_op)
3279 {
3280 case O_constant:
3281 case O_symbol:
3282 case O_big:
3283 case O_uminus:
3284 case O_subtract:
3285 case O_pltrel:
3286 case O_pcrel:
3287 case O_tlsfunc:
3288 case O_tlsarg:
3289 case O_tpoff:
3290 case O_dtpoff:
3291 return TRUE;
3292 default:
3293 return FALSE;
3294 }
3295 }
3296
3297
3298 /* This will check to see if the value can be converted into the
3299 operand type. It will return TRUE if it does not fit. */
3300
3301 static bfd_boolean
3302 xg_check_operand (int32 value, xtensa_opcode opcode, int operand)
3303 {
3304 uint32 valbuf = value;
3305 if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf))
3306 return TRUE;
3307 return FALSE;
3308 }
3309
3310
3311 /* Assumes: All immeds are constants. Check that all constants fit
3312 into their immeds; return FALSE if not. */
3313
3314 static bfd_boolean
3315 xg_immeds_fit (const TInsn *insn)
3316 {
3317 xtensa_isa isa = xtensa_default_isa;
3318 int i;
3319
3320 int n = insn->ntok;
3321 gas_assert (insn->insn_type == ITYPE_INSN);
3322 for (i = 0; i < n; ++i)
3323 {
3324 const expressionS *exp = &insn->tok[i];
3325
3326 if (xtensa_operand_is_register (isa, insn->opcode, i) == 1)
3327 continue;
3328
3329 switch (exp->X_op)
3330 {
3331 case O_register:
3332 case O_constant:
3333 if (xg_check_operand (exp->X_add_number, insn->opcode, i))
3334 return FALSE;
3335 break;
3336
3337 default:
3338 /* The symbol should have a fixup associated with it. */
3339 gas_assert (FALSE);
3340 break;
3341 }
3342 }
3343 return TRUE;
3344 }
3345
3346
3347 /* This should only be called after we have an initial
3348 estimate of the addresses. */
3349
3350 static bfd_boolean
3351 xg_symbolic_immeds_fit (const TInsn *insn,
3352 segT pc_seg,
3353 fragS *pc_frag,
3354 offsetT pc_offset,
3355 long stretch)
3356 {
3357 xtensa_isa isa = xtensa_default_isa;
3358 symbolS *symbolP;
3359 fragS *sym_frag;
3360 offsetT target, pc;
3361 uint32 new_offset;
3362 int i;
3363 int n = insn->ntok;
3364
3365 gas_assert (insn->insn_type == ITYPE_INSN);
3366
3367 for (i = 0; i < n; ++i)
3368 {
3369 const expressionS *exp = &insn->tok[i];
3370
3371 if (xtensa_operand_is_register (isa, insn->opcode, i) == 1)
3372 continue;
3373
3374 switch (exp->X_op)
3375 {
3376 case O_register:
3377 case O_constant:
3378 if (xg_check_operand (exp->X_add_number, insn->opcode, i))
3379 return FALSE;
3380 break;
3381
3382 case O_lo16:
3383 case O_hi16:
3384 /* Check for the worst case. */
3385 if (xg_check_operand (0xffff, insn->opcode, i))
3386 return FALSE;
3387 break;
3388
3389 case O_symbol:
3390 /* We only allow symbols for PC-relative references.
3391 If pc_frag == 0, then we don't have frag locations yet. */
3392 if (pc_frag == 0
3393 || xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 0)
3394 return FALSE;
3395
3396 /* If it is a weak symbol or a symbol in a different section,
3397 it cannot be known to fit at assembly time. */
3398 if (S_IS_WEAK (exp->X_add_symbol)
3399 || S_GET_SEGMENT (exp->X_add_symbol) != pc_seg)
3400 {
3401 /* For a direct call with --no-longcalls, be optimistic and
3402 assume it will be in range. If the symbol is weak and
3403 undefined, it may remain undefined at link-time, in which
3404 case it will have a zero value and almost certainly be out
3405 of range for a direct call; thus, relax for undefined weak
3406 symbols even if longcalls is not enabled. */
3407 if (is_direct_call_opcode (insn->opcode)
3408 && ! pc_frag->tc_frag_data.use_longcalls
3409 && (! S_IS_WEAK (exp->X_add_symbol)
3410 || S_IS_DEFINED (exp->X_add_symbol)))
3411 return TRUE;
3412
3413 return FALSE;
3414 }
3415
3416 symbolP = exp->X_add_symbol;
3417 sym_frag = symbol_get_frag (symbolP);
3418 target = S_GET_VALUE (symbolP) + exp->X_add_number;
3419 pc = pc_frag->fr_address + pc_offset;
3420
3421 /* If frag has yet to be reached on this pass, assume it
3422 will move by STRETCH just as we did. If this is not so,
3423 it will be because some frag between grows, and that will
3424 force another pass. Beware zero-length frags. There
3425 should be a faster way to do this. */
3426
3427 if (stretch != 0
3428 && sym_frag->relax_marker != pc_frag->relax_marker
3429 && S_GET_SEGMENT (symbolP) == pc_seg)
3430 {
3431 target += stretch;
3432 }
3433
3434 new_offset = target;
3435 xtensa_operand_do_reloc (isa, insn->opcode, i, &new_offset, pc);
3436 if (xg_check_operand (new_offset, insn->opcode, i))
3437 return FALSE;
3438 break;
3439
3440 default:
3441 /* The symbol should have a fixup associated with it. */
3442 return FALSE;
3443 }
3444 }
3445
3446 return TRUE;
3447 }
3448
3449
3450 /* Return TRUE on success. */
3451
3452 static bfd_boolean
3453 xg_build_to_insn (TInsn *targ, TInsn *insn, BuildInstr *bi)
3454 {
3455 BuildOp *op;
3456 symbolS *sym;
3457
3458 tinsn_init (targ);
3459 targ->debug_line = insn->debug_line;
3460 targ->loc_directive_seen = insn->loc_directive_seen;
3461 switch (bi->typ)
3462 {
3463 case INSTR_INSTR:
3464 op = bi->ops;
3465 targ->opcode = bi->opcode;
3466 targ->insn_type = ITYPE_INSN;
3467 targ->is_specific_opcode = FALSE;
3468
3469 for (; op != NULL; op = op->next)
3470 {
3471 int op_num = op->op_num;
3472 int op_data = op->op_data;
3473
3474 gas_assert (op->op_num < MAX_INSN_ARGS);
3475
3476 if (targ->ntok <= op_num)
3477 targ->ntok = op_num + 1;
3478
3479 switch (op->typ)
3480 {
3481 case OP_CONSTANT:
3482 set_expr_const (&targ->tok[op_num], op_data);
3483 break;
3484 case OP_OPERAND:
3485 gas_assert (op_data < insn->ntok);
3486 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3487 break;
3488 case OP_FREEREG:
3489 if (insn->extra_arg.X_op != O_register)
3490 return FALSE;
3491 copy_expr (&targ->tok[op_num], &insn->extra_arg);
3492 break;
3493 case OP_LITERAL:
3494 sym = get_special_literal_symbol ();
3495 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
3496 if (insn->tok[op_data].X_op == O_tlsfunc
3497 || insn->tok[op_data].X_op == O_tlsarg)
3498 copy_expr (&targ->extra_arg, &insn->tok[op_data]);
3499 break;
3500 case OP_LABEL:
3501 sym = get_special_label_symbol ();
3502 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
3503 break;
3504 case OP_OPERAND_HI16U:
3505 case OP_OPERAND_LOW16U:
3506 gas_assert (op_data < insn->ntok);
3507 if (expr_is_const (&insn->tok[op_data]))
3508 {
3509 long val;
3510 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3511 val = xg_apply_userdef_op_fn (op->typ,
3512 targ->tok[op_num].
3513 X_add_number);
3514 targ->tok[op_num].X_add_number = val;
3515 }
3516 else
3517 {
3518 /* For const16 we can create relocations for these. */
3519 if (targ->opcode == XTENSA_UNDEFINED
3520 || (targ->opcode != xtensa_const16_opcode))
3521 return FALSE;
3522 gas_assert (op_data < insn->ntok);
3523 /* Need to build a O_lo16 or O_hi16. */
3524 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3525 if (targ->tok[op_num].X_op == O_symbol)
3526 {
3527 if (op->typ == OP_OPERAND_HI16U)
3528 targ->tok[op_num].X_op = O_hi16;
3529 else if (op->typ == OP_OPERAND_LOW16U)
3530 targ->tok[op_num].X_op = O_lo16;
3531 else
3532 return FALSE;
3533 }
3534 }
3535 break;
3536 default:
3537 /* currently handles:
3538 OP_OPERAND_LOW8
3539 OP_OPERAND_HI24S
3540 OP_OPERAND_F32MINUS */
3541 if (xg_has_userdef_op_fn (op->typ))
3542 {
3543 gas_assert (op_data < insn->ntok);
3544 if (expr_is_const (&insn->tok[op_data]))
3545 {
3546 long val;
3547 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3548 val = xg_apply_userdef_op_fn (op->typ,
3549 targ->tok[op_num].
3550 X_add_number);
3551 targ->tok[op_num].X_add_number = val;
3552 }
3553 else
3554 return FALSE; /* We cannot use a relocation for this. */
3555 break;
3556 }
3557 gas_assert (0);
3558 break;
3559 }
3560 }
3561 break;
3562
3563 case INSTR_LITERAL_DEF:
3564 op = bi->ops;
3565 targ->opcode = XTENSA_UNDEFINED;
3566 targ->insn_type = ITYPE_LITERAL;
3567 targ->is_specific_opcode = FALSE;
3568 for (; op != NULL; op = op->next)
3569 {
3570 int op_num = op->op_num;
3571 int op_data = op->op_data;
3572 gas_assert (op->op_num < MAX_INSN_ARGS);
3573
3574 if (targ->ntok <= op_num)
3575 targ->ntok = op_num + 1;
3576
3577 switch (op->typ)
3578 {
3579 case OP_OPERAND:
3580 gas_assert (op_data < insn->ntok);
3581 /* We can only pass resolvable literals through. */
3582 if (!xg_valid_literal_expression (&insn->tok[op_data]))
3583 return FALSE;
3584 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3585 break;
3586 case OP_LITERAL:
3587 case OP_CONSTANT:
3588 case OP_LABEL:
3589 default:
3590 gas_assert (0);
3591 break;
3592 }
3593 }
3594 break;
3595
3596 case INSTR_LABEL_DEF:
3597 op = bi->ops;
3598 targ->opcode = XTENSA_UNDEFINED;
3599 targ->insn_type = ITYPE_LABEL;
3600 targ->is_specific_opcode = FALSE;
3601 /* Literal with no ops is a label? */
3602 gas_assert (op == NULL);
3603 break;
3604
3605 default:
3606 gas_assert (0);
3607 }
3608
3609 return TRUE;
3610 }
3611
3612
3613 /* Return TRUE on success. */
3614
3615 static bfd_boolean
3616 xg_build_to_stack (IStack *istack, TInsn *insn, BuildInstr *bi)
3617 {
3618 for (; bi != NULL; bi = bi->next)
3619 {
3620 TInsn *next_insn = istack_push_space (istack);
3621
3622 if (!xg_build_to_insn (next_insn, insn, bi))
3623 return FALSE;
3624 }
3625 return TRUE;
3626 }
3627
3628
3629 /* Return TRUE on valid expansion. */
3630
3631 static bfd_boolean
3632 xg_expand_to_stack (IStack *istack, TInsn *insn, int lateral_steps)
3633 {
3634 int stack_size = istack->ninsn;
3635 int steps_taken = 0;
3636 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
3637 TransitionList *l;
3638
3639 gas_assert (insn->insn_type == ITYPE_INSN);
3640 gas_assert (insn->opcode < table->num_opcodes);
3641
3642 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3643 {
3644 TransitionRule *rule = l->rule;
3645
3646 if (xg_instruction_matches_rule (insn, rule))
3647 {
3648 if (lateral_steps == steps_taken)
3649 {
3650 int i;
3651
3652 /* This is it. Expand the rule to the stack. */
3653 if (!xg_build_to_stack (istack, insn, rule->to_instr))
3654 return FALSE;
3655
3656 /* Check to see if it fits. */
3657 for (i = stack_size; i < istack->ninsn; i++)
3658 {
3659 TInsn *tinsn = &istack->insn[i];
3660
3661 if (tinsn->insn_type == ITYPE_INSN
3662 && !tinsn_has_symbolic_operands (tinsn)
3663 && !xg_immeds_fit (tinsn))
3664 {
3665 istack->ninsn = stack_size;
3666 return FALSE;
3667 }
3668 }
3669 return TRUE;
3670 }
3671 steps_taken++;
3672 }
3673 }
3674 return FALSE;
3675 }
3676
3677 \f
3678 /* Relax the assembly instruction at least "min_steps".
3679 Return the number of steps taken.
3680
3681 For relaxation to correctly terminate, every relaxation chain must
3682 terminate in one of two ways:
3683
3684 1. If the chain from one instruction to the next consists entirely of
3685 single instructions, then the chain *must* handle all possible
3686 immediates without failing. It must not ever fail because an
3687 immediate is out of range. The MOVI.N -> MOVI -> L32R relaxation
3688 chain is one example. L32R loads 32 bits, and there cannot be an
3689 immediate larger than 32 bits, so it satisfies this condition.
3690 Single instruction relaxation chains are as defined by
3691 xg_is_single_relaxable_instruction.
3692
3693 2. Otherwise, the chain must end in a multi-instruction expansion: e.g.,
3694 BNEZ.N -> BNEZ -> BNEZ.W15 -> BENZ.N/J
3695
3696 Strictly speaking, in most cases you can violate condition 1 and be OK
3697 -- in particular when the last two instructions have the same single
3698 size. But nevertheless, you should guarantee the above two conditions.
3699
3700 We could fix this so that single-instruction expansions correctly
3701 terminate when they can't handle the range, but the error messages are
3702 worse, and it actually turns out that in every case but one (18-bit wide
3703 branches), you need a multi-instruction expansion to get the full range
3704 anyway. And because 18-bit branches are handled identically to 15-bit
3705 branches, there isn't any point in changing it. */
3706
3707 static int
3708 xg_assembly_relax (IStack *istack,
3709 TInsn *insn,
3710 segT pc_seg,
3711 fragS *pc_frag, /* if pc_frag == 0, not pc-relative */
3712 offsetT pc_offset, /* offset in fragment */
3713 int min_steps, /* minimum conversion steps */
3714 long stretch) /* number of bytes stretched so far */
3715 {
3716 int steps_taken = 0;
3717
3718 /* Some of its immeds don't fit. Try to build a relaxed version.
3719 This may go through a couple of stages of single instruction
3720 transformations before we get there. */
3721
3722 TInsn single_target;
3723 TInsn current_insn;
3724 int lateral_steps = 0;
3725 int istack_size = istack->ninsn;
3726
3727 if (xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
3728 && steps_taken >= min_steps)
3729 {
3730 istack_push (istack, insn);
3731 return steps_taken;
3732 }
3733 current_insn = *insn;
3734
3735 /* Walk through all of the single instruction expansions. */
3736 while (xg_is_single_relaxable_insn (&current_insn, &single_target, FALSE))
3737 {
3738 steps_taken++;
3739 if (xg_symbolic_immeds_fit (&single_target, pc_seg, pc_frag, pc_offset,
3740 stretch))
3741 {
3742 if (steps_taken >= min_steps)
3743 {
3744 istack_push (istack, &single_target);
3745 return steps_taken;
3746 }
3747 }
3748 current_insn = single_target;
3749 }
3750
3751 /* Now check for a multi-instruction expansion. */
3752 while (xg_is_relaxable_insn (&current_insn, lateral_steps))
3753 {
3754 if (xg_symbolic_immeds_fit (&current_insn, pc_seg, pc_frag, pc_offset,
3755 stretch))
3756 {
3757 if (steps_taken >= min_steps)
3758 {
3759 istack_push (istack, &current_insn);
3760 return steps_taken;
3761 }
3762 }
3763 steps_taken++;
3764 if (xg_expand_to_stack (istack, &current_insn, lateral_steps))
3765 {
3766 if (steps_taken >= min_steps)
3767 return steps_taken;
3768 }
3769 lateral_steps++;
3770 istack->ninsn = istack_size;
3771 }
3772
3773 /* It's not going to work -- use the original. */
3774 istack_push (istack, insn);
3775 return steps_taken;
3776 }
3777
3778
3779 static void
3780 xg_finish_frag (char *last_insn,
3781 enum xtensa_relax_statesE frag_state,
3782 enum xtensa_relax_statesE slot0_state,
3783 int max_growth,
3784 bfd_boolean is_insn)
3785 {
3786 /* Finish off this fragment so that it has at LEAST the desired
3787 max_growth. If it doesn't fit in this fragment, close this one
3788 and start a new one. In either case, return a pointer to the
3789 beginning of the growth area. */
3790
3791 fragS *old_frag;
3792
3793 frag_grow (max_growth);
3794 old_frag = frag_now;
3795
3796 frag_now->fr_opcode = last_insn;
3797 if (is_insn)
3798 frag_now->tc_frag_data.is_insn = TRUE;
3799
3800 frag_var (rs_machine_dependent, max_growth, max_growth,
3801 frag_state, frag_now->fr_symbol, frag_now->fr_offset, last_insn);
3802
3803 old_frag->tc_frag_data.slot_subtypes[0] = slot0_state;
3804 xtensa_set_frag_assembly_state (frag_now);
3805
3806 /* Just to make sure that we did not split it up. */
3807 gas_assert (old_frag->fr_next == frag_now);
3808 }
3809
3810
3811 /* Return TRUE if the target frag is one of the next non-empty frags. */
3812
3813 static bfd_boolean
3814 is_next_frag_target (const fragS *fragP, const fragS *target)
3815 {
3816 if (fragP == NULL)
3817 return FALSE;
3818
3819 for (; fragP; fragP = fragP->fr_next)
3820 {
3821 if (fragP == target)
3822 return TRUE;
3823 if (fragP->fr_fix != 0)
3824 return FALSE;
3825 if (fragP->fr_type == rs_fill && fragP->fr_offset != 0)
3826 return FALSE;
3827 if ((fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
3828 && ((fragP->fr_address % (1 << fragP->fr_offset)) != 0))
3829 return FALSE;
3830 if (fragP->fr_type == rs_space)
3831 return FALSE;
3832 }
3833 return FALSE;
3834 }
3835
3836
3837 static bfd_boolean
3838 is_branch_jmp_to_next (TInsn *insn, fragS *fragP)
3839 {
3840 xtensa_isa isa = xtensa_default_isa;
3841 int i;
3842 int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
3843 int target_op = -1;
3844 symbolS *sym;
3845 fragS *target_frag;
3846
3847 if (xtensa_opcode_is_branch (isa, insn->opcode) != 1
3848 && xtensa_opcode_is_jump (isa, insn->opcode) != 1)
3849 return FALSE;
3850
3851 for (i = 0; i < num_ops; i++)
3852 {
3853 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1)
3854 {
3855 target_op = i;
3856 break;
3857 }
3858 }
3859 if (target_op == -1)
3860 return FALSE;
3861
3862 if (insn->ntok <= target_op)
3863 return FALSE;
3864
3865 if (insn->tok[target_op].X_op != O_symbol)
3866 return FALSE;
3867
3868 sym = insn->tok[target_op].X_add_symbol;
3869 if (sym == NULL)
3870 return FALSE;
3871
3872 if (insn->tok[target_op].X_add_number != 0)
3873 return FALSE;
3874
3875 target_frag = symbol_get_frag (sym);
3876 if (target_frag == NULL)
3877 return FALSE;
3878
3879 if (is_next_frag_target (fragP->fr_next, target_frag)
3880 && S_GET_VALUE (sym) == target_frag->fr_address)
3881 return TRUE;
3882
3883 return FALSE;
3884 }
3885
3886
3887 static void
3888 xg_add_branch_and_loop_targets (TInsn *insn)
3889 {
3890 xtensa_isa isa = xtensa_default_isa;
3891 int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
3892
3893 if (xtensa_opcode_is_loop (isa, insn->opcode) == 1)
3894 {
3895 int i = 1;
3896 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1
3897 && insn->tok[i].X_op == O_symbol)
3898 symbol_get_tc (insn->tok[i].X_add_symbol)->is_loop_target = TRUE;
3899 return;
3900 }
3901
3902 if (xtensa_opcode_is_branch (isa, insn->opcode) == 1
3903 || xtensa_opcode_is_loop (isa, insn->opcode) == 1)
3904 {
3905 int i;
3906
3907 for (i = 0; i < insn->ntok && i < num_ops; i++)
3908 {
3909 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1
3910 && insn->tok[i].X_op == O_symbol)
3911 {
3912 symbolS *sym = insn->tok[i].X_add_symbol;
3913 symbol_get_tc (sym)->is_branch_target = TRUE;
3914 if (S_IS_DEFINED (sym))
3915 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
3916 }
3917 }
3918 }
3919 }
3920
3921
3922 /* Return FALSE if no error. */
3923
3924 static bfd_boolean
3925 xg_build_token_insn (BuildInstr *instr_spec, TInsn *old_insn, TInsn *new_insn)
3926 {
3927 int num_ops = 0;
3928 BuildOp *b_op;
3929
3930 switch (instr_spec->typ)
3931 {
3932 case INSTR_INSTR:
3933 new_insn->insn_type = ITYPE_INSN;
3934 new_insn->opcode = instr_spec->opcode;
3935 break;
3936 case INSTR_LITERAL_DEF:
3937 new_insn->insn_type = ITYPE_LITERAL;
3938 new_insn->opcode = XTENSA_UNDEFINED;
3939 break;
3940 case INSTR_LABEL_DEF:
3941 abort ();
3942 }
3943 new_insn->is_specific_opcode = FALSE;
3944 new_insn->debug_line = old_insn->debug_line;
3945 new_insn->loc_directive_seen = old_insn->loc_directive_seen;
3946
3947 for (b_op = instr_spec->ops; b_op != NULL; b_op = b_op->next)
3948 {
3949 expressionS *exp;
3950 const expressionS *src_exp;
3951
3952 num_ops++;
3953 switch (b_op->typ)
3954 {
3955 case OP_CONSTANT:
3956 /* The expression must be the constant. */
3957 gas_assert (b_op->op_num < MAX_INSN_ARGS);
3958 exp = &new_insn->tok[b_op->op_num];
3959 set_expr_const (exp, b_op->op_data);
3960 break;
3961
3962 case OP_OPERAND:
3963 gas_assert (b_op->op_num < MAX_INSN_ARGS);
3964 gas_assert (b_op->op_data < (unsigned) old_insn->ntok);
3965 src_exp = &old_insn->tok[b_op->op_data];
3966 exp = &new_insn->tok[b_op->op_num];
3967 copy_expr (exp, src_exp);
3968 break;
3969
3970 case OP_LITERAL:
3971 case OP_LABEL:
3972 as_bad (_("can't handle generation of literal/labels yet"));
3973 gas_assert (0);
3974
3975 default:
3976 as_bad (_("can't handle undefined OP TYPE"));
3977 gas_assert (0);
3978 }
3979 }
3980
3981 new_insn->ntok = num_ops;
3982 return FALSE;
3983 }
3984
3985
3986 /* Return TRUE if it was simplified. */
3987
3988 static bfd_boolean
3989 xg_simplify_insn (TInsn *old_insn, TInsn *new_insn)
3990 {
3991 TransitionRule *rule;
3992 BuildInstr *insn_spec;
3993
3994 if (old_insn->is_specific_opcode || !density_supported)
3995 return FALSE;
3996
3997 rule = xg_instruction_match (old_insn);
3998 if (rule == NULL)
3999 return FALSE;
4000
4001 insn_spec = rule->to_instr;
4002 /* There should only be one. */
4003 gas_assert (insn_spec != NULL);
4004 gas_assert (insn_spec->next == NULL);
4005 if (insn_spec->next != NULL)
4006 return FALSE;
4007
4008 xg_build_token_insn (insn_spec, old_insn, new_insn);
4009
4010 return TRUE;
4011 }
4012
4013
4014 /* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i ->
4015 l32i.n. (2) Check the number of operands. (3) Place the instruction
4016 tokens into the stack or relax it and place multiple
4017 instructions/literals onto the stack. Return FALSE if no error. */
4018
4019 static bfd_boolean
4020 xg_expand_assembly_insn (IStack *istack, TInsn *orig_insn)
4021 {
4022 int noperands;
4023 TInsn new_insn;
4024 bfd_boolean do_expand;
4025
4026 tinsn_init (&new_insn);
4027
4028 /* Narrow it if we can. xg_simplify_insn now does all the
4029 appropriate checking (e.g., for the density option). */
4030 if (xg_simplify_insn (orig_insn, &new_insn))
4031 orig_insn = &new_insn;
4032
4033 noperands = xtensa_opcode_num_operands (xtensa_default_isa,
4034 orig_insn->opcode);
4035 if (orig_insn->ntok < noperands)
4036 {
4037 as_bad (_("found %d operands for '%s': Expected %d"),
4038 orig_insn->ntok,
4039 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
4040 noperands);
4041 return TRUE;
4042 }
4043 if (orig_insn->ntok > noperands)
4044 as_warn (_("found too many (%d) operands for '%s': Expected %d"),
4045 orig_insn->ntok,
4046 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
4047 noperands);
4048
4049 /* If there are not enough operands, we will assert above. If there
4050 are too many, just cut out the extras here. */
4051 orig_insn->ntok = noperands;
4052
4053 if (tinsn_has_invalid_symbolic_operands (orig_insn))
4054 return TRUE;
4055
4056 /* Special case for extui opcode which has constraints not handled
4057 by the ordinary operand encoding checks. The number of operands
4058 and related syntax issues have already been checked. */
4059 if (orig_insn->opcode == xtensa_extui_opcode)
4060 {
4061 int shiftimm = orig_insn->tok[2].X_add_number;
4062 int maskimm = orig_insn->tok[3].X_add_number;
4063 if (shiftimm + maskimm > 32)
4064 {
4065 as_bad (_("immediate operands sum to greater than 32"));
4066 return TRUE;
4067 }
4068 }
4069
4070 /* If the instruction will definitely need to be relaxed, it is better
4071 to expand it now for better scheduling. Decide whether to expand
4072 now.... */
4073 do_expand = (!orig_insn->is_specific_opcode && use_transform ());
4074
4075 /* Calls should be expanded to longcalls only in the backend relaxation
4076 so that the assembly scheduler will keep the L32R/CALLX instructions
4077 adjacent. */
4078 if (is_direct_call_opcode (orig_insn->opcode))
4079 do_expand = FALSE;
4080
4081 if (tinsn_has_symbolic_operands (orig_insn))
4082 {
4083 /* The values of symbolic operands are not known yet, so only expand
4084 now if an operand is "complex" (e.g., difference of symbols) and
4085 will have to be stored as a literal regardless of the value. */
4086 if (!tinsn_has_complex_operands (orig_insn))
4087 do_expand = FALSE;
4088 }
4089 else if (xg_immeds_fit (orig_insn))
4090 do_expand = FALSE;
4091
4092 if (do_expand)
4093 xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
4094 else
4095 istack_push (istack, orig_insn);
4096
4097 return FALSE;
4098 }
4099
4100
4101 /* Return TRUE if the section flags are marked linkonce
4102 or the name is .gnu.linkonce.*. */
4103
4104 static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
4105
4106 static bfd_boolean
4107 get_is_linkonce_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec)
4108 {
4109 flagword flags, link_once_flags;
4110
4111 flags = bfd_get_section_flags (abfd, sec);
4112 link_once_flags = (flags & SEC_LINK_ONCE);
4113
4114 /* Flags might not be set yet. */
4115 if (!link_once_flags
4116 && strncmp (segment_name (sec), ".gnu.linkonce.", linkonce_len) == 0)
4117 link_once_flags = SEC_LINK_ONCE;
4118
4119 return (link_once_flags != 0);
4120 }
4121
4122
4123 static void
4124 xtensa_add_literal_sym (symbolS *sym)
4125 {
4126 sym_list *l;
4127
4128 l = (sym_list *) xmalloc (sizeof (sym_list));
4129 l->sym = sym;
4130 l->next = literal_syms;
4131 literal_syms = l;
4132 }
4133
4134
4135 static symbolS *
4136 xtensa_create_literal_symbol (segT sec, fragS *frag)
4137 {
4138 static int lit_num = 0;
4139 static char name[256];
4140 symbolS *symbolP;
4141
4142 sprintf (name, ".L_lit_sym%d", lit_num);
4143
4144 /* Create a local symbol. If it is in a linkonce section, we have to
4145 be careful to make sure that if it is used in a relocation that the
4146 symbol will be in the output file. */
4147 if (get_is_linkonce_section (stdoutput, sec))
4148 {
4149 symbolP = symbol_new (name, sec, 0, frag);
4150 S_CLEAR_EXTERNAL (symbolP);
4151 /* symbolP->local = 1; */
4152 }
4153 else
4154 symbolP = symbol_new (name, sec, 0, frag);
4155
4156 xtensa_add_literal_sym (symbolP);
4157
4158 lit_num++;
4159 return symbolP;
4160 }
4161
4162
4163 /* Currently all literals that are generated here are 32-bit L32R targets. */
4164
4165 static symbolS *
4166 xg_assemble_literal (/* const */ TInsn *insn)
4167 {
4168 emit_state state;
4169 symbolS *lit_sym = NULL;
4170 bfd_reloc_code_real_type reloc;
4171 bfd_boolean pcrel = FALSE;
4172 char *p;
4173
4174 /* size = 4 for L32R. It could easily be larger when we move to
4175 larger constants. Add a parameter later. */
4176 offsetT litsize = 4;
4177 offsetT litalign = 2; /* 2^2 = 4 */
4178 expressionS saved_loc;
4179 expressionS * emit_val;
4180
4181 set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
4182
4183 gas_assert (insn->insn_type == ITYPE_LITERAL);
4184 gas_assert (insn->ntok == 1); /* must be only one token here */
4185
4186 xtensa_switch_to_literal_fragment (&state);
4187
4188 emit_val = &insn->tok[0];
4189 if (emit_val->X_op == O_big)
4190 {
4191 int size = emit_val->X_add_number * CHARS_PER_LITTLENUM;
4192 if (size > litsize)
4193 {
4194 /* This happens when someone writes a "movi a2, big_number". */
4195 as_bad_where (frag_now->fr_file, frag_now->fr_line,
4196 _("invalid immediate"));
4197 xtensa_restore_emit_state (&state);
4198 return NULL;
4199 }
4200 }
4201
4202 /* Force a 4-byte align here. Note that this opens a new frag, so all
4203 literals done with this function have a frag to themselves. That's
4204 important for the way text section literals work. */
4205 frag_align (litalign, 0, 0);
4206 record_alignment (now_seg, litalign);
4207
4208 switch (emit_val->X_op)
4209 {
4210 case O_pcrel:
4211 pcrel = TRUE;
4212 /* fall through */
4213 case O_pltrel:
4214 case O_tlsfunc:
4215 case O_tlsarg:
4216 case O_tpoff:
4217 case O_dtpoff:
4218 p = frag_more (litsize);
4219 xtensa_set_frag_assembly_state (frag_now);
4220 reloc = map_operator_to_reloc (emit_val->X_op, TRUE);
4221 if (emit_val->X_add_symbol)
4222 emit_val->X_op = O_symbol;
4223 else
4224 emit_val->X_op = O_constant;
4225 fix_new_exp (frag_now, p - frag_now->fr_literal,
4226 litsize, emit_val, pcrel, reloc);
4227 break;
4228
4229 default:
4230 emit_expr (emit_val, litsize);
4231 break;
4232 }
4233
4234 gas_assert (frag_now->tc_frag_data.literal_frag == NULL);
4235 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
4236 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
4237 lit_sym = frag_now->fr_symbol;
4238
4239 /* Go back. */
4240 xtensa_restore_emit_state (&state);
4241 return lit_sym;
4242 }
4243
4244
4245 static void
4246 xg_assemble_literal_space (/* const */ int size, int slot)
4247 {
4248 emit_state state;
4249 /* We might have to do something about this alignment. It only
4250 takes effect if something is placed here. */
4251 offsetT litalign = 2; /* 2^2 = 4 */
4252 fragS *lit_saved_frag;
4253
4254 gas_assert (size % 4 == 0);
4255
4256 xtensa_switch_to_literal_fragment (&state);
4257
4258 /* Force a 4-byte align here. */
4259 frag_align (litalign, 0, 0);
4260 record_alignment (now_seg, litalign);
4261
4262 frag_grow (size);
4263
4264 lit_saved_frag = frag_now;
4265 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
4266 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
4267 xg_finish_frag (0, RELAX_LITERAL, 0, size, FALSE);
4268
4269 /* Go back. */
4270 xtensa_restore_emit_state (&state);
4271 frag_now->tc_frag_data.literal_frags[slot] = lit_saved_frag;
4272 }
4273
4274
4275 /* Put in a fixup record based on the opcode.
4276 Return TRUE on success. */
4277
4278 static bfd_boolean
4279 xg_add_opcode_fix (TInsn *tinsn,
4280 int opnum,
4281 xtensa_format fmt,
4282 int slot,
4283 expressionS *exp,
4284 fragS *fragP,
4285 offsetT offset)
4286 {
4287 xtensa_opcode opcode = tinsn->opcode;
4288 bfd_reloc_code_real_type reloc;
4289 reloc_howto_type *howto;
4290 int fmt_length;
4291 fixS *the_fix;
4292
4293 reloc = BFD_RELOC_NONE;
4294
4295 /* First try the special cases for "alternate" relocs. */
4296 if (opcode == xtensa_l32r_opcode)
4297 {
4298 if (fragP->tc_frag_data.use_absolute_literals)
4299 reloc = encode_alt_reloc (slot);
4300 }
4301 else if (opcode == xtensa_const16_opcode)
4302 {
4303 if (exp->X_op == O_lo16)
4304 {
4305 reloc = encode_reloc (slot);
4306 exp->X_op = O_symbol;
4307 }
4308 else if (exp->X_op == O_hi16)
4309 {
4310 reloc = encode_alt_reloc (slot);
4311 exp->X_op = O_symbol;
4312 }
4313 }
4314
4315 if (opnum != get_relaxable_immed (opcode))
4316 {
4317 as_bad (_("invalid relocation for operand %i of '%s'"),
4318 opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode));
4319 return FALSE;
4320 }
4321
4322 /* Handle erroneous "@h" and "@l" expressions here before they propagate
4323 into the symbol table where the generic portions of the assembler
4324 won't know what to do with them. */
4325 if (exp->X_op == O_lo16 || exp->X_op == O_hi16)
4326 {
4327 as_bad (_("invalid expression for operand %i of '%s'"),
4328 opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode));
4329 return FALSE;
4330 }
4331
4332 /* Next try the generic relocs. */
4333 if (reloc == BFD_RELOC_NONE)
4334 reloc = encode_reloc (slot);
4335 if (reloc == BFD_RELOC_NONE)
4336 {
4337 as_bad (_("invalid relocation in instruction slot %i"), slot);
4338 return FALSE;
4339 }
4340
4341 howto = bfd_reloc_type_lookup (stdoutput, reloc);
4342 if (!howto)
4343 {
4344 as_bad (_("undefined symbol for opcode \"%s\""),
4345 xtensa_opcode_name (xtensa_default_isa, opcode));
4346 return FALSE;
4347 }
4348
4349 fmt_length = xtensa_format_length (xtensa_default_isa, fmt);
4350 the_fix = fix_new_exp (fragP, offset, fmt_length, exp,
4351 howto->pc_relative, reloc);
4352 the_fix->fx_no_overflow = 1;
4353 the_fix->tc_fix_data.X_add_symbol = exp->X_add_symbol;
4354 the_fix->tc_fix_data.X_add_number = exp->X_add_number;
4355 the_fix->tc_fix_data.slot = slot;
4356
4357 return TRUE;
4358 }
4359
4360
4361 static bfd_boolean
4362 xg_emit_insn_to_buf (TInsn *tinsn,
4363 char *buf,
4364 fragS *fragP,
4365 offsetT offset,
4366 bfd_boolean build_fix)
4367 {
4368 static xtensa_insnbuf insnbuf = NULL;
4369 bfd_boolean has_symbolic_immed = FALSE;
4370 bfd_boolean ok = TRUE;
4371
4372 if (!insnbuf)
4373 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4374
4375 has_symbolic_immed = tinsn_to_insnbuf (tinsn, insnbuf);
4376 if (has_symbolic_immed && build_fix)
4377 {
4378 /* Add a fixup. */
4379 xtensa_format fmt = xg_get_single_format (tinsn->opcode);
4380 int slot = xg_get_single_slot (tinsn->opcode);
4381 int opnum = get_relaxable_immed (tinsn->opcode);
4382 expressionS *exp = &tinsn->tok[opnum];
4383
4384 if (!xg_add_opcode_fix (tinsn, opnum, fmt, slot, exp, fragP, offset))
4385 ok = FALSE;
4386 }
4387 fragP->tc_frag_data.is_insn = TRUE;
4388 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf,
4389 (unsigned char *) buf, 0);
4390 return ok;
4391 }
4392
4393
4394 static void
4395 xg_resolve_literals (TInsn *insn, symbolS *lit_sym)
4396 {
4397 symbolS *sym = get_special_literal_symbol ();
4398 int i;
4399 if (lit_sym == 0)
4400 return;
4401 gas_assert (insn->insn_type == ITYPE_INSN);
4402 for (i = 0; i < insn->ntok; i++)
4403 if (insn->tok[i].X_add_symbol == sym)
4404 insn->tok[i].X_add_symbol = lit_sym;
4405
4406 }
4407
4408
4409 static void
4410 xg_resolve_labels (TInsn *insn, symbolS *label_sym)
4411 {
4412 symbolS *sym = get_special_label_symbol ();
4413 int i;
4414 for (i = 0; i < insn->ntok; i++)
4415 if (insn->tok[i].X_add_symbol == sym)
4416 insn->tok[i].X_add_symbol = label_sym;
4417
4418 }
4419
4420
4421 /* Return TRUE if the instruction can write to the specified
4422 integer register. */
4423
4424 static bfd_boolean
4425 is_register_writer (const TInsn *insn, const char *regset, int regnum)
4426 {
4427 int i;
4428 int num_ops;
4429 xtensa_isa isa = xtensa_default_isa;
4430
4431 num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
4432
4433 for (i = 0; i < num_ops; i++)
4434 {
4435 char inout;
4436 inout = xtensa_operand_inout (isa, insn->opcode, i);
4437 if ((inout == 'o' || inout == 'm')
4438 && xtensa_operand_is_register (isa, insn->opcode, i) == 1)
4439 {
4440 xtensa_regfile opnd_rf =
4441 xtensa_operand_regfile (isa, insn->opcode, i);
4442 if (!strcmp (xtensa_regfile_shortname (isa, opnd_rf), regset))
4443 {
4444 if ((insn->tok[i].X_op == O_register)
4445 && (insn->tok[i].X_add_number == regnum))
4446 return TRUE;
4447 }
4448 }
4449 }
4450 return FALSE;
4451 }
4452
4453
4454 static bfd_boolean
4455 is_bad_loopend_opcode (const TInsn *tinsn)
4456 {
4457 xtensa_opcode opcode = tinsn->opcode;
4458
4459 if (opcode == XTENSA_UNDEFINED)
4460 return FALSE;
4461
4462 if (opcode == xtensa_call0_opcode
4463 || opcode == xtensa_callx0_opcode
4464 || opcode == xtensa_call4_opcode
4465 || opcode == xtensa_callx4_opcode
4466 || opcode == xtensa_call8_opcode
4467 || opcode == xtensa_callx8_opcode
4468 || opcode == xtensa_call12_opcode
4469 || opcode == xtensa_callx12_opcode
4470 || opcode == xtensa_isync_opcode
4471 || opcode == xtensa_ret_opcode
4472 || opcode == xtensa_ret_n_opcode
4473 || opcode == xtensa_retw_opcode
4474 || opcode == xtensa_retw_n_opcode
4475 || opcode == xtensa_waiti_opcode
4476 || opcode == xtensa_rsr_lcount_opcode)
4477 return TRUE;
4478
4479 return FALSE;
4480 }
4481
4482
4483 /* Labels that begin with ".Ln" or ".LM" are unaligned.
4484 This allows the debugger to add unaligned labels.
4485 Also, the assembler generates stabs labels that need
4486 not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */
4487
4488 static bfd_boolean
4489 is_unaligned_label (symbolS *sym)
4490 {
4491 const char *name = S_GET_NAME (sym);
4492 static size_t fake_size = 0;
4493
4494 if (name
4495 && name[0] == '.'
4496 && name[1] == 'L' && (name[2] == 'n' || name[2] == 'M'))
4497 return TRUE;
4498
4499 /* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */
4500 if (fake_size == 0)
4501 fake_size = strlen (FAKE_LABEL_NAME);
4502
4503 if (name
4504 && strncmp (FAKE_LABEL_NAME, name, fake_size) == 0
4505 && (name[fake_size] == 'F'
4506 || name[fake_size] == 'L'
4507 || (name[fake_size] == 'e'
4508 && strncmp ("endfunc", name+fake_size, 7) == 0)))
4509 return TRUE;
4510
4511 return FALSE;
4512 }
4513
4514
4515 static fragS *
4516 next_non_empty_frag (const fragS *fragP)
4517 {
4518 fragS *next_fragP = fragP->fr_next;
4519
4520 /* Sometimes an empty will end up here due storage allocation issues.
4521 So we have to skip until we find something legit. */
4522 while (next_fragP && next_fragP->fr_fix == 0)
4523 next_fragP = next_fragP->fr_next;
4524
4525 if (next_fragP == NULL || next_fragP->fr_fix == 0)
4526 return NULL;
4527
4528 return next_fragP;
4529 }
4530
4531
4532 static bfd_boolean
4533 next_frag_opcode_is_loop (const fragS *fragP, xtensa_opcode *opcode)
4534 {
4535 xtensa_opcode out_opcode;
4536 const fragS *next_fragP = next_non_empty_frag (fragP);
4537
4538 if (next_fragP == NULL)
4539 return FALSE;
4540
4541 out_opcode = get_opcode_from_buf (next_fragP->fr_literal, 0);
4542 if (xtensa_opcode_is_loop (xtensa_default_isa, out_opcode) == 1)
4543 {
4544 *opcode = out_opcode;
4545 return TRUE;
4546 }
4547 return FALSE;
4548 }
4549
4550
4551 static int
4552 frag_format_size (const fragS *fragP)
4553 {
4554 static xtensa_insnbuf insnbuf = NULL;
4555 xtensa_isa isa = xtensa_default_isa;
4556 xtensa_format fmt;
4557 int fmt_size;
4558
4559 if (!insnbuf)
4560 insnbuf = xtensa_insnbuf_alloc (isa);
4561
4562 if (fragP == NULL)
4563 return XTENSA_UNDEFINED;
4564
4565 xtensa_insnbuf_from_chars (isa, insnbuf,
4566 (unsigned char *) fragP->fr_literal, 0);
4567
4568 fmt = xtensa_format_decode (isa, insnbuf);
4569 if (fmt == XTENSA_UNDEFINED)
4570 return XTENSA_UNDEFINED;
4571 fmt_size = xtensa_format_length (isa, fmt);
4572
4573 /* If the next format won't be changing due to relaxation, just
4574 return the length of the first format. */
4575 if (fragP->fr_opcode != fragP->fr_literal)
4576 return fmt_size;
4577
4578 /* If during relaxation we have to pull an instruction out of a
4579 multi-slot instruction, we will return the more conservative
4580 number. This works because alignment on bigger instructions
4581 is more restrictive than alignment on smaller instructions.
4582 This is more conservative than we would like, but it happens
4583 infrequently. */
4584
4585 if (xtensa_format_num_slots (xtensa_default_isa, fmt) > 1)
4586 return fmt_size;
4587
4588 /* If we aren't doing one of our own relaxations or it isn't
4589 slot-based, then the insn size won't change. */
4590 if (fragP->fr_type != rs_machine_dependent)
4591 return fmt_size;
4592 if (fragP->fr_subtype != RELAX_SLOTS)
4593 return fmt_size;
4594
4595 /* If an instruction is about to grow, return the longer size. */
4596 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP1
4597 || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP2
4598 || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP3)
4599 {
4600 /* For most frags at RELAX_IMMED_STEPX, with X > 0, the first
4601 instruction in the relaxed version is of length 3. (The case
4602 where we have to pull the instruction out of a FLIX bundle
4603 is handled conservatively above.) However, frags with opcodes
4604 that are expanding to wide branches end up having formats that
4605 are not determinable by the RELAX_IMMED_STEPX enumeration, and
4606 we can't tell directly what format the relaxer picked. This
4607 is a wart in the design of the relaxer that should someday be
4608 fixed, but would require major changes, or at least should
4609 be accompanied by major changes to make use of that data.
4610
4611 In any event, we can tell that we are expanding from a single-slot
4612 format to a wider one with the logic below. */
4613
4614 int i;
4615 int relaxed_size = fmt_size + fragP->tc_frag_data.text_expansion[0];
4616
4617 for (i = 0; i < xtensa_isa_num_formats (isa); i++)
4618 {
4619 if (relaxed_size == xtensa_format_length (isa, i))
4620 return relaxed_size;
4621 }
4622
4623 return 3;
4624 }
4625
4626 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
4627 return 2 + fragP->tc_frag_data.text_expansion[0];
4628
4629 return fmt_size;
4630 }
4631
4632
4633 static int
4634 next_frag_format_size (const fragS *fragP)
4635 {
4636 const fragS *next_fragP = next_non_empty_frag (fragP);
4637 return frag_format_size (next_fragP);
4638 }
4639
4640
4641 /* In early Xtensa Processors, for reasons that are unclear, the ISA
4642 required two-byte instructions to be treated as three-byte instructions
4643 for loop instruction alignment. This restriction was removed beginning
4644 with Xtensa LX. Now the only requirement on loop instruction alignment
4645 is that the first instruction of the loop must appear at an address that
4646 does not cross a fetch boundary. */
4647
4648 static int
4649 get_loop_align_size (int insn_size)
4650 {
4651 if (insn_size == XTENSA_UNDEFINED)
4652 return xtensa_fetch_width;
4653
4654 if (enforce_three_byte_loop_align && insn_size == 2)
4655 return 3;
4656
4657 return insn_size;
4658 }
4659
4660
4661 /* If the next legit fragment is an end-of-loop marker,
4662 switch its state so it will instantiate a NOP. */
4663
4664 static void
4665 update_next_frag_state (fragS *fragP)
4666 {
4667 fragS *next_fragP = fragP->fr_next;
4668 fragS *new_target = NULL;
4669
4670 if (align_targets)
4671 {
4672 /* We are guaranteed there will be one of these... */
4673 while (!(next_fragP->fr_type == rs_machine_dependent
4674 && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE
4675 || next_fragP->fr_subtype == RELAX_UNREACHABLE)))
4676 next_fragP = next_fragP->fr_next;
4677
4678 gas_assert (next_fragP->fr_type == rs_machine_dependent
4679 && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE
4680 || next_fragP->fr_subtype == RELAX_UNREACHABLE));
4681
4682 /* ...and one of these. */
4683 new_target = next_fragP->fr_next;
4684 while (!(new_target->fr_type == rs_machine_dependent
4685 && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN
4686 || new_target->fr_subtype == RELAX_DESIRE_ALIGN)))
4687 new_target = new_target->fr_next;
4688
4689 gas_assert (new_target->fr_type == rs_machine_dependent
4690 && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN
4691 || new_target->fr_subtype == RELAX_DESIRE_ALIGN));
4692 }
4693
4694 while (next_fragP && next_fragP->fr_fix == 0)
4695 {
4696 if (next_fragP->fr_type == rs_machine_dependent
4697 && next_fragP->fr_subtype == RELAX_LOOP_END)
4698 {
4699 next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP;
4700 return;
4701 }
4702
4703 next_fragP = next_fragP->fr_next;
4704 }
4705 }
4706
4707
4708 static bfd_boolean
4709 next_frag_is_branch_target (const fragS *fragP)
4710 {
4711 /* Sometimes an empty will end up here due to storage allocation issues,
4712 so we have to skip until we find something legit. */
4713 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4714 {
4715 if (fragP->tc_frag_data.is_branch_target)
4716 return TRUE;
4717 if (fragP->fr_fix != 0)
4718 break;
4719 }
4720 return FALSE;
4721 }
4722
4723
4724 static bfd_boolean
4725 next_frag_is_loop_target (const fragS *fragP)
4726 {
4727 /* Sometimes an empty will end up here due storage allocation issues.
4728 So we have to skip until we find something legit. */
4729 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4730 {
4731 if (fragP->tc_frag_data.is_loop_target)
4732 return TRUE;
4733 if (fragP->fr_fix != 0)
4734 break;
4735 }
4736 return FALSE;
4737 }
4738
4739
4740 /* As specified in the relaxation table, when a loop instruction is
4741 relaxed, there are 24 bytes between the loop instruction itself and
4742 the first instruction in the loop. */
4743
4744 #define RELAXED_LOOP_INSN_BYTES 24
4745
4746 static addressT
4747 next_frag_pre_opcode_bytes (const fragS *fragp)
4748 {
4749 const fragS *next_fragp = fragp->fr_next;
4750 xtensa_opcode next_opcode;
4751
4752 if (!next_frag_opcode_is_loop (fragp, &next_opcode))
4753 return 0;
4754
4755 /* Sometimes an empty will end up here due to storage allocation issues,
4756 so we have to skip until we find something legit. */
4757 while (next_fragp->fr_fix == 0)
4758 next_fragp = next_fragp->fr_next;
4759
4760 if (next_fragp->fr_type != rs_machine_dependent)
4761 return 0;
4762
4763 /* There is some implicit knowledge encoded in here.
4764 The LOOP instructions that are NOT RELAX_IMMED have
4765 been relaxed. Note that we can assume that the LOOP
4766 instruction is in slot 0 because loops aren't bundleable. */
4767 if (next_fragp->tc_frag_data.slot_subtypes[0] > RELAX_IMMED)
4768 return get_expanded_loop_offset (next_opcode) + RELAXED_LOOP_INSN_BYTES;
4769
4770 return 0;
4771 }
4772
4773
4774 /* Mark a location where we can later insert literal frags. Update
4775 the section's literal_pool_loc, so subsequent literals can be
4776 placed nearest to their use. */
4777
4778 static void
4779 xtensa_mark_literal_pool_location (void)
4780 {
4781 /* Any labels pointing to the current location need
4782 to be adjusted to after the literal pool. */
4783 emit_state s;
4784 fragS *pool_location;
4785
4786 if (use_literal_section)
4787 return;
4788
4789 /* We stash info in these frags so we can later move the literal's
4790 fixes into this frchain's fix list. */
4791 pool_location = frag_now;
4792 frag_now->tc_frag_data.lit_frchain = frchain_now;
4793 frag_now->tc_frag_data.literal_frag = frag_now;
4794 /* Just record this frag. */
4795 xtensa_maybe_create_literal_pool_frag (FALSE, FALSE);
4796 frag_variant (rs_machine_dependent, 0, 0,
4797 RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL);
4798 xtensa_set_frag_assembly_state (frag_now);
4799 frag_now->tc_frag_data.lit_seg = now_seg;
4800 frag_variant (rs_machine_dependent, 0, 0,
4801 RELAX_LITERAL_POOL_END, NULL, 0, NULL);
4802 xtensa_set_frag_assembly_state (frag_now);
4803
4804 /* Now put a frag into the literal pool that points to this location. */
4805 set_literal_pool_location (now_seg, pool_location);
4806 xtensa_switch_to_non_abs_literal_fragment (&s);
4807 frag_align (2, 0, 0);
4808 record_alignment (now_seg, 2);
4809
4810 /* Close whatever frag is there. */
4811 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4812 xtensa_set_frag_assembly_state (frag_now);
4813 frag_now->tc_frag_data.literal_frag = pool_location;
4814 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4815 xtensa_restore_emit_state (&s);
4816 xtensa_set_frag_assembly_state (frag_now);
4817 }
4818
4819
4820 /* Build a nop of the correct size into tinsn. */
4821
4822 static void
4823 build_nop (TInsn *tinsn, int size)
4824 {
4825 tinsn_init (tinsn);
4826 switch (size)
4827 {
4828 case 2:
4829 tinsn->opcode = xtensa_nop_n_opcode;
4830 tinsn->ntok = 0;
4831 if (tinsn->opcode == XTENSA_UNDEFINED)
4832 as_fatal (_("opcode 'NOP.N' unavailable in this configuration"));
4833 break;
4834
4835 case 3:
4836 if (xtensa_nop_opcode == XTENSA_UNDEFINED)
4837 {
4838 tinsn->opcode = xtensa_or_opcode;
4839 set_expr_const (&tinsn->tok[0], 1);
4840 set_expr_const (&tinsn->tok[1], 1);
4841 set_expr_const (&tinsn->tok[2], 1);
4842 tinsn->ntok = 3;
4843 }
4844 else
4845 tinsn->opcode = xtensa_nop_opcode;
4846
4847 gas_assert (tinsn->opcode != XTENSA_UNDEFINED);
4848 }
4849 }
4850
4851
4852 /* Assemble a NOP of the requested size in the buffer. User must have
4853 allocated "buf" with at least "size" bytes. */
4854
4855 static void
4856 assemble_nop (int size, char *buf)
4857 {
4858 static xtensa_insnbuf insnbuf = NULL;
4859 TInsn tinsn;
4860
4861 build_nop (&tinsn, size);
4862
4863 if (!insnbuf)
4864 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4865
4866 tinsn_to_insnbuf (&tinsn, insnbuf);
4867 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf,
4868 (unsigned char *) buf, 0);
4869 }
4870
4871
4872 /* Return the number of bytes for the offset of the expanded loop
4873 instruction. This should be incorporated into the relaxation
4874 specification but is hard-coded here. This is used to auto-align
4875 the loop instruction. It is invalid to call this function if the
4876 configuration does not have loops or if the opcode is not a loop
4877 opcode. */
4878
4879 static addressT
4880 get_expanded_loop_offset (xtensa_opcode opcode)
4881 {
4882 /* This is the OFFSET of the loop instruction in the expanded loop.
4883 This MUST correspond directly to the specification of the loop
4884 expansion. It will be validated on fragment conversion. */
4885 gas_assert (opcode != XTENSA_UNDEFINED);
4886 if (opcode == xtensa_loop_opcode)
4887 return 0;
4888 if (opcode == xtensa_loopnez_opcode)
4889 return 3;
4890 if (opcode == xtensa_loopgtz_opcode)
4891 return 6;
4892 as_fatal (_("get_expanded_loop_offset: invalid opcode"));
4893 return 0;
4894 }
4895
4896
4897 static fragS *
4898 get_literal_pool_location (segT seg)
4899 {
4900 struct litpool_seg *lps = litpool_seg_list.next;
4901 struct litpool_frag *lpf;
4902 for ( ; lps && lps->seg->id != seg->id; lps = lps->next)
4903 ;
4904 if (lps)
4905 {
4906 for (lpf = lps->frag_list.prev; lpf->fragP; lpf = lpf->prev)
4907 { /* Skip "candidates" for now. */
4908 if (lpf->fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN &&
4909 lpf->priority == 1)
4910 return lpf->fragP;
4911 }
4912 /* Must convert a lower-priority pool. */
4913 for (lpf = lps->frag_list.prev; lpf->fragP; lpf = lpf->prev)
4914 {
4915 if (lpf->fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN)
4916 return lpf->fragP;
4917 }
4918 /* Still no match -- try for a low priority pool. */
4919 for (lpf = lps->frag_list.prev; lpf->fragP; lpf = lpf->prev)
4920 {
4921 if (lpf->fragP->fr_subtype == RELAX_LITERAL_POOL_CANDIDATE_BEGIN)
4922 return lpf->fragP;
4923 }
4924 }
4925 return seg_info (seg)->tc_segment_info_data.literal_pool_loc;
4926 }
4927
4928
4929 static void
4930 set_literal_pool_location (segT seg, fragS *literal_pool_loc)
4931 {
4932 seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc;
4933 }
4934
4935
4936 /* Set frag assembly state should be called when a new frag is
4937 opened and after a frag has been closed. */
4938
4939 static void
4940 xtensa_set_frag_assembly_state (fragS *fragP)
4941 {
4942 if (!density_supported)
4943 fragP->tc_frag_data.is_no_density = TRUE;
4944
4945 /* This function is called from subsegs_finish, which is called
4946 after xtensa_end, so we can't use "use_transform" or
4947 "use_schedule" here. */
4948 if (!directive_state[directive_transform])
4949 fragP->tc_frag_data.is_no_transform = TRUE;
4950 if (directive_state[directive_longcalls])
4951 fragP->tc_frag_data.use_longcalls = TRUE;
4952 fragP->tc_frag_data.use_absolute_literals =
4953 directive_state[directive_absolute_literals];
4954 fragP->tc_frag_data.is_assembly_state_set = TRUE;
4955 }
4956
4957
4958 static bfd_boolean
4959 relaxable_section (asection *sec)
4960 {
4961 return ((sec->flags & SEC_DEBUGGING) == 0
4962 && strcmp (sec->name, ".eh_frame") != 0);
4963 }
4964
4965
4966 static void
4967 xtensa_mark_frags_for_org (void)
4968 {
4969 segT *seclist;
4970
4971 /* Walk over each fragment of all of the current segments. If we find
4972 a .org frag in any of the segments, mark all frags prior to it as
4973 "no transform", which will prevent linker optimizations from messing
4974 up the .org distance. This should be done after
4975 xtensa_find_unmarked_state_frags, because we don't want to worry here
4976 about that function trashing the data we save here. */
4977
4978 for (seclist = &stdoutput->sections;
4979 seclist && *seclist;
4980 seclist = &(*seclist)->next)
4981 {
4982 segT sec = *seclist;
4983 segment_info_type *seginfo;
4984 fragS *fragP;
4985 flagword flags;
4986 flags = bfd_get_section_flags (stdoutput, sec);
4987 if (flags & SEC_DEBUGGING)
4988 continue;
4989 if (!(flags & SEC_ALLOC))
4990 continue;
4991
4992 seginfo = seg_info (sec);
4993 if (seginfo && seginfo->frchainP)
4994 {
4995 fragS *last_fragP = seginfo->frchainP->frch_root;
4996 for (fragP = seginfo->frchainP->frch_root; fragP;
4997 fragP = fragP->fr_next)
4998 {
4999 /* cvt_frag_to_fill has changed the fr_type of org frags to
5000 rs_fill, so use the value as cached in rs_subtype here. */
5001 if (fragP->fr_subtype == RELAX_ORG)
5002 {
5003 while (last_fragP != fragP->fr_next)
5004 {
5005 last_fragP->tc_frag_data.is_no_transform = TRUE;
5006 last_fragP = last_fragP->fr_next;
5007 }
5008 }
5009 }
5010 }
5011 }
5012 }
5013
5014
5015 static void
5016 xtensa_find_unmarked_state_frags (void)
5017 {
5018 segT *seclist;
5019
5020 /* Walk over each fragment of all of the current segments. For each
5021 unmarked fragment, mark it with the same info as the previous
5022 fragment. */
5023 for (seclist = &stdoutput->sections;
5024 seclist && *seclist;
5025 seclist = &(*seclist)->next)
5026 {
5027 segT sec = *seclist;
5028 segment_info_type *seginfo;
5029 fragS *fragP;
5030 flagword flags;
5031 flags = bfd_get_section_flags (stdoutput, sec);
5032 if (flags & SEC_DEBUGGING)
5033 continue;
5034 if (!(flags & SEC_ALLOC))
5035 continue;
5036
5037 seginfo = seg_info (sec);
5038 if (seginfo && seginfo->frchainP)
5039 {
5040 fragS *last_fragP = 0;
5041 for (fragP = seginfo->frchainP->frch_root; fragP;
5042 fragP = fragP->fr_next)
5043 {
5044 if (fragP->fr_fix != 0
5045 && !fragP->tc_frag_data.is_assembly_state_set)
5046 {
5047 if (last_fragP == 0)
5048 {
5049 as_warn_where (fragP->fr_file, fragP->fr_line,
5050 _("assembly state not set for first frag in section %s"),
5051 sec->name);
5052 }
5053 else
5054 {
5055 fragP->tc_frag_data.is_assembly_state_set = TRUE;
5056 fragP->tc_frag_data.is_no_density =
5057 last_fragP->tc_frag_data.is_no_density;
5058 fragP->tc_frag_data.is_no_transform =
5059 last_fragP->tc_frag_data.is_no_transform;
5060 fragP->tc_frag_data.use_longcalls =
5061 last_fragP->tc_frag_data.use_longcalls;
5062 fragP->tc_frag_data.use_absolute_literals =
5063 last_fragP->tc_frag_data.use_absolute_literals;
5064 }
5065 }
5066 if (fragP->tc_frag_data.is_assembly_state_set)
5067 last_fragP = fragP;
5068 }
5069 }
5070 }
5071 }
5072
5073
5074 static void
5075 xtensa_find_unaligned_branch_targets (bfd *abfd ATTRIBUTE_UNUSED,
5076 asection *sec,
5077 void *unused ATTRIBUTE_UNUSED)
5078 {
5079 flagword flags = bfd_get_section_flags (abfd, sec);
5080 segment_info_type *seginfo = seg_info (sec);
5081 fragS *frag = seginfo->frchainP->frch_root;
5082
5083 if (flags & SEC_CODE)
5084 {
5085 xtensa_isa isa = xtensa_default_isa;
5086 xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
5087 while (frag != NULL)
5088 {
5089 if (frag->tc_frag_data.is_branch_target)
5090 {
5091 int op_size;
5092 addressT branch_align, frag_addr;
5093 xtensa_format fmt;
5094
5095 xtensa_insnbuf_from_chars
5096 (isa, insnbuf, (unsigned char *) frag->fr_literal, 0);
5097 fmt = xtensa_format_decode (isa, insnbuf);
5098 op_size = xtensa_format_length (isa, fmt);
5099 branch_align = 1 << branch_align_power (sec);
5100 frag_addr = frag->fr_address % branch_align;
5101 if (frag_addr + op_size > branch_align)
5102 as_warn_where (frag->fr_file, frag->fr_line,
5103 _("unaligned branch target: %d bytes at 0x%lx"),
5104 op_size, (long) frag->fr_address);
5105 }
5106 frag = frag->fr_next;
5107 }
5108 xtensa_insnbuf_free (isa, insnbuf);
5109 }
5110 }
5111
5112
5113 static void
5114 xtensa_find_unaligned_loops (bfd *abfd ATTRIBUTE_UNUSED,
5115 asection *sec,
5116 void *unused ATTRIBUTE_UNUSED)
5117 {
5118 flagword flags = bfd_get_section_flags (abfd, sec);
5119 segment_info_type *seginfo = seg_info (sec);
5120 fragS *frag = seginfo->frchainP->frch_root;
5121 xtensa_isa isa = xtensa_default_isa;
5122
5123 if (flags & SEC_CODE)
5124 {
5125 xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
5126 while (frag != NULL)
5127 {
5128 if (frag->tc_frag_data.is_first_loop_insn)
5129 {
5130 int op_size;
5131 addressT frag_addr;
5132 xtensa_format fmt;
5133
5134 if (frag->fr_fix == 0)
5135 frag = next_non_empty_frag (frag);
5136
5137 if (frag)
5138 {
5139 xtensa_insnbuf_from_chars
5140 (isa, insnbuf, (unsigned char *) frag->fr_literal, 0);
5141 fmt = xtensa_format_decode (isa, insnbuf);
5142 op_size = xtensa_format_length (isa, fmt);
5143 frag_addr = frag->fr_address % xtensa_fetch_width;
5144
5145 if (frag_addr + op_size > xtensa_fetch_width)
5146 as_warn_where (frag->fr_file, frag->fr_line,
5147 _("unaligned loop: %d bytes at 0x%lx"),
5148 op_size, (long) frag->fr_address);
5149 }
5150 }
5151 frag = frag->fr_next;
5152 }
5153 xtensa_insnbuf_free (isa, insnbuf);
5154 }
5155 }
5156
5157
5158 static int
5159 xg_apply_fix_value (fixS *fixP, valueT val)
5160 {
5161 xtensa_isa isa = xtensa_default_isa;
5162 static xtensa_insnbuf insnbuf = NULL;
5163 static xtensa_insnbuf slotbuf = NULL;
5164 xtensa_format fmt;
5165 int slot;
5166 bfd_boolean alt_reloc;
5167 xtensa_opcode opcode;
5168 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
5169
5170 if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc)
5171 || alt_reloc)
5172 as_fatal (_("unexpected fix"));
5173
5174 if (!insnbuf)
5175 {
5176 insnbuf = xtensa_insnbuf_alloc (isa);
5177 slotbuf = xtensa_insnbuf_alloc (isa);
5178 }
5179
5180 xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) fixpos, 0);
5181 fmt = xtensa_format_decode (isa, insnbuf);
5182 if (fmt == XTENSA_UNDEFINED)
5183 as_fatal (_("undecodable fix"));
5184 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
5185 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
5186 if (opcode == XTENSA_UNDEFINED)
5187 as_fatal (_("undecodable fix"));
5188
5189 /* CONST16 immediates are not PC-relative, despite the fact that we
5190 reuse the normal PC-relative operand relocations for the low part
5191 of a CONST16 operand. */
5192 if (opcode == xtensa_const16_opcode)
5193 return 0;
5194
5195 xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode,
5196 get_relaxable_immed (opcode), val,
5197 fixP->fx_file, fixP->fx_line);
5198
5199 xtensa_format_set_slot (isa, fmt, slot, insnbuf, slotbuf);
5200 xtensa_insnbuf_to_chars (isa, insnbuf, (unsigned char *) fixpos, 0);
5201
5202 return 1;
5203 }
5204
5205 \f
5206 /* External Functions and Other GAS Hooks. */
5207
5208 const char *
5209 xtensa_target_format (void)
5210 {
5211 return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le");
5212 }
5213
5214
5215 void
5216 xtensa_file_arch_init (bfd *abfd)
5217 {
5218 bfd_set_private_flags (abfd, 0x100 | 0x200);
5219 }
5220
5221
5222 void
5223 md_number_to_chars (char *buf, valueT val, int n)
5224 {
5225 if (target_big_endian)
5226 number_to_chars_bigendian (buf, val, n);
5227 else
5228 number_to_chars_littleendian (buf, val, n);
5229 }
5230
5231
5232 /* This function is called once, at assembler startup time. It should
5233 set up all the tables, etc. that the MD part of the assembler will
5234 need. */
5235
5236 void
5237 md_begin (void)
5238 {
5239 segT current_section = now_seg;
5240 int current_subsec = now_subseg;
5241 xtensa_isa isa;
5242 int i;
5243
5244 xtensa_default_isa = xtensa_isa_init (0, 0);
5245 isa = xtensa_default_isa;
5246
5247 linkrelax = 1;
5248
5249 /* Set up the literal sections. */
5250 memset (&default_lit_sections, 0, sizeof (default_lit_sections));
5251
5252 subseg_set (current_section, current_subsec);
5253
5254 xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi");
5255 xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi");
5256 xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0");
5257 xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4");
5258 xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8");
5259 xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12");
5260 xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0");
5261 xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4");
5262 xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8");
5263 xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12");
5264 xtensa_const16_opcode = xtensa_opcode_lookup (isa, "const16");
5265 xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry");
5266 xtensa_extui_opcode = xtensa_opcode_lookup (isa, "extui");
5267 xtensa_movi_opcode = xtensa_opcode_lookup (isa, "movi");
5268 xtensa_movi_n_opcode = xtensa_opcode_lookup (isa, "movi.n");
5269 xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync");
5270 xtensa_j_opcode = xtensa_opcode_lookup (isa, "j");
5271 xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx");
5272 xtensa_l32r_opcode = xtensa_opcode_lookup (isa, "l32r");
5273 xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop");
5274 xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez");
5275 xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz");
5276 xtensa_nop_opcode = xtensa_opcode_lookup (isa, "nop");
5277 xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n");
5278 xtensa_or_opcode = xtensa_opcode_lookup (isa, "or");
5279 xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret");
5280 xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n");
5281 xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw");
5282 xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n");
5283 xtensa_rsr_lcount_opcode = xtensa_opcode_lookup (isa, "rsr.lcount");
5284 xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti");
5285
5286 for (i = 0; i < xtensa_isa_num_formats (isa); i++)
5287 {
5288 int format_slots = xtensa_format_num_slots (isa, i);
5289 if (format_slots > config_max_slots)
5290 config_max_slots = format_slots;
5291 }
5292
5293 xg_init_vinsn (&cur_vinsn);
5294
5295 xtensa_num_pipe_stages = xtensa_isa_num_pipe_stages (isa);
5296
5297 init_op_placement_info_table ();
5298
5299 /* Set up the assembly state. */
5300 if (!frag_now->tc_frag_data.is_assembly_state_set)
5301 xtensa_set_frag_assembly_state (frag_now);
5302 }
5303
5304
5305 /* TC_INIT_FIX_DATA hook */
5306
5307 void
5308 xtensa_init_fix_data (fixS *x)
5309 {
5310 x->tc_fix_data.slot = 0;
5311 x->tc_fix_data.X_add_symbol = NULL;
5312 x->tc_fix_data.X_add_number = 0;
5313 }
5314
5315
5316 /* tc_frob_label hook */
5317
5318 void
5319 xtensa_frob_label (symbolS *sym)
5320 {
5321 float freq;
5322
5323 if (cur_vinsn.inside_bundle)
5324 {
5325 as_bad (_("labels are not valid inside bundles"));
5326 return;
5327 }
5328
5329 freq = get_subseg_target_freq (now_seg, now_subseg);
5330
5331 /* Since the label was already attached to a frag associated with the
5332 previous basic block, it now needs to be reset to the current frag. */
5333 symbol_set_frag (sym, frag_now);
5334 S_SET_VALUE (sym, (valueT) frag_now_fix ());
5335
5336 if (generating_literals)
5337 xtensa_add_literal_sym (sym);
5338 else
5339 xtensa_add_insn_label (sym);
5340
5341 if (symbol_get_tc (sym)->is_loop_target)
5342 {
5343 if ((get_last_insn_flags (now_seg, now_subseg)
5344 & FLAG_IS_BAD_LOOPEND) != 0)
5345 as_bad (_("invalid last instruction for a zero-overhead loop"));
5346
5347 xtensa_set_frag_assembly_state (frag_now);
5348 frag_var (rs_machine_dependent, 4, 4, RELAX_LOOP_END,
5349 frag_now->fr_symbol, frag_now->fr_offset, NULL);
5350
5351 xtensa_set_frag_assembly_state (frag_now);
5352 xtensa_move_labels (frag_now, 0);
5353 }
5354
5355 /* No target aligning in the absolute section. */
5356 if (now_seg != absolute_section
5357 && !is_unaligned_label (sym)
5358 && !generating_literals)
5359 {
5360 xtensa_set_frag_assembly_state (frag_now);
5361
5362 if (do_align_targets ())
5363 frag_var (rs_machine_dependent, 0, (int) freq,
5364 RELAX_DESIRE_ALIGN_IF_TARGET, frag_now->fr_symbol,
5365 frag_now->fr_offset, NULL);
5366 else
5367 frag_var (rs_fill, 0, 0, frag_now->fr_subtype,
5368 frag_now->fr_symbol, frag_now->fr_offset, NULL);
5369 xtensa_set_frag_assembly_state (frag_now);
5370 xtensa_move_labels (frag_now, 0);
5371 }
5372
5373 /* We need to mark the following properties even if we aren't aligning. */
5374
5375 /* If the label is already known to be a branch target, i.e., a
5376 forward branch, mark the frag accordingly. Backward branches
5377 are handled by xg_add_branch_and_loop_targets. */
5378 if (symbol_get_tc (sym)->is_branch_target)
5379 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
5380
5381 /* Loops only go forward, so they can be identified here. */
5382 if (symbol_get_tc (sym)->is_loop_target)
5383 symbol_get_frag (sym)->tc_frag_data.is_loop_target = TRUE;
5384
5385 dwarf2_emit_label (sym);
5386 }
5387
5388
5389 /* tc_unrecognized_line hook */
5390
5391 int
5392 xtensa_unrecognized_line (int ch)
5393 {
5394 switch (ch)
5395 {
5396 case '{' :
5397 if (cur_vinsn.inside_bundle == 0)
5398 {
5399 /* PR8110: Cannot emit line number info inside a FLIX bundle
5400 when using --gstabs. Temporarily disable debug info. */
5401 generate_lineno_debug ();
5402 if (debug_type == DEBUG_STABS)
5403 {
5404 xt_saved_debug_type = debug_type;
5405 debug_type = DEBUG_NONE;
5406 }
5407
5408 cur_vinsn.inside_bundle = 1;
5409 }
5410 else
5411 {
5412 as_bad (_("extra opening brace"));
5413 return 0;
5414 }
5415 break;
5416
5417 case '}' :
5418 if (cur_vinsn.inside_bundle)
5419 finish_vinsn (&cur_vinsn);
5420 else
5421 {
5422 as_bad (_("extra closing brace"));
5423 return 0;
5424 }
5425 break;
5426 default:
5427 as_bad (_("syntax error"));
5428 return 0;
5429 }
5430 return 1;
5431 }
5432
5433
5434 /* md_flush_pending_output hook */
5435
5436 void
5437 xtensa_flush_pending_output (void)
5438 {
5439 /* This line fixes a bug where automatically generated gstabs info
5440 separates a function label from its entry instruction, ending up
5441 with the literal position between the function label and the entry
5442 instruction and crashing code. It only happens with --gstabs and
5443 --text-section-literals, and when several other obscure relaxation
5444 conditions are met. */
5445 if (outputting_stabs_line_debug)
5446 return;
5447
5448 if (cur_vinsn.inside_bundle)
5449 as_bad (_("missing closing brace"));
5450
5451 /* If there is a non-zero instruction fragment, close it. */
5452 if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn)
5453 {
5454 frag_wane (frag_now);
5455 frag_new (0);
5456 xtensa_set_frag_assembly_state (frag_now);
5457 }
5458 frag_now->tc_frag_data.is_insn = FALSE;
5459
5460 xtensa_clear_insn_labels ();
5461 }
5462
5463
5464 /* We had an error while parsing an instruction. The string might look
5465 like this: "insn arg1, arg2 }". If so, we need to see the closing
5466 brace and reset some fields. Otherwise, the vinsn never gets closed
5467 and the num_slots field will grow past the end of the array of slots,
5468 and bad things happen. */
5469
5470 static void
5471 error_reset_cur_vinsn (void)
5472 {
5473 if (cur_vinsn.inside_bundle)
5474 {
5475 if (*input_line_pointer == '}'
5476 || *(input_line_pointer - 1) == '}'
5477 || *(input_line_pointer - 2) == '}')
5478 xg_clear_vinsn (&cur_vinsn);
5479 }
5480 }
5481
5482
5483 void
5484 md_assemble (char *str)
5485 {
5486 xtensa_isa isa = xtensa_default_isa;
5487 char *opname;
5488 unsigned opnamelen;
5489 bfd_boolean has_underbar = FALSE;
5490 char *arg_strings[MAX_INSN_ARGS];
5491 int num_args;
5492 TInsn orig_insn; /* Original instruction from the input. */
5493
5494 tinsn_init (&orig_insn);
5495
5496 /* Split off the opcode. */
5497 opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
5498 opname = xstrndup (str, opnamelen);
5499
5500 num_args = tokenize_arguments (arg_strings, str + opnamelen);
5501 if (num_args == -1)
5502 {
5503 as_bad (_("syntax error"));
5504 return;
5505 }
5506
5507 if (xg_translate_idioms (&opname, &num_args, arg_strings))
5508 return;
5509
5510 /* Check for an underbar prefix. */
5511 if (*opname == '_')
5512 {
5513 has_underbar = TRUE;
5514 opname += 1;
5515 }
5516
5517 orig_insn.insn_type = ITYPE_INSN;
5518 orig_insn.ntok = 0;
5519 orig_insn.is_specific_opcode = (has_underbar || !use_transform ());
5520 orig_insn.opcode = xtensa_opcode_lookup (isa, opname);
5521
5522 /* Special case: Check for "CALLXn.TLS" psuedo op. If found, grab its
5523 extra argument and set the opcode to "CALLXn". */
5524 if (orig_insn.opcode == XTENSA_UNDEFINED
5525 && strncasecmp (opname, "callx", 5) == 0)
5526 {
5527 unsigned long window_size;
5528 char *suffix;
5529
5530 window_size = strtoul (opname + 5, &suffix, 10);
5531 if (suffix != opname + 5
5532 && (window_size == 0
5533 || window_size == 4
5534 || window_size == 8
5535 || window_size == 12)
5536 && strcasecmp (suffix, ".tls") == 0)
5537 {
5538 switch (window_size)
5539 {
5540 case 0: orig_insn.opcode = xtensa_callx0_opcode; break;
5541 case 4: orig_insn.opcode = xtensa_callx4_opcode; break;
5542 case 8: orig_insn.opcode = xtensa_callx8_opcode; break;
5543 case 12: orig_insn.opcode = xtensa_callx12_opcode; break;
5544 }
5545
5546 if (num_args != 2)
5547 as_bad (_("wrong number of operands for '%s'"), opname);
5548 else
5549 {
5550 bfd_reloc_code_real_type reloc;
5551 char *old_input_line_pointer;
5552 expressionS *tok = &orig_insn.extra_arg;
5553
5554 old_input_line_pointer = input_line_pointer;
5555 input_line_pointer = arg_strings[num_args - 1];
5556
5557 expression (tok);
5558 if (tok->X_op == O_symbol
5559 && ((reloc = xtensa_elf_suffix (&input_line_pointer, tok))
5560 == BFD_RELOC_XTENSA_TLS_CALL))
5561 tok->X_op = map_suffix_reloc_to_operator (reloc);
5562 else
5563 as_bad (_("bad relocation expression for '%s'"), opname);
5564
5565 input_line_pointer = old_input_line_pointer;
5566 num_args -= 1;
5567 }
5568 }
5569 }
5570
5571 /* Special case: Check for "j.l" psuedo op. */
5572 if (orig_insn.opcode == XTENSA_UNDEFINED
5573 && strncasecmp (opname, "j.l", 3) == 0)
5574 {
5575 if (num_args != 2)
5576 as_bad (_("wrong number of operands for '%s'"), opname);
5577 else
5578 {
5579 char *old_input_line_pointer;
5580 expressionS *tok = &orig_insn.extra_arg;
5581
5582 old_input_line_pointer = input_line_pointer;
5583 input_line_pointer = arg_strings[num_args - 1];
5584
5585 expression_maybe_register (xtensa_jx_opcode, 0, tok);
5586 input_line_pointer = old_input_line_pointer;
5587
5588 num_args -= 1;
5589 orig_insn.opcode = xtensa_j_opcode;
5590 }
5591 }
5592
5593 if (orig_insn.opcode == XTENSA_UNDEFINED)
5594 {
5595 xtensa_format fmt = xtensa_format_lookup (isa, opname);
5596 if (fmt == XTENSA_UNDEFINED)
5597 {
5598 as_bad (_("unknown opcode or format name '%s'"), opname);
5599 error_reset_cur_vinsn ();
5600 return;
5601 }
5602 if (!cur_vinsn.inside_bundle)
5603 {
5604 as_bad (_("format names only valid inside bundles"));
5605 error_reset_cur_vinsn ();
5606 return;
5607 }
5608 if (cur_vinsn.format != XTENSA_UNDEFINED)
5609 as_warn (_("multiple formats specified for one bundle; using '%s'"),
5610 opname);
5611 cur_vinsn.format = fmt;
5612 free (has_underbar ? opname - 1 : opname);
5613 error_reset_cur_vinsn ();
5614 return;
5615 }
5616
5617 /* Parse the arguments. */
5618 if (parse_arguments (&orig_insn, num_args, arg_strings))
5619 {
5620 as_bad (_("syntax error"));
5621 error_reset_cur_vinsn ();
5622 return;
5623 }
5624
5625 /* Free the opcode and argument strings, now that they've been parsed. */
5626 free (has_underbar ? opname - 1 : opname);
5627 opname = 0;
5628 while (num_args-- > 0)
5629 free (arg_strings[num_args]);
5630
5631 /* Get expressions for invisible operands. */
5632 if (get_invisible_operands (&orig_insn))
5633 {
5634 error_reset_cur_vinsn ();
5635 return;
5636 }
5637
5638 /* Check for the right number and type of arguments. */
5639 if (tinsn_check_arguments (&orig_insn))
5640 {
5641 error_reset_cur_vinsn ();
5642 return;
5643 }
5644
5645 /* Record the line number for each TInsn, because a FLIX bundle may be
5646 spread across multiple input lines and individual instructions may be
5647 moved around in some cases. */
5648 orig_insn.loc_directive_seen = dwarf2_loc_directive_seen;
5649 dwarf2_where (&orig_insn.debug_line);
5650 dwarf2_consume_line_info ();
5651
5652 xg_add_branch_and_loop_targets (&orig_insn);
5653
5654 /* Check that immediate value for ENTRY is >= 16. */
5655 if (orig_insn.opcode == xtensa_entry_opcode && orig_insn.ntok >= 3)
5656 {
5657 expressionS *exp = &orig_insn.tok[2];
5658 if (exp->X_op == O_constant && exp->X_add_number < 16)
5659 as_warn (_("entry instruction with stack decrement < 16"));
5660 }
5661
5662 /* Finish it off:
5663 assemble_tokens (opcode, tok, ntok);
5664 expand the tokens from the orig_insn into the
5665 stack of instructions that will not expand
5666 unless required at relaxation time. */
5667
5668 if (!cur_vinsn.inside_bundle)
5669 emit_single_op (&orig_insn);
5670 else /* We are inside a bundle. */
5671 {
5672 cur_vinsn.slots[cur_vinsn.num_slots] = orig_insn;
5673 cur_vinsn.num_slots++;
5674 if (*input_line_pointer == '}'
5675 || *(input_line_pointer - 1) == '}'
5676 || *(input_line_pointer - 2) == '}')
5677 finish_vinsn (&cur_vinsn);
5678 }
5679
5680 /* We've just emitted a new instruction so clear the list of labels. */
5681 xtensa_clear_insn_labels ();
5682
5683 xtensa_check_frag_count ();
5684 }
5685
5686
5687 /* HANDLE_ALIGN hook */
5688
5689 /* For a .align directive, we mark the previous block with the alignment
5690 information. This will be placed in the object file in the
5691 property section corresponding to this section. */
5692
5693 void
5694 xtensa_handle_align (fragS *fragP)
5695 {
5696 if (linkrelax
5697 && ! fragP->tc_frag_data.is_literal
5698 && (fragP->fr_type == rs_align
5699 || fragP->fr_type == rs_align_code)
5700 && fragP->fr_offset > 0
5701 && now_seg != bss_section)
5702 {
5703 fragP->tc_frag_data.is_align = TRUE;
5704 fragP->tc_frag_data.alignment = fragP->fr_offset;
5705 }
5706
5707 if (fragP->fr_type == rs_align_test)
5708 {
5709 int count;
5710 count = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
5711 if (count != 0)
5712 as_bad_where (fragP->fr_file, fragP->fr_line,
5713 _("unaligned entry instruction"));
5714 }
5715
5716 if (linkrelax && fragP->fr_type == rs_org)
5717 fragP->fr_subtype = RELAX_ORG;
5718 }
5719
5720
5721 /* TC_FRAG_INIT hook */
5722
5723 void
5724 xtensa_frag_init (fragS *frag)
5725 {
5726 xtensa_set_frag_assembly_state (frag);
5727 }
5728
5729
5730 symbolS *
5731 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
5732 {
5733 return NULL;
5734 }
5735
5736
5737 /* Round up a section size to the appropriate boundary. */
5738
5739 valueT
5740 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
5741 {
5742 return size; /* Byte alignment is fine. */
5743 }
5744
5745
5746 long
5747 md_pcrel_from (fixS *fixP)
5748 {
5749 char *insn_p;
5750 static xtensa_insnbuf insnbuf = NULL;
5751 static xtensa_insnbuf slotbuf = NULL;
5752 int opnum;
5753 uint32 opnd_value;
5754 xtensa_opcode opcode;
5755 xtensa_format fmt;
5756 int slot;
5757 xtensa_isa isa = xtensa_default_isa;
5758 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
5759 bfd_boolean alt_reloc;
5760
5761 if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND)
5762 return 0;
5763
5764 if (fixP->fx_r_type == BFD_RELOC_32_PCREL)
5765 return addr;
5766
5767 if (!insnbuf)
5768 {
5769 insnbuf = xtensa_insnbuf_alloc (isa);
5770 slotbuf = xtensa_insnbuf_alloc (isa);
5771 }
5772
5773 insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where];
5774 xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) insn_p, 0);
5775 fmt = xtensa_format_decode (isa, insnbuf);
5776
5777 if (fmt == XTENSA_UNDEFINED)
5778 as_fatal (_("bad instruction format"));
5779
5780 if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc) != 0)
5781 as_fatal (_("invalid relocation"));
5782
5783 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
5784 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
5785
5786 /* Check for "alternate" relocations (operand not specified). None
5787 of the current uses for these are really PC-relative. */
5788 if (alt_reloc || opcode == xtensa_const16_opcode)
5789 {
5790 if (opcode != xtensa_l32r_opcode
5791 && opcode != xtensa_const16_opcode)
5792 as_fatal (_("invalid relocation for '%s' instruction"),
5793 xtensa_opcode_name (isa, opcode));
5794 return 0;
5795 }
5796
5797 opnum = get_relaxable_immed (opcode);
5798 opnd_value = 0;
5799 if (xtensa_operand_is_PCrelative (isa, opcode, opnum) != 1
5800 || xtensa_operand_do_reloc (isa, opcode, opnum, &opnd_value, addr))
5801 {
5802 as_bad_where (fixP->fx_file,
5803 fixP->fx_line,
5804 _("invalid relocation for operand %d of '%s'"),
5805 opnum, xtensa_opcode_name (isa, opcode));
5806 return 0;
5807 }
5808 return 0 - opnd_value;
5809 }
5810
5811
5812 /* TC_FORCE_RELOCATION hook */
5813
5814 int
5815 xtensa_force_relocation (fixS *fix)
5816 {
5817 switch (fix->fx_r_type)
5818 {
5819 case BFD_RELOC_XTENSA_ASM_EXPAND:
5820 case BFD_RELOC_XTENSA_SLOT0_ALT:
5821 case BFD_RELOC_XTENSA_SLOT1_ALT:
5822 case BFD_RELOC_XTENSA_SLOT2_ALT:
5823 case BFD_RELOC_XTENSA_SLOT3_ALT:
5824 case BFD_RELOC_XTENSA_SLOT4_ALT:
5825 case BFD_RELOC_XTENSA_SLOT5_ALT:
5826 case BFD_RELOC_XTENSA_SLOT6_ALT:
5827 case BFD_RELOC_XTENSA_SLOT7_ALT:
5828 case BFD_RELOC_XTENSA_SLOT8_ALT:
5829 case BFD_RELOC_XTENSA_SLOT9_ALT:
5830 case BFD_RELOC_XTENSA_SLOT10_ALT:
5831 case BFD_RELOC_XTENSA_SLOT11_ALT:
5832 case BFD_RELOC_XTENSA_SLOT12_ALT:
5833 case BFD_RELOC_XTENSA_SLOT13_ALT:
5834 case BFD_RELOC_XTENSA_SLOT14_ALT:
5835 return 1;
5836 default:
5837 break;
5838 }
5839
5840 if (linkrelax && fix->fx_addsy
5841 && relaxable_section (S_GET_SEGMENT (fix->fx_addsy)))
5842 return 1;
5843
5844 return generic_force_reloc (fix);
5845 }
5846
5847
5848 /* TC_VALIDATE_FIX_SUB hook */
5849
5850 int
5851 xtensa_validate_fix_sub (fixS *fix)
5852 {
5853 segT add_symbol_segment, sub_symbol_segment;
5854
5855 /* The difference of two symbols should be resolved by the assembler when
5856 linkrelax is not set. If the linker may relax the section containing
5857 the symbols, then an Xtensa DIFF relocation must be generated so that
5858 the linker knows to adjust the difference value. */
5859 if (!linkrelax || fix->fx_addsy == NULL)
5860 return 0;
5861
5862 /* Make sure both symbols are in the same segment, and that segment is
5863 "normal" and relaxable. If the segment is not "normal", then the
5864 fix is not valid. If the segment is not "relaxable", then the fix
5865 should have been handled earlier. */
5866 add_symbol_segment = S_GET_SEGMENT (fix->fx_addsy);
5867 if (! SEG_NORMAL (add_symbol_segment) ||
5868 ! relaxable_section (add_symbol_segment))
5869 return 0;
5870 sub_symbol_segment = S_GET_SEGMENT (fix->fx_subsy);
5871 return (sub_symbol_segment == add_symbol_segment);
5872 }
5873
5874
5875 /* NO_PSEUDO_DOT hook */
5876
5877 /* This function has nothing to do with pseudo dots, but this is the
5878 nearest macro to where the check needs to take place. FIXME: This
5879 seems wrong. */
5880
5881 bfd_boolean
5882 xtensa_check_inside_bundle (void)
5883 {
5884 if (cur_vinsn.inside_bundle && input_line_pointer[-1] == '.')
5885 as_bad (_("directives are not valid inside bundles"));
5886
5887 /* This function must always return FALSE because it is called via a
5888 macro that has nothing to do with bundling. */
5889 return FALSE;
5890 }
5891
5892
5893 /* md_elf_section_change_hook */
5894
5895 void
5896 xtensa_elf_section_change_hook (void)
5897 {
5898 /* Set up the assembly state. */
5899 if (!frag_now->tc_frag_data.is_assembly_state_set)
5900 xtensa_set_frag_assembly_state (frag_now);
5901 }
5902
5903
5904 /* tc_fix_adjustable hook */
5905
5906 bfd_boolean
5907 xtensa_fix_adjustable (fixS *fixP)
5908 {
5909 /* We need the symbol name for the VTABLE entries. */
5910 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
5911 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5912 return 0;
5913
5914 return 1;
5915 }
5916
5917
5918 /* tc_symbol_new_hook */
5919
5920 symbolS *expr_symbols = NULL;
5921
5922 void
5923 xtensa_symbol_new_hook (symbolS *sym)
5924 {
5925 if (is_leb128_expr && S_GET_SEGMENT (sym) == expr_section)
5926 {
5927 symbol_get_tc (sym)->next_expr_symbol = expr_symbols;
5928 expr_symbols = sym;
5929 }
5930 }
5931
5932
5933 void
5934 md_apply_fix (fixS *fixP, valueT *valP, segT seg)
5935 {
5936 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
5937 valueT val = 0;
5938
5939 /* Subtracted symbols are only allowed for a few relocation types, and
5940 unless linkrelax is enabled, they should not make it to this point. */
5941 if (fixP->fx_subsy && !(linkrelax && (fixP->fx_r_type == BFD_RELOC_32
5942 || fixP->fx_r_type == BFD_RELOC_16
5943 || fixP->fx_r_type == BFD_RELOC_8)))
5944 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
5945
5946 switch (fixP->fx_r_type)
5947 {
5948 case BFD_RELOC_32_PCREL:
5949 case BFD_RELOC_32:
5950 case BFD_RELOC_16:
5951 case BFD_RELOC_8:
5952 if (fixP->fx_subsy)
5953 {
5954 switch (fixP->fx_r_type)
5955 {
5956 case BFD_RELOC_8:
5957 fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF8;
5958 fixP->fx_signed = 0;
5959 break;
5960 case BFD_RELOC_16:
5961 fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF16;
5962 fixP->fx_signed = 0;
5963 break;
5964 case BFD_RELOC_32:
5965 fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF32;
5966 fixP->fx_signed = 0;
5967 break;
5968 default:
5969 break;
5970 }
5971
5972 val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset
5973 - S_GET_VALUE (fixP->fx_subsy));
5974
5975 /* The difference value gets written out, and the DIFF reloc
5976 identifies the address of the subtracted symbol (i.e., the one
5977 with the lowest address). */
5978 *valP = val;
5979 fixP->fx_offset -= val;
5980 fixP->fx_subsy = NULL;
5981 }
5982 else if (! fixP->fx_addsy)
5983 {
5984 val = *valP;
5985 fixP->fx_done = 1;
5986 }
5987 /* fall through */
5988
5989 case BFD_RELOC_XTENSA_PLT:
5990 md_number_to_chars (fixpos, val, fixP->fx_size);
5991 fixP->fx_no_overflow = 0; /* Use the standard overflow check. */
5992 break;
5993
5994 case BFD_RELOC_XTENSA_TLSDESC_FN:
5995 case BFD_RELOC_XTENSA_TLSDESC_ARG:
5996 case BFD_RELOC_XTENSA_TLS_TPOFF:
5997 case BFD_RELOC_XTENSA_TLS_DTPOFF:
5998 S_SET_THREAD_LOCAL (fixP->fx_addsy);
5999 md_number_to_chars (fixpos, 0, fixP->fx_size);
6000 fixP->fx_no_overflow = 0; /* Use the standard overflow check. */
6001 break;
6002
6003 case BFD_RELOC_XTENSA_SLOT0_OP:
6004 case BFD_RELOC_XTENSA_SLOT1_OP:
6005 case BFD_RELOC_XTENSA_SLOT2_OP:
6006 case BFD_RELOC_XTENSA_SLOT3_OP:
6007 case BFD_RELOC_XTENSA_SLOT4_OP:
6008 case BFD_RELOC_XTENSA_SLOT5_OP:
6009 case BFD_RELOC_XTENSA_SLOT6_OP:
6010 case BFD_RELOC_XTENSA_SLOT7_OP:
6011 case BFD_RELOC_XTENSA_SLOT8_OP:
6012 case BFD_RELOC_XTENSA_SLOT9_OP:
6013 case BFD_RELOC_XTENSA_SLOT10_OP:
6014 case BFD_RELOC_XTENSA_SLOT11_OP:
6015 case BFD_RELOC_XTENSA_SLOT12_OP:
6016 case BFD_RELOC_XTENSA_SLOT13_OP:
6017 case BFD_RELOC_XTENSA_SLOT14_OP:
6018 if (linkrelax)
6019 {
6020 /* Write the tentative value of a PC-relative relocation to a
6021 local symbol into the instruction. The value will be ignored
6022 by the linker, and it makes the object file disassembly
6023 readable when all branch targets are encoded in relocations. */
6024
6025 gas_assert (fixP->fx_addsy);
6026 if (S_GET_SEGMENT (fixP->fx_addsy) == seg
6027 && !S_FORCE_RELOC (fixP->fx_addsy, 1))
6028 {
6029 val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset
6030 - md_pcrel_from (fixP));
6031 (void) xg_apply_fix_value (fixP, val);
6032 }
6033 }
6034 else if (! fixP->fx_addsy)
6035 {
6036 val = *valP;
6037 if (xg_apply_fix_value (fixP, val))
6038 fixP->fx_done = 1;
6039 }
6040 break;
6041
6042 case BFD_RELOC_XTENSA_ASM_EXPAND:
6043 case BFD_RELOC_XTENSA_TLS_FUNC:
6044 case BFD_RELOC_XTENSA_TLS_ARG:
6045 case BFD_RELOC_XTENSA_TLS_CALL:
6046 case BFD_RELOC_XTENSA_SLOT0_ALT:
6047 case BFD_RELOC_XTENSA_SLOT1_ALT:
6048 case BFD_RELOC_XTENSA_SLOT2_ALT:
6049 case BFD_RELOC_XTENSA_SLOT3_ALT:
6050 case BFD_RELOC_XTENSA_SLOT4_ALT:
6051 case BFD_RELOC_XTENSA_SLOT5_ALT:
6052 case BFD_RELOC_XTENSA_SLOT6_ALT:
6053 case BFD_RELOC_XTENSA_SLOT7_ALT:
6054 case BFD_RELOC_XTENSA_SLOT8_ALT:
6055 case BFD_RELOC_XTENSA_SLOT9_ALT:
6056 case BFD_RELOC_XTENSA_SLOT10_ALT:
6057 case BFD_RELOC_XTENSA_SLOT11_ALT:
6058 case BFD_RELOC_XTENSA_SLOT12_ALT:
6059 case BFD_RELOC_XTENSA_SLOT13_ALT:
6060 case BFD_RELOC_XTENSA_SLOT14_ALT:
6061 /* These all need to be resolved at link-time. Do nothing now. */
6062 break;
6063
6064 case BFD_RELOC_VTABLE_INHERIT:
6065 case BFD_RELOC_VTABLE_ENTRY:
6066 fixP->fx_done = 0;
6067 break;
6068
6069 default:
6070 as_bad (_("unhandled local relocation fix %s"),
6071 bfd_get_reloc_code_name (fixP->fx_r_type));
6072 }
6073 }
6074
6075
6076 const char *
6077 md_atof (int type, char *litP, int *sizeP)
6078 {
6079 return ieee_md_atof (type, litP, sizeP, target_big_endian);
6080 }
6081
6082
6083 int
6084 md_estimate_size_before_relax (fragS *fragP, segT seg ATTRIBUTE_UNUSED)
6085 {
6086 return total_frag_text_expansion (fragP);
6087 }
6088
6089
6090 /* Translate internal representation of relocation info to BFD target
6091 format. */
6092
6093 arelent *
6094 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
6095 {
6096 arelent *reloc;
6097
6098 reloc = (arelent *) xmalloc (sizeof (arelent));
6099 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
6100 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
6101 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
6102
6103 /* Make sure none of our internal relocations make it this far.
6104 They'd better have been fully resolved by this point. */
6105 gas_assert ((int) fixp->fx_r_type > 0);
6106
6107 reloc->addend = fixp->fx_offset;
6108
6109 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
6110 if (reloc->howto == NULL)
6111 {
6112 as_bad_where (fixp->fx_file, fixp->fx_line,
6113 _("cannot represent `%s' relocation in object file"),
6114 bfd_get_reloc_code_name (fixp->fx_r_type));
6115 free (reloc->sym_ptr_ptr);
6116 free (reloc);
6117 return NULL;
6118 }
6119
6120 if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
6121 as_fatal (_("internal error; cannot generate `%s' relocation"),
6122 bfd_get_reloc_code_name (fixp->fx_r_type));
6123
6124 return reloc;
6125 }
6126
6127 \f
6128 /* Checks for resource conflicts between instructions. */
6129
6130 /* The func unit stuff could be implemented as bit-vectors rather
6131 than the iterative approach here. If it ends up being too
6132 slow, we will switch it. */
6133
6134 resource_table *
6135 new_resource_table (void *data,
6136 int cycles,
6137 int nu,
6138 unit_num_copies_func uncf,
6139 opcode_num_units_func onuf,
6140 opcode_funcUnit_use_unit_func ouuf,
6141 opcode_funcUnit_use_stage_func ousf)
6142 {
6143 int i;
6144 resource_table *rt = (resource_table *) xmalloc (sizeof (resource_table));
6145 rt->data = data;
6146 rt->cycles = cycles;
6147 rt->allocated_cycles = cycles;
6148 rt->num_units = nu;
6149 rt->unit_num_copies = uncf;
6150 rt->opcode_num_units = onuf;
6151 rt->opcode_unit_use = ouuf;
6152 rt->opcode_unit_stage = ousf;
6153
6154 rt->units = (unsigned char **) xcalloc (cycles, sizeof (unsigned char *));
6155 for (i = 0; i < cycles; i++)
6156 rt->units[i] = (unsigned char *) xcalloc (nu, sizeof (unsigned char));
6157
6158 return rt;
6159 }
6160
6161
6162 void
6163 clear_resource_table (resource_table *rt)
6164 {
6165 int i, j;
6166 for (i = 0; i < rt->allocated_cycles; i++)
6167 for (j = 0; j < rt->num_units; j++)
6168 rt->units[i][j] = 0;
6169 }
6170
6171
6172 /* We never shrink it, just fake it into thinking so. */
6173
6174 void
6175 resize_resource_table (resource_table *rt, int cycles)
6176 {
6177 int i, old_cycles;
6178
6179 rt->cycles = cycles;
6180 if (cycles <= rt->allocated_cycles)
6181 return;
6182
6183 old_cycles = rt->allocated_cycles;
6184 rt->allocated_cycles = cycles;
6185
6186 rt->units = xrealloc (rt->units,
6187 rt->allocated_cycles * sizeof (unsigned char *));
6188 for (i = 0; i < old_cycles; i++)
6189 rt->units[i] = xrealloc (rt->units[i],
6190 rt->num_units * sizeof (unsigned char));
6191 for (i = old_cycles; i < cycles; i++)
6192 rt->units[i] = xcalloc (rt->num_units, sizeof (unsigned char));
6193 }
6194
6195
6196 bfd_boolean
6197 resources_available (resource_table *rt, xtensa_opcode opcode, int cycle)
6198 {
6199 int i;
6200 int uses = (rt->opcode_num_units) (rt->data, opcode);
6201
6202 for (i = 0; i < uses; i++)
6203 {
6204 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
6205 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
6206 int copies_in_use = rt->units[stage + cycle][unit];
6207 int copies = (rt->unit_num_copies) (rt->data, unit);
6208 if (copies_in_use >= copies)
6209 return FALSE;
6210 }
6211 return TRUE;
6212 }
6213
6214
6215 void
6216 reserve_resources (resource_table *rt, xtensa_opcode opcode, int cycle)
6217 {
6218 int i;
6219 int uses = (rt->opcode_num_units) (rt->data, opcode);
6220
6221 for (i = 0; i < uses; i++)
6222 {
6223 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
6224 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
6225 /* Note that this allows resources to be oversubscribed. That's
6226 essential to the way the optional scheduler works.
6227 resources_available reports when a resource is over-subscribed,
6228 so it's easy to tell. */
6229 rt->units[stage + cycle][unit]++;
6230 }
6231 }
6232
6233
6234 void
6235 release_resources (resource_table *rt, xtensa_opcode opcode, int cycle)
6236 {
6237 int i;
6238 int uses = (rt->opcode_num_units) (rt->data, opcode);
6239
6240 for (i = 0; i < uses; i++)
6241 {
6242 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
6243 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
6244 gas_assert (rt->units[stage + cycle][unit] > 0);
6245 rt->units[stage + cycle][unit]--;
6246 }
6247 }
6248
6249
6250 /* Wrapper functions make parameterized resource reservation
6251 more convenient. */
6252
6253 int
6254 opcode_funcUnit_use_unit (void *data, xtensa_opcode opcode, int idx)
6255 {
6256 xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
6257 return use->unit;
6258 }
6259
6260
6261 int
6262 opcode_funcUnit_use_stage (void *data, xtensa_opcode opcode, int idx)
6263 {
6264 xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
6265 return use->stage;
6266 }
6267
6268
6269 /* Note that this function does not check issue constraints, but
6270 solely whether the hardware is available to execute the given
6271 instructions together. It also doesn't check if the tinsns
6272 write the same state, or access the same tieports. That is
6273 checked by check_t1_t2_reads_and_writes. */
6274
6275 static bfd_boolean
6276 resources_conflict (vliw_insn *vinsn)
6277 {
6278 int i;
6279 static resource_table *rt = NULL;
6280
6281 /* This is the most common case by far. Optimize it. */
6282 if (vinsn->num_slots == 1)
6283 return FALSE;
6284
6285 if (rt == NULL)
6286 {
6287 xtensa_isa isa = xtensa_default_isa;
6288 rt = new_resource_table
6289 (isa, xtensa_num_pipe_stages,
6290 xtensa_isa_num_funcUnits (isa),
6291 (unit_num_copies_func) xtensa_funcUnit_num_copies,
6292 (opcode_num_units_func) xtensa_opcode_num_funcUnit_uses,
6293 opcode_funcUnit_use_unit,
6294 opcode_funcUnit_use_stage);
6295 }
6296
6297 clear_resource_table (rt);
6298
6299 for (i = 0; i < vinsn->num_slots; i++)
6300 {
6301 if (!resources_available (rt, vinsn->slots[i].opcode, 0))
6302 return TRUE;
6303 reserve_resources (rt, vinsn->slots[i].opcode, 0);
6304 }
6305
6306 return FALSE;
6307 }
6308
6309 \f
6310 /* finish_vinsn, emit_single_op and helper functions. */
6311
6312 static bfd_boolean find_vinsn_conflicts (vliw_insn *);
6313 static xtensa_format xg_find_narrowest_format (vliw_insn *);
6314 static void xg_assemble_vliw_tokens (vliw_insn *);
6315
6316
6317 /* We have reached the end of a bundle; emit into the frag. */
6318
6319 static void
6320 finish_vinsn (vliw_insn *vinsn)
6321 {
6322 IStack slotstack;
6323 int i;
6324
6325 if (find_vinsn_conflicts (vinsn))
6326 {
6327 xg_clear_vinsn (vinsn);
6328 return;
6329 }
6330
6331 /* First, find a format that works. */
6332 if (vinsn->format == XTENSA_UNDEFINED)
6333 vinsn->format = xg_find_narrowest_format (vinsn);
6334
6335 if (xtensa_format_num_slots (xtensa_default_isa, vinsn->format) > 1
6336 && produce_flix == FLIX_NONE)
6337 {
6338 as_bad (_("The option \"--no-allow-flix\" prohibits multi-slot flix."));
6339 xg_clear_vinsn (vinsn);
6340 return;
6341 }
6342
6343 if (vinsn->format == XTENSA_UNDEFINED)
6344 {
6345 as_bad (_("couldn't find a valid instruction format"));
6346 fprintf (stderr, _(" ops were: "));
6347 for (i = 0; i < vinsn->num_slots; i++)
6348 fprintf (stderr, _(" %s;"),
6349 xtensa_opcode_name (xtensa_default_isa,
6350 vinsn->slots[i].opcode));
6351 fprintf (stderr, _("\n"));
6352 xg_clear_vinsn (vinsn);
6353 return;
6354 }
6355
6356 if (vinsn->num_slots
6357 != xtensa_format_num_slots (xtensa_default_isa, vinsn->format))
6358 {
6359 as_bad (_("format '%s' allows %d slots, but there are %d opcodes"),
6360 xtensa_format_name (xtensa_default_isa, vinsn->format),
6361 xtensa_format_num_slots (xtensa_default_isa, vinsn->format),
6362 vinsn->num_slots);
6363 xg_clear_vinsn (vinsn);
6364 return;
6365 }
6366
6367 if (resources_conflict (vinsn))
6368 {
6369 as_bad (_("illegal resource usage in bundle"));
6370 fprintf (stderr, " ops were: ");
6371 for (i = 0; i < vinsn->num_slots; i++)
6372 fprintf (stderr, " %s;",
6373 xtensa_opcode_name (xtensa_default_isa,
6374 vinsn->slots[i].opcode));
6375 fprintf (stderr, "\n");
6376 xg_clear_vinsn (vinsn);
6377 return;
6378 }
6379
6380 for (i = 0; i < vinsn->num_slots; i++)
6381 {
6382 if (vinsn->slots[i].opcode != XTENSA_UNDEFINED)
6383 {
6384 symbolS *lit_sym = NULL;
6385 int j;
6386 bfd_boolean e = FALSE;
6387 bfd_boolean saved_density = density_supported;
6388
6389 /* We don't want to narrow ops inside multi-slot bundles. */
6390 if (vinsn->num_slots > 1)
6391 density_supported = FALSE;
6392
6393 istack_init (&slotstack);
6394 if (vinsn->slots[i].opcode == xtensa_nop_opcode)
6395 {
6396 vinsn->slots[i].opcode =
6397 xtensa_format_slot_nop_opcode (xtensa_default_isa,
6398 vinsn->format, i);
6399 vinsn->slots[i].ntok = 0;
6400 }
6401
6402 if (xg_expand_assembly_insn (&slotstack, &vinsn->slots[i]))
6403 {
6404 e = TRUE;
6405 continue;
6406 }
6407
6408 density_supported = saved_density;
6409
6410 if (e)
6411 {
6412 xg_clear_vinsn (vinsn);
6413 return;
6414 }
6415
6416 for (j = 0; j < slotstack.ninsn; j++)
6417 {
6418 TInsn *insn = &slotstack.insn[j];
6419 if (insn->insn_type == ITYPE_LITERAL)
6420 {
6421 gas_assert (lit_sym == NULL);
6422 lit_sym = xg_assemble_literal (insn);
6423 }
6424 else
6425 {
6426 gas_assert (insn->insn_type == ITYPE_INSN);
6427 if (lit_sym)
6428 xg_resolve_literals (insn, lit_sym);
6429 if (j != slotstack.ninsn - 1)
6430 emit_single_op (insn);
6431 }
6432 }
6433
6434 if (vinsn->num_slots > 1)
6435 {
6436 if (opcode_fits_format_slot
6437 (slotstack.insn[slotstack.ninsn - 1].opcode,
6438 vinsn->format, i))
6439 {
6440 vinsn->slots[i] = slotstack.insn[slotstack.ninsn - 1];
6441 }
6442 else
6443 {
6444 emit_single_op (&slotstack.insn[slotstack.ninsn - 1]);
6445 if (vinsn->format == XTENSA_UNDEFINED)
6446 vinsn->slots[i].opcode = xtensa_nop_opcode;
6447 else
6448 vinsn->slots[i].opcode
6449 = xtensa_format_slot_nop_opcode (xtensa_default_isa,
6450 vinsn->format, i);
6451
6452 vinsn->slots[i].ntok = 0;
6453 }
6454 }
6455 else
6456 {
6457 vinsn->slots[0] = slotstack.insn[slotstack.ninsn - 1];
6458 vinsn->format = XTENSA_UNDEFINED;
6459 }
6460 }
6461 }
6462
6463 /* Now check resource conflicts on the modified bundle. */
6464 if (resources_conflict (vinsn))
6465 {
6466 as_bad (_("illegal resource usage in bundle"));
6467 fprintf (stderr, " ops were: ");
6468 for (i = 0; i < vinsn->num_slots; i++)
6469 fprintf (stderr, " %s;",
6470 xtensa_opcode_name (xtensa_default_isa,
6471 vinsn->slots[i].opcode));
6472 fprintf (stderr, "\n");
6473 xg_clear_vinsn (vinsn);
6474 return;
6475 }
6476
6477 /* First, find a format that works. */
6478 if (vinsn->format == XTENSA_UNDEFINED)
6479 vinsn->format = xg_find_narrowest_format (vinsn);
6480
6481 xg_assemble_vliw_tokens (vinsn);
6482
6483 xg_clear_vinsn (vinsn);
6484
6485 xtensa_check_frag_count ();
6486 }
6487
6488
6489 /* Given an vliw instruction, what conflicts are there in register
6490 usage and in writes to states and queues?
6491
6492 This function does two things:
6493 1. Reports an error when a vinsn contains illegal combinations
6494 of writes to registers states or queues.
6495 2. Marks individual tinsns as not relaxable if the combination
6496 contains antidependencies.
6497
6498 Job 2 handles things like swap semantics in instructions that need
6499 to be relaxed. For example,
6500
6501 addi a0, a1, 100000
6502
6503 normally would be relaxed to
6504
6505 l32r a0, some_label
6506 add a0, a1, a0
6507
6508 _but_, if the above instruction is bundled with an a0 reader, e.g.,
6509
6510 { addi a0, a1, 10000 ; add a2, a0, a4 ; }
6511
6512 then we can't relax it into
6513
6514 l32r a0, some_label
6515 { add a0, a1, a0 ; add a2, a0, a4 ; }
6516
6517 because the value of a0 is trashed before the second add can read it. */
6518
6519 static char check_t1_t2_reads_and_writes (TInsn *, TInsn *);
6520
6521 static bfd_boolean
6522 find_vinsn_conflicts (vliw_insn *vinsn)
6523 {
6524 int i, j;
6525 int branches = 0;
6526 xtensa_isa isa = xtensa_default_isa;
6527
6528 gas_assert (!past_xtensa_end);
6529
6530 for (i = 0 ; i < vinsn->num_slots; i++)
6531 {
6532 TInsn *op1 = &vinsn->slots[i];
6533 if (op1->is_specific_opcode)
6534 op1->keep_wide = TRUE;
6535 else
6536 op1->keep_wide = FALSE;
6537 }
6538
6539 for (i = 0 ; i < vinsn->num_slots; i++)
6540 {
6541 TInsn *op1 = &vinsn->slots[i];
6542
6543 if (xtensa_opcode_is_branch (isa, op1->opcode) == 1)
6544 branches++;
6545
6546 for (j = 0; j < vinsn->num_slots; j++)
6547 {
6548 if (i != j)
6549 {
6550 TInsn *op2 = &vinsn->slots[j];
6551 char conflict_type = check_t1_t2_reads_and_writes (op1, op2);
6552 switch (conflict_type)
6553 {
6554 case 'c':
6555 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same register"),
6556 xtensa_opcode_name (isa, op1->opcode), i,
6557 xtensa_opcode_name (isa, op2->opcode), j);
6558 return TRUE;
6559 case 'd':
6560 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same state"),
6561 xtensa_opcode_name (isa, op1->opcode), i,
6562 xtensa_opcode_name (isa, op2->opcode), j);
6563 return TRUE;
6564 case 'e':
6565 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same port"),
6566 xtensa_opcode_name (isa, op1->opcode), i,
6567 xtensa_opcode_name (isa, op2->opcode), j);
6568 return TRUE;
6569 case 'f':
6570 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) both have volatile port accesses"),
6571 xtensa_opcode_name (isa, op1->opcode), i,
6572 xtensa_opcode_name (isa, op2->opcode), j);
6573 return TRUE;
6574 default:
6575 /* Everything is OK. */
6576 break;
6577 }
6578 op2->is_specific_opcode = (op2->is_specific_opcode
6579 || conflict_type == 'a');
6580 }
6581 }
6582 }
6583
6584 if (branches > 1)
6585 {
6586 as_bad (_("multiple branches or jumps in the same bundle"));
6587 return TRUE;
6588 }
6589
6590 return FALSE;
6591 }
6592
6593
6594 /* Check how the state used by t1 and t2 relate.
6595 Cases found are:
6596
6597 case A: t1 reads a register t2 writes (an antidependency within a bundle)
6598 case B: no relationship between what is read and written (both could
6599 read the same reg though)
6600 case C: t1 writes a register t2 writes (a register conflict within a
6601 bundle)
6602 case D: t1 writes a state that t2 also writes
6603 case E: t1 writes a tie queue that t2 also writes
6604 case F: two volatile queue accesses
6605 */
6606
6607 static char
6608 check_t1_t2_reads_and_writes (TInsn *t1, TInsn *t2)
6609 {
6610 xtensa_isa isa = xtensa_default_isa;
6611 xtensa_regfile t1_regfile, t2_regfile;
6612 int t1_reg, t2_reg;
6613 int t1_base_reg, t1_last_reg;
6614 int t2_base_reg, t2_last_reg;
6615 char t1_inout, t2_inout;
6616 int i, j;
6617 char conflict = 'b';
6618 int t1_states;
6619 int t2_states;
6620 int t1_interfaces;
6621 int t2_interfaces;
6622 bfd_boolean t1_volatile = FALSE;
6623 bfd_boolean t2_volatile = FALSE;
6624
6625 /* Check registers. */
6626 for (j = 0; j < t2->ntok; j++)
6627 {
6628 if (xtensa_operand_is_register (isa, t2->opcode, j) != 1)
6629 continue;
6630
6631 t2_regfile = xtensa_operand_regfile (isa, t2->opcode, j);
6632 t2_base_reg = t2->tok[j].X_add_number;
6633 t2_last_reg = t2_base_reg + xtensa_operand_num_regs (isa, t2->opcode, j);
6634
6635 for (i = 0; i < t1->ntok; i++)
6636 {
6637 if (xtensa_operand_is_register (isa, t1->opcode, i) != 1)
6638 continue;
6639
6640 t1_regfile = xtensa_operand_regfile (isa, t1->opcode, i);
6641
6642 if (t1_regfile != t2_regfile)
6643 continue;
6644
6645 t1_inout = xtensa_operand_inout (isa, t1->opcode, i);
6646 t2_inout = xtensa_operand_inout (isa, t2->opcode, j);
6647
6648 if (xtensa_operand_is_known_reg (isa, t1->opcode, i) == 0
6649 || xtensa_operand_is_known_reg (isa, t2->opcode, j) == 0)
6650 {
6651 if (t1_inout == 'm' || t1_inout == 'o'
6652 || t2_inout == 'm' || t2_inout == 'o')
6653 {
6654 conflict = 'a';
6655 continue;
6656 }
6657 }
6658
6659 t1_base_reg = t1->tok[i].X_add_number;
6660 t1_last_reg = (t1_base_reg
6661 + xtensa_operand_num_regs (isa, t1->opcode, i));
6662
6663 for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++)
6664 {
6665 for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++)
6666 {
6667 if (t1_reg != t2_reg)
6668 continue;
6669
6670 if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
6671 {
6672 conflict = 'a';
6673 continue;
6674 }
6675
6676 if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
6677 {
6678 conflict = 'a';
6679 continue;
6680 }
6681
6682 if (t1_inout != 'i' && t2_inout != 'i')
6683 return 'c';
6684 }
6685 }
6686 }
6687 }
6688
6689 /* Check states. */
6690 t1_states = xtensa_opcode_num_stateOperands (isa, t1->opcode);
6691 t2_states = xtensa_opcode_num_stateOperands (isa, t2->opcode);
6692 for (j = 0; j < t2_states; j++)
6693 {
6694 xtensa_state t2_so = xtensa_stateOperand_state (isa, t2->opcode, j);
6695 t2_inout = xtensa_stateOperand_inout (isa, t2->opcode, j);
6696 for (i = 0; i < t1_states; i++)
6697 {
6698 xtensa_state t1_so = xtensa_stateOperand_state (isa, t1->opcode, i);
6699 t1_inout = xtensa_stateOperand_inout (isa, t1->opcode, i);
6700 if (t1_so != t2_so || xtensa_state_is_shared_or (isa, t1_so) == 1)
6701 continue;
6702
6703 if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
6704 {
6705 conflict = 'a';
6706 continue;
6707 }
6708
6709 if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
6710 {
6711 conflict = 'a';
6712 continue;
6713 }
6714
6715 if (t1_inout != 'i' && t2_inout != 'i')
6716 return 'd';
6717 }
6718 }
6719
6720 /* Check tieports. */
6721 t1_interfaces = xtensa_opcode_num_interfaceOperands (isa, t1->opcode);
6722 t2_interfaces = xtensa_opcode_num_interfaceOperands (isa, t2->opcode);
6723 for (j = 0; j < t2_interfaces; j++)
6724 {
6725 xtensa_interface t2_int
6726 = xtensa_interfaceOperand_interface (isa, t2->opcode, j);
6727 int t2_class = xtensa_interface_class_id (isa, t2_int);
6728
6729 t2_inout = xtensa_interface_inout (isa, t2_int);
6730 if (xtensa_interface_has_side_effect (isa, t2_int) == 1)
6731 t2_volatile = TRUE;
6732
6733 for (i = 0; i < t1_interfaces; i++)
6734 {
6735 xtensa_interface t1_int
6736 = xtensa_interfaceOperand_interface (isa, t1->opcode, j);
6737 int t1_class = xtensa_interface_class_id (isa, t1_int);
6738
6739 t1_inout = xtensa_interface_inout (isa, t1_int);
6740 if (xtensa_interface_has_side_effect (isa, t1_int) == 1)
6741 t1_volatile = TRUE;
6742
6743 if (t1_volatile && t2_volatile && (t1_class == t2_class))
6744 return 'f';
6745
6746 if (t1_int != t2_int)
6747 continue;
6748
6749 if (t2_inout == 'i' && t1_inout == 'o')
6750 {
6751 conflict = 'a';
6752 continue;
6753 }
6754
6755 if (t1_inout == 'i' && t2_inout == 'o')
6756 {
6757 conflict = 'a';
6758 continue;
6759 }
6760
6761 if (t1_inout != 'i' && t2_inout != 'i')
6762 return 'e';
6763 }
6764 }
6765
6766 return conflict;
6767 }
6768
6769
6770 static xtensa_format
6771 xg_find_narrowest_format (vliw_insn *vinsn)
6772 {
6773 /* Right now we assume that the ops within the vinsn are properly
6774 ordered for the slots that the programmer wanted them in. In
6775 other words, we don't rearrange the ops in hopes of finding a
6776 better format. The scheduler handles that. */
6777
6778 xtensa_isa isa = xtensa_default_isa;
6779 xtensa_format format;
6780 xtensa_opcode nop_opcode = xtensa_nop_opcode;
6781
6782 if (vinsn->num_slots == 1)
6783 return xg_get_single_format (vinsn->slots[0].opcode);
6784
6785 for (format = 0; format < xtensa_isa_num_formats (isa); format++)
6786 {
6787 vliw_insn v_copy;
6788 xg_copy_vinsn (&v_copy, vinsn);
6789 if (xtensa_format_num_slots (isa, format) == v_copy.num_slots)
6790 {
6791 int slot;
6792 int fit = 0;
6793 for (slot = 0; slot < v_copy.num_slots; slot++)
6794 {
6795 if (v_copy.slots[slot].opcode == nop_opcode)
6796 {
6797 v_copy.slots[slot].opcode =
6798 xtensa_format_slot_nop_opcode (isa, format, slot);
6799 v_copy.slots[slot].ntok = 0;
6800 }
6801
6802 if (opcode_fits_format_slot (v_copy.slots[slot].opcode,
6803 format, slot))
6804 fit++;
6805 else if (v_copy.num_slots > 1)
6806 {
6807 TInsn widened;
6808 /* Try the widened version. */
6809 if (!v_copy.slots[slot].keep_wide
6810 && !v_copy.slots[slot].is_specific_opcode
6811 && xg_is_single_relaxable_insn (&v_copy.slots[slot],
6812 &widened, TRUE)
6813 && opcode_fits_format_slot (widened.opcode,
6814 format, slot))
6815 {
6816 v_copy.slots[slot] = widened;
6817 fit++;
6818 }
6819 }
6820 }
6821 if (fit == v_copy.num_slots)
6822 {
6823 xg_copy_vinsn (vinsn, &v_copy);
6824 xtensa_format_encode (isa, format, vinsn->insnbuf);
6825 vinsn->format = format;
6826 break;
6827 }
6828 }
6829 }
6830
6831 if (format == xtensa_isa_num_formats (isa))
6832 return XTENSA_UNDEFINED;
6833
6834 return format;
6835 }
6836
6837
6838 /* Return the additional space needed in a frag
6839 for possible relaxations of any ops in a VLIW insn.
6840 Also fill out the relaxations that might be required of
6841 each tinsn in the vinsn. */
6842
6843 static int
6844 relaxation_requirements (vliw_insn *vinsn, bfd_boolean *pfinish_frag)
6845 {
6846 bfd_boolean finish_frag = FALSE;
6847 int extra_space = 0;
6848 int slot;
6849
6850 for (slot = 0; slot < vinsn->num_slots; slot++)
6851 {
6852 TInsn *tinsn = &vinsn->slots[slot];
6853 if (!tinsn_has_symbolic_operands (tinsn))
6854 {
6855 /* A narrow instruction could be widened later to help
6856 alignment issues. */
6857 if (xg_is_single_relaxable_insn (tinsn, 0, TRUE)
6858 && !tinsn->is_specific_opcode
6859 && vinsn->num_slots == 1)
6860 {
6861 /* Difference in bytes between narrow and wide insns... */
6862 extra_space += 1;
6863 tinsn->subtype = RELAX_NARROW;
6864 }
6865 }
6866 else
6867 {
6868 if (workaround_b_j_loop_end
6869 && tinsn->opcode == xtensa_jx_opcode
6870 && use_transform ())
6871 {
6872 /* Add 2 of these. */
6873 extra_space += 3; /* for the nop size */
6874 tinsn->subtype = RELAX_ADD_NOP_IF_PRE_LOOP_END;
6875 }
6876
6877 /* Need to assemble it with space for the relocation. */
6878 if (xg_is_relaxable_insn (tinsn, 0)
6879 && !tinsn->is_specific_opcode)
6880 {
6881 int max_size = xg_get_max_insn_widen_size (tinsn->opcode);
6882 int max_literal_size =
6883 xg_get_max_insn_widen_literal_size (tinsn->opcode);
6884
6885 tinsn->literal_space = max_literal_size;
6886
6887 tinsn->subtype = RELAX_IMMED;
6888 extra_space += max_size;
6889 }
6890 else
6891 {
6892 /* A fix record will be added for this instruction prior
6893 to relaxation, so make it end the frag. */
6894 finish_frag = TRUE;
6895 }
6896 }
6897 }
6898 *pfinish_frag = finish_frag;
6899 return extra_space;
6900 }
6901
6902
6903 static void
6904 bundle_tinsn (TInsn *tinsn, vliw_insn *vinsn)
6905 {
6906 xtensa_isa isa = xtensa_default_isa;
6907 int slot, chosen_slot;
6908
6909 vinsn->format = xg_get_single_format (tinsn->opcode);
6910 gas_assert (vinsn->format != XTENSA_UNDEFINED);
6911 vinsn->num_slots = xtensa_format_num_slots (isa, vinsn->format);
6912
6913 chosen_slot = xg_get_single_slot (tinsn->opcode);
6914 for (slot = 0; slot < vinsn->num_slots; slot++)
6915 {
6916 if (slot == chosen_slot)
6917 vinsn->slots[slot] = *tinsn;
6918 else
6919 {
6920 vinsn->slots[slot].opcode =
6921 xtensa_format_slot_nop_opcode (isa, vinsn->format, slot);
6922 vinsn->slots[slot].ntok = 0;
6923 vinsn->slots[slot].insn_type = ITYPE_INSN;
6924 }
6925 }
6926 }
6927
6928
6929 static bfd_boolean
6930 emit_single_op (TInsn *orig_insn)
6931 {
6932 int i;
6933 IStack istack; /* put instructions into here */
6934 symbolS *lit_sym = NULL;
6935 symbolS *label_sym = NULL;
6936
6937 istack_init (&istack);
6938
6939 /* Special-case for "movi aX, foo" which is guaranteed to need relaxing.
6940 Because the scheduling and bundling characteristics of movi and
6941 l32r or const16 are so different, we can do much better if we relax
6942 it prior to scheduling and bundling, rather than after. */
6943 if ((orig_insn->opcode == xtensa_movi_opcode
6944 || orig_insn->opcode == xtensa_movi_n_opcode)
6945 && !cur_vinsn.inside_bundle
6946 && (orig_insn->tok[1].X_op == O_symbol
6947 || orig_insn->tok[1].X_op == O_pltrel
6948 || orig_insn->tok[1].X_op == O_tlsfunc
6949 || orig_insn->tok[1].X_op == O_tlsarg
6950 || orig_insn->tok[1].X_op == O_tpoff
6951 || orig_insn->tok[1].X_op == O_dtpoff)
6952 && !orig_insn->is_specific_opcode && use_transform ())
6953 xg_assembly_relax (&istack, orig_insn, now_seg, frag_now, 0, 1, 0);
6954 else
6955 if (xg_expand_assembly_insn (&istack, orig_insn))
6956 return TRUE;
6957
6958 for (i = 0; i < istack.ninsn; i++)
6959 {
6960 TInsn *insn = &istack.insn[i];
6961 switch (insn->insn_type)
6962 {
6963 case ITYPE_LITERAL:
6964 gas_assert (lit_sym == NULL);
6965 lit_sym = xg_assemble_literal (insn);
6966 break;
6967 case ITYPE_LABEL:
6968 {
6969 static int relaxed_sym_idx = 0;
6970 char *label = xmalloc (strlen (FAKE_LABEL_NAME) + 12);
6971 sprintf (label, "%s_rl_%x", FAKE_LABEL_NAME, relaxed_sym_idx++);
6972 colon (label);
6973 gas_assert (label_sym == NULL);
6974 label_sym = symbol_find_or_make (label);
6975 gas_assert (label_sym);
6976 free (label);
6977 }
6978 break;
6979 case ITYPE_INSN:
6980 {
6981 vliw_insn v;
6982 if (lit_sym)
6983 xg_resolve_literals (insn, lit_sym);
6984 if (label_sym)
6985 xg_resolve_labels (insn, label_sym);
6986 xg_init_vinsn (&v);
6987 bundle_tinsn (insn, &v);
6988 finish_vinsn (&v);
6989 xg_free_vinsn (&v);
6990 }
6991 break;
6992 default:
6993 gas_assert (0);
6994 break;
6995 }
6996 }
6997 return FALSE;
6998 }
6999
7000
7001 static int
7002 total_frag_text_expansion (fragS *fragP)
7003 {
7004 int slot;
7005 int total_expansion = 0;
7006
7007 for (slot = 0; slot < config_max_slots; slot++)
7008 total_expansion += fragP->tc_frag_data.text_expansion[slot];
7009
7010 return total_expansion;
7011 }
7012
7013
7014 /* Emit a vliw instruction to the current fragment. */
7015
7016 static void
7017 xg_assemble_vliw_tokens (vliw_insn *vinsn)
7018 {
7019 bfd_boolean finish_frag;
7020 bfd_boolean is_jump = FALSE;
7021 bfd_boolean is_branch = FALSE;
7022 xtensa_isa isa = xtensa_default_isa;
7023 int insn_size;
7024 int extra_space;
7025 char *f = NULL;
7026 int slot;
7027 struct dwarf2_line_info debug_line;
7028 bfd_boolean loc_directive_seen = FALSE;
7029 TInsn *tinsn;
7030
7031 memset (&debug_line, 0, sizeof (struct dwarf2_line_info));
7032
7033 if (generating_literals)
7034 {
7035 static int reported = 0;
7036 if (reported < 4)
7037 as_bad_where (frag_now->fr_file, frag_now->fr_line,
7038 _("cannot assemble into a literal fragment"));
7039 if (reported == 3)
7040 as_bad (_("..."));
7041 reported++;
7042 return;
7043 }
7044
7045 if (frag_now_fix () != 0
7046 && (! frag_now->tc_frag_data.is_insn
7047 || (vinsn_has_specific_opcodes (vinsn) && use_transform ())
7048 || (!use_transform ()) != frag_now->tc_frag_data.is_no_transform
7049 || (directive_state[directive_longcalls]
7050 != frag_now->tc_frag_data.use_longcalls)
7051 || (directive_state[directive_absolute_literals]
7052 != frag_now->tc_frag_data.use_absolute_literals)))
7053 {
7054 frag_wane (frag_now);
7055 frag_new (0);
7056 xtensa_set_frag_assembly_state (frag_now);
7057 }
7058
7059 if (workaround_a0_b_retw
7060 && vinsn->num_slots == 1
7061 && (get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0
7062 && xtensa_opcode_is_branch (isa, vinsn->slots[0].opcode) == 1
7063 && use_transform ())
7064 {
7065 has_a0_b_retw = TRUE;
7066
7067 /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW.
7068 After the first assembly pass we will check all of them and
7069 add a nop if needed. */
7070 frag_now->tc_frag_data.is_insn = TRUE;
7071 frag_var (rs_machine_dependent, 4, 4,
7072 RELAX_ADD_NOP_IF_A0_B_RETW,
7073 frag_now->fr_symbol,
7074 frag_now->fr_offset,
7075 NULL);
7076 xtensa_set_frag_assembly_state (frag_now);
7077 frag_now->tc_frag_data.is_insn = TRUE;
7078 frag_var (rs_machine_dependent, 4, 4,
7079 RELAX_ADD_NOP_IF_A0_B_RETW,
7080 frag_now->fr_symbol,
7081 frag_now->fr_offset,
7082 NULL);
7083 xtensa_set_frag_assembly_state (frag_now);
7084 }
7085
7086 for (slot = 0; slot < vinsn->num_slots; slot++)
7087 {
7088 tinsn = &vinsn->slots[slot];
7089
7090 /* See if the instruction implies an aligned section. */
7091 if (xtensa_opcode_is_loop (isa, tinsn->opcode) == 1)
7092 record_alignment (now_seg, 2);
7093
7094 /* Determine the best line number for debug info. */
7095 if ((tinsn->loc_directive_seen || !loc_directive_seen)
7096 && (tinsn->debug_line.filenum != debug_line.filenum
7097 || tinsn->debug_line.line < debug_line.line
7098 || tinsn->debug_line.column < debug_line.column))
7099 debug_line = tinsn->debug_line;
7100 if (tinsn->loc_directive_seen)
7101 loc_directive_seen = TRUE;
7102 }
7103
7104 /* Special cases for instructions that force an alignment... */
7105 /* None of these opcodes are bundle-able. */
7106 if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1)
7107 {
7108 int max_fill;
7109
7110 /* Remember the symbol that marks the end of the loop in the frag
7111 that marks the start of the loop. This way we can easily find
7112 the end of the loop at the beginning, without adding special code
7113 to mark the loop instructions themselves. */
7114 symbolS *target_sym = NULL;
7115 if (vinsn->slots[0].tok[1].X_op == O_symbol)
7116 target_sym = vinsn->slots[0].tok[1].X_add_symbol;
7117
7118 xtensa_set_frag_assembly_state (frag_now);
7119 frag_now->tc_frag_data.is_insn = TRUE;
7120
7121 max_fill = get_text_align_max_fill_size
7122 (get_text_align_power (xtensa_fetch_width),
7123 TRUE, frag_now->tc_frag_data.is_no_density);
7124
7125 if (use_transform ())
7126 frag_var (rs_machine_dependent, max_fill, max_fill,
7127 RELAX_ALIGN_NEXT_OPCODE, target_sym, 0, NULL);
7128 else
7129 frag_var (rs_machine_dependent, 0, 0,
7130 RELAX_CHECK_ALIGN_NEXT_OPCODE, target_sym, 0, NULL);
7131 xtensa_set_frag_assembly_state (frag_now);
7132 }
7133
7134 if (vinsn->slots[0].opcode == xtensa_entry_opcode
7135 && !vinsn->slots[0].is_specific_opcode)
7136 {
7137 xtensa_mark_literal_pool_location ();
7138 xtensa_move_labels (frag_now, 0);
7139 frag_var (rs_align_test, 1, 1, 0, NULL, 2, NULL);
7140 }
7141
7142 if (vinsn->num_slots == 1)
7143 {
7144 if (workaround_a0_b_retw && use_transform ())
7145 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER,
7146 is_register_writer (&vinsn->slots[0], "a", 0));
7147
7148 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND,
7149 is_bad_loopend_opcode (&vinsn->slots[0]));
7150 }
7151 else
7152 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND, FALSE);
7153
7154 insn_size = xtensa_format_length (isa, vinsn->format);
7155
7156 extra_space = relaxation_requirements (vinsn, &finish_frag);
7157
7158 /* vinsn_to_insnbuf will produce the error. */
7159 if (vinsn->format != XTENSA_UNDEFINED)
7160 {
7161 f = frag_more (insn_size + extra_space);
7162 xtensa_set_frag_assembly_state (frag_now);
7163 frag_now->tc_frag_data.is_insn = TRUE;
7164 }
7165
7166 vinsn_to_insnbuf (vinsn, f, frag_now, FALSE);
7167 if (vinsn->format == XTENSA_UNDEFINED)
7168 return;
7169
7170 xtensa_insnbuf_to_chars (isa, vinsn->insnbuf, (unsigned char *) f, 0);
7171
7172 if (debug_type == DEBUG_DWARF2 || loc_directive_seen)
7173 dwarf2_gen_line_info (frag_now_fix () - (insn_size + extra_space),
7174 &debug_line);
7175
7176 for (slot = 0; slot < vinsn->num_slots; slot++)
7177 {
7178 tinsn = &vinsn->slots[slot];
7179 frag_now->tc_frag_data.slot_subtypes[slot] = tinsn->subtype;
7180 frag_now->tc_frag_data.slot_symbols[slot] = tinsn->symbol;
7181 frag_now->tc_frag_data.slot_offsets[slot] = tinsn->offset;
7182 frag_now->tc_frag_data.literal_frags[slot] = tinsn->literal_frag;
7183 if (tinsn->opcode == xtensa_l32r_opcode)
7184 {
7185 frag_now->tc_frag_data.literal_frags[slot] =
7186 tinsn->tok[1].X_add_symbol->sy_frag;
7187 }
7188 if (tinsn->literal_space != 0)
7189 xg_assemble_literal_space (tinsn->literal_space, slot);
7190 frag_now->tc_frag_data.free_reg[slot] = tinsn->extra_arg;
7191
7192 if (tinsn->subtype == RELAX_NARROW)
7193 gas_assert (vinsn->num_slots == 1);
7194 if (xtensa_opcode_is_jump (isa, tinsn->opcode) == 1)
7195 is_jump = TRUE;
7196 if (xtensa_opcode_is_branch (isa, tinsn->opcode) == 1)
7197 is_branch = TRUE;
7198
7199 if (tinsn->subtype || tinsn->symbol || tinsn->offset
7200 || tinsn->literal_frag || is_jump || is_branch)
7201 finish_frag = TRUE;
7202 }
7203
7204 if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
7205 frag_now->tc_frag_data.is_specific_opcode = TRUE;
7206
7207 if (finish_frag)
7208 {
7209 frag_variant (rs_machine_dependent,
7210 extra_space, extra_space, RELAX_SLOTS,
7211 frag_now->fr_symbol, frag_now->fr_offset, f);
7212 xtensa_set_frag_assembly_state (frag_now);
7213 }
7214
7215 /* Special cases for loops:
7216 close_loop_end should be inserted AFTER short_loop.
7217 Make sure that CLOSE loops are processed BEFORE short_loops
7218 when converting them. */
7219
7220 /* "short_loop": Add a NOP if the loop is < 4 bytes. */
7221 if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1
7222 && !vinsn->slots[0].is_specific_opcode)
7223 {
7224 if (workaround_short_loop && use_transform ())
7225 {
7226 maybe_has_short_loop = TRUE;
7227 frag_now->tc_frag_data.is_insn = TRUE;
7228 frag_var (rs_machine_dependent, 4, 4,
7229 RELAX_ADD_NOP_IF_SHORT_LOOP,
7230 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7231 frag_now->tc_frag_data.is_insn = TRUE;
7232 frag_var (rs_machine_dependent, 4, 4,
7233 RELAX_ADD_NOP_IF_SHORT_LOOP,
7234 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7235 }
7236
7237 /* "close_loop_end": Add up to 12 bytes of NOPs to keep a
7238 loop at least 12 bytes away from another loop's end. */
7239 if (workaround_close_loop_end && use_transform ())
7240 {
7241 maybe_has_close_loop_end = TRUE;
7242 frag_now->tc_frag_data.is_insn = TRUE;
7243 frag_var (rs_machine_dependent, 12, 12,
7244 RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
7245 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7246 }
7247 }
7248
7249 if (use_transform ())
7250 {
7251 if (is_jump)
7252 {
7253 gas_assert (finish_frag);
7254 frag_var (rs_machine_dependent,
7255 xtensa_fetch_width, xtensa_fetch_width,
7256 RELAX_UNREACHABLE,
7257 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7258 xtensa_set_frag_assembly_state (frag_now);
7259 xtensa_maybe_create_trampoline_frag ();
7260 /* Always create one here. */
7261 xtensa_maybe_create_literal_pool_frag (TRUE, FALSE);
7262 }
7263 else if (is_branch && do_align_targets ())
7264 {
7265 gas_assert (finish_frag);
7266 frag_var (rs_machine_dependent,
7267 xtensa_fetch_width, xtensa_fetch_width,
7268 RELAX_MAYBE_UNREACHABLE,
7269 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7270 xtensa_set_frag_assembly_state (frag_now);
7271 frag_var (rs_machine_dependent,
7272 0, 0,
7273 RELAX_MAYBE_DESIRE_ALIGN,
7274 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7275 xtensa_set_frag_assembly_state (frag_now);
7276 }
7277 }
7278
7279 /* Now, if the original opcode was a call... */
7280 if (do_align_targets ()
7281 && xtensa_opcode_is_call (isa, vinsn->slots[0].opcode) == 1)
7282 {
7283 float freq = get_subseg_total_freq (now_seg, now_subseg);
7284 frag_now->tc_frag_data.is_insn = TRUE;
7285 frag_var (rs_machine_dependent, 4, (int) freq, RELAX_DESIRE_ALIGN,
7286 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7287 xtensa_set_frag_assembly_state (frag_now);
7288 }
7289
7290 if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
7291 {
7292 frag_wane (frag_now);
7293 frag_new (0);
7294 xtensa_set_frag_assembly_state (frag_now);
7295 }
7296 }
7297
7298 \f
7299 /* xtensa_end and helper functions. */
7300
7301 static void xtensa_cleanup_align_frags (void);
7302 static void xtensa_fix_target_frags (void);
7303 static void xtensa_mark_narrow_branches (void);
7304 static void xtensa_mark_zcl_first_insns (void);
7305 static void xtensa_mark_difference_of_two_symbols (void);
7306 static void xtensa_fix_a0_b_retw_frags (void);
7307 static void xtensa_fix_b_j_loop_end_frags (void);
7308 static void xtensa_fix_close_loop_end_frags (void);
7309 static void xtensa_fix_short_loop_frags (void);
7310 static void xtensa_sanity_check (void);
7311 static void xtensa_add_config_info (void);
7312
7313 void
7314 xtensa_end (void)
7315 {
7316 directive_balance ();
7317 xtensa_flush_pending_output ();
7318
7319 past_xtensa_end = TRUE;
7320
7321 xtensa_move_literals ();
7322
7323 xtensa_reorder_segments ();
7324 xtensa_cleanup_align_frags ();
7325 xtensa_fix_target_frags ();
7326 if (workaround_a0_b_retw && has_a0_b_retw)
7327 xtensa_fix_a0_b_retw_frags ();
7328 if (workaround_b_j_loop_end)
7329 xtensa_fix_b_j_loop_end_frags ();
7330
7331 /* "close_loop_end" should be processed BEFORE "short_loop". */
7332 if (workaround_close_loop_end && maybe_has_close_loop_end)
7333 xtensa_fix_close_loop_end_frags ();
7334
7335 if (workaround_short_loop && maybe_has_short_loop)
7336 xtensa_fix_short_loop_frags ();
7337 if (align_targets)
7338 xtensa_mark_narrow_branches ();
7339 xtensa_mark_zcl_first_insns ();
7340
7341 xtensa_sanity_check ();
7342
7343 xtensa_add_config_info ();
7344
7345 xtensa_check_frag_count ();
7346 }
7347
7348
7349 struct trampoline_frag
7350 {
7351 struct trampoline_frag *next;
7352 bfd_boolean needs_jump_around;
7353 fragS *fragP;
7354 fixS *fixP;
7355 };
7356
7357 struct trampoline_seg
7358 {
7359 struct trampoline_seg *next;
7360 asection *seg;
7361 struct trampoline_frag trampoline_list;
7362 };
7363
7364 static struct trampoline_seg trampoline_seg_list;
7365 #define J_RANGE (128 * 1024)
7366
7367 static int unreachable_count = 0;
7368
7369
7370 static void
7371 xtensa_maybe_create_trampoline_frag (void)
7372 {
7373 if (!use_trampolines)
7374 return;
7375
7376 /* We create an area for possible trampolines every 10 unreachable frags.
7377 These are preferred over the ones not preceded by an unreachable frag,
7378 because we don't have to jump around them. This function is called after
7379 each RELAX_UNREACHABLE frag is created. */
7380
7381 if (++unreachable_count > 10)
7382 {
7383 xtensa_create_trampoline_frag (FALSE);
7384 clear_frag_count ();
7385 unreachable_count = 0;
7386 }
7387 }
7388
7389 static void
7390 xtensa_check_frag_count (void)
7391 {
7392 if (!use_trampolines || frag_now->tc_frag_data.is_no_transform)
7393 return;
7394
7395 /* We create an area for possible trampolines every 8000 frags or so. This
7396 is an estimate based on the max range of a "j" insn (+/-128K) divided
7397 by a typical frag byte count (16), minus a few for safety. This function
7398 is called after each source line is processed. */
7399
7400 if (get_frag_count () > 8000)
7401 {
7402 xtensa_create_trampoline_frag (TRUE);
7403 clear_frag_count ();
7404 unreachable_count = 0;
7405 }
7406
7407 /* We create an area for a possible literal pool every N (default 5000)
7408 frags or so. */
7409 xtensa_maybe_create_literal_pool_frag (TRUE, TRUE);
7410 }
7411
7412 static xtensa_insnbuf trampoline_buf = NULL;
7413 static xtensa_insnbuf trampoline_slotbuf = NULL;
7414
7415 static xtensa_insnbuf litpool_buf = NULL;
7416 static xtensa_insnbuf litpool_slotbuf = NULL;
7417
7418 #define TRAMPOLINE_FRAG_SIZE 3000
7419
7420 static void
7421 xtensa_create_trampoline_frag (bfd_boolean needs_jump_around)
7422 {
7423 /* Emit a frag where we can place intermediate jump instructions,
7424 in case we need to jump farther than 128K bytes.
7425 Each jump instruction takes three bytes.
7426 We allocate enough for 1000 trampolines in each frag.
7427 If that's not enough, oh well. */
7428
7429 struct trampoline_seg *ts = trampoline_seg_list.next;
7430 struct trampoline_frag *tf;
7431 char *varP;
7432 fragS *fragP;
7433 int size = TRAMPOLINE_FRAG_SIZE;
7434
7435 for ( ; ts; ts = ts->next)
7436 {
7437 if (ts->seg == now_seg)
7438 break;
7439 }
7440
7441 if (ts == NULL)
7442 {
7443 ts = (struct trampoline_seg *)xcalloc(sizeof (struct trampoline_seg), 1);
7444 ts->next = trampoline_seg_list.next;
7445 trampoline_seg_list.next = ts;
7446 ts->seg = now_seg;
7447 }
7448
7449 frag_wane (frag_now);
7450 frag_new (0);
7451 xtensa_set_frag_assembly_state (frag_now);
7452 varP = frag_var (rs_machine_dependent, size, size, RELAX_TRAMPOLINE, NULL, 0, NULL);
7453 fragP = (fragS *)(varP - SIZEOF_STRUCT_FRAG);
7454 if (trampoline_buf == NULL)
7455 {
7456 trampoline_buf = xtensa_insnbuf_alloc (xtensa_default_isa);
7457 trampoline_slotbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
7458 }
7459 tf = (struct trampoline_frag *)xmalloc(sizeof (struct trampoline_frag));
7460 tf->next = ts->trampoline_list.next;
7461 ts->trampoline_list.next = tf;
7462 tf->needs_jump_around = needs_jump_around;
7463 tf->fragP = fragP;
7464 tf->fixP = NULL;
7465 }
7466
7467
7468 static struct trampoline_seg *
7469 find_trampoline_seg (asection *seg)
7470 {
7471 struct trampoline_seg *ts = trampoline_seg_list.next;
7472
7473 for ( ; ts; ts = ts->next)
7474 {
7475 if (ts->seg == seg)
7476 return ts;
7477 }
7478
7479 return NULL;
7480 }
7481
7482
7483 void dump_trampolines (void);
7484
7485 void
7486 dump_trampolines (void)
7487 {
7488 struct trampoline_seg *ts = trampoline_seg_list.next;
7489
7490 for ( ; ts; ts = ts->next)
7491 {
7492 asection *seg = ts->seg;
7493
7494 if (seg == NULL)
7495 continue;
7496 fprintf(stderr, "SECTION %s\n", seg->name);
7497 struct trampoline_frag *tf = ts->trampoline_list.next;
7498 for ( ; tf; tf = tf->next)
7499 {
7500 if (tf->fragP == NULL)
7501 continue;
7502 fprintf(stderr, " 0x%08x: fix=%d, jump_around=%s\n",
7503 (int)tf->fragP->fr_address, (int)tf->fragP->fr_fix,
7504 tf->needs_jump_around ? "T" : "F");
7505 }
7506 }
7507 }
7508
7509 static void dump_litpools (void) __attribute__ ((unused));
7510
7511 static void
7512 dump_litpools (void)
7513 {
7514 struct litpool_seg *lps = litpool_seg_list.next;
7515 struct litpool_frag *lpf;
7516
7517 for ( ; lps ; lps = lps->next )
7518 {
7519 printf("litpool seg %s\n", lps->seg->name);
7520 for ( lpf = lps->frag_list.next; lpf->fragP; lpf = lpf->next )
7521 {
7522 fragS *litfrag = lpf->fragP->fr_next;
7523 int count = 0;
7524 while (litfrag && litfrag->fr_subtype != RELAX_LITERAL_POOL_END)
7525 {
7526 if (litfrag->fr_fix == 4)
7527 count++;
7528 litfrag = litfrag->fr_next;
7529 }
7530 printf(" %ld <%d:%d> (%d) [%d]: ",
7531 lpf->addr, lpf->priority, lpf->original_priority,
7532 lpf->fragP->fr_line, count);
7533 //dump_frag(lpf->fragP);
7534 }
7535 }
7536 }
7537
7538 static void
7539 xtensa_maybe_create_literal_pool_frag (bfd_boolean create,
7540 bfd_boolean only_if_needed)
7541 {
7542 struct litpool_seg *lps = litpool_seg_list.next;
7543 fragS *fragP;
7544 struct litpool_frag *lpf;
7545 bfd_boolean needed = FALSE;
7546
7547 if (use_literal_section || !auto_litpools)
7548 return;
7549
7550 for ( ; lps ; lps = lps->next )
7551 {
7552 if (lps->seg == now_seg)
7553 break;
7554 }
7555
7556 if (lps == NULL)
7557 {
7558 lps = (struct litpool_seg *)xcalloc (sizeof (struct litpool_seg), 1);
7559 lps->next = litpool_seg_list.next;
7560 litpool_seg_list.next = lps;
7561 lps->seg = now_seg;
7562 lps->frag_list.next = &lps->frag_list;
7563 lps->frag_list.prev = &lps->frag_list;
7564 }
7565
7566 lps->frag_count++;
7567
7568 if (create)
7569 {
7570 if (only_if_needed)
7571 {
7572 if (past_xtensa_end || !use_transform() ||
7573 frag_now->tc_frag_data.is_no_transform)
7574 {
7575 return;
7576 }
7577 if (auto_litpool_limit <= 0)
7578 {
7579 /* Don't create a litpool based only on frag count. */
7580 return;
7581 }
7582 else if (lps->frag_count > auto_litpool_limit)
7583 {
7584 needed = TRUE;
7585 }
7586 else
7587 {
7588 return;
7589 }
7590 }
7591 else
7592 {
7593 needed = TRUE;
7594 }
7595 }
7596
7597 if (needed)
7598 {
7599 int size = (only_if_needed) ? 3 : 0; /* Space for a "j" insn. */
7600 /* Create a potential site for a literal pool. */
7601 frag_wane (frag_now);
7602 frag_new (0);
7603 xtensa_set_frag_assembly_state (frag_now);
7604 fragP = frag_now;
7605 fragP->tc_frag_data.lit_frchain = frchain_now;
7606 fragP->tc_frag_data.literal_frag = fragP;
7607 frag_var (rs_machine_dependent, size, size,
7608 (only_if_needed) ?
7609 RELAX_LITERAL_POOL_CANDIDATE_BEGIN :
7610 RELAX_LITERAL_POOL_BEGIN,
7611 NULL, 0, NULL);
7612 frag_now->tc_frag_data.lit_seg = now_seg;
7613 frag_variant (rs_machine_dependent, 0, 0,
7614 RELAX_LITERAL_POOL_END, NULL, 0, NULL);
7615 xtensa_set_frag_assembly_state (frag_now);
7616 }
7617 else
7618 {
7619 /* RELAX_LITERAL_POOL_BEGIN frag is being created;
7620 just record it here. */
7621 fragP = frag_now;
7622 }
7623
7624 lpf = (struct litpool_frag *)xmalloc(sizeof (struct litpool_frag));
7625 /* Insert at tail of circular list. */
7626 lpf->addr = 0;
7627 lps->frag_list.prev->next = lpf;
7628 lpf->next = &lps->frag_list;
7629 lpf->prev = lps->frag_list.prev;
7630 lps->frag_list.prev = lpf;
7631 lpf->fragP = fragP;
7632 lpf->priority = (needed) ? (only_if_needed) ? 3 : 2 : 1;
7633 lpf->original_priority = lpf->priority;
7634
7635 lps->frag_count = 0;
7636 }
7637
7638 static void
7639 xtensa_cleanup_align_frags (void)
7640 {
7641 frchainS *frchP;
7642 asection *s;
7643
7644 for (s = stdoutput->sections; s; s = s->next)
7645 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7646 {
7647 fragS *fragP;
7648 /* Walk over all of the fragments in a subsection. */
7649 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7650 {
7651 if ((fragP->fr_type == rs_align
7652 || fragP->fr_type == rs_align_code
7653 || (fragP->fr_type == rs_machine_dependent
7654 && (fragP->fr_subtype == RELAX_DESIRE_ALIGN
7655 || fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)))
7656 && fragP->fr_fix == 0)
7657 {
7658 fragS *next = fragP->fr_next;
7659
7660 while (next
7661 && next->fr_fix == 0
7662 && next->fr_type == rs_machine_dependent
7663 && next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
7664 {
7665 frag_wane (next);
7666 next = next->fr_next;
7667 }
7668 }
7669 /* If we don't widen branch targets, then they
7670 will be easier to align. */
7671 if (fragP->tc_frag_data.is_branch_target
7672 && fragP->fr_opcode == fragP->fr_literal
7673 && fragP->fr_type == rs_machine_dependent
7674 && fragP->fr_subtype == RELAX_SLOTS
7675 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
7676 frag_wane (fragP);
7677 if (fragP->fr_type == rs_machine_dependent
7678 && fragP->fr_subtype == RELAX_UNREACHABLE)
7679 fragP->tc_frag_data.is_unreachable = TRUE;
7680 }
7681 }
7682 }
7683
7684
7685 /* Re-process all of the fragments looking to convert all of the
7686 RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch
7687 target in the next fragment, convert this to RELAX_DESIRE_ALIGN.
7688 Otherwise, convert to a .fill 0. */
7689
7690 static void
7691 xtensa_fix_target_frags (void)
7692 {
7693 frchainS *frchP;
7694 asection *s;
7695
7696 /* When this routine is called, all of the subsections are still intact
7697 so we walk over subsections instead of sections. */
7698 for (s = stdoutput->sections; s; s = s->next)
7699 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7700 {
7701 fragS *fragP;
7702
7703 /* Walk over all of the fragments in a subsection. */
7704 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7705 {
7706 if (fragP->fr_type == rs_machine_dependent
7707 && fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
7708 {
7709 if (next_frag_is_branch_target (fragP))
7710 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
7711 else
7712 frag_wane (fragP);
7713 }
7714 }
7715 }
7716 }
7717
7718
7719 static bfd_boolean is_narrow_branch_guaranteed_in_range (fragS *, TInsn *);
7720
7721 static void
7722 xtensa_mark_narrow_branches (void)
7723 {
7724 frchainS *frchP;
7725 asection *s;
7726
7727 for (s = stdoutput->sections; s; s = s->next)
7728 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7729 {
7730 fragS *fragP;
7731 /* Walk over all of the fragments in a subsection. */
7732 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7733 {
7734 if (fragP->fr_type == rs_machine_dependent
7735 && fragP->fr_subtype == RELAX_SLOTS
7736 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED)
7737 {
7738 vliw_insn vinsn;
7739
7740 vinsn_from_chars (&vinsn, fragP->fr_opcode);
7741 tinsn_immed_from_frag (&vinsn.slots[0], fragP, 0);
7742
7743 if (vinsn.num_slots == 1
7744 && xtensa_opcode_is_branch (xtensa_default_isa,
7745 vinsn.slots[0].opcode) == 1
7746 && xg_get_single_size (vinsn.slots[0].opcode) == 2
7747 && is_narrow_branch_guaranteed_in_range (fragP,
7748 &vinsn.slots[0]))
7749 {
7750 fragP->fr_subtype = RELAX_SLOTS;
7751 fragP->tc_frag_data.slot_subtypes[0] = RELAX_NARROW;
7752 fragP->tc_frag_data.is_aligning_branch = 1;
7753 }
7754 }
7755 }
7756 }
7757 }
7758
7759
7760 /* A branch is typically widened only when its target is out of
7761 range. However, we would like to widen them to align a subsequent
7762 branch target when possible.
7763
7764 Because the branch relaxation code is so convoluted, the optimal solution
7765 (combining the two cases) is difficult to get right in all circumstances.
7766 We therefore go with an "almost as good" solution, where we only
7767 use for alignment narrow branches that definitely will not expand to a
7768 jump and a branch. These functions find and mark these cases. */
7769
7770 /* The range in bytes of BNEZ.N and BEQZ.N. The target operand is encoded
7771 as PC + 4 + imm6, where imm6 is a 6-bit immediate ranging from 0 to 63.
7772 We start counting beginning with the frag after the 2-byte branch, so the
7773 maximum offset is (4 - 2) + 63 = 65. */
7774 #define MAX_IMMED6 65
7775
7776 static offsetT unrelaxed_frag_max_size (fragS *);
7777
7778 static bfd_boolean
7779 is_narrow_branch_guaranteed_in_range (fragS *fragP, TInsn *tinsn)
7780 {
7781 const expressionS *exp = &tinsn->tok[1];
7782 symbolS *symbolP = exp->X_add_symbol;
7783 offsetT max_distance = exp->X_add_number;
7784 fragS *target_frag;
7785
7786 if (exp->X_op != O_symbol)
7787 return FALSE;
7788
7789 target_frag = symbol_get_frag (symbolP);
7790
7791 max_distance += (S_GET_VALUE (symbolP) - target_frag->fr_address);
7792 if (is_branch_jmp_to_next (tinsn, fragP))
7793 return FALSE;
7794
7795 /* The branch doesn't branch over it's own frag,
7796 but over the subsequent ones. */
7797 fragP = fragP->fr_next;
7798 while (fragP != NULL && fragP != target_frag && max_distance <= MAX_IMMED6)
7799 {
7800 max_distance += unrelaxed_frag_max_size (fragP);
7801 fragP = fragP->fr_next;
7802 }
7803 if (max_distance <= MAX_IMMED6 && fragP == target_frag)
7804 return TRUE;
7805 return FALSE;
7806 }
7807
7808
7809 static void
7810 xtensa_mark_zcl_first_insns (void)
7811 {
7812 frchainS *frchP;
7813 asection *s;
7814
7815 for (s = stdoutput->sections; s; s = s->next)
7816 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7817 {
7818 fragS *fragP;
7819 /* Walk over all of the fragments in a subsection. */
7820 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7821 {
7822 if (fragP->fr_type == rs_machine_dependent
7823 && (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE
7824 || fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE))
7825 {
7826 /* Find the loop frag. */
7827 fragS *loop_frag = next_non_empty_frag (fragP);
7828 /* Find the first insn frag. */
7829 fragS *targ_frag = next_non_empty_frag (loop_frag);
7830
7831 /* Handle a corner case that comes up in hardware
7832 diagnostics. The original assembly looks like this:
7833
7834 loop aX, LabelA
7835 <empty_frag>--not found by next_non_empty_frag
7836 loop aY, LabelB
7837
7838 Depending on the start address, the assembler may or
7839 may not change it to look something like this:
7840
7841 loop aX, LabelA
7842 nop--frag isn't empty anymore
7843 loop aY, LabelB
7844
7845 So set up to check the alignment of the nop if it
7846 exists */
7847 while (loop_frag != targ_frag)
7848 {
7849 if (loop_frag->fr_type == rs_machine_dependent
7850 && (loop_frag->fr_subtype == RELAX_ALIGN_NEXT_OPCODE
7851 || loop_frag->fr_subtype
7852 == RELAX_CHECK_ALIGN_NEXT_OPCODE))
7853 targ_frag = loop_frag;
7854 else
7855 loop_frag = loop_frag->fr_next;
7856 }
7857
7858 /* Of course, sometimes (mostly for toy test cases) a
7859 zero-cost loop instruction is the last in a section. */
7860 if (targ_frag)
7861 {
7862 targ_frag->tc_frag_data.is_first_loop_insn = TRUE;
7863 /* Do not widen a frag that is the first instruction of a
7864 zero-cost loop. It makes that loop harder to align. */
7865 if (targ_frag->fr_type == rs_machine_dependent
7866 && targ_frag->fr_subtype == RELAX_SLOTS
7867 && (targ_frag->tc_frag_data.slot_subtypes[0]
7868 == RELAX_NARROW))
7869 {
7870 if (targ_frag->tc_frag_data.is_aligning_branch)
7871 targ_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED;
7872 else
7873 {
7874 frag_wane (targ_frag);
7875 targ_frag->tc_frag_data.slot_subtypes[0] = 0;
7876 }
7877 }
7878 }
7879 if (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)
7880 frag_wane (fragP);
7881 }
7882 }
7883 }
7884 }
7885
7886
7887 /* When a difference-of-symbols expression is encoded as a uleb128 or
7888 sleb128 value, the linker is unable to adjust that value to account for
7889 link-time relaxation. Mark all the code between such symbols so that
7890 its size cannot be changed by linker relaxation. */
7891
7892 static void
7893 xtensa_mark_difference_of_two_symbols (void)
7894 {
7895 symbolS *expr_sym;
7896
7897 for (expr_sym = expr_symbols; expr_sym;
7898 expr_sym = symbol_get_tc (expr_sym)->next_expr_symbol)
7899 {
7900 expressionS *exp = symbol_get_value_expression (expr_sym);
7901
7902 if (exp->X_op == O_subtract)
7903 {
7904 symbolS *left = exp->X_add_symbol;
7905 symbolS *right = exp->X_op_symbol;
7906
7907 /* Difference of two symbols not in the same section
7908 are handled with relocations in the linker. */
7909 if (S_GET_SEGMENT (left) == S_GET_SEGMENT (right))
7910 {
7911 fragS *start;
7912 fragS *end;
7913 fragS *walk;
7914
7915 if (symbol_get_frag (left)->fr_address
7916 <= symbol_get_frag (right)->fr_address)
7917 {
7918 start = symbol_get_frag (left);
7919 end = symbol_get_frag (right);
7920 }
7921 else
7922 {
7923 start = symbol_get_frag (right);
7924 end = symbol_get_frag (left);
7925 }
7926
7927 if (start->tc_frag_data.no_transform_end != NULL)
7928 walk = start->tc_frag_data.no_transform_end;
7929 else
7930 walk = start;
7931 do
7932 {
7933 walk->tc_frag_data.is_no_transform = 1;
7934 walk = walk->fr_next;
7935 }
7936 while (walk && walk->fr_address < end->fr_address);
7937
7938 start->tc_frag_data.no_transform_end = walk;
7939 }
7940 }
7941 }
7942 }
7943
7944
7945 /* Re-process all of the fragments looking to convert all of the
7946 RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a
7947 conditional branch or a retw/retw.n, convert this frag to one that
7948 will generate a NOP. In any case close it off with a .fill 0. */
7949
7950 static bfd_boolean next_instrs_are_b_retw (fragS *);
7951
7952 static void
7953 xtensa_fix_a0_b_retw_frags (void)
7954 {
7955 frchainS *frchP;
7956 asection *s;
7957
7958 /* When this routine is called, all of the subsections are still intact
7959 so we walk over subsections instead of sections. */
7960 for (s = stdoutput->sections; s; s = s->next)
7961 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
7962 {
7963 fragS *fragP;
7964
7965 /* Walk over all of the fragments in a subsection. */
7966 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7967 {
7968 if (fragP->fr_type == rs_machine_dependent
7969 && fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW)
7970 {
7971 if (next_instrs_are_b_retw (fragP))
7972 {
7973 if (fragP->tc_frag_data.is_no_transform)
7974 as_bad (_("instruction sequence (write a0, branch, retw) may trigger hardware errata"));
7975 else
7976 relax_frag_add_nop (fragP);
7977 }
7978 frag_wane (fragP);
7979 }
7980 }
7981 }
7982 }
7983
7984
7985 static bfd_boolean
7986 next_instrs_are_b_retw (fragS *fragP)
7987 {
7988 xtensa_opcode opcode;
7989 xtensa_format fmt;
7990 const fragS *next_fragP = next_non_empty_frag (fragP);
7991 static xtensa_insnbuf insnbuf = NULL;
7992 static xtensa_insnbuf slotbuf = NULL;
7993 xtensa_isa isa = xtensa_default_isa;
7994 int offset = 0;
7995 int slot;
7996 bfd_boolean branch_seen = FALSE;
7997
7998 if (!insnbuf)
7999 {
8000 insnbuf = xtensa_insnbuf_alloc (isa);
8001 slotbuf = xtensa_insnbuf_alloc (isa);
8002 }
8003
8004 if (next_fragP == NULL)
8005 return FALSE;
8006
8007 /* Check for the conditional branch. */
8008 xtensa_insnbuf_from_chars
8009 (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0);
8010 fmt = xtensa_format_decode (isa, insnbuf);
8011 if (fmt == XTENSA_UNDEFINED)
8012 return FALSE;
8013
8014 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
8015 {
8016 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
8017 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
8018
8019 branch_seen = (branch_seen
8020 || xtensa_opcode_is_branch (isa, opcode) == 1);
8021 }
8022
8023 if (!branch_seen)
8024 return FALSE;
8025
8026 offset += xtensa_format_length (isa, fmt);
8027 if (offset == next_fragP->fr_fix)
8028 {
8029 next_fragP = next_non_empty_frag (next_fragP);
8030 offset = 0;
8031 }
8032
8033 if (next_fragP == NULL)
8034 return FALSE;
8035
8036 /* Check for the retw/retw.n. */
8037 xtensa_insnbuf_from_chars
8038 (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0);
8039 fmt = xtensa_format_decode (isa, insnbuf);
8040
8041 /* Because RETW[.N] is not bundleable, a VLIW bundle here means that we
8042 have no problems. */
8043 if (fmt == XTENSA_UNDEFINED
8044 || xtensa_format_num_slots (isa, fmt) != 1)
8045 return FALSE;
8046
8047 xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
8048 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
8049
8050 if (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode)
8051 return TRUE;
8052
8053 return FALSE;
8054 }
8055
8056
8057 /* Re-process all of the fragments looking to convert all of the
8058 RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a
8059 loop end label, convert this frag to one that will generate a NOP.
8060 In any case close it off with a .fill 0. */
8061
8062 static bfd_boolean next_instr_is_loop_end (fragS *);
8063
8064 static void
8065 xtensa_fix_b_j_loop_end_frags (void)
8066 {
8067 frchainS *frchP;
8068 asection *s;
8069
8070 /* When this routine is called, all of the subsections are still intact
8071 so we walk over subsections instead of sections. */
8072 for (s = stdoutput->sections; s; s = s->next)
8073 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
8074 {
8075 fragS *fragP;
8076
8077 /* Walk over all of the fragments in a subsection. */
8078 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
8079 {
8080 if (fragP->fr_type == rs_machine_dependent
8081 && fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END)
8082 {
8083 if (next_instr_is_loop_end (fragP))
8084 {
8085 if (fragP->tc_frag_data.is_no_transform)
8086 as_bad (_("branching or jumping to a loop end may trigger hardware errata"));
8087 else
8088 relax_frag_add_nop (fragP);
8089 }
8090 frag_wane (fragP);
8091 }
8092 }
8093 }
8094 }
8095
8096
8097 static bfd_boolean
8098 next_instr_is_loop_end (fragS *fragP)
8099 {
8100 const fragS *next_fragP;
8101
8102 if (next_frag_is_loop_target (fragP))
8103 return FALSE;
8104
8105 next_fragP = next_non_empty_frag (fragP);
8106 if (next_fragP == NULL)
8107 return FALSE;
8108
8109 if (!next_frag_is_loop_target (next_fragP))
8110 return FALSE;
8111
8112 /* If the size is >= 3 then there is more than one instruction here.
8113 The hardware bug will not fire. */
8114 if (next_fragP->fr_fix > 3)
8115 return FALSE;
8116
8117 return TRUE;
8118 }
8119
8120
8121 /* Re-process all of the fragments looking to convert all of the
8122 RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is
8123 not MY loop's loop end within 12 bytes, add enough nops here to
8124 make it at least 12 bytes away. In any case close it off with a
8125 .fill 0. */
8126
8127 static offsetT min_bytes_to_other_loop_end
8128 (fragS *, fragS *, offsetT);
8129
8130 static void
8131 xtensa_fix_close_loop_end_frags (void)
8132 {
8133 frchainS *frchP;
8134 asection *s;
8135
8136 /* When this routine is called, all of the subsections are still intact
8137 so we walk over subsections instead of sections. */
8138 for (s = stdoutput->sections; s; s = s->next)
8139 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
8140 {
8141 fragS *fragP;
8142
8143 fragS *current_target = NULL;
8144
8145 /* Walk over all of the fragments in a subsection. */
8146 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
8147 {
8148 if (fragP->fr_type == rs_machine_dependent
8149 && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
8150 || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)))
8151 current_target = symbol_get_frag (fragP->fr_symbol);
8152
8153 if (current_target
8154 && fragP->fr_type == rs_machine_dependent
8155 && fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END)
8156 {
8157 offsetT min_bytes;
8158 int bytes_added = 0;
8159
8160 #define REQUIRED_LOOP_DIVIDING_BYTES 12
8161 /* Max out at 12. */
8162 min_bytes = min_bytes_to_other_loop_end
8163 (fragP->fr_next, current_target, REQUIRED_LOOP_DIVIDING_BYTES);
8164
8165 if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES)
8166 {
8167 if (fragP->tc_frag_data.is_no_transform)
8168 as_bad (_("loop end too close to another loop end may trigger hardware errata"));
8169 else
8170 {
8171 while (min_bytes + bytes_added
8172 < REQUIRED_LOOP_DIVIDING_BYTES)
8173 {
8174 int length = 3;
8175
8176 if (fragP->fr_var < length)
8177 as_fatal (_("fr_var %lu < length %d"),
8178 (long) fragP->fr_var, length);
8179 else
8180 {
8181 assemble_nop (length,
8182 fragP->fr_literal + fragP->fr_fix);
8183 fragP->fr_fix += length;
8184 fragP->fr_var -= length;
8185 }
8186 bytes_added += length;
8187 }
8188 }
8189 }
8190 frag_wane (fragP);
8191 }
8192 gas_assert (fragP->fr_type != rs_machine_dependent
8193 || fragP->fr_subtype != RELAX_ADD_NOP_IF_CLOSE_LOOP_END);
8194 }
8195 }
8196 }
8197
8198
8199 static offsetT unrelaxed_frag_min_size (fragS *);
8200
8201 static offsetT
8202 min_bytes_to_other_loop_end (fragS *fragP,
8203 fragS *current_target,
8204 offsetT max_size)
8205 {
8206 offsetT offset = 0;
8207 fragS *current_fragP;
8208
8209 for (current_fragP = fragP;
8210 current_fragP;
8211 current_fragP = current_fragP->fr_next)
8212 {
8213 if (current_fragP->tc_frag_data.is_loop_target
8214 && current_fragP != current_target)
8215 return offset;
8216
8217 offset += unrelaxed_frag_min_size (current_fragP);
8218
8219 if (offset >= max_size)
8220 return max_size;
8221 }
8222 return max_size;
8223 }
8224
8225
8226 static offsetT
8227 unrelaxed_frag_min_size (fragS *fragP)
8228 {
8229 offsetT size = fragP->fr_fix;
8230
8231 /* Add fill size. */
8232 if (fragP->fr_type == rs_fill)
8233 size += fragP->fr_offset;
8234
8235 return size;
8236 }
8237
8238
8239 static offsetT
8240 unrelaxed_frag_max_size (fragS *fragP)
8241 {
8242 offsetT size = fragP->fr_fix;
8243 switch (fragP->fr_type)
8244 {
8245 case 0:
8246 /* Empty frags created by the obstack allocation scheme
8247 end up with type 0. */
8248 break;
8249 case rs_fill:
8250 case rs_org:
8251 case rs_space:
8252 size += fragP->fr_offset;
8253 break;
8254 case rs_align:
8255 case rs_align_code:
8256 case rs_align_test:
8257 case rs_leb128:
8258 case rs_cfa:
8259 case rs_dwarf2dbg:
8260 /* No further adjustments needed. */
8261 break;
8262 case rs_machine_dependent:
8263 if (fragP->fr_subtype != RELAX_DESIRE_ALIGN)
8264 size += fragP->fr_var;
8265 break;
8266 default:
8267 /* We had darn well better know how big it is. */
8268 gas_assert (0);
8269 break;
8270 }
8271
8272 return size;
8273 }
8274
8275
8276 /* Re-process all of the fragments looking to convert all
8277 of the RELAX_ADD_NOP_IF_SHORT_LOOP. If:
8278
8279 A)
8280 1) the instruction size count to the loop end label
8281 is too short (<= 2 instructions),
8282 2) loop has a jump or branch in it
8283
8284 or B)
8285 1) workaround_all_short_loops is TRUE
8286 2) The generating loop was a 'loopgtz' or 'loopnez'
8287 3) the instruction size count to the loop end label is too short
8288 (<= 2 instructions)
8289 then convert this frag (and maybe the next one) to generate a NOP.
8290 In any case close it off with a .fill 0. */
8291
8292 static int count_insns_to_loop_end (fragS *, bfd_boolean, int);
8293 static bfd_boolean branch_before_loop_end (fragS *);
8294
8295 static void
8296 xtensa_fix_short_loop_frags (void)
8297 {
8298 frchainS *frchP;
8299 asection *s;
8300
8301 /* When this routine is called, all of the subsections are still intact
8302 so we walk over subsections instead of sections. */
8303 for (s = stdoutput->sections; s; s = s->next)
8304 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
8305 {
8306 fragS *fragP;
8307 xtensa_opcode current_opcode = XTENSA_UNDEFINED;
8308
8309 /* Walk over all of the fragments in a subsection. */
8310 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
8311 {
8312 if (fragP->fr_type == rs_machine_dependent
8313 && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
8314 || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)))
8315 {
8316 TInsn t_insn;
8317 fragS *loop_frag = next_non_empty_frag (fragP);
8318 tinsn_from_chars (&t_insn, loop_frag->fr_opcode, 0);
8319 current_opcode = t_insn.opcode;
8320 gas_assert (xtensa_opcode_is_loop (xtensa_default_isa,
8321 current_opcode) == 1);
8322 }
8323
8324 if (fragP->fr_type == rs_machine_dependent
8325 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
8326 {
8327 if (count_insns_to_loop_end (fragP->fr_next, TRUE, 3) < 3
8328 && (branch_before_loop_end (fragP->fr_next)
8329 || (workaround_all_short_loops
8330 && current_opcode != XTENSA_UNDEFINED
8331 && current_opcode != xtensa_loop_opcode)))
8332 {
8333 if (fragP->tc_frag_data.is_no_transform)
8334 as_bad (_("loop containing less than three instructions may trigger hardware errata"));
8335 else
8336 relax_frag_add_nop (fragP);
8337 }
8338 frag_wane (fragP);
8339 }
8340 }
8341 }
8342 }
8343
8344
8345 static int unrelaxed_frag_min_insn_count (fragS *);
8346
8347 static int
8348 count_insns_to_loop_end (fragS *base_fragP,
8349 bfd_boolean count_relax_add,
8350 int max_count)
8351 {
8352 fragS *fragP = NULL;
8353 int insn_count = 0;
8354
8355 fragP = base_fragP;
8356
8357 for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next)
8358 {
8359 insn_count += unrelaxed_frag_min_insn_count (fragP);
8360 if (insn_count >= max_count)
8361 return max_count;
8362
8363 if (count_relax_add)
8364 {
8365 if (fragP->fr_type == rs_machine_dependent
8366 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
8367 {
8368 /* In order to add the appropriate number of
8369 NOPs, we count an instruction for downstream
8370 occurrences. */
8371 insn_count++;
8372 if (insn_count >= max_count)
8373 return max_count;
8374 }
8375 }
8376 }
8377 return insn_count;
8378 }
8379
8380
8381 static int
8382 unrelaxed_frag_min_insn_count (fragS *fragP)
8383 {
8384 xtensa_isa isa = xtensa_default_isa;
8385 static xtensa_insnbuf insnbuf = NULL;
8386 int insn_count = 0;
8387 int offset = 0;
8388
8389 if (!fragP->tc_frag_data.is_insn)
8390 return insn_count;
8391
8392 if (!insnbuf)
8393 insnbuf = xtensa_insnbuf_alloc (isa);
8394
8395 /* Decode the fixed instructions. */
8396 while (offset < fragP->fr_fix)
8397 {
8398 xtensa_format fmt;
8399
8400 xtensa_insnbuf_from_chars
8401 (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0);
8402 fmt = xtensa_format_decode (isa, insnbuf);
8403
8404 if (fmt == XTENSA_UNDEFINED)
8405 {
8406 as_fatal (_("undecodable instruction in instruction frag"));
8407 return insn_count;
8408 }
8409 offset += xtensa_format_length (isa, fmt);
8410 insn_count++;
8411 }
8412
8413 return insn_count;
8414 }
8415
8416
8417 static bfd_boolean unrelaxed_frag_has_b_j (fragS *);
8418
8419 static bfd_boolean
8420 branch_before_loop_end (fragS *base_fragP)
8421 {
8422 fragS *fragP;
8423
8424 for (fragP = base_fragP;
8425 fragP && !fragP->tc_frag_data.is_loop_target;
8426 fragP = fragP->fr_next)
8427 {
8428 if (unrelaxed_frag_has_b_j (fragP))
8429 return TRUE;
8430 }
8431 return FALSE;
8432 }
8433
8434
8435 static bfd_boolean
8436 unrelaxed_frag_has_b_j (fragS *fragP)
8437 {
8438 static xtensa_insnbuf insnbuf = NULL;
8439 xtensa_isa isa = xtensa_default_isa;
8440 int offset = 0;
8441
8442 if (!fragP->tc_frag_data.is_insn)
8443 return FALSE;
8444
8445 if (!insnbuf)
8446 insnbuf = xtensa_insnbuf_alloc (isa);
8447
8448 /* Decode the fixed instructions. */
8449 while (offset < fragP->fr_fix)
8450 {
8451 xtensa_format fmt;
8452 int slot;
8453
8454 xtensa_insnbuf_from_chars
8455 (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0);
8456 fmt = xtensa_format_decode (isa, insnbuf);
8457 if (fmt == XTENSA_UNDEFINED)
8458 return FALSE;
8459
8460 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
8461 {
8462 xtensa_opcode opcode =
8463 get_opcode_from_buf (fragP->fr_literal + offset, slot);
8464 if (xtensa_opcode_is_branch (isa, opcode) == 1
8465 || xtensa_opcode_is_jump (isa, opcode) == 1)
8466 return TRUE;
8467 }
8468 offset += xtensa_format_length (isa, fmt);
8469 }
8470 return FALSE;
8471 }
8472
8473
8474 /* Checks to be made after initial assembly but before relaxation. */
8475
8476 static bfd_boolean is_empty_loop (const TInsn *, fragS *);
8477 static bfd_boolean is_local_forward_loop (const TInsn *, fragS *);
8478
8479 static void
8480 xtensa_sanity_check (void)
8481 {
8482 const char *file_name;
8483 unsigned line;
8484 frchainS *frchP;
8485 asection *s;
8486
8487 file_name = as_where (&line);
8488 for (s = stdoutput->sections; s; s = s->next)
8489 for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
8490 {
8491 fragS *fragP;
8492
8493 /* Walk over all of the fragments in a subsection. */
8494 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
8495 {
8496 if (fragP->fr_type == rs_machine_dependent
8497 && fragP->fr_subtype == RELAX_SLOTS
8498 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED)
8499 {
8500 static xtensa_insnbuf insnbuf = NULL;
8501 TInsn t_insn;
8502
8503 if (fragP->fr_opcode != NULL)
8504 {
8505 if (!insnbuf)
8506 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
8507 tinsn_from_chars (&t_insn, fragP->fr_opcode, 0);
8508 tinsn_immed_from_frag (&t_insn, fragP, 0);
8509
8510 if (xtensa_opcode_is_loop (xtensa_default_isa,
8511 t_insn.opcode) == 1)
8512 {
8513 if (is_empty_loop (&t_insn, fragP))
8514 {
8515 new_logical_line (fragP->fr_file, fragP->fr_line);
8516 as_bad (_("invalid empty loop"));
8517 }
8518 if (!is_local_forward_loop (&t_insn, fragP))
8519 {
8520 new_logical_line (fragP->fr_file, fragP->fr_line);
8521 as_bad (_("loop target does not follow "
8522 "loop instruction in section"));
8523 }
8524 }
8525 }
8526 }
8527 }
8528 }
8529 new_logical_line (file_name, line);
8530 }
8531
8532
8533 #define LOOP_IMMED_OPN 1
8534
8535 /* Return TRUE if the loop target is the next non-zero fragment. */
8536
8537 static bfd_boolean
8538 is_empty_loop (const TInsn *insn, fragS *fragP)
8539 {
8540 const expressionS *exp;
8541 symbolS *symbolP;
8542 fragS *next_fragP;
8543
8544 if (insn->insn_type != ITYPE_INSN)
8545 return FALSE;
8546
8547 if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1)
8548 return FALSE;
8549
8550 if (insn->ntok <= LOOP_IMMED_OPN)
8551 return FALSE;
8552
8553 exp = &insn->tok[LOOP_IMMED_OPN];
8554
8555 if (exp->X_op != O_symbol)
8556 return FALSE;
8557
8558 symbolP = exp->X_add_symbol;
8559 if (!symbolP)
8560 return FALSE;
8561
8562 if (symbol_get_frag (symbolP) == NULL)
8563 return FALSE;
8564
8565 if (S_GET_VALUE (symbolP) != 0)
8566 return FALSE;
8567
8568 /* Walk through the zero-size fragments from this one. If we find
8569 the target fragment, then this is a zero-size loop. */
8570
8571 for (next_fragP = fragP->fr_next;
8572 next_fragP != NULL;
8573 next_fragP = next_fragP->fr_next)
8574 {
8575 if (next_fragP == symbol_get_frag (symbolP))
8576 return TRUE;
8577 if (next_fragP->fr_fix != 0)
8578 return FALSE;
8579 }
8580 return FALSE;
8581 }
8582
8583
8584 static bfd_boolean
8585 is_local_forward_loop (const TInsn *insn, fragS *fragP)
8586 {
8587 const expressionS *exp;
8588 symbolS *symbolP;
8589 fragS *next_fragP;
8590
8591 if (insn->insn_type != ITYPE_INSN)
8592 return FALSE;
8593
8594 if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1)
8595 return FALSE;
8596
8597 if (insn->ntok <= LOOP_IMMED_OPN)
8598 return FALSE;
8599
8600 exp = &insn->tok[LOOP_IMMED_OPN];
8601
8602 if (exp->X_op != O_symbol)
8603 return FALSE;
8604
8605 symbolP = exp->X_add_symbol;
8606 if (!symbolP)
8607 return FALSE;
8608
8609 if (symbol_get_frag (symbolP) == NULL)
8610 return FALSE;
8611
8612 /* Walk through fragments until we find the target.
8613 If we do not find the target, then this is an invalid loop. */
8614
8615 for (next_fragP = fragP->fr_next;
8616 next_fragP != NULL;
8617 next_fragP = next_fragP->fr_next)
8618 {
8619 if (next_fragP == symbol_get_frag (symbolP))
8620 return TRUE;
8621 }
8622
8623 return FALSE;
8624 }
8625
8626
8627 #define XTINFO_NAME "Xtensa_Info"
8628 #define XTINFO_NAMESZ 12
8629 #define XTINFO_TYPE 1
8630
8631 static void
8632 xtensa_add_config_info (void)
8633 {
8634 asection *info_sec;
8635 char *data, *p;
8636 int sz;
8637
8638 info_sec = subseg_new (".xtensa.info", 0);
8639 bfd_set_section_flags (stdoutput, info_sec, SEC_HAS_CONTENTS | SEC_READONLY);
8640
8641 data = xmalloc (100);
8642 sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n",
8643 XSHAL_USE_ABSOLUTE_LITERALS, XSHAL_ABI);
8644 sz = strlen (data) + 1;
8645
8646 /* Add enough null terminators to pad to a word boundary. */
8647 do
8648 data[sz++] = 0;
8649 while ((sz & 3) != 0);
8650
8651 /* Follow the standard note section layout:
8652 First write the length of the name string. */
8653 p = frag_more (4);
8654 md_number_to_chars (p, (valueT) XTINFO_NAMESZ, 4);
8655
8656 /* Next comes the length of the "descriptor", i.e., the actual data. */
8657 p = frag_more (4);
8658 md_number_to_chars (p, (valueT) sz, 4);
8659
8660 /* Write the note type. */
8661 p = frag_more (4);
8662 md_number_to_chars (p, (valueT) XTINFO_TYPE, 4);
8663
8664 /* Write the name field. */
8665 p = frag_more (XTINFO_NAMESZ);
8666 memcpy (p, XTINFO_NAME, XTINFO_NAMESZ);
8667
8668 /* Finally, write the descriptor. */
8669 p = frag_more (sz);
8670 memcpy (p, data, sz);
8671
8672 free (data);
8673 }
8674
8675 \f
8676 /* Alignment Functions. */
8677
8678 static int
8679 get_text_align_power (unsigned target_size)
8680 {
8681 if (target_size <= 4)
8682 return 2;
8683
8684 if (target_size <= 8)
8685 return 3;
8686
8687 if (target_size <= 16)
8688 return 4;
8689
8690 if (target_size <= 32)
8691 return 5;
8692
8693 if (target_size <= 64)
8694 return 6;
8695
8696 if (target_size <= 128)
8697 return 7;
8698
8699 if (target_size <= 256)
8700 return 8;
8701
8702 if (target_size <= 512)
8703 return 9;
8704
8705 if (target_size <= 1024)
8706 return 10;
8707
8708 gas_assert (0);
8709 return 0;
8710 }
8711
8712
8713 static int
8714 get_text_align_max_fill_size (int align_pow,
8715 bfd_boolean use_nops,
8716 bfd_boolean use_no_density)
8717 {
8718 if (!use_nops)
8719 return (1 << align_pow);
8720 if (use_no_density)
8721 return 3 * (1 << align_pow);
8722
8723 return 1 + (1 << align_pow);
8724 }
8725
8726
8727 /* Calculate the minimum bytes of fill needed at "address" to align a
8728 target instruction of size "target_size" so that it does not cross a
8729 power-of-two boundary specified by "align_pow". If "use_nops" is FALSE,
8730 the fill can be an arbitrary number of bytes. Otherwise, the space must
8731 be filled by NOP instructions. */
8732
8733 static int
8734 get_text_align_fill_size (addressT address,
8735 int align_pow,
8736 int target_size,
8737 bfd_boolean use_nops,
8738 bfd_boolean use_no_density)
8739 {
8740 addressT alignment, fill, fill_limit, fill_step;
8741 bfd_boolean skip_one = FALSE;
8742
8743 alignment = (1 << align_pow);
8744 gas_assert (target_size > 0 && alignment >= (addressT) target_size);
8745
8746 if (!use_nops)
8747 {
8748 fill_limit = alignment;
8749 fill_step = 1;
8750 }
8751 else if (!use_no_density)
8752 {
8753 /* Combine 2- and 3-byte NOPs to fill anything larger than one. */
8754 fill_limit = alignment * 2;
8755 fill_step = 1;
8756 skip_one = TRUE;
8757 }
8758 else
8759 {
8760 /* Fill with 3-byte NOPs -- can only fill multiples of 3. */
8761 fill_limit = alignment * 3;
8762 fill_step = 3;
8763 }
8764
8765 /* Try all fill sizes until finding one that works. */
8766 for (fill = 0; fill < fill_limit; fill += fill_step)
8767 {
8768 if (skip_one && fill == 1)
8769 continue;
8770 if ((address + fill) >> align_pow
8771 == (address + fill + target_size - 1) >> align_pow)
8772 return fill;
8773 }
8774 gas_assert (0);
8775 return 0;
8776 }
8777
8778
8779 static int
8780 branch_align_power (segT sec)
8781 {
8782 /* If the Xtensa processor has a fetch width of X, and
8783 the section is aligned to at least that boundary, then a branch
8784 target need only fit within that aligned block of memory to avoid
8785 a stall. Otherwise, try to fit branch targets within 4-byte
8786 aligned blocks (which may be insufficient, e.g., if the section
8787 has no alignment, but it's good enough). */
8788 int fetch_align = get_text_align_power(xtensa_fetch_width);
8789 int sec_align = get_recorded_alignment (sec);
8790
8791 if (sec_align >= fetch_align)
8792 return fetch_align;
8793
8794 return 2;
8795 }
8796
8797
8798 /* This will assert if it is not possible. */
8799
8800 static int
8801 get_text_align_nop_count (offsetT fill_size, bfd_boolean use_no_density)
8802 {
8803 int count = 0;
8804
8805 if (use_no_density)
8806 {
8807 gas_assert (fill_size % 3 == 0);
8808 return (fill_size / 3);
8809 }
8810
8811 gas_assert (fill_size != 1); /* Bad argument. */
8812
8813 while (fill_size > 1)
8814 {
8815 int insn_size = 3;
8816 if (fill_size == 2 || fill_size == 4)
8817 insn_size = 2;
8818 fill_size -= insn_size;
8819 count++;
8820 }
8821 gas_assert (fill_size != 1); /* Bad algorithm. */
8822 return count;
8823 }
8824
8825
8826 static int
8827 get_text_align_nth_nop_size (offsetT fill_size,
8828 int n,
8829 bfd_boolean use_no_density)
8830 {
8831 int count = 0;
8832
8833 if (use_no_density)
8834 return 3;
8835
8836 gas_assert (fill_size != 1); /* Bad argument. */
8837
8838 while (fill_size > 1)
8839 {
8840 int insn_size = 3;
8841 if (fill_size == 2 || fill_size == 4)
8842 insn_size = 2;
8843 fill_size -= insn_size;
8844 count++;
8845 if (n + 1 == count)
8846 return insn_size;
8847 }
8848 gas_assert (0);
8849 return 0;
8850 }
8851
8852
8853 /* For the given fragment, find the appropriate address
8854 for it to begin at if we are using NOPs to align it. */
8855
8856 static addressT
8857 get_noop_aligned_address (fragS *fragP, addressT address)
8858 {
8859 /* The rule is: get next fragment's FIRST instruction. Find
8860 the smallest number of bytes that need to be added to
8861 ensure that the next fragment's FIRST instruction will fit
8862 in a single word.
8863
8864 E.G., 2 bytes : 0, 1, 2 mod 4
8865 3 bytes: 0, 1 mod 4
8866
8867 If the FIRST instruction MIGHT be relaxed,
8868 assume that it will become a 3-byte instruction.
8869
8870 Note again here that LOOP instructions are not bundleable,
8871 and this relaxation only applies to LOOP opcodes. */
8872
8873 int fill_size = 0;
8874 int first_insn_size;
8875 int loop_insn_size;
8876 addressT pre_opcode_bytes;
8877 int align_power;
8878 fragS *first_insn;
8879 xtensa_opcode opcode;
8880 bfd_boolean is_loop;
8881
8882 gas_assert (fragP->fr_type == rs_machine_dependent);
8883 gas_assert (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE);
8884
8885 /* Find the loop frag. */
8886 first_insn = next_non_empty_frag (fragP);
8887 /* Now find the first insn frag. */
8888 first_insn = next_non_empty_frag (first_insn);
8889
8890 is_loop = next_frag_opcode_is_loop (fragP, &opcode);
8891 gas_assert (is_loop);
8892 loop_insn_size = xg_get_single_size (opcode);
8893
8894 pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP);
8895 pre_opcode_bytes += loop_insn_size;
8896
8897 /* For loops, the alignment depends on the size of the
8898 instruction following the loop, not the LOOP instruction. */
8899
8900 if (first_insn == NULL)
8901 first_insn_size = xtensa_fetch_width;
8902 else
8903 first_insn_size = get_loop_align_size (frag_format_size (first_insn));
8904
8905 /* If it was 8, then we'll need a larger alignment for the section. */
8906 align_power = get_text_align_power (first_insn_size);
8907 record_alignment (now_seg, align_power);
8908
8909 fill_size = get_text_align_fill_size
8910 (address + pre_opcode_bytes, align_power, first_insn_size, TRUE,
8911 fragP->tc_frag_data.is_no_density);
8912
8913 return address + fill_size;
8914 }
8915
8916
8917 /* 3 mechanisms for relaxing an alignment:
8918
8919 Align to a power of 2.
8920 Align so the next fragment's instruction does not cross a word boundary.
8921 Align the current instruction so that if the next instruction
8922 were 3 bytes, it would not cross a word boundary.
8923
8924 We can align with:
8925
8926 zeros - This is easy; always insert zeros.
8927 nops - 3-byte and 2-byte instructions
8928 2 - 2-byte nop
8929 3 - 3-byte nop
8930 4 - 2 2-byte nops
8931 >=5 : 3-byte instruction + fn (n-3)
8932 widening - widen previous instructions. */
8933
8934 static offsetT
8935 get_aligned_diff (fragS *fragP, addressT address, offsetT *max_diff)
8936 {
8937 addressT target_address, loop_insn_offset;
8938 int target_size;
8939 xtensa_opcode loop_opcode;
8940 bfd_boolean is_loop;
8941 int align_power;
8942 offsetT opt_diff;
8943 offsetT branch_align;
8944 fragS *loop_frag;
8945
8946 gas_assert (fragP->fr_type == rs_machine_dependent);
8947 switch (fragP->fr_subtype)
8948 {
8949 case RELAX_DESIRE_ALIGN:
8950 target_size = next_frag_format_size (fragP);
8951 if (target_size == XTENSA_UNDEFINED)
8952 target_size = 3;
8953 align_power = branch_align_power (now_seg);
8954 branch_align = 1 << align_power;
8955 /* Don't count on the section alignment being as large as the target. */
8956 if (target_size > branch_align)
8957 target_size = branch_align;
8958 opt_diff = get_text_align_fill_size (address, align_power,
8959 target_size, FALSE, FALSE);
8960
8961 *max_diff = (opt_diff + branch_align
8962 - (target_size + ((address + opt_diff) % branch_align)));
8963 gas_assert (*max_diff >= opt_diff);
8964 return opt_diff;
8965
8966 case RELAX_ALIGN_NEXT_OPCODE:
8967 /* The next non-empty frag after this one holds the LOOP instruction
8968 that needs to be aligned. The required alignment depends on the
8969 size of the next non-empty frag after the loop frag, i.e., the
8970 first instruction in the loop. */
8971 loop_frag = next_non_empty_frag (fragP);
8972 target_size = get_loop_align_size (next_frag_format_size (loop_frag));
8973 loop_insn_offset = 0;
8974 is_loop = next_frag_opcode_is_loop (fragP, &loop_opcode);
8975 gas_assert (is_loop);
8976
8977 /* If the loop has been expanded then the LOOP instruction
8978 could be at an offset from this fragment. */
8979 if (loop_frag->tc_frag_data.slot_subtypes[0] != RELAX_IMMED)
8980 loop_insn_offset = get_expanded_loop_offset (loop_opcode);
8981
8982 /* In an ideal world, which is what we are shooting for here,
8983 we wouldn't need to use any NOPs immediately prior to the
8984 LOOP instruction. If this approach fails, relax_frag_loop_align
8985 will call get_noop_aligned_address. */
8986 target_address =
8987 address + loop_insn_offset + xg_get_single_size (loop_opcode);
8988 align_power = get_text_align_power (target_size);
8989 opt_diff = get_text_align_fill_size (target_address, align_power,
8990 target_size, FALSE, FALSE);
8991
8992 *max_diff = xtensa_fetch_width
8993 - ((target_address + opt_diff) % xtensa_fetch_width)
8994 - target_size + opt_diff;
8995 gas_assert (*max_diff >= opt_diff);
8996 return opt_diff;
8997
8998 default:
8999 break;
9000 }
9001 gas_assert (0);
9002 return 0;
9003 }
9004
9005 \f
9006 /* md_relax_frag Hook and Helper Functions. */
9007
9008 static long relax_frag_loop_align (fragS *, long);
9009 static long relax_frag_for_align (fragS *, long);
9010 static long relax_frag_immed
9011 (segT, fragS *, long, int, xtensa_format, int, int *, bfd_boolean);
9012
9013 typedef struct cached_fixup cached_fixupS;
9014 struct cached_fixup
9015 {
9016 int addr;
9017 int target;
9018 int delta;
9019 fixS *fixP;
9020 };
9021
9022 typedef struct fixup_cache fixup_cacheS;
9023 struct fixup_cache
9024 {
9025 cached_fixupS *fixups;
9026 unsigned n_fixups;
9027 unsigned n_max;
9028
9029 segT seg;
9030 fragS *first_frag;
9031 };
9032
9033 static int fixup_order (const void *a, const void *b)
9034 {
9035 const cached_fixupS *pa = a;
9036 const cached_fixupS *pb = b;
9037
9038 if (pa->addr == pb->addr)
9039 {
9040 if (pa->target == pb->target)
9041 {
9042 if (pa->fixP->fx_r_type == pb->fixP->fx_r_type)
9043 return 0;
9044 return pa->fixP->fx_r_type < pb->fixP->fx_r_type ? -1 : 1;
9045 }
9046 return pa->target - pb->target;
9047 }
9048 return pa->addr - pb->addr;
9049 }
9050
9051 static bfd_boolean xtensa_make_cached_fixup (cached_fixupS *o, fixS *fixP)
9052 {
9053 xtensa_isa isa = xtensa_default_isa;
9054 int addr = fixP->fx_frag->fr_address;
9055 int target;
9056 int delta;
9057 symbolS *s = fixP->fx_addsy;
9058 int slot;
9059 xtensa_format fmt;
9060 xtensa_opcode opcode;
9061
9062 if (fixP->fx_r_type < BFD_RELOC_XTENSA_SLOT0_OP ||
9063 fixP->fx_r_type > BFD_RELOC_XTENSA_SLOT14_OP)
9064 return FALSE;
9065 target = S_GET_VALUE (s);
9066 delta = target - addr;
9067
9068 if (abs(delta) < J_RANGE / 2)
9069 return FALSE;
9070
9071 xtensa_insnbuf_from_chars (isa, trampoline_buf,
9072 (unsigned char *) fixP->fx_frag->fr_literal +
9073 fixP->fx_where, 0);
9074 fmt = xtensa_format_decode (isa, trampoline_buf);
9075 gas_assert (fmt != XTENSA_UNDEFINED);
9076 slot = fixP->tc_fix_data.slot;
9077 xtensa_format_get_slot (isa, fmt, slot, trampoline_buf, trampoline_slotbuf);
9078 opcode = xtensa_opcode_decode (isa, fmt, slot, trampoline_slotbuf);
9079 if (opcode != xtensa_j_opcode)
9080 return FALSE;
9081
9082 o->addr = addr;
9083 o->target = target;
9084 o->delta = delta;
9085 o->fixP = fixP;
9086
9087 return TRUE;
9088 }
9089
9090 static void xtensa_realloc_fixup_cache (fixup_cacheS *cache, unsigned add)
9091 {
9092 if (cache->n_fixups + add > cache->n_max)
9093 {
9094 cache->n_max = (cache->n_fixups + add) * 2;
9095 cache->fixups = xrealloc (cache->fixups,
9096 sizeof (*cache->fixups) * cache->n_max);
9097 }
9098 }
9099
9100 static void xtensa_cache_relaxable_fixups (fixup_cacheS *cache,
9101 segment_info_type *seginfo)
9102 {
9103 fixS *fixP;
9104
9105 cache->n_fixups = 0;
9106
9107 for (fixP = seginfo->fix_root; fixP ; fixP = fixP->fx_next)
9108 {
9109 xtensa_realloc_fixup_cache (cache, 1);
9110
9111 if (xtensa_make_cached_fixup (cache->fixups + cache->n_fixups, fixP))
9112 ++cache->n_fixups;
9113 }
9114 qsort (cache->fixups, cache->n_fixups, sizeof (*cache->fixups), fixup_order);
9115 }
9116
9117 static unsigned xtensa_find_first_cached_fixup (const fixup_cacheS *cache,
9118 int addr)
9119 {
9120 unsigned a = 0;
9121 unsigned b = cache->n_fixups;
9122
9123 while (b - a > 1)
9124 {
9125 unsigned c = (a + b) / 2;
9126
9127 if (cache->fixups[c].addr < addr)
9128 a = c;
9129 else
9130 b = c;
9131 }
9132 return a;
9133 }
9134
9135 static void xtensa_delete_cached_fixup (fixup_cacheS *cache, unsigned i)
9136 {
9137 memmove (cache->fixups + i, cache->fixups + i + 1,
9138 (cache->n_fixups - i - 1) * sizeof (*cache->fixups));
9139 --cache->n_fixups;
9140 }
9141
9142 static bfd_boolean xtensa_add_cached_fixup (fixup_cacheS *cache, fixS *fixP)
9143 {
9144 cached_fixupS o;
9145 unsigned i;
9146
9147 if (!xtensa_make_cached_fixup (&o, fixP))
9148 return FALSE;
9149 xtensa_realloc_fixup_cache (cache, 1);
9150 i = xtensa_find_first_cached_fixup (cache, o.addr);
9151 if (i < cache->n_fixups)
9152 {
9153 ++i;
9154 memmove (cache->fixups + i + 1, cache->fixups + i,
9155 (cache->n_fixups - i) * sizeof (*cache->fixups));
9156 }
9157 cache->fixups[i] = o;
9158 ++cache->n_fixups;
9159 return TRUE;
9160 }
9161
9162 /* Return the number of bytes added to this fragment, given that the
9163 input has been stretched already by "stretch". */
9164
9165 long
9166 xtensa_relax_frag (fragS *fragP, long stretch, int *stretched_p)
9167 {
9168 xtensa_isa isa = xtensa_default_isa;
9169 int unreported = fragP->tc_frag_data.unreported_expansion;
9170 long new_stretch = 0;
9171 const char *file_name;
9172 unsigned line;
9173 int lit_size;
9174 static xtensa_insnbuf vbuf = NULL;
9175 int slot, num_slots;
9176 xtensa_format fmt;
9177
9178 file_name = as_where (&line);
9179 new_logical_line (fragP->fr_file, fragP->fr_line);
9180
9181 fragP->tc_frag_data.unreported_expansion = 0;
9182
9183 switch (fragP->fr_subtype)
9184 {
9185 case RELAX_ALIGN_NEXT_OPCODE:
9186 /* Always convert. */
9187 if (fragP->tc_frag_data.relax_seen)
9188 new_stretch = relax_frag_loop_align (fragP, stretch);
9189 break;
9190
9191 case RELAX_LOOP_END:
9192 /* Do nothing. */
9193 break;
9194
9195 case RELAX_LOOP_END_ADD_NOP:
9196 /* Add a NOP and switch to .fill 0. */
9197 new_stretch = relax_frag_add_nop (fragP);
9198 frag_wane (fragP);
9199 break;
9200
9201 case RELAX_DESIRE_ALIGN:
9202 /* Do nothing. The narrowing before this frag will either align
9203 it or not. */
9204 break;
9205
9206 case RELAX_LITERAL:
9207 case RELAX_LITERAL_FINAL:
9208 return 0;
9209
9210 case RELAX_LITERAL_NR:
9211 lit_size = 4;
9212 fragP->fr_subtype = RELAX_LITERAL_FINAL;
9213 gas_assert (unreported == lit_size);
9214 memset (&fragP->fr_literal[fragP->fr_fix], 0, 4);
9215 fragP->fr_var -= lit_size;
9216 fragP->fr_fix += lit_size;
9217 new_stretch = 4;
9218 break;
9219
9220 case RELAX_SLOTS:
9221 if (vbuf == NULL)
9222 vbuf = xtensa_insnbuf_alloc (isa);
9223
9224 xtensa_insnbuf_from_chars
9225 (isa, vbuf, (unsigned char *) fragP->fr_opcode, 0);
9226 fmt = xtensa_format_decode (isa, vbuf);
9227 num_slots = xtensa_format_num_slots (isa, fmt);
9228
9229 for (slot = 0; slot < num_slots; slot++)
9230 {
9231 switch (fragP->tc_frag_data.slot_subtypes[slot])
9232 {
9233 case RELAX_NARROW:
9234 if (fragP->tc_frag_data.relax_seen)
9235 new_stretch += relax_frag_for_align (fragP, stretch);
9236 break;
9237
9238 case RELAX_IMMED:
9239 case RELAX_IMMED_STEP1:
9240 case RELAX_IMMED_STEP2:
9241 case RELAX_IMMED_STEP3:
9242 /* Place the immediate. */
9243 new_stretch += relax_frag_immed
9244 (now_seg, fragP, stretch,
9245 fragP->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
9246 fmt, slot, stretched_p, FALSE);
9247 break;
9248
9249 default:
9250 /* This is OK; see the note in xg_assemble_vliw_tokens. */
9251 break;
9252 }
9253 }
9254 break;
9255
9256 case RELAX_LITERAL_POOL_BEGIN:
9257 if (fragP->fr_var != 0)
9258 {
9259 /* We have a converted "candidate" literal pool;
9260 assemble a jump around it. */
9261 TInsn insn;
9262 if (!litpool_slotbuf)
9263 {
9264 litpool_buf = xtensa_insnbuf_alloc (isa);
9265 litpool_slotbuf = xtensa_insnbuf_alloc (isa);
9266 }
9267 new_stretch += 3;
9268 fragP->tc_frag_data.relax_seen = FALSE; /* Need another pass. */
9269 fragP->tc_frag_data.is_insn = TRUE;
9270 tinsn_init (&insn);
9271 insn.insn_type = ITYPE_INSN;
9272 insn.opcode = xtensa_j_opcode;
9273 insn.ntok = 1;
9274 set_expr_symbol_offset (&insn.tok[0], fragP->fr_symbol,
9275 fragP->fr_fix);
9276 fmt = xg_get_single_format (xtensa_j_opcode);
9277 tinsn_to_slotbuf (fmt, 0, &insn, litpool_slotbuf);
9278 xtensa_format_set_slot (isa, fmt, 0, litpool_buf, litpool_slotbuf);
9279 xtensa_insnbuf_to_chars (isa, litpool_buf,
9280 (unsigned char *)fragP->fr_literal +
9281 fragP->fr_fix, 3);
9282 fragP->fr_fix += 3;
9283 fragP->fr_var -= 3;
9284 /* Add a fix-up. */
9285 fix_new (fragP, 0, 3, fragP->fr_symbol, 0, TRUE,
9286 BFD_RELOC_XTENSA_SLOT0_OP);
9287 }
9288 break;
9289
9290 case RELAX_LITERAL_POOL_END:
9291 case RELAX_LITERAL_POOL_CANDIDATE_BEGIN:
9292 case RELAX_MAYBE_UNREACHABLE:
9293 case RELAX_MAYBE_DESIRE_ALIGN:
9294 /* No relaxation required. */
9295 break;
9296
9297 case RELAX_FILL_NOP:
9298 case RELAX_UNREACHABLE:
9299 if (fragP->tc_frag_data.relax_seen)
9300 new_stretch += relax_frag_for_align (fragP, stretch);
9301 break;
9302
9303 case RELAX_TRAMPOLINE:
9304 if (fragP->tc_frag_data.relax_seen)
9305 {
9306 static fixup_cacheS fixup_cache;
9307 segment_info_type *seginfo = seg_info (now_seg);
9308 int trampaddr = fragP->fr_address + fragP->fr_fix;
9309 int searchaddr = trampaddr < J_RANGE ? 0 : trampaddr - J_RANGE;
9310 unsigned i;
9311
9312 if (now_seg != fixup_cache.seg ||
9313 fragP == fixup_cache.first_frag ||
9314 fixup_cache.first_frag == NULL)
9315 {
9316 xtensa_cache_relaxable_fixups (&fixup_cache, seginfo);
9317 fixup_cache.seg = now_seg;
9318 fixup_cache.first_frag = fragP;
9319 }
9320
9321 /* Scan for jumps that will not reach. */
9322 for (i = xtensa_find_first_cached_fixup (&fixup_cache, searchaddr);
9323 i < fixup_cache.n_fixups; ++i)
9324
9325 {
9326 fixS *fixP = fixup_cache.fixups[i].fixP;
9327 int target = fixup_cache.fixups[i].target;
9328 int addr = fixup_cache.fixups[i].addr;
9329 int delta = fixup_cache.fixups[i].delta + stretch;
9330
9331 trampaddr = fragP->fr_address + fragP->fr_fix;
9332
9333 if (addr + J_RANGE < trampaddr)
9334 continue;
9335 if (addr > trampaddr + J_RANGE)
9336 break;
9337 if (abs (delta) < J_RANGE)
9338 continue;
9339
9340 slot = fixP->tc_fix_data.slot;
9341
9342 if (delta > J_RANGE || delta < -1 * J_RANGE)
9343 { /* Found an out-of-range jump; scan the list of trampolines for the best match. */
9344 struct trampoline_seg *ts = find_trampoline_seg (now_seg);
9345 struct trampoline_frag *tf = ts->trampoline_list.next;
9346 struct trampoline_frag *prev = &ts->trampoline_list;
9347 int lower = (target < addr) ? target : addr;
9348 int upper = (target > addr) ? target : addr;
9349 int midpoint = lower + (upper - lower) / 2;
9350
9351 if ((upper - lower) > 2 * J_RANGE)
9352 {
9353 /* One trampoline won't suffice; we need multiple jumps.
9354 Jump to the trampoline that's farthest, but still in
9355 range relative to the original "j" instruction. */
9356 for ( ; tf; prev = tf, tf = tf->next )
9357 {
9358 int this_addr = tf->fragP->fr_address + tf->fragP->fr_fix;
9359 int next_addr = (tf->next) ? tf->next->fragP->fr_address + tf->next->fragP->fr_fix : 0 ;
9360
9361 if (addr == lower)
9362 {
9363 /* Forward jump. */
9364 if (this_addr - addr < J_RANGE)
9365 break;
9366 }
9367 else
9368 {
9369 /* Backward jump. */
9370 if (next_addr == 0 || addr - next_addr > J_RANGE)
9371 break;
9372 }
9373 }
9374 }
9375 else
9376 {
9377 struct trampoline_frag *best_tf = NULL;
9378 int best_delta = 0;
9379
9380 for ( ; tf; prev = tf, tf = tf->next )
9381 {
9382 int this_addr = tf->fragP->fr_address + tf->fragP->fr_fix;
9383 int this_delta = abs (this_addr - midpoint);
9384
9385 if (!best_tf || this_delta < best_delta)
9386 {
9387 best_tf = tf;
9388 best_delta = this_delta;
9389 }
9390 }
9391 tf = best_tf;
9392 }
9393 if (tf->fragP == fragP)
9394 {
9395 if (abs (addr - trampaddr) < J_RANGE)
9396 { /* The trampoline is in range of original; fix it! */
9397 fixS *newfixP;
9398 int offset;
9399 TInsn insn;
9400 symbolS *lsym;
9401 fragS *fP; /* The out-of-range jump. */
9402
9403 new_stretch += init_trampoline_frag (tf);
9404 offset = fragP->fr_fix; /* Where to assemble the j insn. */
9405 lsym = fragP->fr_symbol;
9406 fP = fixP->fx_frag;
9407 /* Assemble a jump to the target label here. */
9408 tinsn_init (&insn);
9409 insn.insn_type = ITYPE_INSN;
9410 insn.opcode = xtensa_j_opcode;
9411 insn.ntok = 1;
9412 set_expr_symbol_offset (&insn.tok[0], lsym, offset);
9413 fmt = xg_get_single_format (xtensa_j_opcode);
9414 tinsn_to_slotbuf (fmt, 0, &insn, trampoline_slotbuf);
9415 xtensa_format_set_slot (isa, fmt, 0, trampoline_buf, trampoline_slotbuf);
9416 xtensa_insnbuf_to_chars (isa, trampoline_buf, (unsigned char *)fragP->fr_literal + offset, 3);
9417 fragP->fr_fix += 3;
9418 fragP->fr_var -= 3;
9419 /* Add a fix-up for the original j insn. */
9420 newfixP = fix_new (fP, fixP->fx_where, fixP->fx_size, lsym, fragP->fr_fix - 3, TRUE, fixP->fx_r_type);
9421 newfixP->fx_no_overflow = 1;
9422 newfixP->tc_fix_data.X_add_symbol = lsym;
9423 newfixP->tc_fix_data.X_add_number = offset;
9424 newfixP->tc_fix_data.slot = slot;
9425
9426 xtensa_delete_cached_fixup (&fixup_cache, i);
9427 xtensa_add_cached_fixup (&fixup_cache, newfixP);
9428
9429 /* Move the fix-up from the original j insn to this one. */
9430 fixP->fx_frag = fragP;
9431 fixP->fx_where = fragP->fr_fix - 3;
9432 fixP->tc_fix_data.slot = 0;
9433
9434 xtensa_add_cached_fixup (&fixup_cache, fixP);
9435
9436 /* re-do current fixup */
9437 --i;
9438
9439 /* Adjust the jump around this trampoline (if present). */
9440 if (tf->fixP != NULL)
9441 {
9442 tf->fixP->fx_offset += 3;
9443 }
9444 new_stretch += 3;
9445 fragP->tc_frag_data.relax_seen = FALSE; /* Need another pass. */
9446 /* Do we have room for more? */
9447 if (fragP->fr_var < 3)
9448 { /* No, convert to fill. */
9449 frag_wane (fragP);
9450 fragP->fr_subtype = 0;
9451 /* Remove from the trampoline_list. */
9452 prev->next = tf->next;
9453 if (fragP == fixup_cache.first_frag)
9454 fixup_cache.first_frag = NULL;
9455 break;
9456 }
9457 }
9458 }
9459 }
9460 }
9461 }
9462 break;
9463
9464 default:
9465 as_bad (_("bad relaxation state"));
9466 }
9467
9468 /* Tell gas we need another relaxation pass. */
9469 if (! fragP->tc_frag_data.relax_seen)
9470 {
9471 fragP->tc_frag_data.relax_seen = TRUE;
9472 *stretched_p = 1;
9473 }
9474
9475 new_logical_line (file_name, line);
9476 return new_stretch;
9477 }
9478
9479
9480 static long
9481 relax_frag_loop_align (fragS *fragP, long stretch)
9482 {
9483 addressT old_address, old_next_address, old_size;
9484 addressT new_address, new_next_address, new_size;
9485 addressT growth;
9486
9487 /* All the frags with relax_frag_for_alignment prior to this one in the
9488 section have been done, hopefully eliminating the need for a NOP here.
9489 But, this will put it in if necessary. */
9490
9491 /* Calculate the old address of this fragment and the next fragment. */
9492 old_address = fragP->fr_address - stretch;
9493 old_next_address = (fragP->fr_address - stretch + fragP->fr_fix +
9494 fragP->tc_frag_data.text_expansion[0]);
9495 old_size = old_next_address - old_address;
9496
9497 /* Calculate the new address of this fragment and the next fragment. */
9498 new_address = fragP->fr_address;
9499 new_next_address =
9500 get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix);
9501 new_size = new_next_address - new_address;
9502
9503 growth = new_size - old_size;
9504
9505 /* Fix up the text_expansion field and return the new growth. */
9506 fragP->tc_frag_data.text_expansion[0] += growth;
9507 return growth;
9508 }
9509
9510
9511 /* Add a NOP instruction. */
9512
9513 static long
9514 relax_frag_add_nop (fragS *fragP)
9515 {
9516 char *nop_buf = fragP->fr_literal + fragP->fr_fix;
9517 int length = fragP->tc_frag_data.is_no_density ? 3 : 2;
9518 assemble_nop (length, nop_buf);
9519 fragP->tc_frag_data.is_insn = TRUE;
9520
9521 if (fragP->fr_var < length)
9522 {
9523 as_fatal (_("fr_var (%ld) < length (%d)"), (long) fragP->fr_var, length);
9524 return 0;
9525 }
9526
9527 fragP->fr_fix += length;
9528 fragP->fr_var -= length;
9529 return length;
9530 }
9531
9532
9533 static long future_alignment_required (fragS *, long);
9534
9535 static long
9536 relax_frag_for_align (fragS *fragP, long stretch)
9537 {
9538 /* Overview of the relaxation procedure for alignment:
9539 We can widen with NOPs or by widening instructions or by filling
9540 bytes after jump instructions. Find the opportune places and widen
9541 them if necessary. */
9542
9543 long stretch_me;
9544 long diff;
9545
9546 gas_assert (fragP->fr_subtype == RELAX_FILL_NOP
9547 || fragP->fr_subtype == RELAX_UNREACHABLE
9548 || (fragP->fr_subtype == RELAX_SLOTS
9549 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW));
9550
9551 stretch_me = future_alignment_required (fragP, stretch);
9552 diff = stretch_me - fragP->tc_frag_data.text_expansion[0];
9553 if (diff == 0)
9554 return 0;
9555
9556 if (diff < 0)
9557 {
9558 /* We expanded on a previous pass. Can we shrink now? */
9559 long shrink = fragP->tc_frag_data.text_expansion[0] - stretch_me;
9560 if (shrink <= stretch && stretch > 0)
9561 {
9562 fragP->tc_frag_data.text_expansion[0] = stretch_me;
9563 return -shrink;
9564 }
9565 return 0;
9566 }
9567
9568 /* Below here, diff > 0. */
9569 fragP->tc_frag_data.text_expansion[0] = stretch_me;
9570
9571 return diff;
9572 }
9573
9574
9575 /* Return the address of the next frag that should be aligned.
9576
9577 By "address" we mean the address it _would_ be at if there
9578 is no action taken to align it between here and the target frag.
9579 In other words, if no narrows and no fill nops are used between
9580 here and the frag to align, _even_if_ some of the frags we use
9581 to align targets have already expanded on a previous relaxation
9582 pass.
9583
9584 Also, count each frag that may be used to help align the target.
9585
9586 Return 0 if there are no frags left in the chain that need to be
9587 aligned. */
9588
9589 static addressT
9590 find_address_of_next_align_frag (fragS **fragPP,
9591 int *wide_nops,
9592 int *narrow_nops,
9593 int *widens,
9594 bfd_boolean *paddable)
9595 {
9596 fragS *fragP = *fragPP;
9597 addressT address = fragP->fr_address;
9598
9599 /* Do not reset the counts to 0. */
9600
9601 while (fragP)
9602 {
9603 /* Limit this to a small search. */
9604 if (*widens >= (int) xtensa_fetch_width)
9605 {
9606 *fragPP = fragP;
9607 return 0;
9608 }
9609 address += fragP->fr_fix;
9610
9611 if (fragP->fr_type == rs_fill)
9612 address += fragP->fr_offset * fragP->fr_var;
9613 else if (fragP->fr_type == rs_machine_dependent)
9614 {
9615 switch (fragP->fr_subtype)
9616 {
9617 case RELAX_UNREACHABLE:
9618 *paddable = TRUE;
9619 break;
9620
9621 case RELAX_FILL_NOP:
9622 (*wide_nops)++;
9623 if (!fragP->tc_frag_data.is_no_density)
9624 (*narrow_nops)++;
9625 break;
9626
9627 case RELAX_SLOTS:
9628 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
9629 {
9630 (*widens)++;
9631 break;
9632 }
9633 address += total_frag_text_expansion (fragP);
9634 break;
9635
9636 case RELAX_IMMED:
9637 address += fragP->tc_frag_data.text_expansion[0];
9638 break;
9639
9640 case RELAX_ALIGN_NEXT_OPCODE:
9641 case RELAX_DESIRE_ALIGN:
9642 *fragPP = fragP;
9643 return address;
9644
9645 case RELAX_MAYBE_UNREACHABLE:
9646 case RELAX_MAYBE_DESIRE_ALIGN:
9647 /* Do nothing. */
9648 break;
9649
9650 default:
9651 /* Just punt if we don't know the type. */
9652 *fragPP = fragP;
9653 return 0;
9654 }
9655 }
9656 else
9657 {
9658 /* Just punt if we don't know the type. */
9659 *fragPP = fragP;
9660 return 0;
9661 }
9662 fragP = fragP->fr_next;
9663 }
9664
9665 *fragPP = fragP;
9666 return 0;
9667 }
9668
9669
9670 static long bytes_to_stretch (fragS *, int, int, int, int);
9671
9672 static long
9673 future_alignment_required (fragS *fragP, long stretch ATTRIBUTE_UNUSED)
9674 {
9675 fragS *this_frag = fragP;
9676 long address;
9677 int num_widens = 0;
9678 int wide_nops = 0;
9679 int narrow_nops = 0;
9680 bfd_boolean paddable = FALSE;
9681 offsetT local_opt_diff;
9682 offsetT opt_diff;
9683 offsetT max_diff;
9684 int stretch_amount = 0;
9685 int local_stretch_amount;
9686 int global_stretch_amount;
9687
9688 address = find_address_of_next_align_frag
9689 (&fragP, &wide_nops, &narrow_nops, &num_widens, &paddable);
9690
9691 if (!address)
9692 {
9693 if (this_frag->tc_frag_data.is_aligning_branch)
9694 this_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED;
9695 else
9696 frag_wane (this_frag);
9697 }
9698 else
9699 {
9700 local_opt_diff = get_aligned_diff (fragP, address, &max_diff);
9701 opt_diff = local_opt_diff;
9702 gas_assert (opt_diff >= 0);
9703 gas_assert (max_diff >= opt_diff);
9704 if (max_diff == 0)
9705 return 0;
9706
9707 if (fragP)
9708 fragP = fragP->fr_next;
9709
9710 while (fragP && opt_diff < max_diff && address)
9711 {
9712 /* We only use these to determine if we can exit early
9713 because there will be plenty of ways to align future
9714 align frags. */
9715 int glob_widens = 0;
9716 int dnn = 0;
9717 int dw = 0;
9718 bfd_boolean glob_pad = 0;
9719 address = find_address_of_next_align_frag
9720 (&fragP, &glob_widens, &dnn, &dw, &glob_pad);
9721 /* If there is a padable portion, then skip. */
9722 if (glob_pad || glob_widens >= (1 << branch_align_power (now_seg)))
9723 address = 0;
9724
9725 if (address)
9726 {
9727 offsetT next_m_diff;
9728 offsetT next_o_diff;
9729
9730 /* Downrange frags haven't had stretch added to them yet. */
9731 address += stretch;
9732
9733 /* The address also includes any text expansion from this
9734 frag in a previous pass, but we don't want that. */
9735 address -= this_frag->tc_frag_data.text_expansion[0];
9736
9737 /* Assume we are going to move at least opt_diff. In
9738 reality, we might not be able to, but assuming that
9739 we will helps catch cases where moving opt_diff pushes
9740 the next target from aligned to unaligned. */
9741 address += opt_diff;
9742
9743 next_o_diff = get_aligned_diff (fragP, address, &next_m_diff);
9744
9745 /* Now cleanup for the adjustments to address. */
9746 next_o_diff += opt_diff;
9747 next_m_diff += opt_diff;
9748 if (next_o_diff <= max_diff && next_o_diff > opt_diff)
9749 opt_diff = next_o_diff;
9750 if (next_m_diff < max_diff)
9751 max_diff = next_m_diff;
9752 fragP = fragP->fr_next;
9753 }
9754 }
9755
9756 /* If there are enough wideners in between, do it. */
9757 if (paddable)
9758 {
9759 if (this_frag->fr_subtype == RELAX_UNREACHABLE)
9760 {
9761 gas_assert (opt_diff <= (signed) xtensa_fetch_width);
9762 return opt_diff;
9763 }
9764 return 0;
9765 }
9766 local_stretch_amount
9767 = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
9768 num_widens, local_opt_diff);
9769 global_stretch_amount
9770 = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
9771 num_widens, opt_diff);
9772 /* If the condition below is true, then the frag couldn't
9773 stretch the correct amount for the global case, so we just
9774 optimize locally. We'll rely on the subsequent frags to get
9775 the correct alignment in the global case. */
9776 if (global_stretch_amount < local_stretch_amount)
9777 stretch_amount = local_stretch_amount;
9778 else
9779 stretch_amount = global_stretch_amount;
9780
9781 if (this_frag->fr_subtype == RELAX_SLOTS
9782 && this_frag->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
9783 gas_assert (stretch_amount <= 1);
9784 else if (this_frag->fr_subtype == RELAX_FILL_NOP)
9785 {
9786 if (this_frag->tc_frag_data.is_no_density)
9787 gas_assert (stretch_amount == 3 || stretch_amount == 0);
9788 else
9789 gas_assert (stretch_amount <= 3);
9790 }
9791 }
9792 return stretch_amount;
9793 }
9794
9795
9796 /* The idea: widen everything you can to get a target or loop aligned,
9797 then start using NOPs.
9798
9799 wide_nops = the number of wide NOPs available for aligning
9800 narrow_nops = the number of narrow NOPs available for aligning
9801 (a subset of wide_nops)
9802 widens = the number of narrow instructions that should be widened
9803
9804 */
9805
9806 static long
9807 bytes_to_stretch (fragS *this_frag,
9808 int wide_nops,
9809 int narrow_nops,
9810 int num_widens,
9811 int desired_diff)
9812 {
9813 int nops_needed;
9814 int nop_bytes;
9815 int extra_bytes;
9816 int bytes_short = desired_diff - num_widens;
9817
9818 gas_assert (desired_diff >= 0
9819 && desired_diff < (signed) xtensa_fetch_width);
9820 if (desired_diff == 0)
9821 return 0;
9822
9823 gas_assert (wide_nops > 0 || num_widens > 0);
9824
9825 /* Always prefer widening to NOP-filling. */
9826 if (bytes_short < 0)
9827 {
9828 /* There are enough RELAX_NARROW frags after this one
9829 to align the target without widening this frag in any way. */
9830 return 0;
9831 }
9832
9833 if (bytes_short == 0)
9834 {
9835 /* Widen every narrow between here and the align target
9836 and the align target will be properly aligned. */
9837 if (this_frag->fr_subtype == RELAX_FILL_NOP)
9838 return 0;
9839 else
9840 return 1;
9841 }
9842
9843 /* From here we will need at least one NOP to get an alignment.
9844 However, we may not be able to align at all, in which case,
9845 don't widen. */
9846 nops_needed = desired_diff / 3;
9847
9848 /* If there aren't enough nops, don't widen. */
9849 if (nops_needed > wide_nops)
9850 return 0;
9851
9852 /* First try it with all wide nops. */
9853 nop_bytes = nops_needed * 3;
9854 extra_bytes = desired_diff - nop_bytes;
9855
9856 if (nop_bytes + num_widens >= desired_diff)
9857 {
9858 if (this_frag->fr_subtype == RELAX_FILL_NOP)
9859 return 3;
9860 else if (num_widens == extra_bytes)
9861 return 1;
9862 return 0;
9863 }
9864
9865 /* Add a narrow nop. */
9866 nops_needed++;
9867 nop_bytes += 2;
9868 extra_bytes -= 2;
9869 if (narrow_nops == 0 || nops_needed > wide_nops)
9870 return 0;
9871
9872 if (nop_bytes + num_widens >= desired_diff && extra_bytes >= 0)
9873 {
9874 if (this_frag->fr_subtype == RELAX_FILL_NOP)
9875 return !this_frag->tc_frag_data.is_no_density ? 2 : 3;
9876 else if (num_widens == extra_bytes)
9877 return 1;
9878 return 0;
9879 }
9880
9881 /* Replace a wide nop with a narrow nop--we can get here if
9882 extra_bytes was negative in the previous conditional. */
9883 if (narrow_nops == 1)
9884 return 0;
9885 nop_bytes--;
9886 extra_bytes++;
9887 if (nop_bytes + num_widens >= desired_diff)
9888 {
9889 if (this_frag->fr_subtype == RELAX_FILL_NOP)
9890 return !this_frag->tc_frag_data.is_no_density ? 2 : 3;
9891 else if (num_widens == extra_bytes)
9892 return 1;
9893 return 0;
9894 }
9895
9896 /* If we can't satisfy any of the above cases, then we can't align
9897 using padding or fill nops. */
9898 return 0;
9899 }
9900
9901
9902 static struct trampoline_frag *
9903 search_trampolines (TInsn *tinsn, fragS *fragP, bfd_boolean unreachable_only)
9904 {
9905 struct trampoline_seg *ts = find_trampoline_seg (now_seg);
9906 struct trampoline_frag *tf = (ts) ? ts->trampoline_list.next : NULL;
9907 struct trampoline_frag *best_tf = NULL;
9908 int best_delta = 0;
9909 int best_addr = 0;
9910 symbolS *sym = tinsn->tok[0].X_add_symbol;
9911 offsetT target = S_GET_VALUE (sym) + tinsn->tok[0].X_add_number;
9912 offsetT addr = fragP->fr_address;
9913 offsetT lower = (addr < target) ? addr : target;
9914 offsetT upper = (addr > target) ? addr : target;
9915 int delta = upper - lower;
9916 offsetT midpoint = lower + delta / 2;
9917 int this_delta = -1;
9918 int this_addr = -1;
9919
9920 if (delta > 2 * J_RANGE)
9921 {
9922 /* One trampoline won't do; we need multiple.
9923 Choose the farthest trampoline that's still in range of the original
9924 and let a later pass finish the job. */
9925 for ( ; tf; tf = tf->next)
9926 {
9927 int next_addr = (tf->next) ? tf->next->fragP->fr_address + tf->next->fragP->fr_fix : 0;
9928
9929 this_addr = tf->fragP->fr_address + tf->fragP->fr_fix;
9930 if (lower == addr)
9931 {
9932 /* Forward jump. */
9933 if (this_addr - addr < J_RANGE)
9934 break;
9935 }
9936 else
9937 {
9938 /* Backward jump. */
9939 if (next_addr == 0 || addr - next_addr > J_RANGE)
9940 break;
9941 }
9942 }
9943 if (abs (addr - this_addr) < J_RANGE)
9944 return tf;
9945
9946 return NULL;
9947 }
9948 for ( ; tf; tf = tf->next)
9949 {
9950 this_addr = tf->fragP->fr_address + tf->fragP->fr_fix;
9951 this_delta = abs (this_addr - midpoint);
9952 if (unreachable_only && tf->needs_jump_around)
9953 continue;
9954 if (!best_tf || this_delta < best_delta)
9955 {
9956 best_tf = tf;
9957 best_delta = this_delta;
9958 best_addr = this_addr;
9959 }
9960 }
9961
9962 if (best_tf &&
9963 best_delta < J_RANGE &&
9964 abs(best_addr - lower) < J_RANGE &&
9965 abs(best_addr - upper) < J_RANGE)
9966 return best_tf;
9967
9968 return NULL; /* No suitable trampoline found. */
9969 }
9970
9971
9972 static struct trampoline_frag *
9973 get_best_trampoline (TInsn *tinsn, fragS *fragP)
9974 {
9975 struct trampoline_frag *tf = NULL;
9976
9977 tf = search_trampolines (tinsn, fragP, TRUE); /* Try unreachable first. */
9978
9979 if (tf == NULL)
9980 tf = search_trampolines (tinsn, fragP, FALSE); /* Try ones needing a jump-around, too. */
9981
9982 return tf;
9983 }
9984
9985
9986 static void
9987 check_and_update_trampolines (void)
9988 {
9989 struct trampoline_seg *ts = find_trampoline_seg (now_seg);
9990 struct trampoline_frag *tf = ts->trampoline_list.next;
9991 struct trampoline_frag *prev = &ts->trampoline_list;
9992
9993 for ( ; tf; prev = tf, tf = tf->next)
9994 {
9995 if (tf->fragP->fr_var < 3)
9996 {
9997 frag_wane (tf->fragP);
9998 prev->next = tf->next;
9999 tf->fragP = NULL;
10000 }
10001 }
10002 }
10003
10004
10005 static int
10006 init_trampoline_frag (struct trampoline_frag *trampP)
10007 {
10008 fragS *fp = trampP->fragP;
10009 int growth = 0;
10010
10011 if (fp->fr_fix == 0)
10012 {
10013 symbolS *lsym;
10014 char label[10 + 2 * sizeof(fp)];
10015 sprintf (label, ".L0_TR_%p", fp);
10016
10017 lsym = (symbolS *)local_symbol_make (label, now_seg, 0, fp);
10018 fp->fr_symbol = lsym;
10019 if (trampP->needs_jump_around)
10020 {
10021 /* Add a jump around this block of jumps, in case
10022 control flows into this block. */
10023 fixS *fixP;
10024 TInsn insn;
10025 xtensa_format fmt;
10026 xtensa_isa isa = xtensa_default_isa;
10027
10028 fp->tc_frag_data.is_insn = 1;
10029 /* Assemble a jump insn. */
10030 tinsn_init (&insn);
10031 insn.insn_type = ITYPE_INSN;
10032 insn.opcode = xtensa_j_opcode;
10033 insn.ntok = 1;
10034 set_expr_symbol_offset (&insn.tok[0], lsym, 3);
10035 fmt = xg_get_single_format (xtensa_j_opcode);
10036 tinsn_to_slotbuf (fmt, 0, &insn, trampoline_slotbuf);
10037 xtensa_format_set_slot (isa, fmt, 0, trampoline_buf, trampoline_slotbuf);
10038 xtensa_insnbuf_to_chars (isa, trampoline_buf, (unsigned char *)fp->fr_literal, 3);
10039 fp->fr_fix += 3;
10040 fp->fr_var -= 3;
10041 growth = 3;
10042 fixP = fix_new (fp, 0, 3, lsym, 3, TRUE, BFD_RELOC_XTENSA_SLOT0_OP);
10043 trampP->fixP = fixP;
10044 }
10045 }
10046 return growth;
10047 }
10048
10049
10050 static int
10051 add_jump_to_trampoline (struct trampoline_frag *trampP, fragS *origfrag)
10052 {
10053 fragS *tramp = trampP->fragP;
10054 fixS *fixP;
10055 int offset = tramp->fr_fix; /* Where to assemble the j insn. */
10056 TInsn insn;
10057 symbolS *lsym;
10058 symbolS *tsym;
10059 int toffset;
10060 xtensa_format fmt;
10061 xtensa_isa isa = xtensa_default_isa;
10062 int growth = 0;
10063
10064 lsym = tramp->fr_symbol;
10065 /* Assemble a jump to the target label in the trampoline frag. */
10066 tsym = origfrag->tc_frag_data.slot_symbols[0];
10067 toffset = origfrag-> tc_frag_data.slot_offsets[0];
10068 tinsn_init (&insn);
10069 insn.insn_type = ITYPE_INSN;
10070 insn.opcode = xtensa_j_opcode;
10071 insn.ntok = 1;
10072 set_expr_symbol_offset (&insn.tok[0], tsym, toffset);
10073 fmt = xg_get_single_format (xtensa_j_opcode);
10074 tinsn_to_slotbuf (fmt, 0, &insn, trampoline_slotbuf);
10075 xtensa_format_set_slot (isa, fmt, 0, trampoline_buf, trampoline_slotbuf);
10076 xtensa_insnbuf_to_chars (isa, trampoline_buf, (unsigned char *)tramp->fr_literal + offset, 3);
10077 tramp->fr_fix += 3;
10078 tramp->fr_var -= 3;
10079 growth = 3;
10080 /* add a fix-up for the trampoline jump. */
10081 fixP = fix_new (tramp, tramp->fr_fix - 3, 3, tsym, toffset, TRUE, BFD_RELOC_XTENSA_SLOT0_OP);
10082 /* Modify the jump at the start of this trampoline to point past the newly-added jump. */
10083 fixP = trampP->fixP;
10084 if (fixP)
10085 fixP->fx_offset += 3;
10086 /* Modify the original j to point here. */
10087 origfrag->tc_frag_data.slot_symbols[0] = lsym;
10088 origfrag->tc_frag_data.slot_offsets[0] = tramp->fr_fix - 3;
10089 /* If trampoline is full, remove it from the list. */
10090 check_and_update_trampolines ();
10091
10092 return growth;
10093 }
10094
10095
10096 static long
10097 relax_frag_immed (segT segP,
10098 fragS *fragP,
10099 long stretch,
10100 int min_steps,
10101 xtensa_format fmt,
10102 int slot,
10103 int *stretched_p,
10104 bfd_boolean estimate_only)
10105 {
10106 TInsn tinsn;
10107 int old_size;
10108 bfd_boolean negatable_branch = FALSE;
10109 bfd_boolean branch_jmp_to_next = FALSE;
10110 bfd_boolean from_wide_insn = FALSE;
10111 xtensa_isa isa = xtensa_default_isa;
10112 IStack istack;
10113 offsetT frag_offset;
10114 int num_steps;
10115 int num_text_bytes, num_literal_bytes;
10116 int literal_diff, total_text_diff, this_text_diff;
10117
10118 gas_assert (fragP->fr_opcode != NULL);
10119
10120 xg_clear_vinsn (&cur_vinsn);
10121 vinsn_from_chars (&cur_vinsn, fragP->fr_opcode);
10122 if (cur_vinsn.num_slots > 1)
10123 from_wide_insn = TRUE;
10124
10125 tinsn = cur_vinsn.slots[slot];
10126 tinsn_immed_from_frag (&tinsn, fragP, slot);
10127
10128 if (estimate_only && xtensa_opcode_is_loop (isa, tinsn.opcode) == 1)
10129 return 0;
10130
10131 if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
10132 branch_jmp_to_next = is_branch_jmp_to_next (&tinsn, fragP);
10133
10134 negatable_branch = (xtensa_opcode_is_branch (isa, tinsn.opcode) == 1);
10135
10136 old_size = xtensa_format_length (isa, fmt);
10137
10138 /* Special case: replace a branch to the next instruction with a NOP.
10139 This is required to work around a hardware bug in T1040.0 and also
10140 serves as an optimization. */
10141
10142 if (branch_jmp_to_next
10143 && ((old_size == 2) || (old_size == 3))
10144 && !next_frag_is_loop_target (fragP))
10145 return 0;
10146
10147 /* Here is the fun stuff: Get the immediate field from this
10148 instruction. If it fits, we are done. If not, find the next
10149 instruction sequence that fits. */
10150
10151 frag_offset = fragP->fr_opcode - fragP->fr_literal;
10152 istack_init (&istack);
10153 num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP, frag_offset,
10154 min_steps, stretch);
10155 gas_assert (num_steps >= min_steps && num_steps <= RELAX_IMMED_MAXSTEPS);
10156
10157 fragP->tc_frag_data.slot_subtypes[slot] = (int) RELAX_IMMED + num_steps;
10158
10159 /* Figure out the number of bytes needed. */
10160 num_literal_bytes = get_num_stack_literal_bytes (&istack);
10161 literal_diff
10162 = num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot];
10163 num_text_bytes = get_num_stack_text_bytes (&istack);
10164
10165 if (from_wide_insn)
10166 {
10167 int first = 0;
10168 while (istack.insn[first].opcode == XTENSA_UNDEFINED)
10169 first++;
10170
10171 num_text_bytes += old_size;
10172 if (opcode_fits_format_slot (istack.insn[first].opcode, fmt, slot))
10173 num_text_bytes -= xg_get_single_size (istack.insn[first].opcode);
10174 else
10175 {
10176 /* The first instruction in the relaxed sequence will go after
10177 the current wide instruction, and thus its symbolic immediates
10178 might not fit. */
10179
10180 istack_init (&istack);
10181 num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP,
10182 frag_offset + old_size,
10183 min_steps, stretch + old_size);
10184 gas_assert (num_steps >= min_steps && num_steps <= RELAX_IMMED_MAXSTEPS);
10185
10186 fragP->tc_frag_data.slot_subtypes[slot]
10187 = (int) RELAX_IMMED + num_steps;
10188
10189 num_literal_bytes = get_num_stack_literal_bytes (&istack);
10190 literal_diff
10191 = num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot];
10192
10193 num_text_bytes = get_num_stack_text_bytes (&istack) + old_size;
10194 }
10195 }
10196
10197 total_text_diff = num_text_bytes - old_size;
10198 this_text_diff = total_text_diff - fragP->tc_frag_data.text_expansion[slot];
10199
10200 /* It MUST get larger. If not, we could get an infinite loop. */
10201 gas_assert (num_text_bytes >= 0);
10202 gas_assert (literal_diff >= 0);
10203 gas_assert (total_text_diff >= 0);
10204
10205 fragP->tc_frag_data.text_expansion[slot] = total_text_diff;
10206 fragP->tc_frag_data.literal_expansion[slot] = num_literal_bytes;
10207 gas_assert (fragP->tc_frag_data.text_expansion[slot] >= 0);
10208 gas_assert (fragP->tc_frag_data.literal_expansion[slot] >= 0);
10209
10210 /* Find the associated expandable literal for this. */
10211 if (literal_diff != 0)
10212 {
10213 fragS *lit_fragP = fragP->tc_frag_data.literal_frags[slot];
10214 if (lit_fragP)
10215 {
10216 gas_assert (literal_diff == 4);
10217 lit_fragP->tc_frag_data.unreported_expansion += literal_diff;
10218
10219 /* We expect that the literal section state has NOT been
10220 modified yet. */
10221 gas_assert (lit_fragP->fr_type == rs_machine_dependent
10222 && lit_fragP->fr_subtype == RELAX_LITERAL);
10223 lit_fragP->fr_subtype = RELAX_LITERAL_NR;
10224
10225 /* We need to mark this section for another iteration
10226 of relaxation. */
10227 (*stretched_p)++;
10228 }
10229 }
10230
10231 if (negatable_branch && istack.ninsn > 1)
10232 update_next_frag_state (fragP);
10233
10234 /* If last insn is a jump, and it cannot reach its target, try to find a trampoline. */
10235 if (istack.ninsn > 2 &&
10236 istack.insn[istack.ninsn - 1].insn_type == ITYPE_LABEL &&
10237 istack.insn[istack.ninsn - 2].insn_type == ITYPE_INSN &&
10238 istack.insn[istack.ninsn - 2].opcode == xtensa_j_opcode)
10239 {
10240 TInsn *jinsn = &istack.insn[istack.ninsn - 2];
10241
10242 if (!xg_symbolic_immeds_fit (jinsn, segP, fragP, fragP->fr_offset, total_text_diff))
10243 {
10244 struct trampoline_frag *tf = get_best_trampoline (jinsn, fragP);
10245
10246 if (tf)
10247 {
10248 this_text_diff += init_trampoline_frag (tf);
10249 this_text_diff += add_jump_to_trampoline (tf, fragP);
10250 }
10251 else
10252 {
10253 /* If target symbol is undefined, assume it will reach once linked. */
10254 expressionS *exp = &istack.insn[istack.ninsn - 2].tok[0];
10255
10256 if (exp->X_op == O_symbol && S_IS_DEFINED (exp->X_add_symbol))
10257 {
10258 as_bad_where (fragP->fr_file, fragP->fr_line,
10259 _("jump target out of range; no usable trampoline found"));
10260 }
10261 }
10262 }
10263 }
10264
10265 return this_text_diff;
10266 }
10267
10268 \f
10269 /* md_convert_frag Hook and Helper Functions. */
10270
10271 static void convert_frag_align_next_opcode (fragS *);
10272 static void convert_frag_narrow (segT, fragS *, xtensa_format, int);
10273 static void convert_frag_fill_nop (fragS *);
10274 static void convert_frag_immed (segT, fragS *, int, xtensa_format, int);
10275
10276 void
10277 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec, fragS *fragp)
10278 {
10279 static xtensa_insnbuf vbuf = NULL;
10280 xtensa_isa isa = xtensa_default_isa;
10281 int slot;
10282 int num_slots;
10283 xtensa_format fmt;
10284 const char *file_name;
10285 unsigned line;
10286
10287 file_name = as_where (&line);
10288 new_logical_line (fragp->fr_file, fragp->fr_line);
10289
10290 switch (fragp->fr_subtype)
10291 {
10292 case RELAX_ALIGN_NEXT_OPCODE:
10293 /* Always convert. */
10294 convert_frag_align_next_opcode (fragp);
10295 break;
10296
10297 case RELAX_DESIRE_ALIGN:
10298 /* Do nothing. If not aligned already, too bad. */
10299 break;
10300
10301 case RELAX_LITERAL:
10302 case RELAX_LITERAL_FINAL:
10303 break;
10304
10305 case RELAX_SLOTS:
10306 if (vbuf == NULL)
10307 vbuf = xtensa_insnbuf_alloc (isa);
10308
10309 xtensa_insnbuf_from_chars
10310 (isa, vbuf, (unsigned char *) fragp->fr_opcode, 0);
10311 fmt = xtensa_format_decode (isa, vbuf);
10312 num_slots = xtensa_format_num_slots (isa, fmt);
10313
10314 for (slot = 0; slot < num_slots; slot++)
10315 {
10316 switch (fragp->tc_frag_data.slot_subtypes[slot])
10317 {
10318 case RELAX_NARROW:
10319 convert_frag_narrow (sec, fragp, fmt, slot);
10320 break;
10321
10322 case RELAX_IMMED:
10323 case RELAX_IMMED_STEP1:
10324 case RELAX_IMMED_STEP2:
10325 case RELAX_IMMED_STEP3:
10326 /* Place the immediate. */
10327 convert_frag_immed
10328 (sec, fragp,
10329 fragp->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
10330 fmt, slot);
10331 break;
10332
10333 default:
10334 /* This is OK because some slots could have
10335 relaxations and others have none. */
10336 break;
10337 }
10338 }
10339 break;
10340
10341 case RELAX_UNREACHABLE:
10342 memset (&fragp->fr_literal[fragp->fr_fix], 0, fragp->fr_var);
10343 fragp->fr_fix += fragp->tc_frag_data.text_expansion[0];
10344 fragp->fr_var -= fragp->tc_frag_data.text_expansion[0];
10345 frag_wane (fragp);
10346 break;
10347
10348 case RELAX_MAYBE_UNREACHABLE:
10349 case RELAX_MAYBE_DESIRE_ALIGN:
10350 frag_wane (fragp);
10351 break;
10352
10353 case RELAX_FILL_NOP:
10354 convert_frag_fill_nop (fragp);
10355 break;
10356
10357 case RELAX_LITERAL_NR:
10358 if (use_literal_section)
10359 {
10360 /* This should have been handled during relaxation. When
10361 relaxing a code segment, literals sometimes need to be
10362 added to the corresponding literal segment. If that
10363 literal segment has already been relaxed, then we end up
10364 in this situation. Marking the literal segments as data
10365 would make this happen less often (since GAS always relaxes
10366 code before data), but we could still get into trouble if
10367 there are instructions in a segment that is not marked as
10368 containing code. Until we can implement a better solution,
10369 cheat and adjust the addresses of all the following frags.
10370 This could break subsequent alignments, but the linker's
10371 literal coalescing will do that anyway. */
10372
10373 fragS *f;
10374 fragp->fr_subtype = RELAX_LITERAL_FINAL;
10375 gas_assert (fragp->tc_frag_data.unreported_expansion == 4);
10376 memset (&fragp->fr_literal[fragp->fr_fix], 0, 4);
10377 fragp->fr_var -= 4;
10378 fragp->fr_fix += 4;
10379 for (f = fragp->fr_next; f; f = f->fr_next)
10380 f->fr_address += 4;
10381 }
10382 else
10383 as_bad (_("invalid relaxation fragment result"));
10384 break;
10385
10386 case RELAX_TRAMPOLINE:
10387 break;
10388 }
10389
10390 fragp->fr_var = 0;
10391 new_logical_line (file_name, line);
10392 }
10393
10394
10395 static void
10396 convert_frag_align_next_opcode (fragS *fragp)
10397 {
10398 char *nop_buf; /* Location for Writing. */
10399 bfd_boolean use_no_density = fragp->tc_frag_data.is_no_density;
10400 addressT aligned_address;
10401 offsetT fill_size;
10402 int nop, nop_count;
10403
10404 aligned_address = get_noop_aligned_address (fragp, fragp->fr_address +
10405 fragp->fr_fix);
10406 fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix);
10407 nop_count = get_text_align_nop_count (fill_size, use_no_density);
10408 nop_buf = fragp->fr_literal + fragp->fr_fix;
10409
10410 for (nop = 0; nop < nop_count; nop++)
10411 {
10412 int nop_size;
10413 nop_size = get_text_align_nth_nop_size (fill_size, nop, use_no_density);
10414
10415 assemble_nop (nop_size, nop_buf);
10416 nop_buf += nop_size;
10417 }
10418
10419 fragp->fr_fix += fill_size;
10420 fragp->fr_var -= fill_size;
10421 }
10422
10423
10424 static void
10425 convert_frag_narrow (segT segP, fragS *fragP, xtensa_format fmt, int slot)
10426 {
10427 TInsn tinsn, single_target;
10428 int size, old_size, diff;
10429 offsetT frag_offset;
10430
10431 gas_assert (slot == 0);
10432 tinsn_from_chars (&tinsn, fragP->fr_opcode, 0);
10433
10434 if (fragP->tc_frag_data.is_aligning_branch == 1)
10435 {
10436 gas_assert (fragP->tc_frag_data.text_expansion[0] == 1
10437 || fragP->tc_frag_data.text_expansion[0] == 0);
10438 convert_frag_immed (segP, fragP, fragP->tc_frag_data.text_expansion[0],
10439 fmt, slot);
10440 return;
10441 }
10442
10443 if (fragP->tc_frag_data.text_expansion[0] == 0)
10444 {
10445 /* No conversion. */
10446 fragP->fr_var = 0;
10447 return;
10448 }
10449
10450 gas_assert (fragP->fr_opcode != NULL);
10451
10452 /* Frags in this relaxation state should only contain
10453 single instruction bundles. */
10454 tinsn_immed_from_frag (&tinsn, fragP, 0);
10455
10456 /* Just convert it to a wide form.... */
10457 size = 0;
10458 old_size = xg_get_single_size (tinsn.opcode);
10459
10460 tinsn_init (&single_target);
10461 frag_offset = fragP->fr_opcode - fragP->fr_literal;
10462
10463 if (! xg_is_single_relaxable_insn (&tinsn, &single_target, FALSE))
10464 {
10465 as_bad (_("unable to widen instruction"));
10466 return;
10467 }
10468
10469 size = xg_get_single_size (single_target.opcode);
10470 xg_emit_insn_to_buf (&single_target, fragP->fr_opcode, fragP,
10471 frag_offset, TRUE);
10472
10473 diff = size - old_size;
10474 gas_assert (diff >= 0);
10475 gas_assert (diff <= fragP->fr_var);
10476 fragP->fr_var -= diff;
10477 fragP->fr_fix += diff;
10478
10479 /* clean it up */
10480 fragP->fr_var = 0;
10481 }
10482
10483
10484 static void
10485 convert_frag_fill_nop (fragS *fragP)
10486 {
10487 char *loc = &fragP->fr_literal[fragP->fr_fix];
10488 int size = fragP->tc_frag_data.text_expansion[0];
10489 gas_assert ((unsigned) size == (fragP->fr_next->fr_address
10490 - fragP->fr_address - fragP->fr_fix));
10491 if (size == 0)
10492 {
10493 /* No conversion. */
10494 fragP->fr_var = 0;
10495 return;
10496 }
10497 assemble_nop (size, loc);
10498 fragP->tc_frag_data.is_insn = TRUE;
10499 fragP->fr_var -= size;
10500 fragP->fr_fix += size;
10501 frag_wane (fragP);
10502 }
10503
10504
10505 static fixS *fix_new_exp_in_seg
10506 (segT, subsegT, fragS *, int, int, expressionS *, int,
10507 bfd_reloc_code_real_type);
10508 static void convert_frag_immed_finish_loop (segT, fragS *, TInsn *);
10509
10510 static void
10511 convert_frag_immed (segT segP,
10512 fragS *fragP,
10513 int min_steps,
10514 xtensa_format fmt,
10515 int slot)
10516 {
10517 char *immed_instr = fragP->fr_opcode;
10518 TInsn orig_tinsn;
10519 bfd_boolean expanded = FALSE;
10520 bfd_boolean branch_jmp_to_next = FALSE;
10521 char *fr_opcode = fragP->fr_opcode;
10522 xtensa_isa isa = xtensa_default_isa;
10523 bfd_boolean from_wide_insn = FALSE;
10524 int bytes;
10525 bfd_boolean is_loop;
10526
10527 gas_assert (fr_opcode != NULL);
10528
10529 xg_clear_vinsn (&cur_vinsn);
10530
10531 vinsn_from_chars (&cur_vinsn, fr_opcode);
10532 if (cur_vinsn.num_slots > 1)
10533 from_wide_insn = TRUE;
10534
10535 orig_tinsn = cur_vinsn.slots[slot];
10536 tinsn_immed_from_frag (&orig_tinsn, fragP, slot);
10537
10538 is_loop = xtensa_opcode_is_loop (xtensa_default_isa, orig_tinsn.opcode) == 1;
10539
10540 if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
10541 branch_jmp_to_next = is_branch_jmp_to_next (&orig_tinsn, fragP);
10542
10543 if (branch_jmp_to_next && !next_frag_is_loop_target (fragP))
10544 {
10545 /* Conversion just inserts a NOP and marks the fix as completed. */
10546 bytes = xtensa_format_length (isa, fmt);
10547 if (bytes >= 4)
10548 {
10549 cur_vinsn.slots[slot].opcode =
10550 xtensa_format_slot_nop_opcode (isa, cur_vinsn.format, slot);
10551 cur_vinsn.slots[slot].ntok = 0;
10552 }
10553 else
10554 {
10555 bytes += fragP->tc_frag_data.text_expansion[0];
10556 gas_assert (bytes == 2 || bytes == 3);
10557 build_nop (&cur_vinsn.slots[0], bytes);
10558 fragP->fr_fix += fragP->tc_frag_data.text_expansion[0];
10559 }
10560 vinsn_to_insnbuf (&cur_vinsn, fr_opcode, frag_now, TRUE);
10561 xtensa_insnbuf_to_chars
10562 (isa, cur_vinsn.insnbuf, (unsigned char *) fr_opcode, 0);
10563 fragP->fr_var = 0;
10564 }
10565 else
10566 {
10567 /* Here is the fun stuff: Get the immediate field from this
10568 instruction. If it fits, we're done. If not, find the next
10569 instruction sequence that fits. */
10570
10571 IStack istack;
10572 int i;
10573 symbolS *lit_sym = NULL;
10574 int total_size = 0;
10575 int target_offset = 0;
10576 int old_size;
10577 int diff;
10578 symbolS *gen_label = NULL;
10579 offsetT frag_offset;
10580 bfd_boolean first = TRUE;
10581
10582 /* It does not fit. Find something that does and
10583 convert immediately. */
10584 frag_offset = fr_opcode - fragP->fr_literal;
10585 istack_init (&istack);
10586 xg_assembly_relax (&istack, &orig_tinsn,
10587 segP, fragP, frag_offset, min_steps, 0);
10588
10589 old_size = xtensa_format_length (isa, fmt);
10590
10591 /* Assemble this right inline. */
10592
10593 /* First, create the mapping from a label name to the REAL label. */
10594 target_offset = 0;
10595 for (i = 0; i < istack.ninsn; i++)
10596 {
10597 TInsn *tinsn = &istack.insn[i];
10598 fragS *lit_frag;
10599
10600 switch (tinsn->insn_type)
10601 {
10602 case ITYPE_LITERAL:
10603 if (lit_sym != NULL)
10604 as_bad (_("multiple literals in expansion"));
10605 /* First find the appropriate space in the literal pool. */
10606 lit_frag = fragP->tc_frag_data.literal_frags[slot];
10607 if (lit_frag == NULL)
10608 as_bad (_("no registered fragment for literal"));
10609 if (tinsn->ntok != 1)
10610 as_bad (_("number of literal tokens != 1"));
10611
10612 /* Set the literal symbol and add a fixup. */
10613 lit_sym = lit_frag->fr_symbol;
10614 break;
10615
10616 case ITYPE_LABEL:
10617 if (align_targets && !is_loop)
10618 {
10619 fragS *unreach = fragP->fr_next;
10620 while (!(unreach->fr_type == rs_machine_dependent
10621 && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
10622 || unreach->fr_subtype == RELAX_UNREACHABLE)))
10623 {
10624 unreach = unreach->fr_next;
10625 }
10626
10627 gas_assert (unreach->fr_type == rs_machine_dependent
10628 && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
10629 || unreach->fr_subtype == RELAX_UNREACHABLE));
10630
10631 target_offset += unreach->tc_frag_data.text_expansion[0];
10632 }
10633 gas_assert (gen_label == NULL);
10634 gen_label = symbol_new (FAKE_LABEL_NAME, now_seg,
10635 fr_opcode - fragP->fr_literal
10636 + target_offset, fragP);
10637 break;
10638
10639 case ITYPE_INSN:
10640 if (first && from_wide_insn)
10641 {
10642 target_offset += xtensa_format_length (isa, fmt);
10643 first = FALSE;
10644 if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
10645 target_offset += xg_get_single_size (tinsn->opcode);
10646 }
10647 else
10648 target_offset += xg_get_single_size (tinsn->opcode);
10649 break;
10650 }
10651 }
10652
10653 total_size = 0;
10654 first = TRUE;
10655 for (i = 0; i < istack.ninsn; i++)
10656 {
10657 TInsn *tinsn = &istack.insn[i];
10658 fragS *lit_frag;
10659 int size;
10660 segT target_seg;
10661 bfd_reloc_code_real_type reloc_type;
10662
10663 switch (tinsn->insn_type)
10664 {
10665 case ITYPE_LITERAL:
10666 lit_frag = fragP->tc_frag_data.literal_frags[slot];
10667 /* Already checked. */
10668 gas_assert (lit_frag != NULL);
10669 gas_assert (lit_sym != NULL);
10670 gas_assert (tinsn->ntok == 1);
10671 /* Add a fixup. */
10672 target_seg = S_GET_SEGMENT (lit_sym);
10673 gas_assert (target_seg);
10674 reloc_type = map_operator_to_reloc (tinsn->tok[0].X_op, TRUE);
10675 fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4,
10676 &tinsn->tok[0], FALSE, reloc_type);
10677 break;
10678
10679 case ITYPE_LABEL:
10680 break;
10681
10682 case ITYPE_INSN:
10683 xg_resolve_labels (tinsn, gen_label);
10684 xg_resolve_literals (tinsn, lit_sym);
10685 if (from_wide_insn && first)
10686 {
10687 first = FALSE;
10688 if (opcode_fits_format_slot (tinsn->opcode, fmt, slot))
10689 {
10690 cur_vinsn.slots[slot] = *tinsn;
10691 }
10692 else
10693 {
10694 cur_vinsn.slots[slot].opcode =
10695 xtensa_format_slot_nop_opcode (isa, fmt, slot);
10696 cur_vinsn.slots[slot].ntok = 0;
10697 }
10698 vinsn_to_insnbuf (&cur_vinsn, immed_instr, fragP, TRUE);
10699 xtensa_insnbuf_to_chars (isa, cur_vinsn.insnbuf,
10700 (unsigned char *) immed_instr, 0);
10701 fragP->tc_frag_data.is_insn = TRUE;
10702 size = xtensa_format_length (isa, fmt);
10703 if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
10704 {
10705 xg_emit_insn_to_buf
10706 (tinsn, immed_instr + size, fragP,
10707 immed_instr - fragP->fr_literal + size, TRUE);
10708 size += xg_get_single_size (tinsn->opcode);
10709 }
10710 }
10711 else
10712 {
10713 size = xg_get_single_size (tinsn->opcode);
10714 xg_emit_insn_to_buf (tinsn, immed_instr, fragP,
10715 immed_instr - fragP->fr_literal, TRUE);
10716 }
10717 immed_instr += size;
10718 total_size += size;
10719 break;
10720 }
10721 }
10722
10723 diff = total_size - old_size;
10724 gas_assert (diff >= 0);
10725 if (diff != 0)
10726 expanded = TRUE;
10727 gas_assert (diff <= fragP->fr_var);
10728 fragP->fr_var -= diff;
10729 fragP->fr_fix += diff;
10730 }
10731
10732 /* Check for undefined immediates in LOOP instructions. */
10733 if (is_loop)
10734 {
10735 symbolS *sym;
10736 sym = orig_tinsn.tok[1].X_add_symbol;
10737 if (sym != NULL && !S_IS_DEFINED (sym))
10738 {
10739 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
10740 return;
10741 }
10742 sym = orig_tinsn.tok[1].X_op_symbol;
10743 if (sym != NULL && !S_IS_DEFINED (sym))
10744 {
10745 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
10746 return;
10747 }
10748 }
10749
10750 if (expanded && xtensa_opcode_is_loop (isa, orig_tinsn.opcode) == 1)
10751 convert_frag_immed_finish_loop (segP, fragP, &orig_tinsn);
10752
10753 if (expanded && is_direct_call_opcode (orig_tinsn.opcode))
10754 {
10755 /* Add an expansion note on the expanded instruction. */
10756 fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4,
10757 &orig_tinsn.tok[0], TRUE,
10758 BFD_RELOC_XTENSA_ASM_EXPAND);
10759 }
10760 }
10761
10762
10763 /* Add a new fix expression into the desired segment. We have to
10764 switch to that segment to do this. */
10765
10766 static fixS *
10767 fix_new_exp_in_seg (segT new_seg,
10768 subsegT new_subseg,
10769 fragS *frag,
10770 int where,
10771 int size,
10772 expressionS *exp,
10773 int pcrel,
10774 bfd_reloc_code_real_type r_type)
10775 {
10776 fixS *new_fix;
10777 segT seg = now_seg;
10778 subsegT subseg = now_subseg;
10779
10780 gas_assert (new_seg != 0);
10781 subseg_set (new_seg, new_subseg);
10782
10783 new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
10784 subseg_set (seg, subseg);
10785 return new_fix;
10786 }
10787
10788
10789 /* Relax a loop instruction so that it can span loop >256 bytes.
10790
10791 loop as, .L1
10792 .L0:
10793 rsr as, LEND
10794 wsr as, LBEG
10795 addi as, as, lo8 (label-.L1)
10796 addmi as, as, mid8 (label-.L1)
10797 wsr as, LEND
10798 isync
10799 rsr as, LCOUNT
10800 addi as, as, 1
10801 .L1:
10802 <<body>>
10803 label:
10804 */
10805
10806 static void
10807 convert_frag_immed_finish_loop (segT segP, fragS *fragP, TInsn *tinsn)
10808 {
10809 TInsn loop_insn;
10810 TInsn addi_insn;
10811 TInsn addmi_insn;
10812 unsigned long target;
10813 static xtensa_insnbuf insnbuf = NULL;
10814 unsigned int loop_length, loop_length_hi, loop_length_lo;
10815 xtensa_isa isa = xtensa_default_isa;
10816 addressT loop_offset;
10817 addressT addi_offset = 9;
10818 addressT addmi_offset = 12;
10819 fragS *next_fragP;
10820 int target_count;
10821
10822 if (!insnbuf)
10823 insnbuf = xtensa_insnbuf_alloc (isa);
10824
10825 /* Get the loop offset. */
10826 loop_offset = get_expanded_loop_offset (tinsn->opcode);
10827
10828 /* Validate that there really is a LOOP at the loop_offset. Because
10829 loops are not bundleable, we can assume that the instruction will be
10830 in slot 0. */
10831 tinsn_from_chars (&loop_insn, fragP->fr_opcode + loop_offset, 0);
10832 tinsn_immed_from_frag (&loop_insn, fragP, 0);
10833
10834 gas_assert (xtensa_opcode_is_loop (isa, loop_insn.opcode) == 1);
10835 addi_offset += loop_offset;
10836 addmi_offset += loop_offset;
10837
10838 gas_assert (tinsn->ntok == 2);
10839 if (tinsn->tok[1].X_op == O_constant)
10840 target = tinsn->tok[1].X_add_number;
10841 else if (tinsn->tok[1].X_op == O_symbol)
10842 {
10843 /* Find the fragment. */
10844 symbolS *sym = tinsn->tok[1].X_add_symbol;
10845 gas_assert (S_GET_SEGMENT (sym) == segP
10846 || S_GET_SEGMENT (sym) == absolute_section);
10847 target = (S_GET_VALUE (sym) + tinsn->tok[1].X_add_number);
10848 }
10849 else
10850 {
10851 as_bad (_("invalid expression evaluation type %d"), tinsn->tok[1].X_op);
10852 target = 0;
10853 }
10854
10855 loop_length = target - (fragP->fr_address + fragP->fr_fix);
10856 loop_length_hi = loop_length & ~0x0ff;
10857 loop_length_lo = loop_length & 0x0ff;
10858 if (loop_length_lo >= 128)
10859 {
10860 loop_length_lo -= 256;
10861 loop_length_hi += 256;
10862 }
10863
10864 /* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most
10865 32512. If the loop is larger than that, then we just fail. */
10866 if (loop_length_hi > 32512)
10867 as_bad_where (fragP->fr_file, fragP->fr_line,
10868 _("loop too long for LOOP instruction"));
10869
10870 tinsn_from_chars (&addi_insn, fragP->fr_opcode + addi_offset, 0);
10871 gas_assert (addi_insn.opcode == xtensa_addi_opcode);
10872
10873 tinsn_from_chars (&addmi_insn, fragP->fr_opcode + addmi_offset, 0);
10874 gas_assert (addmi_insn.opcode == xtensa_addmi_opcode);
10875
10876 set_expr_const (&addi_insn.tok[2], loop_length_lo);
10877 tinsn_to_insnbuf (&addi_insn, insnbuf);
10878
10879 fragP->tc_frag_data.is_insn = TRUE;
10880 xtensa_insnbuf_to_chars
10881 (isa, insnbuf, (unsigned char *) fragP->fr_opcode + addi_offset, 0);
10882
10883 set_expr_const (&addmi_insn.tok[2], loop_length_hi);
10884 tinsn_to_insnbuf (&addmi_insn, insnbuf);
10885 xtensa_insnbuf_to_chars
10886 (isa, insnbuf, (unsigned char *) fragP->fr_opcode + addmi_offset, 0);
10887
10888 /* Walk through all of the frags from here to the loop end
10889 and mark them as no_transform to keep them from being modified
10890 by the linker. If we ever have a relocation for the
10891 addi/addmi of the difference of two symbols we can remove this. */
10892
10893 target_count = 0;
10894 for (next_fragP = fragP; next_fragP != NULL;
10895 next_fragP = next_fragP->fr_next)
10896 {
10897 next_fragP->tc_frag_data.is_no_transform = TRUE;
10898 if (next_fragP->tc_frag_data.is_loop_target)
10899 target_count++;
10900 if (target_count == 2)
10901 break;
10902 }
10903 }
10904
10905 \f
10906 /* A map that keeps information on a per-subsegment basis. This is
10907 maintained during initial assembly, but is invalid once the
10908 subsegments are smashed together. I.E., it cannot be used during
10909 the relaxation. */
10910
10911 typedef struct subseg_map_struct
10912 {
10913 /* the key */
10914 segT seg;
10915 subsegT subseg;
10916
10917 /* the data */
10918 unsigned flags;
10919 float total_freq; /* fall-through + branch target frequency */
10920 float target_freq; /* branch target frequency alone */
10921
10922 struct subseg_map_struct *next;
10923 } subseg_map;
10924
10925
10926 static subseg_map *sseg_map = NULL;
10927
10928 static subseg_map *
10929 get_subseg_info (segT seg, subsegT subseg)
10930 {
10931 subseg_map *subseg_e;
10932
10933 for (subseg_e = sseg_map; subseg_e; subseg_e = subseg_e->next)
10934 {
10935 if (seg == subseg_e->seg && subseg == subseg_e->subseg)
10936 break;
10937 }
10938 return subseg_e;
10939 }
10940
10941
10942 static subseg_map *
10943 add_subseg_info (segT seg, subsegT subseg)
10944 {
10945 subseg_map *subseg_e = (subseg_map *) xmalloc (sizeof (subseg_map));
10946 memset (subseg_e, 0, sizeof (subseg_map));
10947 subseg_e->seg = seg;
10948 subseg_e->subseg = subseg;
10949 subseg_e->flags = 0;
10950 /* Start off considering every branch target very important. */
10951 subseg_e->target_freq = 1.0;
10952 subseg_e->total_freq = 1.0;
10953 subseg_e->next = sseg_map;
10954 sseg_map = subseg_e;
10955 return subseg_e;
10956 }
10957
10958
10959 static unsigned
10960 get_last_insn_flags (segT seg, subsegT subseg)
10961 {
10962 subseg_map *subseg_e = get_subseg_info (seg, subseg);
10963 if (subseg_e)
10964 return subseg_e->flags;
10965 return 0;
10966 }
10967
10968
10969 static void
10970 set_last_insn_flags (segT seg,
10971 subsegT subseg,
10972 unsigned fl,
10973 bfd_boolean val)
10974 {
10975 subseg_map *subseg_e = get_subseg_info (seg, subseg);
10976 if (! subseg_e)
10977 subseg_e = add_subseg_info (seg, subseg);
10978 if (val)
10979 subseg_e->flags |= fl;
10980 else
10981 subseg_e->flags &= ~fl;
10982 }
10983
10984
10985 static float
10986 get_subseg_total_freq (segT seg, subsegT subseg)
10987 {
10988 subseg_map *subseg_e = get_subseg_info (seg, subseg);
10989 if (subseg_e)
10990 return subseg_e->total_freq;
10991 return 1.0;
10992 }
10993
10994
10995 static float
10996 get_subseg_target_freq (segT seg, subsegT subseg)
10997 {
10998 subseg_map *subseg_e = get_subseg_info (seg, subseg);
10999 if (subseg_e)
11000 return subseg_e->target_freq;
11001 return 1.0;
11002 }
11003
11004
11005 static void
11006 set_subseg_freq (segT seg, subsegT subseg, float total_f, float target_f)
11007 {
11008 subseg_map *subseg_e = get_subseg_info (seg, subseg);
11009 if (! subseg_e)
11010 subseg_e = add_subseg_info (seg, subseg);
11011 subseg_e->total_freq = total_f;
11012 subseg_e->target_freq = target_f;
11013 }
11014
11015 \f
11016 /* Segment Lists and emit_state Stuff. */
11017
11018 static void
11019 xtensa_move_seg_list_to_beginning (seg_list *head)
11020 {
11021 head = head->next;
11022 while (head)
11023 {
11024 segT literal_section = head->seg;
11025
11026 /* Move the literal section to the front of the section list. */
11027 gas_assert (literal_section);
11028 if (literal_section != stdoutput->sections)
11029 {
11030 bfd_section_list_remove (stdoutput, literal_section);
11031 bfd_section_list_prepend (stdoutput, literal_section);
11032 }
11033 head = head->next;
11034 }
11035 }
11036
11037
11038 static void mark_literal_frags (seg_list *);
11039
11040 static void
11041 xtensa_move_literals (void)
11042 {
11043 seg_list *segment;
11044 frchainS *frchain_from, *frchain_to;
11045 fragS *search_frag, *next_frag, *literal_pool, *insert_after;
11046 fragS **frag_splice;
11047 emit_state state;
11048 segT dest_seg;
11049 fixS *fix, *next_fix, **fix_splice;
11050 sym_list *lit;
11051 struct litpool_seg *lps;
11052 const char *init_name = INIT_SECTION_NAME;
11053 const char *fini_name = FINI_SECTION_NAME;
11054 int init_name_len = strlen(init_name);
11055 int fini_name_len = strlen(fini_name);
11056
11057 mark_literal_frags (literal_head->next);
11058
11059 if (use_literal_section)
11060 return;
11061
11062 /* Assign addresses (rough estimates) to the potential literal pool locations
11063 and create new ones if the gaps are too large. */
11064
11065 for (lps = litpool_seg_list.next; lps; lps = lps->next)
11066 {
11067 frchainS *frchP = seg_info (lps->seg)->frchainP;
11068 struct litpool_frag *lpf = lps->frag_list.next;
11069 addressT addr = 0;
11070
11071 for ( ; frchP; frchP = frchP->frch_next)
11072 {
11073 fragS *fragP;
11074 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
11075 {
11076 if (lpf && fragP == lpf->fragP)
11077 {
11078 gas_assert(fragP->fr_type == rs_machine_dependent &&
11079 (fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN ||
11080 fragP->fr_subtype == RELAX_LITERAL_POOL_CANDIDATE_BEGIN));
11081 /* Found a litpool location. */
11082 lpf->addr = addr;
11083 lpf = lpf->next;
11084 }
11085 if (fragP->fr_type == rs_machine_dependent &&
11086 fragP->fr_subtype == RELAX_SLOTS)
11087 {
11088 int slot;
11089 for (slot = 0; slot < MAX_SLOTS; slot++)
11090 {
11091 if (fragP->tc_frag_data.literal_frags[slot])
11092 {
11093 /* L32R; point its literal to the nearest litpool
11094 preferring non-"candidate" positions to avoid
11095 the jump-around. */
11096 fragS *litfrag = fragP->tc_frag_data.literal_frags[slot];
11097 struct litpool_frag *lp = lpf->prev;
11098 if (!lp->fragP)
11099 {
11100 break;
11101 }
11102 while (lp->fragP->fr_subtype ==
11103 RELAX_LITERAL_POOL_CANDIDATE_BEGIN)
11104 {
11105 lp = lp->prev;
11106 if (lp->fragP == NULL)
11107 {
11108 /* End of list; have to bite the bullet.
11109 Take the nearest. */
11110 lp = lpf->prev;
11111 break;
11112 }
11113 /* Does it (conservatively) reach? */
11114 if (addr - lp->addr <= 128 * 1024)
11115 {
11116 if (lp->fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN)
11117 {
11118 /* Found a good one. */
11119 break;
11120 }
11121 else if (lp->prev->fragP &&
11122 addr - lp->prev->addr > 128 * 1024)
11123 {
11124 /* This is still a "candidate" but the next one
11125 will be too far away, so revert to the nearest
11126 one, convert it and add the jump around. */
11127 fragS *poolbeg;
11128 fragS *poolend;
11129 symbolS *lsym;
11130 char label[10 + 2 * sizeof (fragS *)];
11131 lp = lpf->prev;
11132 poolbeg = lp->fragP;
11133 lp->priority = 1;
11134 poolbeg->fr_subtype = RELAX_LITERAL_POOL_BEGIN;
11135 poolend = poolbeg->fr_next;
11136 gas_assert (poolend->fr_type == rs_machine_dependent &&
11137 poolend->fr_subtype == RELAX_LITERAL_POOL_END);
11138 /* Create a local symbol pointing to the
11139 end of the pool. */
11140 sprintf (label, ".L0_LT_%p", poolbeg);
11141 lsym = (symbolS *)local_symbol_make (label, lps->seg,
11142 0, poolend);
11143 poolbeg->fr_symbol = lsym;
11144 /* Rest is done in xtensa_relax_frag. */
11145 }
11146 }
11147 }
11148 if (! litfrag->tc_frag_data.literal_frag)
11149 {
11150 /* Take earliest use of this literal to avoid
11151 forward refs. */
11152 litfrag->tc_frag_data.literal_frag = lp->fragP;
11153 }
11154 }
11155 }
11156 }
11157 addr += fragP->fr_fix;
11158 if (fragP->fr_type == rs_fill)
11159 addr += fragP->fr_offset;
11160 }
11161 }
11162 }
11163
11164 for (segment = literal_head->next; segment; segment = segment->next)
11165 {
11166 const char *seg_name = segment_name (segment->seg);
11167
11168 /* Keep the literals for .init and .fini in separate sections. */
11169 if ((!memcmp (seg_name, init_name, init_name_len) &&
11170 !strcmp (seg_name + init_name_len, ".literal")) ||
11171 (!memcmp (seg_name, fini_name, fini_name_len) &&
11172 !strcmp (seg_name + fini_name_len, ".literal")))
11173 continue;
11174
11175 frchain_from = seg_info (segment->seg)->frchainP;
11176 search_frag = frchain_from->frch_root;
11177 literal_pool = NULL;
11178 frchain_to = NULL;
11179 frag_splice = &(frchain_from->frch_root);
11180
11181 while (search_frag && !search_frag->tc_frag_data.literal_frag)
11182 {
11183 gas_assert (search_frag->fr_fix == 0
11184 || search_frag->fr_type == rs_align);
11185 search_frag = search_frag->fr_next;
11186 }
11187
11188 if (!search_frag)
11189 {
11190 search_frag = frchain_from->frch_root;
11191 as_bad_where (search_frag->fr_file, search_frag->fr_line,
11192 _("literal pool location required for text-section-literals; specify with .literal_position"));
11193 continue;
11194 }
11195
11196 gas_assert (search_frag->tc_frag_data.literal_frag->fr_subtype
11197 == RELAX_LITERAL_POOL_BEGIN);
11198 xtensa_switch_section_emit_state (&state, segment->seg, 0);
11199
11200 /* Make sure that all the frags in this series are closed, and
11201 that there is at least one left over of zero-size. This
11202 prevents us from making a segment with an frchain without any
11203 frags in it. */
11204 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
11205 xtensa_set_frag_assembly_state (frag_now);
11206 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
11207 xtensa_set_frag_assembly_state (frag_now);
11208
11209 while (search_frag != frag_now)
11210 {
11211 next_frag = search_frag->fr_next;
11212 if (search_frag->tc_frag_data.literal_frag)
11213 {
11214 literal_pool = search_frag->tc_frag_data.literal_frag;
11215 gas_assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN);
11216 frchain_to = literal_pool->tc_frag_data.lit_frchain;
11217 gas_assert (frchain_to);
11218 }
11219
11220 if (search_frag->fr_type == rs_fill && search_frag->fr_fix == 0)
11221 {
11222 /* Skip empty fill frags. */
11223 *frag_splice = next_frag;
11224 search_frag = next_frag;
11225 continue;
11226 }
11227
11228 if (search_frag->fr_type == rs_align)
11229 {
11230 /* Skip alignment frags, because the pool as a whole will be
11231 aligned if used, and we don't want to force alignment if the
11232 pool is unused. */
11233 *frag_splice = next_frag;
11234 search_frag = next_frag;
11235 continue;
11236 }
11237
11238 /* First, move the frag out of the literal section and
11239 to the appropriate place. */
11240
11241 /* Insert an aligmnent frag at start of pool. */
11242 if (literal_pool->fr_next->fr_type == rs_machine_dependent &&
11243 literal_pool->fr_next->fr_subtype == RELAX_LITERAL_POOL_END)
11244 {
11245 segT pool_seg = literal_pool->fr_next->tc_frag_data.lit_seg;
11246 emit_state prev_state;
11247 fragS *prev_frag;
11248 fragS *align_frag;
11249 xtensa_switch_section_emit_state (&prev_state, pool_seg, 0);
11250 prev_frag = frag_now;
11251 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
11252 align_frag = frag_now;
11253 frag_align (2, 0, 0);
11254 /* Splice it into the right place. */
11255 prev_frag->fr_next = align_frag->fr_next;
11256 align_frag->fr_next = literal_pool->fr_next;
11257 literal_pool->fr_next = align_frag;
11258 /* Insert after this one. */
11259 literal_pool->tc_frag_data.literal_frag = align_frag;
11260 xtensa_restore_emit_state (&prev_state);
11261 }
11262 insert_after = literal_pool->tc_frag_data.literal_frag;
11263 dest_seg = insert_after->fr_next->tc_frag_data.lit_seg;
11264 /* Skip align frag. */
11265 if (insert_after->fr_next->fr_type == rs_align)
11266 {
11267 insert_after = insert_after->fr_next;
11268 }
11269
11270 *frag_splice = next_frag;
11271 search_frag->fr_next = insert_after->fr_next;
11272 insert_after->fr_next = search_frag;
11273 search_frag->tc_frag_data.lit_seg = dest_seg;
11274 literal_pool->tc_frag_data.literal_frag = search_frag;
11275
11276 /* Now move any fixups associated with this frag to the
11277 right section. */
11278 fix = frchain_from->fix_root;
11279 fix_splice = &(frchain_from->fix_root);
11280 while (fix)
11281 {
11282 next_fix = fix->fx_next;
11283 if (fix->fx_frag == search_frag)
11284 {
11285 *fix_splice = next_fix;
11286 fix->fx_next = frchain_to->fix_root;
11287 frchain_to->fix_root = fix;
11288 if (frchain_to->fix_tail == NULL)
11289 frchain_to->fix_tail = fix;
11290 }
11291 else
11292 fix_splice = &(fix->fx_next);
11293 fix = next_fix;
11294 }
11295 search_frag = next_frag;
11296 }
11297
11298 if (frchain_from->fix_root != NULL)
11299 {
11300 frchain_from = seg_info (segment->seg)->frchainP;
11301 as_warn (_("fixes not all moved from %s"), segment->seg->name);
11302
11303 gas_assert (frchain_from->fix_root == NULL);
11304 }
11305 frchain_from->fix_tail = NULL;
11306 xtensa_restore_emit_state (&state);
11307 }
11308
11309 /* Now fix up the SEGMENT value for all the literal symbols. */
11310 for (lit = literal_syms; lit; lit = lit->next)
11311 {
11312 symbolS *lit_sym = lit->sym;
11313 segT dseg = symbol_get_frag (lit_sym)->tc_frag_data.lit_seg;
11314 if (dseg)
11315 S_SET_SEGMENT (lit_sym, dseg);
11316 }
11317 }
11318
11319
11320 /* Walk over all the frags for segments in a list and mark them as
11321 containing literals. As clunky as this is, we can't rely on frag_var
11322 and frag_variant to get called in all situations. */
11323
11324 static void
11325 mark_literal_frags (seg_list *segment)
11326 {
11327 frchainS *frchain_from;
11328 fragS *search_frag;
11329
11330 while (segment)
11331 {
11332 frchain_from = seg_info (segment->seg)->frchainP;
11333 search_frag = frchain_from->frch_root;
11334 while (search_frag)
11335 {
11336 search_frag->tc_frag_data.is_literal = TRUE;
11337 search_frag = search_frag->fr_next;
11338 }
11339 segment = segment->next;
11340 }
11341 }
11342
11343
11344 static void
11345 xtensa_reorder_seg_list (seg_list *head, segT after)
11346 {
11347 /* Move all of the sections in the section list to come
11348 after "after" in the gnu segment list. */
11349
11350 head = head->next;
11351 while (head)
11352 {
11353 segT literal_section = head->seg;
11354
11355 /* Move the literal section after "after". */
11356 gas_assert (literal_section);
11357 if (literal_section != after)
11358 {
11359 bfd_section_list_remove (stdoutput, literal_section);
11360 bfd_section_list_insert_after (stdoutput, after, literal_section);
11361 }
11362
11363 head = head->next;
11364 }
11365 }
11366
11367
11368 /* Push all the literal segments to the end of the gnu list. */
11369
11370 static void
11371 xtensa_reorder_segments (void)
11372 {
11373 segT sec;
11374 segT last_sec = 0;
11375 int old_count = 0;
11376 int new_count = 0;
11377
11378 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
11379 {
11380 last_sec = sec;
11381 old_count++;
11382 }
11383
11384 /* Now that we have the last section, push all the literal
11385 sections to the end. */
11386 xtensa_reorder_seg_list (literal_head, last_sec);
11387
11388 /* Now perform the final error check. */
11389 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
11390 new_count++;
11391 gas_assert (new_count == old_count);
11392 }
11393
11394
11395 /* Change the emit state (seg, subseg, and frag related stuff) to the
11396 correct location. Return a emit_state which can be passed to
11397 xtensa_restore_emit_state to return to current fragment. */
11398
11399 static void
11400 xtensa_switch_to_literal_fragment (emit_state *result)
11401 {
11402 if (directive_state[directive_absolute_literals])
11403 {
11404 segT lit4_seg = cache_literal_section (TRUE);
11405 xtensa_switch_section_emit_state (result, lit4_seg, 0);
11406 }
11407 else
11408 xtensa_switch_to_non_abs_literal_fragment (result);
11409
11410 /* Do a 4-byte align here. */
11411 frag_align (2, 0, 0);
11412 record_alignment (now_seg, 2);
11413 }
11414
11415
11416 static void
11417 xtensa_switch_to_non_abs_literal_fragment (emit_state *result)
11418 {
11419 static bfd_boolean recursive = FALSE;
11420 fragS *pool_location = get_literal_pool_location (now_seg);
11421 segT lit_seg;
11422 bfd_boolean is_init =
11423 (now_seg && !strcmp (segment_name (now_seg), INIT_SECTION_NAME));
11424 bfd_boolean is_fini =
11425 (now_seg && !strcmp (segment_name (now_seg), FINI_SECTION_NAME));
11426
11427 if (pool_location == NULL
11428 && !use_literal_section
11429 && !recursive
11430 && !is_init && ! is_fini)
11431 {
11432 if (!auto_litpools)
11433 {
11434 as_bad (_("literal pool location required for text-section-literals; specify with .literal_position"));
11435 }
11436
11437 /* When we mark a literal pool location, we want to put a frag in
11438 the literal pool that points to it. But to do that, we want to
11439 switch_to_literal_fragment. But literal sections don't have
11440 literal pools, so their location is always null, so we would
11441 recurse forever. This is kind of hacky, but it works. */
11442
11443 recursive = TRUE;
11444 xtensa_mark_literal_pool_location ();
11445 recursive = FALSE;
11446 }
11447
11448 lit_seg = cache_literal_section (FALSE);
11449 xtensa_switch_section_emit_state (result, lit_seg, 0);
11450
11451 if (!use_literal_section
11452 && !is_init && !is_fini
11453 && get_literal_pool_location (now_seg) != pool_location)
11454 {
11455 /* Close whatever frag is there. */
11456 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
11457 xtensa_set_frag_assembly_state (frag_now);
11458 frag_now->tc_frag_data.literal_frag = pool_location;
11459 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
11460 xtensa_set_frag_assembly_state (frag_now);
11461 }
11462 }
11463
11464
11465 /* Call this function before emitting data into the literal section.
11466 This is a helper function for xtensa_switch_to_literal_fragment.
11467 This is similar to a .section new_now_seg subseg. */
11468
11469 static void
11470 xtensa_switch_section_emit_state (emit_state *state,
11471 segT new_now_seg,
11472 subsegT new_now_subseg)
11473 {
11474 state->name = now_seg->name;
11475 state->now_seg = now_seg;
11476 state->now_subseg = now_subseg;
11477 state->generating_literals = generating_literals;
11478 generating_literals++;
11479 subseg_set (new_now_seg, new_now_subseg);
11480 }
11481
11482
11483 /* Use to restore the emitting into the normal place. */
11484
11485 static void
11486 xtensa_restore_emit_state (emit_state *state)
11487 {
11488 generating_literals = state->generating_literals;
11489 subseg_set (state->now_seg, state->now_subseg);
11490 }
11491
11492
11493 /* Predicate function used to look up a section in a particular group. */
11494
11495 static bfd_boolean
11496 match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
11497 {
11498 const char *gname = inf;
11499 const char *group_name = elf_group_name (sec);
11500
11501 return (group_name == gname
11502 || (group_name != NULL
11503 && gname != NULL
11504 && strcmp (group_name, gname) == 0));
11505 }
11506
11507
11508 /* Get the literal section to be used for the current text section.
11509 The result may be cached in the default_lit_sections structure. */
11510
11511 static segT
11512 cache_literal_section (bfd_boolean use_abs_literals)
11513 {
11514 const char *text_name, *group_name = 0;
11515 const char *base_name, *suffix;
11516 char *name;
11517 segT *pcached;
11518 segT seg, current_section;
11519 int current_subsec;
11520 bfd_boolean linkonce = FALSE;
11521
11522 /* Save the current section/subsection. */
11523 current_section = now_seg;
11524 current_subsec = now_subseg;
11525
11526 /* Clear the cached values if they are no longer valid. */
11527 if (now_seg != default_lit_sections.current_text_seg)
11528 {
11529 default_lit_sections.current_text_seg = now_seg;
11530 default_lit_sections.lit_seg = NULL;
11531 default_lit_sections.lit4_seg = NULL;
11532 }
11533
11534 /* Check if the literal section is already cached. */
11535 if (use_abs_literals)
11536 pcached = &default_lit_sections.lit4_seg;
11537 else
11538 pcached = &default_lit_sections.lit_seg;
11539
11540 if (*pcached)
11541 return *pcached;
11542
11543 text_name = default_lit_sections.lit_prefix;
11544 if (! text_name || ! *text_name)
11545 {
11546 text_name = segment_name (current_section);
11547 group_name = elf_group_name (current_section);
11548 linkonce = (current_section->flags & SEC_LINK_ONCE) != 0;
11549 }
11550
11551 base_name = use_abs_literals ? ".lit4" : ".literal";
11552 if (group_name)
11553 {
11554 name = concat (base_name, ".", group_name, (char *) NULL);
11555 }
11556 else if (strncmp (text_name, ".gnu.linkonce.", linkonce_len) == 0)
11557 {
11558 suffix = strchr (text_name + linkonce_len, '.');
11559
11560 name = concat (".gnu.linkonce", base_name, suffix ? suffix : "",
11561 (char *) NULL);
11562 linkonce = TRUE;
11563 }
11564 else
11565 {
11566 /* If the section name begins or ends with ".text", then replace
11567 that portion instead of appending an additional suffix. */
11568 size_t len = strlen (text_name);
11569 if (len >= 5
11570 && (strcmp (text_name + len - 5, ".text") == 0
11571 || strncmp (text_name, ".text", 5) == 0))
11572 len -= 5;
11573
11574 name = xmalloc (len + strlen (base_name) + 1);
11575 if (strncmp (text_name, ".text", 5) == 0)
11576 {
11577 strcpy (name, base_name);
11578 strcat (name, text_name + 5);
11579 }
11580 else
11581 {
11582 strcpy (name, text_name);
11583 strcpy (name + len, base_name);
11584 }
11585 }
11586
11587 /* Canonicalize section names to allow renaming literal sections.
11588 The group name, if any, came from the current text section and
11589 has already been canonicalized. */
11590 name = tc_canonicalize_symbol_name (name);
11591
11592 seg = bfd_get_section_by_name_if (stdoutput, name, match_section_group,
11593 (void *) group_name);
11594 if (! seg)
11595 {
11596 flagword flags;
11597
11598 seg = subseg_force_new (name, 0);
11599
11600 if (! use_abs_literals)
11601 {
11602 /* Add the newly created literal segment to the list. */
11603 seg_list *n = (seg_list *) xmalloc (sizeof (seg_list));
11604 n->seg = seg;
11605 n->next = literal_head->next;
11606 literal_head->next = n;
11607 }
11608
11609 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC | SEC_LOAD
11610 | (linkonce ? (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD) : 0)
11611 | (use_abs_literals ? SEC_DATA : SEC_CODE));
11612
11613 elf_group_name (seg) = group_name;
11614
11615 bfd_set_section_flags (stdoutput, seg, flags);
11616 bfd_set_section_alignment (stdoutput, seg, 2);
11617 }
11618
11619 *pcached = seg;
11620 subseg_set (current_section, current_subsec);
11621 return seg;
11622 }
11623
11624 \f
11625 /* Property Tables Stuff. */
11626
11627 #define XTENSA_INSN_SEC_NAME ".xt.insn"
11628 #define XTENSA_LIT_SEC_NAME ".xt.lit"
11629 #define XTENSA_PROP_SEC_NAME ".xt.prop"
11630
11631 typedef bfd_boolean (*frag_predicate) (const fragS *);
11632 typedef void (*frag_flags_fn) (const fragS *, frag_flags *);
11633
11634 static bfd_boolean get_frag_is_literal (const fragS *);
11635 static void xtensa_create_property_segments
11636 (frag_predicate, frag_predicate, const char *, xt_section_type);
11637 static void xtensa_create_xproperty_segments
11638 (frag_flags_fn, const char *, xt_section_type);
11639 static bfd_boolean exclude_section_from_property_tables (segT);
11640 static bfd_boolean section_has_property (segT, frag_predicate);
11641 static bfd_boolean section_has_xproperty (segT, frag_flags_fn);
11642 static void add_xt_block_frags
11643 (segT, xtensa_block_info **, frag_predicate, frag_predicate);
11644 static bfd_boolean xtensa_frag_flags_is_empty (const frag_flags *);
11645 static void xtensa_frag_flags_init (frag_flags *);
11646 static void get_frag_property_flags (const fragS *, frag_flags *);
11647 static flagword frag_flags_to_number (const frag_flags *);
11648 static void add_xt_prop_frags (segT, xtensa_block_info **, frag_flags_fn);
11649
11650 /* Set up property tables after relaxation. */
11651
11652 void
11653 xtensa_post_relax_hook (void)
11654 {
11655 xtensa_move_seg_list_to_beginning (literal_head);
11656
11657 xtensa_find_unmarked_state_frags ();
11658 xtensa_mark_frags_for_org ();
11659 xtensa_mark_difference_of_two_symbols ();
11660
11661 xtensa_create_property_segments (get_frag_is_literal,
11662 NULL,
11663 XTENSA_LIT_SEC_NAME,
11664 xt_literal_sec);
11665 xtensa_create_xproperty_segments (get_frag_property_flags,
11666 XTENSA_PROP_SEC_NAME,
11667 xt_prop_sec);
11668
11669 if (warn_unaligned_branch_targets)
11670 bfd_map_over_sections (stdoutput, xtensa_find_unaligned_branch_targets, 0);
11671 bfd_map_over_sections (stdoutput, xtensa_find_unaligned_loops, 0);
11672 }
11673
11674
11675 /* This function is only meaningful after xtensa_move_literals. */
11676
11677 static bfd_boolean
11678 get_frag_is_literal (const fragS *fragP)
11679 {
11680 gas_assert (fragP != NULL);
11681 return fragP->tc_frag_data.is_literal;
11682 }
11683
11684
11685 static void
11686 xtensa_create_property_segments (frag_predicate property_function,
11687 frag_predicate end_property_function,
11688 const char *section_name_base,
11689 xt_section_type sec_type)
11690 {
11691 segT *seclist;
11692
11693 /* Walk over all of the current segments.
11694 Walk over each fragment
11695 For each non-empty fragment,
11696 Build a property record (append where possible). */
11697
11698 for (seclist = &stdoutput->sections;
11699 seclist && *seclist;
11700 seclist = &(*seclist)->next)
11701 {
11702 segT sec = *seclist;
11703
11704 if (exclude_section_from_property_tables (sec))
11705 continue;
11706
11707 if (section_has_property (sec, property_function))
11708 {
11709 segment_info_type *xt_seg_info;
11710 xtensa_block_info **xt_blocks;
11711 segT prop_sec = xtensa_make_property_section (sec, section_name_base);
11712
11713 prop_sec->output_section = prop_sec;
11714 subseg_set (prop_sec, 0);
11715 xt_seg_info = seg_info (prop_sec);
11716 xt_blocks = &xt_seg_info->tc_segment_info_data.blocks[sec_type];
11717
11718 /* Walk over all of the frchains here and add new sections. */
11719 add_xt_block_frags (sec, xt_blocks, property_function,
11720 end_property_function);
11721 }
11722 }
11723
11724 /* Now we fill them out.... */
11725
11726 for (seclist = &stdoutput->sections;
11727 seclist && *seclist;
11728 seclist = &(*seclist)->next)
11729 {
11730 segment_info_type *seginfo;
11731 xtensa_block_info *block;
11732 segT sec = *seclist;
11733
11734 seginfo = seg_info (sec);
11735 block = seginfo->tc_segment_info_data.blocks[sec_type];
11736
11737 if (block)
11738 {
11739 xtensa_block_info *cur_block;
11740 int num_recs = 0;
11741 bfd_size_type rec_size;
11742
11743 for (cur_block = block; cur_block; cur_block = cur_block->next)
11744 num_recs++;
11745
11746 rec_size = num_recs * 8;
11747 bfd_set_section_size (stdoutput, sec, rec_size);
11748
11749 if (num_recs)
11750 {
11751 char *frag_data;
11752 int i;
11753
11754 subseg_set (sec, 0);
11755 frag_data = frag_more (rec_size);
11756 cur_block = block;
11757 for (i = 0; i < num_recs; i++)
11758 {
11759 fixS *fix;
11760
11761 /* Write the fixup. */
11762 gas_assert (cur_block);
11763 fix = fix_new (frag_now, i * 8, 4,
11764 section_symbol (cur_block->sec),
11765 cur_block->offset,
11766 FALSE, BFD_RELOC_32);
11767 fix->fx_file = "<internal>";
11768 fix->fx_line = 0;
11769
11770 /* Write the length. */
11771 md_number_to_chars (&frag_data[4 + i * 8],
11772 cur_block->size, 4);
11773 cur_block = cur_block->next;
11774 }
11775 frag_wane (frag_now);
11776 frag_new (0);
11777 frag_wane (frag_now);
11778 }
11779 }
11780 }
11781 }
11782
11783
11784 static void
11785 xtensa_create_xproperty_segments (frag_flags_fn flag_fn,
11786 const char *section_name_base,
11787 xt_section_type sec_type)
11788 {
11789 segT *seclist;
11790
11791 /* Walk over all of the current segments.
11792 Walk over each fragment.
11793 For each fragment that has instructions,
11794 build an instruction record (append where possible). */
11795
11796 for (seclist = &stdoutput->sections;
11797 seclist && *seclist;
11798 seclist = &(*seclist)->next)
11799 {
11800 segT sec = *seclist;
11801
11802 if (exclude_section_from_property_tables (sec))
11803 continue;
11804
11805 if (section_has_xproperty (sec, flag_fn))
11806 {
11807 segment_info_type *xt_seg_info;
11808 xtensa_block_info **xt_blocks;
11809 segT prop_sec = xtensa_make_property_section (sec, section_name_base);
11810
11811 prop_sec->output_section = prop_sec;
11812 subseg_set (prop_sec, 0);
11813 xt_seg_info = seg_info (prop_sec);
11814 xt_blocks = &xt_seg_info->tc_segment_info_data.blocks[sec_type];
11815
11816 /* Walk over all of the frchains here and add new sections. */
11817 add_xt_prop_frags (sec, xt_blocks, flag_fn);
11818 }
11819 }
11820
11821 /* Now we fill them out.... */
11822
11823 for (seclist = &stdoutput->sections;
11824 seclist && *seclist;
11825 seclist = &(*seclist)->next)
11826 {
11827 segment_info_type *seginfo;
11828 xtensa_block_info *block;
11829 segT sec = *seclist;
11830
11831 seginfo = seg_info (sec);
11832 block = seginfo->tc_segment_info_data.blocks[sec_type];
11833
11834 if (block)
11835 {
11836 xtensa_block_info *cur_block;
11837 int num_recs = 0;
11838 bfd_size_type rec_size;
11839
11840 for (cur_block = block; cur_block; cur_block = cur_block->next)
11841 num_recs++;
11842
11843 rec_size = num_recs * (8 + 4);
11844 bfd_set_section_size (stdoutput, sec, rec_size);
11845 /* elf_section_data (sec)->this_hdr.sh_entsize = 12; */
11846
11847 if (num_recs)
11848 {
11849 char *frag_data;
11850 int i;
11851
11852 subseg_set (sec, 0);
11853 frag_data = frag_more (rec_size);
11854 cur_block = block;
11855 for (i = 0; i < num_recs; i++)
11856 {
11857 fixS *fix;
11858
11859 /* Write the fixup. */
11860 gas_assert (cur_block);
11861 fix = fix_new (frag_now, i * 12, 4,
11862 section_symbol (cur_block->sec),
11863 cur_block->offset,
11864 FALSE, BFD_RELOC_32);
11865 fix->fx_file = "<internal>";
11866 fix->fx_line = 0;
11867
11868 /* Write the length. */
11869 md_number_to_chars (&frag_data[4 + i * 12],
11870 cur_block->size, 4);
11871 md_number_to_chars (&frag_data[8 + i * 12],
11872 frag_flags_to_number (&cur_block->flags),
11873 sizeof (flagword));
11874 cur_block = cur_block->next;
11875 }
11876 frag_wane (frag_now);
11877 frag_new (0);
11878 frag_wane (frag_now);
11879 }
11880 }
11881 }
11882 }
11883
11884
11885 static bfd_boolean
11886 exclude_section_from_property_tables (segT sec)
11887 {
11888 flagword flags = bfd_get_section_flags (stdoutput, sec);
11889
11890 /* Sections that don't contribute to the memory footprint are excluded. */
11891 if ((flags & SEC_DEBUGGING)
11892 || !(flags & SEC_ALLOC)
11893 || (flags & SEC_MERGE))
11894 return TRUE;
11895
11896 /* Linker cie and fde optimizations mess up property entries for
11897 eh_frame sections, but there is nothing inside them relevant to
11898 property tables anyway. */
11899 if (strcmp (sec->name, ".eh_frame") == 0)
11900 return TRUE;
11901
11902 return FALSE;
11903 }
11904
11905
11906 static bfd_boolean
11907 section_has_property (segT sec, frag_predicate property_function)
11908 {
11909 segment_info_type *seginfo = seg_info (sec);
11910 fragS *fragP;
11911
11912 if (seginfo && seginfo->frchainP)
11913 {
11914 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
11915 {
11916 if (property_function (fragP)
11917 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
11918 return TRUE;
11919 }
11920 }
11921 return FALSE;
11922 }
11923
11924
11925 static bfd_boolean
11926 section_has_xproperty (segT sec, frag_flags_fn property_function)
11927 {
11928 segment_info_type *seginfo = seg_info (sec);
11929 fragS *fragP;
11930
11931 if (seginfo && seginfo->frchainP)
11932 {
11933 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
11934 {
11935 frag_flags prop_flags;
11936 property_function (fragP, &prop_flags);
11937 if (!xtensa_frag_flags_is_empty (&prop_flags))
11938 return TRUE;
11939 }
11940 }
11941 return FALSE;
11942 }
11943
11944
11945 /* Two types of block sections exist right now: literal and insns. */
11946
11947 static void
11948 add_xt_block_frags (segT sec,
11949 xtensa_block_info **xt_block,
11950 frag_predicate property_function,
11951 frag_predicate end_property_function)
11952 {
11953 fragS *fragP;
11954
11955 /* Build it if needed. */
11956 while (*xt_block != NULL)
11957 xt_block = &(*xt_block)->next;
11958 /* We are either at NULL at the beginning or at the end. */
11959
11960 /* Walk through the frags. */
11961 if (seg_info (sec)->frchainP)
11962 {
11963 for (fragP = seg_info (sec)->frchainP->frch_root;
11964 fragP;
11965 fragP = fragP->fr_next)
11966 {
11967 if (property_function (fragP)
11968 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
11969 {
11970 if (*xt_block != NULL)
11971 {
11972 if ((*xt_block)->offset + (*xt_block)->size
11973 == fragP->fr_address)
11974 (*xt_block)->size += fragP->fr_fix;
11975 else
11976 xt_block = &((*xt_block)->next);
11977 }
11978 if (*xt_block == NULL)
11979 {
11980 xtensa_block_info *new_block = (xtensa_block_info *)
11981 xmalloc (sizeof (xtensa_block_info));
11982 new_block->sec = sec;
11983 new_block->offset = fragP->fr_address;
11984 new_block->size = fragP->fr_fix;
11985 new_block->next = NULL;
11986 xtensa_frag_flags_init (&new_block->flags);
11987 *xt_block = new_block;
11988 }
11989 if (end_property_function
11990 && end_property_function (fragP))
11991 {
11992 xt_block = &((*xt_block)->next);
11993 }
11994 }
11995 }
11996 }
11997 }
11998
11999
12000 /* Break the encapsulation of add_xt_prop_frags here. */
12001
12002 static bfd_boolean
12003 xtensa_frag_flags_is_empty (const frag_flags *prop_flags)
12004 {
12005 if (prop_flags->is_literal
12006 || prop_flags->is_insn
12007 || prop_flags->is_data
12008 || prop_flags->is_unreachable)
12009 return FALSE;
12010 return TRUE;
12011 }
12012
12013
12014 static void
12015 xtensa_frag_flags_init (frag_flags *prop_flags)
12016 {
12017 memset (prop_flags, 0, sizeof (frag_flags));
12018 }
12019
12020
12021 static void
12022 get_frag_property_flags (const fragS *fragP, frag_flags *prop_flags)
12023 {
12024 xtensa_frag_flags_init (prop_flags);
12025 if (fragP->tc_frag_data.is_literal)
12026 prop_flags->is_literal = TRUE;
12027 if (fragP->tc_frag_data.is_specific_opcode
12028 || fragP->tc_frag_data.is_no_transform)
12029 {
12030 prop_flags->is_no_transform = TRUE;
12031 if (xtensa_frag_flags_is_empty (prop_flags))
12032 prop_flags->is_data = TRUE;
12033 }
12034 if (fragP->tc_frag_data.is_unreachable)
12035 prop_flags->is_unreachable = TRUE;
12036 else if (fragP->tc_frag_data.is_insn)
12037 {
12038 prop_flags->is_insn = TRUE;
12039 if (fragP->tc_frag_data.is_loop_target)
12040 prop_flags->insn.is_loop_target = TRUE;
12041 if (fragP->tc_frag_data.is_branch_target)
12042 prop_flags->insn.is_branch_target = TRUE;
12043 if (fragP->tc_frag_data.is_no_density)
12044 prop_flags->insn.is_no_density = TRUE;
12045 if (fragP->tc_frag_data.use_absolute_literals)
12046 prop_flags->insn.is_abslit = TRUE;
12047 }
12048 if (fragP->tc_frag_data.is_align)
12049 {
12050 prop_flags->is_align = TRUE;
12051 prop_flags->alignment = fragP->tc_frag_data.alignment;
12052 if (xtensa_frag_flags_is_empty (prop_flags))
12053 prop_flags->is_data = TRUE;
12054 }
12055 }
12056
12057
12058 static flagword
12059 frag_flags_to_number (const frag_flags *prop_flags)
12060 {
12061 flagword num = 0;
12062 if (prop_flags->is_literal)
12063 num |= XTENSA_PROP_LITERAL;
12064 if (prop_flags->is_insn)
12065 num |= XTENSA_PROP_INSN;
12066 if (prop_flags->is_data)
12067 num |= XTENSA_PROP_DATA;
12068 if (prop_flags->is_unreachable)
12069 num |= XTENSA_PROP_UNREACHABLE;
12070 if (prop_flags->insn.is_loop_target)
12071 num |= XTENSA_PROP_INSN_LOOP_TARGET;
12072 if (prop_flags->insn.is_branch_target)
12073 {
12074 num |= XTENSA_PROP_INSN_BRANCH_TARGET;
12075 num = SET_XTENSA_PROP_BT_ALIGN (num, prop_flags->insn.bt_align_priority);
12076 }
12077
12078 if (prop_flags->insn.is_no_density)
12079 num |= XTENSA_PROP_INSN_NO_DENSITY;
12080 if (prop_flags->is_no_transform)
12081 num |= XTENSA_PROP_NO_TRANSFORM;
12082 if (prop_flags->insn.is_no_reorder)
12083 num |= XTENSA_PROP_INSN_NO_REORDER;
12084 if (prop_flags->insn.is_abslit)
12085 num |= XTENSA_PROP_INSN_ABSLIT;
12086
12087 if (prop_flags->is_align)
12088 {
12089 num |= XTENSA_PROP_ALIGN;
12090 num = SET_XTENSA_PROP_ALIGNMENT (num, prop_flags->alignment);
12091 }
12092
12093 return num;
12094 }
12095
12096
12097 static bfd_boolean
12098 xtensa_frag_flags_combinable (const frag_flags *prop_flags_1,
12099 const frag_flags *prop_flags_2)
12100 {
12101 /* Cannot combine with an end marker. */
12102
12103 if (prop_flags_1->is_literal != prop_flags_2->is_literal)
12104 return FALSE;
12105 if (prop_flags_1->is_insn != prop_flags_2->is_insn)
12106 return FALSE;
12107 if (prop_flags_1->is_data != prop_flags_2->is_data)
12108 return FALSE;
12109
12110 if (prop_flags_1->is_insn)
12111 {
12112 /* Properties of the beginning of the frag. */
12113 if (prop_flags_2->insn.is_loop_target)
12114 return FALSE;
12115 if (prop_flags_2->insn.is_branch_target)
12116 return FALSE;
12117 if (prop_flags_1->insn.is_no_density !=
12118 prop_flags_2->insn.is_no_density)
12119 return FALSE;
12120 if (prop_flags_1->is_no_transform !=
12121 prop_flags_2->is_no_transform)
12122 return FALSE;
12123 if (prop_flags_1->insn.is_no_reorder !=
12124 prop_flags_2->insn.is_no_reorder)
12125 return FALSE;
12126 if (prop_flags_1->insn.is_abslit !=
12127 prop_flags_2->insn.is_abslit)
12128 return FALSE;
12129 }
12130
12131 if (prop_flags_1->is_align)
12132 return FALSE;
12133
12134 return TRUE;
12135 }
12136
12137
12138 static bfd_vma
12139 xt_block_aligned_size (const xtensa_block_info *xt_block)
12140 {
12141 bfd_vma end_addr;
12142 unsigned align_bits;
12143
12144 if (!xt_block->flags.is_align)
12145 return xt_block->size;
12146
12147 end_addr = xt_block->offset + xt_block->size;
12148 align_bits = xt_block->flags.alignment;
12149 end_addr = ((end_addr + ((1 << align_bits) -1)) >> align_bits) << align_bits;
12150 return end_addr - xt_block->offset;
12151 }
12152
12153
12154 static bfd_boolean
12155 xtensa_xt_block_combine (xtensa_block_info *xt_block,
12156 const xtensa_block_info *xt_block_2)
12157 {
12158 if (xt_block->sec != xt_block_2->sec)
12159 return FALSE;
12160 if (xt_block->offset + xt_block_aligned_size (xt_block)
12161 != xt_block_2->offset)
12162 return FALSE;
12163
12164 if (xt_block_2->size == 0
12165 && (!xt_block_2->flags.is_unreachable
12166 || xt_block->flags.is_unreachable))
12167 {
12168 if (xt_block_2->flags.is_align
12169 && xt_block->flags.is_align)
12170 {
12171 /* Nothing needed. */
12172 if (xt_block->flags.alignment >= xt_block_2->flags.alignment)
12173 return TRUE;
12174 }
12175 else
12176 {
12177 if (xt_block_2->flags.is_align)
12178 {
12179 /* Push alignment to previous entry. */
12180 xt_block->flags.is_align = xt_block_2->flags.is_align;
12181 xt_block->flags.alignment = xt_block_2->flags.alignment;
12182 }
12183 return TRUE;
12184 }
12185 }
12186 if (!xtensa_frag_flags_combinable (&xt_block->flags,
12187 &xt_block_2->flags))
12188 return FALSE;
12189
12190 xt_block->size += xt_block_2->size;
12191
12192 if (xt_block_2->flags.is_align)
12193 {
12194 xt_block->flags.is_align = TRUE;
12195 xt_block->flags.alignment = xt_block_2->flags.alignment;
12196 }
12197
12198 return TRUE;
12199 }
12200
12201
12202 static void
12203 add_xt_prop_frags (segT sec,
12204 xtensa_block_info **xt_block,
12205 frag_flags_fn property_function)
12206 {
12207 fragS *fragP;
12208
12209 /* Build it if needed. */
12210 while (*xt_block != NULL)
12211 {
12212 xt_block = &(*xt_block)->next;
12213 }
12214 /* We are either at NULL at the beginning or at the end. */
12215
12216 /* Walk through the frags. */
12217 if (seg_info (sec)->frchainP)
12218 {
12219 for (fragP = seg_info (sec)->frchainP->frch_root; fragP;
12220 fragP = fragP->fr_next)
12221 {
12222 xtensa_block_info tmp_block;
12223 tmp_block.sec = sec;
12224 tmp_block.offset = fragP->fr_address;
12225 tmp_block.size = fragP->fr_fix;
12226 tmp_block.next = NULL;
12227 property_function (fragP, &tmp_block.flags);
12228
12229 if (!xtensa_frag_flags_is_empty (&tmp_block.flags))
12230 /* && fragP->fr_fix != 0) */
12231 {
12232 if ((*xt_block) == NULL
12233 || !xtensa_xt_block_combine (*xt_block, &tmp_block))
12234 {
12235 xtensa_block_info *new_block;
12236 if ((*xt_block) != NULL)
12237 xt_block = &(*xt_block)->next;
12238 new_block = (xtensa_block_info *)
12239 xmalloc (sizeof (xtensa_block_info));
12240 *new_block = tmp_block;
12241 *xt_block = new_block;
12242 }
12243 }
12244 }
12245 }
12246 }
12247
12248 \f
12249 /* op_placement_info_table */
12250
12251 /* op_placement_info makes it easier to determine which
12252 ops can go in which slots. */
12253
12254 static void
12255 init_op_placement_info_table (void)
12256 {
12257 xtensa_isa isa = xtensa_default_isa;
12258 xtensa_insnbuf ibuf = xtensa_insnbuf_alloc (isa);
12259 xtensa_opcode opcode;
12260 xtensa_format fmt;
12261 int slot;
12262 int num_opcodes = xtensa_isa_num_opcodes (isa);
12263
12264 op_placement_table = (op_placement_info_table)
12265 xmalloc (sizeof (op_placement_info) * num_opcodes);
12266 gas_assert (xtensa_isa_num_formats (isa) < MAX_FORMATS);
12267
12268 for (opcode = 0; opcode < num_opcodes; opcode++)
12269 {
12270 op_placement_info *opi = &op_placement_table[opcode];
12271 /* FIXME: Make tinsn allocation dynamic. */
12272 if (xtensa_opcode_num_operands (isa, opcode) > MAX_INSN_ARGS)
12273 as_fatal (_("too many operands in instruction"));
12274 opi->narrowest = XTENSA_UNDEFINED;
12275 opi->narrowest_size = 0x7F;
12276 opi->narrowest_slot = 0;
12277 opi->formats = 0;
12278 opi->num_formats = 0;
12279 opi->issuef = 0;
12280 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
12281 {
12282 opi->slots[fmt] = 0;
12283 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
12284 {
12285 if (xtensa_opcode_encode (isa, fmt, slot, ibuf, opcode) == 0)
12286 {
12287 int fmt_length = xtensa_format_length (isa, fmt);
12288 opi->issuef++;
12289 set_bit (fmt, opi->formats);
12290 set_bit (slot, opi->slots[fmt]);
12291 if (fmt_length < opi->narrowest_size
12292 || (fmt_length == opi->narrowest_size
12293 && (xtensa_format_num_slots (isa, fmt)
12294 < xtensa_format_num_slots (isa,
12295 opi->narrowest))))
12296 {
12297 opi->narrowest = fmt;
12298 opi->narrowest_size = fmt_length;
12299 opi->narrowest_slot = slot;
12300 }
12301 }
12302 }
12303 if (opi->formats)
12304 opi->num_formats++;
12305 }
12306 }
12307 xtensa_insnbuf_free (isa, ibuf);
12308 }
12309
12310
12311 bfd_boolean
12312 opcode_fits_format_slot (xtensa_opcode opcode, xtensa_format fmt, int slot)
12313 {
12314 return bit_is_set (slot, op_placement_table[opcode].slots[fmt]);
12315 }
12316
12317
12318 /* If the opcode is available in a single slot format, return its size. */
12319
12320 static int
12321 xg_get_single_size (xtensa_opcode opcode)
12322 {
12323 return op_placement_table[opcode].narrowest_size;
12324 }
12325
12326
12327 static xtensa_format
12328 xg_get_single_format (xtensa_opcode opcode)
12329 {
12330 return op_placement_table[opcode].narrowest;
12331 }
12332
12333
12334 static int
12335 xg_get_single_slot (xtensa_opcode opcode)
12336 {
12337 return op_placement_table[opcode].narrowest_slot;
12338 }
12339
12340 \f
12341 /* Instruction Stack Functions (from "xtensa-istack.h"). */
12342
12343 void
12344 istack_init (IStack *stack)
12345 {
12346 stack->ninsn = 0;
12347 }
12348
12349
12350 bfd_boolean
12351 istack_empty (IStack *stack)
12352 {
12353 return (stack->ninsn == 0);
12354 }
12355
12356
12357 bfd_boolean
12358 istack_full (IStack *stack)
12359 {
12360 return (stack->ninsn == MAX_ISTACK);
12361 }
12362
12363
12364 /* Return a pointer to the top IStack entry.
12365 It is an error to call this if istack_empty () is TRUE. */
12366
12367 TInsn *
12368 istack_top (IStack *stack)
12369 {
12370 int rec = stack->ninsn - 1;
12371 gas_assert (!istack_empty (stack));
12372 return &stack->insn[rec];
12373 }
12374
12375
12376 /* Add a new TInsn to an IStack.
12377 It is an error to call this if istack_full () is TRUE. */
12378
12379 void
12380 istack_push (IStack *stack, TInsn *insn)
12381 {
12382 int rec = stack->ninsn;
12383 gas_assert (!istack_full (stack));
12384 stack->insn[rec] = *insn;
12385 stack->ninsn++;
12386 }
12387
12388
12389 /* Clear space for the next TInsn on the IStack and return a pointer
12390 to it. It is an error to call this if istack_full () is TRUE. */
12391
12392 TInsn *
12393 istack_push_space (IStack *stack)
12394 {
12395 int rec = stack->ninsn;
12396 TInsn *insn;
12397 gas_assert (!istack_full (stack));
12398 insn = &stack->insn[rec];
12399 tinsn_init (insn);
12400 stack->ninsn++;
12401 return insn;
12402 }
12403
12404
12405 /* Remove the last pushed instruction. It is an error to call this if
12406 istack_empty () returns TRUE. */
12407
12408 void
12409 istack_pop (IStack *stack)
12410 {
12411 int rec = stack->ninsn - 1;
12412 gas_assert (!istack_empty (stack));
12413 stack->ninsn--;
12414 tinsn_init (&stack->insn[rec]);
12415 }
12416
12417 \f
12418 /* TInsn functions. */
12419
12420 void
12421 tinsn_init (TInsn *dst)
12422 {
12423 memset (dst, 0, sizeof (TInsn));
12424 }
12425
12426
12427 /* Return TRUE if ANY of the operands in the insn are symbolic. */
12428
12429 static bfd_boolean
12430 tinsn_has_symbolic_operands (const TInsn *insn)
12431 {
12432 int i;
12433 int n = insn->ntok;
12434
12435 gas_assert (insn->insn_type == ITYPE_INSN);
12436
12437 for (i = 0; i < n; ++i)
12438 {
12439 switch (insn->tok[i].X_op)
12440 {
12441 case O_register:
12442 case O_constant:
12443 break;
12444 default:
12445 return TRUE;
12446 }
12447 }
12448 return FALSE;
12449 }
12450
12451
12452 bfd_boolean
12453 tinsn_has_invalid_symbolic_operands (const TInsn *insn)
12454 {
12455 xtensa_isa isa = xtensa_default_isa;
12456 int i;
12457 int n = insn->ntok;
12458
12459 gas_assert (insn->insn_type == ITYPE_INSN);
12460
12461 for (i = 0; i < n; ++i)
12462 {
12463 switch (insn->tok[i].X_op)
12464 {
12465 case O_register:
12466 case O_constant:
12467 break;
12468 case O_big:
12469 case O_illegal:
12470 case O_absent:
12471 /* Errors for these types are caught later. */
12472 break;
12473 case O_hi16:
12474 case O_lo16:
12475 default:
12476 /* Symbolic immediates are only allowed on the last immediate
12477 operand. At this time, CONST16 is the only opcode where we
12478 support non-PC-relative relocations. */
12479 if (i != get_relaxable_immed (insn->opcode)
12480 || (xtensa_operand_is_PCrelative (isa, insn->opcode, i) != 1
12481 && insn->opcode != xtensa_const16_opcode))
12482 {
12483 as_bad (_("invalid symbolic operand"));
12484 return TRUE;
12485 }
12486 }
12487 }
12488 return FALSE;
12489 }
12490
12491
12492 /* For assembly code with complex expressions (e.g. subtraction),
12493 we have to build them in the literal pool so that
12494 their results are calculated correctly after relaxation.
12495 The relaxation only handles expressions that
12496 boil down to SYMBOL + OFFSET. */
12497
12498 static bfd_boolean
12499 tinsn_has_complex_operands (const TInsn *insn)
12500 {
12501 int i;
12502 int n = insn->ntok;
12503 gas_assert (insn->insn_type == ITYPE_INSN);
12504 for (i = 0; i < n; ++i)
12505 {
12506 switch (insn->tok[i].X_op)
12507 {
12508 case O_register:
12509 case O_constant:
12510 case O_symbol:
12511 case O_lo16:
12512 case O_hi16:
12513 break;
12514 default:
12515 return TRUE;
12516 }
12517 }
12518 return FALSE;
12519 }
12520
12521
12522 /* Encode a TInsn opcode and its constant operands into slotbuf.
12523 Return TRUE if there is a symbol in the immediate field. This
12524 function assumes that:
12525 1) The number of operands are correct.
12526 2) The insn_type is ITYPE_INSN.
12527 3) The opcode can be encoded in the specified format and slot.
12528 4) Operands are either O_constant or O_symbol, and all constants fit. */
12529
12530 static bfd_boolean
12531 tinsn_to_slotbuf (xtensa_format fmt,
12532 int slot,
12533 TInsn *tinsn,
12534 xtensa_insnbuf slotbuf)
12535 {
12536 xtensa_isa isa = xtensa_default_isa;
12537 xtensa_opcode opcode = tinsn->opcode;
12538 bfd_boolean has_fixup = FALSE;
12539 int noperands = xtensa_opcode_num_operands (isa, opcode);
12540 int i;
12541
12542 gas_assert (tinsn->insn_type == ITYPE_INSN);
12543 if (noperands != tinsn->ntok)
12544 as_fatal (_("operand number mismatch"));
12545
12546 if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opcode))
12547 {
12548 as_bad (_("cannot encode opcode \"%s\" in the given format \"%s\""),
12549 xtensa_opcode_name (isa, opcode), xtensa_format_name (isa, fmt));
12550 return FALSE;
12551 }
12552
12553 for (i = 0; i < noperands; i++)
12554 {
12555 expressionS *exp = &tinsn->tok[i];
12556 int rc;
12557 unsigned line;
12558 const char *file_name;
12559 uint32 opnd_value;
12560
12561 switch (exp->X_op)
12562 {
12563 case O_register:
12564 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
12565 break;
12566 /* The register number has already been checked in
12567 expression_maybe_register, so we don't need to check here. */
12568 opnd_value = exp->X_add_number;
12569 (void) xtensa_operand_encode (isa, opcode, i, &opnd_value);
12570 rc = xtensa_operand_set_field (isa, opcode, i, fmt, slot, slotbuf,
12571 opnd_value);
12572 if (rc != 0)
12573 as_warn (_("xtensa-isa failure: %s"), xtensa_isa_error_msg (isa));
12574 break;
12575
12576 case O_constant:
12577 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
12578 break;
12579 file_name = as_where (&line);
12580 /* It is a constant and we called this function
12581 then we have to try to fit it. */
12582 xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode, i,
12583 exp->X_add_number, file_name, line);
12584 break;
12585
12586 default:
12587 has_fixup = TRUE;
12588 break;
12589 }
12590 }
12591
12592 return has_fixup;
12593 }
12594
12595
12596 /* Encode a single TInsn into an insnbuf. If the opcode can only be encoded
12597 into a multi-slot instruction, fill the other slots with NOPs.
12598 Return TRUE if there is a symbol in the immediate field. See also the
12599 assumptions listed for tinsn_to_slotbuf. */
12600
12601 static bfd_boolean
12602 tinsn_to_insnbuf (TInsn *tinsn, xtensa_insnbuf insnbuf)
12603 {
12604 static xtensa_insnbuf slotbuf = 0;
12605 static vliw_insn vinsn;
12606 xtensa_isa isa = xtensa_default_isa;
12607 bfd_boolean has_fixup = FALSE;
12608 int i;
12609
12610 if (!slotbuf)
12611 {
12612 slotbuf = xtensa_insnbuf_alloc (isa);
12613 xg_init_vinsn (&vinsn);
12614 }
12615
12616 xg_clear_vinsn (&vinsn);
12617
12618 bundle_tinsn (tinsn, &vinsn);
12619
12620 xtensa_format_encode (isa, vinsn.format, insnbuf);
12621
12622 for (i = 0; i < vinsn.num_slots; i++)
12623 {
12624 /* Only one slot may have a fix-up because the rest contains NOPs. */
12625 has_fixup |=
12626 tinsn_to_slotbuf (vinsn.format, i, &vinsn.slots[i], vinsn.slotbuf[i]);
12627 xtensa_format_set_slot (isa, vinsn.format, i, insnbuf, vinsn.slotbuf[i]);
12628 }
12629
12630 return has_fixup;
12631 }
12632
12633
12634 /* Check the instruction arguments. Return TRUE on failure. */
12635
12636 static bfd_boolean
12637 tinsn_check_arguments (const TInsn *insn)
12638 {
12639 xtensa_isa isa = xtensa_default_isa;
12640 xtensa_opcode opcode = insn->opcode;
12641 xtensa_regfile t1_regfile, t2_regfile;
12642 int t1_reg, t2_reg;
12643 int t1_base_reg, t1_last_reg;
12644 int t2_base_reg, t2_last_reg;
12645 char t1_inout, t2_inout;
12646 int i, j;
12647
12648 if (opcode == XTENSA_UNDEFINED)
12649 {
12650 as_bad (_("invalid opcode"));
12651 return TRUE;
12652 }
12653
12654 if (xtensa_opcode_num_operands (isa, opcode) > insn->ntok)
12655 {
12656 as_bad (_("too few operands"));
12657 return TRUE;
12658 }
12659
12660 if (xtensa_opcode_num_operands (isa, opcode) < insn->ntok)
12661 {
12662 as_bad (_("too many operands"));
12663 return TRUE;
12664 }
12665
12666 /* Check registers. */
12667 for (j = 0; j < insn->ntok; j++)
12668 {
12669 if (xtensa_operand_is_register (isa, insn->opcode, j) != 1)
12670 continue;
12671
12672 t2_regfile = xtensa_operand_regfile (isa, insn->opcode, j);
12673 t2_base_reg = insn->tok[j].X_add_number;
12674 t2_last_reg
12675 = t2_base_reg + xtensa_operand_num_regs (isa, insn->opcode, j);
12676
12677 for (i = 0; i < insn->ntok; i++)
12678 {
12679 if (i == j)
12680 continue;
12681
12682 if (xtensa_operand_is_register (isa, insn->opcode, i) != 1)
12683 continue;
12684
12685 t1_regfile = xtensa_operand_regfile (isa, insn->opcode, i);
12686
12687 if (t1_regfile != t2_regfile)
12688 continue;
12689
12690 t1_inout = xtensa_operand_inout (isa, insn->opcode, i);
12691 t2_inout = xtensa_operand_inout (isa, insn->opcode, j);
12692
12693 t1_base_reg = insn->tok[i].X_add_number;
12694 t1_last_reg = (t1_base_reg
12695 + xtensa_operand_num_regs (isa, insn->opcode, i));
12696
12697 for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++)
12698 {
12699 for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++)
12700 {
12701 if (t1_reg != t2_reg)
12702 continue;
12703
12704 if (t1_inout != 'i' && t2_inout != 'i')
12705 {
12706 as_bad (_("multiple writes to the same register"));
12707 return TRUE;
12708 }
12709 }
12710 }
12711 }
12712 }
12713 return FALSE;
12714 }
12715
12716
12717 /* Load an instruction from its encoded form. */
12718
12719 static void
12720 tinsn_from_chars (TInsn *tinsn, char *f, int slot)
12721 {
12722 vliw_insn vinsn;
12723
12724 xg_init_vinsn (&vinsn);
12725 vinsn_from_chars (&vinsn, f);
12726
12727 *tinsn = vinsn.slots[slot];
12728 xg_free_vinsn (&vinsn);
12729 }
12730
12731
12732 static void
12733 tinsn_from_insnbuf (TInsn *tinsn,
12734 xtensa_insnbuf slotbuf,
12735 xtensa_format fmt,
12736 int slot)
12737 {
12738 int i;
12739 xtensa_isa isa = xtensa_default_isa;
12740
12741 /* Find the immed. */
12742 tinsn_init (tinsn);
12743 tinsn->insn_type = ITYPE_INSN;
12744 tinsn->is_specific_opcode = FALSE; /* must not be specific */
12745 tinsn->opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
12746 tinsn->ntok = xtensa_opcode_num_operands (isa, tinsn->opcode);
12747 for (i = 0; i < tinsn->ntok; i++)
12748 {
12749 set_expr_const (&tinsn->tok[i],
12750 xtensa_insnbuf_get_operand (slotbuf, fmt, slot,
12751 tinsn->opcode, i));
12752 }
12753 }
12754
12755
12756 /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */
12757
12758 static void
12759 tinsn_immed_from_frag (TInsn *tinsn, fragS *fragP, int slot)
12760 {
12761 xtensa_opcode opcode = tinsn->opcode;
12762 int opnum;
12763
12764 if (fragP->tc_frag_data.slot_symbols[slot])
12765 {
12766 opnum = get_relaxable_immed (opcode);
12767 gas_assert (opnum >= 0);
12768 set_expr_symbol_offset (&tinsn->tok[opnum],
12769 fragP->tc_frag_data.slot_symbols[slot],
12770 fragP->tc_frag_data.slot_offsets[slot]);
12771 }
12772 tinsn->extra_arg = fragP->tc_frag_data.free_reg[slot];
12773 }
12774
12775
12776 static int
12777 get_num_stack_text_bytes (IStack *istack)
12778 {
12779 int i;
12780 int text_bytes = 0;
12781
12782 for (i = 0; i < istack->ninsn; i++)
12783 {
12784 TInsn *tinsn = &istack->insn[i];
12785 if (tinsn->insn_type == ITYPE_INSN)
12786 text_bytes += xg_get_single_size (tinsn->opcode);
12787 }
12788 return text_bytes;
12789 }
12790
12791
12792 static int
12793 get_num_stack_literal_bytes (IStack *istack)
12794 {
12795 int i;
12796 int lit_bytes = 0;
12797
12798 for (i = 0; i < istack->ninsn; i++)
12799 {
12800 TInsn *tinsn = &istack->insn[i];
12801 if (tinsn->insn_type == ITYPE_LITERAL && tinsn->ntok == 1)
12802 lit_bytes += 4;
12803 }
12804 return lit_bytes;
12805 }
12806
12807 \f
12808 /* vliw_insn functions. */
12809
12810 static void
12811 xg_init_vinsn (vliw_insn *v)
12812 {
12813 int i;
12814 xtensa_isa isa = xtensa_default_isa;
12815
12816 xg_clear_vinsn (v);
12817
12818 v->insnbuf = xtensa_insnbuf_alloc (isa);
12819 if (v->insnbuf == NULL)
12820 as_fatal (_("out of memory"));
12821
12822 for (i = 0; i < config_max_slots; i++)
12823 {
12824 v->slotbuf[i] = xtensa_insnbuf_alloc (isa);
12825 if (v->slotbuf[i] == NULL)
12826 as_fatal (_("out of memory"));
12827 }
12828 }
12829
12830
12831 static void
12832 xg_clear_vinsn (vliw_insn *v)
12833 {
12834 int i;
12835
12836 memset (v, 0, offsetof (vliw_insn, slots)
12837 + sizeof(TInsn) * config_max_slots);
12838
12839 v->format = XTENSA_UNDEFINED;
12840 v->num_slots = 0;
12841 v->inside_bundle = FALSE;
12842
12843 if (xt_saved_debug_type != DEBUG_NONE)
12844 debug_type = xt_saved_debug_type;
12845
12846 for (i = 0; i < config_max_slots; i++)
12847 v->slots[i].opcode = XTENSA_UNDEFINED;
12848 }
12849
12850
12851 static void
12852 xg_copy_vinsn (vliw_insn *dst, vliw_insn *src)
12853 {
12854 memcpy (dst, src,
12855 offsetof(vliw_insn, slots) + src->num_slots * sizeof(TInsn));
12856 dst->insnbuf = src->insnbuf;
12857 memcpy (dst->slotbuf, src->slotbuf, src->num_slots * sizeof(xtensa_insnbuf));
12858 }
12859
12860
12861 static bfd_boolean
12862 vinsn_has_specific_opcodes (vliw_insn *v)
12863 {
12864 int i;
12865
12866 for (i = 0; i < v->num_slots; i++)
12867 {
12868 if (v->slots[i].is_specific_opcode)
12869 return TRUE;
12870 }
12871 return FALSE;
12872 }
12873
12874
12875 static void
12876 xg_free_vinsn (vliw_insn *v)
12877 {
12878 int i;
12879 xtensa_insnbuf_free (xtensa_default_isa, v->insnbuf);
12880 for (i = 0; i < config_max_slots; i++)
12881 xtensa_insnbuf_free (xtensa_default_isa, v->slotbuf[i]);
12882 }
12883
12884
12885 /* Encode a vliw_insn into an insnbuf. Return TRUE if there are any symbolic
12886 operands. See also the assumptions listed for tinsn_to_slotbuf. */
12887
12888 static bfd_boolean
12889 vinsn_to_insnbuf (vliw_insn *vinsn,
12890 char *frag_offset,
12891 fragS *fragP,
12892 bfd_boolean record_fixup)
12893 {
12894 xtensa_isa isa = xtensa_default_isa;
12895 xtensa_format fmt = vinsn->format;
12896 xtensa_insnbuf insnbuf = vinsn->insnbuf;
12897 int slot;
12898 bfd_boolean has_fixup = FALSE;
12899
12900 xtensa_format_encode (isa, fmt, insnbuf);
12901
12902 for (slot = 0; slot < vinsn->num_slots; slot++)
12903 {
12904 TInsn *tinsn = &vinsn->slots[slot];
12905 expressionS *extra_arg = &tinsn->extra_arg;
12906 bfd_boolean tinsn_has_fixup =
12907 tinsn_to_slotbuf (vinsn->format, slot, tinsn,
12908 vinsn->slotbuf[slot]);
12909
12910 xtensa_format_set_slot (isa, fmt, slot,
12911 insnbuf, vinsn->slotbuf[slot]);
12912 if (extra_arg->X_op != O_illegal && extra_arg->X_op != O_register)
12913 {
12914 if (vinsn->num_slots != 1)
12915 as_bad (_("TLS relocation not allowed in FLIX bundle"));
12916 else if (record_fixup)
12917 /* Instructions that generate TLS relocations should always be
12918 relaxed in the front-end. If "record_fixup" is set, then this
12919 function is being called during back-end relaxation, so flag
12920 the unexpected behavior as an error. */
12921 as_bad (_("unexpected TLS relocation"));
12922 else
12923 fix_new (fragP, frag_offset - fragP->fr_literal,
12924 xtensa_format_length (isa, fmt),
12925 extra_arg->X_add_symbol, extra_arg->X_add_number,
12926 FALSE, map_operator_to_reloc (extra_arg->X_op, FALSE));
12927 }
12928 if (tinsn_has_fixup)
12929 {
12930 int i;
12931 xtensa_opcode opcode = tinsn->opcode;
12932 int noperands = xtensa_opcode_num_operands (isa, opcode);
12933 has_fixup = TRUE;
12934
12935 for (i = 0; i < noperands; i++)
12936 {
12937 expressionS* exp = &tinsn->tok[i];
12938 switch (exp->X_op)
12939 {
12940 case O_symbol:
12941 case O_lo16:
12942 case O_hi16:
12943 if (get_relaxable_immed (opcode) == i)
12944 {
12945 /* Add a fix record for the instruction, except if this
12946 function is being called prior to relaxation, i.e.,
12947 if record_fixup is false, and the instruction might
12948 be relaxed later. */
12949 if (record_fixup
12950 || tinsn->is_specific_opcode
12951 || !xg_is_relaxable_insn (tinsn, 0))
12952 {
12953 xg_add_opcode_fix (tinsn, i, fmt, slot, exp, fragP,
12954 frag_offset - fragP->fr_literal);
12955 }
12956 else
12957 {
12958 if (exp->X_op != O_symbol)
12959 as_bad (_("invalid operand"));
12960 tinsn->symbol = exp->X_add_symbol;
12961 tinsn->offset = exp->X_add_number;
12962 }
12963 }
12964 else
12965 as_bad (_("symbolic operand not allowed"));
12966 break;
12967
12968 case O_constant:
12969 case O_register:
12970 break;
12971
12972 default:
12973 as_bad (_("expression too complex"));
12974 break;
12975 }
12976 }
12977 }
12978 }
12979
12980 return has_fixup;
12981 }
12982
12983
12984 static void
12985 vinsn_from_chars (vliw_insn *vinsn, char *f)
12986 {
12987 static xtensa_insnbuf insnbuf = NULL;
12988 static xtensa_insnbuf slotbuf = NULL;
12989 int i;
12990 xtensa_format fmt;
12991 xtensa_isa isa = xtensa_default_isa;
12992
12993 if (!insnbuf)
12994 {
12995 insnbuf = xtensa_insnbuf_alloc (isa);
12996 slotbuf = xtensa_insnbuf_alloc (isa);
12997 }
12998
12999 xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) f, 0);
13000 fmt = xtensa_format_decode (isa, insnbuf);
13001 if (fmt == XTENSA_UNDEFINED)
13002 as_fatal (_("cannot decode instruction format"));
13003 vinsn->format = fmt;
13004 vinsn->num_slots = xtensa_format_num_slots (isa, fmt);
13005
13006 for (i = 0; i < vinsn->num_slots; i++)
13007 {
13008 TInsn *tinsn = &vinsn->slots[i];
13009 xtensa_format_get_slot (isa, fmt, i, insnbuf, slotbuf);
13010 tinsn_from_insnbuf (tinsn, slotbuf, fmt, i);
13011 }
13012 }
13013
13014 \f
13015 /* Expression utilities. */
13016
13017 /* Return TRUE if the expression is an integer constant. */
13018
13019 bfd_boolean
13020 expr_is_const (const expressionS *s)
13021 {
13022 return (s->X_op == O_constant);
13023 }
13024
13025
13026 /* Get the expression constant.
13027 Calling this is illegal if expr_is_const () returns TRUE. */
13028
13029 offsetT
13030 get_expr_const (const expressionS *s)
13031 {
13032 gas_assert (expr_is_const (s));
13033 return s->X_add_number;
13034 }
13035
13036
13037 /* Set the expression to a constant value. */
13038
13039 void
13040 set_expr_const (expressionS *s, offsetT val)
13041 {
13042 s->X_op = O_constant;
13043 s->X_add_number = val;
13044 s->X_add_symbol = NULL;
13045 s->X_op_symbol = NULL;
13046 }
13047
13048
13049 bfd_boolean
13050 expr_is_register (const expressionS *s)
13051 {
13052 return (s->X_op == O_register);
13053 }
13054
13055
13056 /* Get the expression constant.
13057 Calling this is illegal if expr_is_const () returns TRUE. */
13058
13059 offsetT
13060 get_expr_register (const expressionS *s)
13061 {
13062 gas_assert (expr_is_register (s));
13063 return s->X_add_number;
13064 }
13065
13066
13067 /* Set the expression to a symbol + constant offset. */
13068
13069 void
13070 set_expr_symbol_offset (expressionS *s, symbolS *sym, offsetT offset)
13071 {
13072 s->X_op = O_symbol;
13073 s->X_add_symbol = sym;
13074 s->X_op_symbol = NULL; /* unused */
13075 s->X_add_number = offset;
13076 }
13077
13078
13079 /* Return TRUE if the two expressions are equal. */
13080
13081 bfd_boolean
13082 expr_is_equal (expressionS *s1, expressionS *s2)
13083 {
13084 if (s1->X_op != s2->X_op)
13085 return FALSE;
13086 if (s1->X_add_symbol != s2->X_add_symbol)
13087 return FALSE;
13088 if (s1->X_op_symbol != s2->X_op_symbol)
13089 return FALSE;
13090 if (s1->X_add_number != s2->X_add_number)
13091 return FALSE;
13092 return TRUE;
13093 }
13094
13095
13096 static void
13097 copy_expr (expressionS *dst, const expressionS *src)
13098 {
13099 memcpy (dst, src, sizeof (expressionS));
13100 }
13101
13102 \f
13103 /* Support for the "--rename-section" option. */
13104
13105 struct rename_section_struct
13106 {
13107 const char *old_name;
13108 char *new_name;
13109 struct rename_section_struct *next;
13110 };
13111
13112 static struct rename_section_struct *section_rename;
13113
13114
13115 /* Parse the string "oldname=new_name(:oldname2=new_name2)*" and add
13116 entries to the section_rename list. Note: Specifying multiple
13117 renamings separated by colons is not documented and is retained only
13118 for backward compatibility. */
13119
13120 static void
13121 build_section_rename (const char *arg)
13122 {
13123 struct rename_section_struct *r;
13124 char *this_arg = NULL;
13125 char *next_arg = NULL;
13126
13127 for (this_arg = xstrdup (arg); this_arg != NULL; this_arg = next_arg)
13128 {
13129 char *old_name, *new_name;
13130
13131 if (this_arg)
13132 {
13133 next_arg = strchr (this_arg, ':');
13134 if (next_arg)
13135 {
13136 *next_arg = '\0';
13137 next_arg++;
13138 }
13139 }
13140
13141 old_name = this_arg;
13142 new_name = strchr (this_arg, '=');
13143
13144 if (*old_name == '\0')
13145 {
13146 as_warn (_("ignoring extra '-rename-section' delimiter ':'"));
13147 continue;
13148 }
13149 if (!new_name || new_name[1] == '\0')
13150 {
13151 as_warn (_("ignoring invalid '-rename-section' specification: '%s'"),
13152 old_name);
13153 continue;
13154 }
13155 *new_name = '\0';
13156 new_name++;
13157
13158 /* Check for invalid section renaming. */
13159 for (r = section_rename; r != NULL; r = r->next)
13160 {
13161 if (strcmp (r->old_name, old_name) == 0)
13162 as_bad (_("section %s renamed multiple times"), old_name);
13163 if (strcmp (r->new_name, new_name) == 0)
13164 as_bad (_("multiple sections remapped to output section %s"),
13165 new_name);
13166 }
13167
13168 /* Now add it. */
13169 r = (struct rename_section_struct *)
13170 xmalloc (sizeof (struct rename_section_struct));
13171 r->old_name = xstrdup (old_name);
13172 r->new_name = xstrdup (new_name);
13173 r->next = section_rename;
13174 section_rename = r;
13175 }
13176 }
13177
13178
13179 char *
13180 xtensa_section_rename (const char *name)
13181 {
13182 struct rename_section_struct *r = section_rename;
13183
13184 for (r = section_rename; r != NULL; r = r->next)
13185 {
13186 if (strcmp (r->old_name, name) == 0)
13187 return r->new_name;
13188 }
13189
13190 return (char *) name;
13191 }