~ubuntu-branches/ubuntu/utopic/binutils-arm64-cross/utopic

« back to all changes in this revision

Viewing changes to binutils-2.23.52.20130611/gas/config/tc-nios2.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-06-20 17:38:09 UTC
  • Revision ID: package-import@ubuntu.com-20130620173809-app8lzgvymy5fg6c
Tags: 0.7
Build-depend on binutils-source (>= 2.23.52.20130620-1~).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
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.  */
 
35
OBJ_ELF not defined
 
36
#endif
 
37
 
 
38
/* We can choose our endianness at run-time, regardless of configuration.  */
 
39
extern 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.  */
 
43
const 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.  */
 
52
const char line_comment_chars[] = "#";
 
53
 
 
54
/* This array holds machine specific line separator characters.  */
 
55
const char line_separator_chars[] = ";";
 
56
 
 
57
/* Chars that can be used to separate mant from exp in floating point nums.  */
 
58
const 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 */
 
63
const 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
 
 
71
const char *md_shortopts = "r";
 
72
 
 
73
struct option md_longopts[] = {
 
74
#define OPTION_RELAX_ALL (OPTION_MD_BASE + 0)
 
75
  {"relax-all", no_argument, NULL, OPTION_RELAX_ALL},
 
76
#define OPTION_NORELAX (OPTION_MD_BASE + 1)
 
77
  {"no-relax", no_argument, NULL, OPTION_NORELAX},
 
78
#define OPTION_RELAX_SECTION (OPTION_MD_BASE + 2)
 
79
  {"relax-section", no_argument, NULL, OPTION_RELAX_SECTION},
 
80
#define OPTION_EB (OPTION_MD_BASE + 3)
 
81
  {"EB", no_argument, NULL, OPTION_EB},
 
82
#define OPTION_EL (OPTION_MD_BASE + 4)
 
83
  {"EL", no_argument, NULL, OPTION_EL}
 
84
};
 
85
 
 
86
size_t md_longopts_size = sizeof (md_longopts);
 
87
 
 
88
/* The assembler supports three different relaxation modes, controlled by
 
89
   command-line options.  */
 
90
typedef 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.  */
 
98
struct
 
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
 
 
121
typedef 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.  */
 
138
typedef struct nios2_insn_info
 
139
{
 
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];
 
149
 
 
150
  /* This holds information used to generate fixups
 
151
     and eventually relocations if it is not null.  */
 
152
  nios2_insn_relocS *insn_reloc;
 
153
} nios2_insn_infoS;
 
154
 
 
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
 
160
{
 
161
  const char *args;
 
162
  void (*assemble_args_func) (nios2_insn_infoS *insn_info);
 
163
} nios2_arg_infoS;
 
164
 
 
165
/* This struct is used to convert Nios II pseudo-ops into the
 
166
   corresponding real op.  */
 
167
typedef struct nios2_ps_insn_info
 
168
{
 
169
  /* Map this pseudo_op... */
 
170
  const char *pseudo_insn;
 
171
 
 
172
  /* ...to this real instruction.  */
 
173
  const char *insn;
 
174
 
 
175
  /* Call this function to modify the operands....  */
 
176
  void (*arg_modifer_func) (char ** parsed_args, const char *arg, int num,
 
177
                            int start);
 
178
 
 
179
  /* ...with these arguments.  */
 
180
  const char *arg_modifier;
 
181
  int num;
 
182
  int index;
 
183
 
 
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;
 
188
 
 
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)))
 
193
 
 
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)))
 
198
 
 
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)))
 
203
 
 
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)))
 
208
 
 
209
/* The known current alignment of the current section.  */
 
210
static int nios2_current_align;
 
211
static segT nios2_current_align_seg;
 
212
 
 
213
static int nios2_auto_align_on = 1;
 
214
 
 
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;
 
218
 
 
219
#ifdef OBJ_ELF
 
220
/* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
 
221
symbolS *GOT_symbol;
 
222
#endif
 
223
 
 
224
 
 
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.  */
 
229
static valueT
 
230
md_chars_to_number (char *buf, int n)
 
231
{
 
232
  int i;
 
233
  valueT val;
 
234
 
 
235
  gas_assert (n == 1 || n == 2 || n == 4);
 
236
 
 
237
  val = 0;
 
238
  if (target_big_endian)
 
239
    for (i = 0; i < n; ++i)
 
240
      val = val | ((buf[i] & 0xff) << 8 * (n - (i + 1)));
 
241
  else
 
242
    for (i = 0; i < n; ++i)
 
243
      val = val | ((buf[i] & 0xff) << 8 * i);
 
244
  return val;
 
245
}
 
246
 
 
247
 
 
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.  */
 
251
void
 
252
md_number_to_chars (char *buf, valueT val, int n)
 
253
{
 
254
  gas_assert (n == 1 || n == 2 || n == 4 || n == 8);
 
255
  if (target_big_endian)
 
256
    number_to_chars_bigendian (buf, val, n);
 
257
  else
 
258
    number_to_chars_littleendian (buf, val, n);
 
259
}
 
260
 
 
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.  */
 
265
char *
 
266
md_atof (int type, char *litP, int *sizeP)
 
267
{
 
268
  int prec;
 
269
  LITTLENUM_TYPE words[4];
 
270
  char *t;
 
271
  int i;
 
272
 
 
273
  switch (type)
 
274
    {
 
275
    case 'f':
 
276
      prec = 2;
 
277
      break;
 
278
    case 'd':
 
279
      prec = 4;
 
280
      break;
 
281
    default:
 
282
      *sizeP = 0;
 
283
      return _("bad call to md_atof");
 
284
    }
 
285
 
 
286
  t = atof_ieee (input_line_pointer, type, words);
 
287
  if (t)
 
288
    input_line_pointer = t;
 
289
 
 
290
  *sizeP = prec * 2;
 
291
 
 
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);
 
295
  else
 
296
    for (i = 0; i < prec; i++, litP += 2)
 
297
      md_number_to_chars (litP, (valueT) words[i], 2);
 
298
 
 
299
  return NULL;
 
300
}
 
301
 
 
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)
 
305
 
 
306
/* Return true if STR is prefixed with a control register name.  */
 
307
static int
 
308
nios2_control_register_arg_p (const char *str)
 
309
{
 
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"));
 
326
}
 
327
 
 
328
/* Return true if STR is prefixed with a special relocation operator.  */
 
329
static int
 
330
nios2_special_relocation_p (const char *str)
 
331
{
 
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"));
 
346
}
 
347
 
 
348
/* Checks whether the register name is a coprocessor
 
349
   register - returns TRUE if it is, FALSE otherwise.  */
 
350
static bfd_boolean
 
351
nios2_coproc_reg (const char *reg_name)
 
352
{
 
353
  gas_assert (reg_name != NULL);
 
354
 
 
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
 
358
     register name.  */
 
359
  if (strprefix (reg_name, "c")
 
360
      && !strprefix (reg_name, "ctl")
 
361
      && hash_find (nios2_reg_hash, reg_name) != NULL)
 
362
    return TRUE;
 
363
  else
 
364
    return FALSE;
 
365
}
 
366
 
 
367
/* nop fill pattern for text section.  */
 
368
static char const nop[4] = { 0x3a, 0x88, 0x01, 0x00 };
 
369
 
 
370
/* Handles all machine-dependent alignment needs.  */
 
371
static void
 
372
nios2_align (int log_size, const char *pfill, symbolS *label)
 
373
{
 
374
  int align;
 
375
  long max_alignment = 15;
 
376
 
 
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);
 
380
 
 
381
  align = log_size;
 
382
  if (align > max_alignment)
 
383
    {
 
384
      align = max_alignment;
 
385
      as_bad (_("Alignment too large: %d. assumed"), align);
 
386
    }
 
387
  else if (align < 0)
 
388
    {
 
389
      as_warn (_("Alignment negative: 0 assumed"));
 
390
      align = 0;
 
391
    }
 
392
 
 
393
  if (align != 0)
 
394
    {
 
395
      if (subseg_text_p (now_seg) && align >= 2)
 
396
        {
 
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);
 
401
 
 
402
          /* Now fill in the alignment pattern.  */
 
403
          if (pfill != NULL)
 
404
            frag_align_pattern (align, pfill, sizeof nop, 0);
 
405
          else
 
406
            frag_align (align, 0, 0);
 
407
        }
 
408
      else
 
409
        frag_align (align, 0, 0);
 
410
 
 
411
      if (!switched_seg_p)
 
412
        nios2_current_align = align;
 
413
 
 
414
      /* If the last label was in a different section we can't align it.  */
 
415
      if (label != NULL && !switched_seg_p)
 
416
        {
 
417
          symbolS *sym;
 
418
          int label_seen = FALSE;
 
419
          struct frag *old_frag;
 
420
          valueT old_value;
 
421
          valueT new_value;
 
422
 
 
423
          gas_assert (S_GET_SEGMENT (label) == now_seg);
 
424
 
 
425
          old_frag = symbol_get_frag (label);
 
426
          old_value = S_GET_VALUE (label);
 
427
          new_value = (valueT) frag_now_fix ();
 
428
 
 
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)
 
443
              {
 
444
                label_seen = TRUE;
 
445
                symbol_set_frag (sym, frag_now);
 
446
                S_SET_VALUE (sym, new_value);
 
447
              }
 
448
            else if (label_seen && symbol_get_frag (sym) != old_frag)
 
449
              break;
 
450
        }
 
451
      record_alignment (now_seg, align);
 
452
    }
 
453
}
 
454
 
 
455
 
 
456
/** Support for self-check mode.  */
 
457
 
 
458
/* Mode of the assembler.  */
 
459
typedef enum
 
460
{
 
461
  NIOS2_MODE_ASSEMBLE,          /* Ordinary operation.  */
 
462
  NIOS2_MODE_TEST               /* Hidden mode used for self testing.  */
 
463
} NIOS2_MODE;
 
464
 
 
465
static NIOS2_MODE nios2_mode = NIOS2_MODE_ASSEMBLE;
 
466
 
 
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.  */
 
471
static void
 
472
nios2_check_assembly (unsigned int opcode, const char *exp_opcode)
 
473
{
 
474
  if (nios2_mode == NIOS2_MODE_TEST)
 
475
    {
 
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);
 
480
    }
 
481
}
 
482
 
 
483
 
 
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.  */
 
488
static void
 
489
s_nios2_align (int ignore ATTRIBUTE_UNUSED)
 
490
{
 
491
  int align;
 
492
  char fill;
 
493
  const char *pfill = NULL;
 
494
  long max_alignment = 15;
 
495
 
 
496
  align = get_absolute_expression ();
 
497
  if (align > max_alignment)
 
498
    {
 
499
      align = max_alignment;
 
500
      as_bad (_("Alignment too large: %d. assumed"), align);
 
501
    }
 
502
  else if (align < 0)
 
503
    {
 
504
      as_warn (_("Alignment negative: 0 assumed"));
 
505
      align = 0;
 
506
    }
 
507
 
 
508
  if (*input_line_pointer == ',')
 
509
    {
 
510
      input_line_pointer++;
 
511
      fill = get_absolute_expression ();
 
512
      pfill = (const char *) &fill;
 
513
    }
 
514
  else if (subseg_text_p (now_seg))
 
515
    pfill = (const char *) &nop;
 
516
  else
 
517
    {
 
518
      pfill = NULL;
 
519
      nios2_last_label = NULL;
 
520
    }
 
521
 
 
522
  if (align != 0)
 
523
    {
 
524
      nios2_auto_align_on = 1;
 
525
      nios2_align (align, pfill, nios2_last_label);
 
526
      nios2_last_label = NULL;
 
527
    }
 
528
  else
 
529
    nios2_auto_align_on = 0;
 
530
 
 
531
  demand_empty_rest_of_line ();
 
532
}
 
533
 
 
534
/* Handle the .text pseudo-op.  This is like the usual one, but it
 
535
   clears the saved last label and resets known alignment.  */
 
536
static void
 
537
s_nios2_text (int i)
 
538
{
 
539
  s_text (i);
 
540
  nios2_last_label = NULL;
 
541
  nios2_current_align = 0;
 
542
  nios2_current_align_seg = now_seg;
 
543
}
 
544
 
 
545
/* Handle the .data pseudo-op.  This is like the usual one, but it
 
546
   clears the saved last label and resets known alignment.  */
 
547
static void
 
548
s_nios2_data (int i)
 
549
{
 
550
  s_data (i);
 
551
  nios2_last_label = NULL;
 
552
  nios2_current_align = 0;
 
553
  nios2_current_align_seg = now_seg;
 
554
}
 
555
 
 
556
/* Handle the .section pseudo-op.  This is like the usual one, but it
 
557
   clears the saved last label and resets known alignment.  */
 
558
static void
 
559
s_nios2_section (int ignore)
 
560
{
 
561
  obj_elf_section (ignore);
 
562
  nios2_last_label = NULL;
 
563
  nios2_current_align = 0;
 
564
  nios2_current_align_seg = now_seg;
 
565
}
 
566
 
 
567
/* Explicitly unaligned cons.  */
 
568
static void
 
569
s_nios2_ucons (int nbytes)
 
570
{
 
571
  int hold;
 
572
  hold = nios2_auto_align_on;
 
573
  nios2_auto_align_on = 0;
 
574
  cons (nbytes);
 
575
  nios2_auto_align_on = hold;
 
576
}
 
577
 
 
578
/* Handle the .sdata directive.  */
 
579
static void
 
580
s_nios2_sdata (int ignore ATTRIBUTE_UNUSED)
 
581
{
 
582
  get_absolute_expression ();  /* Ignored.  */
 
583
  subseg_new (".sdata", 0);
 
584
  demand_empty_rest_of_line ();
 
585
}
 
586
 
 
587
/* .set sets assembler options eg noat/at and is also used
 
588
   to set symbol values (.equ, .equiv ).  */
 
589
static void
 
590
s_nios2_set (int equiv)
 
591
{
 
592
  char *directive = input_line_pointer;
 
593
  char delim = get_symbol_end ();
 
594
  char *endline = input_line_pointer;
 
595
  *endline = delim;
 
596
 
 
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.  */
 
601
  SKIP_WHITESPACE ();
 
602
  if (is_end_of_line[(unsigned char) *input_line_pointer]) 
 
603
    {
 
604
      bfd_boolean done = TRUE;
 
605
      *endline = 0;
 
606
      
 
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;
 
621
      else
 
622
        done = FALSE;
 
623
        
 
624
      if (done)
 
625
        {
 
626
          *endline = delim;
 
627
          demand_empty_rest_of_line ();
 
628
          return;
 
629
        }
 
630
    }
 
631
 
 
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 
 
634
     a syntax error.  */
 
635
  input_line_pointer = directive;
 
636
  *endline = delim;
 
637
  s_set (equiv);
 
638
}
 
639
 
 
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.  */
 
652
  {"dword", cons, 8},
 
653
  {"half", cons, 2},
 
654
  /* NIOS2 native word size is 4 bytes, so we override
 
655
     the GAS default of 2.  */
 
656
  {"word", cons, 4},
 
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},
 
662
#ifdef OBJ_ELF
 
663
  {"sdata", s_nios2_sdata, 0},
 
664
#endif
 
665
  {"set", s_nios2_set, 0},
 
666
  {NULL, NULL, 0}
 
667
};
 
668
 
 
669
 
 
670
/** Relaxation support. */
 
671
 
 
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.
 
674
 
 
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
 
677
   using two registers.
 
678
  
 
679
   To deal with this, for the pc-relative relaxation mode we convert
 
680
     br label
 
681
   into a series of 16-bit adds, like:
 
682
     nextpc at
 
683
     addi at, at, 32767
 
684
     ...
 
685
     addi at, at, remainder
 
686
     jmp at
 
687
 
 
688
   Similarly, conditional branches are converted from
 
689
     b(condition) r, s, label
 
690
   into a series like:
 
691
     b(opposite condition) r, s, skip
 
692
     nextpc at
 
693
     addi at, at, 32767
 
694
     ...
 
695
     addi at, at, remainder
 
696
     jmp at
 
697
     skip:
 
698
 
 
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.
 
702
 
 
703
   For the -relax-all relaxation mode, the conversions are
 
704
     movhi at, %hi(symbol+offset)
 
705
     ori at, %lo(symbol+offset)
 
706
     jmp at
 
707
   and
 
708
     b(opposite condition), r, s, skip
 
709
     movhi at, %hi(symbol+offset)
 
710
     ori at, %lo(symbol+offset)
 
711
     jmp at
 
712
     skip:
 
713
   respectively.
 
714
*/
 
715
 
 
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
 
726
 
 
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
 
730
   conditional branch.
 
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)
 
743
 
 
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)
 
748
 
 
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
 
753
 
 
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))
 
763
 
 
764
/* Register number of AT, the assembler temporary.  */
 
765
#define AT_REGNUM 1
 
766
 
 
767
/* Determine how many bytes are required to represent the sequence
 
768
   indicated by SUBTYPE.  */
 
769
static int
 
770
nios2_relax_subtype_size (relax_substateT subtype)
 
771
{
 
772
  int n = SUBTYPE_ADDIS (subtype);
 
773
  if (n == 0)
 
774
    /* Regular conditional/unconditional branch instruction.  */
 
775
    return 4;
 
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);
 
780
  else
 
781
    return UBRANCH_ADDIS_TO_SIZE (n);
 
782
}
 
783
 
 
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.  */
 
787
int
 
788
md_estimate_size_before_relax (fragS *fragp, segT segment ATTRIBUTE_UNUSED)
 
789
{
 
790
  return nios2_relax_subtype_size (fragp->fr_subtype);
 
791
}
 
792
 
 
793
/* Implement md_relax_frag, returning the change in size of the frag.  */
 
794
long
 
795
nios2_relax_frag (segT segment, fragS *fragp, long stretch)
 
796
{
 
797
  addressT target = fragp->fr_offset;
 
798
  relax_substateT subtype = fragp->fr_subtype;
 
799
  symbolS *symbolp = fragp->fr_symbol;
 
800
 
 
801
  if (symbolp)
 
802
    {
 
803
      fragS *sym_frag = symbol_get_frag (symbolp);
 
804
      offsetT offset;
 
805
      int n;
 
806
 
 
807
      target += S_GET_VALUE (symbolp);
 
808
 
 
809
      /* See comments in write.c:relax_frag about handling of stretch.  */
 
810
      if (stretch != 0
 
811
          && sym_frag->relax_marker != fragp->relax_marker)
 
812
        {
 
813
          if (stretch < 0 || sym_frag->region == fragp->region)
 
814
            target += stretch;
 
815
          else if (target < fragp->fr_address)
 
816
            target = fragp->fr_next->fr_address + stretch;
 
817
        }
 
818
 
 
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.  */
 
824
        n = 0;
 
825
      else if (nios2_as_options.relax == relax_all)
 
826
        /* Convert to jump.  */
 
827
        n = 1;
 
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.  */
 
833
        {
 
834
          /* The relaxation for conditional branches is offset by 4
 
835
             bytes because we insert the inverted branch around the
 
836
             sequence.  */
 
837
          if (IS_CBRANCH (subtype))
 
838
            offset = offset - 4;
 
839
          if (offset > 0)
 
840
            n = offset / 32767 + 1;
 
841
          else
 
842
            n = offset / -32768 + 1;
 
843
 
 
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)
 
851
            {
 
852
              as_bad_where (fragp->fr_file, fragp->fr_line,
 
853
                            _("branch offset out of range\n"));
 
854
              as_fatal (_("branch relaxation failed\n"));
 
855
            }
 
856
        }
 
857
      else
 
858
        /* We cannot handle this case, diagnose overflow later.  */
 
859
        return 0;
 
860
 
 
861
      if (IS_CBRANCH (subtype))
 
862
        fragp->fr_subtype = CBRANCH_SUBTYPE (n);
 
863
      else
 
864
        fragp->fr_subtype = UBRANCH_SUBTYPE (n);
 
865
 
 
866
      return (nios2_relax_subtype_size (fragp->fr_subtype)
 
867
              - nios2_relax_subtype_size (subtype));
 
868
    }
 
869
 
 
870
  /* If we got here, it's probably an error.  */
 
871
  return 0;
 
872
}
 
873
 
 
874
 
 
875
/* Complete fragp using the data from the relaxation pass. */
 
876
void
 
877
md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED,
 
878
                 fragS *fragp)
 
879
{
 
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;
 
885
  offsetT offset;
 
886
  unsigned int addend_mask, addi_mask;
 
887
  offsetT addend, remainder;
 
888
  int i;
 
889
 
 
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.  */
 
892
  if (n == 0)
 
893
    {
 
894
      fix_new (fragp, fragp->fr_fix, 4, fragp->fr_symbol, fragp->fr_offset, 1,
 
895
               BFD_RELOC_16_PCREL);
 
896
      fragp->fr_fix += 4;
 
897
      return;
 
898
    }
 
899
 
 
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))
 
903
    {
 
904
      unsigned int br_opcode;
 
905
      int nbytes;
 
906
 
 
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;
 
911
      else
 
912
        nbytes = 12;
 
913
 
 
914
      br_opcode = md_chars_to_number (buffer, 4);
 
915
      switch (br_opcode & OP_MASK_OP)
 
916
        {
 
917
        case OP_MATCH_BEQ:
 
918
          br_opcode = (br_opcode & ~OP_MASK_OP) | OP_MATCH_BNE;
 
919
          break;
 
920
        case OP_MATCH_BNE:
 
921
          br_opcode = (br_opcode & ~OP_MASK_OP) | OP_MATCH_BEQ ;
 
922
          break;
 
923
        case OP_MATCH_BGE:
 
924
          br_opcode = (br_opcode & ~OP_MASK_OP) | OP_MATCH_BLT ;
 
925
          break;
 
926
        case OP_MATCH_BGEU:
 
927
          br_opcode = (br_opcode & ~OP_MASK_OP) | OP_MATCH_BLTU ;
 
928
          break;
 
929
        case OP_MATCH_BLT:
 
930
          br_opcode = (br_opcode & ~OP_MASK_OP) | OP_MATCH_BGE ;
 
931
          break;
 
932
        case OP_MATCH_BLTU:
 
933
          br_opcode = (br_opcode & ~OP_MASK_OP) | OP_MATCH_BGEU ;
 
934
          break;
 
935
        default:
 
936
          as_bad_where (fragp->fr_file, fragp->fr_line,
 
937
                        _("expecting conditional branch for relaxation\n"));
 
938
          abort ();
 
939
        }
 
940
 
 
941
      br_opcode = br_opcode | (nbytes << OP_SH_IMM16);
 
942
      md_number_to_chars (buffer, br_opcode, 4);
 
943
      fragp->fr_fix += 4;
 
944
      buffer += 4;
 
945
    }
 
946
 
 
947
  /* Load at for the PC-relative case.  */
 
948
  if (nios2_as_options.relax == relax_section)
 
949
    {
 
950
      /* Insert the nextpc instruction.  */
 
951
      md_number_to_chars (buffer,
 
952
                          OP_MATCH_NEXTPC | (AT_REGNUM << OP_SH_RRD), 4);
 
953
      fragp->fr_fix += 4;
 
954
      buffer += 4;
 
955
  
 
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;
 
959
      if (offset > 0)
 
960
        addend = 32767;
 
961
      else
 
962
        addend = -32768;
 
963
      addend_mask = (((unsigned int)addend) & 0xffff) << OP_SH_IMM16;
 
964
 
 
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 ++)
 
970
        {
 
971
          md_number_to_chars (buffer, addi_mask | addend_mask, 4);
 
972
          fragp->fr_fix += 4;
 
973
          buffer += 4;
 
974
        }
 
975
 
 
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);
 
981
      fragp->fr_fix += 4;
 
982
      buffer += 4;
 
983
    }
 
984
 
 
985
  /* Load at for the absolute case.  */
 
986
  else
 
987
    {
 
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);
 
991
      fragp->fr_fix += 4;
 
992
      buffer += 4;
 
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);
 
996
      fragp->fr_fix += 4;
 
997
      buffer += 4;
 
998
    }
 
999
 
 
1000
  /* Insert the jmp instruction.  */
 
1001
  md_number_to_chars (buffer, OP_MATCH_JMP | (AT_REGNUM << OP_SH_RRS), 4);
 
1002
  fragp->fr_fix += 4;
 
1003
  buffer += 4;
 
1004
}
 
1005
 
 
1006
 
 
1007
/** Fixups and overflow checking.  */
 
1008
 
 
1009
/* Check a fixup for overflow. */
 
1010
static bfd_boolean
 
1011
nios2_check_overflow (valueT fixup, reloc_howto_type *howto)
 
1012
{
 
1013
  /* Apply the rightshift before checking for overflow.  */
 
1014
  fixup = ((signed)fixup) >> howto->rightshift;
 
1015
 
 
1016
  /* Check for overflow - return TRUE if overflow, FALSE if not.  */
 
1017
  switch (howto->complain_on_overflow)
 
1018
    {
 
1019
    case complain_overflow_dont:
 
1020
      break;
 
1021
    case complain_overflow_bitfield:
 
1022
      if ((fixup >> howto->bitsize) != 0
 
1023
          && ((signed) fixup >> howto->bitsize) != -1)
 
1024
        return TRUE;
 
1025
      break;
 
1026
    case complain_overflow_signed:
 
1027
      if ((fixup & 0x80000000) > 0)
 
1028
        {
 
1029
          /* Check for negative overflow.  */
 
1030
          if ((signed) fixup < ((signed) 0x80000000 >> howto->bitsize))
 
1031
            return TRUE;
 
1032
        }
 
1033
      else
 
1034
        {
 
1035
          /* Check for positive overflow.  */
 
1036
          if (fixup >= ((unsigned) 1 << (howto->bitsize - 1)))
 
1037
            return TRUE;
 
1038
        }
 
1039
      break;
 
1040
    case complain_overflow_unsigned:
 
1041
      if ((fixup >> howto->bitsize) != 0)
 
1042
        return TRUE;
 
1043
      break;
 
1044
    default:
 
1045
      as_bad (_("error checking for overflow - broken assembler"));
 
1046
      break;
 
1047
    }
 
1048
  return FALSE;
 
1049
}
 
1050
 
 
1051
/* Emit diagnostic for fixup overflow.  */
 
1052
static void
 
1053
nios2_diagnose_overflow (valueT fixup, reloc_howto_type *howto,
 
1054
                         fixS *fixP, valueT value)
 
1055
{
 
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));
 
1064
  else
 
1065
    {
 
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)
 
1079
        {
 
1080
        case call_target_overflow:
 
1081
          range_min
 
1082
            = ((fixP->fx_frag->fr_address + fixP->fx_where) & 0xf0000000);
 
1083
          range_max = range_min + 0x0fffffff;
 
1084
          address = fixup | range_min;
 
1085
          
 
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);
 
1089
          break;
 
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);
 
1094
          break;
 
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);
 
1099
          break;
 
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);
 
1104
          break;
 
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);
 
1109
          break;
 
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);
 
1114
          break;
 
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);
 
1119
          break;
 
1120
        default:
 
1121
          as_bad_where (fixP->fx_file, fixP->fx_line,
 
1122
                        _("overflow in immediate argument"));
 
1123
          break;
 
1124
        }
 
1125
    }
 
1126
}
 
1127
 
 
1128
/* Apply a fixup to the object file.  */
 
1129
void
 
1130
md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
 
1131
{
 
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_IMM5
 
1143
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_CACHE_OPX
 
1144
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM6
 
1145
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_IMM8
 
1146
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_HI16
 
1147
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_LO16
 
1148
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_HIADJ16
 
1149
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_GPREL
 
1150
                  || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
 
1151
                  || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
 
1152
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_UJMP
 
1153
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_CJMP
 
1154
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_CALLR
 
1155
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_ALIGN
 
1156
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_GOT16
 
1157
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_CALL16
 
1158
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_LO
 
1159
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_HA
 
1160
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_GD16
 
1161
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LDM16
 
1162
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LDO16
 
1163
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_IE16
 
1164
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_LE16
 
1165
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_GOTOFF
 
1166
                  || fixP->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPREL
 
1167
                  /* Add other relocs here as we generate them.  */
 
1168
                  ));
 
1169
 
 
1170
  if (fixP->fx_r_type == BFD_RELOC_64)
 
1171
    {
 
1172
      /* We may reach here due to .8byte directives, but we never output
 
1173
         BFD_RELOC_64; it must be resolved.  */      
 
1174
      if (fixP->fx_addsy != NULL)
 
1175
        as_bad_where (fixP->fx_file, fixP->fx_line,
 
1176
                      _("cannot create 64-bit relocation"));
 
1177
      else
 
1178
        {
 
1179
          md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
 
1180
                              *valP, 8);
 
1181
          fixP->fx_done = 1;
 
1182
        }
 
1183
      return;
 
1184
    }
 
1185
 
 
1186
  /* The value passed in valP can be the value of a fully
 
1187
     resolved expression, or it can be the value of a partially
 
1188
     resolved expression.  In the former case, both fixP->fx_addsy
 
1189
     and fixP->fx_subsy are NULL, and fixP->fx_offset == *valP, and
 
1190
     we can fix up the instruction that fixP relates to.
 
1191
     In the latter case, one or both of fixP->fx_addsy and
 
1192
     fixP->fx_subsy are not NULL, and fixP->fx_offset may or may not
 
1193
     equal *valP.  We don't need to check for fixP->fx_subsy being null
 
1194
     because the generic part of the assembler generates an error if
 
1195
     it is not an absolute symbol.  */
 
1196
  if (fixP->fx_addsy != NULL)
 
1197
    /* Partially resolved expression.  */
 
1198
    {
 
1199
      fixP->fx_addnumber = fixP->fx_offset;
 
1200
      fixP->fx_done = 0;
 
1201
 
 
1202
      switch (fixP->fx_r_type)
 
1203
        {
 
1204
        case BFD_RELOC_NIOS2_TLS_GD16:
 
1205
        case BFD_RELOC_NIOS2_TLS_LDM16:
 
1206
        case BFD_RELOC_NIOS2_TLS_LDO16:
 
1207
        case BFD_RELOC_NIOS2_TLS_IE16:
 
1208
        case BFD_RELOC_NIOS2_TLS_LE16:
 
1209
        case BFD_RELOC_NIOS2_TLS_DTPMOD:
 
1210
        case BFD_RELOC_NIOS2_TLS_DTPREL:
 
1211
        case BFD_RELOC_NIOS2_TLS_TPREL:
 
1212
          S_SET_THREAD_LOCAL (fixP->fx_addsy);
 
1213
          break;
 
1214
        default:
 
1215
          break;
 
1216
        }
 
1217
    }
 
1218
  else
 
1219
    /* Fully resolved fixup.  */
 
1220
    {
 
1221
      reloc_howto_type *howto
 
1222
        = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
 
1223
 
 
1224
      if (howto == NULL)
 
1225
        as_bad_where (fixP->fx_file, fixP->fx_line,
 
1226
                      _("relocation is not supported"));
 
1227
      else
 
1228
        {
 
1229
          valueT fixup = *valP;
 
1230
          valueT value;
 
1231
          char *buf;
 
1232
 
 
1233
          /* If this is a pc-relative relocation, we need to
 
1234
             subtract the current offset within the object file
 
1235
             FIXME : for some reason fixP->fx_pcrel isn't 1 when it should be
 
1236
             so I'm using the howto structure instead to determine this.  */
 
1237
          if (howto->pc_relative == 1)
 
1238
            fixup = fixup - (fixP->fx_frag->fr_address + fixP->fx_where + 4);
 
1239
 
 
1240
          /* Get the instruction or data to be fixed up.  */
 
1241
          buf = fixP->fx_frag->fr_literal + fixP->fx_where;
 
1242
          value = md_chars_to_number (buf, fixP->fx_size);
 
1243
 
 
1244
          /* Check for overflow, emitting a diagnostic if necessary.  */
 
1245
          if (nios2_check_overflow (fixup, howto))
 
1246
            nios2_diagnose_overflow (fixup, howto, fixP, value);
 
1247
 
 
1248
          /* Apply the right shift.  */
 
1249
          fixup = ((signed)fixup) >> howto->rightshift;
 
1250
 
 
1251
          /* Truncate the fixup to right size.  */
 
1252
          switch (fixP->fx_r_type)
 
1253
            {
 
1254
            case BFD_RELOC_NIOS2_HI16:
 
1255
              fixup = (fixup >> 16) & 0xFFFF;
 
1256
              break;
 
1257
            case BFD_RELOC_NIOS2_LO16:
 
1258
              fixup = fixup & 0xFFFF;
 
1259
              break;
 
1260
            case BFD_RELOC_NIOS2_HIADJ16:
 
1261
              fixup = ((fixup >> 16) & 0xFFFF) + ((fixup >> 15) & 0x01);
 
1262
              break;
 
1263
            default:
 
1264
              {
 
1265
                int n = sizeof (fixup) * 8 - howto->bitsize;
 
1266
                fixup = (fixup << n) >> n;
 
1267
                break;
 
1268
              }
 
1269
            }
 
1270
 
 
1271
          /* Fix up the instruction.  */
 
1272
          value = (value & ~howto->dst_mask) | (fixup << howto->bitpos);
 
1273
          md_number_to_chars (buf, value, fixP->fx_size);
 
1274
        }
 
1275
 
 
1276
      fixP->fx_done = 1;
 
1277
    }
 
1278
 
 
1279
  if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
 
1280
    {
 
1281
      fixP->fx_done = 0;
 
1282
      if (fixP->fx_addsy
 
1283
          && !S_IS_DEFINED (fixP->fx_addsy) && !S_IS_WEAK (fixP->fx_addsy))
 
1284
        S_SET_WEAK (fixP->fx_addsy);
 
1285
    }
 
1286
  else if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
 
1287
    fixP->fx_done = 0;
 
1288
}
 
1289
 
 
1290
 
 
1291
 
 
1292
/** Instruction parsing support. */
 
1293
 
 
1294
/* Special relocation directive strings.  */
 
1295
 
 
1296
struct nios2_special_relocS
 
1297
{
 
1298
  const char *string;
 
1299
  bfd_reloc_code_real_type reloc_type;
 
1300
};
 
1301
 
 
1302
struct nios2_special_relocS nios2_special_reloc[] = {
 
1303
  {"%hiadj", BFD_RELOC_NIOS2_HIADJ16},
 
1304
  {"%hi", BFD_RELOC_NIOS2_HI16},
 
1305
  {"%lo", BFD_RELOC_NIOS2_LO16},
 
1306
  {"%gprel", BFD_RELOC_NIOS2_GPREL},
 
1307
  {"%call", BFD_RELOC_NIOS2_CALL16},
 
1308
  {"%gotoff_lo", BFD_RELOC_NIOS2_GOTOFF_LO},
 
1309
  {"%gotoff_hiadj", BFD_RELOC_NIOS2_GOTOFF_HA},
 
1310
  {"%tls_gd", BFD_RELOC_NIOS2_TLS_GD16},
 
1311
  {"%tls_ldm", BFD_RELOC_NIOS2_TLS_LDM16},
 
1312
  {"%tls_ldo", BFD_RELOC_NIOS2_TLS_LDO16},
 
1313
  {"%tls_ie", BFD_RELOC_NIOS2_TLS_IE16},
 
1314
  {"%tls_le", BFD_RELOC_NIOS2_TLS_LE16},
 
1315
  {"%gotoff", BFD_RELOC_NIOS2_GOTOFF},
 
1316
  {"%got", BFD_RELOC_NIOS2_GOT16}
 
1317
};
 
1318
 
 
1319
#define NIOS2_NUM_SPECIAL_RELOCS \
 
1320
        (sizeof(nios2_special_reloc)/sizeof(nios2_special_reloc[0]))
 
1321
const int nios2_num_special_relocs = NIOS2_NUM_SPECIAL_RELOCS;
 
1322
 
 
1323
/* Creates a new nios2_insn_relocS and returns a pointer to it.  */
 
1324
static nios2_insn_relocS *
 
1325
nios2_insn_reloc_new (bfd_reloc_code_real_type reloc_type, unsigned int pcrel)
 
1326
{
 
1327
  nios2_insn_relocS *retval;
 
1328
  retval = (nios2_insn_relocS *) malloc (sizeof (nios2_insn_relocS));
 
1329
  if (retval == NULL)
 
1330
    {
 
1331
      as_bad (_("can't create relocation"));
 
1332
      abort ();
 
1333
    }
 
1334
 
 
1335
  /* Fill out the fields with default values.  */
 
1336
  retval->reloc_next = NULL;
 
1337
  retval->reloc_type = reloc_type;
 
1338
  retval->reloc_pcrel = pcrel;
 
1339
  return retval;
 
1340
}
 
1341
 
 
1342
/* Frees up memory previously allocated by nios2_insn_reloc_new().  */
 
1343
/* FIXME:  this is never called; memory leak?  */
 
1344
#if 0
 
1345
static void
 
1346
nios2_insn_reloc_destroy (nios2_insn_relocS *reloc)
 
1347
{
 
1348
  gas_assert (reloc != NULL);
 
1349
  free (reloc);
 
1350
}
 
1351
#endif
 
1352
 
 
1353
/* The various nios2_assemble_* functions call this
 
1354
   function to generate an expression from a string representing an expression.
 
1355
   It then tries to evaluate the expression, and if it can, returns its value.
 
1356
   If not, it creates a new nios2_insn_relocS and stores the expression and 
 
1357
   reloc_type for future use.  */
 
1358
static unsigned long
 
1359
nios2_assemble_expression (const char *exprstr,
 
1360
                           nios2_insn_infoS *insn,
 
1361
                           nios2_insn_relocS *prev_reloc,
 
1362
                           bfd_reloc_code_real_type reloc_type,
 
1363
                           unsigned int pcrel)
 
1364
{
 
1365
  nios2_insn_relocS *reloc;
 
1366
  char *saved_line_ptr;
 
1367
  unsigned short value;
 
1368
  int i;
 
1369
 
 
1370
  gas_assert (exprstr != NULL);
 
1371
  gas_assert (insn != NULL);
 
1372
 
 
1373
  /* Check for relocation operators.
 
1374
     Change the relocation type and advance the ptr to the start of
 
1375
     the expression proper. */
 
1376
  for (i = 0; i < nios2_num_special_relocs; i++)
 
1377
    if (strstr (exprstr, nios2_special_reloc[i].string) != NULL)
 
1378
      {
 
1379
        reloc_type = nios2_special_reloc[i].reloc_type;
 
1380
        exprstr += strlen (nios2_special_reloc[i].string) + 1;
 
1381
        
 
1382
        /* %lo and %hiadj have different meanings for PC-relative
 
1383
           expressions.  */
 
1384
        if (pcrel)
 
1385
          {
 
1386
            if (reloc_type == BFD_RELOC_NIOS2_LO16)
 
1387
              reloc_type = BFD_RELOC_NIOS2_PCREL_LO;
 
1388
            if (reloc_type == BFD_RELOC_NIOS2_HIADJ16)
 
1389
              reloc_type = BFD_RELOC_NIOS2_PCREL_HA;
 
1390
          }
 
1391
        
 
1392
        break;
 
1393
      }
 
1394
 
 
1395
  /* We potentially have a relocation.  */
 
1396
  reloc = nios2_insn_reloc_new (reloc_type, pcrel);
 
1397
  if (prev_reloc != NULL)
 
1398
    prev_reloc->reloc_next = reloc;
 
1399
  else
 
1400
    insn->insn_reloc = reloc;
 
1401
 
 
1402
  /* Parse the expression string.  */
 
1403
  saved_line_ptr = input_line_pointer;
 
1404
  input_line_pointer = (char *) exprstr;
 
1405
  expression (&reloc->reloc_expression);
 
1406
  input_line_pointer = saved_line_ptr;
 
1407
 
 
1408
  /* This is redundant as the fixup will put this into
 
1409
     the instruction, but it is included here so that
 
1410
     self-test mode (-r) works.  */
 
1411
  value = 0;
 
1412
  if (nios2_mode == NIOS2_MODE_TEST
 
1413
      && reloc->reloc_expression.X_op == O_constant)
 
1414
    value = reloc->reloc_expression.X_add_number;
 
1415
 
 
1416
  return (unsigned long) value;
 
1417
}
 
1418
 
 
1419
/* Argument assemble functions.
 
1420
   All take an instruction argument string, and a pointer
 
1421
   to an instruction opcode. Upon return the insn_opcode
 
1422
   has the relevant fields filled in to represent the arg
 
1423
   string.  The return value is NULL if successful, or
 
1424
   an error message if an error was detected.
 
1425
 
 
1426
   The naming conventions for these functions match the args template
 
1427
   in the nios2_opcode structure, as documented in include/opcode/nios2.h.
 
1428
   For example, nios2_assemble_args_dst is used for instructions with
 
1429
   "d,s,t" args.
 
1430
   See nios2_arg_info_structs below for the exact correspondence.  */
 
1431
 
 
1432
static void
 
1433
nios2_assemble_args_dst (nios2_insn_infoS *insn_info)
 
1434
{
 
1435
  if (insn_info->insn_tokens[1] != NULL
 
1436
      && insn_info->insn_tokens[2] != NULL
 
1437
      && insn_info->insn_tokens[3] != NULL)
 
1438
    {
 
1439
      struct nios2_reg *dst = nios2_reg_lookup (insn_info->insn_tokens[1]);
 
1440
      struct nios2_reg *src1 = nios2_reg_lookup (insn_info->insn_tokens[2]);
 
1441
      struct nios2_reg *src2 = nios2_reg_lookup (insn_info->insn_tokens[3]);
 
1442
 
 
1443
      if (dst == NULL)
 
1444
        as_bad (_("unknown register %s"), insn_info->insn_tokens[1]);
 
1445
      else
 
1446
        SET_INSN_FIELD (RRD, insn_info->insn_code, dst->index);
 
1447
 
 
1448
      if (src1 == NULL)
 
1449
        as_bad (_("unknown register %s"), insn_info->insn_tokens[2]);
 
1450
      else
 
1451
        SET_INSN_FIELD (RRS, insn_info->insn_code, src1->index);
 
1452
 
 
1453
      if (src2 == NULL)
 
1454
        as_bad (_("unknown register %s"), insn_info->insn_tokens[3]);
 
1455
      else
 
1456
        SET_INSN_FIELD (RRT, insn_info->insn_code, src2->index);
 
1457
 
 
1458
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[4]);
 
1459
    }
 
1460
}
 
1461
 
 
1462
static void
 
1463
nios2_assemble_args_tsi (nios2_insn_infoS *insn_info)
 
1464
{
 
1465
  if (insn_info->insn_tokens[1] != NULL &&
 
1466
      insn_info->insn_tokens[2] != NULL && insn_info->insn_tokens[3] != NULL)
 
1467
    {
 
1468
      struct nios2_reg *dst = nios2_reg_lookup (insn_info->insn_tokens[1]);
 
1469
      struct nios2_reg *src1 = nios2_reg_lookup (insn_info->insn_tokens[2]);
 
1470
      unsigned int src2
 
1471
        = nios2_assemble_expression (insn_info->insn_tokens[3], insn_info,
 
1472
                                     insn_info->insn_reloc, BFD_RELOC_NIOS2_S16,
 
1473
                                     0);
 
1474
 
 
1475
      if (dst == NULL)
 
1476
        as_bad (_("unknown register %s"), insn_info->insn_tokens[1]);
 
1477
      else
 
1478
        SET_INSN_FIELD (IRT, insn_info->insn_code, dst->index);
 
1479
 
 
1480
      if (src1 == NULL)
 
1481
        as_bad (_("unknown register %s"), insn_info->insn_tokens[2]);
 
1482
      else
 
1483
        SET_INSN_FIELD (IRS, insn_info->insn_code, src1->index);
 
1484
 
 
1485
      SET_INSN_FIELD (IMM16, insn_info->insn_code, src2);
 
1486
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[4]);
 
1487
      SET_INSN_FIELD (IMM16, insn_info->insn_code, 0);
 
1488
    }
 
1489
}
 
1490
 
 
1491
static void
 
1492
nios2_assemble_args_tsu (nios2_insn_infoS *insn_info)
 
1493
{
 
1494
  if (insn_info->insn_tokens[1] != NULL
 
1495
      && insn_info->insn_tokens[2] != NULL
 
1496
      && insn_info->insn_tokens[3] != NULL)
 
1497
    {
 
1498
      struct nios2_reg *dst = nios2_reg_lookup (insn_info->insn_tokens[1]);
 
1499
      struct nios2_reg *src1 = nios2_reg_lookup (insn_info->insn_tokens[2]);
 
1500
      unsigned int src2
 
1501
        = nios2_assemble_expression (insn_info->insn_tokens[3], insn_info,
 
1502
                                     insn_info->insn_reloc, BFD_RELOC_NIOS2_U16,
 
1503
                                     0);
 
1504
 
 
1505
      if (dst == NULL)
 
1506
        as_bad (_("unknown register %s"), insn_info->insn_tokens[1]);
 
1507
      else
 
1508
        SET_INSN_FIELD (IRT, insn_info->insn_code, dst->index);
 
1509
 
 
1510
      if (src1 == NULL)
 
1511
        as_bad (_("unknown register %s"), insn_info->insn_tokens[2]);
 
1512
      else
 
1513
        SET_INSN_FIELD (IRS, insn_info->insn_code, src1->index);
 
1514
 
 
1515
      SET_INSN_FIELD (IMM16, insn_info->insn_code, src2);
 
1516
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[4]);
 
1517
      SET_INSN_FIELD (IMM16, insn_info->insn_code, 0);
 
1518
    }
 
1519
}
 
1520
 
 
1521
static void
 
1522
nios2_assemble_args_sto (nios2_insn_infoS *insn_info)
 
1523
{
 
1524
  if (insn_info->insn_tokens[1] != NULL
 
1525
      && insn_info->insn_tokens[2] != NULL
 
1526
      && insn_info->insn_tokens[3] != NULL)
 
1527
    {
 
1528
      struct nios2_reg *dst = nios2_reg_lookup (insn_info->insn_tokens[1]);
 
1529
      struct nios2_reg *src1 = nios2_reg_lookup (insn_info->insn_tokens[2]);
 
1530
      unsigned int src2
 
1531
        = nios2_assemble_expression (insn_info->insn_tokens[3], insn_info,
 
1532
                                     insn_info->insn_reloc, BFD_RELOC_16_PCREL,
 
1533
                                     1);
 
1534
 
 
1535
      if (dst == NULL)
 
1536
        as_bad (_("unknown register %s"), insn_info->insn_tokens[1]);
 
1537
      else
 
1538
        SET_INSN_FIELD (IRS, insn_info->insn_code, dst->index);
 
1539
 
 
1540
      if (src1 == NULL)
 
1541
        as_bad (_("unknown register %s"), insn_info->insn_tokens[2]);
 
1542
      else
 
1543
        SET_INSN_FIELD (IRT, insn_info->insn_code, src1->index);
 
1544
 
 
1545
      SET_INSN_FIELD (IMM16, insn_info->insn_code, src2);
 
1546
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[4]);
 
1547
      SET_INSN_FIELD (IMM16, insn_info->insn_code, 0);
 
1548
    }
 
1549
}
 
1550
 
 
1551
static void
 
1552
nios2_assemble_args_o (nios2_insn_infoS *insn_info)
 
1553
{
 
1554
  if (insn_info->insn_tokens[1] != NULL)
 
1555
    {
 
1556
      unsigned long immed
 
1557
        = nios2_assemble_expression (insn_info->insn_tokens[1], insn_info,
 
1558
                                     insn_info->insn_reloc, BFD_RELOC_16_PCREL,
 
1559
                                     1);
 
1560
      SET_INSN_FIELD (IMM16, insn_info->insn_code, immed);
 
1561
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[2]);
 
1562
      SET_INSN_FIELD (IMM16, insn_info->insn_code, 0);
 
1563
    }
 
1564
}
 
1565
 
 
1566
static void
 
1567
nios2_assemble_args_is (nios2_insn_infoS *insn_info)
 
1568
{
 
1569
  if (insn_info->insn_tokens[1] != NULL && insn_info->insn_tokens[2] != NULL)
 
1570
    {
 
1571
      struct nios2_reg *addr_src = nios2_reg_lookup (insn_info->insn_tokens[2]);
 
1572
      unsigned long immed
 
1573
        = nios2_assemble_expression (insn_info->insn_tokens[1], insn_info,
 
1574
                                     insn_info->insn_reloc, BFD_RELOC_NIOS2_S16,
 
1575
                                     0);
 
1576
 
 
1577
      SET_INSN_FIELD (IMM16, insn_info->insn_code, immed);
 
1578
 
 
1579
      if (addr_src == NULL)
 
1580
        as_bad (_("unknown base register %s"), insn_info->insn_tokens[2]);
 
1581
      else
 
1582
        SET_INSN_FIELD (RRS, insn_info->insn_code, addr_src->index);
 
1583
 
 
1584
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[3]);
 
1585
      SET_INSN_FIELD (IMM16, insn_info->insn_code, 0);
 
1586
    }
 
1587
}
 
1588
 
 
1589
static void
 
1590
nios2_assemble_args_m (nios2_insn_infoS *insn_info)
 
1591
{
 
1592
  if (insn_info->insn_tokens[1] != NULL)
 
1593
    {
 
1594
      unsigned long immed
 
1595
        = nios2_assemble_expression (insn_info->insn_tokens[1], insn_info,
 
1596
                                     insn_info->insn_reloc,
 
1597
                                     BFD_RELOC_NIOS2_CALL26, 0);
 
1598
 
 
1599
      SET_INSN_FIELD (IMM26, insn_info->insn_code, immed);
 
1600
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[2]);
 
1601
      SET_INSN_FIELD (IMM26, insn_info->insn_code, 0);
 
1602
    }
 
1603
}
 
1604
 
 
1605
static void
 
1606
nios2_assemble_args_s (nios2_insn_infoS *insn_info)
 
1607
{
 
1608
  if (insn_info->insn_tokens[1] != NULL)
 
1609
    {
 
1610
      struct nios2_reg *src = nios2_reg_lookup (insn_info->insn_tokens[1]);
 
1611
      if (src == NULL)
 
1612
        as_bad (_("unknown register %s"), insn_info->insn_tokens[1]);
 
1613
      else
 
1614
        SET_INSN_FIELD (RRS, insn_info->insn_code, src->index);
 
1615
 
 
1616
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[2]);
 
1617
    }
 
1618
}
 
1619
 
 
1620
static void
 
1621
nios2_assemble_args_tis (nios2_insn_infoS *insn_info)
 
1622
{
 
1623
  if (insn_info->insn_tokens[1] != NULL
 
1624
      && insn_info->insn_tokens[2] != NULL
 
1625
      && insn_info->insn_tokens[3] != NULL)
 
1626
    {
 
1627
      struct nios2_reg *dst = nios2_reg_lookup (insn_info->insn_tokens[1]);
 
1628
      struct nios2_reg *addr_src = nios2_reg_lookup (insn_info->insn_tokens[3]);
 
1629
      unsigned long immed
 
1630
        = nios2_assemble_expression (insn_info->insn_tokens[2], insn_info,
 
1631
                                     insn_info->insn_reloc, BFD_RELOC_NIOS2_S16,
 
1632
                                     0);
 
1633
 
 
1634
      if (addr_src == NULL)
 
1635
        as_bad (_("unknown register %s"), insn_info->insn_tokens[3]);
 
1636
      else
 
1637
        SET_INSN_FIELD (RRS, insn_info->insn_code, addr_src->index);
 
1638
 
 
1639
      if (dst == NULL)
 
1640
        as_bad (_("unknown register %s"), insn_info->insn_tokens[1]);
 
1641
      else
 
1642
        SET_INSN_FIELD (RRT, insn_info->insn_code, dst->index);
 
1643
 
 
1644
      SET_INSN_FIELD (IMM16, insn_info->insn_code, immed);
 
1645
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[4]);
 
1646
      SET_INSN_FIELD (IMM16, insn_info->insn_code, 0);
 
1647
    }
 
1648
}
 
1649
 
 
1650
static void
 
1651
nios2_assemble_args_dc (nios2_insn_infoS *insn_info)
 
1652
{
 
1653
  if (insn_info->insn_tokens[1] != NULL && insn_info->insn_tokens[2] != NULL)
 
1654
    {
 
1655
      struct nios2_reg *ctl = nios2_reg_lookup (insn_info->insn_tokens[2]);
 
1656
      struct nios2_reg *dst = nios2_reg_lookup (insn_info->insn_tokens[1]);
 
1657
 
 
1658
      if (ctl == NULL)
 
1659
        as_bad (_("unknown register %s"), insn_info->insn_tokens[1]);
 
1660
      else
 
1661
        SET_INSN_FIELD (RCTL, insn_info->insn_code, ctl->index);
 
1662
 
 
1663
      if (dst == NULL)
 
1664
        as_bad (_("unknown register %s"), insn_info->insn_tokens[2]);
 
1665
      else
 
1666
        SET_INSN_FIELD (RRD, insn_info->insn_code, dst->index);
 
1667
 
 
1668
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[3]);
 
1669
    }
 
1670
}
 
1671
 
 
1672
static void
 
1673
nios2_assemble_args_cs (nios2_insn_infoS *insn_info)
 
1674
{
 
1675
  if (insn_info->insn_tokens[1] != NULL && insn_info->insn_tokens[2] != NULL)
 
1676
    {
 
1677
      struct nios2_reg *ctl = nios2_reg_lookup (insn_info->insn_tokens[1]);
 
1678
      struct nios2_reg *src = nios2_reg_lookup (insn_info->insn_tokens[2]);
 
1679
 
 
1680
      if (ctl == NULL)
 
1681
        as_bad (_("unknown register %s"), insn_info->insn_tokens[1]);
 
1682
      else if (ctl->index == 4)
 
1683
        as_bad (_("ipending control register (ctl4) is read-only\n"));
 
1684
      else
 
1685
        SET_INSN_FIELD (RCTL, insn_info->insn_code, ctl->index);
 
1686
 
 
1687
      if (src == NULL)
 
1688
        as_bad (_("unknown register %s"), insn_info->insn_tokens[2]);
 
1689
      else
 
1690
        SET_INSN_FIELD (RRS, insn_info->insn_code, src->index);
 
1691
 
 
1692
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[3]);
 
1693
    }
 
1694
}
 
1695
 
 
1696
static void
 
1697
nios2_assemble_args_ds (nios2_insn_infoS * insn_info)
 
1698
{
 
1699
  if (insn_info->insn_tokens[1] != NULL && insn_info->insn_tokens[2] != NULL)
 
1700
    {
 
1701
      struct nios2_reg *dst = nios2_reg_lookup (insn_info->insn_tokens[1]);
 
1702
      struct nios2_reg *src = nios2_reg_lookup (insn_info->insn_tokens[2]);
 
1703
 
 
1704
      if (dst == NULL)
 
1705
        as_bad (_("unknown register %s"), insn_info->insn_tokens[1]);
 
1706
      else
 
1707
        SET_INSN_FIELD (RRD, insn_info->insn_code, dst->index);
 
1708
 
 
1709
      if (src == NULL)
 
1710
        as_bad (_("unknown register %s"), insn_info->insn_tokens[2]);
 
1711
      else
 
1712
        SET_INSN_FIELD (RRS, insn_info->insn_code, src->index);
 
1713
 
 
1714
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[3]);
 
1715
    }
 
1716
}
 
1717
 
 
1718
static void
 
1719
nios2_assemble_args_ldst (nios2_insn_infoS *insn_info)
 
1720
{
 
1721
  if (insn_info->insn_tokens[1] != NULL
 
1722
      && insn_info->insn_tokens[2] != NULL
 
1723
      && insn_info->insn_tokens[3] != NULL
 
1724
      && insn_info->insn_tokens[4] != NULL)
 
1725
    {
 
1726
      unsigned long custom_n
 
1727
        = nios2_assemble_expression (insn_info->insn_tokens[1], insn_info,
 
1728
                                     insn_info->insn_reloc,
 
1729
                                     BFD_RELOC_NIOS2_IMM8, 0);
 
1730
 
 
1731
      struct nios2_reg *dst = nios2_reg_lookup (insn_info->insn_tokens[2]);
 
1732
      struct nios2_reg *src1 = nios2_reg_lookup (insn_info->insn_tokens[3]);
 
1733
      struct nios2_reg *src2 = nios2_reg_lookup (insn_info->insn_tokens[4]);
 
1734
 
 
1735
      SET_INSN_FIELD (CUSTOM_N, insn_info->insn_code, custom_n);
 
1736
 
 
1737
      if (dst == NULL)
 
1738
        as_bad (_("unknown register %s"), insn_info->insn_tokens[2]);
 
1739
      else
 
1740
        SET_INSN_FIELD (RRD, insn_info->insn_code, dst->index);
 
1741
 
 
1742
      if (src1 == NULL)
 
1743
        as_bad (_("unknown register %s"), insn_info->insn_tokens[3]);
 
1744
      else
 
1745
        SET_INSN_FIELD (RRS, insn_info->insn_code, src1->index);
 
1746
 
 
1747
      if (src2 == NULL)
 
1748
        as_bad (_("unknown register %s"), insn_info->insn_tokens[4]);
 
1749
      else
 
1750
        SET_INSN_FIELD (RRT, insn_info->insn_code, src2->index);
 
1751
 
 
1752
      /* Set or clear the bits to indicate whether coprocessor registers are 
 
1753
         used.  */
 
1754
      if (nios2_coproc_reg (insn_info->insn_tokens[2]))
 
1755
        SET_INSN_FIELD (CUSTOM_C, insn_info->insn_code, 0);
 
1756
      else
 
1757
        SET_INSN_FIELD (CUSTOM_C, insn_info->insn_code, 1);
 
1758
 
 
1759
      if (nios2_coproc_reg (insn_info->insn_tokens[3]))
 
1760
        SET_INSN_FIELD (CUSTOM_A, insn_info->insn_code, 0);
 
1761
      else
 
1762
        SET_INSN_FIELD (CUSTOM_A, insn_info->insn_code, 1);
 
1763
 
 
1764
      if (nios2_coproc_reg (insn_info->insn_tokens[4]))
 
1765
        SET_INSN_FIELD (CUSTOM_B, insn_info->insn_code, 0);
 
1766
      else
 
1767
        SET_INSN_FIELD (CUSTOM_B, insn_info->insn_code, 1);
 
1768
 
 
1769
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[5]);
 
1770
    }
 
1771
}
 
1772
 
 
1773
static void
 
1774
nios2_assemble_args_none (nios2_insn_infoS *insn_info ATTRIBUTE_UNUSED)
 
1775
{
 
1776
  /* Nothing to do.  */
 
1777
}
 
1778
 
 
1779
static void
 
1780
nios2_assemble_args_dsj (nios2_insn_infoS *insn_info)
 
1781
{
 
1782
  if (insn_info->insn_tokens[1] != NULL
 
1783
      && insn_info->insn_tokens[2] != NULL
 
1784
      && insn_info->insn_tokens[3] != NULL)
 
1785
    {
 
1786
      struct nios2_reg *dst = nios2_reg_lookup (insn_info->insn_tokens[1]);
 
1787
      struct nios2_reg *src1 = nios2_reg_lookup (insn_info->insn_tokens[2]);
 
1788
 
 
1789
      /* A 5-bit constant expression.  */
 
1790
      unsigned int src2 =
 
1791
        nios2_assemble_expression (insn_info->insn_tokens[3], insn_info,
 
1792
                                   insn_info->insn_reloc,
 
1793
                                   BFD_RELOC_NIOS2_IMM5, 0);
 
1794
 
 
1795
      if (dst == NULL)
 
1796
        as_bad (_("unknown register %s"), insn_info->insn_tokens[1]);
 
1797
      else
 
1798
        SET_INSN_FIELD (RRD, insn_info->insn_code, dst->index);
 
1799
 
 
1800
      if (src1 == NULL)
 
1801
        as_bad (_("unknown register %s"), insn_info->insn_tokens[2]);
 
1802
      else
 
1803
        SET_INSN_FIELD (RRS, insn_info->insn_code, src1->index);
 
1804
 
 
1805
      SET_INSN_FIELD (IMM5, insn_info->insn_code, src2);
 
1806
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[4]);
 
1807
      SET_INSN_FIELD (IMM5, insn_info->insn_code, 0);
 
1808
    }
 
1809
}
 
1810
 
 
1811
static void
 
1812
nios2_assemble_args_d (nios2_insn_infoS *insn_info)
 
1813
{
 
1814
  if (insn_info->insn_tokens[1] != NULL)
 
1815
    {
 
1816
      struct nios2_reg *dst = nios2_reg_lookup (insn_info->insn_tokens[1]);
 
1817
 
 
1818
      if (dst == NULL)
 
1819
        as_bad (_("unknown register %s"), insn_info->insn_tokens[1]);
 
1820
      else
 
1821
        SET_INSN_FIELD (RRD, insn_info->insn_code, dst->index);
 
1822
 
 
1823
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[2]);
 
1824
    }
 
1825
}
 
1826
 
 
1827
static void
 
1828
nios2_assemble_args_b (nios2_insn_infoS *insn_info)
 
1829
{
 
1830
  unsigned int imm5 = 0;
 
1831
 
 
1832
  if (insn_info->insn_tokens[1] != NULL)
 
1833
    {
 
1834
      /* A 5-bit constant expression.  */
 
1835
      imm5 = nios2_assemble_expression (insn_info->insn_tokens[1],
 
1836
                                        insn_info, insn_info->insn_reloc,
 
1837
                                        BFD_RELOC_NIOS2_IMM5, 0);
 
1838
      SET_INSN_FIELD (TRAP_IMM5, insn_info->insn_code, imm5);
 
1839
      nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[2]);
 
1840
    }
 
1841
 
 
1842
  SET_INSN_FIELD (TRAP_IMM5, insn_info->insn_code, imm5);
 
1843
 
 
1844
  nios2_check_assembly (insn_info->insn_code, insn_info->insn_tokens[2]);
 
1845
}
 
1846
 
 
1847
/* This table associates pointers to functions that parse the arguments to an
 
1848
   instruction and fill in the relevant fields of the instruction.  */
 
1849
const nios2_arg_infoS nios2_arg_info_structs[] = {
 
1850
  /* args, assemble_args_func */
 
1851
  {"d,s,t", nios2_assemble_args_dst},
 
1852
  {"d,s,t,E", nios2_assemble_args_dst},
 
1853
  {"t,s,i", nios2_assemble_args_tsi},
 
1854
  {"t,s,i,E", nios2_assemble_args_tsi},
 
1855
  {"t,s,u", nios2_assemble_args_tsu},
 
1856
  {"t,s,u,E", nios2_assemble_args_tsu},
 
1857
  {"s,t,o", nios2_assemble_args_sto},
 
1858
  {"s,t,o,E", nios2_assemble_args_sto},
 
1859
  {"o", nios2_assemble_args_o},
 
1860
  {"o,E", nios2_assemble_args_o},
 
1861
  {"s", nios2_assemble_args_s},
 
1862
  {"s,E", nios2_assemble_args_s},
 
1863
  {"", nios2_assemble_args_none},
 
1864
  {"E", nios2_assemble_args_none},
 
1865
  {"i(s)", nios2_assemble_args_is},
 
1866
  {"i(s)E", nios2_assemble_args_is},
 
1867
  {"m", nios2_assemble_args_m},
 
1868
  {"m,E", nios2_assemble_args_m},
 
1869
  {"t,i(s)", nios2_assemble_args_tis},
 
1870
  {"t,i(s)E", nios2_assemble_args_tis},
 
1871
  {"d,c", nios2_assemble_args_dc},
 
1872
  {"d,c,E", nios2_assemble_args_dc},
 
1873
  {"c,s", nios2_assemble_args_cs},
 
1874
  {"c,s,E", nios2_assemble_args_cs},
 
1875
  {"d,s", nios2_assemble_args_ds},
 
1876
  {"d,s,E", nios2_assemble_args_ds},
 
1877
  {"l,d,s,t", nios2_assemble_args_ldst},
 
1878
  {"l,d,s,t,E", nios2_assemble_args_ldst},
 
1879
  {"d,s,j", nios2_assemble_args_dsj},
 
1880
  {"d,s,j,E", nios2_assemble_args_dsj},
 
1881
  {"d", nios2_assemble_args_d},
 
1882
  {"d,E", nios2_assemble_args_d},
 
1883
  {"b", nios2_assemble_args_b},
 
1884
  {"b,E", nios2_assemble_args_b}
 
1885
};
 
1886
 
 
1887
#define NIOS2_NUM_ARGS \
 
1888
        ((sizeof(nios2_arg_info_structs)/sizeof(nios2_arg_info_structs[0])))
 
1889
const int nios2_num_arg_info_structs = NIOS2_NUM_ARGS;
 
1890
 
 
1891
/* The function consume_arg takes a pointer into a string
 
1892
   of instruction tokens (args) and a pointer into a string
 
1893
   representing the expected sequence of tokens and separators.
 
1894
   It checks whether the first argument in argstr is of the
 
1895
   expected type, throwing an error if it is not, and returns
 
1896
   the pointer argstr.  */
 
1897
static char *
 
1898
nios2_consume_arg (nios2_insn_infoS *insn, char *argstr, const char *parsestr)
 
1899
{
 
1900
  char *temp;
 
1901
  int regno = -1;
 
1902
  
 
1903
  switch (*parsestr)
 
1904
    {
 
1905
    case 'c':
 
1906
      if (!nios2_control_register_arg_p (argstr))
 
1907
        as_bad (_("expecting control register"));
 
1908
      break;
 
1909
    case 'd':
 
1910
    case 's':
 
1911
    case 't':
 
1912
 
 
1913
      /* We check to make sure we don't have a control register.  */
 
1914
      if (nios2_control_register_arg_p (argstr))
 
1915
        as_bad (_("illegal use of control register"));
 
1916
 
 
1917
      /* And whether coprocessor registers are valid here.  */
 
1918
      if (nios2_coproc_reg (argstr)
 
1919
          && insn->insn_nios2_opcode->match != OP_MATCH_CUSTOM)
 
1920
        as_bad (_("illegal use of coprocessor register\n"));
 
1921
 
 
1922
      /* Extract a register number if the register is of the 
 
1923
         form r[0-9]+, if it is a normal register, set
 
1924
         regno to its number (0-31), else set regno to -1.  */
 
1925
      if (argstr[0] == 'r' && ISDIGIT (argstr[1]))
 
1926
        {
 
1927
          char *p = argstr;
 
1928
          
 
1929
          ++p;
 
1930
          regno = 0;
 
1931
          do
 
1932
            {
 
1933
              regno *= 10;
 
1934
              regno += *p - '0';
 
1935
              ++p;
 
1936
            }
 
1937
          while (ISDIGIT (*p));
 
1938
        }
 
1939
      else
 
1940
        regno = -1;
 
1941
 
 
1942
      /* And whether we are using at.  */
 
1943
      if (!nios2_as_options.noat
 
1944
          && (regno == 1 || strprefix (argstr, "at")))
 
1945
        as_warn (_("Register at (r1) can sometimes be corrupted by assembler "
 
1946
                   "optimizations.\n"
 
1947
                   "Use .set noat to turn off those optimizations (and this "
 
1948
                   "warning)."));
 
1949
        
 
1950
      /* And whether we are using oci registers.  */
 
1951
      if (!nios2_as_options.nobreak
 
1952
          && (regno == 25 || strprefix (argstr, "bt")))
 
1953
        as_warn (_("The debugger will corrupt bt (r25). If you don't need to "
 
1954
                   "debug this\n"
 
1955
                   "code then use .set nobreak to turn off this warning."));
 
1956
        
 
1957
      if (!nios2_as_options.nobreak
 
1958
          && (regno == 30 || strprefix (argstr, "ba")))
 
1959
        as_warn (_("The debugger will corrupt ba (r30). If you don't need to "
 
1960
                   "debug this\n"
 
1961
                   "code then use .set nobreak to turn off this warning."));
 
1962
      break;
 
1963
    case 'i':
 
1964
    case 'u':
 
1965
      if (*argstr == '%')
 
1966
        {
 
1967
          if (nios2_special_relocation_p (argstr))
 
1968
            {
 
1969
              /* We zap the parentheses because we don't want them confused
 
1970
                 with separators.  */
 
1971
              temp = strchr (argstr, '(');
 
1972
              if (temp != NULL)
 
1973
                *temp = ' ';
 
1974
              temp = strchr (argstr, ')');
 
1975
              if (temp != NULL)
 
1976
                *temp = ' ';
 
1977
            }
 
1978
          else
 
1979
            as_bad (_("badly formed expression near %s"), argstr);
 
1980
        }
 
1981
      break;
 
1982
    case 'm':
 
1983
    case 'j':
 
1984
    case 'l':
 
1985
    case 'b':
 
1986
      /* We can't have %hi, %lo or %hiadj here.  */
 
1987
      if (*argstr == '%')
 
1988
        as_bad (_("badly formed expression near %s"), argstr);
 
1989
      break;
 
1990
    case 'o':
 
1991
      break;
 
1992
    default:
 
1993
      BAD_CASE (*parsestr);
 
1994
      break;
 
1995
    }
 
1996
 
 
1997
  return argstr;
 
1998
}
 
1999
 
 
2000
/* The function consume_separator takes a pointer into a string
 
2001
   of instruction tokens (args) and a pointer into a string representing
 
2002
   the expected sequence of tokens and separators.  It finds the first
 
2003
   instance of the character pointed to by separator in argstr, and
 
2004
   returns a pointer to the next element of argstr, which is the
 
2005
   following token in the sequence.  */
 
2006
static char *
 
2007
nios2_consume_separator (char *argstr, const char *separator)
 
2008
{
 
2009
  char *p;
 
2010
 
 
2011
  /* If we have a opcode reg, expr(reg) type instruction, and
 
2012
   * we are separating the expr from the (reg), we find the last
 
2013
   * (, just in case the expression has parentheses.  */
 
2014
 
 
2015
  if (*separator == '(')
 
2016
    p = strrchr (argstr, *separator);
 
2017
  else
 
2018
    p = strchr (argstr, *separator);
 
2019
 
 
2020
  if (p != NULL)
 
2021
    *p++ = 0;
 
2022
  else
 
2023
    as_bad (_("expecting %c near %s"), *separator, argstr);
 
2024
  return p;
 
2025
}
 
2026
 
 
2027
 
 
2028
/* The principal argument parsing function which takes a string argstr
 
2029
   representing the instruction arguments for insn, and extracts the argument
 
2030
   tokens matching parsestr into parsed_args.  */
 
2031
static void
 
2032
nios2_parse_args (nios2_insn_infoS *insn, char *argstr,
 
2033
                  const char *parsestr, char **parsed_args)
 
2034
{
 
2035
  char *p;
 
2036
  char *end = NULL;
 
2037
  int i;
 
2038
  p = argstr;
 
2039
  i = 0;
 
2040
  bfd_boolean terminate = FALSE;
 
2041
  
 
2042
  /* This rest of this function is it too fragile and it mostly works,
 
2043
     therefore special case this one.  */
 
2044
  if (*parsestr == 0 && argstr != 0)
 
2045
    {
 
2046
      as_bad (_("too many arguments"));
 
2047
      parsed_args[0] = NULL;
 
2048
      return;
 
2049
    }
 
2050
  
 
2051
  while (p != NULL && !terminate && i < NIOS2_MAX_INSN_TOKENS)
 
2052
    {
 
2053
      parsed_args[i] = nios2_consume_arg (insn, p, parsestr);
 
2054
      ++parsestr;
 
2055
      if (*parsestr != '\0')
 
2056
        {
 
2057
          p = nios2_consume_separator (p, parsestr);
 
2058
          ++parsestr;
 
2059
        }
 
2060
      else
 
2061
        {
 
2062
          /* Check that the argument string has no trailing arguments.  */
 
2063
          /* If we've got a %lo etc relocation, we've zapped the parens with 
 
2064
             spaces.  */
 
2065
          if (nios2_special_relocation_p (p))
 
2066
            end = strpbrk (p, ",");
 
2067
          else
 
2068
            end = strpbrk (p, " ,");
 
2069
 
 
2070
          if (end != NULL)
 
2071
            as_bad (_("too many arguments"));
 
2072
        }
 
2073
 
 
2074
      if (*parsestr == '\0' || (p != NULL && *p == '\0'))
 
2075
        terminate = TRUE;
 
2076
      ++i;
 
2077
    }
 
2078
 
 
2079
  parsed_args[i] = NULL;
 
2080
 
 
2081
  /* The argument to break and trap instructions is optional; complain
 
2082
     for other cases of missing arguments.  */
 
2083
  if (*parsestr != '\0'
 
2084
      && insn->insn_nios2_opcode->match != OP_MATCH_BREAK
 
2085
      && insn->insn_nios2_opcode->match != OP_MATCH_TRAP)
 
2086
    as_bad (_("missing argument"));
 
2087
}
 
2088
 
 
2089
 
 
2090
 
 
2091
/** Support for pseudo-op parsing.  These are macro-like opcodes that
 
2092
    expand into real insns by suitable fiddling with the operands.  */
 
2093
 
 
2094
/* Append the string modifier to the string contained in the argument at
 
2095
   parsed_args[ndx].  */
 
2096
static void
 
2097
nios2_modify_arg (char **parsed_args, const char *modifier,
 
2098
                  int unused ATTRIBUTE_UNUSED, int ndx)
 
2099
{
 
2100
  char *tmp = parsed_args[ndx];
 
2101
 
 
2102
  parsed_args[ndx]
 
2103
    = (char *) malloc (strlen (parsed_args[ndx]) + strlen (modifier) + 1);
 
2104
  strcpy (parsed_args[ndx], tmp);
 
2105
  strcat (parsed_args[ndx], modifier);
 
2106
}
 
2107
 
 
2108
/* Modify parsed_args[ndx] by negating that argument.  */
 
2109
static void
 
2110
nios2_negate_arg (char **parsed_args, const char *modifier ATTRIBUTE_UNUSED,
 
2111
                  int unused ATTRIBUTE_UNUSED, int ndx)
 
2112
{
 
2113
  char *tmp = parsed_args[ndx];
 
2114
 
 
2115
  parsed_args[ndx]
 
2116
    = (char *) malloc (strlen ("~(") + strlen (parsed_args[ndx]) +
 
2117
                       strlen (")+1") + 1);
 
2118
 
 
2119
  strcpy (parsed_args[ndx], "~(");
 
2120
  strcat (parsed_args[ndx], tmp);
 
2121
  strcat (parsed_args[ndx], ")+1");
 
2122
}
 
2123
 
 
2124
/* The function nios2_swap_args swaps the pointers at indices index_1 and
 
2125
   index_2 in the array parsed_args[] - this is used for operand swapping
 
2126
   for comparison operations.  */
 
2127
static void
 
2128
nios2_swap_args (char **parsed_args, const char *unused ATTRIBUTE_UNUSED,
 
2129
                 int index_1, int index_2)
 
2130
{
 
2131
  char *tmp;
 
2132
  gas_assert (index_1 < NIOS2_MAX_INSN_TOKENS
 
2133
              && index_2 < NIOS2_MAX_INSN_TOKENS);
 
2134
  tmp = parsed_args[index_1];
 
2135
  parsed_args[index_1] = parsed_args[index_2];
 
2136
  parsed_args[index_2] = tmp;
 
2137
}
 
2138
 
 
2139
/* This function appends the string appnd to the array of strings in
 
2140
   parsed_args num times starting at index start in the array.  */
 
2141
static void
 
2142
nios2_append_arg (char **parsed_args, const char *appnd, int num,
 
2143
                  int start)
 
2144
{
 
2145
  int i, count;
 
2146
  char *tmp;
 
2147
 
 
2148
  gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS);
 
2149
 
 
2150
  if (nios2_mode == NIOS2_MODE_TEST)
 
2151
    tmp = parsed_args[start];
 
2152
  else
 
2153
    tmp = NULL;
 
2154
 
 
2155
  for (i = start, count = num; count > 0; ++i, --count)
 
2156
    parsed_args[i] = (char *) appnd;
 
2157
 
 
2158
  gas_assert (i == (start + num));
 
2159
  parsed_args[i] = tmp;
 
2160
  parsed_args[i + 1] = NULL;
 
2161
}
 
2162
 
 
2163
/* This function inserts the string insert num times in the array 
 
2164
   parsed_args, starting at the index start.  */
 
2165
static void
 
2166
nios2_insert_arg (char **parsed_args, const char *insert, int num,
 
2167
                  int start)
 
2168
{
 
2169
  int i, count;
 
2170
 
 
2171
  gas_assert ((start + num) < NIOS2_MAX_INSN_TOKENS);
 
2172
 
 
2173
  /* Move the existing arguments up to create space.  */
 
2174
  for (i = NIOS2_MAX_INSN_TOKENS; i - num >= start; --i)
 
2175
    parsed_args[i] = parsed_args[i - num];
 
2176
 
 
2177
  for (i = start, count = num; count > 0; ++i, --count)
 
2178
    parsed_args[i] = (char *) insert;
 
2179
}
 
2180
 
 
2181
/* Cleanup function to free malloc'ed arg strings.  */
 
2182
static void
 
2183
nios2_free_arg (char **parsed_args, int num ATTRIBUTE_UNUSED, int start)
 
2184
{
 
2185
  if (parsed_args[start])
 
2186
    {
 
2187
      free (parsed_args[start]);
 
2188
      parsed_args[start] = NULL;
 
2189
    }
 
2190
}
 
2191
 
 
2192
/* This function swaps the pseudo-op for a real op.  */
 
2193
static nios2_ps_insn_infoS*
 
2194
nios2_translate_pseudo_insn (nios2_insn_infoS *insn)
 
2195
{
 
2196
 
 
2197
  nios2_ps_insn_infoS *ps_insn;
 
2198
 
 
2199
  /* Find which real insn the pseudo-op transates to and
 
2200
     switch the insn_info ptr to point to it.  */
 
2201
  ps_insn = nios2_ps_lookup (insn->insn_nios2_opcode->name);
 
2202
 
 
2203
  if (ps_insn != NULL)
 
2204
    {
 
2205
      insn->insn_nios2_opcode = nios2_opcode_lookup (ps_insn->insn);
 
2206
      insn->insn_tokens[0] = insn->insn_nios2_opcode->name;
 
2207
      /* Modify the args so they work with the real insn.  */
 
2208
      ps_insn->arg_modifer_func ((char **) insn->insn_tokens,
 
2209
                                 ps_insn->arg_modifier, ps_insn->num,
 
2210
                                 ps_insn->index);
 
2211
    }
 
2212
  else
 
2213
    /* we cannot recover from this.  */
 
2214
    as_fatal (_("unrecognized pseudo-instruction %s"),
 
2215
              ps_insn->pseudo_insn);
 
2216
  return ps_insn;
 
2217
}
 
2218
 
 
2219
/* Invoke the cleanup handler for pseudo-insn ps_insn on insn.  */
 
2220
static void
 
2221
nios2_cleanup_pseudo_insn (nios2_insn_infoS *insn,
 
2222
                           nios2_ps_insn_infoS *ps_insn)
 
2223
{
 
2224
  if (ps_insn->arg_cleanup_func)
 
2225
    (ps_insn->arg_cleanup_func) ((char **) insn->insn_tokens,
 
2226
                                 ps_insn->num, ps_insn->index);
 
2227
}
 
2228
 
 
2229
const nios2_ps_insn_infoS nios2_ps_insn_info_structs[] = {
 
2230
  /* pseudo-op, real-op, arg, arg_modifier_func, num, index, arg_cleanup_func */
 
2231
  {"mov", "add", nios2_append_arg, "zero", 1, 3, NULL},
 
2232
  {"movi", "addi", nios2_insert_arg, "zero", 1, 2, NULL},
 
2233
  {"movhi", "orhi", nios2_insert_arg, "zero", 1, 2, NULL},
 
2234
  {"movui", "ori", nios2_insert_arg, "zero", 1, 2, NULL},
 
2235
  {"movia", "orhi", nios2_insert_arg, "zero", 1, 2, NULL},
 
2236
  {"nop", "add", nios2_append_arg, "zero", 3, 1, NULL},
 
2237
  {"bgt", "blt", nios2_swap_args, "", 1, 2, NULL},
 
2238
  {"bgtu", "bltu", nios2_swap_args, "", 1, 2, NULL},
 
2239
  {"ble", "bge", nios2_swap_args, "", 1, 2, NULL},
 
2240
  {"bleu", "bgeu", nios2_swap_args, "", 1, 2, NULL},
 
2241
  {"cmpgt", "cmplt", nios2_swap_args, "", 2, 3, NULL},
 
2242
  {"cmpgtu", "cmpltu", nios2_swap_args, "", 2, 3, NULL},
 
2243
  {"cmple", "cmpge", nios2_swap_args, "", 2, 3, NULL},
 
2244
  {"cmpleu", "cmpgeu", nios2_swap_args, "", 2, 3, NULL},
 
2245
  {"cmpgti", "cmpgei", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
 
2246
  {"cmpgtui", "cmpgeui", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
 
2247
  {"cmplei", "cmplti", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
 
2248
  {"cmpleui", "cmpltui", nios2_modify_arg, "+1", 0, 3, nios2_free_arg},
 
2249
  {"subi", "addi", nios2_negate_arg, "", 0, 3, nios2_free_arg}
 
2250
  /* Add further pseudo-ops here.  */
 
2251
};
 
2252
 
 
2253
#define NIOS2_NUM_PSEUDO_INSNS \
 
2254
        ((sizeof(nios2_ps_insn_info_structs)/ \
 
2255
          sizeof(nios2_ps_insn_info_structs[0])))
 
2256
const int nios2_num_ps_insn_info_structs = NIOS2_NUM_PSEUDO_INSNS;
 
2257
 
 
2258
 
 
2259
/** Assembler output support.  */
 
2260
 
 
2261
static int
 
2262
can_evaluate_expr (nios2_insn_infoS *insn)
 
2263
{
 
2264
  /* Remove this check for null and the invalid insn "ori r9, 1234" seg faults. */
 
2265
  if (!insn->insn_reloc) 
 
2266
    /* ??? Ideally we should do something other than as_fatal here as we can
 
2267
       continue to assemble.
 
2268
       However this function (actually the output_* functions) should not 
 
2269
       have been called in the first place once an illegal instruction had 
 
2270
       been encountered.  */
 
2271
    as_fatal (_("Invalid instruction encountered, cannot recover. No assembly attempted."));
 
2272
 
 
2273
  if (insn->insn_reloc->reloc_expression.X_op == O_constant)
 
2274
    return 1;
 
2275
 
 
2276
  return 0;
 
2277
}
 
2278
 
 
2279
static int
 
2280
get_expr_value (nios2_insn_infoS *insn)
 
2281
{
 
2282
  int value = 0;
 
2283
 
 
2284
  if (insn->insn_reloc->reloc_expression.X_op == O_constant)
 
2285
    value = insn->insn_reloc->reloc_expression.X_add_number;
 
2286
  return value;
 
2287
}
 
2288
 
 
2289
/* Output a normal instruction.  */
 
2290
static void
 
2291
output_insn (nios2_insn_infoS *insn)
 
2292
{
 
2293
  char *f;
 
2294
  nios2_insn_relocS *reloc;
 
2295
 
 
2296
  f = frag_more (4);
 
2297
  /* This allocates enough space for the instruction
 
2298
     and puts it in the current frag.  */
 
2299
  md_number_to_chars (f, insn->insn_code, 4);
 
2300
  /* Emit debug info.  */
 
2301
  dwarf2_emit_insn (4);
 
2302
  /* Create any fixups to be acted on later.  */
 
2303
  for (reloc = insn->insn_reloc; reloc != NULL; reloc = reloc->reloc_next)
 
2304
    fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
 
2305
                 &reloc->reloc_expression, reloc->reloc_pcrel,
 
2306
                 reloc->reloc_type);
 
2307
}
 
2308
 
 
2309
/* Output an unconditional branch.  */
 
2310
static void
 
2311
output_ubranch (nios2_insn_infoS *insn)
 
2312
{
 
2313
  nios2_insn_relocS *reloc = insn->insn_reloc;
 
2314
 
 
2315
  /* If the reloc is NULL, there was an error assembling the branch.  */
 
2316
  if (reloc != NULL)
 
2317
    {
 
2318
      symbolS *symp = reloc->reloc_expression.X_add_symbol;
 
2319
      offsetT offset = reloc->reloc_expression.X_add_number;
 
2320
      char *f;
 
2321
 
 
2322
      /* Tag dwarf2 debug info to the address at the start of the insn.
 
2323
         We must do it before frag_var() below closes off the frag.  */
 
2324
      dwarf2_emit_insn (0);
 
2325
 
 
2326
      /* We create a machine dependent frag which can grow
 
2327
         to accommodate the largest possible instruction sequence
 
2328
         this may generate.  */
 
2329
      f = frag_var (rs_machine_dependent,
 
2330
                    UBRANCH_MAX_SIZE, 4, UBRANCH_SUBTYPE (0),
 
2331
                    symp, offset, NULL);
 
2332
 
 
2333
      md_number_to_chars (f, insn->insn_code, 4);
 
2334
 
 
2335
      /* We leave fixup generation to md_convert_frag.  */
 
2336
    }
 
2337
}
 
2338
 
 
2339
/* Output a conditional branch.  */
 
2340
static void
 
2341
output_cbranch (nios2_insn_infoS *insn)
 
2342
{
 
2343
  nios2_insn_relocS *reloc = insn->insn_reloc;
 
2344
 
 
2345
  /* If the reloc is NULL, there was an error assembling the branch.  */
 
2346
  if (reloc != NULL)
 
2347
    {
 
2348
      symbolS *symp = reloc->reloc_expression.X_add_symbol;
 
2349
      offsetT offset = reloc->reloc_expression.X_add_number;
 
2350
      char *f;
 
2351
 
 
2352
      /* Tag dwarf2 debug info to the address at the start of the insn.
 
2353
         We must do it before frag_var() below closes off the frag.  */
 
2354
      dwarf2_emit_insn (0);
 
2355
 
 
2356
      /* We create a machine dependent frag which can grow
 
2357
         to accommodate the largest possible instruction sequence
 
2358
         this may generate.  */
 
2359
      f = frag_var (rs_machine_dependent,
 
2360
                    CBRANCH_MAX_SIZE, 4, CBRANCH_SUBTYPE (0),
 
2361
                    symp, offset, NULL);
 
2362
 
 
2363
      md_number_to_chars (f, insn->insn_code, 4);
 
2364
 
 
2365
      /* We leave fixup generation to md_convert_frag.  */
 
2366
    }
 
2367
}
 
2368
 
 
2369
/* Output a call sequence.  Since calls are not pc-relative for NIOS2,
 
2370
   but are page-relative, we cannot tell at any stage in assembly
 
2371
   whether a call will be out of range since a section may be linked
 
2372
   at any address.  So if we are relaxing, we convert all call instructions
 
2373
   to long call sequences, and rely on the linker to relax them back to
 
2374
   short calls.  */
 
2375
static void
 
2376
output_call (nios2_insn_infoS *insn)
 
2377
{
 
2378
  /* This allocates enough space for the instruction
 
2379
     and puts it in the current frag.  */
 
2380
  char *f = frag_more (12);
 
2381
  nios2_insn_relocS *reloc = insn->insn_reloc;
 
2382
 
 
2383
  md_number_to_chars (f, OP_MATCH_ORHI | 0x00400000, 4);
 
2384
  dwarf2_emit_insn (4);
 
2385
  fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
 
2386
               &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_HI16);
 
2387
  md_number_to_chars (f + 4, OP_MATCH_ORI | 0x08400000, 4);
 
2388
  dwarf2_emit_insn (4);
 
2389
  fix_new_exp (frag_now, f - frag_now->fr_literal + 4, 4,
 
2390
               &reloc->reloc_expression, 0, BFD_RELOC_NIOS2_LO16);
 
2391
  md_number_to_chars (f + 8, OP_MATCH_CALLR | 0x08000000, 4);
 
2392
  dwarf2_emit_insn (4);
 
2393
}
 
2394
 
 
2395
/* Output an addi - will silently convert to
 
2396
   orhi if rA = r0 and (expr & 0xffff0000) == 0.  */
 
2397
static void
 
2398
output_addi (nios2_insn_infoS *insn)
 
2399
{
 
2400
  if (can_evaluate_expr (insn))
 
2401
    {
 
2402
      int expr_val = get_expr_value (insn);
 
2403
      if (GET_INSN_FIELD (RRS, insn->insn_code) == 0
 
2404
          && (expr_val & 0xffff) == 0
 
2405
          && expr_val != 0)
 
2406
        {
 
2407
          /* We really want a movhi (orhi) here.  */
 
2408
          insn->insn_code = (insn->insn_code & ~OP_MATCH_ADDI) | OP_MATCH_ORHI;
 
2409
          insn->insn_reloc->reloc_expression.X_add_number =
 
2410
            (insn->insn_reloc->reloc_expression.X_add_number >> 16) & 0xffff;
 
2411
          insn->insn_reloc->reloc_type = BFD_RELOC_NIOS2_U16;
 
2412
        }
 
2413
    }
 
2414
 
 
2415
  /* Output an instruction.  */
 
2416
  output_insn (insn);
 
2417
}
 
2418
 
 
2419
static void
 
2420
output_andi (nios2_insn_infoS *insn)
 
2421
{
 
2422
  if (can_evaluate_expr (insn))
 
2423
    {
 
2424
      int expr_val = get_expr_value (insn);
 
2425
      if (expr_val != 0 && (expr_val & 0xffff) == 0)
 
2426
        {
 
2427
          /* We really want a movhi (orhi) here.  */
 
2428
          insn->insn_code = (insn->insn_code & ~OP_MATCH_ANDI) | OP_MATCH_ANDHI;
 
2429
          insn->insn_reloc->reloc_expression.X_add_number =
 
2430
            (insn->insn_reloc->reloc_expression.X_add_number >> 16) & 0xffff;
 
2431
          insn->insn_reloc->reloc_type = BFD_RELOC_NIOS2_U16;
 
2432
        }
 
2433
    }
 
2434
 
 
2435
  /* Output an instruction.  */
 
2436
  output_insn (insn);
 
2437
}
 
2438
 
 
2439
static void
 
2440
output_ori (nios2_insn_infoS *insn)
 
2441
{
 
2442
  if (can_evaluate_expr (insn))
 
2443
    {
 
2444
      int expr_val = get_expr_value (insn);
 
2445
      if (expr_val != 0 && (expr_val & 0xffff) == 0)
 
2446
        {
 
2447
          /* We really want a movhi (orhi) here.  */
 
2448
          insn->insn_code = (insn->insn_code & ~OP_MATCH_ORI) | OP_MATCH_ORHI;
 
2449
          insn->insn_reloc->reloc_expression.X_add_number =
 
2450
            (insn->insn_reloc->reloc_expression.X_add_number >> 16) & 0xffff;
 
2451
          insn->insn_reloc->reloc_type = BFD_RELOC_NIOS2_U16;
 
2452
        }
 
2453
    }
 
2454
 
 
2455
  /* Output an instruction.  */
 
2456
  output_insn (insn);
 
2457
}
 
2458
 
 
2459
static void
 
2460
output_xori (nios2_insn_infoS *insn)
 
2461
{
 
2462
  if (can_evaluate_expr (insn))
 
2463
    {
 
2464
      int expr_val = get_expr_value (insn);
 
2465
      if (expr_val != 0 && (expr_val & 0xffff) == 0)
 
2466
        {
 
2467
          /* We really want a movhi (orhi) here.  */
 
2468
          insn->insn_code = (insn->insn_code & ~OP_MATCH_XORI) | OP_MATCH_XORHI;
 
2469
          insn->insn_reloc->reloc_expression.X_add_number =
 
2470
            (insn->insn_reloc->reloc_expression.X_add_number >> 16) & 0xffff;
 
2471
          insn->insn_reloc->reloc_type = BFD_RELOC_NIOS2_U16;
 
2472
        }
 
2473
    }
 
2474
 
 
2475
  /* Output an instruction.  */
 
2476
  output_insn (insn);
 
2477
}
 
2478
 
 
2479
 
 
2480
/* Output a movhi/addi pair for the movia pseudo-op.  */
 
2481
static void
 
2482
output_movia (nios2_insn_infoS *insn)
 
2483
{
 
2484
  /* This allocates enough space for the instruction
 
2485
     and puts it in the current frag.  */
 
2486
  char *f = frag_more (8);
 
2487
  nios2_insn_relocS *reloc = insn->insn_reloc;
 
2488
  unsigned long reg_index = GET_INSN_FIELD (IRT, insn->insn_code);
 
2489
 
 
2490
  /* If the reloc is NULL, there was an error assembling the movia.  */
 
2491
  if (reloc != NULL)
 
2492
    {
 
2493
      md_number_to_chars (f, insn->insn_code, 4);
 
2494
      dwarf2_emit_insn (4);
 
2495
      md_number_to_chars (f + 4,
 
2496
                          (OP_MATCH_ADDI | (reg_index << OP_SH_IRT)
 
2497
                           | (reg_index << OP_SH_IRS)),
 
2498
                          4);
 
2499
      dwarf2_emit_insn (4);
 
2500
      fix_new (frag_now, f - frag_now->fr_literal, 4,
 
2501
               reloc->reloc_expression.X_add_symbol,
 
2502
               reloc->reloc_expression.X_add_number, 0,
 
2503
               BFD_RELOC_NIOS2_HIADJ16);
 
2504
      fix_new (frag_now, f + 4 - frag_now->fr_literal, 4,
 
2505
               reloc->reloc_expression.X_add_symbol,
 
2506
               reloc->reloc_expression.X_add_number, 0, BFD_RELOC_NIOS2_LO16);
 
2507
    }
 
2508
}
 
2509
 
 
2510
 
 
2511
 
 
2512
/** External interfaces.  */
 
2513
 
 
2514
/* The following functions are called by machine-independent parts of
 
2515
   the assembler. */
 
2516
int
 
2517
md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
 
2518
{
 
2519
  switch (c)
 
2520
    {
 
2521
    case 'r':
 
2522
      /* Hidden option for self-test mode.  */
 
2523
      nios2_mode = NIOS2_MODE_TEST;
 
2524
      break;
 
2525
    case OPTION_RELAX_ALL:
 
2526
      nios2_as_options.relax = relax_all;
 
2527
      break;
 
2528
    case OPTION_NORELAX:
 
2529
      nios2_as_options.relax = relax_none;
 
2530
      break;
 
2531
    case OPTION_RELAX_SECTION:
 
2532
      nios2_as_options.relax = relax_section;
 
2533
      break;
 
2534
    case OPTION_EB:
 
2535
      target_big_endian = 1;
 
2536
      break;
 
2537
    case OPTION_EL:
 
2538
      target_big_endian = 0;
 
2539
      break;
 
2540
    default:
 
2541
      return 0;
 
2542
      break;
 
2543
    }
 
2544
 
 
2545
  return 1;
 
2546
}
 
2547
 
 
2548
/* Implement TARGET_FORMAT.  We can choose to be big-endian or
 
2549
   little-endian at runtime based on a switch.  */
 
2550
const char *
 
2551
nios2_target_format (void)
 
2552
{
 
2553
  return target_big_endian ? "elf32-bignios2" : "elf32-littlenios2";
 
2554
}
 
2555
 
 
2556
/* Machine-dependent usage message. */
 
2557
void
 
2558
md_show_usage (FILE *stream)
 
2559
{
 
2560
  fprintf (stream, "        NIOS2 options:\n"
 
2561
           "  -relax-all            replace all branch and call "
 
2562
           "instructions with jmp and callr sequences\n"
 
2563
           "  -relax-section        replace identified out of range "
 
2564
           "branches with jmp sequences (default)\n"
 
2565
           "  -no-relax             do not replace any branches or calls\n"
 
2566
           "  -EB                   force big-endian byte ordering\n"
 
2567
           "  -EL                   force little-endian byte ordering\n");
 
2568
}
 
2569
 
 
2570
/* This function is called once, at assembler startup time.
 
2571
   It should set up all the tables, etc. that the MD part of the
 
2572
   assembler will need. */
 
2573
void
 
2574
md_begin (void)
 
2575
{
 
2576
  int i;
 
2577
  const char *inserted;
 
2578
 
 
2579
  /* Create and fill a hashtable for the Nios II opcodes, registers and 
 
2580
     arguments.  */
 
2581
  nios2_opcode_hash = hash_new ();
 
2582
  nios2_reg_hash = hash_new ();
 
2583
  nios2_arg_hash = hash_new ();
 
2584
  nios2_ps_hash = hash_new ();
 
2585
 
 
2586
  for (i = 0; i < NUMOPCODES; ++i)
 
2587
    {
 
2588
      inserted
 
2589
        = hash_insert (nios2_opcode_hash, nios2_opcodes[i].name,
 
2590
                       (PTR) & nios2_opcodes[i]);
 
2591
      if (inserted != NULL)
 
2592
        {
 
2593
          fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
 
2594
                   nios2_opcodes[i].name, inserted);
 
2595
          /* Probably a memory allocation problem?  Give up now.  */
 
2596
          as_fatal (_("Broken assembler.  No assembly attempted."));
 
2597
        }
 
2598
    }
 
2599
 
 
2600
  for (i = 0; i < nios2_num_regs; ++i)
 
2601
    {
 
2602
      inserted
 
2603
        = hash_insert (nios2_reg_hash, nios2_regs[i].name,
 
2604
                       (PTR) & nios2_regs[i]);
 
2605
      if (inserted != NULL)
 
2606
        {
 
2607
          fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
 
2608
                   nios2_regs[i].name, inserted);
 
2609
          /* Probably a memory allocation problem?  Give up now.  */
 
2610
          as_fatal (_("Broken assembler.  No assembly attempted."));
 
2611
        }
 
2612
 
 
2613
    }
 
2614
 
 
2615
  for (i = 0; i < nios2_num_arg_info_structs; ++i)
 
2616
    {
 
2617
      inserted
 
2618
        = hash_insert (nios2_arg_hash, nios2_arg_info_structs[i].args,
 
2619
                       (PTR) & nios2_arg_info_structs[i]);
 
2620
      if (inserted != NULL)
 
2621
        {
 
2622
          fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
 
2623
                   nios2_arg_info_structs[i].args, inserted);
 
2624
          /* Probably a memory allocation problem?  Give up now.  */
 
2625
          as_fatal (_("Broken assembler.  No assembly attempted."));
 
2626
        }
 
2627
    }
 
2628
 
 
2629
  for (i = 0; i < nios2_num_ps_insn_info_structs; ++i)
 
2630
    {
 
2631
      inserted
 
2632
        = hash_insert (nios2_ps_hash, nios2_ps_insn_info_structs[i].pseudo_insn,
 
2633
                       (PTR) & nios2_ps_insn_info_structs[i]);
 
2634
      if (inserted != NULL)
 
2635
        {
 
2636
          fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
 
2637
                   nios2_ps_insn_info_structs[i].pseudo_insn, inserted);
 
2638
          /* Probably a memory allocation problem?  Give up now.  */
 
2639
          as_fatal (_("Broken assembler.  No assembly attempted."));
 
2640
        }
 
2641
    }
 
2642
 
 
2643
  /* Assembler option defaults.  */
 
2644
  nios2_as_options.noat = FALSE;
 
2645
  nios2_as_options.nobreak = FALSE;
 
2646
 
 
2647
  /* Debug information is incompatible with relaxation.  */
 
2648
  if (debug_type != DEBUG_UNSPECIFIED)
 
2649
    nios2_as_options.relax = relax_none;
 
2650
 
 
2651
  /* Initialize the alignment data.  */
 
2652
  nios2_current_align_seg = now_seg;
 
2653
  nios2_last_label = NULL;
 
2654
  nios2_current_align = 0;
 
2655
}
 
2656
 
 
2657
 
 
2658
/* Assembles a single line of Nios II assembly language.  */
 
2659
void
 
2660
md_assemble (char *op_str)
 
2661
{
 
2662
  char *argstr; 
 
2663
  char *op_strdup = NULL;
 
2664
  nios2_arg_infoS *arg_info;
 
2665
  unsigned long saved_pinfo = 0;
 
2666
  nios2_insn_infoS thisinsn;
 
2667
  nios2_insn_infoS *insn = &thisinsn;
 
2668
 
 
2669
  /* Make sure we are aligned on a 4-byte boundary.  */
 
2670
  if (nios2_current_align < 2)
 
2671
    nios2_align (2, NULL, nios2_last_label);
 
2672
  else if (nios2_current_align > 2)
 
2673
    nios2_current_align = 2;
 
2674
  nios2_last_label = NULL;
 
2675
 
 
2676
  /* We don't want to clobber to op_str
 
2677
     because we want to be able to use it in messages.  */
 
2678
  op_strdup = strdup (op_str);
 
2679
  insn->insn_tokens[0] = strtok (op_strdup, " ");
 
2680
  argstr = strtok (NULL, "");
 
2681
 
 
2682
  /* Assemble the opcode.  */
 
2683
  insn->insn_nios2_opcode = nios2_opcode_lookup (insn->insn_tokens[0]);
 
2684
  insn->insn_reloc = NULL;
 
2685
 
 
2686
  if (insn->insn_nios2_opcode != NULL)
 
2687
    {
 
2688
      nios2_ps_insn_infoS *ps_insn = NULL;
 
2689
      /* Set the opcode for the instruction.  */
 
2690
      insn->insn_code = insn->insn_nios2_opcode->match;
 
2691
 
 
2692
      /* Parse the arguments pointed to by argstr.  */
 
2693
      if (nios2_mode == NIOS2_MODE_ASSEMBLE)
 
2694
        nios2_parse_args (insn, argstr, insn->insn_nios2_opcode->args,
 
2695
                          (char **) &insn->insn_tokens[1]);
 
2696
      else
 
2697
        nios2_parse_args (insn, argstr, insn->insn_nios2_opcode->args_test,
 
2698
                          (char **) &insn->insn_tokens[1]);
 
2699
 
 
2700
      /* We need to preserve the MOVIA macro as this is clobbered by 
 
2701
         translate_pseudo_insn.  */
 
2702
      if (insn->insn_nios2_opcode->pinfo == NIOS2_INSN_MACRO_MOVIA)
 
2703
        saved_pinfo = NIOS2_INSN_MACRO_MOVIA;
 
2704
      /* If the instruction is an pseudo-instruction, we want to replace it 
 
2705
         with its real equivalent, and then continue.  */
 
2706
      if ((insn->insn_nios2_opcode->pinfo & NIOS2_INSN_MACRO)
 
2707
          == NIOS2_INSN_MACRO)
 
2708
        ps_insn = nios2_translate_pseudo_insn (insn);
 
2709
 
 
2710
      /* Find the assemble function, and call it.  */
 
2711
      arg_info = nios2_arg_lookup (insn->insn_nios2_opcode->args);
 
2712
      if (arg_info != NULL)
 
2713
        {
 
2714
          arg_info->assemble_args_func (insn);
 
2715
 
 
2716
          if (nios2_as_options.relax != relax_none
 
2717
              && !nios2_as_options.noat
 
2718
              && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_UBRANCH)
 
2719
            output_ubranch (insn);
 
2720
          else if (nios2_as_options.relax != relax_none
 
2721
                   && !nios2_as_options.noat
 
2722
                   && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_CBRANCH)
 
2723
            output_cbranch (insn);
 
2724
          else if (nios2_as_options.relax == relax_all
 
2725
                   && !nios2_as_options.noat
 
2726
                   && insn->insn_nios2_opcode->pinfo & NIOS2_INSN_CALL
 
2727
                   && insn->insn_reloc
 
2728
                   && insn->insn_reloc->reloc_type == BFD_RELOC_NIOS2_CALL26)
 
2729
            output_call (insn);
 
2730
          else if (insn->insn_nios2_opcode->pinfo & NIOS2_INSN_ANDI)
 
2731
            output_andi (insn);
 
2732
          else if (insn->insn_nios2_opcode->pinfo & NIOS2_INSN_ORI)
 
2733
            output_ori (insn);
 
2734
          else if (insn->insn_nios2_opcode->pinfo & NIOS2_INSN_XORI)
 
2735
            output_xori (insn);
 
2736
          else if (insn->insn_nios2_opcode->pinfo & NIOS2_INSN_ADDI)
 
2737
            output_addi (insn);
 
2738
          else if (saved_pinfo == NIOS2_INSN_MACRO_MOVIA)
 
2739
            output_movia (insn);
 
2740
          else
 
2741
            output_insn (insn);
 
2742
          if (ps_insn)
 
2743
            nios2_cleanup_pseudo_insn (insn, ps_insn);
 
2744
        }
 
2745
      else
 
2746
        {
 
2747
          /* The assembler is broken.  */
 
2748
          fprintf (stderr,
 
2749
                   _("internal error: %s is not a valid argument syntax\n"),
 
2750
                   insn->insn_nios2_opcode->args);
 
2751
          /* Probably a memory allocation problem.  Give up now.  */
 
2752
          as_fatal (_("Broken assembler.  No assembly attempted."));
 
2753
        }
 
2754
    }
 
2755
  else
 
2756
    /* Unrecognised instruction - error.  */
 
2757
    as_bad (_("unrecognised instruction %s"), insn->insn_tokens[0]);
 
2758
 
 
2759
  /* Don't leak memory.  */
 
2760
  free (op_strdup);
 
2761
}
 
2762
 
 
2763
/* Round up section size.  */
 
2764
valueT
 
2765
md_section_align (asection *seg ATTRIBUTE_UNUSED, valueT size)
 
2766
{
 
2767
  /* I think byte alignment is fine here.  */
 
2768
  return size;
 
2769
}
 
2770
 
 
2771
/* Implement TC_FORCE_RELOCATION.  */
 
2772
int
 
2773
nios2_force_relocation (fixS *fixp)
 
2774
{
 
2775
  if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
 
2776
      || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
 
2777
      || fixp->fx_r_type == BFD_RELOC_NIOS2_ALIGN)
 
2778
    return 1;
 
2779
 
 
2780
  return generic_force_reloc (fixp);
 
2781
}
 
2782
 
 
2783
/* Implement tc_fix_adjustable.  */
 
2784
int
 
2785
nios2_fix_adjustable (fixS *fixp)
 
2786
{
 
2787
  if (fixp->fx_addsy == NULL)
 
2788
    return 1;
 
2789
 
 
2790
#ifdef OBJ_ELF
 
2791
  /* Prevent all adjustments to global symbols.  */
 
2792
  if (OUTPUT_FLAVOR == bfd_target_elf_flavour
 
2793
      && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
 
2794
    return 0;
 
2795
#endif
 
2796
  if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
 
2797
      || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
 
2798
    return 0;
 
2799
 
 
2800
  /* Preserve relocations against symbols with function type.  */
 
2801
  if (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION)
 
2802
    return 0;
 
2803
 
 
2804
  /* Don't allow symbols to be discarded on GOT related relocs.  */
 
2805
  if (fixp->fx_r_type == BFD_RELOC_NIOS2_GOT16
 
2806
      || fixp->fx_r_type == BFD_RELOC_NIOS2_CALL16
 
2807
      || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_LO
 
2808
      || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF_HA
 
2809
      || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_GD16
 
2810
      || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LDM16
 
2811
      || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LDO16
 
2812
      || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_IE16
 
2813
      || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_LE16
 
2814
      || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPMOD
 
2815
      || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_DTPREL
 
2816
      || fixp->fx_r_type == BFD_RELOC_NIOS2_TLS_TPREL
 
2817
      || fixp->fx_r_type == BFD_RELOC_NIOS2_GOTOFF)
 
2818
    return 0;
 
2819
 
 
2820
  return 1;
 
2821
}
 
2822
 
 
2823
/* Implement tc_frob_symbol.  This is called in adjust_reloc_syms;
 
2824
   it is used to remove *ABS* references from the symbol table.  */
 
2825
int
 
2826
nios2_frob_symbol (symbolS *symp)
 
2827
{
 
2828
  if ((OUTPUT_FLAVOR == bfd_target_elf_flavour
 
2829
       && symp == section_symbol (absolute_section))
 
2830
      || !S_IS_DEFINED (symp))
 
2831
    return 1;
 
2832
  else
 
2833
    return 0;
 
2834
}
 
2835
 
 
2836
/* The function tc_gen_reloc creates a relocation structure for the
 
2837
   fixup fixp, and returns a pointer to it.  This structure is passed
 
2838
   to bfd_install_relocation so that it can be written to the object
 
2839
   file for linking.  */
 
2840
arelent *
 
2841
tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
 
2842
{
 
2843
  arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
 
2844
  reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
 
2845
  *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
 
2846
 
 
2847
  reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
 
2848
  reloc->addend = fixp->fx_offset;  /* fixp->fx_addnumber; */
 
2849
 
 
2850
  if (fixp->fx_pcrel)
 
2851
    {
 
2852
      switch (fixp->fx_r_type)
 
2853
        {
 
2854
        case BFD_RELOC_16:
 
2855
          fixp->fx_r_type = BFD_RELOC_16_PCREL;
 
2856
          break;
 
2857
        case BFD_RELOC_NIOS2_LO16:
 
2858
          fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_LO;
 
2859
          break;
 
2860
        case BFD_RELOC_NIOS2_HIADJ16:
 
2861
          fixp->fx_r_type = BFD_RELOC_NIOS2_PCREL_HA;
 
2862
          break;
 
2863
        default:
 
2864
          break;
 
2865
        }
 
2866
    }
 
2867
 
 
2868
  reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
 
2869
  if (reloc->howto == NULL)
 
2870
    {
 
2871
      as_bad_where (fixp->fx_file, fixp->fx_line,
 
2872
                    _("can't represent relocation type %s"),
 
2873
                    bfd_get_reloc_code_name (fixp->fx_r_type));
 
2874
 
 
2875
      /* Set howto to a garbage value so that we can keep going.  */
 
2876
      reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
 
2877
      gas_assert (reloc->howto != NULL);
 
2878
    }
 
2879
  return reloc;
 
2880
}
 
2881
 
 
2882
long
 
2883
md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED)
 
2884
{
 
2885
  return 0;
 
2886
}
 
2887
 
 
2888
/* Called just before the assembler exits.  */
 
2889
void
 
2890
md_end ()
 
2891
{
 
2892
  /* FIXME - not yet implemented */
 
2893
}
 
2894
 
 
2895
/* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
 
2896
   Otherwise we have no need to default values of symbols.  */
 
2897
symbolS *
 
2898
md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
 
2899
{
 
2900
#ifdef OBJ_ELF
 
2901
  if (name[0] == '_' && name[1] == 'G'
 
2902
      && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
 
2903
    {
 
2904
      if (!GOT_symbol)
 
2905
        {
 
2906
          if (symbol_find (name))
 
2907
            as_bad ("GOT already in the symbol table");
 
2908
 
 
2909
          GOT_symbol = symbol_new (name, undefined_section,
 
2910
                                   (valueT) 0, &zero_address_frag);
 
2911
        }
 
2912
 
 
2913
      return GOT_symbol;
 
2914
    }
 
2915
#endif
 
2916
 
 
2917
  return 0;
 
2918
}
 
2919
 
 
2920
/* Implement tc_frob_label.  */
 
2921
void
 
2922
nios2_frob_label (symbolS *lab)
 
2923
{
 
2924
  /* Emit dwarf information.  */
 
2925
  dwarf2_emit_label (lab);
 
2926
 
 
2927
  /* Update the label's address with the current output pointer.  */
 
2928
  symbol_set_frag (lab, frag_now);
 
2929
  S_SET_VALUE (lab, (valueT) frag_now_fix ());
 
2930
 
 
2931
  /* Record this label for future adjustment after we find out what
 
2932
     kind of data it references, and the required alignment therewith.  */
 
2933
  nios2_last_label = lab;
 
2934
}
 
2935
 
 
2936
/* Implement md_cons_align.  */
 
2937
void
 
2938
nios2_cons_align (int size)
 
2939
{
 
2940
  int log_size = 0;
 
2941
  const char *pfill = NULL;
 
2942
 
 
2943
  while ((size >>= 1) != 0)
 
2944
    ++log_size;
 
2945
 
 
2946
  if (subseg_text_p (now_seg))
 
2947
    pfill = (const char *) &nop;
 
2948
  else
 
2949
    pfill = NULL;
 
2950
 
 
2951
  if (nios2_auto_align_on)
 
2952
    nios2_align (log_size, pfill, NULL);
 
2953
 
 
2954
  nios2_last_label = NULL;
 
2955
}
 
2956
 
 
2957
/* Map 's' to SHF_NIOS2_GPREL.  */
 
2958
/* This is from the Alpha code tc-alpha.c.  */
 
2959
int
 
2960
nios2_elf_section_letter (int letter, char **ptr_msg)
 
2961
{
 
2962
  if (letter == 's')
 
2963
    return SHF_NIOS2_GPREL;
 
2964
 
 
2965
  *ptr_msg = _("Bad .section directive: want a,s,w,x,M,S,G,T in string");
 
2966
  return -1;
 
2967
}
 
2968
 
 
2969
/* Map SHF_ALPHA_GPREL to SEC_SMALL_DATA.  */
 
2970
/* This is from the Alpha code tc-alpha.c.  */
 
2971
flagword
 
2972
nios2_elf_section_flags (flagword flags, int attr, int type ATTRIBUTE_UNUSED)
 
2973
{
 
2974
  if (attr & SHF_NIOS2_GPREL)
 
2975
    flags |= SEC_SMALL_DATA;
 
2976
  return flags;
 
2977
}
 
2978
 
 
2979
/* Implement TC_PARSE_CONS_EXPRESSION to handle %tls_ldo(...) */
 
2980
static int nios2_tls_ldo_reloc;
 
2981
 
 
2982
void
 
2983
nios2_cons (expressionS *exp, int size)
 
2984
{
 
2985
  nios2_tls_ldo_reloc = 0;
 
2986
 
 
2987
  SKIP_WHITESPACE ();
 
2988
  if (input_line_pointer[0] == '%')
 
2989
    {
 
2990
      if (strprefix (input_line_pointer + 1, "tls_ldo"))
 
2991
        {
 
2992
          if (size != 4)
 
2993
            as_bad (_("Illegal operands: %%tls_ldo in %d-byte data field"),
 
2994
                    size);
 
2995
          else
 
2996
            {
 
2997
              input_line_pointer += 8;
 
2998
              nios2_tls_ldo_reloc = 1;
 
2999
            }
 
3000
        }
 
3001
      if (nios2_tls_ldo_reloc)
 
3002
        {
 
3003
          SKIP_WHITESPACE ();
 
3004
          if (input_line_pointer[0] != '(')
 
3005
            as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
 
3006
          else
 
3007
            {
 
3008
              int c;
 
3009
              char *end = ++input_line_pointer;
 
3010
              int npar = 0;
 
3011
 
 
3012
              for (c = *end; !is_end_of_line[c]; end++, c = *end)
 
3013
                if (c == '(')
 
3014
                  npar++;
 
3015
                else if (c == ')')
 
3016
                  {
 
3017
                    if (!npar)
 
3018
                      break;
 
3019
                    npar--;
 
3020
                  }
 
3021
 
 
3022
              if (c != ')')
 
3023
                as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
 
3024
              else
 
3025
                {
 
3026
                  *end = '\0';
 
3027
                  expression (exp);
 
3028
                  *end = c;
 
3029
                  if (input_line_pointer != end)
 
3030
                    as_bad (_("Illegal operands: %%tls_ldo requires arguments in ()"));
 
3031
                  else
 
3032
                    {
 
3033
                      input_line_pointer++;
 
3034
                      SKIP_WHITESPACE ();
 
3035
                      c = *input_line_pointer;
 
3036
                      if (! is_end_of_line[c] && c != ',')
 
3037
                        as_bad (_("Illegal operands: garbage after %%tls_ldo()"));
 
3038
                    }
 
3039
                }
 
3040
            }
 
3041
        }
 
3042
    }
 
3043
  if (!nios2_tls_ldo_reloc)
 
3044
    expression (exp);
 
3045
}
 
3046
 
 
3047
/* Implement TC_CONS_FIX_NEW.  */
 
3048
void
 
3049
nios2_cons_fix_new (fragS *frag, int where, unsigned int nbytes,
 
3050
                    expressionS *exp)
 
3051
{
 
3052
  bfd_reloc_code_real_type r;
 
3053
 
 
3054
  r = (nbytes == 1 ? BFD_RELOC_8
 
3055
       : (nbytes == 2 ? BFD_RELOC_16
 
3056
          : (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
 
3057
 
 
3058
  if (nios2_tls_ldo_reloc)
 
3059
    r = BFD_RELOC_NIOS2_TLS_DTPREL;
 
3060
 
 
3061
  fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
 
3062
  nios2_tls_ldo_reloc = 0;
 
3063
}
 
3064
 
 
3065
/* Implement HANDLE_ALIGN.  */
 
3066
void
 
3067
nios2_handle_align (fragS *fragp)
 
3068
{
 
3069
  /* If we are expecting to relax in the linker, then we must output a
 
3070
     relocation to tell the linker we are aligning code.  */
 
3071
  if (nios2_as_options.relax == relax_all
 
3072
      && (fragp->fr_type == rs_align || fragp->fr_type == rs_align_code)
 
3073
      && fragp->fr_address + fragp->fr_fix > 0
 
3074
      && fragp->fr_offset > 1
 
3075
      && now_seg != bss_section)
 
3076
    fix_new (fragp, fragp->fr_fix, 0, &abs_symbol, fragp->fr_offset, 0,
 
3077
             BFD_RELOC_NIOS2_ALIGN);
 
3078
}
 
3079
 
 
3080
/* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
 
3081
   register number.  */
 
3082
int
 
3083
nios2_regname_to_dw2regnum (char *regname)
 
3084
{
 
3085
  struct nios2_reg *r = nios2_reg_lookup (regname);
 
3086
  if (r == NULL)
 
3087
    return -1;
 
3088
  return r->index;
 
3089
}
 
3090
 
 
3091
/* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
 
3092
   unwind information for this procedure.  */
 
3093
void
 
3094
nios2_frame_initial_instructions (void)
 
3095
{
 
3096
  cfi_add_CFA_def_cfa (27, 0);
 
3097
}