1 /* Altera Nios II assembler.
2 Copyright (C) 2012-2015 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
}
86 size_t md_longopts_size
= sizeof (md_longopts
);
88 /* The assembler supports three different relaxation modes, controlled by
89 command-line options. */
97 /* Struct contains all assembler options set with .set. */
100 /* .set noat -> noat = 1 allows assembly code to use at without warning
101 and macro expansions generate a warning.
102 .set at -> noat = 0, assembly code using at warn but macro expansions
103 do not generate warnings. */
106 /* .set nobreak -> nobreak = 1 allows assembly code to use ba,bt without
108 .set break -> nobreak = 0, assembly code using ba,bt warns. */
111 /* .cmd line option -relax-all allows all branches and calls to be replaced
112 with longer versions.
113 -no-relax inhibits branch/call conversion.
114 The default value is relax_section, which relaxes branches within
118 } nios2_as_options
= {FALSE
, FALSE
, relax_section
};
121 typedef struct nios2_insn_reloc
123 /* Any expression in the instruction is parsed into this field,
124 which is passed to fix_new_exp() to generate a fixup. */
125 expressionS reloc_expression
;
127 /* The type of the relocation to be applied. */
128 bfd_reloc_code_real_type reloc_type
;
131 unsigned int reloc_pcrel
;
133 /* The next relocation to be applied to the instruction. */
134 struct nios2_insn_reloc
*reloc_next
;
137 /* This struct is used to hold state when assembling instructions. */
138 typedef struct nios2_insn_info
140 /* Assembled instruction. */
141 unsigned long insn_code
;
143 /* Constant bits masked into insn_code for self-check mode. */
144 unsigned long constant_bits
;
146 /* Pointer to the relevant bit of the opcode table. */
147 const struct nios2_opcode
*insn_nios2_opcode
;
148 /* After parsing ptrs to the tokens in the instruction fill this array
149 it is terminated with a null pointer (hence the first +1).
150 The second +1 is because in some parts of the code the opcode
151 is not counted as a token, but still placed in this array. */
152 const char *insn_tokens
[NIOS2_MAX_INSN_TOKENS
+ 1 + 1];
154 /* This holds information used to generate fixups
155 and eventually relocations if it is not null. */
156 nios2_insn_relocS
*insn_reloc
;
160 /* This struct is used to convert Nios II pseudo-ops into the
161 corresponding real op. */
162 typedef struct nios2_ps_insn_info
164 /* Map this pseudo_op... */
165 const char *pseudo_insn
;
167 /* ...to this real instruction. */
170 /* Call this function to modify the operands.... */
171 void (*arg_modifer_func
) (char ** parsed_args
, const char *arg
, int num
,
174 /* ...with these arguments. */
175 const char *arg_modifier
;
179 /* If arg_modifier_func allocates new memory, provide this function
180 to free it afterwards. */
181 void (*arg_cleanup_func
) (char **parsed_args
, int num
, int start
);
182 } nios2_ps_insn_infoS
;
184 /* Opcode hash table. */
185 static struct hash_control
*nios2_opcode_hash
= NULL
;
186 #define nios2_opcode_lookup(NAME) \
187 ((struct nios2_opcode *) hash_find (nios2_opcode_hash, (NAME)))
189 /* Register hash table. */
190 static struct hash_control
*nios2_reg_hash
= NULL
;
191 #define nios2_reg_lookup(NAME) \
192 ((struct nios2_reg *) hash_find (nios2_reg_hash, (NAME)))
195 /* Pseudo-op hash table. */
196 static struct hash_control
*nios2_ps_hash
= NULL
;
197 #define nios2_ps_lookup(NAME) \
198 ((nios2_ps_insn_infoS *) hash_find (nios2_ps_hash, (NAME)))
200 /* The known current alignment of the current section. */
201 static int nios2_current_align
;
202 static segT nios2_current_align_seg
;
204 static int nios2_auto_align_on
= 1;
206 /* The last seen label in the current section. This is used to auto-align
207 labels preceeding instructions. */
208 static symbolS
*nios2_last_label
;
211 /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
216 /** Utility routines. */
217 /* Function md_chars_to_number takes the sequence of
218 bytes in buf and returns the corresponding value
219 in an int. n must be 1, 2 or 4. */
221 md_chars_to_number (char *buf
, int n
)
226 gas_assert (n
== 1 || n
== 2 || n
== 4);
229 if (target_big_endian
)
230 for (i
= 0; i
< n
; ++i
)
231 val
= val
| ((buf
[i
] & 0xff) << 8 * (n
- (i
+ 1)));
233 for (i
= 0; i
< n
; ++i
)
234 val
= val
| ((buf
[i
] & 0xff) << 8 * i
);
239 /* This function turns a C long int, short int or char
240 into the series of bytes that represent the number
241 on the target machine. */
243 md_number_to_chars (char *buf
, valueT val
, int n
)
245 gas_assert (n
== 1 || n
== 2 || n
== 4 || n
== 8);
246 if (target_big_endian
)
247 number_to_chars_bigendian (buf
, val
, n
);
249 number_to_chars_littleendian (buf
, val
, n
);
252 /* Turn a string in input_line_pointer into a floating point constant
253 of type TYPE, and store the appropriate bytes in *LITP. The number
254 of LITTLENUMS emitted is stored in *SIZEP. An error message is
255 returned, or NULL on OK. */
257 md_atof (int type
, char *litP
, int *sizeP
)
260 LITTLENUM_TYPE words
[4];
274 return _("bad call to md_atof");
277 t
= atof_ieee (input_line_pointer
, type
, words
);
279 input_line_pointer
= t
;
283 if (! target_big_endian
)
284 for (i
= prec
- 1; i
>= 0; i
--, litP
+= 2)
285 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
287 for (i
= 0; i
< prec
; i
++, litP
+= 2)
288 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
293 /* Return true if STR starts with PREFIX, which should be a string literal. */
294 #define strprefix(STR, PREFIX) \
295 (strncmp ((STR), PREFIX, strlen (PREFIX)) == 0)
298 /* Return true if STR is prefixed with a special relocation operator. */
300 nios2_special_relocation_p (const char *str
)
302 return (strprefix (str
, "%lo")
303 || strprefix (str
, "%hi")
304 || strprefix (str
, "%hiadj")
305 || strprefix (str
, "%gprel")
306 || strprefix (str
, "%got")
307 || strprefix (str
, "%call")
308 || strprefix (str
, "%gotoff_lo")
309 || strprefix (str
, "%gotoff_hiadj")
310 || strprefix (str
, "%tls_gd")
311 || strprefix (str
, "%tls_ldm")
312 || strprefix (str
, "%tls_ldo")
313 || strprefix (str
, "%tls_ie")
314 || strprefix (str
, "%tls_le")
315 || strprefix (str
, "%gotoff"));
319 /* nop fill pattern for text section. */
320 static char const nop
[4] = { 0x3a, 0x88, 0x01, 0x00 };
322 /* Handles all machine-dependent alignment needs. */
324 nios2_align (int log_size
, const char *pfill
, symbolS
*label
)
327 long max_alignment
= 15;
329 /* The front end is prone to changing segments out from under us
330 temporarily when -g is in effect. */
331 int switched_seg_p
= (nios2_current_align_seg
!= now_seg
);
334 if (align
> max_alignment
)
336 align
= max_alignment
;
337 as_bad (_("Alignment too large: %d. assumed"), align
);
341 as_warn (_("Alignment negative: 0 assumed"));
347 if (subseg_text_p (now_seg
) && align
>= 2)
349 /* First, make sure we're on a four-byte boundary, in case
350 someone has been putting .byte values the text section. */
351 if (nios2_current_align
< 2 || switched_seg_p
)
352 frag_align (2, 0, 0);
354 /* Now fill in the alignment pattern. */
356 frag_align_pattern (align
, pfill
, sizeof nop
, 0);
358 frag_align (align
, 0, 0);
361 frag_align (align
, 0, 0);
364 nios2_current_align
= align
;
366 /* If the last label was in a different section we can't align it. */
367 if (label
!= NULL
&& !switched_seg_p
)
370 int label_seen
= FALSE
;
371 struct frag
*old_frag
;
375 gas_assert (S_GET_SEGMENT (label
) == now_seg
);
377 old_frag
= symbol_get_frag (label
);
378 old_value
= S_GET_VALUE (label
);
379 new_value
= (valueT
) frag_now_fix ();
381 /* It is possible to have more than one label at a particular
382 address, especially if debugging is enabled, so we must
383 take care to adjust all the labels at this address in this
384 fragment. To save time we search from the end of the symbol
385 list, backwards, since the symbols we are interested in are
386 almost certainly the ones that were most recently added.
387 Also to save time we stop searching once we have seen at least
388 one matching label, and we encounter a label that is no longer
389 in the target fragment. Note, this search is guaranteed to
390 find at least one match when sym == label, so no special case
391 code is necessary. */
392 for (sym
= symbol_lastP
; sym
!= NULL
; sym
= symbol_previous (sym
))
393 if (symbol_get_frag (sym
) == old_frag
394 && S_GET_VALUE (sym
) == old_value
)
397 symbol_set_frag (sym
, frag_now
);
398 S_SET_VALUE (sym
, new_value
);
400 else if (label_seen
&& symbol_get_frag (sym
) != old_frag
)
403 record_alignment (now_seg
, align
);
408 /** Support for self-check mode. */
410 /* Mode of the assembler. */
413 NIOS2_MODE_ASSEMBLE
, /* Ordinary operation. */
414 NIOS2_MODE_TEST
/* Hidden mode used for self testing. */
417 static NIOS2_MODE nios2_mode
= NIOS2_MODE_ASSEMBLE
;
419 /* This function is used to in self-checking mode
420 to check the assembled instruction
421 opcode should be the assembled opcode, and exp_opcode
422 the parsed string representing the expected opcode. */
424 nios2_check_assembly (unsigned int opcode
, const char *exp_opcode
)
426 if (nios2_mode
== NIOS2_MODE_TEST
)
428 if (exp_opcode
== NULL
)
429 as_bad (_("expecting opcode string in self test mode"));
430 else if (opcode
!= strtoul (exp_opcode
, NULL
, 16))
431 as_bad (_("assembly 0x%08x, expected %s"), opcode
, exp_opcode
);
436 /** Support for machine-dependent assembler directives. */
437 /* Handle the .align pseudo-op. This aligns to a power of two. It
438 also adjusts any current instruction label. We treat this the same
439 way the MIPS port does: .align 0 turns off auto alignment. */
441 s_nios2_align (int ignore ATTRIBUTE_UNUSED
)
445 const char *pfill
= NULL
;
446 long max_alignment
= 15;
448 align
= get_absolute_expression ();
449 if (align
> max_alignment
)
451 align
= max_alignment
;
452 as_bad (_("Alignment too large: %d. assumed"), align
);
456 as_warn (_("Alignment negative: 0 assumed"));
460 if (*input_line_pointer
== ',')
462 input_line_pointer
++;
463 fill
= get_absolute_expression ();
464 pfill
= (const char *) &fill
;
466 else if (subseg_text_p (now_seg
))
467 pfill
= (const char *) &nop
;
471 nios2_last_label
= NULL
;
476 nios2_auto_align_on
= 1;
477 nios2_align (align
, pfill
, nios2_last_label
);
478 nios2_last_label
= NULL
;
481 nios2_auto_align_on
= 0;
483 demand_empty_rest_of_line ();
486 /* Handle the .text pseudo-op. This is like the usual one, but it
487 clears the saved last label and resets known alignment. */
492 nios2_last_label
= NULL
;
493 nios2_current_align
= 0;
494 nios2_current_align_seg
= now_seg
;
497 /* Handle the .data pseudo-op. This is like the usual one, but it
498 clears the saved last label and resets known alignment. */
503 nios2_last_label
= NULL
;
504 nios2_current_align
= 0;
505 nios2_current_align_seg
= now_seg
;
508 /* Handle the .section pseudo-op. This is like the usual one, but it
509 clears the saved last label and resets known alignment. */
511 s_nios2_section (int ignore
)
513 obj_elf_section (ignore
);
514 nios2_last_label
= NULL
;
515 nios2_current_align
= 0;
516 nios2_current_align_seg
= now_seg
;
519 /* Explicitly unaligned cons. */
521 s_nios2_ucons (int nbytes
)
524 hold
= nios2_auto_align_on
;
525 nios2_auto_align_on
= 0;
527 nios2_auto_align_on
= hold
;
530 /* Handle the .sdata directive. */
532 s_nios2_sdata (int ignore ATTRIBUTE_UNUSED
)
534 get_absolute_expression (); /* Ignored. */
535 subseg_new (".sdata", 0);
536 demand_empty_rest_of_line ();
539 /* .set sets assembler options eg noat/at and is also used
540 to set symbol values (.equ, .equiv ). */
542 s_nios2_set (int equiv
)
544 char *directive
= input_line_pointer
;
545 char delim
= get_symbol_end ();
546 char *endline
= input_line_pointer
;
549 /* We only want to handle ".set XXX" if the
550 user has tried ".set XXX, YYY" they are not
551 trying a directive. This prevents
552 us from polluting the name space. */
554 if (is_end_of_line
[(unsigned char) *input_line_pointer
])
556 bfd_boolean done
= TRUE
;
559 if (!strcmp (directive
, "noat"))
560 nios2_as_options
.noat
= TRUE
;
561 else if (!strcmp (directive
, "at"))
562 nios2_as_options
.noat
= FALSE
;
563 else if (!strcmp (directive
, "nobreak"))
564 nios2_as_options
.nobreak
= TRUE
;
565 else if (!strcmp (directive
, "break"))
566 nios2_as_options
.nobreak
= FALSE
;
567 else if (!strcmp (directive
, "norelax"))
568 nios2_as_options
.relax
= relax_none
;
569 else if (!strcmp (directive
, "relaxsection"))
570 nios2_as_options
.relax
= relax_section
;
571 else if (!strcmp (directive
, "relaxall"))
572 nios2_as_options
.relax
= relax_all
;
579 demand_empty_rest_of_line ();
584 /* If we fall through to here, either we have ".set XXX, YYY"
585 or we have ".set XXX" where XXX is unknown or we have
587 input_line_pointer
= directive
;
592 /* Machine-dependent assembler directives.
593 Format of each entry is:
594 { "directive", handler_func, param } */
595 const pseudo_typeS md_pseudo_table
[] = {
596 {"align", s_nios2_align
, 0},
597 {"text", s_nios2_text
, 0},
598 {"data", s_nios2_data
, 0},
599 {"section", s_nios2_section
, 0},
600 {"section.s", s_nios2_section
, 0},
601 {"sect", s_nios2_section
, 0},
602 {"sect.s", s_nios2_section
, 0},
603 /* .dword and .half are included for compatibility with MIPS. */
606 /* NIOS2 native word size is 4 bytes, so we override
607 the GAS default of 2. */
609 /* Explicitly unaligned directives. */
610 {"2byte", s_nios2_ucons
, 2},
611 {"4byte", s_nios2_ucons
, 4},
612 {"8byte", s_nios2_ucons
, 8},
613 {"16byte", s_nios2_ucons
, 16},
615 {"sdata", s_nios2_sdata
, 0},
617 {"set", s_nios2_set
, 0},
622 /** Relaxation support. */
624 /* We support two relaxation modes: a limited PC-relative mode with
625 -relax-section (the default), and an absolute jump mode with -relax-all.
627 Nios II PC-relative branch instructions only support 16-bit offsets.
628 And, there's no good way to add a 32-bit constant to the PC without
631 To deal with this, for the pc-relative relaxation mode we convert
633 into a series of 16-bit adds, like:
637 addi at, at, remainder
640 Similarly, conditional branches are converted from
641 b(condition) r, s, label
643 b(opposite condition) r, s, skip
647 addi at, at, remainder
651 The compiler can do a better job, either by converting the branch
652 directly into a JMP (going through the GOT for PIC) or by allocating
653 a second register for the 32-bit displacement.
655 For the -relax-all relaxation mode, the conversions are
656 movhi at, %hi(symbol+offset)
657 ori at, %lo(symbol+offset)
660 b(opposite condition), r, s, skip
661 movhi at, %hi(symbol+offset)
662 ori at, %lo(symbol+offset)
668 /* Arbitrarily limit the number of addis we can insert; we need to be able
669 to specify the maximum growth size for each frag that contains a
670 relaxable branch. There's no point in specifying a huge number here
671 since that means the assembler needs to allocate that much extra
672 memory for every branch, and almost no real code will ever need it.
673 Plus, as already noted a better solution is to just use a jmp, or
674 allocate a second register to hold a 32-bit displacement.
675 FIXME: Rather than making this a constant, it could be controlled by
676 a command-line argument. */
677 #define RELAX_MAX_ADDI 32
679 /* The fr_subtype field represents the target-specific relocation state.
680 It has type relax_substateT (unsigned int). We use it to track the
681 number of addis necessary, plus a bit to track whether this is a
683 Regardless of the smaller RELAX_MAX_ADDI limit, we reserve 16 bits
684 in the fr_subtype to encode the number of addis so that the whole
685 theoretically-valid range is representable.
686 For the -relax-all mode, N = 0 represents an in-range branch and N = 1
687 represents a branch that needs to be relaxed. */
688 #define UBRANCH (0 << 16)
689 #define CBRANCH (1 << 16)
690 #define IS_CBRANCH(SUBTYPE) ((SUBTYPE) & CBRANCH)
691 #define IS_UBRANCH(SUBTYPE) (!IS_CBRANCH (SUBTYPE))
692 #define UBRANCH_SUBTYPE(N) (UBRANCH | (N))
693 #define CBRANCH_SUBTYPE(N) (CBRANCH | (N))
694 #define SUBTYPE_ADDIS(SUBTYPE) ((SUBTYPE) & 0xffff)
696 /* For the -relax-section mode, unconditional branches require 2 extra i
697 nstructions besides the addis, conditional branches require 3. */
698 #define UBRANCH_ADDIS_TO_SIZE(N) (((N) + 2) * 4)
699 #define CBRANCH_ADDIS_TO_SIZE(N) (((N) + 3) * 4)
701 /* For the -relax-all mode, unconditional branches require 3 instructions
702 and conditional branches require 4. */
703 #define UBRANCH_JUMP_SIZE 12
704 #define CBRANCH_JUMP_SIZE 16
706 /* Maximum sizes of relaxation sequences. */
707 #define UBRANCH_MAX_SIZE \
708 (nios2_as_options.relax == relax_all \
709 ? UBRANCH_JUMP_SIZE \
710 : UBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
711 #define CBRANCH_MAX_SIZE \
712 (nios2_as_options.relax == relax_all \
713 ? CBRANCH_JUMP_SIZE \
714 : CBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
716 /* Register number of AT, the assembler temporary. */
719 /* Determine how many bytes are required to represent the sequence
720 indicated by SUBTYPE. */
722 nios2_relax_subtype_size (relax_substateT subtype
)
724 int n
= SUBTYPE_ADDIS (subtype
);
726 /* Regular conditional/unconditional branch instruction. */
728 else if (nios2_as_options
.relax
== relax_all
)
729 return (IS_CBRANCH (subtype
) ? CBRANCH_JUMP_SIZE
: UBRANCH_JUMP_SIZE
);
730 else if (IS_CBRANCH (subtype
))
731 return CBRANCH_ADDIS_TO_SIZE (n
);
733 return UBRANCH_ADDIS_TO_SIZE (n
);
736 /* Estimate size of fragp before relaxation.
737 This could also examine the offset in fragp and adjust
738 fragp->fr_subtype, but we will do that in nios2_relax_frag anyway. */
740 md_estimate_size_before_relax (fragS
*fragp
, segT segment ATTRIBUTE_UNUSED
)
742 return nios2_relax_subtype_size (fragp
->fr_subtype
);
745 /* Implement md_relax_frag, returning the change in size of the frag. */
747 nios2_relax_frag (segT segment
, fragS
*fragp
, long stretch
)
749 addressT target
= fragp
->fr_offset
;
750 relax_substateT subtype
= fragp
->fr_subtype
;
751 symbolS
*symbolp
= fragp
->fr_symbol
;
755 fragS
*sym_frag
= symbol_get_frag (symbolp
);
759 target
+= S_GET_VALUE (symbolp
);
761 /* See comments in write.c:relax_frag about handling of stretch. */
763 && sym_frag
->relax_marker
!= fragp
->relax_marker
)
765 if (stretch
< 0 || sym_frag
->region
== fragp
->region
)
767 else if (target
< fragp
->fr_address
)
768 target
= fragp
->fr_next
->fr_address
+ stretch
;
771 /* We subtract fr_var (4 for 32-bit insns) because all pc relative
772 branches are from the next instruction. */
773 offset
= target
- fragp
->fr_address
- fragp
->fr_fix
- fragp
->fr_var
;
774 if (offset
>= -32768 && offset
<= 32764)
775 /* Fits in PC-relative branch. */
777 else if (nios2_as_options
.relax
== relax_all
)
778 /* Convert to jump. */
780 else if (nios2_as_options
.relax
== relax_section
781 && S_GET_SEGMENT (symbolp
) == segment
782 && S_IS_DEFINED (symbolp
))
783 /* Attempt a PC-relative relaxation on a branch to a defined
784 symbol in the same segment. */
786 /* The relaxation for conditional branches is offset by 4
787 bytes because we insert the inverted branch around the
789 if (IS_CBRANCH (subtype
))
792 n
= offset
/ 32767 + 1;
794 n
= offset
/ -32768 + 1;
796 /* Bail out immediately if relaxation has failed. If we try to
797 defer the diagnostic to md_convert_frag, some pathological test
798 cases (e.g. gcc/testsuite/gcc.c-torture/compile/20001226-1.c)
799 apparently never converge. By returning 0 here we could pretend
800 to the caller that nothing has changed, but that leaves things
801 in an inconsistent state when we get to md_convert_frag. */
802 if (n
> RELAX_MAX_ADDI
)
804 as_bad_where (fragp
->fr_file
, fragp
->fr_line
,
805 _("branch offset out of range\n"));
806 as_fatal (_("branch relaxation failed\n"));
810 /* We cannot handle this case, diagnose overflow later. */
813 if (IS_CBRANCH (subtype
))
814 fragp
->fr_subtype
= CBRANCH_SUBTYPE (n
);
816 fragp
->fr_subtype
= UBRANCH_SUBTYPE (n
);
818 return (nios2_relax_subtype_size (fragp
->fr_subtype
)
819 - nios2_relax_subtype_size (subtype
));
822 /* If we got here, it's probably an error. */
827 /* Complete fragp using the data from the relaxation pass. */
829 md_convert_frag (bfd
*headers ATTRIBUTE_UNUSED
, segT segment ATTRIBUTE_UNUSED
,
832 char *buffer
= fragp
->fr_literal
+ fragp
->fr_fix
;
833 relax_substateT subtype
= fragp
->fr_subtype
;
834 int n
= SUBTYPE_ADDIS (subtype
);
835 addressT target
= fragp
->fr_offset
;
836 symbolS
*symbolp
= fragp
->fr_symbol
;
838 unsigned int addend_mask
, addi_mask
;
839 offsetT addend
, remainder
;
842 /* If we didn't or can't relax, this is a regular branch instruction.
843 We just need to generate the fixup for the symbol and offset. */
846 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
, 1,
852 /* Replace the cbranch at fr_fix with one that has the opposite condition
853 in order to jump around the block of instructions we'll be adding. */
854 if (IS_CBRANCH (subtype
))
856 unsigned int br_opcode
;
857 unsigned int old_op
, new_op
;
860 /* Account for the nextpc and jmp in the pc-relative case, or the two
861 load instructions and jump in the absolute case. */
862 if (nios2_as_options
.relax
== relax_section
)
863 nbytes
= (n
+ 2) * 4;
867 br_opcode
= md_chars_to_number (buffer
, 4);
868 old_op
= GET_IW_R1_OP (br_opcode
);
890 as_bad_where (fragp
->fr_file
, fragp
->fr_line
,
891 _("expecting conditional branch for relaxation\n"));
895 br_opcode
= (br_opcode
& ~IW_R1_OP_SHIFTED_MASK
) | SET_IW_R1_OP (new_op
);
896 br_opcode
= br_opcode
| SET_IW_I_IMM16 (nbytes
);
897 md_number_to_chars (buffer
, br_opcode
, 4);
902 /* Load at for the PC-relative case. */
903 if (nios2_as_options
.relax
== relax_section
)
905 /* Insert the nextpc instruction. */
906 md_number_to_chars (buffer
,
907 MATCH_R1_NEXTPC
| SET_IW_R_C (AT_REGNUM
), 4);
911 /* We need to know whether the offset is positive or negative. */
912 target
+= S_GET_VALUE (symbolp
);
913 offset
= target
- fragp
->fr_address
- fragp
->fr_fix
;
918 addend_mask
= SET_IW_I_IMM16 ((unsigned int)addend
);
920 /* Insert n-1 addi instructions. */
921 addi_mask
= (MATCH_R1_ADDI
922 | SET_IW_I_B (AT_REGNUM
)
923 | SET_IW_I_A (AT_REGNUM
));
924 for (i
= 0; i
< n
- 1; i
++)
926 md_number_to_chars (buffer
, addi_mask
| addend_mask
, 4);
931 /* Insert the last addi instruction to hold the remainder. */
932 remainder
= offset
- addend
* (n
- 1);
933 gas_assert (remainder
>= -32768 && remainder
<= 32767);
934 addend_mask
= SET_IW_I_IMM16 ((unsigned int)remainder
);
935 md_number_to_chars (buffer
, addi_mask
| addend_mask
, 4);
940 /* Load at for the absolute case. */
943 md_number_to_chars (buffer
,
944 (MATCH_R1_ORHI
| SET_IW_I_B (AT_REGNUM
)
947 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
,
948 0, BFD_RELOC_NIOS2_HI16
);
951 md_number_to_chars (buffer
,
952 (MATCH_R1_ORI
| SET_IW_I_B (AT_REGNUM
)
953 | SET_IW_I_A (AT_REGNUM
)),
955 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
,
956 0, BFD_RELOC_NIOS2_LO16
);
961 /* Insert the jmp instruction. */
962 md_number_to_chars (buffer
, MATCH_R1_JMP
| SET_IW_R_A (AT_REGNUM
), 4);
968 /** Fixups and overflow checking. */
970 /* Check a fixup for overflow. */
972 nios2_check_overflow (valueT fixup
, reloc_howto_type
*howto
)
974 /* Apply the rightshift before checking for overflow. */
975 fixup
= ((signed)fixup
) >> howto
->rightshift
;
977 /* Check for overflow - return TRUE if overflow, FALSE if not. */
978 switch (howto
->complain_on_overflow
)
980 case complain_overflow_dont
:
982 case complain_overflow_bitfield
:
983 if ((fixup
>> howto
->bitsize
) != 0
984 && ((signed) fixup
>> howto
->bitsize
) != -1)
987 case complain_overflow_signed
:
988 if ((fixup
& 0x80000000) > 0)
990 /* Check for negative overflow. */
991 if ((signed) fixup
< ((signed) 0x80000000 >> howto
->bitsize
))
996 /* Check for positive overflow. */
997 if (fixup
>= ((unsigned) 1 << (howto
->bitsize
- 1)))
1001 case complain_overflow_unsigned
:
1002 if ((fixup
>> howto
->bitsize
) != 0)
1006 as_bad (_("error checking for overflow - broken assembler"));
1012 /* Emit diagnostic for fixup overflow. */
1014 nios2_diagnose_overflow (valueT fixup
, reloc_howto_type
*howto
,
1015 fixS
*fixP
, valueT value
)
1017 if (fixP
->fx_r_type
== BFD_RELOC_8
1018 || fixP
->fx_r_type
== BFD_RELOC_16
1019 || fixP
->fx_r_type
== BFD_RELOC_32
)
1020 /* These relocs are against data, not instructions. */
1021 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1022 _("immediate value 0x%x truncated to 0x%x"),
1023 (unsigned int) fixup
,
1024 (unsigned int) (~(~(valueT
) 0 << howto
->bitsize
) & fixup
));
1027 /* What opcode is the instruction? This will determine
1028 whether we check for overflow in immediate values
1029 and what error message we get. */
1030 const struct nios2_opcode
*opcode
;
1031 enum overflow_type overflow_msg_type
;
1032 unsigned int range_min
;
1033 unsigned int range_max
;
1034 unsigned int address
;
1036 opcode
= nios2_find_opcode_hash (value
, bfd_get_mach (stdoutput
));
1037 gas_assert (opcode
);
1038 gas_assert (fixP
->fx_size
== opcode
->size
);
1039 overflow_msg_type
= opcode
->overflow_msg
;
1040 switch (overflow_msg_type
)
1042 case call_target_overflow
:
1044 = ((fixP
->fx_frag
->fr_address
+ fixP
->fx_where
) & 0xf0000000);
1045 range_max
= range_min
+ 0x0fffffff;
1046 address
= fixup
| range_min
;
1048 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1049 _("call target address 0x%08x out of range 0x%08x to 0x%08x"),
1050 address
, range_min
, range_max
);
1052 case branch_target_overflow
:
1053 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1054 _("branch offset %d out of range %d to %d"),
1055 (int)fixup
, -32768, 32767);
1057 case address_offset_overflow
:
1058 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1059 _("%s offset %d out of range %d to %d"),
1060 opcode
->name
, (int)fixup
, -32768, 32767);
1062 case signed_immed16_overflow
:
1063 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1064 _("immediate value %d out of range %d to %d"),
1065 (int)fixup
, -32768, 32767);
1067 case unsigned_immed16_overflow
:
1068 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1069 _("immediate value %u out of range %u to %u"),
1070 (unsigned int)fixup
, 0, 65535);
1072 case unsigned_immed5_overflow
:
1073 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1074 _("immediate value %u out of range %u to %u"),
1075 (unsigned int)fixup
, 0, 31);
1077 case custom_opcode_overflow
:
1078 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1079 _("custom instruction opcode %u out of range %u to %u"),
1080 (unsigned int)fixup
, 0, 255);
1083 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1084 _("overflow in immediate argument"));
1090 /* Apply a fixup to the object file. */
1092 md_apply_fix (fixS
*fixP
, valueT
*valP
, segT seg ATTRIBUTE_UNUSED
)
1094 /* Assert that the fixup is one we can handle. */
1095 gas_assert (fixP
!= NULL
&& valP
!= NULL
1096 && (fixP
->fx_r_type
== BFD_RELOC_8
1097 || fixP
->fx_r_type
== BFD_RELOC_16
1098 || fixP
->fx_r_type
== BFD_RELOC_32
1099 || fixP
->fx_r_type
== BFD_RELOC_64
1100 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_S16
1101 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_U16
1102 || fixP
->fx_r_type
== BFD_RELOC_16_PCREL
1103 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL26
1104 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM5
1105 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CACHE_OPX
1106 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM6
1107 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM8
1108 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_HI16
1109 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_LO16
1110 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_HIADJ16
1111 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GPREL
1112 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1113 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
1114 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_UJMP
1115 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CJMP
1116 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALLR
1117 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_ALIGN
1118 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT16
1119 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL16
1120 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_LO
1121 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_HA
1122 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_GD16
1123 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDM16
1124 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDO16
1125 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_IE16
1126 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LE16
1127 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF
1128 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPREL
1129 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL26_NOAT
1130 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT_LO
1131 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT_HA
1132 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL_LO
1133 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL_HA
1134 /* Add other relocs here as we generate them. */
1137 if (fixP
->fx_r_type
== BFD_RELOC_64
)
1139 /* We may reach here due to .8byte directives, but we never output
1140 BFD_RELOC_64; it must be resolved. */
1141 if (fixP
->fx_addsy
!= NULL
)
1142 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1143 _("cannot create 64-bit relocation"));
1146 md_number_to_chars (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
,
1153 /* The value passed in valP can be the value of a fully
1154 resolved expression, or it can be the value of a partially
1155 resolved expression. In the former case, both fixP->fx_addsy
1156 and fixP->fx_subsy are NULL, and fixP->fx_offset == *valP, and
1157 we can fix up the instruction that fixP relates to.
1158 In the latter case, one or both of fixP->fx_addsy and
1159 fixP->fx_subsy are not NULL, and fixP->fx_offset may or may not
1160 equal *valP. We don't need to check for fixP->fx_subsy being null
1161 because the generic part of the assembler generates an error if
1162 it is not an absolute symbol. */
1163 if (fixP
->fx_addsy
!= NULL
)
1164 /* Partially resolved expression. */
1166 fixP
->fx_addnumber
= fixP
->fx_offset
;
1169 switch (fixP
->fx_r_type
)
1171 case BFD_RELOC_NIOS2_TLS_GD16
:
1172 case BFD_RELOC_NIOS2_TLS_LDM16
:
1173 case BFD_RELOC_NIOS2_TLS_LDO16
:
1174 case BFD_RELOC_NIOS2_TLS_IE16
:
1175 case BFD_RELOC_NIOS2_TLS_LE16
:
1176 case BFD_RELOC_NIOS2_TLS_DTPMOD
:
1177 case BFD_RELOC_NIOS2_TLS_DTPREL
:
1178 case BFD_RELOC_NIOS2_TLS_TPREL
:
1179 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
1186 /* Fully resolved fixup. */
1188 reloc_howto_type
*howto
1189 = bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
);
1192 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1193 _("relocation is not supported"));
1196 valueT fixup
= *valP
;
1200 /* If this is a pc-relative relocation, we need to
1201 subtract the current offset within the object file
1202 FIXME : for some reason fixP->fx_pcrel isn't 1 when it should be
1203 so I'm using the howto structure instead to determine this. */
1204 if (howto
->pc_relative
== 1)
1205 fixup
= fixup
- (fixP
->fx_frag
->fr_address
+ fixP
->fx_where
1208 /* Get the instruction or data to be fixed up. */
1209 buf
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1210 value
= md_chars_to_number (buf
, fixP
->fx_size
);
1212 /* Check for overflow, emitting a diagnostic if necessary. */
1213 if (nios2_check_overflow (fixup
, howto
))
1214 nios2_diagnose_overflow (fixup
, howto
, fixP
, value
);
1216 /* Apply the right shift. */
1217 fixup
= ((signed)fixup
) >> howto
->rightshift
;
1219 /* Truncate the fixup to right size. */
1220 switch (fixP
->fx_r_type
)
1222 case BFD_RELOC_NIOS2_HI16
:
1223 fixup
= (fixup
>> 16) & 0xFFFF;
1225 case BFD_RELOC_NIOS2_LO16
:
1226 fixup
= fixup
& 0xFFFF;
1228 case BFD_RELOC_NIOS2_HIADJ16
:
1229 fixup
= ((((fixup
>> 16) & 0xFFFF) + ((fixup
>> 15) & 0x01))
1234 int n
= sizeof (fixup
) * 8 - howto
->bitsize
;
1235 fixup
= (fixup
<< n
) >> n
;
1240 /* Fix up the instruction. */
1241 value
= (value
& ~howto
->dst_mask
) | (fixup
<< howto
->bitpos
);
1242 md_number_to_chars (buf
, value
, fixP
->fx_size
);
1248 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
)
1252 && !S_IS_DEFINED (fixP
->fx_addsy
) && !S_IS_WEAK (fixP
->fx_addsy
))
1253 S_SET_WEAK (fixP
->fx_addsy
);
1255 else if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1261 /** Instruction parsing support. */
1263 /* General internal error routine. */
1266 bad_opcode (const struct nios2_opcode
*op
)
1268 fprintf (stderr
, _("internal error: broken opcode descriptor for `%s %s'\n"),
1269 op
->name
, op
->args
);
1270 as_fatal (_("Broken assembler. No assembly attempted."));
1273 /* Special relocation directive strings. */
1275 struct nios2_special_relocS
1278 bfd_reloc_code_real_type reloc_type
;
1281 /* This table is sorted so that prefix strings are listed after the longer
1282 strings that include them -- e.g., %got after %got_hiadj, etc. */
1284 struct nios2_special_relocS nios2_special_reloc
[] = {
1285 {"%hiadj", BFD_RELOC_NIOS2_HIADJ16
},
1286 {"%hi", BFD_RELOC_NIOS2_HI16
},
1287 {"%lo", BFD_RELOC_NIOS2_LO16
},
1288 {"%gprel", BFD_RELOC_NIOS2_GPREL
},
1289 {"%call_lo", BFD_RELOC_NIOS2_CALL_LO
},
1290 {"%call_hiadj", BFD_RELOC_NIOS2_CALL_HA
},
1291 {"%call", BFD_RELOC_NIOS2_CALL16
},
1292 {"%gotoff_lo", BFD_RELOC_NIOS2_GOTOFF_LO
},
1293 {"%gotoff_hiadj", BFD_RELOC_NIOS2_GOTOFF_HA
},
1294 {"%gotoff", BFD_RELOC_NIOS2_GOTOFF
},
1295 {"%got_hiadj", BFD_RELOC_NIOS2_GOT_HA
},
1296 {"%got_lo", BFD_RELOC_NIOS2_GOT_LO
},
1297 {"%got", BFD_RELOC_NIOS2_GOT16
},
1298 {"%tls_gd", BFD_RELOC_NIOS2_TLS_GD16
},
1299 {"%tls_ldm", BFD_RELOC_NIOS2_TLS_LDM16
},
1300 {"%tls_ldo", BFD_RELOC_NIOS2_TLS_LDO16
},
1301 {"%tls_ie", BFD_RELOC_NIOS2_TLS_IE16
},
1302 {"%tls_le", BFD_RELOC_NIOS2_TLS_LE16
},
1305 #define NIOS2_NUM_SPECIAL_RELOCS \
1306 (sizeof(nios2_special_reloc)/sizeof(nios2_special_reloc[0]))
1307 const int nios2_num_special_relocs
= NIOS2_NUM_SPECIAL_RELOCS
;
1309 /* Creates a new nios2_insn_relocS and returns a pointer to it. */
1310 static nios2_insn_relocS
*
1311 nios2_insn_reloc_new (bfd_reloc_code_real_type reloc_type
, unsigned int pcrel
)
1313 nios2_insn_relocS
*retval
;
1314 retval
= (nios2_insn_relocS
*) malloc (sizeof (nios2_insn_relocS
));
1317 as_bad (_("can't create relocation"));
1321 /* Fill out the fields with default values. */
1322 retval
->reloc_next
= NULL
;
1323 retval
->reloc_type
= reloc_type
;
1324 retval
->reloc_pcrel
= pcrel
;
1328 /* Frees up memory previously allocated by nios2_insn_reloc_new(). */
1329 /* FIXME: this is never called; memory leak? */
1332 nios2_insn_reloc_destroy (nios2_insn_relocS
*reloc
)
1334 gas_assert (reloc
!= NULL
);
1339 /* Look up a register name and validate it for the given regtype.
1340 Return the register mapping or NULL on failure. */
1341 static struct nios2_reg
*
1342 nios2_parse_reg (const char *token
, unsigned long regtype
)
1344 struct nios2_reg
*reg
= nios2_reg_lookup (token
);
1348 as_bad (_("unknown register %s"), token
);
1352 /* Matched a register, but is it the wrong type? */
1353 if (!(regtype
& reg
->regtype
))
1355 if (regtype
& REG_CONTROL
)
1356 as_bad (_("expecting control register"));
1357 else if (reg
->regtype
& REG_CONTROL
)
1358 as_bad (_("illegal use of control register"));
1359 else if (reg
->regtype
& REG_COPROCESSOR
)
1360 as_bad (_("illegal use of coprocessor register"));
1362 as_bad (_("invalid register %s"), token
);
1366 /* Warn for explicit use of special registers. */
1367 if (reg
->regtype
& REG_NORMAL
)
1369 if (!nios2_as_options
.noat
&& reg
->index
== 1)
1370 as_warn (_("Register at (r1) can sometimes be corrupted by "
1371 "assembler optimizations.\n"
1372 "Use .set noat to turn off those optimizations "
1373 "(and this warning)."));
1374 if (!nios2_as_options
.nobreak
&& reg
->index
== 25)
1375 as_warn (_("The debugger will corrupt bt (r25).\n"
1376 "If you don't need to debug this "
1377 "code use .set nobreak to turn off this warning."));
1378 if (!nios2_as_options
.nobreak
&& reg
->index
== 30)
1379 as_warn (_("The debugger will corrupt sstatus/ba (r30).\n"
1380 "If you don't need to debug this "
1381 "code use .set nobreak to turn off this warning."));
1387 /* The various nios2_assemble_* functions call this
1388 function to generate an expression from a string representing an expression.
1389 It then tries to evaluate the expression, and if it can, returns its value.
1390 If not, it creates a new nios2_insn_relocS and stores the expression and
1391 reloc_type for future use. */
1392 static unsigned long
1393 nios2_assemble_expression (const char *exprstr
,
1394 nios2_insn_infoS
*insn
,
1395 bfd_reloc_code_real_type reloc_type
,
1398 nios2_insn_relocS
*reloc
;
1399 char *saved_line_ptr
;
1400 unsigned short value
;
1403 gas_assert (exprstr
!= NULL
);
1404 gas_assert (insn
!= NULL
);
1406 /* Check for relocation operators.
1407 Change the relocation type and advance the ptr to the start of
1408 the expression proper. */
1409 for (i
= 0; i
< nios2_num_special_relocs
; i
++)
1410 if (strstr (exprstr
, nios2_special_reloc
[i
].string
) != NULL
)
1412 reloc_type
= nios2_special_reloc
[i
].reloc_type
;
1413 exprstr
+= strlen (nios2_special_reloc
[i
].string
) + 1;
1415 /* %lo and %hiadj have different meanings for PC-relative
1419 if (reloc_type
== BFD_RELOC_NIOS2_LO16
)
1420 reloc_type
= BFD_RELOC_NIOS2_PCREL_LO
;
1421 if (reloc_type
== BFD_RELOC_NIOS2_HIADJ16
)
1422 reloc_type
= BFD_RELOC_NIOS2_PCREL_HA
;
1428 /* We potentially have a relocation. */
1429 reloc
= nios2_insn_reloc_new (reloc_type
, pcrel
);
1430 reloc
->reloc_next
= insn
->insn_reloc
;
1431 insn
->insn_reloc
= reloc
;
1433 /* Parse the expression string. */
1434 saved_line_ptr
= input_line_pointer
;
1435 input_line_pointer
= (char *) exprstr
;
1436 expression (&reloc
->reloc_expression
);
1437 input_line_pointer
= saved_line_ptr
;
1439 /* This is redundant as the fixup will put this into
1440 the instruction, but it is included here so that
1441 self-test mode (-r) works. */
1443 if (nios2_mode
== NIOS2_MODE_TEST
1444 && reloc
->reloc_expression
.X_op
== O_constant
)
1445 value
= reloc
->reloc_expression
.X_add_number
;
1447 return (unsigned long) value
;
1451 /* Argument assemble functions. */
1453 nios2_assemble_arg_c (const char *token
, nios2_insn_infoS
*insn
)
1455 struct nios2_reg
*reg
= nios2_parse_reg (token
, REG_CONTROL
);
1456 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1464 insn
->insn_code
|= SET_IW_R_IMM5 (reg
->index
);
1472 nios2_assemble_arg_d (const char *token
, nios2_insn_infoS
*insn
)
1474 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1475 unsigned long regtype
= REG_NORMAL
;
1476 struct nios2_reg
*reg
;
1478 if (op
->format
== iw_custom_type
)
1479 regtype
|= REG_COPROCESSOR
;
1480 reg
= nios2_parse_reg (token
, regtype
);
1487 insn
->insn_code
|= SET_IW_R_C (reg
->index
);
1489 case iw_custom_type
:
1490 insn
->insn_code
|= SET_IW_CUSTOM_C (reg
->index
);
1491 if (reg
->regtype
& REG_COPROCESSOR
)
1492 insn
->insn_code
|= SET_IW_CUSTOM_READC (0);
1494 insn
->insn_code
|= SET_IW_CUSTOM_READC (1);
1502 nios2_assemble_arg_s (const char *token
, nios2_insn_infoS
*insn
)
1504 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1505 unsigned long regtype
= REG_NORMAL
;
1506 struct nios2_reg
*reg
;
1508 if (op
->format
== iw_custom_type
)
1509 regtype
|= REG_COPROCESSOR
;
1510 reg
= nios2_parse_reg (token
, regtype
);
1517 insn
->insn_code
|= SET_IW_R_A (reg
->index
);
1520 insn
->insn_code
|= SET_IW_I_A (reg
->index
);
1522 case iw_custom_type
:
1523 insn
->insn_code
|= SET_IW_CUSTOM_A (reg
->index
);
1524 if (reg
->regtype
& REG_COPROCESSOR
)
1525 insn
->insn_code
|= SET_IW_CUSTOM_READA (0);
1527 insn
->insn_code
|= SET_IW_CUSTOM_READA (1);
1535 nios2_assemble_arg_t (const char *token
, nios2_insn_infoS
*insn
)
1537 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1538 unsigned long regtype
= REG_NORMAL
;
1539 struct nios2_reg
*reg
;
1541 if (op
->format
== iw_custom_type
)
1542 regtype
|= REG_COPROCESSOR
;
1543 reg
= nios2_parse_reg (token
, regtype
);
1550 insn
->insn_code
|= SET_IW_R_B (reg
->index
);
1553 insn
->insn_code
|= SET_IW_I_B (reg
->index
);
1555 case iw_custom_type
:
1556 insn
->insn_code
|= SET_IW_CUSTOM_B (reg
->index
);
1557 if (reg
->regtype
& REG_COPROCESSOR
)
1558 insn
->insn_code
|= SET_IW_CUSTOM_READB (0);
1560 insn
->insn_code
|= SET_IW_CUSTOM_READB (1);
1568 nios2_assemble_arg_i (const char *token
, nios2_insn_infoS
*insn
)
1570 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1576 val
= nios2_assemble_expression (token
, insn
,
1577 BFD_RELOC_NIOS2_S16
, 0);
1578 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
1586 nios2_assemble_arg_u (const char *token
, nios2_insn_infoS
*insn
)
1588 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1594 val
= nios2_assemble_expression (token
, insn
,
1595 BFD_RELOC_NIOS2_U16
, 0);
1596 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
1604 nios2_assemble_arg_o (const char *token
, nios2_insn_infoS
*insn
)
1606 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1612 val
= nios2_assemble_expression (token
, insn
,
1613 BFD_RELOC_16_PCREL
, 1);
1614 insn
->constant_bits
|= SET_IW_I_IMM16 (val
);
1622 nios2_assemble_arg_j (const char *token
, nios2_insn_infoS
*insn
)
1624 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1630 val
= nios2_assemble_expression (token
, insn
,
1631 BFD_RELOC_NIOS2_IMM5
, 0);
1632 insn
->constant_bits
|= SET_IW_R_IMM5 (val
);
1640 nios2_assemble_arg_l (const char *token
, nios2_insn_infoS
*insn
)
1642 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1647 case iw_custom_type
:
1648 val
= nios2_assemble_expression (token
, insn
,
1649 BFD_RELOC_NIOS2_IMM8
, 0);
1650 insn
->constant_bits
|= SET_IW_CUSTOM_N (val
);
1658 nios2_assemble_arg_m (const char *token
, nios2_insn_infoS
*insn
)
1660 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1666 val
= nios2_assemble_expression (token
, insn
,
1667 (nios2_as_options
.noat
1668 ? BFD_RELOC_NIOS2_CALL26_NOAT
1669 : BFD_RELOC_NIOS2_CALL26
),
1671 insn
->constant_bits
|= SET_IW_J_IMM26 (val
);
1679 nios2_assemble_args (nios2_insn_infoS
*insn
)
1681 const struct nios2_opcode
*op
= insn
->insn_nios2_opcode
;
1683 unsigned int tokidx
, ntok
;
1685 /* Make sure there are enough arguments. */
1686 ntok
= (op
->pinfo
& NIOS2_INSN_OPTARG
) ? op
->num_args
- 1 : op
->num_args
;
1687 for (tokidx
= 1; tokidx
<= ntok
; tokidx
++)
1688 if (insn
->insn_tokens
[tokidx
] == NULL
)
1690 as_bad ("missing argument");
1694 for (argptr
= op
->args
, tokidx
= 1;
1695 *argptr
&& insn
->insn_tokens
[tokidx
];
1705 nios2_assemble_arg_c (insn
->insn_tokens
[tokidx
++], insn
);
1709 nios2_assemble_arg_d (insn
->insn_tokens
[tokidx
++], insn
);
1713 nios2_assemble_arg_s (insn
->insn_tokens
[tokidx
++], insn
);
1717 nios2_assemble_arg_t (insn
->insn_tokens
[tokidx
++], insn
);
1721 nios2_assemble_arg_i (insn
->insn_tokens
[tokidx
++], insn
);
1725 nios2_assemble_arg_u (insn
->insn_tokens
[tokidx
++], insn
);
1729 nios2_assemble_arg_o (insn
->insn_tokens
[tokidx
++], insn
);
1733 nios2_assemble_arg_j (insn
->insn_tokens
[tokidx
++], insn
);
1737 nios2_assemble_arg_l (insn
->insn_tokens
[tokidx
++], insn
);
1741 nios2_assemble_arg_m (insn
->insn_tokens
[tokidx
++], insn
);
1749 /* Perform argument checking. */
1750 nios2_check_assembly (insn
->insn_code
| insn
->constant_bits
,
1751 insn
->insn_tokens
[tokidx
]);
1755 /* The function consume_arg takes a pointer into a string
1756 of instruction tokens (args) and a pointer into a string
1757 representing the expected sequence of tokens and separators.
1758 It checks whether the first argument in argstr is of the
1759 expected type, throwing an error if it is not, and returns
1760 the pointer argstr. */
1762 nios2_consume_arg (char *argstr
, const char *parsestr
)
1778 if (nios2_special_relocation_p (argstr
))
1780 /* We zap the parentheses because we don't want them confused
1782 temp
= strchr (argstr
, '(');
1785 temp
= strchr (argstr
, ')');
1790 as_bad (_("badly formed expression near %s"), argstr
);
1796 /* We can't have %hi, %lo or %hiadj here. */
1798 as_bad (_("badly formed expression near %s"), argstr
);
1804 BAD_CASE (*parsestr
);
1811 /* The function consume_separator takes a pointer into a string
1812 of instruction tokens (args) and a pointer into a string representing
1813 the expected sequence of tokens and separators. It finds the first
1814 instance of the character pointed to by separator in argstr, and
1815 returns a pointer to the next element of argstr, which is the
1816 following token in the sequence. */
1818 nios2_consume_separator (char *argstr
, const char *separator
)
1822 /* If we have a opcode reg, expr(reg) type instruction, and
1823 * we are separating the expr from the (reg), we find the last
1824 * (, just in case the expression has parentheses. */
1826 if (*separator
== '(')
1827 p
= strrchr (argstr
, *separator
);
1829 p
= strchr (argstr
, *separator
);
1837 /* The principal argument parsing function which takes a string argstr
1838 representing the instruction arguments for insn, and extracts the argument
1839 tokens matching parsestr into parsed_args. */
1841 nios2_parse_args (nios2_insn_infoS
*insn
, char *argstr
,
1842 const char *parsestr
, char **parsed_args
)
1849 bfd_boolean terminate
= FALSE
;
1851 /* This rest of this function is it too fragile and it mostly works,
1852 therefore special case this one. */
1853 if (*parsestr
== 0 && argstr
!= 0)
1855 as_bad (_("too many arguments"));
1856 parsed_args
[0] = NULL
;
1860 while (p
!= NULL
&& !terminate
&& i
< NIOS2_MAX_INSN_TOKENS
)
1862 parsed_args
[i
] = nios2_consume_arg (p
, parsestr
);
1864 while (*parsestr
== '(' || *parsestr
== ')' || *parsestr
== ',')
1867 p
= nios2_consume_separator (p
, parsestr
);
1868 /* Check for missing separators. */
1869 if (!p
&& !(insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_OPTARG
))
1871 as_bad (_("expecting %c near %s"), *parsestr
, context
);
1877 if (*parsestr
== '\0')
1879 /* Check that the argument string has no trailing arguments. */
1880 end
= strpbrk (p
, ",");
1882 as_bad (_("too many arguments"));
1885 if (*parsestr
== '\0' || (p
!= NULL
&& *p
== '\0'))
1890 parsed_args
[i
] = NULL
;
1895 /** Support for pseudo-op parsing. These are macro-like opcodes that
1896 expand into real insns by suitable fiddling with the operands. */
1898 /* Append the string modifier to the string contained in the argument at
1899 parsed_args[ndx]. */
1901 nios2_modify_arg (char **parsed_args
, const char *modifier
,
1902 int unused ATTRIBUTE_UNUSED
, int ndx
)
1904 char *tmp
= parsed_args
[ndx
];
1907 = (char *) malloc (strlen (parsed_args
[ndx
]) + strlen (modifier
) + 1);
1908 strcpy (parsed_args
[ndx
], tmp
);
1909 strcat (parsed_args
[ndx
], modifier
);
1912 /* Modify parsed_args[ndx] by negating that argument. */
1914 nios2_negate_arg (char **parsed_args
, const char *modifier ATTRIBUTE_UNUSED
,
1915 int unused ATTRIBUTE_UNUSED
, int ndx
)
1917 char *tmp
= parsed_args
[ndx
];
1920 = (char *) malloc (strlen ("~(") + strlen (parsed_args
[ndx
]) +
1921 strlen (")+1") + 1);
1923 strcpy (parsed_args
[ndx
], "~(");
1924 strcat (parsed_args
[ndx
], tmp
);
1925 strcat (parsed_args
[ndx
], ")+1");
1928 /* The function nios2_swap_args swaps the pointers at indices index_1 and
1929 index_2 in the array parsed_args[] - this is used for operand swapping
1930 for comparison operations. */
1932 nios2_swap_args (char **parsed_args
, const char *unused ATTRIBUTE_UNUSED
,
1933 int index_1
, int index_2
)
1936 gas_assert (index_1
< NIOS2_MAX_INSN_TOKENS
1937 && index_2
< NIOS2_MAX_INSN_TOKENS
);
1938 tmp
= parsed_args
[index_1
];
1939 parsed_args
[index_1
] = parsed_args
[index_2
];
1940 parsed_args
[index_2
] = tmp
;
1943 /* This function appends the string appnd to the array of strings in
1944 parsed_args num times starting at index start in the array. */
1946 nios2_append_arg (char **parsed_args
, const char *appnd
, int num
,
1952 gas_assert ((start
+ num
) < NIOS2_MAX_INSN_TOKENS
);
1954 if (nios2_mode
== NIOS2_MODE_TEST
)
1955 tmp
= parsed_args
[start
];
1959 for (i
= start
, count
= num
; count
> 0; ++i
, --count
)
1960 parsed_args
[i
] = (char *) appnd
;
1962 gas_assert (i
== (start
+ num
));
1963 parsed_args
[i
] = tmp
;
1964 parsed_args
[i
+ 1] = NULL
;
1967 /* This function inserts the string insert num times in the array
1968 parsed_args, starting at the index start. */
1970 nios2_insert_arg (char **parsed_args
, const char *insert
, int num
,
1975 gas_assert ((start
+ num
) < NIOS2_MAX_INSN_TOKENS
);
1977 /* Move the existing arguments up to create space. */
1978 for (i
= NIOS2_MAX_INSN_TOKENS
; i
- num
>= start
; --i
)
1979 parsed_args
[i
] = parsed_args
[i
- num
];
1981 for (i
= start
, count
= num
; count
> 0; ++i
, --count
)
1982 parsed_args
[i
] = (char *) insert
;
1985 /* Cleanup function to free malloc'ed arg strings. */
1987 nios2_free_arg (char **parsed_args
, int num ATTRIBUTE_UNUSED
, int start
)
1989 if (parsed_args
[start
])
1991 free (parsed_args
[start
]);
1992 parsed_args
[start
] = NULL
;
1996 /* This function swaps the pseudo-op for a real op. */
1997 static nios2_ps_insn_infoS
*
1998 nios2_translate_pseudo_insn (nios2_insn_infoS
*insn
)
2001 nios2_ps_insn_infoS
*ps_insn
;
2003 /* Find which real insn the pseudo-op transates to and
2004 switch the insn_info ptr to point to it. */
2005 ps_insn
= nios2_ps_lookup (insn
->insn_nios2_opcode
->name
);
2007 if (ps_insn
!= NULL
)
2009 insn
->insn_nios2_opcode
= nios2_opcode_lookup (ps_insn
->insn
);
2010 insn
->insn_tokens
[0] = insn
->insn_nios2_opcode
->name
;
2011 /* Modify the args so they work with the real insn. */
2012 ps_insn
->arg_modifer_func ((char **) insn
->insn_tokens
,
2013 ps_insn
->arg_modifier
, ps_insn
->num
,
2017 /* we cannot recover from this. */
2018 as_fatal (_("unrecognized pseudo-instruction %s"),
2019 ps_insn
->pseudo_insn
);
2023 /* Invoke the cleanup handler for pseudo-insn ps_insn on insn. */
2025 nios2_cleanup_pseudo_insn (nios2_insn_infoS
*insn
,
2026 nios2_ps_insn_infoS
*ps_insn
)
2028 if (ps_insn
->arg_cleanup_func
)
2029 (ps_insn
->arg_cleanup_func
) ((char **) insn
->insn_tokens
,
2030 ps_insn
->num
, ps_insn
->index
);
2033 const nios2_ps_insn_infoS nios2_ps_insn_info_structs
[] = {
2034 /* pseudo-op, real-op, arg, arg_modifier_func, num, index, arg_cleanup_func */
2035 {"mov", "add", nios2_append_arg
, "zero", 1, 3, NULL
},
2036 {"movi", "addi", nios2_insert_arg
, "zero", 1, 2, NULL
},
2037 {"movhi", "orhi", nios2_insert_arg
, "zero", 1, 2, NULL
},
2038 {"movui", "ori", nios2_insert_arg
, "zero", 1, 2, NULL
},
2039 {"movia", "orhi", nios2_insert_arg
, "zero", 1, 2, NULL
},
2040 {"nop", "add", nios2_append_arg
, "zero", 3, 1, NULL
},
2041 {"bgt", "blt", nios2_swap_args
, "", 1, 2, NULL
},
2042 {"bgtu", "bltu", nios2_swap_args
, "", 1, 2, NULL
},
2043 {"ble", "bge", nios2_swap_args
, "", 1, 2, NULL
},
2044 {"bleu", "bgeu", nios2_swap_args
, "", 1, 2, NULL
},
2045 {"cmpgt", "cmplt", nios2_swap_args
, "", 2, 3, NULL
},
2046 {"cmpgtu", "cmpltu", nios2_swap_args
, "", 2, 3, NULL
},
2047 {"cmple", "cmpge", nios2_swap_args
, "", 2, 3, NULL
},
2048 {"cmpleu", "cmpgeu", nios2_swap_args
, "", 2, 3, NULL
},
2049 {"cmpgti", "cmpgei", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2050 {"cmpgtui", "cmpgeui", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2051 {"cmplei", "cmplti", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2052 {"cmpleui", "cmpltui", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2053 {"subi", "addi", nios2_negate_arg
, "", 0, 3, nios2_free_arg
}
2054 /* Add further pseudo-ops here. */
2057 #define NIOS2_NUM_PSEUDO_INSNS \
2058 ((sizeof(nios2_ps_insn_info_structs)/ \
2059 sizeof(nios2_ps_insn_info_structs[0])))
2060 const int nios2_num_ps_insn_info_structs
= NIOS2_NUM_PSEUDO_INSNS
;
2063 /** Assembler output support. */
2065 /* Output a normal instruction. */
2067 output_insn (nios2_insn_infoS
*insn
)
2070 nios2_insn_relocS
*reloc
;
2071 f
= frag_more (insn
->insn_nios2_opcode
->size
);
2072 /* This allocates enough space for the instruction
2073 and puts it in the current frag. */
2074 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
2075 /* Emit debug info. */
2076 dwarf2_emit_insn (insn
->insn_nios2_opcode
->size
);
2077 /* Create any fixups to be acted on later. */
2079 for (reloc
= insn
->insn_reloc
; reloc
!= NULL
; reloc
= reloc
->reloc_next
)
2080 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
,
2081 insn
->insn_nios2_opcode
->size
,
2082 &reloc
->reloc_expression
, reloc
->reloc_pcrel
,
2086 /* Output an unconditional branch. */
2088 output_ubranch (nios2_insn_infoS
*insn
)
2090 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2092 /* If the reloc is NULL, there was an error assembling the branch. */
2095 symbolS
*symp
= reloc
->reloc_expression
.X_add_symbol
;
2096 offsetT offset
= reloc
->reloc_expression
.X_add_number
;
2099 /* Tag dwarf2 debug info to the address at the start of the insn.
2100 We must do it before frag_var() below closes off the frag. */
2101 dwarf2_emit_insn (0);
2103 /* We create a machine dependent frag which can grow
2104 to accommodate the largest possible instruction sequence
2105 this may generate. */
2106 f
= frag_var (rs_machine_dependent
,
2107 UBRANCH_MAX_SIZE
, insn
->insn_nios2_opcode
->size
,
2108 UBRANCH_SUBTYPE (0), symp
, offset
, NULL
);
2110 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
2112 /* We leave fixup generation to md_convert_frag. */
2116 /* Output a conditional branch. */
2118 output_cbranch (nios2_insn_infoS
*insn
)
2120 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2122 /* If the reloc is NULL, there was an error assembling the branch. */
2125 symbolS
*symp
= reloc
->reloc_expression
.X_add_symbol
;
2126 offsetT offset
= reloc
->reloc_expression
.X_add_number
;
2129 /* Tag dwarf2 debug info to the address at the start of the insn.
2130 We must do it before frag_var() below closes off the frag. */
2131 dwarf2_emit_insn (0);
2133 /* We create a machine dependent frag which can grow
2134 to accommodate the largest possible instruction sequence
2135 this may generate. */
2136 f
= frag_var (rs_machine_dependent
,
2137 CBRANCH_MAX_SIZE
, insn
->insn_nios2_opcode
->size
,
2138 CBRANCH_SUBTYPE (0), symp
, offset
, NULL
);
2140 md_number_to_chars (f
, insn
->insn_code
, insn
->insn_nios2_opcode
->size
);
2142 /* We leave fixup generation to md_convert_frag. */
2146 /* Output a call sequence. Since calls are not pc-relative for NIOS2,
2147 but are page-relative, we cannot tell at any stage in assembly
2148 whether a call will be out of range since a section may be linked
2149 at any address. So if we are relaxing, we convert all call instructions
2150 to long call sequences, and rely on the linker to relax them back to
2153 output_call (nios2_insn_infoS
*insn
)
2155 /* This allocates enough space for the instruction
2156 and puts it in the current frag. */
2157 char *f
= frag_more (12);
2158 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2160 md_number_to_chars (f
,
2161 (MATCH_R1_ORHI
| SET_IW_I_B (AT_REGNUM
)
2164 dwarf2_emit_insn (4);
2165 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
, 4,
2166 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_HI16
);
2167 md_number_to_chars (f
+ 4,
2168 (MATCH_R1_ORI
| SET_IW_I_B (AT_REGNUM
)
2169 | SET_IW_I_A (AT_REGNUM
)),
2171 dwarf2_emit_insn (4);
2172 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ 4, 4,
2173 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_LO16
);
2174 md_number_to_chars (f
+ 8, MATCH_R1_CALLR
| SET_IW_R_A (AT_REGNUM
), 4);
2175 dwarf2_emit_insn (4);
2178 /* Output a movhi/addi pair for the movia pseudo-op. */
2180 output_movia (nios2_insn_infoS
*insn
)
2182 /* This allocates enough space for the instruction
2183 and puts it in the current frag. */
2184 char *f
= frag_more (8);
2185 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2186 unsigned long reg_index
= GET_IW_I_B (insn
->insn_code
);
2188 /* If the reloc is NULL, there was an error assembling the movia. */
2191 md_number_to_chars (f
, insn
->insn_code
, 4);
2192 dwarf2_emit_insn (4);
2193 fix_new (frag_now
, f
- frag_now
->fr_literal
, 4,
2194 reloc
->reloc_expression
.X_add_symbol
,
2195 reloc
->reloc_expression
.X_add_number
, 0,
2196 BFD_RELOC_NIOS2_HIADJ16
);
2197 md_number_to_chars (f
+ 4,
2198 (MATCH_R1_ADDI
| SET_IW_I_A (reg_index
)
2199 | SET_IW_I_B (reg_index
)),
2201 dwarf2_emit_insn (4);
2202 fix_new (frag_now
, f
+ 4 - frag_now
->fr_literal
, 4,
2203 reloc
->reloc_expression
.X_add_symbol
,
2204 reloc
->reloc_expression
.X_add_number
, 0, BFD_RELOC_NIOS2_LO16
);
2210 /** External interfaces. */
2212 /* The following functions are called by machine-independent parts of
2215 md_parse_option (int c
, char *arg ATTRIBUTE_UNUSED
)
2220 /* Hidden option for self-test mode. */
2221 nios2_mode
= NIOS2_MODE_TEST
;
2223 case OPTION_RELAX_ALL
:
2224 nios2_as_options
.relax
= relax_all
;
2226 case OPTION_NORELAX
:
2227 nios2_as_options
.relax
= relax_none
;
2229 case OPTION_RELAX_SECTION
:
2230 nios2_as_options
.relax
= relax_section
;
2233 target_big_endian
= 1;
2236 target_big_endian
= 0;
2246 /* Implement TARGET_FORMAT. We can choose to be big-endian or
2247 little-endian at runtime based on a switch. */
2249 nios2_target_format (void)
2251 return target_big_endian
? "elf32-bignios2" : "elf32-littlenios2";
2254 /* Machine-dependent usage message. */
2256 md_show_usage (FILE *stream
)
2258 fprintf (stream
, " NIOS2 options:\n"
2259 " -relax-all replace all branch and call "
2260 "instructions with jmp and callr sequences\n"
2261 " -relax-section replace identified out of range "
2262 "branches with jmp sequences (default)\n"
2263 " -no-relax do not replace any branches or calls\n"
2264 " -EB force big-endian byte ordering\n"
2265 " -EL force little-endian byte ordering\n");
2268 /* This function is called once, at assembler startup time.
2269 It should set up all the tables, etc. that the MD part of the
2270 assembler will need. */
2275 const char *inserted
;
2277 /* Create and fill a hashtable for the Nios II opcodes, registers and
2279 nios2_opcode_hash
= hash_new ();
2280 nios2_reg_hash
= hash_new ();
2281 nios2_ps_hash
= hash_new ();
2283 for (i
= 0; i
< nios2_num_opcodes
; ++i
)
2286 = hash_insert (nios2_opcode_hash
, nios2_opcodes
[i
].name
,
2287 (PTR
) & nios2_opcodes
[i
]);
2288 if (inserted
!= NULL
)
2290 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
2291 nios2_opcodes
[i
].name
, inserted
);
2292 /* Probably a memory allocation problem? Give up now. */
2293 as_fatal (_("Broken assembler. No assembly attempted."));
2297 for (i
= 0; i
< nios2_num_regs
; ++i
)
2300 = hash_insert (nios2_reg_hash
, nios2_regs
[i
].name
,
2301 (PTR
) & nios2_regs
[i
]);
2302 if (inserted
!= NULL
)
2304 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
2305 nios2_regs
[i
].name
, inserted
);
2306 /* Probably a memory allocation problem? Give up now. */
2307 as_fatal (_("Broken assembler. No assembly attempted."));
2312 for (i
= 0; i
< nios2_num_ps_insn_info_structs
; ++i
)
2315 = hash_insert (nios2_ps_hash
, nios2_ps_insn_info_structs
[i
].pseudo_insn
,
2316 (PTR
) & nios2_ps_insn_info_structs
[i
]);
2317 if (inserted
!= NULL
)
2319 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
2320 nios2_ps_insn_info_structs
[i
].pseudo_insn
, inserted
);
2321 /* Probably a memory allocation problem? Give up now. */
2322 as_fatal (_("Broken assembler. No assembly attempted."));
2326 /* Assembler option defaults. */
2327 nios2_as_options
.noat
= FALSE
;
2328 nios2_as_options
.nobreak
= FALSE
;
2330 /* Debug information is incompatible with relaxation. */
2331 if (debug_type
!= DEBUG_UNSPECIFIED
)
2332 nios2_as_options
.relax
= relax_none
;
2334 /* Initialize the alignment data. */
2335 nios2_current_align_seg
= now_seg
;
2336 nios2_last_label
= NULL
;
2337 nios2_current_align
= 0;
2341 /* Assembles a single line of Nios II assembly language. */
2343 md_assemble (char *op_str
)
2346 char *op_strdup
= NULL
;
2347 unsigned long saved_pinfo
= 0;
2348 nios2_insn_infoS thisinsn
;
2349 nios2_insn_infoS
*insn
= &thisinsn
;
2351 /* Make sure we are aligned on a 4-byte boundary. */
2352 if (nios2_current_align
< 2)
2353 nios2_align (2, NULL
, nios2_last_label
);
2354 else if (nios2_current_align
> 2)
2355 nios2_current_align
= 2;
2356 nios2_last_label
= NULL
;
2358 /* We don't want to clobber to op_str
2359 because we want to be able to use it in messages. */
2360 op_strdup
= strdup (op_str
);
2361 insn
->insn_tokens
[0] = strtok (op_strdup
, " ");
2362 argstr
= strtok (NULL
, "");
2364 /* Assemble the opcode. */
2365 insn
->insn_nios2_opcode
= nios2_opcode_lookup (insn
->insn_tokens
[0]);
2366 insn
->insn_reloc
= NULL
;
2368 if (insn
->insn_nios2_opcode
!= NULL
)
2370 nios2_ps_insn_infoS
*ps_insn
= NULL
;
2371 /* Set the opcode for the instruction. */
2372 insn
->insn_code
= insn
->insn_nios2_opcode
->match
;
2373 insn
->constant_bits
= 0;
2375 /* Parse the arguments pointed to by argstr. */
2376 if (nios2_mode
== NIOS2_MODE_ASSEMBLE
)
2377 nios2_parse_args (insn
, argstr
, insn
->insn_nios2_opcode
->args
,
2378 (char **) &insn
->insn_tokens
[1]);
2380 nios2_parse_args (insn
, argstr
, insn
->insn_nios2_opcode
->args_test
,
2381 (char **) &insn
->insn_tokens
[1]);
2383 /* We need to preserve the MOVIA macro as this is clobbered by
2384 translate_pseudo_insn. */
2385 if (insn
->insn_nios2_opcode
->pinfo
== NIOS2_INSN_MACRO_MOVIA
)
2386 saved_pinfo
= NIOS2_INSN_MACRO_MOVIA
;
2387 /* If the instruction is an pseudo-instruction, we want to replace it
2388 with its real equivalent, and then continue. */
2389 if ((insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_MACRO
)
2390 == NIOS2_INSN_MACRO
)
2391 ps_insn
= nios2_translate_pseudo_insn (insn
);
2393 /* Assemble the parsed arguments into the instruction word. */
2394 nios2_assemble_args (insn
);
2396 /* Handle relaxation and other transformations. */
2397 if (nios2_as_options
.relax
!= relax_none
2398 && !nios2_as_options
.noat
2399 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_UBRANCH
)
2400 output_ubranch (insn
);
2401 else if (nios2_as_options
.relax
!= relax_none
2402 && !nios2_as_options
.noat
2403 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_CBRANCH
)
2404 output_cbranch (insn
);
2405 else if (nios2_as_options
.relax
== relax_all
2406 && !nios2_as_options
.noat
2407 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_CALL
2409 && ((insn
->insn_reloc
->reloc_type
2410 == BFD_RELOC_NIOS2_CALL26
)
2411 || (insn
->insn_reloc
->reloc_type
2412 == BFD_RELOC_NIOS2_CALL26_NOAT
)))
2414 else if (saved_pinfo
== NIOS2_INSN_MACRO_MOVIA
)
2415 output_movia (insn
);
2419 nios2_cleanup_pseudo_insn (insn
, ps_insn
);
2422 /* Unrecognised instruction - error. */
2423 as_bad (_("unrecognised instruction %s"), insn
->insn_tokens
[0]);
2425 /* Don't leak memory. */
2429 /* Round up section size. */
2431 md_section_align (asection
*seg ATTRIBUTE_UNUSED
, valueT size
)
2433 /* I think byte alignment is fine here. */
2437 /* Implement TC_FORCE_RELOCATION. */
2439 nios2_force_relocation (fixS
*fixp
)
2441 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
2442 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
2443 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_ALIGN
)
2446 return generic_force_reloc (fixp
);
2449 /* Implement tc_fix_adjustable. */
2451 nios2_fix_adjustable (fixS
*fixp
)
2453 if (fixp
->fx_addsy
== NULL
)
2457 /* Prevent all adjustments to global symbols. */
2458 if (OUTPUT_FLAVOR
== bfd_target_elf_flavour
2459 && (S_IS_EXTERNAL (fixp
->fx_addsy
) || S_IS_WEAK (fixp
->fx_addsy
)))
2462 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
2463 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
2466 /* Preserve relocations against symbols with function type. */
2467 if (symbol_get_bfdsym (fixp
->fx_addsy
)->flags
& BSF_FUNCTION
)
2470 /* Don't allow symbols to be discarded on GOT related relocs. */
2471 if (fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT16
2472 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL16
2473 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_LO
2474 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_HA
2475 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_GD16
2476 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDM16
2477 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDO16
2478 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_IE16
2479 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LE16
2480 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPMOD
2481 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPREL
2482 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_TPREL
2483 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF
2484 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT_LO
2485 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT_HA
2486 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL_LO
2487 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL_HA
2494 /* Implement tc_frob_symbol. This is called in adjust_reloc_syms;
2495 it is used to remove *ABS* references from the symbol table. */
2497 nios2_frob_symbol (symbolS
*symp
)
2499 if ((OUTPUT_FLAVOR
== bfd_target_elf_flavour
2500 && symp
== section_symbol (absolute_section
))
2501 || !S_IS_DEFINED (symp
))
2507 /* The function tc_gen_reloc creates a relocation structure for the
2508 fixup fixp, and returns a pointer to it. This structure is passed
2509 to bfd_install_relocation so that it can be written to the object
2510 file for linking. */
2512 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
2514 arelent
*reloc
= (arelent
*) xmalloc (sizeof (arelent
));
2515 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
2516 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
2518 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
2519 reloc
->addend
= fixp
->fx_offset
; /* fixp->fx_addnumber; */
2523 switch (fixp
->fx_r_type
)
2526 fixp
->fx_r_type
= BFD_RELOC_16_PCREL
;
2528 case BFD_RELOC_NIOS2_LO16
:
2529 fixp
->fx_r_type
= BFD_RELOC_NIOS2_PCREL_LO
;
2531 case BFD_RELOC_NIOS2_HIADJ16
:
2532 fixp
->fx_r_type
= BFD_RELOC_NIOS2_PCREL_HA
;
2539 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
2540 if (reloc
->howto
== NULL
)
2542 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2543 _("can't represent relocation type %s"),
2544 bfd_get_reloc_code_name (fixp
->fx_r_type
));
2546 /* Set howto to a garbage value so that we can keep going. */
2547 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, BFD_RELOC_32
);
2548 gas_assert (reloc
->howto
!= NULL
);
2554 md_pcrel_from (fixS
*fixP ATTRIBUTE_UNUSED
)
2559 /* Called just before the assembler exits. */
2563 /* FIXME - not yet implemented */
2566 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
2567 Otherwise we have no need to default values of symbols. */
2569 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
2572 if (name
[0] == '_' && name
[1] == 'G'
2573 && strcmp (name
, GLOBAL_OFFSET_TABLE_NAME
) == 0)
2577 if (symbol_find (name
))
2578 as_bad ("GOT already in the symbol table");
2580 GOT_symbol
= symbol_new (name
, undefined_section
,
2581 (valueT
) 0, &zero_address_frag
);
2591 /* Implement tc_frob_label. */
2593 nios2_frob_label (symbolS
*lab
)
2595 /* Emit dwarf information. */
2596 dwarf2_emit_label (lab
);
2598 /* Update the label's address with the current output pointer. */
2599 symbol_set_frag (lab
, frag_now
);
2600 S_SET_VALUE (lab
, (valueT
) frag_now_fix ());
2602 /* Record this label for future adjustment after we find out what
2603 kind of data it references, and the required alignment therewith. */
2604 nios2_last_label
= lab
;
2607 /* Implement md_cons_align. */
2609 nios2_cons_align (int size
)
2612 const char *pfill
= NULL
;
2614 while ((size
>>= 1) != 0)
2617 if (subseg_text_p (now_seg
))
2618 pfill
= (const char *) &nop
;
2622 if (nios2_auto_align_on
)
2623 nios2_align (log_size
, pfill
, NULL
);
2625 nios2_last_label
= NULL
;
2628 /* Map 's' to SHF_NIOS2_GPREL. */
2629 /* This is from the Alpha code tc-alpha.c. */
2631 nios2_elf_section_letter (int letter
, char **ptr_msg
)
2634 return SHF_NIOS2_GPREL
;
2636 *ptr_msg
= _("Bad .section directive: want a,s,w,x,M,S,G,T in string");
2640 /* Map SHF_ALPHA_GPREL to SEC_SMALL_DATA. */
2641 /* This is from the Alpha code tc-alpha.c. */
2643 nios2_elf_section_flags (flagword flags
, int attr
, int type ATTRIBUTE_UNUSED
)
2645 if (attr
& SHF_NIOS2_GPREL
)
2646 flags
|= SEC_SMALL_DATA
;
2650 /* Implement TC_PARSE_CONS_EXPRESSION to handle %tls_ldo(...) */
2651 bfd_reloc_code_real_type
2652 nios2_cons (expressionS
*exp
, int size
)
2654 bfd_reloc_code_real_type nios2_tls_ldo_reloc
= BFD_RELOC_NONE
;
2657 if (input_line_pointer
[0] == '%')
2659 if (strprefix (input_line_pointer
+ 1, "tls_ldo"))
2662 as_bad (_("Illegal operands: %%tls_ldo in %d-byte data field"),
2666 input_line_pointer
+= 8;
2667 nios2_tls_ldo_reloc
= BFD_RELOC_NIOS2_TLS_DTPREL
;
2670 if (nios2_tls_ldo_reloc
!= BFD_RELOC_NONE
)
2673 if (input_line_pointer
[0] != '(')
2674 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2678 char *end
= ++input_line_pointer
;
2681 for (c
= *end
; !is_end_of_line
[c
]; end
++, c
= *end
)
2692 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2698 if (input_line_pointer
!= end
)
2699 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2702 input_line_pointer
++;
2704 c
= *input_line_pointer
;
2705 if (! is_end_of_line
[c
] && c
!= ',')
2706 as_bad (_("Illegal operands: garbage after %%tls_ldo()"));
2712 if (nios2_tls_ldo_reloc
== BFD_RELOC_NONE
)
2714 return nios2_tls_ldo_reloc
;
2717 /* Implement HANDLE_ALIGN. */
2719 nios2_handle_align (fragS
*fragp
)
2721 /* If we are expecting to relax in the linker, then we must output a
2722 relocation to tell the linker we are aligning code. */
2723 if (nios2_as_options
.relax
== relax_all
2724 && (fragp
->fr_type
== rs_align
|| fragp
->fr_type
== rs_align_code
)
2725 && fragp
->fr_address
+ fragp
->fr_fix
> 0
2726 && fragp
->fr_offset
> 1
2727 && now_seg
!= bss_section
)
2728 fix_new (fragp
, fragp
->fr_fix
, 0, &abs_symbol
, fragp
->fr_offset
, 0,
2729 BFD_RELOC_NIOS2_ALIGN
);
2732 /* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
2735 nios2_regname_to_dw2regnum (char *regname
)
2737 struct nios2_reg
*r
= nios2_reg_lookup (regname
);
2743 /* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
2744 unwind information for this procedure. */
2746 nios2_frame_initial_instructions (void)
2748 cfi_add_CFA_def_cfa (27, 0);