1 /* Altera Nios II assembler.
2 Copyright (C) 2012-2022 Free Software Foundation, Inc.
3 Contributed by Nigel Gray (ngray@altera.com).
4 Contributed by Mentor Graphics, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24 #include "opcode/nios2.h"
25 #include "elf/nios2.h"
28 #include "dwarf2dbg.h"
30 #include "safe-ctype.h"
31 #include "dw2gencfi.h"
34 /* We are not supporting any other target so we throw a compile time error. */
38 /* We can choose our endianness at run-time, regardless of configuration. */
39 extern int target_big_endian
;
41 /* This array holds the chars that always start a comment. If the
42 pre-processor is disabled, these aren't very useful. */
43 const char comment_chars
[] = "#";
45 /* This array holds the chars that only start a comment at the beginning of
46 a line. If the line seems to have the form '# 123 filename'
47 .line and .file directives will appear in the pre-processed output. */
48 /* Note that input_file.c hand checks for '#' at the beginning of the
49 first line of the input file. This is because the compiler outputs
50 #NO_APP at the beginning of its output. */
51 /* Also note that C style comments are always supported. */
52 const char line_comment_chars
[] = "#";
54 /* This array holds machine specific line separator characters. */
55 const char line_separator_chars
[] = ";";
57 /* Chars that can be used to separate mant from exp in floating point nums. */
58 const char EXP_CHARS
[] = "eE";
60 /* Chars that mean this number is a floating point constant. */
63 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
65 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
66 changed in read.c. Ideally it shouldn't have to know about it at all,
67 but nothing is ideal around here. */
69 /* Machine-dependent command-line options. */
71 const char *md_shortopts
= "r";
73 struct option md_longopts
[] = {
74 #define OPTION_RELAX_ALL (OPTION_MD_BASE + 0)
75 {"relax-all", no_argument
, NULL
, OPTION_RELAX_ALL
},
76 #define OPTION_NORELAX (OPTION_MD_BASE + 1)
77 {"no-relax", no_argument
, NULL
, OPTION_NORELAX
},
78 #define OPTION_RELAX_SECTION (OPTION_MD_BASE + 2)
79 {"relax-section", no_argument
, NULL
, OPTION_RELAX_SECTION
},
80 #define OPTION_EB (OPTION_MD_BASE + 3)
81 {"EB", no_argument
, NULL
, OPTION_EB
},
82 #define OPTION_EL (OPTION_MD_BASE + 4)
83 {"EL", no_argument
, NULL
, OPTION_EL
},
84 #define OPTION_MARCH (OPTION_MD_BASE + 5)
85 {"march", required_argument
, NULL
, OPTION_MARCH
}
88 size_t md_longopts_size
= sizeof (md_longopts
);
90 /* The assembler supports three different relaxation modes, controlled by
91 command-line options. */
99 /* Struct contains all assembler options set with .set. */
102 /* .set noat -> noat = 1 allows assembly code to use at without warning
103 and macro expansions generate a warning.
104 .set at -> noat = 0, assembly code using at warn but macro expansions
105 do not generate warnings. */
108 /* .set nobreak -> nobreak = 1 allows assembly code to use ba,bt without
110 .set break -> nobreak = 0, assembly code using ba,bt warns. */
113 /* .cmd line option -relax-all allows all branches and calls to be replaced
114 with longer versions.
115 -no-relax inhibits branch/call conversion.
116 The default value is relax_section, which relaxes branches within
120 } nios2_as_options
= {false, false, relax_section
};
123 typedef struct nios2_insn_reloc
125 /* Any expression in the instruction is parsed into this field,
126 which is passed to fix_new_exp() to generate a fixup. */
127 expressionS reloc_expression
;
129 /* The type of the relocation to be applied. */
130 bfd_reloc_code_real_type reloc_type
;
133 unsigned int reloc_pcrel
;
135 /* The next relocation to be applied to the instruction. */
136 struct nios2_insn_reloc
*reloc_next
;
139 /* This struct is used to hold state when assembling instructions. */
140 typedef struct nios2_insn_info
142 /* Assembled instruction. */
143 unsigned long insn_code
;
145 /* Constant bits masked into insn_code for self-check mode. */
146 unsigned long constant_bits
;
148 /* Pointer to the relevant bit of the opcode table. */
149 const struct nios2_opcode
*insn_nios2_opcode
;
150 /* After parsing ptrs to the tokens in the instruction fill this array
151 it is terminated with a null pointer (hence the first +1).
152 The second +1 is because in some parts of the code the opcode
153 is not counted as a token, but still placed in this array. */
154 const char *insn_tokens
[NIOS2_MAX_INSN_TOKENS
+ 1 + 1];
156 /* This holds information used to generate fixups
157 and eventually relocations if it is not null. */
158 nios2_insn_relocS
*insn_reloc
;
162 /* This struct is used to convert Nios II pseudo-ops into the
163 corresponding real op. */
164 typedef struct nios2_ps_insn_info
166 /* Map this pseudo_op... */
167 const char *pseudo_insn
;
169 /* ...to this real instruction. */
172 /* Call this function to modify the operands.... */
173 void (*arg_modifer_func
) (char ** parsed_args
, const char *arg
, int num
,
176 /* ...with these arguments. */
177 const char *arg_modifier
;
181 /* If arg_modifier_func allocates new memory, provide this function
182 to free it afterwards. */
183 void (*arg_cleanup_func
) (char **parsed_args
, int num
, int start
);
184 } nios2_ps_insn_infoS
;
186 /* Opcode hash table. */
187 static htab_t nios2_opcode_hash
= NULL
;
188 #define nios2_opcode_lookup(NAME) \
189 ((struct nios2_opcode *) str_hash_find (nios2_opcode_hash, (NAME)))
191 /* Register hash table. */
192 static htab_t nios2_reg_hash
= NULL
;
193 #define nios2_reg_lookup(NAME) \
194 ((struct nios2_reg *) str_hash_find (nios2_reg_hash, (NAME)))
197 /* Pseudo-op hash table. */
198 static htab_t nios2_ps_hash
= NULL
;
199 #define nios2_ps_lookup(NAME) \
200 ((nios2_ps_insn_infoS *) str_hash_find (nios2_ps_hash, (NAME)))
202 /* The known current alignment of the current section. */
203 static int nios2_current_align
;
204 static segT nios2_current_align_seg
;
206 static int nios2_auto_align_on
= 1;
208 /* The last seen label in the current section. This is used to auto-align
209 labels preceding instructions. */
210 static symbolS
*nios2_last_label
;
212 /* If we saw a 16-bit CDX instruction, we can align on 2-byte boundaries
213 instead of 4-bytes. Use this to keep track of the minimum power-of-2
215 static int nios2_min_align
= 2;
218 /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
222 /* The processor architecture value, EF_NIOS2_ARCH_R1 by default. */
223 static int nios2_architecture
= EF_NIOS2_ARCH_R1
;
226 /** Utility routines. */
227 /* Function md_chars_to_number takes the sequence of
228 bytes in buf and returns the corresponding value
229 in an int. n must be 1, 2 or 4. */
231 md_chars_to_number (char *buf
, int n
)
236 gas_assert (n
== 1 || n
== 2 || n
== 4);
239 if (target_big_endian
)
240 for (i
= 0; i
< n
; ++i
)
241 val
= val
| ((valueT
) (buf
[i
] & 0xff) << 8 * (n
- (i
+ 1)));
243 for (i
= 0; i
< n
; ++i
)
244 val
= val
| ((valueT
) (buf
[i
] & 0xff) << 8 * i
);
249 /* This function turns a C long int, short int or char
250 into the series of bytes that represent the number
251 on the target machine. */
253 md_number_to_chars (char *buf
, valueT val
, int n
)
255 gas_assert (n
== 1 || n
== 2 || n
== 4 || n
== 8);
256 if (target_big_endian
)
257 number_to_chars_bigendian (buf
, val
, n
);
259 number_to_chars_littleendian (buf
, val
, n
);
262 /* Turn a string in input_line_pointer into a floating point constant
263 of type TYPE, and store the appropriate bytes in *LITP. The number
264 of LITTLENUMS emitted is stored in *SIZEP. An error message is
265 returned, or NULL on OK. */
267 md_atof (int type
, char *litP
, int *sizeP
)
270 LITTLENUM_TYPE words
[4];
284 return _("bad call to md_atof");
287 t
= atof_ieee (input_line_pointer
, type
, words
);
289 input_line_pointer
= t
;
293 if (! target_big_endian
)
294 for (i
= prec
- 1; i
>= 0; i
--, litP
+= 2)
295 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
297 for (i
= 0; i
< prec
; i
++, litP
+= 2)
298 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
303 /* Return true if STR is prefixed with a special relocation operator. */
305 nios2_special_relocation_p (const char *str
)
307 return (startswith (str
, "%lo")
308 || startswith (str
, "%hi")
309 || startswith (str
, "%hiadj")
310 || startswith (str
, "%gprel")
311 || startswith (str
, "%got")
312 || startswith (str
, "%call")
313 || startswith (str
, "%gotoff_lo")
314 || startswith (str
, "%gotoff_hiadj")
315 || startswith (str
, "%tls_gd")
316 || startswith (str
, "%tls_ldm")
317 || startswith (str
, "%tls_ldo")
318 || startswith (str
, "%tls_ie")
319 || startswith (str
, "%tls_le")
320 || startswith (str
, "%gotoff"));
324 /* nop fill patterns for text section. */
325 static char const nop_r1
[4] = { 0x3a, 0x88, 0x01, 0x00 };
326 static char const nop_r2
[4] = { 0x20, 0x00, 0x00, 0xc4 };
327 static char const nop_r2_cdx
[2] = { 0x3b, 0x00 };
328 static char const *nop32
= nop_r1
;
329 static char const *nop16
= NULL
;
331 /* Handles all machine-dependent alignment needs. */
333 nios2_align (int log_size
, const char *pfill
, symbolS
*label
)
336 long max_alignment
= 15;
338 /* The front end is prone to changing segments out from under us
339 temporarily when -g is in effect. */
340 int switched_seg_p
= (nios2_current_align_seg
!= now_seg
);
343 if (align
> max_alignment
)
345 align
= max_alignment
;
346 as_bad (_("Alignment too large: %d. assumed"), align
);
350 as_warn (_("Alignment negative: 0 assumed"));
356 if (subseg_text_p (now_seg
) && align
>= nios2_min_align
)
358 /* First, make sure we're on the minimum boundary, in case
359 someone has been putting .byte values the text section. */
360 if (nios2_current_align
< nios2_min_align
|| switched_seg_p
)
361 frag_align (nios2_min_align
, 0, 0);
363 /* If we might be on a 2-byte boundary, first align to a
364 4-byte boundary using the 2-byte nop as fill. */
365 if (nios2_min_align
== 1
366 && align
> nios2_min_align
370 frag_align_pattern (2, nop16
, 2, 0);
373 /* Now fill in the alignment pattern. */
375 frag_align_pattern (align
, pfill
, 4, 0);
377 frag_align (align
, 0, 0);
380 frag_align (align
, 0, 0);
383 nios2_current_align
= align
;
385 /* If the last label was in a different section we can't align it. */
386 if (label
!= NULL
&& !switched_seg_p
)
389 int label_seen
= false;
390 struct frag
*old_frag
;
394 gas_assert (S_GET_SEGMENT (label
) == now_seg
);
396 old_frag
= symbol_get_frag (label
);
397 old_value
= S_GET_VALUE (label
);
398 new_value
= (valueT
) frag_now_fix ();
400 /* It is possible to have more than one label at a particular
401 address, especially if debugging is enabled, so we must
402 take care to adjust all the labels at this address in this
403 fragment. To save time we search from the end of the symbol
404 list, backwards, since the symbols we are interested in are
405 almost certainly the ones that were most recently added.
406 Also to save time we stop searching once we have seen at least
407 one matching label, and we encounter a label that is no longer
408 in the target fragment. Note, this search is guaranteed to
409 find at least one match when sym == label, so no special case
410 code is necessary. */
411 for (sym
= symbol_lastP
; sym
!= NULL
; sym
= symbol_previous (sym
))
412 if (symbol_get_frag (sym
) == old_frag
413 && S_GET_VALUE (sym
) == old_value
)
416 symbol_set_frag (sym
, frag_now
);
417 S_SET_VALUE (sym
, new_value
);
419 else if (label_seen
&& symbol_get_frag (sym
) != old_frag
)
422 record_alignment (now_seg
, align
);
427 /** Support for self-check mode. */
429 /* Mode of the assembler. */
432 NIOS2_MODE_ASSEMBLE
, /* Ordinary operation. */
433 NIOS2_MODE_TEST
/* Hidden mode used for self testing. */
436 static NIOS2_MODE nios2_mode
= NIOS2_MODE_ASSEMBLE
;
438 /* This function is used to in self-checking mode
439 to check the assembled instruction
440 opcode should be the assembled opcode, and exp_opcode
441 the parsed string representing the expected opcode. */
443 nios2_check_assembly (unsigned int opcode
, const char *exp_opcode
)
445 if (nios2_mode
== NIOS2_MODE_TEST
)
447 if (exp_opcode
== NULL
)
448 as_bad (_("expecting opcode string in self test mode"));
449 else if (opcode
!= strtoul (exp_opcode
, NULL
, 16))
450 as_bad (_("assembly 0x%08x, expected %s"), opcode
, exp_opcode
);
455 /** Support for machine-dependent assembler directives. */
456 /* Handle the .align pseudo-op. This aligns to a power of two. It
457 also adjusts any current instruction label. We treat this the same
458 way the MIPS port does: .align 0 turns off auto alignment. */
460 s_nios2_align (int ignore ATTRIBUTE_UNUSED
)
464 const char *pfill
= NULL
;
465 long max_alignment
= 15;
467 align
= get_absolute_expression ();
468 if (align
> max_alignment
)
470 align
= max_alignment
;
471 as_bad (_("Alignment too large: %d. assumed"), align
);
475 as_warn (_("Alignment negative: 0 assumed"));
479 if (*input_line_pointer
== ',')
481 input_line_pointer
++;
482 fill
= get_absolute_expression ();
483 pfill
= (const char *) &fill
;
485 else if (subseg_text_p (now_seg
))
486 pfill
= (const char *) nop32
;
490 nios2_last_label
= NULL
;
495 nios2_auto_align_on
= 1;
496 nios2_align (align
, pfill
, nios2_last_label
);
497 nios2_last_label
= NULL
;
500 nios2_auto_align_on
= 0;
502 demand_empty_rest_of_line ();
505 /* Handle the .text pseudo-op. This is like the usual one, but it
506 clears the saved last label and resets known alignment. */
511 nios2_last_label
= NULL
;
512 nios2_current_align
= 0;
513 nios2_current_align_seg
= now_seg
;
516 /* Handle the .data pseudo-op. This is like the usual one, but it
517 clears the saved last label and resets known alignment. */
522 nios2_last_label
= NULL
;
523 nios2_current_align
= 0;
524 nios2_current_align_seg
= now_seg
;
527 /* Handle the .section pseudo-op. This is like the usual one, but it
528 clears the saved last label and resets known alignment. */
530 s_nios2_section (int ignore
)
532 obj_elf_section (ignore
);
533 nios2_last_label
= NULL
;
534 nios2_current_align
= 0;
535 nios2_current_align_seg
= now_seg
;
538 /* Explicitly unaligned cons. */
540 s_nios2_ucons (int nbytes
)
543 hold
= nios2_auto_align_on
;
544 nios2_auto_align_on
= 0;
546 nios2_auto_align_on
= hold
;
549 /* Handle the .sdata directive. */
551 s_nios2_sdata (int ignore ATTRIBUTE_UNUSED
)
553 get_absolute_expression (); /* Ignored. */
554 subseg_new (".sdata", 0);
555 demand_empty_rest_of_line ();
558 /* .set sets assembler options eg noat/at and is also used
559 to set symbol values (.equ, .equiv ). */
561 s_nios2_set (int equiv
)
563 char *save
= input_line_pointer
;
565 char delim
= get_symbol_name (&directive
);
566 char *endline
= input_line_pointer
;
568 (void) restore_line_pointer (delim
);
570 /* We only want to handle ".set XXX" if the
571 user has tried ".set XXX, YYY" they are not
572 trying a directive. This prevents
573 us from polluting the name space. */
575 if (is_end_of_line
[(unsigned char) *input_line_pointer
])
580 if (!strcmp (directive
, "noat"))
581 nios2_as_options
.noat
= true;
582 else if (!strcmp (directive
, "at"))
583 nios2_as_options
.noat
= false;
584 else if (!strcmp (directive
, "nobreak"))
585 nios2_as_options
.nobreak
= true;
586 else if (!strcmp (directive
, "break"))
587 nios2_as_options
.nobreak
= false;
588 else if (!strcmp (directive
, "norelax"))
589 nios2_as_options
.relax
= relax_none
;
590 else if (!strcmp (directive
, "relaxsection"))
591 nios2_as_options
.relax
= relax_section
;
592 else if (!strcmp (directive
, "relaxall"))
593 nios2_as_options
.relax
= relax_all
;
600 demand_empty_rest_of_line ();
605 /* If we fall through to here, either we have ".set XXX, YYY"
606 or we have ".set XXX" where XXX is unknown or we have
608 input_line_pointer
= save
;
612 /* Machine-dependent assembler directives.
613 Format of each entry is:
614 { "directive", handler_func, param } */
615 const pseudo_typeS md_pseudo_table
[] = {
616 {"align", s_nios2_align
, 0},
617 {"text", s_nios2_text
, 0},
618 {"data", s_nios2_data
, 0},
619 {"section", s_nios2_section
, 0},
620 {"section.s", s_nios2_section
, 0},
621 {"sect", s_nios2_section
, 0},
622 {"sect.s", s_nios2_section
, 0},
623 /* .dword and .half are included for compatibility with MIPS. */
626 /* NIOS2 native word size is 4 bytes, so we override
627 the GAS default of 2. */
629 /* Explicitly unaligned directives. */
630 {"2byte", s_nios2_ucons
, 2},
631 {"4byte", s_nios2_ucons
, 4},
632 {"8byte", s_nios2_ucons
, 8},
633 {"16byte", s_nios2_ucons
, 16},
635 {"sdata", s_nios2_sdata
, 0},
637 {"set", s_nios2_set
, 0},
642 /** Relaxation support. */
644 /* We support two relaxation modes: a limited PC-relative mode with
645 -relax-section (the default), and an absolute jump mode with -relax-all.
647 Nios II PC-relative branch instructions only support 16-bit offsets.
648 And, there's no good way to add a 32-bit constant to the PC without
651 To deal with this, for the pc-relative relaxation mode we convert
653 into a series of 16-bit adds, like:
657 addi at, at, remainder
660 Similarly, conditional branches are converted from
661 b(condition) r, s, label
663 b(opposite condition) r, s, skip
667 addi at, at, remainder
671 The compiler can do a better job, either by converting the branch
672 directly into a JMP (going through the GOT for PIC) or by allocating
673 a second register for the 32-bit displacement.
675 For the -relax-all relaxation mode, the conversions are
676 movhi at, %hi(symbol+offset)
677 ori at, %lo(symbol+offset)
680 b(opposite condition), r, s, skip
681 movhi at, %hi(symbol+offset)
682 ori at, %lo(symbol+offset)
687 16-bit CDX branch instructions are relaxed first into equivalent
688 32-bit branches and then the above transformations are applied
693 /* Arbitrarily limit the number of addis we can insert; we need to be able
694 to specify the maximum growth size for each frag that contains a
695 relaxable branch. There's no point in specifying a huge number here
696 since that means the assembler needs to allocate that much extra
697 memory for every branch, and almost no real code will ever need it.
698 Plus, as already noted a better solution is to just use a jmp, or
699 allocate a second register to hold a 32-bit displacement.
700 FIXME: Rather than making this a constant, it could be controlled by
701 a command-line argument. */
702 #define RELAX_MAX_ADDI 32
704 /* The fr_subtype field represents the target-specific relocation state.
705 It has type relax_substateT (unsigned int). We use it to track the
706 number of addis necessary, plus a bit to track whether this is a
707 conditional branch and a bit for 16-bit CDX instructions.
708 Regardless of the smaller RELAX_MAX_ADDI limit, we reserve 16 bits
709 in the fr_subtype to encode the number of addis so that the whole
710 theoretically-valid range is representable.
711 For the -relax-all mode, N = 0 represents an in-range branch and N = 1
712 represents a branch that needs to be relaxed. */
713 #define UBRANCH (0 << 16)
714 #define CBRANCH (1 << 16)
715 #define CDXBRANCH (1 << 17)
716 #define IS_CBRANCH(SUBTYPE) ((SUBTYPE) & CBRANCH)
717 #define IS_UBRANCH(SUBTYPE) (!IS_CBRANCH (SUBTYPE))
718 #define IS_CDXBRANCH(SUBTYPE) ((SUBTYPE) & CDXBRANCH)
719 #define UBRANCH_SUBTYPE(N) (UBRANCH | (N))
720 #define CBRANCH_SUBTYPE(N) (CBRANCH | (N))
721 #define CDX_UBRANCH_SUBTYPE(N) (CDXBRANCH | UBRANCH | (N))
722 #define CDX_CBRANCH_SUBTYPE(N) (CDXBRANCH | CBRANCH | (N))
723 #define SUBTYPE_ADDIS(SUBTYPE) ((SUBTYPE) & 0xffff)
725 /* For the -relax-section mode, unconditional branches require 2 extra
726 instructions besides the addis, conditional branches require 3. */
727 #define UBRANCH_ADDIS_TO_SIZE(N) (((N) + 2) * 4)
728 #define CBRANCH_ADDIS_TO_SIZE(N) (((N) + 3) * 4)
730 /* For the -relax-all mode, unconditional branches require 3 instructions
731 and conditional branches require 4. */
732 #define UBRANCH_JUMP_SIZE 12
733 #define CBRANCH_JUMP_SIZE 16
735 /* Maximum sizes of relaxation sequences. */
736 #define UBRANCH_MAX_SIZE \
737 (nios2_as_options.relax == relax_all \
738 ? UBRANCH_JUMP_SIZE \
739 : UBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
740 #define CBRANCH_MAX_SIZE \
741 (nios2_as_options.relax == relax_all \
742 ? CBRANCH_JUMP_SIZE \
743 : CBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
745 /* Register number of AT, the assembler temporary. */
748 /* Determine how many bytes are required to represent the sequence
749 indicated by SUBTYPE. */
751 nios2_relax_subtype_size (relax_substateT subtype
)
753 int n
= SUBTYPE_ADDIS (subtype
);
755 /* Regular conditional/unconditional branch instruction. */
756 return (IS_CDXBRANCH (subtype
) ? 2 : 4);
757 else if (nios2_as_options
.relax
== relax_all
)
758 return (IS_CBRANCH (subtype
) ? CBRANCH_JUMP_SIZE
: UBRANCH_JUMP_SIZE
);
759 else if (IS_CBRANCH (subtype
))
760 return CBRANCH_ADDIS_TO_SIZE (n
);
762 return UBRANCH_ADDIS_TO_SIZE (n
);
765 /* Estimate size of fragp before relaxation.
766 This could also examine the offset in fragp and adjust
767 fragp->fr_subtype, but we will do that in nios2_relax_frag anyway. */
769 md_estimate_size_before_relax (fragS
*fragp
, segT segment ATTRIBUTE_UNUSED
)
771 return nios2_relax_subtype_size (fragp
->fr_subtype
);
774 /* Implement md_relax_frag, returning the change in size of the frag. */
776 nios2_relax_frag (segT segment
, fragS
*fragp
, long stretch
)
778 addressT target
= fragp
->fr_offset
;
779 relax_substateT subtype
= fragp
->fr_subtype
;
780 symbolS
*symbolp
= fragp
->fr_symbol
;
784 fragS
*sym_frag
= symbol_get_frag (symbolp
);
789 target
+= S_GET_VALUE (symbolp
);
791 /* See comments in write.c:relax_frag about handling of stretch. */
793 && sym_frag
->relax_marker
!= fragp
->relax_marker
)
795 if (stretch
< 0 || sym_frag
->region
== fragp
->region
)
797 else if (target
< fragp
->fr_address
)
798 target
= fragp
->fr_next
->fr_address
+ stretch
;
801 /* We subtract fr_var (4 for 32-bit insns) because all pc relative
802 branches are from the next instruction. */
803 offset
= target
- fragp
->fr_address
- fragp
->fr_fix
- fragp
->fr_var
;
804 if (IS_CDXBRANCH (subtype
) && IS_UBRANCH (subtype
)
805 && offset
>= -1024 && offset
< 1024)
806 /* PC-relative CDX branch with 11-bit offset. */
808 else if (IS_CDXBRANCH (subtype
) && IS_CBRANCH (subtype
)
809 && offset
>= -128 && offset
< 128)
810 /* PC-relative CDX branch with 8-bit offset. */
812 else if (offset
>= -32768 && offset
< 32768)
813 /* Fits in PC-relative branch. */
815 else if (nios2_as_options
.relax
== relax_all
)
816 /* Convert to jump. */
818 else if (nios2_as_options
.relax
== relax_section
819 && S_GET_SEGMENT (symbolp
) == segment
820 && S_IS_DEFINED (symbolp
))
821 /* Attempt a PC-relative relaxation on a branch to a defined
822 symbol in the same segment. */
824 /* The relaxation for conditional branches is offset by 4
825 bytes because we insert the inverted branch around the
827 if (IS_CBRANCH (subtype
))
830 n
= offset
/ 32767 + 1;
832 n
= offset
/ -32768 + 1;
834 /* Bail out immediately if relaxation has failed. If we try to
835 defer the diagnostic to md_convert_frag, some pathological test
836 cases (e.g. gcc/testsuite/gcc.c-torture/compile/20001226-1.c)
837 apparently never converge. By returning 0 here we could pretend
838 to the caller that nothing has changed, but that leaves things
839 in an inconsistent state when we get to md_convert_frag. */
840 if (n
> RELAX_MAX_ADDI
)
842 as_bad_where (fragp
->fr_file
, fragp
->fr_line
,
843 _("branch offset out of range\n"));
844 as_fatal (_("branch relaxation failed\n"));
848 /* We cannot handle this case, diagnose overflow later. */
852 fragp
->fr_subtype
= subtype
;
853 else if (IS_CBRANCH (subtype
))
854 fragp
->fr_subtype
= CBRANCH_SUBTYPE (n
);
856 fragp
->fr_subtype
= UBRANCH_SUBTYPE (n
);
858 return (nios2_relax_subtype_size (fragp
->fr_subtype
)
859 - nios2_relax_subtype_size (subtype
));
862 /* If we got here, it's probably an error. */
867 /* Complete fragp using the data from the relaxation pass. */
869 md_convert_frag (bfd
*headers ATTRIBUTE_UNUSED
, segT segment ATTRIBUTE_UNUSED
,
872 char *buffer
= fragp
->fr_literal
+ fragp
->fr_fix
;
873 relax_substateT subtype
= fragp
->fr_subtype
;
874 int n
= SUBTYPE_ADDIS (subtype
);
875 addressT target
= fragp
->fr_offset
;
876 symbolS
*symbolp
= fragp
->fr_symbol
;
878 unsigned int addend_mask
, addi_mask
, op
;
879 offsetT addend
, remainder
;
881 bool is_r2
= (bfd_get_mach (stdoutput
) == bfd_mach_nios2r2
);
883 /* If this is a CDX branch we're not relaxing, just generate the fixup. */
884 if (IS_CDXBRANCH (subtype
))
887 fix_new (fragp
, fragp
->fr_fix
, 2, fragp
->fr_symbol
,
889 (IS_UBRANCH (subtype
)
890 ? BFD_RELOC_NIOS2_R2_I10_1_PCREL
891 : BFD_RELOC_NIOS2_R2_T1I7_1_PCREL
));
896 /* If this is a CDX branch we are relaxing, turn it into an equivalent
897 32-bit branch and then fall through to the normal non-CDX cases. */
898 if (fragp
->fr_var
== 2)
900 unsigned int opcode
= md_chars_to_number (buffer
, 2);
902 if (IS_CBRANCH (subtype
))
904 unsigned int reg
= nios2_r2_reg3_mappings
[GET_IW_T1I7_A3 (opcode
)];
905 if (GET_IW_R2_OP (opcode
) == R2_OP_BNEZ_N
)
906 opcode
= MATCH_R2_BNE
| SET_IW_F2I16_A (reg
);
908 opcode
= MATCH_R2_BEQ
| SET_IW_F2I16_A (reg
);
911 opcode
= MATCH_R2_BR
;
912 md_number_to_chars (buffer
, opcode
, 4);
916 /* If we didn't or can't relax, this is a regular branch instruction.
917 We just need to generate the fixup for the symbol and offset. */
920 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
,
921 fragp
->fr_offset
, 1, BFD_RELOC_16_PCREL
);
926 /* Replace the cbranch at fr_fix with one that has the opposite condition
927 in order to jump around the block of instructions we'll be adding. */
928 if (IS_CBRANCH (subtype
))
930 unsigned int br_opcode
;
931 unsigned int old_op
, new_op
;
934 /* Account for the nextpc and jmp in the pc-relative case, or the two
935 load instructions and jump in the absolute case. */
936 if (nios2_as_options
.relax
== relax_section
)
937 nbytes
= (n
+ 2) * 4;
941 br_opcode
= md_chars_to_number (buffer
, 4);
944 old_op
= GET_IW_R2_OP (br_opcode
);
968 br_opcode
= ((br_opcode
& ~IW_R2_OP_SHIFTED_MASK
)
969 | SET_IW_R2_OP (new_op
));
970 br_opcode
= br_opcode
| SET_IW_F2I16_IMM16 (nbytes
);
974 old_op
= GET_IW_R1_OP (br_opcode
);
998 br_opcode
= ((br_opcode
& ~IW_R1_OP_SHIFTED_MASK
)
999 | SET_IW_R1_OP (new_op
));
1000 br_opcode
= br_opcode
| SET_IW_I_IMM16 (nbytes
);
1002 md_number_to_chars (buffer
, br_opcode
, 4);
1007 /* Load at for the PC-relative case. */
1008 if (nios2_as_options
.relax
== relax_section
)
1010 /* Insert the nextpc instruction. */
1012 op
= MATCH_R2_NEXTPC
| SET_IW_F3X6L5_C (AT_REGNUM
);
1014 op
= MATCH_R1_NEXTPC
| SET_IW_R_C (AT_REGNUM
);
1015 md_number_to_chars (buffer
, op
, 4);
1019 /* We need to know whether the offset is positive or negative. */
1020 target
+= S_GET_VALUE (symbolp
);
1021 offset
= target
- fragp
->fr_address
- fragp
->fr_fix
;
1027 addend_mask
= SET_IW_F2I16_IMM16 ((unsigned int)addend
);
1029 addend_mask
= SET_IW_I_IMM16 ((unsigned int)addend
);
1031 /* Insert n-1 addi instructions. */
1033 addi_mask
= (MATCH_R2_ADDI
1034 | SET_IW_F2I16_B (AT_REGNUM
)
1035 | SET_IW_F2I16_A (AT_REGNUM
));
1037 addi_mask
= (MATCH_R1_ADDI
1038 | SET_IW_I_B (AT_REGNUM
)
1039 | SET_IW_I_A (AT_REGNUM
));
1040 for (i
= 0; i
< n
- 1; i
++)
1042 md_number_to_chars (buffer
, addi_mask
| addend_mask
, 4);
1047 /* Insert the last addi instruction to hold the remainder. */
1048 remainder
= offset
- addend
* (n
- 1);
1049 gas_assert (remainder
>= -32768 && remainder
<= 32767);
1051 addend_mask
= SET_IW_F2I16_IMM16 ((unsigned int)remainder
);
1053 addend_mask
= SET_IW_I_IMM16 ((unsigned int)remainder
);
1054 md_number_to_chars (buffer
, addi_mask
| addend_mask
, 4);
1059 /* Load at for the absolute case. */
1063 op
= MATCH_R2_ORHI
| SET_IW_F2I16_B (AT_REGNUM
) | SET_IW_F2I16_A (0);
1065 op
= MATCH_R1_ORHI
| SET_IW_I_B (AT_REGNUM
) | SET_IW_I_A (0);
1066 md_number_to_chars (buffer
, op
, 4);
1067 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
,
1068 0, BFD_RELOC_NIOS2_HI16
);
1072 op
= (MATCH_R2_ORI
| SET_IW_F2I16_B (AT_REGNUM
)
1073 | SET_IW_F2I16_A (AT_REGNUM
));
1075 op
= (MATCH_R1_ORI
| SET_IW_I_B (AT_REGNUM
)
1076 | SET_IW_I_A (AT_REGNUM
));
1077 md_number_to_chars (buffer
, op
, 4);
1078 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
,
1079 0, BFD_RELOC_NIOS2_LO16
);
1084 /* Insert the jmp instruction. */
1086 op
= MATCH_R2_JMP
| SET_IW_F3X6L5_A (AT_REGNUM
);
1088 op
= MATCH_R1_JMP
| SET_IW_R_A (AT_REGNUM
);
1089 md_number_to_chars (buffer
, op
, 4);
1095 /** Fixups and overflow checking. */
1097 /* Check a fixup for overflow. */
1099 nios2_check_overflow (valueT fixup
, reloc_howto_type
*howto
)
1101 /* If there is a rightshift, check that the low-order bits are
1102 zero before applying it. */
1103 if (howto
->rightshift
)
1105 if ((~(~((valueT
) 0) << howto
->rightshift
) & fixup
)
1106 && howto
->complain_on_overflow
!= complain_overflow_dont
)
1108 fixup
= ((signed)fixup
) >> howto
->rightshift
;
1111 /* Check for overflow - return TRUE if overflow, FALSE if not. */
1112 switch (howto
->complain_on_overflow
)
1114 case complain_overflow_dont
:
1116 case complain_overflow_bitfield
:
1117 if ((fixup
>> howto
->bitsize
) != 0
1118 && ((signed) fixup
>> howto
->bitsize
) != -1)
1121 case complain_overflow_signed
:
1122 if ((fixup
& 0x80000000) > 0)
1124 /* Check for negative overflow. */
1125 if ((signed) fixup
< (signed) (~0U << (howto
->bitsize
- 1)))
1130 /* Check for positive overflow. */
1131 if (fixup
>= ((unsigned) 1 << (howto
->bitsize
- 1)))
1135 case complain_overflow_unsigned
:
1136 if ((fixup
>> howto
->bitsize
) != 0)
1140 as_bad (_("error checking for overflow - broken assembler"));
1146 /* Emit diagnostic for fixup overflow. */
1148 nios2_diagnose_overflow (valueT fixup
, reloc_howto_type
*howto
,
1149 fixS
*fixP
, valueT value
)
1151 if (fixP
->fx_r_type
== BFD_RELOC_8
1152 || fixP
->fx_r_type
== BFD_RELOC_16
1153 || fixP
->fx_r_type
== BFD_RELOC_32
)
1154 /* These relocs are against data, not instructions. */
1155 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1156 _("immediate value 0x%x truncated to 0x%x"),
1157 (unsigned int) fixup
,
1158 (unsigned int) (~(~(valueT
) 0 << howto
->bitsize
) & fixup
));
1161 /* What opcode is the instruction? This will determine
1162 whether we check for overflow in immediate values
1163 and what error message we get. */
1164 const struct nios2_opcode
*opcode
;
1165 enum overflow_type overflow_msg_type
;
1166 unsigned int range_min
;
1167 unsigned int range_max
;
1168 unsigned int address
;
1170 opcode
= nios2_find_opcode_hash (value
, bfd_get_mach (stdoutput
));
1171 gas_assert (opcode
);
1172 gas_assert (fixP
->fx_size
== opcode
->size
);
1173 overflow_msg_type
= opcode
->overflow_msg
;
1174 switch (overflow_msg_type
)
1176 case call_target_overflow
:
1178 = ((fixP
->fx_frag
->fr_address
+ fixP
->fx_where
) & 0xf0000000);
1179 range_max
= range_min
+ 0x0fffffff;
1180 address
= fixup
| range_min
;
1182 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1183 _("call target address 0x%08x out of range 0x%08x to 0x%08x"),
1184 address
, range_min
, range_max
);
1186 case branch_target_overflow
:
1187 if (opcode
->format
== iw_i_type
|| opcode
->format
== iw_F2I16_type
)
1188 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1189 _("branch offset %d out of range %d to %d"),
1190 (int)fixup
, -32768, 32767);
1192 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1193 _("branch offset %d out of range"),
1196 case address_offset_overflow
:
1197 if (opcode
->format
== iw_i_type
|| opcode
->format
== iw_F2I16_type
)
1198 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1199 _("%s offset %d out of range %d to %d"),
1200 opcode
->name
, (int)fixup
, -32768, 32767);
1202 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1203 _("%s offset %d out of range"),
1204 opcode
->name
, (int)fixup
);
1206 case signed_immed16_overflow
:
1207 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1208 _("immediate value %d out of range %d to %d"),
1209 (int)fixup
, -32768, 32767);
1211 case unsigned_immed16_overflow
:
1212 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1213 _("immediate value %u out of range %u to %u"),
1214 (unsigned int)fixup
, 0, 65535);
1216 case unsigned_immed5_overflow
:
1217 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1218 _("immediate value %u out of range %u to %u"),
1219 (unsigned int)fixup
, 0, 31);
1221 case signed_immed12_overflow
:
1222 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1223 _("immediate value %d out of range %d to %d"),
1224 (int)fixup
, -2048, 2047);
1226 case custom_opcode_overflow
:
1227 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1228 _("custom instruction opcode %u out of range %u to %u"),
1229 (unsigned int)fixup
, 0, 255);
1232 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1233 _("overflow in immediate argument"));
1239 /* Apply a fixup to the object file. */
1241 md_apply_fix (fixS
*fixP
, valueT
*valP
, segT seg ATTRIBUTE_UNUSED
)
1243 /* Assert that the fixup is one we can handle. */
1244 gas_assert (fixP
!= NULL
&& valP
!= NULL
1245 && (fixP
->fx_r_type
== BFD_RELOC_8
1246 || fixP
->fx_r_type
== BFD_RELOC_16
1247 || fixP
->fx_r_type
== BFD_RELOC_32
1248 || fixP
->fx_r_type
== BFD_RELOC_64
1249 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_S16
1250 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_U16
1251 || fixP
->fx_r_type
== BFD_RELOC_16_PCREL
1252 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL26
1253 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM5
1254 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CACHE_OPX
1255 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM6
1256 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM8
1257 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_HI16
1258 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_LO16
1259 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_HIADJ16
1260 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GPREL
1261 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1262 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
1263 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_UJMP
1264 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CJMP
1265 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALLR
1266 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_ALIGN
1267 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT16
1268 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL16
1269 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_LO
1270 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_HA
1271 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_GD16
1272 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDM16
1273 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDO16
1274 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_IE16
1275 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LE16
1276 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF
1277 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPREL
1278 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL26_NOAT
1279 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT_LO
1280 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT_HA
1281 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL_LO
1282 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL_HA
1283 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_S12
1284 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_I10_1_PCREL
1285 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T1I7_1_PCREL
1286 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T1I7_2
1287 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T2I4
1288 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T2I4_1
1289 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T2I4_2
1290 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_X1I7_2
1291 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_X2L5
1292 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_F1I5_2
1293 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_L5I4X1
1294 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T1X1I6
1295 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_R2_T1X1I6_2
1296 /* Add other relocs here as we generate them. */
1299 if (fixP
->fx_r_type
== BFD_RELOC_64
)
1301 /* We may reach here due to .8byte directives, but we never output
1302 BFD_RELOC_64; it must be resolved. */
1303 if (fixP
->fx_addsy
!= NULL
)
1304 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1305 _("cannot create 64-bit relocation"));
1308 md_number_to_chars (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
,
1315 /* The value passed in valP can be the value of a fully
1316 resolved expression, or it can be the value of a partially
1317 resolved expression. In the former case, both fixP->fx_addsy
1318 and fixP->fx_subsy are NULL, and fixP->fx_offset == *valP, and
1319 we can fix up the instruction that fixP relates to.
1320 In the latter case, one or both of fixP->fx_addsy and
1321 fixP->fx_subsy are not NULL, and fixP->fx_offset may or may not
1322 equal *valP. We don't need to check for fixP->fx_subsy being null
1323 because the generic part of the assembler generates an error if
1324 it is not an absolute symbol. */
1325 if (fixP
->fx_addsy
!= NULL
)
1326 /* Partially resolved expression. */
1328 fixP
->fx_addnumber
= fixP
->fx_offset
;
1331 switch (fixP
->fx_r_type
)
1333 case BFD_RELOC_NIOS2_TLS_GD16
:
1334 case BFD_RELOC_NIOS2_TLS_LDM16
:
1335 case BFD_RELOC_NIOS2_TLS_LDO16
:
1336 case BFD_RELOC_NIOS2_TLS_IE16
:
1337 case BFD_RELOC_NIOS2_TLS_LE16
:
1338 case BFD_RELOC_NIOS2_TLS_DTPMOD
:
1339 case BFD_RELOC_NIOS2_TLS_DTPREL
:
1340 case BFD_RELOC_NIOS2_TLS_TPREL
:
1341 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
1348 /* Fully resolved fixup. */
1350 reloc_howto_type
*howto
1351 = bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
);
1354 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1355 _("relocation is not supported"));
1358 valueT fixup
= *valP
;
1362 /* If this is a pc-relative relocation, we need to
1363 subtract the current offset within the object file
1364 FIXME : for some reason fixP->fx_pcrel isn't 1 when it should be
1365 so I'm using the howto structure instead to determine this. */
1366 if (howto
->pc_relative
== 1)
1368 fixup
= (fixup
- (fixP
->fx_frag
->fr_address
+ fixP
->fx_where
1373 /* Get the instruction or data to be fixed up. */
1374 buf
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1375 value
= md_chars_to_number (buf
, fixP
->fx_size
);
1377 /* Check for overflow, emitting a diagnostic if necessary. */
1378 if (nios2_check_overflow (fixup
, howto
))
1379 nios2_diagnose_overflow (fixup
, howto
, fixP
, value
);
1381 /* Apply the right shift. */
1382 fixup
= (offsetT
) fixup
>> howto
->rightshift
;
1384 /* Truncate the fixup to right size. */
1385 switch (fixP
->fx_r_type
)
1387 case BFD_RELOC_NIOS2_HI16
:
1388 fixup
= (fixup
>> 16) & 0xFFFF;
1390 case BFD_RELOC_NIOS2_LO16
:
1391 fixup
= fixup
& 0xFFFF;
1393 case BFD_RELOC_NIOS2_HIADJ16
:
1394 fixup
= ((fixup
+ 0x8000) >> 16) & 0xFFFF;
1398 fixup
&= ((valueT
) 2 << (howto
->bitsize
- 1)) - 1;
1403 /* Fix up the instruction. */
1404 value
= (value
& ~howto
->dst_mask
) | (fixup
<< howto
->bitpos
);
1405 md_number_to_chars (buf
, value
, fixP
->fx_size
);
1411 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
)
1415 && !S_IS_DEFINED (fixP
->fx_addsy
) && !S_IS_WEAK (fixP
->fx_addsy
))
1416 S_SET_WEAK (fixP
->fx_addsy
);
1418 else if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1424 /** Instruction parsing support. */
1426 /* General internal error routine. */
1429 bad_opcode (const struct nios2_opcode
*op
)
1431 fprintf (stderr
, _("internal error: broken opcode descriptor for `%s %s'\n"),
1432 op
->name
, op
->args
);
1433 as_fatal (_("Broken assembler. No assembly attempted."));
1436 /* Special relocation directive strings. */
1438 struct nios2_special_relocS
1441 bfd_reloc_code_real_type reloc_type
;
1444 /* This table is sorted so that prefix strings are listed after the longer
1445 strings that include them -- e.g., %got after %got_hiadj, etc. */
1447 struct nios2_special_relocS nios2_special_reloc
[] = {
1448 {"%hiadj", BFD_RELOC_NIOS2_HIADJ16
},
1449 {"%hi", BFD_RELOC_NIOS2_HI16
},
1450 {"%lo", BFD_RELOC_NIOS2_LO16
},
1451 {"%gprel", BFD_RELOC_NIOS2_GPREL
},
1452 {"%call_lo", BFD_RELOC_NIOS2_CALL_LO
},
1453 {"%call_hiadj", BFD_RELOC_NIOS2_CALL_HA
},
1454 {"%call", BFD_RELOC_NIOS2_CALL16
},
1455 {"%gotoff_lo", BFD_RELOC_NIOS2_GOTOFF_LO
},
1456 {"%gotoff_hiadj", BFD_RELOC_NIOS2_GOTOFF_HA
},
1457 {"%gotoff", BFD_RELOC_NIOS2_GOTOFF
},
1458 {"%got_hiadj", BFD_RELOC_NIOS2_GOT_HA
},
1459 {"%got_lo", BFD_RELOC_NIOS2_GOT_LO
},
1460 {"%got", BFD_RELOC_NIOS2_GOT16
},
1461 {"%tls_gd", BFD_RELOC_NIOS2_TLS_GD16
},
1462 {"%tls_ldm", BFD_RELOC_NIOS2_TLS_LDM16
},
1463 {"%tls_ldo", BFD_RELOC_NIOS2_TLS_LDO16
},
1464 {"%tls_ie", BFD_RELOC_NIOS2_TLS_IE16
},
1465 {"%tls_le", BFD_RELOC_NIOS2_TLS_LE16
},
1468 #define NIOS2_NUM_SPECIAL_RELOCS \
1469 (sizeof(nios2_special_reloc)/sizeof(nios2_special_reloc[0]))
1470 const int nios2_num_special_relocs
= NIOS2_NUM_SPECIAL_RELOCS
;
1472 /* Creates a new nios2_insn_relocS and returns a pointer to it. */
1473 static nios2_insn_relocS
*
1474 nios2_insn_reloc_new (bfd_reloc_code_real_type reloc_type
, unsigned int pcrel
)
1476 nios2_insn_relocS
*retval
;
1477 retval
= XNEW (nios2_insn_relocS
);
1480 as_bad (_("can't create relocation"));
1484 /* Fill out the fields with default values. */
1485 retval
->reloc_next
= NULL
;
1486 retval
->reloc_type
= reloc_type
;
1487 retval
->reloc_pcrel
= pcrel
;
1491 /* Frees up memory previously allocated by nios2_insn_reloc_new(). */
1492 /* FIXME: this is never called; memory leak? */
1495 nios2_insn_reloc_destroy (nios2_insn_relocS
*reloc
)
1497 gas_assert (reloc
!= NULL
);
1502 /* Look up a register name and validate it for the given regtype.
1503 Return the register mapping or NULL on failure. */
1504 static struct nios2_reg
*
1505 nios2_parse_reg (const char *token
, unsigned long regtype
)
1507 struct nios2_reg
*reg
= nios2_reg_lookup (token
);
1511 as_bad (_("unknown register %s"), token
);
1515 /* Matched a register, but is it the wrong type? */
1516 if (!(regtype
& reg
->regtype
))
1518 if (regtype
& REG_CONTROL
)
1519 as_bad (_("expecting control register"));
1520 else if (reg
->regtype
& REG_CONTROL
)
1521 as_bad (_("illegal use of control register"));
1522 else if (reg
->regtype
& REG_COPROCESSOR
)
1523 as_bad (_("illegal use of coprocessor register"));
1525 as_bad (_("invalid register %s"), token
);
1529 /* Warn for explicit use of special registers. */
1530 if (reg
->regtype
& REG_NORMAL
)
1532 if (!nios2_as_options
.noat
&& reg
->index
== 1)
1533 as_warn (_("Register at (r1) can sometimes be corrupted by "
1534 "assembler optimizations.\n"
1535 "Use .set noat to turn off those optimizations "
1536 "(and this warning)."));
1537 if (!nios2_as_options
.nobreak
&& reg
->index
== 25)
1538 as_warn (_("The debugger will corrupt bt (r25).\n"
1539 "If you don't need to debug this "
1540 "code use .set nobreak to turn off this warning."));
1541 if (!nios2_as_options
.nobreak
&& reg
->index
== 30)
1542 as_warn (_("The debugger will corrupt sstatus/ba (r30).\n"
1543 "If you don't need to debug this "
1544 "code use .set nobreak to turn off this warning."));
1550 /* This function parses a reglist for ldwm/stwm and push.n/pop.n
1551 instructions, given as a brace-enclosed register list. The tokenizer
1552 has replaced commas in the token with spaces.
1553 The return value is a bitmask of registers in the set. It also
1554 sets nios2_reglist_mask and nios2_reglist_dir to allow error checking
1555 when parsing the base register. */
1557 static unsigned long nios2_reglist_mask
;
1558 static int nios2_reglist_dir
;
1560 static unsigned long
1561 nios2_parse_reglist (char *token
, const struct nios2_opcode
*op
)
1563 unsigned long mask
= 0;
1565 unsigned long regtype
= 0;
1567 const char *regname
;
1569 nios2_reglist_mask
= 0;
1570 nios2_reglist_dir
= 0;
1572 if (op
->match
== MATCH_R2_LDWM
|| op
->match
== MATCH_R2_STWM
)
1577 else if (op
->match
== MATCH_R2_PUSH_N
)
1582 else if (op
->match
== MATCH_R2_POP_N
)
1590 for (regname
= strtok (token
, "{ }");
1592 regname
= strtok (NULL
, "{ }"))
1595 struct nios2_reg
*reg
= nios2_parse_reg (regname
, regtype
);
1601 /* Make sure registers are listed in proper sequence. */
1606 as_bad ("duplicate register %s\n", reg
->name
);
1610 dir
= (regno
< last
? -1 : 1);
1611 else if ((dir
> 0 && regno
< last
)
1612 || (dir
< 0 && regno
> last
)
1613 || (op
->match
== MATCH_R2_PUSH_N
1614 && ! ((last
== 31 && regno
== 28)
1615 || (last
== 31 && regno
<= 23)
1616 || (last
== 28 && regno
<= 23)
1617 || (regno
< 23 && regno
== last
- 1)))
1618 || (op
->match
== MATCH_R2_POP_N
1619 && ! ((regno
== 31 && last
== 28)
1620 || (regno
== 31 && last
<= 23)
1621 || (regno
== 28 && last
<= 23)
1622 || (last
< 23 && last
== regno
- 1))))
1624 as_bad ("invalid register order");
1629 mask
|= 1UL << regno
;
1633 /* Check that all ldwm/stwm regs belong to the same set. */
1634 if ((op
->match
== MATCH_R2_LDWM
|| op
->match
== MATCH_R2_STWM
)
1635 && (mask
& 0x00003ffc) && (mask
& 0x90ffc000))
1637 as_bad ("invalid register set in reglist");
1641 /* Check that push.n/pop.n regs include RA. */
1642 if ((op
->match
== MATCH_R2_PUSH_N
|| op
->match
== MATCH_R2_POP_N
)
1643 && ((mask
& 0x80000000) == 0))
1645 as_bad ("reglist must include ra (r31)");
1649 /* Check that there is at least one register in the set. */
1652 as_bad ("reglist must include at least one register");
1656 /* OK, reglist passed validation. */
1657 nios2_reglist_mask
= mask
;
1658 nios2_reglist_dir
= dir
;
1662 /* This function parses the base register and options used by the ldwm/stwm
1663 instructions. Returns the base register and sets the option arguments
1664 accordingly. On failure, returns NULL. */
1665 static struct nios2_reg
*
1666 nios2_parse_base_register (char *str
, int *direction
, int *writeback
, int *ret
)
1669 struct nios2_reg
*reg
;
1676 if (startswith (str
, "--"))
1682 /* Extract the base register. */
1685 as_bad ("expected '(' before base register");
1690 str
= strchr (str
, ')');
1693 as_bad ("expected ')' after base register");
1698 reg
= nios2_parse_reg (regname
, REG_NORMAL
);
1703 if (startswith (str
, "++"))
1709 /* Ensure that either -- or ++ is specified, but not both. */
1710 if (*direction
== 0)
1712 as_bad ("invalid base register syntax");
1716 /* Check for options. The tokenizer has replaced commas with spaces. */
1721 if (startswith (str
, "writeback"))
1726 else if (startswith (str
, "ret"))
1733 as_bad ("invalid option syntax");
1742 /* The various nios2_assemble_* functions call this
1743 function to generate an expression from a string representing an expression.
1744 It then tries to evaluate the expression, and if it can, returns its value.
1745 If not, it creates a new nios2_insn_relocS and stores the expression and
1746 reloc_type for future use. */
1747 static unsigned long
1748 nios2_assemble_expression (const char *exprstr
,
1749 nios2_insn_infoS
*insn
,
1750 bfd_reloc_code_real_type orig_reloc_type
,
1753 nios2_insn_relocS
*reloc
;
1754 char *saved_line_ptr
;
1755 unsigned long value
= 0;
1757 bfd_reloc_code_real_type reloc_type
= orig_reloc_type
;
1759 gas_assert (exprstr
!= NULL
);
1760 gas_assert (insn
!= NULL
);
1762 /* Check for relocation operators.
1763 Change the relocation type and advance the ptr to the start of
1764 the expression proper. */
1765 for (i
= 0; i
< nios2_num_special_relocs
; i
++)
1766 if (strstr (exprstr
, nios2_special_reloc
[i
].string
) != NULL
)
1768 reloc_type
= nios2_special_reloc
[i
].reloc_type
;
1769 exprstr
+= strlen (nios2_special_reloc
[i
].string
) + 1;
1771 /* %lo and %hiadj have different meanings for PC-relative
1775 if (reloc_type
== BFD_RELOC_NIOS2_LO16
)
1776 reloc_type
= BFD_RELOC_NIOS2_PCREL_LO
;
1777 if (reloc_type
== BFD_RELOC_NIOS2_HIADJ16
)
1778 reloc_type
= BFD_RELOC_NIOS2_PCREL_HA
;
1784 /* No relocation allowed; we must have a constant expression. */
1785 if (orig_reloc_type
== BFD_RELOC_NONE
)
1789 /* Parse the expression string. */
1790 saved_line_ptr
= input_line_pointer
;
1791 input_line_pointer
= (char *) exprstr
;
1793 input_line_pointer
= saved_line_ptr
;
1795 /* If we don't have a constant, give an error. */
1796 if (reloc_type
!= orig_reloc_type
|| exp
.X_op
!= O_constant
)
1797 as_bad (_("expression must be constant"));
1799 value
= exp
.X_add_number
;
1800 return (unsigned long) value
;
1803 /* We potentially have a relocation. */
1804 reloc
= nios2_insn_reloc_new (reloc_type
, pcrel
);
1805 reloc
->reloc_next
= insn
->insn_reloc
;
1806 insn
->insn_reloc
= reloc
;
1808 /* Parse the expression string. */
1809 saved_line_ptr
= input_line_pointer
;
1810 input_line_pointer
= (char *) exprstr
;
1811 expression (&reloc
->reloc_expression
);
1812 input_line_pointer
= saved_line_ptr
;
1814 /* This is redundant as the fixup will put this into
1815 the instruction, but it is included here so that
1816 self-test mode (-r) works. */
1817 if (nios2_mode
== NIOS2_MODE_TEST
1818 && reloc
->reloc_expression
.X_op
== O_constant
)
1819 value
= reloc
->reloc_expression
.X_add_number
;
1821 return (unsigned long) value
;
1824 /* Encode a 3-bit register number, giving an error if this is not possible. */
1826 nios2_assemble_reg3 (const char *token
)
1828 struct nios2_reg
*reg
= nios2_parse_reg (token
, REG_3BIT
);
1834 for (j
= 0; j
< nios2_num_r2_reg3_mappings
; j
++)
1835 if (nios2_r2_reg3_mappings
[j
] == reg
->index
)
1838 /* Should never get here if we passed validation. */
1839 as_bad (_("invalid register %s"), token
);
1843 /* Argument assemble functions. */
1846 /* Control register index. */
1848 nios2_assemble_arg_c (const char *token
, nios2_insn_infoS
*insn
)
1850 struct nios2_reg
*reg
= nios2_parse_reg (token
, REG_CONTROL
);
1851 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1859 insn
->insn_code
|= SET_IW_R_IMM5 (reg
->index
);
1861 case iw_F3X6L5_type
:
1862 insn
->insn_code
|= SET_IW_F3X6L5_IMM5 (reg
->index
);
1869 /* Destination register. */
1871 nios2_assemble_arg_d (const char *token
, nios2_insn_infoS
*insn
)
1873 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1874 unsigned long regtype
= REG_NORMAL
;
1875 struct nios2_reg
*reg
;
1877 if (op
->format
== iw_custom_type
|| op
->format
== iw_F3X8_type
)
1878 regtype
|= REG_COPROCESSOR
;
1879 reg
= nios2_parse_reg (token
, regtype
);
1886 insn
->insn_code
|= SET_IW_R_C (reg
->index
);
1888 case iw_custom_type
:
1889 insn
->insn_code
|= SET_IW_CUSTOM_C (reg
->index
);
1890 if (reg
->regtype
& REG_COPROCESSOR
)
1891 insn
->insn_code
|= SET_IW_CUSTOM_READC (0);
1893 insn
->insn_code
|= SET_IW_CUSTOM_READC (1);
1895 case iw_F3X6L5_type
:
1897 insn
->insn_code
|= SET_IW_F3X6L5_C (reg
->index
);
1900 insn
->insn_code
|= SET_IW_F3X8_C (reg
->index
);
1901 if (reg
->regtype
& REG_COPROCESSOR
)
1902 insn
->insn_code
|= SET_IW_F3X8_READC (0);
1904 insn
->insn_code
|= SET_IW_F3X8_READC (1);
1907 insn
->insn_code
|= SET_IW_F2_B (reg
->index
);
1914 /* Source register 1. */
1916 nios2_assemble_arg_s (const char *token
, nios2_insn_infoS
*insn
)
1918 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1919 unsigned long regtype
= REG_NORMAL
;
1920 struct nios2_reg
*reg
;
1922 if (op
->format
== iw_custom_type
|| op
->format
== iw_F3X8_type
)
1923 regtype
|= REG_COPROCESSOR
;
1924 reg
= nios2_parse_reg (token
, regtype
);
1931 if (op
->match
== MATCH_R1_JMP
&& reg
->index
== 31)
1932 as_bad (_("r31 cannot be used with jmp; use ret instead"));
1933 insn
->insn_code
|= SET_IW_R_A (reg
->index
);
1936 insn
->insn_code
|= SET_IW_I_A (reg
->index
);
1938 case iw_custom_type
:
1939 insn
->insn_code
|= SET_IW_CUSTOM_A (reg
->index
);
1940 if (reg
->regtype
& REG_COPROCESSOR
)
1941 insn
->insn_code
|= SET_IW_CUSTOM_READA (0);
1943 insn
->insn_code
|= SET_IW_CUSTOM_READA (1);
1946 insn
->insn_code
|= SET_IW_F2I16_A (reg
->index
);
1948 case iw_F2X4I12_type
:
1949 insn
->insn_code
|= SET_IW_F2X4I12_A (reg
->index
);
1951 case iw_F1X4I12_type
:
1952 insn
->insn_code
|= SET_IW_F1X4I12_A (reg
->index
);
1954 case iw_F1X4L17_type
:
1955 insn
->insn_code
|= SET_IW_F1X4L17_A (reg
->index
);
1957 case iw_F3X6L5_type
:
1959 if (op
->match
== MATCH_R2_JMP
&& reg
->index
== 31)
1960 as_bad (_("r31 cannot be used with jmp; use ret instead"));
1961 insn
->insn_code
|= SET_IW_F3X6L5_A (reg
->index
);
1963 case iw_F2X6L10_type
:
1964 insn
->insn_code
|= SET_IW_F2X6L10_A (reg
->index
);
1967 insn
->insn_code
|= SET_IW_F3X8_A (reg
->index
);
1968 if (reg
->regtype
& REG_COPROCESSOR
)
1969 insn
->insn_code
|= SET_IW_F3X8_READA (0);
1971 insn
->insn_code
|= SET_IW_F3X8_READA (1);
1974 if (op
->match
== MATCH_R2_JMPR_N
&& reg
->index
== 31)
1975 as_bad (_("r31 cannot be used with jmpr.n; use ret.n instead"));
1976 insn
->insn_code
|= SET_IW_F1X1_A (reg
->index
);
1979 /* Implicit stack pointer reference. */
1980 if (reg
->index
!= 27)
1981 as_bad (_("invalid register %s"), token
);
1984 insn
->insn_code
|= SET_IW_F2_A (reg
->index
);
1991 /* Source register 2. */
1993 nios2_assemble_arg_t (const char *token
, nios2_insn_infoS
*insn
)
1995 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1996 unsigned long regtype
= REG_NORMAL
;
1997 struct nios2_reg
*reg
;
1999 if (op
->format
== iw_custom_type
|| op
->format
== iw_F3X8_type
)
2000 regtype
|= REG_COPROCESSOR
;
2001 reg
= nios2_parse_reg (token
, regtype
);
2008 insn
->insn_code
|= SET_IW_R_B (reg
->index
);
2011 insn
->insn_code
|= SET_IW_I_B (reg
->index
);
2013 case iw_custom_type
:
2014 insn
->insn_code
|= SET_IW_CUSTOM_B (reg
->index
);
2015 if (reg
->regtype
& REG_COPROCESSOR
)
2016 insn
->insn_code
|= SET_IW_CUSTOM_READB (0);
2018 insn
->insn_code
|= SET_IW_CUSTOM_READB (1);
2021 insn
->insn_code
|= SET_IW_F2I16_B (reg
->index
);
2023 case iw_F2X4I12_type
:
2024 insn
->insn_code
|= SET_IW_F2X4I12_B (reg
->index
);
2026 case iw_F3X6L5_type
:
2028 insn
->insn_code
|= SET_IW_F3X6L5_B (reg
->index
);
2030 case iw_F2X6L10_type
:
2031 insn
->insn_code
|= SET_IW_F2X6L10_B (reg
->index
);
2034 insn
->insn_code
|= SET_IW_F3X8_B (reg
->index
);
2035 if (reg
->regtype
& REG_COPROCESSOR
)
2036 insn
->insn_code
|= SET_IW_F3X8_READB (0);
2038 insn
->insn_code
|= SET_IW_F3X8_READB (1);
2041 insn
->insn_code
|= SET_IW_F1I5_B (reg
->index
);
2044 insn
->insn_code
|= SET_IW_F2_B (reg
->index
);
2046 case iw_T1X1I6_type
:
2047 /* Implicit zero register reference. */
2048 if (reg
->index
!= 0)
2049 as_bad (_("invalid register %s"), token
);
2057 /* Destination register w/3-bit encoding. */
2059 nios2_assemble_arg_D (const char *token
, nios2_insn_infoS
*insn
)
2061 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2062 int reg
= nios2_assemble_reg3 (token
);
2067 insn
->insn_code
|= SET_IW_T1I7_A3 (reg
);
2069 case iw_T2X1L3_type
:
2070 insn
->insn_code
|= SET_IW_T2X1L3_B3 (reg
);
2072 case iw_T2X1I3_type
:
2073 insn
->insn_code
|= SET_IW_T2X1I3_B3 (reg
);
2076 insn
->insn_code
|= SET_IW_T3X1_C3 (reg
);
2079 /* Some instructions using this encoding take 3 register arguments,
2080 requiring the destination register to be the same as the first
2082 if (op
->num_args
== 3)
2083 insn
->insn_code
|= SET_IW_T2X3_A3 (reg
);
2085 insn
->insn_code
|= SET_IW_T2X3_B3 (reg
);
2092 /* Source register w/3-bit encoding. */
2094 nios2_assemble_arg_S (const char *token
, nios2_insn_infoS
*insn
)
2096 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2097 int reg
= nios2_assemble_reg3 (token
);
2102 insn
->insn_code
|= SET_IW_T1I7_A3 (reg
);
2105 insn
->insn_code
|= SET_IW_T2I4_A3 (reg
);
2107 case iw_T2X1L3_type
:
2108 insn
->insn_code
|= SET_IW_T2X1L3_A3 (reg
);
2110 case iw_T2X1I3_type
:
2111 insn
->insn_code
|= SET_IW_T2X1I3_A3 (reg
);
2114 insn
->insn_code
|= SET_IW_T3X1_A3 (reg
);
2117 /* Some instructions using this encoding take 3 register arguments,
2118 requiring the destination register to be the same as the first
2120 if (op
->num_args
== 3)
2122 int dreg
= GET_IW_T2X3_A3 (insn
->insn_code
);
2124 as_bad ("source and destination registers must be the same");
2127 insn
->insn_code
|= SET_IW_T2X3_A3 (reg
);
2129 case iw_T1X1I6_type
:
2130 insn
->insn_code
|= SET_IW_T1X1I6_A3 (reg
);
2137 /* Source register 2 w/3-bit encoding. */
2139 nios2_assemble_arg_T (const char *token
, nios2_insn_infoS
*insn
)
2141 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2142 int reg
= nios2_assemble_reg3 (token
);
2147 insn
->insn_code
|= SET_IW_T2I4_B3 (reg
);
2150 insn
->insn_code
|= SET_IW_T3X1_B3 (reg
);
2153 insn
->insn_code
|= SET_IW_T2X3_B3 (reg
);
2160 /* 16-bit signed immediate. */
2162 nios2_assemble_arg_i (const char *token
, nios2_insn_infoS
*insn
)
2164 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2170 val
= nios2_assemble_expression (token
, insn
,
2171 BFD_RELOC_NIOS2_S16
, 0);
2172 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
2175 val
= nios2_assemble_expression (token
, insn
,
2176 BFD_RELOC_NIOS2_S16
, 0);
2177 insn
->constant_bits
|= SET_IW_F2I16_IMM16 (val
);
2184 /* 12-bit signed immediate. */
2186 nios2_assemble_arg_I (const char *token
, nios2_insn_infoS
*insn
)
2188 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2193 case iw_F2X4I12_type
:
2194 val
= nios2_assemble_expression (token
, insn
,
2195 BFD_RELOC_NIOS2_R2_S12
, 0);
2196 insn
->constant_bits
|= SET_IW_F2X4I12_IMM12 (val
);
2198 case iw_F1X4I12_type
:
2199 val
= nios2_assemble_expression (token
, insn
,
2200 BFD_RELOC_NIOS2_R2_S12
, 0);
2201 insn
->constant_bits
|= SET_IW_F2X4I12_IMM12 (val
);
2208 /* 16-bit unsigned immediate. */
2210 nios2_assemble_arg_u (const char *token
, nios2_insn_infoS
*insn
)
2212 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2218 val
= nios2_assemble_expression (token
, insn
,
2219 BFD_RELOC_NIOS2_U16
, 0);
2220 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
2223 val
= nios2_assemble_expression (token
, insn
,
2224 BFD_RELOC_NIOS2_U16
, 0);
2225 insn
->constant_bits
|= SET_IW_F2I16_IMM16 (val
);
2232 /* 7-bit unsigned immediate with 2-bit shift. */
2234 nios2_assemble_arg_U (const char *token
, nios2_insn_infoS
*insn
)
2236 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2242 val
= nios2_assemble_expression (token
, insn
,
2243 BFD_RELOC_NIOS2_R2_T1I7_2
, 0);
2244 insn
->constant_bits
|= SET_IW_T1I7_IMM7 (val
>> 2);
2247 val
= nios2_assemble_expression (token
, insn
,
2248 BFD_RELOC_NIOS2_R2_X1I7_2
, 0);
2249 insn
->constant_bits
|= SET_IW_X1I7_IMM7 (val
>> 2);
2256 /* 5-bit unsigned immediate with 2-bit shift. */
2258 nios2_assemble_arg_V (const char *token
, nios2_insn_infoS
*insn
)
2260 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2266 val
= nios2_assemble_expression (token
, insn
,
2267 BFD_RELOC_NIOS2_R2_F1I5_2
, 0);
2268 insn
->constant_bits
|= SET_IW_F1I5_IMM5 (val
>> 2);
2275 /* 4-bit unsigned immediate with 2-bit shift. */
2277 nios2_assemble_arg_W (const char *token
, nios2_insn_infoS
*insn
)
2279 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2285 val
= nios2_assemble_expression (token
, insn
,
2286 BFD_RELOC_NIOS2_R2_T2I4_2
, 0);
2287 insn
->constant_bits
|= SET_IW_T2I4_IMM4 (val
>> 2);
2289 case iw_L5I4X1_type
:
2290 /* This argument is optional for push.n/pop.n, and defaults to
2291 zero if unspecified. */
2295 val
= nios2_assemble_expression (token
, insn
,
2296 BFD_RELOC_NIOS2_R2_L5I4X1
, 0);
2297 insn
->constant_bits
|= SET_IW_L5I4X1_IMM4 (val
>> 2);
2304 /* 4-bit unsigned immediate with 1-bit shift. */
2306 nios2_assemble_arg_X (const char *token
, nios2_insn_infoS
*insn
)
2308 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2314 val
= nios2_assemble_expression (token
, insn
,
2315 BFD_RELOC_NIOS2_R2_T2I4_1
, 0);
2316 insn
->constant_bits
|= SET_IW_T2I4_IMM4 (val
>> 1);
2323 /* 4-bit unsigned immediate without shift. */
2325 nios2_assemble_arg_Y (const char *token
, nios2_insn_infoS
*insn
)
2327 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2333 val
= nios2_assemble_expression (token
, insn
,
2334 BFD_RELOC_NIOS2_R2_T2I4
, 0);
2335 insn
->constant_bits
|= SET_IW_T2I4_IMM4 (val
);
2343 /* 16-bit signed immediate address offset. */
2345 nios2_assemble_arg_o (const char *token
, nios2_insn_infoS
*insn
)
2347 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2353 val
= nios2_assemble_expression (token
, insn
,
2354 BFD_RELOC_16_PCREL
, 1);
2355 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
2358 val
= nios2_assemble_expression (token
, insn
,
2359 BFD_RELOC_16_PCREL
, 1);
2360 insn
->constant_bits
|= SET_IW_F2I16_IMM16 (val
);
2367 /* 10-bit signed address offset with 1-bit shift. */
2369 nios2_assemble_arg_O (const char *token
, nios2_insn_infoS
*insn
)
2371 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2377 val
= nios2_assemble_expression (token
, insn
,
2378 BFD_RELOC_NIOS2_R2_I10_1_PCREL
, 1);
2379 insn
->constant_bits
|= SET_IW_I10_IMM10 (val
>> 1);
2386 /* 7-bit signed address offset with 1-bit shift. */
2388 nios2_assemble_arg_P (const char *token
, nios2_insn_infoS
*insn
)
2390 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2396 val
= nios2_assemble_expression (token
, insn
,
2397 BFD_RELOC_NIOS2_R2_T1I7_1_PCREL
, 1);
2398 insn
->constant_bits
|= SET_IW_T1I7_IMM7 (val
>> 1);
2405 /* 5-bit unsigned immediate. */
2407 nios2_assemble_arg_j (const char *token
, nios2_insn_infoS
*insn
)
2409 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2415 val
= nios2_assemble_expression (token
, insn
,
2416 BFD_RELOC_NIOS2_IMM5
, 0);
2417 insn
->constant_bits
|= SET_IW_R_IMM5 (val
);
2419 case iw_F3X6L5_type
:
2420 if (op
->match
== MATCH_R2_ENI
)
2421 /* Value must be constant 0 or 1. */
2423 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2424 if (val
!= 0 && val
!= 1)
2425 as_bad ("invalid eni argument %u", val
);
2426 insn
->insn_code
|= SET_IW_F3X6L5_IMM5 (val
);
2430 val
= nios2_assemble_expression (token
, insn
,
2431 BFD_RELOC_NIOS2_IMM5
, 0);
2432 insn
->constant_bits
|= SET_IW_F3X6L5_IMM5 (val
);
2435 case iw_F2X6L10_type
:
2436 /* Only constant expression without relocation permitted for
2438 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2440 as_bad ("invalid bit position %u", val
);
2441 insn
->insn_code
|= SET_IW_F2X6L10_MSB (val
);
2444 val
= nios2_assemble_expression (token
, insn
,
2445 BFD_RELOC_NIOS2_R2_X2L5
, 0);
2446 insn
->constant_bits
|= SET_IW_X2L5_IMM5 (val
);
2453 /* Second 5-bit unsigned immediate field. */
2455 nios2_assemble_arg_k (const char *token
, nios2_insn_infoS
*insn
)
2457 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2462 case iw_F2X6L10_type
:
2463 /* Only constant expression without relocation permitted for
2465 val
= nios2_assemble_expression (token
, insn
,
2468 as_bad ("invalid bit position %u", val
);
2469 else if (GET_IW_F2X6L10_MSB (insn
->insn_code
) < val
)
2470 as_bad ("MSB must be greater than or equal to LSB");
2471 insn
->insn_code
|= SET_IW_F2X6L10_LSB (val
);
2478 /* 8-bit unsigned immediate. */
2480 nios2_assemble_arg_l (const char *token
, nios2_insn_infoS
*insn
)
2482 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2487 case iw_custom_type
:
2488 val
= nios2_assemble_expression (token
, insn
,
2489 BFD_RELOC_NIOS2_IMM8
, 0);
2490 insn
->constant_bits
|= SET_IW_CUSTOM_N (val
);
2493 val
= nios2_assemble_expression (token
, insn
,
2494 BFD_RELOC_NIOS2_IMM8
, 0);
2495 insn
->constant_bits
|= SET_IW_F3X8_N (val
);
2502 /* 26-bit unsigned immediate. */
2504 nios2_assemble_arg_m (const char *token
, nios2_insn_infoS
*insn
)
2506 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2512 val
= nios2_assemble_expression (token
, insn
,
2513 (nios2_as_options
.noat
2514 ? BFD_RELOC_NIOS2_CALL26_NOAT
2515 : BFD_RELOC_NIOS2_CALL26
),
2517 insn
->constant_bits
|= SET_IW_J_IMM26 (val
);
2520 val
= nios2_assemble_expression (token
, insn
,
2521 (nios2_as_options
.noat
2522 ? BFD_RELOC_NIOS2_CALL26_NOAT
2523 : BFD_RELOC_NIOS2_CALL26
),
2525 insn
->constant_bits
|= SET_IW_L26_IMM26 (val
);
2532 /* 6-bit unsigned immediate with no shifting. */
2534 nios2_assemble_arg_M (const char *token
, nios2_insn_infoS
*insn
)
2536 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2541 case iw_T1X1I6_type
:
2542 val
= nios2_assemble_expression (token
, insn
,
2543 BFD_RELOC_NIOS2_R2_T1X1I6
, 0);
2544 insn
->constant_bits
|= SET_IW_T1X1I6_IMM6 (val
);
2551 /* 6-bit unsigned immediate with 2-bit shift. */
2553 nios2_assemble_arg_N (const char *token
, nios2_insn_infoS
*insn
)
2555 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2560 case iw_T1X1I6_type
:
2561 val
= nios2_assemble_expression (token
, insn
,
2562 BFD_RELOC_NIOS2_R2_T1X1I6_2
, 0);
2563 insn
->constant_bits
|= SET_IW_T1X1I6_IMM6 (val
>> 2);
2571 /* Encoded enumeration for addi.n/subi.n. */
2573 nios2_assemble_arg_e (const char *token
, nios2_insn_infoS
*insn
)
2575 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2581 case iw_T2X1I3_type
:
2582 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2583 for (i
= 0; i
< nios2_num_r2_asi_n_mappings
; i
++)
2584 if (val
== nios2_r2_asi_n_mappings
[i
])
2586 if (i
== nios2_num_r2_asi_n_mappings
)
2588 as_bad (_("Invalid constant operand %s"), token
);
2591 insn
->insn_code
|= SET_IW_T2X1I3_IMM3 ((unsigned)i
);
2598 /* Encoded enumeration for slli.n/srli.n. */
2600 nios2_assemble_arg_f (const char *token
, nios2_insn_infoS
*insn
)
2602 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2608 case iw_T2X1L3_type
:
2609 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2610 for (i
= 0; i
< nios2_num_r2_shi_n_mappings
; i
++)
2611 if (val
== nios2_r2_shi_n_mappings
[i
])
2613 if (i
== nios2_num_r2_shi_n_mappings
)
2615 as_bad (_("Invalid constant operand %s"), token
);
2618 insn
->insn_code
|= SET_IW_T2X1L3_SHAMT ((unsigned)i
);
2625 /* Encoded enumeration for andi.n. */
2627 nios2_assemble_arg_g (const char *token
, nios2_insn_infoS
*insn
)
2629 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2636 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2637 for (i
= 0; i
< nios2_num_r2_andi_n_mappings
; i
++)
2638 if (val
== nios2_r2_andi_n_mappings
[i
])
2640 if (i
== nios2_num_r2_andi_n_mappings
)
2642 as_bad (_("Invalid constant operand %s"), token
);
2645 insn
->insn_code
|= SET_IW_T2I4_IMM4 ((unsigned)i
);
2652 /* Encoded enumeration for movi.n. */
2654 nios2_assemble_arg_h (const char *token
, nios2_insn_infoS
*insn
)
2656 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2663 val
= nios2_assemble_expression (token
, insn
, BFD_RELOC_NONE
, 0);
2665 if ((signed) i
== -1)
2671 else if (i
< 0 || i
> 125)
2673 as_bad (_("Invalid constant operand %s"), token
);
2676 insn
->insn_code
|= SET_IW_T1I7_IMM7 (val
);
2683 /* Encoded REGMASK for ldwm/stwm or push.n/pop.n. */
2685 nios2_assemble_arg_R (const char *token
, nios2_insn_infoS
*insn
)
2687 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2689 char *buf
= strdup (token
);
2690 unsigned long reglist
= nios2_parse_reglist (buf
, op
);
2698 case iw_F1X4L17_type
:
2699 /* Encoding for ldwm/stwm. */
2700 if (reglist
& 0x00003ffc)
2701 mask
= reglist
>> 2;
2704 insn
->insn_code
|= SET_IW_F1X4L17_RS (1);
2705 mask
= (reglist
& 0x00ffc000) >> 14;
2706 if (reglist
& (1 << 28))
2708 if (reglist
& (1u << 31))
2711 insn
->insn_code
|= SET_IW_F1X4L17_REGMASK (mask
);
2714 case iw_L5I4X1_type
:
2715 /* Encoding for push.n/pop.n. */
2716 if (reglist
& (1 << 28))
2717 insn
->insn_code
|= SET_IW_L5I4X1_FP (1);
2718 mask
= reglist
& 0x00ff0000;
2723 for (i
= 0; i
< nios2_num_r2_reg_range_mappings
; i
++)
2724 if (nios2_r2_reg_range_mappings
[i
] == mask
)
2726 if (i
== nios2_num_r2_reg_range_mappings
)
2728 as_bad ("invalid reglist");
2731 insn
->insn_code
|= SET_IW_L5I4X1_REGRANGE (i
);
2732 insn
->insn_code
|= SET_IW_L5I4X1_CS (1);
2741 /* Base register for ldwm/stwm. */
2743 nios2_assemble_arg_B (const char *token
, nios2_insn_infoS
*insn
)
2745 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2746 int direction
, writeback
, ret
;
2747 char *str
= strdup (token
);
2748 struct nios2_reg
*reg
2749 = nios2_parse_base_register (str
, &direction
, &writeback
, &ret
);
2757 case iw_F1X4L17_type
:
2758 /* For ldwm, check to see if the base register is already inside the
2760 if (op
->match
== MATCH_R2_LDWM
2761 && (nios2_reglist_mask
& (1 << reg
->index
)))
2763 as_bad ("invalid base register; %s is inside the reglist", reg
->name
);
2767 /* For stwm, ret option is not allowed. */
2768 if (op
->match
== MATCH_R2_STWM
&& ret
)
2770 as_bad ("invalid option syntax");
2774 /* Check that the direction matches the ordering of the reglist. */
2775 if (nios2_reglist_dir
&& direction
!= nios2_reglist_dir
)
2777 as_bad ("reglist order does not match increment/decrement mode");
2781 insn
->insn_code
|= SET_IW_F1X4L17_A (reg
->index
);
2783 insn
->insn_code
|= SET_IW_F1X4L17_ID (1);
2785 insn
->insn_code
|= SET_IW_F1X4L17_WB (1);
2787 insn
->insn_code
|= SET_IW_F1X4L17_PC (1);
2796 nios2_assemble_args (nios2_insn_infoS
*insn
)
2798 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
2800 unsigned int tokidx
, ntok
;
2802 /* Make sure there are enough arguments. */
2803 ntok
= (op
->pinfo
& NIOS2_INSN_OPTARG
) ? op
->num_args
- 1 : op
->num_args
;
2804 for (tokidx
= 1; tokidx
<= ntok
; tokidx
++)
2805 if (insn
->insn_tokens
[tokidx
] == NULL
)
2807 as_bad ("missing argument");
2811 for (argptr
= op
->args
, tokidx
= 1;
2812 *argptr
&& insn
->insn_tokens
[tokidx
];
2822 nios2_assemble_arg_c (insn
->insn_tokens
[tokidx
++], insn
);
2826 nios2_assemble_arg_d (insn
->insn_tokens
[tokidx
++], insn
);
2830 nios2_assemble_arg_s (insn
->insn_tokens
[tokidx
++], insn
);
2834 nios2_assemble_arg_t (insn
->insn_tokens
[tokidx
++], insn
);
2838 nios2_assemble_arg_D (insn
->insn_tokens
[tokidx
++], insn
);
2842 nios2_assemble_arg_S (insn
->insn_tokens
[tokidx
++], insn
);
2846 nios2_assemble_arg_T (insn
->insn_tokens
[tokidx
++], insn
);
2850 nios2_assemble_arg_i (insn
->insn_tokens
[tokidx
++], insn
);
2854 nios2_assemble_arg_I (insn
->insn_tokens
[tokidx
++], insn
);
2858 nios2_assemble_arg_u (insn
->insn_tokens
[tokidx
++], insn
);
2862 nios2_assemble_arg_U (insn
->insn_tokens
[tokidx
++], insn
);
2866 nios2_assemble_arg_V (insn
->insn_tokens
[tokidx
++], insn
);
2870 nios2_assemble_arg_W (insn
->insn_tokens
[tokidx
++], insn
);
2874 nios2_assemble_arg_X (insn
->insn_tokens
[tokidx
++], insn
);
2878 nios2_assemble_arg_Y (insn
->insn_tokens
[tokidx
++], insn
);
2882 nios2_assemble_arg_o (insn
->insn_tokens
[tokidx
++], insn
);
2886 nios2_assemble_arg_O (insn
->insn_tokens
[tokidx
++], insn
);
2890 nios2_assemble_arg_P (insn
->insn_tokens
[tokidx
++], insn
);
2894 nios2_assemble_arg_j (insn
->insn_tokens
[tokidx
++], insn
);
2898 nios2_assemble_arg_k (insn
->insn_tokens
[tokidx
++], insn
);
2902 nios2_assemble_arg_l (insn
->insn_tokens
[tokidx
++], insn
);
2906 nios2_assemble_arg_m (insn
->insn_tokens
[tokidx
++], insn
);
2910 nios2_assemble_arg_M (insn
->insn_tokens
[tokidx
++], insn
);
2914 nios2_assemble_arg_N (insn
->insn_tokens
[tokidx
++], insn
);
2918 nios2_assemble_arg_e (insn
->insn_tokens
[tokidx
++], insn
);
2922 nios2_assemble_arg_f (insn
->insn_tokens
[tokidx
++], insn
);
2926 nios2_assemble_arg_g (insn
->insn_tokens
[tokidx
++], insn
);
2930 nios2_assemble_arg_h (insn
->insn_tokens
[tokidx
++], insn
);
2934 nios2_assemble_arg_R (insn
->insn_tokens
[tokidx
++], insn
);
2938 nios2_assemble_arg_B (insn
->insn_tokens
[tokidx
++], insn
);
2946 /* Perform argument checking. */
2947 nios2_check_assembly (insn
->insn_code
| insn
->constant_bits
,
2948 insn
->insn_tokens
[tokidx
]);
2952 /* The function consume_arg takes a pointer into a string
2953 of instruction tokens (args) and a pointer into a string
2954 representing the expected sequence of tokens and separators.
2955 It checks whether the first argument in argstr is of the
2956 expected type, throwing an error if it is not, and returns
2957 the pointer argstr. */
2959 nios2_consume_arg (char *argstr
, const char *parsestr
)
2978 if (nios2_special_relocation_p (argstr
))
2980 /* We zap the parentheses because we don't want them confused
2982 temp
= strchr (argstr
, '(');
2985 temp
= strchr (argstr
, ')');
2990 as_bad (_("badly formed expression near %s"), argstr
);
3012 /* We can't have %hi, %lo or %hiadj here. */
3014 as_bad (_("badly formed expression near %s"), argstr
);
3018 /* Register list for ldwm/stwm or push.n/pop.n. Replace the commas
3019 in the list with spaces so we don't confuse them with separators. */
3022 as_bad ("missing '{' in register list");
3025 for (temp
= argstr
+ 1; *temp
; temp
++)
3029 else if (*temp
== ',')
3034 as_bad ("missing '}' in register list");
3040 /* Base register and options for ldwm/stwm. This is the final argument
3041 and consumes the rest of the argument string; replace commas
3042 with spaces so that the token splitter doesn't think they are
3043 separate arguments. */
3044 for (temp
= argstr
; *temp
; temp
++)
3053 BAD_CASE (*parsestr
);
3060 /* The function consume_separator takes a pointer into a string
3061 of instruction tokens (args) and a pointer into a string representing
3062 the expected sequence of tokens and separators. It finds the first
3063 instance of the character pointed to by separator in argstr, and
3064 returns a pointer to the next element of argstr, which is the
3065 following token in the sequence. */
3067 nios2_consume_separator (char *argstr
, const char *separator
)
3071 /* If we have a opcode reg, expr(reg) type instruction, and
3072 * we are separating the expr from the (reg), we find the last
3073 * (, just in case the expression has parentheses. */
3075 if (*separator
== '(')
3076 p
= strrchr (argstr
, *separator
);
3078 p
= strchr (argstr
, *separator
);
3085 /* The principal argument parsing function which takes a string argstr
3086 representing the instruction arguments for insn, and extracts the argument
3087 tokens matching parsestr into parsed_args. */
3089 nios2_parse_args (nios2_insn_infoS
*insn
, char *argstr
,
3090 const char *parsestr
, char **parsed_args
)
3097 bool terminate
= false;
3099 /* This rest of this function is it too fragile and it mostly works,
3100 therefore special case this one. */
3101 if (*parsestr
== 0 && argstr
!= 0)
3103 as_bad (_("too many arguments"));
3104 parsed_args
[0] = NULL
;
3108 while (p
!= NULL
&& !terminate
&& i
< NIOS2_MAX_INSN_TOKENS
)
3110 parsed_args
[i
] = nios2_consume_arg (p
, parsestr
);
3112 while (*parsestr
== '(' || *parsestr
== ')' || *parsestr
== ',')
3115 p
= nios2_consume_separator (p
, parsestr
);
3116 /* Check for missing separators. */
3117 if (!p
&& !(insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_OPTARG
))
3119 as_bad (_("expecting %c near %s"), *parsestr
, context
);
3125 if (*parsestr
== '\0')
3127 /* Check that the argument string has no trailing arguments. */
3128 end
= strpbrk (p
, ",");
3130 as_bad (_("too many arguments"));
3133 if (*parsestr
== '\0' || (p
!= NULL
&& *p
== '\0'))
3138 parsed_args
[i
] = NULL
;
3143 /** Support for pseudo-op parsing. These are macro-like opcodes that
3144 expand into real insns by suitable fiddling with the operands. */
3146 /* Append the string modifier to the string contained in the argument at
3147 parsed_args[ndx]. */
3149 nios2_modify_arg (char **parsed_args
, const char *modifier
,
3150 int unused ATTRIBUTE_UNUSED
, int ndx
)
3152 char *tmp
= parsed_args
[ndx
];
3154 parsed_args
[ndx
] = concat (tmp
, modifier
, (char *) NULL
);
3157 /* Modify parsed_args[ndx] by negating that argument. */
3159 nios2_negate_arg (char **parsed_args
, const char *modifier ATTRIBUTE_UNUSED
,
3160 int unused ATTRIBUTE_UNUSED
, int ndx
)
3162 char *tmp
= parsed_args
[ndx
];
3164 parsed_args
[ndx
] = concat ("~(", tmp
, ")+1", (char *) NULL
);
3167 /* The function nios2_swap_args swaps the pointers at indices index_1 and
3168 index_2 in the array parsed_args[] - this is used for operand swapping
3169 for comparison operations. */
3171 nios2_swap_args (char **parsed_args
, const char *unused ATTRIBUTE_UNUSED
,
3172 int index_1
, int index_2
)
3175 gas_assert (index_1
< NIOS2_MAX_INSN_TOKENS
3176 && index_2
< NIOS2_MAX_INSN_TOKENS
);
3177 tmp
= parsed_args
[index_1
];
3178 parsed_args
[index_1
] = parsed_args
[index_2
];
3179 parsed_args
[index_2
] = tmp
;
3182 /* This function appends the string appnd to the array of strings in
3183 parsed_args num times starting at index start in the array. */
3185 nios2_append_arg (char **parsed_args
, const char *appnd
, int num
,
3191 gas_assert ((start
+ num
) < NIOS2_MAX_INSN_TOKENS
);
3193 if (nios2_mode
== NIOS2_MODE_TEST
)
3194 tmp
= parsed_args
[start
];
3198 for (i
= start
, count
= num
; count
> 0; ++i
, --count
)
3199 parsed_args
[i
] = (char *) appnd
;
3201 gas_assert (i
== (start
+ num
));
3202 parsed_args
[i
] = tmp
;
3203 parsed_args
[i
+ 1] = NULL
;
3206 /* This function inserts the string insert num times in the array
3207 parsed_args, starting at the index start. */
3209 nios2_insert_arg (char **parsed_args
, const char *insert
, int num
,
3214 gas_assert ((start
+ num
) < NIOS2_MAX_INSN_TOKENS
);
3216 /* Move the existing arguments up to create space. */
3217 for (i
= NIOS2_MAX_INSN_TOKENS
; i
- num
>= start
; --i
)
3218 parsed_args
[i
] = parsed_args
[i
- num
];
3220 for (i
= start
, count
= num
; count
> 0; ++i
, --count
)
3221 parsed_args
[i
] = (char *) insert
;
3224 /* Cleanup function to free malloc'ed arg strings. */
3226 nios2_free_arg (char **parsed_args
, int num ATTRIBUTE_UNUSED
, int start
)
3228 free (parsed_args
[start
]);
3229 parsed_args
[start
] = NULL
;
3232 /* This function swaps the pseudo-op for a real op. */
3233 static nios2_ps_insn_infoS
*
3234 nios2_translate_pseudo_insn (nios2_insn_infoS
*insn
)
3237 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
3238 nios2_ps_insn_infoS
*ps_insn
;
3239 unsigned int tokidx
, ntok
;
3241 /* Find which real insn the pseudo-op translates to and
3242 switch the insn_info ptr to point to it. */
3243 ps_insn
= nios2_ps_lookup (op
->name
);
3245 if (ps_insn
!= NULL
)
3247 insn
->insn_nios2_opcode
= nios2_opcode_lookup (ps_insn
->insn
);
3248 insn
->insn_tokens
[0] = insn
->insn_nios2_opcode
->name
;
3250 /* Make sure there are enough arguments. */
3251 ntok
= ((op
->pinfo
& NIOS2_INSN_OPTARG
)
3252 ? op
->num_args
- 1 : op
->num_args
);
3253 for (tokidx
= 1; tokidx
<= ntok
; tokidx
++)
3254 if (insn
->insn_tokens
[tokidx
] == NULL
)
3256 as_bad ("missing argument");
3260 /* Modify the args so they work with the real insn. */
3261 ps_insn
->arg_modifer_func ((char **) insn
->insn_tokens
,
3262 ps_insn
->arg_modifier
, ps_insn
->num
,
3266 /* we cannot recover from this. */
3267 as_fatal (_("unrecognized pseudo-instruction %s"),
3268 insn
->insn_nios2_opcode
->name
);
3272 /* Invoke the cleanup handler for pseudo-insn ps_insn on insn. */
3274 nios2_cleanup_pseudo_insn (nios2_insn_infoS
*insn
,
3275 nios2_ps_insn_infoS
*ps_insn
)
3277 if (ps_insn
->arg_cleanup_func
)
3278 (ps_insn
->arg_cleanup_func
) ((char **) insn
->insn_tokens
,
3279 ps_insn
->num
, ps_insn
->index
);
3282 const nios2_ps_insn_infoS nios2_ps_insn_info_structs
[] = {
3283 /* pseudo-op, real-op, arg, arg_modifier_func, num, index, arg_cleanup_func */
3284 {"mov", "add", nios2_append_arg
, "zero", 1, 3, NULL
},
3285 {"movi", "addi", nios2_insert_arg
, "zero", 1, 2, NULL
},
3286 {"movhi", "orhi", nios2_insert_arg
, "zero", 1, 2, NULL
},
3287 {"movui", "ori", nios2_insert_arg
, "zero", 1, 2, NULL
},
3288 {"movia", "orhi", nios2_insert_arg
, "zero", 1, 2, NULL
},
3289 {"nop", "add", nios2_append_arg
, "zero", 3, 1, NULL
},
3290 {"bgt", "blt", nios2_swap_args
, "", 1, 2, NULL
},
3291 {"bgtu", "bltu", nios2_swap_args
, "", 1, 2, NULL
},
3292 {"ble", "bge", nios2_swap_args
, "", 1, 2, NULL
},
3293 {"bleu", "bgeu", nios2_swap_args
, "", 1, 2, NULL
},
3294 {"cmpgt", "cmplt", nios2_swap_args
, "", 2, 3, NULL
},
3295 {"cmpgtu", "cmpltu", nios2_swap_args
, "", 2, 3, NULL
},
3296 {"cmple", "cmpge", nios2_swap_args
, "", 2, 3, NULL
},
3297 {"cmpleu", "cmpgeu", nios2_swap_args
, "", 2, 3, NULL
},
3298 {"cmpgti", "cmpgei", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
3299 {"cmpgtui", "cmpgeui", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
3300 {"cmplei", "cmplti", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
3301 {"cmpleui", "cmpltui", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
3302 {"subi", "addi", nios2_negate_arg
, "", 0, 3, nios2_free_arg
},
3303 {"nop.n", "mov.n", nios2_append_arg
, "zero", 2, 1, NULL
}
3304 /* Add further pseudo-ops here. */
3307 #define NIOS2_NUM_PSEUDO_INSNS \
3308 ((sizeof(nios2_ps_insn_info_structs)/ \
3309 sizeof(nios2_ps_insn_info_structs[0])))
3310 const int nios2_num_ps_insn_info_structs
= NIOS2_NUM_PSEUDO_INSNS
;
3313 /** Assembler output support. */
3315 /* Output a normal instruction. */
3317 output_insn (nios2_insn_infoS
*insn
)
3320 nios2_insn_relocS
*reloc
;
3321 f
= frag_more (insn
->insn_nios2_opcode
->size
);
3322 /* This allocates enough space for the instruction
3323 and puts it in the current frag. */
3324 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
3325 /* Emit debug info. */
3326 dwarf2_emit_insn (insn
->insn_nios2_opcode
->size
);
3327 /* Create any fixups to be acted on later. */
3329 for (reloc
= insn
->insn_reloc
; reloc
!= NULL
; reloc
= reloc
->reloc_next
)
3330 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
,
3331 insn
->insn_nios2_opcode
->size
,
3332 &reloc
->reloc_expression
, reloc
->reloc_pcrel
,
3336 /* Output an unconditional branch. */
3338 output_ubranch (nios2_insn_infoS
*insn
)
3340 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
3342 /* If the reloc is NULL, there was an error assembling the branch. */
3345 symbolS
*symp
= reloc
->reloc_expression
.X_add_symbol
;
3346 offsetT offset
= reloc
->reloc_expression
.X_add_number
;
3348 bool is_cdx
= (insn
->insn_nios2_opcode
->size
== 2);
3350 /* Tag dwarf2 debug info to the address at the start of the insn.
3351 We must do it before frag_var() below closes off the frag. */
3352 dwarf2_emit_insn (0);
3354 /* We create a machine dependent frag which can grow
3355 to accommodate the largest possible instruction sequence
3356 this may generate. */
3357 f
= frag_var (rs_machine_dependent
,
3358 UBRANCH_MAX_SIZE
, insn
->insn_nios2_opcode
->size
,
3359 (is_cdx
? CDX_UBRANCH_SUBTYPE (0) : UBRANCH_SUBTYPE (0)),
3360 symp
, offset
, NULL
);
3362 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
3364 /* We leave fixup generation to md_convert_frag. */
3368 /* Output a conditional branch. */
3370 output_cbranch (nios2_insn_infoS
*insn
)
3372 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
3374 /* If the reloc is NULL, there was an error assembling the branch. */
3377 symbolS
*symp
= reloc
->reloc_expression
.X_add_symbol
;
3378 offsetT offset
= reloc
->reloc_expression
.X_add_number
;
3380 bool is_cdx
= (insn
->insn_nios2_opcode
->size
== 2);
3382 /* Tag dwarf2 debug info to the address at the start of the insn.
3383 We must do it before frag_var() below closes off the frag. */
3384 dwarf2_emit_insn (0);
3386 /* We create a machine dependent frag which can grow
3387 to accommodate the largest possible instruction sequence
3388 this may generate. */
3389 f
= frag_var (rs_machine_dependent
,
3390 CBRANCH_MAX_SIZE
, insn
->insn_nios2_opcode
->size
,
3391 (is_cdx
? CDX_CBRANCH_SUBTYPE (0) : CBRANCH_SUBTYPE (0)),
3392 symp
, offset
, NULL
);
3394 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
3396 /* We leave fixup generation to md_convert_frag. */
3400 /* Output a call sequence. Since calls are not pc-relative for NIOS2,
3401 but are page-relative, we cannot tell at any stage in assembly
3402 whether a call will be out of range since a section may be linked
3403 at any address. So if we are relaxing, we convert all call instructions
3404 to long call sequences, and rely on the linker to relax them back to
3407 output_call (nios2_insn_infoS
*insn
)
3409 /* This allocates enough space for the instruction
3410 and puts it in the current frag. */
3411 char *f
= frag_more (12);
3412 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
3413 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
3418 md_number_to_chars (f
,
3419 (MATCH_R1_ORHI
| SET_IW_I_B (AT_REGNUM
)
3422 dwarf2_emit_insn (4);
3423 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
, 4,
3424 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_HI16
);
3425 md_number_to_chars (f
+ 4,
3426 (MATCH_R1_ORI
| SET_IW_I_B (AT_REGNUM
)
3427 | SET_IW_I_A (AT_REGNUM
)),
3429 dwarf2_emit_insn (4);
3430 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ 4, 4,
3431 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_LO16
);
3432 md_number_to_chars (f
+ 8, MATCH_R1_CALLR
| SET_IW_R_A (AT_REGNUM
), 4);
3433 dwarf2_emit_insn (4);
3436 md_number_to_chars (f
,
3437 (MATCH_R2_ORHI
| SET_IW_F2I16_B (AT_REGNUM
)
3438 | SET_IW_F2I16_A (0)),
3440 dwarf2_emit_insn (4);
3441 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
, 4,
3442 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_HI16
);
3443 md_number_to_chars (f
+ 4,
3444 (MATCH_R2_ORI
| SET_IW_F2I16_B (AT_REGNUM
)
3445 | SET_IW_F2I16_A (AT_REGNUM
)),
3447 dwarf2_emit_insn (4);
3448 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ 4, 4,
3449 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_LO16
);
3450 md_number_to_chars (f
+ 8, MATCH_R2_CALLR
| SET_IW_F3X6L5_A (AT_REGNUM
),
3452 dwarf2_emit_insn (4);
3459 /* Output a movhi/addi pair for the movia pseudo-op. */
3461 output_movia (nios2_insn_infoS
*insn
)
3463 /* This allocates enough space for the instruction
3464 and puts it in the current frag. */
3465 char *f
= frag_more (8);
3466 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
3467 unsigned long reg
, code
= 0;
3468 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
3470 /* If the reloc is NULL, there was an error assembling the movia. */
3476 reg
= GET_IW_I_B (insn
->insn_code
);
3477 code
= MATCH_R1_ADDI
| SET_IW_I_A (reg
) | SET_IW_I_B (reg
);
3480 reg
= GET_IW_F2I16_B (insn
->insn_code
);
3481 code
= MATCH_R2_ADDI
| SET_IW_F2I16_A (reg
) | SET_IW_F2I16_B (reg
);
3487 md_number_to_chars (f
, insn
->insn_code
, 4);
3488 dwarf2_emit_insn (4);
3489 fix_new (frag_now
, f
- frag_now
->fr_literal
, 4,
3490 reloc
->reloc_expression
.X_add_symbol
,
3491 reloc
->reloc_expression
.X_add_number
, 0,
3492 BFD_RELOC_NIOS2_HIADJ16
);
3493 md_number_to_chars (f
+ 4, code
, 4);
3494 dwarf2_emit_insn (4);
3495 fix_new (frag_now
, f
+ 4 - frag_now
->fr_literal
, 4,
3496 reloc
->reloc_expression
.X_add_symbol
,
3497 reloc
->reloc_expression
.X_add_number
, 0, BFD_RELOC_NIOS2_LO16
);
3503 /** External interfaces. */
3505 /* Update the selected architecture based on ARCH, giving an error if
3506 ARCH is an invalid value. */
3509 nios2_use_arch (const char *arch
)
3511 if (strcmp (arch
, "nios2") == 0 || strcmp (arch
, "r1") == 0)
3513 nios2_architecture
|= EF_NIOS2_ARCH_R1
;
3514 nios2_opcodes
= (struct nios2_opcode
*) nios2_r1_opcodes
;
3515 nios2_num_opcodes
= nios2_num_r1_opcodes
;
3520 else if (strcmp (arch
, "r2") == 0)
3522 nios2_architecture
|= EF_NIOS2_ARCH_R2
;
3523 nios2_opcodes
= (struct nios2_opcode
*) nios2_r2_opcodes
;
3524 nios2_num_opcodes
= nios2_num_r2_opcodes
;
3530 as_bad (_("unknown architecture '%s'"), arch
);
3533 /* The following functions are called by machine-independent parts of
3536 md_parse_option (int c
, const char *arg ATTRIBUTE_UNUSED
)
3541 /* Hidden option for self-test mode. */
3542 nios2_mode
= NIOS2_MODE_TEST
;
3544 case OPTION_RELAX_ALL
:
3545 nios2_as_options
.relax
= relax_all
;
3547 case OPTION_NORELAX
:
3548 nios2_as_options
.relax
= relax_none
;
3550 case OPTION_RELAX_SECTION
:
3551 nios2_as_options
.relax
= relax_section
;
3554 target_big_endian
= 1;
3557 target_big_endian
= 0;
3560 nios2_use_arch (arg
);
3570 /* Implement TARGET_FORMAT. We can choose to be big-endian or
3571 little-endian at runtime based on a switch. */
3573 nios2_target_format (void)
3575 return target_big_endian
? "elf32-bignios2" : "elf32-littlenios2";
3578 /* Machine-dependent usage message. */
3580 md_show_usage (FILE *stream
)
3582 fprintf (stream
, " NIOS2 options:\n"
3583 " -relax-all replace all branch and call "
3584 "instructions with jmp and callr sequences\n"
3585 " -relax-section replace identified out of range "
3586 "branches with jmp sequences (default)\n"
3587 " -no-relax do not replace any branches or calls\n"
3588 " -EB force big-endian byte ordering\n"
3589 " -EL force little-endian byte ordering\n"
3590 " -march=ARCH enable instructions from architecture ARCH\n");
3594 /* This function is called once, at assembler startup time.
3595 It should set up all the tables, etc. that the MD part of the
3596 assembler will need. */
3602 switch (nios2_architecture
)
3605 case EF_NIOS2_ARCH_R1
:
3606 bfd_default_set_arch_mach (stdoutput
, bfd_arch_nios2
, bfd_mach_nios2r1
);
3608 case EF_NIOS2_ARCH_R2
:
3609 if (target_big_endian
)
3610 as_fatal (_("Big-endian R2 is not supported."));
3611 bfd_default_set_arch_mach (stdoutput
, bfd_arch_nios2
, bfd_mach_nios2r2
);
3615 /* Create and fill a hashtable for the Nios II opcodes, registers and
3617 nios2_opcode_hash
= str_htab_create ();
3618 nios2_reg_hash
= str_htab_create ();
3619 nios2_ps_hash
= str_htab_create ();
3621 for (i
= 0; i
< nios2_num_opcodes
; ++i
)
3622 if (str_hash_insert (nios2_opcode_hash
, nios2_opcodes
[i
].name
,
3623 &nios2_opcodes
[i
], 0) != NULL
)
3624 as_fatal (_("duplicate %s"), nios2_opcodes
[i
].name
);
3626 for (i
= 0; i
< nios2_num_regs
; ++i
)
3627 if (str_hash_insert (nios2_reg_hash
, nios2_regs
[i
].name
,
3628 &nios2_regs
[i
], 0) != NULL
)
3629 as_fatal (_("duplicate %s"), nios2_regs
[i
].name
);
3631 for (i
= 0; i
< nios2_num_ps_insn_info_structs
; ++i
)
3632 if (str_hash_insert (nios2_ps_hash
,
3633 nios2_ps_insn_info_structs
[i
].pseudo_insn
,
3634 &nios2_ps_insn_info_structs
[i
], 0) != NULL
)
3635 as_fatal (_("duplicate %s"), nios2_ps_insn_info_structs
[i
].pseudo_insn
);
3637 /* Assembler option defaults. */
3638 nios2_as_options
.noat
= false;
3639 nios2_as_options
.nobreak
= false;
3641 /* Initialize the alignment data. */
3642 nios2_current_align_seg
= now_seg
;
3643 nios2_last_label
= NULL
;
3644 nios2_current_align
= 0;
3645 nios2_min_align
= 2;
3649 /* Assembles a single line of Nios II assembly language. */
3651 md_assemble (char *op_str
)
3654 char *op_strdup
= NULL
;
3655 unsigned long saved_pinfo
= 0;
3656 nios2_insn_infoS thisinsn
;
3657 nios2_insn_infoS
*insn
= &thisinsn
;
3658 bool ps_error
= false;
3660 /* Make sure we are aligned on an appropriate boundary. */
3661 if (nios2_current_align
< nios2_min_align
)
3662 nios2_align (nios2_min_align
, NULL
, nios2_last_label
);
3663 else if (nios2_current_align
> nios2_min_align
)
3664 nios2_current_align
= nios2_min_align
;
3665 nios2_last_label
= NULL
;
3667 /* We don't want to clobber to op_str
3668 because we want to be able to use it in messages. */
3669 op_strdup
= strdup (op_str
);
3670 insn
->insn_tokens
[0] = strtok (op_strdup
, " ");
3671 argstr
= strtok (NULL
, "");
3673 /* Assemble the opcode. */
3674 insn
->insn_nios2_opcode
= nios2_opcode_lookup (insn
->insn_tokens
[0]);
3675 insn
->insn_reloc
= NULL
;
3677 if (insn
->insn_nios2_opcode
!= NULL
)
3679 nios2_ps_insn_infoS
*ps_insn
= NULL
;
3681 /* Note if we've seen a 16-bit instruction. */
3682 if (insn
->insn_nios2_opcode
->size
== 2)
3683 nios2_min_align
= 1;
3685 /* Set the opcode for the instruction. */
3686 insn
->insn_code
= insn
->insn_nios2_opcode
->match
;
3687 insn
->constant_bits
= 0;
3689 /* Parse the arguments pointed to by argstr. */
3690 if (nios2_mode
== NIOS2_MODE_ASSEMBLE
)
3691 nios2_parse_args (insn
, argstr
, insn
->insn_nios2_opcode
->args
,
3692 (char **) &insn
->insn_tokens
[1]);
3694 nios2_parse_args (insn
, argstr
, insn
->insn_nios2_opcode
->args_test
,
3695 (char **) &insn
->insn_tokens
[1]);
3697 /* We need to preserve the MOVIA macro as this is clobbered by
3698 translate_pseudo_insn. */
3699 if (insn
->insn_nios2_opcode
->pinfo
== NIOS2_INSN_MACRO_MOVIA
)
3700 saved_pinfo
= NIOS2_INSN_MACRO_MOVIA
;
3701 /* If the instruction is an pseudo-instruction, we want to replace it
3702 with its real equivalent, and then continue. */
3703 if ((insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_MACRO
)
3704 == NIOS2_INSN_MACRO
)
3706 ps_insn
= nios2_translate_pseudo_insn (insn
);
3711 /* If we found invalid pseudo-instruction syntax, the error's already
3712 been diagnosed in nios2_translate_pseudo_insn, so skip
3713 remaining processing. */
3716 /* Assemble the parsed arguments into the instruction word. */
3717 nios2_assemble_args (insn
);
3719 /* Handle relaxation and other transformations. */
3720 if (nios2_as_options
.relax
!= relax_none
3721 && !nios2_as_options
.noat
3722 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_UBRANCH
)
3723 output_ubranch (insn
);
3724 else if (nios2_as_options
.relax
!= relax_none
3725 && !nios2_as_options
.noat
3726 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_CBRANCH
)
3727 output_cbranch (insn
);
3728 else if (nios2_as_options
.relax
== relax_all
3729 && !nios2_as_options
.noat
3730 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_CALL
3732 && ((insn
->insn_reloc
->reloc_type
3733 == BFD_RELOC_NIOS2_CALL26
)
3734 || (insn
->insn_reloc
->reloc_type
3735 == BFD_RELOC_NIOS2_CALL26_NOAT
)))
3737 else if (saved_pinfo
== NIOS2_INSN_MACRO_MOVIA
)
3738 output_movia (insn
);
3742 nios2_cleanup_pseudo_insn (insn
, ps_insn
);
3746 /* Unrecognised instruction - error. */
3747 as_bad (_("unrecognised instruction %s"), insn
->insn_tokens
[0]);
3749 /* Don't leak memory. */
3753 /* Round up section size. */
3755 md_section_align (asection
*seg ATTRIBUTE_UNUSED
, valueT size
)
3757 /* I think byte alignment is fine here. */
3761 /* Implement TC_FORCE_RELOCATION. */
3763 nios2_force_relocation (fixS
*fixp
)
3765 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
3766 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
3767 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_ALIGN
)
3770 return generic_force_reloc (fixp
);
3773 /* Implement tc_fix_adjustable. */
3775 nios2_fix_adjustable (fixS
*fixp
)
3777 if (fixp
->fx_addsy
== NULL
)
3781 /* Prevent all adjustments to global symbols. */
3782 if (OUTPUT_FLAVOR
== bfd_target_elf_flavour
3783 && (S_IS_EXTERNAL (fixp
->fx_addsy
) || S_IS_WEAK (fixp
->fx_addsy
)))
3786 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
3787 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
3790 /* Preserve relocations against symbols with function type. */
3791 if (symbol_get_bfdsym (fixp
->fx_addsy
)->flags
& BSF_FUNCTION
)
3794 /* Don't allow symbols to be discarded on GOT related relocs. */
3795 if (fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT16
3796 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL16
3797 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_LO
3798 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_HA
3799 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_GD16
3800 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDM16
3801 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDO16
3802 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_IE16
3803 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LE16
3804 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPMOD
3805 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPREL
3806 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_TPREL
3807 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF
3808 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT_LO
3809 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT_HA
3810 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL_LO
3811 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL_HA
3818 /* Implement tc_frob_symbol. This is called in adjust_reloc_syms;
3819 it is used to remove *ABS* references from the symbol table. */
3821 nios2_frob_symbol (symbolS
*symp
)
3823 if ((OUTPUT_FLAVOR
== bfd_target_elf_flavour
3824 && symp
== section_symbol (absolute_section
))
3825 || !S_IS_DEFINED (symp
))
3831 /* The function tc_gen_reloc creates a relocation structure for the
3832 fixup fixp, and returns a pointer to it. This structure is passed
3833 to bfd_install_relocation so that it can be written to the object
3834 file for linking. */
3836 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
3838 arelent
*reloc
= XNEW (arelent
);
3839 reloc
->sym_ptr_ptr
= XNEW (asymbol
*);
3840 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
3842 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
3843 reloc
->addend
= fixp
->fx_offset
; /* fixp->fx_addnumber; */
3847 switch (fixp
->fx_r_type
)
3850 fixp
->fx_r_type
= BFD_RELOC_16_PCREL
;
3852 case BFD_RELOC_NIOS2_LO16
:
3853 fixp
->fx_r_type
= BFD_RELOC_NIOS2_PCREL_LO
;
3855 case BFD_RELOC_NIOS2_HIADJ16
:
3856 fixp
->fx_r_type
= BFD_RELOC_NIOS2_PCREL_HA
;
3863 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
3864 if (reloc
->howto
== NULL
)
3866 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
3867 _("can't represent relocation type %s"),
3868 bfd_get_reloc_code_name (fixp
->fx_r_type
));
3870 /* Set howto to a garbage value so that we can keep going. */
3871 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, BFD_RELOC_32
);
3872 gas_assert (reloc
->howto
!= NULL
);
3878 md_pcrel_from (fixS
*fixP ATTRIBUTE_UNUSED
)
3883 /* Called just before the assembler exits. */
3887 /* FIXME - not yet implemented */
3890 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
3891 Otherwise we have no need to default values of symbols. */
3893 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
3896 if (name
[0] == '_' && name
[1] == 'G'
3897 && strcmp (name
, GLOBAL_OFFSET_TABLE_NAME
) == 0)
3901 if (symbol_find (name
))
3902 as_bad ("GOT already in the symbol table");
3904 GOT_symbol
= symbol_new (name
, undefined_section
,
3905 &zero_address_frag
, 0);
3915 /* Implement tc_frob_label. */
3917 nios2_frob_label (symbolS
*lab
)
3919 /* Emit dwarf information. */
3920 dwarf2_emit_label (lab
);
3922 /* Update the label's address with the current output pointer. */
3923 symbol_set_frag (lab
, frag_now
);
3924 S_SET_VALUE (lab
, (valueT
) frag_now_fix ());
3926 /* Record this label for future adjustment after we find out what
3927 kind of data it references, and the required alignment therewith. */
3928 nios2_last_label
= lab
;
3931 /* Implement md_cons_align. */
3933 nios2_cons_align (int size
)
3936 const char *pfill
= NULL
;
3938 while ((size
>>= 1) != 0)
3941 if (subseg_text_p (now_seg
))
3942 pfill
= (const char *) nop32
;
3946 if (nios2_auto_align_on
)
3947 nios2_align (log_size
, pfill
, NULL
);
3949 nios2_last_label
= NULL
;
3952 /* Map 's' to SHF_NIOS2_GPREL. */
3953 /* This is from the Alpha code tc-alpha.c. */
3955 nios2_elf_section_letter (int letter
, const char **ptr_msg
)
3958 return SHF_NIOS2_GPREL
;
3960 *ptr_msg
= _("Bad .section directive: want a,s,w,x,M,S,G,T in string");
3964 /* Map SHF_ALPHA_GPREL to SEC_SMALL_DATA. */
3965 /* This is from the Alpha code tc-alpha.c. */
3967 nios2_elf_section_flags (flagword flags
, int attr
, int type ATTRIBUTE_UNUSED
)
3969 if (attr
& SHF_NIOS2_GPREL
)
3970 flags
|= SEC_SMALL_DATA
;
3974 /* Implement TC_PARSE_CONS_EXPRESSION to handle %tls_ldo(...) and
3976 bfd_reloc_code_real_type
3977 nios2_cons (expressionS
*exp
, int size
)
3979 bfd_reloc_code_real_type explicit_reloc
= BFD_RELOC_NONE
;
3980 const char *reloc_name
= NULL
;
3983 if (input_line_pointer
[0] == '%')
3985 if (startswith (input_line_pointer
+ 1, "tls_ldo"))
3987 reloc_name
= "%tls_ldo";
3989 as_bad (_("Illegal operands: %%tls_ldo in %d-byte data field"),
3993 input_line_pointer
+= 8;
3994 explicit_reloc
= BFD_RELOC_NIOS2_TLS_DTPREL
;
3997 else if (startswith (input_line_pointer
+ 1, "gotoff"))
3999 reloc_name
= "%gotoff";
4001 as_bad (_("Illegal operands: %%gotoff in %d-byte data field"),
4005 input_line_pointer
+= 7;
4006 explicit_reloc
= BFD_RELOC_NIOS2_GOTOFF
;
4010 if (explicit_reloc
!= BFD_RELOC_NONE
)
4013 if (input_line_pointer
[0] != '(')
4014 as_bad (_("Illegal operands: %s requires arguments in ()"),
4019 char *end
= ++input_line_pointer
;
4022 for (c
= *end
; !is_end_of_line
[c
]; end
++, c
= *end
)
4033 as_bad (_("Illegal operands: %s requires arguments in ()"),
4040 if (input_line_pointer
!= end
)
4041 as_bad (_("Illegal operands: %s requires arguments in ()"),
4045 input_line_pointer
++;
4047 c
= *input_line_pointer
;
4048 if (! is_end_of_line
[c
] && c
!= ',')
4049 as_bad (_("Illegal operands: garbage after %s()"),
4056 if (explicit_reloc
== BFD_RELOC_NONE
)
4058 return explicit_reloc
;
4061 /* Implement HANDLE_ALIGN. */
4063 nios2_handle_align (fragS
*fragp
)
4065 /* If we are expecting to relax in the linker, then we must output a
4066 relocation to tell the linker we are aligning code. */
4067 if (nios2_as_options
.relax
== relax_all
4068 && (fragp
->fr_type
== rs_align
|| fragp
->fr_type
== rs_align_code
)
4069 && fragp
->fr_address
+ fragp
->fr_fix
> 0
4070 && fragp
->fr_offset
> 1
4071 && now_seg
!= bss_section
)
4072 fix_new (fragp
, fragp
->fr_fix
, 0, &abs_symbol
, fragp
->fr_offset
, 0,
4073 BFD_RELOC_NIOS2_ALIGN
);
4076 /* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
4079 nios2_regname_to_dw2regnum (char *regname
)
4081 struct nios2_reg
*r
= nios2_reg_lookup (regname
);
4087 /* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
4088 unwind information for this procedure. */
4090 nios2_frame_initial_instructions (void)
4092 cfi_add_CFA_def_cfa (27, 0);
4096 /* Some special processing for a Nios II ELF file. */
4099 nios2_elf_final_processing (void)
4101 elf_elfheader (stdoutput
)->e_flags
= nios2_architecture
;