* config/tc-xtensa.c (xtensa_mark_literal_pool_location): Remove
[deliverable/binutils-gdb.git] / gas / config / tc-xtensa.c
1 /* tc-xtensa.c -- Assemble Xtensa instructions.
2 Copyright 2003 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 MA 02111-1307, USA. */
20
21 #include <string.h>
22 #include "as.h"
23 #include "sb.h"
24 #include "safe-ctype.h"
25 #include "tc-xtensa.h"
26 #include "frags.h"
27 #include "subsegs.h"
28 #include "xtensa-relax.h"
29 #include "xtensa-istack.h"
30 #include "struc-symbol.h"
31 #include "xtensa-config.h"
32
33 #ifndef uint32
34 #define uint32 unsigned int
35 #endif
36 #ifndef int32
37 #define int32 signed int
38 #endif
39
40 /* Notes:
41
42 There are 3 forms for instructions,
43 1) the MEMORY format -- this is the encoding 2 or 3 byte instruction
44 2) the TInsn -- handles instructions/labels and literals;
45 all operands are assumed to be expressions
46 3) the IStack -- a stack of TInsn. this allows us to
47 reason about the generated expansion instructions
48
49 Naming conventions (used somewhat inconsistently):
50 The xtensa_ functions are exported
51 The xg_ functions are internal
52
53 We also have a couple of different extensibility mechanisms.
54 1) The idiom replacement:
55 This is used when a line is first parsed to
56 replace an instruction pattern with another instruction
57 It is currently limited to replacements of instructions
58 with constant operands.
59 2) The xtensa-relax.c mechanism that has stronger instruction
60 replacement patterns. When an instruction's immediate field
61 does not fit the next instruction sequence is attempted.
62 In addition, "narrow" opcodes are supported this way. */
63
64
65 /* Define characters with special meanings to GAS. */
66 const char comment_chars[] = "#";
67 const char line_comment_chars[] = "#";
68 const char line_separator_chars[] = ";";
69 const char EXP_CHARS[] = "eE";
70 const char FLT_CHARS[] = "rRsSfFdDxXpP";
71
72
73 /* Flag to indicate whether the hardware supports the density option.
74 If not, enabling density instructions (via directives or --density flag)
75 is illegal. */
76
77 #if STATIC_LIBISA
78 bfd_boolean density_supported = XCHAL_HAVE_DENSITY;
79 #else
80 bfd_boolean density_supported = TRUE;
81 #endif
82
83 #define XTENSA_FETCH_WIDTH 4
84
85 /* Flags for properties of the last instruction in a segment. */
86 #define FLAG_IS_A0_WRITER 0x1
87 #define FLAG_IS_BAD_LOOPEND 0x2
88
89
90 /* We define a special segment names ".literal" to place literals
91 into. The .fini and .init sections are special because they
92 contain code that is moved together by the linker. We give them
93 their own special .fini.literal and .init.literal sections. */
94
95 #define LITERAL_SECTION_NAME xtensa_section_rename (".literal")
96 #define FINI_SECTION_NAME xtensa_section_rename (".fini")
97 #define INIT_SECTION_NAME xtensa_section_rename (".init")
98 #define FINI_LITERAL_SECTION_NAME xtensa_section_rename (".fini.literal")
99 #define INIT_LITERAL_SECTION_NAME xtensa_section_rename (".init.literal")
100
101
102 /* This type is used for the directive_stack to keep track of the
103 state of the literal collection pools. */
104
105 typedef struct lit_state_struct
106 {
107 const char *lit_seg_name;
108 const char *init_lit_seg_name;
109 const char *fini_lit_seg_name;
110 segT lit_seg;
111 segT init_lit_seg;
112 segT fini_lit_seg;
113 } lit_state;
114
115 static lit_state default_lit_sections;
116
117
118 /* We keep lists of literal segments. The seg_list type is the node
119 for such a list. The *_literal_head locals are the heads of the
120 various lists. All of these lists have a dummy node at the start. */
121
122 typedef struct seg_list_struct
123 {
124 struct seg_list_struct *next;
125 segT seg;
126 } seg_list;
127
128 static seg_list literal_head_h;
129 static seg_list *literal_head = &literal_head_h;
130 static seg_list init_literal_head_h;
131 static seg_list *init_literal_head = &init_literal_head_h;
132 static seg_list fini_literal_head_h;
133 static seg_list *fini_literal_head = &fini_literal_head_h;
134
135
136 /* Global flag to indicate when we are emitting literals. */
137 int generating_literals = 0;
138
139
140 /* Structure for saving the current state before emitting literals. */
141 typedef struct emit_state_struct
142 {
143 const char *name;
144 segT now_seg;
145 subsegT now_subseg;
146 int generating_literals;
147 } emit_state;
148
149
150 /* Directives. */
151
152 typedef enum
153 {
154 directive_none = 0,
155 directive_literal,
156 directive_density,
157 directive_generics,
158 directive_relax,
159 directive_freeregs,
160 directive_longcalls,
161 directive_literal_prefix
162 } directiveE;
163
164 typedef struct
165 {
166 const char *name;
167 bfd_boolean can_be_negated;
168 } directive_infoS;
169
170 const directive_infoS directive_info[] =
171 {
172 {"none", FALSE},
173 {"literal", FALSE},
174 {"density", TRUE},
175 {"generics", TRUE},
176 {"relax", TRUE},
177 {"freeregs", FALSE},
178 {"longcalls", TRUE},
179 {"literal_prefix", FALSE}
180 };
181
182 bfd_boolean directive_state[] =
183 {
184 FALSE, /* none */
185 FALSE, /* literal */
186 #if STATIC_LIBISA && !XCHAL_HAVE_DENSITY
187 FALSE, /* density */
188 #else
189 TRUE, /* density */
190 #endif
191 TRUE, /* generics */
192 TRUE, /* relax */
193 FALSE, /* freeregs */
194 FALSE, /* longcalls */
195 FALSE /* literal_prefix */
196 };
197
198
199 enum xtensa_relax_statesE
200 {
201 RELAX_ALIGN_NEXT_OPCODE,
202 /* Use the first opcode of the next fragment to determine the
203 alignment requirements. This is ONLY used for LOOPS
204 currently. */
205
206 RELAX_DESIRE_ALIGN_IF_TARGET,
207 /* These are placed in front of labels. They will all be converted
208 to RELAX_DESIRE_ALIGN / RELAX_LOOP_END or rs_fill of 0 before
209 relaxation begins. */
210
211 RELAX_ADD_NOP_IF_A0_B_RETW,
212 /* These are placed in front of conditional branches. It will be
213 turned into a NOP (using a1) if the branch is immediately
214 followed by a RETW or RETW.N. Otherwise it will be turned into
215 an rs_fill of 0 before relaxation begins. */
216
217 RELAX_ADD_NOP_IF_PRE_LOOP_END,
218 /* These are placed after JX instructions. It will be turned into a
219 NOP if there is one instruction before a loop end label.
220 Otherwise it will be turned into an rs_fill of 0 before
221 relaxation begins. This is used to avoid a hardware TIE
222 interlock issue prior to T1040. */
223
224 RELAX_ADD_NOP_IF_SHORT_LOOP,
225 /* These are placed after LOOP instructions. It will be turned into
226 a NOP when: (1) there are less than 3 instructions in the loop;
227 we place 2 of these in a row to add up to 2 NOPS in short loops;
228 or (2) The instructions in the loop do not include a branch or
229 jump. Otherwise it will be turned into an rs_fill of 0 before
230 relaxation begins. This is used to avoid hardware bug
231 PR3830. */
232
233 RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
234 /* These are placed after LOOP instructions. It will be turned into
235 a NOP if there are less than 12 bytes to the end of some other
236 loop's end. Otherwise it will be turned into an rs_fill of 0
237 before relaxation begins. This is used to avoid hardware bug
238 PR3830. */
239
240 RELAX_DESIRE_ALIGN,
241 /* The next fragment like its first instruction to NOT cross a
242 4-byte boundary. */
243
244 RELAX_LOOP_END,
245 /* This will be turned into a NOP or NOP.N if the previous
246 instruction is expanded to negate a loop. */
247
248 RELAX_LOOP_END_ADD_NOP,
249 /* When the code density option is available, this will generate a
250 NOP.N marked RELAX_NARROW. Otherwise, it will create an rs_fill
251 fragment with a NOP in it. */
252
253 RELAX_LITERAL,
254 /* Another fragment could generate an expansion here but has not yet. */
255
256 RELAX_LITERAL_NR,
257 /* Expansion has been generated by an instruction that generates a
258 literal. However, the stretch has NOT been reported yet in this
259 fragment. */
260
261 RELAX_LITERAL_FINAL,
262 /* Expansion has been generated by an instruction that generates a
263 literal. */
264
265 RELAX_LITERAL_POOL_BEGIN,
266 RELAX_LITERAL_POOL_END,
267 /* Technically these are not relaxations at all, but mark a location
268 to store literals later. Note that fr_var stores the frchain for
269 BEGIN frags and fr_var stores now_seg for END frags. */
270
271 RELAX_NARROW,
272 /* The last instruction in this fragment (at->fr_opcode) can be
273 freely replaced with a single wider instruction if a future
274 alignment desires or needs it. */
275
276 RELAX_IMMED,
277 /* The last instruction in this fragment (at->fr_opcode) contains
278 the value defined by fr_symbol (fr_offset = 0). If the value
279 does not fit, use the specified expansion. This is similar to
280 "NARROW", except that these may not be expanded in order to align
281 code. */
282
283 RELAX_IMMED_STEP1,
284 /* The last instruction in this fragment (at->fr_opcode) contains a
285 literal. It has already been expanded at least 1 step. */
286
287 RELAX_IMMED_STEP2
288 /* The last instruction in this fragment (at->fr_opcode) contains a
289 literal. It has already been expanded at least 2 steps. */
290 };
291
292 /* This is used as a stopper to bound the number of steps that
293 can be taken. */
294 #define RELAX_IMMED_MAXSTEPS (RELAX_IMMED_STEP2 - RELAX_IMMED)
295
296
297 typedef bfd_boolean (*frag_predicate) (const fragS *);
298
299
300 /* Directive functions. */
301
302 static bfd_boolean use_generics
303 PARAMS ((void));
304 static bfd_boolean use_longcalls
305 PARAMS ((void));
306 static bfd_boolean code_density_available
307 PARAMS ((void));
308 static bfd_boolean can_relax
309 PARAMS ((void));
310 static void directive_push
311 PARAMS ((directiveE, bfd_boolean, const void *));
312 static void directive_pop
313 PARAMS ((directiveE *, bfd_boolean *, const char **,
314 unsigned int *, const void **));
315 static void directive_balance
316 PARAMS ((void));
317 static bfd_boolean inside_directive
318 PARAMS ((directiveE));
319 static void get_directive
320 PARAMS ((directiveE *, bfd_boolean *));
321 static void xtensa_begin_directive
322 PARAMS ((int));
323 static void xtensa_end_directive
324 PARAMS ((int));
325 static void xtensa_literal_prefix
326 PARAMS ((char const *, int));
327 static void xtensa_literal_position
328 PARAMS ((int));
329 static void xtensa_literal_pseudo
330 PARAMS ((int));
331
332 /* Parsing and Idiom Translation Functions. */
333
334 static const char *expression_end
335 PARAMS ((const char *));
336 static unsigned tc_get_register
337 PARAMS ((const char *));
338 static void expression_maybe_register
339 PARAMS ((xtensa_operand, expressionS *));
340 static int tokenize_arguments
341 PARAMS ((char **, char *));
342 static bfd_boolean parse_arguments
343 PARAMS ((TInsn *, int, char **));
344 static int xg_translate_idioms
345 PARAMS ((char **, int *, char **));
346 static int xg_translate_sysreg_op
347 PARAMS ((char **, int *, char **));
348 static void xg_reverse_shift_count
349 PARAMS ((char **));
350 static int xg_arg_is_constant
351 PARAMS ((char *, offsetT *));
352 static void xg_replace_opname
353 PARAMS ((char **, char *));
354 static int xg_check_num_args
355 PARAMS ((int *, int, char *, char **));
356
357 /* Functions for dealing with the Xtensa ISA. */
358
359 static bfd_boolean operand_is_immed
360 PARAMS ((xtensa_operand));
361 static bfd_boolean operand_is_pcrel_label
362 PARAMS ((xtensa_operand));
363 static int get_relaxable_immed
364 PARAMS ((xtensa_opcode));
365 static xtensa_opcode get_opcode_from_buf
366 PARAMS ((const char *));
367 static bfd_boolean is_direct_call_opcode
368 PARAMS ((xtensa_opcode));
369 static bfd_boolean is_call_opcode
370 PARAMS ((xtensa_opcode));
371 static bfd_boolean is_entry_opcode
372 PARAMS ((xtensa_opcode));
373 static bfd_boolean is_loop_opcode
374 PARAMS ((xtensa_opcode));
375 static bfd_boolean is_the_loop_opcode
376 PARAMS ((xtensa_opcode));
377 static bfd_boolean is_jx_opcode
378 PARAMS ((xtensa_opcode));
379 static bfd_boolean is_windowed_return_opcode
380 PARAMS ((xtensa_opcode));
381 static bfd_boolean is_conditional_branch_opcode
382 PARAMS ((xtensa_opcode));
383 static bfd_boolean is_branch_or_jump_opcode
384 PARAMS ((xtensa_opcode));
385 static bfd_reloc_code_real_type opnum_to_reloc
386 PARAMS ((int));
387 static int reloc_to_opnum
388 PARAMS ((bfd_reloc_code_real_type));
389 static void xtensa_insnbuf_set_operand
390 PARAMS ((xtensa_insnbuf, xtensa_opcode, xtensa_operand, int32,
391 const char *, unsigned int));
392 static uint32 xtensa_insnbuf_get_operand
393 PARAMS ((xtensa_insnbuf, xtensa_opcode, int));
394 static void xtensa_insnbuf_set_immediate_field
395 PARAMS ((xtensa_opcode, xtensa_insnbuf, int32, const char *,
396 unsigned int));
397 static bfd_boolean is_negatable_branch
398 PARAMS ((TInsn *));
399
400 /* Functions for Internal Lists of Symbols. */
401 static void xtensa_define_label
402 PARAMS ((symbolS *));
403 static void add_target_symbol
404 PARAMS ((symbolS *, bfd_boolean));
405 static symbolS *xtensa_find_label
406 PARAMS ((fragS *, offsetT, bfd_boolean));
407 static void map_over_defined_symbols
408 PARAMS ((void (*fn) (symbolS *)));
409 static bfd_boolean is_loop_target_label
410 PARAMS ((symbolS *));
411 static void xtensa_mark_target_fragments
412 PARAMS ((void));
413
414 /* Various Other Internal Functions. */
415
416 static bfd_boolean is_unique_insn_expansion
417 PARAMS ((TransitionRule *));
418 static int xg_get_insn_size
419 PARAMS ((TInsn *));
420 static int xg_get_build_instr_size
421 PARAMS ((BuildInstr *));
422 static bfd_boolean xg_is_narrow_insn
423 PARAMS ((TInsn *));
424 static bfd_boolean xg_is_single_relaxable_insn
425 PARAMS ((TInsn *));
426 static int xg_get_max_narrow_insn_size
427 PARAMS ((xtensa_opcode));
428 static int xg_get_max_insn_widen_size
429 PARAMS ((xtensa_opcode));
430 static int xg_get_max_insn_widen_literal_size
431 PARAMS ((xtensa_opcode));
432 static bfd_boolean xg_is_relaxable_insn
433 PARAMS ((TInsn *, int));
434 static symbolS *get_special_literal_symbol
435 PARAMS ((void));
436 static symbolS *get_special_label_symbol
437 PARAMS ((void));
438 static bfd_boolean xg_build_to_insn
439 PARAMS ((TInsn *, TInsn *, BuildInstr *));
440 static bfd_boolean xg_build_to_stack
441 PARAMS ((IStack *, TInsn *, BuildInstr *));
442 static bfd_boolean xg_expand_to_stack
443 PARAMS ((IStack *, TInsn *, int));
444 static bfd_boolean xg_expand_narrow
445 PARAMS ((TInsn *, TInsn *));
446 static bfd_boolean xg_immeds_fit
447 PARAMS ((const TInsn *));
448 static bfd_boolean xg_symbolic_immeds_fit
449 PARAMS ((const TInsn *, segT, fragS *, offsetT, long));
450 static bfd_boolean xg_check_operand
451 PARAMS ((int32, xtensa_operand));
452 static int is_dnrange
453 PARAMS ((fragS *, symbolS *, long));
454 static int xg_assembly_relax
455 PARAMS ((IStack *, TInsn *, segT, fragS *, offsetT, int, long));
456 static void xg_force_frag_space
457 PARAMS ((int));
458 static void xg_finish_frag
459 PARAMS ((char *, enum xtensa_relax_statesE, int, bfd_boolean));
460 static bfd_boolean is_branch_jmp_to_next
461 PARAMS ((TInsn *, fragS *));
462 static void xg_add_branch_and_loop_targets
463 PARAMS ((TInsn *));
464 static bfd_boolean xg_instruction_matches_rule
465 PARAMS ((TInsn *, TransitionRule *));
466 static TransitionRule *xg_instruction_match
467 PARAMS ((TInsn *));
468 static bfd_boolean xg_build_token_insn
469 PARAMS ((BuildInstr *, TInsn *, TInsn *));
470 static bfd_boolean xg_simplify_insn
471 PARAMS ((TInsn *, TInsn *));
472 static bfd_boolean xg_expand_assembly_insn
473 PARAMS ((IStack *, TInsn *));
474 static symbolS *xg_assemble_literal
475 PARAMS ((TInsn *));
476 static void xg_assemble_literal_space
477 PARAMS ((int));
478 static symbolS *xtensa_create_literal_symbol
479 PARAMS ((segT, fragS *));
480 static bfd_boolean get_is_linkonce_section
481 PARAMS ((bfd *, segT));
482 static bfd_boolean xg_emit_insn
483 PARAMS ((TInsn *, bfd_boolean));
484 static bfd_boolean xg_emit_insn_to_buf
485 PARAMS ((TInsn *, char *, fragS *, offsetT, bfd_boolean));
486 static bfd_boolean xg_add_opcode_fix
487 PARAMS ((xtensa_opcode, int, expressionS *, fragS *, offsetT));
488 static void xg_resolve_literals
489 PARAMS ((TInsn *, symbolS *));
490 static void xg_resolve_labels
491 PARAMS ((TInsn *, symbolS *));
492 static void xg_assemble_tokens
493 PARAMS ((TInsn *));
494 static bfd_boolean is_register_writer
495 PARAMS ((const TInsn *, const char *, int));
496 static bfd_boolean is_bad_loopend_opcode
497 PARAMS ((const TInsn *));
498 static bfd_boolean is_unaligned_label
499 PARAMS ((symbolS *));
500 static fragS *next_non_empty_frag
501 PARAMS ((const fragS *));
502 static xtensa_opcode next_frag_opcode
503 PARAMS ((const fragS *));
504 static void update_next_frag_nop_state
505 PARAMS ((fragS *));
506 static bfd_boolean next_frag_is_branch_target
507 PARAMS ((const fragS *));
508 static bfd_boolean next_frag_is_loop_target
509 PARAMS ((const fragS *));
510 static addressT next_frag_pre_opcode_bytes
511 PARAMS ((const fragS *));
512 static bfd_boolean is_next_frag_target
513 PARAMS ((const fragS *, const fragS *));
514 static void xtensa_mark_literal_pool_location
515 PARAMS ((void));
516 static void xtensa_move_labels
517 PARAMS ((fragS *, valueT, fragS *, valueT));
518 static void assemble_nop
519 PARAMS ((size_t, char *));
520 static addressT get_expanded_loop_offset
521 PARAMS ((xtensa_opcode));
522 static fragS *get_literal_pool_location
523 PARAMS ((segT));
524 static void set_literal_pool_location
525 PARAMS ((segT, fragS *));
526
527 /* Helpers for xtensa_end(). */
528
529 static void xtensa_cleanup_align_frags
530 PARAMS ((void));
531 static void xtensa_fix_target_frags
532 PARAMS ((void));
533 static bfd_boolean frag_can_negate_branch
534 PARAMS ((fragS *));
535 static void xtensa_fix_a0_b_retw_frags
536 PARAMS ((void));
537 static bfd_boolean next_instrs_are_b_retw
538 PARAMS ((fragS *));
539 static void xtensa_fix_b_j_loop_end_frags
540 PARAMS ((void));
541 static bfd_boolean next_instr_is_loop_end
542 PARAMS ((fragS *));
543 static void xtensa_fix_close_loop_end_frags
544 PARAMS ((void));
545 static size_t min_bytes_to_other_loop_end
546 PARAMS ((fragS *, fragS *, offsetT, size_t));
547 static size_t unrelaxed_frag_min_size
548 PARAMS ((fragS *));
549 static void xtensa_fix_short_loop_frags
550 PARAMS ((void));
551 static size_t count_insns_to_loop_end
552 PARAMS ((fragS *, bfd_boolean, size_t));
553 static size_t unrelaxed_frag_min_insn_count
554 PARAMS ((fragS *));
555 static bfd_boolean branch_before_loop_end
556 PARAMS ((fragS *));
557 static bfd_boolean unrelaxed_frag_has_b_j
558 PARAMS ((fragS *));
559 static void xtensa_sanity_check
560 PARAMS ((void));
561 static bfd_boolean is_empty_loop
562 PARAMS ((const TInsn *, fragS *));
563 static bfd_boolean is_local_forward_loop
564 PARAMS ((const TInsn *, fragS *));
565
566 /* Alignment Functions. */
567
568 static size_t get_text_align_power
569 PARAMS ((int));
570 static addressT get_text_align_max_fill_size
571 PARAMS ((int, bfd_boolean, bfd_boolean));
572 static addressT get_text_align_fill_size
573 PARAMS ((addressT, int, int, bfd_boolean, bfd_boolean));
574 static size_t get_text_align_nop_count
575 PARAMS ((size_t, bfd_boolean));
576 static size_t get_text_align_nth_nop_size
577 PARAMS ((size_t, size_t, bfd_boolean));
578 static addressT get_noop_aligned_address
579 PARAMS ((fragS *, addressT));
580 static addressT get_widen_aligned_address
581 PARAMS ((fragS *, addressT));
582
583 /* Helpers for xtensa_relax_frag(). */
584
585 static long relax_frag_text_align
586 PARAMS ((fragS *, long));
587 static long relax_frag_add_nop
588 PARAMS ((fragS *));
589 static long relax_frag_narrow
590 PARAMS ((fragS *, long));
591 static bfd_boolean future_alignment_required
592 PARAMS ((fragS *, long));
593 static long relax_frag_immed
594 PARAMS ((segT, fragS *, long, int, int *));
595
596 /* Helpers for md_convert_frag(). */
597
598 static void convert_frag_align_next_opcode
599 PARAMS ((fragS *));
600 static void convert_frag_narrow
601 PARAMS ((fragS *));
602 static void convert_frag_immed
603 PARAMS ((segT, fragS *, int));
604 static fixS *fix_new_exp_in_seg
605 PARAMS ((segT, subsegT, fragS *, int, int, expressionS *, int,
606 bfd_reloc_code_real_type));
607 static void convert_frag_immed_finish_loop
608 PARAMS ((segT, fragS *, TInsn *));
609 static offsetT get_expression_value
610 PARAMS ((segT, expressionS *));
611
612 /* Flags for the Last Instruction in Each Subsegment. */
613
614 static unsigned get_last_insn_flags
615 PARAMS ((segT, subsegT));
616 static void set_last_insn_flags
617 PARAMS ((segT, subsegT, unsigned, bfd_boolean));
618
619 /* Segment list functions. */
620
621 static void xtensa_remove_section
622 PARAMS ((segT));
623 static void xtensa_insert_section
624 PARAMS ((segT, segT));
625 static void xtensa_move_seg_list_to_beginning
626 PARAMS ((seg_list *));
627 static void xtensa_move_literals
628 PARAMS ((void));
629 static void xtensa_move_frag_symbol
630 PARAMS ((symbolS *));
631 static void xtensa_move_frag_symbols
632 PARAMS ((void));
633 static void xtensa_reorder_seg_list
634 PARAMS ((seg_list *, segT));
635 static void xtensa_reorder_segments
636 PARAMS ((void));
637 static segT get_last_sec
638 PARAMS ((void));
639 static void xtensa_switch_to_literal_fragment
640 PARAMS ((emit_state *));
641 static void xtensa_switch_section_emit_state
642 PARAMS ((emit_state *, segT, subsegT));
643 static void xtensa_restore_emit_state
644 PARAMS ((emit_state *));
645 static void cache_literal_section
646 PARAMS ((seg_list *, const char *, segT *));
647 static segT retrieve_literal_seg
648 PARAMS ((seg_list *, const char *));
649 static segT seg_present
650 PARAMS ((const char *));
651 static void add_seg_list
652 PARAMS ((seg_list *, segT));
653
654 /* Property Table (e.g., ".xt.insn" and ".xt.lit") Functions. */
655
656 static void xtensa_create_property_segments
657 PARAMS ((frag_predicate, const char *, xt_section_type));
658 static segment_info_type *retrieve_segment_info
659 PARAMS ((segT));
660 static segT retrieve_xtensa_section
661 PARAMS ((char *));
662 static bfd_boolean section_has_property
663 PARAMS ((segT sec, frag_predicate));
664 static void add_xt_block_frags
665 PARAMS ((segT, segT, xtensa_block_info **, frag_predicate));
666 static bfd_boolean get_frag_is_literal
667 PARAMS ((const fragS *));
668 static bfd_boolean get_frag_is_insn
669 PARAMS ((const fragS *));
670
671 /* Import from elf32-xtensa.c in BFD library. */
672 extern char *xtensa_get_property_section_name
673 PARAMS ((bfd *, asection *, const char *));
674
675 /* TInsn and IStack functions. */
676 static bfd_boolean tinsn_has_symbolic_operands
677 PARAMS ((const TInsn *));
678 static bfd_boolean tinsn_has_invalid_symbolic_operands
679 PARAMS ((const TInsn *));
680 static bfd_boolean tinsn_has_complex_operands
681 PARAMS ((const TInsn *));
682 static bfd_boolean tinsn_to_insnbuf
683 PARAMS ((TInsn *, xtensa_insnbuf));
684 static bfd_boolean tinsn_check_arguments
685 PARAMS ((const TInsn *));
686 static void tinsn_from_chars
687 PARAMS ((TInsn *, char *));
688 static void tinsn_immed_from_frag
689 PARAMS ((TInsn *, fragS *));
690 static int get_num_stack_text_bytes
691 PARAMS ((IStack *));
692 static int get_num_stack_literal_bytes
693 PARAMS ((IStack *));
694
695 /* Expression Utilities. */
696 bfd_boolean expr_is_const
697 PARAMS ((const expressionS *));
698 offsetT get_expr_const
699 PARAMS ((const expressionS *));
700 void set_expr_const
701 PARAMS ((expressionS *, offsetT));
702 void set_expr_symbol_offset
703 PARAMS ((expressionS *, symbolS *, offsetT));
704 bfd_boolean expr_is_equal
705 PARAMS ((expressionS *, expressionS *));
706 static void copy_expr
707 PARAMS ((expressionS *, const expressionS *));
708
709 #ifdef XTENSA_SECTION_RENAME
710 static void build_section_rename
711 PARAMS ((const char *));
712 static void add_section_rename
713 PARAMS ((char *, char *));
714 #endif
715
716
717 /* ISA imported from bfd. */
718 extern xtensa_isa xtensa_default_isa;
719
720 extern int target_big_endian;
721
722 static xtensa_opcode xtensa_addi_opcode;
723 static xtensa_opcode xtensa_addmi_opcode;
724 static xtensa_opcode xtensa_call0_opcode;
725 static xtensa_opcode xtensa_call4_opcode;
726 static xtensa_opcode xtensa_call8_opcode;
727 static xtensa_opcode xtensa_call12_opcode;
728 static xtensa_opcode xtensa_callx0_opcode;
729 static xtensa_opcode xtensa_callx4_opcode;
730 static xtensa_opcode xtensa_callx8_opcode;
731 static xtensa_opcode xtensa_callx12_opcode;
732 static xtensa_opcode xtensa_entry_opcode;
733 static xtensa_opcode xtensa_isync_opcode;
734 static xtensa_opcode xtensa_j_opcode;
735 static xtensa_opcode xtensa_jx_opcode;
736 static xtensa_opcode xtensa_loop_opcode;
737 static xtensa_opcode xtensa_loopnez_opcode;
738 static xtensa_opcode xtensa_loopgtz_opcode;
739 static xtensa_opcode xtensa_nop_n_opcode;
740 static xtensa_opcode xtensa_or_opcode;
741 static xtensa_opcode xtensa_ret_opcode;
742 static xtensa_opcode xtensa_ret_n_opcode;
743 static xtensa_opcode xtensa_retw_opcode;
744 static xtensa_opcode xtensa_retw_n_opcode;
745 static xtensa_opcode xtensa_rsr_opcode;
746 static xtensa_opcode xtensa_waiti_opcode;
747
748 \f
749 /* Command-line Options. */
750
751 bfd_boolean use_literal_section = TRUE;
752 static bfd_boolean align_targets = TRUE;
753 static bfd_boolean align_only_targets = FALSE;
754 static bfd_boolean software_a0_b_retw_interlock = TRUE;
755 static bfd_boolean has_a0_b_retw = FALSE;
756 static bfd_boolean workaround_a0_b_retw = TRUE;
757
758 static bfd_boolean software_avoid_b_j_loop_end = TRUE;
759 static bfd_boolean workaround_b_j_loop_end = TRUE;
760 static bfd_boolean maybe_has_b_j_loop_end = FALSE;
761
762 static bfd_boolean software_avoid_short_loop = TRUE;
763 static bfd_boolean workaround_short_loop = TRUE;
764 static bfd_boolean maybe_has_short_loop = FALSE;
765
766 static bfd_boolean software_avoid_close_loop_end = TRUE;
767 static bfd_boolean workaround_close_loop_end = TRUE;
768 static bfd_boolean maybe_has_close_loop_end = FALSE;
769
770 /* When avoid_short_loops is true, all loops with early exits must
771 have at least 3 instructions. avoid_all_short_loops is a modifier
772 to the avoid_short_loop flag. In addition to the avoid_short_loop
773 actions, all straightline loopgtz and loopnez must have at least 3
774 instructions. */
775
776 static bfd_boolean software_avoid_all_short_loops = TRUE;
777 static bfd_boolean workaround_all_short_loops = TRUE;
778
779 /* This is on a per-instruction basis. */
780 static bfd_boolean specific_opcode = FALSE;
781
782 enum
783 {
784 option_density = OPTION_MD_BASE,
785 option_no_density,
786
787 option_relax,
788 option_no_relax,
789
790 option_generics,
791 option_no_generics,
792
793 option_text_section_literals,
794 option_no_text_section_literals,
795
796 option_align_targets,
797 option_no_align_targets,
798
799 option_align_only_targets,
800 option_no_align_only_targets,
801
802 option_longcalls,
803 option_no_longcalls,
804
805 option_workaround_a0_b_retw,
806 option_no_workaround_a0_b_retw,
807
808 option_workaround_b_j_loop_end,
809 option_no_workaround_b_j_loop_end,
810
811 option_workaround_short_loop,
812 option_no_workaround_short_loop,
813
814 option_workaround_all_short_loops,
815 option_no_workaround_all_short_loops,
816
817 option_workaround_close_loop_end,
818 option_no_workaround_close_loop_end,
819
820 option_no_workarounds,
821
822 #ifdef XTENSA_SECTION_RENAME
823 option_literal_section_name,
824 option_text_section_name,
825 option_data_section_name,
826 option_bss_section_name,
827 option_rename_section_name,
828 #endif
829
830 option_eb,
831 option_el
832 };
833
834 const char *md_shortopts = "";
835
836 struct option md_longopts[] =
837 {
838 {"density", no_argument, NULL, option_density},
839 {"no-density", no_argument, NULL, option_no_density},
840 /* At least as early as alameda, --[no-]relax didn't work as
841 documented, so as of albany, --[no-]relax is equivalent to
842 --[no-]generics. Both of these will be deprecated in
843 BearValley. */
844 {"relax", no_argument, NULL, option_generics},
845 {"no-relax", no_argument, NULL, option_no_generics},
846 {"generics", no_argument, NULL, option_generics},
847 {"no-generics", no_argument, NULL, option_no_generics},
848 {"text-section-literals", no_argument, NULL, option_text_section_literals},
849 {"no-text-section-literals", no_argument, NULL,
850 option_no_text_section_literals},
851 /* This option was changed from -align-target to -target-align
852 because it conflicted with the "-al" option. */
853 {"target-align", no_argument, NULL, option_align_targets},
854 {"no-target-align", no_argument, NULL,
855 option_no_align_targets},
856 #if 0
857 /* This option should do a better job aligning targets because
858 it will only attempt to align targets that are the target of a
859 branch. */
860 { "target-align-only", no_argument, NULL, option_align_only_targets },
861 { "no-target-align-only", no_argument, NULL, option_no_align_only_targets },
862 #endif /* 0 */
863 {"longcalls", no_argument, NULL, option_longcalls},
864 {"no-longcalls", no_argument, NULL, option_no_longcalls},
865
866 {"no-workaround-a0-b-retw", no_argument, NULL,
867 option_no_workaround_a0_b_retw},
868 {"workaround-a0-b-retw", no_argument, NULL, option_workaround_a0_b_retw},
869
870 {"no-workaround-b-j-loop-end", no_argument, NULL,
871 option_no_workaround_b_j_loop_end},
872 {"workaround-b-j-loop-end", no_argument, NULL,
873 option_workaround_b_j_loop_end},
874
875 {"no-workaround-short-loops", no_argument, NULL,
876 option_no_workaround_short_loop},
877 {"workaround-short-loops", no_argument, NULL, option_workaround_short_loop},
878
879 {"no-workaround-all-short-loops", no_argument, NULL,
880 option_no_workaround_all_short_loops},
881 {"workaround-all-short-loop", no_argument, NULL,
882 option_workaround_all_short_loops},
883
884 {"no-workaround-close-loop-end", no_argument, NULL,
885 option_no_workaround_close_loop_end},
886 {"workaround-close-loop-end", no_argument, NULL,
887 option_workaround_close_loop_end},
888
889 {"no-workarounds", no_argument, NULL, option_no_workarounds},
890
891 #ifdef XTENSA_SECTION_RENAME
892 {"literal-section-name", required_argument, NULL,
893 option_literal_section_name},
894 {"text-section-name", required_argument, NULL,
895 option_text_section_name},
896 {"data-section-name", required_argument, NULL,
897 option_data_section_name},
898 {"rename-section", required_argument, NULL,
899 option_rename_section_name},
900 {"bss-section-name", required_argument, NULL,
901 option_bss_section_name},
902 #endif /* XTENSA_SECTION_RENAME */
903
904 {NULL, no_argument, NULL, 0}
905 };
906
907 size_t md_longopts_size = sizeof md_longopts;
908
909
910 int
911 md_parse_option (c, arg)
912 int c;
913 char *arg;
914 {
915 switch (c)
916 {
917 case option_density:
918 if (!density_supported)
919 {
920 as_bad (_("'--density' option not supported in this Xtensa "
921 "configuration"));
922 return 0;
923 }
924 directive_state[directive_density] = TRUE;
925 return 1;
926 case option_no_density:
927 directive_state[directive_density] = FALSE;
928 return 1;
929 case option_generics:
930 directive_state[directive_generics] = TRUE;
931 return 1;
932 case option_no_generics:
933 directive_state[directive_generics] = FALSE;
934 return 1;
935 case option_longcalls:
936 directive_state[directive_longcalls] = TRUE;
937 return 1;
938 case option_no_longcalls:
939 directive_state[directive_longcalls] = FALSE;
940 return 1;
941 case option_text_section_literals:
942 use_literal_section = FALSE;
943 return 1;
944 case option_no_text_section_literals:
945 use_literal_section = TRUE;
946 return 1;
947 case option_workaround_a0_b_retw:
948 workaround_a0_b_retw = TRUE;
949 software_a0_b_retw_interlock = TRUE;
950 return 1;
951 case option_no_workaround_a0_b_retw:
952 workaround_a0_b_retw = FALSE;
953 software_a0_b_retw_interlock = FALSE;
954 return 1;
955 case option_workaround_b_j_loop_end:
956 workaround_b_j_loop_end = TRUE;
957 software_avoid_b_j_loop_end = TRUE;
958 return 1;
959 case option_no_workaround_b_j_loop_end:
960 workaround_b_j_loop_end = FALSE;
961 software_avoid_b_j_loop_end = FALSE;
962 return 1;
963
964 case option_workaround_short_loop:
965 workaround_short_loop = TRUE;
966 software_avoid_short_loop = TRUE;
967 return 1;
968 case option_no_workaround_short_loop:
969 workaround_short_loop = FALSE;
970 software_avoid_short_loop = FALSE;
971 return 1;
972
973 case option_workaround_all_short_loops:
974 workaround_all_short_loops = TRUE;
975 software_avoid_all_short_loops = TRUE;
976 return 1;
977 case option_no_workaround_all_short_loops:
978 workaround_all_short_loops = FALSE;
979 software_avoid_all_short_loops = FALSE;
980 return 1;
981
982 case option_workaround_close_loop_end:
983 workaround_close_loop_end = TRUE;
984 software_avoid_close_loop_end = TRUE;
985 return 1;
986 case option_no_workaround_close_loop_end:
987 workaround_close_loop_end = FALSE;
988 software_avoid_close_loop_end = FALSE;
989 return 1;
990
991 case option_no_workarounds:
992 workaround_a0_b_retw = FALSE;
993 software_a0_b_retw_interlock = FALSE;
994 workaround_b_j_loop_end = FALSE;
995 software_avoid_b_j_loop_end = FALSE;
996 workaround_short_loop = FALSE;
997 software_avoid_short_loop = FALSE;
998 workaround_all_short_loops = FALSE;
999 software_avoid_all_short_loops = FALSE;
1000 workaround_close_loop_end = FALSE;
1001 software_avoid_close_loop_end = FALSE;
1002 return 1;
1003
1004 case option_align_targets:
1005 align_targets = TRUE;
1006 return 1;
1007 case option_no_align_targets:
1008 align_targets = FALSE;
1009 return 1;
1010
1011 case option_align_only_targets:
1012 align_only_targets = TRUE;
1013 return 1;
1014 case option_no_align_only_targets:
1015 align_only_targets = FALSE;
1016 return 1;
1017
1018 #ifdef XTENSA_SECTION_RENAME
1019 case option_literal_section_name:
1020 add_section_rename (".literal", arg);
1021 as_warn (_("'--literal-section-name' is deprecated; "
1022 "use '--rename-section .literal=NEWNAME'"));
1023 return 1;
1024
1025 case option_text_section_name:
1026 add_section_rename (".text", arg);
1027 as_warn (_("'--text-section-name' is deprecated; "
1028 "use '--rename-section .text=NEWNAME'"));
1029 return 1;
1030
1031 case option_data_section_name:
1032 add_section_rename (".data", arg);
1033 as_warn (_("'--data-section-name' is deprecated; "
1034 "use '--rename-section .data=NEWNAME'"));
1035 return 1;
1036
1037 case option_bss_section_name:
1038 add_section_rename (".bss", arg);
1039 as_warn (_("'--bss-section-name' is deprecated; "
1040 "use '--rename-section .bss=NEWNAME'"));
1041 return 1;
1042
1043 case option_rename_section_name:
1044 build_section_rename (arg);
1045 return 1;
1046 #endif /* XTENSA_SECTION_RENAME */
1047
1048 case 'Q':
1049 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
1050 should be emitted or not. FIXME: Not implemented. */
1051 return 1;
1052
1053 default:
1054 return 0;
1055 }
1056 }
1057
1058
1059 void
1060 md_show_usage (stream)
1061 FILE *stream;
1062 {
1063 fputs ("\nXtensa options:\n"
1064 "--[no-]density [Do not] emit density instructions\n"
1065 "--[no-]relax [Do not] perform branch relaxation\n"
1066 "--[no-]generics [Do not] transform instructions\n"
1067 "--[no-]longcalls [Do not] emit 32-bit call sequences\n"
1068 "--[no-]target-align [Do not] try to align branch targets\n"
1069 "--[no-]text-section-literals\n"
1070 " [Do not] put literals in the text section\n"
1071 "--no-workarounds Do not use any Xtensa workarounds\n"
1072 #ifdef XTENSA_SECTION_RENAME
1073 "--rename-section old=new(:old1=new1)*\n"
1074 " Rename section 'old' to 'new'\n"
1075 "\nThe following Xtensa options are deprecated\n"
1076 "--literal-section-name Name of literal section (default .literal)\n"
1077 "--text-section-name Name of text section (default .text)\n"
1078 "--data-section-name Name of data section (default .data)\n"
1079 "--bss-section-name Name of bss section (default .bss)\n"
1080 #endif
1081 , stream);
1082 }
1083
1084 \f
1085 /* Directive data and functions. */
1086
1087 typedef struct state_stackS_struct
1088 {
1089 directiveE directive;
1090 bfd_boolean negated;
1091 bfd_boolean old_state;
1092 const char *file;
1093 unsigned int line;
1094 const void *datum;
1095 struct state_stackS_struct *prev;
1096 } state_stackS;
1097
1098 state_stackS *directive_state_stack;
1099
1100 const pseudo_typeS md_pseudo_table[] =
1101 {
1102 {"align", s_align_bytes, 0}, /* Defaulting is invalid (0) */
1103 {"literal_position", xtensa_literal_position, 0},
1104 {"frame", s_ignore, 0}, /* formerly used for STABS debugging */
1105 {"word", cons, 4},
1106 {"begin", xtensa_begin_directive, 0},
1107 {"end", xtensa_end_directive, 0},
1108 {"literal", xtensa_literal_pseudo, 0},
1109 {NULL, 0, 0},
1110 };
1111
1112
1113 bfd_boolean
1114 use_generics ()
1115 {
1116 return directive_state[directive_generics];
1117 }
1118
1119
1120 bfd_boolean
1121 use_longcalls ()
1122 {
1123 return directive_state[directive_longcalls];
1124 }
1125
1126
1127 bfd_boolean
1128 code_density_available ()
1129 {
1130 return directive_state[directive_density];
1131 }
1132
1133
1134 bfd_boolean
1135 can_relax ()
1136 {
1137 return use_generics ();
1138 }
1139
1140
1141 static void
1142 directive_push (directive, negated, datum)
1143 directiveE directive;
1144 bfd_boolean negated;
1145 const void *datum;
1146 {
1147 char *file;
1148 unsigned int line;
1149 state_stackS *stack = (state_stackS *) xmalloc (sizeof (state_stackS));
1150
1151 as_where (&file, &line);
1152
1153 stack->directive = directive;
1154 stack->negated = negated;
1155 stack->old_state = directive_state[directive];
1156 stack->file = file;
1157 stack->line = line;
1158 stack->datum = datum;
1159 stack->prev = directive_state_stack;
1160 directive_state_stack = stack;
1161
1162 directive_state[directive] = !negated;
1163 }
1164
1165 static void
1166 directive_pop (directive, negated, file, line, datum)
1167 directiveE *directive;
1168 bfd_boolean *negated;
1169 const char **file;
1170 unsigned int *line;
1171 const void **datum;
1172 {
1173 state_stackS *top = directive_state_stack;
1174
1175 if (!directive_state_stack)
1176 {
1177 as_bad (_("unmatched end directive"));
1178 *directive = directive_none;
1179 return;
1180 }
1181
1182 directive_state[directive_state_stack->directive] = top->old_state;
1183 *directive = top->directive;
1184 *negated = top->negated;
1185 *file = top->file;
1186 *line = top->line;
1187 *datum = top->datum;
1188 directive_state_stack = top->prev;
1189 free (top);
1190 }
1191
1192
1193 static void
1194 directive_balance ()
1195 {
1196 while (directive_state_stack)
1197 {
1198 directiveE directive;
1199 bfd_boolean negated;
1200 const char *file;
1201 unsigned int line;
1202 const void *datum;
1203
1204 directive_pop (&directive, &negated, &file, &line, &datum);
1205 as_warn_where ((char *) file, line,
1206 _(".begin directive with no matching .end directive"));
1207 }
1208 }
1209
1210
1211 static bfd_boolean
1212 inside_directive (dir)
1213 directiveE dir;
1214 {
1215 state_stackS *top = directive_state_stack;
1216
1217 while (top && top->directive != dir)
1218 top = top->prev;
1219
1220 return (top != NULL);
1221 }
1222
1223
1224 static void
1225 get_directive (directive, negated)
1226 directiveE *directive;
1227 bfd_boolean *negated;
1228 {
1229 int len;
1230 unsigned i;
1231
1232 if (strncmp (input_line_pointer, "no-", 3) != 0)
1233 *negated = FALSE;
1234 else
1235 {
1236 *negated = TRUE;
1237 input_line_pointer += 3;
1238 }
1239
1240 len = strspn (input_line_pointer,
1241 "abcdefghijklmnopqrstuvwxyz_/0123456789.");
1242
1243 for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i)
1244 {
1245 if (strncmp (input_line_pointer, directive_info[i].name, len) == 0)
1246 {
1247 input_line_pointer += len;
1248 *directive = (directiveE) i;
1249 if (*negated && !directive_info[i].can_be_negated)
1250 as_bad (_("directive %s can't be negated"),
1251 directive_info[i].name);
1252 return;
1253 }
1254 }
1255
1256 as_bad (_("unknown directive"));
1257 *directive = (directiveE) XTENSA_UNDEFINED;
1258 }
1259
1260
1261 static void
1262 xtensa_begin_directive (ignore)
1263 int ignore ATTRIBUTE_UNUSED;
1264 {
1265 directiveE directive;
1266 bfd_boolean negated;
1267 emit_state *state;
1268 int len;
1269 lit_state *ls;
1270
1271 get_directive (&directive, &negated);
1272 if (directive == (directiveE) XTENSA_UNDEFINED)
1273 {
1274 discard_rest_of_line ();
1275 return;
1276 }
1277
1278 switch (directive)
1279 {
1280 case directive_literal:
1281 state = (emit_state *) xmalloc (sizeof (emit_state));
1282 xtensa_switch_to_literal_fragment (state);
1283 directive_push (directive_literal, negated, state);
1284 break;
1285
1286 case directive_literal_prefix:
1287 /* Check to see if the current fragment is a literal
1288 fragment. If it is, then this operation is not allowed. */
1289 if (frag_now->tc_frag_data.is_literal)
1290 {
1291 as_bad (_("cannot set literal_prefix inside literal fragment"));
1292 return;
1293 }
1294
1295 /* Allocate the literal state for this section and push
1296 onto the directive stack. */
1297 ls = xmalloc (sizeof (lit_state));
1298 assert (ls);
1299
1300 *ls = default_lit_sections;
1301
1302 directive_push (directive_literal_prefix, negated, ls);
1303
1304 /* Parse the new prefix from the input_line_pointer. */
1305 SKIP_WHITESPACE ();
1306 len = strspn (input_line_pointer,
1307 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1308 "abcdefghijklmnopqrstuvwxyz_/0123456789.$");
1309
1310 /* Process the new prefix. */
1311 xtensa_literal_prefix (input_line_pointer, len);
1312
1313 /* Skip the name in the input line. */
1314 input_line_pointer += len;
1315 break;
1316
1317 case directive_freeregs:
1318 /* This information is currently unused, but we'll accept the statement
1319 and just discard the rest of the line. This won't check the syntax,
1320 but it will accept every correct freeregs directive. */
1321 input_line_pointer += strcspn (input_line_pointer, "\n");
1322 directive_push (directive_freeregs, negated, 0);
1323 break;
1324
1325 case directive_density:
1326 if (!density_supported && !negated)
1327 {
1328 as_warn (_("Xtensa density option not supported; ignored"));
1329 break;
1330 }
1331 /* fall through */
1332
1333 default:
1334 directive_push (directive, negated, 0);
1335 break;
1336 }
1337
1338 demand_empty_rest_of_line ();
1339 }
1340
1341
1342 static void
1343 xtensa_end_directive (ignore)
1344 int ignore ATTRIBUTE_UNUSED;
1345 {
1346 directiveE begin_directive, end_directive;
1347 bfd_boolean begin_negated, end_negated;
1348 const char *file;
1349 unsigned int line;
1350 emit_state *state;
1351 lit_state *s;
1352
1353 get_directive (&end_directive, &end_negated);
1354 if (end_directive == (directiveE) XTENSA_UNDEFINED)
1355 {
1356 discard_rest_of_line ();
1357 return;
1358 }
1359
1360 if (end_directive == directive_density && !density_supported && !end_negated)
1361 {
1362 as_warn (_("Xtensa density option not supported; ignored"));
1363 demand_empty_rest_of_line ();
1364 return;
1365 }
1366
1367 directive_pop (&begin_directive, &begin_negated, &file, &line,
1368 (const void **) &state);
1369
1370 if (begin_directive != directive_none)
1371 {
1372 if (begin_directive != end_directive || begin_negated != end_negated)
1373 {
1374 as_bad (_("does not match begin %s%s at %s:%d"),
1375 begin_negated ? "no-" : "",
1376 directive_info[begin_directive].name, file, line);
1377 }
1378 else
1379 {
1380 switch (end_directive)
1381 {
1382 case directive_literal:
1383 frag_var (rs_fill, 0, 0, 0, NULL, 0, NULL);
1384 xtensa_restore_emit_state (state);
1385 free (state);
1386 break;
1387
1388 case directive_freeregs:
1389 break;
1390
1391 case directive_literal_prefix:
1392 /* Restore the default collection sections from saved state. */
1393 s = (lit_state *) state;
1394 assert (s);
1395
1396 if (use_literal_section)
1397 default_lit_sections = *s;
1398
1399 /* free the state storage */
1400 free (s);
1401 break;
1402
1403 default:
1404 break;
1405 }
1406 }
1407 }
1408
1409 demand_empty_rest_of_line ();
1410 }
1411
1412
1413 /* Place an aligned literal fragment at the current location. */
1414
1415 static void
1416 xtensa_literal_position (ignore)
1417 int ignore ATTRIBUTE_UNUSED;
1418 {
1419 if (inside_directive (directive_literal))
1420 as_warn (_(".literal_position inside literal directive; ignoring"));
1421 else if (!use_literal_section)
1422 xtensa_mark_literal_pool_location ();
1423
1424 demand_empty_rest_of_line ();
1425 }
1426
1427
1428 /* Support .literal label, value@plt + offset. */
1429
1430 static void
1431 xtensa_literal_pseudo (ignored)
1432 int ignored ATTRIBUTE_UNUSED;
1433 {
1434 emit_state state;
1435 char *p, *base_name;
1436 char c;
1437 expressionS expP;
1438 segT dest_seg;
1439
1440 /* If we are using text-section literals, then this is the right value... */
1441 dest_seg = now_seg;
1442
1443 base_name = input_line_pointer;
1444
1445 xtensa_switch_to_literal_fragment (&state);
1446
1447 /* ...but if we aren't using text-section-literals, then we
1448 need to put them in the section we just switched to. */
1449 if (use_literal_section)
1450 dest_seg = now_seg;
1451
1452 /* All literals are aligned to four-byte boundaries
1453 which is handled by switch to literal fragment. */
1454 /* frag_align (2, 0, 0); */
1455
1456 c = get_symbol_end ();
1457 /* Just after name is now '\0'. */
1458 p = input_line_pointer;
1459 *p = c;
1460 SKIP_WHITESPACE ();
1461
1462 if (*input_line_pointer != ',' && *input_line_pointer != ':')
1463 {
1464 as_bad (_("expected comma or colon after symbol name; "
1465 "rest of line ignored"));
1466 ignore_rest_of_line ();
1467 xtensa_restore_emit_state (&state);
1468 return;
1469 }
1470 *p = 0;
1471
1472 colon (base_name);
1473
1474 do
1475 {
1476 input_line_pointer++; /* skip ',' or ':' */
1477
1478 expr (0, &expP);
1479
1480 /* We only support 4-byte literals with .literal. */
1481 emit_expr (&expP, 4);
1482 }
1483 while (*input_line_pointer == ',');
1484
1485 *p = c;
1486
1487 demand_empty_rest_of_line ();
1488
1489 xtensa_restore_emit_state (&state);
1490 }
1491
1492
1493 static void
1494 xtensa_literal_prefix (start, len)
1495 char const *start;
1496 int len;
1497 {
1498 segT s_now; /* Storage for the current seg and subseg. */
1499 subsegT ss_now;
1500 char *name; /* Pointer to the name itself. */
1501 char *newname;
1502
1503 if (!use_literal_section)
1504 return;
1505
1506 /* Store away the current section and subsection. */
1507 s_now = now_seg;
1508 ss_now = now_subseg;
1509
1510 /* Get a null-terminated copy of the name. */
1511 name = xmalloc (len + 1);
1512 assert (name);
1513
1514 strncpy (name, start, len);
1515 name[len] = 0;
1516
1517 /* Allocate the sections (interesting note: the memory pointing to
1518 the name is actually used for the name by the new section). */
1519 newname = xmalloc (len + strlen (".literal") + 1);
1520 strcpy (newname, name);
1521 strcpy (newname + len, ".literal");
1522
1523 /* Note that retrieve_literal_seg does not create a segment if
1524 it already exists. */
1525 default_lit_sections.lit_seg = NULL; /* retrieved on demand */
1526
1527 /* Canonicalizing section names allows renaming literal
1528 sections to occur correctly. */
1529 default_lit_sections.lit_seg_name =
1530 tc_canonicalize_symbol_name (newname);
1531
1532 free (name);
1533
1534 /* Restore the current section and subsection and set the
1535 generation into the old segment. */
1536 subseg_set (s_now, ss_now);
1537 }
1538
1539 \f
1540 /* Parsing and Idiom Translation. */
1541
1542 static const char *
1543 expression_end (name)
1544 const char *name;
1545 {
1546 while (1)
1547 {
1548 switch (*name)
1549 {
1550 case ';':
1551 case '\0':
1552 case ',':
1553 return name;
1554 case ' ':
1555 case '\t':
1556 ++name;
1557 continue;
1558 default:
1559 return 0;
1560 }
1561 }
1562 }
1563
1564
1565 #define ERROR_REG_NUM ((unsigned) -1)
1566
1567 static unsigned
1568 tc_get_register (prefix)
1569 const char *prefix;
1570 {
1571 unsigned reg;
1572 const char *next_expr;
1573 const char *old_line_pointer;
1574
1575 SKIP_WHITESPACE ();
1576 old_line_pointer = input_line_pointer;
1577
1578 if (*input_line_pointer == '$')
1579 ++input_line_pointer;
1580
1581 /* Accept "sp" as a synonym for "a1". */
1582 if (input_line_pointer[0] == 's' && input_line_pointer[1] == 'p'
1583 && expression_end (input_line_pointer + 2))
1584 {
1585 input_line_pointer += 2;
1586 return 1; /* AR[1] */
1587 }
1588
1589 while (*input_line_pointer++ == *prefix++)
1590 ;
1591 --input_line_pointer;
1592 --prefix;
1593
1594 if (*prefix)
1595 {
1596 as_bad (_("bad register name: %s"), old_line_pointer);
1597 return ERROR_REG_NUM;
1598 }
1599
1600 if (!ISDIGIT ((unsigned char) *input_line_pointer))
1601 {
1602 as_bad (_("bad register number: %s"), input_line_pointer);
1603 return ERROR_REG_NUM;
1604 }
1605
1606 reg = 0;
1607
1608 while (ISDIGIT ((int) *input_line_pointer))
1609 reg = reg * 10 + *input_line_pointer++ - '0';
1610
1611 if (!(next_expr = expression_end (input_line_pointer)))
1612 {
1613 as_bad (_("bad register name: %s"), old_line_pointer);
1614 return ERROR_REG_NUM;
1615 }
1616
1617 input_line_pointer = (char *) next_expr;
1618
1619 return reg;
1620 }
1621
1622
1623 #define PLT_SUFFIX "@PLT"
1624 #define plt_suffix "@plt"
1625
1626 static void
1627 expression_maybe_register (opnd, tok)
1628 xtensa_operand opnd;
1629 expressionS *tok;
1630 {
1631 char *kind = xtensa_operand_kind (opnd);
1632
1633 if ((strlen (kind) == 1)
1634 && (*kind == 'l' || *kind == 'L' || *kind == 'i' || *kind == 'r'))
1635 {
1636 segT t = expression (tok);
1637 if (t == absolute_section && operand_is_pcrel_label (opnd))
1638 {
1639 assert (tok->X_op == O_constant);
1640 tok->X_op = O_symbol;
1641 tok->X_add_symbol = &abs_symbol;
1642 }
1643 if (tok->X_op == O_symbol
1644 && (!strncmp (input_line_pointer, PLT_SUFFIX,
1645 strlen (PLT_SUFFIX) - 1)
1646 || !strncmp (input_line_pointer, plt_suffix,
1647 strlen (plt_suffix) - 1)))
1648 {
1649 symbol_get_tc (tok->X_add_symbol)->plt = 1;
1650 input_line_pointer += strlen (plt_suffix);
1651 }
1652 }
1653 else
1654 {
1655 unsigned reg = tc_get_register (kind);
1656
1657 if (reg != ERROR_REG_NUM) /* Already errored */
1658 {
1659 uint32 buf = reg;
1660 if ((xtensa_operand_encode (opnd, &buf) != xtensa_encode_result_ok)
1661 || (reg != xtensa_operand_decode (opnd, buf)))
1662 as_bad (_("register number out of range"));
1663 }
1664
1665 tok->X_op = O_register;
1666 tok->X_add_symbol = 0;
1667 tok->X_add_number = reg;
1668 }
1669 }
1670
1671
1672 /* Split up the arguments for an opcode or pseudo-op. */
1673
1674 static int
1675 tokenize_arguments (args, str)
1676 char **args;
1677 char *str;
1678 {
1679 char *old_input_line_pointer;
1680 bfd_boolean saw_comma = FALSE;
1681 bfd_boolean saw_arg = FALSE;
1682 int num_args = 0;
1683 char *arg_end, *arg;
1684 int arg_len;
1685
1686 /* Save and restore input_line_pointer around this function. */
1687 old_input_line_pointer = input_line_pointer;
1688 input_line_pointer = str;
1689
1690 while (*input_line_pointer)
1691 {
1692 SKIP_WHITESPACE ();
1693 switch (*input_line_pointer)
1694 {
1695 case '\0':
1696 goto fini;
1697
1698 case ',':
1699 input_line_pointer++;
1700 if (saw_comma || !saw_arg)
1701 goto err;
1702 saw_comma = TRUE;
1703 break;
1704
1705 default:
1706 if (!saw_comma && saw_arg)
1707 goto err;
1708
1709 arg_end = input_line_pointer + 1;
1710 while (!expression_end (arg_end))
1711 arg_end += 1;
1712
1713 arg_len = arg_end - input_line_pointer;
1714 arg = (char *) xmalloc (arg_len + 1);
1715 args[num_args] = arg;
1716
1717 strncpy (arg, input_line_pointer, arg_len);
1718 arg[arg_len] = '\0';
1719
1720 input_line_pointer = arg_end;
1721 num_args += 1;
1722 saw_comma = FALSE;
1723 saw_arg = TRUE;
1724 break;
1725 }
1726 }
1727
1728 fini:
1729 if (saw_comma)
1730 goto err;
1731 input_line_pointer = old_input_line_pointer;
1732 return num_args;
1733
1734 err:
1735 input_line_pointer = old_input_line_pointer;
1736 return -1;
1737 }
1738
1739
1740 /* Parse the arguments to an opcode. Return true on error. */
1741
1742 static bfd_boolean
1743 parse_arguments (insn, num_args, arg_strings)
1744 TInsn *insn;
1745 int num_args;
1746 char **arg_strings;
1747 {
1748 expressionS *tok = insn->tok;
1749 xtensa_opcode opcode = insn->opcode;
1750 bfd_boolean had_error = TRUE;
1751 xtensa_isa isa = xtensa_default_isa;
1752 int n;
1753 int opcode_operand_count;
1754 int actual_operand_count = 0;
1755 xtensa_operand opnd = NULL;
1756 char *old_input_line_pointer;
1757
1758 if (insn->insn_type == ITYPE_LITERAL)
1759 opcode_operand_count = 1;
1760 else
1761 opcode_operand_count = xtensa_num_operands (isa, opcode);
1762
1763 memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS);
1764
1765 /* Save and restore input_line_pointer around this function. */
1766 old_input_line_pointer = input_line_pointer;
1767
1768 for (n = 0; n < num_args; n++)
1769 {
1770 input_line_pointer = arg_strings[n];
1771
1772 if (actual_operand_count >= opcode_operand_count)
1773 {
1774 as_warn (_("too many arguments"));
1775 goto err;
1776 }
1777 assert (actual_operand_count < MAX_INSN_ARGS);
1778
1779 opnd = xtensa_get_operand (isa, opcode, actual_operand_count);
1780 expression_maybe_register (opnd, tok);
1781
1782 if (tok->X_op == O_illegal || tok->X_op == O_absent)
1783 goto err;
1784 actual_operand_count++;
1785 tok++;
1786 }
1787
1788 insn->ntok = tok - insn->tok;
1789 had_error = FALSE;
1790
1791 err:
1792 input_line_pointer = old_input_line_pointer;
1793 return had_error;
1794 }
1795
1796
1797 static void
1798 xg_reverse_shift_count (cnt_argp)
1799 char **cnt_argp;
1800 {
1801 char *cnt_arg, *new_arg;
1802 cnt_arg = *cnt_argp;
1803
1804 /* replace the argument with "31-(argument)" */
1805 new_arg = (char *) xmalloc (strlen (cnt_arg) + 6);
1806 sprintf (new_arg, "31-(%s)", cnt_arg);
1807
1808 free (cnt_arg);
1809 *cnt_argp = new_arg;
1810 }
1811
1812
1813 /* If "arg" is a constant expression, return non-zero with the value
1814 in *valp. */
1815
1816 static int
1817 xg_arg_is_constant (arg, valp)
1818 char *arg;
1819 offsetT *valp;
1820 {
1821 expressionS exp;
1822 char *save_ptr = input_line_pointer;
1823
1824 input_line_pointer = arg;
1825 expression (&exp);
1826 input_line_pointer = save_ptr;
1827
1828 if (exp.X_op == O_constant)
1829 {
1830 *valp = exp.X_add_number;
1831 return 1;
1832 }
1833
1834 return 0;
1835 }
1836
1837
1838 static void
1839 xg_replace_opname (popname, newop)
1840 char **popname;
1841 char *newop;
1842 {
1843 free (*popname);
1844 *popname = (char *) xmalloc (strlen (newop) + 1);
1845 strcpy (*popname, newop);
1846 }
1847
1848
1849 static int
1850 xg_check_num_args (pnum_args, expected_num, opname, arg_strings)
1851 int *pnum_args;
1852 int expected_num;
1853 char *opname;
1854 char **arg_strings;
1855 {
1856 int num_args = *pnum_args;
1857
1858 if (num_args < expected_num)
1859 {
1860 as_bad (_("not enough operands (%d) for '%s'; expected %d"),
1861 num_args, opname, expected_num);
1862 return -1;
1863 }
1864
1865 if (num_args > expected_num)
1866 {
1867 as_warn (_("too many operands (%d) for '%s'; expected %d"),
1868 num_args, opname, expected_num);
1869 while (num_args-- > expected_num)
1870 {
1871 free (arg_strings[num_args]);
1872 arg_strings[num_args] = 0;
1873 }
1874 *pnum_args = expected_num;
1875 return -1;
1876 }
1877
1878 return 0;
1879 }
1880
1881
1882 static int
1883 xg_translate_sysreg_op (popname, pnum_args, arg_strings)
1884 char **popname;
1885 int *pnum_args;
1886 char **arg_strings;
1887 {
1888 char *opname, *new_opname;
1889 offsetT val;
1890 bfd_boolean has_underbar = FALSE;
1891
1892 opname = *popname;
1893 if (*opname == '_')
1894 {
1895 has_underbar = TRUE;
1896 opname += 1;
1897 }
1898
1899 /* Opname == [rw]ur... */
1900
1901 if (opname[3] == '\0')
1902 {
1903 /* If the register is not specified as part of the opcode,
1904 then get it from the operand and move it to the opcode. */
1905
1906 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
1907 return -1;
1908
1909 if (!xg_arg_is_constant (arg_strings[1], &val))
1910 {
1911 as_bad (_("register number for `%s' is not a constant"), opname);
1912 return -1;
1913 }
1914 if ((unsigned) val > 255)
1915 {
1916 as_bad (_("register number (%ld) for `%s' is out of range"),
1917 val, opname);
1918 return -1;
1919 }
1920
1921 /* Remove the last argument, which is now part of the opcode. */
1922 free (arg_strings[1]);
1923 arg_strings[1] = 0;
1924 *pnum_args = 1;
1925
1926 /* Translate the opcode. */
1927 new_opname = (char *) xmalloc (8);
1928 sprintf (new_opname, "%s%cur%u", (has_underbar ? "_" : ""),
1929 opname[0], (unsigned) val);
1930 free (*popname);
1931 *popname = new_opname;
1932 }
1933
1934 return 0;
1935 }
1936
1937
1938 /* If the instruction is an idiom (i.e., a built-in macro), translate it.
1939 Returns non-zero if an error was found. */
1940
1941 static int
1942 xg_translate_idioms (popname, pnum_args, arg_strings)
1943 char **popname;
1944 int *pnum_args;
1945 char **arg_strings;
1946 {
1947 char *opname = *popname;
1948 bfd_boolean has_underbar = FALSE;
1949
1950 if (*opname == '_')
1951 {
1952 has_underbar = TRUE;
1953 opname += 1;
1954 }
1955
1956 if (strcmp (opname, "mov") == 0)
1957 {
1958 if (!has_underbar && code_density_available ())
1959 xg_replace_opname (popname, "mov.n");
1960 else
1961 {
1962 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
1963 return -1;
1964 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
1965 arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
1966 strcpy (arg_strings[2], arg_strings[1]);
1967 *pnum_args = 3;
1968 }
1969 return 0;
1970 }
1971
1972 if (strcmp (opname, "bbsi.l") == 0)
1973 {
1974 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
1975 return -1;
1976 xg_replace_opname (popname, (has_underbar ? "_bbsi" : "bbsi"));
1977 if (target_big_endian)
1978 xg_reverse_shift_count (&arg_strings[1]);
1979 return 0;
1980 }
1981
1982 if (strcmp (opname, "bbci.l") == 0)
1983 {
1984 if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
1985 return -1;
1986 xg_replace_opname (popname, (has_underbar ? "_bbci" : "bbci"));
1987 if (target_big_endian)
1988 xg_reverse_shift_count (&arg_strings[1]);
1989 return 0;
1990 }
1991
1992 if (strcmp (opname, "nop") == 0)
1993 {
1994 if (!has_underbar && code_density_available ())
1995 xg_replace_opname (popname, "nop.n");
1996 else
1997 {
1998 if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
1999 return -1;
2000 xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
2001 arg_strings[0] = (char *) xmalloc (3);
2002 arg_strings[1] = (char *) xmalloc (3);
2003 arg_strings[2] = (char *) xmalloc (3);
2004 strcpy (arg_strings[0], "a1");
2005 strcpy (arg_strings[1], "a1");
2006 strcpy (arg_strings[2], "a1");
2007 *pnum_args = 3;
2008 }
2009 return 0;
2010 }
2011
2012 if ((opname[0] == 'r' || opname[0] == 'w')
2013 && opname[1] == 'u'
2014 && opname[2] == 'r')
2015 return xg_translate_sysreg_op (popname, pnum_args, arg_strings);
2016
2017
2018 /* WIDENING DENSITY OPCODES
2019
2020 questionable relaxations (widening) from old "tai" idioms:
2021
2022 ADD.N --> ADD
2023 BEQZ.N --> BEQZ
2024 RET.N --> RET
2025 RETW.N --> RETW
2026 MOVI.N --> MOVI
2027 MOV.N --> MOV
2028 NOP.N --> NOP
2029
2030 Note: this incomplete list was imported to match the "tai"
2031 behavior; other density opcodes are not handled.
2032
2033 The xtensa-relax code may know how to do these but it doesn't do
2034 anything when these density opcodes appear inside a no-density
2035 region. Somehow GAS should either print an error when that happens
2036 or do the widening. The old "tai" behavior was to do the widening.
2037 For now, I'll make it widen but print a warning.
2038
2039 FIXME: GAS needs to detect density opcodes inside no-density
2040 regions and treat them as errors. This code should be removed
2041 when that is done. */
2042
2043 if (use_generics ()
2044 && !has_underbar
2045 && density_supported
2046 && !code_density_available ())
2047 {
2048 if (strcmp (opname, "add.n") == 0)
2049 xg_replace_opname (popname, "add");
2050
2051 else if (strcmp (opname, "beqz.n") == 0)
2052 xg_replace_opname (popname, "beqz");
2053
2054 else if (strcmp (opname, "ret.n") == 0)
2055 xg_replace_opname (popname, "ret");
2056
2057 else if (strcmp (opname, "retw.n") == 0)
2058 xg_replace_opname (popname, "retw");
2059
2060 else if (strcmp (opname, "movi.n") == 0)
2061 xg_replace_opname (popname, "movi");
2062
2063 else if (strcmp (opname, "mov.n") == 0)
2064 {
2065 if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
2066 return -1;
2067 xg_replace_opname (popname, "or");
2068 arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
2069 strcpy (arg_strings[2], arg_strings[1]);
2070 *pnum_args = 3;
2071 }
2072
2073 else if (strcmp (opname, "nop.n") == 0)
2074 {
2075 if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
2076 return -1;
2077 xg_replace_opname (popname, "or");
2078 arg_strings[0] = (char *) xmalloc (3);
2079 arg_strings[1] = (char *) xmalloc (3);
2080 arg_strings[2] = (char *) xmalloc (3);
2081 strcpy (arg_strings[0], "a1");
2082 strcpy (arg_strings[1], "a1");
2083 strcpy (arg_strings[2], "a1");
2084 *pnum_args = 3;
2085 }
2086 }
2087
2088 return 0;
2089 }
2090
2091 \f
2092 /* Functions for dealing with the Xtensa ISA. */
2093
2094 /* Return true if the given operand is an immed or target instruction,
2095 i.e., has a reloc associated with it. Currently, this is only true
2096 if the operand kind is "i, "l" or "L". */
2097
2098 static bfd_boolean
2099 operand_is_immed (opnd)
2100 xtensa_operand opnd;
2101 {
2102 const char *opkind = xtensa_operand_kind (opnd);
2103 if (opkind[0] == '\0' || opkind[1] != '\0')
2104 return FALSE;
2105 switch (opkind[0])
2106 {
2107 case 'i':
2108 case 'l':
2109 case 'L':
2110 return TRUE;
2111 }
2112 return FALSE;
2113 }
2114
2115
2116 /* Return true if the given operand is a pc-relative label. This is
2117 true for "l", "L", and "r" operand kinds. */
2118
2119 bfd_boolean
2120 operand_is_pcrel_label (opnd)
2121 xtensa_operand opnd;
2122 {
2123 const char *opkind = xtensa_operand_kind (opnd);
2124 if (opkind[0] == '\0' || opkind[1] != '\0')
2125 return FALSE;
2126 switch (opkind[0])
2127 {
2128 case 'r':
2129 case 'l':
2130 case 'L':
2131 return TRUE;
2132 }
2133 return FALSE;
2134 }
2135
2136
2137 /* Currently the assembler only allows us to use a single target per
2138 fragment. Because of this, only one operand for a given
2139 instruction may be symbolic. If there is an operand of kind "lrL",
2140 the last one is chosen. Otherwise, the result is the number of the
2141 last operand of type "i", and if there are none of those, we fail
2142 and return -1. */
2143
2144 int
2145 get_relaxable_immed (opcode)
2146 xtensa_opcode opcode;
2147 {
2148 int last_immed = -1;
2149 int noperands, opi;
2150 xtensa_operand operand;
2151
2152 if (opcode == XTENSA_UNDEFINED)
2153 return -1;
2154
2155 noperands = xtensa_num_operands (xtensa_default_isa, opcode);
2156 for (opi = noperands - 1; opi >= 0; opi--)
2157 {
2158 operand = xtensa_get_operand (xtensa_default_isa, opcode, opi);
2159 if (operand_is_pcrel_label (operand))
2160 return opi;
2161 if (last_immed == -1 && operand_is_immed (operand))
2162 last_immed = opi;
2163 }
2164 return last_immed;
2165 }
2166
2167
2168 xtensa_opcode
2169 get_opcode_from_buf (buf)
2170 const char *buf;
2171 {
2172 static xtensa_insnbuf insnbuf = NULL;
2173 xtensa_opcode opcode;
2174 xtensa_isa isa = xtensa_default_isa;
2175 if (!insnbuf)
2176 insnbuf = xtensa_insnbuf_alloc (isa);
2177
2178 xtensa_insnbuf_from_chars (isa, insnbuf, buf);
2179 opcode = xtensa_decode_insn (isa, insnbuf);
2180 return opcode;
2181 }
2182
2183
2184 static bfd_boolean
2185 is_direct_call_opcode (opcode)
2186 xtensa_opcode opcode;
2187 {
2188 if (opcode == XTENSA_UNDEFINED)
2189 return FALSE;
2190
2191 return (opcode == xtensa_call0_opcode
2192 || opcode == xtensa_call4_opcode
2193 || opcode == xtensa_call8_opcode
2194 || opcode == xtensa_call12_opcode);
2195 }
2196
2197
2198 static bfd_boolean
2199 is_call_opcode (opcode)
2200 xtensa_opcode opcode;
2201 {
2202 if (is_direct_call_opcode (opcode))
2203 return TRUE;
2204
2205 if (opcode == XTENSA_UNDEFINED)
2206 return FALSE;
2207
2208 return (opcode == xtensa_callx0_opcode
2209 || opcode == xtensa_callx4_opcode
2210 || opcode == xtensa_callx8_opcode
2211 || opcode == xtensa_callx12_opcode);
2212 }
2213
2214
2215 /* Return true if the opcode is an entry opcode. This is used because
2216 "entry" adds an implicit ".align 4" and also the entry instruction
2217 has an extra check for an operand value. */
2218
2219 static bfd_boolean
2220 is_entry_opcode (opcode)
2221 xtensa_opcode opcode;
2222 {
2223 if (opcode == XTENSA_UNDEFINED)
2224 return FALSE;
2225
2226 return (opcode == xtensa_entry_opcode);
2227 }
2228
2229
2230 /* Return true if it is one of the loop opcodes. Loops are special
2231 because they need automatic alignment and they have a relaxation so
2232 complex that we hard-coded it. */
2233
2234 static bfd_boolean
2235 is_loop_opcode (opcode)
2236 xtensa_opcode opcode;
2237 {
2238 if (opcode == XTENSA_UNDEFINED)
2239 return FALSE;
2240
2241 return (opcode == xtensa_loop_opcode
2242 || opcode == xtensa_loopnez_opcode
2243 || opcode == xtensa_loopgtz_opcode);
2244 }
2245
2246
2247 static bfd_boolean
2248 is_the_loop_opcode (opcode)
2249 xtensa_opcode opcode;
2250 {
2251 if (opcode == XTENSA_UNDEFINED)
2252 return FALSE;
2253
2254 return (opcode == xtensa_loop_opcode);
2255 }
2256
2257
2258 static bfd_boolean
2259 is_jx_opcode (opcode)
2260 xtensa_opcode opcode;
2261 {
2262 if (opcode == XTENSA_UNDEFINED)
2263 return FALSE;
2264
2265 return (opcode == xtensa_jx_opcode);
2266 }
2267
2268
2269 /* Return true if the opcode is a retw or retw.n.
2270 Needed to add nops to avoid a hardware interlock issue. */
2271
2272 static bfd_boolean
2273 is_windowed_return_opcode (opcode)
2274 xtensa_opcode opcode;
2275 {
2276 if (opcode == XTENSA_UNDEFINED)
2277 return FALSE;
2278
2279 return (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode);
2280 }
2281
2282
2283 /* Return true if the opcode type is "l" and the opcode is NOT a jump. */
2284
2285 static bfd_boolean
2286 is_conditional_branch_opcode (opcode)
2287 xtensa_opcode opcode;
2288 {
2289 xtensa_isa isa = xtensa_default_isa;
2290 int num_ops, i;
2291
2292 if (opcode == xtensa_j_opcode && opcode != XTENSA_UNDEFINED)
2293 return FALSE;
2294
2295 num_ops = xtensa_num_operands (isa, opcode);
2296 for (i = 0; i < num_ops; i++)
2297 {
2298 xtensa_operand operand = xtensa_get_operand (isa, opcode, i);
2299 if (strcmp (xtensa_operand_kind (operand), "l") == 0)
2300 return TRUE;
2301 }
2302 return FALSE;
2303 }
2304
2305
2306 /* Return true if the given opcode is a conditional branch
2307 instruction, i.e., currently this is true if the instruction
2308 is a jx or has an operand with 'l' type and is not a loop. */
2309
2310 bfd_boolean
2311 is_branch_or_jump_opcode (opcode)
2312 xtensa_opcode opcode;
2313 {
2314 int opn, op_count;
2315
2316 if (opcode == XTENSA_UNDEFINED)
2317 return FALSE;
2318
2319 if (is_loop_opcode (opcode))
2320 return FALSE;
2321
2322 if (is_jx_opcode (opcode))
2323 return TRUE;
2324
2325 op_count = xtensa_num_operands (xtensa_default_isa, opcode);
2326 for (opn = 0; opn < op_count; opn++)
2327 {
2328 xtensa_operand opnd =
2329 xtensa_get_operand (xtensa_default_isa, opcode, opn);
2330 const char *opkind = xtensa_operand_kind (opnd);
2331 if (opkind && opkind[0] == 'l' && opkind[1] == '\0')
2332 return TRUE;
2333 }
2334 return FALSE;
2335 }
2336
2337
2338 /* Convert from operand numbers to BFD relocation type code.
2339 Return BFD_RELOC_NONE on failure. */
2340
2341 bfd_reloc_code_real_type
2342 opnum_to_reloc (opnum)
2343 int opnum;
2344 {
2345 switch (opnum)
2346 {
2347 case 0:
2348 return BFD_RELOC_XTENSA_OP0;
2349 case 1:
2350 return BFD_RELOC_XTENSA_OP1;
2351 case 2:
2352 return BFD_RELOC_XTENSA_OP2;
2353 default:
2354 break;
2355 }
2356 return BFD_RELOC_NONE;
2357 }
2358
2359
2360 /* Convert from BFD relocation type code to operand number.
2361 Return -1 on failure. */
2362
2363 int
2364 reloc_to_opnum (reloc)
2365 bfd_reloc_code_real_type reloc;
2366 {
2367 switch (reloc)
2368 {
2369 case BFD_RELOC_XTENSA_OP0:
2370 return 0;
2371 case BFD_RELOC_XTENSA_OP1:
2372 return 1;
2373 case BFD_RELOC_XTENSA_OP2:
2374 return 2;
2375 default:
2376 break;
2377 }
2378 return -1;
2379 }
2380
2381
2382 static void
2383 xtensa_insnbuf_set_operand (insnbuf, opcode, operand, value, file, line)
2384 xtensa_insnbuf insnbuf;
2385 xtensa_opcode opcode;
2386 xtensa_operand operand;
2387 int32 value;
2388 const char *file;
2389 unsigned int line;
2390 {
2391 xtensa_encode_result encode_result;
2392 uint32 valbuf = value;
2393
2394 encode_result = xtensa_operand_encode (operand, &valbuf);
2395
2396 switch (encode_result)
2397 {
2398 case xtensa_encode_result_ok:
2399 break;
2400 case xtensa_encode_result_align:
2401 as_bad_where ((char *) file, line,
2402 _("operand %d not properly aligned for '%s'"),
2403 value, xtensa_opcode_name (xtensa_default_isa, opcode));
2404 break;
2405 case xtensa_encode_result_not_in_table:
2406 as_bad_where ((char *) file, line,
2407 _("operand %d not in immediate table for '%s'"),
2408 value, xtensa_opcode_name (xtensa_default_isa, opcode));
2409 break;
2410 case xtensa_encode_result_too_high:
2411 as_bad_where ((char *) file, line,
2412 _("operand %d too large for '%s'"), value,
2413 xtensa_opcode_name (xtensa_default_isa, opcode));
2414 break;
2415 case xtensa_encode_result_too_low:
2416 as_bad_where ((char *) file, line,
2417 _("operand %d too small for '%s'"), value,
2418 xtensa_opcode_name (xtensa_default_isa, opcode));
2419 break;
2420 case xtensa_encode_result_not_ok:
2421 as_bad_where ((char *) file, line,
2422 _("operand %d is invalid for '%s'"), value,
2423 xtensa_opcode_name (xtensa_default_isa, opcode));
2424 break;
2425 default:
2426 abort ();
2427 }
2428
2429 xtensa_operand_set_field (operand, insnbuf, valbuf);
2430 }
2431
2432
2433 static uint32
2434 xtensa_insnbuf_get_operand (insnbuf, opcode, opnum)
2435 xtensa_insnbuf insnbuf;
2436 xtensa_opcode opcode;
2437 int opnum;
2438 {
2439 xtensa_operand op = xtensa_get_operand (xtensa_default_isa, opcode, opnum);
2440 return xtensa_operand_decode (op, xtensa_operand_get_field (op, insnbuf));
2441 }
2442
2443
2444 static void
2445 xtensa_insnbuf_set_immediate_field (opcode, insnbuf, value, file, line)
2446 xtensa_opcode opcode;
2447 xtensa_insnbuf insnbuf;
2448 int32 value;
2449 const char *file;
2450 unsigned int line;
2451 {
2452 xtensa_isa isa = xtensa_default_isa;
2453 int last_opnd = xtensa_num_operands (isa, opcode) - 1;
2454 xtensa_operand operand = xtensa_get_operand (isa, opcode, last_opnd);
2455 xtensa_insnbuf_set_operand (insnbuf, opcode, operand, value, file, line);
2456 }
2457
2458
2459 static bfd_boolean
2460 is_negatable_branch (insn)
2461 TInsn *insn;
2462 {
2463 xtensa_isa isa = xtensa_default_isa;
2464 int i;
2465 int num_ops = xtensa_num_operands (isa, insn->opcode);
2466
2467 for (i = 0; i < num_ops; i++)
2468 {
2469 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
2470 char *kind = xtensa_operand_kind (opnd);
2471 if (strlen (kind) == 1 && *kind == 'l')
2472 return TRUE;
2473 }
2474 return FALSE;
2475 }
2476
2477 \f
2478 /* Lists for recording various properties of symbols. */
2479
2480 typedef struct symbol_consS_struct
2481 {
2482 symbolS *first;
2483 /* These are used for the target taken. */
2484 int is_loop_target:1;
2485 int is_branch_target:1;
2486 int is_literal:1;
2487 int is_moved:1;
2488 struct symbol_consS_struct *rest;
2489 } symbol_consS;
2490
2491 symbol_consS *defined_symbols = 0;
2492 symbol_consS *branch_targets = 0;
2493
2494
2495 static void
2496 xtensa_define_label (sym)
2497 symbolS *sym;
2498 {
2499 symbol_consS *cons = (symbol_consS *) xmalloc (sizeof (symbol_consS));
2500
2501 cons->first = sym;
2502 cons->is_branch_target = 0;
2503 cons->is_loop_target = 0;
2504 cons->is_literal = generating_literals ? 1 : 0;
2505 cons->is_moved = 0;
2506 cons->rest = defined_symbols;
2507 defined_symbols = cons;
2508 }
2509
2510
2511 void
2512 add_target_symbol (sym, is_loop)
2513 symbolS *sym;
2514 bfd_boolean is_loop;
2515 {
2516 symbol_consS *cons, *sym_e;
2517
2518 for (sym_e = branch_targets; sym_e; sym_e = sym_e->rest)
2519 {
2520 if (sym_e->first == sym)
2521 {
2522 if (is_loop)
2523 sym_e->is_loop_target = 1;
2524 else
2525 sym_e->is_branch_target = 1;
2526 return;
2527 }
2528 }
2529
2530 cons = (symbol_consS *) xmalloc (sizeof (symbol_consS));
2531 cons->first = sym;
2532 cons->is_branch_target = (is_loop ? 0 : 1);
2533 cons->is_loop_target = (is_loop ? 1 : 0);
2534 cons->rest = branch_targets;
2535 branch_targets = cons;
2536 }
2537
2538
2539 /* Find the symbol at a given position. (Note: the "loops_ok"
2540 argument is provided to allow ignoring labels that define loop
2541 ends. This fixes a bug where the NOPs to align a loop opcode were
2542 included in a previous zero-cost loop:
2543
2544 loop a0, loopend
2545 <loop1 body>
2546 loopend:
2547
2548 loop a2, loopend2
2549 <loop2 body>
2550
2551 would become:
2552
2553 loop a0, loopend
2554 <loop1 body>
2555 nop.n <===== bad!
2556 loopend:
2557
2558 loop a2, loopend2
2559 <loop2 body>
2560
2561 This argument is used to prevent moving the NOP to before the
2562 loop-end label, which is what you want in this special case.) */
2563
2564 static symbolS *
2565 xtensa_find_label (fragP, offset, loops_ok)
2566 fragS *fragP;
2567 offsetT offset;
2568 bfd_boolean loops_ok;
2569 {
2570 symbol_consS *consP;
2571
2572 for (consP = defined_symbols; consP; consP = consP->rest)
2573 {
2574 symbolS *symP = consP->first;
2575
2576 if (S_GET_SEGMENT (symP) == now_seg
2577 && symbol_get_frag (symP) == fragP
2578 && symbol_constant_p (symP)
2579 && S_GET_VALUE (symP) == fragP->fr_address + (unsigned) offset
2580 && (loops_ok || !is_loop_target_label (symP)))
2581 return symP;
2582 }
2583 return NULL;
2584 }
2585
2586
2587 static void
2588 map_over_defined_symbols (fn)
2589 void (*fn) PARAMS ((symbolS *));
2590 {
2591 symbol_consS *sym_cons;
2592
2593 for (sym_cons = defined_symbols; sym_cons; sym_cons = sym_cons->rest)
2594 fn (sym_cons->first);
2595 }
2596
2597
2598 static bfd_boolean
2599 is_loop_target_label (sym)
2600 symbolS *sym;
2601 {
2602 symbol_consS *sym_e;
2603
2604 for (sym_e = branch_targets; sym_e; sym_e = sym_e->rest)
2605 {
2606 if (sym_e->first == sym)
2607 return sym_e->is_loop_target;
2608 }
2609 return FALSE;
2610 }
2611
2612
2613 /* Walk over all of the symbols that are branch target labels and
2614 loop target labels. Mark the associated fragments for these with
2615 the appropriate flags. */
2616
2617 static void
2618 xtensa_mark_target_fragments ()
2619 {
2620 symbol_consS *sym_e;
2621
2622 for (sym_e = branch_targets; sym_e; sym_e = sym_e->rest)
2623 {
2624 symbolS *sym = sym_e->first;
2625
2626 if (symbol_get_frag (sym)
2627 && symbol_constant_p (sym)
2628 && S_GET_VALUE (sym) == 0)
2629 {
2630 if (sym_e->is_branch_target)
2631 symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
2632 if (sym_e->is_loop_target)
2633 symbol_get_frag (sym)->tc_frag_data.is_loop_target = TRUE;
2634 }
2635 }
2636 }
2637
2638 \f
2639 /* Various Other Internal Functions. */
2640
2641 static bfd_boolean
2642 is_unique_insn_expansion (r)
2643 TransitionRule *r;
2644 {
2645 if (!r->to_instr || r->to_instr->next != NULL)
2646 return FALSE;
2647 if (r->to_instr->typ != INSTR_INSTR)
2648 return FALSE;
2649 return TRUE;
2650 }
2651
2652
2653 static int
2654 xg_get_insn_size (insn)
2655 TInsn *insn;
2656 {
2657 assert (insn->insn_type == ITYPE_INSN);
2658 return xtensa_insn_length (xtensa_default_isa, insn->opcode);
2659 }
2660
2661
2662 static int
2663 xg_get_build_instr_size (insn)
2664 BuildInstr *insn;
2665 {
2666 assert (insn->typ == INSTR_INSTR);
2667 return xtensa_insn_length (xtensa_default_isa, insn->opcode);
2668 }
2669
2670
2671 bfd_boolean
2672 xg_is_narrow_insn (insn)
2673 TInsn *insn;
2674 {
2675 TransitionTable *table = xg_build_widen_table ();
2676 TransitionList *l;
2677 int num_match = 0;
2678 assert (insn->insn_type == ITYPE_INSN);
2679 assert (insn->opcode < table->num_opcodes);
2680
2681 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2682 {
2683 TransitionRule *rule = l->rule;
2684
2685 if (xg_instruction_matches_rule (insn, rule)
2686 && is_unique_insn_expansion (rule))
2687 {
2688 /* It only generates one instruction... */
2689 assert (insn->insn_type == ITYPE_INSN);
2690 /* ...and it is a larger instruction. */
2691 if (xg_get_insn_size (insn)
2692 < xg_get_build_instr_size (rule->to_instr))
2693 {
2694 num_match++;
2695 if (num_match > 1)
2696 return FALSE;
2697 }
2698 }
2699 }
2700 return (num_match == 1);
2701 }
2702
2703
2704 bfd_boolean
2705 xg_is_single_relaxable_insn (insn)
2706 TInsn *insn;
2707 {
2708 TransitionTable *table = xg_build_widen_table ();
2709 TransitionList *l;
2710 int num_match = 0;
2711 assert (insn->insn_type == ITYPE_INSN);
2712 assert (insn->opcode < table->num_opcodes);
2713
2714 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2715 {
2716 TransitionRule *rule = l->rule;
2717
2718 if (xg_instruction_matches_rule (insn, rule)
2719 && is_unique_insn_expansion (rule))
2720 {
2721 assert (insn->insn_type == ITYPE_INSN);
2722 /* ... and it is a larger instruction. */
2723 if (xg_get_insn_size (insn)
2724 <= xg_get_build_instr_size (rule->to_instr))
2725 {
2726 num_match++;
2727 if (num_match > 1)
2728 return FALSE;
2729 }
2730 }
2731 }
2732 return (num_match == 1);
2733 }
2734
2735
2736 /* Return the largest size instruction that this instruction can
2737 expand to. Currently, in all cases, this is 3 bytes. Of course we
2738 could just calculate this once and generate a table. */
2739
2740 int
2741 xg_get_max_narrow_insn_size (opcode)
2742 xtensa_opcode opcode;
2743 {
2744 /* Go ahead and compute it, but it better be 3. */
2745 TransitionTable *table = xg_build_widen_table ();
2746 TransitionList *l;
2747 int old_size = xtensa_insn_length (xtensa_default_isa, opcode);
2748 assert (opcode < table->num_opcodes);
2749
2750 /* Actually we can do better. Check to see of Only one applies. */
2751 for (l = table->table[opcode]; l != NULL; l = l->next)
2752 {
2753 TransitionRule *rule = l->rule;
2754
2755 /* If it only generates one instruction. */
2756 if (is_unique_insn_expansion (rule))
2757 {
2758 int new_size = xtensa_insn_length (xtensa_default_isa,
2759 rule->to_instr->opcode);
2760 if (new_size > old_size)
2761 {
2762 assert (new_size == 3);
2763 return 3;
2764 }
2765 }
2766 }
2767 return old_size;
2768 }
2769
2770
2771 /* Return the maximum number of bytes this opcode can expand to. */
2772
2773 int
2774 xg_get_max_insn_widen_size (opcode)
2775 xtensa_opcode opcode;
2776 {
2777 TransitionTable *table = xg_build_widen_table ();
2778 TransitionList *l;
2779 int max_size = xtensa_insn_length (xtensa_default_isa, opcode);
2780
2781 assert (opcode < table->num_opcodes);
2782
2783 for (l = table->table[opcode]; l != NULL; l = l->next)
2784 {
2785 TransitionRule *rule = l->rule;
2786 BuildInstr *build_list;
2787 int this_size = 0;
2788
2789 if (!rule)
2790 continue;
2791 build_list = rule->to_instr;
2792 if (is_unique_insn_expansion (rule))
2793 {
2794 assert (build_list->typ == INSTR_INSTR);
2795 this_size = xg_get_max_insn_widen_size (build_list->opcode);
2796 }
2797 else
2798 for (; build_list != NULL; build_list = build_list->next)
2799 {
2800 switch (build_list->typ)
2801 {
2802 case INSTR_INSTR:
2803 this_size += xtensa_insn_length (xtensa_default_isa,
2804 build_list->opcode);
2805
2806 break;
2807 case INSTR_LITERAL_DEF:
2808 case INSTR_LABEL_DEF:
2809 default:
2810 break;
2811 }
2812 }
2813 if (this_size > max_size)
2814 max_size = this_size;
2815 }
2816 return max_size;
2817 }
2818
2819
2820 /* Return the maximum number of literal bytes this opcode can generate. */
2821
2822 int
2823 xg_get_max_insn_widen_literal_size (opcode)
2824 xtensa_opcode opcode;
2825 {
2826 TransitionTable *table = xg_build_widen_table ();
2827 TransitionList *l;
2828 int max_size = 0;
2829
2830 assert (opcode < table->num_opcodes);
2831
2832 for (l = table->table[opcode]; l != NULL; l = l->next)
2833 {
2834 TransitionRule *rule = l->rule;
2835 BuildInstr *build_list;
2836 int this_size = 0;
2837
2838 if (!rule)
2839 continue;
2840 build_list = rule->to_instr;
2841 if (is_unique_insn_expansion (rule))
2842 {
2843 assert (build_list->typ == INSTR_INSTR);
2844 this_size = xg_get_max_insn_widen_literal_size (build_list->opcode);
2845 }
2846 else
2847 for (; build_list != NULL; build_list = build_list->next)
2848 {
2849 switch (build_list->typ)
2850 {
2851 case INSTR_LITERAL_DEF:
2852 /* hard coded 4-byte literal. */
2853 this_size += 4;
2854 break;
2855 case INSTR_INSTR:
2856 case INSTR_LABEL_DEF:
2857 default:
2858 break;
2859 }
2860 }
2861 if (this_size > max_size)
2862 max_size = this_size;
2863 }
2864 return max_size;
2865 }
2866
2867
2868 bfd_boolean
2869 xg_is_relaxable_insn (insn, lateral_steps)
2870 TInsn *insn;
2871 int lateral_steps;
2872 {
2873 int steps_taken = 0;
2874 TransitionTable *table = xg_build_widen_table ();
2875 TransitionList *l;
2876
2877 assert (insn->insn_type == ITYPE_INSN);
2878 assert (insn->opcode < table->num_opcodes);
2879
2880 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
2881 {
2882 TransitionRule *rule = l->rule;
2883
2884 if (xg_instruction_matches_rule (insn, rule))
2885 {
2886 if (steps_taken == lateral_steps)
2887 return TRUE;
2888 steps_taken++;
2889 }
2890 }
2891 return FALSE;
2892 }
2893
2894
2895 static symbolS *
2896 get_special_literal_symbol ()
2897 {
2898 static symbolS *sym = NULL;
2899
2900 if (sym == NULL)
2901 sym = symbol_find_or_make ("SPECIAL_LITERAL0\001");
2902 return sym;
2903 }
2904
2905
2906 static symbolS *
2907 get_special_label_symbol ()
2908 {
2909 static symbolS *sym = NULL;
2910
2911 if (sym == NULL)
2912 sym = symbol_find_or_make ("SPECIAL_LABEL0\001");
2913 return sym;
2914 }
2915
2916
2917 /* Return true on success. */
2918
2919 bfd_boolean
2920 xg_build_to_insn (targ, insn, bi)
2921 TInsn *targ;
2922 TInsn *insn;
2923 BuildInstr *bi;
2924 {
2925 BuildOp *op;
2926 symbolS *sym;
2927
2928 memset (targ, 0, sizeof (TInsn));
2929 switch (bi->typ)
2930 {
2931 case INSTR_INSTR:
2932 op = bi->ops;
2933 targ->opcode = bi->opcode;
2934 targ->insn_type = ITYPE_INSN;
2935 targ->is_specific_opcode = FALSE;
2936
2937 for (; op != NULL; op = op->next)
2938 {
2939 int op_num = op->op_num;
2940 int op_data = op->op_data;
2941
2942 assert (op->op_num < MAX_INSN_ARGS);
2943
2944 if (targ->ntok <= op_num)
2945 targ->ntok = op_num + 1;
2946
2947 switch (op->typ)
2948 {
2949 case OP_CONSTANT:
2950 set_expr_const (&targ->tok[op_num], op_data);
2951 break;
2952 case OP_OPERAND:
2953 assert (op_data < insn->ntok);
2954 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
2955 break;
2956 case OP_LITERAL:
2957 sym = get_special_literal_symbol ();
2958 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
2959 break;
2960 case OP_LABEL:
2961 sym = get_special_label_symbol ();
2962 set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
2963 break;
2964 default:
2965 /* currently handles:
2966 OP_OPERAND_LOW8
2967 OP_OPERAND_HI24S
2968 OP_OPERAND_F32MINUS */
2969 if (xg_has_userdef_op_fn (op->typ))
2970 {
2971 assert (op_data < insn->ntok);
2972 if (expr_is_const (&insn->tok[op_data]))
2973 {
2974 long val;
2975 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
2976 val = xg_apply_userdef_op_fn (op->typ,
2977 targ->tok[op_num].
2978 X_add_number);
2979 targ->tok[op_num].X_add_number = val;
2980 }
2981 else
2982 return FALSE; /* We cannot use a relocation for this. */
2983 break;
2984 }
2985 assert (0);
2986 break;
2987 }
2988 }
2989 break;
2990
2991 case INSTR_LITERAL_DEF:
2992 op = bi->ops;
2993 targ->opcode = XTENSA_UNDEFINED;
2994 targ->insn_type = ITYPE_LITERAL;
2995 targ->is_specific_opcode = FALSE;
2996 for (; op != NULL; op = op->next)
2997 {
2998 int op_num = op->op_num;
2999 int op_data = op->op_data;
3000 assert (op->op_num < MAX_INSN_ARGS);
3001
3002 if (targ->ntok <= op_num)
3003 targ->ntok = op_num + 1;
3004
3005 switch (op->typ)
3006 {
3007 case OP_OPERAND:
3008 assert (op_data < insn->ntok);
3009 copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
3010 break;
3011 case OP_LITERAL:
3012 case OP_CONSTANT:
3013 case OP_LABEL:
3014 default:
3015 assert (0);
3016 break;
3017 }
3018 }
3019 break;
3020
3021 case INSTR_LABEL_DEF:
3022 op = bi->ops;
3023 targ->opcode = XTENSA_UNDEFINED;
3024 targ->insn_type = ITYPE_LABEL;
3025 targ->is_specific_opcode = FALSE;
3026 /* Literal with no ops. is a label? */
3027 assert (op == NULL);
3028 break;
3029
3030 default:
3031 assert (0);
3032 }
3033
3034 return TRUE;
3035 }
3036
3037
3038 /* Return true on success. */
3039
3040 bfd_boolean
3041 xg_build_to_stack (istack, insn, bi)
3042 IStack *istack;
3043 TInsn *insn;
3044 BuildInstr *bi;
3045 {
3046 for (; bi != NULL; bi = bi->next)
3047 {
3048 TInsn *next_insn = istack_push_space (istack);
3049
3050 if (!xg_build_to_insn (next_insn, insn, bi))
3051 return FALSE;
3052 }
3053 return TRUE;
3054 }
3055
3056
3057 /* Return true on valid expansion. */
3058
3059 bfd_boolean
3060 xg_expand_to_stack (istack, insn, lateral_steps)
3061 IStack *istack;
3062 TInsn *insn;
3063 int lateral_steps;
3064 {
3065 int stack_size = istack->ninsn;
3066 int steps_taken = 0;
3067 TransitionTable *table = xg_build_widen_table ();
3068 TransitionList *l;
3069
3070 assert (insn->insn_type == ITYPE_INSN);
3071 assert (insn->opcode < table->num_opcodes);
3072
3073 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3074 {
3075 TransitionRule *rule = l->rule;
3076
3077 if (xg_instruction_matches_rule (insn, rule))
3078 {
3079 if (lateral_steps == steps_taken)
3080 {
3081 int i;
3082
3083 /* This is it. Expand the rule to the stack. */
3084 if (!xg_build_to_stack (istack, insn, rule->to_instr))
3085 return FALSE;
3086
3087 /* Check to see if it fits. */
3088 for (i = stack_size; i < istack->ninsn; i++)
3089 {
3090 TInsn *insn = &istack->insn[i];
3091
3092 if (insn->insn_type == ITYPE_INSN
3093 && !tinsn_has_symbolic_operands (insn)
3094 && !xg_immeds_fit (insn))
3095 {
3096 istack->ninsn = stack_size;
3097 return FALSE;
3098 }
3099 }
3100 return TRUE;
3101 }
3102 steps_taken++;
3103 }
3104 }
3105 return FALSE;
3106 }
3107
3108
3109 bfd_boolean
3110 xg_expand_narrow (targ, insn)
3111 TInsn *targ;
3112 TInsn *insn;
3113 {
3114 TransitionTable *table = xg_build_widen_table ();
3115 TransitionList *l;
3116
3117 assert (insn->insn_type == ITYPE_INSN);
3118 assert (insn->opcode < table->num_opcodes);
3119
3120 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3121 {
3122 TransitionRule *rule = l->rule;
3123 if (xg_instruction_matches_rule (insn, rule)
3124 && is_unique_insn_expansion (rule))
3125 {
3126 /* Is it a larger instruction? */
3127 if (xg_get_insn_size (insn)
3128 <= xg_get_build_instr_size (rule->to_instr))
3129 {
3130 xg_build_to_insn (targ, insn, rule->to_instr);
3131 return FALSE;
3132 }
3133 }
3134 }
3135 return TRUE;
3136 }
3137
3138
3139 /* Assumes: All immeds are constants. Check that all constants fit
3140 into their immeds; return false if not. */
3141
3142 static bfd_boolean
3143 xg_immeds_fit (insn)
3144 const TInsn *insn;
3145 {
3146 int i;
3147
3148 int n = insn->ntok;
3149 assert (insn->insn_type == ITYPE_INSN);
3150 for (i = 0; i < n; ++i)
3151 {
3152 const expressionS *expr = &insn->tok[i];
3153 xtensa_operand opnd = xtensa_get_operand (xtensa_default_isa,
3154 insn->opcode, i);
3155 if (!operand_is_immed (opnd))
3156 continue;
3157
3158 switch (expr->X_op)
3159 {
3160 case O_register:
3161 case O_constant:
3162 {
3163 if (xg_check_operand (expr->X_add_number, opnd))
3164 return FALSE;
3165 }
3166 break;
3167 default:
3168 /* The symbol should have a fixup associated with it. */
3169 assert (FALSE);
3170 break;
3171 }
3172 }
3173 return TRUE;
3174 }
3175
3176
3177 /* This should only be called after we have an initial
3178 estimate of the addresses. */
3179
3180 static bfd_boolean
3181 xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
3182 const TInsn *insn;
3183 segT pc_seg;
3184 fragS *pc_frag;
3185 offsetT pc_offset;
3186 long stretch;
3187 {
3188 symbolS *symbolP;
3189 offsetT target, pc, new_offset;
3190 int i;
3191 int n = insn->ntok;
3192
3193 assert (insn->insn_type == ITYPE_INSN);
3194
3195 for (i = 0; i < n; ++i)
3196 {
3197 const expressionS *expr = &insn->tok[i];
3198 xtensa_operand opnd = xtensa_get_operand (xtensa_default_isa,
3199 insn->opcode, i);
3200 if (!operand_is_immed (opnd))
3201 continue;
3202
3203 switch (expr->X_op)
3204 {
3205 case O_register:
3206 case O_constant:
3207 if (xg_check_operand (expr->X_add_number, opnd))
3208 return FALSE;
3209 break;
3210
3211 case O_symbol:
3212 /* We only allow symbols for pc-relative stuff.
3213 If pc_frag == 0, then we don't have frag locations yet. */
3214 if (pc_frag == 0)
3215 return FALSE;
3216
3217 /* If it is PC-relative and the symbol is in the same segment as
3218 the PC.... */
3219 if (!xtensa_operand_isPCRelative (opnd)
3220 || S_GET_SEGMENT (expr->X_add_symbol) != pc_seg)
3221 return FALSE;
3222
3223 symbolP = expr->X_add_symbol;
3224 target = S_GET_VALUE (symbolP) + expr->X_add_number;
3225 pc = pc_frag->fr_address + pc_offset;
3226
3227 /* If frag has yet to be reached on this pass, assume it
3228 will move by STRETCH just as we did. If this is not so,
3229 it will be because some frag between grows, and that will
3230 force another pass. Beware zero-length frags. There
3231 should be a faster way to do this. */
3232
3233 if (stretch && is_dnrange (pc_frag, symbolP, stretch))
3234 target += stretch;
3235
3236 new_offset = xtensa_operand_do_reloc (opnd, target, pc);
3237 if (xg_check_operand (new_offset, opnd))
3238 return FALSE;
3239 break;
3240
3241 default:
3242 /* The symbol should have a fixup associated with it. */
3243 return FALSE;
3244 }
3245 }
3246
3247 return TRUE;
3248 }
3249
3250
3251 /* This will check to see if the value can be converted into the
3252 operand type. It will return true if it does not fit. */
3253
3254 static bfd_boolean
3255 xg_check_operand (value, operand)
3256 int32 value;
3257 xtensa_operand operand;
3258 {
3259 uint32 valbuf = value;
3260 return (xtensa_operand_encode (operand, &valbuf) != xtensa_encode_result_ok);
3261 }
3262
3263
3264 /* Check if a symbol is pointing to somewhere after
3265 the start frag, given that the segment has stretched
3266 by stretch during relaxation.
3267
3268 This is more complicated than it might appear at first blush
3269 because of the stretching that goes on. Here is how the check
3270 works:
3271
3272 If the symbol and the frag are in the same segment, then
3273 the symbol could be down range. Note that this function
3274 assumes that start_frag is in now_seg.
3275
3276 If the symbol is pointing to a frag with an address greater than
3277 than the start_frag's address, then it _could_ be down range.
3278
3279 The problem comes because target_frag may or may not have had
3280 stretch bytes added to its address already, depending on if it is
3281 before or after start frag. (And if we knew that, then we wouldn't
3282 need this function.) start_frag has definitely already had stretch
3283 bytes added to its address.
3284
3285 If target_frag's address hasn't been adjusted yet, then to
3286 determine if it comes after start_frag, we need to subtract
3287 stretch from start_frag's address.
3288
3289 If target_frag's address has been adjusted, then it might have
3290 been adjusted such that it comes after start_frag's address minus
3291 stretch bytes.
3292
3293 So, in that case, we scan for it down stream to within
3294 stretch bytes. We could search to the end of the fr_chain, but
3295 that ends up taking too much time (over a minute on some gnu
3296 tests). */
3297
3298 int
3299 is_dnrange (start_frag, sym, stretch)
3300 fragS *start_frag;
3301 symbolS *sym;
3302 long stretch;
3303 {
3304 if (S_GET_SEGMENT (sym) == now_seg)
3305 {
3306 fragS *cur_frag = symbol_get_frag (sym);
3307
3308 if (cur_frag->fr_address >= start_frag->fr_address - stretch)
3309 {
3310 int distance = stretch;
3311
3312 while (cur_frag && distance >= 0)
3313 {
3314 distance -= cur_frag->fr_fix;
3315 if (cur_frag == start_frag)
3316 return 0;
3317 cur_frag = cur_frag->fr_next;
3318 }
3319 return 1;
3320 }
3321 }
3322 return 0;
3323 }
3324
3325 \f
3326 /* Relax the assembly instruction at least "min_steps".
3327 Return the number of steps taken. */
3328
3329 int
3330 xg_assembly_relax (istack, insn, pc_seg, pc_frag, pc_offset, min_steps,
3331 stretch)
3332 IStack *istack;
3333 TInsn *insn;
3334 segT pc_seg;
3335 fragS *pc_frag; /* If pc_frag == 0, then no pc-relative. */
3336 offsetT pc_offset; /* Offset in fragment. */
3337 int min_steps; /* Minimum number of conversion steps. */
3338 long stretch; /* Number of bytes stretched so far. */
3339 {
3340 int steps_taken = 0;
3341
3342 /* assert (has no symbolic operands)
3343 Some of its immeds don't fit.
3344 Try to build a relaxed version.
3345 This may go through a couple of stages
3346 of single instruction transformations before
3347 we get there. */
3348
3349 TInsn single_target;
3350 TInsn current_insn;
3351 int lateral_steps = 0;
3352 int istack_size = istack->ninsn;
3353
3354 if (xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
3355 && steps_taken >= min_steps)
3356 {
3357 istack_push (istack, insn);
3358 return steps_taken;
3359 }
3360 tinsn_copy (&current_insn, insn);
3361
3362 /* Walk through all of the single instruction expansions. */
3363 while (xg_is_single_relaxable_insn (&current_insn))
3364 {
3365 int error_val = xg_expand_narrow (&single_target, &current_insn);
3366
3367 assert (!error_val);
3368
3369 if (xg_symbolic_immeds_fit (&single_target, pc_seg, pc_frag, pc_offset,
3370 stretch))
3371 {
3372 steps_taken++;
3373 if (steps_taken >= min_steps)
3374 {
3375 istack_push (istack, &single_target);
3376 return steps_taken;
3377 }
3378 }
3379 tinsn_copy (&current_insn, &single_target);
3380 }
3381
3382 /* Now check for a multi-instruction expansion. */
3383 while (xg_is_relaxable_insn (&current_insn, lateral_steps))
3384 {
3385 if (xg_symbolic_immeds_fit (&current_insn, pc_seg, pc_frag, pc_offset,
3386 stretch))
3387 {
3388 if (steps_taken >= min_steps)
3389 {
3390 istack_push (istack, &current_insn);
3391 return steps_taken;
3392 }
3393 }
3394 steps_taken++;
3395 if (xg_expand_to_stack (istack, &current_insn, lateral_steps))
3396 {
3397 if (steps_taken >= min_steps)
3398 return steps_taken;
3399 }
3400 lateral_steps++;
3401 istack->ninsn = istack_size;
3402 }
3403
3404 /* It's not going to work -- use the original. */
3405 istack_push (istack, insn);
3406 return steps_taken;
3407 }
3408
3409
3410 static void
3411 xg_force_frag_space (size)
3412 int size;
3413 {
3414 /* This may have the side effect of creating a new fragment for the
3415 space to go into. I just do not like the name of the "frag"
3416 functions. */
3417 frag_grow (size);
3418 }
3419
3420
3421 void
3422 xg_finish_frag (last_insn, state, max_growth, is_insn)
3423 char *last_insn;
3424 enum xtensa_relax_statesE state;
3425 int max_growth;
3426 bfd_boolean is_insn;
3427 {
3428 /* Finish off this fragment so that it has at LEAST the desired
3429 max_growth. If it doesn't fit in this fragment, close this one
3430 and start a new one. In either case, return a pointer to the
3431 beginning of the growth area. */
3432
3433 fragS *old_frag;
3434 xg_force_frag_space (max_growth);
3435
3436 old_frag = frag_now;
3437
3438 frag_now->fr_opcode = last_insn;
3439 if (is_insn)
3440 frag_now->tc_frag_data.is_insn = TRUE;
3441
3442 frag_var (rs_machine_dependent, max_growth, max_growth,
3443 state, frag_now->fr_symbol, frag_now->fr_offset, last_insn);
3444
3445 /* Just to make sure that we did not split it up. */
3446 assert (old_frag->fr_next == frag_now);
3447 }
3448
3449
3450 static bfd_boolean
3451 is_branch_jmp_to_next (insn, fragP)
3452 TInsn *insn;
3453 fragS *fragP;
3454 {
3455 xtensa_isa isa = xtensa_default_isa;
3456 int i;
3457 int num_ops = xtensa_num_operands (isa, insn->opcode);
3458 int target_op = -1;
3459 symbolS *sym;
3460 fragS *target_frag;
3461
3462 if (is_loop_opcode (insn->opcode))
3463 return FALSE;
3464
3465 for (i = 0; i < num_ops; i++)
3466 {
3467 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
3468 char *kind = xtensa_operand_kind (opnd);
3469 if (strlen (kind) == 1 && *kind == 'l')
3470 {
3471 target_op = i;
3472 break;
3473 }
3474 }
3475 if (target_op == -1)
3476 return FALSE;
3477
3478 if (insn->ntok <= target_op)
3479 return FALSE;
3480
3481 if (insn->tok[target_op].X_op != O_symbol)
3482 return FALSE;
3483
3484 sym = insn->tok[target_op].X_add_symbol;
3485 if (sym == NULL)
3486 return FALSE;
3487
3488 if (insn->tok[target_op].X_add_number != 0)
3489 return FALSE;
3490
3491 target_frag = symbol_get_frag (sym);
3492 if (target_frag == NULL)
3493 return FALSE;
3494
3495 if (is_next_frag_target (fragP->fr_next, target_frag)
3496 && S_GET_VALUE (sym) == target_frag->fr_address)
3497 return TRUE;
3498
3499 return FALSE;
3500 }
3501
3502
3503 static void
3504 xg_add_branch_and_loop_targets (insn)
3505 TInsn *insn;
3506 {
3507 xtensa_isa isa = xtensa_default_isa;
3508 int num_ops = xtensa_num_operands (isa, insn->opcode);
3509
3510 if (is_loop_opcode (insn->opcode))
3511 {
3512 int i = 1;
3513 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
3514 char *kind = xtensa_operand_kind (opnd);
3515 if (strlen (kind) == 1 && *kind == 'l')
3516 if (insn->tok[i].X_op == O_symbol)
3517 add_target_symbol (insn->tok[i].X_add_symbol, TRUE);
3518 return;
3519 }
3520
3521 /* Currently, we do not add branch targets. This is an optimization
3522 for later that tries to align only branch targets, not just any
3523 label in a text section. */
3524
3525 if (align_only_targets)
3526 {
3527 int i;
3528
3529 for (i = 0; i < insn->ntok && i < num_ops; i++)
3530 {
3531 xtensa_operand opnd = xtensa_get_operand (isa, insn->opcode, i);
3532 char *kind = xtensa_operand_kind (opnd);
3533 if (strlen (kind) == 1 && *kind == 'l'
3534 && insn->tok[i].X_op == O_symbol)
3535 add_target_symbol (insn->tok[i].X_add_symbol, FALSE);
3536 }
3537 }
3538 }
3539
3540
3541 /* Return the transition rule that matches or NULL if none matches. */
3542
3543 bfd_boolean
3544 xg_instruction_matches_rule (insn, rule)
3545 TInsn *insn;
3546 TransitionRule *rule;
3547 {
3548 PreconditionList *condition_l;
3549
3550 if (rule->opcode != insn->opcode)
3551 return FALSE;
3552
3553 for (condition_l = rule->conditions;
3554 condition_l != NULL;
3555 condition_l = condition_l->next)
3556 {
3557 expressionS *exp1;
3558 expressionS *exp2;
3559 Precondition *cond = condition_l->precond;
3560
3561 switch (cond->typ)
3562 {
3563 case OP_CONSTANT:
3564 /* The expression must be the constant. */
3565 assert (cond->op_num < insn->ntok);
3566 exp1 = &insn->tok[cond->op_num];
3567 if (!expr_is_const (exp1))
3568 return FALSE;
3569 switch (cond->cmp)
3570 {
3571 case OP_EQUAL:
3572 if (get_expr_const (exp1) != cond->op_data)
3573 return FALSE;
3574 break;
3575 case OP_NOTEQUAL:
3576 if (get_expr_const (exp1) == cond->op_data)
3577 return FALSE;
3578 break;
3579 }
3580 break;
3581
3582 case OP_OPERAND:
3583 assert (cond->op_num < insn->ntok);
3584 assert (cond->op_data < insn->ntok);
3585 exp1 = &insn->tok[cond->op_num];
3586 exp2 = &insn->tok[cond->op_data];
3587
3588 switch (cond->cmp)
3589 {
3590 case OP_EQUAL:
3591 if (!expr_is_equal (exp1, exp2))
3592 return FALSE;
3593 break;
3594 case OP_NOTEQUAL:
3595 if (expr_is_equal (exp1, exp2))
3596 return FALSE;
3597 break;
3598 }
3599 break;
3600
3601 case OP_LITERAL:
3602 case OP_LABEL:
3603 default:
3604 return FALSE;
3605 }
3606 }
3607 return TRUE;
3608 }
3609
3610
3611 TransitionRule *
3612 xg_instruction_match (insn)
3613 TInsn *insn;
3614 {
3615 TransitionTable *table = xg_build_simplify_table ();
3616 TransitionList *l;
3617 assert (insn->opcode < table->num_opcodes);
3618
3619 /* Walk through all of the possible transitions. */
3620 for (l = table->table[insn->opcode]; l != NULL; l = l->next)
3621 {
3622 TransitionRule *rule = l->rule;
3623 if (xg_instruction_matches_rule (insn, rule))
3624 return rule;
3625 }
3626 return NULL;
3627 }
3628
3629
3630 /* Return false if no error. */
3631
3632 bfd_boolean
3633 xg_build_token_insn (instr_spec, old_insn, new_insn)
3634 BuildInstr *instr_spec;
3635 TInsn *old_insn;
3636 TInsn *new_insn;
3637 {
3638 int num_ops = 0;
3639 BuildOp *b_op;
3640
3641 switch (instr_spec->typ)
3642 {
3643 case INSTR_INSTR:
3644 new_insn->insn_type = ITYPE_INSN;
3645 new_insn->opcode = instr_spec->opcode;
3646 new_insn->is_specific_opcode = FALSE;
3647 break;
3648 case INSTR_LITERAL_DEF:
3649 new_insn->insn_type = ITYPE_LITERAL;
3650 new_insn->opcode = XTENSA_UNDEFINED;
3651 new_insn->is_specific_opcode = FALSE;
3652 break;
3653 case INSTR_LABEL_DEF:
3654 as_bad (_("INSTR_LABEL_DEF not supported yet"));
3655 break;
3656 }
3657
3658 for (b_op = instr_spec->ops; b_op != NULL; b_op = b_op->next)
3659 {
3660 expressionS *exp;
3661 const expressionS *src_exp;
3662
3663 num_ops++;
3664 switch (b_op->typ)
3665 {
3666 case OP_CONSTANT:
3667 /* The expression must be the constant. */
3668 assert (b_op->op_num < MAX_INSN_ARGS);
3669 exp = &new_insn->tok[b_op->op_num];
3670 set_expr_const (exp, b_op->op_data);
3671 break;
3672
3673 case OP_OPERAND:
3674 assert (b_op->op_num < MAX_INSN_ARGS);
3675 assert (b_op->op_data < (unsigned) old_insn->ntok);
3676 src_exp = &old_insn->tok[b_op->op_data];
3677 exp = &new_insn->tok[b_op->op_num];
3678 copy_expr (exp, src_exp);
3679 break;
3680
3681 case OP_LITERAL:
3682 case OP_LABEL:
3683 as_bad (_("can't handle generation of literal/labels yet"));
3684 assert (0);
3685
3686 default:
3687 as_bad (_("can't handle undefined OP TYPE"));
3688 assert (0);
3689 }
3690 }
3691
3692 new_insn->ntok = num_ops;
3693 return FALSE;
3694 }
3695
3696
3697 /* Return true if it was simplified. */
3698
3699 bfd_boolean
3700 xg_simplify_insn (old_insn, new_insn)
3701 TInsn *old_insn;
3702 TInsn *new_insn;
3703 {
3704 TransitionRule *rule = xg_instruction_match (old_insn);
3705 BuildInstr *insn_spec;
3706 if (rule == NULL)
3707 return FALSE;
3708
3709 insn_spec = rule->to_instr;
3710 /* There should only be one. */
3711 assert (insn_spec != NULL);
3712 assert (insn_spec->next == NULL);
3713 if (insn_spec->next != NULL)
3714 return FALSE;
3715
3716 xg_build_token_insn (insn_spec, old_insn, new_insn);
3717
3718 return TRUE;
3719 }
3720
3721
3722 /* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i ->
3723 l32i.n. (2) Check the number of operands. (3) Place the instruction
3724 tokens into the stack or if we can relax it at assembly time, place
3725 multiple instructions/literals onto the stack. Return false if no
3726 error. */
3727
3728 static bfd_boolean
3729 xg_expand_assembly_insn (istack, orig_insn)
3730 IStack *istack;
3731 TInsn *orig_insn;
3732 {
3733 int noperands;
3734 TInsn new_insn;
3735 memset (&new_insn, 0, sizeof (TInsn));
3736
3737 /* On return, we will be using the "use_tokens" with "use_ntok".
3738 This will reduce things like addi to addi.n. */
3739 if (code_density_available () && !orig_insn->is_specific_opcode)
3740 {
3741 if (xg_simplify_insn (orig_insn, &new_insn))
3742 orig_insn = &new_insn;
3743 }
3744
3745 noperands = xtensa_num_operands (xtensa_default_isa, orig_insn->opcode);
3746 if (orig_insn->ntok < noperands)
3747 {
3748 as_bad (_("found %d operands for '%s': Expected %d"),
3749 orig_insn->ntok,
3750 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3751 noperands);
3752 return TRUE;
3753 }
3754 if (orig_insn->ntok > noperands)
3755 as_warn (_("found too many (%d) operands for '%s': Expected %d"),
3756 orig_insn->ntok,
3757 xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
3758 noperands);
3759
3760 /* If there are not enough operands, we will assert above. If there
3761 are too many, just cut out the extras here. */
3762
3763 orig_insn->ntok = noperands;
3764
3765 /* Cases:
3766
3767 Instructions with all constant immeds:
3768 Assemble them and relax the instruction if possible.
3769 Give error if not possible; no fixup needed.
3770
3771 Instructions with symbolic immeds:
3772 Assemble them with a Fix up (that may cause instruction expansion).
3773 Also close out the fragment if the fixup may cause instruction expansion.
3774
3775 There are some other special cases where we need alignment.
3776 1) before certain instructions with required alignment (OPCODE_ALIGN)
3777 2) before labels that have jumps (LABEL_ALIGN)
3778 3) after call instructions (RETURN_ALIGN)
3779 Multiple of these may be possible on the same fragment.
3780 If so, make sure to satisfy the required alignment.
3781 Then try to get the desired alignment. */
3782
3783 if (tinsn_has_invalid_symbolic_operands (orig_insn))
3784 return TRUE;
3785
3786 if (orig_insn->is_specific_opcode || !can_relax ())
3787 {
3788 istack_push (istack, orig_insn);
3789 return FALSE;
3790 }
3791
3792 if (tinsn_has_symbolic_operands (orig_insn))
3793 {
3794 if (tinsn_has_complex_operands (orig_insn))
3795 xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
3796 else
3797 istack_push (istack, orig_insn);
3798 }
3799 else
3800 {
3801 if (xg_immeds_fit (orig_insn))
3802 istack_push (istack, orig_insn);
3803 else
3804 xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
3805 }
3806
3807 #if 0
3808 for (i = 0; i < istack->ninsn; i++)
3809 {
3810 if (xg_simplify_insn (&new_insn, &istack->insn[i]))
3811 istack->insn[i] = new_insn;
3812 }
3813 #endif
3814
3815 return FALSE;
3816 }
3817
3818
3819 /* Currently all literals that are generated here are 32-bit L32R targets. */
3820
3821 symbolS *
3822 xg_assemble_literal (insn)
3823 /* const */ TInsn *insn;
3824 {
3825 emit_state state;
3826 symbolS *lit_sym = NULL;
3827
3828 /* size = 4 for L32R. It could easily be larger when we move to
3829 larger constants. Add a parameter later. */
3830 offsetT litsize = 4;
3831 offsetT litalign = 2; /* 2^2 = 4 */
3832 expressionS saved_loc;
3833 set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
3834
3835 assert (insn->insn_type == ITYPE_LITERAL);
3836 assert (insn->ntok = 1); /* must be only one token here */
3837
3838 xtensa_switch_to_literal_fragment (&state);
3839
3840 /* Force a 4-byte align here. Note that this opens a new frag, so all
3841 literals done with this function have a frag to themselves. That's
3842 important for the way text section literals work. */
3843 frag_align (litalign, 0, 0);
3844
3845 emit_expr (&insn->tok[0], litsize);
3846
3847 assert (frag_now->tc_frag_data.literal_frag == NULL);
3848 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
3849 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
3850 lit_sym = frag_now->fr_symbol;
3851 frag_now->tc_frag_data.is_literal = TRUE;
3852
3853 /* Go back. */
3854 xtensa_restore_emit_state (&state);
3855 return lit_sym;
3856 }
3857
3858
3859 static void
3860 xg_assemble_literal_space (size)
3861 /* const */ int size;
3862 {
3863 emit_state state;
3864 /* We might have to do something about this alignment. It only
3865 takes effect if something is placed here. */
3866 offsetT litalign = 2; /* 2^2 = 4 */
3867 fragS *lit_saved_frag;
3868
3869 expressionS saved_loc;
3870
3871 assert (size % 4 == 0);
3872 set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
3873
3874 xtensa_switch_to_literal_fragment (&state);
3875
3876 /* Force a 4-byte align here. */
3877 frag_align (litalign, 0, 0);
3878
3879 xg_force_frag_space (size);
3880
3881 lit_saved_frag = frag_now;
3882 frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
3883 frag_now->tc_frag_data.is_literal = TRUE;
3884 frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
3885 xg_finish_frag (0, RELAX_LITERAL, size, FALSE);
3886
3887 /* Go back. */
3888 xtensa_restore_emit_state (&state);
3889 frag_now->tc_frag_data.literal_frag = lit_saved_frag;
3890 }
3891
3892
3893 symbolS *
3894 xtensa_create_literal_symbol (sec, frag)
3895 segT sec;
3896 fragS *frag;
3897 {
3898 static int lit_num = 0;
3899 static char name[256];
3900 symbolS *symbolP;
3901
3902 sprintf (name, ".L_lit_sym%d", lit_num);
3903
3904 /* Create a local symbol. If it is in a linkonce section, we have to
3905 be careful to make sure that if it is used in a relocation that the
3906 symbol will be in the output file. */
3907 if (get_is_linkonce_section (stdoutput, sec))
3908 {
3909 symbolP = symbol_new (name, sec, 0, frag);
3910 S_CLEAR_EXTERNAL (symbolP);
3911 /* symbolP->local = 1; */
3912 }
3913 else
3914 symbolP = symbol_new (name, sec, 0, frag);
3915
3916 frag->tc_frag_data.is_literal = TRUE;
3917 lit_num++;
3918 return symbolP;
3919 }
3920
3921
3922 /* Return true if the section flags are marked linkonce
3923 or the name is .gnu.linkonce*. */
3924
3925 bfd_boolean
3926 get_is_linkonce_section (abfd, sec)
3927 bfd *abfd ATTRIBUTE_UNUSED;
3928 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 {
3938 static size_t len = sizeof ".gnu.linkonce.t.";
3939
3940 if (strncmp (segment_name (sec), ".gnu.linkonce.t.", len - 1) == 0)
3941 link_once_flags = SEC_LINK_ONCE;
3942 }
3943 return (link_once_flags != 0);
3944 }
3945
3946
3947 /* Emit an instruction to the current fragment. If record_fix is true,
3948 then this instruction will not change and we can go ahead and record
3949 the fixup. If record_fix is false, then the instruction may change
3950 and we are going to close out this fragment. Go ahead and set the
3951 fr_symbol and fr_offset instead of adding a fixup. */
3952
3953 static bfd_boolean
3954 xg_emit_insn (t_insn, record_fix)
3955 TInsn *t_insn;
3956 bfd_boolean record_fix;
3957 {
3958 bfd_boolean ok = TRUE;
3959 xtensa_isa isa = xtensa_default_isa;
3960 xtensa_opcode opcode = t_insn->opcode;
3961 bfd_boolean has_fixup = FALSE;
3962 int noperands;
3963 int i, byte_count;
3964 fragS *oldfrag;
3965 size_t old_size;
3966 char *f;
3967 static xtensa_insnbuf insnbuf = NULL;
3968
3969 /* Use a static pointer to the insn buffer so we don't have to call
3970 malloc each time through. */
3971 if (!insnbuf)
3972 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
3973
3974 has_fixup = tinsn_to_insnbuf (t_insn, insnbuf);
3975
3976 noperands = xtensa_num_operands (isa, opcode);
3977 assert (noperands == t_insn->ntok);
3978
3979 byte_count = xtensa_insn_length (isa, opcode);
3980 oldfrag = frag_now;
3981 /* This should NEVER cause us to jump into a new frag;
3982 we've already reserved space. */
3983 old_size = frag_now_fix ();
3984 f = frag_more (byte_count);
3985 assert (oldfrag == frag_now);
3986
3987 /* This needs to generate a record that lists the parts that are
3988 instructions. */
3989 if (!frag_now->tc_frag_data.is_insn)
3990 {
3991 /* If we are at the beginning of a fragment, switch this
3992 fragment to an instruction fragment. */
3993 if (now_seg != absolute_section && old_size != 0)
3994 as_warn (_("instruction fragment may contain data"));
3995 frag_now->tc_frag_data.is_insn = TRUE;
3996 }
3997
3998 xtensa_insnbuf_to_chars (isa, insnbuf, f);
3999
4000 /* Now spit out the opcode fixup.... */
4001 if (!has_fixup)
4002 return !ok;
4003
4004 for (i = 0; i < noperands; ++i)
4005 {
4006 expressionS *expr = &t_insn->tok[i];
4007 switch (expr->X_op)
4008 {
4009 case O_symbol:
4010 if (get_relaxable_immed (opcode) == i)
4011 {
4012 if (record_fix)
4013 {
4014 if (!xg_add_opcode_fix (opcode, i, expr, frag_now,
4015 f - frag_now->fr_literal))
4016 ok = FALSE;
4017 }
4018 else
4019 {
4020 /* Write it to the fr_offset, fr_symbol. */
4021 frag_now->fr_symbol = expr->X_add_symbol;
4022 frag_now->fr_offset = expr->X_add_number;
4023 }
4024 }
4025 else
4026 {
4027 as_bad (_("invalid operand %d on '%s'"),
4028 i, xtensa_opcode_name (isa, opcode));
4029 ok = FALSE;
4030 }
4031 break;
4032
4033 case O_constant:
4034 case O_register:
4035 break;
4036
4037 default:
4038 as_bad (_("invalid expression for operand %d on '%s'"),
4039 i, xtensa_opcode_name (isa, opcode));
4040 ok = FALSE;
4041 break;
4042 }
4043 }
4044
4045 return !ok;
4046 }
4047
4048
4049 static bfd_boolean
4050 xg_emit_insn_to_buf (t_insn, buf, fragP, offset, build_fix)
4051 TInsn *t_insn;
4052 char *buf;
4053 fragS *fragP;
4054 offsetT offset;
4055 bfd_boolean build_fix;
4056 {
4057 static xtensa_insnbuf insnbuf = NULL;
4058 bfd_boolean has_symbolic_immed = FALSE;
4059 bfd_boolean ok = TRUE;
4060 if (!insnbuf)
4061 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4062
4063 has_symbolic_immed = tinsn_to_insnbuf (t_insn, insnbuf);
4064 if (has_symbolic_immed && build_fix)
4065 {
4066 /* Add a fixup. */
4067 int opnum = get_relaxable_immed (t_insn->opcode);
4068 expressionS *exp = &t_insn->tok[opnum];
4069
4070 if (!xg_add_opcode_fix (t_insn->opcode,
4071 opnum, exp, fragP, offset))
4072 ok = FALSE;
4073 }
4074 fragP->tc_frag_data.is_insn = TRUE;
4075 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
4076 return ok;
4077 }
4078
4079
4080 /* Put in a fixup record based on the opcode.
4081 Return true on success. */
4082
4083 bfd_boolean
4084 xg_add_opcode_fix (opcode, opnum, expr, fragP, offset)
4085 xtensa_opcode opcode;
4086 int opnum;
4087 expressionS *expr;
4088 fragS *fragP;
4089 offsetT offset;
4090 {
4091 bfd_reloc_code_real_type reloc;
4092 reloc_howto_type *howto;
4093 int insn_length;
4094 fixS *the_fix;
4095
4096 reloc = opnum_to_reloc (opnum);
4097 if (reloc == BFD_RELOC_NONE)
4098 {
4099 as_bad (_("invalid relocation operand %i on '%s'"),
4100 opnum, xtensa_opcode_name (xtensa_default_isa, opcode));
4101 return FALSE;
4102 }
4103
4104 howto = bfd_reloc_type_lookup (stdoutput, reloc);
4105
4106 if (!howto)
4107 {
4108 as_bad (_("undefined symbol for opcode \"%s\"."),
4109 xtensa_opcode_name (xtensa_default_isa, opcode));
4110 return FALSE;
4111 }
4112
4113 insn_length = xtensa_insn_length (xtensa_default_isa, opcode);
4114 the_fix = fix_new_exp (fragP, offset, insn_length, expr,
4115 howto->pc_relative, reloc);
4116
4117 if (expr->X_add_symbol &&
4118 (S_IS_EXTERNAL (expr->X_add_symbol) || S_IS_WEAK (expr->X_add_symbol)))
4119 the_fix->fx_plt = TRUE;
4120
4121 return TRUE;
4122 }
4123
4124
4125 void
4126 xg_resolve_literals (insn, lit_sym)
4127 TInsn *insn;
4128 symbolS *lit_sym;
4129 {
4130 symbolS *sym = get_special_literal_symbol ();
4131 int i;
4132 if (lit_sym == 0)
4133 return;
4134 assert (insn->insn_type == ITYPE_INSN);
4135 for (i = 0; i < insn->ntok; i++)
4136 if (insn->tok[i].X_add_symbol == sym)
4137 insn->tok[i].X_add_symbol = lit_sym;
4138
4139 }
4140
4141
4142 void
4143 xg_resolve_labels (insn, label_sym)
4144 TInsn *insn;
4145 symbolS *label_sym;
4146 {
4147 symbolS *sym = get_special_label_symbol ();
4148 int i;
4149 /* assert(!insn->is_literal); */
4150 for (i = 0; i < insn->ntok; i++)
4151 if (insn->tok[i].X_add_symbol == sym)
4152 insn->tok[i].X_add_symbol = label_sym;
4153
4154 }
4155
4156
4157 static void
4158 xg_assemble_tokens (insn)
4159 /*const */ TInsn *insn;
4160 {
4161 /* By the time we get here, there's not too much left to do.
4162 1) Check our assumptions.
4163 2) Check if the current instruction is "narrow".
4164 If so, then finish the frag, create another one.
4165 We could also go back to change some previous
4166 "narrow" frags into no-change ones if we have more than
4167 MAX_NARROW_ALIGNMENT of them without alignment restrictions
4168 between them.
4169
4170 Cases:
4171 1) It has constant operands and doesn't fit.
4172 Go ahead and assemble it so it will fail.
4173 2) It has constant operands that fit.
4174 If narrow and !is_specific_opcode,
4175 assemble it and put in a relocation
4176 else
4177 assemble it.
4178 3) It has a symbolic immediate operand
4179 a) Find the worst-case relaxation required
4180 b) Find the worst-case literal pool space required.
4181 Insert appropriate alignment & space in the literal.
4182 Assemble it.
4183 Add the relocation. */
4184
4185 assert (insn->insn_type == ITYPE_INSN);
4186
4187 if (!tinsn_has_symbolic_operands (insn))
4188 {
4189 if (xg_is_narrow_insn (insn) && !insn->is_specific_opcode)
4190 {
4191 /* assemble it but add max required space */
4192 int max_size = xg_get_max_narrow_insn_size (insn->opcode);
4193 int min_size = xg_get_insn_size (insn);
4194 char *last_insn;
4195 assert (max_size == 3);
4196 /* make sure we have enough space to widen it */
4197 xg_force_frag_space (max_size);
4198 /* Output the instruction. It may cause an error if some
4199 operands do not fit. */
4200 last_insn = frag_more (0);
4201 if (xg_emit_insn (insn, TRUE))
4202 as_warn (_("instruction with constant operands does not fit"));
4203 xg_finish_frag (last_insn, RELAX_NARROW, max_size - min_size, TRUE);
4204 }
4205 else
4206 {
4207 /* Assemble it. No relocation needed. */
4208 int max_size = xg_get_insn_size (insn);
4209 xg_force_frag_space (max_size);
4210 if (xg_emit_insn (insn, FALSE))
4211 as_warn (_("instruction with constant operands does not "
4212 "fit without widening"));
4213 /* frag_more (max_size); */
4214
4215 /* Special case for jx. If the jx is the next to last
4216 instruction in a loop, we will add a NOP after it. This
4217 avoids a hardware issue that could occur if the jx jumped
4218 to the next instruction. */
4219 if (software_avoid_b_j_loop_end
4220 && is_jx_opcode (insn->opcode))
4221 {
4222 maybe_has_b_j_loop_end = TRUE;
4223 /* add 2 of these */
4224 frag_now->tc_frag_data.is_insn = TRUE;
4225 frag_var (rs_machine_dependent, 4, 4,
4226 RELAX_ADD_NOP_IF_PRE_LOOP_END,
4227 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4228 }
4229 }
4230 }
4231 else
4232 {
4233 /* Need to assemble it with space for the relocation. */
4234 if (!insn->is_specific_opcode)
4235 {
4236 /* Assemble it but add max required space. */
4237 char *last_insn;
4238 int min_size = xg_get_insn_size (insn);
4239 int max_size = xg_get_max_insn_widen_size (insn->opcode);
4240 int max_literal_size =
4241 xg_get_max_insn_widen_literal_size (insn->opcode);
4242
4243 #if 0
4244 symbolS *immed_sym = xg_get_insn_immed_symbol (insn);
4245 set_frag_segment (frag_now, now_seg);
4246 #endif /* 0 */
4247
4248 /* Make sure we have enough space to widen the instruction.
4249 This may open a new fragment. */
4250 xg_force_frag_space (max_size);
4251 if (max_literal_size != 0)
4252 xg_assemble_literal_space (max_literal_size);
4253
4254 /* Output the instruction. It may cause an error if some
4255 operands do not fit. Emit the incomplete instruction. */
4256 last_insn = frag_more (0);
4257 xg_emit_insn (insn, FALSE);
4258
4259 xg_finish_frag (last_insn, RELAX_IMMED, max_size - min_size, TRUE);
4260
4261 /* Special cases for loops:
4262 close_loop_end should be inserted AFTER short_loop.
4263 Make sure that CLOSE loops are processed BEFORE short_loops
4264 when converting them. */
4265
4266 /* "short_loop": add a NOP if the loop is < 4 bytes. */
4267 if (software_avoid_short_loop
4268 && is_loop_opcode (insn->opcode))
4269 {
4270 maybe_has_short_loop = TRUE;
4271 frag_now->tc_frag_data.is_insn = TRUE;
4272 frag_var (rs_machine_dependent, 4, 4,
4273 RELAX_ADD_NOP_IF_SHORT_LOOP,
4274 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4275 frag_now->tc_frag_data.is_insn = TRUE;
4276 frag_var (rs_machine_dependent, 4, 4,
4277 RELAX_ADD_NOP_IF_SHORT_LOOP,
4278 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4279 }
4280
4281 /* "close_loop_end": Add up to 12 bytes of NOPs to keep a
4282 loop at least 12 bytes away from another loop's loop
4283 end. */
4284 if (software_avoid_close_loop_end
4285 && is_loop_opcode (insn->opcode))
4286 {
4287 maybe_has_close_loop_end = TRUE;
4288 frag_now->tc_frag_data.is_insn = TRUE;
4289 frag_var (rs_machine_dependent, 12, 12,
4290 RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
4291 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4292 }
4293 }
4294 else
4295 {
4296 /* Assemble it in place. No expansion will be required,
4297 but we'll still need a relocation record. */
4298 int max_size = xg_get_insn_size (insn);
4299 xg_force_frag_space (max_size);
4300 if (xg_emit_insn (insn, TRUE))
4301 as_warn (_("instruction's constant operands do not fit"));
4302 }
4303 }
4304 }
4305
4306
4307 /* Return true if the instruction can write to the specified
4308 integer register. */
4309
4310 static bfd_boolean
4311 is_register_writer (insn, regset, regnum)
4312 const TInsn *insn;
4313 const char *regset;
4314 int regnum;
4315 {
4316 int i;
4317 int num_ops;
4318 xtensa_isa isa = xtensa_default_isa;
4319
4320 num_ops = xtensa_num_operands (isa, insn->opcode);
4321
4322 for (i = 0; i < num_ops; i++)
4323 {
4324 xtensa_operand operand = xtensa_get_operand (isa, insn->opcode, i);
4325 char inout = xtensa_operand_inout (operand);
4326
4327 if (inout == '>' || inout == '=')
4328 {
4329 if (strcmp (xtensa_operand_kind (operand), regset) == 0)
4330 {
4331 if ((insn->tok[i].X_op == O_register)
4332 && (insn->tok[i].X_add_number == regnum))
4333 return TRUE;
4334 }
4335 }
4336 }
4337 return FALSE;
4338 }
4339
4340
4341 static bfd_boolean
4342 is_bad_loopend_opcode (tinsn)
4343 const TInsn * tinsn;
4344 {
4345 xtensa_opcode opcode = tinsn->opcode;
4346
4347 if (opcode == XTENSA_UNDEFINED)
4348 return FALSE;
4349
4350 if (opcode == xtensa_call0_opcode
4351 || opcode == xtensa_callx0_opcode
4352 || opcode == xtensa_call4_opcode
4353 || opcode == xtensa_callx4_opcode
4354 || opcode == xtensa_call8_opcode
4355 || opcode == xtensa_callx8_opcode
4356 || opcode == xtensa_call12_opcode
4357 || opcode == xtensa_callx12_opcode
4358 || opcode == xtensa_isync_opcode
4359 || opcode == xtensa_ret_opcode
4360 || opcode == xtensa_ret_n_opcode
4361 || opcode == xtensa_retw_opcode
4362 || opcode == xtensa_retw_n_opcode
4363 || opcode == xtensa_waiti_opcode)
4364 return TRUE;
4365
4366 /* An RSR of LCOUNT is illegal as the last opcode in a loop. */
4367 if (opcode == xtensa_rsr_opcode
4368 && tinsn->ntok >= 2
4369 && tinsn->tok[1].X_op == O_constant
4370 && tinsn->tok[1].X_add_number == 2)
4371 return TRUE;
4372
4373 return FALSE;
4374 }
4375
4376
4377 /* Labels that begin with ".Ln" or ".LM" are unaligned.
4378 This allows the debugger to add unaligned labels.
4379 Also, the assembler generates stabs labels that need
4380 not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */
4381
4382 bfd_boolean
4383 is_unaligned_label (sym)
4384 symbolS *sym;
4385 {
4386 const char *name = S_GET_NAME (sym);
4387 static size_t fake_size = 0;
4388
4389 if (name
4390 && name[0] == '.'
4391 && name[1] == 'L' && (name[2] == 'n' || name[2] == 'M'))
4392 return TRUE;
4393
4394 /* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */
4395 if (fake_size == 0)
4396 fake_size = strlen (FAKE_LABEL_NAME);
4397
4398 if (name
4399 && strncmp (FAKE_LABEL_NAME, name, fake_size) == 0
4400 && (name[fake_size] == 'F'
4401 || name[fake_size] == 'L'
4402 || (name[fake_size] == 'e'
4403 && strncmp ("endfunc", name+fake_size, 7) == 0)))
4404 return TRUE;
4405
4406 return FALSE;
4407 }
4408
4409
4410 fragS *
4411 next_non_empty_frag (fragP)
4412 const fragS *fragP;
4413 {
4414 fragS *next_fragP = fragP->fr_next;
4415
4416 /* Sometimes an empty will end up here due storage allocation issues.
4417 So we have to skip until we find something legit. */
4418 while (next_fragP && next_fragP->fr_fix == 0)
4419 next_fragP = next_fragP->fr_next;
4420
4421 if (next_fragP == NULL || next_fragP->fr_fix == 0)
4422 return NULL;
4423
4424 return next_fragP;
4425 }
4426
4427
4428 xtensa_opcode
4429 next_frag_opcode (fragP)
4430 const fragS * fragP;
4431 {
4432 const fragS *next_fragP = next_non_empty_frag (fragP);
4433 static xtensa_insnbuf insnbuf = NULL;
4434 xtensa_isa isa = xtensa_default_isa;
4435
4436 if (!insnbuf)
4437 insnbuf = xtensa_insnbuf_alloc (isa);
4438
4439 if (next_fragP == NULL)
4440 return XTENSA_UNDEFINED;
4441
4442 xtensa_insnbuf_from_chars (isa, insnbuf, next_fragP->fr_literal);
4443 return xtensa_decode_insn (isa, insnbuf);
4444 }
4445
4446
4447 /* Return true if the target frag is one of the next non-empty frags. */
4448
4449 bfd_boolean
4450 is_next_frag_target (fragP, target)
4451 const fragS *fragP;
4452 const fragS *target;
4453 {
4454 if (fragP == NULL)
4455 return FALSE;
4456
4457 for (; fragP; fragP = fragP->fr_next)
4458 {
4459 if (fragP == target)
4460 return TRUE;
4461 if (fragP->fr_fix != 0)
4462 return FALSE;
4463 if (fragP->fr_type == rs_fill && fragP->fr_offset != 0)
4464 return FALSE;
4465 if ((fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
4466 && ((fragP->fr_address % (1 << fragP->fr_offset)) != 0))
4467 return FALSE;
4468 if (fragP->fr_type == rs_space)
4469 return FALSE;
4470 }
4471 return FALSE;
4472 }
4473
4474
4475 /* If the next legit fragment is an end-of-loop marker,
4476 switch its state so it will instantiate a NOP. */
4477
4478 static void
4479 update_next_frag_nop_state (fragP)
4480 fragS *fragP;
4481 {
4482 fragS *next_fragP = fragP->fr_next;
4483
4484 while (next_fragP && next_fragP->fr_fix == 0)
4485 {
4486 if (next_fragP->fr_type == rs_machine_dependent
4487 && next_fragP->fr_subtype == RELAX_LOOP_END)
4488 {
4489 next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP;
4490 return;
4491 }
4492 next_fragP = next_fragP->fr_next;
4493 }
4494 }
4495
4496
4497 static bfd_boolean
4498 next_frag_is_branch_target (fragP)
4499 const fragS *fragP;
4500 {
4501 /* Sometimes an empty will end up here due storage allocation issues,
4502 so we have to skip until we find something legit. */
4503 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4504 {
4505 if (fragP->tc_frag_data.is_branch_target)
4506 return TRUE;
4507 if (fragP->fr_fix != 0)
4508 break;
4509 }
4510 return FALSE;
4511 }
4512
4513
4514 static bfd_boolean
4515 next_frag_is_loop_target (fragP)
4516 const fragS *fragP;
4517 {
4518 /* Sometimes an empty will end up here due storage allocation issues.
4519 So we have to skip until we find something legit. */
4520 for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
4521 {
4522 if (fragP->tc_frag_data.is_loop_target)
4523 return TRUE;
4524 if (fragP->fr_fix != 0)
4525 break;
4526 }
4527 return FALSE;
4528 }
4529
4530
4531 static addressT
4532 next_frag_pre_opcode_bytes (fragp)
4533 const fragS *fragp;
4534 {
4535 const fragS *next_fragp = fragp->fr_next;
4536
4537 xtensa_opcode next_opcode = next_frag_opcode (fragp);
4538 if (!is_loop_opcode (next_opcode))
4539 return 0;
4540
4541 /* Sometimes an empty will end up here due storage allocation issues.
4542 So we have to skip until we find something legit. */
4543 while (next_fragp->fr_fix == 0)
4544 next_fragp = next_fragp->fr_next;
4545
4546 if (next_fragp->fr_type != rs_machine_dependent)
4547 return 0;
4548
4549 /* There is some implicit knowledge encoded in here.
4550 The LOOP instructions that are NOT RELAX_IMMED have
4551 been relaxed. */
4552 if (next_fragp->fr_subtype > RELAX_IMMED)
4553 return get_expanded_loop_offset (next_opcode);
4554
4555 return 0;
4556 }
4557
4558
4559 /* Mark a location where we can later insert literal frags. Update
4560 the section's literal_pool_loc, so subsequent literals can be
4561 placed nearest to their use. */
4562
4563 static void
4564 xtensa_mark_literal_pool_location ()
4565 {
4566 /* Any labels pointing to the current location need
4567 to be adjusted to after the literal pool. */
4568 emit_state s;
4569 fragS *pool_location;
4570
4571 frag_align (2, 0, 0);
4572
4573 /* We stash info in the fr_var of these frags
4574 so we can later move the literal's fixes into this
4575 frchain's fix list. We can use fr_var because fr_var's
4576 interpretation depends solely on the fr_type and subtype. */
4577 pool_location = frag_now;
4578 frag_variant (rs_machine_dependent, 0, (int) frchain_now,
4579 RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL);
4580 frag_variant (rs_machine_dependent, 0, (int) now_seg,
4581 RELAX_LITERAL_POOL_END, NULL, 0, NULL);
4582
4583 /* Now put a frag into the literal pool that points to this location. */
4584 set_literal_pool_location (now_seg, pool_location);
4585 xtensa_switch_to_literal_fragment (&s);
4586
4587 /* Close whatever frag is there. */
4588 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4589 frag_now->tc_frag_data.literal_frag = pool_location;
4590 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
4591 xtensa_restore_emit_state (&s);
4592 }
4593
4594
4595 static void
4596 xtensa_move_labels (old_frag, old_offset, new_frag, new_offset)
4597 fragS *old_frag;
4598 valueT old_offset;
4599 fragS *new_frag ATTRIBUTE_UNUSED;
4600 valueT new_offset;
4601 {
4602 symbolS *old_sym;
4603
4604 /* Repeat until there are no more.... */
4605 for (old_sym = xtensa_find_label (old_frag, old_offset, TRUE);
4606 old_sym;
4607 old_sym = xtensa_find_label (old_frag, old_offset, TRUE))
4608 {
4609 S_SET_VALUE (old_sym, (valueT) new_offset);
4610 symbol_set_frag (old_sym, frag_now);
4611 }
4612 }
4613
4614
4615 /* Assemble a NOP of the requested size in the buffer. User must have
4616 allocated "buf" with at least "size" bytes. */
4617
4618 void
4619 assemble_nop (size, buf)
4620 size_t size;
4621 char *buf;
4622 {
4623 static xtensa_insnbuf insnbuf = NULL;
4624 TInsn t_insn;
4625 if (!insnbuf)
4626 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
4627
4628 tinsn_init (&t_insn);
4629 switch (size)
4630 {
4631 case 2:
4632 t_insn.opcode = xtensa_nop_n_opcode;
4633 t_insn.ntok = 0;
4634 if (t_insn.opcode == XTENSA_UNDEFINED)
4635 as_fatal (_("opcode 'NOP.N' unavailable in this configuration"));
4636 tinsn_to_insnbuf (&t_insn, insnbuf);
4637 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
4638 break;
4639
4640 case 3:
4641 t_insn.opcode = xtensa_or_opcode;
4642 assert (t_insn.opcode != XTENSA_UNDEFINED);
4643 if (t_insn.opcode == XTENSA_UNDEFINED)
4644 as_fatal (_("opcode 'OR' unavailable in this configuration"));
4645 set_expr_const (&t_insn.tok[0], 1);
4646 set_expr_const (&t_insn.tok[1], 1);
4647 set_expr_const (&t_insn.tok[2], 1);
4648 t_insn.ntok = 3;
4649 tinsn_to_insnbuf (&t_insn, insnbuf);
4650 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, buf);
4651 break;
4652
4653 default:
4654 as_fatal (_("invalid %d-byte NOP requested"), size);
4655 }
4656 }
4657
4658
4659 /* Return the number of bytes for the offset of the expanded loop
4660 instruction. This should be incorporated into the relaxation
4661 specification but is hard-coded here. This is used to auto-align
4662 the loop instruction. It is invalid to call this function if the
4663 configuration does not have loops or if the opcode is not a loop
4664 opcode. */
4665
4666 static addressT
4667 get_expanded_loop_offset (opcode)
4668 xtensa_opcode opcode;
4669 {
4670 /* This is the OFFSET of the loop instruction in the expanded loop.
4671 This MUST correspond directly to the specification of the loop
4672 expansion. It will be validated on fragment conversion. */
4673 if (opcode == XTENSA_UNDEFINED)
4674 as_fatal (_("get_expanded_loop_offset: undefined opcode"));
4675 if (opcode == xtensa_loop_opcode)
4676 return 0;
4677 if (opcode == xtensa_loopnez_opcode)
4678 return 3;
4679 if (opcode == xtensa_loopgtz_opcode)
4680 return 6;
4681 as_fatal (_("get_expanded_loop_offset: invalid opcode"));
4682 return 0;
4683 }
4684
4685
4686 fragS *
4687 get_literal_pool_location (seg)
4688 segT seg;
4689 {
4690 return seg_info (seg)->tc_segment_info_data.literal_pool_loc;
4691 }
4692
4693
4694 static void
4695 set_literal_pool_location (seg, literal_pool_loc)
4696 segT seg;
4697 fragS *literal_pool_loc;
4698 {
4699 seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc;
4700 }
4701
4702 \f
4703 /* External Functions and Other GAS Hooks. */
4704
4705 const char *
4706 xtensa_target_format ()
4707 {
4708 return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le");
4709 }
4710
4711
4712 void
4713 xtensa_file_arch_init (abfd)
4714 bfd *abfd;
4715 {
4716 bfd_set_private_flags (abfd, 0x100 | 0x200);
4717 }
4718
4719
4720 void
4721 md_number_to_chars (buf, val, n)
4722 char *buf;
4723 valueT val;
4724 int n;
4725 {
4726 if (target_big_endian)
4727 number_to_chars_bigendian (buf, val, n);
4728 else
4729 number_to_chars_littleendian (buf, val, n);
4730 }
4731
4732
4733 /* This function is called once, at assembler startup time. It should
4734 set up all the tables, etc. that the MD part of the assembler will
4735 need. */
4736
4737 void
4738 md_begin ()
4739 {
4740 segT current_section = now_seg;
4741 int current_subsec = now_subseg;
4742 xtensa_isa isa;
4743
4744 #if STATIC_LIBISA
4745 isa = xtensa_isa_init ();
4746 #else
4747 /* ISA was already initialized by xtensa_init(). */
4748 isa = xtensa_default_isa;
4749 #endif
4750
4751 /* Set up the .literal, .fini.literal and .init.literal sections. */
4752 memset (&default_lit_sections, 0, sizeof (default_lit_sections));
4753 default_lit_sections.init_lit_seg_name = INIT_LITERAL_SECTION_NAME;
4754 default_lit_sections.fini_lit_seg_name = FINI_LITERAL_SECTION_NAME;
4755 default_lit_sections.lit_seg_name = LITERAL_SECTION_NAME;
4756
4757 subseg_set (current_section, current_subsec);
4758
4759 xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi");
4760 xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi");
4761 xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0");
4762 xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4");
4763 xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8");
4764 xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12");
4765 xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0");
4766 xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4");
4767 xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8");
4768 xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12");
4769 xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry");
4770 xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync");
4771 xtensa_j_opcode = xtensa_opcode_lookup (isa, "j");
4772 xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx");
4773 xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop");
4774 xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez");
4775 xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz");
4776 xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n");
4777 xtensa_or_opcode = xtensa_opcode_lookup (isa, "or");
4778 xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret");
4779 xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n");
4780 xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw");
4781 xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n");
4782 xtensa_rsr_opcode = xtensa_opcode_lookup (isa, "rsr");
4783 xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti");
4784 }
4785
4786
4787 /* tc_frob_label hook */
4788
4789 void
4790 xtensa_frob_label (sym)
4791 symbolS *sym;
4792 {
4793 xtensa_define_label (sym);
4794 if (is_loop_target_label (sym)
4795 && (get_last_insn_flags (now_seg, now_subseg)
4796 & FLAG_IS_BAD_LOOPEND) != 0)
4797 as_bad (_("invalid last instruction for a zero-overhead loop"));
4798
4799 /* No target aligning in the absolute section. */
4800 if (now_seg != absolute_section
4801 && align_targets
4802 && !is_unaligned_label (sym)
4803 && !frag_now->tc_frag_data.is_literal)
4804 {
4805 fragS *old_frag = frag_now;
4806 offsetT old_offset = frag_now_fix ();
4807 /* frag_now->tc_frag_data.is_insn = TRUE; */
4808 frag_var (rs_machine_dependent, 4, 4,
4809 RELAX_DESIRE_ALIGN_IF_TARGET,
4810 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4811 xtensa_move_labels (old_frag, old_offset, frag_now, 0);
4812 /* Once we know whether or not the label is a branch target
4813 We will suppress some of these alignments. */
4814 }
4815 }
4816
4817
4818 /* md_flush_pending_output hook */
4819
4820 void
4821 xtensa_flush_pending_output ()
4822 {
4823 /* If there is a non-zero instruction fragment, close it. */
4824 if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn)
4825 {
4826 frag_wane (frag_now);
4827 frag_new (0);
4828 }
4829 frag_now->tc_frag_data.is_insn = FALSE;
4830 }
4831
4832
4833 void
4834 md_assemble (str)
4835 char *str;
4836 {
4837 xtensa_isa isa = xtensa_default_isa;
4838 char *opname;
4839 unsigned opnamelen;
4840 bfd_boolean has_underbar = FALSE;
4841 char *arg_strings[MAX_INSN_ARGS];
4842 int num_args;
4843 IStack istack; /* Put instructions into here. */
4844 TInsn orig_insn; /* Original instruction from the input. */
4845 int i;
4846 symbolS *lit_sym = NULL;
4847
4848 if (frag_now->tc_frag_data.is_literal)
4849 {
4850 static bfd_boolean reported = 0;
4851 if (reported < 4)
4852 as_bad (_("cannot assemble '%s' into a literal fragment"), str);
4853 if (reported == 3)
4854 as_bad (_("..."));
4855 reported++;
4856 return;
4857 }
4858
4859 istack_init (&istack);
4860 tinsn_init (&orig_insn);
4861
4862 /* Split off the opcode. */
4863 opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
4864 opname = xmalloc (opnamelen + 1);
4865 memcpy (opname, str, opnamelen);
4866 opname[opnamelen] = '\0';
4867
4868 num_args = tokenize_arguments (arg_strings, str + opnamelen);
4869 if (num_args == -1)
4870 {
4871 as_bad (_("syntax error"));
4872 return;
4873 }
4874
4875 if (xg_translate_idioms (&opname, &num_args, arg_strings))
4876 return;
4877
4878 /* Check for an underbar prefix. */
4879 if (*opname == '_')
4880 {
4881 has_underbar = TRUE;
4882 opname += 1;
4883 }
4884
4885 orig_insn.insn_type = ITYPE_INSN;
4886 orig_insn.ntok = 0;
4887 orig_insn.is_specific_opcode = (has_underbar || !use_generics ());
4888 specific_opcode = orig_insn.is_specific_opcode;
4889
4890 orig_insn.opcode = xtensa_opcode_lookup (isa, opname);
4891 if (orig_insn.opcode == XTENSA_UNDEFINED)
4892 {
4893 as_bad (_("unknown opcode %s"), opname);
4894 return;
4895 }
4896
4897 if (frag_now_fix () != 0 && !frag_now->tc_frag_data.is_insn)
4898 {
4899 frag_wane (frag_now);
4900 frag_new (0);
4901 }
4902
4903 if (software_a0_b_retw_interlock)
4904 {
4905 if ((get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0
4906 && is_conditional_branch_opcode (orig_insn.opcode))
4907 {
4908 has_a0_b_retw = TRUE;
4909
4910 /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW.
4911 After the first assembly pass we will check all of them and
4912 add a nop if needed. */
4913 frag_now->tc_frag_data.is_insn = TRUE;
4914 frag_var (rs_machine_dependent, 4, 4,
4915 RELAX_ADD_NOP_IF_A0_B_RETW,
4916 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4917 frag_now->tc_frag_data.is_insn = TRUE;
4918 frag_var (rs_machine_dependent, 4, 4,
4919 RELAX_ADD_NOP_IF_A0_B_RETW,
4920 frag_now->fr_symbol, frag_now->fr_offset, NULL);
4921 }
4922 }
4923
4924 /* Special case: The call instructions should be marked "specific opcode"
4925 to keep them from expanding. */
4926 if (!use_longcalls () && is_direct_call_opcode (orig_insn.opcode))
4927 orig_insn.is_specific_opcode = TRUE;
4928
4929 /* Parse the arguments. */
4930 if (parse_arguments (&orig_insn, num_args, arg_strings))
4931 {
4932 as_bad (_("syntax error"));
4933 return;
4934 }
4935
4936 /* Free the opcode and argument strings, now that they've been parsed. */
4937 free (has_underbar ? opname - 1 : opname);
4938 opname = 0;
4939 while (num_args-- > 0)
4940 free (arg_strings[num_args]);
4941
4942 /* Check for the right number and type of arguments. */
4943 if (tinsn_check_arguments (&orig_insn))
4944 return;
4945
4946 /* See if the instruction implies an aligned section. */
4947 if (is_entry_opcode (orig_insn.opcode) || is_loop_opcode (orig_insn.opcode))
4948 record_alignment (now_seg, 2);
4949
4950 xg_add_branch_and_loop_targets (&orig_insn);
4951
4952 /* Special cases for instructions that force an alignment... */
4953 if (!orig_insn.is_specific_opcode && is_loop_opcode (orig_insn.opcode))
4954 {
4955 fragS *old_frag = frag_now;
4956 offsetT old_offset = frag_now_fix ();
4957 symbolS *old_sym = NULL;
4958 size_t max_fill;
4959
4960 frag_now->tc_frag_data.is_insn = TRUE;
4961 frag_now->tc_frag_data.is_no_density = !code_density_available ();
4962 max_fill = get_text_align_max_fill_size
4963 (get_text_align_power (XTENSA_FETCH_WIDTH),
4964 TRUE, frag_now->tc_frag_data.is_no_density);
4965 frag_var (rs_machine_dependent, max_fill, max_fill,
4966 RELAX_ALIGN_NEXT_OPCODE, frag_now->fr_symbol,
4967 frag_now->fr_offset, NULL);
4968
4969 /* Repeat until there are no more. */
4970 while ((old_sym = xtensa_find_label (old_frag, old_offset, FALSE)))
4971 {
4972 S_SET_VALUE (old_sym, (valueT) 0);
4973 symbol_set_frag (old_sym, frag_now);
4974 }
4975 }
4976
4977 /* Special-case for "entry" instruction. */
4978 if (is_entry_opcode (orig_insn.opcode))
4979 {
4980 /* Check that the second opcode (#1) is >= 16. */
4981 if (orig_insn.ntok >= 2)
4982 {
4983 expressionS *exp = &orig_insn.tok[1];
4984 switch (exp->X_op)
4985 {
4986 case O_constant:
4987 if (exp->X_add_number < 16)
4988 as_warn (_("entry instruction with stack decrement < 16"));
4989 break;
4990
4991 default:
4992 as_warn (_("entry instruction with non-constant decrement"));
4993 }
4994 }
4995
4996 if (!orig_insn.is_specific_opcode)
4997 {
4998 fragS *label_target = frag_now;
4999 offsetT label_offset = frag_now_fix ();
5000
5001 xtensa_mark_literal_pool_location ();
5002
5003 /* Automatically align ENTRY instructions. */
5004 xtensa_move_labels (label_target, label_offset, frag_now, 0);
5005 frag_align (2, 0, 0);
5006 }
5007 }
5008
5009 if (software_a0_b_retw_interlock)
5010 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER,
5011 is_register_writer (&orig_insn, "a", 0));
5012
5013 set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND,
5014 is_bad_loopend_opcode (&orig_insn));
5015
5016 /* Finish it off:
5017 assemble_tokens (opcode, tok, ntok);
5018 expand the tokens from the orig_insn into the
5019 stack of instructions that will not expand
5020 unless required at relaxation time. */
5021 if (xg_expand_assembly_insn (&istack, &orig_insn))
5022 return;
5023
5024 for (i = 0; i < istack.ninsn; i++)
5025 {
5026 TInsn *insn = &istack.insn[i];
5027 if (insn->insn_type == ITYPE_LITERAL)
5028 {
5029 assert (lit_sym == NULL);
5030 lit_sym = xg_assemble_literal (insn);
5031 }
5032 else
5033 {
5034 if (lit_sym)
5035 xg_resolve_literals (insn, lit_sym);
5036 xg_assemble_tokens (insn);
5037 }
5038 }
5039
5040 /* Now, if the original opcode was a call... */
5041 if (align_targets && is_call_opcode (orig_insn.opcode))
5042 {
5043 frag_now->tc_frag_data.is_insn = TRUE;
5044 frag_var (rs_machine_dependent, 4, 4,
5045 RELAX_DESIRE_ALIGN,
5046 frag_now->fr_symbol,
5047 frag_now->fr_offset,
5048 NULL);
5049 }
5050 }
5051
5052
5053 /* TC_CONS_FIX_NEW hook: Check for "@PLT" suffix on symbol references.
5054 If found, use an XTENSA_PLT reloc for 4-byte values. Otherwise, this
5055 is the same as the standard code in read.c. */
5056
5057 void
5058 xtensa_cons_fix_new (frag, where, size, exp)
5059 fragS *frag;
5060 int where;
5061 int size;
5062 expressionS *exp;
5063 {
5064 bfd_reloc_code_real_type r;
5065 bfd_boolean plt = FALSE;
5066
5067 if (*input_line_pointer == '@')
5068 {
5069 if (!strncmp (input_line_pointer, PLT_SUFFIX, strlen (PLT_SUFFIX) - 1)
5070 && !strncmp (input_line_pointer, plt_suffix,
5071 strlen (plt_suffix) - 1))
5072 {
5073 as_bad (_("undefined @ suffix '%s', expected '%s'"),
5074 input_line_pointer, plt_suffix);
5075 ignore_rest_of_line ();
5076 return;
5077 }
5078
5079 input_line_pointer += strlen (plt_suffix);
5080 plt = TRUE;
5081 }
5082
5083 switch (size)
5084 {
5085 case 1:
5086 r = BFD_RELOC_8;
5087 break;
5088 case 2:
5089 r = BFD_RELOC_16;
5090 break;
5091 case 4:
5092 r = plt ? BFD_RELOC_XTENSA_PLT : BFD_RELOC_32;
5093 break;
5094 case 8:
5095 r = BFD_RELOC_64;
5096 break;
5097 default:
5098 as_bad (_("unsupported BFD relocation size %u"), size);
5099 r = BFD_RELOC_32;
5100 break;
5101 }
5102 fix_new_exp (frag, where, size, exp, 0, r);
5103 }
5104
5105
5106 /* TC_FRAG_INIT hook */
5107
5108 void
5109 xtensa_frag_init (frag)
5110 fragS *frag;
5111 {
5112 frag->tc_frag_data.is_no_density = !code_density_available ();
5113 }
5114
5115
5116 symbolS *
5117 md_undefined_symbol (name)
5118 char *name ATTRIBUTE_UNUSED;
5119 {
5120 return NULL;
5121 }
5122
5123
5124 /* Round up a section size to the appropriate boundary. */
5125
5126 valueT
5127 md_section_align (segment, size)
5128 segT segment ATTRIBUTE_UNUSED;
5129 valueT size;
5130 {
5131 return size; /* Byte alignment is fine. */
5132 }
5133
5134
5135 long
5136 md_pcrel_from (fixP)
5137 fixS *fixP;
5138 {
5139 char *insn_p;
5140 static xtensa_insnbuf insnbuf = NULL;
5141 int opnum;
5142 xtensa_operand operand;
5143 xtensa_opcode opcode;
5144 xtensa_isa isa = xtensa_default_isa;
5145 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
5146
5147 if (fixP->fx_done)
5148 return addr;
5149
5150 if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND)
5151 return addr;
5152
5153 if (!insnbuf)
5154 insnbuf = xtensa_insnbuf_alloc (isa);
5155
5156 insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where];
5157 xtensa_insnbuf_from_chars (isa, insnbuf, insn_p);
5158 opcode = xtensa_decode_insn (isa, insnbuf);
5159
5160 opnum = reloc_to_opnum (fixP->fx_r_type);
5161
5162 if (opnum < 0)
5163 as_fatal (_("invalid operand relocation for '%s' instruction"),
5164 xtensa_opcode_name (isa, opcode));
5165 if (opnum >= xtensa_num_operands (isa, opcode))
5166 as_fatal (_("invalid relocation for operand %d in '%s' instruction"),
5167 opnum, xtensa_opcode_name (isa, opcode));
5168 operand = xtensa_get_operand (isa, opcode, opnum);
5169 if (!operand)
5170 {
5171 as_warn_where (fixP->fx_file,
5172 fixP->fx_line,
5173 _("invalid relocation type %d for %s instruction"),
5174 fixP->fx_r_type, xtensa_opcode_name (isa, opcode));
5175 return addr;
5176 }
5177
5178 if (!operand_is_pcrel_label (operand))
5179 {
5180 as_bad_where (fixP->fx_file,
5181 fixP->fx_line,
5182 _("invalid relocation for operand %d of '%s'"),
5183 opnum, xtensa_opcode_name (isa, opcode));
5184 return addr;
5185 }
5186 if (!xtensa_operand_isPCRelative (operand))
5187 {
5188 as_warn_where (fixP->fx_file,
5189 fixP->fx_line,
5190 _("non-PCREL relocation operand %d for '%s': %s"),
5191 opnum, xtensa_opcode_name (isa, opcode),
5192 bfd_get_reloc_code_name (fixP->fx_r_type));
5193 return addr;
5194 }
5195
5196 return 0 - xtensa_operand_do_reloc (operand, 0, addr);
5197 }
5198
5199
5200 /* tc_symbol_new_hook */
5201
5202 void
5203 xtensa_symbol_new_hook (symbolP)
5204 symbolS *symbolP;
5205 {
5206 symbol_get_tc (symbolP)->plt = 0;
5207 }
5208
5209
5210 /* tc_fix_adjustable hook */
5211
5212 bfd_boolean
5213 xtensa_fix_adjustable (fixP)
5214 fixS *fixP;
5215 {
5216 /* We need the symbol name for the VTABLE entries. */
5217 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
5218 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5219 return 0;
5220
5221 return 1;
5222 }
5223
5224
5225 void
5226 md_apply_fix3 (fixP, valP, seg)
5227 fixS *fixP;
5228 valueT *valP;
5229 segT seg ATTRIBUTE_UNUSED;
5230 {
5231 if (fixP->fx_pcrel == 0 && fixP->fx_addsy == 0)
5232 {
5233 /* This happens when the relocation is within the current section.
5234 It seems this implies a PCREL operation. We'll catch it and error
5235 if not. */
5236
5237 char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
5238 static xtensa_insnbuf insnbuf = NULL;
5239 xtensa_opcode opcode;
5240 xtensa_isa isa;
5241
5242 switch (fixP->fx_r_type)
5243 {
5244 case BFD_RELOC_XTENSA_ASM_EXPAND:
5245 fixP->fx_done = 1;
5246 break;
5247
5248 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
5249 as_bad (_("unhandled local relocation fix %s"),
5250 bfd_get_reloc_code_name (fixP->fx_r_type));
5251 break;
5252
5253 case BFD_RELOC_32:
5254 case BFD_RELOC_16:
5255 case BFD_RELOC_8:
5256 /* The only one we support that isn't an instruction field. */
5257 md_number_to_chars (fixpos, *valP, fixP->fx_size);
5258 fixP->fx_done = 1;
5259 break;
5260
5261 case BFD_RELOC_XTENSA_OP0:
5262 case BFD_RELOC_XTENSA_OP1:
5263 case BFD_RELOC_XTENSA_OP2:
5264 isa = xtensa_default_isa;
5265 if (!insnbuf)
5266 insnbuf = xtensa_insnbuf_alloc (isa);
5267
5268 xtensa_insnbuf_from_chars (isa, insnbuf, fixpos);
5269 opcode = xtensa_decode_insn (isa, insnbuf);
5270 if (opcode == XTENSA_UNDEFINED)
5271 as_fatal (_("undecodable FIX"));
5272
5273 xtensa_insnbuf_set_immediate_field (opcode, insnbuf, *valP,
5274 fixP->fx_file, fixP->fx_line);
5275
5276 fixP->fx_frag->tc_frag_data.is_insn = TRUE;
5277 xtensa_insnbuf_to_chars (isa, insnbuf, fixpos);
5278 fixP->fx_done = 1;
5279 break;
5280
5281 case BFD_RELOC_VTABLE_INHERIT:
5282 case BFD_RELOC_VTABLE_ENTRY:
5283 fixP->fx_done = 0;
5284 break;
5285
5286 default:
5287 as_bad (_("unhandled local relocation fix %s"),
5288 bfd_get_reloc_code_name (fixP->fx_r_type));
5289 }
5290 }
5291 }
5292
5293
5294 char *
5295 md_atof (type, litP, sizeP)
5296 int type;
5297 char *litP;
5298 int *sizeP;
5299 {
5300 int prec;
5301 LITTLENUM_TYPE words[4];
5302 char *t;
5303 int i;
5304
5305 switch (type)
5306 {
5307 case 'f':
5308 prec = 2;
5309 break;
5310
5311 case 'd':
5312 prec = 4;
5313 break;
5314
5315 default:
5316 *sizeP = 0;
5317 return "bad call to md_atof";
5318 }
5319
5320 t = atof_ieee (input_line_pointer, type, words);
5321 if (t)
5322 input_line_pointer = t;
5323
5324 *sizeP = prec * 2;
5325
5326 for (i = prec - 1; i >= 0; i--)
5327 {
5328 int idx = i;
5329 if (target_big_endian)
5330 idx = (prec - 1 - i);
5331
5332 md_number_to_chars (litP, (valueT) words[idx], 2);
5333 litP += 2;
5334 }
5335
5336 return NULL;
5337 }
5338
5339
5340 int
5341 md_estimate_size_before_relax (fragP, seg)
5342 fragS *fragP;
5343 segT seg ATTRIBUTE_UNUSED;
5344 {
5345 return fragP->tc_frag_data.text_expansion;
5346 }
5347
5348
5349 /* Translate internal representation of relocation info to BFD target
5350 format. */
5351
5352 arelent *
5353 tc_gen_reloc (section, fixp)
5354 asection *section ATTRIBUTE_UNUSED;
5355 fixS *fixp;
5356 {
5357 arelent *reloc;
5358
5359 reloc = (arelent *) xmalloc (sizeof (arelent));
5360 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5361 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5362 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
5363
5364 /* Make sure none of our internal relocations make it this far.
5365 They'd better have been fully resolved by this point. */
5366 assert ((int) fixp->fx_r_type > 0);
5367
5368 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
5369 if (reloc->howto == NULL)
5370 {
5371 as_bad_where (fixp->fx_file, fixp->fx_line,
5372 _("cannot represent `%s' relocation in object file"),
5373 bfd_get_reloc_code_name (fixp->fx_r_type));
5374 return NULL;
5375 }
5376
5377 if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
5378 {
5379 as_fatal (_("internal error? cannot generate `%s' relocation"),
5380 bfd_get_reloc_code_name (fixp->fx_r_type));
5381 }
5382 assert (!fixp->fx_pcrel == !reloc->howto->pc_relative);
5383
5384 reloc->addend = fixp->fx_offset;
5385
5386 switch (fixp->fx_r_type)
5387 {
5388 case BFD_RELOC_XTENSA_OP0:
5389 case BFD_RELOC_XTENSA_OP1:
5390 case BFD_RELOC_XTENSA_OP2:
5391 case BFD_RELOC_XTENSA_ASM_EXPAND:
5392 case BFD_RELOC_32:
5393 case BFD_RELOC_XTENSA_PLT:
5394 case BFD_RELOC_VTABLE_INHERIT:
5395 case BFD_RELOC_VTABLE_ENTRY:
5396 break;
5397
5398 case BFD_RELOC_XTENSA_ASM_SIMPLIFY:
5399 as_warn (_("emitting simplification relocation"));
5400 break;
5401
5402 default:
5403 as_warn (_("emitting unknown relocation"));
5404 }
5405
5406 return reloc;
5407 }
5408
5409 \f
5410 void
5411 xtensa_end ()
5412 {
5413 directive_balance ();
5414 xtensa_move_literals ();
5415
5416 xtensa_reorder_segments ();
5417 xtensa_mark_target_fragments ();
5418 xtensa_cleanup_align_frags ();
5419 xtensa_fix_target_frags ();
5420 if (software_a0_b_retw_interlock && has_a0_b_retw)
5421 xtensa_fix_a0_b_retw_frags ();
5422 if (software_avoid_b_j_loop_end && maybe_has_b_j_loop_end)
5423 xtensa_fix_b_j_loop_end_frags ();
5424
5425 /* "close_loop_end" should be processed BEFORE "short_loop". */
5426 if (software_avoid_close_loop_end && maybe_has_close_loop_end)
5427 xtensa_fix_close_loop_end_frags ();
5428
5429 if (software_avoid_short_loop && maybe_has_short_loop)
5430 xtensa_fix_short_loop_frags ();
5431
5432 xtensa_sanity_check ();
5433 }
5434
5435
5436 static void
5437 xtensa_cleanup_align_frags ()
5438 {
5439 frchainS *frchP;
5440
5441 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5442 {
5443 fragS *fragP;
5444
5445 /* Walk over all of the fragments in a subsection. */
5446 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5447 {
5448 if ((fragP->fr_type == rs_align
5449 || fragP->fr_type == rs_align_code
5450 || (fragP->fr_type == rs_machine_dependent
5451 && (fragP->fr_subtype == RELAX_DESIRE_ALIGN
5452 || fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)))
5453 && fragP->fr_fix == 0)
5454 {
5455 fragS * next = fragP->fr_next;
5456
5457 while (next
5458 && next->fr_type == rs_machine_dependent
5459 && next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
5460 {
5461 frag_wane (next);
5462 next = next->fr_next;
5463 }
5464 }
5465 }
5466 }
5467 }
5468
5469
5470 /* Re-process all of the fragments looking to convert all of the
5471 RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch
5472 target in the next fragment, convert this to RELAX_DESIRE_ALIGN.
5473 If the next fragment starts with a loop target, AND the previous
5474 fragment can be expanded to negate the branch, convert this to a
5475 RELAX_LOOP_END. Otherwise, convert to a .fill 0. */
5476
5477 static void
5478 xtensa_fix_target_frags ()
5479 {
5480 frchainS *frchP;
5481
5482 /* When this routine is called, all of the subsections are still intact
5483 so we walk over subsections instead of sections. */
5484 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5485 {
5486 bfd_boolean prev_frag_can_negate_branch = FALSE;
5487 fragS *fragP;
5488
5489 /* Walk over all of the fragments in a subsection. */
5490 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5491 {
5492 if (fragP->fr_type == rs_machine_dependent
5493 && fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
5494 {
5495 if (next_frag_is_loop_target (fragP))
5496 {
5497 if (prev_frag_can_negate_branch)
5498 fragP->fr_subtype = RELAX_LOOP_END;
5499 else
5500 {
5501 if (!align_only_targets ||
5502 next_frag_is_branch_target (fragP))
5503 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
5504 else
5505 frag_wane (fragP);
5506 }
5507 }
5508 else if (!align_only_targets
5509 || next_frag_is_branch_target (fragP))
5510 fragP->fr_subtype = RELAX_DESIRE_ALIGN;
5511 else
5512 frag_wane (fragP);
5513 }
5514 if (fragP->fr_fix != 0)
5515 prev_frag_can_negate_branch = FALSE;
5516 if (frag_can_negate_branch (fragP))
5517 prev_frag_can_negate_branch = TRUE;
5518 }
5519 }
5520 }
5521
5522
5523 static bfd_boolean
5524 frag_can_negate_branch (fragP)
5525 fragS *fragP;
5526 {
5527 if (fragP->fr_type == rs_machine_dependent
5528 && fragP->fr_subtype == RELAX_IMMED)
5529 {
5530 TInsn t_insn;
5531 tinsn_from_chars (&t_insn, fragP->fr_opcode);
5532 if (is_negatable_branch (&t_insn))
5533 return TRUE;
5534 }
5535 return FALSE;
5536 }
5537
5538
5539 /* Re-process all of the fragments looking to convert all of the
5540 RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a
5541 conditional branch or a retw/retw.n, convert this frag to one that
5542 will generate a NOP. In any case close it off with a .fill 0. */
5543
5544 static void
5545 xtensa_fix_a0_b_retw_frags ()
5546 {
5547 frchainS *frchP;
5548
5549 /* When this routine is called, all of the subsections are still intact
5550 so we walk over subsections instead of sections. */
5551 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5552 {
5553 fragS *fragP;
5554
5555 /* Walk over all of the fragments in a subsection. */
5556 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5557 {
5558 if (fragP->fr_type == rs_machine_dependent
5559 && fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW)
5560 {
5561 if (next_instrs_are_b_retw (fragP))
5562 relax_frag_add_nop (fragP);
5563 else
5564 frag_wane (fragP);
5565 }
5566 }
5567 }
5568 }
5569
5570
5571 bfd_boolean
5572 next_instrs_are_b_retw (fragP)
5573 fragS * fragP;
5574 {
5575 xtensa_opcode opcode;
5576 const fragS *next_fragP = next_non_empty_frag (fragP);
5577 static xtensa_insnbuf insnbuf = NULL;
5578 xtensa_isa isa = xtensa_default_isa;
5579 int offset = 0;
5580
5581 if (!insnbuf)
5582 insnbuf = xtensa_insnbuf_alloc (isa);
5583
5584 if (next_fragP == NULL)
5585 return FALSE;
5586
5587 /* Check for the conditional branch. */
5588 xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset]);
5589 opcode = xtensa_decode_insn (isa, insnbuf);
5590
5591 if (!is_conditional_branch_opcode (opcode))
5592 return FALSE;
5593
5594 offset += xtensa_insn_length (isa, opcode);
5595 if (offset == next_fragP->fr_fix)
5596 {
5597 next_fragP = next_non_empty_frag (next_fragP);
5598 offset = 0;
5599 }
5600 if (next_fragP == NULL)
5601 return FALSE;
5602
5603 /* Check for the retw/retw.n. */
5604 xtensa_insnbuf_from_chars (isa, insnbuf, &next_fragP->fr_literal[offset]);
5605 opcode = xtensa_decode_insn (isa, insnbuf);
5606
5607 if (is_windowed_return_opcode (opcode))
5608 return TRUE;
5609 return FALSE;
5610 }
5611
5612
5613 /* Re-process all of the fragments looking to convert all of the
5614 RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a
5615 loop end label, convert this frag to one that will generate a NOP.
5616 In any case close it off with a .fill 0. */
5617
5618 static void
5619 xtensa_fix_b_j_loop_end_frags ()
5620 {
5621 frchainS *frchP;
5622
5623 /* When this routine is called, all of the subsections are still intact
5624 so we walk over subsections instead of sections. */
5625 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5626 {
5627 fragS *fragP;
5628
5629 /* Walk over all of the fragments in a subsection. */
5630 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5631 {
5632 if (fragP->fr_type == rs_machine_dependent
5633 && fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END)
5634 {
5635 if (next_instr_is_loop_end (fragP))
5636 relax_frag_add_nop (fragP);
5637 else
5638 frag_wane (fragP);
5639 }
5640 }
5641 }
5642 }
5643
5644
5645 bfd_boolean
5646 next_instr_is_loop_end (fragP)
5647 fragS * fragP;
5648 {
5649 const fragS *next_fragP;
5650
5651 if (next_frag_is_loop_target (fragP))
5652 return FALSE;
5653
5654 next_fragP = next_non_empty_frag (fragP);
5655 if (next_fragP == NULL)
5656 return FALSE;
5657
5658 if (!next_frag_is_loop_target (next_fragP))
5659 return FALSE;
5660
5661 /* If the size is >= 3 then there is more than one instruction here.
5662 The hardware bug will not fire. */
5663 if (next_fragP->fr_fix > 3)
5664 return FALSE;
5665
5666 return TRUE;
5667 }
5668
5669
5670 /* Re-process all of the fragments looking to convert all of the
5671 RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is
5672 not MY loop's loop end within 12 bytes, add enough nops here to
5673 make it at least 12 bytes away. In any case close it off with a
5674 .fill 0. */
5675
5676 static void
5677 xtensa_fix_close_loop_end_frags ()
5678 {
5679 frchainS *frchP;
5680
5681 /* When this routine is called, all of the subsections are still intact
5682 so we walk over subsections instead of sections. */
5683 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5684 {
5685 fragS *fragP;
5686
5687 fragS *current_target = NULL;
5688 offsetT current_offset = 0;
5689
5690 /* Walk over all of the fragments in a subsection. */
5691 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5692 {
5693 if (fragP->fr_type == rs_machine_dependent
5694 && fragP->fr_subtype == RELAX_IMMED)
5695 {
5696 /* Read it. If the instruction is a loop, get the target. */
5697 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_opcode);
5698 if (is_loop_opcode (opcode))
5699 {
5700 TInsn t_insn;
5701
5702 tinsn_from_chars (&t_insn, fragP->fr_opcode);
5703 tinsn_immed_from_frag (&t_insn, fragP);
5704
5705 /* Get the current fragment target. */
5706 if (fragP->fr_symbol)
5707 {
5708 current_target = symbol_get_frag (fragP->fr_symbol);
5709 current_offset = fragP->fr_offset;
5710 }
5711 }
5712 }
5713
5714 if (current_target
5715 && fragP->fr_type == rs_machine_dependent
5716 && fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END)
5717 {
5718 size_t min_bytes;
5719 size_t bytes_added = 0;
5720
5721 #define REQUIRED_LOOP_DIVIDING_BYTES 12
5722 /* Max out at 12. */
5723 min_bytes = min_bytes_to_other_loop_end
5724 (fragP->fr_next, current_target, current_offset,
5725 REQUIRED_LOOP_DIVIDING_BYTES);
5726
5727 if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES)
5728 {
5729 while (min_bytes + bytes_added
5730 < REQUIRED_LOOP_DIVIDING_BYTES)
5731 {
5732 int length = 3;
5733
5734 if (fragP->fr_var < length)
5735 as_warn (_("fr_var %lu < length %d; ignoring"),
5736 fragP->fr_var, length);
5737 else
5738 {
5739 assemble_nop (length,
5740 fragP->fr_literal + fragP->fr_fix);
5741 fragP->fr_fix += length;
5742 fragP->fr_var -= length;
5743 }
5744 bytes_added += length;
5745 }
5746 }
5747 frag_wane (fragP);
5748 }
5749 }
5750 }
5751 }
5752
5753
5754 size_t
5755 min_bytes_to_other_loop_end (fragP, current_target, current_offset, max_size)
5756 fragS *fragP;
5757 fragS *current_target;
5758 offsetT current_offset;
5759 size_t max_size;
5760 {
5761 size_t offset = 0;
5762 fragS *current_fragP;
5763
5764 for (current_fragP = fragP;
5765 current_fragP;
5766 current_fragP = current_fragP->fr_next)
5767 {
5768 if (current_fragP->tc_frag_data.is_loop_target
5769 && current_fragP != current_target)
5770 return offset + current_offset;
5771
5772 offset += unrelaxed_frag_min_size (current_fragP);
5773
5774 if (offset + current_offset >= max_size)
5775 return max_size;
5776 }
5777 return max_size;
5778 }
5779
5780
5781 size_t
5782 unrelaxed_frag_min_size (fragP)
5783 fragS * fragP;
5784 {
5785 size_t size = fragP->fr_fix;
5786
5787 /* add fill size */
5788 if (fragP->fr_type == rs_fill)
5789 size += fragP->fr_offset;
5790
5791 return size;
5792 }
5793
5794
5795 /* Re-process all of the fragments looking to convert all
5796 of the RELAX_ADD_NOP_IF_SHORT_LOOP. If:
5797
5798 A)
5799 1) the instruction size count to the loop end label
5800 is too short (<= 2 instructions),
5801 2) loop has a jump or branch in it
5802
5803 or B)
5804 1) software_avoid_all_short_loops is true
5805 2) The generating loop was a 'loopgtz' or 'loopnez'
5806 3) the instruction size count to the loop end label is too short
5807 (<= 2 instructions)
5808 then convert this frag (and maybe the next one) to generate a NOP.
5809 In any case close it off with a .fill 0. */
5810
5811 static void
5812 xtensa_fix_short_loop_frags ()
5813 {
5814 frchainS *frchP;
5815
5816 /* When this routine is called, all of the subsections are still intact
5817 so we walk over subsections instead of sections. */
5818 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5819 {
5820 fragS *fragP;
5821 fragS *current_target = NULL;
5822 offsetT current_offset = 0;
5823 xtensa_opcode current_opcode = XTENSA_UNDEFINED;
5824
5825 /* Walk over all of the fragments in a subsection. */
5826 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5827 {
5828 /* check on the current loop */
5829 if (fragP->fr_type == rs_machine_dependent
5830 && fragP->fr_subtype == RELAX_IMMED)
5831 {
5832 /* Read it. If the instruction is a loop, get the target. */
5833 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_opcode);
5834 if (is_loop_opcode (opcode))
5835 {
5836 TInsn t_insn;
5837
5838 tinsn_from_chars (&t_insn, fragP->fr_opcode);
5839 tinsn_immed_from_frag (&t_insn, fragP);
5840
5841 /* Get the current fragment target. */
5842 if (fragP->fr_symbol)
5843 {
5844 current_target = symbol_get_frag (fragP->fr_symbol);
5845 current_offset = fragP->fr_offset;
5846 current_opcode = opcode;
5847 }
5848 }
5849 }
5850
5851 if (fragP->fr_type == rs_machine_dependent
5852 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
5853 {
5854 size_t insn_count =
5855 count_insns_to_loop_end (fragP->fr_next, TRUE, 3);
5856 if (insn_count < 3
5857 && (branch_before_loop_end (fragP->fr_next)
5858 || (software_avoid_all_short_loops
5859 && current_opcode != XTENSA_UNDEFINED
5860 && !is_the_loop_opcode (current_opcode))))
5861 relax_frag_add_nop (fragP);
5862 else
5863 frag_wane (fragP);
5864 }
5865 }
5866 }
5867 }
5868
5869
5870 size_t
5871 count_insns_to_loop_end (base_fragP, count_relax_add, max_count)
5872 fragS *base_fragP;
5873 bfd_boolean count_relax_add;
5874 size_t max_count;
5875 {
5876 fragS *fragP = NULL;
5877 size_t insn_count = 0;
5878
5879 fragP = base_fragP;
5880
5881 for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next)
5882 {
5883 insn_count += unrelaxed_frag_min_insn_count (fragP);
5884 if (insn_count >= max_count)
5885 return max_count;
5886
5887 if (count_relax_add)
5888 {
5889 if (fragP->fr_type == rs_machine_dependent
5890 && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
5891 {
5892 /* In order to add the appropriate number of
5893 NOPs, we count an instruction for downstream
5894 occurrences. */
5895 insn_count++;
5896 if (insn_count >= max_count)
5897 return max_count;
5898 }
5899 }
5900 }
5901 return insn_count;
5902 }
5903
5904
5905 size_t
5906 unrelaxed_frag_min_insn_count (fragP)
5907 fragS *fragP;
5908 {
5909 size_t insn_count = 0;
5910 int offset = 0;
5911
5912 if (!fragP->tc_frag_data.is_insn)
5913 return insn_count;
5914
5915 /* Decode the fixed instructions. */
5916 while (offset < fragP->fr_fix)
5917 {
5918 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_literal + offset);
5919 if (opcode == XTENSA_UNDEFINED)
5920 {
5921 as_fatal (_("undecodable instruction in instruction frag"));
5922 return insn_count;
5923 }
5924 offset += xtensa_insn_length (xtensa_default_isa, opcode);
5925 insn_count++;
5926 }
5927
5928 return insn_count;
5929 }
5930
5931
5932 bfd_boolean
5933 branch_before_loop_end (base_fragP)
5934 fragS *base_fragP;
5935 {
5936 fragS *fragP;
5937
5938 for (fragP = base_fragP;
5939 fragP && !fragP->tc_frag_data.is_loop_target;
5940 fragP = fragP->fr_next)
5941 {
5942 if (unrelaxed_frag_has_b_j (fragP))
5943 return TRUE;
5944 }
5945 return FALSE;
5946 }
5947
5948
5949 bfd_boolean
5950 unrelaxed_frag_has_b_j (fragP)
5951 fragS *fragP;
5952 {
5953 size_t insn_count = 0;
5954 int offset = 0;
5955
5956 if (!fragP->tc_frag_data.is_insn)
5957 return FALSE;
5958
5959 /* Decode the fixed instructions. */
5960 while (offset < fragP->fr_fix)
5961 {
5962 xtensa_opcode opcode = get_opcode_from_buf (fragP->fr_literal + offset);
5963 if (opcode == XTENSA_UNDEFINED)
5964 {
5965 as_fatal (_("undecodable instruction in instruction frag"));
5966 return insn_count;
5967 }
5968 if (is_branch_or_jump_opcode (opcode))
5969 return TRUE;
5970 offset += xtensa_insn_length (xtensa_default_isa, opcode);
5971 }
5972 return FALSE;
5973 }
5974
5975
5976 /* Checks to be made after initial assembly but before relaxation. */
5977
5978 static void
5979 xtensa_sanity_check ()
5980 {
5981 char *file_name;
5982 int line;
5983
5984 frchainS *frchP;
5985
5986 as_where (&file_name, &line);
5987 for (frchP = frchain_root; frchP; frchP = frchP->frch_next)
5988 {
5989 fragS *fragP;
5990
5991 /* Walk over all of the fragments in a subsection. */
5992 for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
5993 {
5994 /* Currently we only check for empty loops here. */
5995 if (fragP->fr_type == rs_machine_dependent
5996 && fragP->fr_subtype == RELAX_IMMED)
5997 {
5998 static xtensa_insnbuf insnbuf = NULL;
5999 TInsn t_insn;
6000
6001 if (fragP->fr_opcode != NULL)
6002 {
6003 if (!insnbuf)
6004 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6005 tinsn_from_chars (&t_insn, fragP->fr_opcode);
6006 tinsn_immed_from_frag (&t_insn, fragP);
6007
6008 if (is_loop_opcode (t_insn.opcode))
6009 {
6010 if (is_empty_loop (&t_insn, fragP))
6011 {
6012 new_logical_line (fragP->fr_file, fragP->fr_line);
6013 as_bad (_("invalid empty loop"));
6014 }
6015 if (!is_local_forward_loop (&t_insn, fragP))
6016 {
6017 new_logical_line (fragP->fr_file, fragP->fr_line);
6018 as_bad (_("loop target does not follow "
6019 "loop instruction in section"));
6020 }
6021 }
6022 }
6023 }
6024 }
6025 }
6026 new_logical_line (file_name, line);
6027 }
6028
6029
6030 #define LOOP_IMMED_OPN 1
6031
6032 /* Return true if the loop target is the next non-zero fragment. */
6033
6034 bfd_boolean
6035 is_empty_loop (insn, fragP)
6036 const TInsn *insn;
6037 fragS *fragP;
6038 {
6039 const expressionS *expr;
6040 symbolS *symbolP;
6041 fragS *next_fragP;
6042
6043 if (insn->insn_type != ITYPE_INSN)
6044 return FALSE;
6045
6046 if (!is_loop_opcode (insn->opcode))
6047 return FALSE;
6048
6049 if (insn->ntok <= LOOP_IMMED_OPN)
6050 return FALSE;
6051
6052 expr = &insn->tok[LOOP_IMMED_OPN];
6053
6054 if (expr->X_op != O_symbol)
6055 return FALSE;
6056
6057 symbolP = expr->X_add_symbol;
6058 if (!symbolP)
6059 return FALSE;
6060
6061 if (symbol_get_frag (symbolP) == NULL)
6062 return FALSE;
6063
6064 if (S_GET_VALUE (symbolP) != 0)
6065 return FALSE;
6066
6067 /* Walk through the zero-size fragments from this one. If we find
6068 the target fragment, then this is a zero-size loop. */
6069 for (next_fragP = fragP->fr_next;
6070 next_fragP != NULL;
6071 next_fragP = next_fragP->fr_next)
6072 {
6073 if (next_fragP == symbol_get_frag (symbolP))
6074 return TRUE;
6075 if (next_fragP->fr_fix != 0)
6076 return FALSE;
6077 }
6078 return FALSE;
6079 }
6080
6081
6082 bfd_boolean
6083 is_local_forward_loop (insn, fragP)
6084 const TInsn *insn;
6085 fragS *fragP;
6086 {
6087 const expressionS *expr;
6088 symbolS *symbolP;
6089 fragS *next_fragP;
6090
6091 if (insn->insn_type != ITYPE_INSN)
6092 return FALSE;
6093
6094 if (!is_loop_opcode (insn->opcode))
6095 return FALSE;
6096
6097 if (insn->ntok <= LOOP_IMMED_OPN)
6098 return FALSE;
6099
6100 expr = &insn->tok[LOOP_IMMED_OPN];
6101
6102 if (expr->X_op != O_symbol)
6103 return FALSE;
6104
6105 symbolP = expr->X_add_symbol;
6106 if (!symbolP)
6107 return FALSE;
6108
6109 if (symbol_get_frag (symbolP) == NULL)
6110 return FALSE;
6111
6112 /* Walk through fragments until we find the target.
6113 If we do not find the target, then this is an invalid loop. */
6114 for (next_fragP = fragP->fr_next;
6115 next_fragP != NULL;
6116 next_fragP = next_fragP->fr_next)
6117 if (next_fragP == symbol_get_frag (symbolP))
6118 return TRUE;
6119
6120 return FALSE;
6121 }
6122
6123 \f
6124 /* Alignment Functions. */
6125
6126 size_t
6127 get_text_align_power (target_size)
6128 int target_size;
6129 {
6130 size_t i = 0;
6131 for (i = 0; i < sizeof (size_t); i++)
6132 {
6133 if (target_size <= (1 << i))
6134 return i;
6135 }
6136 as_fatal (_("get_text_align_power: argument too large"));
6137 return 0;
6138 }
6139
6140
6141 addressT
6142 get_text_align_max_fill_size (align_pow, use_nops, use_no_density)
6143 int align_pow;
6144 bfd_boolean use_nops;
6145 bfd_boolean use_no_density;
6146 {
6147 if (!use_nops)
6148 return (1 << align_pow);
6149 if (use_no_density)
6150 return 3 * (1 << align_pow);
6151
6152 return 1 + (1 << align_pow);
6153 }
6154
6155
6156 /* get_text_align_fill_size ()
6157
6158 Desired alignments:
6159 give the address
6160 target_size = size of next instruction
6161 align_pow = get_text_align_power (target_size).
6162 use_nops = 0
6163 use_no_density = 0;
6164 Loop alignments:
6165 address = current address + loop instruction size;
6166 target_size = 3 (for 2 or 3 byte target)
6167 = 8 (for 8 byte target)
6168 align_pow = get_text_align_power (target_size);
6169 use_nops = 1
6170 use_no_density = set appropriately
6171 Text alignments:
6172 address = current address + loop instruction size;
6173 target_size = 0
6174 align_pow = get_text_align_power (target_size);
6175 use_nops = 0
6176 use_no_density = 0. */
6177
6178 addressT
6179 get_text_align_fill_size (address, align_pow, target_size,
6180 use_nops, use_no_density)
6181 addressT address;
6182 int align_pow;
6183 int target_size;
6184 bfd_boolean use_nops;
6185 bfd_boolean use_no_density;
6186 {
6187 /* Input arguments:
6188
6189 align_pow: log2 (required alignment).
6190
6191 target_size: alignment must allow the new_address and
6192 new_address+target_size-1.
6193
6194 use_nops: if true, then we can only use 2 or 3 byte nops.
6195
6196 use_no_density: if use_nops and use_no_density, we can only use
6197 3-byte nops.
6198
6199 Usually, for non-zero target_size, the align_pow is the power of 2
6200 that is greater than or equal to the target_size. This handles the
6201 2-byte, 3-byte and 8-byte instructions. */
6202
6203 size_t alignment = (1 << align_pow);
6204 if (!use_nops)
6205 {
6206 /* This is the easy case. */
6207 size_t mod;
6208 mod = address % alignment;
6209 if (mod != 0)
6210 mod = alignment - mod;
6211 assert ((address + mod) % alignment == 0);
6212 return mod;
6213 }
6214
6215 /* This is the slightly harder case. */
6216 assert ((int) alignment >= target_size);
6217 assert (target_size > 0);
6218 if (!use_no_density)
6219 {
6220 size_t i;
6221 for (i = 0; i < alignment * 2; i++)
6222 {
6223 if (i == 1)
6224 continue;
6225 if ((address + i) >> align_pow ==
6226 (address + i + target_size - 1) >> align_pow)
6227 return i;
6228 }
6229 }
6230 else
6231 {
6232 size_t i;
6233
6234 /* Can only fill multiples of 3. */
6235 for (i = 0; i <= alignment * 3; i += 3)
6236 {
6237 if ((address + i) >> align_pow ==
6238 (address + i + target_size - 1) >> align_pow)
6239 return i;
6240 }
6241 }
6242 assert (0);
6243 return 0;
6244 }
6245
6246
6247 /* This will assert if it is not possible. */
6248
6249 size_t
6250 get_text_align_nop_count (fill_size, use_no_density)
6251 size_t fill_size;
6252 bfd_boolean use_no_density;
6253 {
6254 size_t count = 0;
6255 if (use_no_density)
6256 {
6257 assert (fill_size % 3 == 0);
6258 return (fill_size / 3);
6259 }
6260
6261 assert (fill_size != 1); /* Bad argument. */
6262
6263 while (fill_size > 1)
6264 {
6265 size_t insn_size = 3;
6266 if (fill_size == 2 || fill_size == 4)
6267 insn_size = 2;
6268 fill_size -= insn_size;
6269 count++;
6270 }
6271 assert (fill_size != 1); /* Bad algorithm. */
6272 return count;
6273 }
6274
6275
6276 size_t
6277 get_text_align_nth_nop_size (fill_size, n, use_no_density)
6278 size_t fill_size;
6279 size_t n;
6280 bfd_boolean use_no_density;
6281 {
6282 size_t count = 0;
6283
6284 assert (get_text_align_nop_count (fill_size, use_no_density) > n);
6285
6286 if (use_no_density)
6287 return 3;
6288
6289 while (fill_size > 1)
6290 {
6291 size_t insn_size = 3;
6292 if (fill_size == 2 || fill_size == 4)
6293 insn_size = 2;
6294 fill_size -= insn_size;
6295 count++;
6296 if (n + 1 == count)
6297 return insn_size;
6298 }
6299 assert (0);
6300 return 0;
6301 }
6302
6303
6304 /* For the given fragment, find the appropriate address
6305 for it to begin at if we are using NOPs to align it. */
6306
6307 static addressT
6308 get_noop_aligned_address (fragP, address)
6309 fragS *fragP;
6310 addressT address;
6311 {
6312 static xtensa_insnbuf insnbuf = NULL;
6313 size_t fill_size = 0;
6314
6315 if (!insnbuf)
6316 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6317
6318 switch (fragP->fr_type)
6319 {
6320 case rs_machine_dependent:
6321 if (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
6322 {
6323 /* The rule is: get next fragment's FIRST instruction. Find
6324 the smallest number of bytes that need to be added to
6325 ensure that the next fragment's FIRST instruction will fit
6326 in a single word.
6327
6328 E.G., 2 bytes : 0, 1, 2 mod 4
6329 3 bytes: 0, 1 mod 4
6330
6331 If the FIRST instruction MIGHT be relaxed,
6332 assume that it will become a 3 byte instruction. */
6333
6334 int target_insn_size;
6335 xtensa_opcode opcode = next_frag_opcode (fragP);
6336 addressT pre_opcode_bytes;
6337
6338 if (opcode == XTENSA_UNDEFINED)
6339 {
6340 as_bad_where (fragP->fr_file, fragP->fr_line,
6341 _("invalid opcode for RELAX_ALIGN_NEXT_OPCODE"));
6342 as_fatal (_("cannot continue"));
6343 }
6344
6345 target_insn_size = xtensa_insn_length (xtensa_default_isa, opcode);
6346
6347 pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP);
6348
6349 if (is_loop_opcode (opcode))
6350 {
6351 /* next_fragP should be the loop. */
6352 const fragS *next_fragP = next_non_empty_frag (fragP);
6353 xtensa_opcode next_opcode = next_frag_opcode (next_fragP);
6354 size_t alignment;
6355
6356 pre_opcode_bytes += target_insn_size;
6357
6358 /* For loops, the alignment depends on the size of the
6359 instruction following the loop, not the loop instruction. */
6360 if (next_opcode == XTENSA_UNDEFINED)
6361 target_insn_size = 3;
6362 else
6363 {
6364 target_insn_size =
6365 xtensa_insn_length (xtensa_default_isa, next_opcode);
6366
6367 if (target_insn_size == 2)
6368 target_insn_size = 3; /* ISA specifies this. */
6369 }
6370
6371 /* If it was 8, then we'll need a larger alignment
6372 for the section. */
6373 alignment = get_text_align_power (target_insn_size);
6374
6375 /* Is Now_seg valid */
6376 record_alignment (now_seg, alignment);
6377 }
6378 else
6379 as_fatal (_("expected loop opcode in relax align next target"));
6380
6381 fill_size = get_text_align_fill_size
6382 (address + pre_opcode_bytes,
6383 get_text_align_power (target_insn_size),
6384 target_insn_size, TRUE, fragP->tc_frag_data.is_no_density);
6385 }
6386 break;
6387 #if 0
6388 case rs_align:
6389 case rs_align_code:
6390 fill_size = get_text_align_fill_size
6391 (address, fragP->fr_offset, 1, TRUE,
6392 fragP->tc_frag_data.is_no_density);
6393 break;
6394 #endif
6395 default:
6396 as_fatal (_("expected align_code or RELAX_ALIGN_NEXT_OPCODE"));
6397 }
6398
6399 return address + fill_size;
6400 }
6401
6402
6403 /* 3 mechanisms for relaxing an alignment:
6404
6405 Align to a power of 2.
6406 Align so the next fragment's instruction does not cross a word boundary.
6407 Align the current instruction so that if the next instruction
6408 were 3 bytes, it would not cross a word boundary.
6409
6410 We can align with:
6411
6412 zeros - This is easy; always insert zeros.
6413 nops - 3 and 2 byte instructions
6414 2 - 2 byte nop
6415 3 - 3 byte nop
6416 4 - 2, 2-byte nops
6417 >=5 : 3 byte instruction + fn(n-3)
6418 widening - widen previous instructions. */
6419
6420 static addressT
6421 get_widen_aligned_address (fragP, address)
6422 fragS *fragP;
6423 addressT address;
6424 {
6425 addressT align_pow, new_address, loop_insn_offset;
6426 fragS *next_frag;
6427 int insn_size;
6428 xtensa_opcode opcode, next_opcode;
6429 static xtensa_insnbuf insnbuf = NULL;
6430
6431 if (!insnbuf)
6432 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6433
6434 if (fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
6435 {
6436 align_pow = fragP->fr_offset;
6437 new_address = ((address + ((1 << align_pow) - 1))
6438 << align_pow) >> align_pow;
6439 return new_address;
6440 }
6441
6442 if (fragP->fr_type == rs_machine_dependent)
6443 {
6444 switch (fragP->fr_subtype)
6445 {
6446 case RELAX_DESIRE_ALIGN:
6447
6448 /* The rule is: get the next fragment's FIRST instruction.
6449 Find the smallest number of bytes needed to be added
6450 in order to ensure that the next fragment is FIRST
6451 instruction will fit in a single word.
6452 i.e. 2 bytes : 0, 1, 2. mod 4
6453 3 bytes: 0, 1 mod 4
6454 If the FIRST instruction MIGHT be relaxed,
6455 assume that it will become a 3-byte instruction. */
6456
6457 insn_size = 3;
6458 /* Check to see if it might be 2 bytes. */
6459 next_opcode = next_frag_opcode (fragP);
6460 if (next_opcode != XTENSA_UNDEFINED
6461 && xtensa_insn_length (xtensa_default_isa, next_opcode) == 2)
6462 insn_size = 2;
6463
6464 assert (insn_size <= 4);
6465 for (new_address = address; new_address < address + 4; new_address++)
6466 {
6467 if (new_address >> 2 == (new_address + insn_size - 1) >> 2)
6468 return new_address;
6469 }
6470 as_bad (_("internal error aligning"));
6471 return address;
6472
6473 case RELAX_ALIGN_NEXT_OPCODE:
6474 /* The rule is: get next fragment's FIRST instruction.
6475 Find the smallest number of bytes needed to be added
6476 in order to ensure that the next fragment's FIRST
6477 instruction will fit in a single word.
6478 i.e. 2 bytes : 0, 1, 2. mod 4
6479 3 bytes: 0, 1 mod 4
6480 If the FIRST instruction MIGHT be relaxed,
6481 assume that it will become a 3 byte instruction. */
6482
6483 opcode = next_frag_opcode (fragP);
6484 if (opcode == XTENSA_UNDEFINED)
6485 {
6486 as_bad_where (fragP->fr_file, fragP->fr_line,
6487 _("invalid opcode for RELAX_ALIGN_NEXT_OPCODE"));
6488 as_fatal (_("cannot continue"));
6489 }
6490 insn_size = xtensa_insn_length (xtensa_default_isa, opcode);
6491 assert (insn_size <= 4);
6492 assert (is_loop_opcode (opcode));
6493
6494 loop_insn_offset = 0;
6495 next_frag = next_non_empty_frag (fragP);
6496
6497 /* If the loop has been expanded then the loop
6498 instruction could be at an offset from this fragment. */
6499 if (next_frag->fr_subtype != RELAX_IMMED)
6500 loop_insn_offset = get_expanded_loop_offset (opcode);
6501
6502 for (new_address = address; new_address < address + 4; new_address++)
6503 {
6504 if ((new_address + loop_insn_offset + insn_size) >> 2 ==
6505 (new_address + loop_insn_offset + insn_size + 2) >> 2)
6506 return new_address;
6507 }
6508 as_bad (_("internal error aligning"));
6509 return address;
6510
6511 default:
6512 as_bad (_("internal error aligning"));
6513 return address;
6514 }
6515 }
6516 as_bad (_("internal error aligning"));
6517 return address;
6518 }
6519
6520 \f
6521 /* md_relax_frag Hook and Helper Functions. */
6522
6523 /* Return the number of bytes added to this fragment, given that the
6524 input has been stretched already by "stretch". */
6525
6526 long
6527 xtensa_relax_frag (fragP, stretch, stretched_p)
6528 fragS *fragP;
6529 long stretch;
6530 int *stretched_p;
6531 {
6532 int unreported = fragP->tc_frag_data.unreported_expansion;
6533 long new_stretch = 0;
6534 char *file_name;
6535 int line, lit_size;
6536
6537 as_where (&file_name, &line);
6538 new_logical_line (fragP->fr_file, fragP->fr_line);
6539
6540 fragP->tc_frag_data.unreported_expansion = 0;
6541
6542 switch (fragP->fr_subtype)
6543 {
6544 case RELAX_ALIGN_NEXT_OPCODE:
6545 /* Always convert. */
6546 new_stretch = relax_frag_text_align (fragP, stretch);
6547 break;
6548
6549 case RELAX_LOOP_END:
6550 /* Do nothing. */
6551 break;
6552
6553 case RELAX_LOOP_END_ADD_NOP:
6554 /* Add a NOP and switch to .fill 0. */
6555 new_stretch = relax_frag_add_nop (fragP);
6556 break;
6557
6558 case RELAX_DESIRE_ALIGN:
6559 /* We REALLY want to change the relaxation order here. This
6560 should do NOTHING. The narrowing before it will either align
6561 it or not. */
6562 break;
6563
6564 case RELAX_LITERAL:
6565 case RELAX_LITERAL_FINAL:
6566 return 0;
6567
6568 case RELAX_LITERAL_NR:
6569 lit_size = 4;
6570 fragP->fr_subtype = RELAX_LITERAL_FINAL;
6571 assert (unreported == lit_size);
6572 memset (&fragP->fr_literal[fragP->fr_fix], 0, 4);
6573 fragP->fr_var -= lit_size;
6574 fragP->fr_fix += lit_size;
6575 new_stretch = 4;
6576 break;
6577
6578 case RELAX_NARROW:
6579 new_stretch = relax_frag_narrow (fragP, stretch);
6580 break;
6581
6582 case RELAX_IMMED:
6583 case RELAX_IMMED_STEP1:
6584 case RELAX_IMMED_STEP2:
6585 /* Place the immediate. */
6586 new_stretch = relax_frag_immed (now_seg, fragP, stretch,
6587 fragP->fr_subtype - RELAX_IMMED,
6588 stretched_p);
6589 break;
6590
6591 case RELAX_LITERAL_POOL_BEGIN:
6592 case RELAX_LITERAL_POOL_END:
6593 /* No relaxation required. */
6594 break;
6595
6596 default:
6597 as_bad (_("bad relaxation state"));
6598 }
6599
6600 new_logical_line (file_name, line);
6601 return new_stretch;
6602 }
6603
6604
6605 static long
6606 relax_frag_text_align (fragP, stretch)
6607 fragS *fragP;
6608 long stretch;
6609 {
6610 addressT old_address, old_next_address, old_size;
6611 addressT new_address, new_next_address, new_size;
6612 addressT growth;
6613
6614 /* Overview of the relaxation procedure for alignment
6615 inside an executable section:
6616
6617 The old size is stored in the tc_frag_data.text_expansion field.
6618
6619 Calculate the new address, fix up the text_expansion and
6620 return the growth. */
6621
6622 /* Calculate the old address of this fragment and the next fragment. */
6623 old_address = fragP->fr_address - stretch;
6624 old_next_address = (fragP->fr_address - stretch + fragP->fr_fix +
6625 fragP->tc_frag_data.text_expansion);
6626 old_size = old_next_address - old_address;
6627
6628 /* Calculate the new address of this fragment and the next fragment. */
6629 new_address = fragP->fr_address;
6630 new_next_address =
6631 get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix);
6632 new_size = new_next_address - new_address;
6633
6634 growth = new_size - old_size;
6635
6636 /* Fix up the text_expansion field and return the new growth. */
6637 fragP->tc_frag_data.text_expansion += growth;
6638 return growth;
6639 }
6640
6641
6642 /* Add a NOP (i.e., "or a1, a1, a1"). Use the 3-byte one because we
6643 don't know about the availability of density yet. TODO: When the
6644 flags are stored per fragment, use NOP.N when possible. */
6645
6646 static long
6647 relax_frag_add_nop (fragP)
6648 fragS *fragP;
6649 {
6650 static xtensa_insnbuf insnbuf = NULL;
6651 TInsn t_insn;
6652 char *nop_buf = fragP->fr_literal + fragP->fr_fix;
6653 int length;
6654 if (!insnbuf)
6655 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6656
6657 tinsn_init (&t_insn);
6658 t_insn.opcode = xtensa_or_opcode;
6659 assert (t_insn.opcode != XTENSA_UNDEFINED);
6660
6661 t_insn.ntok = 3;
6662 set_expr_const (&t_insn.tok[0], 1);
6663 set_expr_const (&t_insn.tok[1], 1);
6664 set_expr_const (&t_insn.tok[2], 1);
6665
6666 tinsn_to_insnbuf (&t_insn, insnbuf);
6667 fragP->tc_frag_data.is_insn = TRUE;
6668 xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf, nop_buf);
6669
6670 length = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
6671 if (fragP->fr_var < length)
6672 {
6673 as_warn (_("fr_var (%ld) < length (%d); ignoring"),
6674 fragP->fr_var, length);
6675 frag_wane (fragP);
6676 return 0;
6677 }
6678
6679 fragP->fr_fix += length;
6680 fragP->fr_var -= length;
6681 frag_wane (fragP);
6682 return length;
6683 }
6684
6685
6686 static long
6687 relax_frag_narrow (fragP, stretch)
6688 fragS *fragP;
6689 long stretch;
6690 {
6691 /* Overview of the relaxation procedure for alignment inside an
6692 executable section: Find the number of widenings required and the
6693 number of nop bytes required. Store the number of bytes ALREADY
6694 widened. If there are enough instructions to widen (must go back
6695 ONLY through NARROW fragments), mark each of the fragments as TO BE
6696 widened, recalculate the fragment addresses. */
6697
6698 assert (fragP->fr_type == rs_machine_dependent
6699 && fragP->fr_subtype == RELAX_NARROW);
6700
6701 if (!future_alignment_required (fragP, 0))
6702 {
6703 /* If already expanded but no longer needed because of a prior
6704 stretch, it is SAFE to unexpand because the next fragment will
6705 NEVER start at an address > the previous time through the
6706 relaxation. */
6707 if (fragP->tc_frag_data.text_expansion)
6708 {
6709 if (stretch > 0)
6710 {
6711 fragP->tc_frag_data.text_expansion = 0;
6712 return -1;
6713 }
6714 /* Otherwise we have to live with this bad choice. */
6715 return 0;
6716 }
6717 return 0;
6718 }
6719
6720 if (fragP->tc_frag_data.text_expansion == 0)
6721 {
6722 fragP->tc_frag_data.text_expansion = 1;
6723 return 1;
6724 }
6725
6726 return 0;
6727 }
6728
6729
6730 static bfd_boolean
6731 future_alignment_required (fragP, stretch)
6732 fragS *fragP;
6733 long stretch;
6734 {
6735 long address = fragP->fr_address + stretch;
6736 int num_widens = 0;
6737 addressT aligned_address;
6738 offsetT desired_diff;
6739
6740 while (fragP)
6741 {
6742 /* Limit this to a small search. */
6743 if (num_widens > 8)
6744 return FALSE;
6745 address += fragP->fr_fix;
6746
6747 switch (fragP->fr_type)
6748 {
6749 case rs_fill:
6750 address += fragP->fr_offset * fragP->fr_var;
6751 break;
6752
6753 case rs_machine_dependent:
6754 switch (fragP->fr_subtype)
6755 {
6756 case RELAX_NARROW:
6757 /* address += fragP->fr_fix; */
6758 num_widens++;
6759 break;
6760
6761 case RELAX_IMMED:
6762 address += (/* fragP->fr_fix + */
6763 fragP->tc_frag_data.text_expansion);
6764 break;
6765
6766 case RELAX_ALIGN_NEXT_OPCODE:
6767 case RELAX_DESIRE_ALIGN:
6768 /* address += fragP->fr_fix; */
6769 aligned_address = get_widen_aligned_address (fragP, address);
6770 desired_diff = aligned_address - address;
6771 assert (desired_diff >= 0);
6772 /* If there are enough wideners in between do it. */
6773 /* return (num_widens == desired_diff); */
6774 if (num_widens == desired_diff)
6775 return TRUE;
6776 if (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
6777 return FALSE;
6778 break;
6779
6780 default:
6781 return FALSE;
6782 }
6783 break;
6784
6785 default:
6786 return FALSE;
6787 }
6788 fragP = fragP->fr_next;
6789 }
6790
6791 return FALSE;
6792 }
6793
6794
6795 static long
6796 relax_frag_immed (segP, fragP, stretch, min_steps, stretched_p)
6797 segT segP;
6798 fragS *fragP;
6799 long stretch;
6800 int min_steps;
6801 int *stretched_p;
6802 {
6803 static xtensa_insnbuf insnbuf = NULL;
6804 TInsn t_insn;
6805 int old_size;
6806 bfd_boolean negatable_branch = FALSE;
6807 bfd_boolean branch_jmp_to_next = FALSE;
6808 IStack istack;
6809 offsetT frag_offset;
6810 int num_steps;
6811 fragS *lit_fragP;
6812 int num_text_bytes, num_literal_bytes;
6813 int literal_diff, text_diff;
6814
6815 assert (fragP->fr_opcode != NULL);
6816
6817 if (!insnbuf)
6818 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
6819
6820 tinsn_from_chars (&t_insn, fragP->fr_opcode);
6821 tinsn_immed_from_frag (&t_insn, fragP);
6822
6823 negatable_branch = is_negatable_branch (&t_insn);
6824
6825 old_size = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
6826
6827 if (software_avoid_b_j_loop_end)
6828 branch_jmp_to_next = is_branch_jmp_to_next (&t_insn, fragP);
6829
6830 /* Special case: replace a branch to the next instruction with a NOP.
6831 This is required to work around a hardware bug in T1040.0 and also
6832 serves as an optimization. */
6833
6834 if (branch_jmp_to_next
6835 && ((old_size == 2) || (old_size == 3))
6836 && !next_frag_is_loop_target (fragP))
6837 return 0;
6838
6839 /* Here is the fun stuff: Get the immediate field from this
6840 instruction. If it fits, we are done. If not, find the next
6841 instruction sequence that fits. */
6842
6843 frag_offset = fragP->fr_opcode - fragP->fr_literal;
6844 istack_init (&istack);
6845 num_steps = xg_assembly_relax (&istack, &t_insn, segP, fragP, frag_offset,
6846 min_steps, stretch);
6847 if (num_steps < min_steps)
6848 {
6849 as_fatal (_("internal error: relaxation failed"));
6850 return 0;
6851 }
6852
6853 if (num_steps > RELAX_IMMED_MAXSTEPS)
6854 {
6855 as_fatal (_("internal error: relaxation requires too many steps"));
6856 return 0;
6857 }
6858
6859 fragP->fr_subtype = (int) RELAX_IMMED + num_steps;
6860
6861 /* Figure out the number of bytes needed. */
6862 lit_fragP = 0;
6863 num_text_bytes = get_num_stack_text_bytes (&istack) - old_size;
6864 num_literal_bytes = get_num_stack_literal_bytes (&istack);
6865 literal_diff = num_literal_bytes - fragP->tc_frag_data.literal_expansion;
6866 text_diff = num_text_bytes - fragP->tc_frag_data.text_expansion;
6867
6868 /* It MUST get larger. If not, we could get an infinite loop. */
6869 know (num_text_bytes >= 0);
6870 know (literal_diff >= 0 && text_diff >= 0);
6871
6872 fragP->tc_frag_data.text_expansion = num_text_bytes;
6873 fragP->tc_frag_data.literal_expansion = num_literal_bytes;
6874
6875 /* Find the associated expandable literal for this. */
6876 if (literal_diff != 0)
6877 {
6878 lit_fragP = fragP->tc_frag_data.literal_frag;
6879 if (lit_fragP)
6880 {
6881 assert (literal_diff == 4);
6882 lit_fragP->tc_frag_data.unreported_expansion += literal_diff;
6883
6884 /* We expect that the literal section state has NOT been
6885 modified yet. */
6886 assert (lit_fragP->fr_type == rs_machine_dependent
6887 && lit_fragP->fr_subtype == RELAX_LITERAL);
6888 lit_fragP->fr_subtype = RELAX_LITERAL_NR;
6889
6890 /* We need to mark this section for another iteration
6891 of relaxation. */
6892 (*stretched_p)++;
6893 }
6894 }
6895
6896 /* This implicitly uses the assumption that a branch is negated
6897 when the size of the output increases by at least 2 bytes. */
6898
6899 if (negatable_branch && num_text_bytes >= 2)
6900 {
6901 /* If next frag is a loop end, then switch it to add a NOP. */
6902 update_next_frag_nop_state (fragP);
6903 }
6904
6905 return text_diff;
6906 }
6907
6908 \f
6909 /* md_convert_frag Hook and Helper Functions. */
6910
6911 void
6912 md_convert_frag (abfd, sec, fragp)
6913 bfd *abfd ATTRIBUTE_UNUSED;
6914 segT sec;
6915 fragS *fragp;
6916 {
6917 char *file_name;
6918 int line;
6919
6920 as_where (&file_name, &line);
6921 new_logical_line (fragp->fr_file, fragp->fr_line);
6922
6923 switch (fragp->fr_subtype)
6924 {
6925 case RELAX_ALIGN_NEXT_OPCODE:
6926 /* Always convert. */
6927 convert_frag_align_next_opcode (fragp);
6928 break;
6929
6930 case RELAX_DESIRE_ALIGN:
6931 /* Do nothing. If not aligned already, too bad. */
6932 break;
6933
6934 case RELAX_LITERAL:
6935 case RELAX_LITERAL_FINAL:
6936 break;
6937
6938 case RELAX_NARROW:
6939 /* No conversion. */
6940 convert_frag_narrow (fragp);
6941 break;
6942
6943 case RELAX_IMMED:
6944 case RELAX_IMMED_STEP1:
6945 case RELAX_IMMED_STEP2:
6946 /* Place the immediate. */
6947 convert_frag_immed (sec, fragp, fragp->fr_subtype - RELAX_IMMED);
6948 break;
6949
6950 case RELAX_LITERAL_NR:
6951 if (use_literal_section)
6952 {
6953 /* This should have been handled during relaxation. When
6954 relaxing a code segment, literals sometimes need to be
6955 added to the corresponding literal segment. If that
6956 literal segment has already been relaxed, then we end up
6957 in this situation. Marking the literal segments as data
6958 would make this happen less often (since GAS always relaxes
6959 code before data), but we could still get into trouble if
6960 there are instructions in a segment that is not marked as
6961 containing code. Until we can implement a better solution,
6962 cheat and adjust the addresses of all the following frags.
6963 This could break subsequent alignments, but the linker's
6964 literal coalescing will do that anyway. */
6965
6966 fragS *f;
6967 fragp->fr_subtype = RELAX_LITERAL_FINAL;
6968 assert (fragp->tc_frag_data.unreported_expansion == 4);
6969 memset (&fragp->fr_literal[fragp->fr_fix], 0, 4);
6970 fragp->fr_var -= 4;
6971 fragp->fr_fix += 4;
6972 for (f = fragp->fr_next; f; f = f->fr_next)
6973 f->fr_address += 4;
6974 }
6975 else
6976 as_bad (_("invalid relaxation fragment result"));
6977 break;
6978 }
6979
6980 fragp->fr_var = 0;
6981 new_logical_line (file_name, line);
6982 }
6983
6984
6985 void
6986 convert_frag_align_next_opcode (fragp)
6987 fragS *fragp;
6988 {
6989 char *nop_buf; /* Location for Writing. */
6990 size_t i;
6991
6992 bfd_boolean use_no_density = fragp->tc_frag_data.is_no_density;
6993 addressT aligned_address;
6994 size_t fill_size, nop_count;
6995
6996 aligned_address = get_noop_aligned_address (fragp, fragp->fr_address +
6997 fragp->fr_fix);
6998 fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix);
6999 nop_count = get_text_align_nop_count (fill_size, use_no_density);
7000 nop_buf = fragp->fr_literal + fragp->fr_fix;
7001
7002 for (i = 0; i < nop_count; i++)
7003 {
7004 size_t nop_size;
7005 nop_size = get_text_align_nth_nop_size (fill_size, i, use_no_density);
7006
7007 assemble_nop (nop_size, nop_buf);
7008 nop_buf += nop_size;
7009 }
7010
7011 fragp->fr_fix += fill_size;
7012 fragp->fr_var -= fill_size;
7013 }
7014
7015
7016 static void
7017 convert_frag_narrow (fragP)
7018 fragS *fragP;
7019 {
7020 static xtensa_insnbuf insnbuf = NULL;
7021 TInsn t_insn, single_target;
7022 int size, old_size, diff, error_val;
7023 offsetT frag_offset;
7024
7025 if (fragP->tc_frag_data.text_expansion == 0)
7026 {
7027 /* No conversion. */
7028 fragP->fr_var = 0;
7029 return;
7030 }
7031
7032 assert (fragP->fr_opcode != NULL);
7033
7034 if (!insnbuf)
7035 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
7036
7037 tinsn_from_chars (&t_insn, fragP->fr_opcode);
7038 tinsn_immed_from_frag (&t_insn, fragP);
7039
7040 /* Just convert it to a wide form.... */
7041 size = 0;
7042 old_size = xtensa_insn_length (xtensa_default_isa, t_insn.opcode);
7043
7044 tinsn_init (&single_target);
7045 frag_offset = fragP->fr_opcode - fragP->fr_literal;
7046
7047 error_val = xg_expand_narrow (&single_target, &t_insn);
7048 if (error_val)
7049 as_bad (_("unable to widen instruction"));
7050
7051 size = xtensa_insn_length (xtensa_default_isa, single_target.opcode);
7052 xg_emit_insn_to_buf (&single_target, fragP->fr_opcode,
7053 fragP, frag_offset, TRUE);
7054
7055 diff = size - old_size;
7056 assert (diff >= 0);
7057 assert (diff <= fragP->fr_var);
7058 fragP->fr_var -= diff;
7059 fragP->fr_fix += diff;
7060
7061 /* clean it up */
7062 fragP->fr_var = 0;
7063 }
7064
7065
7066 static void
7067 convert_frag_immed (segP, fragP, min_steps)
7068 segT segP;
7069 fragS *fragP;
7070 int min_steps;
7071 {
7072 char *immed_instr = fragP->fr_opcode;
7073 static xtensa_insnbuf insnbuf = NULL;
7074 TInsn orig_t_insn;
7075 bfd_boolean expanded = FALSE;
7076 char *fr_opcode = fragP->fr_opcode;
7077 bfd_boolean branch_jmp_to_next = FALSE;
7078 int size;
7079
7080 assert (fragP->fr_opcode != NULL);
7081
7082 if (!insnbuf)
7083 insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
7084
7085 tinsn_from_chars (&orig_t_insn, fragP->fr_opcode);
7086 tinsn_immed_from_frag (&orig_t_insn, fragP);
7087
7088 /* Here is the fun stuff: Get the immediate field from this
7089 instruction. If it fits, we're done. If not, find the next
7090 instruction sequence that fits. */
7091
7092 if (software_avoid_b_j_loop_end)
7093 branch_jmp_to_next = is_branch_jmp_to_next (&orig_t_insn, fragP);
7094
7095 if (branch_jmp_to_next && !next_frag_is_loop_target (fragP))
7096 {
7097 /* Conversion just inserts a NOP and marks the fix as completed. */
7098 size = xtensa_insn_length (xtensa_default_isa, orig_t_insn.opcode);
7099 assemble_nop (size, fragP->fr_opcode);
7100 fragP->fr_var = 0;
7101 }
7102 else
7103 {
7104 IStack istack;
7105 int i;
7106 symbolS *lit_sym = NULL;
7107 int total_size = 0;
7108 int old_size;
7109 int diff;
7110 symbolS *gen_label = NULL;
7111 offsetT frag_offset;
7112
7113 /* It does not fit. Find something that does and
7114 convert immediately. */
7115 frag_offset = fragP->fr_opcode - fragP->fr_literal;
7116 istack_init (&istack);
7117 xg_assembly_relax (&istack, &orig_t_insn,
7118 segP, fragP, frag_offset, min_steps, 0);
7119
7120 old_size = xtensa_insn_length (xtensa_default_isa, orig_t_insn.opcode);
7121
7122 /* Assemble this right inline. */
7123
7124 /* First, create the mapping from a label name to the REAL label. */
7125 total_size = 0;
7126 for (i = 0; i < istack.ninsn; i++)
7127 {
7128 TInsn *t_insn = &istack.insn[i];
7129 int size = 0;
7130 fragS *lit_frag;
7131
7132 switch (t_insn->insn_type)
7133 {
7134 case ITYPE_LITERAL:
7135 if (lit_sym != NULL)
7136 as_bad (_("multiple literals in expansion"));
7137 /* First find the appropriate space in the literal pool. */
7138 lit_frag = fragP->tc_frag_data.literal_frag;
7139 if (lit_frag == NULL)
7140 as_bad (_("no registered fragment for literal"));
7141 if (t_insn->ntok != 1)
7142 as_bad (_("number of literal tokens != 1"));
7143
7144 /* Set the literal symbol and add a fixup. */
7145 lit_sym = lit_frag->fr_symbol;
7146 break;
7147
7148 case ITYPE_LABEL:
7149 assert (gen_label == NULL);
7150 gen_label = symbol_new (FAKE_LABEL_NAME, now_seg,
7151 fragP->fr_opcode - fragP->fr_literal +
7152 total_size, fragP);
7153 break;
7154
7155 case ITYPE_INSN:
7156 size = xtensa_insn_length (xtensa_default_isa, t_insn->opcode);
7157 total_size += size;
7158 break;
7159 }
7160 }
7161
7162 total_size = 0;
7163 for (i = 0; i < istack.ninsn; i++)
7164 {
7165 TInsn *t_insn = &istack.insn[i];
7166 fragS *lit_frag;
7167 int size;
7168 segT target_seg;
7169
7170 switch (t_insn->insn_type)
7171 {
7172 case ITYPE_LITERAL:
7173 lit_frag = fragP->tc_frag_data.literal_frag;
7174 /* already checked */
7175 assert (lit_frag != NULL);
7176 assert (lit_sym != NULL);
7177 assert (t_insn->ntok == 1);
7178 /* add a fixup */
7179 target_seg = S_GET_SEGMENT (lit_sym);
7180 assert (target_seg);
7181 fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4,
7182 &t_insn->tok[0], FALSE, BFD_RELOC_32);
7183 break;
7184
7185 case ITYPE_LABEL:
7186 break;
7187
7188 case ITYPE_INSN:
7189 xg_resolve_labels (t_insn, gen_label);
7190 xg_resolve_literals (t_insn, lit_sym);
7191 size = xtensa_insn_length (xtensa_default_isa, t_insn->opcode);
7192 total_size += size;
7193 xg_emit_insn_to_buf (t_insn, immed_instr, fragP,
7194 immed_instr - fragP->fr_literal, TRUE);
7195 immed_instr += size;
7196 break;
7197 }
7198 }
7199
7200 diff = total_size - old_size;
7201 assert (diff >= 0);
7202 if (diff != 0)
7203 expanded = TRUE;
7204 assert (diff <= fragP->fr_var);
7205 fragP->fr_var -= diff;
7206 fragP->fr_fix += diff;
7207 }
7208
7209 /* Clean it up. */
7210 fragP->fr_var = 0;
7211
7212 /* Check for undefined immediates in LOOP instructions. */
7213 if (is_loop_opcode (orig_t_insn.opcode))
7214 {
7215 symbolS *sym;
7216 sym = orig_t_insn.tok[1].X_add_symbol;
7217 if (sym != NULL && !S_IS_DEFINED (sym))
7218 {
7219 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
7220 return;
7221 }
7222 sym = orig_t_insn.tok[1].X_op_symbol;
7223 if (sym != NULL && !S_IS_DEFINED (sym))
7224 {
7225 as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
7226 return;
7227 }
7228 }
7229
7230 if (expanded && is_loop_opcode (orig_t_insn.opcode))
7231 convert_frag_immed_finish_loop (segP, fragP, &orig_t_insn);
7232
7233 if (expanded && is_direct_call_opcode (orig_t_insn.opcode))
7234 {
7235 /* Add an expansion note on the expanded instruction. */
7236 fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4,
7237 &orig_t_insn.tok[0], TRUE,
7238 BFD_RELOC_XTENSA_ASM_EXPAND);
7239
7240 }
7241 }
7242
7243
7244 /* Add a new fix expression into the desired segment. We have to
7245 switch to that segment to do this. */
7246
7247 static fixS *
7248 fix_new_exp_in_seg (new_seg, new_subseg,
7249 frag, where, size, exp, pcrel, r_type)
7250 segT new_seg;
7251 subsegT new_subseg;
7252 fragS *frag;
7253 int where;
7254 int size;
7255 expressionS *exp;
7256 int pcrel;
7257 bfd_reloc_code_real_type r_type;
7258 {
7259 fixS *new_fix;
7260 segT seg = now_seg;
7261 subsegT subseg = now_subseg;
7262 assert (new_seg != 0);
7263 subseg_set (new_seg, new_subseg);
7264
7265 if (r_type == BFD_RELOC_32
7266 && exp->X_add_symbol
7267 && symbol_get_tc (exp->X_add_symbol)->plt == 1)
7268 {
7269 r_type = BFD_RELOC_XTENSA_PLT;
7270 }
7271
7272 new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
7273 subseg_set (seg, subseg);
7274 return new_fix;
7275 }
7276
7277
7278 /* Relax a loop instruction so that it can span loop >256 bytes. */
7279 /*
7280 loop as, .L1
7281 .L0:
7282 rsr as, LEND
7283 wsr as, LBEG
7284 addi as, as, lo8(label-.L1)
7285 addmi as, as, mid8(label-.L1)
7286 wsr as, LEND
7287 isync
7288 rsr as, LCOUNT
7289 addi as, as, 1
7290 .L1:
7291 <<body>>
7292 label: */
7293
7294 static void
7295 convert_frag_immed_finish_loop (segP, fragP, t_insn)
7296 segT segP;
7297 fragS *fragP;
7298 TInsn *t_insn;
7299 {
7300 TInsn loop_insn;
7301 TInsn addi_insn;
7302 TInsn addmi_insn;
7303 unsigned long target;
7304 static xtensa_insnbuf insnbuf = NULL;
7305 unsigned int loop_length, loop_length_hi, loop_length_lo;
7306 xtensa_isa isa = xtensa_default_isa;
7307 addressT loop_offset;
7308 addressT addi_offset = 9;
7309 addressT addmi_offset = 12;
7310
7311 if (!insnbuf)
7312 insnbuf = xtensa_insnbuf_alloc (isa);
7313
7314 /* Get the loop offset. */
7315 loop_offset = get_expanded_loop_offset (t_insn->opcode);
7316 /* Validate that there really is a LOOP at the loop_offset. */
7317 tinsn_from_chars (&loop_insn, fragP->fr_opcode + loop_offset);
7318
7319 if (!is_loop_opcode (loop_insn.opcode))
7320 {
7321 as_bad_where (fragP->fr_file, fragP->fr_line,
7322 _("loop relaxation specification does not correspond"));
7323 assert (0);
7324 }
7325 addi_offset += loop_offset;
7326 addmi_offset += loop_offset;
7327
7328 assert (t_insn->ntok == 2);
7329 target = get_expression_value (segP, &t_insn->tok[1]);
7330
7331 know (symbolP);
7332 know (symbolP->sy_frag);
7333 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
7334 || symbol_get_frag (symbolP) == &zero_address_frag);
7335
7336 loop_length = target - (fragP->fr_address + fragP->fr_fix);
7337 loop_length_hi = loop_length & ~0x0ff;
7338 loop_length_lo = loop_length & 0x0ff;
7339 if (loop_length_lo >= 128)
7340 {
7341 loop_length_lo -= 256;
7342 loop_length_hi += 256;
7343 }
7344
7345 /* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most
7346 32512. If the loop is larger than that, then we just fail. */
7347 if (loop_length_hi > 32512)
7348 as_bad_where (fragP->fr_file, fragP->fr_line,
7349 _("loop too long for LOOP instruction"));
7350
7351 tinsn_from_chars (&addi_insn, fragP->fr_opcode + addi_offset);
7352 assert (addi_insn.opcode == xtensa_addi_opcode);
7353
7354 tinsn_from_chars (&addmi_insn, fragP->fr_opcode + addmi_offset);
7355 assert (addmi_insn.opcode == xtensa_addmi_opcode);
7356
7357 set_expr_const (&addi_insn.tok[2], loop_length_lo);
7358 tinsn_to_insnbuf (&addi_insn, insnbuf);
7359
7360 fragP->tc_frag_data.is_insn = TRUE;
7361 xtensa_insnbuf_to_chars (isa, insnbuf, fragP->fr_opcode + addi_offset);
7362
7363 set_expr_const (&addmi_insn.tok[2], loop_length_hi);
7364 tinsn_to_insnbuf (&addmi_insn, insnbuf);
7365 xtensa_insnbuf_to_chars (isa, insnbuf, fragP->fr_opcode + addmi_offset);
7366 }
7367
7368
7369 static offsetT
7370 get_expression_value (segP, exp)
7371 segT segP;
7372 expressionS *exp;
7373 {
7374 if (exp->X_op == O_constant)
7375 return exp->X_add_number;
7376 if (exp->X_op == O_symbol)
7377 {
7378 /* Find the fragment. */
7379 symbolS *sym = exp->X_add_symbol;
7380
7381 assert (S_GET_SEGMENT (sym) == segP
7382 || S_GET_SEGMENT (sym) == absolute_section);
7383
7384 return (S_GET_VALUE (sym) + exp->X_add_number);
7385 }
7386 as_bad (_("invalid expression evaluation type %d"), exp->X_op);
7387 return 0;
7388 }
7389
7390 \f
7391 /* A map that keeps information on a per-subsegment basis. This is
7392 maintained during initial assembly, but is invalid once the
7393 subsegments are smashed together. I.E., it cannot be used during
7394 the relaxation. */
7395
7396 typedef struct subseg_map_struct
7397 {
7398 /* the key */
7399 segT seg;
7400 subsegT subseg;
7401
7402 /* the data */
7403 unsigned flags;
7404
7405 struct subseg_map_struct *next;
7406 } subseg_map;
7407
7408 static subseg_map *sseg_map = NULL;
7409
7410
7411 static unsigned
7412 get_last_insn_flags (seg, subseg)
7413 segT seg;
7414 subsegT subseg;
7415 {
7416 subseg_map *subseg_e;
7417
7418 for (subseg_e = sseg_map; subseg_e != NULL; subseg_e = subseg_e->next)
7419 if (seg == subseg_e->seg && subseg == subseg_e->subseg)
7420 return subseg_e->flags;
7421
7422 return 0;
7423 }
7424
7425
7426 static void
7427 set_last_insn_flags (seg, subseg, fl, val)
7428 segT seg;
7429 subsegT subseg;
7430 unsigned fl;
7431 bfd_boolean val;
7432 {
7433 subseg_map *subseg_e;
7434
7435 for (subseg_e = sseg_map; subseg_e; subseg_e = subseg_e->next)
7436 if (seg == subseg_e->seg && subseg == subseg_e->subseg)
7437 break;
7438
7439 if (!subseg_e)
7440 {
7441 subseg_e = (subseg_map *) xmalloc (sizeof (subseg_map));
7442 memset (subseg_e, 0, sizeof (subseg_map));
7443 subseg_e->seg = seg;
7444 subseg_e->subseg = subseg;
7445 subseg_e->flags = 0;
7446 subseg_e->next = sseg_map;
7447 sseg_map = subseg_e;
7448 }
7449
7450 if (val)
7451 subseg_e->flags |= fl;
7452 else
7453 subseg_e->flags &= ~fl;
7454 }
7455
7456 \f
7457 /* Segment Lists and emit_state Stuff. */
7458
7459 /* Remove the segment from the global sections list. */
7460
7461 static void
7462 xtensa_remove_section (sec)
7463 segT sec;
7464 {
7465 /* Handle brain-dead bfd_section_list_remove macro, which
7466 expect the address of the prior section's "next" field, not
7467 just the address of the section to remove. */
7468
7469 segT *ps_next_ptr = &stdoutput->sections;
7470 while (*ps_next_ptr != sec && *ps_next_ptr != NULL)
7471 ps_next_ptr = &(*ps_next_ptr)->next;
7472
7473 assert (*ps_next_ptr != NULL);
7474
7475 bfd_section_list_remove (stdoutput, ps_next_ptr);
7476 }
7477
7478
7479 static void
7480 xtensa_insert_section (after_sec, sec)
7481 segT after_sec;
7482 segT sec;
7483 {
7484 segT *after_sec_next;
7485 if (after_sec == NULL)
7486 after_sec_next = &stdoutput->sections;
7487 else
7488 after_sec_next = &after_sec->next;
7489
7490 bfd_section_list_insert (stdoutput, after_sec_next, sec);
7491 }
7492
7493
7494 static void
7495 xtensa_move_seg_list_to_beginning (head)
7496 seg_list *head;
7497 {
7498 head = head->next;
7499 while (head)
7500 {
7501 segT literal_section = head->seg;
7502
7503 /* Move the literal section to the front of the section list. */
7504 assert (literal_section);
7505 xtensa_remove_section (literal_section);
7506 xtensa_insert_section (NULL, literal_section);
7507
7508 head = head->next;
7509 }
7510 }
7511
7512
7513 void
7514 xtensa_move_literals ()
7515 {
7516 seg_list *segment;
7517 frchainS *frchain_from, *frchain_to;
7518 fragS *search_frag, *next_frag, *last_frag, *literal_pool, *insert_after;
7519 fragS **frag_splice;
7520 emit_state state;
7521 segT dest_seg;
7522 fixS *fix, *next_fix, **fix_splice;
7523
7524 /* As clunky as this is, we can't rely on frag_var
7525 and frag_variant to get called in all situations. */
7526
7527 segment = literal_head->next;
7528 while (segment)
7529 {
7530 frchain_from = seg_info (segment->seg)->frchainP;
7531 search_frag = frchain_from->frch_root;
7532 while (search_frag)
7533 {
7534 search_frag->tc_frag_data.is_literal = TRUE;
7535 search_frag = search_frag->fr_next;
7536 }
7537 segment = segment->next;
7538 }
7539
7540 if (use_literal_section)
7541 return;
7542
7543 segment = literal_head->next;
7544 while (segment)
7545 {
7546 frchain_from = seg_info (segment->seg)->frchainP;
7547 search_frag = frchain_from->frch_root;
7548 literal_pool = NULL;
7549 frchain_to = NULL;
7550 frag_splice = &(frchain_from->frch_root);
7551
7552 while (!search_frag->tc_frag_data.literal_frag)
7553 {
7554 assert (search_frag->fr_fix == 0
7555 || search_frag->fr_type == rs_align);
7556 search_frag = search_frag->fr_next;
7557 }
7558
7559 assert (search_frag->tc_frag_data.literal_frag->fr_subtype
7560 == RELAX_LITERAL_POOL_BEGIN);
7561 xtensa_switch_section_emit_state (&state, segment->seg, 0);
7562
7563 /* Make sure that all the frags in this series are closed, and
7564 that there is at least one left over of zero-size. This
7565 prevents us from making a segment with an frchain without any
7566 frags in it. */
7567 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7568 last_frag = frag_now;
7569 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7570
7571 while (search_frag != frag_now)
7572 {
7573 next_frag = search_frag->fr_next;
7574
7575 /* First, move the frag out of the literal section and
7576 to the appropriate place. */
7577 if (search_frag->tc_frag_data.literal_frag)
7578 {
7579 literal_pool = search_frag->tc_frag_data.literal_frag;
7580 assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN);
7581 /* Note that we set this fr_var to be a fix
7582 chain when we created the literal pool location
7583 as RELAX_LITERAL_POOL_BEGIN. */
7584 frchain_to = (frchainS *) literal_pool->fr_var;
7585 }
7586 insert_after = literal_pool;
7587
7588 while (insert_after->fr_next->fr_subtype != RELAX_LITERAL_POOL_END)
7589 insert_after = insert_after->fr_next;
7590
7591 dest_seg = (segT) insert_after->fr_next->fr_var;
7592
7593 *frag_splice = next_frag;
7594 search_frag->fr_next = insert_after->fr_next;
7595 insert_after->fr_next = search_frag;
7596 search_frag->tc_frag_data.lit_seg = dest_seg;
7597
7598 /* Now move any fixups associated with this frag to the
7599 right section. */
7600 fix = frchain_from->fix_root;
7601 fix_splice = &(frchain_from->fix_root);
7602 while (fix)
7603 {
7604 next_fix = fix->fx_next;
7605 if (fix->fx_frag == search_frag)
7606 {
7607 *fix_splice = next_fix;
7608 fix->fx_next = frchain_to->fix_root;
7609 frchain_to->fix_root = fix;
7610 if (frchain_to->fix_tail == NULL)
7611 frchain_to->fix_tail = fix;
7612 }
7613 else
7614 fix_splice = &(fix->fx_next);
7615 fix = next_fix;
7616 }
7617 search_frag = next_frag;
7618 }
7619
7620 if (frchain_from->fix_root != NULL)
7621 {
7622 frchain_from = seg_info (segment->seg)->frchainP;
7623 as_warn (_("fixes not all moved from %s"), segment->seg->name);
7624
7625 assert (frchain_from->fix_root == NULL);
7626 }
7627 frchain_from->fix_tail = NULL;
7628 xtensa_restore_emit_state (&state);
7629 segment = segment->next;
7630 }
7631
7632 xtensa_move_frag_symbols ();
7633 }
7634
7635
7636 static void
7637 xtensa_move_frag_symbol (sym)
7638 symbolS *sym;
7639 {
7640 fragS *frag = symbol_get_frag (sym);
7641
7642 if (frag->tc_frag_data.lit_seg != (segT) 0)
7643 S_SET_SEGMENT (sym, frag->tc_frag_data.lit_seg);
7644 }
7645
7646
7647 static void
7648 xtensa_move_frag_symbols ()
7649 {
7650 symbolS *symbolP;
7651
7652 /* Although you might think that only one of these lists should be
7653 searched, it turns out that the difference of the two sets
7654 (either way) is not empty. They do overlap quite a bit,
7655 however. */
7656
7657 for (symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next)
7658 xtensa_move_frag_symbol (symbolP);
7659
7660 map_over_defined_symbols (xtensa_move_frag_symbol);
7661 }
7662
7663
7664 static void
7665 xtensa_reorder_seg_list (head, after)
7666 seg_list *head;
7667 segT after;
7668 {
7669 /* Move all of the sections in the section list to come
7670 after "after" in the gnu segment list. */
7671
7672 head = head->next;
7673 while (head)
7674 {
7675 segT literal_section = head->seg;
7676
7677 /* Move the literal section after "after". */
7678 assert (literal_section);
7679 if (literal_section != after)
7680 {
7681 xtensa_remove_section (literal_section);
7682 xtensa_insert_section (after, literal_section);
7683 }
7684
7685 head = head->next;
7686 }
7687 }
7688
7689
7690 /* Push all the literal segments to the end of the gnu list. */
7691
7692 void
7693 xtensa_reorder_segments ()
7694 {
7695 segT sec;
7696 segT last_sec;
7697 int old_count = 0;
7698 int new_count = 0;
7699
7700 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
7701 old_count++;
7702
7703 /* Now that we have the last section, push all the literal
7704 sections to the end. */
7705 last_sec = get_last_sec ();
7706 xtensa_reorder_seg_list (literal_head, last_sec);
7707 xtensa_reorder_seg_list (init_literal_head, last_sec);
7708 xtensa_reorder_seg_list (fini_literal_head, last_sec);
7709
7710 /* Now perform the final error check. */
7711 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
7712 new_count++;
7713 assert (new_count == old_count);
7714 }
7715
7716
7717 segT
7718 get_last_sec ()
7719 {
7720 segT last_sec = stdoutput->sections;
7721 while (last_sec->next != NULL)
7722 last_sec = last_sec->next;
7723
7724 return last_sec;
7725 }
7726
7727
7728 /* Change the emit state (seg, subseg, and frag related stuff) to the
7729 correct location. Return a emit_state which can be passed to
7730 xtensa_restore_emit_state to return to current fragment. */
7731
7732 void
7733 xtensa_switch_to_literal_fragment (result)
7734 emit_state *result;
7735 {
7736 /* When we mark a literal pool location, we want to put a frag in
7737 the literal pool that points to it. But to do that, we want to
7738 switch_to_literal_fragment. But literal sections don't have
7739 literal pools, so their location is always null, so we would
7740 recurse forever. This is kind of hacky, but it works. */
7741
7742 static bfd_boolean recursive = FALSE;
7743 fragS *pool_location = get_literal_pool_location (now_seg);
7744 bfd_boolean is_init =
7745 (now_seg && !strcmp (segment_name (now_seg), INIT_SECTION_NAME));
7746
7747 bfd_boolean is_fini =
7748 (now_seg && !strcmp (segment_name (now_seg), FINI_SECTION_NAME));
7749
7750
7751 if (pool_location == NULL
7752 && !use_literal_section
7753 && !recursive
7754 && !is_init && ! is_fini)
7755 {
7756 as_warn (_("inlining literal pool; "
7757 "specify location with .literal_position."));
7758 recursive = TRUE;
7759 xtensa_mark_literal_pool_location ();
7760 recursive = FALSE;
7761 }
7762
7763 /* Special case: If we are in the ".fini" or ".init" section, then
7764 we will ALWAYS be generating to the ".fini.literal" and
7765 ".init.literal" sections. */
7766
7767 if (is_init)
7768 {
7769 cache_literal_section (init_literal_head,
7770 default_lit_sections.init_lit_seg_name,
7771 &default_lit_sections.init_lit_seg);
7772 xtensa_switch_section_emit_state (result,
7773 default_lit_sections.init_lit_seg, 0);
7774 }
7775 else if (is_fini)
7776 {
7777 cache_literal_section (fini_literal_head,
7778 default_lit_sections.fini_lit_seg_name,
7779 &default_lit_sections.fini_lit_seg);
7780 xtensa_switch_section_emit_state (result,
7781 default_lit_sections.fini_lit_seg, 0);
7782 }
7783 else
7784 {
7785 cache_literal_section (literal_head,
7786 default_lit_sections.lit_seg_name,
7787 &default_lit_sections.lit_seg);
7788 xtensa_switch_section_emit_state (result,
7789 default_lit_sections.lit_seg, 0);
7790 }
7791
7792 if (!use_literal_section &&
7793 !is_init && !is_fini &&
7794 get_literal_pool_location (now_seg) != pool_location)
7795 {
7796 /* Close whatever frag is there. */
7797 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7798 frag_now->tc_frag_data.literal_frag = pool_location;
7799 frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
7800 }
7801
7802 /* Do a 4 byte align here. */
7803 frag_align (2, 0, 0);
7804 }
7805
7806
7807 /* Call this function before emitting data into the literal section.
7808 This is a helper function for xtensa_switch_to_literal_fragment.
7809 This is similar to a .section new_now_seg subseg. */
7810
7811 void
7812 xtensa_switch_section_emit_state (state, new_now_seg, new_now_subseg)
7813 emit_state *state;
7814 segT new_now_seg;
7815 subsegT new_now_subseg;
7816 {
7817 state->name = now_seg->name;
7818 state->now_seg = now_seg;
7819 state->now_subseg = now_subseg;
7820 state->generating_literals = generating_literals;
7821 generating_literals++;
7822 subseg_new (segment_name (new_now_seg), new_now_subseg);
7823 }
7824
7825
7826 /* Use to restore the emitting into the normal place. */
7827
7828 void
7829 xtensa_restore_emit_state (state)
7830 emit_state *state;
7831 {
7832 generating_literals = state->generating_literals;
7833 subseg_new (state->name, state->now_subseg);
7834 }
7835
7836
7837 /* Get a segment of a given name. If the segment is already
7838 present, return it; otherwise, create a new one. */
7839
7840 static void
7841 cache_literal_section (head, name, seg)
7842 seg_list *head;
7843 const char *name;
7844 segT *seg;
7845 {
7846 segT current_section = now_seg;
7847 int current_subsec = now_subseg;
7848
7849 if (*seg != 0)
7850 return;
7851 *seg = retrieve_literal_seg (head, name);
7852 subseg_set (current_section, current_subsec);
7853 }
7854
7855
7856 /* Get a segment of a given name. If the segment is already
7857 present, return it; otherwise, create a new one. */
7858
7859 static segT
7860 retrieve_literal_seg (head, name)
7861 seg_list *head;
7862 const char *name;
7863 {
7864 segT ret = 0;
7865
7866 assert (head);
7867
7868 ret = seg_present (name);
7869 if (!ret)
7870 {
7871 ret = subseg_new (name, (subsegT) 0);
7872 add_seg_list (head, ret);
7873 bfd_set_section_flags (stdoutput, ret, SEC_HAS_CONTENTS |
7874 SEC_READONLY | SEC_ALLOC | SEC_LOAD | SEC_CODE);
7875 bfd_set_section_alignment (stdoutput, ret, 2);
7876 }
7877
7878 return ret;
7879 }
7880
7881
7882 /* Return a segment of a given name if it is present. */
7883
7884 static segT
7885 seg_present (name)
7886 const char *name;
7887 {
7888 segT seg;
7889 seg = stdoutput->sections;
7890
7891 while (seg)
7892 {
7893 if (!strcmp (segment_name (seg), name))
7894 return seg;
7895 seg = seg->next;
7896 }
7897
7898 return 0;
7899 }
7900
7901
7902 /* Add a segment to a segment list. */
7903
7904 static void
7905 add_seg_list (head, seg)
7906 seg_list *head;
7907 segT seg;
7908 {
7909 seg_list *n;
7910 n = (seg_list *) xmalloc (sizeof (seg_list));
7911 assert (n);
7912
7913 n->seg = seg;
7914 n->next = head->next;
7915 head->next = n;
7916 }
7917
7918 \f
7919 /* Set up Property Tables after Relaxation. */
7920
7921 #define XTENSA_INSN_SEC_NAME ".xt.insn"
7922 #define XTENSA_LIT_SEC_NAME ".xt.lit"
7923
7924 void
7925 xtensa_post_relax_hook ()
7926 {
7927 xtensa_move_seg_list_to_beginning (literal_head);
7928 xtensa_move_seg_list_to_beginning (init_literal_head);
7929 xtensa_move_seg_list_to_beginning (fini_literal_head);
7930
7931 xtensa_create_property_segments (get_frag_is_insn,
7932 XTENSA_INSN_SEC_NAME,
7933 xt_literal_sec);
7934 if (use_literal_section)
7935 xtensa_create_property_segments (get_frag_is_literal,
7936 XTENSA_LIT_SEC_NAME,
7937 xt_insn_sec);
7938 }
7939
7940
7941 static bfd_boolean
7942 get_frag_is_literal (fragP)
7943 const fragS *fragP;
7944 {
7945 assert (fragP != NULL);
7946 return (fragP->tc_frag_data.is_literal);
7947 }
7948
7949
7950 static bfd_boolean
7951 get_frag_is_insn (fragP)
7952 const fragS *fragP;
7953 {
7954 assert (fragP != NULL);
7955 return (fragP->tc_frag_data.is_insn);
7956 }
7957
7958
7959 static void
7960 xtensa_create_property_segments (property_function, section_name_base,
7961 sec_type)
7962 frag_predicate property_function;
7963 const char * section_name_base;
7964 xt_section_type sec_type;
7965 {
7966 segT *seclist;
7967
7968 /* Walk over all of the current segments.
7969 Walk over each fragment
7970 For each fragment that has instructions
7971 Build an instruction record (append where possible). */
7972
7973 for (seclist = &stdoutput->sections;
7974 seclist && *seclist;
7975 seclist = &(*seclist)->next)
7976 {
7977 segT sec = *seclist;
7978 if (section_has_property (sec, property_function))
7979 {
7980 char * property_section_name =
7981 xtensa_get_property_section_name (stdoutput, sec,
7982 section_name_base);
7983 segT insn_sec = retrieve_xtensa_section (property_section_name);
7984 segment_info_type *xt_seg_info = retrieve_segment_info (insn_sec);
7985 xtensa_block_info ** xt_blocks =
7986 &xt_seg_info->tc_segment_info_data.blocks[sec_type];
7987 /* Walk over all of the frchains here and add new sections. */
7988 add_xt_block_frags (sec, insn_sec, xt_blocks, property_function);
7989 }
7990 }
7991
7992 /* Now we fill them out.... */
7993
7994 for (seclist = &stdoutput->sections;
7995 seclist && *seclist;
7996 seclist = &(*seclist)->next)
7997 {
7998 segment_info_type *seginfo;
7999 xtensa_block_info *block;
8000 segT sec = *seclist;
8001 seginfo = seg_info (sec);
8002 block = seginfo->tc_segment_info_data.blocks[sec_type];
8003
8004 if (block)
8005 {
8006 xtensa_block_info *cur_block;
8007 /* This is a section with some data. */
8008 size_t num_recs = 0;
8009 size_t rec_size;
8010
8011 for (cur_block = block; cur_block; cur_block = cur_block->next)
8012 num_recs++;
8013
8014 rec_size = num_recs * 8;
8015 bfd_set_section_size (stdoutput, sec, rec_size);
8016
8017 /* In order to make this work with the assembler, we have to
8018 build some frags and then build the "fixups" for it. It
8019 would be easier to just set the contents then set the
8020 arlents. */
8021
8022 if (num_recs)
8023 {
8024 /* Allocate a fragment and leak it. */
8025 fragS *fragP;
8026 size_t frag_size;
8027 fixS *fixes;
8028 frchainS *frchainP;
8029 size_t i;
8030 char *frag_data;
8031
8032 frag_size = sizeof (fragS) + rec_size;
8033 fragP = (fragS *) xmalloc (frag_size);
8034
8035 memset (fragP, 0, frag_size);
8036 fragP->fr_address = 0;
8037 fragP->fr_next = NULL;
8038 fragP->fr_fix = rec_size;
8039 fragP->fr_var = 0;
8040 fragP->fr_type = rs_fill;
8041 /* the rest are zeros */
8042
8043 frchainP = seginfo->frchainP;
8044 frchainP->frch_root = fragP;
8045 frchainP->frch_last = fragP;
8046
8047 fixes = (fixS *) xmalloc (sizeof (fixS) * num_recs);
8048 memset (fixes, 0, sizeof (fixS) * num_recs);
8049
8050 seginfo->fix_root = fixes;
8051 seginfo->fix_tail = &fixes[num_recs - 1];
8052 cur_block = block;
8053 frag_data = &fragP->fr_literal[0];
8054 for (i = 0; i < num_recs; i++)
8055 {
8056 fixS *fix = &fixes[i];
8057 assert (cur_block);
8058
8059 /* Write the fixup. */
8060 if (i != num_recs - 1)
8061 fix->fx_next = &fixes[i + 1];
8062 else
8063 fix->fx_next = NULL;
8064 fix->fx_size = 4;
8065 fix->fx_done = 0;
8066 fix->fx_frag = fragP;
8067 fix->fx_where = i * 8;
8068 fix->fx_addsy = section_symbol (cur_block->sec);
8069 fix->fx_offset = cur_block->offset;
8070 fix->fx_r_type = BFD_RELOC_32;
8071 fix->fx_file = "Internal Assembly";
8072 fix->fx_line = 0;
8073
8074 /* Write the length. */
8075 md_number_to_chars (&frag_data[4 + 8 * i],
8076 cur_block->size, 4);
8077 cur_block = cur_block->next;
8078 }
8079 }
8080 }
8081 }
8082 }
8083
8084
8085 segment_info_type *
8086 retrieve_segment_info (seg)
8087 segT seg;
8088 {
8089 segment_info_type *seginfo;
8090 seginfo = (segment_info_type *) bfd_get_section_userdata (stdoutput, seg);
8091 if (!seginfo)
8092 {
8093 frchainS *frchainP;
8094
8095 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
8096 memset ((PTR) seginfo, 0, sizeof (*seginfo));
8097 seginfo->fix_root = NULL;
8098 seginfo->fix_tail = NULL;
8099 seginfo->bfd_section = seg;
8100 seginfo->sym = 0;
8101 /* We will not be dealing with these, only our special ones. */
8102 #if 0
8103 if (seg == bfd_abs_section_ptr)
8104 abs_seg_info = seginfo;
8105 else if (seg == bfd_und_section_ptr)
8106 und_seg_info = seginfo;
8107 else
8108 #endif
8109 bfd_set_section_userdata (stdoutput, seg, (PTR) seginfo);
8110 #if 0
8111 seg_fix_rootP = &segment_info[seg].fix_root;
8112 seg_fix_tailP = &segment_info[seg].fix_tail;
8113 #endif
8114
8115 frchainP = (frchainS *) xmalloc (sizeof (frchainS));
8116 frchainP->frch_root = NULL;
8117 frchainP->frch_last = NULL;
8118 frchainP->frch_next = NULL;
8119 frchainP->frch_seg = seg;
8120 frchainP->frch_subseg = 0;
8121 frchainP->fix_root = NULL;
8122 frchainP->fix_tail = NULL;
8123 /* Do not init the objstack. */
8124 /* obstack_begin (&frchainP->frch_obstack, chunksize); */
8125 /* frchainP->frch_frag_now = fragP; */
8126 frchainP->frch_frag_now = NULL;
8127
8128 seginfo->frchainP = frchainP;
8129 }
8130
8131 return seginfo;
8132 }
8133
8134
8135 segT
8136 retrieve_xtensa_section (sec_name)
8137 char *sec_name;
8138 {
8139 bfd *abfd = stdoutput;
8140 flagword flags, out_flags, link_once_flags;
8141 segT s;
8142
8143 flags = bfd_get_section_flags (abfd, now_seg);
8144 link_once_flags = (flags & SEC_LINK_ONCE);
8145 if (link_once_flags)
8146 link_once_flags |= (flags & SEC_LINK_DUPLICATES);
8147 out_flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY | link_once_flags);
8148
8149 s = bfd_make_section_old_way (abfd, sec_name);
8150 if (s == NULL)
8151 as_bad (_("could not create section %s"), sec_name);
8152 if (!bfd_set_section_flags (abfd, s, out_flags))
8153 as_bad (_("invalid flag combination on section %s"), sec_name);
8154
8155 return s;
8156 }
8157
8158
8159 bfd_boolean
8160 section_has_property (sec, property_function)
8161 segT sec;
8162 frag_predicate property_function;
8163 {
8164 segment_info_type *seginfo = seg_info (sec);
8165 fragS *fragP;
8166
8167 if (seginfo && seginfo->frchainP)
8168 {
8169 for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
8170 {
8171 if (property_function (fragP)
8172 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
8173 return TRUE;
8174 }
8175 }
8176 return FALSE;
8177 }
8178
8179
8180 /* Two types of block sections exist right now: literal and insns. */
8181
8182 void
8183 add_xt_block_frags (sec, xt_block_sec, xt_block, property_function)
8184 segT sec;
8185 segT xt_block_sec;
8186 xtensa_block_info **xt_block;
8187 frag_predicate property_function;
8188 {
8189 segment_info_type *seg_info;
8190 segment_info_type *xt_seg_info;
8191 bfd_vma seg_offset;
8192 fragS *fragP;
8193
8194 xt_seg_info = retrieve_segment_info (xt_block_sec);
8195 seg_info = retrieve_segment_info (sec);
8196
8197 /* Build it if needed. */
8198 while (*xt_block != NULL)
8199 xt_block = &(*xt_block)->next;
8200 /* We are either at NULL at the beginning or at the end. */
8201
8202 /* Walk through the frags. */
8203 seg_offset = 0;
8204
8205 if (seg_info->frchainP)
8206 {
8207 for (fragP = seg_info->frchainP->frch_root;
8208 fragP;
8209 fragP = fragP->fr_next)
8210 {
8211 if (property_function (fragP)
8212 && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
8213 {
8214 if (*xt_block != NULL)
8215 {
8216 if ((*xt_block)->offset + (*xt_block)->size
8217 == fragP->fr_address)
8218 (*xt_block)->size += fragP->fr_fix;
8219 else
8220 xt_block = &((*xt_block)->next);
8221 }
8222 if (*xt_block == NULL)
8223 {
8224 xtensa_block_info *new_block = (xtensa_block_info *)
8225 xmalloc (sizeof (xtensa_block_info));
8226 new_block->sec = sec;
8227 new_block->offset = fragP->fr_address;
8228 new_block->size = fragP->fr_fix;
8229 new_block->next = NULL;
8230 *xt_block = new_block;
8231 }
8232 }
8233 }
8234 }
8235 }
8236
8237 \f
8238 /* Instruction Stack Functions (from "xtensa-istack.h"). */
8239
8240 void
8241 istack_init (stack)
8242 IStack *stack;
8243 {
8244 memset (stack, 0, sizeof (IStack));
8245 stack->ninsn = 0;
8246 }
8247
8248
8249 bfd_boolean
8250 istack_empty (stack)
8251 IStack *stack;
8252 {
8253 return (stack->ninsn == 0);
8254 }
8255
8256
8257 bfd_boolean
8258 istack_full (stack)
8259 IStack *stack;
8260 {
8261 return (stack->ninsn == MAX_ISTACK);
8262 }
8263
8264
8265 /* Return a pointer to the top IStack entry.
8266 It is an error to call this if istack_empty () is true. */
8267
8268 TInsn *
8269 istack_top (stack)
8270 IStack *stack;
8271 {
8272 int rec = stack->ninsn - 1;
8273 assert (!istack_empty (stack));
8274 return &stack->insn[rec];
8275 }
8276
8277
8278 /* Add a new TInsn to an IStack.
8279 It is an error to call this if istack_full () is true. */
8280
8281 void
8282 istack_push (stack, insn)
8283 IStack *stack;
8284 TInsn *insn;
8285 {
8286 int rec = stack->ninsn;
8287 assert (!istack_full (stack));
8288 tinsn_copy (&stack->insn[rec], insn);
8289 stack->ninsn++;
8290 }
8291
8292
8293 /* Clear space for the next TInsn on the IStack and return a pointer
8294 to it. It is an error to call this if istack_full () is true. */
8295
8296 TInsn *
8297 istack_push_space (stack)
8298 IStack *stack;
8299 {
8300 int rec = stack->ninsn;
8301 TInsn *insn;
8302 assert (!istack_full (stack));
8303 insn = &stack->insn[rec];
8304 memset (insn, 0, sizeof (TInsn));
8305 stack->ninsn++;
8306 return insn;
8307 }
8308
8309
8310 /* Remove the last pushed instruction. It is an error to call this if
8311 istack_empty () returns true. */
8312
8313 void
8314 istack_pop (stack)
8315 IStack *stack;
8316 {
8317 int rec = stack->ninsn - 1;
8318 assert (!istack_empty (stack));
8319 stack->ninsn--;
8320 memset (&stack->insn[rec], 0, sizeof (TInsn));
8321 }
8322
8323 \f
8324 /* TInsn functions. */
8325
8326 void
8327 tinsn_init (dst)
8328 TInsn *dst;
8329 {
8330 memset (dst, 0, sizeof (TInsn));
8331 }
8332
8333
8334 void
8335 tinsn_copy (dst, src)
8336 TInsn *dst;
8337 const TInsn *src;
8338 {
8339 tinsn_init (dst);
8340 memcpy (dst, src, sizeof (TInsn));
8341 }
8342
8343
8344 /* Get the ``num''th token of the TInsn.
8345 It is illegal to call this if num > insn->ntoks. */
8346
8347 expressionS *
8348 tinsn_get_tok (insn, num)
8349 TInsn *insn;
8350 int num;
8351 {
8352 assert (num < insn->ntok);
8353 return &insn->tok[num];
8354 }
8355
8356
8357 /* Return true if ANY of the operands in the insn are symbolic. */
8358
8359 static bfd_boolean
8360 tinsn_has_symbolic_operands (insn)
8361 const TInsn *insn;
8362 {
8363 int i;
8364 int n = insn->ntok;
8365
8366 assert (insn->insn_type == ITYPE_INSN);
8367
8368 for (i = 0; i < n; ++i)
8369 {
8370 switch (insn->tok[i].X_op)
8371 {
8372 case O_register:
8373 case O_constant:
8374 break;
8375 default:
8376 return TRUE;
8377 }
8378 }
8379 return FALSE;
8380 }
8381
8382
8383 bfd_boolean
8384 tinsn_has_invalid_symbolic_operands (insn)
8385 const TInsn *insn;
8386 {
8387 int i;
8388 int n = insn->ntok;
8389
8390 assert (insn->insn_type == ITYPE_INSN);
8391
8392 for (i = 0; i < n; ++i)
8393 {
8394 switch (insn->tok[i].X_op)
8395 {
8396 case O_register:
8397 case O_constant:
8398 break;
8399 default:
8400 if (i == get_relaxable_immed (insn->opcode))
8401 break;
8402 as_bad (_("invalid symbolic operand %d on '%s'"),
8403 i, xtensa_opcode_name (xtensa_default_isa, insn->opcode));
8404 return TRUE;
8405 }
8406 }
8407 return FALSE;
8408 }
8409
8410
8411 /* For assembly code with complex expressions (e.g. subtraction),
8412 we have to build them in the literal pool so that
8413 their results are calculated correctly after relaxation.
8414 The relaxation only handles expressions that
8415 boil down to SYMBOL + OFFSET. */
8416
8417 static bfd_boolean
8418 tinsn_has_complex_operands (insn)
8419 const TInsn *insn;
8420 {
8421 int i;
8422 int n = insn->ntok;
8423 assert (insn->insn_type == ITYPE_INSN);
8424 for (i = 0; i < n; ++i)
8425 {
8426 switch (insn->tok[i].X_op)
8427 {
8428 case O_register:
8429 case O_constant:
8430 case O_symbol:
8431 break;
8432 default:
8433 return TRUE;
8434 }
8435 }
8436 return FALSE;
8437 }
8438
8439
8440 /* Convert the constant operands in the t_insn to insnbuf.
8441 Return true if there is a symbol in the immediate field.
8442
8443 Before this is called,
8444 1) the number of operands are correct
8445 2) the t_insn is a ITYPE_INSN
8446 3) ONLY the relaxable_ is built
8447 4) All operands are O_constant, O_symbol. All constants fit
8448 The return value tells whether there are any remaining O_symbols. */
8449
8450 static bfd_boolean
8451 tinsn_to_insnbuf (t_insn, insnbuf)
8452 TInsn *t_insn;
8453 xtensa_insnbuf insnbuf;
8454 {
8455 xtensa_isa isa = xtensa_default_isa;
8456 xtensa_opcode opcode = t_insn->opcode;
8457 bfd_boolean has_fixup = FALSE;
8458 int noperands = xtensa_num_operands (isa, opcode);
8459 int i;
8460 uint32 opnd_value;
8461 char *file_name;
8462 int line;
8463
8464 assert (t_insn->insn_type == ITYPE_INSN);
8465 if (noperands != t_insn->ntok)
8466 as_fatal (_("operand number mismatch"));
8467
8468 xtensa_encode_insn (isa, opcode, insnbuf);
8469
8470 for (i = 0; i < noperands; ++i)
8471 {
8472 expressionS *expr = &t_insn->tok[i];
8473 xtensa_operand operand = xtensa_get_operand (isa, opcode, i);
8474 switch (expr->X_op)
8475 {
8476 case O_register:
8477 /* The register number has already been checked in
8478 expression_maybe_register, so we don't need to check here. */
8479 opnd_value = expr->X_add_number;
8480 (void) xtensa_operand_encode (operand, &opnd_value);
8481 xtensa_operand_set_field (operand, insnbuf, opnd_value);
8482 break;
8483
8484 case O_constant:
8485 as_where (&file_name, &line);
8486 /* It is a constant and we called this function,
8487 then we have to try to fit it. */
8488 xtensa_insnbuf_set_operand (insnbuf, opcode, operand,
8489 expr->X_add_number, file_name, line);
8490 break;
8491
8492 case O_symbol:
8493 default:
8494 has_fixup = TRUE;
8495 break;
8496 }
8497 }
8498 return has_fixup;
8499 }
8500
8501
8502 /* Check the instruction arguments. Return true on failure. */
8503
8504 bfd_boolean
8505 tinsn_check_arguments (insn)
8506 const TInsn *insn;
8507 {
8508 xtensa_isa isa = xtensa_default_isa;
8509 xtensa_opcode opcode = insn->opcode;
8510
8511 if (opcode == XTENSA_UNDEFINED)
8512 {
8513 as_bad (_("invalid opcode"));
8514 return TRUE;
8515 }
8516
8517 if (xtensa_num_operands (isa, opcode) > insn->ntok)
8518 {
8519 as_bad (_("too few operands"));
8520 return TRUE;
8521 }
8522
8523 if (xtensa_num_operands (isa, opcode) < insn->ntok)
8524 {
8525 as_bad (_("too many operands"));
8526 return TRUE;
8527 }
8528 return FALSE;
8529 }
8530
8531
8532 /* Load an instruction from its encoded form. */
8533
8534 static void
8535 tinsn_from_chars (t_insn, f)
8536 TInsn *t_insn;
8537 char *f;
8538 {
8539 static xtensa_insnbuf insnbuf = NULL;
8540 int i;
8541 xtensa_opcode opcode;
8542 xtensa_isa isa = xtensa_default_isa;
8543
8544 if (!insnbuf)
8545 insnbuf = xtensa_insnbuf_alloc (isa);
8546
8547 xtensa_insnbuf_from_chars (isa, insnbuf, f);
8548 opcode = xtensa_decode_insn (isa, insnbuf);
8549
8550 /* Find the immed. */
8551 tinsn_init (t_insn);
8552 t_insn->insn_type = ITYPE_INSN;
8553 t_insn->is_specific_opcode = FALSE; /* Must not be specific. */
8554 t_insn->opcode = opcode;
8555 t_insn->ntok = xtensa_num_operands (isa, opcode);
8556 for (i = 0; i < t_insn->ntok; i++)
8557 {
8558 set_expr_const (&t_insn->tok[i],
8559 xtensa_insnbuf_get_operand (insnbuf, opcode, i));
8560 }
8561 }
8562
8563
8564 /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */
8565
8566 static void
8567 tinsn_immed_from_frag (t_insn, fragP)
8568 TInsn *t_insn;
8569 fragS *fragP;
8570 {
8571 xtensa_opcode opcode = t_insn->opcode;
8572 int opnum;
8573
8574 if (fragP->fr_symbol)
8575 {
8576 opnum = get_relaxable_immed (opcode);
8577 set_expr_symbol_offset (&t_insn->tok[opnum],
8578 fragP->fr_symbol, fragP->fr_offset);
8579 }
8580 }
8581
8582
8583 static int
8584 get_num_stack_text_bytes (istack)
8585 IStack *istack;
8586 {
8587 int i;
8588 int text_bytes = 0;
8589
8590 for (i = 0; i < istack->ninsn; i++)
8591 {
8592 TInsn *t_insn = &istack->insn[i];
8593 if (t_insn->insn_type == ITYPE_INSN)
8594 text_bytes += xg_get_insn_size (t_insn);
8595 }
8596 return text_bytes;
8597 }
8598
8599
8600 static int
8601 get_num_stack_literal_bytes (istack)
8602 IStack *istack;
8603 {
8604 int i;
8605 int lit_bytes = 0;
8606
8607 for (i = 0; i < istack->ninsn; i++)
8608 {
8609 TInsn *t_insn = &istack->insn[i];
8610
8611 if (t_insn->insn_type == ITYPE_LITERAL && t_insn->ntok == 1)
8612 lit_bytes += 4;
8613 }
8614 return lit_bytes;
8615 }
8616
8617 \f
8618 /* Expression utilities. */
8619
8620 /* Return true if the expression is an integer constant. */
8621
8622 bfd_boolean
8623 expr_is_const (s)
8624 const expressionS *s;
8625 {
8626 return (s->X_op == O_constant);
8627 }
8628
8629
8630 /* Get the expression constant.
8631 Calling this is illegal if expr_is_const () returns true. */
8632
8633 offsetT
8634 get_expr_const (s)
8635 const expressionS *s;
8636 {
8637 assert (expr_is_const (s));
8638 return s->X_add_number;
8639 }
8640
8641
8642 /* Set the expression to a constant value. */
8643
8644 void
8645 set_expr_const (s, val)
8646 expressionS *s;
8647 offsetT val;
8648 {
8649 s->X_op = O_constant;
8650 s->X_add_number = val;
8651 s->X_add_symbol = NULL;
8652 s->X_op_symbol = NULL;
8653 }
8654
8655
8656 /* Set the expression to a symbol + constant offset. */
8657
8658 void
8659 set_expr_symbol_offset (s, sym, offset)
8660 expressionS *s;
8661 symbolS *sym;
8662 offsetT offset;
8663 {
8664 s->X_op = O_symbol;
8665 s->X_add_symbol = sym;
8666 s->X_op_symbol = NULL; /* unused */
8667 s->X_add_number = offset;
8668 }
8669
8670
8671 bfd_boolean
8672 expr_is_equal (s1, s2)
8673 expressionS *s1;
8674 expressionS *s2;
8675 {
8676 if (s1->X_op != s2->X_op)
8677 return FALSE;
8678 if (s1->X_add_symbol != s2->X_add_symbol)
8679 return FALSE;
8680 if (s1->X_op_symbol != s2->X_op_symbol)
8681 return FALSE;
8682 if (s1->X_add_number != s2->X_add_number)
8683 return FALSE;
8684 return TRUE;
8685 }
8686
8687
8688 static void
8689 copy_expr (dst, src)
8690 expressionS *dst;
8691 const expressionS *src;
8692 {
8693 memcpy (dst, src, sizeof (expressionS));
8694 }
8695
8696 \f
8697 /* Support for Tensilica's "--rename-section" option. */
8698
8699 #ifdef XTENSA_SECTION_RENAME
8700
8701 struct rename_section_struct
8702 {
8703 char *old_name;
8704 char *new_name;
8705 struct rename_section_struct *next;
8706 };
8707
8708 static struct rename_section_struct *section_rename;
8709
8710
8711 /* Parse the string oldname=new_name:oldname2=new_name2
8712 and call add_section_rename. */
8713
8714 void
8715 build_section_rename (arg)
8716 const char *arg;
8717 {
8718 char *this_arg = NULL;
8719 char *next_arg = NULL;
8720
8721 for (this_arg = strdup (arg); this_arg != NULL; this_arg = next_arg)
8722 {
8723 if (this_arg)
8724 {
8725 next_arg = strchr (this_arg, ':');
8726 if (next_arg)
8727 {
8728 *next_arg = '\0';
8729 next_arg++;
8730 }
8731 }
8732 {
8733 char *old_name = this_arg;
8734 char *new_name = strchr (this_arg, '=');
8735
8736 if (*old_name == '\0')
8737 {
8738 as_warn (_("ignoring extra '-rename-section' delimiter ':'"));
8739 continue;
8740 }
8741 if (!new_name || new_name[1] == '\0')
8742 {
8743 as_warn (_("ignoring invalid '-rename-section' "
8744 "specification: '%s'"), old_name);
8745 continue;
8746 }
8747 *new_name = '\0';
8748 new_name++;
8749 add_section_rename (old_name, new_name);
8750 }
8751 }
8752 }
8753
8754
8755 static void
8756 add_section_rename (old_name, new_name)
8757 char *old_name;
8758 char *new_name;
8759 {
8760 struct rename_section_struct *r = section_rename;
8761
8762 /* Check for invalid section renaming. */
8763 for (r = section_rename; r != NULL; r = r->next)
8764 {
8765 if (strcmp (r->old_name, old_name) == 0)
8766 as_bad (_("section %s renamed multiple times"), old_name);
8767 if (strcmp (r->new_name, new_name) == 0)
8768 as_bad (_("multiple sections remapped to output section %s"),
8769 new_name);
8770 }
8771
8772 /* Now add it. */
8773 r = (struct rename_section_struct *)
8774 xmalloc (sizeof (struct rename_section_struct));
8775 r->old_name = strdup (old_name);
8776 r->new_name = strdup (new_name);
8777 r->next = section_rename;
8778 section_rename = r;
8779 }
8780
8781
8782 const char *
8783 xtensa_section_rename (name)
8784 const char *name;
8785 {
8786 struct rename_section_struct *r = section_rename;
8787
8788 for (r = section_rename; r != NULL; r = r->next)
8789 if (strcmp (r->old_name, name) == 0)
8790 return r->new_name;
8791
8792 return name;
8793 }
8794
8795 #endif /* XTENSA_SECTION_RENAME */
This page took 0.203871 seconds and 5 git commands to generate.