Coverage Report

Created: 2023-08-28 06:23

/src/binutils-gdb/opcodes/nios2-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* Altera Nios II disassemble routines
2
   Copyright (C) 2012-2023 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 the GNU opcodes library.
7
8
   This library 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
   It is distributed in the hope that it will be useful, but WITHOUT
14
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16
   License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with this file; see the file COPYING.  If not, write to the
20
   Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
21
   MA 02110-1301, USA.  */
22
23
#include "sysdep.h"
24
#include "disassemble.h"
25
#include "opintl.h"
26
#include "opcode/nios2.h"
27
#include "libiberty.h"
28
#include <string.h>
29
#include <assert.h>
30
31
/* No symbol table is available when this code runs out in an embedded
32
   system as when it is used for disassembler support in a monitor.  */
33
#if !defined(EMBEDDED_ENV)
34
#define SYMTAB_AVAILABLE 1
35
#include "elf-bfd.h"
36
#include "elf/nios2.h"
37
#endif
38
39
/* Default length of Nios II instruction in bytes.  */
40
0
#define INSNLEN 4
41
42
/* Data structures used by the opcode hash table.  */
43
typedef struct _nios2_opcode_hash
44
{
45
  const struct nios2_opcode *opcode;
46
  struct _nios2_opcode_hash *next;
47
} nios2_opcode_hash;
48
49
/* Hash table size.  */
50
0
#define OPCODE_HASH_SIZE (IW_R1_OP_UNSHIFTED_MASK + 1)
51
52
/* Extract the opcode from an instruction word.  */
53
static unsigned int
54
nios2_r1_extract_opcode (unsigned int x)
55
0
{
56
0
  return GET_IW_R1_OP (x);
57
0
}
58
59
static unsigned int
60
nios2_r2_extract_opcode (unsigned int x)
61
0
{
62
0
  return GET_IW_R2_OP (x);
63
0
}
64
65
/* We maintain separate hash tables for R1 and R2 opcodes, and pseudo-ops
66
   are stored in a different table than regular instructions.  */
67
68
typedef struct _nios2_disassembler_state
69
{
70
  const struct nios2_opcode *opcodes;
71
  const int *num_opcodes;
72
  unsigned int (*extract_opcode) (unsigned int);
73
  nios2_opcode_hash *hash[OPCODE_HASH_SIZE];
74
  nios2_opcode_hash *ps_hash[OPCODE_HASH_SIZE];
75
  const struct nios2_opcode *nop;
76
  bool init;
77
} nios2_disassembler_state;
78
79
static nios2_disassembler_state
80
nios2_r1_disassembler_state = {
81
  nios2_r1_opcodes,
82
  &nios2_num_r1_opcodes,
83
  nios2_r1_extract_opcode,
84
  {},
85
  {},
86
  NULL,
87
  0
88
};
89
90
static nios2_disassembler_state
91
nios2_r2_disassembler_state = {
92
  nios2_r2_opcodes,
93
  &nios2_num_r2_opcodes,
94
  nios2_r2_extract_opcode,
95
  {},
96
  {},
97
  NULL,
98
  0
99
};
100
101
/* Function to initialize the opcode hash table.  */
102
static void
103
nios2_init_opcode_hash (nios2_disassembler_state *state)
104
0
{
105
0
  unsigned int i;
106
0
  register const struct nios2_opcode *op;
107
108
0
  for (i = 0; i < OPCODE_HASH_SIZE; i++)
109
0
    for (op = state->opcodes; op < &state->opcodes[*(state->num_opcodes)]; op++)
110
0
      {
111
0
  nios2_opcode_hash *new_hash;
112
0
  nios2_opcode_hash **bucket = NULL;
113
114
0
  if ((op->pinfo & NIOS2_INSN_MACRO) == NIOS2_INSN_MACRO)
115
0
    {
116
0
      if (i == state->extract_opcode (op->match)
117
0
    && (op->pinfo & (NIOS2_INSN_MACRO_MOV | NIOS2_INSN_MACRO_MOVI)
118
0
        & 0x7fffffff))
119
0
        {
120
0
    bucket = &(state->ps_hash[i]);
121
0
    if (strcmp (op->name, "nop") == 0)
122
0
      state->nop = op;
123
0
        }
124
0
    }
125
0
  else if (i == state->extract_opcode (op->match))
126
0
    bucket = &(state->hash[i]);
127
128
0
  if (bucket)
129
0
    {
130
0
      new_hash =
131
0
        (nios2_opcode_hash *) malloc (sizeof (nios2_opcode_hash));
132
0
      if (new_hash == NULL)
133
0
        {
134
    /* xgettext:c-format */
135
0
    opcodes_error_handler (_("out of memory"));
136
0
    exit (1);
137
0
        }
138
0
      new_hash->opcode = op;
139
0
      new_hash->next = NULL;
140
0
      while (*bucket)
141
0
        bucket = &((*bucket)->next);
142
0
      *bucket = new_hash;
143
0
    }
144
0
      }
145
0
  state->init = 1;
146
147
#ifdef DEBUG_HASHTABLE
148
  for (i = 0; i < OPCODE_HASH_SIZE; ++i)
149
    {
150
      nios2_opcode_hash *tmp_hash = state->hash[i];
151
      printf ("index: 0x%02X  ops: ", i);
152
      while (tmp_hash != NULL)
153
  {
154
    printf ("%s ", tmp_hash->opcode->name);
155
    tmp_hash = tmp_hash->next;
156
  }
157
      printf ("\n");
158
    }
159
160
  for (i = 0; i < OPCODE_HASH_SIZE; ++i)
161
    {
162
      nios2_opcode_hash *tmp_hash = state->ps_hash[i];
163
      printf ("index: 0x%02X  ops: ", i);
164
      while (tmp_hash != NULL)
165
  {
166
    printf ("%s ", tmp_hash->opcode->name);
167
    tmp_hash = tmp_hash->next;
168
  }
169
      printf ("\n");
170
    }
171
#endif /* DEBUG_HASHTABLE */
172
0
}
173
174
/* Return a pointer to an nios2_opcode struct for a given instruction
175
   word OPCODE for bfd machine MACH, or NULL if there is an error.  */
176
const struct nios2_opcode *
177
nios2_find_opcode_hash (unsigned long opcode, unsigned long mach)
178
0
{
179
0
  nios2_opcode_hash *entry;
180
0
  nios2_disassembler_state *state;
181
182
  /* Select the right instruction set, hash tables, and opcode accessor
183
     for the mach variant.  */
184
0
  if (mach == bfd_mach_nios2r2)
185
0
    state = &nios2_r2_disassembler_state;
186
0
  else
187
0
    state = &nios2_r1_disassembler_state;
188
189
  /* Build a hash table to shorten the search time.  */
190
0
  if (!state->init)
191
0
    nios2_init_opcode_hash (state);
192
193
  /* Check for NOP first.  Both NOP and MOV are macros that expand into
194
     an ADD instruction, and we always want to give priority to NOP.  */
195
0
  if (state->nop->match == (opcode & state->nop->mask))
196
0
    return state->nop;
197
198
  /* First look in the pseudo-op hashtable.  */
199
0
  for (entry = state->ps_hash[state->extract_opcode (opcode)];
200
0
       entry; entry = entry->next)
201
0
    if (entry->opcode->match == (opcode & entry->opcode->mask))
202
0
      return entry->opcode;
203
204
  /* Otherwise look in the main hashtable.  */
205
0
  for (entry = state->hash[state->extract_opcode (opcode)];
206
0
       entry; entry = entry->next)
207
0
    if (entry->opcode->match == (opcode & entry->opcode->mask))
208
0
      return entry->opcode;
209
210
0
  return NULL;
211
0
}
212
213
/* There are 32 regular registers, 32 coprocessor registers,
214
   and 32 control registers.  */
215
0
#define NUMREGNAMES 32
216
217
/* Return a pointer to the base of the coprocessor register name array.  */
218
static struct nios2_reg *
219
nios2_coprocessor_regs (void)
220
0
{
221
0
  static struct nios2_reg *cached = NULL;
222
223
0
  if (!cached)
224
0
    {
225
0
      int i;
226
0
      for (i = NUMREGNAMES; i < nios2_num_regs; i++)
227
0
  if (!strcmp (nios2_regs[i].name, "c0"))
228
0
    {
229
0
      cached = nios2_regs + i;
230
0
      break;
231
0
    }
232
0
      assert (cached);
233
0
    }
234
0
  return cached;
235
0
}
236
237
/* Return a pointer to the base of the control register name array.  */
238
static struct nios2_reg *
239
nios2_control_regs (void)
240
0
{
241
0
  static struct nios2_reg *cached = NULL;
242
243
0
  if (!cached)
244
0
    {
245
0
      int i;
246
0
      for (i = NUMREGNAMES; i < nios2_num_regs; i++)
247
0
  if (!strcmp (nios2_regs[i].name, "status"))
248
0
    {
249
0
      cached = nios2_regs + i;
250
0
      break;
251
0
    }
252
0
      assert (cached);
253
0
    }
254
0
  return cached;
255
0
}
256
257
/* Helper routine to report internal errors.  */
258
static void
259
bad_opcode (const struct nios2_opcode *op)
260
0
{
261
0
  opcodes_error_handler
262
    /* xgettext:c-format */
263
0
    (_("internal error: broken opcode descriptor for `%s %s'"),
264
0
     op->name, op->args);
265
0
  abort ();
266
0
}
267
268
/* The function nios2_print_insn_arg uses the character pointed
269
   to by ARGPTR to determine how it print the next token or separator
270
   character in the arguments to an instruction.  */
271
static int
272
nios2_print_insn_arg (const char *argptr,
273
          unsigned long opcode, bfd_vma address,
274
          disassemble_info *info,
275
          const struct nios2_opcode *op)
276
0
{
277
0
  unsigned long i = 0;
278
0
  long s = 0;
279
0
  int32_t o = 0;
280
0
  struct nios2_reg *reg_base;
281
282
0
  switch (*argptr)
283
0
    {
284
0
    case ',':
285
0
    case '(':
286
0
    case ')':
287
0
      (*info->fprintf_func) (info->stream, "%c", *argptr);
288
0
      break;
289
290
0
    case 'c':
291
      /* Control register index.  */
292
0
      switch (op->format)
293
0
  {
294
0
  case iw_r_type:
295
0
    i = GET_IW_R_IMM5 (opcode);
296
0
    break;
297
0
  case iw_F3X6L5_type:
298
0
    i = GET_IW_F3X6L5_IMM5 (opcode);
299
0
    break;
300
0
  default:
301
0
    bad_opcode (op);
302
0
  }
303
0
      reg_base = nios2_control_regs ();
304
0
      (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
305
0
      break;
306
307
0
    case 'd':
308
0
      reg_base = nios2_regs;
309
0
      switch (op->format)
310
0
  {
311
0
  case iw_r_type:
312
0
    i = GET_IW_R_C (opcode);
313
0
    break;
314
0
  case iw_custom_type:
315
0
    i = GET_IW_CUSTOM_C (opcode);
316
0
    if (GET_IW_CUSTOM_READC (opcode) == 0)
317
0
      reg_base = nios2_coprocessor_regs ();
318
0
    break;
319
0
  case iw_F3X6L5_type:
320
0
  case iw_F3X6_type:
321
0
    i = GET_IW_F3X6L5_C (opcode);
322
0
    break;
323
0
  case iw_F3X8_type:
324
0
    i = GET_IW_F3X8_C (opcode);
325
0
    if (GET_IW_F3X8_READC (opcode) == 0)
326
0
      reg_base = nios2_coprocessor_regs ();
327
0
    break;
328
0
  case iw_F2_type:
329
0
    i = GET_IW_F2_B (opcode);
330
0
    break;
331
0
  default:
332
0
    bad_opcode (op);
333
0
  }
334
0
      if (i < NUMREGNAMES)
335
0
  (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
336
0
      else
337
0
  (*info->fprintf_func) (info->stream, "unknown");
338
0
      break;
339
340
0
    case 's':
341
0
      reg_base = nios2_regs;
342
0
      switch (op->format)
343
0
  {
344
0
  case iw_r_type:
345
0
    i = GET_IW_R_A (opcode);
346
0
    break;
347
0
  case iw_i_type:
348
0
    i = GET_IW_I_A (opcode);
349
0
    break;
350
0
  case iw_custom_type:
351
0
    i = GET_IW_CUSTOM_A (opcode);
352
0
    if (GET_IW_CUSTOM_READA (opcode) == 0)
353
0
      reg_base = nios2_coprocessor_regs ();
354
0
    break;
355
0
  case iw_F2I16_type:
356
0
    i = GET_IW_F2I16_A (opcode);
357
0
    break;
358
0
  case iw_F2X4I12_type:
359
0
    i = GET_IW_F2X4I12_A (opcode);
360
0
    break;
361
0
  case iw_F1X4I12_type:
362
0
    i = GET_IW_F1X4I12_A (opcode);
363
0
    break;
364
0
  case iw_F1X4L17_type:
365
0
    i = GET_IW_F1X4L17_A (opcode);
366
0
    break;
367
0
  case iw_F3X6L5_type:
368
0
  case iw_F3X6_type:
369
0
    i = GET_IW_F3X6L5_A (opcode);
370
0
    break;
371
0
  case iw_F2X6L10_type:
372
0
    i = GET_IW_F2X6L10_A (opcode);
373
0
    break;
374
0
  case iw_F3X8_type:
375
0
    i = GET_IW_F3X8_A (opcode);
376
0
    if (GET_IW_F3X8_READA (opcode) == 0)
377
0
      reg_base = nios2_coprocessor_regs ();
378
0
    break;
379
0
  case iw_F1X1_type:
380
0
    i = GET_IW_F1X1_A (opcode);
381
0
    break;
382
0
  case iw_F1I5_type:
383
0
    i = 27;   /* Implicit stack pointer reference.  */
384
0
    break;
385
0
  case iw_F2_type:
386
0
    i = GET_IW_F2_A (opcode);
387
0
    break;
388
0
  default:
389
0
    bad_opcode (op);
390
0
  }
391
0
      if (i < NUMREGNAMES)
392
0
  (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
393
0
      else
394
0
  (*info->fprintf_func) (info->stream, "unknown");
395
0
      break;
396
397
0
    case 't':
398
0
      reg_base = nios2_regs;
399
0
      switch (op->format)
400
0
  {
401
0
  case iw_r_type:
402
0
    i = GET_IW_R_B (opcode);
403
0
    break;
404
0
  case iw_i_type:
405
0
    i = GET_IW_I_B (opcode);
406
0
    break;
407
0
  case iw_custom_type:
408
0
    i = GET_IW_CUSTOM_B (opcode);
409
0
    if (GET_IW_CUSTOM_READB (opcode) == 0)
410
0
      reg_base = nios2_coprocessor_regs ();
411
0
    break;
412
0
  case iw_F2I16_type:
413
0
    i = GET_IW_F2I16_B (opcode);
414
0
    break;
415
0
  case iw_F2X4I12_type:
416
0
    i = GET_IW_F2X4I12_B (opcode);
417
0
    break;
418
0
  case iw_F3X6L5_type:
419
0
  case iw_F3X6_type:
420
0
    i = GET_IW_F3X6L5_B (opcode);
421
0
    break;
422
0
  case iw_F2X6L10_type:
423
0
    i = GET_IW_F2X6L10_B (opcode);
424
0
    break;
425
0
  case iw_F3X8_type:
426
0
    i = GET_IW_F3X8_B (opcode);
427
0
    if (GET_IW_F3X8_READB (opcode) == 0)
428
0
      reg_base = nios2_coprocessor_regs ();
429
0
    break;
430
0
  case iw_F1I5_type:
431
0
    i = GET_IW_F1I5_B (opcode);
432
0
    break;
433
0
  case iw_F2_type:
434
0
    i = GET_IW_F2_B (opcode);
435
0
    break;
436
0
  case iw_T1X1I6_type:
437
0
    i = 0;
438
0
    break;
439
0
  default:
440
0
    bad_opcode (op);
441
0
  }
442
0
      if (i < NUMREGNAMES)
443
0
  (*info->fprintf_func) (info->stream, "%s", reg_base[i].name);
444
0
      else
445
0
  (*info->fprintf_func) (info->stream, "unknown");
446
0
      break;
447
448
0
    case 'D':
449
0
      switch (op->format)
450
0
  {
451
0
  case iw_T1I7_type:
452
0
    i = GET_IW_T1I7_A3 (opcode);
453
0
    break;
454
0
  case iw_T2X1L3_type:
455
0
    i = GET_IW_T2X1L3_B3 (opcode);
456
0
    break;
457
0
  case iw_T2X1I3_type:
458
0
    i = GET_IW_T2X1I3_B3 (opcode);
459
0
    break;
460
0
  case iw_T3X1_type:
461
0
    i = GET_IW_T3X1_C3 (opcode);
462
0
    break;
463
0
  case iw_T2X3_type:
464
0
    if (op->num_args == 3)
465
0
      i = GET_IW_T2X3_A3 (opcode);
466
0
    else
467
0
      i = GET_IW_T2X3_B3 (opcode);
468
0
    break;
469
0
  default:
470
0
    bad_opcode (op);
471
0
  }
472
0
      i = nios2_r2_reg3_mappings[i];
473
0
      (*info->fprintf_func) (info->stream, "%s", nios2_regs[i].name);
474
0
      break;
475
476
0
    case 'M':
477
      /* 6-bit unsigned immediate with no shift.  */
478
0
      switch (op->format)
479
0
  {
480
0
  case iw_T1X1I6_type:
481
0
    i = GET_IW_T1X1I6_IMM6 (opcode);
482
0
    break;
483
0
  default:
484
0
    bad_opcode (op);
485
0
  }
486
0
      (*info->fprintf_func) (info->stream, "%ld", i);
487
0
      break;
488
489
0
    case 'N':
490
      /* 6-bit unsigned immediate with 2-bit shift.  */
491
0
      switch (op->format)
492
0
  {
493
0
  case iw_T1X1I6_type:
494
0
    i = GET_IW_T1X1I6_IMM6 (opcode) << 2;
495
0
    break;
496
0
  default:
497
0
    bad_opcode (op);
498
0
  }
499
0
      (*info->fprintf_func) (info->stream, "%ld", i);
500
0
      break;
501
502
0
    case 'S':
503
0
      switch (op->format)
504
0
  {
505
0
  case iw_T1I7_type:
506
0
    i = GET_IW_T1I7_A3 (opcode);
507
0
    break;
508
0
  case iw_T2I4_type:
509
0
    i = GET_IW_T2I4_A3 (opcode);
510
0
    break;
511
0
  case iw_T2X1L3_type:
512
0
    i = GET_IW_T2X1L3_A3 (opcode);
513
0
    break;
514
0
  case iw_T2X1I3_type:
515
0
    i = GET_IW_T2X1I3_A3 (opcode);
516
0
    break;
517
0
  case iw_T3X1_type:
518
0
    i = GET_IW_T3X1_A3 (opcode);
519
0
    break;
520
0
  case iw_T2X3_type:
521
0
    i = GET_IW_T2X3_A3 (opcode);
522
0
    break;
523
0
  case iw_T1X1I6_type:
524
0
    i = GET_IW_T1X1I6_A3 (opcode);
525
0
    break;
526
0
  default:
527
0
    bad_opcode (op);
528
0
  }
529
0
      i = nios2_r2_reg3_mappings[i];
530
0
      (*info->fprintf_func) (info->stream, "%s", nios2_regs[i].name);
531
0
      break;
532
533
0
    case 'T':
534
0
      switch (op->format)
535
0
  {
536
0
  case iw_T2I4_type:
537
0
    i = GET_IW_T2I4_B3 (opcode);
538
0
    break;
539
0
  case iw_T3X1_type:
540
0
    i = GET_IW_T3X1_B3 (opcode);
541
0
    break;
542
0
  case iw_T2X3_type:
543
0
    i = GET_IW_T2X3_B3 (opcode);
544
0
    break;
545
0
  default:
546
0
    bad_opcode (op);
547
0
  }
548
0
      i = nios2_r2_reg3_mappings[i];
549
0
      (*info->fprintf_func) (info->stream, "%s", nios2_regs[i].name);
550
0
      break;
551
552
0
    case 'i':
553
      /* 16-bit signed immediate.  */
554
0
      switch (op->format)
555
0
  {
556
0
  case iw_i_type:
557
0
    s = ((int32_t) ((GET_IW_I_IMM16 (opcode) & 0xffff) ^ 0x8000)
558
0
         - 0x8000);
559
0
    break;
560
0
  case iw_F2I16_type:
561
0
    s = ((int32_t) ((GET_IW_F2I16_IMM16 (opcode) & 0xffff) ^ 0x8000)
562
0
         - 0x8000);
563
0
    break;
564
0
  default:
565
0
    bad_opcode (op);
566
0
  }
567
0
      (*info->fprintf_func) (info->stream, "%ld", s);
568
0
      break;
569
570
0
    case 'I':
571
      /* 12-bit signed immediate.  */
572
0
      switch (op->format)
573
0
  {
574
0
  case iw_F2X4I12_type:
575
0
    s = ((int32_t) ((GET_IW_F2X4I12_IMM12 (opcode) & 0xfff) ^ 0x800)
576
0
         - 0x800);
577
0
    break;
578
0
  case iw_F1X4I12_type:
579
0
    s = ((int32_t) ((GET_IW_F1X4I12_IMM12 (opcode) & 0xfff) ^ 0x800)
580
0
         - 0x800);
581
0
    break;
582
0
  default:
583
0
    bad_opcode (op);
584
0
  }
585
0
      (*info->fprintf_func) (info->stream, "%ld", s);
586
0
      break;
587
588
0
    case 'u':
589
      /* 16-bit unsigned immediate.  */
590
0
      switch (op->format)
591
0
  {
592
0
  case iw_i_type:
593
0
    i = GET_IW_I_IMM16 (opcode);
594
0
    break;
595
0
  case iw_F2I16_type:
596
0
    i = GET_IW_F2I16_IMM16 (opcode);
597
0
    break;
598
0
  default:
599
0
    bad_opcode (op);
600
0
  }
601
0
      (*info->fprintf_func) (info->stream, "%ld", i);
602
0
      break;
603
604
0
    case 'U':
605
      /* 7-bit unsigned immediate with 2-bit shift.  */
606
0
      switch (op->format)
607
0
  {
608
0
  case iw_T1I7_type:
609
0
    i = GET_IW_T1I7_IMM7 (opcode) << 2;
610
0
    break;
611
0
  case iw_X1I7_type:
612
0
    i = GET_IW_X1I7_IMM7 (opcode) << 2;
613
0
    break;
614
0
  default:
615
0
    bad_opcode (op);
616
0
  }
617
0
      (*info->fprintf_func) (info->stream, "%ld", i);
618
0
      break;
619
620
0
    case 'V':
621
      /* 5-bit unsigned immediate with 2-bit shift.  */
622
0
      switch (op->format)
623
0
  {
624
0
  case iw_F1I5_type:
625
0
    i = GET_IW_F1I5_IMM5 (opcode) << 2;
626
0
    break;
627
0
  default:
628
0
    bad_opcode (op);
629
0
  }
630
0
      (*info->fprintf_func) (info->stream, "%ld", i);
631
0
      break;
632
633
0
    case 'W':
634
      /* 4-bit unsigned immediate with 2-bit shift.  */
635
0
      switch (op->format)
636
0
  {
637
0
  case iw_T2I4_type:
638
0
    i = GET_IW_T2I4_IMM4 (opcode) << 2;
639
0
    break;
640
0
  case iw_L5I4X1_type:
641
0
    i = GET_IW_L5I4X1_IMM4 (opcode) << 2;
642
0
    break;
643
0
  default:
644
0
    bad_opcode (op);
645
0
  }
646
0
      (*info->fprintf_func) (info->stream, "%ld", i);
647
0
      break;
648
649
0
    case 'X':
650
      /* 4-bit unsigned immediate with 1-bit shift.  */
651
0
      switch (op->format)
652
0
  {
653
0
  case iw_T2I4_type:
654
0
    i = GET_IW_T2I4_IMM4 (opcode) << 1;
655
0
    break;
656
0
  default:
657
0
    bad_opcode (op);
658
0
  }
659
0
      (*info->fprintf_func) (info->stream, "%ld", i);
660
0
      break;
661
662
0
    case 'Y':
663
      /* 4-bit unsigned immediate without shift.  */
664
0
      switch (op->format)
665
0
  {
666
0
  case iw_T2I4_type:
667
0
    i = GET_IW_T2I4_IMM4 (opcode);
668
0
    break;
669
0
  default:
670
0
    bad_opcode (op);
671
0
  }
672
0
      (*info->fprintf_func) (info->stream, "%ld", i);
673
0
      break;
674
675
0
    case 'o':
676
      /* 16-bit signed immediate address offset.  */
677
0
      switch (op->format)
678
0
  {
679
0
  case iw_i_type:
680
0
    o = ((GET_IW_I_IMM16 (opcode) & 0xffff) ^ 0x8000) - 0x8000;
681
0
    break;
682
0
  case iw_F2I16_type:
683
0
    o = ((GET_IW_F2I16_IMM16 (opcode) & 0xffff) ^ 0x8000) - 0x8000;
684
0
    break;
685
0
  default:
686
0
    bad_opcode (op);
687
0
  }
688
0
      address = address + 4 + o;
689
0
      (*info->print_address_func) (address, info);
690
0
      break;
691
692
0
    case 'O':
693
      /* 10-bit signed address offset with 1-bit shift.  */
694
0
      switch (op->format)
695
0
  {
696
0
  case iw_I10_type:
697
0
    o = (((GET_IW_I10_IMM10 (opcode) & 0x3ff) ^ 0x200) - 0x200) * 2;
698
0
    break;
699
0
  default:
700
0
    bad_opcode (op);
701
0
  }
702
0
      address = address + 2 + o;
703
0
      (*info->print_address_func) (address, info);
704
0
      break;
705
706
0
    case 'P':
707
      /* 7-bit signed address offset with 1-bit shift.  */
708
0
      switch (op->format)
709
0
  {
710
0
  case iw_T1I7_type:
711
0
    o = (((GET_IW_T1I7_IMM7 (opcode) & 0x7f) ^ 0x40) - 0x40) * 2;
712
0
    break;
713
0
  default:
714
0
    bad_opcode (op);
715
0
  }
716
0
      address = address + 2 + o;
717
0
      (*info->print_address_func) (address, info);
718
0
      break;
719
720
0
    case 'j':
721
      /* 5-bit unsigned immediate.  */
722
0
      switch (op->format)
723
0
  {
724
0
  case iw_r_type:
725
0
    i = GET_IW_R_IMM5 (opcode);
726
0
    break;
727
0
  case iw_F3X6L5_type:
728
0
    i = GET_IW_F3X6L5_IMM5 (opcode);
729
0
    break;
730
0
  case iw_F2X6L10_type:
731
0
    i = GET_IW_F2X6L10_MSB (opcode);
732
0
    break;
733
0
  case iw_X2L5_type:
734
0
    i = GET_IW_X2L5_IMM5 (opcode);
735
0
    break;
736
0
  default:
737
0
    bad_opcode (op);
738
0
  }
739
0
      (*info->fprintf_func) (info->stream, "%ld", i);
740
0
      break;
741
742
0
    case 'k':
743
      /* Second 5-bit unsigned immediate field.  */
744
0
      switch (op->format)
745
0
  {
746
0
  case iw_F2X6L10_type:
747
0
    i = GET_IW_F2X6L10_LSB (opcode);
748
0
    break;
749
0
  default:
750
0
    bad_opcode (op);
751
0
  }
752
0
      (*info->fprintf_func) (info->stream, "%ld", i);
753
0
      break;
754
755
0
    case 'l':
756
      /* 8-bit unsigned immediate.  */
757
0
      switch (op->format)
758
0
  {
759
0
  case iw_custom_type:
760
0
    i = GET_IW_CUSTOM_N (opcode);
761
0
    break;
762
0
  case iw_F3X8_type:
763
0
    i = GET_IW_F3X8_N (opcode);
764
0
    break;
765
0
  default:
766
0
    bad_opcode (op);
767
0
  }
768
0
      (*info->fprintf_func) (info->stream, "%lu", i);
769
0
      break;
770
771
0
    case 'm':
772
      /* 26-bit unsigned immediate.  */
773
0
      switch (op->format)
774
0
  {
775
0
  case iw_j_type:
776
0
    i = GET_IW_J_IMM26 (opcode);
777
0
    break;
778
0
  case iw_L26_type:
779
0
    i = GET_IW_L26_IMM26 (opcode);
780
0
    break;
781
0
  default:
782
0
    bad_opcode (op);
783
0
  }
784
      /* This translates to an address because it's only used in call
785
   instructions.  */
786
0
      address = (address & 0xf0000000) | (i << 2);
787
0
      (*info->print_address_func) (address, info);
788
0
      break;
789
790
0
    case 'e':
791
      /* Encoded enumeration for addi.n/subi.n.  */
792
0
      switch (op->format)
793
0
  {
794
0
  case iw_T2X1I3_type:
795
0
    i = nios2_r2_asi_n_mappings[GET_IW_T2X1I3_IMM3 (opcode)];
796
0
    break;
797
0
  default:
798
0
    bad_opcode (op);
799
0
  }
800
0
      (*info->fprintf_func) (info->stream, "%lu", i);
801
0
      break;
802
803
0
    case 'f':
804
      /* Encoded enumeration for slli.n/srli.n.  */
805
0
      switch (op->format)
806
0
  {
807
0
  case iw_T2X1L3_type:
808
0
    i = nios2_r2_shi_n_mappings[GET_IW_T2X1I3_IMM3 (opcode)];
809
0
    break;
810
0
  default:
811
0
    bad_opcode (op);
812
0
  }
813
0
      (*info->fprintf_func) (info->stream, "%lu", i);
814
0
      break;
815
816
0
    case 'g':
817
      /* Encoded enumeration for andi.n.  */
818
0
      switch (op->format)
819
0
  {
820
0
  case iw_T2I4_type:
821
0
    i = nios2_r2_andi_n_mappings[GET_IW_T2I4_IMM4 (opcode)];
822
0
    break;
823
0
  default:
824
0
    bad_opcode (op);
825
0
  }
826
0
      (*info->fprintf_func) (info->stream, "%lu", i);
827
0
      break;
828
829
0
    case 'h':
830
      /* Encoded enumeration for movi.n.  */
831
0
      switch (op->format)
832
0
  {
833
0
  case iw_T1I7_type:
834
0
    i = GET_IW_T1I7_IMM7 (opcode);
835
0
    if (i == 125)
836
0
      i = 0xff;
837
0
    else if (i == 126)
838
0
      i = -2;
839
0
    else if (i == 127)
840
0
      i = -1;
841
0
    break;
842
0
  default:
843
0
    bad_opcode (op);
844
0
  }
845
0
      (*info->fprintf_func) (info->stream, "%ld", i);
846
0
      break;
847
848
0
    case 'R':
849
0
      {
850
0
  unsigned long reglist = 0;
851
0
  int dir = 1;
852
0
  int k, t;
853
854
0
  switch (op->format)
855
0
    {
856
0
    case iw_F1X4L17_type:
857
      /* Encoding for ldwm/stwm.  */
858
0
      i = GET_IW_F1X4L17_REGMASK (opcode);
859
0
      if (GET_IW_F1X4L17_RS (opcode))
860
0
        {
861
0
    reglist = ((i << 14) & 0x00ffc000);
862
0
    if (i & (1 << 10))
863
0
      reglist |= (1 << 28);
864
0
    if (i & (1 << 11))
865
0
      reglist |= (1u << 31);
866
0
        }
867
0
      else
868
0
        reglist = i << 2;
869
0
      dir = GET_IW_F1X4L17_REGMASK (opcode) ? 1 : -1;
870
0
      break;
871
872
0
    case iw_L5I4X1_type:
873
      /* Encoding for push.n/pop.n.  */
874
0
      reglist |= (1u << 31);
875
0
      if (GET_IW_L5I4X1_FP (opcode))
876
0
        reglist |= (1 << 28);
877
0
      if (GET_IW_L5I4X1_CS (opcode))
878
0
        {
879
0
    int val = GET_IW_L5I4X1_REGRANGE (opcode);
880
0
    reglist |= nios2_r2_reg_range_mappings[val];
881
0
        }
882
0
      dir = (op->match == MATCH_R2_POP_N ? 1 : -1);
883
0
      break;
884
885
0
    default:
886
0
      bad_opcode (op);
887
0
    }
888
889
0
  t = 0;
890
0
  (*info->fprintf_func) (info->stream, "{");
891
0
  for (k = (dir == 1 ? 0 : 31);
892
0
       (dir == 1 && k < 32) || (dir == -1 && k >= 0);
893
0
       k += dir)
894
0
    if (reglist & (1u << k))
895
0
      {
896
0
        if (t)
897
0
    (*info->fprintf_func) (info->stream, ",");
898
0
        else
899
0
    t++;
900
0
        (*info->fprintf_func) (info->stream, "%s", nios2_regs[k].name);
901
0
      }
902
0
  (*info->fprintf_func) (info->stream, "}");
903
0
  break;
904
0
      }
905
906
0
    case 'B':
907
      /* Base register and options for ldwm/stwm.  */
908
0
      switch (op->format)
909
0
  {
910
0
  case iw_F1X4L17_type:
911
0
    if (GET_IW_F1X4L17_ID (opcode) == 0)
912
0
      (*info->fprintf_func) (info->stream, "--");
913
914
0
    i = GET_IW_F1X4I12_A (opcode);
915
0
    (*info->fprintf_func) (info->stream, "(%s)",
916
0
         nios2_builtin_regs[i].name);
917
918
0
    if (GET_IW_F1X4L17_ID (opcode))
919
0
      (*info->fprintf_func) (info->stream, "++");
920
0
    if (GET_IW_F1X4L17_WB (opcode))
921
0
      (*info->fprintf_func) (info->stream, ",writeback");
922
0
    if (GET_IW_F1X4L17_PC (opcode))
923
0
      (*info->fprintf_func) (info->stream, ",ret");
924
0
    break;
925
0
  default:
926
0
    bad_opcode (op);
927
0
  }
928
0
      break;
929
930
0
    default:
931
0
      (*info->fprintf_func) (info->stream, "unknown");
932
0
      break;
933
0
    }
934
0
  return 0;
935
0
}
936
937
/* nios2_disassemble does all the work of disassembling a Nios II
938
   instruction opcode.  */
939
static int
940
nios2_disassemble (bfd_vma address, unsigned long opcode,
941
       disassemble_info *info)
942
0
{
943
0
  const struct nios2_opcode *op;
944
945
0
  info->bytes_per_line = INSNLEN;
946
0
  info->bytes_per_chunk = INSNLEN;
947
0
  info->display_endian = info->endian;
948
0
  info->insn_info_valid = 1;
949
0
  info->branch_delay_insns = 0;
950
0
  info->data_size = 0;
951
0
  info->insn_type = dis_nonbranch;
952
0
  info->target = 0;
953
0
  info->target2 = 0;
954
955
  /* Find the major opcode and use this to disassemble
956
     the instruction and its arguments.  */
957
0
  op = nios2_find_opcode_hash (opcode, info->mach);
958
959
0
  if (op != NULL)
960
0
    {
961
0
      const char *argstr = op->args;
962
0
      (*info->fprintf_func) (info->stream, "%s", op->name);
963
0
      if (argstr != NULL && *argstr != '\0')
964
0
  {
965
0
    (*info->fprintf_func) (info->stream, "\t");
966
0
    while (*argstr != '\0')
967
0
      {
968
0
        nios2_print_insn_arg (argstr, opcode, address, info, op);
969
0
        ++argstr;
970
0
      }
971
0
  }
972
      /* Tell the caller how far to advance the program counter.  */
973
0
      info->bytes_per_chunk = op->size;
974
0
      return op->size;
975
0
    }
976
0
  else
977
0
    {
978
      /* Handle undefined instructions.  */
979
0
      info->insn_type = dis_noninsn;
980
0
      (*info->fprintf_func) (info->stream, "0x%lx", opcode);
981
0
      return INSNLEN;
982
0
    }
983
0
}
984
985
986
/* print_insn_nios2 is the main disassemble function for Nios II.
987
   The function diassembler(abfd) (source in disassemble.c) returns a
988
   pointer to this either print_insn_big_nios2 or
989
   print_insn_little_nios2, which in turn call this function when the
990
   bfd machine type is Nios II. print_insn_nios2 reads the
991
   instruction word at the address given, and prints the disassembled
992
   instruction on the stream info->stream using info->fprintf_func. */
993
994
static int
995
print_insn_nios2 (bfd_vma address, disassemble_info *info,
996
      enum bfd_endian endianness)
997
0
{
998
0
  bfd_byte buffer[INSNLEN];
999
0
  int status;
1000
1001
0
  status = (*info->read_memory_func) (address, buffer, INSNLEN, info);
1002
0
  if (status == 0)
1003
0
    {
1004
0
      unsigned long insn;
1005
0
      if (endianness == BFD_ENDIAN_BIG)
1006
0
  insn = (unsigned long) bfd_getb32 (buffer);
1007
0
      else
1008
0
  insn = (unsigned long) bfd_getl32 (buffer);
1009
0
      return nios2_disassemble (address, insn, info);
1010
0
    }
1011
1012
  /* We might have a 16-bit R2 instruction at the end of memory.  Try that.  */
1013
0
  if (info->mach == bfd_mach_nios2r2)
1014
0
    {
1015
0
      status = (*info->read_memory_func) (address, buffer, 2, info);
1016
0
      if (status == 0)
1017
0
  {
1018
0
    unsigned long insn;
1019
0
    if (endianness == BFD_ENDIAN_BIG)
1020
0
      insn = (unsigned long) bfd_getb16 (buffer);
1021
0
    else
1022
0
      insn = (unsigned long) bfd_getl16 (buffer);
1023
0
    return nios2_disassemble (address, insn, info);
1024
0
  }
1025
0
    }
1026
1027
  /* If we got here, we couldn't read anything.  */
1028
0
  (*info->memory_error_func) (status, address, info);
1029
0
  return -1;
1030
0
}
1031
1032
/* These two functions are the main entry points, accessed from
1033
   disassemble.c.  */
1034
int
1035
print_insn_big_nios2 (bfd_vma address, disassemble_info *info)
1036
0
{
1037
0
  return print_insn_nios2 (address, info, BFD_ENDIAN_BIG);
1038
0
}
1039
1040
int
1041
print_insn_little_nios2 (bfd_vma address, disassemble_info *info)
1042
0
{
1043
0
  return print_insn_nios2 (address, info, BFD_ENDIAN_LITTLE);
1044
0
}