Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/riscv-dis.c
Line
Count
Source
1
/* RISC-V disassembler
2
   Copyright (C) 2011-2026 Free Software Foundation, Inc.
3
4
   Contributed by Andrew Waterman (andrew@sifive.com).
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 "disassemble.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
#include "elfxx-riscv.h"
31
32
#include <stdint.h>
33
#include <ctype.h>
34
35
/* The RISC-V disassembler produces styled output using
36
   disassemble_info::fprintf_styled_func.  This define prevents use of
37
   disassemble_info::fprintf_func which is for unstyled output.  */
38
#define fprintf_func please_use_fprintf_styled_func_instead
39
40
struct riscv_private_data
41
{
42
  bfd_vma gp;
43
  bfd_vma print_addr;
44
  bfd_vma hi_addr[OP_MASK_RD + 1];
45
  bool to_print_addr;
46
  bool has_gp;
47
  bool has_jvt_base;
48
  asection *jvt_section;
49
  bfd_byte *jvt_content;
50
  bfd_size_type jvt_size;
51
  /* Current XLEN for the disassembler.  */
52
  unsigned xlen;
53
  /* Default ISA specification version.  */
54
  enum riscv_spec_class default_isa_spec;
55
  /* Used for architecture parser.  */
56
  riscv_parse_subset_t riscv_rps_dis;
57
  /* Default architecture string for the object file.  It will be changed once
58
     elf architecture attribute exits.  This is used for mapping symbol $x.  */
59
  const char* default_arch;
60
  /* Owning copy of the ISA string given by the "arch=" option, which
61
     DEFAULT_ARCH points at while the option is in effect.  The option
62
     string itself is freed once the options are parsed.  */
63
  char *arch_option;
64
  /* Used for mapping symbols.  */
65
  int last_map_symbol;
66
  bfd_vma last_stop_offset;
67
  bfd_vma last_map_symbol_boundary;
68
  enum riscv_seg_mstate last_map_state;
69
  asection *last_map_section;
70
  /* Register names as used by the disassembler.  */
71
  const char (*riscv_gpr_names)[NRC];
72
  const char (*riscv_fpr_names)[NRC];
73
  /* If set, disassemble as most general instruction.  */
74
  bool no_aliases;
75
  /* If set, disassemble without checking architecture string, just like what
76
     we did at the beginning.  */
77
  bool all_ext;
78
};
79
80
/* Set default RISC-V disassembler options.  */
81
82
static void
83
set_default_riscv_dis_options (struct disassemble_info *info)
84
1.61k
{
85
1.61k
  struct riscv_private_data *pd = info->private_data;
86
1.61k
  pd->riscv_gpr_names = riscv_gpr_names_abi;
87
1.61k
  pd->riscv_fpr_names = riscv_fpr_names_abi;
88
1.61k
  pd->no_aliases = false;
89
1.61k
  pd->all_ext = false;
90
1.61k
}
91
92
static void riscv_dis_parse_subset (struct disassemble_info *, const char *);
93
94
/* Disassemble according to ISA from now on.  If ISA does not parse,
95
   keep the previous ISA.  */
96
97
static void
98
riscv_dis_set_arch (struct disassemble_info *info, const char *isa)
99
0
{
100
0
  struct riscv_private_data *pd = info->private_data;
101
102
0
  riscv_release_subset_list (pd->riscv_rps_dis.subset_list);
103
0
  if (!riscv_parse_subset (&pd->riscv_rps_dis, isa))
104
0
    {
105
      /* The subset list now holds whatever parsed before the error, and
106
   has no ISA string, so this parses the previous ISA again.  */
107
0
      riscv_release_subset_list (pd->riscv_rps_dis.subset_list);
108
0
      riscv_dis_parse_subset (info, pd->default_arch);
109
0
      return;
110
0
    }
111
0
  riscv_arch_str (pd->xlen, pd->riscv_rps_dis.subset_list, true/* update */);
112
113
  /* Keep an owning copy: ISA points into the option string, which is
114
     freed once the options are parsed, while DEFAULT_ARCH is read again
115
     on every "$x" mapping symbol.  */
116
0
  free (pd->arch_option);
117
0
  pd->arch_option = xstrdup (isa);
118
0
  pd->default_arch = pd->arch_option;
119
0
}
120
121
/* Parse RISC-V disassembler option (without arguments).  */
122
123
static bool
124
parse_riscv_dis_option_without_args (const char *option,
125
             struct disassemble_info *info)
126
0
{
127
0
  struct riscv_private_data *pd = info->private_data;
128
0
  if (strcmp (option, "no-aliases") == 0)
129
0
    pd->no_aliases = true;
130
0
  else if (strcmp (option, "numeric") == 0)
131
0
    {
132
0
      pd->riscv_gpr_names = riscv_gpr_names_numeric;
133
0
      pd->riscv_fpr_names = riscv_fpr_names_numeric;
134
0
    }
135
0
  else if (strcmp (option, "max") == 0)
136
0
    pd->all_ext = true;
137
0
  else
138
0
    return false;
139
0
  return true;
140
0
}
141
142
static bool
143
riscv_vtype_altfmt_supported (struct riscv_private_data *pd)
144
0
{
145
0
  return (pd->all_ext
146
0
    || riscv_subset_supports (&pd->riscv_rps_dis, "zvfbfa")
147
0
    || riscv_subset_supports (&pd->riscv_rps_dis, "zvfofp8min")
148
0
    || riscv_subset_supports (&pd->riscv_rps_dis, "zvfqwbdota8f")
149
0
    || riscv_subset_supports (&pd->riscv_rps_dis, "zvfqwdota8f")
150
0
    || riscv_subset_supports (&pd->riscv_rps_dis, "zvfwbdota16bf")
151
0
    || riscv_subset_supports (&pd->riscv_rps_dis, "zvfwdota16bf")
152
0
    || riscv_subset_supports (&pd->riscv_rps_dis, "zvqwbdota8i")
153
0
    || riscv_subset_supports (&pd->riscv_rps_dis, "zvqwbdota16i")
154
0
    || riscv_subset_supports (&pd->riscv_rps_dis, "zvqwdota8i")
155
0
    || riscv_subset_supports (&pd->riscv_rps_dis, "zvqwdota16i"));
156
0
}
157
158
/* Parse RISC-V disassembler option (possibly with arguments).  */
159
160
static void
161
parse_riscv_dis_option (char *option, struct disassemble_info *info)
162
0
{
163
0
  char *equal, *value;
164
165
0
  if (parse_riscv_dis_option_without_args (option, info))
166
0
    return;
167
168
0
  equal = strchr (option, '=');
169
0
  if (equal == NULL)
170
0
    {
171
      /* The option without '=' should be defined above.  */
172
0
      opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
173
0
      return;
174
0
    }
175
0
  if (equal == option
176
0
      || *(equal + 1) == '\0')
177
0
    {
178
      /* Invalid options with '=', no option name before '=',
179
       and no value after '='.  */
180
0
      opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
181
0
                            option);
182
0
      return;
183
0
    }
184
185
0
  *equal = '\0';
186
0
  value = equal + 1;
187
0
  if (strcmp (option, "arch") == 0)
188
0
    riscv_dis_set_arch (info, value);
189
0
  else if (strcmp (option, "priv-spec") == 0)
190
0
    opcodes_error_handler (_("%s=%s is deprecated and ignored, the "
191
0
           "privileged spec is always 1.13; use binutils "
192
0
           "2.47 or earlier to select another version"),
193
0
         option, value);
194
0
  else
195
0
    {
196
      /* xgettext:c-format */
197
0
      opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
198
0
    }
199
0
}
200
201
/* Parse RISC-V disassembler options.  */
202
203
static void
204
parse_riscv_dis_options (const char *opts_in, struct disassemble_info *info)
205
0
{
206
0
  char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts;
207
208
0
  set_default_riscv_dis_options (info);
209
210
0
  for ( ; opt_end != NULL; opt = opt_end + 1)
211
0
    {
212
0
      if ((opt_end = strchr (opt, ',')) != NULL)
213
0
  *opt_end = 0;
214
0
      parse_riscv_dis_option (opt, info);
215
0
    }
216
217
0
  free (opts);
218
0
}
219
220
/* Print one argument from an array.  */
221
222
static void
223
arg_print (struct disassemble_info *info, unsigned long val,
224
     const char* const* array, size_t size)
225
19.9k
{
226
19.9k
  const char *s = val >= size || array[val] == NULL ? "unknown" : array[val];
227
19.9k
  (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s", s);
228
19.9k
}
229
230
/* If we need to print an address, set its value and state.  */
231
232
static void
233
maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset,
234
         int wide)
235
150k
{
236
150k
  if (pd->hi_addr[base_reg] != (bfd_vma)-1)
237
17.4k
    {
238
17.4k
      pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset;
239
17.4k
      pd->hi_addr[base_reg] = -1;
240
17.4k
    }
241
133k
  else if (base_reg == X_GP && pd->has_gp)
242
0
    pd->print_addr = pd->gp + offset;
243
133k
  else if (base_reg == X_TP || base_reg == 0)
244
13.7k
    pd->print_addr = offset;
245
119k
  else
246
119k
    return;  /* Don't print the address.  */
247
31.1k
  pd->to_print_addr = true;
248
249
  /* Sign-extend a 32-bit value to a 64-bit value.  */
250
31.1k
  if (wide)
251
7.34k
    pd->print_addr = (bfd_vma)(int32_t) pd->print_addr;
252
253
  /* Fit into a 32-bit value on RV32.  */
254
31.1k
  if (pd->xlen == 32)
255
0
    pd->print_addr = (bfd_vma)(uint32_t)pd->print_addr;
256
31.1k
}
257
258
/* Set pd->xlen from ELF class if not already known.  */
259
260
static void
261
riscv_set_xlen_from_elf (struct riscv_private_data *pd,
262
       struct disassemble_info *info)
263
2.09M
{
264
2.09M
  if (pd->xlen != 0)
265
2.09M
    return;
266
267
1.61k
  if (info->mach == bfd_mach_riscv64)
268
246
    pd->xlen = 64;
269
1.37k
  else if (info->mach == bfd_mach_riscv32)
270
503
    pd->xlen = 32;
271
867
  else if (info->section != NULL)
272
0
    {
273
0
      Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
274
0
      pd->xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
275
0
    }
276
1.61k
}
277
278
/* Try to print target address of cm.jalt and cm.jt.  */
279
280
static void
281
maybe_print_jvt_address (struct riscv_private_data *pd, int index)
282
0
{
283
0
  if (!pd->has_jvt_base || pd->jvt_section == NULL || pd->jvt_content == NULL
284
0
      || pd->xlen == 0)
285
0
    return;
286
287
0
  bfd_size_type entry_size = pd->xlen / 8;
288
0
  bfd_size_type offset = entry_size * (bfd_size_type) index;
289
0
  if (index < 0 || index > (ZCMT_TOTAL_ENTRIES - 1)
290
0
      || offset + entry_size > pd->jvt_size)
291
0
    return;
292
293
0
  bfd_byte *packet = pd->jvt_content + offset;
294
295
0
  pd->to_print_addr = true;
296
0
  pd->print_addr = bfd_get_bits (packet, pd->xlen,
297
0
         bfd_big_endian (pd->jvt_section->owner));
298
0
}
299
300
/* Get Zcmp reg_list field.  */
301
302
static void
303
print_reg_list (disassemble_info *info, insn_t l)
304
0
{
305
0
  struct riscv_private_data *pd = info->private_data;
306
0
  bool numeric = pd->riscv_gpr_names == riscv_gpr_names_numeric;
307
0
  unsigned reg_list = (int)EXTRACT_OPERAND (REG_LIST, l);
308
0
  unsigned r_start = numeric ? X_S2 : X_S0;
309
0
  info->fprintf_styled_func (info->stream, dis_style_register,
310
0
           "%s", pd->riscv_gpr_names[X_RA]);
311
312
0
  if (reg_list == 5)
313
0
    {
314
0
      info->fprintf_styled_func (info->stream, dis_style_text, ",");
315
0
      info->fprintf_styled_func (info->stream, dis_style_register,
316
0
         "%s", pd->riscv_gpr_names[X_S0]);
317
0
    }
318
0
  else if (reg_list == 6 || (numeric && reg_list > 6))
319
0
    {
320
0
      info->fprintf_styled_func (info->stream, dis_style_text, ",");
321
0
      info->fprintf_styled_func (info->stream, dis_style_register,
322
0
         "%s", pd->riscv_gpr_names[X_S0]);
323
0
      info->fprintf_styled_func (info->stream, dis_style_text, "-");
324
0
      info->fprintf_styled_func (info->stream, dis_style_register,
325
0
         "%s", pd->riscv_gpr_names[X_S1]);
326
0
    }
327
328
0
  if (reg_list == 15)
329
0
    {
330
0
      info->fprintf_styled_func (info->stream, dis_style_text, ",");
331
0
      info->fprintf_styled_func (info->stream, dis_style_register,
332
0
         "%s", pd->riscv_gpr_names[r_start]);
333
0
      info->fprintf_styled_func (info->stream, dis_style_text, "-");
334
0
      info->fprintf_styled_func (info->stream, dis_style_register,
335
0
         "%s", pd->riscv_gpr_names[X_S11]);
336
0
    }
337
0
  else if (reg_list == 7 && numeric)
338
0
    {
339
0
      info->fprintf_styled_func (info->stream, dis_style_text, ",");
340
0
      info->fprintf_styled_func (info->stream, dis_style_register,
341
0
         "%s", pd->riscv_gpr_names[X_S2]);
342
0
    }
343
0
  else if (reg_list > 6)
344
0
    {
345
0
      info->fprintf_styled_func (info->stream, dis_style_text, ",");
346
0
      info->fprintf_styled_func (info->stream, dis_style_register,
347
0
         "%s", pd->riscv_gpr_names[r_start]);
348
0
      info->fprintf_styled_func (info->stream, dis_style_text, "-");
349
0
      info->fprintf_styled_func (info->stream, dis_style_register,
350
0
         "%s", pd->riscv_gpr_names[reg_list + 11]);
351
0
    }
352
0
}
353
354
/* Get Zcmp sp adjustment immediate.  */
355
356
static int
357
riscv_get_spimm (insn_t l, int xlen)
358
0
{
359
0
  int spimm = riscv_get_sp_base(l, xlen);
360
0
  spimm += EXTRACT_ZCMP_SPIMM (l);
361
0
  if (((l ^ MATCH_CM_PUSH) & MASK_CM_PUSH) == 0)
362
0
    spimm *= -1;
363
0
  return spimm;
364
0
}
365
366
/* Get s-register regno by using sreg number.
367
   e.g. the regno of s0 is 8, so
368
   riscv_zcmp_get_sregno (0) equals 8.  */
369
370
static unsigned
371
riscv_zcmp_get_sregno (unsigned sreg_idx)
372
0
{
373
0
  return sreg_idx > 1 ?
374
0
      sreg_idx + 16 : sreg_idx + 8;
375
0
}
376
377
/* Print insn arguments for 32/64-bit code.  */
378
379
static void
380
print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info)
381
1.69M
{
382
1.69M
  struct riscv_private_data *pd = info->private_data;
383
1.69M
  int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1;
384
1.69M
  int rd = (l >> OP_SH_RD) & OP_MASK_RD;
385
1.69M
  fprintf_styled_ftype print = info->fprintf_styled_func;
386
1.69M
  const char *opargStart;
387
388
1.69M
  if (*oparg != '\0')
389
1.34M
    print (info->stream, dis_style_text, "\t");
390
391
8.37M
  for (; *oparg != '\0'; oparg++)
392
6.68M
    {
393
6.68M
      opargStart = oparg;
394
6.68M
      switch (*oparg)
395
6.68M
  {
396
2.93M
  case 'C': /* RVC */
397
2.93M
    switch (*++oparg)
398
2.93M
      {
399
442k
      case 's': /* RS1 x8-x15.  */
400
471k
      case 'w': /* RS1 x8-x15.  */
401
471k
        print (info->stream, dis_style_register, "%s",
402
471k
         pd->riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]);
403
471k
        break;
404
412k
      case 't': /* RS2 x8-x15.  */
405
412k
      case 'x': /* RS2 x8-x15.  */
406
412k
        print (info->stream, dis_style_register, "%s",
407
412k
         pd->riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
408
412k
        break;
409
180k
      case 'U': /* RS1, constrained to equal RD.  */
410
180k
        print (info->stream, dis_style_register,
411
180k
         "%s", pd->riscv_gpr_names[rd]);
412
180k
        break;
413
427k
      case 'c': /* RS1, constrained to equal sp.  */
414
427k
        print (info->stream, dis_style_register, "%s",
415
427k
         pd->riscv_gpr_names[X_SP]);
416
427k
        break;
417
103k
      case 'V': /* RS2 */
418
103k
        print (info->stream, dis_style_register, "%s",
419
103k
         pd->riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]);
420
103k
        break;
421
90.2k
      case 'o':
422
179k
      case 'j':
423
179k
        if (((l & MASK_C_ADDI) == MATCH_C_ADDI) && rd != 0)
424
64.4k
    maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 0);
425
179k
        if (pd->xlen == 64
426
179k
      && ((l & MASK_C_ADDIW) == MATCH_C_ADDIW) && rd != 0)
427
45.7k
    maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 1);
428
179k
        print (info->stream, dis_style_immediate, "%d",
429
179k
         (int)EXTRACT_CITYPE_IMM (l));
430
179k
        break;
431
93.7k
      case 'k':
432
93.7k
        print (info->stream, dis_style_address_offset, "%d",
433
93.7k
         (int)EXTRACT_CLTYPE_LW_IMM (l));
434
93.7k
        break;
435
252k
      case 'l':
436
252k
        print (info->stream, dis_style_address_offset, "%d",
437
252k
         (int)EXTRACT_CLTYPE_LD_IMM (l));
438
252k
        break;
439
38.1k
      case 'm':
440
38.1k
        print (info->stream, dis_style_address_offset, "%d",
441
38.1k
         (int)EXTRACT_CITYPE_LWSP_IMM (l));
442
38.1k
        break;
443
84.6k
      case 'n':
444
84.6k
        print (info->stream, dis_style_address_offset, "%d",
445
84.6k
         (int)EXTRACT_CITYPE_LDSP_IMM (l));
446
84.6k
        break;
447
183k
      case 'K':
448
183k
        print (info->stream, dis_style_immediate, "%d",
449
183k
         (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l));
450
183k
        break;
451
12.0k
      case 'L':
452
12.0k
        print (info->stream, dis_style_immediate, "%d",
453
12.0k
         (int)EXTRACT_CITYPE_ADDI16SP_IMM (l));
454
12.0k
        break;
455
32.2k
      case 'M':
456
32.2k
        print (info->stream, dis_style_address_offset, "%d",
457
32.2k
         (int)EXTRACT_CSSTYPE_SWSP_IMM (l));
458
32.2k
        break;
459
65.2k
      case 'N':
460
65.2k
        print (info->stream, dis_style_address_offset, "%d",
461
65.2k
         (int)EXTRACT_CSSTYPE_SDSP_IMM (l));
462
65.2k
        break;
463
64.5k
      case 'p':
464
64.5k
        info->target = EXTRACT_CBTYPE_IMM (l) + pc;
465
64.5k
        (*info->print_address_func) (info->target, info);
466
64.5k
        break;
467
27.8k
      case 'a':
468
27.8k
        info->target = EXTRACT_CJTYPE_IMM (l) + pc;
469
27.8k
        (*info->print_address_func) (info->target, info);
470
27.8k
        break;
471
49.0k
      case 'u':
472
49.0k
        print (info->stream, dis_style_immediate, "0x%x",
473
49.0k
         (unsigned)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1)));
474
49.0k
        break;
475
113k
      case '>':
476
113k
        print (info->stream, dis_style_immediate, "0x%x",
477
113k
         (unsigned)EXTRACT_CITYPE_IMM (l) & 0x3f);
478
113k
        break;
479
0
      case '<':
480
0
        print (info->stream, dis_style_immediate, "0x%x",
481
0
         (unsigned)EXTRACT_CITYPE_IMM (l) & 0x1f);
482
0
        break;
483
23.5k
      case 'T': /* Floating-point RS2.  */
484
23.5k
        print (info->stream, dis_style_register, "%s",
485
23.5k
         pd->riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]);
486
23.5k
        break;
487
121k
      case 'D': /* Floating-point RS2 x8-x15.  */
488
121k
        print (info->stream, dis_style_register, "%s",
489
121k
         pd->riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
490
121k
        break;
491
2.93M
      }
492
2.93M
    break;
493
494
2.93M
  case 'V': /* RVV */
495
0
    switch (*++oparg)
496
0
      {
497
0
      case 'd':
498
0
      case 'f':
499
0
        print (info->stream, dis_style_register, "%s",
500
0
         riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
501
0
        break;
502
0
      case 'e':
503
0
        if (!EXTRACT_OPERAND (VWD, l))
504
0
    print (info->stream, dis_style_register, "%s",
505
0
           pd->riscv_gpr_names[0]);
506
0
        else
507
0
    print (info->stream, dis_style_register, "%s",
508
0
           riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
509
0
        break;
510
0
      case 's':
511
0
        print (info->stream, dis_style_register, "%s",
512
0
         riscv_vecr_names_numeric[EXTRACT_OPERAND (VS1, l)]);
513
0
        break;
514
0
      case 't':
515
0
      case 'u': /* VS1 == VS2 already verified at this point.  */
516
0
      case 'v': /* VD == VS1 == VS2 already verified at this point.  */
517
0
        print (info->stream, dis_style_register, "%s",
518
0
         riscv_vecr_names_numeric[EXTRACT_OPERAND (VS2, l)]);
519
0
        break;
520
0
      case 'q':
521
0
        {
522
0
    unsigned int vs2 = EXTRACT_OPERAND (VS2, l);
523
0
    print (info->stream, dis_style_register, "%s",
524
0
           riscv_vecr_names_numeric[vs2 & 0x18]);
525
0
        }
526
0
        break;
527
0
      case 'r':
528
0
        print (info->stream, dis_style_immediate, "%u",
529
0
         (unsigned int) (EXTRACT_OPERAND (VS2, l) & 0x7) * 8);
530
0
        break;
531
0
      case '0':
532
0
        print (info->stream, dis_style_register, "%s",
533
0
         riscv_vecr_names_numeric[0]);
534
0
        break;
535
0
      case 'b':
536
0
      case 'c':
537
0
        {
538
0
    int imm = (*oparg == 'b') ? EXTRACT_RVV_VB_IMM (l)
539
0
            : EXTRACT_RVV_VC_IMM (l);
540
0
    unsigned int imm_vlmul = EXTRACT_OPERAND (VLMUL, imm);
541
0
    unsigned int imm_vsew = EXTRACT_OPERAND (VSEW, imm);
542
0
    unsigned int imm_vta = EXTRACT_OPERAND (VTA, imm);
543
0
    unsigned int imm_vma = EXTRACT_OPERAND (VMA, imm);
544
0
    unsigned int imm_vtype_altfmt
545
0
      = EXTRACT_OPERAND (VTYPE_ALTFMT, imm);
546
0
    bool imm_vtype_altfmt_supported
547
0
      = (imm_vtype_altfmt
548
0
         && riscv_vtype_altfmt_supported (pd));
549
0
    unsigned int imm_vtype_res = (imm >> 9);
550
551
0
    if (imm_vsew < ARRAY_SIZE (riscv_vsew)
552
0
        && imm_vlmul < ARRAY_SIZE (riscv_vlmul)
553
0
        && imm_vta < ARRAY_SIZE (riscv_vta)
554
0
        && imm_vma < ARRAY_SIZE (riscv_vma)
555
0
        && !imm_vtype_res
556
0
        && (imm_vtype_altfmt == 0
557
0
      || (imm_vtype_altfmt_supported
558
0
          && imm_vsew < ARRAY_SIZE (riscv_vsew_altfmt)))
559
0
        && riscv_vsew[imm_vsew] != NULL
560
0
        && riscv_vlmul[imm_vlmul] != NULL)
561
0
      print (info->stream, dis_style_text, "%s,%s,%s,%s",
562
0
       imm_vtype_altfmt
563
0
       ? riscv_vsew_altfmt[imm_vsew]
564
0
       : riscv_vsew[imm_vsew],
565
0
       riscv_vlmul[imm_vlmul], riscv_vta[imm_vta],
566
0
       riscv_vma[imm_vma]);
567
0
    else
568
0
      print (info->stream, dis_style_immediate, "%d", imm);
569
0
        }
570
0
        break;
571
0
      case 'i':
572
0
        print (info->stream, dis_style_immediate, "%d",
573
0
         (int)EXTRACT_RVV_VI_IMM (l));
574
0
        break;
575
0
      case 'j':
576
0
        print (info->stream, dis_style_immediate, "%d",
577
0
         (int)EXTRACT_RVV_VI_UIMM (l));
578
0
        break;
579
0
      case 'k':
580
0
        print (info->stream, dis_style_immediate, "%d",
581
0
         (int)EXTRACT_RVV_OFFSET (l));
582
0
        break;
583
0
      case 'l':
584
0
        print (info->stream, dis_style_immediate, "%d",
585
0
         (int)EXTRACT_RVV_VI_UIMM6 (l));
586
0
        break;
587
0
      case 'm':
588
0
        if (!EXTRACT_OPERAND (VMASK, l))
589
0
    {
590
0
      print (info->stream, dis_style_text, ",");
591
0
      print (info->stream, dis_style_register, "%s",
592
0
       riscv_vecm_names_numeric[0]);
593
0
    }
594
0
        break;
595
0
      }
596
0
    break;
597
598
1.76M
  case ',':
599
2.35M
  case '(':
600
2.95M
  case ')':
601
2.95M
  case '[':
602
2.95M
  case ']':
603
2.95M
  case '{':
604
2.95M
  case '}':
605
2.95M
    print (info->stream, dis_style_text, "%c", *oparg);
606
2.95M
    break;
607
608
1.68k
  case '0':
609
    /* Only print constant 0 if it is the last argument.  */
610
1.68k
    if (!oparg[1])
611
0
      print (info->stream, dis_style_immediate, "0");
612
1.68k
    break;
613
614
0
  case 'r':
615
0
    print (info->stream, dis_style_register, "%s",
616
0
     pd->riscv_gpr_names[EXTRACT_OPERAND (RS3, l)]);
617
0
    break;
618
619
56.0k
  case 's':
620
56.0k
    if ((l & MASK_JALR) == MATCH_JALR)
621
3.80k
      maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
622
56.0k
    print (info->stream, dis_style_register, "%s",
623
56.0k
     pd->riscv_gpr_names[rs1]);
624
56.0k
    break;
625
626
20.0k
  case 't':
627
20.0k
    print (info->stream, dis_style_register, "%s",
628
20.0k
     pd->riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]);
629
20.0k
    break;
630
631
18.2k
  case 'u':
632
18.2k
    print (info->stream, dis_style_immediate, "0x%x",
633
18.2k
     (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS);
634
18.2k
    break;
635
636
19.9k
  case 'm':
637
19.9k
    if (EXTRACT_OPERAND (RM, l) == OP_MASK_RM)
638
1.87k
      break;
639
18.0k
    print (info->stream, dis_style_text, ",");
640
    /* Fall through. */
641
18.0k
  case 'M':
642
18.0k
    arg_print (info, EXTRACT_OPERAND (RM, l),
643
18.0k
         riscv_rm, ARRAY_SIZE (riscv_rm));
644
18.0k
    break;
645
646
945
  case 'P':
647
945
    arg_print (info, EXTRACT_OPERAND (PRED, l),
648
945
         riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
649
945
    break;
650
651
945
  case 'Q':
652
945
    arg_print (info, EXTRACT_OPERAND (SUCC, l),
653
945
         riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
654
945
    break;
655
656
18.2k
  case 'o':
657
18.2k
    maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
658
    /* Fall through.  */
659
26.3k
  case 'j':
660
26.3k
    if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0)
661
24.8k
        || (l & MASK_JALR) == MATCH_JALR)
662
4.29k
      maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
663
26.3k
    if (pd->xlen == 64
664
26.3k
        && ((l & MASK_ADDIW) == MATCH_ADDIW) && rs1 != 0)
665
2.15k
      maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 1);
666
26.3k
    print (info->stream, dis_style_immediate, "%d",
667
26.3k
     (int)EXTRACT_ITYPE_IMM (l));
668
26.3k
    break;
669
670
12.0k
  case 'q':
671
12.0k
    maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l), 0);
672
12.0k
    print (info->stream, dis_style_address_offset, "%d",
673
12.0k
     (int)EXTRACT_STYPE_IMM (l));
674
12.0k
    break;
675
676
11.4k
  case 'a':
677
11.4k
    info->target = EXTRACT_JTYPE_IMM (l) + pc;
678
11.4k
    (*info->print_address_func) (info->target, info);
679
11.4k
    break;
680
681
8.49k
  case 'p':
682
8.49k
    info->target = EXTRACT_BTYPE_IMM (l) + pc;
683
8.49k
    (*info->print_address_func) (info->target, info);
684
8.49k
    break;
685
686
471k
  case 'd':
687
471k
    if ((l & MASK_AUIPC) == MATCH_AUIPC)
688
8.76k
      pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l);
689
462k
    else if ((l & MASK_LUI) == MATCH_LUI)
690
9.52k
      pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l);
691
453k
    else if ((l & MASK_C_LUI) == MATCH_C_LUI)
692
49.0k
      pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l);
693
471k
    print (info->stream, dis_style_register, "%s",
694
471k
     pd->riscv_gpr_names[rd]);
695
471k
    break;
696
697
0
  case 'y':
698
0
    print (info->stream, dis_style_immediate, "0x%x",
699
0
     EXTRACT_OPERAND (BS, l));
700
0
    break;
701
702
0
  case 'z':
703
0
    print (info->stream, dis_style_register, "%s",
704
0
     pd->riscv_gpr_names[0]);
705
0
    break;
706
707
362
  case '>':
708
362
    print (info->stream, dis_style_immediate, "0x%x",
709
362
     EXTRACT_OPERAND (SHAMT, l));
710
362
    break;
711
712
278
  case '<':
713
278
    print (info->stream, dis_style_immediate, "0x%x",
714
278
     EXTRACT_OPERAND (SHAMTW, l));
715
278
    break;
716
717
20.0k
  case 'S':
718
20.1k
  case 'U':
719
20.1k
    print (info->stream, dis_style_register, "%s",
720
20.1k
     pd->riscv_fpr_names[rs1]);
721
20.1k
    break;
722
723
24.5k
  case 'T':
724
24.5k
    print (info->stream, dis_style_register, "%s",
725
24.5k
     pd->riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]);
726
24.5k
    break;
727
728
62.5k
  case 'D':
729
62.5k
    print (info->stream, dis_style_register, "%s",
730
62.5k
     pd->riscv_fpr_names[rd]);
731
62.5k
    break;
732
733
18.5k
  case 'R':
734
18.5k
    print (info->stream, dis_style_register, "%s",
735
18.5k
     pd->riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]);
736
18.5k
    break;
737
738
10.3k
  case 'E':
739
10.3k
    {
740
10.3k
      static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs.  */
741
10.3k
      static bool init_csr = false;
742
10.3k
      unsigned int csr = EXTRACT_OPERAND (CSR, l);
743
744
10.3k
      if (!init_csr)
745
10.3k
        {
746
10.3k
    unsigned int i;
747
42.2M
    for (i = 0; i < 4096; i++)
748
42.2M
      riscv_csr_hash[i] = NULL;
749
750
10.3k
#define DECLARE_CSR(name, num, class)       \
751
4.73M
    if (riscv_csr_hash[num] == NULL)     \
752
4.73M
      riscv_csr_hash[num] = #name;
753
10.3k
#define DECLARE_CSR_ALIAS(name, num, class)     \
754
154k
    DECLARE_CSR (name, num, class)
755
10.3k
#include "opcode/riscv-opc.h"
756
10.3k
#undef DECLARE_CSR
757
10.3k
        }
758
759
10.3k
      if (riscv_csr_hash[csr] != NULL)
760
2.81k
        if (riscv_subset_supports (&pd->riscv_rps_dis, "xtheadvector")
761
0
      && (csr == CSR_VSTART
762
0
          || csr == CSR_VXSAT
763
0
          || csr == CSR_VXRM
764
0
          || csr == CSR_VL
765
0
          || csr == CSR_VTYPE
766
0
          || csr == CSR_VLENB))
767
0
    print (info->stream, dis_style_register, "%s",
768
0
           concat ("th.", riscv_csr_hash[csr], NULL));
769
2.81k
        else
770
2.81k
    print (info->stream, dis_style_register, "%s",
771
2.81k
           riscv_csr_hash[csr]);
772
7.49k
      else
773
7.49k
        print (info->stream, dis_style_immediate, "0x%x", csr);
774
10.3k
      break;
775
20.0k
    }
776
777
0
  case 'Y':
778
0
    print (info->stream, dis_style_immediate, "0x%x",
779
0
     EXTRACT_OPERAND (RNUM, l));
780
0
    break;
781
782
7.06k
  case 'Z':
783
7.06k
    print (info->stream, dis_style_immediate, "%d", rs1);
784
7.06k
    break;
785
786
0
  case 'W': /* Various operands for standard z extensions.  */
787
0
    switch (*++oparg)
788
0
      {
789
0
      case 'i':
790
0
        switch (*++oparg)
791
0
    {
792
0
    case 'f':
793
0
      print (info->stream, dis_style_address_offset, "%d",
794
0
       (int) EXTRACT_STYPE_IMM (l));
795
0
      break;
796
0
    default:
797
0
      goto undefined_modifier;
798
0
    }
799
0
        break;
800
0
      case 'f':
801
0
        switch (*++oparg)
802
0
    {
803
0
    case 'v':
804
0
      if (riscv_fli_symval[rs1])
805
0
        print (info->stream, dis_style_text, "%s",
806
0
         riscv_fli_symval[rs1]);
807
0
      else
808
0
        print (info->stream, dis_style_immediate, "%a",
809
0
         riscv_fli_numval[rs1]);
810
0
      break;
811
0
    default:
812
0
      goto undefined_modifier;
813
0
    }
814
0
        break;
815
0
      case 'c': /* Zcb extension 16 bits length instruction fields. */
816
0
        switch (*++oparg)
817
0
    {
818
0
    case '1':
819
0
        print (info->stream, dis_style_register, "%s",
820
0
          pd->riscv_gpr_names[riscv_zcmp_get_sregno (EXTRACT_OPERAND (SREG1, l))]);
821
0
        break;
822
0
    case '2':
823
0
        print (info->stream, dis_style_register, "%s",
824
0
          pd->riscv_gpr_names[riscv_zcmp_get_sregno (EXTRACT_OPERAND (SREG2, l))]);
825
0
        break;
826
0
    case 'b':
827
0
      print (info->stream, dis_style_immediate, "%d",
828
0
       (int)EXTRACT_ZCB_BYTE_UIMM (l));
829
0
      break;
830
0
    case 'h':
831
0
      print (info->stream, dis_style_immediate, "%d",
832
0
       (int)EXTRACT_ZCB_HALFWORD_UIMM (l));
833
0
      break;
834
0
    case 'r':
835
0
      print_reg_list (info, l);
836
0
      break;
837
0
    case 'p':
838
0
      print (info->stream, dis_style_immediate, "%d",
839
0
       riscv_get_spimm (l, pd->xlen));
840
0
      break;
841
0
    case 'i':
842
0
    case 'I':
843
0
      print (info->stream, dis_style_address_offset,
844
0
       "%" PRIu64, EXTRACT_ZCMT_INDEX (l));
845
0
      maybe_print_jvt_address (pd, EXTRACT_ZCMT_INDEX (l));
846
0
      break;
847
0
    default:
848
0
      goto undefined_modifier;
849
0
    }
850
0
        break;
851
0
      default:
852
0
        goto undefined_modifier;
853
0
      }
854
0
    break;
855
856
0
  case 'X': /* Vendor-specific operands.  */
857
0
    switch (*++oparg)
858
0
      {
859
0
      case 't': /* Vendor-specific (T-head) operands.  */
860
0
        {
861
0
    size_t n;
862
0
    size_t s;
863
0
    bool sign;
864
0
    switch (*++oparg)
865
0
      {
866
0
      case 'V':
867
0
       ++oparg;
868
0
       if (*oparg != 'c')
869
0
          goto undefined_modifier;
870
871
0
        int imm = (*oparg == 'b') ? EXTRACT_RVV_VB_IMM (l)
872
0
                : EXTRACT_RVV_VC_IMM (l);
873
0
        unsigned int imm_vediv = EXTRACT_OPERAND (XTHEADVEDIV, imm);
874
0
        unsigned int imm_vlmul = EXTRACT_OPERAND (XTHEADVLMUL, imm);
875
0
        unsigned int imm_vsew = EXTRACT_OPERAND (XTHEADVSEW, imm);
876
0
        unsigned int imm_vtype_res
877
0
          = EXTRACT_OPERAND (XTHEADVTYPE_RES, imm);
878
0
        if (imm_vsew < ARRAY_SIZE (riscv_vsew)
879
0
      && imm_vlmul < ARRAY_SIZE (riscv_th_vlen)
880
0
      && imm_vediv < ARRAY_SIZE (riscv_th_vediv)
881
0
      && ! imm_vtype_res)
882
0
          print (info->stream, dis_style_text, "%s,%s,%s",
883
0
           riscv_vsew[imm_vsew], riscv_th_vlen[imm_vlmul],
884
0
           riscv_th_vediv[imm_vediv]);
885
0
        else
886
0
          print (info->stream, dis_style_immediate, "%d", imm);
887
0
        break;
888
0
      case 'l': /* Integer immediate, literal.  */
889
0
        oparg++;
890
0
        while (*oparg && *oparg != ',')
891
0
          {
892
0
      print (info->stream, dis_style_immediate, "%c", *oparg);
893
0
      oparg++;
894
0
          }
895
0
        oparg--;
896
0
        break;
897
0
      case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S.  */
898
0
        sign = true;
899
0
        goto print_imm;
900
0
      case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S.  */
901
0
        sign = false;
902
0
        goto print_imm;
903
0
      print_imm:
904
0
        n = strtol (oparg + 1, (char **)&oparg, 10);
905
0
        if (*oparg != '@')
906
0
          goto undefined_modifier;
907
0
        s = strtol (oparg + 1, (char **)&oparg, 10);
908
0
        oparg--;
909
910
0
        if (!sign)
911
0
          print (info->stream, dis_style_immediate, "%lu",
912
0
           (unsigned long)EXTRACT_U_IMM (n, s, l));
913
0
        else
914
0
          print (info->stream, dis_style_immediate, "%li",
915
0
           (signed long)EXTRACT_S_IMM (n, s, l));
916
0
        break;
917
0
      default:
918
0
        goto undefined_modifier;
919
0
      }
920
0
        }
921
0
        break;
922
0
      case 'c': /* Vendor-specific (CORE-V) operands.  */
923
0
        switch (*++oparg)
924
0
    {
925
0
      case '2':
926
0
        print (info->stream, dis_style_immediate, "%d",
927
0
      ((int) EXTRACT_CV_IS2_UIMM5 (l)));
928
0
        break;
929
0
      case '3':
930
0
        print (info->stream, dis_style_immediate, "%d",
931
0
      ((int) EXTRACT_CV_IS3_UIMM5 (l)));
932
0
        break;
933
0
      case '4':
934
0
        print (info->stream, dis_style_immediate, "%d",
935
0
         ((int) EXTRACT_CV_BI_IMM5 (l)));
936
0
        break;
937
0
      case '5':
938
0
        print (info->stream, dis_style_immediate, "%d",
939
0
         ((int) EXTRACT_CV_SIMD_IMM6 (l)));
940
0
        break;
941
0
      case '6':
942
0
        print (info->stream, dis_style_immediate, "%d",
943
0
         ((int) EXTRACT_CV_BITMANIP_UIMM5 (l)));
944
0
        break;
945
0
      case '7':
946
0
        print (info->stream, dis_style_immediate, "%d",
947
0
         ((int) EXTRACT_CV_BITMANIP_UIMM2 (l)));
948
0
        break;
949
0
      case '8':
950
0
        print (info->stream, dis_style_immediate, "%d",
951
0
         ((int) EXTRACT_CV_SIMD_UIMM6 (l)));
952
0
        ++oparg;
953
0
        break;
954
0
      default:
955
0
        goto undefined_modifier;
956
0
    }
957
0
        break;
958
0
      case 's': /* Vendor-specific (SiFive) operands.  */
959
0
        switch (*++oparg)
960
0
    {
961
    /* SiFive vector coprocessor interface.  */
962
0
    case 'd':
963
0
      print (info->stream, dis_style_register, "0x%x",
964
0
       (unsigned) EXTRACT_OPERAND (RD, l));
965
0
      break;
966
0
    case 't':
967
0
      print (info->stream, dis_style_register, "0x%x",
968
0
       (unsigned) EXTRACT_OPERAND (RS2, l));
969
0
      break;
970
0
    case 'O':
971
0
      switch (*++oparg)
972
0
        {
973
0
        case '2':
974
0
          print (info->stream, dis_style_register, "0x%x",
975
0
           (unsigned) EXTRACT_OPERAND (XSO2, l));
976
0
          break;
977
0
        case '1':
978
0
          print (info->stream, dis_style_register, "0x%x",
979
0
           (unsigned) EXTRACT_OPERAND (XSO1, l));
980
0
          break;
981
0
        }
982
0
      break;
983
0
    }
984
0
        break;
985
0
      case 'm': /* Vendor-specific (MIPS) operands.  */
986
0
        switch (*++oparg)
987
0
    {
988
0
    case '@':
989
0
      print (info->stream, dis_style_register, "0x%x",
990
0
       (unsigned) EXTRACT_OPERAND (MIPS_HINT, l));
991
0
      break;
992
0
    case '#':
993
0
      print (info->stream, dis_style_register, "0x%x",
994
0
       (unsigned) EXTRACT_OPERAND (MIPS_IMM9, l));
995
0
      break;
996
0
    case '$':
997
0
      print (info->stream, dis_style_immediate, "%d",
998
0
       (unsigned)EXTRACT_MIPS_LDP_IMM (l));
999
0
      break;
1000
0
    case '%':
1001
0
      print (info->stream, dis_style_immediate, "%d",
1002
0
       (unsigned)EXTRACT_MIPS_LWP_IMM (l));
1003
0
      break;
1004
0
    case '^':
1005
0
      print (info->stream, dis_style_immediate, "%d",
1006
0
       (unsigned)EXTRACT_MIPS_SDP_IMM (l));
1007
0
      break;
1008
0
    case '&':
1009
0
      print (info->stream, dis_style_immediate, "%d",
1010
0
       (unsigned)EXTRACT_MIPS_SWP_IMM (l));
1011
0
      break;
1012
0
    default:
1013
0
      goto undefined_modifier;
1014
0
    }
1015
0
        break;
1016
0
      case 'p': /* Vendor-specific (SpacemiT) operands.  */
1017
0
        switch (*++oparg)
1018
0
    {
1019
0
    case 'V':
1020
0
      switch (*++oparg)
1021
0
        {
1022
0
        case 'd':
1023
0
          {
1024
0
      unsigned vd = EXTRACT_OPERAND (SPACEMIT_IME_VD, l) * 2;
1025
0
      print (info->stream, dis_style_register, "%s",
1026
0
             riscv_vecr_names_numeric[vd]);
1027
0
          }
1028
0
          break;
1029
0
        case 's':
1030
0
          {
1031
0
      unsigned vs = EXTRACT_OPERAND (SPACEMIT_IME_VS1, l) * 2;
1032
0
      print (info->stream, dis_style_register, "%s",
1033
0
             riscv_vecr_names_numeric[vs]);
1034
0
          }
1035
0
          break;
1036
0
        case 'm':
1037
0
          {
1038
0
      unsigned vm = EXTRACT_OPERAND (SPACEMIT_IME_VMASK, l);
1039
0
      print (info->stream, dis_style_register, "%s",
1040
0
             riscv_vecr_names_numeric[vm]);
1041
0
          }
1042
0
          break;
1043
0
        default:
1044
0
          goto undefined_modifier;
1045
0
        }
1046
0
      break;
1047
0
    case 'n': /* Xpn/Xpb: uimm2 stride, encoded across bits 7,15.  */
1048
0
    case 'b':
1049
0
      {
1050
0
        unsigned sp = ((l >> 7) & 1) | (((l >> 15) & 1) << 1);
1051
0
        print (info->stream, dis_style_immediate, "%u", sp);
1052
0
      }
1053
0
      break;
1054
0
    case 'u': /* XpuN@S: N-bit unsigned immediate at bit S.  */
1055
0
      {
1056
0
        long n = strtol (oparg + 1, (char **)&oparg, 10);
1057
0
        if (*oparg != '@')
1058
0
          goto undefined_modifier;
1059
0
        long s = strtol (oparg + 1, (char **)&oparg, 10);
1060
0
        oparg--;
1061
0
        unsigned val = (l >> s) & ((1U << n) - 1);
1062
0
        print (info->stream, dis_style_immediate, "%u", val);
1063
0
      }
1064
0
      break;
1065
0
    case 'w':
1066
      /* Xpw: optional data-width suffix, i8 only.  WI==3 (i8)
1067
         is the default and is omitted from the output.  */
1068
0
      {
1069
0
        unsigned wi = EXTRACT_OPERAND (SPACEMIT_IME_WI, l);
1070
0
        if (wi != 3)
1071
0
          goto undefined_modifier;
1072
0
      }
1073
0
      break;
1074
0
    case 'x':
1075
      /* Xpx: optional data-width suffix, i4 or i8.  WI==3 (i8)
1076
         is the default and is omitted from the output.  */
1077
0
      {
1078
0
        unsigned wi = EXTRACT_OPERAND (SPACEMIT_IME_WI, l);
1079
0
        if (wi == 2)
1080
0
          print (info->stream, dis_style_text, ",i4");
1081
0
        else if (wi != 3)
1082
0
          goto undefined_modifier;
1083
0
      }
1084
0
      break;
1085
0
    default:
1086
0
      goto undefined_modifier;
1087
0
    }
1088
0
        break;
1089
0
      default:
1090
0
        goto undefined_modifier;
1091
0
      }
1092
0
    break;
1093
1094
0
  default:
1095
0
  undefined_modifier:
1096
    /* xgettext:c-format */
1097
0
    print (info->stream, dis_style_text,
1098
0
     _("# internal error, undefined modifier (%c)"),
1099
0
     *opargStart);
1100
0
    return;
1101
6.68M
  }
1102
6.68M
    }
1103
1.69M
}
1104
1105
/* Print the RISC-V instruction at address MEMADDR in debugged memory,
1106
   on using INFO.  Returns length of the instruction, in bytes.
1107
   BIGENDIAN must be 1 if this is big-endian code, 0 if
1108
   this is little-endian code.  */
1109
1110
static int
1111
riscv_disassemble_insn (bfd_vma memaddr,
1112
      insn_t word,
1113
      const bfd_byte *packet,
1114
      disassemble_info *info)
1115
2.15M
{
1116
2.15M
  const struct riscv_opcode *op;
1117
2.15M
  static bool init = false;
1118
2.15M
  static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
1119
2.15M
  struct riscv_private_data *pd = info->private_data;
1120
2.15M
  int insnlen, i;
1121
2.15M
  bool printed;
1122
1123
2.16M
#define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
1124
1125
  /* Build a hash table to shorten the search time.  */
1126
2.15M
  if (! init)
1127
2
    {
1128
5.84k
      for (op = riscv_opcodes; op->name; op++)
1129
5.84k
  if (!riscv_hash[OP_HASH_IDX (op->match)])
1130
60
    riscv_hash[OP_HASH_IDX (op->match)] = op;
1131
1132
2
      init = true;
1133
2
    }
1134
1135
2.15M
  insnlen = riscv_insn_length (word);
1136
1137
  /* RISC-V instructions are always little-endian.  */
1138
2.15M
  info->endian_code = BFD_ENDIAN_LITTLE;
1139
1140
2.15M
  info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2;
1141
2.15M
  info->bytes_per_line = 8;
1142
  /* We don't support constant pools, so this must be code.  */
1143
2.15M
  info->display_endian = info->endian_code;
1144
2.15M
  info->insn_info_valid = 1;
1145
2.15M
  info->branch_delay_insns = 0;
1146
2.15M
  info->data_size = 0;
1147
2.15M
  info->insn_type = dis_nonbranch;
1148
2.15M
  info->target = 0;
1149
2.15M
  info->target2 = 0;
1150
1151
2.15M
  op = riscv_hash[OP_HASH_IDX (word)];
1152
2.15M
  if (op != NULL)
1153
2.09M
    {
1154
      /* If XLEN is not known, get its value from the ELF class.  */
1155
2.09M
      riscv_set_xlen_from_elf (pd, info);
1156
1157
      /* If arch has the Zfinx extension, replace FPR with GPR.  */
1158
2.09M
      if (riscv_subset_supports (&pd->riscv_rps_dis, "zfinx"))
1159
0
  pd->riscv_fpr_names = pd->riscv_gpr_names;
1160
2.09M
      else
1161
2.09M
  pd->riscv_fpr_names = pd->riscv_gpr_names == riscv_gpr_names_abi ?
1162
2.09M
            riscv_fpr_names_abi : riscv_fpr_names_numeric;
1163
1164
1.30G
      for (; op->name; op++)
1165
1.29G
  {
1166
    /* Ignore macro insns.  */
1167
1.29G
    if (op->pinfo == INSN_MACRO)
1168
33.2M
      continue;
1169
    /* Does the opcode match?  */
1170
1.26G
    if (! (op->match_func) (op, word))
1171
1.26G
      continue;
1172
    /* Is this a pseudo-instruction and may we print it as such?  */
1173
1.91M
    if (pd->no_aliases && (op->pinfo & INSN_ALIAS))
1174
0
      continue;
1175
    /* Is this instruction restricted to a certain value of XLEN?  */
1176
1.91M
    if ((op->xlen_requirement != 0)
1177
334k
        && (op->xlen_requirement != pd->xlen))
1178
58.2k
      continue;
1179
    /* Is this instruction supported by the current architecture?
1180
       Also recognize Zcmt when the JVT base symbol identifies its table.  */
1181
1.85M
    if (!pd->all_ext)
1182
1.85M
      {
1183
1.85M
        if (!riscv_multi_subset_supports (&pd->riscv_rps_dis,
1184
1.85M
            op->insn_class)
1185
166k
      && !(pd->has_jvt_base && op->insn_class == INSN_CLASS_ZCMT))
1186
166k
    continue;
1187
1188
1.69M
        if (pd->xlen == 32
1189
0
      && riscv_subset_supports (&pd->riscv_rps_dis, "zdinx"))
1190
0
    {
1191
0
      if ((op->pinfo & INSN_RV32_EVEN_D)
1192
0
          && (word & (1u << OP_SH_RD)))
1193
0
        continue;
1194
0
      if ((op->pinfo & INSN_RV32_EVEN_S)
1195
0
          && (word & (1u << OP_SH_RS1)))
1196
0
        continue;
1197
0
      if ((op->pinfo & INSN_RV32_EVEN_T)
1198
0
          && (word & (1u << OP_SH_RS2)))
1199
0
        continue;
1200
0
      if ((op->pinfo & INSN_RV32_EVEN_R)
1201
0
          && (word & (1u << OP_SH_RS3)))
1202
0
        continue;
1203
0
    }
1204
1205
1.69M
        if (pd->xlen <= 64
1206
1.69M
      && riscv_subset_supports (&pd->riscv_rps_dis, "zqinx"))
1207
0
    {
1208
0
      unsigned int mask = pd->xlen == 32 ? 3 : 1;
1209
1210
0
      if ((op->pinfo & INSN_RV64_EVEN_D)
1211
0
          && (word & (mask << OP_SH_RD)))
1212
0
        continue;
1213
0
      if ((op->pinfo & INSN_RV64_EVEN_S)
1214
0
          && (word & (mask << OP_SH_RS1)))
1215
0
        continue;
1216
0
      if ((op->pinfo & INSN_RV64_EVEN_T)
1217
0
          && (word & (mask << OP_SH_RS2)))
1218
0
        continue;
1219
0
      if ((op->pinfo & INSN_RV64_EVEN_R)
1220
0
          && (word & (mask << OP_SH_RS3)))
1221
0
        continue;
1222
0
    }
1223
1.69M
      }
1224
1225
    /* It's a match.  */
1226
1.69M
    (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic,
1227
1.69M
          "%s", op->name);
1228
1.69M
    print_insn_args (op->args, word, memaddr, info);
1229
1230
    /* Try to disassemble multi-instruction addressing sequences.  */
1231
1.69M
    if (pd->to_print_addr)
1232
28.8k
      {
1233
28.8k
        info->target = pd->print_addr;
1234
28.8k
        (*info->fprintf_styled_func)
1235
28.8k
    (info->stream, dis_style_comment_start, " # ");
1236
28.8k
        (*info->print_address_func) (info->target, info);
1237
28.8k
        pd->to_print_addr = false;
1238
28.8k
      }
1239
1240
    /* Finish filling out insn_info fields.  */
1241
1.69M
    switch (op->pinfo & INSN_TYPE)
1242
1.69M
      {
1243
32.5k
      case INSN_BRANCH:
1244
32.5k
        info->insn_type = dis_branch;
1245
32.5k
        break;
1246
73.0k
      case INSN_CONDBRANCH:
1247
73.0k
        info->insn_type = dis_condbranch;
1248
73.0k
        break;
1249
12.7k
      case INSN_JSR:
1250
12.7k
        info->insn_type = dis_jsr;
1251
12.7k
        break;
1252
596k
      case INSN_DREF:
1253
596k
        info->insn_type = dis_dref;
1254
596k
        break;
1255
976k
      default:
1256
976k
        break;
1257
1.69M
      }
1258
1259
1.69M
    if (op->pinfo & INSN_DATA_SIZE)
1260
596k
      {
1261
596k
        int size = ((op->pinfo & INSN_DATA_SIZE)
1262
596k
        >> INSN_DATA_SIZE_SHIFT);
1263
596k
        info->data_size = 1 << (size - 1);
1264
596k
      }
1265
1266
1.69M
    return insnlen;
1267
1.69M
  }
1268
2.09M
    }
1269
1270
  /* We did not find a match, so just print the instruction bits in
1271
     the shape of an assembler .insn directive.  */
1272
465k
  info->insn_type = dis_noninsn;
1273
465k
  (*info->fprintf_styled_func)
1274
465k
    (info->stream, dis_style_assembler_directive, ".insn");
1275
465k
  (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1276
465k
  (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1277
465k
        "%d", insnlen);
1278
465k
  (*info->fprintf_styled_func) (info->stream, dis_style_text, ", ");
1279
465k
  (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x");
1280
1.37M
  for (i = insnlen, printed = false; i >= 2; )
1281
912k
    {
1282
912k
      i -= 2;
1283
912k
      word = bfd_get_bits (packet + i, 16, false);
1284
912k
      if (!word && !printed && i)
1285
30.7k
  continue;
1286
1287
881k
      (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1288
881k
            "%04x", (unsigned int) word);
1289
881k
      printed = true;
1290
881k
    }
1291
1292
465k
  return insnlen;
1293
2.15M
}
1294
1295
/* Decide if we need to parse the architecture string again, also record the
1296
   string into the current subset list.  */
1297
1298
static void
1299
riscv_dis_parse_subset (struct disassemble_info *info, const char *arch_new)
1300
1.61k
{
1301
1.61k
  struct riscv_private_data *pd = info->private_data;
1302
1.61k
  const char *arch_subset_list = pd->riscv_rps_dis.subset_list->arch_str;
1303
1.61k
  if (arch_subset_list == NULL || strcmp (arch_subset_list, arch_new) != 0)
1304
1.61k
    {
1305
1.61k
      riscv_release_subset_list (pd->riscv_rps_dis.subset_list);
1306
1.61k
      riscv_parse_subset (&pd->riscv_rps_dis, arch_new);
1307
1.61k
      riscv_arch_str (pd->xlen, pd->riscv_rps_dis.subset_list,
1308
1.61k
          true/* update */);
1309
1.61k
    }
1310
1.61k
}
1311
1312
/* If we find the suitable mapping symbol update the STATE.
1313
   Otherwise, do nothing.  */
1314
1315
static void
1316
riscv_update_map_state (int n,
1317
      enum riscv_seg_mstate *state,
1318
      struct disassemble_info *info)
1319
0
{
1320
0
  struct riscv_private_data *pd = info->private_data;
1321
0
  const char *name;
1322
1323
  /* If the symbol is in a different section, ignore it.  */
1324
0
  if (info->section != NULL
1325
0
      && info->section != info->symtab[n]->section)
1326
0
    return;
1327
1328
0
  name = bfd_asymbol_name(info->symtab[n]);
1329
0
  if (strcmp (name, "$d") == 0)
1330
0
    *state = MAP_DATA;
1331
0
  else if (strcmp (name, "$x") == 0)
1332
0
    {
1333
0
      *state = MAP_INSN;
1334
0
      riscv_dis_parse_subset (info, pd->default_arch);
1335
0
    }
1336
0
  else if (strncmp (name, "$xrv", 4) == 0)
1337
0
    {
1338
0
      *state = MAP_INSN;
1339
1340
      /* ISA mapping string may be numbered, suffixed with '.n'. Do not
1341
   consider this as part of the ISA string.  */
1342
0
      const char *suffix = strchr (name, '.');
1343
0
      if (suffix)
1344
0
  {
1345
0
    int suffix_index = (int)(suffix - name);
1346
0
    char *name_substr = xmalloc (suffix_index + 1);
1347
0
    strncpy (name_substr, name, suffix_index);
1348
0
    name_substr[suffix_index] = '\0';
1349
0
    riscv_dis_parse_subset (info, name_substr + 2);
1350
0
    free (name_substr);
1351
0
  }
1352
0
      else
1353
0
  riscv_dis_parse_subset (info, name + 2);
1354
0
    }
1355
0
}
1356
1357
/* Return true if we find the suitable mapping symbol.
1358
   Otherwise, return false.  */
1359
1360
static bool
1361
riscv_is_valid_mapping_symbol (int n,
1362
           struct disassemble_info *info)
1363
1.64k
{
1364
1.64k
  const char *name;
1365
1366
  /* If the symbol is in a different section, ignore it.  */
1367
1.64k
  if (info->section != NULL
1368
1.64k
      && info->section != info->symtab[n]->section)
1369
892
    return false;
1370
1371
754
  name = bfd_asymbol_name(info->symtab[n]);
1372
754
  return riscv_elf_is_mapping_symbols (name);
1373
1.64k
}
1374
1375
/* Check the sorted symbol table (sorted by the symbol value), find the
1376
   suitable mapping symbols.  */
1377
1378
static enum riscv_seg_mstate
1379
riscv_search_mapping_symbol (bfd_vma memaddr,
1380
           struct disassemble_info *info)
1381
2.15M
{
1382
2.15M
  struct riscv_private_data *pd = info->private_data;
1383
2.15M
  enum riscv_seg_mstate mstate;
1384
2.15M
  bool from_last_map_symbol;
1385
2.15M
  bool found = false;
1386
2.15M
  int symbol = -1;
1387
2.15M
  int n;
1388
1389
  /* Return the last map state if the address is still within the range of the
1390
     last mapping symbol.  */
1391
2.15M
  if (pd->last_map_section == info->section
1392
2.15M
      && (memaddr < pd->last_map_symbol_boundary))
1393
0
    return pd->last_map_state;
1394
1395
2.15M
  pd->last_map_section = info->section;
1396
1397
  /* Decide whether to print the data or instruction by default, in case
1398
     we can not find the corresponding mapping symbols.  */
1399
2.15M
  mstate = MAP_DATA;
1400
2.15M
  if ((info->section
1401
1.43M
       && info->section->flags & SEC_CODE)
1402
2.15M
      || !info->section)
1403
721k
    mstate = MAP_INSN;
1404
1405
2.15M
  if (info->symtab_size == 0
1406
632
      || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour)
1407
2.15M
    return mstate;
1408
1409
  /* Reset the last_map_symbol if we start to dump a new section.  */
1410
632
  if (memaddr <= 0)
1411
9
    pd->last_map_symbol = -1;
1412
1413
  /* If the last stop offset is different from the current one, then
1414
     don't use the last_map_symbol to search.  We usually reset the
1415
     info->stop_offset when handling a new section.  */
1416
632
  from_last_map_symbol = (pd->last_map_symbol >= 0
1417
0
        && info->stop_offset == pd->last_stop_offset);
1418
1419
  /* Start scanning from wherever we finished last time, or the start
1420
     of the function.  */
1421
632
  n = from_last_map_symbol ? pd->last_map_symbol : info->symtab_pos + 1;
1422
1423
  /* Find the suitable mapping symbol to dump.  */
1424
1.66k
  for (; n < info->symtab_size; n++)
1425
1.42k
    {
1426
1.42k
      bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1427
      /* We have searched all possible symbols in the range.  */
1428
1.42k
      if (addr > memaddr)
1429
400
  break;
1430
1.02k
      if (riscv_is_valid_mapping_symbol (n, info))
1431
0
  {
1432
0
    symbol = n;
1433
0
    found = true;
1434
    /* Do not stop searching, in case there are some mapping
1435
       symbols have the same value, but have different names.
1436
       Use the last one.  */
1437
0
  }
1438
1.02k
    }
1439
1440
  /* We can not find the suitable mapping symbol above.  Therefore, we
1441
     look forwards and try to find it again, but don't go past the start
1442
     of the section.  Otherwise a data section without mapping symbols
1443
     can pick up a text mapping symbol of a preceeding section.  */
1444
632
  if (!found)
1445
632
    {
1446
632
      n = from_last_map_symbol ? pd->last_map_symbol : info->symtab_pos;
1447
1448
1.24k
      for (; n >= 0; n--)
1449
617
  {
1450
617
    bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1451
    /* We have searched all possible symbols in the range.  */
1452
617
    if (addr < (info->section ? info->section->vma : 0))
1453
0
      break;
1454
    /* Stop searching once we find the closed mapping symbol.  */
1455
617
    if (riscv_is_valid_mapping_symbol (n, info))
1456
0
      {
1457
0
        symbol = n;
1458
0
        found = true;
1459
0
        break;
1460
0
      }
1461
617
  }
1462
632
    }
1463
1464
632
  if (found)
1465
0
    {
1466
0
      riscv_update_map_state (symbol, &mstate, info);
1467
1468
      /* Find the next mapping symbol to determine the boundary of this mapping
1469
   symbol.  */
1470
1471
0
      bool found_next = false;
1472
      /* Try to found next mapping symbol.  */
1473
0
      for (n = symbol + 1; n < info->symtab_size; n++)
1474
0
  {
1475
0
    if (info->symtab[symbol]->section != info->symtab[n]->section)
1476
0
      continue;
1477
1478
0
    bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1479
0
    const char *sym_name = bfd_asymbol_name(info->symtab[n]);
1480
0
    if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd'))
1481
0
      {
1482
        /* The next mapping symbol has been found, and it represents the
1483
     boundary of this mapping symbol.  */
1484
0
        found_next = true;
1485
0
        pd->last_map_symbol_boundary = addr;
1486
0
        break;
1487
0
      }
1488
0
  }
1489
1490
      /* No further mapping symbol has been found, indicating that the boundary
1491
   of the current mapping symbol is the end of this section.  */
1492
0
      if (!found_next)
1493
0
  pd->last_map_symbol_boundary = info->section->vma
1494
0
               + info->section->size;
1495
0
    }
1496
1497
  /* Save the information for next use.  */
1498
632
  pd->last_map_symbol = symbol;
1499
632
  pd->last_stop_offset = info->stop_offset;
1500
1501
632
  return mstate;
1502
2.15M
}
1503
1504
/* Decide which data size we should print.  */
1505
1506
static bfd_vma
1507
riscv_data_length (bfd_vma memaddr,
1508
       disassemble_info *info)
1509
0
{
1510
0
  struct riscv_private_data *pd = info->private_data;
1511
0
  bfd_vma length;
1512
0
  bool found = false;
1513
1514
0
  length = 4;
1515
0
  if (info->symtab_size != 0
1516
0
      && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour
1517
0
      && pd->last_map_symbol >= 0)
1518
0
    {
1519
0
      int n;
1520
0
      enum riscv_seg_mstate m = MAP_NONE;
1521
0
      for (n = pd->last_map_symbol + 1; n < info->symtab_size; n++)
1522
0
  {
1523
0
    bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1524
0
    if (addr > memaddr
1525
0
        && riscv_is_valid_mapping_symbol (n, info))
1526
0
      {
1527
0
        if (addr - memaddr < length)
1528
0
    length = addr - memaddr;
1529
0
        found = true;
1530
0
        riscv_update_map_state (n, &m, info);
1531
0
        break;
1532
0
      }
1533
0
  }
1534
0
    }
1535
0
  if (!found)
1536
0
    {
1537
      /* Do not set the length which exceeds the section size.  */
1538
0
      bfd_vma offset = info->section->vma + info->section->size;
1539
0
      offset -= memaddr;
1540
0
      length = (offset < length) ? offset : length;
1541
0
    }
1542
0
  length = length == 3 ? 2 : length;
1543
0
  return length;
1544
0
}
1545
1546
/* Dump the data contents.  */
1547
1548
static int
1549
riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED,
1550
      insn_t data,
1551
      const bfd_byte *packet ATTRIBUTE_UNUSED,
1552
      disassemble_info *info)
1553
880
{
1554
880
  info->display_endian = info->endian;
1555
880
  int i;
1556
1557
880
  switch (info->bytes_per_chunk)
1558
880
    {
1559
395
    case 1:
1560
395
      info->bytes_per_line = 6;
1561
395
      (*info->fprintf_styled_func)
1562
395
  (info->stream, dis_style_assembler_directive, ".byte");
1563
395
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1564
395
      (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1565
395
            "0x%02x", (unsigned)data);
1566
395
      break;
1567
195
    case 2:
1568
195
      info->bytes_per_line = 8;
1569
195
      (*info->fprintf_styled_func)
1570
195
  (info->stream, dis_style_assembler_directive, ".short");
1571
195
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1572
195
      (*info->fprintf_styled_func)
1573
195
  (info->stream, dis_style_immediate, "0x%04x", (unsigned) data);
1574
195
      break;
1575
26
    case 4:
1576
26
      info->bytes_per_line = 8;
1577
26
      (*info->fprintf_styled_func)
1578
26
  (info->stream, dis_style_assembler_directive, ".word");
1579
26
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1580
26
      (*info->fprintf_styled_func)
1581
26
  (info->stream, dis_style_immediate, "0x%08lx",
1582
26
   (unsigned long) data);
1583
26
      break;
1584
25
    case 8:
1585
25
      info->bytes_per_line = 8;
1586
25
      (*info->fprintf_styled_func)
1587
25
  (info->stream, dis_style_assembler_directive, ".dword");
1588
25
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1589
25
      (*info->fprintf_styled_func)
1590
25
  (info->stream, dis_style_immediate, "0x%016llx",
1591
25
   (unsigned long long) data);
1592
25
      break;
1593
239
    default:
1594
      /* Arbitrary data so just print the bits in the shape of an .<N>byte
1595
   directive.  */
1596
239
      info->bytes_per_line = info->bytes_per_chunk;
1597
239
      (*info->fprintf_styled_func)
1598
239
  (info->stream, dis_style_assembler_directive, ".%dbyte", info->bytes_per_chunk);
1599
239
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1600
239
      (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x");
1601
1.98k
      for (i = info->bytes_per_line; i > 0;)
1602
1.74k
  {
1603
1.74k
    i--;
1604
1.74k
    data = bfd_get_bits (packet + i, 8, false);
1605
1.74k
    (*info->fprintf_styled_func)
1606
1.74k
      (info->stream, dis_style_immediate, "%02x",
1607
1.74k
        (unsigned) data);
1608
1.74k
  }
1609
239
      break;
1610
880
    }
1611
880
  return info->bytes_per_chunk;
1612
880
}
1613
1614
/* DATA is the function address in jump table entry.
1615
   Lookup the address in symbols and print the function name.  */
1616
static int
1617
riscv_disassemble_jvt (bfd_vma memaddr, insn_t data,
1618
           const bfd_byte *packet,
1619
           disassemble_info *info)
1620
0
{
1621
0
  struct riscv_private_data *pd = info->private_data;
1622
0
  if (pd == NULL || pd->xlen == 0 || info->section == NULL)
1623
0
    return -1;
1624
1625
0
  bfd_size_type entry_size = pd->xlen / 8;
1626
0
  bfd_vma offset = memaddr - info->section->vma;
1627
0
  bfd_size_type entry_offset = (offset / entry_size) * entry_size;
1628
0
  bfd_size_type entry_delta = offset - entry_offset;
1629
0
  int idx = entry_offset / entry_size;
1630
0
  if (idx < 0 || idx > (ZCMT_TOTAL_ENTRIES - 1))
1631
0
    opcodes_error_handler (_ ("invalid jvt index: %i"), idx);
1632
1633
0
  const char *inst_str;
1634
0
  if (idx <= ZCMT_JT_END)
1635
0
    inst_str = "jvt.jt";
1636
0
  else
1637
0
    inst_str = "jvt.jalt";
1638
1639
0
  if (pd->jvt_content != NULL && entry_offset + entry_size <= pd->jvt_size)
1640
0
    packet = pd->jvt_content + entry_offset;
1641
0
  data = bfd_get_bits (packet, pd->xlen,
1642
0
           bfd_big_endian (info->section->owner));
1643
1644
0
  const char *funcname = "<unknown>";
1645
0
  for (int i = 0; i < info->symtab_size; i++)
1646
0
    {
1647
0
      bfd_vma addr = bfd_asymbol_value (info->symtab[i]);
1648
0
      if (addr == (bfd_vma) data)
1649
0
  {
1650
0
    funcname = info->symtab[i]->name;
1651
0
    break;
1652
0
  }
1653
0
    }
1654
0
  (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s[%i]:\t%s",
1655
0
        inst_str, idx, funcname);
1656
0
  return entry_size - entry_delta;
1657
0
}
1658
1659
static bool
1660
riscv_init_disasm_info (struct disassemble_info *info)
1661
1.61k
{
1662
1.61k
  int i;
1663
1.61k
  struct riscv_private_data *pd =
1664
1.61k
  xcalloc (1, sizeof (struct riscv_private_data));
1665
1.61k
  pd->gp = 0;
1666
1.61k
  pd->print_addr = 0;
1667
53.3k
  for (i = 0; i < (int) ARRAY_SIZE (pd->hi_addr); i++)
1668
51.7k
    pd->hi_addr[i] = -1;
1669
1.61k
  pd->to_print_addr = false;
1670
1671
1.61k
  pd->has_jvt_base = false;
1672
1.61k
  pd->has_gp = false;
1673
1.61k
  pd->jvt_section = NULL;
1674
1.61k
  pd->jvt_content = NULL;
1675
1.61k
  pd->jvt_size = 0;
1676
1677
1.61k
  if (info->section != NULL)
1678
534
    {
1679
534
      bfd *abfd = info->section->owner;
1680
534
      if (abfd && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1681
532
  {
1682
532
    asection *s = bfd_get_section_by_name (abfd, TABLE_JUMP_SEC_NAME);
1683
532
    if (s != NULL && bfd_section_size (s) > 0)
1684
0
      {
1685
0
        bfd_size_type size = bfd_section_size (s);
1686
0
        bfd_byte *buf = (bfd_byte *) xmalloc (size);
1687
0
        if (bfd_get_section_contents (abfd, s, buf, 0, size))
1688
0
    {
1689
0
      pd->jvt_section = s;
1690
0
      pd->jvt_content = buf;
1691
0
      pd->jvt_size = size;
1692
0
    }
1693
0
        else
1694
0
    free (buf);
1695
0
      }
1696
532
  }
1697
534
    }
1698
1699
1.65k
  for (i = 0; i < info->symtab_size; i++)
1700
36
    {
1701
36
      asymbol *sym = info->symtab[i];
1702
36
      if (strcmp (bfd_asymbol_name (sym), RISCV_GP_SYMBOL) == 0)
1703
0
  {
1704
0
    pd->gp = bfd_asymbol_value (sym);
1705
0
    pd->has_gp = true;
1706
0
  }
1707
1708
36
      if (strcmp (bfd_asymbol_name (sym), RISCV_TABLE_JUMP_BASE_SYMBOL) == 0)
1709
0
  {
1710
0
    bfd_vma jvt_base = bfd_asymbol_value (sym);
1711
0
    if (pd->jvt_section != NULL
1712
0
        && jvt_base == bfd_section_vma (pd->jvt_section))
1713
0
      pd->has_jvt_base = true;
1714
0
  }
1715
36
    }
1716
1717
1.61k
  pd->xlen = 0;
1718
1.61k
  riscv_set_xlen_from_elf (pd, info);
1719
1.61k
  pd->default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1;
1720
1721
1.61k
  pd->riscv_rps_dis.subset_list = xcalloc (1, sizeof (riscv_parse_subset_t));
1722
1.61k
  pd->riscv_rps_dis.error_handler = opcodes_error_handler;
1723
1.61k
  pd->riscv_rps_dis.xlen = &pd->xlen;
1724
1.61k
  pd->riscv_rps_dis.isa_spec = &pd->default_isa_spec;
1725
1.61k
  pd->riscv_rps_dis.check_unknown_prefixed_ext = false;
1726
1.61k
  pd->default_arch = "rv64gc";
1727
1.61k
  if (info->section != NULL)
1728
534
    {
1729
534
      bfd *abfd = info->section->owner;
1730
534
      if (abfd && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1731
532
  {
1732
532
    const char *sec_name =
1733
532
    get_elf_backend_data (abfd)->obj_attrs_section;
1734
532
    if (bfd_get_section_by_name (abfd, sec_name) != NULL)
1735
0
      {
1736
0
        obj_attribute *attr = elf_known_obj_attributes_proc (abfd);
1737
0
        pd->default_arch = attr[Tag_RISCV_arch].s;
1738
0
      }
1739
532
  }
1740
534
    }
1741
1742
1.61k
  pd->last_map_symbol = -1;
1743
1.61k
  pd->last_stop_offset = 0;
1744
1.61k
  pd->last_map_symbol_boundary = 0;
1745
1.61k
  pd->last_map_state = MAP_NONE;
1746
1.61k
  pd->last_map_section = NULL;
1747
1.61k
  pd->riscv_gpr_names = NULL;
1748
1.61k
  pd->riscv_fpr_names = NULL;
1749
1.61k
  pd->no_aliases = false;
1750
1.61k
  pd->all_ext = false;
1751
1752
1.61k
  info->private_data = pd;
1753
1.61k
  riscv_dis_parse_subset (info, pd->default_arch);
1754
1.61k
  return true;
1755
1.61k
}
1756
1757
/* Fetch an instruction. If only a partial instruction is able to be fetched,
1758
   return the number of accessible bytes.  */
1759
1760
static bfd_vma
1761
fetch_insn (bfd_vma memaddr,
1762
      bfd_byte *packet,
1763
      bfd_vma dump_size,
1764
      struct disassemble_info *info,
1765
      volatile int *status)
1766
4.31M
{
1767
4.31M
  do
1768
4.31M
    {
1769
4.31M
      *status = (*info->read_memory_func) (memaddr, packet, dump_size, info);
1770
4.31M
    }
1771
4.31M
  while (*status != 0 && dump_size-- > 1);
1772
1773
4.31M
  return dump_size;
1774
4.31M
}
1775
1776
int
1777
print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
1778
2.15M
{
1779
2.15M
  bfd_byte packet[RISCV_MAX_INSN_LEN];
1780
2.15M
  insn_t insn = 0;
1781
2.15M
  bfd_vma dump_size, bytes_fetched;
1782
2.15M
  int status;
1783
2.15M
  enum riscv_seg_mstate mstate;
1784
2.15M
  int (*riscv_disassembler) (bfd_vma, insn_t, const bfd_byte *,
1785
2.15M
           struct disassemble_info *);
1786
1787
2.15M
  if (info->private_data == NULL && !riscv_init_disasm_info (info))
1788
0
    return -1;
1789
1790
2.15M
  if (info->disassembler_options != NULL)
1791
0
    {
1792
0
      parse_riscv_dis_options (info->disassembler_options, info);
1793
      /* Avoid repeatedly parsing the options.  */
1794
0
      info->disassembler_options = NULL;
1795
0
    }
1796
2.15M
  else if (((struct riscv_private_data *) info->private_data)->riscv_gpr_names == NULL)
1797
1.61k
    set_default_riscv_dis_options (info);
1798
1799
2.15M
  mstate = riscv_search_mapping_symbol (memaddr, info);
1800
  /* Save the last mapping state.  */
1801
2.15M
  ((struct riscv_private_data *) info->private_data)->last_map_state = mstate;
1802
1803
  /* Set the size to dump.  */
1804
2.15M
  if (info->section != NULL
1805
1.43M
      && strcmp (info->section->name, TABLE_JUMP_SEC_NAME) == 0)
1806
0
    {
1807
0
      struct riscv_private_data *pd = info->private_data;
1808
0
      riscv_set_xlen_from_elf (pd, info);
1809
1810
0
      dump_size = pd->xlen / 8;
1811
0
      info->bytes_per_chunk = dump_size;
1812
0
      info->bytes_per_line = dump_size;
1813
0
      info->endian_code = bfd_big_endian (info->section->owner)
1814
0
         ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
1815
0
      info->display_endian = info->endian_code;
1816
0
      riscv_disassembler = riscv_disassemble_jvt;
1817
0
    }
1818
2.15M
  else if (mstate == MAP_DATA && (info->flags & DISASSEMBLE_DATA) == 0)
1819
0
    {
1820
0
      dump_size = riscv_data_length (memaddr, info);
1821
0
      info->bytes_per_chunk = dump_size;
1822
0
      riscv_disassembler = riscv_disassemble_data;
1823
0
    }
1824
2.15M
  else
1825
2.15M
    {
1826
      /* Get the first 2-bytes to check the lenghth of instruction.  */
1827
2.15M
      bytes_fetched = fetch_insn (memaddr, packet, 2, info, &status);
1828
2.15M
      if (status != 0)
1829
3
  {
1830
3
    (*info->memory_error_func) (status, memaddr, info);
1831
3
    return -1;
1832
3
  }
1833
2.15M
      else if (bytes_fetched != 2)
1834
395
       {
1835
    /* Only the first byte was able to be read.  Dump the partial
1836
       instruction.  */
1837
395
    dump_size = bytes_fetched;
1838
395
    info->bytes_per_chunk = dump_size;
1839
395
    riscv_disassembler = riscv_disassemble_data;
1840
395
    goto print;
1841
395
       }
1842
2.15M
      insn = (insn_t) bfd_getl16 (packet);
1843
2.15M
      dump_size = riscv_insn_length (insn);
1844
2.15M
      riscv_disassembler = riscv_disassemble_insn;
1845
2.15M
    }
1846
1847
2.15M
  bytes_fetched = fetch_insn (memaddr, packet, dump_size, info, &status);
1848
1849
2.15M
  if (status != 0)
1850
0
    {
1851
0
      (*info->memory_error_func) (status, memaddr, info);
1852
0
      return -1;
1853
0
    }
1854
2.15M
  else if (bytes_fetched != dump_size)
1855
485
    {
1856
485
      dump_size = bytes_fetched;
1857
485
      info->bytes_per_chunk = dump_size;
1858
485
      riscv_disassembler = riscv_disassemble_data;
1859
485
    }
1860
1861
2.15M
 print:
1862
1863
2.15M
  insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false);
1864
1865
2.15M
  return (*riscv_disassembler) (memaddr, insn, packet, info);
1866
2.15M
}
1867
1868
/* Prevent use of the fake labels that are generated as part of the DWARF
1869
   and for relaxable relocations in the assembler.  */
1870
1871
bool
1872
riscv_symbol_is_valid (asymbol * sym,
1873
                       struct disassemble_info * info ATTRIBUTE_UNUSED)
1874
39
{
1875
39
  const char * name;
1876
1877
39
  if (sym == NULL)
1878
0
    return false;
1879
1880
39
  name = bfd_asymbol_name (sym);
1881
1882
39
  return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0
1883
39
    && !riscv_elf_is_mapping_symbols (name));
1884
39
}
1885

1886
1887
/* Indices into option argument vector for options accepting an argument.
1888
   Use RISCV_OPTION_ARG_NONE for options accepting no argument.  */
1889
1890
typedef enum
1891
{
1892
  RISCV_OPTION_ARG_NONE = -1,
1893
  RISCV_OPTION_ARG_PRIV_SPEC,
1894
  RISCV_OPTION_ARG_ARCH,
1895
1896
  RISCV_OPTION_ARG_COUNT
1897
} riscv_option_arg_t;
1898
1899
/* Valid RISCV disassembler options.  */
1900
1901
static const struct
1902
{
1903
  const char *name;
1904
  const char *description;
1905
  riscv_option_arg_t arg;
1906
} riscv_options[] =
1907
{
1908
  { "max",
1909
    N_("Disassemble without checking architecture string."),
1910
    RISCV_OPTION_ARG_NONE },
1911
  { "numeric",
1912
    N_("Print numeric register names, rather than ABI names."),
1913
    RISCV_OPTION_ARG_NONE },
1914
  { "no-aliases",
1915
    N_("Disassemble only into canonical instructions."),
1916
    RISCV_OPTION_ARG_NONE },
1917
  { "priv-spec=",
1918
    N_("Deprecated and ignored, the CSR is printed according to the "
1919
       "privilege spec 1.13."),
1920
    RISCV_OPTION_ARG_PRIV_SPEC },
1921
  { "arch=",
1922
    N_("Disassemble according to the given ISA string."),
1923
    RISCV_OPTION_ARG_ARCH }
1924
};
1925
1926
/* Build the structure representing valid RISCV disassembler options.
1927
   This is done dynamically for maintenance ease purpose; a static
1928
   initializer would be unreadable.  */
1929
1930
const disasm_options_and_args_t *
1931
disassembler_options_riscv (void)
1932
0
{
1933
0
  static disasm_options_and_args_t *opts_and_args;
1934
1935
0
  if (opts_and_args == NULL)
1936
0
    {
1937
0
      size_t num_options = ARRAY_SIZE (riscv_options);
1938
0
      size_t num_args = RISCV_OPTION_ARG_COUNT;
1939
0
      disasm_option_arg_t *args;
1940
0
      disasm_options_t *opts;
1941
0
      size_t i;
1942
1943
0
      args = XNEWVEC (disasm_option_arg_t, num_args + 1);
1944
1945
0
      args[RISCV_OPTION_ARG_PRIV_SPEC].name = "SPEC";
1946
      /* The option is deprecated, so accept any value.  */
1947
0
      args[RISCV_OPTION_ARG_PRIV_SPEC].values = NULL;
1948
1949
      /* Any ISA string is accepted, so no candidate value list.  */
1950
0
      args[RISCV_OPTION_ARG_ARCH].name = "ISA";
1951
0
      args[RISCV_OPTION_ARG_ARCH].values = NULL;
1952
1953
      /* The array we return must be NULL terminated.  */
1954
0
      args[num_args].name = NULL;
1955
0
      args[num_args].values = NULL;
1956
1957
0
      opts_and_args = XNEW (disasm_options_and_args_t);
1958
0
      opts_and_args->args = args;
1959
1960
0
      opts = &opts_and_args->options;
1961
0
      opts->name = XNEWVEC (const char *, num_options + 1);
1962
0
      opts->description = XNEWVEC (const char *, num_options + 1);
1963
0
      opts->arg = XNEWVEC (const disasm_option_arg_t *, num_options + 1);
1964
0
      for (i = 0; i < num_options; i++)
1965
0
  {
1966
0
    opts->name[i] = riscv_options[i].name;
1967
0
    opts->description[i] = _(riscv_options[i].description);
1968
0
    if (riscv_options[i].arg != RISCV_OPTION_ARG_NONE)
1969
0
      opts->arg[i] = &args[riscv_options[i].arg];
1970
0
    else
1971
0
      opts->arg[i] = NULL;
1972
0
  }
1973
      /* The array we return must be NULL terminated.  */
1974
0
      opts->name[i] = NULL;
1975
0
      opts->description[i] = NULL;
1976
0
      opts->arg[i] = NULL;
1977
0
    }
1978
1979
0
  return opts_and_args;
1980
0
}
1981
1982
void
1983
print_riscv_disassembler_options (FILE *stream)
1984
0
{
1985
0
  const disasm_options_and_args_t *opts_and_args;
1986
0
  const disasm_option_arg_t *args;
1987
0
  const disasm_options_t *opts;
1988
0
  size_t max_len = 0;
1989
0
  size_t i;
1990
0
  size_t j;
1991
1992
0
  opts_and_args = disassembler_options_riscv ();
1993
0
  opts = &opts_and_args->options;
1994
0
  args = opts_and_args->args;
1995
1996
0
  fprintf (stream, _("\n\
1997
0
The following RISC-V specific disassembler options are supported for use\n\
1998
0
with the -M switch (multiple options should be separated by commas):\n"));
1999
0
  fprintf (stream, "\n");
2000
2001
  /* Compute the length of the longest option name.  */
2002
0
  for (i = 0; opts->name[i] != NULL; i++)
2003
0
    {
2004
0
      size_t len = strlen (opts->name[i]);
2005
2006
0
      if (opts->arg[i] != NULL)
2007
0
  len += strlen (opts->arg[i]->name);
2008
0
      if (max_len < len)
2009
0
  max_len = len;
2010
0
    }
2011
2012
0
  for (i = 0, max_len++; opts->name[i] != NULL; i++)
2013
0
    {
2014
0
      fprintf (stream, "  %s", opts->name[i]);
2015
0
      if (opts->arg[i] != NULL)
2016
0
  fprintf (stream, "%s", opts->arg[i]->name);
2017
0
      if (opts->description[i] != NULL)
2018
0
  {
2019
0
    size_t len = strlen (opts->name[i]);
2020
2021
0
    if (opts->arg != NULL && opts->arg[i] != NULL)
2022
0
      len += strlen (opts->arg[i]->name);
2023
0
    fprintf (stream, "%*c %s", (int) (max_len - len), ' ',
2024
0
                   opts->description[i]);
2025
0
  }
2026
0
      fprintf (stream, "\n");
2027
0
    }
2028
2029
0
  for (i = 0; args[i].name != NULL; i++)
2030
0
    {
2031
0
      if (args[i].values == NULL)
2032
0
  continue;
2033
0
      fprintf (stream, _("\n\
2034
0
  For the options above, the following values are supported for \"%s\":\n   "),
2035
0
         args[i].name);
2036
0
      for (j = 0; args[i].values[j] != NULL; j++)
2037
0
  fprintf (stream, " %s", args[i].values[j]);
2038
0
      fprintf (stream, _("\n"));
2039
0
    }
2040
2041
0
  fprintf (stream, _("\n"));
2042
0
}
2043
2044
void disassemble_free_riscv (struct disassemble_info *info ATTRIBUTE_UNUSED)
2045
1.70k
{
2046
1.70k
  struct riscv_private_data *pd = info->private_data;
2047
1.70k
  if (pd)
2048
1.61k
    {
2049
1.61k
      riscv_release_subset_list (pd->riscv_rps_dis.subset_list);
2050
1.61k
      free (pd->riscv_rps_dis.subset_list);
2051
1.61k
      free (pd->jvt_content);
2052
1.61k
      free (pd->arch_option);
2053
1.61k
    }
2054
1.70k
}