]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-xtensa.c
gas/
[thirdparty/binutils-gdb.git] / gas / config / tc-xtensa.c
1 /* tc-xtensa.c -- Assemble Xtensa instructions.
2 Copyright 2003, 2004 Free Software Foundation, Inc.
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
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 MA 02111-1307, USA. */
20
21 #include <string.h>
22 #include <limits.h>
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"
31 #include "dwarf2dbg.h"
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
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. */
61 const char comment_chars[] = "#";
62 const char line_comment_chars[] = "#";
63 const char line_separator_chars[] = ";";
64 const char EXP_CHARS[] = "eE";
65 const char FLT_CHARS[] = "rRsSfFdDxXpP";
66
67
68 /* Flags to indicate whether the hardware supports the density and
69 absolute literals options. */
70
71 bfd_boolean density_supported = XCHAL_HAVE_DENSITY;
72 bfd_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
76
77 static vliw_insn cur_vinsn;
78
79 size_t xtensa_fetch_width = XCHAL_INST_FETCH_WIDTH;
80
81 static 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. */
86 static bfd_boolean past_xtensa_end = FALSE;
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")
99 #define LIT4_SECTION_NAME xtensa_section_rename (".lit4")
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
106 /* This type is used for the directive_stack to keep track of the
107 state of the literal collection pools. */
108
109 typedef struct lit_state_struct
110 {
111 const char *lit_seg_name;
112 const char *lit4_seg_name;
113 const char *init_lit_seg_name;
114 const char *fini_lit_seg_name;
115 segT lit_seg;
116 segT lit4_seg;
117 segT init_lit_seg;
118 segT fini_lit_seg;
119 } lit_state;
120
121 static 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
128 typedef struct seg_list_struct
129 {
130 struct seg_list_struct *next;
131 segT seg;
132 } seg_list;
133
134 static seg_list literal_head_h;
135 static seg_list *literal_head = &literal_head_h;
136 static seg_list init_literal_head_h;
137 static seg_list *init_literal_head = &init_literal_head_h;
138 static seg_list fini_literal_head_h;
139 static seg_list *fini_literal_head = &fini_literal_head_h;
140
141
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
148 typedef struct sym_list_struct
149 {
150 struct sym_list_struct *next;
151 symbolS *sym;
152 } sym_list;
153
154 static sym_list *insn_labels = NULL;
155 static sym_list *free_insn_labels = NULL;
156 static sym_list *saved_insn_labels = NULL;
157
158 static sym_list *literal_syms;
159
160
161 /* Flags to determine whether to prefer const16 or l32r
162 if both options are available. */
163 int prefer_const16 = 0;
164 int prefer_l32r = 0;
165
166 /* Global flag to indicate when we are emitting literals. */
167 int generating_literals = 0;
168
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
255 typedef struct frag_flags_struct frag_flags;
256
257 struct 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. */
291 struct 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
300
301 /* Structure for saving the current state before emitting literals. */
302 typedef 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
311 /* Opcode placement information */
312
313 typedef 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
320 typedef 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
354 op_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
364 /* Directives. */
365
366 typedef enum
367 {
368 directive_none = 0,
369 directive_literal,
370 directive_density,
371 directive_transform,
372 directive_freeregs,
373 directive_longcalls,
374 directive_literal_prefix,
375 directive_schedule,
376 directive_absolute_literals,
377 directive_last_directive
378 } directiveE;
379
380 typedef struct
381 {
382 const char *name;
383 bfd_boolean can_be_negated;
384 } directive_infoS;
385
386 const directive_infoS directive_info[] =
387 {
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 }
397 };
398
399 bfd_boolean directive_state[] =
400 {
401 FALSE, /* none */
402 FALSE, /* literal */
403 #if !XCHAL_HAVE_DENSITY
404 FALSE, /* density */
405 #else
406 TRUE, /* density */
407 #endif
408 TRUE, /* transform */
409 FALSE, /* freeregs */
410 FALSE, /* longcalls */
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
418 };
419
420
421 /* Directive functions. */
422
423 static void xtensa_begin_directive (int);
424 static void xtensa_end_directive (int);
425 static void xtensa_dwarf2_directive_loc (int);
426 static void xtensa_literal_prefix (char const *, int);
427 static void xtensa_literal_position (int);
428 static void xtensa_literal_pseudo (int);
429 static void xtensa_frequency_pseudo (int);
430 static void xtensa_elf_cons (int);
431
432 /* Parsing and Idiom Translation. */
433
434 static bfd_reloc_code_real_type xtensa_elf_suffix (char **, expressionS *);
435
436 /* Various Other Internal Functions. */
437
438 static void xtensa_mark_literal_pool_location (void);
439 static addressT get_expanded_loop_offset (xtensa_opcode);
440 static fragS *get_literal_pool_location (segT);
441 static void set_literal_pool_location (segT, fragS *);
442 static void xtensa_set_frag_assembly_state (fragS *);
443 static void finish_vinsn (vliw_insn *);
444 static bfd_boolean emit_single_op (TInsn *);
445 static int total_frag_text_expansion (fragS *);
446
447 /* Alignment Functions. */
448
449 static size_t get_text_align_power (int);
450 static addressT get_text_align_max_fill_size (int, bfd_boolean, bfd_boolean);
451
452 /* Helpers for xtensa_relax_frag(). */
453
454 static long relax_frag_add_nop (fragS *);
455
456 /* Accessors for additional per-subsegment information. */
457
458 static unsigned get_last_insn_flags (segT, subsegT);
459 static void set_last_insn_flags (segT, subsegT, unsigned, bfd_boolean);
460 static float get_subseg_total_freq (segT, subsegT);
461 static float get_subseg_target_freq (segT, subsegT);
462 static void set_subseg_freq (segT, subsegT, float, float);
463
464 /* Segment list functions. */
465
466 static void xtensa_move_literals (void);
467 static void xtensa_reorder_segments (void);
468 static void xtensa_switch_to_literal_fragment (emit_state *);
469 static void xtensa_switch_to_non_abs_literal_fragment (emit_state *);
470 static void xtensa_switch_section_emit_state (emit_state *, segT, subsegT);
471 static void xtensa_restore_emit_state (emit_state *);
472 static void cache_literal_section
473 (seg_list *, const char *, segT *, bfd_boolean);
474
475 /* Import from elf32-xtensa.c in BFD library. */
476
477 extern char *xtensa_get_property_section_name (asection *, const char *);
478
479 /* op_placement_info functions. */
480
481 static void init_op_placement_info_table (void);
482 extern bfd_boolean opcode_fits_format_slot (xtensa_opcode, xtensa_format, int);
483 static int xg_get_single_size (xtensa_opcode);
484 static xtensa_format xg_get_single_format (xtensa_opcode);
485
486 /* TInsn and IStack functions. */
487
488 static bfd_boolean tinsn_has_symbolic_operands (const TInsn *);
489 static bfd_boolean tinsn_has_invalid_symbolic_operands (const TInsn *);
490 static bfd_boolean tinsn_has_complex_operands (const TInsn *);
491 static bfd_boolean tinsn_to_insnbuf (TInsn *, xtensa_insnbuf);
492 static bfd_boolean tinsn_check_arguments (const TInsn *);
493 static void tinsn_from_chars (TInsn *, char *, int);
494 static void tinsn_immed_from_frag (TInsn *, fragS *, int);
495 static int get_num_stack_text_bytes (IStack *);
496 static int get_num_stack_literal_bytes (IStack *);
497
498 /* vliw_insn functions. */
499
500 static void xg_init_vinsn (vliw_insn *);
501 static void xg_clear_vinsn (vliw_insn *);
502 static bfd_boolean vinsn_has_specific_opcodes (vliw_insn *);
503 static void xg_free_vinsn (vliw_insn *);
504 static bfd_boolean vinsn_to_insnbuf
505 (vliw_insn *, char *, fragS *, bfd_boolean);
506 static void vinsn_from_chars (vliw_insn *, char *);
507
508 /* Expression Utilities. */
509
510 bfd_boolean expr_is_const (const expressionS *);
511 offsetT get_expr_const (const expressionS *);
512 void set_expr_const (expressionS *, offsetT);
513 bfd_boolean expr_is_register (const expressionS *);
514 offsetT get_expr_register (const expressionS *);
515 void set_expr_symbol_offset (expressionS *, symbolS *, offsetT);
516 static void set_expr_symbol_offset_diff
517 (expressionS *, symbolS *, symbolS *, offsetT);
518 bfd_boolean expr_is_equal (expressionS *, expressionS *);
519 static void copy_expr (expressionS *, const expressionS *);
520
521 /* Section renaming. */
522
523 static void build_section_rename (const char *);
524
525
526 /* ISA imported from bfd. */
527 extern xtensa_isa xtensa_default_isa;
528
529 extern int target_big_endian;
530
531 static xtensa_opcode xtensa_addi_opcode;
532 static xtensa_opcode xtensa_addmi_opcode;
533 static xtensa_opcode xtensa_call0_opcode;
534 static xtensa_opcode xtensa_call4_opcode;
535 static xtensa_opcode xtensa_call8_opcode;
536 static xtensa_opcode xtensa_call12_opcode;
537 static xtensa_opcode xtensa_callx0_opcode;
538 static xtensa_opcode xtensa_callx4_opcode;
539 static xtensa_opcode xtensa_callx8_opcode;
540 static xtensa_opcode xtensa_callx12_opcode;
541 static xtensa_opcode xtensa_const16_opcode;
542 static xtensa_opcode xtensa_entry_opcode;
543 static xtensa_opcode xtensa_movi_opcode;
544 static xtensa_opcode xtensa_movi_n_opcode;
545 static xtensa_opcode xtensa_isync_opcode;
546 static xtensa_opcode xtensa_jx_opcode;
547 static xtensa_opcode xtensa_l32r_opcode;
548 static xtensa_opcode xtensa_loop_opcode;
549 static xtensa_opcode xtensa_loopnez_opcode;
550 static xtensa_opcode xtensa_loopgtz_opcode;
551 static xtensa_opcode xtensa_nop_opcode;
552 static xtensa_opcode xtensa_nop_n_opcode;
553 static xtensa_opcode xtensa_or_opcode;
554 static xtensa_opcode xtensa_ret_opcode;
555 static xtensa_opcode xtensa_ret_n_opcode;
556 static xtensa_opcode xtensa_retw_opcode;
557 static xtensa_opcode xtensa_retw_n_opcode;
558 static xtensa_opcode xtensa_rsr_lcount_opcode;
559 static xtensa_opcode xtensa_waiti_opcode;
560
561 \f
562 /* Command-line Options. */
563
564 bfd_boolean use_literal_section = TRUE;
565 static bfd_boolean align_targets = TRUE;
566 static bfd_boolean warn_unaligned_branch_targets = FALSE;
567 static bfd_boolean has_a0_b_retw = FALSE;
568 static bfd_boolean workaround_a0_b_retw = FALSE;
569 static bfd_boolean workaround_b_j_loop_end = FALSE;
570 static bfd_boolean workaround_short_loop = FALSE;
571 static bfd_boolean maybe_has_short_loop = FALSE;
572 static bfd_boolean workaround_close_loop_end = FALSE;
573 static bfd_boolean maybe_has_close_loop_end = FALSE;
574
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. */
580
581 static bfd_boolean workaround_all_short_loops = FALSE;
582
583
584 static void
585 xtensa_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
602 enum
603 {
604 option_density = OPTION_MD_BASE,
605 option_no_density,
606
607 option_relax,
608 option_no_relax,
609
610 option_link_relax,
611 option_no_link_relax,
612
613 option_generics,
614 option_no_generics,
615
616 option_transform,
617 option_no_transform,
618
619 option_text_section_literals,
620 option_no_text_section_literals,
621
622 option_absolute_literals,
623 option_no_absolute_literals,
624
625 option_align_targets,
626 option_no_align_targets,
627
628 option_warn_unaligned_targets,
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
650 option_rename_section_name,
651
652 option_prefer_l32r,
653 option_prefer_const16,
654
655 option_target_hardware
656 };
657
658 const char *md_shortopts = "";
659
660 struct option md_longopts[] =
661 {
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 },
679 /* This option was changed from -align-target to -target-align
680 because it conflicted with the "-al" option. */
681 { "target-align", no_argument, NULL, option_align_targets },
682 { "no-target-align", no_argument, NULL, option_no_align_targets },
683 { "warn-unaligned-targets", no_argument, NULL,
684 option_warn_unaligned_targets },
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 },
691
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 },
696
697 { "no-workaround-short-loops", no_argument, NULL,
698 option_no_workaround_short_loop },
699 { "workaround-short-loops", no_argument, NULL,
700 option_workaround_short_loop },
701
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 },
716
717 { "rename-section", required_argument, NULL, option_rename_section_name },
718
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 }
725 };
726
727 size_t md_longopts_size = sizeof md_longopts;
728
729
730 int
731 md_parse_option (int c, char *arg)
732 {
733 switch (c)
734 {
735 case option_density:
736 as_warn (_("--density option is ignored"));
737 return 1;
738 case option_no_density:
739 as_warn (_("--no-density option is ignored"));
740 return 1;
741 case option_link_relax:
742 linkrelax = 1;
743 return 1;
744 case option_no_link_relax:
745 linkrelax = 0;
746 return 1;
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);
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;
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
783 case option_workaround_a0_b_retw:
784 workaround_a0_b_retw = TRUE;
785 return 1;
786 case option_no_workaround_a0_b_retw:
787 workaround_a0_b_retw = FALSE;
788 return 1;
789 case option_workaround_b_j_loop_end:
790 workaround_b_j_loop_end = TRUE;
791 return 1;
792 case option_no_workaround_b_j_loop_end:
793 workaround_b_j_loop_end = FALSE;
794 return 1;
795
796 case option_workaround_short_loop:
797 workaround_short_loop = TRUE;
798 return 1;
799 case option_no_workaround_short_loop:
800 workaround_short_loop = FALSE;
801 return 1;
802
803 case option_workaround_all_short_loops:
804 workaround_all_short_loops = TRUE;
805 return 1;
806 case option_no_workaround_all_short_loops:
807 workaround_all_short_loops = FALSE;
808 return 1;
809
810 case option_workaround_close_loop_end:
811 workaround_close_loop_end = TRUE;
812 return 1;
813 case option_no_workaround_close_loop_end:
814 workaround_close_loop_end = FALSE;
815 return 1;
816
817 case option_no_workarounds:
818 workaround_a0_b_retw = FALSE;
819 workaround_b_j_loop_end = FALSE;
820 workaround_short_loop = FALSE;
821 workaround_all_short_loops = FALSE;
822 workaround_close_loop_end = FALSE;
823 return 1;
824
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
832 case option_warn_unaligned_targets:
833 warn_unaligned_branch_targets = TRUE;
834 return 1;
835
836 case option_rename_section_name:
837 build_section_rename (arg);
838 return 1;
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
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
895 default:
896 return 0;
897 }
898 }
899
900
901 void
902 md_show_usage (FILE *stream)
903 {
904 fputs ("\n\
905 Xtensa options:\n\
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);
914 }
915
916 \f
917 /* Functions related to the list of current label symbols. */
918
919 static void
920 xtensa_add_insn_label (symbolS *sym)
921 {
922 sym_list *l;
923
924 if (!free_insn_labels)
925 l = (sym_list *) xmalloc (sizeof (sym_list));
926 else
927 {
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
938 static void
939 xtensa_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
974 static void
975 xtensa_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 }
987 }
988 }
989
990 \f
991 /* Directive data and functions. */
992
993 typedef 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
1004 state_stackS *directive_state_stack;
1005
1006 const pseudo_typeS md_pseudo_table[] =
1007 {
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 },
1020 };
1021
1022
1023 static bfd_boolean
1024 use_transform (void)
1025 {
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];
1030 }
1031
1032
1033 static bfd_boolean
1034 use_longcalls (void)
1035 {
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 ();
1040 }
1041
1042
1043 static bfd_boolean
1044 do_align_targets (void)
1045 {
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 ();
1050 }
1051
1052
1053 static void
1054 directive_push (directiveE directive, bfd_boolean negated, const void *datum)
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
1074
1075 static void
1076 directive_pop (directiveE *directive,
1077 bfd_boolean *negated,
1078 const char **file,
1079 unsigned int *line,
1080 const void **datum)
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
1102 static void
1103 directive_balance (void)
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
1120 static bfd_boolean
1121 inside_directive (directiveE dir)
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
1132 static void
1133 get_directive (directiveE *directive, bfd_boolean *negated)
1134 {
1135 int len;
1136 unsigned i;
1137 char *directive_string;
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,
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;
1166
1167 for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i)
1168 {
1169 if (strncmp (directive_string, directive_info[i].name, len) == 0)
1170 {
1171 input_line_pointer += len;
1172 *directive = (directiveE) i;
1173 if (*negated && !directive_info[i].can_be_negated)
1174 as_bad (_("directive %s cannot be negated"),
1175 directive_info[i].name);
1176 return;
1177 }
1178 }
1179
1180 as_bad (_("unknown directive"));
1181 *directive = (directiveE) XTENSA_UNDEFINED;
1182 }
1183
1184
1185 static void
1186 xtensa_begin_directive (int ignore ATTRIBUTE_UNUSED)
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
1201 if (cur_vinsn.inside_bundle)
1202 as_bad (_("directives are not valid inside bundles"));
1203
1204 switch (directive)
1205 {
1206 case directive_literal:
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 }
1214 as_warn (_(".begin literal is deprecated; use .literal instead"));
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:
1221 /* Have to flush pending output because a movi relaxed to an l32r
1222 might produce a literal. */
1223 md_flush_pending_output ();
1224 /* Check to see if the current fragment is a literal
1225 fragment. If it is, then this operation is not allowed. */
1226 if (generating_literals)
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
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
1270 case directive_density:
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)
1277 {
1278 as_warn (_("Xtensa absolute literals option not supported; ignored"));
1279 break;
1280 }
1281 xtensa_set_frag_assembly_state (frag_now);
1282 directive_push (directive, negated, 0);
1283 break;
1284
1285 default:
1286 md_flush_pending_output ();
1287 xtensa_set_frag_assembly_state (frag_now);
1288 directive_push (directive, negated, 0);
1289 break;
1290 }
1291
1292 demand_empty_rest_of_line ();
1293 }
1294
1295
1296 static void
1297 xtensa_end_directive (int ignore ATTRIBUTE_UNUSED)
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;
1304 emit_state **state_ptr;
1305 lit_state *s;
1306
1307 if (cur_vinsn.inside_bundle)
1308 as_bad (_("directives are not valid inside bundles"));
1309
1310 get_directive (&end_directive, &end_negated);
1311
1312 md_flush_pending_output ();
1313
1314 switch (end_directive)
1315 {
1316 case (directiveE) XTENSA_UNDEFINED:
1317 discard_rest_of_line ();
1318 return;
1319
1320 case directive_density:
1321 as_warn (_(".end [no-]density is ignored"));
1322 demand_empty_rest_of_line ();
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;
1336 }
1337
1338 state_ptr = &state; /* use state_ptr to avoid type-punning warning */
1339 directive_pop (&begin_directive, &begin_negated, &file, &line,
1340 (const void **) state_ptr);
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);
1357 xtensa_set_frag_assembly_state (frag_now);
1358 free (state);
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 }
1365 break;
1366
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
1379 case directive_schedule:
1380 case directive_freeregs:
1381 break;
1382
1383 default:
1384 xtensa_set_frag_assembly_state (frag_now);
1385 break;
1386 }
1387 }
1388 }
1389
1390 demand_empty_rest_of_line ();
1391 }
1392
1393
1394 /* Wrap dwarf2 functions so that we correctly support the .loc directive. */
1395
1396 static bfd_boolean xtensa_loc_directive_seen = FALSE;
1397
1398 static void
1399 xtensa_dwarf2_directive_loc (int x)
1400 {
1401 xtensa_loc_directive_seen = TRUE;
1402 dwarf2_directive_loc (x);
1403 }
1404
1405
1406 static void
1407 xtensa_dwarf2_emit_insn (int size, struct dwarf2_line_info *loc)
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
1416 /* Place an aligned literal fragment at the current location. */
1417
1418 static void
1419 xtensa_literal_position (int ignore ATTRIBUTE_UNUSED)
1420 {
1421 md_flush_pending_output ();
1422
1423 if (inside_directive (directive_literal))
1424 as_warn (_(".literal_position inside literal directive; ignoring"));
1425 xtensa_mark_literal_pool_location ();
1426
1427 demand_empty_rest_of_line ();
1428 xtensa_clear_insn_labels ();
1429 }
1430
1431
1432 /* Support .literal label, expr, ... */
1433
1434 static void
1435 xtensa_literal_pseudo (int ignored ATTRIBUTE_UNUSED)
1436 {
1437 emit_state state;
1438 char *p, *base_name;
1439 char c;
1440 segT dest_seg;
1441
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
1449 md_flush_pending_output ();
1450
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
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
1463 /* ...but if we aren't using text-section-literals, then we
1464 need to put them in the section we just switched to. */
1465 if (use_literal_section || directive_state[directive_absolute_literals])
1466 dest_seg = now_seg;
1467
1468 /* All literals are aligned to four-byte boundaries. */
1469 frag_align (2, 0, 0);
1470 record_alignment (now_seg, 2);
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
1488 colon (base_name);
1489
1490 *p = c;
1491 input_line_pointer++; /* skip ',' or ':' */
1492
1493 xtensa_elf_cons (4);
1494
1495 xtensa_restore_emit_state (&state);
1496
1497 /* Restore the list of current labels. */
1498 xtensa_clear_insn_labels ();
1499 insn_labels = saved_insn_labels;
1500 }
1501
1502
1503 static void
1504 xtensa_literal_prefix (char const *start, int len)
1505 {
1506 char *name, *linkonce_suffix;
1507 char *newname, *newname4;
1508 size_t linkonce_len;
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). */
1519
1520 newname = xmalloc (len + strlen (".literal") + 1);
1521 newname4 = xmalloc (len + strlen (".lit4") + 1);
1522
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");
1529
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
1549 /* Note that cache_literal_section does not create a segment if
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
1555 sections to occur correctly. */
1556 default_lit_sections.lit_seg_name = tc_canonicalize_symbol_name (newname);
1557 default_lit_sections.lit4_seg_name = tc_canonicalize_symbol_name (newname4);
1558
1559 free (name);
1560 }
1561
1562
1563 /* Support ".frequency branch_target_frequency fall_through_frequency". */
1564
1565 static void
1566 xtensa_frequency_pseudo (int ignored ATTRIBUTE_UNUSED)
1567 {
1568 float fall_through_f, target_f;
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
1586 set_subseg_freq (now_seg, now_subseg, target_f + fall_through_f, target_f);
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
1595 static void
1596 xtensa_elf_cons (int nbytes)
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
1651 \f
1652 /* Parsing and Idiom Translation. */
1653
1654 /* Parse @plt, etc. and return the desired relocation. */
1655 static bfd_reloc_code_real_type
1656 xtensa_elf_suffix (char **str_p, expressionS *exp_p)
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 }
1673
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;
1725 }
1726
1727
1728 static const char *
1729 expression_end (const char *name)
1730 {
1731 while (1)
1732 {
1733 switch (*name)
1734 {
1735 case '}':
1736 case ';':
1737 case '\0':
1738 case ',':
1739 case ':':
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
1754 static unsigned
1755 tc_get_register (const char *prefix)
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
1809 static void
1810 expression_maybe_register (xtensa_opcode opc, int opnd, expressionS *tok)
1811 {
1812 xtensa_isa isa = xtensa_default_isa;
1813
1814 /* Check if this is an immediate operand. */
1815 if (xtensa_operand_is_register (isa, opc, opnd) == 0)
1816 {
1817 bfd_reloc_code_real_type reloc;
1818 segT t = expression (tok);
1819 if (t == absolute_section
1820 && xtensa_operand_is_PCrelative (isa, opc, opnd) == 1)
1821 {
1822 assert (tok->X_op == O_constant);
1823 tok->X_op = O_symbol;
1824 tok->X_add_symbol = &abs_symbol;
1825 }
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))
1830 {
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 }
1856 }
1857 }
1858 else
1859 {
1860 xtensa_regfile opnd_rf = xtensa_operand_regfile (isa, opc, opnd);
1861 unsigned reg = tc_get_register (xtensa_regfile_shortname (isa, opnd_rf));
1862
1863 if (reg != ERROR_REG_NUM) /* Already errored */
1864 {
1865 uint32 buf = reg;
1866 if (xtensa_operand_encode (isa, opc, opnd, &buf))
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
1879 static int
1880 tokenize_arguments (char **args, char *str)
1881 {
1882 char *old_input_line_pointer;
1883 bfd_boolean saw_comma = FALSE;
1884 bfd_boolean saw_arg = FALSE;
1885 bfd_boolean saw_colon = FALSE;
1886 int num_args = 0;
1887 char *arg_end, *arg;
1888 int arg_len;
1889
1890 /* Save and restore input_line_pointer around this function. */
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':
1900 case '}':
1901 goto fini;
1902
1903 case ':':
1904 input_line_pointer++;
1905 if (saw_comma || saw_colon || !saw_arg)
1906 goto err;
1907 saw_colon = TRUE;
1908 break;
1909
1910 case ',':
1911 input_line_pointer++;
1912 if (saw_comma || saw_colon || !saw_arg)
1913 goto err;
1914 saw_comma = TRUE;
1915 break;
1916
1917 default:
1918 if (!saw_comma && !saw_colon && saw_arg)
1919 goto err;
1920
1921 arg_end = input_line_pointer + 1;
1922 while (!expression_end (arg_end))
1923 arg_end += 1;
1924
1925 arg_len = arg_end - input_line_pointer;
1926 arg = (char *) xmalloc ((saw_colon ? 1 : 0) + arg_len + 1);
1927 args[num_args] = arg;
1928
1929 if (saw_colon)
1930 *arg++ = ':';
1931 strncpy (arg, input_line_pointer, arg_len);
1932 arg[arg_len] = '\0';
1933
1934 input_line_pointer = arg_end;
1935 num_args += 1;
1936 saw_comma = FALSE;
1937 saw_colon = FALSE;
1938 saw_arg = TRUE;
1939 break;
1940 }
1941 }
1942
1943 fini:
1944 if (saw_comma || saw_colon)
1945 goto err;
1946 input_line_pointer = old_input_line_pointer;
1947 return num_args;
1948
1949 err:
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"));
1958 input_line_pointer = old_input_line_pointer;
1959 return -1;
1960 }
1961
1962
1963 /* Parse the arguments to an opcode. Return TRUE on error. */
1964
1965 static bfd_boolean
1966 parse_arguments (TInsn *insn, int num_args, char **arg_strings)
1967 {
1968 expressionS *tok, *last_tok;
1969 xtensa_opcode opcode = insn->opcode;
1970 bfd_boolean had_error = TRUE;
1971 xtensa_isa isa = xtensa_default_isa;
1972 int n, num_regs = 0;
1973 int opcode_operand_count;
1974 int opnd_cnt, last_opnd_cnt;
1975 unsigned int next_reg = 0;
1976 char *old_input_line_pointer;
1977
1978 if (insn->insn_type == ITYPE_LITERAL)
1979 opcode_operand_count = 1;
1980 else
1981 opcode_operand_count = xtensa_opcode_num_operands (isa, opcode);
1982
1983 tok = insn->tok;
1984 memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS);
1985
1986 /* Save and restore input_line_pointer around this function. */
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 }
1999
2000 for (n = 0; n < num_args; n++)
2001 {
2002 input_line_pointer = arg_strings[n];
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;
2038
2039 last_tok = tok;
2040 last_opnd_cnt = opnd_cnt;
2041
2042 do
2043 {
2044 opnd_cnt += 1;
2045 tok++;
2046 }
2047 while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0);
2048 }
2049 }
2050
2051 if (num_regs > 0 && ((int) next_reg != last_tok->X_add_number + 1))
2052 goto err;
2053
2054 insn->ntok = tok - insn->tok;
2055 had_error = FALSE;
2056
2057 err:
2058 input_line_pointer = old_input_line_pointer;
2059 return had_error;
2060 }
2061
2062
2063 static int
2064 get_invisible_operands (TInsn *insn)
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
2131 static void
2132 xg_reverse_shift_count (char **cnt_argp)
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
2149 static int
2150 xg_arg_is_constant (char *arg, offsetT *valp)
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
2169 static void
2170 xg_replace_opname (char **popname, char *newop)
2171 {
2172 free (*popname);
2173 *popname = (char *) xmalloc (strlen (newop) + 1);
2174 strcpy (*popname, newop);
2175 }
2176
2177
2178 static int
2179 xg_check_num_args (int *pnum_args,
2180 int expected_num,
2181 char *opname,
2182 char **arg_strings)
2183 {
2184 int num_args = *pnum_args;
2185
2186 if (num_args < expected_num)
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
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
2213 static int
2214 xg_translate_sysreg_op (char **popname, int *pnum_args, char **arg_strings)
2215 {
2216 xtensa_isa isa = xtensa_default_isa;
2217 xtensa_sysreg sr;
2218 char *opname, *new_opname;
2219 const char *sr_name;
2220 int is_user, is_write;
2221 bfd_boolean has_underbar = FALSE;
2222
2223 opname = *popname;
2224 if (*opname == '_')
2225 {
2226 has_underbar = TRUE;
2227 opname += 1;
2228 }
2229 is_user = (opname[1] == 'u');
2230 is_write = (opname[0] == 'w');
2231
2232 /* Opname == [rw]ur or [rwx]sr... */
2233
2234 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2235 return -1;
2236
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;
2248 if (!xg_arg_is_constant (arg_strings[1], &val))
2249 {
2250 as_bad (_("invalid register '%s' for '%s' instruction"),
2251 arg_strings[1], opname);
2252 return -1;
2253 }
2254 sr = xtensa_sysreg_lookup (isa, val, is_user);
2255 if (sr == XTENSA_UNDEFINED)
2256 {
2257 as_bad (_("invalid register number (%ld) for '%s' instruction"),
2258 val, opname);
2259 return -1;
2260 }
2261 }
2262
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
2283 static int
2284 xtensa_translate_old_userreg_ops (char **popname)
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
2344 static int
2345 xtensa_translate_zero_immed (char *old_op,
2346 char *new_op,
2347 char **popname,
2348 int *pnum_args,
2349 char **arg_strings)
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;
2359
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;
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
2378 static int
2379 xg_translate_idioms (char **popname, int *pnum_args, char **arg_strings)
2380 {
2381 char *opname = *popname;
2382 bfd_boolean has_underbar = FALSE;
2383
2384 if (cur_vinsn.inside_bundle)
2385 return 0;
2386
2387 if (*opname == '_')
2388 {
2389 has_underbar = TRUE;
2390 opname += 1;
2391 }
2392
2393 if (strcmp (opname, "mov") == 0)
2394 {
2395 if (use_transform () && !has_underbar && density_supported)
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
2429 if (xtensa_nop_opcode == XTENSA_UNDEFINED
2430 && strcmp (opname, "nop") == 0)
2431 {
2432 if (use_transform () && !has_underbar && density_supported)
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
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')
2456 return xg_translate_sysreg_op (popname, pnum_args, arg_strings);
2457
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);
2463
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;
2471
2472 if (xtensa_translate_zero_immed ("beqi", "beqz", popname,
2473 pnum_args, arg_strings))
2474 return -1;
2475
2476 if (xtensa_translate_zero_immed ("bgei", "bgez", popname,
2477 pnum_args, arg_strings))
2478 return -1;
2479
2480 if (xtensa_translate_zero_immed ("blti", "bltz", popname,
2481 pnum_args, arg_strings))
2482 return -1;
2483 }
2484
2485 return 0;
2486 }
2487
2488 \f
2489 /* Functions for dealing with the Xtensa ISA. */
2490
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. */
2497
2498 static int
2499 get_relaxable_immed (xtensa_opcode opcode)
2500 {
2501 int last_immed = -1;
2502 int noperands, opi;
2503
2504 if (opcode == XTENSA_UNDEFINED)
2505 return -1;
2506
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;
2517 }
2518 return last_immed;
2519 }
2520
2521
2522 static xtensa_opcode
2523 get_opcode_from_buf (const char *buf, int slot)
2524 {
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)
2531 {
2532 insnbuf = xtensa_insnbuf_alloc (isa);
2533 slotbuf = xtensa_insnbuf_alloc (isa);
2534 }
2535
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;
2540
2541 if (slot >= xtensa_format_num_slots (isa, fmt))
2542 return XTENSA_UNDEFINED;
2543
2544 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
2545 return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
2546 }
2547
2548
2549 #ifdef TENSILICA_DEBUG
2550
2551 /* For debugging, print out the mapping of opcode numbers to opcodes. */
2552
2553 static void
2554 xtensa_print_insn_table (void)
2555 {
2556 int num_opcodes, num_operands;
2557 xtensa_opcode opcode;
2558 xtensa_isa isa = xtensa_default_isa;
2559
2560 num_opcodes = xtensa_isa_num_opcodes (xtensa_default_isa);
2561 for (opcode = 0; opcode < num_opcodes; opcode++)
2562 {
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");
2582 }
2583 }
2584
2585
2586 static void
2587 print_vliw_insn (xtensa_insnbuf vbuf)
2588 {
2589 xtensa_isa isa = xtensa_default_isa;
2590 xtensa_format f = xtensa_format_decode (isa, vbuf);
2591 xtensa_insnbuf sbuf = xtensa_insnbuf_alloc (isa);
2592 int op;
2593
2594 fprintf (stderr, "format = %d\n", f);
2595
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);
2622 }
2623
2624 #endif /* TENSILICA_DEBUG */
2625
2626
2627 static bfd_boolean
2628 is_direct_call_opcode (xtensa_opcode opcode)
2629 {
2630 xtensa_isa isa = xtensa_default_isa;
2631 int n, num_operands;
2632
2633 if (xtensa_opcode_is_call (isa, opcode) == 0)
2634 return FALSE;
2635
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;
2644 }
2645
2646
2647 /* Convert from BFD relocation type code to slot and operand number.
2648 Returns non-zero on failure. */
2649
2650 static int
2651 decode_reloc (bfd_reloc_code_real_type reloc, int *slot, bfd_boolean *is_alt)
2652 {
2653 if (reloc >= BFD_RELOC_XTENSA_SLOT0_OP
2654 && reloc <= BFD_RELOC_XTENSA_SLOT14_OP)
2655 {
2656 *slot = reloc - BFD_RELOC_XTENSA_SLOT0_OP;
2657 *is_alt = FALSE;
2658 }
2659 else if (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT
2660 && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT)
2661 {
2662 *slot = reloc - BFD_RELOC_XTENSA_SLOT0_ALT;
2663 *is_alt = TRUE;
2664 }
2665 else
2666 return -1;
2667
2668 return 0;
2669 }
2670
2671
2672 /* Convert from slot number to BFD relocation type code for the
2673 standard PC-relative relocations. Return BFD_RELOC_NONE on
2674 failure. */
2675
2676 static bfd_reloc_code_real_type
2677 encode_reloc (int slot)
2678 {
2679 if (slot < 0 || slot > 14)
2680 return BFD_RELOC_NONE;
2681
2682 return BFD_RELOC_XTENSA_SLOT0_OP + slot;
2683 }
2684
2685
2686 /* Convert from slot numbers to BFD relocation type code for the
2687 "alternate" relocations. Return BFD_RELOC_NONE on failure. */
2688
2689 static bfd_reloc_code_real_type
2690 encode_alt_reloc (int slot)
2691 {
2692 if (slot < 0 || slot > 14)
2693 return BFD_RELOC_NONE;
2694
2695 return BFD_RELOC_XTENSA_SLOT0_ALT + slot;
2696 }
2697
2698
2699 static void
2700 xtensa_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)
2708 {
2709 uint32 valbuf = value;
2710
2711 if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf))
2712 {
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;
2723 }
2724
2725 xtensa_operand_set_field (xtensa_default_isa, opcode, operand, fmt, slot,
2726 slotbuf, valbuf);
2727 }
2728
2729
2730 static uint32
2731 xtensa_insnbuf_get_operand (xtensa_insnbuf slotbuf,
2732 xtensa_format fmt,
2733 int slot,
2734 xtensa_opcode opcode,
2735 int opnum)
2736 {
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;
2742 }
2743
2744 \f
2745 /* Checks for rules from xtensa-relax tables. */
2746
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. */
2752
2753 static bfd_boolean
2754 xg_instruction_matches_option_term (TInsn *insn ATTRIBUTE_UNUSED,
2755 const ReqOrOption *option)
2756 {
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 }
2769 }
2770
2771
2772 static bfd_boolean
2773 xg_instruction_matches_or_options (TInsn *insn,
2774 const ReqOrOptionList *or_option)
2775 {
2776 const ReqOrOption *option;
2777 /* Must match each of the AND terms. */
2778 for (option = or_option; option != NULL; option = option->next)
2779 {
2780 if (xg_instruction_matches_option_term (insn, option))
2781 return TRUE;
2782 }
2783 return FALSE;
2784 }
2785
2786
2787 static bfd_boolean
2788 xg_instruction_matches_options (TInsn *insn, const ReqOptionList *options)
2789 {
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)
2795 {
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;
2800 }
2801 return TRUE;
2802 }
2803
2804
2805 /* Return the transition rule that matches or NULL if none matches. */
2806
2807 static bfd_boolean
2808 xg_instruction_matches_rule (TInsn *insn, TransitionRule *rule)
2809 {
2810 PreconditionList *condition_l;
2811
2812 if (rule->opcode != insn->opcode)
2813 return FALSE;
2814
2815 for (condition_l = rule->conditions;
2816 condition_l != NULL;
2817 condition_l = condition_l->next)
2818 {
2819 expressionS *exp1;
2820 expressionS *exp2;
2821 Precondition *cond = condition_l->precond;
2822
2823 switch (cond->typ)
2824 {
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
2897 static int
2898 transition_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
2964 static TransitionRule *
2965 xg_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
2984 static bfd_boolean
2985 is_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
2995 static int
2996 xg_get_build_instr_size (BuildInstr *insn)
2997 {
2998 assert (insn->typ == INSTR_INSTR);
2999 return xg_get_single_size (insn->opcode);
3000 }
3001
3002
3003 static bfd_boolean
3004 xg_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
3035 static bfd_boolean
3036 xg_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
3069 static int
3070 xg_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:
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
3115 static int
3116 xg_get_max_insn_widen_literal_size (xtensa_opcode opcode)
3117 {
3118 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
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:
3144 /* Hard-coded 4-byte literal. */
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
3160 static bfd_boolean
3161 xg_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
3185 static symbolS *
3186 get_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
3196 static symbolS *
3197 get_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
3207 static bfd_boolean
3208 xg_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
3228 static bfd_boolean
3229 xg_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
3241 static bfd_boolean
3242 xg_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
3276 static bfd_boolean
3277 xg_symbolic_immeds_fit (const TInsn *insn,
3278 segT pc_seg,
3279 fragS *pc_frag,
3280 offsetT pc_offset,
3281 long stretch)
3282 {
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;
3290
3291 assert (insn->insn_type == ITYPE_INSN);
3292
3293 for (i = 0; i < n; ++i)
3294 {
3295 const expressionS *expr = &insn->tok[i];
3296 if (xtensa_operand_is_register (isa, insn->opcode, i) == 1)
3297 continue;
3298
3299 switch (expr->X_op)
3300 {
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;
3306
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;
3313
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;
3319
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;
3325
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;
3332
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;
3337
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;
3364 }
3365
3366
3367 /* Return TRUE on success. */
3368
3369 static bfd_boolean
3370 xg_build_to_insn (TInsn *targ, TInsn *insn, BuildInstr *bi)
3371 {
3372 BuildOp *op;
3373 symbolS *sym;
3374
3375 memset (targ, 0, sizeof (TInsn));
3376 targ->loc = insn->loc;
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;
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;
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);
3489 /* We can only pass resolvable literals through. */
3490 if (!xg_valid_literal_expression (&insn->tok[op_data]))
3491 return FALSE;
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;
3509 /* Literal with no ops is a label? */
3510 assert (op == NULL);
3511 break;
3512
3513 default:
3514 assert (0);
3515 }
3516
3517 return TRUE;
3518 }
3519
3520
3521 /* Return TRUE on success. */
3522
3523 static bfd_boolean
3524 xg_build_to_stack (IStack *istack, TInsn *insn, BuildInstr *bi)
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
3537 /* Return TRUE on valid expansion. */
3538
3539 static bfd_boolean
3540 xg_expand_to_stack (IStack *istack, TInsn *insn, int lateral_steps)
3541 {
3542 int stack_size = istack->ninsn;
3543 int steps_taken = 0;
3544 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
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
3586 static bfd_boolean
3587 xg_expand_narrow (TInsn *targ, TInsn *insn)
3588 {
3589 TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
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? */
3602 if (xg_get_single_size (insn->opcode)
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
3613 \f
3614 /* Relax the assembly instruction at least "min_steps".
3615 Return the number of steps taken. */
3616
3617 static int
3618 xg_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 */
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 }
3646 current_insn = *insn;
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 }
3665 current_insn = single_target;
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
3696 static void
3697 xg_force_frag_space (int size)
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
3706 static void
3707 xg_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)
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;
3719
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,
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);
3733
3734 /* Just to make sure that we did not split it up. */
3735 assert (old_frag->fr_next == frag_now);
3736 }
3737
3738
3739 /* Return TRUE if the target frag is one of the next non-empty frags. */
3740
3741 static bfd_boolean
3742 is_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
3765 static bfd_boolean
3766 is_branch_jmp_to_next (TInsn *insn, fragS *fragP)
3767 {
3768 xtensa_isa isa = xtensa_default_isa;
3769 int i;
3770 int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
3771 int target_op = -1;
3772 symbolS *sym;
3773 fragS *target_frag;
3774
3775 if (xtensa_opcode_is_branch (isa, insn->opcode) == 0
3776 && xtensa_opcode_is_jump (isa, insn->opcode) == 0)
3777 return FALSE;
3778
3779 for (i = 0; i < num_ops; i++)
3780 {
3781 if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1)
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
3815 static void
3816 xg_add_branch_and_loop_targets (TInsn *insn)
3817 {
3818 xtensa_isa isa = xtensa_default_isa;
3819 int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
3820
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 }
3829
3830 if (xtensa_opcode_is_branch (isa, insn->opcode) == 1
3831 || xtensa_opcode_is_loop (isa, insn->opcode) == 1)
3832 {
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 }
3846 }
3847 }
3848
3849
3850 /* Return FALSE if no error. */
3851
3852 static bfd_boolean
3853 xg_build_token_insn (BuildInstr *instr_spec, TInsn *old_insn, TInsn *new_insn)
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;
3864 new_insn->loc = old_insn->loc;
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;
3870 new_insn->loc = old_insn->loc;
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
3916 /* Return TRUE if it was simplified. */
3917
3918 static bfd_boolean
3919 xg_simplify_insn (TInsn *old_insn, TInsn *new_insn)
3920 {
3921 TransitionRule *rule;
3922 BuildInstr *insn_spec;
3923
3924 if (old_insn->is_specific_opcode || !density_supported)
3925 return FALSE;
3926
3927 rule = xg_instruction_match (old_insn);
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
3947 multiple instructions/literals onto the stack. Return FALSE if no
3948 error. */
3949
3950 static bfd_boolean
3951 xg_expand_assembly_insn (IStack *istack, TInsn *orig_insn)
3952 {
3953 int noperands;
3954 TInsn new_insn;
3955 memset (&new_insn, 0, sizeof (TInsn));
3956
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;
3961
3962 noperands = xtensa_opcode_num_operands (xtensa_default_isa,
3963 orig_insn->opcode);
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
3978 /* If there are not enough operands, we will assert above. If there
3979 are too many, just cut out the extras here. */
3980
3981 orig_insn->ntok = noperands;
3982
3983 /* Cases:
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).
3991 Also close out the fragment if the fixup may cause instruction expansion.
3992
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)
3997 Multiple of these may be possible on the same fragment.
3998 If so, make sure to satisfy the required alignment.
3999 Then try to get the desired alignment. */
4000
4001 if (tinsn_has_invalid_symbolic_operands (orig_insn))
4002 return TRUE;
4003
4004 if (orig_insn->is_specific_opcode || !use_transform ())
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
4025 return FALSE;
4026 }
4027
4028
4029 /* Return TRUE if the section flags are marked linkonce
4030 or the name is .gnu.linkonce*. */
4031
4032 static bfd_boolean
4033 get_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
4052 static void
4053 xtensa_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
4064 static symbolS *
4065 xtensa_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
4093 /* Currently all literals that are generated here are 32-bit L32R targets. */
4094
4095 static symbolS *
4096 xg_assemble_literal (/* const */ TInsn *insn)
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;
4106 expressionS * emit_val;
4107
4108 set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
4109
4110 assert (insn->insn_type == ITYPE_LITERAL);
4111 assert (insn->ntok == 1); /* must be only one token here */
4112
4113 xtensa_switch_to_literal_fragment (&state);
4114
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
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);
4133 record_alignment (now_seg, litalign);
4134
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);
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
4161 static void
4162 xg_assemble_literal_space (/* const */ int size, int slot)
4163 {
4164 emit_state state;
4165 /* We might have to do something about this alignment. It only
4166 takes effect if something is placed here. */
4167 offsetT litalign = 2; /* 2^2 = 4 */
4168 fragS *lit_saved_frag;
4169
4170 assert (size % 4 == 0);
4171
4172 xtensa_switch_to_literal_fragment (&state);
4173
4174 /* Force a 4-byte align here. */
4175 frag_align (litalign, 0, 0);
4176 record_alignment (now_seg, litalign);
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);
4184 xg_finish_frag (0, RELAX_LITERAL, 0, size, FALSE);
4185
4186 /* Go back. */
4187 xtensa_restore_emit_state (&state);
4188 frag_now->tc_frag_data.literal_frags[slot] = lit_saved_frag;
4189 }
4190
4191
4192 /* Put in a fixup record based on the opcode.
4193 Return TRUE on success. */
4194
4195 static bfd_boolean
4196 xg_add_opcode_fix (TInsn *tinsn,
4197 int opnum,
4198 xtensa_format fmt,
4199 int slot,
4200 expressionS *expr,
4201 fragS *fragP,
4202 offsetT offset)
4203 {
4204 xtensa_opcode opcode = tinsn->opcode;
4205 bfd_reloc_code_real_type reloc;
4206 reloc_howto_type *howto;
4207 int fmt_length;
4208 fixS *the_fix;
4209
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))
4233 {
4234 as_bad (_("invalid relocation for operand %i of '%s'"),
4235 opnum, xtensa_opcode_name (xtensa_default_isa, opcode));
4236 return FALSE;
4237 }
4238
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 }
4257
4258 howto = bfd_reloc_type_lookup (stdoutput, reloc);
4259 if (!howto)
4260 {
4261 as_bad (_("undefined symbol for opcode \"%s\""),
4262 xtensa_opcode_name (xtensa_default_isa, opcode));
4263 return FALSE;
4264 }
4265
4266 fmt_length = xtensa_format_length (xtensa_default_isa, fmt);
4267 the_fix = fix_new_exp (fragP, offset, fmt_length, expr,
4268 howto->pc_relative, reloc);
4269
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
4283 static bfd_boolean
4284 xg_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];
4303
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;
4310 }
4311
4312
4313 static void
4314 xg_resolve_literals (TInsn *insn, symbolS *lit_sym)
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
4328 static void
4329 xg_resolve_labels (TInsn *insn, symbolS *label_sym)
4330 {
4331 symbolS *sym = get_special_label_symbol ();
4332 int i;
4333 /* assert (!insn->is_literal); */
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
4341 /* Return TRUE if the instruction can write to the specified
4342 integer register. */
4343
4344 static bfd_boolean
4345 is_register_writer (const TInsn *insn, const char *regset, int regnum)
4346 {
4347 int i;
4348 int num_ops;
4349 xtensa_isa isa = xtensa_default_isa;
4350
4351 num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
4352
4353 for (i = 0; i < num_ops; i++)
4354 {
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)
4359 {
4360 xtensa_regfile opnd_rf =
4361 xtensa_operand_regfile (isa, insn->opcode, i);
4362 if (!strcmp (xtensa_regfile_shortname (isa, opnd_rf), regset))
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
4374 static bfd_boolean
4375 is_bad_loopend_opcode (const TInsn *tinsn)
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
4395 || opcode == xtensa_waiti_opcode
4396 || opcode == xtensa_rsr_lcount_opcode)
4397 return TRUE;
4398
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
4408 static bfd_boolean
4409 is_unaligned_label (symbolS *sym)
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
4423 if (name
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
4435 static fragS *
4436 next_non_empty_frag (const fragS *fragP)
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
4452 static bfd_boolean
4453 next_frag_opcode_is_loop (const fragS *fragP, xtensa_opcode *opcode)
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
4471 static int
4472 frag_format_size (const fragS *fragP)
4473 {
4474 static xtensa_insnbuf insnbuf = NULL;
4475 xtensa_isa isa = xtensa_default_isa;
4476 xtensa_format fmt;
4477 int fmt_size;
4478
4479 if (!insnbuf)
4480 insnbuf = xtensa_insnbuf_alloc (isa);
4481
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)
4489 return XTENSA_UNDEFINED;
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];
4521
4522 return fmt_size;
4523 }
4524
4525
4526 static int
4527 next_frag_format_size (const fragS *fragP)
4528 {
4529 const fragS *next_fragP = next_non_empty_frag (fragP);
4530 return frag_format_size (next_fragP);
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
4537 static void
4538 update_next_frag_state (fragS *fragP)
4539 {
4540 fragS *next_fragP = fragP->fr_next;
4541 fragS *new_target = NULL;
4542
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
4567 while (next_fragP && next_fragP->fr_fix == 0)
4568 {
4569 if (next_fragP->fr_type == rs_machine_dependent
4570 && next_fragP->fr_subtype == RELAX_LOOP_END)
4571 {
4572 next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP;
4573 return;
4574 }
4575
4576 next_fragP = next_fragP->fr_next;
4577 }
4578 }
4579
4580
4581 static bfd_boolean
4582 next_frag_is_branch_target (const fragS *fragP)
4583 {
4584 /* Sometimes an empty will end up here due to storage allocation issues,
4585 so we have to skip until we find something legit. */
4586 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4587 {
4588 if (fragP->tc_frag_data.is_branch_target)
4589 return TRUE;
4590 if (fragP->fr_fix != 0)
4591 break;
4592 }
4593 return FALSE;
4594 }
4595
4596
4597 static bfd_boolean
4598 next_frag_is_loop_target (const fragS *fragP)
4599 {
4600 /* Sometimes an empty will end up here due storage allocation issues.
4601 So we have to skip until we find something legit. */
4602 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4603 {
4604 if (fragP->tc_frag_data.is_loop_target)
4605 return TRUE;
4606 if (fragP->fr_fix != 0)
4607 break;
4608 }
4609 return FALSE;
4610 }
4611
4612
4613 static addressT
4614 next_frag_pre_opcode_bytes (const fragS *fragp)
4615 {
4616 const fragS *next_fragp = fragp->fr_next;
4617 xtensa_opcode next_opcode;
4618
4619 if (!next_frag_opcode_is_loop (fragp, &next_opcode))
4620 return 0;
4621
4622 /* Sometimes an empty will end up here due to storage allocation issues,
4623 so we have to skip until we find something legit. */
4624 while (next_fragp->fr_fix == 0)
4625 next_fragp = next_fragp->fr_next;
4626
4627 if (next_fragp->fr_type != rs_machine_dependent)
4628 return 0;
4629
4630 /* There is some implicit knowledge encoded in here.
4631 The LOOP instructions that are NOT RELAX_IMMED have
4632 been relaxed. Note that we can assume that the LOOP
4633 instruction is in slot 0 because loops aren't bundleable. */
4634 if (next_fragp->tc_frag_data.slot_subtypes[0] > RELAX_IMMED)
4635 return get_expanded_loop_offset (next_opcode);
4636
4637 return 0;
4638 }
4639
4640
4641 /* Mark a location where we can later insert literal frags. Update
4642 the section's literal_pool_loc, so subsequent literals can be
4643 placed nearest to their use. */
4644
4645 static void
4646 xtensa_mark_literal_pool_location (void)
4647 {
4648 /* Any labels pointing to the current location need
4649 to be adjusted to after the literal pool. */
4650 emit_state s;
4651 fragS *pool_location;
4652
4653 if (use_literal_section && !directive_state[directive_absolute_literals])
4654 return;
4655
4656 frag_align (2, 0, 0);
4657 record_alignment (now_seg, 2);
4658
4659 /* We stash info in the fr_var of these frags
4660 so we can later move the literal's fixes into this
4661 frchain's fix list. We can use fr_var because fr_var's
4662 interpretation depends solely on the fr_type and subtype. */
4663 pool_location = frag_now;
4664 frag_variant (rs_machine_dependent, 0, (int) frchain_now,
4665 RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL);
4666 xtensa_set_frag_assembly_state (frag_now);
4667 frag_variant (rs_machine_dependent, 0, (int) now_seg,
4668 RELAX_LITERAL_POOL_END, NULL, 0, NULL);
4669 xtensa_set_frag_assembly_state (frag_now);
4670
4671 /* Now put a frag into the literal pool that points to this location. */
4672 set_literal_pool_location (now_seg, pool_location);
4673 xtensa_switch_to_non_abs_literal_fragment (&s);
4674 frag_align (2, 0, 0);
4675 record_alignment (now_seg, 2);
4676
4677 /* Close whatever frag is there. */
4678 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4679 xtensa_set_frag_assembly_state (frag_now);
4680 frag_now->tc_frag_data.literal_frag = pool_location;
4681 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4682 xtensa_restore_emit_state (&s);
4683 xtensa_set_frag_assembly_state (frag_now);
4684 }
4685
4686
4687 /* Build a nop of the correct size into tinsn. */
4688
4689 static void
4690 build_nop (TInsn *tinsn, int size)
4691 {
4692 tinsn_init (tinsn);
4693 switch (size)
4694 {
4695 case 2:
4696 tinsn->opcode = xtensa_nop_n_opcode;
4697 tinsn->ntok = 0;
4698 if (tinsn->opcode == XTENSA_UNDEFINED)
4699 as_fatal (_("opcode 'NOP.N' unavailable in this configuration"));
4700 break;
4701
4702 case 3:
4703 if (xtensa_nop_opcode == XTENSA_UNDEFINED)
4704 {
4705 tinsn->opcode = xtensa_or_opcode;
4706 set_expr_const (&tinsn->tok[0], 1);
4707 set_expr_const (&tinsn->tok[1], 1);
4708 set_expr_const (&tinsn->tok[2], 1);
4709 tinsn->ntok = 3;
4710 }
4711 else
4712 tinsn->opcode = xtensa_nop_opcode;
4713
4714 assert (tinsn->opcode != XTENSA_UNDEFINED);
4715 }
4716 }
4717
4718
4719 /* Assemble a NOP of the requested size in the buffer. User must have
4720 allocated "buf" with at least "size" bytes. */
4721
4722 static void
4723 assemble_nop (size_t size, char *buf)
4724 {
4725 static xtensa_insnbuf insnbuf = NULL;
4726 TInsn tinsn;
4727
4728 build_nop (&tinsn, size);
4729
4730 if (!insnbuf)
4731 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4732
4733 tinsn_to_insnbuf (&tinsn, insnbuf);
4734 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf, 0);
4735 }
4736
4737
4738 /* Return the number of bytes for the offset of the expanded loop
4739 instruction. This should be incorporated into the relaxation
4740 specification but is hard-coded here. This is used to auto-align
4741 the loop instruction. It is invalid to call this function if the
4742 configuration does not have loops or if the opcode is not a loop
4743 opcode. */
4744
4745 static addressT
4746 get_expanded_loop_offset (xtensa_opcode opcode)
4747 {
4748 /* This is the OFFSET of the loop instruction in the expanded loop.
4749 This MUST correspond directly to the specification of the loop
4750 expansion. It will be validated on fragment conversion. */
4751 assert (opcode != XTENSA_UNDEFINED);
4752 if (opcode == xtensa_loop_opcode)
4753 return 0;
4754 if (opcode == xtensa_loopnez_opcode)
4755 return 3;
4756 if (opcode == xtensa_loopgtz_opcode)
4757 return 6;
4758 as_fatal (_("get_expanded_loop_offset: invalid opcode"));
4759 return 0;
4760 }
4761
4762
4763 static fragS *
4764 get_literal_pool_location (segT seg)
4765 {
4766 return seg_info (seg)->tc_segment_info_data.literal_pool_loc;
4767 }
4768
4769
4770 static void
4771 set_literal_pool_location (segT seg, fragS *literal_pool_loc)
4772 {
4773 seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc;
4774 }
4775
4776
4777 /* Set frag assembly state should be called when a new frag is
4778 opened and after a frag has been closed. */
4779
4780 static void
4781 xtensa_set_frag_assembly_state (fragS *fragP)
4782 {
4783 if (!density_supported)
4784 fragP->tc_frag_data.is_no_density = TRUE;
4785
4786 /* This function is called from subsegs_finish, which is called
4787 after xtensa_end, so we can't use "use_transform" or
4788 "use_schedule" here. */
4789 if (!directive_state[directive_transform])
4790 fragP->tc_frag_data.is_no_transform = TRUE;
4791 fragP->tc_frag_data.use_absolute_literals =
4792 directive_state[directive_absolute_literals];
4793 fragP->tc_frag_data.is_assembly_state_set = TRUE;
4794 }
4795
4796
4797 static bfd_boolean
4798 relaxable_section (asection *sec)
4799 {
4800 return (sec->flags & SEC_DEBUGGING) == 0;
4801 }
4802
4803
4804 static void
4805 xtensa_find_unmarked_state_frags (void)
4806 {
4807 segT *seclist;
4808
4809 /* Walk over each fragment of all of the current segments. For each
4810 unmarked fragment, mark it with the same info as the previous
4811 fragment. */
4812 for (seclist = &stdoutput->sections;
4813 seclist && *seclist;
4814 seclist = &(*seclist)->next)
4815 {
4816 segT sec = *seclist;
4817 segment_info_type *seginfo;
4818 fragS *fragP;
4819 flagword flags;
4820 flags = bfd_get_section_flags (stdoutput, sec);
4821 if (flags & SEC_DEBUGGING)
4822 continue;
4823 if (!(flags & SEC_ALLOC))
4824 continue;
4825
4826 seginfo = seg_info (sec);
4827 if (seginfo && seginfo->frchainP)
4828 {
4829 fragS *last_fragP = 0;
4830 for (fragP = seginfo->frchainP->frch_root; fragP;
4831 fragP = fragP->fr_next)
4832 {
4833 if (fragP->fr_fix != 0
4834 && !fragP->tc_frag_data.is_assembly_state_set)
4835 {
4836 if (last_fragP == 0)
4837 {
4838 as_warn_where (fragP->fr_file, fragP->fr_line,
4839 _("assembly state not set for first frag in section %s"),
4840 sec->name);
4841 }
4842 else
4843 {
4844 fragP->tc_frag_data.is_assembly_state_set = TRUE;
4845 fragP->tc_frag_data.is_no_density =
4846 last_fragP->tc_frag_data.is_no_density;
4847 fragP->tc_frag_data.is_no_transform =
4848 last_fragP->tc_frag_data.is_no_transform;
4849 fragP->tc_frag_data.use_absolute_literals =
4850 last_fragP->tc_frag_data.use_absolute_literals;
4851 }
4852 }
4853 if (fragP->tc_frag_data.is_assembly_state_set)
4854 last_fragP = fragP;
4855 }
4856 }
4857 }
4858 }
4859
4860
4861 static void
4862 xtensa_find_unaligned_branch_targets (bfd *abfd ATTRIBUTE_UNUSED,
4863 asection *sec,
4864 void *unused ATTRIBUTE_UNUSED)
4865 {
4866 flagword flags = bfd_get_section_flags (abfd, sec);
4867 segment_info_type *seginfo = seg_info (sec);
4868 fragS *frag = seginfo->frchainP->frch_root;
4869
4870 if (flags & SEC_CODE)
4871 {
4872 xtensa_isa isa = xtensa_default_isa;
4873 xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
4874 while (frag != NULL)
4875 {
4876 if (frag->tc_frag_data.is_branch_target)
4877 {
4878 int op_size;
4879 int frag_addr;
4880 xtensa_format fmt;
4881
4882 xtensa_insnbuf_from_chars (isa, insnbuf, frag->fr_literal, 0);
4883 fmt = xtensa_format_decode (isa, insnbuf);
4884 op_size = xtensa_format_length (isa, fmt);
4885 frag_addr = frag->fr_address % xtensa_fetch_width;
4886 if (frag_addr + op_size > (int) xtensa_fetch_width)
4887 as_warn_where (frag->fr_file, frag->fr_line,
4888 _("unaligned branch target: %d bytes at 0x%lx"),
4889 op_size, frag->fr_address);
4890 }
4891 frag = frag->fr_next;
4892 }
4893 xtensa_insnbuf_free (isa, insnbuf);
4894 }
4895 }
4896
4897
4898 static void
4899 xtensa_find_unaligned_loops (bfd *abfd ATTRIBUTE_UNUSED,
4900 asection *sec,
4901 void *unused ATTRIBUTE_UNUSED)
4902 {
4903 flagword flags = bfd_get_section_flags (abfd, sec);
4904 segment_info_type *seginfo = seg_info (sec);
4905 fragS *frag = seginfo->frchainP->frch_root;
4906 xtensa_isa isa = xtensa_default_isa;
4907
4908 if (flags & SEC_CODE)
4909 {
4910 xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
4911 while (frag != NULL)
4912 {
4913 if (frag->tc_frag_data.is_first_loop_insn)
4914 {
4915 int op_size;
4916 int frag_addr;
4917 xtensa_format fmt;
4918
4919 xtensa_insnbuf_from_chars (isa, insnbuf, frag->fr_literal, 0);
4920 fmt = xtensa_format_decode (isa, insnbuf);
4921 op_size = xtensa_format_length (isa, fmt);
4922 frag_addr = frag->fr_address % xtensa_fetch_width;
4923
4924 if (frag_addr + op_size > (signed) xtensa_fetch_width)
4925 as_warn_where (frag->fr_file, frag->fr_line,
4926 _("unaligned loop: %d bytes at 0x%lx"),
4927 op_size, frag->fr_address);
4928 }
4929 frag = frag->fr_next;
4930 }
4931 xtensa_insnbuf_free (isa, insnbuf);
4932 }
4933 }
4934
4935
4936 static void
4937 xg_apply_tentative_value (fixS *fixP, valueT val)
4938 {
4939 xtensa_isa isa = xtensa_default_isa;
4940 static xtensa_insnbuf insnbuf = NULL;
4941 static xtensa_insnbuf slotbuf = NULL;
4942 xtensa_format fmt;
4943 int slot;
4944 bfd_boolean alt_reloc;
4945 xtensa_opcode opcode;
4946 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
4947
4948 (void) decode_reloc (fixP->fx_r_type, &slot, &alt_reloc);
4949 if (alt_reloc)
4950 as_fatal (_("unexpected fix"));
4951
4952 if (!insnbuf)
4953 {
4954 insnbuf = xtensa_insnbuf_alloc (isa);
4955 slotbuf = xtensa_insnbuf_alloc (isa);
4956 }
4957
4958 xtensa_insnbuf_from_chars (isa, insnbuf, fixpos, 0);
4959 fmt = xtensa_format_decode (isa, insnbuf);
4960 if (fmt == XTENSA_UNDEFINED)
4961 as_fatal (_("undecodable fix"));
4962 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
4963 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
4964 if (opcode == XTENSA_UNDEFINED)
4965 as_fatal (_("undecodable fix"));
4966
4967 /* CONST16 immediates are not PC-relative, despite the fact that we
4968 reuse the normal PC-relative operand relocations for the low part
4969 of a CONST16 operand. The code in tc_gen_reloc does not decode
4970 the opcodes so it is more convenient to detect this special case
4971 here. */
4972 if (opcode == xtensa_const16_opcode)
4973 return;
4974
4975 xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode,
4976 get_relaxable_immed (opcode), val,
4977 fixP->fx_file, fixP->fx_line);
4978
4979 xtensa_format_set_slot (isa, fmt, slot, insnbuf, slotbuf);
4980 xtensa_insnbuf_to_chars (isa, insnbuf, fixpos, 0);
4981 }
4982
4983 \f
4984 /* External Functions and Other GAS Hooks. */
4985
4986 const char *
4987 xtensa_target_format (void)
4988 {
4989 return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le");
4990 }
4991
4992
4993 void
4994 xtensa_file_arch_init (bfd *abfd)
4995 {
4996 bfd_set_private_flags (abfd, 0x100 | 0x200);
4997 }
4998
4999
5000 void
5001 md_number_to_chars (char *buf, valueT val, int n)
5002 {
5003 if (target_big_endian)
5004 number_to_chars_bigendian (buf, val, n);
5005 else
5006 number_to_chars_littleendian (buf, val, n);
5007 }
5008
5009
5010 /* This function is called once, at assembler startup time. It should
5011 set up all the tables, etc. that the MD part of the assembler will
5012 need. */
5013
5014 void
5015 md_begin (void)
5016 {
5017 segT current_section = now_seg;
5018 int current_subsec = now_subseg;
5019 xtensa_isa isa;
5020
5021 xtensa_default_isa = xtensa_isa_init (0, 0);
5022 isa = xtensa_default_isa;
5023
5024 linkrelax = 1;
5025
5026 /* Set up the .literal, .fini.literal and .init.literal sections. */
5027 memset (&default_lit_sections, 0, sizeof (default_lit_sections));
5028 default_lit_sections.init_lit_seg_name = INIT_LITERAL_SECTION_NAME;
5029 default_lit_sections.fini_lit_seg_name = FINI_LITERAL_SECTION_NAME;
5030 default_lit_sections.lit_seg_name = LITERAL_SECTION_NAME;
5031 default_lit_sections.lit4_seg_name = LIT4_SECTION_NAME;
5032
5033 subseg_set (current_section, current_subsec);
5034
5035 xg_init_vinsn (&cur_vinsn);
5036
5037 xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi");
5038 xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi");
5039 xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0");
5040 xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4");
5041 xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8");
5042 xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12");
5043 xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0");
5044 xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4");
5045 xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8");
5046 xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12");
5047 xtensa_const16_opcode = xtensa_opcode_lookup (isa, "const16");
5048 xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry");
5049 xtensa_movi_opcode = xtensa_opcode_lookup (isa, "movi");
5050 xtensa_movi_n_opcode = xtensa_opcode_lookup (isa, "movi.n");
5051 xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync");
5052 xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx");
5053 xtensa_l32r_opcode = xtensa_opcode_lookup (isa, "l32r");
5054 xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop");
5055 xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez");
5056 xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz");
5057 xtensa_nop_opcode = xtensa_opcode_lookup (isa, "nop");
5058 xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n");
5059 xtensa_or_opcode = xtensa_opcode_lookup (isa, "or");
5060 xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret");
5061 xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n");
5062 xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw");
5063 xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n");
5064 xtensa_rsr_lcount_opcode = xtensa_opcode_lookup (isa, "rsr.lcount");
5065 xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti");
5066
5067 init_op_placement_info_table ();
5068
5069 /* Set up the assembly state. */
5070 if (!frag_now->tc_frag_data.is_assembly_state_set)
5071 xtensa_set_frag_assembly_state (frag_now);
5072 }
5073
5074
5075 /* TC_INIT_FIX_DATA hook */
5076
5077 void
5078 xtensa_init_fix_data (fixS *x)
5079 {
5080 x->tc_fix_data.slot = 0;
5081 x->tc_fix_data.X_add_symbol = NULL;
5082 x->tc_fix_data.X_add_number = 0;
5083 }
5084
5085
5086 /* tc_frob_label hook */
5087
5088 void
5089 xtensa_frob_label (symbolS *sym)
5090 {
5091 /* Since the label was already attached to a frag associated with the
5092 previous basic block, it now needs to be reset to the current frag. */
5093 symbol_set_frag (sym, frag_now);
5094 S_SET_VALUE (sym, (valueT) frag_now_fix ());
5095
5096 if (generating_literals)
5097 xtensa_add_literal_sym (sym);
5098 else
5099 xtensa_add_insn_label (sym);
5100
5101 if (symbol_get_tc (sym)->is_loop_target
5102 && (get_last_insn_flags (now_seg, now_subseg)
5103 & FLAG_IS_BAD_LOOPEND) != 0)
5104 as_bad (_("invalid last instruction for a zero-overhead loop"));
5105
5106 /* No target aligning in the absolute section. */
5107 if (now_seg != absolute_section
5108 && do_align_targets ()
5109 && !is_unaligned_label (sym)
5110 && !generating_literals)
5111 {
5112 float freq = get_subseg_target_freq (now_seg, now_subseg);
5113 xtensa_set_frag_assembly_state (frag_now);
5114
5115 /* The only time this type of frag grows is when there is a
5116 negatable branch that needs to be relaxed as the last
5117 instruction in a zero-overhead loop. Because alignment frags
5118 are so common, marking them all as possibly growing four
5119 bytes makes any worst-case analysis appear much worse than it
5120 is. So, we make fr_var not actually reflect the amount of
5121 memory allocated at the end of this frag, but rather the
5122 amount of memory this frag might grow. The "4, 0" below
5123 allocates four bytes at the end of the frag for room to grow
5124 if we need to relax a loop end with a NOP. Frags prior to
5125 this one might grow to align this one, but the frag itself
5126 won't grow unless it meets the condition above. */
5127
5128 #define RELAX_LOOP_END_BYTES 4
5129
5130 frag_var (rs_machine_dependent,
5131 RELAX_LOOP_END_BYTES, (int) freq,
5132 RELAX_DESIRE_ALIGN_IF_TARGET,
5133 frag_now->fr_symbol, frag_now->fr_offset, NULL);
5134 xtensa_set_frag_assembly_state (frag_now);
5135 xtensa_move_labels (frag_now, 0, TRUE);
5136 }
5137
5138 /* We need to mark the following properties even if we aren't aligning. */
5139
5140 /* If the label is already known to be a branch target, i.e., a
5141 forward branch, mark the frag accordingly. Backward branches
5142 are handled by xg_add_branch_and_loop_targets. */
5143 if (symbol_get_tc (sym)->is_branch_target)
5144 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
5145
5146 /* Loops only go forward, so they can be identified here. */
5147 if (symbol_get_tc (sym)->is_loop_target)
5148 symbol_get_frag (sym)->tc_frag_data.is_loop_target = TRUE;
5149 }
5150
5151
5152 /* tc_unrecognized_line hook */
5153
5154 int
5155 xtensa_unrecognized_line (int ch)
5156 {
5157 switch (ch)
5158 {
5159 case '{' :
5160 if (cur_vinsn.inside_bundle == 0)
5161 {
5162 /* PR8110: Cannot emit line number info inside a FLIX bundle
5163 when using --gstabs. Temporarily disable debug info. */
5164 generate_lineno_debug ();
5165 if (debug_type == DEBUG_STABS)
5166 {
5167 xt_saved_debug_type = debug_type;
5168 debug_type = DEBUG_NONE;
5169 }
5170
5171 cur_vinsn.inside_bundle = 1;
5172 }
5173 else
5174 {
5175 as_bad (_("extra opening brace"));
5176 return 0;
5177 }
5178 break;
5179
5180 case '}' :
5181 if (cur_vinsn.inside_bundle)
5182 finish_vinsn (&cur_vinsn);
5183 else
5184 {
5185 as_bad (_("extra closing brace"));
5186 return 0;
5187 }
5188 break;
5189 default:
5190 as_bad (_("syntax error"));
5191 return 0;
5192 }
5193 return 1;
5194 }
5195
5196
5197 /* md_flush_pending_output hook */
5198
5199 void
5200 xtensa_flush_pending_output (void)
5201 {
5202 if (cur_vinsn.inside_bundle)
5203 as_bad (_("missing closing brace"));
5204
5205 /* If there is a non-zero instruction fragment, close it. */
5206 if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn)
5207 {
5208 frag_wane (frag_now);
5209 frag_new (0);
5210 xtensa_set_frag_assembly_state (frag_now);
5211 }
5212 frag_now->tc_frag_data.is_insn = FALSE;
5213
5214 xtensa_clear_insn_labels ();
5215 }
5216
5217
5218 /* We had an error while parsing an instruction. The string might look
5219 like this: "insn arg1, arg2 }". If so, we need to see the closing
5220 brace and reset some fields. Otherwise, the vinsn never gets closed
5221 and the num_slots field will grow past the end of the array of slots,
5222 and bad things happen. */
5223
5224 static void
5225 error_reset_cur_vinsn (void)
5226 {
5227 if (cur_vinsn.inside_bundle)
5228 {
5229 if (*input_line_pointer == '}'
5230 || *(input_line_pointer - 1) == '}'
5231 || *(input_line_pointer - 2) == '}')
5232 xg_clear_vinsn (&cur_vinsn);
5233 }
5234 }
5235
5236
5237 void
5238 md_assemble (char *str)
5239 {
5240 xtensa_isa isa = xtensa_default_isa;
5241 char *opname;
5242 unsigned opnamelen;
5243 bfd_boolean has_underbar = FALSE;
5244 char *arg_strings[MAX_INSN_ARGS];
5245 int num_args;
5246 TInsn orig_insn; /* Original instruction from the input. */
5247
5248 tinsn_init (&orig_insn);
5249
5250 /* Split off the opcode. */
5251 opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
5252 opname = xmalloc (opnamelen + 1);
5253 memcpy (opname, str, opnamelen);
5254 opname[opnamelen] = '\0';
5255
5256 num_args = tokenize_arguments (arg_strings, str + opnamelen);
5257 if (num_args == -1)
5258 {
5259 as_bad (_("syntax error"));
5260 return;
5261 }
5262
5263 if (xg_translate_idioms (&opname, &num_args, arg_strings))
5264 return;
5265
5266 /* Check for an underbar prefix. */
5267 if (*opname == '_')
5268 {
5269 has_underbar = TRUE;
5270 opname += 1;
5271 }
5272
5273 orig_insn.insn_type = ITYPE_INSN;
5274 orig_insn.ntok = 0;
5275 orig_insn.is_specific_opcode = (has_underbar || !use_transform ());
5276
5277 orig_insn.opcode = xtensa_opcode_lookup (isa, opname);
5278 if (orig_insn.opcode == XTENSA_UNDEFINED)
5279 {
5280 xtensa_format fmt = xtensa_format_lookup (isa, opname);
5281 if (fmt == XTENSA_UNDEFINED)
5282 {
5283 as_bad (_("unknown opcode or format name '%s'"), opname);
5284 error_reset_cur_vinsn ();
5285 return;
5286 }
5287 if (!cur_vinsn.inside_bundle)
5288 {
5289 as_bad (_("format names only valid inside bundles"));
5290 error_reset_cur_vinsn ();
5291 return;
5292 }
5293 if (cur_vinsn.format != XTENSA_UNDEFINED)
5294 as_warn (_("multiple formats specified for one bundle; using '%s'"),
5295 opname);
5296 cur_vinsn.format = fmt;
5297 free (has_underbar ? opname - 1 : opname);
5298 error_reset_cur_vinsn ();
5299 return;
5300 }
5301
5302 /* Special case: The call instructions should be marked "specific opcode"
5303 to keep them from expanding. */
5304 if (!use_longcalls () && is_direct_call_opcode (orig_insn.opcode))
5305 orig_insn.is_specific_opcode = TRUE;
5306
5307 /* Parse the arguments. */
5308 if (parse_arguments (&orig_insn, num_args, arg_strings))
5309 {
5310 as_bad (_("syntax error"));
5311 error_reset_cur_vinsn ();
5312 return;
5313 }
5314
5315 /* Free the opcode and argument strings, now that they've been parsed. */
5316 free (has_underbar ? opname - 1 : opname);
5317 opname = 0;
5318 while (num_args-- > 0)
5319 free (arg_strings[num_args]);
5320
5321 /* Get expressions for invisible operands. */
5322 if (get_invisible_operands (&orig_insn))
5323 {
5324 error_reset_cur_vinsn ();
5325 return;
5326 }
5327
5328 /* Check for the right number and type of arguments. */
5329 if (tinsn_check_arguments (&orig_insn))
5330 {
5331 error_reset_cur_vinsn ();
5332 return;
5333 }
5334
5335 dwarf2_where (&orig_insn.loc);
5336
5337 xg_add_branch_and_loop_targets (&orig_insn);
5338
5339 /* Special-case for "entry" instruction. */
5340 if (orig_insn.opcode == xtensa_entry_opcode)
5341 {
5342 /* Check that the third opcode (#2) is >= 16. */
5343 if (orig_insn.ntok >= 3)
5344 {
5345 expressionS *exp = &orig_insn.tok[2];
5346 switch (exp->X_op)
5347 {
5348 case O_constant:
5349 if (exp->X_add_number < 16)
5350 as_warn (_("entry instruction with stack decrement < 16"));
5351 break;
5352
5353 default:
5354 as_warn (_("entry instruction with non-constant decrement"));
5355 }
5356 }
5357 }
5358
5359 /* Finish it off:
5360 assemble_tokens (opcode, tok, ntok);
5361 expand the tokens from the orig_insn into the
5362 stack of instructions that will not expand
5363 unless required at relaxation time. */
5364
5365 if (!cur_vinsn.inside_bundle)
5366 emit_single_op (&orig_insn);
5367 else /* We are inside a bundle. */
5368 {
5369 cur_vinsn.slots[cur_vinsn.num_slots] = orig_insn;
5370 cur_vinsn.num_slots++;
5371 if (*input_line_pointer == '}'
5372 || *(input_line_pointer - 1) == '}'
5373 || *(input_line_pointer - 2) == '}')
5374 finish_vinsn (&cur_vinsn);
5375 }
5376
5377 /* We've just emitted a new instruction so clear the list of labels. */
5378 xtensa_clear_insn_labels ();
5379 }
5380
5381
5382 /* HANDLE_ALIGN hook */
5383
5384 /* For a .align directive, we mark the previous block with the alignment
5385 information. This will be placed in the object file in the
5386 property section corresponding to this section. */
5387
5388 void
5389 xtensa_handle_align (fragS *fragP)
5390 {
5391 if (linkrelax
5392 && ! fragP->tc_frag_data.is_literal
5393 && (fragP->fr_type == rs_align
5394 || fragP->fr_type == rs_align_code)
5395 && fragP->fr_address + fragP->fr_fix > 0
5396 && fragP->fr_offset > 0
5397 && now_seg != bss_section)
5398 {
5399 fragP->tc_frag_data.is_align = TRUE;
5400 fragP->tc_frag_data.alignment = fragP->fr_offset;
5401 }
5402
5403 if (fragP->fr_type == rs_align_test)
5404 {
5405 int count;
5406 count = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
5407 if (count != 0)
5408 as_bad_where (fragP->fr_file, fragP->fr_line,
5409 _("unaligned entry instruction"));
5410 }
5411 }
5412
5413
5414 /* TC_FRAG_INIT hook */
5415
5416 void
5417 xtensa_frag_init (fragS *frag)
5418 {
5419 xtensa_set_frag_assembly_state (frag);
5420 }
5421
5422
5423 symbolS *
5424 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
5425 {
5426 return NULL;
5427 }
5428
5429
5430 /* Round up a section size to the appropriate boundary. */
5431
5432 valueT
5433 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
5434 {
5435 return size; /* Byte alignment is fine. */
5436 }
5437
5438
5439 long
5440 md_pcrel_from (fixS *fixP)
5441 {
5442 char *insn_p;
5443 static xtensa_insnbuf insnbuf = NULL;
5444 static xtensa_insnbuf slotbuf = NULL;
5445 int opnum;
5446 uint32 opnd_value;
5447 xtensa_opcode opcode;
5448 xtensa_format fmt;
5449 int slot;
5450 xtensa_isa isa = xtensa_default_isa;
5451 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
5452 bfd_boolean alt_reloc;
5453
5454 if (fixP->fx_done)
5455 return addr;
5456
5457 if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND)
5458 return addr;
5459
5460 if (!insnbuf)
5461 {
5462 insnbuf = xtensa_insnbuf_alloc (isa);
5463 slotbuf = xtensa_insnbuf_alloc (isa);
5464 }
5465
5466 insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where];
5467 xtensa_insnbuf_from_chars (isa, insnbuf, insn_p, 0);
5468 fmt = xtensa_format_decode (isa, insnbuf);
5469
5470 if (fmt == XTENSA_UNDEFINED)
5471 as_fatal (_("bad instruction format"));
5472
5473 if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc) != 0)
5474 as_fatal (_("invalid relocation"));
5475
5476 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
5477 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
5478
5479 /* Check for "alternate" relocation (operand not specified). */
5480 if (alt_reloc || opcode == xtensa_const16_opcode)
5481 {
5482 if (opcode != xtensa_l32r_opcode
5483 && opcode != xtensa_const16_opcode)
5484 as_fatal (_("invalid relocation for '%s' instruction"),
5485 xtensa_opcode_name (isa, opcode));
5486 return addr;
5487 }
5488
5489 opnum = get_relaxable_immed (opcode);
5490 opnd_value = 0;
5491 if (xtensa_operand_is_PCrelative (isa, opcode, opnum) != 1
5492 || xtensa_operand_do_reloc (isa, opcode, opnum, &opnd_value, addr))
5493 {
5494 as_bad_where (fixP->fx_file,
5495 fixP->fx_line,
5496 _("invalid relocation for operand %d of '%s'"),
5497 opnum, xtensa_opcode_name (isa, opcode));
5498 return addr;
5499 }
5500 return 0 - opnd_value;
5501 }
5502
5503
5504 /* TC_FORCE_RELOCATION hook */
5505
5506 int
5507 xtensa_force_relocation (fixS *fix)
5508 {
5509 switch (fix->fx_r_type)
5510 {
5511 case BFD_RELOC_XTENSA_SLOT0_ALT:
5512 case BFD_RELOC_XTENSA_SLOT1_ALT:
5513 case BFD_RELOC_XTENSA_SLOT2_ALT:
5514 case BFD_RELOC_XTENSA_SLOT3_ALT:
5515 case BFD_RELOC_XTENSA_SLOT4_ALT:
5516 case BFD_RELOC_XTENSA_SLOT5_ALT:
5517 case BFD_RELOC_XTENSA_SLOT6_ALT:
5518 case BFD_RELOC_XTENSA_SLOT7_ALT:
5519 case BFD_RELOC_XTENSA_SLOT8_ALT:
5520 case BFD_RELOC_XTENSA_SLOT9_ALT:
5521 case BFD_RELOC_XTENSA_SLOT10_ALT:
5522 case BFD_RELOC_XTENSA_SLOT11_ALT:
5523 case BFD_RELOC_XTENSA_SLOT12_ALT:
5524 case BFD_RELOC_XTENSA_SLOT13_ALT:
5525 case BFD_RELOC_XTENSA_SLOT14_ALT:
5526 case BFD_RELOC_VTABLE_INHERIT:
5527 case BFD_RELOC_VTABLE_ENTRY:
5528 return 1;
5529 default:
5530 break;
5531 }
5532
5533 if (linkrelax && fix->fx_addsy
5534 && relaxable_section (S_GET_SEGMENT (fix->fx_addsy)))
5535 return 1;
5536
5537 return generic_force_reloc (fix);
5538 }
5539
5540
5541 /* NO_PSEUDO_DOT hook */
5542
5543 /* This function has nothing to do with pseudo dots, but this is the
5544 nearest macro to where the check needs to take place. FIXME: This
5545 seems wrong. */
5546
5547 bfd_boolean
5548 xtensa_check_inside_bundle (void)
5549 {
5550 if (cur_vinsn.inside_bundle && input_line_pointer[-1] == '.')
5551 as_bad (_("directives are not valid inside bundles"));
5552
5553 /* This function must always return FALSE because it is called via a
5554 macro that has nothing to do with bundling. */
5555 return FALSE;
5556 }
5557
5558
5559 /* md_elf_section_change_hook */
5560
5561 void
5562 xtensa_elf_section_change_hook (void)
5563 {
5564 /* Set up the assembly state. */
5565 if (!frag_now->tc_frag_data.is_assembly_state_set)
5566 xtensa_set_frag_assembly_state (frag_now);
5567 }
5568
5569
5570 /* tc_fix_adjustable hook */
5571
5572 bfd_boolean
5573 xtensa_fix_adjustable (fixS *fixP)
5574 {
5575 /* An offset is not allowed in combination with the difference of two
5576 symbols, but that cannot be easily detected after a local symbol
5577 has been adjusted to a (section+offset) form. Return 0 so that such
5578 an fix will not be adjusted. */
5579 if (fixP->fx_subsy && fixP->fx_addsy && fixP->fx_offset
5580 && relaxable_section (S_GET_SEGMENT (fixP->fx_subsy)))
5581 return 0;
5582
5583 /* We need the symbol name for the VTABLE entries. */
5584 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
5585 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5586 return 0;
5587
5588 if (fixP->fx_addsy
5589 && (S_IS_EXTERNAL (fixP->fx_addsy) || S_IS_WEAK (fixP->fx_addsy)))
5590 return 0;
5591
5592 #if 0
5593 /* We may someday want to enable this code to preserve relocations for
5594 non-PC-relative fixes, possibly under control of a PIC flag. */
5595 return (fixP->fx_pcrel
5596 || (fixP->fx_subsy != NULL
5597 && (S_GET_SEGMENT (fixP->fx_subsy)
5598 == S_GET_SEGMENT (fixP->fx_addsy)))
5599 || S_IS_LOCAL (fixP->fx_addsy));
5600 #else
5601 return 1;
5602 #endif
5603 }
5604
5605
5606 void
5607 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
5608 {
5609 if (fixP->fx_pcrel == 0 && fixP->fx_addsy == 0)
5610 {
5611 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
5612
5613 switch (fixP->fx_r_type)
5614 {
5615 case BFD_RELOC_XTENSA_ASM_EXPAND:
5616 fixP->fx_done = 1;
5617 break;
5618
5619 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
5620 as_bad (_("unhandled local relocation fix %s"),
5621 bfd_get_reloc_code_name (fixP->fx_r_type));
5622 break;
5623
5624 case BFD_RELOC_32:
5625 case BFD_RELOC_16:
5626 case BFD_RELOC_8:
5627 /* The only one we support that isn't an instruction field. */
5628 md_number_to_chars (fixpos, *valP, fixP->fx_size);
5629 fixP->fx_done = 1;
5630 break;
5631
5632 case BFD_RELOC_VTABLE_INHERIT:
5633 case BFD_RELOC_VTABLE_ENTRY:
5634 fixP->fx_done = 0;
5635 break;
5636
5637 default:
5638 as_bad (_("unhandled local relocation fix %s"),
5639 bfd_get_reloc_code_name (fixP->fx_r_type));
5640 }
5641 }
5642 }
5643
5644
5645 char *
5646 md_atof (int type, char *litP, int *sizeP)
5647 {
5648 int prec;
5649 LITTLENUM_TYPE words[4];
5650 char *t;
5651 int i;
5652
5653 switch (type)
5654 {
5655 case 'f':
5656 prec = 2;
5657 break;
5658
5659 case 'd':
5660 prec = 4;
5661 break;
5662
5663 default:
5664 *sizeP = 0;
5665 return "bad call to md_atof";
5666 }
5667
5668 t = atof_ieee (input_line_pointer, type, words);
5669 if (t)
5670 input_line_pointer = t;
5671
5672 *sizeP = prec * 2;
5673
5674 for (i = prec - 1; i >= 0; i--)
5675 {
5676 int idx = i;
5677 if (target_big_endian)
5678 idx = (prec - 1 - i);
5679
5680 md_number_to_chars (litP, (valueT) words[idx], 2);
5681 litP += 2;
5682 }
5683
5684 return NULL;
5685 }
5686
5687
5688 int
5689 md_estimate_size_before_relax (fragS *fragP, segT seg ATTRIBUTE_UNUSED)
5690 {
5691 return total_frag_text_expansion (fragP);
5692 }
5693
5694
5695 /* Translate internal representation of relocation info to BFD target
5696 format. */
5697
5698 arelent *
5699 tc_gen_reloc (asection *section, fixS *fixp)
5700 {
5701 arelent *reloc;
5702 bfd_boolean apply_tentative_value = FALSE;
5703
5704 reloc = (arelent *) xmalloc (sizeof (arelent));
5705 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5706 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5707 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
5708
5709 /* Make sure none of our internal relocations make it this far.
5710 They'd better have been fully resolved by this point. */
5711 assert ((int) fixp->fx_r_type > 0);
5712
5713 if (linkrelax && fixp->fx_subsy
5714 && (fixp->fx_r_type == BFD_RELOC_8
5715 || fixp->fx_r_type == BFD_RELOC_16
5716 || fixp->fx_r_type == BFD_RELOC_32))
5717 {
5718 int diff_size = 0;
5719 bfd_vma diff_value, diff_mask = 0;
5720
5721 switch (fixp->fx_r_type)
5722 {
5723 case BFD_RELOC_8:
5724 fixp->fx_r_type = BFD_RELOC_XTENSA_DIFF8;
5725 diff_size = 1;
5726 diff_mask = 0xff;
5727 break;
5728 case BFD_RELOC_16:
5729 fixp->fx_r_type = BFD_RELOC_XTENSA_DIFF16;
5730 diff_size = 2;
5731 diff_mask = 0xffff;
5732 break;
5733 case BFD_RELOC_32:
5734 fixp->fx_r_type = BFD_RELOC_XTENSA_DIFF32;
5735 diff_size = 4;
5736 diff_mask = 0xffffffff;
5737 break;
5738 default:
5739 break;
5740 }
5741
5742 /* An offset is only allowed when it results from adjusting a local
5743 symbol into a section-relative offset. If the offset came from the
5744 original expression, tc_fix_adjustable will have prevented the fix
5745 from being converted to a section-relative form so that we can flag
5746 the error here. */
5747 if (fixp->fx_offset != 0 && !symbol_section_p (fixp->fx_addsy))
5748 {
5749 as_bad_where (fixp->fx_file, fixp->fx_line,
5750 _("cannot represent subtraction with an offset"));
5751 free (reloc->sym_ptr_ptr);
5752 free (reloc);
5753 return NULL;
5754 }
5755
5756 assert (S_GET_SEGMENT (fixp->fx_addsy)
5757 == S_GET_SEGMENT (fixp->fx_subsy));
5758
5759 diff_value = (S_GET_VALUE (fixp->fx_addsy) + fixp->fx_offset
5760 - S_GET_VALUE (fixp->fx_subsy));
5761
5762 /* Check for overflow. */
5763 if ((diff_value & ~diff_mask) != 0)
5764 {
5765 as_bad_where (fixp->fx_file, fixp->fx_line,
5766 _("value of %ld too large"), diff_value);
5767 free (reloc->sym_ptr_ptr);
5768 free (reloc);
5769 return NULL;
5770 }
5771
5772 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
5773 diff_value, diff_size);
5774 reloc->addend = fixp->fx_offset - diff_value;
5775 }
5776 else
5777 {
5778 reloc->addend = fixp->fx_offset;
5779
5780 switch (fixp->fx_r_type)
5781 {
5782 case BFD_RELOC_XTENSA_SLOT0_OP:
5783 case BFD_RELOC_XTENSA_SLOT1_OP:
5784 case BFD_RELOC_XTENSA_SLOT2_OP:
5785 case BFD_RELOC_XTENSA_SLOT3_OP:
5786 case BFD_RELOC_XTENSA_SLOT4_OP:
5787 case BFD_RELOC_XTENSA_SLOT5_OP:
5788 case BFD_RELOC_XTENSA_SLOT6_OP:
5789 case BFD_RELOC_XTENSA_SLOT7_OP:
5790 case BFD_RELOC_XTENSA_SLOT8_OP:
5791 case BFD_RELOC_XTENSA_SLOT9_OP:
5792 case BFD_RELOC_XTENSA_SLOT10_OP:
5793 case BFD_RELOC_XTENSA_SLOT11_OP:
5794 case BFD_RELOC_XTENSA_SLOT12_OP:
5795 case BFD_RELOC_XTENSA_SLOT13_OP:
5796 case BFD_RELOC_XTENSA_SLOT14_OP:
5797 /* As a special case, the immediate value for a CONST16 opcode
5798 should not be applied, since this kind of relocation is
5799 handled specially for CONST16 and is not really PC-relative.
5800 Rather than decode the opcode here, just wait and handle it
5801 in xg_apply_tentative_value. */
5802 apply_tentative_value = TRUE;
5803 break;
5804
5805 case BFD_RELOC_XTENSA_SLOT0_ALT:
5806 case BFD_RELOC_XTENSA_SLOT1_ALT:
5807 case BFD_RELOC_XTENSA_SLOT2_ALT:
5808 case BFD_RELOC_XTENSA_SLOT3_ALT:
5809 case BFD_RELOC_XTENSA_SLOT4_ALT:
5810 case BFD_RELOC_XTENSA_SLOT5_ALT:
5811 case BFD_RELOC_XTENSA_SLOT6_ALT:
5812 case BFD_RELOC_XTENSA_SLOT7_ALT:
5813 case BFD_RELOC_XTENSA_SLOT8_ALT:
5814 case BFD_RELOC_XTENSA_SLOT9_ALT:
5815 case BFD_RELOC_XTENSA_SLOT10_ALT:
5816 case BFD_RELOC_XTENSA_SLOT11_ALT:
5817 case BFD_RELOC_XTENSA_SLOT12_ALT:
5818 case BFD_RELOC_XTENSA_SLOT13_ALT:
5819 case BFD_RELOC_XTENSA_SLOT14_ALT:
5820 case BFD_RELOC_XTENSA_ASM_EXPAND:
5821 case BFD_RELOC_32:
5822 case BFD_RELOC_XTENSA_PLT:
5823 case BFD_RELOC_VTABLE_INHERIT:
5824 case BFD_RELOC_VTABLE_ENTRY:
5825 break;
5826
5827 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
5828 as_warn (_("emitting simplification relocation"));
5829 break;
5830
5831 default:
5832 as_warn (_("emitting unknown relocation"));
5833 }
5834 }
5835
5836 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
5837 if (reloc->howto == NULL)
5838 {
5839 as_bad_where (fixp->fx_file, fixp->fx_line,
5840 _("cannot represent `%s' relocation in object file"),
5841 bfd_get_reloc_code_name (fixp->fx_r_type));
5842 free (reloc->sym_ptr_ptr);
5843 free (reloc);
5844 return NULL;
5845 }
5846
5847 if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
5848 as_fatal (_("internal error? cannot generate `%s' relocation"),
5849 bfd_get_reloc_code_name (fixp->fx_r_type));
5850
5851 /* Write the tentative value of a PC-relative relocation to a local symbol
5852 into the instruction. The value will be ignored by the linker, and it
5853 makes the object file disassembly readable when the linkrelax flag is
5854 set and all branch targets are encoded in relocations. */
5855
5856 if (linkrelax && apply_tentative_value && fixp->fx_pcrel)
5857 {
5858 valueT val;
5859 assert (fixp->fx_addsy);
5860 if (S_GET_SEGMENT (fixp->fx_addsy) == section && !fixp->fx_plt
5861 && !S_FORCE_RELOC (fixp->fx_addsy, 1))
5862 {
5863 val = (S_GET_VALUE (fixp->fx_addsy) + fixp->fx_offset
5864 - md_pcrel_from (fixp));
5865 xg_apply_tentative_value (fixp, val);
5866 }
5867 }
5868
5869 return reloc;
5870 }
5871
5872 \f
5873 /* Checks for resource conflicts between instructions. */
5874
5875 /* The func unit stuff could be implemented as bit-vectors rather
5876 than the iterative approach here. If it ends up being too
5877 slow, we will switch it. */
5878
5879 resource_table *
5880 new_resource_table (void *data,
5881 int cycles,
5882 int nu,
5883 unit_num_copies_func uncf,
5884 opcode_num_units_func onuf,
5885 opcode_funcUnit_use_unit_func ouuf,
5886 opcode_funcUnit_use_stage_func ousf)
5887 {
5888 int i;
5889 resource_table *rt = (resource_table *) xmalloc (sizeof (resource_table));
5890 rt->data = data;
5891 rt->cycles = cycles;
5892 rt->allocated_cycles = cycles;
5893 rt->num_units = nu;
5894 rt->unit_num_copies = uncf;
5895 rt->opcode_num_units = onuf;
5896 rt->opcode_unit_use = ouuf;
5897 rt->opcode_unit_stage = ousf;
5898
5899 rt->units = (char **) xcalloc (cycles, sizeof (char *));
5900 for (i = 0; i < cycles; i++)
5901 rt->units[i] = (char *) xcalloc (nu, sizeof (char));
5902
5903 return rt;
5904 }
5905
5906
5907 void
5908 clear_resource_table (resource_table *rt)
5909 {
5910 int i, j;
5911 for (i = 0; i < rt->allocated_cycles; i++)
5912 for (j = 0; j < rt->num_units; j++)
5913 rt->units[i][j] = 0;
5914 }
5915
5916
5917 /* We never shrink it, just fake it into thinking so. */
5918
5919 void
5920 resize_resource_table (resource_table *rt, int cycles)
5921 {
5922 int i, old_cycles;
5923
5924 rt->cycles = cycles;
5925 if (cycles <= rt->allocated_cycles)
5926 return;
5927
5928 old_cycles = rt->allocated_cycles;
5929 rt->allocated_cycles = cycles;
5930
5931 rt->units = xrealloc (rt->units, sizeof (char *) * rt->allocated_cycles);
5932 for (i = 0; i < old_cycles; i++)
5933 rt->units[i] = xrealloc (rt->units[i], sizeof (char) * rt->num_units);
5934 for (i = old_cycles; i < cycles; i++)
5935 rt->units[i] = xcalloc (rt->num_units, sizeof (char));
5936 }
5937
5938
5939 bfd_boolean
5940 resources_available (resource_table *rt, xtensa_opcode opcode, int cycle)
5941 {
5942 int i;
5943 int uses = (rt->opcode_num_units) (rt->data, opcode);
5944
5945 for (i = 0; i < uses; i++)
5946 {
5947 xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
5948 int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
5949 int copies_in_use = rt->units[stage + cycle][unit];
5950 int copies = (rt->unit_num_copies) (rt->data, unit);
5951 if (copies_in_use >= copies)
5952 return FALSE;
5953 }
5954 return TRUE;
5955 }
5956
5957
5958 void
5959 reserve_resources (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 /* Note that this allows resources to be oversubscribed. That's
5969 essential to the way the optional scheduler works.
5970 resources_available reports when a resource is over-subscribed,
5971 so it's easy to tell. */
5972 rt->units[stage + cycle][unit]++;
5973 }
5974 }
5975
5976
5977 void
5978 release_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 rt->units[stage + cycle][unit]--;
5988 assert (rt->units[stage + cycle][unit] >= 0);
5989 }
5990 }
5991
5992
5993 /* Wrapper functions make parameterized resource reservation
5994 more convenient. */
5995
5996 int
5997 opcode_funcUnit_use_unit (void *data, xtensa_opcode opcode, int idx)
5998 {
5999 xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
6000 return use->unit;
6001 }
6002
6003
6004 int
6005 opcode_funcUnit_use_stage (void *data, xtensa_opcode opcode, int idx)
6006 {
6007 xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
6008 return use->stage;
6009 }
6010
6011
6012 /* Note that this function does not check issue constraints, but
6013 solely whether the hardware is available to execute the given
6014 instructions together. It also doesn't check if the tinsns
6015 write the same state, or access the same tieports. That is
6016 checked by check_t1_t2_read_write. */
6017
6018 static bfd_boolean
6019 resources_conflict (vliw_insn *vinsn)
6020 {
6021 int i;
6022 static resource_table *rt = NULL;
6023
6024 /* This is the most common case by far. Optimize it. */
6025 if (vinsn->num_slots == 1)
6026 return FALSE;
6027
6028 if (rt == NULL)
6029 {
6030 xtensa_isa isa = xtensa_default_isa;
6031 rt = new_resource_table
6032 (isa, xtensa_isa_num_pipe_stages (isa),
6033 xtensa_isa_num_funcUnits (isa),
6034 (unit_num_copies_func) xtensa_funcUnit_num_copies,
6035 (opcode_num_units_func) xtensa_opcode_num_funcUnit_uses,
6036 opcode_funcUnit_use_unit,
6037 opcode_funcUnit_use_stage);
6038 }
6039
6040 clear_resource_table (rt);
6041
6042 for (i = 0; i < vinsn->num_slots; i++)
6043 {
6044 if (!resources_available (rt, vinsn->slots[i].opcode, 0))
6045 return TRUE;
6046 reserve_resources (rt, vinsn->slots[i].opcode, 0);
6047 }
6048
6049 return FALSE;
6050 }
6051
6052 \f
6053 /* finish_vinsn, emit_single_op and helper functions. */
6054
6055 static bfd_boolean find_vinsn_conflicts (vliw_insn *);
6056 static xtensa_format xg_find_narrowest_format (vliw_insn *);
6057 static void bundle_single_op (TInsn *);
6058 static void xg_assemble_vliw_tokens (vliw_insn *);
6059
6060
6061 /* We have reached the end of a bundle; emit into the frag. */
6062
6063 static void
6064 finish_vinsn (vliw_insn *vinsn)
6065 {
6066 IStack slotstack;
6067 int i;
6068 char *file_name;
6069 int line;
6070
6071 if (find_vinsn_conflicts (vinsn))
6072 return;
6073
6074 /* First, find a format that works. */
6075 if (vinsn->format == XTENSA_UNDEFINED)
6076 vinsn->format = xg_find_narrowest_format (vinsn);
6077
6078 if (vinsn->format == XTENSA_UNDEFINED)
6079 {
6080 as_where (&file_name, &line);
6081 as_bad_where (file_name, line,
6082 _("couldn't find a valid instruction format"));
6083 fprintf (stderr, _(" ops were: "));
6084 for (i = 0; i < vinsn->num_slots; i++)
6085 fprintf (stderr, _(" %s;"),
6086 xtensa_opcode_name (xtensa_default_isa,
6087 vinsn->slots[i].opcode));
6088 fprintf (stderr, _("\n"));
6089 xg_clear_vinsn (vinsn);
6090 return;
6091 }
6092
6093 if (vinsn->num_slots
6094 != xtensa_format_num_slots (xtensa_default_isa, vinsn->format))
6095 {
6096 as_bad (_("format '%s' allows %d slots, but there are %d opcodes"),
6097 xtensa_format_name (xtensa_default_isa, vinsn->format),
6098 xtensa_format_num_slots (xtensa_default_isa, vinsn->format),
6099 vinsn->num_slots);
6100 xg_clear_vinsn (vinsn);
6101 return;
6102 }
6103
6104 if (resources_conflict (vinsn))
6105 {
6106 as_where (&file_name, &line);
6107 as_bad_where (file_name, line, _("illegal resource usage in bundle"));
6108 fprintf (stderr, " ops were: ");
6109 for (i = 0; i < vinsn->num_slots; i++)
6110 fprintf (stderr, " %s;",
6111 xtensa_opcode_name (xtensa_default_isa,
6112 vinsn->slots[i].opcode));
6113 fprintf (stderr, "\n");
6114 xg_clear_vinsn (vinsn);
6115 return;
6116 }
6117
6118 for (i = 0; i < vinsn->num_slots; i++)
6119 {
6120 if (vinsn->slots[i].opcode != XTENSA_UNDEFINED)
6121 {
6122 symbolS *lit_sym = NULL;
6123 int j;
6124 bfd_boolean e = FALSE;
6125 bfd_boolean saved_density = density_supported;
6126
6127 /* We don't want to narrow ops inside multi-slot bundles. */
6128 if (vinsn->num_slots > 1)
6129 density_supported = FALSE;
6130
6131 istack_init (&slotstack);
6132 if (vinsn->slots[i].opcode == xtensa_nop_opcode)
6133 {
6134 vinsn->slots[i].opcode =
6135 xtensa_format_slot_nop_opcode (xtensa_default_isa,
6136 vinsn->format, i);
6137 vinsn->slots[i].ntok = 0;
6138 }
6139
6140 if (xg_expand_assembly_insn (&slotstack, &vinsn->slots[i]))
6141 {
6142 e = TRUE;
6143 continue;
6144 }
6145
6146 density_supported = saved_density;
6147
6148 if (e)
6149 {
6150 xg_clear_vinsn (vinsn);
6151 return;
6152 }
6153
6154 for (j = 0; j < slotstack.ninsn - 1; j++)
6155 {
6156 TInsn *insn = &slotstack.insn[j];
6157 if (insn->insn_type == ITYPE_LITERAL)
6158 {
6159 assert (lit_sym == NULL);
6160 lit_sym = xg_assemble_literal (insn);
6161 }
6162 else
6163 {
6164 if (lit_sym)
6165 xg_resolve_literals (insn, lit_sym);
6166 emit_single_op (insn);
6167 }
6168 }
6169
6170 if (vinsn->num_slots > 1)
6171 {
6172 if (opcode_fits_format_slot
6173 (slotstack.insn[slotstack.ninsn - 1].opcode,
6174 vinsn->format, i))
6175 {
6176 vinsn->slots[i] = slotstack.insn[slotstack.ninsn - 1];
6177 }
6178 else
6179 {
6180 bundle_single_op (&slotstack.insn[slotstack.ninsn - 1]);
6181 if (vinsn->format == XTENSA_UNDEFINED)
6182 vinsn->slots[i].opcode = xtensa_nop_opcode;
6183 else
6184 vinsn->slots[i].opcode
6185 = xtensa_format_slot_nop_opcode (xtensa_default_isa,
6186 vinsn->format, i);
6187
6188 vinsn->slots[i].ntok = 0;
6189 }
6190 }
6191 else
6192 {
6193 vinsn->slots[0] = slotstack.insn[slotstack.ninsn - 1];
6194 vinsn->format = XTENSA_UNDEFINED;
6195 }
6196 }
6197 }
6198
6199 /* Now check resource conflicts on the modified bundle. */
6200 if (resources_conflict (vinsn))
6201 {
6202 as_where (&file_name, &line);
6203 as_bad_where (file_name, line, _("illegal resource usage in bundle"));
6204 fprintf (stderr, " ops were: ");
6205 for (i = 0; i < vinsn->num_slots; i++)
6206 fprintf (stderr, " %s;",
6207 xtensa_opcode_name (xtensa_default_isa,
6208 vinsn->slots[i].opcode));
6209 fprintf (stderr, "\n");
6210 xg_clear_vinsn (vinsn);
6211 return;
6212 }
6213
6214 /* First, find a format that works. */
6215 if (vinsn->format == XTENSA_UNDEFINED)
6216 vinsn->format = xg_find_narrowest_format (vinsn);
6217
6218 xg_assemble_vliw_tokens (vinsn);
6219
6220 xg_clear_vinsn (vinsn);
6221 }
6222
6223
6224 /* Given an vliw instruction, what conflicts are there in register
6225 usage and in writes to states and queues?
6226
6227 This function does two things:
6228 1. Reports an error when a vinsn contains illegal combinations
6229 of writes to registers states or queues.
6230 2. Marks individual tinsns as not relaxable if the combination
6231 contains antidependencies.
6232
6233 Job 2 handles things like swap semantics in instructions that need
6234 to be relaxed. For example,
6235
6236 addi a0, a1, 100000
6237
6238 normally would be relaxed to
6239
6240 l32r a0, some_label
6241 add a0, a1, a0
6242
6243 _but_, if the above instruction is bundled with an a0 reader, e.g.,
6244
6245 { addi a0, a1, 10000 ; add a2, a0, a4 ; }
6246
6247 then we can't relax it into
6248
6249 l32r a0, some_label
6250 { add a0, a1, a0 ; add a2, a0, a4 ; }
6251
6252 because the value of a0 is trashed before the second add can read it. */
6253
6254 static char check_t1_t2_reads_and_writes (TInsn *, TInsn *);
6255
6256 static bfd_boolean
6257 find_vinsn_conflicts (vliw_insn *vinsn)
6258 {
6259 int i, j;
6260 int branches = 0;
6261 xtensa_isa isa = xtensa_default_isa;
6262
6263 assert (!past_xtensa_end);
6264
6265 for (i = 0 ; i < vinsn->num_slots; i++)
6266 {
6267 TInsn *op1 = &vinsn->slots[i];
6268 if (op1->is_specific_opcode)
6269 op1->keep_wide = TRUE;
6270 else
6271 op1->keep_wide = FALSE;
6272 }
6273
6274 for (i = 0 ; i < vinsn->num_slots; i++)
6275 {
6276 TInsn *op1 = &vinsn->slots[i];
6277
6278 if (xtensa_opcode_is_branch (isa, op1->opcode) == 1)
6279 branches++;
6280
6281 for (j = 0; j < vinsn->num_slots; j++)
6282 {
6283 if (i != j)
6284 {
6285 TInsn *op2 = &vinsn->slots[j];
6286 char conflict_type = check_t1_t2_reads_and_writes (op1, op2);
6287 switch (conflict_type)
6288 {
6289 case 'c':
6290 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same register"),
6291 xtensa_opcode_name (isa, op1->opcode), i,
6292 xtensa_opcode_name (isa, op2->opcode), j);
6293 return TRUE;
6294 case 'd':
6295 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same state"),
6296 xtensa_opcode_name (isa, op1->opcode), i,
6297 xtensa_opcode_name (isa, op2->opcode), j);
6298 return TRUE;
6299 case 'e':
6300 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same queue"),
6301 xtensa_opcode_name (isa, op1->opcode), i,
6302 xtensa_opcode_name (isa, op2->opcode), j);
6303 return TRUE;
6304 case 'f':
6305 as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) both have volatile queue accesses"),
6306 xtensa_opcode_name (isa, op1->opcode), i,
6307 xtensa_opcode_name (isa, op2->opcode), j);
6308 return TRUE;
6309 default:
6310 /* Everything is OK. */
6311 break;
6312 }
6313 op2->is_specific_opcode = (op2->is_specific_opcode
6314 || conflict_type == 'a');
6315 }
6316 }
6317 }
6318
6319 if (branches > 1)
6320 {
6321 as_bad (_("multiple branches or jumps in the same bundle"));
6322 return TRUE;
6323 }
6324
6325 return FALSE;
6326 }
6327
6328
6329 /* Check how the result registers of t1 and t2 relate.
6330 Cases found are:
6331
6332 case A: t1 reads a register t2 writes (an antidependency within a bundle)
6333 case B: no relationship between what is read and written (both could
6334 read the same reg though)
6335 case C: t1 writes a register t2 writes (a register conflict within a
6336 bundle)
6337 case D: t1 writes a state that t2 also writes
6338 case E: t1 writes a tie queue that t2 also writes
6339 case F: two volatile queue writes
6340 */
6341
6342 static char
6343 check_t1_t2_reads_and_writes (TInsn *t1, TInsn *t2)
6344 {
6345 xtensa_isa isa = xtensa_default_isa;
6346 xtensa_regfile t1_regfile, t2_regfile;
6347 int t1_reg, t2_reg;
6348 int t1_base_reg, t1_last_reg;
6349 int t2_base_reg, t2_last_reg;
6350 char t1_inout, t2_inout;
6351 int i, j;
6352 char conflict = 'b';
6353 int t1_states;
6354 int t2_states;
6355 int t1_interfaces;
6356 int t2_interfaces;
6357 bfd_boolean t1_volatile = FALSE;
6358 bfd_boolean t2_volatile = FALSE;
6359
6360 /* Check registers. */
6361 for (j = 0; j < t2->ntok; j++)
6362 {
6363 if (xtensa_operand_is_register (isa, t2->opcode, j) != 1)
6364 continue;
6365
6366 t2_regfile = xtensa_operand_regfile (isa, t2->opcode, j);
6367 t2_base_reg = t2->tok[j].X_add_number;
6368 t2_last_reg = t2_base_reg + xtensa_operand_num_regs (isa, t2->opcode, j);
6369
6370 for (i = 0; i < t1->ntok; i++)
6371 {
6372 if (xtensa_operand_is_register (isa, t1->opcode, i) != 1)
6373 continue;
6374
6375 t1_regfile = xtensa_operand_regfile (isa, t1->opcode, i);
6376
6377 if (t1_regfile != t2_regfile)
6378 continue;
6379
6380 t1_inout = xtensa_operand_inout (isa, t1->opcode, i);
6381 t2_inout = xtensa_operand_inout (isa, t2->opcode, j);
6382
6383 if (xtensa_operand_is_known_reg (isa, t1->opcode, i) == 0
6384 || xtensa_operand_is_known_reg (isa, t2->opcode, j) == 0)
6385 {
6386 if (t1_inout == 'm' || t1_inout == 'o'
6387 || t2_inout == 'm' || t2_inout == 'o')
6388 {
6389 conflict = 'a';
6390 continue;
6391 }
6392 }
6393
6394 t1_base_reg = t1->tok[i].X_add_number;
6395 t1_last_reg = (t1_base_reg
6396 + xtensa_operand_num_regs (isa, t1->opcode, i));
6397
6398 for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++)
6399 {
6400 for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++)
6401 {
6402 if (t1_reg != t2_reg)
6403 continue;
6404
6405 if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
6406 {
6407 conflict = 'a';
6408 continue;
6409 }
6410
6411 if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
6412 {
6413 conflict = 'a';
6414 continue;
6415 }
6416
6417 if (t1_inout != 'i' && t2_inout != 'i')
6418 return 'c';
6419 }
6420 }
6421 }
6422 }
6423
6424 /* Check states. */
6425 t1_states = xtensa_opcode_num_stateOperands (isa, t1->opcode);
6426 t2_states = xtensa_opcode_num_stateOperands (isa, t2->opcode);
6427 for (j = 0; j < t2_states; j++)
6428 {
6429 xtensa_state t2_so = xtensa_stateOperand_state (isa, t2->opcode, j);
6430 t2_inout = xtensa_stateOperand_inout (isa, t2->opcode, j);
6431 for (i = 0; i < t1_states; i++)
6432 {
6433 xtensa_state t1_so = xtensa_stateOperand_state (isa, t1->opcode, i);
6434 t1_inout = xtensa_stateOperand_inout (isa, t1->opcode, i);
6435 if (t1_so != t2_so)
6436 continue;
6437
6438 if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
6439 {
6440 conflict = 'a';
6441 continue;
6442 }
6443
6444 if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
6445 {
6446 conflict = 'a';
6447 continue;
6448 }
6449
6450 if (t1_inout != 'i' && t2_inout != 'i')
6451 return 'd';
6452 }
6453 }
6454
6455 /* Check tieports. */
6456 t1_interfaces = xtensa_opcode_num_interfaceOperands (isa, t1->opcode);
6457 t2_interfaces = xtensa_opcode_num_interfaceOperands (isa, t2->opcode);
6458 for (j = 0; j < t2_interfaces; j++)
6459 {
6460 xtensa_interface t2_int
6461 = xtensa_interfaceOperand_interface (isa, t2->opcode, j);
6462 t2_inout = xtensa_interface_inout (isa, j);
6463 if (xtensa_interface_has_side_effect (isa, t2_int) == 1
6464 && t2_inout != 'i')
6465 t2_volatile = TRUE;
6466 for (i = 0; i < t1_interfaces; i++)
6467 {
6468 xtensa_interface t1_int
6469 = xtensa_interfaceOperand_interface (isa, t1->opcode, j);
6470 t1_inout = xtensa_interface_inout (isa, i);
6471 if (xtensa_interface_has_side_effect (isa, t1_int) == 1
6472 && t1_inout != 'i')
6473 t1_volatile = TRUE;
6474
6475 if (t1_int != t2_int)
6476 continue;
6477
6478 if (t2_inout == 'i' && t1_inout == 'o')
6479 {
6480 conflict = 'a';
6481 continue;
6482 }
6483
6484 if (t1_inout == 'i' && t2_inout == 'o')
6485 {
6486 conflict = 'a';
6487 continue;
6488 }
6489
6490 if (t1_inout != 'i' && t2_inout != 'i')
6491 return 'e';
6492 }
6493 }
6494
6495 if (t1_volatile && t2_volatile)
6496 return 'f';
6497
6498 return conflict;
6499 }
6500
6501
6502 static xtensa_format
6503 xg_find_narrowest_format (vliw_insn *vinsn)
6504 {
6505 /* Right now we assume that the ops within the vinsn are properly
6506 ordered for the slots that the programmer wanted them in. In
6507 other words, we don't rearrange the ops in hopes of finding a
6508 better format. The scheduler handles that. */
6509
6510 xtensa_isa isa = xtensa_default_isa;
6511 xtensa_format format;
6512 vliw_insn v_copy = *vinsn;
6513 xtensa_opcode nop_opcode = xtensa_nop_opcode;
6514
6515 for (format = 0; format < xtensa_isa_num_formats (isa); format++)
6516 {
6517 v_copy = *vinsn;
6518 if (xtensa_format_num_slots (isa, format) == v_copy.num_slots)
6519 {
6520 int slot;
6521 int fit = 0;
6522 for (slot = 0; slot < v_copy.num_slots; slot++)
6523 {
6524 if (v_copy.slots[slot].opcode == nop_opcode)
6525 {
6526 v_copy.slots[slot].opcode =
6527 xtensa_format_slot_nop_opcode (isa, format, slot);
6528 v_copy.slots[slot].ntok = 0;
6529 }
6530
6531 if (opcode_fits_format_slot (v_copy.slots[slot].opcode,
6532 format, slot))
6533 fit++;
6534 else if (v_copy.num_slots > 1)
6535 {
6536 TInsn widened;
6537 /* Try the widened version. */
6538 if (!v_copy.slots[slot].keep_wide
6539 && !v_copy.slots[slot].is_specific_opcode
6540 && xg_is_narrow_insn (&v_copy.slots[slot])
6541 && !xg_expand_narrow (&widened, &v_copy.slots[slot])
6542 && opcode_fits_format_slot (widened.opcode,
6543 format, slot))
6544 {
6545 /* The xg_is_narrow clause requires some explanation:
6546
6547 addi can be "widened" to an addmi, which is then
6548 expanded to an addmi/addi pair if the immediate
6549 requires it, but here we must have a single widen
6550 only.
6551
6552 xg_is_narrow tells us that addi isn't really
6553 narrow. The widen_spec_list says that there are
6554 other cases. */
6555
6556 v_copy.slots[slot] = widened;
6557 fit++;
6558 }
6559 }
6560 }
6561 if (fit == v_copy.num_slots)
6562 {
6563 *vinsn = v_copy;
6564 xtensa_format_encode (isa, format, vinsn->insnbuf);
6565 vinsn->format = format;
6566 break;
6567 }
6568 }
6569 }
6570
6571 if (format == xtensa_isa_num_formats (isa))
6572 return XTENSA_UNDEFINED;
6573
6574 return format;
6575 }
6576
6577
6578 /* Return the additional space needed in a frag
6579 for possible relaxations of any ops in a VLIW insn.
6580 Also fill out the relaxations that might be required of
6581 each tinsn in the vinsn. */
6582
6583 static int
6584 relaxation_requirements (vliw_insn *vinsn)
6585 {
6586 int extra_space = 0;
6587 int slot;
6588
6589 for (slot = 0; slot < vinsn->num_slots; slot++)
6590 {
6591 TInsn *tinsn = &vinsn->slots[slot];
6592 if (!tinsn_has_symbolic_operands (tinsn))
6593 {
6594 /* A narrow instruction could be widened later to help
6595 alignment issues. */
6596 if (xg_is_narrow_insn (tinsn)
6597 && !tinsn->is_specific_opcode
6598 && vinsn->num_slots == 1)
6599 {
6600 /* Difference in bytes between narrow and wide insns... */
6601 extra_space += 1;
6602 tinsn->subtype = RELAX_NARROW;
6603 tinsn->record_fix = TRUE;
6604 break;
6605 }
6606 else
6607 {
6608 tinsn->record_fix = FALSE;
6609 /* No extra_space needed. */
6610 }
6611 }
6612 else
6613 {
6614 if (workaround_b_j_loop_end
6615 && tinsn->opcode == xtensa_jx_opcode
6616 && use_transform ())
6617 {
6618 /* Add 2 of these. */
6619 extra_space += 3; /* for the nop size */
6620 tinsn->subtype = RELAX_ADD_NOP_IF_PRE_LOOP_END;
6621 }
6622
6623 /* Need to assemble it with space for the relocation. */
6624 if (xg_is_relaxable_insn (tinsn, 0)
6625 && !tinsn->is_specific_opcode)
6626 {
6627 int max_size = xg_get_max_insn_widen_size (tinsn->opcode);
6628 int max_literal_size =
6629 xg_get_max_insn_widen_literal_size (tinsn->opcode);
6630
6631 tinsn->literal_space = max_literal_size;
6632
6633 tinsn->subtype = RELAX_IMMED;
6634 tinsn->record_fix = FALSE;
6635 extra_space += max_size;
6636 }
6637 else
6638 {
6639 tinsn->record_fix = TRUE;
6640 /* No extra space needed. */
6641 }
6642 }
6643 }
6644 return extra_space;
6645 }
6646
6647
6648 static void
6649 bundle_single_op (TInsn *orig_insn)
6650 {
6651 xtensa_isa isa = xtensa_default_isa;
6652 vliw_insn v;
6653 int slot;
6654
6655 xg_init_vinsn (&v);
6656 v.format = op_placement_table[orig_insn->opcode].narrowest;
6657 assert (v.format != XTENSA_UNDEFINED);
6658 v.num_slots = xtensa_format_num_slots (isa, v.format);
6659
6660 for (slot = 0;
6661 !opcode_fits_format_slot (orig_insn->opcode, v.format, slot);
6662 slot++)
6663 {
6664 v.slots[slot].opcode =
6665 xtensa_format_slot_nop_opcode (isa, v.format, slot);
6666 v.slots[slot].ntok = 0;
6667 v.slots[slot].insn_type = ITYPE_INSN;
6668 }
6669
6670 v.slots[slot] = *orig_insn;
6671 slot++;
6672
6673 for ( ; slot < v.num_slots; slot++)
6674 {
6675 v.slots[slot].opcode =
6676 xtensa_format_slot_nop_opcode (isa, v.format, slot);
6677 v.slots[slot].ntok = 0;
6678 v.slots[slot].insn_type = ITYPE_INSN;
6679 }
6680
6681 finish_vinsn (&v);
6682 xg_free_vinsn (&v);
6683 }
6684
6685
6686 static bfd_boolean
6687 emit_single_op (TInsn *orig_insn)
6688 {
6689 int i;
6690 IStack istack; /* put instructions into here */
6691 symbolS *lit_sym = NULL;
6692 symbolS *label_sym = NULL;
6693
6694 istack_init (&istack);
6695
6696 /* Special-case for "movi aX, foo" which is guaranteed to need relaxing.
6697 Because the scheduling and bundling characteristics of movi and
6698 l32r or const16 are so different, we can do much better if we relax
6699 it prior to scheduling and bundling, rather than after. */
6700 if ((orig_insn->opcode == xtensa_movi_opcode
6701 || orig_insn->opcode == xtensa_movi_n_opcode)
6702 && !cur_vinsn.inside_bundle
6703 && (orig_insn->tok[1].X_op == O_symbol
6704 || orig_insn->tok[1].X_op == O_pltrel))
6705 xg_assembly_relax (&istack, orig_insn, now_seg, frag_now, 0, 1, 0);
6706 else
6707 if (xg_expand_assembly_insn (&istack, orig_insn))
6708 return TRUE;
6709
6710 for (i = 0; i < istack.ninsn; i++)
6711 {
6712 TInsn *insn = &istack.insn[i];
6713 switch (insn->insn_type)
6714 {
6715 case ITYPE_LITERAL:
6716 assert (lit_sym == NULL);
6717 lit_sym = xg_assemble_literal (insn);
6718 break;
6719 case ITYPE_LABEL:
6720 {
6721 static int relaxed_sym_idx = 0;
6722 char *label = xmalloc (strlen (FAKE_LABEL_NAME) + 12);
6723 sprintf (label, "%s_rl_%x", FAKE_LABEL_NAME, relaxed_sym_idx++);
6724 colon (label);
6725 assert (label_sym == NULL);
6726 label_sym = symbol_find_or_make (label);
6727 assert (label_sym);
6728 free (label);
6729 }
6730 break;
6731 case ITYPE_INSN:
6732 if (lit_sym)
6733 xg_resolve_literals (insn, lit_sym);
6734 if (label_sym)
6735 xg_resolve_labels (insn, label_sym);
6736 bundle_single_op (insn);
6737 break;
6738 default:
6739 assert (0);
6740 break;
6741 }
6742 }
6743 return FALSE;
6744 }
6745
6746
6747 static int
6748 total_frag_text_expansion (fragS *fragP)
6749 {
6750 int slot;
6751 int total_expansion = 0;
6752
6753 for (slot = 0; slot < MAX_SLOTS; slot++)
6754 total_expansion += fragP->tc_frag_data.text_expansion[slot];
6755
6756 return total_expansion;
6757 }
6758
6759
6760 /* Emit a vliw instruction to the current fragment. */
6761
6762 static void
6763 xg_assemble_vliw_tokens (vliw_insn *vinsn)
6764 {
6765 bfd_boolean finish_frag = FALSE;
6766 bfd_boolean is_jump = FALSE;
6767 bfd_boolean is_branch = FALSE;
6768 xtensa_isa isa = xtensa_default_isa;
6769 int i;
6770 int insn_size;
6771 int extra_space;
6772 char *f = NULL;
6773 int slot;
6774 struct dwarf2_line_info best_loc;
6775
6776 best_loc.line = INT_MAX;
6777
6778 if (generating_literals)
6779 {
6780 static int reported = 0;
6781 if (reported < 4)
6782 as_bad_where (frag_now->fr_file, frag_now->fr_line,
6783 _("cannot assemble into a literal fragment"));
6784 if (reported == 3)
6785 as_bad (_("..."));
6786 reported++;
6787 return;
6788 }
6789
6790 if (frag_now_fix () != 0
6791 && (! frag_now->tc_frag_data.is_insn
6792 || (vinsn_has_specific_opcodes (vinsn) && use_transform ())
6793 || !use_transform () != frag_now->tc_frag_data.is_no_transform
6794 || (directive_state[directive_absolute_literals]
6795 != frag_now->tc_frag_data.use_absolute_literals)))
6796 {
6797 frag_wane (frag_now);
6798 frag_new (0);
6799 xtensa_set_frag_assembly_state (frag_now);
6800 }
6801
6802 if (workaround_a0_b_retw
6803 && vinsn->num_slots == 1
6804 && (get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0
6805 && xtensa_opcode_is_branch (isa, vinsn->slots[0].opcode) == 1
6806 && use_transform ())
6807 {
6808 has_a0_b_retw = TRUE;
6809
6810 /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW.
6811 After the first assembly pass we will check all of them and
6812 add a nop if needed. */
6813 frag_now->tc_frag_data.is_insn = TRUE;
6814 frag_var (rs_machine_dependent, 4, 4,
6815 RELAX_ADD_NOP_IF_A0_B_RETW,
6816 frag_now->fr_symbol,
6817 frag_now->fr_offset,
6818 NULL);
6819 xtensa_set_frag_assembly_state (frag_now);
6820 frag_now->tc_frag_data.is_insn = TRUE;
6821 frag_var (rs_machine_dependent, 4, 4,
6822 RELAX_ADD_NOP_IF_A0_B_RETW,
6823 frag_now->fr_symbol,
6824 frag_now->fr_offset,
6825 NULL);
6826 xtensa_set_frag_assembly_state (frag_now);
6827 }
6828
6829 for (i = 0; i < vinsn->num_slots; i++)
6830 {
6831 /* See if the instruction implies an aligned section. */
6832 if (xtensa_opcode_is_loop (isa, vinsn->slots[i].opcode) == 1)
6833 record_alignment (now_seg, 2);
6834
6835 /* Also determine the best line number for debug info. */
6836 best_loc = vinsn->slots[i].loc.line < best_loc.line
6837 ? vinsn->slots[i].loc : best_loc;
6838 }
6839
6840 /* Special cases for instructions that force an alignment... */
6841 /* None of these opcodes are bundle-able. */
6842 if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1)
6843 {
6844 size_t max_fill;
6845
6846 xtensa_set_frag_assembly_state (frag_now);
6847 frag_now->tc_frag_data.is_insn = TRUE;
6848
6849 max_fill = get_text_align_max_fill_size
6850 (get_text_align_power (xtensa_fetch_width),
6851 TRUE, frag_now->tc_frag_data.is_no_density);
6852
6853 if (use_transform ())
6854 frag_var (rs_machine_dependent, max_fill, max_fill,
6855 RELAX_ALIGN_NEXT_OPCODE,
6856 frag_now->fr_symbol,
6857 frag_now->fr_offset,
6858 NULL);
6859 else
6860 frag_var (rs_machine_dependent, 0, 0,
6861 RELAX_CHECK_ALIGN_NEXT_OPCODE, 0, 0, NULL);
6862 xtensa_set_frag_assembly_state (frag_now);
6863
6864 xtensa_move_labels (frag_now, 0, FALSE);
6865 }
6866
6867 if (vinsn->slots[0].opcode == xtensa_entry_opcode
6868 && !vinsn->slots[0].is_specific_opcode)
6869 {
6870 xtensa_mark_literal_pool_location ();
6871 xtensa_move_labels (frag_now, 0, TRUE);
6872 frag_var (rs_align_test, 1, 1, 0, NULL, 2, NULL);
6873 }
6874
6875 if (vinsn->num_slots == 1)
6876 {
6877 if (workaround_a0_b_retw && use_transform ())
6878 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER,
6879 is_register_writer (&vinsn->slots[0], "a", 0));
6880
6881 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND,
6882 is_bad_loopend_opcode (&vinsn->slots[0]));
6883 }
6884 else
6885 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND, FALSE);
6886
6887 insn_size = xtensa_format_length (isa, vinsn->format);
6888
6889 extra_space = relaxation_requirements (vinsn);
6890
6891 /* vinsn_to_insnbuf will produce the error. */
6892 if (vinsn->format != XTENSA_UNDEFINED)
6893 {
6894 f = (char *) frag_more (insn_size + extra_space);
6895 xtensa_set_frag_assembly_state (frag_now);
6896 frag_now->tc_frag_data.is_insn = TRUE;
6897 }
6898
6899 vinsn_to_insnbuf (vinsn, f, frag_now, TRUE);
6900 if (vinsn->format == XTENSA_UNDEFINED)
6901 return;
6902
6903 xtensa_insnbuf_to_chars (isa, vinsn->insnbuf, f, 0);
6904
6905 xtensa_dwarf2_emit_insn (insn_size - extra_space, &best_loc);
6906
6907 for (slot = 0; slot < vinsn->num_slots; slot++)
6908 {
6909 TInsn *tinsn = &vinsn->slots[slot];
6910 frag_now->tc_frag_data.slot_subtypes[slot] = tinsn->subtype;
6911 frag_now->tc_frag_data.slot_symbols[slot] = tinsn->symbol;
6912 frag_now->tc_frag_data.slot_sub_symbols[slot] = tinsn->sub_symbol;
6913 frag_now->tc_frag_data.slot_offsets[slot] = tinsn->offset;
6914 frag_now->tc_frag_data.literal_frags[slot] = tinsn->literal_frag;
6915 if (tinsn->literal_space != 0)
6916 xg_assemble_literal_space (tinsn->literal_space, slot);
6917
6918 if (tinsn->subtype == RELAX_NARROW)
6919 assert (vinsn->num_slots == 1);
6920 if (xtensa_opcode_is_jump (isa, tinsn->opcode) == 1)
6921 is_jump = TRUE;
6922 if (xtensa_opcode_is_branch (isa, tinsn->opcode) == 1)
6923 is_branch = TRUE;
6924
6925 if (tinsn->subtype || tinsn->symbol || tinsn->record_fix
6926 || tinsn->offset || tinsn->literal_frag || is_jump || is_branch)
6927 finish_frag = TRUE;
6928 }
6929
6930 if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
6931 frag_now->tc_frag_data.is_specific_opcode = TRUE;
6932
6933 if (finish_frag)
6934 {
6935 frag_variant (rs_machine_dependent,
6936 extra_space, extra_space, RELAX_SLOTS,
6937 frag_now->fr_symbol, frag_now->fr_offset, f);
6938 xtensa_set_frag_assembly_state (frag_now);
6939 }
6940
6941 /* Special cases for loops:
6942 close_loop_end should be inserted AFTER short_loop.
6943 Make sure that CLOSE loops are processed BEFORE short_loops
6944 when converting them. */
6945
6946 /* "short_loop": Add a NOP if the loop is < 4 bytes. */
6947 if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode)
6948 && !vinsn->slots[0].is_specific_opcode)
6949 {
6950 if (workaround_short_loop && use_transform ())
6951 {
6952 maybe_has_short_loop = TRUE;
6953 frag_now->tc_frag_data.is_insn = TRUE;
6954 frag_var (rs_machine_dependent, 4, 4,
6955 RELAX_ADD_NOP_IF_SHORT_LOOP,
6956 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6957 frag_now->tc_frag_data.is_insn = TRUE;
6958 frag_var (rs_machine_dependent, 4, 4,
6959 RELAX_ADD_NOP_IF_SHORT_LOOP,
6960 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6961 }
6962
6963 /* "close_loop_end": Add up to 12 bytes of NOPs to keep a
6964 loop at least 12 bytes away from another loop's end. */
6965 if (workaround_close_loop_end && use_transform ())
6966 {
6967 maybe_has_close_loop_end = TRUE;
6968 frag_now->tc_frag_data.is_insn = TRUE;
6969 frag_var (rs_machine_dependent, 12, 12,
6970 RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
6971 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6972 }
6973 }
6974
6975 if (use_transform ())
6976 {
6977 if (is_jump)
6978 {
6979 assert (finish_frag);
6980 frag_var (rs_machine_dependent,
6981 UNREACHABLE_MAX_WIDTH, UNREACHABLE_MAX_WIDTH,
6982 RELAX_UNREACHABLE,
6983 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6984 xtensa_set_frag_assembly_state (frag_now);
6985 }
6986 else if (is_branch && align_targets)
6987 {
6988 assert (finish_frag);
6989 frag_var (rs_machine_dependent,
6990 UNREACHABLE_MAX_WIDTH, UNREACHABLE_MAX_WIDTH,
6991 RELAX_MAYBE_UNREACHABLE,
6992 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6993 xtensa_set_frag_assembly_state (frag_now);
6994 frag_var (rs_machine_dependent,
6995 0, 0,
6996 RELAX_MAYBE_DESIRE_ALIGN,
6997 frag_now->fr_symbol, frag_now->fr_offset, NULL);
6998 xtensa_set_frag_assembly_state (frag_now);
6999 }
7000 }
7001
7002 /* Now, if the original opcode was a call... */
7003 if (do_align_targets ()
7004 && xtensa_opcode_is_call (isa, vinsn->slots[0].opcode) == 1)
7005 {
7006 float freq = get_subseg_total_freq (now_seg, now_subseg);
7007 frag_now->tc_frag_data.is_insn = TRUE;
7008 frag_var (rs_machine_dependent, 4, (int) freq, RELAX_DESIRE_ALIGN,
7009 frag_now->fr_symbol, frag_now->fr_offset, NULL);
7010 xtensa_set_frag_assembly_state (frag_now);
7011 }
7012
7013 if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
7014 {
7015 frag_wane (frag_now);
7016 frag_new (0);
7017 xtensa_set_frag_assembly_state (frag_now);
7018 }
7019 }
7020
7021 \f
7022 /* xtensa_end and helper functions. */
7023
7024 static void xtensa_cleanup_align_frags (void);
7025 static void xtensa_fix_target_frags (void);
7026 static void xtensa_mark_narrow_branches (void);
7027 static void xtensa_mark_zcl_first_insns (void);
7028 static void xtensa_fix_a0_b_retw_frags (void);
7029 static void xtensa_fix_b_j_loop_end_frags (void);
7030 static void xtensa_fix_close_loop_end_frags (void);
7031 static void xtensa_fix_short_loop_frags (void);
7032 static void xtensa_sanity_check (void);
7033
7034 void
7035 xtensa_end (void)
7036 {
7037 directive_balance ();
7038 xtensa_flush_pending_output ();
7039
7040 past_xtensa_end = TRUE;
7041
7042 xtensa_move_literals ();
7043
7044 xtensa_reorder_segments ();
7045 xtensa_cleanup_align_frags ();
7046 xtensa_fix_target_frags ();
7047 if (workaround_a0_b_retw && has_a0_b_retw)
7048 xtensa_fix_a0_b_retw_frags ();
7049 if (workaround_b_j_loop_end)
7050 xtensa_fix_b_j_loop_end_frags ();
7051
7052 /* "close_loop_end" should be processed BEFORE "short_loop". */
7053 if (workaround_close_loop_end && maybe_has_close_loop_end)
7054 xtensa_fix_close_loop_end_frags ();
7055
7056 if (workaround_short_loop && maybe_has_short_loop)
7057 xtensa_fix_short_loop_frags ();
7058 xtensa_mark_narrow_branches ();
7059 xtensa_mark_zcl_first_insns ();
7060
7061 xtensa_sanity_check ();
7062 }
7063
7064
7065 static void
7066 xtensa_cleanup_align_frags (void)
7067 {
7068 frchainS *frchP;
7069
7070 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7071 {
7072 fragS *fragP;
7073 /* Walk over all of the fragments in a subsection. */
7074 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7075 {
7076 if ((fragP->fr_type == rs_align
7077 || fragP->fr_type == rs_align_code
7078 || (fragP->fr_type == rs_machine_dependent
7079 && (fragP->fr_subtype == RELAX_DESIRE_ALIGN
7080 || fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)))
7081 && fragP->fr_fix == 0)
7082 {
7083 fragS *next = fragP->fr_next;
7084
7085 while (next
7086 && next->fr_fix == 0
7087 && next->fr_type == rs_machine_dependent
7088 && next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
7089 {
7090 frag_wane (next);
7091 next = next->fr_next;
7092 }
7093 }
7094 /* If we don't widen branch targets, then they
7095 will be easier to align. */
7096 if (fragP->tc_frag_data.is_branch_target
7097 && fragP->fr_opcode == fragP->fr_literal
7098 && fragP->fr_type == rs_machine_dependent
7099 && fragP->fr_subtype == RELAX_SLOTS
7100 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
7101 frag_wane (fragP);
7102 if (fragP->fr_type == rs_machine_dependent
7103 && fragP->fr_subtype == RELAX_UNREACHABLE)
7104 fragP->tc_frag_data.is_unreachable = TRUE;
7105 }
7106 }
7107 }
7108
7109
7110 /* Re-process all of the fragments looking to convert all of the
7111 RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch
7112 target in the next fragment, convert this to RELAX_DESIRE_ALIGN.
7113 If the next fragment starts with a loop target, AND the previous
7114 fragment can be expanded to negate the branch, convert this to a
7115 RELAX_LOOP_END. Otherwise, convert to a .fill 0. */
7116
7117 static bfd_boolean frag_can_negate_branch (fragS *);
7118
7119 static void
7120 xtensa_fix_target_frags (void)
7121 {
7122 frchainS *frchP;
7123
7124 /* When this routine is called, all of the subsections are still intact
7125 so we walk over subsections instead of sections. */
7126 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7127 {
7128 bfd_boolean prev_frag_can_negate_branch = FALSE;
7129 fragS *fragP;
7130
7131 /* Walk over all of the fragments in a subsection. */
7132 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7133 {
7134 if (fragP->fr_type == rs_machine_dependent
7135 && fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
7136 {
7137 if (next_frag_is_loop_target (fragP))
7138 {
7139 if (prev_frag_can_negate_branch)
7140 {
7141 fragP->fr_subtype = RELAX_LOOP_END;
7142 /* See the comment near the frag_var with a
7143 RELAX_DESIRE_ALIGN to see why we do this. */
7144 fragP->fr_var = RELAX_LOOP_END_BYTES;
7145 }
7146 else
7147 {
7148 if (next_frag_is_branch_target (fragP))
7149 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
7150 else
7151 frag_wane (fragP);
7152 }
7153 }
7154 else if (next_frag_is_branch_target (fragP))
7155 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
7156 else
7157 frag_wane (fragP);
7158 }
7159 if (fragP->fr_fix != 0)
7160 prev_frag_can_negate_branch = FALSE;
7161 if (frag_can_negate_branch (fragP))
7162 prev_frag_can_negate_branch = TRUE;
7163 }
7164 }
7165 }
7166
7167
7168 static bfd_boolean
7169 frag_can_negate_branch (fragS *fragP)
7170 {
7171 xtensa_isa isa = xtensa_default_isa;
7172 vliw_insn vinsn;
7173 int slot;
7174
7175 if (fragP->fr_type != rs_machine_dependent
7176 || fragP->fr_subtype != RELAX_SLOTS)
7177 return FALSE;
7178
7179 vinsn_from_chars (&vinsn, fragP->fr_opcode);
7180
7181 for (slot = 0; slot < xtensa_format_num_slots (isa, vinsn.format); slot++)
7182 {
7183 if ((fragP->tc_frag_data.slot_subtypes[slot] == RELAX_IMMED)
7184 && xtensa_opcode_is_branch (isa, vinsn.slots[slot].opcode) == 1)
7185 return TRUE;
7186 }
7187
7188 return FALSE;
7189 }
7190
7191
7192 static bfd_boolean is_narrow_branch_guaranteed_in_range (fragS *, TInsn *);
7193
7194 static void
7195 xtensa_mark_narrow_branches (void)
7196 {
7197 frchainS *frchP;
7198
7199 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7200 {
7201 fragS *fragP;
7202 /* Walk over all of the fragments in a subsection. */
7203 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7204 {
7205 if (fragP->fr_type == rs_machine_dependent
7206 && fragP->fr_subtype == RELAX_SLOTS
7207 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED)
7208 {
7209 vliw_insn vinsn;
7210 const expressionS *expr;
7211 symbolS *symbolP;
7212
7213 vinsn_from_chars (&vinsn, fragP->fr_opcode);
7214 tinsn_immed_from_frag (&vinsn.slots[0], fragP, 0);
7215
7216 expr = &vinsn.slots[0].tok[1];
7217 symbolP = expr->X_add_symbol;
7218
7219 if (vinsn.num_slots == 1
7220 && xtensa_opcode_is_branch (xtensa_default_isa,
7221 vinsn.slots[0].opcode)
7222 && xg_get_single_size (vinsn.slots[0].opcode) == 2
7223 && is_narrow_branch_guaranteed_in_range (fragP,
7224 &vinsn.slots[0]))
7225 {
7226 fragP->fr_subtype = RELAX_SLOTS;
7227 fragP->tc_frag_data.slot_subtypes[0] = RELAX_NARROW;
7228 }
7229 }
7230 }
7231 }
7232 }
7233
7234
7235 /* A branch is typically widened only when its target is out of
7236 range. However, we would like to widen them to align a subsequent
7237 branch target when possible.
7238
7239 Because the branch relaxation code is so convoluted, the optimal solution
7240 (combining the two cases) is difficult to get right in all circumstances.
7241 We therefore go with an "almost as good" solution, where we only
7242 use for alignment narrow branches that definitely will not expand to a
7243 jump and a branch. These functions find and mark these cases. */
7244
7245 /* The range in bytes of BNEZ.N and BEQZ.N. The target operand is encoded
7246 as PC + 4 + imm6, where imm6 is a 6-bit immediate ranging from 0 to 63.
7247 We start counting beginning with the frag after the 2-byte branch, so the
7248 maximum offset is (4 - 2) + 63 = 65. */
7249 #define MAX_IMMED6 65
7250
7251 static size_t unrelaxed_frag_max_size (fragS *);
7252
7253 static bfd_boolean
7254 is_narrow_branch_guaranteed_in_range (fragS *fragP, TInsn *tinsn)
7255 {
7256 const expressionS *expr = &tinsn->tok[1];
7257 symbolS *symbolP = expr->X_add_symbol;
7258 fragS *target_frag = symbol_get_frag (symbolP);
7259 size_t max_distance = expr->X_add_number;
7260 max_distance += (S_GET_VALUE (symbolP) - target_frag->fr_address);
7261 if (is_branch_jmp_to_next (tinsn, fragP))
7262 return FALSE;
7263
7264 /* The branch doesn't branch over it's own frag,
7265 but over the subsequent ones. */
7266 fragP = fragP->fr_next;
7267 while (fragP != NULL && fragP != target_frag && max_distance <= MAX_IMMED6)
7268 {
7269 max_distance += unrelaxed_frag_max_size (fragP);
7270 fragP = fragP->fr_next;
7271 }
7272 if (max_distance <= MAX_IMMED6 && fragP == target_frag)
7273 return TRUE;
7274 return FALSE;
7275 }
7276
7277
7278 static void
7279 xtensa_mark_zcl_first_insns (void)
7280 {
7281 frchainS *frchP;
7282
7283 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7284 {
7285 fragS *fragP;
7286 /* Walk over all of the fragments in a subsection. */
7287 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7288 {
7289 if (fragP->fr_type == rs_machine_dependent
7290 && (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE
7291 || fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE))
7292 {
7293 /* Find the loop frag. */
7294 fragS *targ_frag = next_non_empty_frag (fragP);
7295 /* Find the first insn frag. */
7296 targ_frag = next_non_empty_frag (targ_frag);
7297
7298 /* Of course, sometimes (mostly for toy test cases) a
7299 zero-cost loop instruction is the last in a section. */
7300 if (targ_frag)
7301 {
7302 targ_frag->tc_frag_data.is_first_loop_insn = TRUE;
7303 if (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)
7304 frag_wane (fragP);
7305 }
7306 }
7307 }
7308 }
7309 }
7310
7311
7312 /* Re-process all of the fragments looking to convert all of the
7313 RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a
7314 conditional branch or a retw/retw.n, convert this frag to one that
7315 will generate a NOP. In any case close it off with a .fill 0. */
7316
7317 static bfd_boolean next_instrs_are_b_retw (fragS *);
7318
7319 static void
7320 xtensa_fix_a0_b_retw_frags (void)
7321 {
7322 frchainS *frchP;
7323
7324 /* When this routine is called, all of the subsections are still intact
7325 so we walk over subsections instead of sections. */
7326 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7327 {
7328 fragS *fragP;
7329
7330 /* Walk over all of the fragments in a subsection. */
7331 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7332 {
7333 if (fragP->fr_type == rs_machine_dependent
7334 && fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW)
7335 {
7336 if (next_instrs_are_b_retw (fragP))
7337 {
7338 if (fragP->tc_frag_data.is_no_transform)
7339 as_bad (_("instruction sequence (write a0, branch, retw) may trigger hardware errata"));
7340 else
7341 relax_frag_add_nop (fragP);
7342 }
7343 frag_wane (fragP);
7344 }
7345 }
7346 }
7347 }
7348
7349
7350 static bfd_boolean
7351 next_instrs_are_b_retw (fragS *fragP)
7352 {
7353 xtensa_opcode opcode;
7354 xtensa_format fmt;
7355 const fragS *next_fragP = next_non_empty_frag (fragP);
7356 static xtensa_insnbuf insnbuf = NULL;
7357 static xtensa_insnbuf slotbuf = NULL;
7358 xtensa_isa isa = xtensa_default_isa;
7359 int offset = 0;
7360 int slot;
7361 bfd_boolean branch_seen = FALSE;
7362
7363 if (!insnbuf)
7364 {
7365 insnbuf = xtensa_insnbuf_alloc (isa);
7366 slotbuf = xtensa_insnbuf_alloc (isa);
7367 }
7368
7369 if (next_fragP == NULL)
7370 return FALSE;
7371
7372 /* Check for the conditional branch. */
7373 xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset], 0);
7374 fmt = xtensa_format_decode (isa, insnbuf);
7375 if (fmt == XTENSA_UNDEFINED)
7376 return FALSE;
7377
7378 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
7379 {
7380 xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
7381 opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
7382
7383 branch_seen = (branch_seen
7384 || xtensa_opcode_is_branch (isa, opcode) == 1);
7385 }
7386
7387 if (!branch_seen)
7388 return FALSE;
7389
7390 offset += xtensa_format_length (isa, fmt);
7391 if (offset == next_fragP->fr_fix)
7392 {
7393 next_fragP = next_non_empty_frag (next_fragP);
7394 offset = 0;
7395 }
7396
7397 if (next_fragP == NULL)
7398 return FALSE;
7399
7400 /* Check for the retw/retw.n. */
7401 xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset], 0);
7402 fmt = xtensa_format_decode (isa, insnbuf);
7403
7404 /* Because RETW[.N] is not bundleable, a VLIW bundle here means that we
7405 have no problems. */
7406 if (fmt == XTENSA_UNDEFINED
7407 || xtensa_format_num_slots (isa, fmt) != 1)
7408 return FALSE;
7409
7410 xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
7411 opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
7412
7413 if (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode)
7414 return TRUE;
7415
7416 return FALSE;
7417 }
7418
7419
7420 /* Re-process all of the fragments looking to convert all of the
7421 RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a
7422 loop end label, convert this frag to one that will generate a NOP.
7423 In any case close it off with a .fill 0. */
7424
7425 static bfd_boolean next_instr_is_loop_end (fragS *);
7426
7427 static void
7428 xtensa_fix_b_j_loop_end_frags (void)
7429 {
7430 frchainS *frchP;
7431
7432 /* When this routine is called, all of the subsections are still intact
7433 so we walk over subsections instead of sections. */
7434 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7435 {
7436 fragS *fragP;
7437
7438 /* Walk over all of the fragments in a subsection. */
7439 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7440 {
7441 if (fragP->fr_type == rs_machine_dependent
7442 && fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END)
7443 {
7444 if (next_instr_is_loop_end (fragP))
7445 {
7446 if (fragP->tc_frag_data.is_no_transform)
7447 as_bad (_("branching or jumping to a loop end may trigger hardware errata"));
7448 else
7449 relax_frag_add_nop (fragP);
7450 }
7451 frag_wane (fragP);
7452 }
7453 }
7454 }
7455 }
7456
7457
7458 static bfd_boolean
7459 next_instr_is_loop_end (fragS *fragP)
7460 {
7461 const fragS *next_fragP;
7462
7463 if (next_frag_is_loop_target (fragP))
7464 return FALSE;
7465
7466 next_fragP = next_non_empty_frag (fragP);
7467 if (next_fragP == NULL)
7468 return FALSE;
7469
7470 if (!next_frag_is_loop_target (next_fragP))
7471 return FALSE;
7472
7473 /* If the size is >= 3 then there is more than one instruction here.
7474 The hardware bug will not fire. */
7475 if (next_fragP->fr_fix > 3)
7476 return FALSE;
7477
7478 return TRUE;
7479 }
7480
7481
7482 /* Re-process all of the fragments looking to convert all of the
7483 RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is
7484 not MY loop's loop end within 12 bytes, add enough nops here to
7485 make it at least 12 bytes away. In any case close it off with a
7486 .fill 0. */
7487
7488 static size_t min_bytes_to_other_loop_end (fragS *, fragS *, offsetT, size_t);
7489
7490 static void
7491 xtensa_fix_close_loop_end_frags (void)
7492 {
7493 frchainS *frchP;
7494
7495 /* When this routine is called, all of the subsections are still intact
7496 so we walk over subsections instead of sections. */
7497 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7498 {
7499 fragS *fragP;
7500
7501 fragS *current_target = NULL;
7502 offsetT current_offset = 0;
7503
7504 /* Walk over all of the fragments in a subsection. */
7505 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7506 {
7507 if (fragP->fr_type == rs_machine_dependent
7508 && ((fragP->fr_subtype == RELAX_IMMED)
7509 || ((fragP->fr_subtype == RELAX_SLOTS)
7510 && (fragP->tc_frag_data.slot_subtypes[0]
7511 == RELAX_IMMED))))
7512 {
7513 /* Read it. If the instruction is a loop, get the target. */
7514 TInsn t_insn;
7515 tinsn_from_chars (&t_insn, fragP->fr_opcode, 0);
7516 if (xtensa_opcode_is_loop (xtensa_default_isa,
7517 t_insn.opcode) == 1)
7518 {
7519 /* Get the current fragment target. */
7520 if (fragP->tc_frag_data.slot_symbols[0])
7521 {
7522 symbolS *sym = fragP->tc_frag_data.slot_symbols[0];
7523 current_target = symbol_get_frag (sym);
7524 current_offset = fragP->fr_offset;
7525 }
7526 }
7527 }
7528
7529 if (current_target
7530 && fragP->fr_type == rs_machine_dependent
7531 && fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END)
7532 {
7533 size_t min_bytes;
7534 size_t bytes_added = 0;
7535
7536 #define REQUIRED_LOOP_DIVIDING_BYTES 12
7537 /* Max out at 12. */
7538 min_bytes = min_bytes_to_other_loop_end
7539 (fragP->fr_next, current_target, current_offset,
7540 REQUIRED_LOOP_DIVIDING_BYTES);
7541
7542 if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES)
7543 {
7544 if (fragP->tc_frag_data.is_no_transform)
7545 as_bad (_("loop end too close to another loop end may trigger hardware errata"));
7546 else
7547 {
7548 while (min_bytes + bytes_added
7549 < REQUIRED_LOOP_DIVIDING_BYTES)
7550 {
7551 int length = 3;
7552
7553 if (fragP->fr_var < length)
7554 as_fatal (_("fr_var %lu < length %d"),
7555 fragP->fr_var, length);
7556 else
7557 {
7558 assemble_nop (length,
7559 fragP->fr_literal + fragP->fr_fix);
7560 fragP->fr_fix += length;
7561 fragP->fr_var -= length;
7562 }
7563 bytes_added += length;
7564 }
7565 }
7566 }
7567 frag_wane (fragP);
7568 }
7569 assert (fragP->fr_type != rs_machine_dependent
7570 || fragP->fr_subtype != RELAX_ADD_NOP_IF_CLOSE_LOOP_END);
7571 }
7572 }
7573 }
7574
7575
7576 static size_t unrelaxed_frag_min_size (fragS *);
7577
7578 static size_t
7579 min_bytes_to_other_loop_end (fragS *fragP,
7580 fragS *current_target,
7581 offsetT current_offset,
7582 size_t max_size)
7583 {
7584 size_t offset = 0;
7585 fragS *current_fragP;
7586
7587 for (current_fragP = fragP;
7588 current_fragP;
7589 current_fragP = current_fragP->fr_next)
7590 {
7591 if (current_fragP->tc_frag_data.is_loop_target
7592 && current_fragP != current_target)
7593 return offset + current_offset;
7594
7595 offset += unrelaxed_frag_min_size (current_fragP);
7596
7597 if (offset + current_offset >= max_size)
7598 return max_size;
7599 }
7600 return max_size;
7601 }
7602
7603
7604 static size_t
7605 unrelaxed_frag_min_size (fragS *fragP)
7606 {
7607 size_t size = fragP->fr_fix;
7608
7609 /* add fill size */
7610 if (fragP->fr_type == rs_fill)
7611 size += fragP->fr_offset;
7612
7613 return size;
7614 }
7615
7616
7617 static size_t
7618 unrelaxed_frag_max_size (fragS *fragP)
7619 {
7620 size_t size = fragP->fr_fix;
7621 switch (fragP->fr_type)
7622 {
7623 case 0:
7624 /* Empty frags created by the obstack allocation scheme
7625 end up with type 0. */
7626 break;
7627 case rs_fill:
7628 case rs_org:
7629 case rs_space:
7630 size += fragP->fr_offset;
7631 break;
7632 case rs_align:
7633 case rs_align_code:
7634 case rs_align_test:
7635 case rs_leb128:
7636 case rs_cfa:
7637 case rs_dwarf2dbg:
7638 /* No further adjustments needed. */
7639 break;
7640 case rs_machine_dependent:
7641 if (fragP->fr_subtype != RELAX_DESIRE_ALIGN)
7642 size += fragP->fr_var;
7643 break;
7644 default:
7645 /* We had darn well better know how big it is. */
7646 assert (0);
7647 break;
7648 }
7649
7650 return size;
7651 }
7652
7653
7654 /* Re-process all of the fragments looking to convert all
7655 of the RELAX_ADD_NOP_IF_SHORT_LOOP. If:
7656
7657 A)
7658 1) the instruction size count to the loop end label
7659 is too short (<= 2 instructions),
7660 2) loop has a jump or branch in it
7661
7662 or B)
7663 1) workaround_all_short_loops is TRUE
7664 2) The generating loop was a 'loopgtz' or 'loopnez'
7665 3) the instruction size count to the loop end label is too short
7666 (<= 2 instructions)
7667 then convert this frag (and maybe the next one) to generate a NOP.
7668 In any case close it off with a .fill 0. */
7669
7670 static size_t count_insns_to_loop_end (fragS *, bfd_boolean, size_t);
7671 static bfd_boolean branch_before_loop_end (fragS *);
7672
7673 static void
7674 xtensa_fix_short_loop_frags (void)
7675 {
7676 frchainS *frchP;
7677
7678 /* When this routine is called, all of the subsections are still intact
7679 so we walk over subsections instead of sections. */
7680 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7681 {
7682 fragS *fragP;
7683 fragS *current_target = NULL;
7684 offsetT current_offset = 0;
7685 xtensa_opcode current_opcode = XTENSA_UNDEFINED;
7686
7687 /* Walk over all of the fragments in a subsection. */
7688 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7689 {
7690 /* Check on the current loop. */
7691 if (fragP->fr_type == rs_machine_dependent
7692 && ((fragP->fr_subtype == RELAX_IMMED)
7693 || ((fragP->fr_subtype == RELAX_SLOTS)
7694 && (fragP->tc_frag_data.slot_subtypes[0]
7695 == RELAX_IMMED))))
7696 {
7697 TInsn t_insn;
7698
7699 /* Read it. If the instruction is a loop, get the target. */
7700 tinsn_from_chars (&t_insn, fragP->fr_opcode, 0);
7701 if (xtensa_opcode_is_loop (xtensa_default_isa,
7702 t_insn.opcode) == 1)
7703 {
7704 /* Get the current fragment target. */
7705 if (fragP->tc_frag_data.slot_symbols[0])
7706 {
7707 symbolS *sym = fragP->tc_frag_data.slot_symbols[0];
7708 current_target = symbol_get_frag (sym);
7709 current_offset = fragP->fr_offset;
7710 current_opcode = t_insn.opcode;
7711 }
7712 }
7713 }
7714
7715 if (fragP->fr_type == rs_machine_dependent
7716 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
7717 {
7718 size_t insn_count =
7719 count_insns_to_loop_end (fragP->fr_next, TRUE, 3);
7720 if (insn_count < 3
7721 && (branch_before_loop_end (fragP->fr_next)
7722 || (workaround_all_short_loops
7723 && current_opcode != XTENSA_UNDEFINED
7724 && current_opcode != xtensa_loop_opcode)))
7725 {
7726 if (fragP->tc_frag_data.is_no_transform)
7727 as_bad (_("loop containing less than three instructions may trigger hardware errata"));
7728 else
7729 relax_frag_add_nop (fragP);
7730 }
7731 frag_wane (fragP);
7732 }
7733 }
7734 }
7735 }
7736
7737
7738 static size_t unrelaxed_frag_min_insn_count (fragS *);
7739
7740 static size_t
7741 count_insns_to_loop_end (fragS *base_fragP,
7742 bfd_boolean count_relax_add,
7743 size_t max_count)
7744 {
7745 fragS *fragP = NULL;
7746 size_t insn_count = 0;
7747
7748 fragP = base_fragP;
7749
7750 for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next)
7751 {
7752 insn_count += unrelaxed_frag_min_insn_count (fragP);
7753 if (insn_count >= max_count)
7754 return max_count;
7755
7756 if (count_relax_add)
7757 {
7758 if (fragP->fr_type == rs_machine_dependent
7759 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
7760 {
7761 /* In order to add the appropriate number of
7762 NOPs, we count an instruction for downstream
7763 occurrences. */
7764 insn_count++;
7765 if (insn_count >= max_count)
7766 return max_count;
7767 }
7768 }
7769 }
7770 return insn_count;
7771 }
7772
7773
7774 static size_t
7775 unrelaxed_frag_min_insn_count (fragS *fragP)
7776 {
7777 xtensa_isa isa = xtensa_default_isa;
7778 static xtensa_insnbuf insnbuf = NULL;
7779 size_t insn_count = 0;
7780 int offset = 0;
7781
7782 if (!fragP->tc_frag_data.is_insn)
7783 return insn_count;
7784
7785 if (!insnbuf)
7786 insnbuf = xtensa_insnbuf_alloc (isa);
7787
7788 /* Decode the fixed instructions. */
7789 while (offset < fragP->fr_fix)
7790 {
7791 xtensa_format fmt;
7792
7793 xtensa_insnbuf_from_chars (isa, insnbuf, fragP->fr_literal + offset, 0);
7794 fmt = xtensa_format_decode (isa, insnbuf);
7795
7796 if (fmt == XTENSA_UNDEFINED)
7797 {
7798 as_fatal (_("undecodable instruction in instruction frag"));
7799 return insn_count;
7800 }
7801 offset += xtensa_format_length (isa, fmt);
7802 insn_count++;
7803 }
7804
7805 return insn_count;
7806 }
7807
7808
7809 static bfd_boolean unrelaxed_frag_has_b_j (fragS *);
7810
7811 static bfd_boolean
7812 branch_before_loop_end (fragS *base_fragP)
7813 {
7814 fragS *fragP;
7815
7816 for (fragP = base_fragP;
7817 fragP && !fragP->tc_frag_data.is_loop_target;
7818 fragP = fragP->fr_next)
7819 {
7820 if (unrelaxed_frag_has_b_j (fragP))
7821 return TRUE;
7822 }
7823 return FALSE;
7824 }
7825
7826
7827 static bfd_boolean
7828 unrelaxed_frag_has_b_j (fragS *fragP)
7829 {
7830 static xtensa_insnbuf insnbuf = NULL;
7831 xtensa_isa isa = xtensa_default_isa;
7832 int offset = 0;
7833
7834 if (!fragP->tc_frag_data.is_insn)
7835 return FALSE;
7836
7837 if (!insnbuf)
7838 insnbuf = xtensa_insnbuf_alloc (isa);
7839
7840 /* Decode the fixed instructions. */
7841 while (offset < fragP->fr_fix)
7842 {
7843 xtensa_format fmt;
7844 int slot;
7845
7846 xtensa_insnbuf_from_chars (isa, insnbuf, fragP->fr_literal + offset, 0);
7847 fmt = xtensa_format_decode (isa, insnbuf);
7848 if (fmt == XTENSA_UNDEFINED)
7849 return FALSE;
7850
7851 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
7852 {
7853 xtensa_opcode opcode =
7854 get_opcode_from_buf (fragP->fr_literal + offset, slot);
7855 if (xtensa_opcode_is_branch (isa, opcode) == 1
7856 || xtensa_opcode_is_jump (isa, opcode) == 1)
7857 return TRUE;
7858 }
7859 offset += xtensa_format_length (isa, fmt);
7860 }
7861 return FALSE;
7862 }
7863
7864
7865 /* Checks to be made after initial assembly but before relaxation. */
7866
7867 static bfd_boolean is_empty_loop (const TInsn *, fragS *);
7868 static bfd_boolean is_local_forward_loop (const TInsn *, fragS *);
7869
7870 static void
7871 xtensa_sanity_check (void)
7872 {
7873 char *file_name;
7874 int line;
7875
7876 frchainS *frchP;
7877
7878 as_where (&file_name, &line);
7879 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
7880 {
7881 fragS *fragP;
7882
7883 /* Walk over all of the fragments in a subsection. */
7884 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
7885 {
7886 /* Currently we only check for empty loops here. */
7887 if (fragP->fr_type == rs_machine_dependent
7888 && fragP->fr_subtype == RELAX_IMMED)
7889 {
7890 static xtensa_insnbuf insnbuf = NULL;
7891 TInsn t_insn;
7892
7893 if (fragP->fr_opcode != NULL)
7894 {
7895 if (!insnbuf)
7896 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
7897 tinsn_from_chars (&t_insn, fragP->fr_opcode, 0);
7898 tinsn_immed_from_frag (&t_insn, fragP, 0);
7899
7900 if (xtensa_opcode_is_loop (xtensa_default_isa,
7901 t_insn.opcode) == 1)
7902 {
7903 if (is_empty_loop (&t_insn, fragP))
7904 {
7905 new_logical_line (fragP->fr_file, fragP->fr_line);
7906 as_bad (_("invalid empty loop"));
7907 }
7908 if (!is_local_forward_loop (&t_insn, fragP))
7909 {
7910 new_logical_line (fragP->fr_file, fragP->fr_line);
7911 as_bad (_("loop target does not follow "
7912 "loop instruction in section"));
7913 }
7914 }
7915 }
7916 }
7917 }
7918 }
7919 new_logical_line (file_name, line);
7920 }
7921
7922
7923 #define LOOP_IMMED_OPN 1
7924
7925 /* Return TRUE if the loop target is the next non-zero fragment. */
7926
7927 static bfd_boolean
7928 is_empty_loop (const TInsn *insn, fragS *fragP)
7929 {
7930 const expressionS *expr;
7931 symbolS *symbolP;
7932 fragS *next_fragP;
7933
7934 if (insn->insn_type != ITYPE_INSN)
7935 return FALSE;
7936
7937 if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1)
7938 return FALSE;
7939
7940 if (insn->ntok <= LOOP_IMMED_OPN)
7941 return FALSE;
7942
7943 expr = &insn->tok[LOOP_IMMED_OPN];
7944
7945 if (expr->X_op != O_symbol)
7946 return FALSE;
7947
7948 symbolP = expr->X_add_symbol;
7949 if (!symbolP)
7950 return FALSE;
7951
7952 if (symbol_get_frag (symbolP) == NULL)
7953 return FALSE;
7954
7955 if (S_GET_VALUE (symbolP) != 0)
7956 return FALSE;
7957
7958 /* Walk through the zero-size fragments from this one. If we find
7959 the target fragment, then this is a zero-size loop. */
7960
7961 for (next_fragP = fragP->fr_next;
7962 next_fragP != NULL;
7963 next_fragP = next_fragP->fr_next)
7964 {
7965 if (next_fragP == symbol_get_frag (symbolP))
7966 return TRUE;
7967 if (next_fragP->fr_fix != 0)
7968 return FALSE;
7969 }
7970 return FALSE;
7971 }
7972
7973
7974 static bfd_boolean
7975 is_local_forward_loop (const TInsn *insn, fragS *fragP)
7976 {
7977 const expressionS *expr;
7978 symbolS *symbolP;
7979 fragS *next_fragP;
7980
7981 if (insn->insn_type != ITYPE_INSN)
7982 return FALSE;
7983
7984 if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) == 0)
7985 return FALSE;
7986
7987 if (insn->ntok <= LOOP_IMMED_OPN)
7988 return FALSE;
7989
7990 expr = &insn->tok[LOOP_IMMED_OPN];
7991
7992 if (expr->X_op != O_symbol)
7993 return FALSE;
7994
7995 symbolP = expr->X_add_symbol;
7996 if (!symbolP)
7997 return FALSE;
7998
7999 if (symbol_get_frag (symbolP) == NULL)
8000 return FALSE;
8001
8002 /* Walk through fragments until we find the target.
8003 If we do not find the target, then this is an invalid loop. */
8004
8005 for (next_fragP = fragP->fr_next;
8006 next_fragP != NULL;
8007 next_fragP = next_fragP->fr_next)
8008 {
8009 if (next_fragP == symbol_get_frag (symbolP))
8010 return TRUE;
8011 }
8012
8013 return FALSE;
8014 }
8015
8016 \f
8017 /* Alignment Functions. */
8018
8019 static size_t
8020 get_text_align_power (int target_size)
8021 {
8022 size_t i = 0;
8023 for (i = 0; i < sizeof (size_t); i++)
8024 {
8025 if (target_size <= (1 << i))
8026 return i;
8027 }
8028 assert (0);
8029 return 0;
8030 }
8031
8032
8033 static addressT
8034 get_text_align_max_fill_size (int align_pow,
8035 bfd_boolean use_nops,
8036 bfd_boolean use_no_density)
8037 {
8038 if (!use_nops)
8039 return (1 << align_pow);
8040 if (use_no_density)
8041 return 3 * (1 << align_pow);
8042
8043 return 1 + (1 << align_pow);
8044 }
8045
8046
8047 /* get_text_align_fill_size ()
8048
8049 Desired alignments:
8050 give the address
8051 target_size = size of next instruction
8052 align_pow = get_text_align_power (target_size).
8053 use_nops = 0
8054 use_no_density = 0;
8055 Loop alignments:
8056 address = current address + loop instruction size;
8057 target_size = 3 (for 2 or 3 byte target)
8058 = 4 (for 4 byte target)
8059 = 8 (for 8 byte target)
8060 align_pow = get_text_align_power (target_size);
8061 use_nops = 1
8062 use_no_density = set appropriately
8063 Text alignments:
8064 address = current address + loop instruction size;
8065 target_size = 0
8066 align_pow = get_text_align_power (target_size);
8067 use_nops = 0
8068 use_no_density = 0. */
8069
8070 static addressT
8071 get_text_align_fill_size (addressT address,
8072 int align_pow,
8073 int target_size,
8074 bfd_boolean use_nops,
8075 bfd_boolean use_no_density)
8076 {
8077 /* Input arguments:
8078
8079 align_pow: log2 (required alignment).
8080
8081 target_size: alignment must allow the new_address and
8082 new_address+target_size-1.
8083
8084 use_nops: if TRUE, then we can only use 2- or 3-byte nops.
8085
8086 use_no_density: if use_nops and use_no_density, we can only use
8087 3-byte nops.
8088
8089 Usually the align_pow is the power of 2 that is greater than
8090 or equal to the target_size. This handles the 2-byte, 3-byte
8091 and 8-byte instructions.
8092
8093 Two cases:
8094
8095 (1) aligning an instruction properly, but without using NOPs.
8096 E.G.: a 3-byte instruction can go on any address where address mod 4
8097 is zero or one. The aligner uses this case to find the optimal
8098 number of fill bytes for relax_frag_for_align.
8099
8100 (2) aligning an instruction properly, but where we might need to use
8101 extra NOPs. E.G.: when the aligner couldn't find enough widenings
8102 or similar to get the optimal location. */
8103
8104 size_t alignment = (1 << align_pow);
8105
8106 assert (target_size != 0);
8107
8108 if (!use_nops)
8109 {
8110 unsigned fill_bytes;
8111 for (fill_bytes = 0; fill_bytes < alignment; fill_bytes++)
8112 {
8113 addressT end_address = address + target_size - 1 + fill_bytes;
8114 addressT start_address = address + fill_bytes;
8115 if ((end_address >> align_pow) == (start_address >> align_pow))
8116 return fill_bytes;
8117 }
8118 assert (0);
8119 }
8120
8121 /* This is the slightly harder case. */
8122 assert ((int) alignment >= target_size);
8123 assert (target_size > 0);
8124 if (!use_no_density)
8125 {
8126 size_t i;
8127 for (i = 0; i < alignment * 2; i++)
8128 {
8129 if (i == 1)
8130 continue;
8131 if ((address + i) >> align_pow
8132 == (address + i + target_size - 1) >> align_pow)
8133 return i;
8134 }
8135 }
8136 else
8137 {
8138 size_t i;
8139
8140 /* Can only fill multiples of 3. */
8141 for (i = 0; i <= alignment * 3; i += 3)
8142 {
8143 if ((address + i) >> align_pow
8144 == (address + i + target_size - 1) >> align_pow)
8145 return i;
8146 }
8147 }
8148 assert (0);
8149 return 0;
8150 }
8151
8152
8153 /* This will assert if it is not possible. */
8154
8155 static size_t
8156 get_text_align_nop_count (size_t fill_size, bfd_boolean use_no_density)
8157 {
8158 size_t count = 0;
8159 if (use_no_density)
8160 {
8161 assert (fill_size % 3 == 0);
8162 return (fill_size / 3);
8163 }
8164
8165 assert (fill_size != 1); /* Bad argument. */
8166
8167 while (fill_size > 1)
8168 {
8169 size_t insn_size = 3;
8170 if (fill_size == 2 || fill_size == 4)
8171 insn_size = 2;
8172 fill_size -= insn_size;
8173 count++;
8174 }
8175 assert (fill_size != 1); /* Bad algorithm. */
8176 return count;
8177 }
8178
8179
8180 static size_t
8181 get_text_align_nth_nop_size (size_t fill_size,
8182 size_t n,
8183 bfd_boolean use_no_density)
8184 {
8185 size_t count = 0;
8186
8187 assert (get_text_align_nop_count (fill_size, use_no_density) > n);
8188
8189 if (use_no_density)
8190 return 3;
8191
8192 while (fill_size > 1)
8193 {
8194 size_t insn_size = 3;
8195 if (fill_size == 2 || fill_size == 4)
8196 insn_size = 2;
8197 fill_size -= insn_size;
8198 count++;
8199 if (n + 1 == count)
8200 return insn_size;
8201 }
8202 assert (0);
8203 return 0;
8204 }
8205
8206
8207 /* For the given fragment, find the appropriate address
8208 for it to begin at if we are using NOPs to align it. */
8209
8210 static addressT
8211 get_noop_aligned_address (fragS *fragP, addressT address)
8212 {
8213 /* The rule is: get next fragment's FIRST instruction. Find
8214 the smallest number of bytes that need to be added to
8215 ensure that the next fragment's FIRST instruction will fit
8216 in a single word.
8217
8218 E.G., 2 bytes : 0, 1, 2 mod 4
8219 3 bytes: 0, 1 mod 4
8220
8221 If the FIRST instruction MIGHT be relaxed,
8222 assume that it will become a 3-byte instruction.
8223
8224 Note again here that LOOP instructions are not bundleable,
8225 and this relaxation only applies to LOOP opcodes. */
8226
8227 size_t fill_size = 0;
8228 int first_insn_size;
8229 int loop_insn_size;
8230 addressT pre_opcode_bytes;
8231 size_t alignment;
8232 fragS *first_insn;
8233 xtensa_opcode opcode;
8234 bfd_boolean is_loop;
8235
8236 assert (fragP->fr_type == rs_machine_dependent);
8237 assert (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE);
8238
8239 /* Find the loop frag. */
8240 first_insn = next_non_empty_frag (fragP);
8241 /* Now find the first insn frag. */
8242 first_insn = next_non_empty_frag (first_insn);
8243
8244 is_loop = next_frag_opcode_is_loop (fragP, &opcode);
8245 assert (is_loop);
8246 loop_insn_size = xg_get_single_size (opcode);
8247
8248 pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP);
8249 pre_opcode_bytes += loop_insn_size;
8250
8251 /* For loops, the alignment depends on the size of the
8252 instruction following the loop, not the LOOP instruction. */
8253
8254 if (first_insn == NULL)
8255 return address;
8256
8257 assert (first_insn->tc_frag_data.is_first_loop_insn);
8258
8259 first_insn_size = frag_format_size (first_insn);
8260
8261 if (first_insn_size == 2 || first_insn_size == XTENSA_UNDEFINED)
8262 first_insn_size = 3; /* ISA specifies this */
8263
8264 /* If it was 8, then we'll need a larger alignment for the section. */
8265 alignment = get_text_align_power (first_insn_size);
8266
8267 /* Is now_seg valid? */
8268 record_alignment (now_seg, alignment);
8269
8270 fill_size = get_text_align_fill_size
8271 (address + pre_opcode_bytes,
8272 get_text_align_power (first_insn_size),
8273 first_insn_size, TRUE, fragP->tc_frag_data.is_no_density);
8274
8275 return address + fill_size;
8276 }
8277
8278
8279 /* 3 mechanisms for relaxing an alignment:
8280
8281 Align to a power of 2.
8282 Align so the next fragment's instruction does not cross a word boundary.
8283 Align the current instruction so that if the next instruction
8284 were 3 bytes, it would not cross a word boundary.
8285
8286 We can align with:
8287
8288 zeros - This is easy; always insert zeros.
8289 nops - 3-byte and 2-byte instructions
8290 2 - 2-byte nop
8291 3 - 3-byte nop
8292 4 - 2 2-byte nops
8293 >=5 : 3-byte instruction + fn (n-3)
8294 widening - widen previous instructions. */
8295
8296 static addressT
8297 get_aligned_diff (fragS *fragP, addressT address, addressT *max_diff)
8298 {
8299 addressT target_address, loop_insn_offset;
8300 int target_size;
8301 xtensa_opcode loop_opcode;
8302 bfd_boolean is_loop;
8303 int text_align_power;
8304 addressT opt_diff;
8305
8306 assert (fragP->fr_type == rs_machine_dependent);
8307 switch (fragP->fr_subtype)
8308 {
8309 case RELAX_DESIRE_ALIGN:
8310 target_size = next_frag_format_size (fragP);
8311 if (target_size == XTENSA_UNDEFINED)
8312 target_size = 3;
8313 text_align_power = get_text_align_power (xtensa_fetch_width);
8314 opt_diff = get_text_align_fill_size (address, text_align_power,
8315 target_size, FALSE, FALSE);
8316
8317 *max_diff = opt_diff + xtensa_fetch_width
8318 - (target_size + ((address + opt_diff) % xtensa_fetch_width));
8319 assert (*max_diff >= opt_diff);
8320 return opt_diff;
8321
8322 case RELAX_ALIGN_NEXT_OPCODE:
8323 target_size = next_frag_format_size (fragP);
8324 loop_insn_offset = 0;
8325 is_loop = next_frag_opcode_is_loop (fragP, &loop_opcode);
8326 assert (is_loop);
8327
8328 /* If the loop has been expanded then the LOOP instruction
8329 could be at an offset from this fragment. */
8330 if (next_non_empty_frag(fragP)->tc_frag_data.slot_subtypes[0]
8331 != RELAX_IMMED)
8332 loop_insn_offset = get_expanded_loop_offset (loop_opcode);
8333
8334 if (target_size == 2)
8335 target_size = 3; /* ISA specifies this */
8336
8337 /* In an ideal world, which is what we are shooting for here,
8338 we wouldn't need to use any NOPs immediately prior to the
8339 LOOP instruction. If this approach fails, relax_frag_loop_align
8340 will call get_noop_aligned_address. */
8341 target_address =
8342 address + loop_insn_offset + xg_get_single_size (loop_opcode);
8343 text_align_power = get_text_align_power (target_size),
8344 opt_diff = get_text_align_fill_size (target_address, text_align_power,
8345 target_size, FALSE, FALSE);
8346
8347 *max_diff = xtensa_fetch_width
8348 - ((target_address + opt_diff) % xtensa_fetch_width)
8349 - target_size + opt_diff;
8350 assert (*max_diff >= opt_diff);
8351 return opt_diff;
8352
8353 default:
8354 break;
8355 }
8356 assert (0);
8357 return 0;
8358 }
8359
8360 \f
8361 /* md_relax_frag Hook and Helper Functions. */
8362
8363 static long relax_frag_loop_align (fragS *, long);
8364 static long relax_frag_for_align (fragS *, long);
8365 static long relax_frag_immed
8366 (segT, fragS *, long, int, xtensa_format, int, int *, bfd_boolean);
8367
8368
8369 /* Return the number of bytes added to this fragment, given that the
8370 input has been stretched already by "stretch". */
8371
8372 long
8373 xtensa_relax_frag (fragS *fragP, long stretch, int *stretched_p)
8374 {
8375 xtensa_isa isa = xtensa_default_isa;
8376 int unreported = fragP->tc_frag_data.unreported_expansion;
8377 long new_stretch = 0;
8378 char *file_name;
8379 int line, lit_size;
8380 static xtensa_insnbuf vbuf = NULL;
8381 int slot, num_slots;
8382 xtensa_format fmt;
8383
8384 as_where (&file_name, &line);
8385 new_logical_line (fragP->fr_file, fragP->fr_line);
8386
8387 fragP->tc_frag_data.unreported_expansion = 0;
8388
8389 switch (fragP->fr_subtype)
8390 {
8391 case RELAX_ALIGN_NEXT_OPCODE:
8392 /* Always convert. */
8393 if (fragP->tc_frag_data.relax_seen)
8394 new_stretch = relax_frag_loop_align (fragP, stretch);
8395 break;
8396
8397 case RELAX_LOOP_END:
8398 /* Do nothing. */
8399 break;
8400
8401 case RELAX_LOOP_END_ADD_NOP:
8402 /* Add a NOP and switch to .fill 0. */
8403 new_stretch = relax_frag_add_nop (fragP);
8404 frag_wane (fragP);
8405 break;
8406
8407 case RELAX_DESIRE_ALIGN:
8408 /* Do nothing. The narrowing before this frag will either align
8409 it or not. */
8410 break;
8411
8412 case RELAX_LITERAL:
8413 case RELAX_LITERAL_FINAL:
8414 return 0;
8415
8416 case RELAX_LITERAL_NR:
8417 lit_size = 4;
8418 fragP->fr_subtype = RELAX_LITERAL_FINAL;
8419 assert (unreported == lit_size);
8420 memset (&fragP->fr_literal[fragP->fr_fix], 0, 4);
8421 fragP->fr_var -= lit_size;
8422 fragP->fr_fix += lit_size;
8423 new_stretch = 4;
8424 break;
8425
8426 case RELAX_SLOTS:
8427 if (vbuf == NULL)
8428 vbuf = xtensa_insnbuf_alloc (isa);
8429
8430 xtensa_insnbuf_from_chars (isa, vbuf, fragP->fr_opcode, 0);
8431 fmt = xtensa_format_decode (isa, vbuf);
8432 num_slots = xtensa_format_num_slots (isa, fmt);
8433
8434 for (slot = 0; slot < num_slots; slot++)
8435 {
8436 switch (fragP->tc_frag_data.slot_subtypes[slot])
8437 {
8438 case RELAX_NARROW:
8439 if (fragP->tc_frag_data.relax_seen)
8440 new_stretch += relax_frag_for_align (fragP, stretch);
8441 break;
8442
8443 case RELAX_IMMED:
8444 case RELAX_IMMED_STEP1:
8445 case RELAX_IMMED_STEP2:
8446 /* Place the immediate. */
8447 new_stretch += relax_frag_immed
8448 (now_seg, fragP, stretch,
8449 fragP->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
8450 fmt, slot, stretched_p, FALSE);
8451 break;
8452
8453 default:
8454 /* This is OK; see the note in xg_assemble_vliw_tokens. */
8455 break;
8456 }
8457 }
8458 break;
8459
8460 case RELAX_LITERAL_POOL_BEGIN:
8461 case RELAX_LITERAL_POOL_END:
8462 case RELAX_MAYBE_UNREACHABLE:
8463 case RELAX_MAYBE_DESIRE_ALIGN:
8464 /* No relaxation required. */
8465 break;
8466
8467 case RELAX_FILL_NOP:
8468 case RELAX_UNREACHABLE:
8469 if (fragP->tc_frag_data.relax_seen)
8470 new_stretch += relax_frag_for_align (fragP, stretch);
8471 break;
8472
8473 default:
8474 as_bad (_("bad relaxation state"));
8475 }
8476
8477 /* Tell gas we need another relaxation pass. */
8478 if (! fragP->tc_frag_data.relax_seen)
8479 {
8480 fragP->tc_frag_data.relax_seen = TRUE;
8481 *stretched_p = 1;
8482 }
8483
8484 new_logical_line (file_name, line);
8485 return new_stretch;
8486 }
8487
8488
8489 static long
8490 relax_frag_loop_align (fragS *fragP, long stretch)
8491 {
8492 addressT old_address, old_next_address, old_size;
8493 addressT new_address, new_next_address, new_size;
8494 addressT growth;
8495
8496 /* All the frags with relax_frag_for_alignment prior to this one in the
8497 section have been done, hopefully eliminating the need for a NOP here.
8498 But, this will put it in if necessary. */
8499
8500 /* Calculate the old address of this fragment and the next fragment. */
8501 old_address = fragP->fr_address - stretch;
8502 old_next_address = (fragP->fr_address - stretch + fragP->fr_fix +
8503 fragP->tc_frag_data.text_expansion[0]);
8504 old_size = old_next_address - old_address;
8505
8506 /* Calculate the new address of this fragment and the next fragment. */
8507 new_address = fragP->fr_address;
8508 new_next_address =
8509 get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix);
8510 new_size = new_next_address - new_address;
8511
8512 growth = new_size - old_size;
8513
8514 /* Fix up the text_expansion field and return the new growth. */
8515 fragP->tc_frag_data.text_expansion[0] += growth;
8516 return growth;
8517 }
8518
8519
8520 /* Add a NOP instruction. */
8521
8522 static long
8523 relax_frag_add_nop (fragS *fragP)
8524 {
8525 char *nop_buf = fragP->fr_literal + fragP->fr_fix;
8526 int length = fragP->tc_frag_data.is_no_density ? 3 : 2;
8527 assemble_nop (length, nop_buf);
8528 fragP->tc_frag_data.is_insn = TRUE;
8529
8530 if (fragP->fr_var < length)
8531 {
8532 as_fatal (_("fr_var (%ld) < length (%d)"), fragP->fr_var, length);
8533 return 0;
8534 }
8535
8536 fragP->fr_fix += length;
8537 fragP->fr_var -= length;
8538 return length;
8539 }
8540
8541
8542 static long future_alignment_required (fragS *, long);
8543
8544 static long
8545 relax_frag_for_align (fragS *fragP, long stretch)
8546 {
8547 /* Overview of the relaxation procedure for alignment:
8548 We can widen with NOPs or by widening instructions or by filling
8549 bytes after jump instructions. Find the opportune places and widen
8550 them if necessary. */
8551
8552 long stretch_me;
8553 long diff;
8554
8555 assert (fragP->fr_subtype == RELAX_FILL_NOP
8556 || fragP->fr_subtype == RELAX_UNREACHABLE
8557 || (fragP->fr_subtype == RELAX_SLOTS
8558 && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW));
8559
8560 stretch_me = future_alignment_required (fragP, stretch);
8561 diff = stretch_me - fragP->tc_frag_data.text_expansion[0];
8562 if (diff == 0)
8563 return 0;
8564
8565 if (diff < 0)
8566 {
8567 /* We expanded on a previous pass. Can we shrink now? */
8568 long shrink = fragP->tc_frag_data.text_expansion[0] - stretch_me;
8569 if (shrink <= stretch && stretch > 0)
8570 {
8571 fragP->tc_frag_data.text_expansion[0] = stretch_me;
8572 return -shrink;
8573 }
8574 return 0;
8575 }
8576
8577 /* Below here, diff > 0. */
8578 fragP->tc_frag_data.text_expansion[0] = stretch_me;
8579
8580 return diff;
8581 }
8582
8583
8584 /* Return the address of the next frag that should be aligned.
8585
8586 By "address" we mean the address it _would_ be at if there
8587 is no action taken to align it between here and the target frag.
8588 In other words, if no narrows and no fill nops are used between
8589 here and the frag to align, _even_if_ some of the frags we use
8590 to align targets have already expanded on a previous relaxation
8591 pass.
8592
8593 Also, count each frag that may be used to help align the target.
8594
8595 Return 0 if there are no frags left in the chain that need to be
8596 aligned. */
8597
8598 static addressT
8599 find_address_of_next_align_frag (fragS **fragPP,
8600 int *wide_nops,
8601 int *narrow_nops,
8602 int *widens,
8603 bfd_boolean *paddable)
8604 {
8605 fragS *fragP = *fragPP;
8606 addressT address = fragP->fr_address;
8607
8608 /* Do not reset the counts to 0. */
8609
8610 while (fragP)
8611 {
8612 /* Limit this to a small search. */
8613 if (*widens > 8)
8614 {
8615 *fragPP = fragP;
8616 return 0;
8617 }
8618 address += fragP->fr_fix;
8619
8620 if (fragP->fr_type == rs_fill)
8621 address += fragP->fr_offset * fragP->fr_var;
8622 else if (fragP->fr_type == rs_machine_dependent)
8623 {
8624 switch (fragP->fr_subtype)
8625 {
8626 case RELAX_UNREACHABLE:
8627 *paddable = TRUE;
8628 break;
8629
8630 case RELAX_FILL_NOP:
8631 (*wide_nops)++;
8632 if (!fragP->tc_frag_data.is_no_density)
8633 (*narrow_nops)++;
8634 break;
8635
8636 case RELAX_SLOTS:
8637 if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
8638 {
8639 (*widens)++;
8640 break;
8641 }
8642 address += total_frag_text_expansion (fragP);;
8643 break;
8644
8645 case RELAX_IMMED:
8646 address += fragP->tc_frag_data.text_expansion[0];
8647 break;
8648
8649 case RELAX_ALIGN_NEXT_OPCODE:
8650 case RELAX_DESIRE_ALIGN:
8651 *fragPP = fragP;
8652 return address;
8653
8654 case RELAX_MAYBE_UNREACHABLE:
8655 case RELAX_MAYBE_DESIRE_ALIGN:
8656 /* Do nothing. */
8657 break;
8658
8659 default:
8660 /* Just punt if we don't know the type. */
8661 *fragPP = fragP;
8662 return 0;
8663 }
8664 }
8665 else
8666 {
8667 /* Just punt if we don't know the type. */
8668 *fragPP = fragP;
8669 return 0;
8670 }
8671 fragP = fragP->fr_next;
8672 }
8673
8674 *fragPP = fragP;
8675 return 0;
8676 }
8677
8678
8679 static long bytes_to_stretch (fragS *, int, int, int, int);
8680
8681 /* Undefine LOOKAHEAD_ALIGNER to get the older behavior.
8682 I'll leave this in until I am more confident this works. */
8683
8684 #define LOOKAHEAD_ALIGNER 1
8685
8686 static long
8687 future_alignment_required (fragS *fragP, long stretch ATTRIBUTE_UNUSED)
8688 {
8689 fragS *this_frag = fragP;
8690 long address;
8691 int num_widens = 0;
8692 int wide_nops = 0;
8693 int narrow_nops = 0;
8694 bfd_boolean paddable = FALSE;
8695 offsetT local_opt_diff;
8696 offsetT opt_diff;
8697 offsetT max_diff;
8698 int stretch_amount = 0;
8699 int local_stretch_amount;
8700 int global_stretch_amount;
8701
8702 address = find_address_of_next_align_frag
8703 (&fragP, &wide_nops, &narrow_nops, &num_widens, &paddable);
8704
8705 if (address)
8706 {
8707 local_opt_diff = get_aligned_diff (fragP, address, &max_diff);
8708 opt_diff = local_opt_diff;
8709 assert (opt_diff >= 0);
8710 assert (max_diff >= opt_diff);
8711 if (max_diff == 0)
8712 return 0;
8713 #ifdef LOOKAHEAD_ALIGNER
8714 if (fragP)
8715 fragP = fragP->fr_next;
8716
8717 while (fragP && opt_diff < max_diff && address)
8718 {
8719 /* We only use these to determine if we can exit early
8720 because there will be plenty of ways to align future
8721 align frags. */
8722 unsigned int glob_widens = 0;
8723 int dnn = 0;
8724 int dw = 0;
8725 bfd_boolean glob_pad = 0;
8726 address = find_address_of_next_align_frag
8727 (&fragP, &glob_widens, &dnn, &dw, &glob_pad);
8728 /* If there is a padable portion, then skip. */
8729 if (glob_pad || (glob_widens >= xtensa_fetch_width))
8730 break;
8731
8732 if (address)
8733 {
8734 offsetT next_m_diff;
8735 offsetT next_o_diff;
8736
8737 /* Downrange frags haven't had stretch added to them yet. */
8738 address += stretch;
8739
8740 /* The address also includes any text expansion from this
8741 frag in a previous pass, but we don't want that. */
8742 address -= this_frag->tc_frag_data.text_expansion[0];
8743
8744 /* Assume we are going to move at least opt_diff. In
8745 reality, we might not be able to, but assuming that
8746 we will helps catch cases where moving opt_diff pushes
8747 the next target from aligned to unaligned. */
8748 address += opt_diff;
8749
8750 next_o_diff = get_aligned_diff (fragP, address, &next_m_diff);
8751
8752 /* Now cleanup for the adjustments to address. */
8753 next_o_diff += opt_diff;
8754 next_m_diff += opt_diff;
8755 if (next_o_diff <= max_diff && next_o_diff > opt_diff)
8756 opt_diff = next_o_diff;
8757 if (next_m_diff < max_diff)
8758 max_diff = next_m_diff;
8759 fragP = fragP->fr_next;
8760 }
8761 }
8762 #endif /* LOOKAHEAD_ALIGNER */
8763 /* If there are enough wideners in between, do it. */
8764 if (paddable)
8765 {
8766 if (this_frag->fr_subtype == RELAX_UNREACHABLE)
8767 {
8768 assert (opt_diff <= UNREACHABLE_MAX_WIDTH);
8769 return opt_diff;
8770 }
8771 return 0;
8772 }
8773 local_stretch_amount
8774 = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
8775 num_widens, local_opt_diff);
8776 #ifdef LOOKAHEAD_ALIGNER
8777 global_stretch_amount
8778 = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
8779 num_widens, opt_diff);
8780 /* If the condition below is true, then the frag couldn't
8781 stretch the correct amount for the global case, so we just
8782 optimize locally. We'll rely on the subsequent frags to get
8783 the correct alignment in the global case. */
8784 if (global_stretch_amount < local_stretch_amount)
8785 stretch_amount = local_stretch_amount;
8786 else
8787 stretch_amount = global_stretch_amount;
8788 #else /* ! LOOKAHEAD_ALIGNER */
8789 stretch_amount = local_stretch_amount;
8790 #endif /* ! LOOKAHEAD_ALIGNER */
8791 if (this_frag->fr_subtype == RELAX_SLOTS
8792 && this_frag->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
8793 assert (stretch_amount <= 1);
8794 else if (this_frag->fr_subtype == RELAX_FILL_NOP)
8795 {
8796 if (this_frag->tc_frag_data.is_no_density)
8797 assert (stretch_amount == 3 || stretch_amount == 0);
8798 else
8799 assert (stretch_amount <= 3);
8800 }
8801 }
8802 return stretch_amount;
8803 }
8804
8805
8806 /* The idea: widen everything you can to get a target or loop aligned,
8807 then start using NOPs.
8808
8809 When we must have a NOP, here is a table of how we decide
8810 (so you don't have to fight through the control flow below):
8811
8812 wide_nops = the number of wide NOPs available for aligning
8813 narrow_nops = the number of narrow NOPs available for aligning
8814 (a subset of wide_nops)
8815 widens = the number of narrow instructions that should be widened
8816
8817 Desired wide narrow
8818 Diff nop nop widens
8819 1 0 0 1
8820 2 0 1 0
8821 3a 1 0 0
8822 b 0 1 1 (case 3a makes this case unnecessary)
8823 4a 1 0 1
8824 b 0 2 0
8825 c 0 1 2 (case 4a makes this case unnecessary)
8826 5a 1 0 2
8827 b 1 1 0
8828 c 0 2 1 (case 5b makes this case unnecessary)
8829 6a 2 0 0
8830 b 1 0 3
8831 c 0 1 4 (case 6b makes this case unneccesary)
8832 d 1 1 1 (case 6a makes this case unnecessary)
8833 e 0 2 2 (case 6a makes this case unnecessary)
8834 f 0 3 0 (case 6a makes this case unnecessary)
8835 7a 1 0 4
8836 b 2 0 1
8837 c 1 1 2 (case 7b makes this case unnecessary)
8838 d 0 1 5 (case 7a makes this case unnecessary)
8839 e 0 2 3 (case 7b makes this case unnecessary)
8840 f 0 3 1 (case 7b makes this case unnecessary)
8841 g 1 2 1 (case 7b makes this case unnecessary)
8842 */
8843
8844 static long
8845 bytes_to_stretch (fragS *this_frag,
8846 int wide_nops,
8847 int narrow_nops,
8848 int num_widens,
8849 int desired_diff)
8850 {
8851 int bytes_short = desired_diff - num_widens;
8852
8853 assert (desired_diff >= 0 && desired_diff < 8);
8854 if (desired_diff == 0)
8855 return 0;
8856
8857 assert (wide_nops > 0 || num_widens > 0);
8858
8859 /* Always prefer widening to NOP-filling. */
8860 if (bytes_short < 0)
8861 {
8862 /* There are enough RELAX_NARROW frags after this one
8863 to align the target without widening this frag in any way. */
8864 return 0;
8865 }
8866
8867 if (bytes_short == 0)
8868 {
8869 /* Widen every narrow between here and the align target
8870 and the align target will be properly aligned. */
8871 if (this_frag->fr_subtype == RELAX_FILL_NOP)
8872 return 0;
8873 else
8874 return 1;
8875 }
8876
8877 /* From here we will need at least one NOP to get an alignment.
8878 However, we may not be able to align at all, in which case,
8879 don't widen. */
8880 if (this_frag->fr_subtype == RELAX_FILL_NOP)
8881 {
8882 switch (desired_diff)
8883 {
8884 case 1:
8885 return 0;
8886 case 2:
8887 if (!this_frag->tc_frag_data.is_no_density && narrow_nops == 1)
8888 return 2; /* case 2 */
8889 return 0;
8890 case 3:
8891 if (wide_nops > 1)
8892 return 0;
8893 else
8894 return 3; /* case 3a */
8895 case 4:
8896 if (num_widens >= 1 && wide_nops == 1)
8897 return 3; /* case 4a */
8898 if (!this_frag->tc_frag_data.is_no_density && narrow_nops == 2)
8899 return 2; /* case 4b */
8900 return 0;
8901 case 5:
8902 if (num_widens >= 2 && wide_nops == 1)
8903 return 3; /* case 5a */
8904 /* We will need two nops. Are there enough nops
8905 between here and the align target? */
8906 if (wide_nops < 2 || narrow_nops == 0)
8907 return 0;
8908 /* Are there other nops closer that can serve instead? */
8909 if (wide_nops > 2 && narrow_nops > 1)
8910 return 0;
8911 /* Take the density one first, because there might not be
8912 another density one available. */
8913 if (!this_frag->tc_frag_data.is_no_density)
8914 return 2; /* case 5b narrow */
8915 else
8916 return 3; /* case 5b wide */
8917 return 0;
8918 case 6:
8919 if (wide_nops == 2)
8920 return 3; /* case 6a */
8921 else if (num_widens >= 3 && wide_nops == 1)
8922 return 3; /* case 6b */
8923 return 0;
8924 case 7:
8925 if (wide_nops == 1 && num_widens >= 4)
8926 return 3; /* case 7a */
8927 else if (wide_nops == 2 && num_widens >= 1)
8928 return 3; /* case 7b */
8929 return 0;
8930 default:
8931 assert (0);
8932 }
8933 }
8934 else
8935 {
8936 /* We will need a NOP no matter what, but should we widen
8937 this instruction to help?
8938
8939 This is a RELAX_FRAG_NARROW frag. */
8940 switch (desired_diff)
8941 {
8942 case 1:
8943 assert (0);
8944 return 0;
8945 case 2:
8946 case 3:
8947 return 0;
8948 case 4:
8949 if (wide_nops >= 1 && num_widens == 1)
8950 return 1; /* case 4a */
8951 return 0;
8952 case 5:
8953 if (wide_nops >= 1 && num_widens == 2)
8954 return 1; /* case 5a */
8955 return 0;
8956 case 6:
8957 if (wide_nops >= 2)
8958 return 0; /* case 6a */
8959 else if (wide_nops >= 1 && num_widens == 3)
8960 return 1; /* case 6b */
8961 return 0;
8962 case 7:
8963 if (wide_nops >= 1 && num_widens == 4)
8964 return 1; /* case 7a */
8965 else if (wide_nops >= 2 && num_widens == 1)
8966 return 1; /* case 7b */
8967 return 0;
8968 default:
8969 assert (0);
8970 return 0;
8971 }
8972 }
8973 assert (0);
8974 return 0;
8975 }
8976
8977
8978 static long
8979 relax_frag_immed (segT segP,
8980 fragS *fragP,
8981 long stretch,
8982 int min_steps,
8983 xtensa_format fmt,
8984 int slot,
8985 int *stretched_p,
8986 bfd_boolean estimate_only)
8987 {
8988 TInsn tinsn;
8989 vliw_insn orig_vinsn;
8990 int old_size;
8991 bfd_boolean negatable_branch = FALSE;
8992 bfd_boolean branch_jmp_to_next = FALSE;
8993 bfd_boolean wide_insn = FALSE;
8994 xtensa_isa isa = xtensa_default_isa;
8995 IStack istack;
8996 offsetT frag_offset;
8997 int num_steps;
8998 fragS *lit_fragP;
8999 int num_text_bytes, num_literal_bytes;
9000 int literal_diff, total_text_diff, this_text_diff, first;
9001
9002 assert (fragP->fr_opcode != NULL);
9003
9004 xg_init_vinsn (&orig_vinsn);
9005 vinsn_from_chars (&orig_vinsn, fragP->fr_opcode);
9006 if (xtensa_format_num_slots (isa, fmt) > 1)
9007 wide_insn = TRUE;
9008
9009 tinsn = orig_vinsn.slots[slot];
9010 tinsn_immed_from_frag (&tinsn, fragP, slot);
9011
9012 if (estimate_only && xtensa_opcode_is_loop (isa, tinsn.opcode))
9013 return 0;
9014
9015 if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
9016 branch_jmp_to_next = is_branch_jmp_to_next (&tinsn, fragP);
9017
9018 negatable_branch = (xtensa_opcode_is_branch (isa, tinsn.opcode) == 1);
9019
9020 old_size = xtensa_format_length (isa, fmt);
9021
9022 /* Special case: replace a branch to the next instruction with a NOP.
9023 This is required to work around a hardware bug in T1040.0 and also
9024 serves as an optimization. */
9025
9026 if (branch_jmp_to_next
9027 && ((old_size == 2) || (old_size == 3))
9028 && !next_frag_is_loop_target (fragP))
9029 return 0;
9030
9031 /* Here is the fun stuff: Get the immediate field from this
9032 instruction. If it fits, we are done. If not, find the next
9033 instruction sequence that fits. */
9034
9035 frag_offset = fragP->fr_opcode - fragP->fr_literal;
9036 istack_init (&istack);
9037 num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP, frag_offset,
9038 min_steps, stretch);
9039 if (num_steps < min_steps)
9040 {
9041 as_fatal (_("internal error: relaxation failed"));
9042 return 0;
9043 }
9044
9045 if (num_steps > RELAX_IMMED_MAXSTEPS)
9046 {
9047 as_fatal (_("internal error: relaxation requires too many steps"));
9048 return 0;
9049 }
9050
9051 fragP->tc_frag_data.slot_subtypes[slot] = (int) RELAX_IMMED + num_steps;
9052
9053 /* Figure out the number of bytes needed. */
9054 lit_fragP = 0;
9055 num_literal_bytes = get_num_stack_literal_bytes (&istack);
9056 literal_diff =
9057 num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot];
9058 first = 0;
9059 while (istack.insn[first].opcode == XTENSA_UNDEFINED)
9060 first++;
9061 num_text_bytes = get_num_stack_text_bytes (&istack);
9062 if (wide_insn)
9063 {
9064 num_text_bytes += old_size;
9065 if (opcode_fits_format_slot (istack.insn[first].opcode, fmt, slot))
9066 num_text_bytes -= xg_get_single_size (istack.insn[first].opcode);
9067 }
9068 total_text_diff = num_text_bytes - old_size;
9069 this_text_diff = total_text_diff - fragP->tc_frag_data.text_expansion[slot];
9070
9071 /* It MUST get larger. If not, we could get an infinite loop. */
9072 assert (num_text_bytes >= 0);
9073 assert (literal_diff >= 0);
9074 assert (total_text_diff >= 0);
9075
9076 fragP->tc_frag_data.text_expansion[slot] = total_text_diff;
9077 fragP->tc_frag_data.literal_expansion[slot] = num_literal_bytes;
9078 assert (fragP->tc_frag_data.text_expansion[slot] >= 0);
9079 assert (fragP->tc_frag_data.literal_expansion[slot] >= 0);
9080
9081 /* Find the associated expandable literal for this. */
9082 if (literal_diff != 0)
9083 {
9084 lit_fragP = fragP->tc_frag_data.literal_frags[slot];
9085 if (lit_fragP)
9086 {
9087 assert (literal_diff == 4);
9088 lit_fragP->tc_frag_data.unreported_expansion += literal_diff;
9089
9090 /* We expect that the literal section state has NOT been
9091 modified yet. */
9092 assert (lit_fragP->fr_type == rs_machine_dependent
9093 && lit_fragP->fr_subtype == RELAX_LITERAL);
9094 lit_fragP->fr_subtype = RELAX_LITERAL_NR;
9095
9096 /* We need to mark this section for another iteration
9097 of relaxation. */
9098 (*stretched_p)++;
9099 }
9100 }
9101
9102 if (negatable_branch && istack.ninsn > 1)
9103 update_next_frag_state (fragP);
9104
9105 return this_text_diff;
9106 }
9107
9108 \f
9109 /* md_convert_frag Hook and Helper Functions. */
9110
9111 static void convert_frag_align_next_opcode (fragS *);
9112 static void convert_frag_narrow (segT, fragS *, xtensa_format, int);
9113 static void convert_frag_fill_nop (fragS *);
9114 static void convert_frag_immed (segT, fragS *, int, xtensa_format, int);
9115
9116 void
9117 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec, fragS *fragp)
9118 {
9119 static xtensa_insnbuf vbuf = NULL;
9120 xtensa_isa isa = xtensa_default_isa;
9121 int slot;
9122 int num_slots;
9123 xtensa_format fmt;
9124 char *file_name;
9125 int line;
9126
9127 as_where (&file_name, &line);
9128 new_logical_line (fragp->fr_file, fragp->fr_line);
9129
9130 switch (fragp->fr_subtype)
9131 {
9132 case RELAX_ALIGN_NEXT_OPCODE:
9133 /* Always convert. */
9134 convert_frag_align_next_opcode (fragp);
9135 break;
9136
9137 case RELAX_DESIRE_ALIGN:
9138 /* Do nothing. If not aligned already, too bad. */
9139 break;
9140
9141 case RELAX_LITERAL:
9142 case RELAX_LITERAL_FINAL:
9143 break;
9144
9145 case RELAX_SLOTS:
9146 if (vbuf == NULL)
9147 vbuf = xtensa_insnbuf_alloc (isa);
9148
9149 xtensa_insnbuf_from_chars (isa, vbuf, fragp->fr_opcode, 0);
9150 fmt = xtensa_format_decode (isa, vbuf);
9151 num_slots = xtensa_format_num_slots (isa, fmt);
9152
9153 for (slot = 0; slot < num_slots; slot++)
9154 {
9155 switch (fragp->tc_frag_data.slot_subtypes[slot])
9156 {
9157 case RELAX_NARROW:
9158 convert_frag_narrow (sec, fragp, fmt, slot);
9159 break;
9160
9161 case RELAX_IMMED:
9162 case RELAX_IMMED_STEP1:
9163 case RELAX_IMMED_STEP2:
9164 /* Place the immediate. */
9165 convert_frag_immed
9166 (sec, fragp,
9167 fragp->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
9168 fmt, slot);
9169 break;
9170
9171 default:
9172 /* This is OK because some slots could have
9173 relaxations and others have none. */
9174 break;
9175 }
9176 }
9177 break;
9178
9179 case RELAX_UNREACHABLE:
9180 memset (&fragp->fr_literal[fragp->fr_fix], 0, fragp->fr_var);
9181 fragp->fr_fix += fragp->tc_frag_data.text_expansion[0];
9182 fragp->fr_var -= fragp->tc_frag_data.text_expansion[0];
9183 frag_wane (fragp);
9184 break;
9185
9186 case RELAX_MAYBE_UNREACHABLE:
9187 case RELAX_MAYBE_DESIRE_ALIGN:
9188 frag_wane (fragp);
9189 break;
9190
9191 case RELAX_FILL_NOP:
9192 convert_frag_fill_nop (fragp);
9193 break;
9194
9195 case RELAX_LITERAL_NR:
9196 if (use_literal_section)
9197 {
9198 /* This should have been handled during relaxation. When
9199 relaxing a code segment, literals sometimes need to be
9200 added to the corresponding literal segment. If that
9201 literal segment has already been relaxed, then we end up
9202 in this situation. Marking the literal segments as data
9203 would make this happen less often (since GAS always relaxes
9204 code before data), but we could still get into trouble if
9205 there are instructions in a segment that is not marked as
9206 containing code. Until we can implement a better solution,
9207 cheat and adjust the addresses of all the following frags.
9208 This could break subsequent alignments, but the linker's
9209 literal coalescing will do that anyway. */
9210
9211 fragS *f;
9212 fragp->fr_subtype = RELAX_LITERAL_FINAL;
9213 assert (fragp->tc_frag_data.unreported_expansion == 4);
9214 memset (&fragp->fr_literal[fragp->fr_fix], 0, 4);
9215 fragp->fr_var -= 4;
9216 fragp->fr_fix += 4;
9217 for (f = fragp->fr_next; f; f = f->fr_next)
9218 f->fr_address += 4;
9219 }
9220 else
9221 as_bad (_("invalid relaxation fragment result"));
9222 break;
9223 }
9224
9225 fragp->fr_var = 0;
9226 new_logical_line (file_name, line);
9227 }
9228
9229
9230 static void
9231 convert_frag_align_next_opcode (fragS *fragp)
9232 {
9233 char *nop_buf; /* Location for Writing. */
9234 size_t i;
9235
9236 bfd_boolean use_no_density = fragp->tc_frag_data.is_no_density;
9237 addressT aligned_address;
9238 size_t fill_size, nop_count;
9239
9240 aligned_address = get_noop_aligned_address (fragp, fragp->fr_address +
9241 fragp->fr_fix);
9242 fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix);
9243 nop_count = get_text_align_nop_count (fill_size, use_no_density);
9244 nop_buf = fragp->fr_literal + fragp->fr_fix;
9245
9246 for (i = 0; i < nop_count; i++)
9247 {
9248 size_t nop_size;
9249 nop_size = get_text_align_nth_nop_size (fill_size, i, use_no_density);
9250
9251 assemble_nop (nop_size, nop_buf);
9252 nop_buf += nop_size;
9253 }
9254
9255 fragp->fr_fix += fill_size;
9256 fragp->fr_var -= fill_size;
9257 }
9258
9259
9260 static void
9261 convert_frag_narrow (segT segP, fragS *fragP, xtensa_format fmt, int slot)
9262 {
9263 TInsn tinsn, single_target;
9264 xtensa_format single_fmt;
9265 int size, old_size, diff, error_val;
9266 offsetT frag_offset;
9267
9268 assert (slot == 0);
9269 tinsn_from_chars (&tinsn, fragP->fr_opcode, 0);
9270
9271 if (xtensa_opcode_is_branch (xtensa_default_isa, tinsn.opcode) == 1)
9272 {
9273 assert (fragP->tc_frag_data.text_expansion[0] == 1
9274 || fragP->tc_frag_data.text_expansion[0] == 0);
9275 convert_frag_immed (segP, fragP, fragP->tc_frag_data.text_expansion[0],
9276 fmt, slot);
9277 return;
9278 }
9279
9280 if (fragP->tc_frag_data.text_expansion[0] == 0)
9281 {
9282 /* No conversion. */
9283 fragP->fr_var = 0;
9284 return;
9285 }
9286
9287 assert (fragP->fr_opcode != NULL);
9288
9289 /* Frags in this relaxation state should only contain
9290 single instruction bundles. */
9291 tinsn_immed_from_frag (&tinsn, fragP, 0);
9292
9293 /* Just convert it to a wide form.... */
9294 size = 0;
9295 old_size = xg_get_single_size (tinsn.opcode);
9296
9297 tinsn_init (&single_target);
9298 frag_offset = fragP->fr_opcode - fragP->fr_literal;
9299
9300 error_val = xg_expand_narrow (&single_target, &tinsn);
9301 if (error_val)
9302 {
9303 as_bad (_("unable to widen instruction"));
9304 return;
9305 }
9306
9307 size = xg_get_single_size (single_target.opcode);
9308 single_fmt = xg_get_single_format (single_target.opcode);
9309
9310 xg_emit_insn_to_buf (&single_target, single_fmt, fragP->fr_opcode,
9311 fragP, frag_offset, TRUE);
9312
9313 diff = size - old_size;
9314 assert (diff >= 0);
9315 assert (diff <= fragP->fr_var);
9316 fragP->fr_var -= diff;
9317 fragP->fr_fix += diff;
9318
9319 /* clean it up */
9320 fragP->fr_var = 0;
9321 }
9322
9323
9324 static void
9325 convert_frag_fill_nop (fragS *fragP)
9326 {
9327 char *loc = &fragP->fr_literal[fragP->fr_fix];
9328 int size = fragP->tc_frag_data.text_expansion[0];
9329 assert ((unsigned) size == (fragP->fr_next->fr_address
9330 - fragP->fr_address - fragP->fr_fix));
9331 if (size == 0)
9332 {
9333 /* No conversion. */
9334 fragP->fr_var = 0;
9335 return;
9336 }
9337 assemble_nop (size, loc);
9338 fragP->tc_frag_data.is_insn = TRUE;
9339 fragP->fr_var -= size;
9340 fragP->fr_fix += size;
9341 frag_wane (fragP);
9342 }
9343
9344
9345 static fixS *fix_new_exp_in_seg
9346 (segT, subsegT, fragS *, int, int, expressionS *, int,
9347 bfd_reloc_code_real_type);
9348 static void convert_frag_immed_finish_loop (segT, fragS *, TInsn *);
9349
9350 static void
9351 convert_frag_immed (segT segP,
9352 fragS *fragP,
9353 int min_steps,
9354 xtensa_format fmt,
9355 int slot)
9356 {
9357 char *immed_instr = fragP->fr_opcode;
9358 TInsn orig_tinsn;
9359 bfd_boolean expanded = FALSE;
9360 bfd_boolean branch_jmp_to_next = FALSE;
9361 char *fr_opcode = fragP->fr_opcode;
9362 vliw_insn orig_vinsn;
9363 xtensa_isa isa = xtensa_default_isa;
9364 bfd_boolean wide_insn = FALSE;
9365 int bytes;
9366 bfd_boolean is_loop;
9367
9368 assert (fr_opcode != NULL);
9369
9370 xg_init_vinsn (&orig_vinsn);
9371
9372 vinsn_from_chars (&orig_vinsn, fr_opcode);
9373 if (xtensa_format_num_slots (isa, fmt) > 1)
9374 wide_insn = TRUE;
9375
9376 orig_tinsn = orig_vinsn.slots[slot];
9377 tinsn_immed_from_frag (&orig_tinsn, fragP, slot);
9378
9379 is_loop = xtensa_opcode_is_loop (xtensa_default_isa, orig_tinsn.opcode) == 1;
9380
9381 if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
9382 branch_jmp_to_next = is_branch_jmp_to_next (&orig_tinsn, fragP);
9383
9384 if (branch_jmp_to_next && !next_frag_is_loop_target (fragP))
9385 {
9386 /* Conversion just inserts a NOP and marks the fix as completed. */
9387 bytes = xtensa_format_length (isa, fmt);
9388 if (bytes >= 4)
9389 {
9390 orig_vinsn.slots[slot].opcode =
9391 xtensa_format_slot_nop_opcode (isa, orig_vinsn.format, slot);
9392 orig_vinsn.slots[slot].ntok = 0;
9393 }
9394 else
9395 {
9396 bytes += fragP->tc_frag_data.text_expansion[0];
9397 assert (bytes == 2 || bytes == 3);
9398 build_nop (&orig_vinsn.slots[0], bytes);
9399 fragP->fr_fix += fragP->tc_frag_data.text_expansion[0];
9400 }
9401 vinsn_to_insnbuf (&orig_vinsn, fr_opcode, frag_now, FALSE);
9402 xtensa_insnbuf_to_chars (isa, orig_vinsn.insnbuf, fr_opcode, 0);
9403 fragP->fr_var = 0;
9404 }
9405 else if (!orig_tinsn.is_specific_opcode)
9406 {
9407 /* Here is the fun stuff: Get the immediate field from this
9408 instruction. If it fits, we're done. If not, find the next
9409 instruction sequence that fits. */
9410
9411 IStack istack;
9412 int i;
9413 symbolS *lit_sym = NULL;
9414 int total_size = 0;
9415 int target_offset = 0;
9416 int old_size;
9417 int diff;
9418 symbolS *gen_label = NULL;
9419 offsetT frag_offset;
9420 bfd_boolean first = TRUE;
9421 bfd_boolean last_is_jump;
9422
9423 /* It does not fit. Find something that does and
9424 convert immediately. */
9425 frag_offset = fr_opcode - fragP->fr_literal;
9426 istack_init (&istack);
9427 xg_assembly_relax (&istack, &orig_tinsn,
9428 segP, fragP, frag_offset, min_steps, 0);
9429
9430 old_size = xtensa_format_length (isa, fmt);
9431
9432 /* Assemble this right inline. */
9433
9434 /* First, create the mapping from a label name to the REAL label. */
9435 target_offset = 0;
9436 for (i = 0; i < istack.ninsn; i++)
9437 {
9438 TInsn *tinsn = &istack.insn[i];
9439 fragS *lit_frag;
9440
9441 switch (tinsn->insn_type)
9442 {
9443 case ITYPE_LITERAL:
9444 if (lit_sym != NULL)
9445 as_bad (_("multiple literals in expansion"));
9446 /* First find the appropriate space in the literal pool. */
9447 lit_frag = fragP->tc_frag_data.literal_frags[slot];
9448 if (lit_frag == NULL)
9449 as_bad (_("no registered fragment for literal"));
9450 if (tinsn->ntok != 1)
9451 as_bad (_("number of literal tokens != 1"));
9452
9453 /* Set the literal symbol and add a fixup. */
9454 lit_sym = lit_frag->fr_symbol;
9455 break;
9456
9457 case ITYPE_LABEL:
9458 if (align_targets && !is_loop)
9459 {
9460 fragS *unreach = fragP->fr_next;
9461 while (!(unreach->fr_type == rs_machine_dependent
9462 && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
9463 || unreach->fr_subtype == RELAX_UNREACHABLE)))
9464 {
9465 unreach = unreach->fr_next;
9466 }
9467
9468 assert (unreach->fr_type == rs_machine_dependent
9469 && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
9470 || unreach->fr_subtype == RELAX_UNREACHABLE));
9471
9472 target_offset += unreach->tc_frag_data.text_expansion[0];
9473 }
9474 assert (gen_label == NULL);
9475 gen_label = symbol_new (FAKE_LABEL_NAME, now_seg,
9476 fr_opcode - fragP->fr_literal
9477 + target_offset, fragP);
9478 break;
9479
9480 case ITYPE_INSN:
9481 if (first && wide_insn)
9482 {
9483 target_offset += xtensa_format_length (isa, fmt);
9484 first = FALSE;
9485 if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9486 target_offset += xg_get_single_size (tinsn->opcode);
9487 }
9488 else
9489 target_offset += xg_get_single_size (tinsn->opcode);
9490 break;
9491 }
9492 }
9493
9494 total_size = 0;
9495 first = TRUE;
9496 last_is_jump = FALSE;
9497 for (i = 0; i < istack.ninsn; i++)
9498 {
9499 TInsn *tinsn = &istack.insn[i];
9500 fragS *lit_frag;
9501 int size;
9502 segT target_seg;
9503 bfd_reloc_code_real_type reloc_type;
9504
9505 switch (tinsn->insn_type)
9506 {
9507 case ITYPE_LITERAL:
9508 lit_frag = fragP->tc_frag_data.literal_frags[slot];
9509 /* Already checked. */
9510 assert (lit_frag != NULL);
9511 assert (lit_sym != NULL);
9512 assert (tinsn->ntok == 1);
9513 /* Add a fixup. */
9514 target_seg = S_GET_SEGMENT (lit_sym);
9515 assert (target_seg);
9516 if (tinsn->tok[0].X_op == O_pltrel)
9517 reloc_type = BFD_RELOC_XTENSA_PLT;
9518 else
9519 reloc_type = BFD_RELOC_32;
9520 fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4,
9521 &tinsn->tok[0], FALSE, reloc_type);
9522 break;
9523
9524 case ITYPE_LABEL:
9525 break;
9526
9527 case ITYPE_INSN:
9528 xg_resolve_labels (tinsn, gen_label);
9529 xg_resolve_literals (tinsn, lit_sym);
9530 if (wide_insn && first)
9531 {
9532 first = FALSE;
9533 if (opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9534 {
9535 tinsn->record_fix = TRUE;
9536 orig_vinsn.slots[slot] = *tinsn;
9537 }
9538 else
9539 {
9540 orig_vinsn.slots[slot].opcode =
9541 xtensa_format_slot_nop_opcode (isa, fmt, slot);
9542 orig_vinsn.slots[slot].ntok = 0;
9543 orig_vinsn.slots[slot].record_fix = FALSE;
9544 }
9545 vinsn_to_insnbuf (&orig_vinsn, immed_instr, fragP, TRUE);
9546 xtensa_insnbuf_to_chars (isa, orig_vinsn.insnbuf,
9547 immed_instr, 0);
9548 fragP->tc_frag_data.is_insn = TRUE;
9549 size = xtensa_format_length (isa, fmt);
9550 if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
9551 {
9552 xtensa_format single_fmt =
9553 xg_get_single_format (tinsn->opcode);
9554
9555 xg_emit_insn_to_buf
9556 (tinsn, single_fmt, immed_instr + size, fragP,
9557 immed_instr - fragP->fr_literal + size, TRUE);
9558 size += xg_get_single_size (tinsn->opcode);
9559 }
9560 }
9561 else
9562 {
9563 xtensa_format single_format;
9564 size = xg_get_single_size (tinsn->opcode);
9565 single_format = xg_get_single_format (tinsn->opcode);
9566 xg_emit_insn_to_buf (tinsn, single_format, immed_instr,
9567 fragP,
9568 immed_instr - fragP->fr_literal, TRUE);
9569 #if 0
9570 /* Code to recognize branch-around expansion
9571 so the fragment is properly marked as ending in a
9572 jump. */
9573 if ((((i == istack.ninsn - 2)
9574 && (istack.insn[istack.ninsn-1].insn_type
9575 == ITYPE_LABEL))
9576 || i == istack.ninsn -1)
9577 && xtensa_opcode_is_jump (xtensa_default_isa,
9578 tinsn->opcode) == 1
9579 && fragP->fr_next != NULL
9580 && ! fragP->fr_next->tc_frag_data.is_unreachable)
9581 {
9582 /* Create a new unreachable frag of zero size. */
9583 size_t frag_size = sizeof (fragS);
9584 fragS *new_fragP = (fragS *) xmalloc (frag_size);
9585 memset (new_fragP, 0, frag_size);
9586 new_fragP->fr_address = fragP->fr_next->fr_address;
9587 new_fragP->fr_next = fragP->fr_next;
9588 new_fragP->fr_fix = 0;
9589 new_fragP->fr_var = 0;
9590 new_fragP->fr_type = rs_fill;
9591 new_fragP->tc_frag_data.is_unreachable = TRUE;
9592 /* The rest are zeros.... */
9593 /* Link it in to the chain. */
9594 fragP->fr_next = new_fragP;
9595 }
9596 #endif
9597 }
9598 immed_instr += size;
9599 total_size += size;
9600 break;
9601 }
9602 }
9603
9604 diff = total_size - old_size;
9605 assert (diff >= 0);
9606 if (diff != 0)
9607 expanded = TRUE;
9608 assert (diff <= fragP->fr_var);
9609 fragP->fr_var -= diff;
9610 fragP->fr_fix += diff;
9611 }
9612
9613 /* Clean it up. */
9614 xg_free_vinsn (&orig_vinsn);
9615
9616 /* Check for undefined immediates in LOOP instructions. */
9617 if (is_loop)
9618 {
9619 symbolS *sym;
9620 sym = orig_tinsn.tok[1].X_add_symbol;
9621 if (sym != NULL && !S_IS_DEFINED (sym))
9622 {
9623 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
9624 return;
9625 }
9626 sym = orig_tinsn.tok[1].X_op_symbol;
9627 if (sym != NULL && !S_IS_DEFINED (sym))
9628 {
9629 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
9630 return;
9631 }
9632 }
9633
9634 if (expanded && xtensa_opcode_is_loop (isa, orig_tinsn.opcode) == 1)
9635 convert_frag_immed_finish_loop (segP, fragP, &orig_tinsn);
9636
9637 if (expanded && is_direct_call_opcode (orig_tinsn.opcode))
9638 {
9639 /* Add an expansion note on the expanded instruction. */
9640 fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4,
9641 &orig_tinsn.tok[0], TRUE,
9642 BFD_RELOC_XTENSA_ASM_EXPAND);
9643 }
9644 }
9645
9646
9647 /* Add a new fix expression into the desired segment. We have to
9648 switch to that segment to do this. */
9649
9650 static fixS *
9651 fix_new_exp_in_seg (segT new_seg,
9652 subsegT new_subseg,
9653 fragS *frag,
9654 int where,
9655 int size,
9656 expressionS *exp,
9657 int pcrel,
9658 bfd_reloc_code_real_type r_type)
9659 {
9660 fixS *new_fix;
9661 segT seg = now_seg;
9662 subsegT subseg = now_subseg;
9663
9664 assert (new_seg != 0);
9665 subseg_set (new_seg, new_subseg);
9666
9667 new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
9668 subseg_set (seg, subseg);
9669 return new_fix;
9670 }
9671
9672
9673 /* Relax a loop instruction so that it can span loop >256 bytes.
9674
9675 loop as, .L1
9676 .L0:
9677 rsr as, LEND
9678 wsr as, LBEG
9679 addi as, as, lo8 (label-.L1)
9680 addmi as, as, mid8 (label-.L1)
9681 wsr as, LEND
9682 isync
9683 rsr as, LCOUNT
9684 addi as, as, 1
9685 .L1:
9686 <<body>>
9687 label:
9688 */
9689
9690 static void
9691 convert_frag_immed_finish_loop (segT segP, fragS *fragP, TInsn *tinsn)
9692 {
9693 TInsn loop_insn;
9694 TInsn addi_insn;
9695 TInsn addmi_insn;
9696 unsigned long target;
9697 static xtensa_insnbuf insnbuf = NULL;
9698 unsigned int loop_length, loop_length_hi, loop_length_lo;
9699 xtensa_isa isa = xtensa_default_isa;
9700 addressT loop_offset;
9701 addressT addi_offset = 9;
9702 addressT addmi_offset = 12;
9703 fragS *next_fragP;
9704 size_t target_count;
9705
9706 if (!insnbuf)
9707 insnbuf = xtensa_insnbuf_alloc (isa);
9708
9709 /* Get the loop offset. */
9710 loop_offset = get_expanded_loop_offset (tinsn->opcode);
9711
9712 /* Validate that there really is a LOOP at the loop_offset. Because
9713 loops are not bundleable, we can assume that the instruction will be
9714 in slot 0. */
9715 tinsn_from_chars (&loop_insn, fragP->fr_opcode + loop_offset, 0);
9716 tinsn_immed_from_frag (&loop_insn, fragP, 0);
9717
9718 assert (xtensa_opcode_is_loop (isa, loop_insn.opcode) == 1);
9719 addi_offset += loop_offset;
9720 addmi_offset += loop_offset;
9721
9722 assert (tinsn->ntok == 2);
9723 if (tinsn->tok[1].X_op == O_constant)
9724 target = tinsn->tok[1].X_add_number;
9725 else if (tinsn->tok[1].X_op == O_symbol)
9726 {
9727 /* Find the fragment. */
9728 symbolS *sym = tinsn->tok[1].X_add_symbol;
9729 assert (S_GET_SEGMENT (sym) == segP
9730 || S_GET_SEGMENT (sym) == absolute_section);
9731 target = (S_GET_VALUE (sym) + tinsn->tok[1].X_add_number);
9732 }
9733 else
9734 {
9735 as_bad (_("invalid expression evaluation type %d"), tinsn->tok[1].X_op);
9736 target = 0;
9737 }
9738
9739 know (symbolP);
9740 know (symbolP->sy_frag);
9741 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
9742 || symbol_get_frag (symbolP) == &zero_address_frag);
9743
9744 loop_length = target - (fragP->fr_address + fragP->fr_fix);
9745 loop_length_hi = loop_length & ~0x0ff;
9746 loop_length_lo = loop_length & 0x0ff;
9747 if (loop_length_lo >= 128)
9748 {
9749 loop_length_lo -= 256;
9750 loop_length_hi += 256;
9751 }
9752
9753 /* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most
9754 32512. If the loop is larger than that, then we just fail. */
9755 if (loop_length_hi > 32512)
9756 as_bad_where (fragP->fr_file, fragP->fr_line,
9757 _("loop too long for LOOP instruction"));
9758
9759 tinsn_from_chars (&addi_insn, fragP->fr_opcode + addi_offset, 0);
9760 assert (addi_insn.opcode == xtensa_addi_opcode);
9761
9762 tinsn_from_chars (&addmi_insn, fragP->fr_opcode + addmi_offset, 0);
9763 assert (addmi_insn.opcode == xtensa_addmi_opcode);
9764
9765 set_expr_const (&addi_insn.tok[2], loop_length_lo);
9766 tinsn_to_insnbuf (&addi_insn, insnbuf);
9767
9768 fragP->tc_frag_data.is_insn = TRUE;
9769 xtensa_insnbuf_to_chars (isa, insnbuf, fragP->fr_opcode + addi_offset, 0);
9770
9771 set_expr_const (&addmi_insn.tok[2], loop_length_hi);
9772 tinsn_to_insnbuf (&addmi_insn, insnbuf);
9773 xtensa_insnbuf_to_chars (isa, insnbuf, fragP->fr_opcode + addmi_offset, 0);
9774
9775 /* Walk through all of the frags from here to the loop end
9776 and mark them as no_transform to keep them from being modified
9777 by the linker. If we ever have a relocation for the
9778 addi/addmi of the difference of two symbols we can remove this. */
9779
9780 target_count = 0;
9781 for (next_fragP = fragP; next_fragP != NULL;
9782 next_fragP = next_fragP->fr_next)
9783 {
9784 next_fragP->tc_frag_data.is_no_transform = TRUE;
9785 if (next_fragP->tc_frag_data.is_loop_target)
9786 target_count++;
9787 if (target_count == 2)
9788 break;
9789 }
9790 }
9791
9792 \f
9793 /* A map that keeps information on a per-subsegment basis. This is
9794 maintained during initial assembly, but is invalid once the
9795 subsegments are smashed together. I.E., it cannot be used during
9796 the relaxation. */
9797
9798 typedef struct subseg_map_struct
9799 {
9800 /* the key */
9801 segT seg;
9802 subsegT subseg;
9803
9804 /* the data */
9805 unsigned flags;
9806 float total_freq; /* fall-through + branch target frequency */
9807 float target_freq; /* branch target frequency alone */
9808
9809 struct subseg_map_struct *next;
9810 } subseg_map;
9811
9812
9813 static subseg_map *sseg_map = NULL;
9814
9815 static subseg_map *
9816 get_subseg_info (segT seg, subsegT subseg)
9817 {
9818 subseg_map *subseg_e;
9819
9820 for (subseg_e = sseg_map; subseg_e; subseg_e = subseg_e->next)
9821 {
9822 if (seg == subseg_e->seg && subseg == subseg_e->subseg)
9823 break;
9824 }
9825 return subseg_e;
9826 }
9827
9828
9829 static subseg_map *
9830 add_subseg_info (segT seg, subsegT subseg)
9831 {
9832 subseg_map *subseg_e = (subseg_map *) xmalloc (sizeof (subseg_map));
9833 memset (subseg_e, 0, sizeof (subseg_map));
9834 subseg_e->seg = seg;
9835 subseg_e->subseg = subseg;
9836 subseg_e->flags = 0;
9837 /* Start off considering every branch target very important. */
9838 subseg_e->target_freq = 1.0;
9839 subseg_e->total_freq = 1.0;
9840 subseg_e->next = sseg_map;
9841 sseg_map = subseg_e;
9842 return subseg_e;
9843 }
9844
9845
9846 static unsigned
9847 get_last_insn_flags (segT seg, subsegT subseg)
9848 {
9849 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9850 if (subseg_e)
9851 return subseg_e->flags;
9852 return 0;
9853 }
9854
9855
9856 static void
9857 set_last_insn_flags (segT seg,
9858 subsegT subseg,
9859 unsigned fl,
9860 bfd_boolean val)
9861 {
9862 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9863 if (! subseg_e)
9864 subseg_e = add_subseg_info (seg, subseg);
9865 if (val)
9866 subseg_e->flags |= fl;
9867 else
9868 subseg_e->flags &= ~fl;
9869 }
9870
9871
9872 static float
9873 get_subseg_total_freq (segT seg, subsegT subseg)
9874 {
9875 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9876 if (subseg_e)
9877 return subseg_e->total_freq;
9878 return 1.0;
9879 }
9880
9881
9882 static float
9883 get_subseg_target_freq (segT seg, subsegT subseg)
9884 {
9885 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9886 if (subseg_e)
9887 return subseg_e->target_freq;
9888 return 1.0;
9889 }
9890
9891
9892 static void
9893 set_subseg_freq (segT seg, subsegT subseg, float total_f, float target_f)
9894 {
9895 subseg_map *subseg_e = get_subseg_info (seg, subseg);
9896 if (! subseg_e)
9897 subseg_e = add_subseg_info (seg, subseg);
9898 subseg_e->total_freq = total_f;
9899 subseg_e->target_freq = target_f;
9900 }
9901
9902 \f
9903 /* Segment Lists and emit_state Stuff. */
9904
9905 /* Remove the segment from the global sections list. */
9906
9907 static void
9908 xtensa_remove_section (segT sec)
9909 {
9910 /* Handle brain-dead bfd_section_list_remove macro, which
9911 expect the address of the prior section's "next" field, not
9912 just the address of the section to remove. */
9913
9914 segT *ps_next_ptr = &stdoutput->sections;
9915 while (*ps_next_ptr != sec && *ps_next_ptr != NULL)
9916 ps_next_ptr = &(*ps_next_ptr)->next;
9917
9918 assert (*ps_next_ptr != NULL);
9919
9920 bfd_section_list_remove (stdoutput, ps_next_ptr);
9921 }
9922
9923
9924 static void
9925 xtensa_insert_section (segT after_sec, segT sec)
9926 {
9927 segT *after_sec_next;
9928 if (after_sec == NULL)
9929 after_sec_next = &stdoutput->sections;
9930 else
9931 after_sec_next = &after_sec->next;
9932
9933 bfd_section_list_insert (stdoutput, after_sec_next, sec);
9934 }
9935
9936
9937 static void
9938 xtensa_move_seg_list_to_beginning (seg_list *head)
9939 {
9940 head = head->next;
9941 while (head)
9942 {
9943 segT literal_section = head->seg;
9944
9945 /* Move the literal section to the front of the section list. */
9946 assert (literal_section);
9947 xtensa_remove_section (literal_section);
9948 xtensa_insert_section (NULL, literal_section);
9949
9950 head = head->next;
9951 }
9952 }
9953
9954
9955 static void mark_literal_frags (seg_list *);
9956
9957 static void
9958 xtensa_move_literals (void)
9959 {
9960 seg_list *segment;
9961 frchainS *frchain_from, *frchain_to;
9962 fragS *search_frag, *next_frag, *last_frag, *literal_pool, *insert_after;
9963 fragS **frag_splice;
9964 emit_state state;
9965 segT dest_seg;
9966 fixS *fix, *next_fix, **fix_splice;
9967 sym_list *lit;
9968
9969 mark_literal_frags (literal_head->next);
9970 mark_literal_frags (init_literal_head->next);
9971 mark_literal_frags (fini_literal_head->next);
9972
9973 if (use_literal_section)
9974 return;
9975
9976 segment = literal_head->next;
9977 while (segment)
9978 {
9979 frchain_from = seg_info (segment->seg)->frchainP;
9980 search_frag = frchain_from->frch_root;
9981 literal_pool = NULL;
9982 frchain_to = NULL;
9983 frag_splice = &(frchain_from->frch_root);
9984
9985 while (!search_frag->tc_frag_data.literal_frag)
9986 {
9987 assert (search_frag->fr_fix == 0
9988 || search_frag->fr_type == rs_align);
9989 search_frag = search_frag->fr_next;
9990 }
9991
9992 assert (search_frag->tc_frag_data.literal_frag->fr_subtype
9993 == RELAX_LITERAL_POOL_BEGIN);
9994 xtensa_switch_section_emit_state (&state, segment->seg, 0);
9995
9996 /* Make sure that all the frags in this series are closed, and
9997 that there is at least one left over of zero-size. This
9998 prevents us from making a segment with an frchain without any
9999 frags in it. */
10000 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10001 xtensa_set_frag_assembly_state (frag_now);
10002 last_frag = frag_now;
10003 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10004 xtensa_set_frag_assembly_state (frag_now);
10005
10006 while (search_frag != frag_now)
10007 {
10008 next_frag = search_frag->fr_next;
10009
10010 /* First, move the frag out of the literal section and
10011 to the appropriate place. */
10012 if (search_frag->tc_frag_data.literal_frag)
10013 {
10014 literal_pool = search_frag->tc_frag_data.literal_frag;
10015 assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN);
10016 /* Note that we set this fr_var to be a fix
10017 chain when we created the literal pool location
10018 as RELAX_LITERAL_POOL_BEGIN. */
10019 frchain_to = (frchainS *) literal_pool->fr_var;
10020 }
10021 insert_after = literal_pool;
10022
10023 while (insert_after->fr_next->fr_subtype != RELAX_LITERAL_POOL_END)
10024 insert_after = insert_after->fr_next;
10025
10026 dest_seg = (segT) insert_after->fr_next->fr_var;
10027
10028 *frag_splice = next_frag;
10029 search_frag->fr_next = insert_after->fr_next;
10030 insert_after->fr_next = search_frag;
10031 search_frag->tc_frag_data.lit_seg = dest_seg;
10032
10033 /* Now move any fixups associated with this frag to the
10034 right section. */
10035 fix = frchain_from->fix_root;
10036 fix_splice = &(frchain_from->fix_root);
10037 while (fix)
10038 {
10039 next_fix = fix->fx_next;
10040 if (fix->fx_frag == search_frag)
10041 {
10042 *fix_splice = next_fix;
10043 fix->fx_next = frchain_to->fix_root;
10044 frchain_to->fix_root = fix;
10045 if (frchain_to->fix_tail == NULL)
10046 frchain_to->fix_tail = fix;
10047 }
10048 else
10049 fix_splice = &(fix->fx_next);
10050 fix = next_fix;
10051 }
10052 search_frag = next_frag;
10053 }
10054
10055 if (frchain_from->fix_root != NULL)
10056 {
10057 frchain_from = seg_info (segment->seg)->frchainP;
10058 as_warn (_("fixes not all moved from %s"), segment->seg->name);
10059
10060 assert (frchain_from->fix_root == NULL);
10061 }
10062 frchain_from->fix_tail = NULL;
10063 xtensa_restore_emit_state (&state);
10064 segment = segment->next;
10065 }
10066
10067 /* Now fix up the SEGMENT value for all the literal symbols. */
10068 for (lit = literal_syms; lit; lit = lit->next)
10069 {
10070 symbolS *lit_sym = lit->sym;
10071 segT dest_seg = symbol_get_frag (lit_sym)->tc_frag_data.lit_seg;
10072 if (dest_seg)
10073 S_SET_SEGMENT (lit_sym, dest_seg);
10074 }
10075 }
10076
10077
10078 /* Walk over all the frags for segments in a list and mark them as
10079 containing literals. As clunky as this is, we can't rely on frag_var
10080 and frag_variant to get called in all situations. */
10081
10082 static void
10083 mark_literal_frags (seg_list *segment)
10084 {
10085 frchainS *frchain_from;
10086 fragS *search_frag;
10087
10088 while (segment)
10089 {
10090 frchain_from = seg_info (segment->seg)->frchainP;
10091 search_frag = frchain_from->frch_root;
10092 while (search_frag)
10093 {
10094 search_frag->tc_frag_data.is_literal = TRUE;
10095 search_frag = search_frag->fr_next;
10096 }
10097 segment = segment->next;
10098 }
10099 }
10100
10101
10102 static void
10103 xtensa_reorder_seg_list (seg_list *head, segT after)
10104 {
10105 /* Move all of the sections in the section list to come
10106 after "after" in the gnu segment list. */
10107
10108 head = head->next;
10109 while (head)
10110 {
10111 segT literal_section = head->seg;
10112
10113 /* Move the literal section after "after". */
10114 assert (literal_section);
10115 if (literal_section != after)
10116 {
10117 xtensa_remove_section (literal_section);
10118 xtensa_insert_section (after, literal_section);
10119 }
10120
10121 head = head->next;
10122 }
10123 }
10124
10125
10126 /* Push all the literal segments to the end of the gnu list. */
10127
10128 static void
10129 xtensa_reorder_segments (void)
10130 {
10131 segT sec;
10132 segT last_sec = 0;
10133 int old_count = 0;
10134 int new_count = 0;
10135
10136 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
10137 {
10138 last_sec = sec;
10139 old_count++;
10140 }
10141
10142 /* Now that we have the last section, push all the literal
10143 sections to the end. */
10144 xtensa_reorder_seg_list (literal_head, last_sec);
10145 xtensa_reorder_seg_list (init_literal_head, last_sec);
10146 xtensa_reorder_seg_list (fini_literal_head, last_sec);
10147
10148 /* Now perform the final error check. */
10149 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
10150 new_count++;
10151 assert (new_count == old_count);
10152 }
10153
10154
10155 /* Change the emit state (seg, subseg, and frag related stuff) to the
10156 correct location. Return a emit_state which can be passed to
10157 xtensa_restore_emit_state to return to current fragment. */
10158
10159 static void
10160 xtensa_switch_to_literal_fragment (emit_state *result)
10161 {
10162 if (directive_state[directive_absolute_literals])
10163 {
10164 cache_literal_section (0, default_lit_sections.lit4_seg_name,
10165 &default_lit_sections.lit4_seg, FALSE);
10166 xtensa_switch_section_emit_state (result,
10167 default_lit_sections.lit4_seg, 0);
10168 }
10169 else
10170 xtensa_switch_to_non_abs_literal_fragment (result);
10171
10172 /* Do a 4-byte align here. */
10173 frag_align (2, 0, 0);
10174 record_alignment (now_seg, 2);
10175 }
10176
10177
10178 static void
10179 xtensa_switch_to_non_abs_literal_fragment (emit_state *result)
10180 {
10181 /* When we mark a literal pool location, we want to put a frag in
10182 the literal pool that points to it. But to do that, we want to
10183 switch_to_literal_fragment. But literal sections don't have
10184 literal pools, so their location is always null, so we would
10185 recurse forever. This is kind of hacky, but it works. */
10186
10187 static bfd_boolean recursive = FALSE;
10188 fragS *pool_location = get_literal_pool_location (now_seg);
10189 bfd_boolean is_init =
10190 (now_seg && !strcmp (segment_name (now_seg), INIT_SECTION_NAME));
10191
10192 bfd_boolean is_fini =
10193 (now_seg && !strcmp (segment_name (now_seg), FINI_SECTION_NAME));
10194
10195 if (pool_location == NULL
10196 && !use_literal_section
10197 && !recursive
10198 && !is_init && ! is_fini)
10199 {
10200 as_bad (_("literal pool location required for text-section-literals; specify with .literal_position"));
10201 recursive = TRUE;
10202 xtensa_mark_literal_pool_location ();
10203 recursive = FALSE;
10204 }
10205
10206 /* Special case: If we are in the ".fini" or ".init" section, then
10207 we will ALWAYS be generating to the ".fini.literal" and
10208 ".init.literal" sections. */
10209
10210 if (is_init)
10211 {
10212 cache_literal_section (init_literal_head,
10213 default_lit_sections.init_lit_seg_name,
10214 &default_lit_sections.init_lit_seg, TRUE);
10215 xtensa_switch_section_emit_state (result,
10216 default_lit_sections.init_lit_seg, 0);
10217 }
10218 else if (is_fini)
10219 {
10220 cache_literal_section (fini_literal_head,
10221 default_lit_sections.fini_lit_seg_name,
10222 &default_lit_sections.fini_lit_seg, TRUE);
10223 xtensa_switch_section_emit_state (result,
10224 default_lit_sections.fini_lit_seg, 0);
10225 }
10226 else
10227 {
10228 cache_literal_section (literal_head,
10229 default_lit_sections.lit_seg_name,
10230 &default_lit_sections.lit_seg, TRUE);
10231 xtensa_switch_section_emit_state (result,
10232 default_lit_sections.lit_seg, 0);
10233 }
10234
10235 if (!use_literal_section
10236 && !is_init && !is_fini
10237 && get_literal_pool_location (now_seg) != pool_location)
10238 {
10239 /* Close whatever frag is there. */
10240 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10241 xtensa_set_frag_assembly_state (frag_now);
10242 frag_now->tc_frag_data.literal_frag = pool_location;
10243 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
10244 xtensa_set_frag_assembly_state (frag_now);
10245 }
10246 }
10247
10248
10249 /* Call this function before emitting data into the literal section.
10250 This is a helper function for xtensa_switch_to_literal_fragment.
10251 This is similar to a .section new_now_seg subseg. */
10252
10253 static void
10254 xtensa_switch_section_emit_state (emit_state *state,
10255 segT new_now_seg,
10256 subsegT new_now_subseg)
10257 {
10258 state->name = now_seg->name;
10259 state->now_seg = now_seg;
10260 state->now_subseg = now_subseg;
10261 state->generating_literals = generating_literals;
10262 generating_literals++;
10263 subseg_new (segment_name (new_now_seg), new_now_subseg);
10264 }
10265
10266
10267 /* Use to restore the emitting into the normal place. */
10268
10269 static void
10270 xtensa_restore_emit_state (emit_state *state)
10271 {
10272 generating_literals = state->generating_literals;
10273 subseg_new (state->name, state->now_subseg);
10274 }
10275
10276
10277 /* Get a segment of a given name. If the segment is already
10278 present, return it; otherwise, create a new one. */
10279
10280 static void
10281 cache_literal_section (seg_list *head,
10282 const char *name,
10283 segT *pseg,
10284 bfd_boolean is_code)
10285 {
10286 segT current_section = now_seg;
10287 int current_subsec = now_subseg;
10288 segT seg;
10289
10290 if (*pseg != 0)
10291 return;
10292
10293 /* Check if the named section exists. */
10294 for (seg = stdoutput->sections; seg; seg = seg->next)
10295 {
10296 if (!strcmp (segment_name (seg), name))
10297 break;
10298 }
10299
10300 if (!seg)
10301 {
10302 /* Create a new literal section. */
10303 seg = subseg_new (name, (subsegT) 0);
10304 if (head)
10305 {
10306 /* Add the newly created literal segment to the specified list. */
10307 seg_list *n = (seg_list *) xmalloc (sizeof (seg_list));
10308 n->seg = seg;
10309 n->next = head->next;
10310 head->next = n;
10311 }
10312 bfd_set_section_flags (stdoutput, seg, SEC_HAS_CONTENTS |
10313 SEC_READONLY | SEC_ALLOC | SEC_LOAD
10314 | (is_code ? SEC_CODE : SEC_DATA));
10315 bfd_set_section_alignment (stdoutput, seg, 2);
10316 }
10317
10318 *pseg = seg;
10319 subseg_set (current_section, current_subsec);
10320 }
10321
10322 \f
10323 /* Property Tables Stuff. */
10324
10325 #define XTENSA_INSN_SEC_NAME ".xt.insn"
10326 #define XTENSA_LIT_SEC_NAME ".xt.lit"
10327 #define XTENSA_PROP_SEC_NAME ".xt.prop"
10328
10329 typedef bfd_boolean (*frag_predicate) (const fragS *);
10330 typedef void (*frag_flags_fn) (const fragS *, frag_flags *);
10331
10332 static bfd_boolean get_frag_is_literal (const fragS *);
10333 static void xtensa_create_property_segments
10334 (frag_predicate, frag_predicate, const char *, xt_section_type);
10335 static void xtensa_create_xproperty_segments
10336 (frag_flags_fn, const char *, xt_section_type);
10337 static segment_info_type *retrieve_segment_info (segT);
10338 static segT retrieve_xtensa_section (char *);
10339 static bfd_boolean section_has_property (segT, frag_predicate);
10340 static bfd_boolean section_has_xproperty (segT, frag_flags_fn);
10341 static void add_xt_block_frags
10342 (segT, segT, xtensa_block_info **, frag_predicate, frag_predicate);
10343 static bfd_boolean xtensa_frag_flags_is_empty (const frag_flags *);
10344 static void xtensa_frag_flags_init (frag_flags *);
10345 static void get_frag_property_flags (const fragS *, frag_flags *);
10346 static bfd_vma frag_flags_to_number (const frag_flags *);
10347 static void add_xt_prop_frags
10348 (segT, segT, xtensa_block_info **, frag_flags_fn);
10349
10350 /* Set up property tables after relaxation. */
10351
10352 void
10353 xtensa_post_relax_hook (void)
10354 {
10355 xtensa_move_seg_list_to_beginning (literal_head);
10356 xtensa_move_seg_list_to_beginning (init_literal_head);
10357 xtensa_move_seg_list_to_beginning (fini_literal_head);
10358
10359 xtensa_find_unmarked_state_frags ();
10360
10361 if (use_literal_section)
10362 xtensa_create_property_segments (get_frag_is_literal,
10363 NULL,
10364 XTENSA_LIT_SEC_NAME,
10365 xt_literal_sec);
10366 xtensa_create_xproperty_segments (get_frag_property_flags,
10367 XTENSA_PROP_SEC_NAME,
10368 xt_prop_sec);
10369
10370 if (warn_unaligned_branch_targets)
10371 bfd_map_over_sections (stdoutput, xtensa_find_unaligned_branch_targets, 0);
10372 bfd_map_over_sections (stdoutput, xtensa_find_unaligned_loops, 0);
10373 }
10374
10375
10376 /* This function is only meaningful after xtensa_move_literals. */
10377
10378 static bfd_boolean
10379 get_frag_is_literal (const fragS *fragP)
10380 {
10381 assert (fragP != NULL);
10382 return fragP->tc_frag_data.is_literal;
10383 }
10384
10385
10386 static void
10387 xtensa_create_property_segments (frag_predicate property_function,
10388 frag_predicate end_property_function,
10389 const char *section_name_base,
10390 xt_section_type sec_type)
10391 {
10392 segT *seclist;
10393
10394 /* Walk over all of the current segments.
10395 Walk over each fragment
10396 For each non-empty fragment,
10397 Build a property record (append where possible). */
10398
10399 for (seclist = &stdoutput->sections;
10400 seclist && *seclist;
10401 seclist = &(*seclist)->next)
10402 {
10403 segT sec = *seclist;
10404 flagword flags;
10405
10406 flags = bfd_get_section_flags (stdoutput, sec);
10407 if (flags & SEC_DEBUGGING)
10408 continue;
10409 if (!(flags & SEC_ALLOC))
10410 continue;
10411
10412 if (section_has_property (sec, property_function))
10413 {
10414 char *property_section_name =
10415 xtensa_get_property_section_name (sec, section_name_base);
10416 segT insn_sec = retrieve_xtensa_section (property_section_name);
10417 segment_info_type *xt_seg_info = retrieve_segment_info (insn_sec);
10418 xtensa_block_info **xt_blocks =
10419 &xt_seg_info->tc_segment_info_data.blocks[sec_type];
10420 /* Walk over all of the frchains here and add new sections. */
10421 add_xt_block_frags (sec, insn_sec, xt_blocks, property_function,
10422 end_property_function);
10423 }
10424 }
10425
10426 /* Now we fill them out.... */
10427
10428 for (seclist = &stdoutput->sections;
10429 seclist && *seclist;
10430 seclist = &(*seclist)->next)
10431 {
10432 segment_info_type *seginfo;
10433 xtensa_block_info *block;
10434 segT sec = *seclist;
10435
10436 seginfo = seg_info (sec);
10437 block = seginfo->tc_segment_info_data.blocks[sec_type];
10438
10439 if (block)
10440 {
10441 xtensa_block_info *cur_block;
10442 /* This is a section with some data. */
10443 int num_recs = 0;
10444 size_t rec_size;
10445
10446 for (cur_block = block; cur_block; cur_block = cur_block->next)
10447 num_recs++;
10448
10449 rec_size = num_recs * 8;
10450 bfd_set_section_size (stdoutput, sec, rec_size);
10451
10452 /* In order to make this work with the assembler, we have to
10453 build some frags and then build the "fixups" for it. It
10454 would be easier to just set the contents then set the
10455 arlents. */
10456
10457 if (num_recs)
10458 {
10459 /* Allocate a fragment and leak it. */
10460 fragS *fragP;
10461 size_t frag_size;
10462 fixS *fixes;
10463 frchainS *frchainP;
10464 int i;
10465 char *frag_data;
10466
10467 frag_size = sizeof (fragS) + rec_size;
10468 fragP = (fragS *) xmalloc (frag_size);
10469
10470 memset (fragP, 0, frag_size);
10471 fragP->fr_address = 0;
10472 fragP->fr_next = NULL;
10473 fragP->fr_fix = rec_size;
10474 fragP->fr_var = 0;
10475 fragP->fr_type = rs_fill;
10476 /* The rest are zeros. */
10477
10478 frchainP = seginfo->frchainP;
10479 frchainP->frch_root = fragP;
10480 frchainP->frch_last = fragP;
10481
10482 fixes = (fixS *) xmalloc (sizeof (fixS) * num_recs);
10483 memset (fixes, 0, sizeof (fixS) * num_recs);
10484
10485 seginfo->fix_root = fixes;
10486 seginfo->fix_tail = &fixes[num_recs - 1];
10487 cur_block = block;
10488 frag_data = &fragP->fr_literal[0];
10489 for (i = 0; i < num_recs; i++)
10490 {
10491 fixS *fix = &fixes[i];
10492 assert (cur_block);
10493
10494 /* Write the fixup. */
10495 if (i != num_recs - 1)
10496 fix->fx_next = &fixes[i + 1];
10497 else
10498 fix->fx_next = NULL;
10499 fix->fx_size = 4;
10500 fix->fx_done = 0;
10501 fix->fx_frag = fragP;
10502 fix->fx_where = i * 8;
10503 fix->fx_addsy = section_symbol (cur_block->sec);
10504 fix->fx_offset = cur_block->offset;
10505 fix->fx_r_type = BFD_RELOC_32;
10506 fix->fx_file = "Internal Assembly";
10507 fix->fx_line = 0;
10508
10509 /* Write the length. */
10510 md_number_to_chars (&frag_data[4 + 8 * i],
10511 cur_block->size, 4);
10512 cur_block = cur_block->next;
10513 }
10514 }
10515 }
10516 }
10517 }
10518
10519
10520 static void
10521 xtensa_create_xproperty_segments (frag_flags_fn flag_fn,
10522 const char *section_name_base,
10523 xt_section_type sec_type)
10524 {
10525 segT *seclist;
10526
10527 /* Walk over all of the current segments.
10528 Walk over each fragment.
10529 For each fragment that has instructions,
10530 build an instruction record (append where possible). */
10531
10532 for (seclist = &stdoutput->sections;
10533 seclist && *seclist;
10534 seclist = &(*seclist)->next)
10535 {
10536 segT sec = *seclist;
10537 flagword flags;
10538
10539 flags = bfd_get_section_flags (stdoutput, sec);
10540 if (flags & SEC_DEBUGGING)
10541 continue;
10542 if (!(flags & SEC_ALLOC))
10543 continue;
10544
10545 if (section_has_xproperty (sec, flag_fn))
10546 {
10547 char *property_section_name =
10548 xtensa_get_property_section_name (sec, section_name_base);
10549 segT insn_sec = retrieve_xtensa_section (property_section_name);
10550 segment_info_type *xt_seg_info = retrieve_segment_info (insn_sec);
10551 xtensa_block_info **xt_blocks =
10552 &xt_seg_info->tc_segment_info_data.blocks[sec_type];
10553 /* Walk over all of the frchains here and add new sections. */
10554 add_xt_prop_frags (sec, insn_sec, xt_blocks, flag_fn);
10555 }
10556 }
10557
10558 /* Now we fill them out.... */
10559
10560 for (seclist = &stdoutput->sections;
10561 seclist && *seclist;
10562 seclist = &(*seclist)->next)
10563 {
10564 segment_info_type *seginfo;
10565 xtensa_block_info *block;
10566 segT sec = *seclist;
10567
10568 seginfo = seg_info (sec);
10569 block = seginfo->tc_segment_info_data.blocks[sec_type];
10570
10571 if (block)
10572 {
10573 xtensa_block_info *cur_block;
10574 /* This is a section with some data. */
10575 int num_recs = 0;
10576 size_t rec_size;
10577
10578 for (cur_block = block; cur_block; cur_block = cur_block->next)
10579 num_recs++;
10580
10581 rec_size = num_recs * (8 + 4);
10582 bfd_set_section_size (stdoutput, sec, rec_size);
10583
10584 /* elf_section_data (sec)->this_hdr.sh_entsize = 12; */
10585
10586 /* In order to make this work with the assembler, we have to build
10587 some frags then build the "fixups" for it. It would be easier to
10588 just set the contents then set the arlents. */
10589
10590 if (num_recs)
10591 {
10592 /* Allocate a fragment and (unfortunately) leak it. */
10593 fragS *fragP;
10594 size_t frag_size;
10595 fixS *fixes;
10596 frchainS *frchainP;
10597 int i;
10598 char *frag_data;
10599
10600 frag_size = sizeof (fragS) + rec_size;
10601 fragP = (fragS *) xmalloc (frag_size);
10602
10603 memset (fragP, 0, frag_size);
10604 fragP->fr_address = 0;
10605 fragP->fr_next = NULL;
10606 fragP->fr_fix = rec_size;
10607 fragP->fr_var = 0;
10608 fragP->fr_type = rs_fill;
10609 /* The rest are zeros. */
10610
10611 frchainP = seginfo->frchainP;
10612 frchainP->frch_root = fragP;
10613 frchainP->frch_last = fragP;
10614
10615 fixes = (fixS *) xmalloc (sizeof (fixS) * num_recs);
10616 memset (fixes, 0, sizeof (fixS) * num_recs);
10617
10618 seginfo->fix_root = fixes;
10619 seginfo->fix_tail = &fixes[num_recs - 1];
10620 cur_block = block;
10621 frag_data = &fragP->fr_literal[0];
10622 for (i = 0; i < num_recs; i++)
10623 {
10624 fixS *fix = &fixes[i];
10625 assert (cur_block);
10626
10627 /* Write the fixup. */
10628 if (i != num_recs - 1)
10629 fix->fx_next = &fixes[i + 1];
10630 else
10631 fix->fx_next = NULL;
10632 fix->fx_size = 4;
10633 fix->fx_done = 0;
10634 fix->fx_frag = fragP;
10635 fix->fx_where = i * (8 + 4);
10636 fix->fx_addsy = section_symbol (cur_block->sec);
10637 fix->fx_offset = cur_block->offset;
10638 fix->fx_r_type = BFD_RELOC_32;
10639 fix->fx_file = "Internal Assembly";
10640 fix->fx_line = 0;
10641
10642 /* Write the length. */
10643 md_number_to_chars (&frag_data[4 + (8+4) * i],
10644 cur_block->size, 4);
10645 md_number_to_chars (&frag_data[8 + (8+4) * i],
10646 frag_flags_to_number (&cur_block->flags),
10647 4);
10648 cur_block = cur_block->next;
10649 }
10650 }
10651 }
10652 }
10653 }
10654
10655
10656 static segment_info_type *
10657 retrieve_segment_info (segT seg)
10658 {
10659 segment_info_type *seginfo;
10660 seginfo = (segment_info_type *) bfd_get_section_userdata (stdoutput, seg);
10661 if (!seginfo)
10662 {
10663 frchainS *frchainP;
10664
10665 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
10666 memset ((void *) seginfo, 0, sizeof (*seginfo));
10667 seginfo->fix_root = NULL;
10668 seginfo->fix_tail = NULL;
10669 seginfo->bfd_section = seg;
10670 seginfo->sym = 0;
10671 /* We will not be dealing with these, only our special ones. */
10672 #if 0
10673 if (seg == bfd_abs_section_ptr)
10674 abs_seg_info = seginfo;
10675 else if (seg == bfd_und_section_ptr)
10676 und_seg_info = seginfo;
10677 else
10678 #endif
10679 bfd_set_section_userdata (stdoutput, seg, (void *) seginfo);
10680 #if 0
10681 seg_fix_rootP = &segment_info[seg].fix_root;
10682 seg_fix_tailP = &segment_info[seg].fix_tail;
10683 #endif
10684
10685 frchainP = (frchainS *) xmalloc (sizeof (frchainS));
10686 frchainP->frch_root = NULL;
10687 frchainP->frch_last = NULL;
10688 frchainP->frch_next = NULL;
10689 frchainP->frch_seg = seg;
10690 frchainP->frch_subseg = 0;
10691 frchainP->fix_root = NULL;
10692 frchainP->fix_tail = NULL;
10693 /* Do not init the objstack. */
10694 /* obstack_begin (&frchainP->frch_obstack, chunksize); */
10695 /* frchainP->frch_frag_now = fragP; */
10696 frchainP->frch_frag_now = NULL;
10697
10698 seginfo->frchainP = frchainP;
10699 }
10700
10701 return seginfo;
10702 }
10703
10704
10705 static segT
10706 retrieve_xtensa_section (char *sec_name)
10707 {
10708 bfd *abfd = stdoutput;
10709 flagword flags, out_flags, link_once_flags;
10710 segT s;
10711
10712 flags = bfd_get_section_flags (abfd, now_seg);
10713 link_once_flags = (flags & SEC_LINK_ONCE);
10714 if (link_once_flags)
10715 link_once_flags |= (flags & SEC_LINK_DUPLICATES);
10716 out_flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY | link_once_flags);
10717
10718 s = bfd_make_section_old_way (abfd, sec_name);
10719 if (s == NULL)
10720 as_bad (_("could not create section %s"), sec_name);
10721 if (!bfd_set_section_flags (abfd, s, out_flags))
10722 as_bad (_("invalid flag combination on section %s"), sec_name);
10723
10724 return s;
10725 }
10726
10727
10728 static bfd_boolean
10729 section_has_property (segT sec, frag_predicate property_function)
10730 {
10731 segment_info_type *seginfo = seg_info (sec);
10732 fragS *fragP;
10733
10734 if (seginfo && seginfo->frchainP)
10735 {
10736 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
10737 {
10738 if (property_function (fragP)
10739 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
10740 return TRUE;
10741 }
10742 }
10743 return FALSE;
10744 }
10745
10746
10747 static bfd_boolean
10748 section_has_xproperty (segT sec, frag_flags_fn property_function)
10749 {
10750 segment_info_type *seginfo = seg_info (sec);
10751 fragS *fragP;
10752
10753 if (seginfo && seginfo->frchainP)
10754 {
10755 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
10756 {
10757 frag_flags prop_flags;
10758 property_function (fragP, &prop_flags);
10759 if (!xtensa_frag_flags_is_empty (&prop_flags))
10760 return TRUE;
10761 }
10762 }
10763 return FALSE;
10764 }
10765
10766
10767 /* Two types of block sections exist right now: literal and insns. */
10768
10769 static void
10770 add_xt_block_frags (segT sec,
10771 segT xt_block_sec,
10772 xtensa_block_info **xt_block,
10773 frag_predicate property_function,
10774 frag_predicate end_property_function)
10775 {
10776 segment_info_type *seg_info;
10777 segment_info_type *xt_seg_info;
10778 bfd_vma seg_offset;
10779 fragS *fragP;
10780
10781 xt_seg_info = retrieve_segment_info (xt_block_sec);
10782 seg_info = retrieve_segment_info (sec);
10783
10784 /* Build it if needed. */
10785 while (*xt_block != NULL)
10786 xt_block = &(*xt_block)->next;
10787 /* We are either at NULL at the beginning or at the end. */
10788
10789 /* Walk through the frags. */
10790 seg_offset = 0;
10791
10792 if (seg_info->frchainP)
10793 {
10794 for (fragP = seg_info->frchainP->frch_root;
10795 fragP;
10796 fragP = fragP->fr_next)
10797 {
10798 if (property_function (fragP)
10799 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
10800 {
10801 if (*xt_block != NULL)
10802 {
10803 if ((*xt_block)->offset + (*xt_block)->size
10804 == fragP->fr_address)
10805 (*xt_block)->size += fragP->fr_fix;
10806 else
10807 xt_block = &((*xt_block)->next);
10808 }
10809 if (*xt_block == NULL)
10810 {
10811 xtensa_block_info *new_block = (xtensa_block_info *)
10812 xmalloc (sizeof (xtensa_block_info));
10813 new_block->sec = sec;
10814 new_block->offset = fragP->fr_address;
10815 new_block->size = fragP->fr_fix;
10816 new_block->next = NULL;
10817 xtensa_frag_flags_init (&new_block->flags);
10818 *xt_block = new_block;
10819 }
10820 if (end_property_function
10821 && end_property_function (fragP))
10822 {
10823 xt_block = &((*xt_block)->next);
10824 }
10825 }
10826 }
10827 }
10828 }
10829
10830
10831 /* Break the encapsulation of add_xt_prop_frags here. */
10832
10833 static bfd_boolean
10834 xtensa_frag_flags_is_empty (const frag_flags *prop_flags)
10835 {
10836 if (prop_flags->is_literal
10837 || prop_flags->is_insn
10838 || prop_flags->is_data
10839 || prop_flags->is_unreachable)
10840 return FALSE;
10841 return TRUE;
10842 }
10843
10844
10845 static void
10846 xtensa_frag_flags_init (frag_flags *prop_flags)
10847 {
10848 memset (prop_flags, 0, sizeof (frag_flags));
10849 }
10850
10851
10852 static void
10853 get_frag_property_flags (const fragS *fragP, frag_flags *prop_flags)
10854 {
10855 xtensa_frag_flags_init (prop_flags);
10856 if (fragP->tc_frag_data.is_literal)
10857 prop_flags->is_literal = TRUE;
10858 if (fragP->tc_frag_data.is_unreachable)
10859 prop_flags->is_unreachable = TRUE;
10860 else if (fragP->tc_frag_data.is_insn)
10861 {
10862 prop_flags->is_insn = TRUE;
10863 if (fragP->tc_frag_data.is_loop_target)
10864 prop_flags->insn.is_loop_target = TRUE;
10865 if (fragP->tc_frag_data.is_branch_target)
10866 prop_flags->insn.is_branch_target = TRUE;
10867 if (fragP->tc_frag_data.is_specific_opcode
10868 || fragP->tc_frag_data.is_no_transform)
10869 prop_flags->insn.is_no_transform = TRUE;
10870 if (fragP->tc_frag_data.is_no_density)
10871 prop_flags->insn.is_no_density = TRUE;
10872 if (fragP->tc_frag_data.use_absolute_literals)
10873 prop_flags->insn.is_abslit = TRUE;
10874 }
10875 if (fragP->tc_frag_data.is_align)
10876 {
10877 prop_flags->is_align = TRUE;
10878 prop_flags->alignment = fragP->tc_frag_data.alignment;
10879 if (xtensa_frag_flags_is_empty (prop_flags))
10880 prop_flags->is_data = TRUE;
10881 }
10882 }
10883
10884
10885 static bfd_vma
10886 frag_flags_to_number (const frag_flags *prop_flags)
10887 {
10888 bfd_vma num = 0;
10889 if (prop_flags->is_literal)
10890 num |= XTENSA_PROP_LITERAL;
10891 if (prop_flags->is_insn)
10892 num |= XTENSA_PROP_INSN;
10893 if (prop_flags->is_data)
10894 num |= XTENSA_PROP_DATA;
10895 if (prop_flags->is_unreachable)
10896 num |= XTENSA_PROP_UNREACHABLE;
10897 if (prop_flags->insn.is_loop_target)
10898 num |= XTENSA_PROP_INSN_LOOP_TARGET;
10899 if (prop_flags->insn.is_branch_target)
10900 {
10901 num |= XTENSA_PROP_INSN_BRANCH_TARGET;
10902 num = SET_XTENSA_PROP_BT_ALIGN (num, prop_flags->insn.bt_align_priority);
10903 }
10904
10905 if (prop_flags->insn.is_no_density)
10906 num |= XTENSA_PROP_INSN_NO_DENSITY;
10907 if (prop_flags->insn.is_no_transform)
10908 num |= XTENSA_PROP_INSN_NO_TRANSFORM;
10909 if (prop_flags->insn.is_no_reorder)
10910 num |= XTENSA_PROP_INSN_NO_REORDER;
10911 if (prop_flags->insn.is_abslit)
10912 num |= XTENSA_PROP_INSN_ABSLIT;
10913
10914 if (prop_flags->is_align)
10915 {
10916 num |= XTENSA_PROP_ALIGN;
10917 num = SET_XTENSA_PROP_ALIGNMENT (num, prop_flags->alignment);
10918 }
10919
10920 return num;
10921 }
10922
10923
10924 static bfd_boolean
10925 xtensa_frag_flags_combinable (const frag_flags *prop_flags_1,
10926 const frag_flags *prop_flags_2)
10927 {
10928 /* Cannot combine with an end marker. */
10929
10930 if (prop_flags_1->is_literal != prop_flags_2->is_literal)
10931 return FALSE;
10932 if (prop_flags_1->is_insn != prop_flags_2->is_insn)
10933 return FALSE;
10934 if (prop_flags_1->is_data != prop_flags_2->is_data)
10935 return FALSE;
10936
10937 if (prop_flags_1->is_insn)
10938 {
10939 /* Properties of the beginning of the frag. */
10940 if (prop_flags_2->insn.is_loop_target)
10941 return FALSE;
10942 if (prop_flags_2->insn.is_branch_target)
10943 return FALSE;
10944 if (prop_flags_1->insn.is_no_density !=
10945 prop_flags_2->insn.is_no_density)
10946 return FALSE;
10947 if (prop_flags_1->insn.is_no_transform !=
10948 prop_flags_2->insn.is_no_transform)
10949 return FALSE;
10950 if (prop_flags_1->insn.is_no_reorder !=
10951 prop_flags_2->insn.is_no_reorder)
10952 return FALSE;
10953 if (prop_flags_1->insn.is_abslit !=
10954 prop_flags_2->insn.is_abslit)
10955 return FALSE;
10956 }
10957
10958 if (prop_flags_1->is_align)
10959 return FALSE;
10960
10961 return TRUE;
10962 }
10963
10964
10965 static bfd_vma
10966 xt_block_aligned_size (const xtensa_block_info *xt_block)
10967 {
10968 bfd_vma end_addr;
10969 size_t align_bits;
10970
10971 if (!xt_block->flags.is_align)
10972 return xt_block->size;
10973
10974 end_addr = xt_block->offset + xt_block->size;
10975 align_bits = xt_block->flags.alignment;
10976 end_addr = ((end_addr + ((1 << align_bits) -1)) >> align_bits) << align_bits;
10977 return end_addr - xt_block->offset;
10978 }
10979
10980
10981 static bfd_boolean
10982 xtensa_xt_block_combine (xtensa_block_info *xt_block,
10983 const xtensa_block_info *xt_block_2)
10984 {
10985 if (xt_block->sec != xt_block_2->sec)
10986 return FALSE;
10987 if (xt_block->offset + xt_block_aligned_size (xt_block)
10988 != xt_block_2->offset)
10989 return FALSE;
10990
10991 if (xt_block_2->size == 0
10992 && (!xt_block_2->flags.is_unreachable
10993 || xt_block->flags.is_unreachable))
10994 {
10995 if (xt_block_2->flags.is_align
10996 && xt_block->flags.is_align)
10997 {
10998 /* Nothing needed. */
10999 if (xt_block->flags.alignment >= xt_block_2->flags.alignment)
11000 return TRUE;
11001 }
11002 else
11003 {
11004 if (xt_block_2->flags.is_align)
11005 {
11006 /* Push alignment to previous entry. */
11007 xt_block->flags.is_align = xt_block_2->flags.is_align;
11008 xt_block->flags.alignment = xt_block_2->flags.alignment;
11009 }
11010 return TRUE;
11011 }
11012 }
11013 if (!xtensa_frag_flags_combinable (&xt_block->flags,
11014 &xt_block_2->flags))
11015 return FALSE;
11016
11017 xt_block->size += xt_block_2->size;
11018
11019 if (xt_block_2->flags.is_align)
11020 {
11021 xt_block->flags.is_align = TRUE;
11022 xt_block->flags.alignment = xt_block_2->flags.alignment;
11023 }
11024
11025 return TRUE;
11026 }
11027
11028
11029 static void
11030 add_xt_prop_frags (segT sec,
11031 segT xt_block_sec,
11032 xtensa_block_info **xt_block,
11033 frag_flags_fn property_function)
11034 {
11035 segment_info_type *seg_info;
11036 segment_info_type *xt_seg_info;
11037 bfd_vma seg_offset;
11038 fragS *fragP;
11039
11040 xt_seg_info = retrieve_segment_info (xt_block_sec);
11041 seg_info = retrieve_segment_info (sec);
11042 /* Build it if needed. */
11043 while (*xt_block != NULL)
11044 {
11045 xt_block = &(*xt_block)->next;
11046 }
11047 /* We are either at NULL at the beginning or at the end. */
11048
11049 /* Walk through the frags. */
11050 seg_offset = 0;
11051
11052 if (seg_info->frchainP)
11053 {
11054 for (fragP = seg_info->frchainP->frch_root; fragP;
11055 fragP = fragP->fr_next)
11056 {
11057 xtensa_block_info tmp_block;
11058 tmp_block.sec = sec;
11059 tmp_block.offset = fragP->fr_address;
11060 tmp_block.size = fragP->fr_fix;
11061 tmp_block.next = NULL;
11062 property_function (fragP, &tmp_block.flags);
11063
11064 if (!xtensa_frag_flags_is_empty (&tmp_block.flags))
11065 /* && fragP->fr_fix != 0) */
11066 {
11067 if ((*xt_block) == NULL
11068 || !xtensa_xt_block_combine (*xt_block, &tmp_block))
11069 {
11070 xtensa_block_info *new_block;
11071 if ((*xt_block) != NULL)
11072 xt_block = &(*xt_block)->next;
11073 new_block = (xtensa_block_info *)
11074 xmalloc (sizeof (xtensa_block_info));
11075 *new_block = tmp_block;
11076 *xt_block = new_block;
11077 }
11078 }
11079 }
11080 }
11081 }
11082
11083 \f
11084 /* op_placement_info_table */
11085
11086 /* op_placement_info makes it easier to determine which
11087 ops can go in which slots. */
11088
11089 static void
11090 init_op_placement_info_table (void)
11091 {
11092 xtensa_isa isa = xtensa_default_isa;
11093 xtensa_insnbuf ibuf = xtensa_insnbuf_alloc (isa);
11094 xtensa_opcode opcode;
11095 xtensa_format fmt;
11096 int slot;
11097 int num_opcodes = xtensa_isa_num_opcodes (isa);
11098
11099 op_placement_table = (op_placement_info_table)
11100 xmalloc (sizeof (op_placement_info) * num_opcodes);
11101 assert (xtensa_isa_num_formats (isa) < MAX_FORMATS);
11102
11103 for (opcode = 0; opcode < num_opcodes; opcode++)
11104 {
11105 op_placement_info *opi = &op_placement_table[opcode];
11106 /* FIXME: Make tinsn allocation dynamic. */
11107 if (xtensa_opcode_num_operands (isa, opcode) >= MAX_INSN_ARGS)
11108 as_fatal (_("too many operands in instruction"));
11109 opi->single = XTENSA_UNDEFINED;
11110 opi->single_size = 0;
11111 opi->widest = XTENSA_UNDEFINED;
11112 opi->widest_size = 0;
11113 opi->narrowest = XTENSA_UNDEFINED;
11114 opi->narrowest_size = 0x7F;
11115 opi->formats = 0;
11116 opi->num_formats = 0;
11117 opi->issuef = 0;
11118 for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
11119 {
11120 opi->slots[fmt] = 0;
11121 for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
11122 {
11123 if (xtensa_opcode_encode (isa, fmt, slot, ibuf, opcode) == 0)
11124 {
11125 int fmt_length = xtensa_format_length (isa, fmt);
11126 opi->issuef++;
11127 set_bit (fmt, opi->formats);
11128 set_bit (slot, opi->slots[fmt]);
11129 /* opi->slot_count[fmt]++; */
11130 if (fmt_length < opi->narrowest_size)
11131 {
11132 opi->narrowest = fmt;
11133 opi->narrowest_size = fmt_length;
11134 }
11135 if (fmt_length > opi->widest_size)
11136 {
11137 opi->widest = fmt;
11138 opi->widest_size = fmt_length;
11139 }
11140 if (xtensa_format_num_slots (isa, fmt) == 1)
11141 {
11142 if (opi->single_size == 0
11143 || fmt_length < opi->single_size)
11144 {
11145 opi->single = fmt;
11146 opi->single_size = fmt_length;
11147 }
11148 }
11149 }
11150 }
11151 if (opi->formats)
11152 opi->num_formats++;
11153 }
11154 }
11155 xtensa_insnbuf_free (isa, ibuf);
11156 }
11157
11158
11159 bfd_boolean
11160 opcode_fits_format_slot (xtensa_opcode opcode, xtensa_format fmt, int slot)
11161 {
11162 return bit_is_set (slot, op_placement_table[opcode].slots[fmt]);
11163 }
11164
11165
11166 /* If the opcode is available in a single slot format, return its size. */
11167
11168 static int
11169 xg_get_single_size (xtensa_opcode opcode)
11170 {
11171 assert (op_placement_table[opcode].single != XTENSA_UNDEFINED);
11172 return op_placement_table[opcode].single_size;
11173 }
11174
11175
11176 static xtensa_format
11177 xg_get_single_format (xtensa_opcode opcode)
11178 {
11179 return op_placement_table[opcode].single;
11180 }
11181
11182 \f
11183 /* Instruction Stack Functions (from "xtensa-istack.h"). */
11184
11185 void
11186 istack_init (IStack *stack)
11187 {
11188 memset (stack, 0, sizeof (IStack));
11189 stack->ninsn = 0;
11190 }
11191
11192
11193 bfd_boolean
11194 istack_empty (IStack *stack)
11195 {
11196 return (stack->ninsn == 0);
11197 }
11198
11199
11200 bfd_boolean
11201 istack_full (IStack *stack)
11202 {
11203 return (stack->ninsn == MAX_ISTACK);
11204 }
11205
11206
11207 /* Return a pointer to the top IStack entry.
11208 It is an error to call this if istack_empty () is TRUE. */
11209
11210 TInsn *
11211 istack_top (IStack *stack)
11212 {
11213 int rec = stack->ninsn - 1;
11214 assert (!istack_empty (stack));
11215 return &stack->insn[rec];
11216 }
11217
11218
11219 /* Add a new TInsn to an IStack.
11220 It is an error to call this if istack_full () is TRUE. */
11221
11222 void
11223 istack_push (IStack *stack, TInsn *insn)
11224 {
11225 int rec = stack->ninsn;
11226 assert (!istack_full (stack));
11227 stack->insn[rec] = *insn;
11228 stack->ninsn++;
11229 }
11230
11231
11232 /* Clear space for the next TInsn on the IStack and return a pointer
11233 to it. It is an error to call this if istack_full () is TRUE. */
11234
11235 TInsn *
11236 istack_push_space (IStack *stack)
11237 {
11238 int rec = stack->ninsn;
11239 TInsn *insn;
11240 assert (!istack_full (stack));
11241 insn = &stack->insn[rec];
11242 memset (insn, 0, sizeof (TInsn));
11243 stack->ninsn++;
11244 return insn;
11245 }
11246
11247
11248 /* Remove the last pushed instruction. It is an error to call this if
11249 istack_empty () returns TRUE. */
11250
11251 void
11252 istack_pop (IStack *stack)
11253 {
11254 int rec = stack->ninsn - 1;
11255 assert (!istack_empty (stack));
11256 stack->ninsn--;
11257 memset (&stack->insn[rec], 0, sizeof (TInsn));
11258 }
11259
11260 \f
11261 /* TInsn functions. */
11262
11263 void
11264 tinsn_init (TInsn *dst)
11265 {
11266 memset (dst, 0, sizeof (TInsn));
11267 }
11268
11269
11270 /* Get the ``num''th token of the TInsn.
11271 It is illegal to call this if num > insn->ntoks. */
11272
11273 expressionS *
11274 tinsn_get_tok (TInsn *insn, int num)
11275 {
11276 assert (num < insn->ntok);
11277 return &insn->tok[num];
11278 }
11279
11280
11281 /* Return TRUE if ANY of the operands in the insn are symbolic. */
11282
11283 static bfd_boolean
11284 tinsn_has_symbolic_operands (const TInsn *insn)
11285 {
11286 int i;
11287 int n = insn->ntok;
11288
11289 assert (insn->insn_type == ITYPE_INSN);
11290
11291 for (i = 0; i < n; ++i)
11292 {
11293 switch (insn->tok[i].X_op)
11294 {
11295 case O_register:
11296 case O_constant:
11297 break;
11298 default:
11299 return TRUE;
11300 }
11301 }
11302 return FALSE;
11303 }
11304
11305
11306 bfd_boolean
11307 tinsn_has_invalid_symbolic_operands (const TInsn *insn)
11308 {
11309 xtensa_isa isa = xtensa_default_isa;
11310 int i;
11311 int n = insn->ntok;
11312
11313 assert (insn->insn_type == ITYPE_INSN);
11314
11315 for (i = 0; i < n; ++i)
11316 {
11317 switch (insn->tok[i].X_op)
11318 {
11319 case O_register:
11320 case O_constant:
11321 break;
11322 case O_big:
11323 case O_illegal:
11324 case O_absent:
11325 /* Errors for these types are caught later. */
11326 break;
11327 case O_hi16:
11328 case O_lo16:
11329 default:
11330 /* Symbolic immediates are only allowed on the last immediate
11331 operand. At this time, CONST16 is the only opcode where we
11332 support non-PC-relative relocations. (It isn't necessary
11333 to complain about non-PC-relative relocations here, but
11334 otherwise, no error is reported until the relocations are
11335 generated, and the assembler won't get that far if there
11336 are any other errors. It's nice to see all the problems
11337 at once.) */
11338 if (i != get_relaxable_immed (insn->opcode)
11339 || (xtensa_operand_is_PCrelative (isa, insn->opcode, i) != 1
11340 && insn->opcode != xtensa_const16_opcode))
11341 {
11342 as_bad (_("invalid symbolic operand %d on '%s'"),
11343 i, xtensa_opcode_name (isa, insn->opcode));
11344 return TRUE;
11345 }
11346 }
11347 }
11348 return FALSE;
11349 }
11350
11351
11352 /* For assembly code with complex expressions (e.g. subtraction),
11353 we have to build them in the literal pool so that
11354 their results are calculated correctly after relaxation.
11355 The relaxation only handles expressions that
11356 boil down to SYMBOL + OFFSET. */
11357
11358 static bfd_boolean
11359 tinsn_has_complex_operands (const TInsn *insn)
11360 {
11361 int i;
11362 int n = insn->ntok;
11363 assert (insn->insn_type == ITYPE_INSN);
11364 for (i = 0; i < n; ++i)
11365 {
11366 switch (insn->tok[i].X_op)
11367 {
11368 case O_register:
11369 case O_constant:
11370 case O_symbol:
11371 case O_lo16:
11372 case O_hi16:
11373 break;
11374 default:
11375 return TRUE;
11376 }
11377 }
11378 return FALSE;
11379 }
11380
11381
11382 /* Convert the constant operands in the tinsn to insnbuf.
11383 Return TRUE if there is a symbol in the immediate field.
11384
11385 Before this is called,
11386 1) the number of operands are correct
11387 2) the tinsn is a ITYPE_INSN
11388 3) ONLY the relaxable_ is built
11389 4) All operands are O_constant, O_symbol. All constants fit
11390 The return value tells whether there are any remaining O_symbols. */
11391
11392 static bfd_boolean
11393 tinsn_to_insnbuf (TInsn *tinsn, xtensa_insnbuf insnbuf)
11394 {
11395 static xtensa_insnbuf slotbuf = 0;
11396 xtensa_isa isa = xtensa_default_isa;
11397 xtensa_opcode opcode = tinsn->opcode;
11398 xtensa_format fmt = xg_get_single_format (opcode);
11399 bfd_boolean has_fixup = FALSE;
11400 int noperands = xtensa_opcode_num_operands (isa, opcode);
11401 int i;
11402 uint32 opnd_value;
11403 char *file_name;
11404 int line;
11405
11406 if (!slotbuf)
11407 slotbuf = xtensa_insnbuf_alloc (isa);
11408
11409 assert (tinsn->insn_type == ITYPE_INSN);
11410 if (noperands != tinsn->ntok)
11411 as_fatal (_("operand number mismatch"));
11412
11413 if (xtensa_opcode_encode (isa, fmt, 0, slotbuf, opcode))
11414 as_fatal (_("cannot encode opcode"));
11415
11416 for (i = 0; i < noperands; ++i)
11417 {
11418 expressionS *expr = &tinsn->tok[i];
11419 switch (expr->X_op)
11420 {
11421 case O_register:
11422 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11423 break;
11424 /* The register number has already been checked in
11425 expression_maybe_register, so we don't need to check here. */
11426 opnd_value = expr->X_add_number;
11427 (void) xtensa_operand_encode (isa, opcode, i, &opnd_value);
11428 xtensa_operand_set_field (isa, opcode, i, fmt, 0,
11429 slotbuf, opnd_value);
11430 break;
11431
11432 case O_constant:
11433 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11434 break;
11435 as_where (&file_name, &line);
11436 /* It is a constant and we called this function,
11437 then we have to try to fit it. */
11438 xtensa_insnbuf_set_operand (slotbuf, fmt, 0, opcode, i,
11439 expr->X_add_number, file_name, line);
11440 break;
11441
11442 default:
11443 has_fixup = TRUE;
11444 break;
11445 }
11446 }
11447
11448 xtensa_format_encode (isa, fmt, insnbuf);
11449 xtensa_format_set_slot (isa, fmt, 0, insnbuf, slotbuf);
11450
11451 return has_fixup;
11452 }
11453
11454
11455 /* Convert the constant operands in the tinsn to slotbuf.
11456 Return TRUE if there is a symbol in the immediate field.
11457 (Eventually this should replace tinsn_to_insnbuf.) */
11458
11459 /* Before this is called,
11460 1) the number of operands are correct
11461 2) the tinsn is a ITYPE_INSN
11462 3) ONLY the relaxable_ is built
11463 4) All operands are
11464 O_constant, O_symbol
11465 All constants fit
11466
11467 The return value tells whether there are any remaining O_symbols. */
11468
11469 static bfd_boolean
11470 tinsn_to_slotbuf (xtensa_format fmt,
11471 int slot,
11472 TInsn *tinsn,
11473 xtensa_insnbuf slotbuf)
11474 {
11475 xtensa_isa isa = xtensa_default_isa;
11476 xtensa_opcode opcode = tinsn->opcode;
11477 bfd_boolean has_fixup = FALSE;
11478 int noperands = xtensa_opcode_num_operands (isa, opcode);
11479 int i;
11480
11481 *((int *) &slotbuf[0]) = 0;
11482 *((int *) &slotbuf[1]) = 0;
11483 assert (tinsn->insn_type == ITYPE_INSN);
11484 if (noperands != tinsn->ntok)
11485 as_fatal (_("operand number mismatch"));
11486
11487 if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opcode))
11488 {
11489 as_bad (_("cannot encode opcode \"%s\" in the given format \"%s\""),
11490 xtensa_opcode_name (isa, opcode), xtensa_format_name (isa, fmt));
11491 return FALSE;
11492 }
11493
11494 for (i = 0; i < noperands; i++)
11495 {
11496 expressionS *expr = &tinsn->tok[i];
11497 int rc, line;
11498 char *file_name;
11499 uint32 opnd_value;
11500
11501 switch (expr->X_op)
11502 {
11503 case O_register:
11504 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11505 break;
11506 /* The register number has already been checked in
11507 expression_maybe_register, so we don't need to check here. */
11508 opnd_value = expr->X_add_number;
11509 (void) xtensa_operand_encode (isa, opcode, i, &opnd_value);
11510 rc = xtensa_operand_set_field (isa, opcode, i, fmt, slot, slotbuf,
11511 opnd_value);
11512 if (rc != 0)
11513 as_warn (_("xtensa-isa failure: %s"), xtensa_isa_error_msg (isa));
11514 break;
11515
11516 case O_constant:
11517 if (xtensa_operand_is_visible (isa, opcode, i) == 0)
11518 break;
11519 as_where (&file_name, &line);
11520 /* It is a constant and we called this function
11521 then we have to try to fit it. */
11522 xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode, i,
11523 expr->X_add_number, file_name, line);
11524 break;
11525
11526 default:
11527 has_fixup = TRUE;
11528 break;
11529 }
11530 }
11531
11532 return has_fixup;
11533 }
11534
11535
11536 /* Check the instruction arguments. Return TRUE on failure. */
11537
11538 static bfd_boolean
11539 tinsn_check_arguments (const TInsn *insn)
11540 {
11541 xtensa_isa isa = xtensa_default_isa;
11542 xtensa_opcode opcode = insn->opcode;
11543
11544 if (opcode == XTENSA_UNDEFINED)
11545 {
11546 as_bad (_("invalid opcode"));
11547 return TRUE;
11548 }
11549
11550 if (xtensa_opcode_num_operands (isa, opcode) > insn->ntok)
11551 {
11552 as_bad (_("too few operands"));
11553 return TRUE;
11554 }
11555
11556 if (xtensa_opcode_num_operands (isa, opcode) < insn->ntok)
11557 {
11558 as_bad (_("too many operands"));
11559 return TRUE;
11560 }
11561 return FALSE;
11562 }
11563
11564
11565 /* Load an instruction from its encoded form. */
11566
11567 static void
11568 tinsn_from_chars (TInsn *tinsn, char *f, int slot)
11569 {
11570 vliw_insn vinsn;
11571
11572 xg_init_vinsn (&vinsn);
11573 vinsn_from_chars (&vinsn, f);
11574
11575 *tinsn = vinsn.slots[slot];
11576 xg_free_vinsn (&vinsn);
11577 }
11578
11579
11580 static void
11581 tinsn_from_insnbuf (TInsn *tinsn,
11582 xtensa_insnbuf slotbuf,
11583 xtensa_format fmt,
11584 int slot)
11585 {
11586 int i;
11587 xtensa_isa isa = xtensa_default_isa;
11588
11589 /* Find the immed. */
11590 tinsn_init (tinsn);
11591 tinsn->insn_type = ITYPE_INSN;
11592 tinsn->is_specific_opcode = FALSE; /* must not be specific */
11593 tinsn->opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
11594 tinsn->ntok = xtensa_opcode_num_operands (isa, tinsn->opcode);
11595 for (i = 0; i < tinsn->ntok; i++)
11596 {
11597 set_expr_const (&tinsn->tok[i],
11598 xtensa_insnbuf_get_operand (slotbuf, fmt, slot,
11599 tinsn->opcode, i));
11600 }
11601 }
11602
11603
11604 /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */
11605
11606 static void
11607 tinsn_immed_from_frag (TInsn *tinsn, fragS *fragP, int slot)
11608 {
11609 xtensa_opcode opcode = tinsn->opcode;
11610 int opnum;
11611
11612 if (fragP->tc_frag_data.slot_symbols[slot])
11613 {
11614 opnum = get_relaxable_immed (opcode);
11615 assert (opnum >= 0);
11616 if (fragP->tc_frag_data.slot_sub_symbols[slot])
11617 {
11618 set_expr_symbol_offset_diff
11619 (&tinsn->tok[opnum],
11620 fragP->tc_frag_data.slot_symbols[slot],
11621 fragP->tc_frag_data.slot_sub_symbols[slot],
11622 fragP->tc_frag_data.slot_offsets[slot]);
11623 }
11624 else
11625 {
11626 set_expr_symbol_offset
11627 (&tinsn->tok[opnum],
11628 fragP->tc_frag_data.slot_symbols[slot],
11629 fragP->tc_frag_data.slot_offsets[slot]);
11630 }
11631 }
11632 }
11633
11634
11635 static int
11636 get_num_stack_text_bytes (IStack *istack)
11637 {
11638 int i;
11639 int text_bytes = 0;
11640
11641 for (i = 0; i < istack->ninsn; i++)
11642 {
11643 TInsn *tinsn = &istack->insn[i];
11644 if (tinsn->insn_type == ITYPE_INSN)
11645 text_bytes += xg_get_single_size (tinsn->opcode);
11646 }
11647 return text_bytes;
11648 }
11649
11650
11651 static int
11652 get_num_stack_literal_bytes (IStack *istack)
11653 {
11654 int i;
11655 int lit_bytes = 0;
11656
11657 for (i = 0; i < istack->ninsn; i++)
11658 {
11659 TInsn *tinsn = &istack->insn[i];
11660 if (tinsn->insn_type == ITYPE_LITERAL && tinsn->ntok == 1)
11661 lit_bytes += 4;
11662 }
11663 return lit_bytes;
11664 }
11665
11666 \f
11667 /* vliw_insn functions. */
11668
11669 static void
11670 xg_init_vinsn (vliw_insn *v)
11671 {
11672 int i;
11673 xtensa_isa isa = xtensa_default_isa;
11674
11675 xg_clear_vinsn (v);
11676
11677 v->insnbuf = xtensa_insnbuf_alloc (isa);
11678 if (v->insnbuf == NULL)
11679 as_fatal (_("out of memory"));
11680
11681 for (i = 0; i < MAX_SLOTS; i++)
11682 {
11683 tinsn_init (&v->slots[i]);
11684 v->slots[i].opcode = XTENSA_UNDEFINED;
11685 v->slotbuf[i] = xtensa_insnbuf_alloc (isa);
11686 if (v->slotbuf[i] == NULL)
11687 as_fatal (_("out of memory"));
11688 }
11689 }
11690
11691
11692 static void
11693 xg_clear_vinsn (vliw_insn *v)
11694 {
11695 int i;
11696 v->format = XTENSA_UNDEFINED;
11697 v->num_slots = 0;
11698 v->inside_bundle = FALSE;
11699
11700 if (xt_saved_debug_type != DEBUG_NONE)
11701 debug_type = xt_saved_debug_type;
11702
11703 for (i = 0; i < MAX_SLOTS; i++)
11704 {
11705 memset (&v->slots[i], 0, sizeof (TInsn));
11706 v->slots[i].opcode = XTENSA_UNDEFINED;
11707 }
11708 }
11709
11710
11711 static bfd_boolean
11712 vinsn_has_specific_opcodes (vliw_insn *v)
11713 {
11714 int i;
11715
11716 for (i = 0; i < v->num_slots; i++)
11717 {
11718 if (v->slots[i].is_specific_opcode)
11719 return TRUE;
11720 }
11721 return FALSE;
11722 }
11723
11724
11725 static void
11726 xg_free_vinsn (vliw_insn *v)
11727 {
11728 int i;
11729 xtensa_insnbuf_free (xtensa_default_isa, v->insnbuf);
11730 for (i = 0; i < MAX_SLOTS; i++)
11731 xtensa_insnbuf_free (xtensa_default_isa, v->slotbuf[i]);
11732 }
11733
11734
11735 /* Before this is called, we should have
11736 filled out the following fields:
11737
11738 1) the number of operands for each opcode are correct
11739 2) the tinsn in the slots are ITYPE_INSN
11740 3) ONLY the relaxable_ is built
11741 4) All operands are
11742 O_constant, O_symbol
11743 All constants fit
11744
11745 The return value tells whether there are any remaining O_symbols. */
11746
11747 static bfd_boolean
11748 vinsn_to_insnbuf (vliw_insn *vinsn,
11749 char *frag_offset,
11750 fragS *fragP,
11751 bfd_boolean record_fixup)
11752 {
11753 xtensa_isa isa = xtensa_default_isa;
11754 xtensa_format fmt = vinsn->format;
11755 xtensa_insnbuf insnbuf = vinsn->insnbuf;
11756 int slot;
11757 bfd_boolean has_fixup = FALSE;
11758
11759 xtensa_format_encode (isa, fmt, insnbuf);
11760
11761 for (slot = 0; slot < vinsn->num_slots; slot++)
11762 {
11763 TInsn *tinsn = &vinsn->slots[slot];
11764 bfd_boolean tinsn_has_fixup =
11765 tinsn_to_slotbuf (vinsn->format, slot, tinsn,
11766 vinsn->slotbuf[slot]);
11767
11768 xtensa_format_set_slot (isa, fmt, slot,
11769 insnbuf, vinsn->slotbuf[slot]);
11770 /* tinsn_has_fixup tracks if there is a fixup at all.
11771 record_fixup controls globally. I.E., we use this
11772 function from several places, some of which are after
11773 fixups have already been recorded. Finally,
11774 tinsn->record_fixup controls based on the individual ops,
11775 which may or may not need it based on the relaxation
11776 requirements. */
11777 if (tinsn_has_fixup && record_fixup)
11778 {
11779 int i;
11780 xtensa_opcode opcode = tinsn->opcode;
11781 int noperands = xtensa_opcode_num_operands (isa, opcode);
11782 has_fixup = TRUE;
11783
11784 for (i = 0; i < noperands; i++)
11785 {
11786 expressionS* expr = &tinsn->tok[i];
11787 switch (expr->X_op)
11788 {
11789 case O_symbol:
11790 case O_lo16:
11791 case O_hi16:
11792 if (get_relaxable_immed (opcode) == i)
11793 {
11794 if (tinsn->record_fix || expr->X_op != O_symbol)
11795 {
11796 if (!xg_add_opcode_fix
11797 (tinsn, i, fmt, slot, expr, fragP,
11798 frag_offset - fragP->fr_literal))
11799 as_bad (_("instruction with constant operands does not fit"));
11800 }
11801 else
11802 {
11803 tinsn->symbol = expr->X_add_symbol;
11804 tinsn->offset = expr->X_add_number;
11805 }
11806 }
11807 else
11808 as_bad (_("invalid operand %d on '%s'"),
11809 i, xtensa_opcode_name (isa, opcode));
11810 break;
11811
11812 case O_constant:
11813 case O_register:
11814 break;
11815
11816 case O_subtract:
11817 if (get_relaxable_immed (opcode) == i)
11818 {
11819 if (tinsn->record_fix)
11820 as_bad (_("invalid subtract operand"));
11821 else
11822 {
11823 tinsn->symbol = expr->X_add_symbol;
11824 tinsn->sub_symbol = expr->X_op_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 default:
11834 as_bad (_("invalid expression for operand %d on '%s'"),
11835 i, xtensa_opcode_name (isa, opcode));
11836 break;
11837 }
11838 }
11839 }
11840 }
11841
11842 return has_fixup;
11843 }
11844
11845
11846 static void
11847 vinsn_from_chars (vliw_insn *vinsn, char *f)
11848 {
11849 static xtensa_insnbuf insnbuf = NULL;
11850 static xtensa_insnbuf slotbuf = NULL;
11851 int i;
11852 xtensa_format fmt;
11853 xtensa_isa isa = xtensa_default_isa;
11854
11855 if (!insnbuf)
11856 {
11857 insnbuf = xtensa_insnbuf_alloc (isa);
11858 slotbuf = xtensa_insnbuf_alloc (isa);
11859 }
11860
11861 xtensa_insnbuf_from_chars (isa, insnbuf, f, 0);
11862 fmt = xtensa_format_decode (isa, insnbuf);
11863 if (fmt == XTENSA_UNDEFINED)
11864 as_fatal (_("cannot decode instruction format"));
11865 vinsn->format = fmt;
11866 vinsn->num_slots = xtensa_format_num_slots (isa, fmt);
11867
11868 for (i = 0; i < vinsn->num_slots; i++)
11869 {
11870 TInsn *tinsn = &vinsn->slots[i];
11871 xtensa_format_get_slot (isa, fmt, i, insnbuf, slotbuf);
11872 tinsn_from_insnbuf (tinsn, slotbuf, fmt, i);
11873 }
11874 }
11875
11876 \f
11877 /* Expression utilities. */
11878
11879 /* Return TRUE if the expression is an integer constant. */
11880
11881 bfd_boolean
11882 expr_is_const (const expressionS *s)
11883 {
11884 return (s->X_op == O_constant);
11885 }
11886
11887
11888 /* Get the expression constant.
11889 Calling this is illegal if expr_is_const () returns TRUE. */
11890
11891 offsetT
11892 get_expr_const (const expressionS *s)
11893 {
11894 assert (expr_is_const (s));
11895 return s->X_add_number;
11896 }
11897
11898
11899 /* Set the expression to a constant value. */
11900
11901 void
11902 set_expr_const (expressionS *s, offsetT val)
11903 {
11904 s->X_op = O_constant;
11905 s->X_add_number = val;
11906 s->X_add_symbol = NULL;
11907 s->X_op_symbol = NULL;
11908 }
11909
11910
11911 bfd_boolean
11912 expr_is_register (const expressionS *s)
11913 {
11914 return (s->X_op == O_register);
11915 }
11916
11917
11918 /* Get the expression constant.
11919 Calling this is illegal if expr_is_const () returns TRUE. */
11920
11921 offsetT
11922 get_expr_register (const expressionS *s)
11923 {
11924 assert (expr_is_register (s));
11925 return s->X_add_number;
11926 }
11927
11928
11929 /* Set the expression to a symbol + constant offset. */
11930
11931 void
11932 set_expr_symbol_offset (expressionS *s, symbolS *sym, offsetT offset)
11933 {
11934 s->X_op = O_symbol;
11935 s->X_add_symbol = sym;
11936 s->X_op_symbol = NULL; /* unused */
11937 s->X_add_number = offset;
11938 }
11939
11940
11941 /* Set the expression to symbol - minus_sym + offset. */
11942
11943 static void
11944 set_expr_symbol_offset_diff (expressionS *s,
11945 symbolS *sym,
11946 symbolS *minus_sym,
11947 offsetT offset)
11948 {
11949 s->X_op = O_subtract;
11950 s->X_add_symbol = sym;
11951 s->X_op_symbol = minus_sym; /* unused */
11952 s->X_add_number = offset;
11953 }
11954
11955
11956 /* Return TRUE if the two expressions are equal. */
11957
11958 bfd_boolean
11959 expr_is_equal (expressionS *s1, expressionS *s2)
11960 {
11961 if (s1->X_op != s2->X_op)
11962 return FALSE;
11963 if (s1->X_add_symbol != s2->X_add_symbol)
11964 return FALSE;
11965 if (s1->X_op_symbol != s2->X_op_symbol)
11966 return FALSE;
11967 if (s1->X_add_number != s2->X_add_number)
11968 return FALSE;
11969 return TRUE;
11970 }
11971
11972
11973 static void
11974 copy_expr (expressionS *dst, const expressionS *src)
11975 {
11976 memcpy (dst, src, sizeof (expressionS));
11977 }
11978
11979 \f
11980 /* Support for the "--rename-section" option. */
11981
11982 struct rename_section_struct
11983 {
11984 char *old_name;
11985 char *new_name;
11986 struct rename_section_struct *next;
11987 };
11988
11989 static struct rename_section_struct *section_rename;
11990
11991
11992 /* Parse the string "oldname=new_name(:oldname2=new_name2)*" and add
11993 entries to the section_rename list. Note: Specifying multiple
11994 renamings separated by colons is not documented and is retained only
11995 for backward compatibility. */
11996
11997 static void
11998 build_section_rename (const char *arg)
11999 {
12000 struct rename_section_struct *r;
12001 char *this_arg = NULL;
12002 char *next_arg = NULL;
12003
12004 for (this_arg = xstrdup (arg); this_arg != NULL; this_arg = next_arg)
12005 {
12006 char *old_name, *new_name;
12007
12008 if (this_arg)
12009 {
12010 next_arg = strchr (this_arg, ':');
12011 if (next_arg)
12012 {
12013 *next_arg = '\0';
12014 next_arg++;
12015 }
12016 }
12017
12018 old_name = this_arg;
12019 new_name = strchr (this_arg, '=');
12020
12021 if (*old_name == '\0')
12022 {
12023 as_warn (_("ignoring extra '-rename-section' delimiter ':'"));
12024 continue;
12025 }
12026 if (!new_name || new_name[1] == '\0')
12027 {
12028 as_warn (_("ignoring invalid '-rename-section' specification: '%s'"),
12029 old_name);
12030 continue;
12031 }
12032 *new_name = '\0';
12033 new_name++;
12034
12035 /* Check for invalid section renaming. */
12036 for (r = section_rename; r != NULL; r = r->next)
12037 {
12038 if (strcmp (r->old_name, old_name) == 0)
12039 as_bad (_("section %s renamed multiple times"), old_name);
12040 if (strcmp (r->new_name, new_name) == 0)
12041 as_bad (_("multiple sections remapped to output section %s"),
12042 new_name);
12043 }
12044
12045 /* Now add it. */
12046 r = (struct rename_section_struct *)
12047 xmalloc (sizeof (struct rename_section_struct));
12048 r->old_name = xstrdup (old_name);
12049 r->new_name = xstrdup (new_name);
12050 r->next = section_rename;
12051 section_rename = r;
12052 }
12053 }
12054
12055
12056 char *
12057 xtensa_section_rename (char *name)
12058 {
12059 struct rename_section_struct *r = section_rename;
12060
12061 for (r = section_rename; r != NULL; r = r->next)
12062 {
12063 if (strcmp (r->old_name, name) == 0)
12064 return r->new_name;
12065 }
12066
12067 return name;
12068 }