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