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