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