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