OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [tools/] [riscv-gnu-toolchain-master/] [binutils/] [opcodes/] [riscv-dis.c] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 serginhofr
/* RISC-V disassembler
2
   Copyright 2011-2015 Free Software Foundation, Inc.
3
 
4
   Contributed by Andrew Waterman (waterman@cs.berkeley.edu) at UC Berkeley.
5
   Based on MIPS target.
6
 
7
   This file is part of the GNU opcodes library.
8
 
9
   This library is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 3, or (at your option)
12
   any later version.
13
 
14
   It is distributed in the hope that it will be useful, but WITHOUT
15
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17
   License for more details.
18
 
19
   You should have received a copy of the GNU General Public License
20
   along with this program; see the file COPYING3. If not,
21
   see <http://www.gnu.org/licenses/>.  */
22
 
23
#include "sysdep.h"
24
#include "dis-asm.h"
25
#include "libiberty.h"
26
#include "opcode/riscv.h"
27
#include "opintl.h"
28
#include "elf-bfd.h"
29
#include "elf/riscv.h"
30
 
31
#include <stdint.h>
32
#include <ctype.h>
33
 
34
struct riscv_private_data
35
{
36
  bfd_vma gp;
37
  bfd_vma print_addr;
38
  bfd_vma hi_addr[OP_MASK_RD + 1];
39
};
40
 
41
static const char * const *riscv_gpr_names;
42
static const char * const *riscv_fpr_names;
43
 
44
/* Other options */
45
static int no_aliases;  /* If set disassemble as most general inst.  */
46
 
47
static void
48
set_default_riscv_dis_options (void)
49
{
50
  riscv_gpr_names = riscv_gpr_names_abi;
51
  riscv_fpr_names = riscv_fpr_names_abi;
52
  no_aliases = 0;
53
}
54
 
55
static void
56
parse_riscv_dis_option (const char *option)
57
{
58
  if (CONST_STRNEQ (option, "no-aliases"))
59
    no_aliases = 1;
60
  else if (CONST_STRNEQ (option, "numeric"))
61
    {
62
      riscv_gpr_names = riscv_gpr_names_numeric;
63
      riscv_fpr_names = riscv_fpr_names_numeric;
64
    }
65
  else
66
    {
67
      /* Invalid option.  */
68
      fprintf (stderr, _("Unrecognized disassembler option: %s\n"), option);
69
    }
70
}
71
 
72
static void
73
parse_riscv_dis_options (const char *opts_in)
74
{
75
  char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts;
76
 
77
  set_default_riscv_dis_options ();
78
 
79
  for ( ; opt_end != NULL; opt = opt_end + 1)
80
    {
81
      if ((opt_end = strchr (opt, ',')) != NULL)
82
        *opt_end = 0;
83
      parse_riscv_dis_option (opt);
84
    }
85
 
86
  free (opts);
87
}
88
 
89
/* Print one argument from an array.  */
90
 
91
static void
92
arg_print (struct disassemble_info *info, unsigned long val,
93
           const char* const* array, size_t size)
94
{
95
  const char *s = val >= size || array[val] == NULL ? "unknown" : array[val];
96
  (*info->fprintf_func) (info->stream, "%s", s);
97
}
98
 
99
static void
100
maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset)
101
{
102
  if (pd->hi_addr[base_reg] != (bfd_vma)-1)
103
    {
104
      pd->print_addr = pd->hi_addr[base_reg] + offset;
105
      pd->hi_addr[base_reg] = -1;
106
    }
107
  else if (base_reg == X_GP && pd->gp != (bfd_vma)-1)
108
    pd->print_addr = pd->gp + offset;
109
  else if (base_reg == X_TP)
110
    pd->print_addr = offset;
111
}
112
 
113
/* Print insn arguments for 32/64-bit code.  */
114
 
115
static void
116
print_insn_args (const char *d, insn_t l, bfd_vma pc, disassemble_info *info)
117
{
118
  struct riscv_private_data *pd = info->private_data;
119
  int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1;
120
  int rd = (l >> OP_SH_RD) & OP_MASK_RD;
121
  fprintf_ftype print = info->fprintf_func;
122
 
123
  if (*d != '\0')
124
    print (info->stream, "\t");
125
 
126
  for (; *d != '\0'; d++)
127
    {
128
      switch (*d)
129
        {
130
        /* Xcustom */
131
        case '^':
132
          switch (*++d)
133
            {
134
            case 'd':
135
              print (info->stream, "%d", rd);
136
              break;
137
            case 's':
138
              print (info->stream, "%d", rs1);
139
              break;
140
            case 't':
141
              print (info->stream, "%d", (int) EXTRACT_OPERAND (RS2, l));
142
              break;
143
            case 'j':
144
              print (info->stream, "%d", (int) EXTRACT_OPERAND (CUSTOM_IMM, l));
145
              break;
146
            }
147
          break;
148
 
149
        case 'C': /* RVC */
150
          switch (*++d)
151
            {
152
            case 's': /* RS1 x8-x15 */
153
            case 'w': /* RS1 x8-x15 */
154
              print (info->stream, "%s",
155
                     riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]);
156
              break;
157
            case 't': /* RS2 x8-x15 */
158
            case 'x': /* RS2 x8-x15 */
159
              print (info->stream, "%s",
160
                     riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
161
              break;
162
            case 'U': /* RS1, constrained to equal RD */
163
              print (info->stream, "%s", riscv_gpr_names[rd]);
164
              break;
165
            case 'c': /* RS1, constrained to equal sp */
166
              print (info->stream, "%s", riscv_gpr_names[X_SP]);
167
              break;
168
            case 'V': /* RS2 */
169
              print (info->stream, "%s",
170
                     riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]);
171
              break;
172
            case 'i':
173
              print (info->stream, "%d", (int)EXTRACT_RVC_SIMM3 (l));
174
              break;
175
            case 'j':
176
              print (info->stream, "%d", (int)EXTRACT_RVC_IMM (l));
177
              break;
178
            case 'k':
179
              print (info->stream, "%d", (int)EXTRACT_RVC_LW_IMM (l));
180
              break;
181
            case 'l':
182
              print (info->stream, "%d", (int)EXTRACT_RVC_LD_IMM (l));
183
              break;
184
            case 'm':
185
              print (info->stream, "%d", (int)EXTRACT_RVC_LWSP_IMM (l));
186
              break;
187
            case 'n':
188
              print (info->stream, "%d", (int)EXTRACT_RVC_LDSP_IMM (l));
189
              break;
190
            case 'K':
191
              print (info->stream, "%d", (int)EXTRACT_RVC_ADDI4SPN_IMM (l));
192
              break;
193
            case 'L':
194
              print (info->stream, "%d", (int)EXTRACT_RVC_ADDI16SP_IMM (l));
195
              break;
196
            case 'M':
197
              print (info->stream, "%d", (int)EXTRACT_RVC_SWSP_IMM (l));
198
              break;
199
            case 'N':
200
              print (info->stream, "%d", (int)EXTRACT_RVC_SDSP_IMM (l));
201
              break;
202
            case 'p':
203
              info->target = EXTRACT_RVC_B_IMM (l) + pc;
204
              (*info->print_address_func) (info->target, info);
205
              break;
206
            case 'a':
207
              info->target = EXTRACT_RVC_J_IMM (l) + pc;
208
              (*info->print_address_func) (info->target, info);
209
              break;
210
            case 'u':
211
              print (info->stream, "0x%x",
212
                     (int) (EXTRACT_RVC_IMM (l) & (RISCV_BIGIMM_REACH-1)));
213
              break;
214
            case '>':
215
              print (info->stream, "0x%x", (int) EXTRACT_RVC_IMM (l) & 0x3f);
216
              break;
217
            case '<':
218
              print (info->stream, "0x%x", (int) EXTRACT_RVC_IMM (l) & 0x1f);
219
              break;
220
            case 'T': /* floating-point RS2 */
221
              print (info->stream, "%s",
222
                     riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]);
223
              break;
224
            case 'D': /* floating-point RS2 x8-x15 */
225
              print (info->stream, "%s",
226
                     riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
227
              break;
228
            }
229
          break;
230
 
231
        case ',':
232
        case '(':
233
        case ')':
234
        case '[':
235
        case ']':
236
          print (info->stream, "%c", *d);
237
          break;
238
 
239
        case '0':
240
          /* Only print constant 0 if it is the last argument */
241
          if (!d[1])
242
            print (info->stream, "0");
243
          break;
244
 
245
        case 'b':
246
        case 's':
247
          print (info->stream, "%s", riscv_gpr_names[rs1]);
248
          break;
249
 
250
        case 't':
251
          print (info->stream, "%s",
252
                 riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]);
253
          break;
254
 
255
        case 'u':
256
          print (info->stream, "0x%x",
257
                 (unsigned) EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS);
258
          break;
259
 
260
        case 'm':
261
          arg_print (info, EXTRACT_OPERAND (RM, l),
262
                     riscv_rm, ARRAY_SIZE (riscv_rm));
263
          break;
264
 
265
        case 'P':
266
          arg_print (info, EXTRACT_OPERAND (PRED, l),
267
                     riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
268
          break;
269
 
270
        case 'Q':
271
          arg_print (info, EXTRACT_OPERAND (SUCC, l),
272
                     riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
273
          break;
274
 
275
        case 'o':
276
          maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l));
277
        case 'j':
278
          if ((l & MASK_ADDI) == MATCH_ADDI || (l & MASK_JALR) == MATCH_JALR)
279
            maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l));
280
          print (info->stream, "%d", (int) EXTRACT_ITYPE_IMM (l));
281
          break;
282
 
283
        case 'q':
284
          maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l));
285
          print (info->stream, "%d", (int) EXTRACT_STYPE_IMM (l));
286
          break;
287
 
288
        case 'a':
289
          info->target = EXTRACT_UJTYPE_IMM (l) + pc;
290
          (*info->print_address_func) (info->target, info);
291
          break;
292
 
293
        case 'p':
294
          info->target = EXTRACT_SBTYPE_IMM (l) + pc;
295
          (*info->print_address_func) (info->target, info);
296
          break;
297
 
298
        case 'd':
299
          if ((l & MASK_AUIPC) == MATCH_AUIPC)
300
            pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l);
301
          else if ((l & MASK_LUI) == MATCH_LUI)
302
            pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l);
303
          else if ((l & MASK_C_LUI) == MATCH_C_LUI)
304
            pd->hi_addr[rd] = EXTRACT_RVC_LUI_IMM (l);
305
          print (info->stream, "%s", riscv_gpr_names[rd]);
306
          break;
307
 
308
        case 'z':
309
          print (info->stream, "%s", riscv_gpr_names[0]);
310
          break;
311
 
312
        case '>':
313
          print (info->stream, "0x%x", (int) EXTRACT_OPERAND (SHAMT, l));
314
          break;
315
 
316
        case '<':
317
          print (info->stream, "0x%x", (int) EXTRACT_OPERAND (SHAMTW, l));
318
          break;
319
 
320
        case 'S':
321
        case 'U':
322
          print (info->stream, "%s", riscv_fpr_names[rs1]);
323
          break;
324
 
325
        case 'T':
326
          print (info->stream, "%s", riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]);
327
          break;
328
 
329
        case 'D':
330
          print (info->stream, "%s", riscv_fpr_names[rd]);
331
          break;
332
 
333
        case 'R':
334
          print (info->stream, "%s", riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]);
335
          break;
336
 
337
        case 'E':
338
          {
339
            const char* csr_name = NULL;
340
            unsigned int csr = EXTRACT_OPERAND (CSR, l);
341
            switch (csr)
342
              {
343
              #define DECLARE_CSR(name, num) case num: csr_name = #name; break;
344
              #include "opcode/riscv-opc.h"
345
              #undef DECLARE_CSR
346
              }
347
            if (csr_name)
348
              print (info->stream, "%s", csr_name);
349
            else
350
              print (info->stream, "0x%x", csr);
351
            break;
352
          }
353
 
354
        case 'Z':
355
          print (info->stream, "%d", rs1);
356
          break;
357
 
358
        default:
359
          /* xgettext:c-format */
360
          print (info->stream, _("# internal error, undefined modifier (%c)"),
361
                 *d);
362
          return;
363
        }
364
    }
365
}
366
 
367
/* Print the RISC-V instruction at address MEMADDR in debugged memory,
368
   on using INFO.  Returns length of the instruction, in bytes.
369
   BIGENDIAN must be 1 if this is big-endian code, 0 if
370
   this is little-endian code.  */
371
 
372
static int
373
riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info)
374
{
375
  const struct riscv_opcode *op;
376
  static bfd_boolean init = 0;
377
  static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
378
  struct riscv_private_data *pd;
379
  int insnlen;
380
 
381
#define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
382
 
383
  /* Build a hash table to shorten the search time.  */
384
  if (! init)
385
    {
386
      for (op = riscv_opcodes; op < &riscv_opcodes[NUMOPCODES]; op++)
387
        if (!riscv_hash[OP_HASH_IDX (op->match)])
388
          riscv_hash[OP_HASH_IDX (op->match)] = op;
389
 
390
      init = 1;
391
    }
392
 
393
  if (info->private_data == NULL)
394
    {
395
      int i;
396
 
397
      pd = info->private_data = xcalloc (1, sizeof (struct riscv_private_data));
398
      pd->gp = -1;
399
      pd->print_addr = -1;
400
      for (i = 0; i < (int) ARRAY_SIZE (pd->hi_addr); i++)
401
        pd->hi_addr[i] = -1;
402
 
403
      for (i = 0; i < info->symtab_size; i++)
404
        if (strcmp (bfd_asymbol_name (info->symtab[i]), "_gp") == 0)
405
          pd->gp = bfd_asymbol_value (info->symtab[i]);
406
    }
407
  else
408
    pd = info->private_data;
409
 
410
  insnlen = riscv_insn_length (word);
411
 
412
  info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2;
413
  info->bytes_per_line = 8;
414
  info->display_endian = info->endian;
415
  info->insn_info_valid = 1;
416
  info->branch_delay_insns = 0;
417
  info->data_size = 0;
418
  info->insn_type = dis_nonbranch;
419
  info->target = 0;
420
  info->target2 = 0;
421
 
422
  op = riscv_hash[OP_HASH_IDX (word)];
423
  if (op != NULL)
424
    {
425
      int xlen = 0;
426
 
427
      /* The incoming section might not always be complete.  */
428
      if (info->section != NULL)
429
        {
430
          Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
431
          xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
432
        }
433
 
434
      for (; op < &riscv_opcodes[NUMOPCODES]; op++)
435
        {
436
          /* Does the opcode match?  */
437
          if (! (op->match_func) (op, word))
438
            continue;
439
          /* Is this a pseudo-instruction and may we print it as such?  */
440
          if (no_aliases && (op->pinfo & INSN_ALIAS))
441
            continue;
442
          /* Is this instruction restricted to a certain value of XLEN?  */
443
          if (isdigit (op->subset[0]) && atoi (op->subset) != xlen)
444
            continue;
445
 
446
          /* It's a match.  */
447
          (*info->fprintf_func) (info->stream, "%s", op->name);
448
          print_insn_args (op->args, word, memaddr, info);
449
 
450
          /* Try to disassemble multi-instruction addressing sequences.  */
451
          if (pd->print_addr != (bfd_vma)-1)
452
            {
453
              info->target = pd->print_addr;
454
              (*info->fprintf_func) (info->stream, " # ");
455
              (*info->print_address_func) (info->target, info);
456
              pd->print_addr = -1;
457
            }
458
 
459
          return insnlen;
460
        }
461
    }
462
 
463
  /* We did not find a match, so just print the instruction bits.  */
464
  info->insn_type = dis_noninsn;
465
  (*info->fprintf_func) (info->stream, "0x%llx", (unsigned long long)word);
466
  return insnlen;
467
}
468
 
469
int
470
print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
471
{
472
  bfd_byte packet[2];
473
  insn_t insn = 0;
474
  bfd_vma n;
475
  int status;
476
 
477
  if (info->disassembler_options != NULL)
478
    {
479
      parse_riscv_dis_options (info->disassembler_options);
480
      /* Avoid repeatedly parsing the options.  */
481
      info->disassembler_options = NULL;
482
    }
483
  else if (riscv_gpr_names == NULL)
484
    set_default_riscv_dis_options ();
485
 
486
  /* Instructions are a sequence of 2-byte packets in little-endian order.  */
487
  for (n = 0; n < sizeof (insn) && n < riscv_insn_length (insn); n += 2)
488
    {
489
      status = (*info->read_memory_func) (memaddr + n, packet, 2, info);
490
      if (status != 0)
491
        {
492
          /* Don't fail just because we fell off the end.  */
493
          if (n > 0)
494
            break;
495
          (*info->memory_error_func) (status, memaddr, info);
496
          return status;
497
        }
498
 
499
      insn |= ((insn_t) bfd_getl16 (packet)) << (8 * n);
500
    }
501
 
502
  return riscv_disassemble_insn (memaddr, insn, info);
503
}
504
 
505
void
506
print_riscv_disassembler_options (FILE *stream)
507
{
508
  fprintf (stream, _("\n\
509
The following RISC-V-specific disassembler options are supported for use\n\
510
with the -M switch (multiple options should be separated by commas):\n"));
511
 
512
  fprintf (stream, _("\n\
513
  numeric       Print numeric reigster names, rather than ABI names.\n"));
514
 
515
  fprintf (stream, _("\n\
516
  no-aliases    Disassemble only into canonical instructions, rather\n\
517
                than into pseudoinstructions.\n"));
518
 
519
  fprintf (stream, _("\n"));
520
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.