1 /* Altera Nios II assembler.
2 Copyright (C) 2012, 2013 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
;
142 /* Pointer to the relevant bit of the opcode table. */
143 const struct nios2_opcode
*insn_nios2_opcode
;
144 /* After parsing ptrs to the tokens in the instruction fill this array
145 it is terminated with a null pointer (hence the first +1).
146 The second +1 is because in some parts of the code the opcode
147 is not counted as a token, but still placed in this array. */
148 const char *insn_tokens
[NIOS2_MAX_INSN_TOKENS
+ 1 + 1];
150 /* This holds information used to generate fixups
151 and eventually relocations if it is not null. */
152 nios2_insn_relocS
*insn_reloc
;
155 /* This struct associates an argument assemble function with
156 an argument syntax string. Used by the assembler to find out
157 how to parse and assemble a set of instruction operands and
158 return the instruction field values. */
159 typedef struct nios2_arg_info
162 void (*assemble_args_func
) (nios2_insn_infoS
*insn_info
);
165 /* This struct is used to convert Nios II pseudo-ops into the
166 corresponding real op. */
167 typedef struct nios2_ps_insn_info
169 /* Map this pseudo_op... */
170 const char *pseudo_insn
;
172 /* ...to this real instruction. */
175 /* Call this function to modify the operands.... */
176 void (*arg_modifer_func
) (char ** parsed_args
, const char *arg
, int num
,
179 /* ...with these arguments. */
180 const char *arg_modifier
;
184 /* If arg_modifier_func allocates new memory, provide this function
185 to free it afterwards. */
186 void (*arg_cleanup_func
) (char **parsed_args
, int num
, int start
);
187 } nios2_ps_insn_infoS
;
189 /* Opcode hash table. */
190 static struct hash_control
*nios2_opcode_hash
= NULL
;
191 #define nios2_opcode_lookup(NAME) \
192 ((struct nios2_opcode *) hash_find (nios2_opcode_hash, (NAME)))
194 /* Register hash table. */
195 static struct hash_control
*nios2_reg_hash
= NULL
;
196 #define nios2_reg_lookup(NAME) \
197 ((struct nios2_reg *) hash_find (nios2_reg_hash, (NAME)))
199 /* Parse args hash table. */
200 static struct hash_control
*nios2_arg_hash
= NULL
;
201 #define nios2_arg_lookup(NAME) \
202 ((nios2_arg_infoS *) hash_find (nios2_arg_hash, (NAME)))
204 /* Pseudo-op hash table. */
205 static struct hash_control
*nios2_ps_hash
= NULL
;
206 #define nios2_ps_lookup(NAME) \
207 ((nios2_ps_insn_infoS *) hash_find (nios2_ps_hash, (NAME)))
209 /* The known current alignment of the current section. */
210 static int nios2_current_align
;
211 static segT nios2_current_align_seg
;
213 static int nios2_auto_align_on
= 1;
215 /* The last seen label in the current section. This is used to auto-align
216 labels preceeding instructions. */
217 static symbolS
*nios2_last_label
;
220 /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
225 /** Utility routines. */
226 /* Function md_chars_to_number takes the sequence of
227 bytes in buf and returns the corresponding value
228 in an int. n must be 1, 2 or 4. */
230 md_chars_to_number (char *buf
, int n
)
235 gas_assert (n
== 1 || n
== 2 || n
== 4);
238 if (target_big_endian
)
239 for (i
= 0; i
< n
; ++i
)
240 val
= val
| ((buf
[i
] & 0xff) << 8 * (n
- (i
+ 1)));
242 for (i
= 0; i
< n
; ++i
)
243 val
= val
| ((buf
[i
] & 0xff) << 8 * i
);
248 /* This function turns a C long int, short int or char
249 into the series of bytes that represent the number
250 on the target machine. */
252 md_number_to_chars (char *buf
, valueT val
, int n
)
254 gas_assert (n
== 1 || n
== 2 || n
== 4 || n
== 8);
255 if (target_big_endian
)
256 number_to_chars_bigendian (buf
, val
, n
);
258 number_to_chars_littleendian (buf
, val
, n
);
261 /* Turn a string in input_line_pointer into a floating point constant
262 of type TYPE, and store the appropriate bytes in *LITP. The number
263 of LITTLENUMS emitted is stored in *SIZEP. An error message is
264 returned, or NULL on OK. */
266 md_atof (int type
, char *litP
, int *sizeP
)
269 LITTLENUM_TYPE words
[4];
283 return _("bad call to md_atof");
286 t
= atof_ieee (input_line_pointer
, type
, words
);
288 input_line_pointer
= t
;
292 if (! target_big_endian
)
293 for (i
= prec
- 1; i
>= 0; i
--, litP
+= 2)
294 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
296 for (i
= 0; i
< prec
; i
++, litP
+= 2)
297 md_number_to_chars (litP
, (valueT
) words
[i
], 2);
302 /* Return true if STR starts with PREFIX, which should be a string literal. */
303 #define strprefix(STR, PREFIX) \
304 (strncmp ((STR), PREFIX, strlen (PREFIX)) == 0)
306 /* Return true if STR is prefixed with a control register name. */
308 nios2_control_register_arg_p (const char *str
)
310 return (strprefix (str
, "ctl")
311 || strprefix (str
, "cpuid")
312 || strprefix (str
, "status")
313 || strprefix (str
, "estatus")
314 || strprefix (str
, "bstatus")
315 || strprefix (str
, "ienable")
316 || strprefix (str
, "ipending")
317 || strprefix (str
, "exception")
318 || strprefix (str
, "pteaddr")
319 || strprefix (str
, "tlbacc")
320 || strprefix (str
, "tlbmisc")
321 || strprefix (str
, "eccinj")
322 || strprefix (str
, "config")
323 || strprefix (str
, "mpubase")
324 || strprefix (str
, "mpuacc")
325 || strprefix (str
, "badaddr"));
328 /* Return true if STR is prefixed with a special relocation operator. */
330 nios2_special_relocation_p (const char *str
)
332 return (strprefix (str
, "%lo")
333 || strprefix (str
, "%hi")
334 || strprefix (str
, "%hiadj")
335 || strprefix (str
, "%gprel")
336 || strprefix (str
, "%got")
337 || strprefix (str
, "%call")
338 || strprefix (str
, "%gotoff_lo")
339 || strprefix (str
, "%gotoff_hiadj")
340 || strprefix (str
, "%tls_gd")
341 || strprefix (str
, "%tls_ldm")
342 || strprefix (str
, "%tls_ldo")
343 || strprefix (str
, "%tls_ie")
344 || strprefix (str
, "%tls_le")
345 || strprefix (str
, "%gotoff"));
348 /* Checks whether the register name is a coprocessor
349 register - returns TRUE if it is, FALSE otherwise. */
351 nios2_coproc_reg (const char *reg_name
)
353 gas_assert (reg_name
!= NULL
);
355 /* Check that we do have a valid register name and that it is a
356 coprocessor register.
357 It must begin with c, not be a control register, and be a valid
359 if (strprefix (reg_name
, "c")
360 && !strprefix (reg_name
, "ctl")
361 && hash_find (nios2_reg_hash
, reg_name
) != NULL
)
367 /* nop fill pattern for text section. */
368 static char const nop
[4] = { 0x3a, 0x88, 0x01, 0x00 };
370 /* Handles all machine-dependent alignment needs. */
372 nios2_align (int log_size
, const char *pfill
, symbolS
*label
)
375 long max_alignment
= 15;
377 /* The front end is prone to changing segments out from under us
378 temporarily when -g is in effect. */
379 int switched_seg_p
= (nios2_current_align_seg
!= now_seg
);
382 if (align
> max_alignment
)
384 align
= max_alignment
;
385 as_bad (_("Alignment too large: %d. assumed"), align
);
389 as_warn (_("Alignment negative: 0 assumed"));
395 if (subseg_text_p (now_seg
) && align
>= 2)
397 /* First, make sure we're on a four-byte boundary, in case
398 someone has been putting .byte values the text section. */
399 if (nios2_current_align
< 2 || switched_seg_p
)
400 frag_align (2, 0, 0);
402 /* Now fill in the alignment pattern. */
404 frag_align_pattern (align
, pfill
, sizeof nop
, 0);
406 frag_align (align
, 0, 0);
409 frag_align (align
, 0, 0);
412 nios2_current_align
= align
;
414 /* If the last label was in a different section we can't align it. */
415 if (label
!= NULL
&& !switched_seg_p
)
418 int label_seen
= FALSE
;
419 struct frag
*old_frag
;
423 gas_assert (S_GET_SEGMENT (label
) == now_seg
);
425 old_frag
= symbol_get_frag (label
);
426 old_value
= S_GET_VALUE (label
);
427 new_value
= (valueT
) frag_now_fix ();
429 /* It is possible to have more than one label at a particular
430 address, especially if debugging is enabled, so we must
431 take care to adjust all the labels at this address in this
432 fragment. To save time we search from the end of the symbol
433 list, backwards, since the symbols we are interested in are
434 almost certainly the ones that were most recently added.
435 Also to save time we stop searching once we have seen at least
436 one matching label, and we encounter a label that is no longer
437 in the target fragment. Note, this search is guaranteed to
438 find at least one match when sym == label, so no special case
439 code is necessary. */
440 for (sym
= symbol_lastP
; sym
!= NULL
; sym
= symbol_previous (sym
))
441 if (symbol_get_frag (sym
) == old_frag
442 && S_GET_VALUE (sym
) == old_value
)
445 symbol_set_frag (sym
, frag_now
);
446 S_SET_VALUE (sym
, new_value
);
448 else if (label_seen
&& symbol_get_frag (sym
) != old_frag
)
451 record_alignment (now_seg
, align
);
456 /** Support for self-check mode. */
458 /* Mode of the assembler. */
461 NIOS2_MODE_ASSEMBLE
, /* Ordinary operation. */
462 NIOS2_MODE_TEST
/* Hidden mode used for self testing. */
465 static NIOS2_MODE nios2_mode
= NIOS2_MODE_ASSEMBLE
;
467 /* This function is used to in self-checking mode
468 to check the assembled instruction
469 opcode should be the assembled opcode, and exp_opcode
470 the parsed string representing the expected opcode. */
472 nios2_check_assembly (unsigned int opcode
, const char *exp_opcode
)
474 if (nios2_mode
== NIOS2_MODE_TEST
)
476 if (exp_opcode
== NULL
)
477 as_bad (_("expecting opcode string in self test mode"));
478 else if (opcode
!= strtoul (exp_opcode
, NULL
, 16))
479 as_bad (_("assembly 0x%08x, expected %s"), opcode
, exp_opcode
);
484 /** Support for machine-dependent assembler directives. */
485 /* Handle the .align pseudo-op. This aligns to a power of two. It
486 also adjusts any current instruction label. We treat this the same
487 way the MIPS port does: .align 0 turns off auto alignment. */
489 s_nios2_align (int ignore ATTRIBUTE_UNUSED
)
493 const char *pfill
= NULL
;
494 long max_alignment
= 15;
496 align
= get_absolute_expression ();
497 if (align
> max_alignment
)
499 align
= max_alignment
;
500 as_bad (_("Alignment too large: %d. assumed"), align
);
504 as_warn (_("Alignment negative: 0 assumed"));
508 if (*input_line_pointer
== ',')
510 input_line_pointer
++;
511 fill
= get_absolute_expression ();
512 pfill
= (const char *) &fill
;
514 else if (subseg_text_p (now_seg
))
515 pfill
= (const char *) &nop
;
519 nios2_last_label
= NULL
;
524 nios2_auto_align_on
= 1;
525 nios2_align (align
, pfill
, nios2_last_label
);
526 nios2_last_label
= NULL
;
529 nios2_auto_align_on
= 0;
531 demand_empty_rest_of_line ();
534 /* Handle the .text pseudo-op. This is like the usual one, but it
535 clears the saved last label and resets known alignment. */
540 nios2_last_label
= NULL
;
541 nios2_current_align
= 0;
542 nios2_current_align_seg
= now_seg
;
545 /* Handle the .data pseudo-op. This is like the usual one, but it
546 clears the saved last label and resets known alignment. */
551 nios2_last_label
= NULL
;
552 nios2_current_align
= 0;
553 nios2_current_align_seg
= now_seg
;
556 /* Handle the .section pseudo-op. This is like the usual one, but it
557 clears the saved last label and resets known alignment. */
559 s_nios2_section (int ignore
)
561 obj_elf_section (ignore
);
562 nios2_last_label
= NULL
;
563 nios2_current_align
= 0;
564 nios2_current_align_seg
= now_seg
;
567 /* Explicitly unaligned cons. */
569 s_nios2_ucons (int nbytes
)
572 hold
= nios2_auto_align_on
;
573 nios2_auto_align_on
= 0;
575 nios2_auto_align_on
= hold
;
578 /* Handle the .sdata directive. */
580 s_nios2_sdata (int ignore ATTRIBUTE_UNUSED
)
582 get_absolute_expression (); /* Ignored. */
583 subseg_new (".sdata", 0);
584 demand_empty_rest_of_line ();
587 /* .set sets assembler options eg noat/at and is also used
588 to set symbol values (.equ, .equiv ). */
590 s_nios2_set (int equiv
)
592 char *directive
= input_line_pointer
;
593 char delim
= get_symbol_end ();
594 char *endline
= input_line_pointer
;
597 /* We only want to handle ".set XXX" if the
598 user has tried ".set XXX, YYY" they are not
599 trying a directive. This prevents
600 us from polluting the name space. */
602 if (is_end_of_line
[(unsigned char) *input_line_pointer
])
604 bfd_boolean done
= TRUE
;
607 if (!strcmp (directive
, "noat"))
608 nios2_as_options
.noat
= TRUE
;
609 else if (!strcmp (directive
, "at"))
610 nios2_as_options
.noat
= FALSE
;
611 else if (!strcmp (directive
, "nobreak"))
612 nios2_as_options
.nobreak
= TRUE
;
613 else if (!strcmp (directive
, "break"))
614 nios2_as_options
.nobreak
= FALSE
;
615 else if (!strcmp (directive
, "norelax"))
616 nios2_as_options
.relax
= relax_none
;
617 else if (!strcmp (directive
, "relaxsection"))
618 nios2_as_options
.relax
= relax_section
;
619 else if (!strcmp (directive
, "relaxall"))
620 nios2_as_options
.relax
= relax_all
;
627 demand_empty_rest_of_line ();
632 /* If we fall through to here, either we have ".set XXX, YYY"
633 or we have ".set XXX" where XXX is unknown or we have
635 input_line_pointer
= directive
;
640 /* Machine-dependent assembler directives.
641 Format of each entry is:
642 { "directive", handler_func, param } */
643 const pseudo_typeS md_pseudo_table
[] = {
644 {"align", s_nios2_align
, 0},
645 {"text", s_nios2_text
, 0},
646 {"data", s_nios2_data
, 0},
647 {"section", s_nios2_section
, 0},
648 {"section.s", s_nios2_section
, 0},
649 {"sect", s_nios2_section
, 0},
650 {"sect.s", s_nios2_section
, 0},
651 /* .dword and .half are included for compatibility with MIPS. */
654 /* NIOS2 native word size is 4 bytes, so we override
655 the GAS default of 2. */
657 /* Explicitly unaligned directives. */
658 {"2byte", s_nios2_ucons
, 2},
659 {"4byte", s_nios2_ucons
, 4},
660 {"8byte", s_nios2_ucons
, 8},
661 {"16byte", s_nios2_ucons
, 16},
663 {"sdata", s_nios2_sdata
, 0},
665 {"set", s_nios2_set
, 0},
670 /** Relaxation support. */
672 /* We support two relaxation modes: a limited PC-relative mode with
673 -relax-section (the default), and an absolute jump mode with -relax-all.
675 Nios II PC-relative branch instructions only support 16-bit offsets.
676 And, there's no good way to add a 32-bit constant to the PC without
679 To deal with this, for the pc-relative relaxation mode we convert
681 into a series of 16-bit adds, like:
685 addi at, at, remainder
688 Similarly, conditional branches are converted from
689 b(condition) r, s, label
691 b(opposite condition) r, s, skip
695 addi at, at, remainder
699 The compiler can do a better job, either by converting the branch
700 directly into a JMP (going through the GOT for PIC) or by allocating
701 a second register for the 32-bit displacement.
703 For the -relax-all relaxation mode, the conversions are
704 movhi at, %hi(symbol+offset)
705 ori at, %lo(symbol+offset)
708 b(opposite condition), r, s, skip
709 movhi at, %hi(symbol+offset)
710 ori at, %lo(symbol+offset)
716 /* Arbitrarily limit the number of addis we can insert; we need to be able
717 to specify the maximum growth size for each frag that contains a
718 relaxable branch. There's no point in specifying a huge number here
719 since that means the assembler needs to allocate that much extra
720 memory for every branch, and almost no real code will ever need it.
721 Plus, as already noted a better solution is to just use a jmp, or
722 allocate a second register to hold a 32-bit displacement.
723 FIXME: Rather than making this a constant, it could be controlled by
724 a command-line argument. */
725 #define RELAX_MAX_ADDI 32
727 /* The fr_subtype field represents the target-specific relocation state.
728 It has type relax_substateT (unsigned int). We use it to track the
729 number of addis necessary, plus a bit to track whether this is a
731 Regardless of the smaller RELAX_MAX_ADDI limit, we reserve 16 bits
732 in the fr_subtype to encode the number of addis so that the whole
733 theoretically-valid range is representable.
734 For the -relax-all mode, N = 0 represents an in-range branch and N = 1
735 represents a branch that needs to be relaxed. */
736 #define UBRANCH (0 << 16)
737 #define CBRANCH (1 << 16)
738 #define IS_CBRANCH(SUBTYPE) ((SUBTYPE) & CBRANCH)
739 #define IS_UBRANCH(SUBTYPE) (!IS_CBRANCH (SUBTYPE))
740 #define UBRANCH_SUBTYPE(N) (UBRANCH | (N))
741 #define CBRANCH_SUBTYPE(N) (CBRANCH | (N))
742 #define SUBTYPE_ADDIS(SUBTYPE) ((SUBTYPE) & 0xffff)
744 /* For the -relax-section mode, unconditional branches require 2 extra i
745 nstructions besides the addis, conditional branches require 3. */
746 #define UBRANCH_ADDIS_TO_SIZE(N) (((N) + 2) * 4)
747 #define CBRANCH_ADDIS_TO_SIZE(N) (((N) + 3) * 4)
749 /* For the -relax-all mode, unconditional branches require 3 instructions
750 and conditional branches require 4. */
751 #define UBRANCH_JUMP_SIZE 12
752 #define CBRANCH_JUMP_SIZE 16
754 /* Maximum sizes of relaxation sequences. */
755 #define UBRANCH_MAX_SIZE \
756 (nios2_as_options.relax == relax_all \
757 ? UBRANCH_JUMP_SIZE \
758 : UBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
759 #define CBRANCH_MAX_SIZE \
760 (nios2_as_options.relax == relax_all \
761 ? CBRANCH_JUMP_SIZE \
762 : CBRANCH_ADDIS_TO_SIZE (RELAX_MAX_ADDI))
764 /* Register number of AT, the assembler temporary. */
767 /* Determine how many bytes are required to represent the sequence
768 indicated by SUBTYPE. */
770 nios2_relax_subtype_size (relax_substateT subtype
)
772 int n
= SUBTYPE_ADDIS (subtype
);
774 /* Regular conditional/unconditional branch instruction. */
776 else if (nios2_as_options
.relax
== relax_all
)
777 return (IS_CBRANCH (subtype
) ? CBRANCH_JUMP_SIZE
: UBRANCH_JUMP_SIZE
);
778 else if (IS_CBRANCH (subtype
))
779 return CBRANCH_ADDIS_TO_SIZE (n
);
781 return UBRANCH_ADDIS_TO_SIZE (n
);
784 /* Estimate size of fragp before relaxation.
785 This could also examine the offset in fragp and adjust
786 fragp->fr_subtype, but we will do that in nios2_relax_frag anyway. */
788 md_estimate_size_before_relax (fragS
*fragp
, segT segment ATTRIBUTE_UNUSED
)
790 return nios2_relax_subtype_size (fragp
->fr_subtype
);
793 /* Implement md_relax_frag, returning the change in size of the frag. */
795 nios2_relax_frag (segT segment
, fragS
*fragp
, long stretch
)
797 addressT target
= fragp
->fr_offset
;
798 relax_substateT subtype
= fragp
->fr_subtype
;
799 symbolS
*symbolp
= fragp
->fr_symbol
;
803 fragS
*sym_frag
= symbol_get_frag (symbolp
);
807 target
+= S_GET_VALUE (symbolp
);
809 /* See comments in write.c:relax_frag about handling of stretch. */
811 && sym_frag
->relax_marker
!= fragp
->relax_marker
)
813 if (stretch
< 0 || sym_frag
->region
== fragp
->region
)
815 else if (target
< fragp
->fr_address
)
816 target
= fragp
->fr_next
->fr_address
+ stretch
;
819 /* We subtract 4 because all pc relative branches are
820 from the next instruction. */
821 offset
= target
- fragp
->fr_address
- fragp
->fr_fix
- 4;
822 if (offset
>= -32768 && offset
<= 32764)
823 /* Fits in PC-relative branch. */
825 else if (nios2_as_options
.relax
== relax_all
)
826 /* Convert to jump. */
828 else if (nios2_as_options
.relax
== relax_section
829 && S_GET_SEGMENT (symbolp
) == segment
830 && S_IS_DEFINED (symbolp
))
831 /* Attempt a PC-relative relaxation on a branch to a defined
832 symbol in the same segment. */
834 /* The relaxation for conditional branches is offset by 4
835 bytes because we insert the inverted branch around the
837 if (IS_CBRANCH (subtype
))
840 n
= offset
/ 32767 + 1;
842 n
= offset
/ -32768 + 1;
844 /* Bail out immediately if relaxation has failed. If we try to
845 defer the diagnostic to md_convert_frag, some pathological test
846 cases (e.g. gcc/testsuite/gcc.c-torture/compile/20001226-1.c)
847 apparently never converge. By returning 0 here we could pretend
848 to the caller that nothing has changed, but that leaves things
849 in an inconsistent state when we get to md_convert_frag. */
850 if (n
> RELAX_MAX_ADDI
)
852 as_bad_where (fragp
->fr_file
, fragp
->fr_line
,
853 _("branch offset out of range\n"));
854 as_fatal (_("branch relaxation failed\n"));
858 /* We cannot handle this case, diagnose overflow later. */
861 if (IS_CBRANCH (subtype
))
862 fragp
->fr_subtype
= CBRANCH_SUBTYPE (n
);
864 fragp
->fr_subtype
= UBRANCH_SUBTYPE (n
);
866 return (nios2_relax_subtype_size (fragp
->fr_subtype
)
867 - nios2_relax_subtype_size (subtype
));
870 /* If we got here, it's probably an error. */
875 /* Complete fragp using the data from the relaxation pass. */
877 md_convert_frag (bfd
*headers ATTRIBUTE_UNUSED
, segT segment ATTRIBUTE_UNUSED
,
880 char *buffer
= fragp
->fr_literal
+ fragp
->fr_fix
;
881 relax_substateT subtype
= fragp
->fr_subtype
;
882 int n
= SUBTYPE_ADDIS (subtype
);
883 addressT target
= fragp
->fr_offset
;
884 symbolS
*symbolp
= fragp
->fr_symbol
;
886 unsigned int addend_mask
, addi_mask
;
887 offsetT addend
, remainder
;
890 /* If we didn't or can't relax, this is a regular branch instruction.
891 We just need to generate the fixup for the symbol and offset. */
894 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
, 1,
900 /* Replace the cbranch at fr_fix with one that has the opposite condition
901 in order to jump around the block of instructions we'll be adding. */
902 if (IS_CBRANCH (subtype
))
904 unsigned int br_opcode
;
907 /* Account for the nextpc and jmp in the pc-relative case, or the two
908 load instructions and jump in the absolute case. */
909 if (nios2_as_options
.relax
== relax_section
)
910 nbytes
= (n
+ 2) * 4;
914 br_opcode
= md_chars_to_number (buffer
, 4);
915 switch (br_opcode
& OP_MASK_OP
)
918 br_opcode
= (br_opcode
& ~OP_MASK_OP
) | OP_MATCH_BNE
;
921 br_opcode
= (br_opcode
& ~OP_MASK_OP
) | OP_MATCH_BEQ
;
924 br_opcode
= (br_opcode
& ~OP_MASK_OP
) | OP_MATCH_BLT
;
927 br_opcode
= (br_opcode
& ~OP_MASK_OP
) | OP_MATCH_BLTU
;
930 br_opcode
= (br_opcode
& ~OP_MASK_OP
) | OP_MATCH_BGE
;
933 br_opcode
= (br_opcode
& ~OP_MASK_OP
) | OP_MATCH_BGEU
;
936 as_bad_where (fragp
->fr_file
, fragp
->fr_line
,
937 _("expecting conditional branch for relaxation\n"));
941 br_opcode
= br_opcode
| (nbytes
<< OP_SH_IMM16
);
942 md_number_to_chars (buffer
, br_opcode
, 4);
947 /* Load at for the PC-relative case. */
948 if (nios2_as_options
.relax
== relax_section
)
950 /* Insert the nextpc instruction. */
951 md_number_to_chars (buffer
,
952 OP_MATCH_NEXTPC
| (AT_REGNUM
<< OP_SH_RRD
), 4);
956 /* We need to know whether the offset is positive or negative. */
957 target
+= S_GET_VALUE (symbolp
);
958 offset
= target
- fragp
->fr_address
- fragp
->fr_fix
;
963 addend_mask
= (((unsigned int)addend
) & 0xffff) << OP_SH_IMM16
;
965 /* Insert n-1 addi instructions. */
966 addi_mask
= (OP_MATCH_ADDI
967 | (AT_REGNUM
<< OP_SH_IRD
)
968 | (AT_REGNUM
<< OP_SH_IRS
));
969 for (i
= 0; i
< n
- 1; i
++)
971 md_number_to_chars (buffer
, addi_mask
| addend_mask
, 4);
976 /* Insert the last addi instruction to hold the remainder. */
977 remainder
= offset
- addend
* (n
- 1);
978 gas_assert (remainder
>= -32768 && remainder
<= 32767);
979 addend_mask
= (((unsigned int)remainder
) & 0xffff) << OP_SH_IMM16
;
980 md_number_to_chars (buffer
, addi_mask
| addend_mask
, 4);
985 /* Load at for the absolute case. */
988 md_number_to_chars (buffer
, OP_MATCH_ORHI
| 0x00400000, 4);
989 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
,
990 0, BFD_RELOC_NIOS2_HI16
);
993 md_number_to_chars (buffer
, OP_MATCH_ORI
| 0x08400000, 4);
994 fix_new (fragp
, fragp
->fr_fix
, 4, fragp
->fr_symbol
, fragp
->fr_offset
,
995 0, BFD_RELOC_NIOS2_LO16
);
1000 /* Insert the jmp instruction. */
1001 md_number_to_chars (buffer
, OP_MATCH_JMP
| (AT_REGNUM
<< OP_SH_RRS
), 4);
1007 /** Fixups and overflow checking. */
1009 /* Check a fixup for overflow. */
1011 nios2_check_overflow (valueT fixup
, reloc_howto_type
*howto
)
1013 /* Apply the rightshift before checking for overflow. */
1014 fixup
= ((signed)fixup
) >> howto
->rightshift
;
1016 /* Check for overflow - return TRUE if overflow, FALSE if not. */
1017 switch (howto
->complain_on_overflow
)
1019 case complain_overflow_dont
:
1021 case complain_overflow_bitfield
:
1022 if ((fixup
>> howto
->bitsize
) != 0
1023 && ((signed) fixup
>> howto
->bitsize
) != -1)
1026 case complain_overflow_signed
:
1027 if ((fixup
& 0x80000000) > 0)
1029 /* Check for negative overflow. */
1030 if ((signed) fixup
< ((signed) 0x80000000 >> howto
->bitsize
))
1035 /* Check for positive overflow. */
1036 if (fixup
>= ((unsigned) 1 << (howto
->bitsize
- 1)))
1040 case complain_overflow_unsigned
:
1041 if ((fixup
>> howto
->bitsize
) != 0)
1045 as_bad (_("error checking for overflow - broken assembler"));
1051 /* Emit diagnostic for fixup overflow. */
1053 nios2_diagnose_overflow (valueT fixup
, reloc_howto_type
*howto
,
1054 fixS
*fixP
, valueT value
)
1056 if (fixP
->fx_r_type
== BFD_RELOC_8
1057 || fixP
->fx_r_type
== BFD_RELOC_16
1058 || fixP
->fx_r_type
== BFD_RELOC_32
)
1059 /* These relocs are against data, not instructions. */
1060 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1061 _("immediate value 0x%x truncated to 0x%x"),
1062 (unsigned int) fixup
,
1063 (unsigned int) (~(~(valueT
) 0 << howto
->bitsize
) & fixup
));
1066 /* What opcode is the instruction? This will determine
1067 whether we check for overflow in immediate values
1068 and what error message we get. */
1069 const struct nios2_opcode
*opcode
;
1070 enum overflow_type overflow_msg_type
;
1071 unsigned int range_min
;
1072 unsigned int range_max
;
1073 unsigned int address
;
1074 gas_assert (fixP
->fx_size
== 4);
1075 opcode
= nios2_find_opcode_hash (value
);
1076 gas_assert (opcode
);
1077 overflow_msg_type
= opcode
->overflow_msg
;
1078 switch (overflow_msg_type
)
1080 case call_target_overflow
:
1082 = ((fixP
->fx_frag
->fr_address
+ fixP
->fx_where
) & 0xf0000000);
1083 range_max
= range_min
+ 0x0fffffff;
1084 address
= fixup
| range_min
;
1086 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1087 _("call target address 0x%08x out of range 0x%08x to 0x%08x"),
1088 address
, range_min
, range_max
);
1090 case branch_target_overflow
:
1091 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1092 _("branch offset %d out of range %d to %d"),
1093 (int)fixup
, -32768, 32767);
1095 case address_offset_overflow
:
1096 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1097 _("%s offset %d out of range %d to %d"),
1098 opcode
->name
, (int)fixup
, -32768, 32767);
1100 case signed_immed16_overflow
:
1101 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1102 _("immediate value %d out of range %d to %d"),
1103 (int)fixup
, -32768, 32767);
1105 case unsigned_immed16_overflow
:
1106 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1107 _("immediate value %u out of range %u to %u"),
1108 (unsigned int)fixup
, 0, 65535);
1110 case unsigned_immed5_overflow
:
1111 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1112 _("immediate value %u out of range %u to %u"),
1113 (unsigned int)fixup
, 0, 31);
1115 case custom_opcode_overflow
:
1116 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1117 _("custom instruction opcode %u out of range %u to %u"),
1118 (unsigned int)fixup
, 0, 255);
1121 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1122 _("overflow in immediate argument"));
1128 /* Apply a fixup to the object file. */
1130 md_apply_fix (fixS
*fixP
, valueT
*valP
, segT seg ATTRIBUTE_UNUSED
)
1132 /* Assert that the fixup is one we can handle. */
1133 gas_assert (fixP
!= NULL
&& valP
!= NULL
1134 && (fixP
->fx_r_type
== BFD_RELOC_8
1135 || fixP
->fx_r_type
== BFD_RELOC_16
1136 || fixP
->fx_r_type
== BFD_RELOC_32
1137 || fixP
->fx_r_type
== BFD_RELOC_64
1138 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_S16
1139 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_U16
1140 || fixP
->fx_r_type
== BFD_RELOC_16_PCREL
1141 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL26
1142 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL26_NOAT
1143 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM5
1144 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CACHE_OPX
1145 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM6
1146 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_IMM8
1147 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_HI16
1148 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_LO16
1149 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_HIADJ16
1150 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GPREL
1151 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1152 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
1153 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_UJMP
1154 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CJMP
1155 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALLR
1156 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_ALIGN
1157 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOT16
1158 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_CALL16
1159 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_LO
1160 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_HA
1161 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_GD16
1162 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDM16
1163 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDO16
1164 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_IE16
1165 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LE16
1166 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF
1167 || fixP
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPREL
1168 /* Add other relocs here as we generate them. */
1171 if (fixP
->fx_r_type
== BFD_RELOC_64
)
1173 /* We may reach here due to .8byte directives, but we never output
1174 BFD_RELOC_64; it must be resolved. */
1175 if (fixP
->fx_addsy
!= NULL
)
1176 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1177 _("cannot create 64-bit relocation"));
1180 md_number_to_chars (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
,
1187 /* The value passed in valP can be the value of a fully
1188 resolved expression, or it can be the value of a partially
1189 resolved expression. In the former case, both fixP->fx_addsy
1190 and fixP->fx_subsy are NULL, and fixP->fx_offset == *valP, and
1191 we can fix up the instruction that fixP relates to.
1192 In the latter case, one or both of fixP->fx_addsy and
1193 fixP->fx_subsy are not NULL, and fixP->fx_offset may or may not
1194 equal *valP. We don't need to check for fixP->fx_subsy being null
1195 because the generic part of the assembler generates an error if
1196 it is not an absolute symbol. */
1197 if (fixP
->fx_addsy
!= NULL
)
1198 /* Partially resolved expression. */
1200 fixP
->fx_addnumber
= fixP
->fx_offset
;
1203 switch (fixP
->fx_r_type
)
1205 case BFD_RELOC_NIOS2_TLS_GD16
:
1206 case BFD_RELOC_NIOS2_TLS_LDM16
:
1207 case BFD_RELOC_NIOS2_TLS_LDO16
:
1208 case BFD_RELOC_NIOS2_TLS_IE16
:
1209 case BFD_RELOC_NIOS2_TLS_LE16
:
1210 case BFD_RELOC_NIOS2_TLS_DTPMOD
:
1211 case BFD_RELOC_NIOS2_TLS_DTPREL
:
1212 case BFD_RELOC_NIOS2_TLS_TPREL
:
1213 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
1220 /* Fully resolved fixup. */
1222 reloc_howto_type
*howto
1223 = bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
);
1226 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1227 _("relocation is not supported"));
1230 valueT fixup
= *valP
;
1234 /* If this is a pc-relative relocation, we need to
1235 subtract the current offset within the object file
1236 FIXME : for some reason fixP->fx_pcrel isn't 1 when it should be
1237 so I'm using the howto structure instead to determine this. */
1238 if (howto
->pc_relative
== 1)
1239 fixup
= fixup
- (fixP
->fx_frag
->fr_address
+ fixP
->fx_where
+ 4);
1241 /* Get the instruction or data to be fixed up. */
1242 buf
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1243 value
= md_chars_to_number (buf
, fixP
->fx_size
);
1245 /* Check for overflow, emitting a diagnostic if necessary. */
1246 if (nios2_check_overflow (fixup
, howto
))
1247 nios2_diagnose_overflow (fixup
, howto
, fixP
, value
);
1249 /* Apply the right shift. */
1250 fixup
= ((signed)fixup
) >> howto
->rightshift
;
1252 /* Truncate the fixup to right size. */
1253 switch (fixP
->fx_r_type
)
1255 case BFD_RELOC_NIOS2_HI16
:
1256 fixup
= (fixup
>> 16) & 0xFFFF;
1258 case BFD_RELOC_NIOS2_LO16
:
1259 fixup
= fixup
& 0xFFFF;
1261 case BFD_RELOC_NIOS2_HIADJ16
:
1262 fixup
= ((((fixup
>> 16) & 0xFFFF) + ((fixup
>> 15) & 0x01))
1267 int n
= sizeof (fixup
) * 8 - howto
->bitsize
;
1268 fixup
= (fixup
<< n
) >> n
;
1273 /* Fix up the instruction. */
1274 value
= (value
& ~howto
->dst_mask
) | (fixup
<< howto
->bitpos
);
1275 md_number_to_chars (buf
, value
, fixP
->fx_size
);
1281 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
)
1285 && !S_IS_DEFINED (fixP
->fx_addsy
) && !S_IS_WEAK (fixP
->fx_addsy
))
1286 S_SET_WEAK (fixP
->fx_addsy
);
1288 else if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1294 /** Instruction parsing support. */
1296 /* Special relocation directive strings. */
1298 struct nios2_special_relocS
1301 bfd_reloc_code_real_type reloc_type
;
1304 struct nios2_special_relocS nios2_special_reloc
[] = {
1305 {"%hiadj", BFD_RELOC_NIOS2_HIADJ16
},
1306 {"%hi", BFD_RELOC_NIOS2_HI16
},
1307 {"%lo", BFD_RELOC_NIOS2_LO16
},
1308 {"%gprel", BFD_RELOC_NIOS2_GPREL
},
1309 {"%call", BFD_RELOC_NIOS2_CALL16
},
1310 {"%gotoff_lo", BFD_RELOC_NIOS2_GOTOFF_LO
},
1311 {"%gotoff_hiadj", BFD_RELOC_NIOS2_GOTOFF_HA
},
1312 {"%tls_gd", BFD_RELOC_NIOS2_TLS_GD16
},
1313 {"%tls_ldm", BFD_RELOC_NIOS2_TLS_LDM16
},
1314 {"%tls_ldo", BFD_RELOC_NIOS2_TLS_LDO16
},
1315 {"%tls_ie", BFD_RELOC_NIOS2_TLS_IE16
},
1316 {"%tls_le", BFD_RELOC_NIOS2_TLS_LE16
},
1317 {"%gotoff", BFD_RELOC_NIOS2_GOTOFF
},
1318 {"%got", BFD_RELOC_NIOS2_GOT16
}
1321 #define NIOS2_NUM_SPECIAL_RELOCS \
1322 (sizeof(nios2_special_reloc)/sizeof(nios2_special_reloc[0]))
1323 const int nios2_num_special_relocs
= NIOS2_NUM_SPECIAL_RELOCS
;
1325 /* Creates a new nios2_insn_relocS and returns a pointer to it. */
1326 static nios2_insn_relocS
*
1327 nios2_insn_reloc_new (bfd_reloc_code_real_type reloc_type
, unsigned int pcrel
)
1329 nios2_insn_relocS
*retval
;
1330 retval
= (nios2_insn_relocS
*) malloc (sizeof (nios2_insn_relocS
));
1333 as_bad (_("can't create relocation"));
1337 /* Fill out the fields with default values. */
1338 retval
->reloc_next
= NULL
;
1339 retval
->reloc_type
= reloc_type
;
1340 retval
->reloc_pcrel
= pcrel
;
1344 /* Frees up memory previously allocated by nios2_insn_reloc_new(). */
1345 /* FIXME: this is never called; memory leak? */
1348 nios2_insn_reloc_destroy (nios2_insn_relocS
*reloc
)
1350 gas_assert (reloc
!= NULL
);
1355 /* The various nios2_assemble_* functions call this
1356 function to generate an expression from a string representing an expression.
1357 It then tries to evaluate the expression, and if it can, returns its value.
1358 If not, it creates a new nios2_insn_relocS and stores the expression and
1359 reloc_type for future use. */
1360 static unsigned long
1361 nios2_assemble_expression (const char *exprstr
,
1362 nios2_insn_infoS
*insn
,
1363 nios2_insn_relocS
*prev_reloc
,
1364 bfd_reloc_code_real_type reloc_type
,
1367 nios2_insn_relocS
*reloc
;
1368 char *saved_line_ptr
;
1369 unsigned short value
;
1372 gas_assert (exprstr
!= NULL
);
1373 gas_assert (insn
!= NULL
);
1375 /* Check for relocation operators.
1376 Change the relocation type and advance the ptr to the start of
1377 the expression proper. */
1378 for (i
= 0; i
< nios2_num_special_relocs
; i
++)
1379 if (strstr (exprstr
, nios2_special_reloc
[i
].string
) != NULL
)
1381 reloc_type
= nios2_special_reloc
[i
].reloc_type
;
1382 exprstr
+= strlen (nios2_special_reloc
[i
].string
) + 1;
1384 /* %lo and %hiadj have different meanings for PC-relative
1388 if (reloc_type
== BFD_RELOC_NIOS2_LO16
)
1389 reloc_type
= BFD_RELOC_NIOS2_PCREL_LO
;
1390 if (reloc_type
== BFD_RELOC_NIOS2_HIADJ16
)
1391 reloc_type
= BFD_RELOC_NIOS2_PCREL_HA
;
1397 /* We potentially have a relocation. */
1398 reloc
= nios2_insn_reloc_new (reloc_type
, pcrel
);
1399 if (prev_reloc
!= NULL
)
1400 prev_reloc
->reloc_next
= reloc
;
1402 insn
->insn_reloc
= reloc
;
1404 /* Parse the expression string. */
1405 saved_line_ptr
= input_line_pointer
;
1406 input_line_pointer
= (char *) exprstr
;
1407 expression (&reloc
->reloc_expression
);
1408 input_line_pointer
= saved_line_ptr
;
1410 /* This is redundant as the fixup will put this into
1411 the instruction, but it is included here so that
1412 self-test mode (-r) works. */
1414 if (nios2_mode
== NIOS2_MODE_TEST
1415 && reloc
->reloc_expression
.X_op
== O_constant
)
1416 value
= reloc
->reloc_expression
.X_add_number
;
1418 return (unsigned long) value
;
1421 /* Argument assemble functions.
1422 All take an instruction argument string, and a pointer
1423 to an instruction opcode. Upon return the insn_opcode
1424 has the relevant fields filled in to represent the arg
1425 string. The return value is NULL if successful, or
1426 an error message if an error was detected.
1428 The naming conventions for these functions match the args template
1429 in the nios2_opcode structure, as documented in include/opcode/nios2.h.
1430 For example, nios2_assemble_args_dst is used for instructions with
1432 See nios2_arg_info_structs below for the exact correspondence. */
1435 nios2_assemble_args_dst (nios2_insn_infoS
*insn_info
)
1437 if (insn_info
->insn_tokens
[1] != NULL
1438 && insn_info
->insn_tokens
[2] != NULL
1439 && insn_info
->insn_tokens
[3] != NULL
)
1441 struct nios2_reg
*dst
= nios2_reg_lookup (insn_info
->insn_tokens
[1]);
1442 struct nios2_reg
*src1
= nios2_reg_lookup (insn_info
->insn_tokens
[2]);
1443 struct nios2_reg
*src2
= nios2_reg_lookup (insn_info
->insn_tokens
[3]);
1446 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[1]);
1448 SET_INSN_FIELD (RRD
, insn_info
->insn_code
, dst
->index
);
1451 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[2]);
1453 SET_INSN_FIELD (RRS
, insn_info
->insn_code
, src1
->index
);
1456 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[3]);
1458 SET_INSN_FIELD (RRT
, insn_info
->insn_code
, src2
->index
);
1460 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[4]);
1465 nios2_assemble_args_tsi (nios2_insn_infoS
*insn_info
)
1467 if (insn_info
->insn_tokens
[1] != NULL
&&
1468 insn_info
->insn_tokens
[2] != NULL
&& insn_info
->insn_tokens
[3] != NULL
)
1470 struct nios2_reg
*dst
= nios2_reg_lookup (insn_info
->insn_tokens
[1]);
1471 struct nios2_reg
*src1
= nios2_reg_lookup (insn_info
->insn_tokens
[2]);
1473 = nios2_assemble_expression (insn_info
->insn_tokens
[3], insn_info
,
1474 insn_info
->insn_reloc
, BFD_RELOC_NIOS2_S16
,
1478 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[1]);
1480 SET_INSN_FIELD (IRT
, insn_info
->insn_code
, dst
->index
);
1483 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[2]);
1485 SET_INSN_FIELD (IRS
, insn_info
->insn_code
, src1
->index
);
1487 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, src2
);
1488 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[4]);
1489 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, 0);
1494 nios2_assemble_args_tsu (nios2_insn_infoS
*insn_info
)
1496 if (insn_info
->insn_tokens
[1] != NULL
1497 && insn_info
->insn_tokens
[2] != NULL
1498 && insn_info
->insn_tokens
[3] != NULL
)
1500 struct nios2_reg
*dst
= nios2_reg_lookup (insn_info
->insn_tokens
[1]);
1501 struct nios2_reg
*src1
= nios2_reg_lookup (insn_info
->insn_tokens
[2]);
1503 = nios2_assemble_expression (insn_info
->insn_tokens
[3], insn_info
,
1504 insn_info
->insn_reloc
, BFD_RELOC_NIOS2_U16
,
1508 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[1]);
1510 SET_INSN_FIELD (IRT
, insn_info
->insn_code
, dst
->index
);
1513 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[2]);
1515 SET_INSN_FIELD (IRS
, insn_info
->insn_code
, src1
->index
);
1517 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, src2
);
1518 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[4]);
1519 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, 0);
1524 nios2_assemble_args_sto (nios2_insn_infoS
*insn_info
)
1526 if (insn_info
->insn_tokens
[1] != NULL
1527 && insn_info
->insn_tokens
[2] != NULL
1528 && insn_info
->insn_tokens
[3] != NULL
)
1530 struct nios2_reg
*dst
= nios2_reg_lookup (insn_info
->insn_tokens
[1]);
1531 struct nios2_reg
*src1
= nios2_reg_lookup (insn_info
->insn_tokens
[2]);
1533 = nios2_assemble_expression (insn_info
->insn_tokens
[3], insn_info
,
1534 insn_info
->insn_reloc
, BFD_RELOC_16_PCREL
,
1538 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[1]);
1540 SET_INSN_FIELD (IRS
, insn_info
->insn_code
, dst
->index
);
1543 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[2]);
1545 SET_INSN_FIELD (IRT
, insn_info
->insn_code
, src1
->index
);
1547 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, src2
);
1548 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[4]);
1549 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, 0);
1554 nios2_assemble_args_o (nios2_insn_infoS
*insn_info
)
1556 if (insn_info
->insn_tokens
[1] != NULL
)
1559 = nios2_assemble_expression (insn_info
->insn_tokens
[1], insn_info
,
1560 insn_info
->insn_reloc
, BFD_RELOC_16_PCREL
,
1562 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, immed
);
1563 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[2]);
1564 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, 0);
1569 nios2_assemble_args_is (nios2_insn_infoS
*insn_info
)
1571 if (insn_info
->insn_tokens
[1] != NULL
&& insn_info
->insn_tokens
[2] != NULL
)
1573 struct nios2_reg
*addr_src
= nios2_reg_lookup (insn_info
->insn_tokens
[2]);
1575 = nios2_assemble_expression (insn_info
->insn_tokens
[1], insn_info
,
1576 insn_info
->insn_reloc
, BFD_RELOC_NIOS2_S16
,
1579 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, immed
);
1581 if (addr_src
== NULL
)
1582 as_bad (_("unknown base register %s"), insn_info
->insn_tokens
[2]);
1584 SET_INSN_FIELD (RRS
, insn_info
->insn_code
, addr_src
->index
);
1586 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[3]);
1587 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, 0);
1592 nios2_assemble_args_m (nios2_insn_infoS
*insn_info
)
1594 if (insn_info
->insn_tokens
[1] != NULL
)
1597 = nios2_assemble_expression (insn_info
->insn_tokens
[1], insn_info
,
1598 insn_info
->insn_reloc
,
1599 (nios2_as_options
.noat
1600 ? BFD_RELOC_NIOS2_CALL26_NOAT
1601 : BFD_RELOC_NIOS2_CALL26
),
1604 SET_INSN_FIELD (IMM26
, insn_info
->insn_code
, immed
);
1605 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[2]);
1606 SET_INSN_FIELD (IMM26
, insn_info
->insn_code
, 0);
1611 nios2_assemble_args_s (nios2_insn_infoS
*insn_info
)
1613 if (insn_info
->insn_tokens
[1] != NULL
)
1615 struct nios2_reg
*src
= nios2_reg_lookup (insn_info
->insn_tokens
[1]);
1617 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[1]);
1619 SET_INSN_FIELD (RRS
, insn_info
->insn_code
, src
->index
);
1621 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[2]);
1626 nios2_assemble_args_tis (nios2_insn_infoS
*insn_info
)
1628 if (insn_info
->insn_tokens
[1] != NULL
1629 && insn_info
->insn_tokens
[2] != NULL
1630 && insn_info
->insn_tokens
[3] != NULL
)
1632 struct nios2_reg
*dst
= nios2_reg_lookup (insn_info
->insn_tokens
[1]);
1633 struct nios2_reg
*addr_src
= nios2_reg_lookup (insn_info
->insn_tokens
[3]);
1635 = nios2_assemble_expression (insn_info
->insn_tokens
[2], insn_info
,
1636 insn_info
->insn_reloc
, BFD_RELOC_NIOS2_S16
,
1639 if (addr_src
== NULL
)
1640 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[3]);
1642 SET_INSN_FIELD (RRS
, insn_info
->insn_code
, addr_src
->index
);
1645 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[1]);
1647 SET_INSN_FIELD (RRT
, insn_info
->insn_code
, dst
->index
);
1649 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, immed
);
1650 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[4]);
1651 SET_INSN_FIELD (IMM16
, insn_info
->insn_code
, 0);
1656 nios2_assemble_args_dc (nios2_insn_infoS
*insn_info
)
1658 if (insn_info
->insn_tokens
[1] != NULL
&& insn_info
->insn_tokens
[2] != NULL
)
1660 struct nios2_reg
*ctl
= nios2_reg_lookup (insn_info
->insn_tokens
[2]);
1661 struct nios2_reg
*dst
= nios2_reg_lookup (insn_info
->insn_tokens
[1]);
1664 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[1]);
1666 SET_INSN_FIELD (RCTL
, insn_info
->insn_code
, ctl
->index
);
1669 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[2]);
1671 SET_INSN_FIELD (RRD
, insn_info
->insn_code
, dst
->index
);
1673 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[3]);
1678 nios2_assemble_args_cs (nios2_insn_infoS
*insn_info
)
1680 if (insn_info
->insn_tokens
[1] != NULL
&& insn_info
->insn_tokens
[2] != NULL
)
1682 struct nios2_reg
*ctl
= nios2_reg_lookup (insn_info
->insn_tokens
[1]);
1683 struct nios2_reg
*src
= nios2_reg_lookup (insn_info
->insn_tokens
[2]);
1686 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[1]);
1687 else if (ctl
->index
== 4)
1688 as_bad (_("ipending control register (ctl4) is read-only\n"));
1690 SET_INSN_FIELD (RCTL
, insn_info
->insn_code
, ctl
->index
);
1693 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[2]);
1695 SET_INSN_FIELD (RRS
, insn_info
->insn_code
, src
->index
);
1697 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[3]);
1702 nios2_assemble_args_ds (nios2_insn_infoS
* insn_info
)
1704 if (insn_info
->insn_tokens
[1] != NULL
&& insn_info
->insn_tokens
[2] != NULL
)
1706 struct nios2_reg
*dst
= nios2_reg_lookup (insn_info
->insn_tokens
[1]);
1707 struct nios2_reg
*src
= nios2_reg_lookup (insn_info
->insn_tokens
[2]);
1710 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[1]);
1712 SET_INSN_FIELD (RRD
, insn_info
->insn_code
, dst
->index
);
1715 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[2]);
1717 SET_INSN_FIELD (RRS
, insn_info
->insn_code
, src
->index
);
1719 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[3]);
1724 nios2_assemble_args_ldst (nios2_insn_infoS
*insn_info
)
1726 if (insn_info
->insn_tokens
[1] != NULL
1727 && insn_info
->insn_tokens
[2] != NULL
1728 && insn_info
->insn_tokens
[3] != NULL
1729 && insn_info
->insn_tokens
[4] != NULL
)
1731 unsigned long custom_n
1732 = nios2_assemble_expression (insn_info
->insn_tokens
[1], insn_info
,
1733 insn_info
->insn_reloc
,
1734 BFD_RELOC_NIOS2_IMM8
, 0);
1736 struct nios2_reg
*dst
= nios2_reg_lookup (insn_info
->insn_tokens
[2]);
1737 struct nios2_reg
*src1
= nios2_reg_lookup (insn_info
->insn_tokens
[3]);
1738 struct nios2_reg
*src2
= nios2_reg_lookup (insn_info
->insn_tokens
[4]);
1740 SET_INSN_FIELD (CUSTOM_N
, insn_info
->insn_code
, custom_n
);
1743 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[2]);
1745 SET_INSN_FIELD (RRD
, insn_info
->insn_code
, dst
->index
);
1748 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[3]);
1750 SET_INSN_FIELD (RRS
, insn_info
->insn_code
, src1
->index
);
1753 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[4]);
1755 SET_INSN_FIELD (RRT
, insn_info
->insn_code
, src2
->index
);
1757 /* Set or clear the bits to indicate whether coprocessor registers are
1759 if (nios2_coproc_reg (insn_info
->insn_tokens
[2]))
1760 SET_INSN_FIELD (CUSTOM_C
, insn_info
->insn_code
, 0);
1762 SET_INSN_FIELD (CUSTOM_C
, insn_info
->insn_code
, 1);
1764 if (nios2_coproc_reg (insn_info
->insn_tokens
[3]))
1765 SET_INSN_FIELD (CUSTOM_A
, insn_info
->insn_code
, 0);
1767 SET_INSN_FIELD (CUSTOM_A
, insn_info
->insn_code
, 1);
1769 if (nios2_coproc_reg (insn_info
->insn_tokens
[4]))
1770 SET_INSN_FIELD (CUSTOM_B
, insn_info
->insn_code
, 0);
1772 SET_INSN_FIELD (CUSTOM_B
, insn_info
->insn_code
, 1);
1774 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[5]);
1779 nios2_assemble_args_none (nios2_insn_infoS
*insn_info ATTRIBUTE_UNUSED
)
1781 /* Nothing to do. */
1785 nios2_assemble_args_dsj (nios2_insn_infoS
*insn_info
)
1787 if (insn_info
->insn_tokens
[1] != NULL
1788 && insn_info
->insn_tokens
[2] != NULL
1789 && insn_info
->insn_tokens
[3] != NULL
)
1791 struct nios2_reg
*dst
= nios2_reg_lookup (insn_info
->insn_tokens
[1]);
1792 struct nios2_reg
*src1
= nios2_reg_lookup (insn_info
->insn_tokens
[2]);
1794 /* A 5-bit constant expression. */
1796 nios2_assemble_expression (insn_info
->insn_tokens
[3], insn_info
,
1797 insn_info
->insn_reloc
,
1798 BFD_RELOC_NIOS2_IMM5
, 0);
1801 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[1]);
1803 SET_INSN_FIELD (RRD
, insn_info
->insn_code
, dst
->index
);
1806 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[2]);
1808 SET_INSN_FIELD (RRS
, insn_info
->insn_code
, src1
->index
);
1810 SET_INSN_FIELD (IMM5
, insn_info
->insn_code
, src2
);
1811 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[4]);
1812 SET_INSN_FIELD (IMM5
, insn_info
->insn_code
, 0);
1817 nios2_assemble_args_d (nios2_insn_infoS
*insn_info
)
1819 if (insn_info
->insn_tokens
[1] != NULL
)
1821 struct nios2_reg
*dst
= nios2_reg_lookup (insn_info
->insn_tokens
[1]);
1824 as_bad (_("unknown register %s"), insn_info
->insn_tokens
[1]);
1826 SET_INSN_FIELD (RRD
, insn_info
->insn_code
, dst
->index
);
1828 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[2]);
1833 nios2_assemble_args_b (nios2_insn_infoS
*insn_info
)
1835 unsigned int imm5
= 0;
1837 if (insn_info
->insn_tokens
[1] != NULL
)
1839 /* A 5-bit constant expression. */
1840 imm5
= nios2_assemble_expression (insn_info
->insn_tokens
[1],
1841 insn_info
, insn_info
->insn_reloc
,
1842 BFD_RELOC_NIOS2_IMM5
, 0);
1843 SET_INSN_FIELD (TRAP_IMM5
, insn_info
->insn_code
, imm5
);
1844 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[2]);
1847 SET_INSN_FIELD (TRAP_IMM5
, insn_info
->insn_code
, imm5
);
1849 nios2_check_assembly (insn_info
->insn_code
, insn_info
->insn_tokens
[2]);
1852 /* This table associates pointers to functions that parse the arguments to an
1853 instruction and fill in the relevant fields of the instruction. */
1854 const nios2_arg_infoS nios2_arg_info_structs
[] = {
1855 /* args, assemble_args_func */
1856 {"d,s,t", nios2_assemble_args_dst
},
1857 {"d,s,t,E", nios2_assemble_args_dst
},
1858 {"t,s,i", nios2_assemble_args_tsi
},
1859 {"t,s,i,E", nios2_assemble_args_tsi
},
1860 {"t,s,u", nios2_assemble_args_tsu
},
1861 {"t,s,u,E", nios2_assemble_args_tsu
},
1862 {"s,t,o", nios2_assemble_args_sto
},
1863 {"s,t,o,E", nios2_assemble_args_sto
},
1864 {"o", nios2_assemble_args_o
},
1865 {"o,E", nios2_assemble_args_o
},
1866 {"s", nios2_assemble_args_s
},
1867 {"s,E", nios2_assemble_args_s
},
1868 {"", nios2_assemble_args_none
},
1869 {"E", nios2_assemble_args_none
},
1870 {"i(s)", nios2_assemble_args_is
},
1871 {"i(s)E", nios2_assemble_args_is
},
1872 {"m", nios2_assemble_args_m
},
1873 {"m,E", nios2_assemble_args_m
},
1874 {"t,i(s)", nios2_assemble_args_tis
},
1875 {"t,i(s)E", nios2_assemble_args_tis
},
1876 {"d,c", nios2_assemble_args_dc
},
1877 {"d,c,E", nios2_assemble_args_dc
},
1878 {"c,s", nios2_assemble_args_cs
},
1879 {"c,s,E", nios2_assemble_args_cs
},
1880 {"d,s", nios2_assemble_args_ds
},
1881 {"d,s,E", nios2_assemble_args_ds
},
1882 {"l,d,s,t", nios2_assemble_args_ldst
},
1883 {"l,d,s,t,E", nios2_assemble_args_ldst
},
1884 {"d,s,j", nios2_assemble_args_dsj
},
1885 {"d,s,j,E", nios2_assemble_args_dsj
},
1886 {"d", nios2_assemble_args_d
},
1887 {"d,E", nios2_assemble_args_d
},
1888 {"b", nios2_assemble_args_b
},
1889 {"b,E", nios2_assemble_args_b
}
1892 #define NIOS2_NUM_ARGS \
1893 ((sizeof(nios2_arg_info_structs)/sizeof(nios2_arg_info_structs[0])))
1894 const int nios2_num_arg_info_structs
= NIOS2_NUM_ARGS
;
1896 /* The function consume_arg takes a pointer into a string
1897 of instruction tokens (args) and a pointer into a string
1898 representing the expected sequence of tokens and separators.
1899 It checks whether the first argument in argstr is of the
1900 expected type, throwing an error if it is not, and returns
1901 the pointer argstr. */
1903 nios2_consume_arg (nios2_insn_infoS
*insn
, char *argstr
, const char *parsestr
)
1911 if (!nios2_control_register_arg_p (argstr
))
1912 as_bad (_("expecting control register"));
1918 /* We check to make sure we don't have a control register. */
1919 if (nios2_control_register_arg_p (argstr
))
1920 as_bad (_("illegal use of control register"));
1922 /* And whether coprocessor registers are valid here. */
1923 if (nios2_coproc_reg (argstr
)
1924 && insn
->insn_nios2_opcode
->match
!= OP_MATCH_CUSTOM
)
1925 as_bad (_("illegal use of coprocessor register\n"));
1927 /* Extract a register number if the register is of the
1928 form r[0-9]+, if it is a normal register, set
1929 regno to its number (0-31), else set regno to -1. */
1930 if (argstr
[0] == 'r' && ISDIGIT (argstr
[1]))
1942 while (ISDIGIT (*p
));
1947 /* And whether we are using at. */
1948 if (!nios2_as_options
.noat
1949 && (regno
== 1 || strprefix (argstr
, "at")))
1950 as_warn (_("Register at (r1) can sometimes be corrupted by assembler "
1952 "Use .set noat to turn off those optimizations (and this "
1955 /* And whether we are using oci registers. */
1956 if (!nios2_as_options
.nobreak
1957 && (regno
== 25 || strprefix (argstr
, "bt")))
1958 as_warn (_("The debugger will corrupt bt (r25).\n"
1959 "If you don't need to debug this "
1960 "code use .set nobreak to turn off this warning."));
1962 if (!nios2_as_options
.nobreak
1964 || strprefix (argstr
, "ba")
1965 || strprefix (argstr
, "sstatus")))
1966 as_warn (_("The debugger will corrupt sstatus/ba (r30).\n"
1967 "If you don't need to debug this "
1968 "code use .set nobreak to turn off this warning."));
1974 if (nios2_special_relocation_p (argstr
))
1976 /* We zap the parentheses because we don't want them confused
1978 temp
= strchr (argstr
, '(');
1981 temp
= strchr (argstr
, ')');
1986 as_bad (_("badly formed expression near %s"), argstr
);
1993 /* We can't have %hi, %lo or %hiadj here. */
1995 as_bad (_("badly formed expression near %s"), argstr
);
2000 BAD_CASE (*parsestr
);
2007 /* The function consume_separator takes a pointer into a string
2008 of instruction tokens (args) and a pointer into a string representing
2009 the expected sequence of tokens and separators. It finds the first
2010 instance of the character pointed to by separator in argstr, and
2011 returns a pointer to the next element of argstr, which is the
2012 following token in the sequence. */
2014 nios2_consume_separator (char *argstr
, const char *separator
)
2018 /* If we have a opcode reg, expr(reg) type instruction, and
2019 * we are separating the expr from the (reg), we find the last
2020 * (, just in case the expression has parentheses. */
2022 if (*separator
== '(')
2023 p
= strrchr (argstr
, *separator
);
2025 p
= strchr (argstr
, *separator
);
2030 as_bad (_("expecting %c near %s"), *separator
, argstr
);
2035 /* The principal argument parsing function which takes a string argstr
2036 representing the instruction arguments for insn, and extracts the argument
2037 tokens matching parsestr into parsed_args. */
2039 nios2_parse_args (nios2_insn_infoS
*insn
, char *argstr
,
2040 const char *parsestr
, char **parsed_args
)
2047 bfd_boolean terminate
= FALSE
;
2049 /* This rest of this function is it too fragile and it mostly works,
2050 therefore special case this one. */
2051 if (*parsestr
== 0 && argstr
!= 0)
2053 as_bad (_("too many arguments"));
2054 parsed_args
[0] = NULL
;
2058 while (p
!= NULL
&& !terminate
&& i
< NIOS2_MAX_INSN_TOKENS
)
2060 parsed_args
[i
] = nios2_consume_arg (insn
, p
, parsestr
);
2062 if (*parsestr
!= '\0')
2064 p
= nios2_consume_separator (p
, parsestr
);
2069 /* Check that the argument string has no trailing arguments. */
2070 /* If we've got a %lo etc relocation, we've zapped the parens with
2072 if (nios2_special_relocation_p (p
))
2073 end
= strpbrk (p
, ",");
2075 end
= strpbrk (p
, " ,");
2078 as_bad (_("too many arguments"));
2081 if (*parsestr
== '\0' || (p
!= NULL
&& *p
== '\0'))
2086 parsed_args
[i
] = NULL
;
2088 /* The argument to break and trap instructions is optional; complain
2089 for other cases of missing arguments. */
2090 if (*parsestr
!= '\0'
2091 && insn
->insn_nios2_opcode
->match
!= OP_MATCH_BREAK
2092 && insn
->insn_nios2_opcode
->match
!= OP_MATCH_TRAP
)
2093 as_bad (_("missing argument"));
2098 /** Support for pseudo-op parsing. These are macro-like opcodes that
2099 expand into real insns by suitable fiddling with the operands. */
2101 /* Append the string modifier to the string contained in the argument at
2102 parsed_args[ndx]. */
2104 nios2_modify_arg (char **parsed_args
, const char *modifier
,
2105 int unused ATTRIBUTE_UNUSED
, int ndx
)
2107 char *tmp
= parsed_args
[ndx
];
2110 = (char *) malloc (strlen (parsed_args
[ndx
]) + strlen (modifier
) + 1);
2111 strcpy (parsed_args
[ndx
], tmp
);
2112 strcat (parsed_args
[ndx
], modifier
);
2115 /* Modify parsed_args[ndx] by negating that argument. */
2117 nios2_negate_arg (char **parsed_args
, const char *modifier ATTRIBUTE_UNUSED
,
2118 int unused ATTRIBUTE_UNUSED
, int ndx
)
2120 char *tmp
= parsed_args
[ndx
];
2123 = (char *) malloc (strlen ("~(") + strlen (parsed_args
[ndx
]) +
2124 strlen (")+1") + 1);
2126 strcpy (parsed_args
[ndx
], "~(");
2127 strcat (parsed_args
[ndx
], tmp
);
2128 strcat (parsed_args
[ndx
], ")+1");
2131 /* The function nios2_swap_args swaps the pointers at indices index_1 and
2132 index_2 in the array parsed_args[] - this is used for operand swapping
2133 for comparison operations. */
2135 nios2_swap_args (char **parsed_args
, const char *unused ATTRIBUTE_UNUSED
,
2136 int index_1
, int index_2
)
2139 gas_assert (index_1
< NIOS2_MAX_INSN_TOKENS
2140 && index_2
< NIOS2_MAX_INSN_TOKENS
);
2141 tmp
= parsed_args
[index_1
];
2142 parsed_args
[index_1
] = parsed_args
[index_2
];
2143 parsed_args
[index_2
] = tmp
;
2146 /* This function appends the string appnd to the array of strings in
2147 parsed_args num times starting at index start in the array. */
2149 nios2_append_arg (char **parsed_args
, const char *appnd
, int num
,
2155 gas_assert ((start
+ num
) < NIOS2_MAX_INSN_TOKENS
);
2157 if (nios2_mode
== NIOS2_MODE_TEST
)
2158 tmp
= parsed_args
[start
];
2162 for (i
= start
, count
= num
; count
> 0; ++i
, --count
)
2163 parsed_args
[i
] = (char *) appnd
;
2165 gas_assert (i
== (start
+ num
));
2166 parsed_args
[i
] = tmp
;
2167 parsed_args
[i
+ 1] = NULL
;
2170 /* This function inserts the string insert num times in the array
2171 parsed_args, starting at the index start. */
2173 nios2_insert_arg (char **parsed_args
, const char *insert
, int num
,
2178 gas_assert ((start
+ num
) < NIOS2_MAX_INSN_TOKENS
);
2180 /* Move the existing arguments up to create space. */
2181 for (i
= NIOS2_MAX_INSN_TOKENS
; i
- num
>= start
; --i
)
2182 parsed_args
[i
] = parsed_args
[i
- num
];
2184 for (i
= start
, count
= num
; count
> 0; ++i
, --count
)
2185 parsed_args
[i
] = (char *) insert
;
2188 /* Cleanup function to free malloc'ed arg strings. */
2190 nios2_free_arg (char **parsed_args
, int num ATTRIBUTE_UNUSED
, int start
)
2192 if (parsed_args
[start
])
2194 free (parsed_args
[start
]);
2195 parsed_args
[start
] = NULL
;
2199 /* This function swaps the pseudo-op for a real op. */
2200 static nios2_ps_insn_infoS
*
2201 nios2_translate_pseudo_insn (nios2_insn_infoS
*insn
)
2204 nios2_ps_insn_infoS
*ps_insn
;
2206 /* Find which real insn the pseudo-op transates to and
2207 switch the insn_info ptr to point to it. */
2208 ps_insn
= nios2_ps_lookup (insn
->insn_nios2_opcode
->name
);
2210 if (ps_insn
!= NULL
)
2212 insn
->insn_nios2_opcode
= nios2_opcode_lookup (ps_insn
->insn
);
2213 insn
->insn_tokens
[0] = insn
->insn_nios2_opcode
->name
;
2214 /* Modify the args so they work with the real insn. */
2215 ps_insn
->arg_modifer_func ((char **) insn
->insn_tokens
,
2216 ps_insn
->arg_modifier
, ps_insn
->num
,
2220 /* we cannot recover from this. */
2221 as_fatal (_("unrecognized pseudo-instruction %s"),
2222 ps_insn
->pseudo_insn
);
2226 /* Invoke the cleanup handler for pseudo-insn ps_insn on insn. */
2228 nios2_cleanup_pseudo_insn (nios2_insn_infoS
*insn
,
2229 nios2_ps_insn_infoS
*ps_insn
)
2231 if (ps_insn
->arg_cleanup_func
)
2232 (ps_insn
->arg_cleanup_func
) ((char **) insn
->insn_tokens
,
2233 ps_insn
->num
, ps_insn
->index
);
2236 const nios2_ps_insn_infoS nios2_ps_insn_info_structs
[] = {
2237 /* pseudo-op, real-op, arg, arg_modifier_func, num, index, arg_cleanup_func */
2238 {"mov", "add", nios2_append_arg
, "zero", 1, 3, NULL
},
2239 {"movi", "addi", nios2_insert_arg
, "zero", 1, 2, NULL
},
2240 {"movhi", "orhi", nios2_insert_arg
, "zero", 1, 2, NULL
},
2241 {"movui", "ori", nios2_insert_arg
, "zero", 1, 2, NULL
},
2242 {"movia", "orhi", nios2_insert_arg
, "zero", 1, 2, NULL
},
2243 {"nop", "add", nios2_append_arg
, "zero", 3, 1, NULL
},
2244 {"bgt", "blt", nios2_swap_args
, "", 1, 2, NULL
},
2245 {"bgtu", "bltu", nios2_swap_args
, "", 1, 2, NULL
},
2246 {"ble", "bge", nios2_swap_args
, "", 1, 2, NULL
},
2247 {"bleu", "bgeu", nios2_swap_args
, "", 1, 2, NULL
},
2248 {"cmpgt", "cmplt", nios2_swap_args
, "", 2, 3, NULL
},
2249 {"cmpgtu", "cmpltu", nios2_swap_args
, "", 2, 3, NULL
},
2250 {"cmple", "cmpge", nios2_swap_args
, "", 2, 3, NULL
},
2251 {"cmpleu", "cmpgeu", nios2_swap_args
, "", 2, 3, NULL
},
2252 {"cmpgti", "cmpgei", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2253 {"cmpgtui", "cmpgeui", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2254 {"cmplei", "cmplti", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2255 {"cmpleui", "cmpltui", nios2_modify_arg
, "+1", 0, 3, nios2_free_arg
},
2256 {"subi", "addi", nios2_negate_arg
, "", 0, 3, nios2_free_arg
}
2257 /* Add further pseudo-ops here. */
2260 #define NIOS2_NUM_PSEUDO_INSNS \
2261 ((sizeof(nios2_ps_insn_info_structs)/ \
2262 sizeof(nios2_ps_insn_info_structs[0])))
2263 const int nios2_num_ps_insn_info_structs
= NIOS2_NUM_PSEUDO_INSNS
;
2266 /** Assembler output support. */
2269 can_evaluate_expr (nios2_insn_infoS
*insn
)
2271 /* Remove this check for null and the invalid insn "ori r9, 1234" seg faults. */
2272 if (!insn
->insn_reloc
)
2273 /* ??? Ideally we should do something other than as_fatal here as we can
2274 continue to assemble.
2275 However this function (actually the output_* functions) should not
2276 have been called in the first place once an illegal instruction had
2277 been encountered. */
2278 as_fatal (_("Invalid instruction encountered, cannot recover. No assembly attempted."));
2280 if (insn
->insn_reloc
->reloc_expression
.X_op
== O_constant
)
2287 get_expr_value (nios2_insn_infoS
*insn
)
2291 if (insn
->insn_reloc
->reloc_expression
.X_op
== O_constant
)
2292 value
= insn
->insn_reloc
->reloc_expression
.X_add_number
;
2296 /* Output a normal instruction. */
2298 output_insn (nios2_insn_infoS
*insn
)
2301 nios2_insn_relocS
*reloc
;
2304 /* This allocates enough space for the instruction
2305 and puts it in the current frag. */
2306 md_number_to_chars (f
, insn
->insn_code
, 4);
2307 /* Emit debug info. */
2308 dwarf2_emit_insn (4);
2309 /* Create any fixups to be acted on later. */
2310 for (reloc
= insn
->insn_reloc
; reloc
!= NULL
; reloc
= reloc
->reloc_next
)
2311 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
, 4,
2312 &reloc
->reloc_expression
, reloc
->reloc_pcrel
,
2316 /* Output an unconditional branch. */
2318 output_ubranch (nios2_insn_infoS
*insn
)
2320 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2322 /* If the reloc is NULL, there was an error assembling the branch. */
2325 symbolS
*symp
= reloc
->reloc_expression
.X_add_symbol
;
2326 offsetT offset
= reloc
->reloc_expression
.X_add_number
;
2329 /* Tag dwarf2 debug info to the address at the start of the insn.
2330 We must do it before frag_var() below closes off the frag. */
2331 dwarf2_emit_insn (0);
2333 /* We create a machine dependent frag which can grow
2334 to accommodate the largest possible instruction sequence
2335 this may generate. */
2336 f
= frag_var (rs_machine_dependent
,
2337 UBRANCH_MAX_SIZE
, 4, UBRANCH_SUBTYPE (0),
2338 symp
, offset
, NULL
);
2340 md_number_to_chars (f
, insn
->insn_code
, 4);
2342 /* We leave fixup generation to md_convert_frag. */
2346 /* Output a conditional branch. */
2348 output_cbranch (nios2_insn_infoS
*insn
)
2350 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2352 /* If the reloc is NULL, there was an error assembling the branch. */
2355 symbolS
*symp
= reloc
->reloc_expression
.X_add_symbol
;
2356 offsetT offset
= reloc
->reloc_expression
.X_add_number
;
2359 /* Tag dwarf2 debug info to the address at the start of the insn.
2360 We must do it before frag_var() below closes off the frag. */
2361 dwarf2_emit_insn (0);
2363 /* We create a machine dependent frag which can grow
2364 to accommodate the largest possible instruction sequence
2365 this may generate. */
2366 f
= frag_var (rs_machine_dependent
,
2367 CBRANCH_MAX_SIZE
, 4, CBRANCH_SUBTYPE (0),
2368 symp
, offset
, NULL
);
2370 md_number_to_chars (f
, insn
->insn_code
, 4);
2372 /* We leave fixup generation to md_convert_frag. */
2376 /* Output a call sequence. Since calls are not pc-relative for NIOS2,
2377 but are page-relative, we cannot tell at any stage in assembly
2378 whether a call will be out of range since a section may be linked
2379 at any address. So if we are relaxing, we convert all call instructions
2380 to long call sequences, and rely on the linker to relax them back to
2383 output_call (nios2_insn_infoS
*insn
)
2385 /* This allocates enough space for the instruction
2386 and puts it in the current frag. */
2387 char *f
= frag_more (12);
2388 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2390 md_number_to_chars (f
, OP_MATCH_ORHI
| 0x00400000, 4);
2391 dwarf2_emit_insn (4);
2392 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
, 4,
2393 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_HI16
);
2394 md_number_to_chars (f
+ 4, OP_MATCH_ORI
| 0x08400000, 4);
2395 dwarf2_emit_insn (4);
2396 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ 4, 4,
2397 &reloc
->reloc_expression
, 0, BFD_RELOC_NIOS2_LO16
);
2398 md_number_to_chars (f
+ 8, OP_MATCH_CALLR
| 0x08000000, 4);
2399 dwarf2_emit_insn (4);
2402 /* Output an addi - will silently convert to
2403 orhi if rA = r0 and (expr & 0xffff0000) == 0. */
2405 output_addi (nios2_insn_infoS
*insn
)
2407 if (can_evaluate_expr (insn
))
2409 int expr_val
= get_expr_value (insn
);
2410 if (GET_INSN_FIELD (RRS
, insn
->insn_code
) == 0
2411 && (expr_val
& 0xffff) == 0
2414 /* We really want a movhi (orhi) here. */
2415 insn
->insn_code
= (insn
->insn_code
& ~OP_MATCH_ADDI
) | OP_MATCH_ORHI
;
2416 insn
->insn_reloc
->reloc_expression
.X_add_number
=
2417 (insn
->insn_reloc
->reloc_expression
.X_add_number
>> 16) & 0xffff;
2418 insn
->insn_reloc
->reloc_type
= BFD_RELOC_NIOS2_U16
;
2422 /* Output an instruction. */
2427 output_andi (nios2_insn_infoS
*insn
)
2429 if (can_evaluate_expr (insn
))
2431 int expr_val
= get_expr_value (insn
);
2432 if (expr_val
!= 0 && (expr_val
& 0xffff) == 0)
2434 /* We really want a movhi (orhi) here. */
2435 insn
->insn_code
= (insn
->insn_code
& ~OP_MATCH_ANDI
) | OP_MATCH_ANDHI
;
2436 insn
->insn_reloc
->reloc_expression
.X_add_number
=
2437 (insn
->insn_reloc
->reloc_expression
.X_add_number
>> 16) & 0xffff;
2438 insn
->insn_reloc
->reloc_type
= BFD_RELOC_NIOS2_U16
;
2442 /* Output an instruction. */
2447 output_ori (nios2_insn_infoS
*insn
)
2449 if (can_evaluate_expr (insn
))
2451 int expr_val
= get_expr_value (insn
);
2452 if (expr_val
!= 0 && (expr_val
& 0xffff) == 0)
2454 /* We really want a movhi (orhi) here. */
2455 insn
->insn_code
= (insn
->insn_code
& ~OP_MATCH_ORI
) | OP_MATCH_ORHI
;
2456 insn
->insn_reloc
->reloc_expression
.X_add_number
=
2457 (insn
->insn_reloc
->reloc_expression
.X_add_number
>> 16) & 0xffff;
2458 insn
->insn_reloc
->reloc_type
= BFD_RELOC_NIOS2_U16
;
2462 /* Output an instruction. */
2467 output_xori (nios2_insn_infoS
*insn
)
2469 if (can_evaluate_expr (insn
))
2471 int expr_val
= get_expr_value (insn
);
2472 if (expr_val
!= 0 && (expr_val
& 0xffff) == 0)
2474 /* We really want a movhi (orhi) here. */
2475 insn
->insn_code
= (insn
->insn_code
& ~OP_MATCH_XORI
) | OP_MATCH_XORHI
;
2476 insn
->insn_reloc
->reloc_expression
.X_add_number
=
2477 (insn
->insn_reloc
->reloc_expression
.X_add_number
>> 16) & 0xffff;
2478 insn
->insn_reloc
->reloc_type
= BFD_RELOC_NIOS2_U16
;
2482 /* Output an instruction. */
2487 /* Output a movhi/addi pair for the movia pseudo-op. */
2489 output_movia (nios2_insn_infoS
*insn
)
2491 /* This allocates enough space for the instruction
2492 and puts it in the current frag. */
2493 char *f
= frag_more (8);
2494 nios2_insn_relocS
*reloc
= insn
->insn_reloc
;
2495 unsigned long reg_index
= GET_INSN_FIELD (IRT
, insn
->insn_code
);
2497 /* If the reloc is NULL, there was an error assembling the movia. */
2500 md_number_to_chars (f
, insn
->insn_code
, 4);
2501 dwarf2_emit_insn (4);
2502 md_number_to_chars (f
+ 4,
2503 (OP_MATCH_ADDI
| (reg_index
<< OP_SH_IRT
)
2504 | (reg_index
<< OP_SH_IRS
)),
2506 dwarf2_emit_insn (4);
2507 fix_new (frag_now
, f
- frag_now
->fr_literal
, 4,
2508 reloc
->reloc_expression
.X_add_symbol
,
2509 reloc
->reloc_expression
.X_add_number
, 0,
2510 BFD_RELOC_NIOS2_HIADJ16
);
2511 fix_new (frag_now
, f
+ 4 - frag_now
->fr_literal
, 4,
2512 reloc
->reloc_expression
.X_add_symbol
,
2513 reloc
->reloc_expression
.X_add_number
, 0, BFD_RELOC_NIOS2_LO16
);
2519 /** External interfaces. */
2521 /* The following functions are called by machine-independent parts of
2524 md_parse_option (int c
, char *arg ATTRIBUTE_UNUSED
)
2529 /* Hidden option for self-test mode. */
2530 nios2_mode
= NIOS2_MODE_TEST
;
2532 case OPTION_RELAX_ALL
:
2533 nios2_as_options
.relax
= relax_all
;
2535 case OPTION_NORELAX
:
2536 nios2_as_options
.relax
= relax_none
;
2538 case OPTION_RELAX_SECTION
:
2539 nios2_as_options
.relax
= relax_section
;
2542 target_big_endian
= 1;
2545 target_big_endian
= 0;
2555 /* Implement TARGET_FORMAT. We can choose to be big-endian or
2556 little-endian at runtime based on a switch. */
2558 nios2_target_format (void)
2560 return target_big_endian
? "elf32-bignios2" : "elf32-littlenios2";
2563 /* Machine-dependent usage message. */
2565 md_show_usage (FILE *stream
)
2567 fprintf (stream
, " NIOS2 options:\n"
2568 " -relax-all replace all branch and call "
2569 "instructions with jmp and callr sequences\n"
2570 " -relax-section replace identified out of range "
2571 "branches with jmp sequences (default)\n"
2572 " -no-relax do not replace any branches or calls\n"
2573 " -EB force big-endian byte ordering\n"
2574 " -EL force little-endian byte ordering\n");
2577 /* This function is called once, at assembler startup time.
2578 It should set up all the tables, etc. that the MD part of the
2579 assembler will need. */
2584 const char *inserted
;
2586 /* Create and fill a hashtable for the Nios II opcodes, registers and
2588 nios2_opcode_hash
= hash_new ();
2589 nios2_reg_hash
= hash_new ();
2590 nios2_arg_hash
= hash_new ();
2591 nios2_ps_hash
= hash_new ();
2593 for (i
= 0; i
< NUMOPCODES
; ++i
)
2596 = hash_insert (nios2_opcode_hash
, nios2_opcodes
[i
].name
,
2597 (PTR
) & nios2_opcodes
[i
]);
2598 if (inserted
!= NULL
)
2600 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
2601 nios2_opcodes
[i
].name
, inserted
);
2602 /* Probably a memory allocation problem? Give up now. */
2603 as_fatal (_("Broken assembler. No assembly attempted."));
2607 for (i
= 0; i
< nios2_num_regs
; ++i
)
2610 = hash_insert (nios2_reg_hash
, nios2_regs
[i
].name
,
2611 (PTR
) & nios2_regs
[i
]);
2612 if (inserted
!= NULL
)
2614 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
2615 nios2_regs
[i
].name
, inserted
);
2616 /* Probably a memory allocation problem? Give up now. */
2617 as_fatal (_("Broken assembler. No assembly attempted."));
2622 for (i
= 0; i
< nios2_num_arg_info_structs
; ++i
)
2625 = hash_insert (nios2_arg_hash
, nios2_arg_info_structs
[i
].args
,
2626 (PTR
) & nios2_arg_info_structs
[i
]);
2627 if (inserted
!= NULL
)
2629 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
2630 nios2_arg_info_structs
[i
].args
, inserted
);
2631 /* Probably a memory allocation problem? Give up now. */
2632 as_fatal (_("Broken assembler. No assembly attempted."));
2636 for (i
= 0; i
< nios2_num_ps_insn_info_structs
; ++i
)
2639 = hash_insert (nios2_ps_hash
, nios2_ps_insn_info_structs
[i
].pseudo_insn
,
2640 (PTR
) & nios2_ps_insn_info_structs
[i
]);
2641 if (inserted
!= NULL
)
2643 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
2644 nios2_ps_insn_info_structs
[i
].pseudo_insn
, inserted
);
2645 /* Probably a memory allocation problem? Give up now. */
2646 as_fatal (_("Broken assembler. No assembly attempted."));
2650 /* Assembler option defaults. */
2651 nios2_as_options
.noat
= FALSE
;
2652 nios2_as_options
.nobreak
= FALSE
;
2654 /* Debug information is incompatible with relaxation. */
2655 if (debug_type
!= DEBUG_UNSPECIFIED
)
2656 nios2_as_options
.relax
= relax_none
;
2658 /* Initialize the alignment data. */
2659 nios2_current_align_seg
= now_seg
;
2660 nios2_last_label
= NULL
;
2661 nios2_current_align
= 0;
2665 /* Assembles a single line of Nios II assembly language. */
2667 md_assemble (char *op_str
)
2670 char *op_strdup
= NULL
;
2671 nios2_arg_infoS
*arg_info
;
2672 unsigned long saved_pinfo
= 0;
2673 nios2_insn_infoS thisinsn
;
2674 nios2_insn_infoS
*insn
= &thisinsn
;
2676 /* Make sure we are aligned on a 4-byte boundary. */
2677 if (nios2_current_align
< 2)
2678 nios2_align (2, NULL
, nios2_last_label
);
2679 else if (nios2_current_align
> 2)
2680 nios2_current_align
= 2;
2681 nios2_last_label
= NULL
;
2683 /* We don't want to clobber to op_str
2684 because we want to be able to use it in messages. */
2685 op_strdup
= strdup (op_str
);
2686 insn
->insn_tokens
[0] = strtok (op_strdup
, " ");
2687 argstr
= strtok (NULL
, "");
2689 /* Assemble the opcode. */
2690 insn
->insn_nios2_opcode
= nios2_opcode_lookup (insn
->insn_tokens
[0]);
2691 insn
->insn_reloc
= NULL
;
2693 if (insn
->insn_nios2_opcode
!= NULL
)
2695 nios2_ps_insn_infoS
*ps_insn
= NULL
;
2696 /* Set the opcode for the instruction. */
2697 insn
->insn_code
= insn
->insn_nios2_opcode
->match
;
2699 /* Parse the arguments pointed to by argstr. */
2700 if (nios2_mode
== NIOS2_MODE_ASSEMBLE
)
2701 nios2_parse_args (insn
, argstr
, insn
->insn_nios2_opcode
->args
,
2702 (char **) &insn
->insn_tokens
[1]);
2704 nios2_parse_args (insn
, argstr
, insn
->insn_nios2_opcode
->args_test
,
2705 (char **) &insn
->insn_tokens
[1]);
2707 /* We need to preserve the MOVIA macro as this is clobbered by
2708 translate_pseudo_insn. */
2709 if (insn
->insn_nios2_opcode
->pinfo
== NIOS2_INSN_MACRO_MOVIA
)
2710 saved_pinfo
= NIOS2_INSN_MACRO_MOVIA
;
2711 /* If the instruction is an pseudo-instruction, we want to replace it
2712 with its real equivalent, and then continue. */
2713 if ((insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_MACRO
)
2714 == NIOS2_INSN_MACRO
)
2715 ps_insn
= nios2_translate_pseudo_insn (insn
);
2717 /* Find the assemble function, and call it. */
2718 arg_info
= nios2_arg_lookup (insn
->insn_nios2_opcode
->args
);
2719 if (arg_info
!= NULL
)
2721 arg_info
->assemble_args_func (insn
);
2723 if (nios2_as_options
.relax
!= relax_none
2724 && !nios2_as_options
.noat
2725 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_UBRANCH
)
2726 output_ubranch (insn
);
2727 else if (nios2_as_options
.relax
!= relax_none
2728 && !nios2_as_options
.noat
2729 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_CBRANCH
)
2730 output_cbranch (insn
);
2731 else if (nios2_as_options
.relax
== relax_all
2732 && !nios2_as_options
.noat
2733 && insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_CALL
2735 && ((insn
->insn_reloc
->reloc_type
2736 == BFD_RELOC_NIOS2_CALL26
)
2737 || (insn
->insn_reloc
->reloc_type
2738 == BFD_RELOC_NIOS2_CALL26_NOAT
)))
2740 else if (insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_ANDI
)
2742 else if (insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_ORI
)
2744 else if (insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_XORI
)
2746 else if (insn
->insn_nios2_opcode
->pinfo
& NIOS2_INSN_ADDI
)
2748 else if (saved_pinfo
== NIOS2_INSN_MACRO_MOVIA
)
2749 output_movia (insn
);
2753 nios2_cleanup_pseudo_insn (insn
, ps_insn
);
2757 /* The assembler is broken. */
2759 _("internal error: %s is not a valid argument syntax\n"),
2760 insn
->insn_nios2_opcode
->args
);
2761 /* Probably a memory allocation problem. Give up now. */
2762 as_fatal (_("Broken assembler. No assembly attempted."));
2766 /* Unrecognised instruction - error. */
2767 as_bad (_("unrecognised instruction %s"), insn
->insn_tokens
[0]);
2769 /* Don't leak memory. */
2773 /* Round up section size. */
2775 md_section_align (asection
*seg ATTRIBUTE_UNUSED
, valueT size
)
2777 /* I think byte alignment is fine here. */
2781 /* Implement TC_FORCE_RELOCATION. */
2783 nios2_force_relocation (fixS
*fixp
)
2785 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
2786 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
2787 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_ALIGN
)
2790 return generic_force_reloc (fixp
);
2793 /* Implement tc_fix_adjustable. */
2795 nios2_fix_adjustable (fixS
*fixp
)
2797 if (fixp
->fx_addsy
== NULL
)
2801 /* Prevent all adjustments to global symbols. */
2802 if (OUTPUT_FLAVOR
== bfd_target_elf_flavour
2803 && (S_IS_EXTERNAL (fixp
->fx_addsy
) || S_IS_WEAK (fixp
->fx_addsy
)))
2806 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
2807 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
2810 /* Preserve relocations against symbols with function type. */
2811 if (symbol_get_bfdsym (fixp
->fx_addsy
)->flags
& BSF_FUNCTION
)
2814 /* Don't allow symbols to be discarded on GOT related relocs. */
2815 if (fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOT16
2816 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_CALL16
2817 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_LO
2818 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF_HA
2819 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_GD16
2820 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDM16
2821 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LDO16
2822 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_IE16
2823 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_LE16
2824 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPMOD
2825 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_DTPREL
2826 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_TLS_TPREL
2827 || fixp
->fx_r_type
== BFD_RELOC_NIOS2_GOTOFF
)
2833 /* Implement tc_frob_symbol. This is called in adjust_reloc_syms;
2834 it is used to remove *ABS* references from the symbol table. */
2836 nios2_frob_symbol (symbolS
*symp
)
2838 if ((OUTPUT_FLAVOR
== bfd_target_elf_flavour
2839 && symp
== section_symbol (absolute_section
))
2840 || !S_IS_DEFINED (symp
))
2846 /* The function tc_gen_reloc creates a relocation structure for the
2847 fixup fixp, and returns a pointer to it. This structure is passed
2848 to bfd_install_relocation so that it can be written to the object
2849 file for linking. */
2851 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
2853 arelent
*reloc
= (arelent
*) xmalloc (sizeof (arelent
));
2854 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
2855 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
2857 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
2858 reloc
->addend
= fixp
->fx_offset
; /* fixp->fx_addnumber; */
2862 switch (fixp
->fx_r_type
)
2865 fixp
->fx_r_type
= BFD_RELOC_16_PCREL
;
2867 case BFD_RELOC_NIOS2_LO16
:
2868 fixp
->fx_r_type
= BFD_RELOC_NIOS2_PCREL_LO
;
2870 case BFD_RELOC_NIOS2_HIADJ16
:
2871 fixp
->fx_r_type
= BFD_RELOC_NIOS2_PCREL_HA
;
2878 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
2879 if (reloc
->howto
== NULL
)
2881 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2882 _("can't represent relocation type %s"),
2883 bfd_get_reloc_code_name (fixp
->fx_r_type
));
2885 /* Set howto to a garbage value so that we can keep going. */
2886 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, BFD_RELOC_32
);
2887 gas_assert (reloc
->howto
!= NULL
);
2893 md_pcrel_from (fixS
*fixP ATTRIBUTE_UNUSED
)
2898 /* Called just before the assembler exits. */
2902 /* FIXME - not yet implemented */
2905 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
2906 Otherwise we have no need to default values of symbols. */
2908 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
2911 if (name
[0] == '_' && name
[1] == 'G'
2912 && strcmp (name
, GLOBAL_OFFSET_TABLE_NAME
) == 0)
2916 if (symbol_find (name
))
2917 as_bad ("GOT already in the symbol table");
2919 GOT_symbol
= symbol_new (name
, undefined_section
,
2920 (valueT
) 0, &zero_address_frag
);
2930 /* Implement tc_frob_label. */
2932 nios2_frob_label (symbolS
*lab
)
2934 /* Emit dwarf information. */
2935 dwarf2_emit_label (lab
);
2937 /* Update the label's address with the current output pointer. */
2938 symbol_set_frag (lab
, frag_now
);
2939 S_SET_VALUE (lab
, (valueT
) frag_now_fix ());
2941 /* Record this label for future adjustment after we find out what
2942 kind of data it references, and the required alignment therewith. */
2943 nios2_last_label
= lab
;
2946 /* Implement md_cons_align. */
2948 nios2_cons_align (int size
)
2951 const char *pfill
= NULL
;
2953 while ((size
>>= 1) != 0)
2956 if (subseg_text_p (now_seg
))
2957 pfill
= (const char *) &nop
;
2961 if (nios2_auto_align_on
)
2962 nios2_align (log_size
, pfill
, NULL
);
2964 nios2_last_label
= NULL
;
2967 /* Map 's' to SHF_NIOS2_GPREL. */
2968 /* This is from the Alpha code tc-alpha.c. */
2970 nios2_elf_section_letter (int letter
, char **ptr_msg
)
2973 return SHF_NIOS2_GPREL
;
2975 *ptr_msg
= _("Bad .section directive: want a,s,w,x,M,S,G,T in string");
2979 /* Map SHF_ALPHA_GPREL to SEC_SMALL_DATA. */
2980 /* This is from the Alpha code tc-alpha.c. */
2982 nios2_elf_section_flags (flagword flags
, int attr
, int type ATTRIBUTE_UNUSED
)
2984 if (attr
& SHF_NIOS2_GPREL
)
2985 flags
|= SEC_SMALL_DATA
;
2989 /* Implement TC_PARSE_CONS_EXPRESSION to handle %tls_ldo(...) */
2990 static int nios2_tls_ldo_reloc
;
2993 nios2_cons (expressionS
*exp
, int size
)
2995 nios2_tls_ldo_reloc
= 0;
2998 if (input_line_pointer
[0] == '%')
3000 if (strprefix (input_line_pointer
+ 1, "tls_ldo"))
3003 as_bad (_("Illegal operands: %%tls_ldo in %d-byte data field"),
3007 input_line_pointer
+= 8;
3008 nios2_tls_ldo_reloc
= 1;
3011 if (nios2_tls_ldo_reloc
)
3014 if (input_line_pointer
[0] != '(')
3015 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
3019 char *end
= ++input_line_pointer
;
3022 for (c
= *end
; !is_end_of_line
[c
]; end
++, c
= *end
)
3033 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
3039 if (input_line_pointer
!= end
)
3040 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
3043 input_line_pointer
++;
3045 c
= *input_line_pointer
;
3046 if (! is_end_of_line
[c
] && c
!= ',')
3047 as_bad (_("Illegal operands: garbage after %%tls_ldo()"));
3053 if (!nios2_tls_ldo_reloc
)
3057 /* Implement TC_CONS_FIX_NEW. */
3059 nios2_cons_fix_new (fragS
*frag
, int where
, unsigned int nbytes
,
3062 bfd_reloc_code_real_type r
;
3064 r
= (nbytes
== 1 ? BFD_RELOC_8
3065 : (nbytes
== 2 ? BFD_RELOC_16
3066 : (nbytes
== 4 ? BFD_RELOC_32
: BFD_RELOC_64
)));
3068 if (nios2_tls_ldo_reloc
)
3069 r
= BFD_RELOC_NIOS2_TLS_DTPREL
;
3071 fix_new_exp (frag
, where
, (int) nbytes
, exp
, 0, r
);
3072 nios2_tls_ldo_reloc
= 0;
3075 /* Implement HANDLE_ALIGN. */
3077 nios2_handle_align (fragS
*fragp
)
3079 /* If we are expecting to relax in the linker, then we must output a
3080 relocation to tell the linker we are aligning code. */
3081 if (nios2_as_options
.relax
== relax_all
3082 && (fragp
->fr_type
== rs_align
|| fragp
->fr_type
== rs_align_code
)
3083 && fragp
->fr_address
+ fragp
->fr_fix
> 0
3084 && fragp
->fr_offset
> 1
3085 && now_seg
!= bss_section
)
3086 fix_new (fragp
, fragp
->fr_fix
, 0, &abs_symbol
, fragp
->fr_offset
, 0,
3087 BFD_RELOC_NIOS2_ALIGN
);
3090 /* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
3093 nios2_regname_to_dw2regnum (char *regname
)
3095 struct nios2_reg
*r
= nios2_reg_lookup (regname
);
3101 /* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
3102 unwind information for this procedure. */
3104 nios2_frame_initial_instructions (void)
3106 cfi_add_CFA_def_cfa (27, 0);