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