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