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