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