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