gdb: microblaze: delete useless stubs
[deliverable/binutils-gdb.git] / gas / config / tc-nios2.c
CommitLineData
36591ba1 1/* Altera Nios II assembler.
b90efa5b 2 Copyright (C) 2012-2015 Free Software Foundation, Inc.
36591ba1
SL
3 Contributed by Nigel Gray (ngray@altera.com).
4 Contributed by Mentor Graphics, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
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)
11 any later version.
12
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.
17
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
21 02110-1301, USA. */
22
23#include "as.h"
24#include "opcode/nios2.h"
25#include "elf/nios2.h"
26#include "tc-nios2.h"
27#include "bfd.h"
28#include "dwarf2dbg.h"
29#include "subsegs.h"
30#include "safe-ctype.h"
31#include "dw2gencfi.h"
32
33#ifndef OBJ_ELF
34/* We are not supporting any other target so we throw a compile time error. */
35OBJ_ELF not defined
36#endif
37
38/* We can choose our endianness at run-time, regardless of configuration. */
39extern int target_big_endian;
40
41/* This array holds the chars that always start a comment. If the
42 pre-processor is disabled, these aren't very useful. */
43const char comment_chars[] = "#";
44
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. */
52const char line_comment_chars[] = "#";
53
54/* This array holds machine specific line separator characters. */
55const char line_separator_chars[] = ";";
56
57/* Chars that can be used to separate mant from exp in floating point nums. */
58const char EXP_CHARS[] = "eE";
59
60/* Chars that mean this number is a floating point constant. */
61/* As in 0f12.456 */
62/* or 0d1.2345e12 */
63const char FLT_CHARS[] = "rRsSfFdDxXpP";
64
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. */
68
69/* Machine-dependent command-line options. */
70
71const char *md_shortopts = "r";
72
73struct option md_longopts[] = {
74#define OPTION_RELAX_ALL (OPTION_MD_BASE + 0)
75 {"relax-all", no_argument, NULL, OPTION_RELAX_ALL},
76#define OPTION_NORELAX (OPTION_MD_BASE + 1)
77 {"no-relax", no_argument, NULL, OPTION_NORELAX},
78#define OPTION_RELAX_SECTION (OPTION_MD_BASE + 2)
79 {"relax-section", no_argument, NULL, OPTION_RELAX_SECTION},
80#define OPTION_EB (OPTION_MD_BASE + 3)
81 {"EB", no_argument, NULL, OPTION_EB},
82#define OPTION_EL (OPTION_MD_BASE + 4)
83 {"EL", no_argument, NULL, OPTION_EL}
84};
85
86size_t md_longopts_size = sizeof (md_longopts);
87
88/* The assembler supports three different relaxation modes, controlled by
89 command-line options. */
90typedef enum
91{
92 relax_section = 0,
93 relax_none,
94 relax_all
95} relax_optionT;
96
97/* Struct contains all assembler options set with .set. */
98struct
99{
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. */
104 bfd_boolean noat;
105
106 /* .set nobreak -> nobreak = 1 allows assembly code to use ba,bt without
107 warning.
108 .set break -> nobreak = 0, assembly code using ba,bt warns. */
109 bfd_boolean nobreak;
110
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
115 a section. */
116 relax_optionT relax;
117
118} nios2_as_options = {FALSE, FALSE, relax_section};
119
120
121typedef struct nios2_insn_reloc
122{
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;
126
127 /* The type of the relocation to be applied. */
128 bfd_reloc_code_real_type reloc_type;
129
130 /* PC-relative. */
131 unsigned int reloc_pcrel;
132
133 /* The next relocation to be applied to the instruction. */
134 struct nios2_insn_reloc *reloc_next;
135} nios2_insn_relocS;
136
137/* This struct is used to hold state when assembling instructions. */
138typedef struct nios2_insn_info
139{
140 /* Assembled instruction. */
141 unsigned long insn_code;
96ba4233
SL
142
143 /* Constant bits masked into insn_code for self-check mode. */
144 unsigned long constant_bits;
145
36591ba1
SL
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];
153
154 /* This holds information used to generate fixups
155 and eventually relocations if it is not null. */
156 nios2_insn_relocS *insn_reloc;
157} nios2_insn_infoS;
158
36591ba1
SL
159
160/* This struct is used to convert Nios II pseudo-ops into the
161 corresponding real op. */
162typedef struct nios2_ps_insn_info
163{
164 /* Map this pseudo_op... */
165 const char *pseudo_insn;
166
167 /* ...to this real instruction. */
168 const char *insn;
169
170 /* Call this function to modify the operands.... */
171 void (*arg_modifer_func) (char ** parsed_args, const char *arg, int num,
172 int start);
173
174 /* ...with these arguments. */
175 const char *arg_modifier;
176 int num;
177 int index;
178
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;
183
184/* Opcode hash table. */
185static struct hash_control *nios2_opcode_hash = NULL;
186#define nios2_opcode_lookup(NAME) \
187 ((struct nios2_opcode *) hash_find (nios2_opcode_hash, (NAME)))
188
189/* Register hash table. */
190static struct hash_control *nios2_reg_hash = NULL;
191#define nios2_reg_lookup(NAME) \
192 ((struct nios2_reg *) hash_find (nios2_reg_hash, (NAME)))
193
36591ba1
SL
194
195/* Pseudo-op hash table. */
196static struct hash_control *nios2_ps_hash = NULL;
197#define nios2_ps_lookup(NAME) \
198 ((nios2_ps_insn_infoS *) hash_find (nios2_ps_hash, (NAME)))
199
200/* The known current alignment of the current section. */
201static int nios2_current_align;
202static segT nios2_current_align_seg;
203
204static int nios2_auto_align_on = 1;
205
206/* The last seen label in the current section. This is used to auto-align
207 labels preceeding instructions. */
208static symbolS *nios2_last_label;
209
210#ifdef OBJ_ELF
211/* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
212symbolS *GOT_symbol;
213#endif
214
215\f
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. */
220static valueT
221md_chars_to_number (char *buf, int n)
222{
223 int i;
224 valueT val;
225
226 gas_assert (n == 1 || n == 2 || n == 4);
227
228 val = 0;
229 if (target_big_endian)
230 for (i = 0; i < n; ++i)
231 val = val | ((buf[i] & 0xff) << 8 * (n - (i + 1)));
232 else
233 for (i = 0; i < n; ++i)
234 val = val | ((buf[i] & 0xff) << 8 * i);
235 return val;
236}
237
238
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. */
242void
243md_number_to_chars (char *buf, valueT val, int n)
244{
245 gas_assert (n == 1 || n == 2 || n == 4 || n == 8);
246 if (target_big_endian)
247 number_to_chars_bigendian (buf, val, n);
248 else
249 number_to_chars_littleendian (buf, val, n);
250}
251
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. */
256char *
257md_atof (int type, char *litP, int *sizeP)
258{
259 int prec;
260 LITTLENUM_TYPE words[4];
261 char *t;
262 int i;
263
264 switch (type)
265 {
266 case 'f':
267 prec = 2;
268 break;
269 case 'd':
270 prec = 4;
271 break;
272 default:
273 *sizeP = 0;
274 return _("bad call to md_atof");
275 }
276
277 t = atof_ieee (input_line_pointer, type, words);
278 if (t)
279 input_line_pointer = t;
280
281 *sizeP = prec * 2;
282
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);
286 else
287 for (i = 0; i < prec; i++, litP += 2)
288 md_number_to_chars (litP, (valueT) words[i], 2);
289
290 return NULL;
291}
292
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)
296
36591ba1
SL
297
298/* Return true if STR is prefixed with a special relocation operator. */
299static int
300nios2_special_relocation_p (const char *str)
301{
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"));
316}
317
36591ba1
SL
318
319/* nop fill pattern for text section. */
320static char const nop[4] = { 0x3a, 0x88, 0x01, 0x00 };
321
322/* Handles all machine-dependent alignment needs. */
323static void
324nios2_align (int log_size, const char *pfill, symbolS *label)
325{
326 int align;
327 long max_alignment = 15;
328
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);
332
333 align = log_size;
334 if (align > max_alignment)
335 {
336 align = max_alignment;
337 as_bad (_("Alignment too large: %d. assumed"), align);
338 }
339 else if (align < 0)
340 {
341 as_warn (_("Alignment negative: 0 assumed"));
342 align = 0;
343 }
344
345 if (align != 0)
346 {
347 if (subseg_text_p (now_seg) && align >= 2)
348 {
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);
353
354 /* Now fill in the alignment pattern. */
355 if (pfill != NULL)
356 frag_align_pattern (align, pfill, sizeof nop, 0);
357 else
358 frag_align (align, 0, 0);
359 }
360 else
361 frag_align (align, 0, 0);
362
363 if (!switched_seg_p)
364 nios2_current_align = align;
365
366 /* If the last label was in a different section we can't align it. */
367 if (label != NULL && !switched_seg_p)
368 {
369 symbolS *sym;
370 int label_seen = FALSE;
371 struct frag *old_frag;
372 valueT old_value;
373 valueT new_value;
374
375 gas_assert (S_GET_SEGMENT (label) == now_seg);
376
377 old_frag = symbol_get_frag (label);
378 old_value = S_GET_VALUE (label);
379 new_value = (valueT) frag_now_fix ();
380
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)
395 {
396 label_seen = TRUE;
397 symbol_set_frag (sym, frag_now);
398 S_SET_VALUE (sym, new_value);
399 }
400 else if (label_seen && symbol_get_frag (sym) != old_frag)
401 break;
402 }
403 record_alignment (now_seg, align);
404 }
405}
406
407\f
408/** Support for self-check mode. */
409
410/* Mode of the assembler. */
411typedef enum
412{
413 NIOS2_MODE_ASSEMBLE, /* Ordinary operation. */
414 NIOS2_MODE_TEST /* Hidden mode used for self testing. */
415} NIOS2_MODE;
416
417static NIOS2_MODE nios2_mode = NIOS2_MODE_ASSEMBLE;
418
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. */
423static void
424nios2_check_assembly (unsigned int opcode, const char *exp_opcode)
425{
426 if (nios2_mode == NIOS2_MODE_TEST)
427 {
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);
432 }
433}
434
435\f
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. */
440static void
441s_nios2_align (int ignore ATTRIBUTE_UNUSED)
442{
443 int align;
444 char fill;
445 const char *pfill = NULL;
446 long max_alignment = 15;
447
448 align = get_absolute_expression ();
449 if (align > max_alignment)
450 {
451 align = max_alignment;
452 as_bad (_("Alignment too large: %d. assumed"), align);
453 }
454 else if (align < 0)
455 {
456 as_warn (_("Alignment negative: 0 assumed"));
457 align = 0;
458 }
459
460 if (*input_line_pointer == ',')
461 {
462 input_line_pointer++;
463 fill = get_absolute_expression ();
464 pfill = (const char *) &fill;
465 }
466 else if (subseg_text_p (now_seg))
467 pfill = (const char *) &nop;
468 else
469 {
470 pfill = NULL;
471 nios2_last_label = NULL;
472 }
473
474 if (align != 0)
475 {
476 nios2_auto_align_on = 1;
477 nios2_align (align, pfill, nios2_last_label);
478 nios2_last_label = NULL;
479 }
480 else
481 nios2_auto_align_on = 0;
482
483 demand_empty_rest_of_line ();
484}
485
486/* Handle the .text pseudo-op. This is like the usual one, but it
487 clears the saved last label and resets known alignment. */
488static void
489s_nios2_text (int i)
490{
491 s_text (i);
492 nios2_last_label = NULL;
493 nios2_current_align = 0;
494 nios2_current_align_seg = now_seg;
495}
496
497/* Handle the .data pseudo-op. This is like the usual one, but it
498 clears the saved last label and resets known alignment. */
499static void
500s_nios2_data (int i)
501{
502 s_data (i);
503 nios2_last_label = NULL;
504 nios2_current_align = 0;
505 nios2_current_align_seg = now_seg;
506}
507
508/* Handle the .section pseudo-op. This is like the usual one, but it
509 clears the saved last label and resets known alignment. */
510static void
511s_nios2_section (int ignore)
512{
513 obj_elf_section (ignore);
514 nios2_last_label = NULL;
515 nios2_current_align = 0;
516 nios2_current_align_seg = now_seg;
517}
518
519/* Explicitly unaligned cons. */
520static void
521s_nios2_ucons (int nbytes)
522{
523 int hold;
524 hold = nios2_auto_align_on;
525 nios2_auto_align_on = 0;
526 cons (nbytes);
527 nios2_auto_align_on = hold;
528}
529
530/* Handle the .sdata directive. */
531static void
532s_nios2_sdata (int ignore ATTRIBUTE_UNUSED)
533{
534 get_absolute_expression (); /* Ignored. */
535 subseg_new (".sdata", 0);
536 demand_empty_rest_of_line ();
537}
538
539/* .set sets assembler options eg noat/at and is also used
540 to set symbol values (.equ, .equiv ). */
541static void
542s_nios2_set (int equiv)
543{
544 char *directive = input_line_pointer;
545 char delim = get_symbol_end ();
546 char *endline = input_line_pointer;
547 *endline = delim;
548
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. */
553 SKIP_WHITESPACE ();
554 if (is_end_of_line[(unsigned char) *input_line_pointer])
555 {
556 bfd_boolean done = TRUE;
557 *endline = 0;
558
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;
573 else
574 done = FALSE;
575
576 if (done)
577 {
578 *endline = delim;
579 demand_empty_rest_of_line ();
580 return;
581 }
582 }
583
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
586 a syntax error. */
587 input_line_pointer = directive;
588 *endline = delim;
589 s_set (equiv);
590}
591
592/* Machine-dependent assembler directives.
593 Format of each entry is:
594 { "directive", handler_func, param } */
595const 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. */
604 {"dword", cons, 8},
605 {"half", cons, 2},
606 /* NIOS2 native word size is 4 bytes, so we override
607 the GAS default of 2. */
608 {"word", cons, 4},
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},
614#ifdef OBJ_ELF
615 {"sdata", s_nios2_sdata, 0},
616#endif
617 {"set", s_nios2_set, 0},
618 {NULL, NULL, 0}
619};
620
621\f
622/** Relaxation support. */
623
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.
626
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
629 using two registers.
630
631 To deal with this, for the pc-relative relaxation mode we convert
632 br label
633 into a series of 16-bit adds, like:
634 nextpc at
635 addi at, at, 32767
636 ...
637 addi at, at, remainder
638 jmp at
639
640 Similarly, conditional branches are converted from
641 b(condition) r, s, label
642 into a series like:
643 b(opposite condition) r, s, skip
644 nextpc at
645 addi at, at, 32767
646 ...
647 addi at, at, remainder
648 jmp at
649 skip:
650
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.
654
655 For the -relax-all relaxation mode, the conversions are
656 movhi at, %hi(symbol+offset)
657 ori at, %lo(symbol+offset)
658 jmp at
659 and
660 b(opposite condition), r, s, skip
661 movhi at, %hi(symbol+offset)
662 ori at, %lo(symbol+offset)
663 jmp at
664 skip:
665 respectively.
666*/
667
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
678
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
682 conditional branch.
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)
695
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)
700
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
705
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))
715
716/* Register number of AT, the assembler temporary. */
717#define AT_REGNUM 1
718
719/* Determine how many bytes are required to represent the sequence
720 indicated by SUBTYPE. */
721static int
722nios2_relax_subtype_size (relax_substateT subtype)
723{
724 int n = SUBTYPE_ADDIS (subtype);
725 if (n == 0)
726 /* Regular conditional/unconditional branch instruction. */
727 return 4;
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);
732 else
733 return UBRANCH_ADDIS_TO_SIZE (n);
734}
735
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. */
739int
740md_estimate_size_before_relax (fragS *fragp, segT segment ATTRIBUTE_UNUSED)
741{
742 return nios2_relax_subtype_size (fragp->fr_subtype);
743}
744
745/* Implement md_relax_frag, returning the change in size of the frag. */
746long
747nios2_relax_frag (segT segment, fragS *fragp, long stretch)
748{
749 addressT target = fragp->fr_offset;
750 relax_substateT subtype = fragp->fr_subtype;
751 symbolS *symbolp = fragp->fr_symbol;
752
753 if (symbolp)
754 {
755 fragS *sym_frag = symbol_get_frag (symbolp);
756 offsetT offset;
757 int n;
758
759 target += S_GET_VALUE (symbolp);
760
761 /* See comments in write.c:relax_frag about handling of stretch. */
762 if (stretch != 0
763 && sym_frag->relax_marker != fragp->relax_marker)
764 {
765 if (stretch < 0 || sym_frag->region == fragp->region)
766 target += stretch;
767 else if (target < fragp->fr_address)
768 target = fragp->fr_next->fr_address + stretch;
769 }
770
96ba4233
SL
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;
36591ba1
SL
774 if (offset >= -32768 && offset <= 32764)
775 /* Fits in PC-relative branch. */
776 n = 0;
777 else if (nios2_as_options.relax == relax_all)
778 /* Convert to jump. */
779 n = 1;
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. */
785 {
786 /* The relaxation for conditional branches is offset by 4
787 bytes because we insert the inverted branch around the
788 sequence. */
789 if (IS_CBRANCH (subtype))
790 offset = offset - 4;
791 if (offset > 0)
792 n = offset / 32767 + 1;
793 else
794 n = offset / -32768 + 1;
795
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)
803 {
804 as_bad_where (fragp->fr_file, fragp->fr_line,
805 _("branch offset out of range\n"));
806 as_fatal (_("branch relaxation failed\n"));
807 }
808 }
809 else
810 /* We cannot handle this case, diagnose overflow later. */
811 return 0;
812
813 if (IS_CBRANCH (subtype))
814 fragp->fr_subtype = CBRANCH_SUBTYPE (n);
815 else
816 fragp->fr_subtype = UBRANCH_SUBTYPE (n);
817
818 return (nios2_relax_subtype_size (fragp->fr_subtype)
819 - nios2_relax_subtype_size (subtype));
820 }
821
822 /* If we got here, it's probably an error. */
823 return 0;
824}
825
826
827/* Complete fragp using the data from the relaxation pass. */
828void
829md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED,
830 fragS *fragp)
831{
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;
837 offsetT offset;
838 unsigned int addend_mask, addi_mask;
839 offsetT addend, remainder;
840 int i;
841
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. */
844 if (n == 0)
845 {
846 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset, 1,
847 BFD_RELOC_16_PCREL);
848 fragp->fr_fix += 4;
849 return;
850 }
851
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))
855 {
856 unsigned int br_opcode;
96ba4233 857 unsigned int old_op, new_op;
36591ba1
SL
858 int nbytes;
859
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;
864 else
865 nbytes = 12;
866
867 br_opcode = md_chars_to_number (buffer, 4);
96ba4233
SL
868 old_op = GET_IW_R1_OP (br_opcode);
869 switch (old_op)
36591ba1 870 {
96ba4233
SL
871 case R1_OP_BEQ:
872 new_op = R1_OP_BNE;
36591ba1 873 break;
96ba4233
SL
874 case R1_OP_BNE:
875 new_op = R1_OP_BEQ;
36591ba1 876 break;
96ba4233
SL
877 case R1_OP_BGE:
878 new_op = R1_OP_BLT;
36591ba1 879 break;
96ba4233
SL
880 case R1_OP_BGEU:
881 new_op = R1_OP_BLTU;
36591ba1 882 break;
96ba4233
SL
883 case R1_OP_BLT:
884 new_op = R1_OP_BGE;
36591ba1 885 break;
96ba4233
SL
886 case R1_OP_BLTU:
887 new_op = R1_OP_BGEU;
36591ba1
SL
888 break;
889 default:
890 as_bad_where (fragp->fr_file, fragp->fr_line,
891 _("expecting conditional branch for relaxation\n"));
892 abort ();
893 }
894
96ba4233
SL
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);
36591ba1
SL
897 md_number_to_chars (buffer, br_opcode, 4);
898 fragp->fr_fix += 4;
899 buffer += 4;
900 }
901
902 /* Load at for the PC-relative case. */
903 if (nios2_as_options.relax == relax_section)
904 {
905 /* Insert the nextpc instruction. */
906 md_number_to_chars (buffer,
96ba4233 907 MATCH_R1_NEXTPC | SET_IW_R_C (AT_REGNUM), 4);
36591ba1
SL
908 fragp->fr_fix += 4;
909 buffer += 4;
910
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;
914 if (offset > 0)
915 addend = 32767;
916 else
917 addend = -32768;
96ba4233 918 addend_mask = SET_IW_I_IMM16 ((unsigned int)addend);
36591ba1
SL
919
920 /* Insert n-1 addi instructions. */
96ba4233
SL
921 addi_mask = (MATCH_R1_ADDI
922 | SET_IW_I_B (AT_REGNUM)
923 | SET_IW_I_A (AT_REGNUM));
36591ba1
SL
924 for (i = 0; i < n - 1; i ++)
925 {
926 md_number_to_chars (buffer, addi_mask | addend_mask, 4);
927 fragp->fr_fix += 4;
928 buffer += 4;
929 }
930
931 /* Insert the last addi instruction to hold the remainder. */
932 remainder = offset - addend * (n - 1);
933 gas_assert (remainder >= -32768 && remainder <= 32767);
96ba4233 934 addend_mask = SET_IW_I_IMM16 ((unsigned int)remainder);
36591ba1
SL
935 md_number_to_chars (buffer, addi_mask | addend_mask, 4);
936 fragp->fr_fix += 4;
937 buffer += 4;
938 }
939
940 /* Load at for the absolute case. */
941 else
942 {
96ba4233
SL
943 md_number_to_chars (buffer,
944 (MATCH_R1_ORHI | SET_IW_I_B (AT_REGNUM)
945 | SET_IW_I_A (0)),
946 4);
36591ba1
SL
947 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset,
948 0, BFD_RELOC_NIOS2_HI16);
949 fragp->fr_fix += 4;
950 buffer += 4;
96ba4233
SL
951 md_number_to_chars (buffer,
952 (MATCH_R1_ORI | SET_IW_I_B (AT_REGNUM)
953 | SET_IW_I_A (AT_REGNUM)),
954 4);
36591ba1
SL
955 fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset,
956 0, BFD_RELOC_NIOS2_LO16);
957 fragp->fr_fix += 4;
958 buffer += 4;
959 }
960
961 /* Insert the jmp instruction. */
96ba4233 962 md_number_to_chars (buffer, MATCH_R1_JMP | SET_IW_R_A (AT_REGNUM), 4);
36591ba1
SL
963 fragp->fr_fix += 4;
964 buffer += 4;
965}
966
967\f
968/** Fixups and overflow checking. */
969
970/* Check a fixup for overflow. */
971static bfd_boolean
972nios2_check_overflow (valueT fixup, reloc_howto_type *howto)
973{
974 /* Apply the rightshift before checking for overflow. */
975 fixup = ((signed)fixup) >> howto->rightshift;
976
977 /* Check for overflow - return TRUE if overflow, FALSE if not. */
978 switch (howto->complain_on_overflow)
979 {
980 case complain_overflow_dont:
981 break;
982 case complain_overflow_bitfield:
983 if ((fixup >> howto->bitsize) != 0
984 && ((signed) fixup >> howto->bitsize) != -1)
985 return TRUE;
986 break;
987 case complain_overflow_signed:
988 if ((fixup & 0x80000000) > 0)
989 {
990 /* Check for negative overflow. */
991 if ((signed) fixup < ((signed) 0x80000000 >> howto->bitsize))
992 return TRUE;
993 }
994 else
995 {
996 /* Check for positive overflow. */
997 if (fixup >= ((unsigned) 1 << (howto->bitsize - 1)))
998 return TRUE;
999 }
1000 break;
1001 case complain_overflow_unsigned:
1002 if ((fixup >> howto->bitsize) != 0)
1003 return TRUE;
1004 break;
1005 default:
1006 as_bad (_("error checking for overflow - broken assembler"));
1007 break;
1008 }
1009 return FALSE;
1010}
1011
1012/* Emit diagnostic for fixup overflow. */
1013static void
1014nios2_diagnose_overflow (valueT fixup, reloc_howto_type *howto,
1015 fixS *fixP, valueT value)
1016{
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));
1025 else
1026 {
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;
96ba4233 1035
b4714c7c 1036 opcode = nios2_find_opcode_hash (value, bfd_get_mach (stdoutput));
36591ba1 1037 gas_assert (opcode);
96ba4233 1038 gas_assert (fixP->fx_size == opcode->size);
36591ba1
SL
1039 overflow_msg_type = opcode->overflow_msg;
1040 switch (overflow_msg_type)
1041 {
1042 case call_target_overflow:
1043 range_min
1044 = ((fixP->fx_frag->fr_address + fixP->fx_where) & 0xf0000000);
1045 range_max = range_min + 0x0fffffff;
1046 address = fixup | range_min;
1047
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);
1051 break;
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);
1056 break;
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);
1061 break;
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);
1066 break;
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);
1071 break;
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);
1076 break;
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);
1081 break;
1082 default:
1083 as_bad_where (fixP->fx_file, fixP->fx_line,
1084 _("overflow in immediate argument"));
1085 break;
1086 }
1087 }
1088}
1089
1090/* Apply a fixup to the object file. */
1091void
1092md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
1093{
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
1c2de463
SL
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
36591ba1
SL
1134 /* Add other relocs here as we generate them. */
1135 ));
1136
1137 if (fixP->fx_r_type == BFD_RELOC_64)
1138 {
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"));
1144 else
1145 {
1146 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1147 *valP, 8);
1148 fixP->fx_done = 1;
1149 }
1150 return;
1151 }
1152
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. */
1165 {
1166 fixP->fx_addnumber = fixP->fx_offset;
1167 fixP->fx_done = 0;
1168
1169 switch (fixP->fx_r_type)
1170 {
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);
1180 break;
1181 default:
1182 break;
1183 }
1184 }
1185 else
1186 /* Fully resolved fixup. */
1187 {
1188 reloc_howto_type *howto
1189 = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
1190
1191 if (howto == NULL)
1192 as_bad_where (fixP->fx_file, fixP->fx_line,
1193 _("relocation is not supported"));
1194 else
1195 {
1196 valueT fixup = *valP;
1197 valueT value;
1198 char *buf;
1199
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)
96ba4233
SL
1205 fixup = fixup - (fixP->fx_frag->fr_address + fixP->fx_where
1206 + fixP->fx_size);
36591ba1
SL
1207
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);
1211
1212 /* Check for overflow, emitting a diagnostic if necessary. */
1213 if (nios2_check_overflow (fixup, howto))
1214 nios2_diagnose_overflow (fixup, howto, fixP, value);
1215
1216 /* Apply the right shift. */
1217 fixup = ((signed)fixup) >> howto->rightshift;
1218
1219 /* Truncate the fixup to right size. */
1220 switch (fixP->fx_r_type)
1221 {
1222 case BFD_RELOC_NIOS2_HI16:
1223 fixup = (fixup >> 16) & 0xFFFF;
1224 break;
1225 case BFD_RELOC_NIOS2_LO16:
1226 fixup = fixup & 0xFFFF;
1227 break;
1228 case BFD_RELOC_NIOS2_HIADJ16:
5d5755a7
SL
1229 fixup = ((((fixup >> 16) & 0xFFFF) + ((fixup >> 15) & 0x01))
1230 & 0xFFFF);
36591ba1
SL
1231 break;
1232 default:
1233 {
1234 int n = sizeof (fixup) * 8 - howto->bitsize;
1235 fixup = (fixup << n) >> n;
1236 break;
1237 }
1238 }
1239
1240 /* Fix up the instruction. */
1241 value = (value & ~howto->dst_mask) | (fixup << howto->bitpos);
1242 md_number_to_chars (buf, value, fixP->fx_size);
1243 }
1244
1245 fixP->fx_done = 1;
1246 }
1247
1248 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
1249 {
1250 fixP->fx_done = 0;
1251 if (fixP->fx_addsy
1252 && !S_IS_DEFINED (fixP->fx_addsy) && !S_IS_WEAK (fixP->fx_addsy))
1253 S_SET_WEAK (fixP->fx_addsy);
1254 }
1255 else if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1256 fixP->fx_done = 0;
1257}
1258
1259
1260\f
1261/** Instruction parsing support. */
1262
96ba4233
SL
1263/* General internal error routine. */
1264
1265static void
1266bad_opcode (const struct nios2_opcode *op)
1267{
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."));
1271}
1272
36591ba1
SL
1273/* Special relocation directive strings. */
1274
1275struct nios2_special_relocS
1276{
1277 const char *string;
1278 bfd_reloc_code_real_type reloc_type;
1279};
1280
1c2de463
SL
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. */
1283
36591ba1
SL
1284struct 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},
1c2de463
SL
1289 {"%call_lo", BFD_RELOC_NIOS2_CALL_LO},
1290 {"%call_hiadj", BFD_RELOC_NIOS2_CALL_HA},
36591ba1
SL
1291 {"%call", BFD_RELOC_NIOS2_CALL16},
1292 {"%gotoff_lo", BFD_RELOC_NIOS2_GOTOFF_LO},
1293 {"%gotoff_hiadj", BFD_RELOC_NIOS2_GOTOFF_HA},
1c2de463
SL
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},
36591ba1
SL
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},
36591ba1
SL
1303};
1304
1305#define NIOS2_NUM_SPECIAL_RELOCS \
1306 (sizeof(nios2_special_reloc)/sizeof(nios2_special_reloc[0]))
1307const int nios2_num_special_relocs = NIOS2_NUM_SPECIAL_RELOCS;
1308
1309/* Creates a new nios2_insn_relocS and returns a pointer to it. */
1310static nios2_insn_relocS *
1311nios2_insn_reloc_new (bfd_reloc_code_real_type reloc_type, unsigned int pcrel)
1312{
1313 nios2_insn_relocS *retval;
1314 retval = (nios2_insn_relocS *) malloc (sizeof (nios2_insn_relocS));
1315 if (retval == NULL)
1316 {
1317 as_bad (_("can't create relocation"));
1318 abort ();
1319 }
1320
1321 /* Fill out the fields with default values. */
1322 retval->reloc_next = NULL;
1323 retval->reloc_type = reloc_type;
1324 retval->reloc_pcrel = pcrel;
1325 return retval;
1326}
1327
1328/* Frees up memory previously allocated by nios2_insn_reloc_new(). */
1329/* FIXME: this is never called; memory leak? */
1330#if 0
1331static void
1332nios2_insn_reloc_destroy (nios2_insn_relocS *reloc)
1333{
1334 gas_assert (reloc != NULL);
1335 free (reloc);
1336}
1337#endif
1338
96ba4233
SL
1339/* Look up a register name and validate it for the given regtype.
1340 Return the register mapping or NULL on failure. */
1341static struct nios2_reg *
1342nios2_parse_reg (const char *token, unsigned long regtype)
1343{
1344 struct nios2_reg *reg = nios2_reg_lookup (token);
1345
1346 if (reg == NULL)
1347 {
1348 as_bad (_("unknown register %s"), token);
1349 return NULL;
1350 }
1351
1352 /* Matched a register, but is it the wrong type? */
1353 if (!(regtype & reg->regtype))
1354 {
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"));
1361 else
1362 as_bad (_("invalid register %s"), token);
1363 return NULL;
1364 }
1365
1366 /* Warn for explicit use of special registers. */
1367 if (reg->regtype & REG_NORMAL)
1368 {
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."));
1382 }
1383
1384 return reg;
1385}
1386
36591ba1
SL
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. */
1392static unsigned long
1393nios2_assemble_expression (const char *exprstr,
1394 nios2_insn_infoS *insn,
36591ba1
SL
1395 bfd_reloc_code_real_type reloc_type,
1396 unsigned int pcrel)
1397{
1398 nios2_insn_relocS *reloc;
1399 char *saved_line_ptr;
1400 unsigned short value;
1401 int i;
1402
1403 gas_assert (exprstr != NULL);
1404 gas_assert (insn != NULL);
1405
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)
1411 {
1412 reloc_type = nios2_special_reloc[i].reloc_type;
1413 exprstr += strlen (nios2_special_reloc[i].string) + 1;
1414
1415 /* %lo and %hiadj have different meanings for PC-relative
1416 expressions. */
1417 if (pcrel)
1418 {
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;
1423 }
1424
1425 break;
1426 }
1427
1428 /* We potentially have a relocation. */
1429 reloc = nios2_insn_reloc_new (reloc_type, pcrel);
96ba4233
SL
1430 reloc->reloc_next = insn->insn_reloc;
1431 insn->insn_reloc = reloc;
36591ba1
SL
1432
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;
1438
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. */
1442 value = 0;
1443 if (nios2_mode == NIOS2_MODE_TEST
1444 && reloc->reloc_expression.X_op == O_constant)
1445 value = reloc->reloc_expression.X_add_number;
1446
1447 return (unsigned long) value;
1448}
1449
36591ba1 1450
96ba4233
SL
1451/* Argument assemble functions. */
1452static void
1453nios2_assemble_arg_c (const char *token, nios2_insn_infoS *insn)
36591ba1 1454{
96ba4233
SL
1455 struct nios2_reg *reg = nios2_parse_reg (token, REG_CONTROL);
1456 const struct nios2_opcode *op = insn->insn_nios2_opcode;
36591ba1 1457
96ba4233
SL
1458 if (reg == NULL)
1459 return;
36591ba1 1460
96ba4233
SL
1461 switch (op->format)
1462 {
1463 case iw_r_type:
1464 insn->insn_code |= SET_IW_R_IMM5 (reg->index);
1465 break;
1466 default:
1467 bad_opcode (op);
36591ba1
SL
1468 }
1469}
1470
96ba4233
SL
1471static void
1472nios2_assemble_arg_d (const char *token, nios2_insn_infoS *insn)
36591ba1 1473{
96ba4233
SL
1474 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1475 unsigned long regtype = REG_NORMAL;
1476 struct nios2_reg *reg;
36591ba1 1477
96ba4233
SL
1478 if (op->format == iw_custom_type)
1479 regtype |= REG_COPROCESSOR;
1480 reg = nios2_parse_reg (token, regtype);
1481 if (reg == NULL)
1482 return;
36591ba1 1483
96ba4233 1484 switch (op->format)
36591ba1 1485 {
96ba4233
SL
1486 case iw_r_type:
1487 insn->insn_code |= SET_IW_R_C (reg->index);
1488 break;
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);
36591ba1 1493 else
96ba4233
SL
1494 insn->insn_code |= SET_IW_CUSTOM_READC (1);
1495 break;
1496 default:
1497 bad_opcode (op);
36591ba1
SL
1498 }
1499}
1500
96ba4233
SL
1501static void
1502nios2_assemble_arg_s (const char *token, nios2_insn_infoS *insn)
36591ba1 1503{
96ba4233
SL
1504 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1505 unsigned long regtype = REG_NORMAL;
1506 struct nios2_reg *reg;
36591ba1 1507
96ba4233
SL
1508 if (op->format == iw_custom_type)
1509 regtype |= REG_COPROCESSOR;
1510 reg = nios2_parse_reg (token, regtype);
1511 if (reg == NULL)
1512 return;
36591ba1 1513
96ba4233 1514 switch (op->format)
36591ba1 1515 {
96ba4233
SL
1516 case iw_r_type:
1517 insn->insn_code |= SET_IW_R_A (reg->index);
1518 break;
1519 case iw_i_type:
1520 insn->insn_code |= SET_IW_I_A (reg->index);
1521 break;
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);
1526 else
1527 insn->insn_code |= SET_IW_CUSTOM_READA (1);
1528 break;
1529 default:
1530 bad_opcode (op);
36591ba1
SL
1531 }
1532}
1533
96ba4233
SL
1534static void
1535nios2_assemble_arg_t (const char *token, nios2_insn_infoS *insn)
36591ba1 1536{
96ba4233
SL
1537 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1538 unsigned long regtype = REG_NORMAL;
1539 struct nios2_reg *reg;
36591ba1 1540
96ba4233
SL
1541 if (op->format == iw_custom_type)
1542 regtype |= REG_COPROCESSOR;
1543 reg = nios2_parse_reg (token, regtype);
1544 if (reg == NULL)
1545 return;
36591ba1 1546
96ba4233
SL
1547 switch (op->format)
1548 {
1549 case iw_r_type:
1550 insn->insn_code |= SET_IW_R_B (reg->index);
1551 break;
1552 case iw_i_type:
1553 insn->insn_code |= SET_IW_I_B (reg->index);
1554 break;
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);
36591ba1 1559 else
96ba4233
SL
1560 insn->insn_code |= SET_IW_CUSTOM_READB (1);
1561 break;
1562 default:
1563 bad_opcode (op);
36591ba1
SL
1564 }
1565}
1566
96ba4233
SL
1567static void
1568nios2_assemble_arg_i (const char *token, nios2_insn_infoS *insn)
36591ba1 1569{
96ba4233
SL
1570 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1571 unsigned int val;
36591ba1 1572
96ba4233 1573 switch (op->format)
36591ba1 1574 {
96ba4233
SL
1575 case iw_i_type:
1576 val = nios2_assemble_expression (token, insn,
1577 BFD_RELOC_NIOS2_S16, 0);
1578 insn->constant_bits |= SET_IW_I_IMM16 (val);
1579 break;
1580 default:
1581 bad_opcode (op);
36591ba1
SL
1582 }
1583}
1584
96ba4233
SL
1585static void
1586nios2_assemble_arg_u (const char *token, nios2_insn_infoS *insn)
36591ba1 1587{
96ba4233
SL
1588 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1589 unsigned int val;
36591ba1 1590
96ba4233 1591 switch (op->format)
36591ba1 1592 {
96ba4233
SL
1593 case iw_i_type:
1594 val = nios2_assemble_expression (token, insn,
1595 BFD_RELOC_NIOS2_U16, 0);
1596 insn->constant_bits |= SET_IW_I_IMM16 (val);
1597 break;
1598 default:
1599 bad_opcode (op);
36591ba1
SL
1600 }
1601}
1602
96ba4233
SL
1603static void
1604nios2_assemble_arg_o (const char *token, nios2_insn_infoS *insn)
36591ba1 1605{
96ba4233
SL
1606 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1607 unsigned int val;
36591ba1 1608
96ba4233
SL
1609 switch (op->format)
1610 {
1611 case iw_i_type:
1612 val = nios2_assemble_expression (token, insn,
1613 BFD_RELOC_16_PCREL, 1);
1614 insn->constant_bits |= SET_IW_I_IMM16 (val);
1615 break;
1616 default:
1617 bad_opcode (op);
36591ba1
SL
1618 }
1619}
1620
96ba4233
SL
1621static void
1622nios2_assemble_arg_j (const char *token, nios2_insn_infoS *insn)
dad60f8e 1623{
96ba4233
SL
1624 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1625 unsigned int val;
dad60f8e 1626
96ba4233
SL
1627 switch (op->format)
1628 {
1629 case iw_r_type:
1630 val = nios2_assemble_expression (token, insn,
1631 BFD_RELOC_NIOS2_IMM5, 0);
1632 insn->constant_bits |= SET_IW_R_IMM5 (val);
1633 break;
1634 default:
1635 bad_opcode (op);
dad60f8e
SL
1636 }
1637}
1638
96ba4233
SL
1639static void
1640nios2_assemble_arg_l (const char *token, nios2_insn_infoS *insn)
36591ba1 1641{
96ba4233
SL
1642 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1643 unsigned int val;
36591ba1 1644
96ba4233
SL
1645 switch (op->format)
1646 {
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);
1651 break;
1652 default:
1653 bad_opcode (op);
36591ba1
SL
1654 }
1655}
1656
96ba4233
SL
1657static void
1658nios2_assemble_arg_m (const char *token, nios2_insn_infoS *insn)
36591ba1 1659{
96ba4233
SL
1660 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1661 unsigned int val;
36591ba1 1662
96ba4233 1663 switch (op->format)
36591ba1 1664 {
96ba4233
SL
1665 case iw_j_type:
1666 val = nios2_assemble_expression (token, insn,
1667 (nios2_as_options.noat
1668 ? BFD_RELOC_NIOS2_CALL26_NOAT
1669 : BFD_RELOC_NIOS2_CALL26),
1670 0);
1671 insn->constant_bits |= SET_IW_J_IMM26 (val);
1672 break;
1673 default:
1674 bad_opcode (op);
36591ba1
SL
1675 }
1676}
1677
1678static void
96ba4233 1679nios2_assemble_args (nios2_insn_infoS *insn)
36591ba1 1680{
96ba4233
SL
1681 const struct nios2_opcode *op = insn->insn_nios2_opcode;
1682 const char *argptr;
1683 unsigned int tokidx, ntok;
36591ba1 1684
96ba4233
SL
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)
1689 {
1690 as_bad ("missing argument");
1691 return;
1692 }
36591ba1 1693
96ba4233
SL
1694 for (argptr = op->args, tokidx = 1;
1695 *argptr && insn->insn_tokens[tokidx];
1696 argptr++)
1697 switch (*argptr)
1698 {
1699 case ',':
1700 case '(':
1701 case ')':
1702 break;
36591ba1 1703
96ba4233
SL
1704 case 'c':
1705 nios2_assemble_arg_c (insn->insn_tokens[tokidx++], insn);
1706 break;
1707
1708 case 'd':
1709 nios2_assemble_arg_d (insn->insn_tokens[tokidx++], insn);
1710 break;
1711
1712 case 's':
1713 nios2_assemble_arg_s (insn->insn_tokens[tokidx++], insn);
1714 break;
1715
1716 case 't':
1717 nios2_assemble_arg_t (insn->insn_tokens[tokidx++], insn);
1718 break;
1719
1720 case 'i':
1721 nios2_assemble_arg_i (insn->insn_tokens[tokidx++], insn);
1722 break;
1723
1724 case 'u':
1725 nios2_assemble_arg_u (insn->insn_tokens[tokidx++], insn);
1726 break;
1727
1728 case 'o':
1729 nios2_assemble_arg_o (insn->insn_tokens[tokidx++], insn);
1730 break;
1731
1732 case 'j':
1733 nios2_assemble_arg_j (insn->insn_tokens[tokidx++], insn);
1734 break;
1735
1736 case 'l':
1737 nios2_assemble_arg_l (insn->insn_tokens[tokidx++], insn);
1738 break;
1739
1740 case 'm':
1741 nios2_assemble_arg_m (insn->insn_tokens[tokidx++], insn);
1742 break;
1743
1744 default:
1745 bad_opcode (op);
1746 break;
1747 }
36591ba1 1748
96ba4233
SL
1749 /* Perform argument checking. */
1750 nios2_check_assembly (insn->insn_code | insn->constant_bits,
1751 insn->insn_tokens[tokidx]);
1752}
36591ba1 1753
36591ba1
SL
1754
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. */
1761static char *
96ba4233 1762nios2_consume_arg (char *argstr, const char *parsestr)
36591ba1
SL
1763{
1764 char *temp;
36591ba1
SL
1765
1766 switch (*parsestr)
1767 {
1768 case 'c':
36591ba1
SL
1769 case 'd':
1770 case 's':
1771 case 't':
36591ba1 1772 break;
96ba4233 1773
36591ba1
SL
1774 case 'i':
1775 case 'u':
1776 if (*argstr == '%')
1777 {
1778 if (nios2_special_relocation_p (argstr))
1779 {
1780 /* We zap the parentheses because we don't want them confused
1781 with separators. */
1782 temp = strchr (argstr, '(');
1783 if (temp != NULL)
1784 *temp = ' ';
1785 temp = strchr (argstr, ')');
1786 if (temp != NULL)
1787 *temp = ' ';
1788 }
1789 else
1790 as_bad (_("badly formed expression near %s"), argstr);
1791 }
1792 break;
1793 case 'm':
1794 case 'j':
36591ba1 1795 case 'l':
36591ba1
SL
1796 /* We can't have %hi, %lo or %hiadj here. */
1797 if (*argstr == '%')
1798 as_bad (_("badly formed expression near %s"), argstr);
1799 break;
531a94fd 1800 case 'o':
fad16e30 1801 case 'E':
531a94fd 1802 break;
36591ba1 1803 default:
531a94fd 1804 BAD_CASE (*parsestr);
36591ba1
SL
1805 break;
1806 }
1807
1808 return argstr;
1809}
1810
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. */
1817static char *
1818nios2_consume_separator (char *argstr, const char *separator)
1819{
1820 char *p;
1821
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. */
1825
1826 if (*separator == '(')
1827 p = strrchr (argstr, *separator);
1828 else
1829 p = strchr (argstr, *separator);
1830
1831 if (p != NULL)
1832 *p++ = 0;
36591ba1
SL
1833 return p;
1834}
1835
1836
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. */
1840static void
1841nios2_parse_args (nios2_insn_infoS *insn, char *argstr,
1842 const char *parsestr, char **parsed_args)
1843{
1844 char *p;
1845 char *end = NULL;
1846 int i;
1847 p = argstr;
1848 i = 0;
1849 bfd_boolean terminate = FALSE;
1850
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)
1854 {
1855 as_bad (_("too many arguments"));
1856 parsed_args[0] = NULL;
1857 return;
1858 }
1859
1860 while (p != NULL && !terminate && i < NIOS2_MAX_INSN_TOKENS)
1861 {
96ba4233 1862 parsed_args[i] = nios2_consume_arg (p, parsestr);
36591ba1 1863 ++parsestr;
96ba4233 1864 while (*parsestr == '(' || *parsestr == ')' || *parsestr == ',')
36591ba1 1865 {
96ba4233 1866 char *context = p;
36591ba1 1867 p = nios2_consume_separator (p, parsestr);
96ba4233
SL
1868 /* Check for missing separators. */
1869 if (!p && !(insn->insn_nios2_opcode->pinfo & NIOS2_INSN_OPTARG))
1870 {
1871 as_bad (_("expecting %c near %s"), *parsestr, context);
1872 break;
1873 }
36591ba1
SL
1874 ++parsestr;
1875 }
96ba4233
SL
1876
1877 if (*parsestr == '\0')
36591ba1
SL
1878 {
1879 /* Check that the argument string has no trailing arguments. */
96ba4233 1880 end = strpbrk (p, ",");
36591ba1
SL
1881 if (end != NULL)
1882 as_bad (_("too many arguments"));
1883 }
1884
1885 if (*parsestr == '\0' || (p != NULL && *p == '\0'))
1886 terminate = TRUE;
1887 ++i;
1888 }
1889
1890 parsed_args[i] = NULL;
36591ba1
SL
1891}
1892
1893
1894\f
1895/** Support for pseudo-op parsing. These are macro-like opcodes that
1896 expand into real insns by suitable fiddling with the operands. */
1897
1898/* Append the string modifier to the string contained in the argument at
1899 parsed_args[ndx]. */
1900static void
1901nios2_modify_arg (char **parsed_args, const char *modifier,
1902 int unused ATTRIBUTE_UNUSED, int ndx)
1903{
1904 char *tmp = parsed_args[ndx];
1905
1906 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);
1910}
1911
1912/* Modify parsed_args[ndx] by negating that argument. */
1913static void
1914nios2_negate_arg (char **parsed_args, const char *modifier ATTRIBUTE_UNUSED,
1915 int unused ATTRIBUTE_UNUSED, int ndx)
1916{
1917 char *tmp = parsed_args[ndx];
1918
1919 parsed_args[ndx]
1920 = (char *) malloc (strlen ("~(") + strlen (parsed_args[ndx]) +
1921 strlen (")+1") + 1);
1922
1923 strcpy (parsed_args[ndx], "~(");
1924 strcat (parsed_args[ndx], tmp);
1925 strcat (parsed_args[ndx], ")+1");
1926}
1927
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. */
1931static void
1932nios2_swap_args (char **parsed_args, const char *unused ATTRIBUTE_UNUSED,
1933 int index_1, int index_2)
1934{
1935 char *tmp;
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;
1941}
1942
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. */
1945static void
1946nios2_append_arg (char **parsed_args, const char *appnd, int num,
1947 int start)
1948{
1949 int i, count;
1950 char *tmp;
1951
1952 gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS);
1953
1954 if (nios2_mode == NIOS2_MODE_TEST)
1955 tmp = parsed_args[start];
1956 else
1957 tmp = NULL;
1958
1959 for (i = start, count = num; count > 0; ++i, --count)
1960 parsed_args[i] = (char *) appnd;
1961
1962 gas_assert (i == (start + num));
1963 parsed_args[i] = tmp;
1964 parsed_args[i + 1] = NULL;
1965}
1966
1967/* This function inserts the string insert num times in the array
1968 parsed_args, starting at the index start. */
1969static void
1970nios2_insert_arg (char **parsed_args, const char *insert, int num,
1971 int start)
1972{
1973 int i, count;
1974
1975 gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS);
1976
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];
1980
1981 for (i = start, count = num; count > 0; ++i, --count)
1982 parsed_args[i] = (char *) insert;
1983}
1984
1985/* Cleanup function to free malloc'ed arg strings. */
1986static void
1987nios2_free_arg (char **parsed_args, int num ATTRIBUTE_UNUSED, int start)
1988{
1989 if (parsed_args[start])
1990 {
1991 free (parsed_args[start]);
1992 parsed_args[start] = NULL;
1993 }
1994}
1995
1996/* This function swaps the pseudo-op for a real op. */
1997static nios2_ps_insn_infoS*
1998nios2_translate_pseudo_insn (nios2_insn_infoS *insn)
1999{
2000
2001 nios2_ps_insn_infoS *ps_insn;
2002
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);
2006
2007 if (ps_insn != NULL)
2008 {
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,
2014 ps_insn->index);
2015 }
2016 else
2017 /* we cannot recover from this. */
2018 as_fatal (_("unrecognized pseudo-instruction %s"),
2019 ps_insn->pseudo_insn);
2020 return ps_insn;
2021}
2022
2023/* Invoke the cleanup handler for pseudo-insn ps_insn on insn. */
2024static void
2025nios2_cleanup_pseudo_insn (nios2_insn_infoS *insn,
2026 nios2_ps_insn_infoS *ps_insn)
2027{
2028 if (ps_insn->arg_cleanup_func)
2029 (ps_insn->arg_cleanup_func) ((char **) insn->insn_tokens,
2030 ps_insn->num, ps_insn->index);
2031}
2032
2033const 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. */
2055};
2056
2057#define NIOS2_NUM_PSEUDO_INSNS \
2058 ((sizeof(nios2_ps_insn_info_structs)/ \
2059 sizeof(nios2_ps_insn_info_structs[0])))
2060const int nios2_num_ps_insn_info_structs = NIOS2_NUM_PSEUDO_INSNS;
2061
2062\f
2063/** Assembler output support. */
2064
36591ba1
SL
2065/* Output a normal instruction. */
2066static void
2067output_insn (nios2_insn_infoS *insn)
2068{
2069 char *f;
96ba4233
SL
2070 nios2_insn_relocS *reloc;
2071 f = frag_more (insn->insn_nios2_opcode->size);
36591ba1
SL
2072 /* This allocates enough space for the instruction
2073 and puts it in the current frag. */
96ba4233 2074 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
36591ba1 2075 /* Emit debug info. */
96ba4233 2076 dwarf2_emit_insn (insn->insn_nios2_opcode->size);
36591ba1 2077 /* Create any fixups to be acted on later. */
96ba4233 2078
36591ba1 2079 for (reloc = insn->insn_reloc; reloc != NULL; reloc = reloc->reloc_next)
96ba4233
SL
2080 fix_new_exp (frag_now, f - frag_now->fr_literal,
2081 insn->insn_nios2_opcode->size,
36591ba1
SL
2082 &reloc->reloc_expression, reloc->reloc_pcrel,
2083 reloc->reloc_type);
2084}
2085
2086/* Output an unconditional branch. */
2087static void
2088output_ubranch (nios2_insn_infoS *insn)
2089{
2090 nios2_insn_relocS *reloc = insn->insn_reloc;
2091
2092 /* If the reloc is NULL, there was an error assembling the branch. */
2093 if (reloc != NULL)
2094 {
2095 symbolS *symp = reloc->reloc_expression.X_add_symbol;
2096 offsetT offset = reloc->reloc_expression.X_add_number;
2097 char *f;
2098
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);
2102
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,
96ba4233
SL
2107 UBRANCH_MAX_SIZE, insn->insn_nios2_opcode->size,
2108 UBRANCH_SUBTYPE (0), symp, offset, NULL);
36591ba1 2109
96ba4233 2110 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
36591ba1
SL
2111
2112 /* We leave fixup generation to md_convert_frag. */
2113 }
2114}
2115
2116/* Output a conditional branch. */
2117static void
2118output_cbranch (nios2_insn_infoS *insn)
2119{
2120 nios2_insn_relocS *reloc = insn->insn_reloc;
2121
2122 /* If the reloc is NULL, there was an error assembling the branch. */
2123 if (reloc != NULL)
2124 {
2125 symbolS *symp = reloc->reloc_expression.X_add_symbol;
2126 offsetT offset = reloc->reloc_expression.X_add_number;
2127 char *f;
2128
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);
2132
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,
96ba4233
SL
2137 CBRANCH_MAX_SIZE, insn->insn_nios2_opcode->size,
2138 CBRANCH_SUBTYPE (0), symp, offset, NULL);
36591ba1 2139
96ba4233 2140 md_number_to_chars (f, insn->insn_code, insn->insn_nios2_opcode->size);
36591ba1
SL
2141
2142 /* We leave fixup generation to md_convert_frag. */
2143 }
2144}
2145
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
2151 short calls. */
2152static void
2153output_call (nios2_insn_infoS *insn)
2154{
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;
2159
96ba4233
SL
2160 md_number_to_chars (f,
2161 (MATCH_R1_ORHI | SET_IW_I_B (AT_REGNUM)
2162 | SET_IW_I_A (0)),
2163 4);
36591ba1
SL
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);
96ba4233
SL
2167 md_number_to_chars (f + 4,
2168 (MATCH_R1_ORI | SET_IW_I_B (AT_REGNUM)
2169 | SET_IW_I_A (AT_REGNUM)),
2170 4);
36591ba1
SL
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);
96ba4233 2174 md_number_to_chars (f + 8, MATCH_R1_CALLR | SET_IW_R_A (AT_REGNUM), 4);
36591ba1
SL
2175 dwarf2_emit_insn (4);
2176}
2177
36591ba1
SL
2178/* Output a movhi/addi pair for the movia pseudo-op. */
2179static void
2180output_movia (nios2_insn_infoS *insn)
2181{
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;
96ba4233 2186 unsigned long reg_index = GET_IW_I_B (insn->insn_code);
36591ba1
SL
2187
2188 /* If the reloc is NULL, there was an error assembling the movia. */
2189 if (reloc != NULL)
2190 {
2191 md_number_to_chars (f, insn->insn_code, 4);
2192 dwarf2_emit_insn (4);
36591ba1
SL
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);
96ba4233
SL
2197 md_number_to_chars (f + 4,
2198 (MATCH_R1_ADDI | SET_IW_I_A (reg_index)
2199 | SET_IW_I_B (reg_index)),
2200 4);
2201 dwarf2_emit_insn (4);
36591ba1
SL
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);
2205 }
2206}
2207
2208
2209\f
2210/** External interfaces. */
2211
2212/* The following functions are called by machine-independent parts of
2213 the assembler. */
2214int
2215md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
2216{
2217 switch (c)
2218 {
2219 case 'r':
2220 /* Hidden option for self-test mode. */
2221 nios2_mode = NIOS2_MODE_TEST;
2222 break;
2223 case OPTION_RELAX_ALL:
2224 nios2_as_options.relax = relax_all;
2225 break;
2226 case OPTION_NORELAX:
2227 nios2_as_options.relax = relax_none;
2228 break;
2229 case OPTION_RELAX_SECTION:
2230 nios2_as_options.relax = relax_section;
2231 break;
2232 case OPTION_EB:
2233 target_big_endian = 1;
2234 break;
2235 case OPTION_EL:
2236 target_big_endian = 0;
2237 break;
2238 default:
2239 return 0;
2240 break;
2241 }
2242
2243 return 1;
2244}
2245
2246/* Implement TARGET_FORMAT. We can choose to be big-endian or
2247 little-endian at runtime based on a switch. */
2248const char *
2249nios2_target_format (void)
2250{
2251 return target_big_endian ? "elf32-bignios2" : "elf32-littlenios2";
2252}
2253
2254/* Machine-dependent usage message. */
2255void
2256md_show_usage (FILE *stream)
2257{
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");
2266}
2267
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. */
2271void
2272md_begin (void)
2273{
2274 int i;
2275 const char *inserted;
2276
2277 /* Create and fill a hashtable for the Nios II opcodes, registers and
2278 arguments. */
2279 nios2_opcode_hash = hash_new ();
2280 nios2_reg_hash = hash_new ();
36591ba1
SL
2281 nios2_ps_hash = hash_new ();
2282
96ba4233 2283 for (i = 0; i < nios2_num_opcodes; ++i)
36591ba1
SL
2284 {
2285 inserted
2286 = hash_insert (nios2_opcode_hash, nios2_opcodes[i].name,
2287 (PTR) & nios2_opcodes[i]);
2288 if (inserted != NULL)
2289 {
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."));
2294 }
2295 }
2296
2297 for (i = 0; i < nios2_num_regs; ++i)
2298 {
2299 inserted
2300 = hash_insert (nios2_reg_hash, nios2_regs[i].name,
2301 (PTR) & nios2_regs[i]);
2302 if (inserted != NULL)
2303 {
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."));
2308 }
2309
2310 }
2311
36591ba1
SL
2312 for (i = 0; i < nios2_num_ps_insn_info_structs; ++i)
2313 {
2314 inserted
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)
2318 {
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."));
2323 }
2324 }
2325
2326 /* Assembler option defaults. */
2327 nios2_as_options.noat = FALSE;
2328 nios2_as_options.nobreak = FALSE;
2329
2330 /* Debug information is incompatible with relaxation. */
2331 if (debug_type != DEBUG_UNSPECIFIED)
2332 nios2_as_options.relax = relax_none;
2333
2334 /* Initialize the alignment data. */
2335 nios2_current_align_seg = now_seg;
2336 nios2_last_label = NULL;
2337 nios2_current_align = 0;
2338}
2339
2340
2341/* Assembles a single line of Nios II assembly language. */
2342void
2343md_assemble (char *op_str)
2344{
2345 char *argstr;
2346 char *op_strdup = NULL;
36591ba1
SL
2347 unsigned long saved_pinfo = 0;
2348 nios2_insn_infoS thisinsn;
2349 nios2_insn_infoS *insn = &thisinsn;
2350
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;
2357
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, "");
2363
2364 /* Assemble the opcode. */
2365 insn->insn_nios2_opcode = nios2_opcode_lookup (insn->insn_tokens[0]);
2366 insn->insn_reloc = NULL;
2367
2368 if (insn->insn_nios2_opcode != NULL)
2369 {
2370 nios2_ps_insn_infoS *ps_insn = NULL;
2371 /* Set the opcode for the instruction. */
2372 insn->insn_code = insn->insn_nios2_opcode->match;
96ba4233 2373 insn->constant_bits = 0;
36591ba1
SL
2374
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]);
2379 else
2380 nios2_parse_args (insn, argstr, insn->insn_nios2_opcode->args_test,
2381 (char **) &insn->insn_tokens[1]);
2382
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);
2392
96ba4233
SL
2393 /* Assemble the parsed arguments into the instruction word. */
2394 nios2_assemble_args (insn);
2395
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
2408 && insn->insn_reloc
2409 && ((insn->insn_reloc->reloc_type
2410 == BFD_RELOC_NIOS2_CALL26)
2411 || (insn->insn_reloc->reloc_type
2412 == BFD_RELOC_NIOS2_CALL26_NOAT)))
2413 output_call (insn);
96ba4233
SL
2414 else if (saved_pinfo == NIOS2_INSN_MACRO_MOVIA)
2415 output_movia (insn);
36591ba1 2416 else
96ba4233
SL
2417 output_insn (insn);
2418 if (ps_insn)
2419 nios2_cleanup_pseudo_insn (insn, ps_insn);
36591ba1
SL
2420 }
2421 else
2422 /* Unrecognised instruction - error. */
2423 as_bad (_("unrecognised instruction %s"), insn->insn_tokens[0]);
2424
2425 /* Don't leak memory. */
2426 free (op_strdup);
2427}
2428
2429/* Round up section size. */
2430valueT
2431md_section_align (asection *seg ATTRIBUTE_UNUSED, valueT size)
2432{
2433 /* I think byte alignment is fine here. */
2434 return size;
2435}
2436
2437/* Implement TC_FORCE_RELOCATION. */
2438int
2439nios2_force_relocation (fixS *fixp)
2440{
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)
2444 return 1;
2445
2446 return generic_force_reloc (fixp);
2447}
2448
2449/* Implement tc_fix_adjustable. */
2450int
2451nios2_fix_adjustable (fixS *fixp)
2452{
2453 if (fixp->fx_addsy == NULL)
2454 return 1;
2455
2456#ifdef OBJ_ELF
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)))
2460 return 0;
2461#endif
2462 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2463 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2464 return 0;
2465
2466 /* Preserve relocations against symbols with function type. */
2467 if (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION)
2468 return 0;
2469
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
1c2de463
SL
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
2488 )
36591ba1
SL
2489 return 0;
2490
2491 return 1;
2492}
2493
2494/* Implement tc_frob_symbol. This is called in adjust_reloc_syms;
2495 it is used to remove *ABS* references from the symbol table. */
2496int
2497nios2_frob_symbol (symbolS *symp)
2498{
2499 if ((OUTPUT_FLAVOR == bfd_target_elf_flavour
2500 && symp == section_symbol (absolute_section))
2501 || !S_IS_DEFINED (symp))
2502 return 1;
2503 else
2504 return 0;
2505}
2506
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. */
2511arelent *
2512tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
2513{
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);
2517
2518 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2519 reloc->addend = fixp->fx_offset; /* fixp->fx_addnumber; */
2520
2521 if (fixp->fx_pcrel)
2522 {
2523 switch (fixp->fx_r_type)
2524 {
2525 case BFD_RELOC_16:
2526 fixp->fx_r_type = BFD_RELOC_16_PCREL;
2527 break;
2528 case BFD_RELOC_NIOS2_LO16:
2529 fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_LO;
2530 break;
2531 case BFD_RELOC_NIOS2_HIADJ16:
2532 fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_HA;
2533 break;
2534 default:
2535 break;
2536 }
2537 }
2538
2539 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2540 if (reloc->howto == NULL)
2541 {
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));
2545
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);
2549 }
2550 return reloc;
2551}
2552
2553long
2554md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED)
2555{
2556 return 0;
2557}
2558
2559/* Called just before the assembler exits. */
2560void
2561md_end ()
2562{
2563 /* FIXME - not yet implemented */
2564}
2565
2566/* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
2567 Otherwise we have no need to default values of symbols. */
2568symbolS *
2569md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
2570{
2571#ifdef OBJ_ELF
2572 if (name[0] == '_' && name[1] == 'G'
2573 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
2574 {
2575 if (!GOT_symbol)
2576 {
2577 if (symbol_find (name))
2578 as_bad ("GOT already in the symbol table");
2579
2580 GOT_symbol = symbol_new (name, undefined_section,
2581 (valueT) 0, &zero_address_frag);
2582 }
2583
2584 return GOT_symbol;
2585 }
2586#endif
2587
2588 return 0;
2589}
2590
2591/* Implement tc_frob_label. */
2592void
2593nios2_frob_label (symbolS *lab)
2594{
2595 /* Emit dwarf information. */
2596 dwarf2_emit_label (lab);
2597
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 ());
2601
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;
2605}
2606
2607/* Implement md_cons_align. */
2608void
2609nios2_cons_align (int size)
2610{
2611 int log_size = 0;
2612 const char *pfill = NULL;
2613
2614 while ((size >>= 1) != 0)
2615 ++log_size;
2616
2617 if (subseg_text_p (now_seg))
2618 pfill = (const char *) &nop;
2619 else
2620 pfill = NULL;
2621
2622 if (nios2_auto_align_on)
2623 nios2_align (log_size, pfill, NULL);
2624
2625 nios2_last_label = NULL;
2626}
2627
2628/* Map 's' to SHF_NIOS2_GPREL. */
2629/* This is from the Alpha code tc-alpha.c. */
2630int
2631nios2_elf_section_letter (int letter, char **ptr_msg)
2632{
2633 if (letter == 's')
2634 return SHF_NIOS2_GPREL;
2635
2636 *ptr_msg = _("Bad .section directive: want a,s,w,x,M,S,G,T in string");
2637 return -1;
2638}
2639
2640/* Map SHF_ALPHA_GPREL to SEC_SMALL_DATA. */
2641/* This is from the Alpha code tc-alpha.c. */
2642flagword
2643nios2_elf_section_flags (flagword flags, int attr, int type ATTRIBUTE_UNUSED)
2644{
2645 if (attr & SHF_NIOS2_GPREL)
2646 flags |= SEC_SMALL_DATA;
2647 return flags;
2648}
2649
2650/* Implement TC_PARSE_CONS_EXPRESSION to handle %tls_ldo(...) */
62ebcb5c 2651bfd_reloc_code_real_type
36591ba1
SL
2652nios2_cons (expressionS *exp, int size)
2653{
62ebcb5c 2654 bfd_reloc_code_real_type nios2_tls_ldo_reloc = BFD_RELOC_NONE;
36591ba1
SL
2655
2656 SKIP_WHITESPACE ();
2657 if (input_line_pointer[0] == '%')
2658 {
2659 if (strprefix (input_line_pointer + 1, "tls_ldo"))
2660 {
2661 if (size != 4)
2662 as_bad (_("Illegal operands: %%tls_ldo in %d-byte data field"),
2663 size);
2664 else
2665 {
2666 input_line_pointer += 8;
62ebcb5c 2667 nios2_tls_ldo_reloc = BFD_RELOC_NIOS2_TLS_DTPREL;
36591ba1
SL
2668 }
2669 }
62ebcb5c 2670 if (nios2_tls_ldo_reloc != BFD_RELOC_NONE)
36591ba1
SL
2671 {
2672 SKIP_WHITESPACE ();
2673 if (input_line_pointer[0] != '(')
2674 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2675 else
2676 {
2677 int c;
2678 char *end = ++input_line_pointer;
2679 int npar = 0;
2680
2681 for (c = *end; !is_end_of_line[c]; end++, c = *end)
2682 if (c == '(')
2683 npar++;
2684 else if (c == ')')
2685 {
2686 if (!npar)
2687 break;
2688 npar--;
2689 }
2690
2691 if (c != ')')
2692 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2693 else
2694 {
2695 *end = '\0';
2696 expression (exp);
2697 *end = c;
2698 if (input_line_pointer != end)
2699 as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
2700 else
2701 {
2702 input_line_pointer++;
2703 SKIP_WHITESPACE ();
2704 c = *input_line_pointer;
2705 if (! is_end_of_line[c] && c != ',')
2706 as_bad (_("Illegal operands: garbage after %%tls_ldo()"));
2707 }
2708 }
2709 }
2710 }
2711 }
62ebcb5c 2712 if (nios2_tls_ldo_reloc == BFD_RELOC_NONE)
36591ba1 2713 expression (exp);
62ebcb5c 2714 return nios2_tls_ldo_reloc;
36591ba1
SL
2715}
2716
2717/* Implement HANDLE_ALIGN. */
2718void
2719nios2_handle_align (fragS *fragp)
2720{
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);
2730}
2731
2732/* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
2733 register number. */
2734int
2735nios2_regname_to_dw2regnum (char *regname)
2736{
2737 struct nios2_reg *r = nios2_reg_lookup (regname);
2738 if (r == NULL)
2739 return -1;
2740 return r->index;
2741}
2742
2743/* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
2744 unwind information for this procedure. */
2745void
2746nios2_frame_initial_instructions (void)
2747{
2748 cfi_add_CFA_def_cfa (27, 0);
2749}
This page took 0.249509 seconds and 4 git commands to generate.