]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - opcodes/riscv-dis.c
RISC-V: Renamed INSN_CLASS for floating point in integer extensions.
[thirdparty/binutils-gdb.git] / opcodes / riscv-dis.c
CommitLineData
e23eba97 1/* RISC-V disassembler
a2c58332 2 Copyright (C) 2011-2022 Free Software Foundation, Inc.
e23eba97
NC
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"
88c1242d 24#include "disassemble.h"
e23eba97
NC
25#include "libiberty.h"
26#include "opcode/riscv.h"
27#include "opintl.h"
28#include "elf-bfd.h"
29#include "elf/riscv.h"
f786c359 30#include "elfxx-riscv.h"
e23eba97 31
3dfb1b6d 32#include <stdint.h>
e23eba97
NC
33#include <ctype.h>
34
f786c359 35static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1;
3d73d29e 36static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE;
8f595e9b 37
f786c359
NC
38unsigned xlen = 0;
39
40static riscv_subset_list_t riscv_subsets;
41static riscv_parse_subset_t riscv_rps_dis =
42{
43 &riscv_subsets, /* subset_list. */
44 opcodes_error_handler,/* error_handler. */
45 &xlen, /* xlen. */
46 &default_isa_spec, /* isa_spec. */
47 false, /* check_unknown_prefixed_ext. */
48};
49
e23eba97
NC
50struct riscv_private_data
51{
52 bfd_vma gp;
53 bfd_vma print_addr;
54 bfd_vma hi_addr[OP_MASK_RD + 1];
8fe1be5f
TO
55 bool to_print_addr;
56 bool has_gp;
e23eba97
NC
57};
58
9b9b1092
NC
59/* Used for mapping symbols. */
60static int last_map_symbol = -1;
61static bfd_vma last_stop_offset = 0;
62enum riscv_seg_mstate last_map_state;
63
e23eba97
NC
64static const char * const *riscv_gpr_names;
65static const char * const *riscv_fpr_names;
66
dcd709e0
NC
67/* If set, disassemble as most general instruction. */
68static int no_aliases;
e23eba97
NC
69
70static void
71set_default_riscv_dis_options (void)
72{
73 riscv_gpr_names = riscv_gpr_names_abi;
74 riscv_fpr_names = riscv_fpr_names_abi;
75 no_aliases = 0;
76}
77
78933a4a 78static bool
8f595e9b 79parse_riscv_dis_option_without_args (const char *option)
e23eba97
NC
80{
81 if (strcmp (option, "no-aliases") == 0)
82 no_aliases = 1;
83 else if (strcmp (option, "numeric") == 0)
84 {
85 riscv_gpr_names = riscv_gpr_names_numeric;
86 riscv_fpr_names = riscv_fpr_names_numeric;
87 }
8f595e9b 88 else
78933a4a
AM
89 return false;
90 return true;
8f595e9b
NC
91}
92
93static void
94parse_riscv_dis_option (const char *option)
95{
96 char *equal, *value;
97
98 if (parse_riscv_dis_option_without_args (option))
99 return;
100
101 equal = strchr (option, '=');
102 if (equal == NULL)
103 {
104 /* The option without '=' should be defined above. */
105 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
106 return;
107 }
108 if (equal == option
109 || *(equal + 1) == '\0')
110 {
111 /* Invalid options with '=', no option name before '=',
112 and no value after '='. */
113 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
114 option);
115 return;
116 }
117
118 *equal = '\0';
119 value = equal + 1;
120 if (strcmp (option, "priv-spec") == 0)
121 {
3d73d29e
NC
122 enum riscv_spec_class priv_spec = PRIV_SPEC_CLASS_NONE;
123 const char *name = NULL;
124
125 RISCV_GET_PRIV_SPEC_CLASS (value, priv_spec);
126 if (priv_spec == PRIV_SPEC_CLASS_NONE)
b800637e 127 opcodes_error_handler (_("unknown privileged spec set by %s=%s"),
8152e040
NC
128 option, value);
129 else if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
130 default_priv_spec = priv_spec;
131 else if (default_priv_spec != priv_spec)
3d73d29e
NC
132 {
133 RISCV_GET_PRIV_SPEC_NAME (name, default_priv_spec);
134 opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, "
135 "the elf privilege attribute is %s"),
136 option, value, name);
137 }
8f595e9b 138 }
e23eba97
NC
139 else
140 {
a6743a54
AM
141 /* xgettext:c-format */
142 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
e23eba97
NC
143 }
144}
145
146static void
147parse_riscv_dis_options (const char *opts_in)
148{
149 char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts;
150
151 set_default_riscv_dis_options ();
152
153 for ( ; opt_end != NULL; opt = opt_end + 1)
154 {
155 if ((opt_end = strchr (opt, ',')) != NULL)
156 *opt_end = 0;
157 parse_riscv_dis_option (opt);
158 }
159
160 free (opts);
161}
162
163/* Print one argument from an array. */
164
165static void
166arg_print (struct disassemble_info *info, unsigned long val,
167 const char* const* array, size_t size)
168{
169 const char *s = val >= size || array[val] == NULL ? "unknown" : array[val];
49d31dc9 170 (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s", s);
e23eba97
NC
171}
172
173static void
c7dee848
JW
174maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset,
175 int wide)
e23eba97
NC
176{
177 if (pd->hi_addr[base_reg] != (bfd_vma)-1)
178 {
35fd2b2b 179 pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset;
e23eba97
NC
180 pd->hi_addr[base_reg] = -1;
181 }
8fe1be5f 182 else if (base_reg == X_GP && pd->has_gp)
e23eba97
NC
183 pd->print_addr = pd->gp + offset;
184 else if (base_reg == X_TP || base_reg == 0)
185 pd->print_addr = offset;
48525554
TO
186 else
187 return; /* Don't print the address. */
8fe1be5f 188 pd->to_print_addr = true;
c7dee848
JW
189
190 /* Sign-extend a 32-bit value to a 64-bit value. */
191 if (wide)
192 pd->print_addr = (bfd_vma)(int32_t) pd->print_addr;
48525554
TO
193
194 /* Fit into a 32-bit value on RV32. */
195 if (xlen == 32)
196 pd->print_addr = (bfd_vma)(uint32_t)pd->print_addr;
e23eba97
NC
197}
198
199/* Print insn arguments for 32/64-bit code. */
200
201static void
437e2ff1 202print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info)
e23eba97
NC
203{
204 struct riscv_private_data *pd = info->private_data;
205 int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1;
206 int rd = (l >> OP_SH_RD) & OP_MASK_RD;
49d31dc9 207 fprintf_styled_ftype print = info->fprintf_styled_func;
437e2ff1 208 const char *opargStart;
e23eba97 209
437e2ff1 210 if (*oparg != '\0')
49d31dc9 211 print (info->stream, dis_style_text, "\t");
e23eba97 212
437e2ff1 213 for (; *oparg != '\0'; oparg++)
e23eba97 214 {
437e2ff1
NC
215 opargStart = oparg;
216 switch (*oparg)
e23eba97
NC
217 {
218 case 'C': /* RVC */
437e2ff1 219 switch (*++oparg)
e23eba97 220 {
dcd709e0
NC
221 case 's': /* RS1 x8-x15. */
222 case 'w': /* RS1 x8-x15. */
49d31dc9 223 print (info->stream, dis_style_register, "%s",
e23eba97
NC
224 riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]);
225 break;
dcd709e0
NC
226 case 't': /* RS2 x8-x15. */
227 case 'x': /* RS2 x8-x15. */
49d31dc9 228 print (info->stream, dis_style_register, "%s",
e23eba97
NC
229 riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
230 break;
dcd709e0 231 case 'U': /* RS1, constrained to equal RD. */
49d31dc9
AB
232 print (info->stream, dis_style_register,
233 "%s", riscv_gpr_names[rd]);
e23eba97 234 break;
dcd709e0 235 case 'c': /* RS1, constrained to equal sp. */
49d31dc9
AB
236 print (info->stream, dis_style_register, "%s",
237 riscv_gpr_names[X_SP]);
e23eba97
NC
238 break;
239 case 'V': /* RS2 */
49d31dc9 240 print (info->stream, dis_style_register, "%s",
e23eba97
NC
241 riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]);
242 break;
f91d48de 243 case 'o':
e23eba97 244 case 'j':
c7dee848
JW
245 if (((l & MASK_C_ADDI) == MATCH_C_ADDI) && rd != 0)
246 maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 0);
247 if (info->mach == bfd_mach_riscv64
248 && ((l & MASK_C_ADDIW) == MATCH_C_ADDIW) && rd != 0)
249 maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 1);
49d31dc9
AB
250 print (info->stream, dis_style_immediate, "%d",
251 (int)EXTRACT_CITYPE_IMM (l));
e23eba97
NC
252 break;
253 case 'k':
49d31dc9
AB
254 print (info->stream, dis_style_address_offset, "%d",
255 (int)EXTRACT_CLTYPE_LW_IMM (l));
e23eba97
NC
256 break;
257 case 'l':
49d31dc9
AB
258 print (info->stream, dis_style_address_offset, "%d",
259 (int)EXTRACT_CLTYPE_LD_IMM (l));
e23eba97
NC
260 break;
261 case 'm':
49d31dc9
AB
262 print (info->stream, dis_style_address_offset, "%d",
263 (int)EXTRACT_CITYPE_LWSP_IMM (l));
e23eba97
NC
264 break;
265 case 'n':
49d31dc9
AB
266 print (info->stream, dis_style_address_offset, "%d",
267 (int)EXTRACT_CITYPE_LDSP_IMM (l));
e23eba97
NC
268 break;
269 case 'K':
49d31dc9
AB
270 print (info->stream, dis_style_immediate, "%d",
271 (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l));
e23eba97
NC
272 break;
273 case 'L':
49d31dc9
AB
274 print (info->stream, dis_style_immediate, "%d",
275 (int)EXTRACT_CITYPE_ADDI16SP_IMM (l));
e23eba97
NC
276 break;
277 case 'M':
49d31dc9
AB
278 print (info->stream, dis_style_address_offset, "%d",
279 (int)EXTRACT_CSSTYPE_SWSP_IMM (l));
e23eba97
NC
280 break;
281 case 'N':
49d31dc9
AB
282 print (info->stream, dis_style_address_offset, "%d",
283 (int)EXTRACT_CSSTYPE_SDSP_IMM (l));
e23eba97
NC
284 break;
285 case 'p':
5a9f5403 286 info->target = EXTRACT_CBTYPE_IMM (l) + pc;
e23eba97
NC
287 (*info->print_address_func) (info->target, info);
288 break;
289 case 'a':
5a9f5403 290 info->target = EXTRACT_CJTYPE_IMM (l) + pc;
e23eba97
NC
291 (*info->print_address_func) (info->target, info);
292 break;
293 case 'u':
49d31dc9 294 print (info->stream, dis_style_immediate, "0x%x",
5a9f5403 295 (int)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1)));
e23eba97
NC
296 break;
297 case '>':
49d31dc9
AB
298 print (info->stream, dis_style_immediate, "0x%x",
299 (int)EXTRACT_CITYPE_IMM (l) & 0x3f);
e23eba97
NC
300 break;
301 case '<':
49d31dc9
AB
302 print (info->stream, dis_style_immediate, "0x%x",
303 (int)EXTRACT_CITYPE_IMM (l) & 0x1f);
e23eba97 304 break;
dcd709e0 305 case 'T': /* Floating-point RS2. */
49d31dc9 306 print (info->stream, dis_style_register, "%s",
e23eba97
NC
307 riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]);
308 break;
dcd709e0 309 case 'D': /* Floating-point RS2 x8-x15. */
49d31dc9 310 print (info->stream, dis_style_register, "%s",
e23eba97
NC
311 riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
312 break;
313 }
314 break;
315
65e4a99a
NC
316 case 'V': /* RVV */
317 switch (*++oparg)
318 {
319 case 'd':
320 case 'f':
49d31dc9 321 print (info->stream, dis_style_register, "%s",
65e4a99a
NC
322 riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
323 break;
324 case 'e':
325 if (!EXTRACT_OPERAND (VWD, l))
49d31dc9
AB
326 print (info->stream, dis_style_register, "%s",
327 riscv_gpr_names[0]);
65e4a99a 328 else
49d31dc9 329 print (info->stream, dis_style_register, "%s",
65e4a99a
NC
330 riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
331 break;
332 case 's':
49d31dc9 333 print (info->stream, dis_style_register, "%s",
65e4a99a
NC
334 riscv_vecr_names_numeric[EXTRACT_OPERAND (VS1, l)]);
335 break;
336 case 't':
337 case 'u': /* VS1 == VS2 already verified at this point. */
338 case 'v': /* VD == VS1 == VS2 already verified at this point. */
49d31dc9 339 print (info->stream, dis_style_register, "%s",
65e4a99a
NC
340 riscv_vecr_names_numeric[EXTRACT_OPERAND (VS2, l)]);
341 break;
342 case '0':
49d31dc9
AB
343 print (info->stream, dis_style_register, "%s",
344 riscv_vecr_names_numeric[0]);
65e4a99a
NC
345 break;
346 case 'b':
347 case 'c':
348 {
349 int imm = (*oparg == 'b') ? EXTRACT_RVV_VB_IMM (l)
350 : EXTRACT_RVV_VC_IMM (l);
351 unsigned int imm_vlmul = EXTRACT_OPERAND (VLMUL, imm);
352 unsigned int imm_vsew = EXTRACT_OPERAND (VSEW, imm);
353 unsigned int imm_vta = EXTRACT_OPERAND (VTA, imm);
354 unsigned int imm_vma = EXTRACT_OPERAND (VMA, imm);
abfdb09f 355 unsigned int imm_vtype_res = (imm >> 8);
65e4a99a
NC
356
357 if (imm_vsew < ARRAY_SIZE (riscv_vsew)
358 && imm_vlmul < ARRAY_SIZE (riscv_vlmul)
359 && imm_vta < ARRAY_SIZE (riscv_vta)
360 && imm_vma < ARRAY_SIZE (riscv_vma)
ee083a9e
NC
361 && !imm_vtype_res
362 && riscv_vsew[imm_vsew] != NULL
363 && riscv_vlmul[imm_vlmul] != NULL)
49d31dc9
AB
364 print (info->stream, dis_style_text, "%s,%s,%s,%s",
365 riscv_vsew[imm_vsew],
65e4a99a
NC
366 riscv_vlmul[imm_vlmul], riscv_vta[imm_vta],
367 riscv_vma[imm_vma]);
368 else
49d31dc9 369 print (info->stream, dis_style_immediate, "%d", imm);
65e4a99a
NC
370 }
371 break;
372 case 'i':
49d31dc9
AB
373 print (info->stream, dis_style_immediate, "%d",
374 (int)EXTRACT_RVV_VI_IMM (l));
65e4a99a
NC
375 break;
376 case 'j':
49d31dc9
AB
377 print (info->stream, dis_style_immediate, "%d",
378 (int)EXTRACT_RVV_VI_UIMM (l));
65e4a99a
NC
379 break;
380 case 'k':
49d31dc9
AB
381 print (info->stream, dis_style_immediate, "%d",
382 (int)EXTRACT_RVV_OFFSET (l));
65e4a99a
NC
383 break;
384 case 'm':
385 if (! EXTRACT_OPERAND (VMASK, l))
49d31dc9
AB
386 print (info->stream, dis_style_register, ",%s",
387 riscv_vecm_names_numeric[0]);
65e4a99a
NC
388 break;
389 }
390 break;
391
e23eba97
NC
392 case ',':
393 case '(':
394 case ')':
395 case '[':
396 case ']':
49d31dc9 397 print (info->stream, dis_style_text, "%c", *oparg);
e23eba97
NC
398 break;
399
400 case '0':
dcd709e0 401 /* Only print constant 0 if it is the last argument. */
437e2ff1 402 if (!oparg[1])
49d31dc9 403 print (info->stream, dis_style_immediate, "0");
e23eba97
NC
404 break;
405
e23eba97 406 case 's':
35eeb78f 407 if ((l & MASK_JALR) == MATCH_JALR)
48525554 408 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
49d31dc9 409 print (info->stream, dis_style_register, "%s", riscv_gpr_names[rs1]);
e23eba97
NC
410 break;
411
412 case 't':
49d31dc9 413 print (info->stream, dis_style_register, "%s",
e23eba97
NC
414 riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]);
415 break;
416
417 case 'u':
49d31dc9 418 print (info->stream, dis_style_immediate, "0x%x",
e23eba97
NC
419 (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS);
420 break;
421
422 case 'm':
423 arg_print (info, EXTRACT_OPERAND (RM, l),
424 riscv_rm, ARRAY_SIZE (riscv_rm));
425 break;
426
427 case 'P':
428 arg_print (info, EXTRACT_OPERAND (PRED, l),
429 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
430 break;
431
432 case 'Q':
433 arg_print (info, EXTRACT_OPERAND (SUCC, l),
434 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
435 break;
436
437 case 'o':
c7dee848 438 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
b52d3cfc 439 /* Fall through. */
e23eba97
NC
440 case 'j':
441 if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0)
442 || (l & MASK_JALR) == MATCH_JALR)
c7dee848
JW
443 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
444 if (info->mach == bfd_mach_riscv64
445 && ((l & MASK_ADDIW) == MATCH_ADDIW) && rs1 != 0)
446 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 1);
49d31dc9
AB
447 print (info->stream, dis_style_immediate, "%d",
448 (int)EXTRACT_ITYPE_IMM (l));
e23eba97
NC
449 break;
450
451 case 'q':
c7dee848 452 maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l), 0);
49d31dc9
AB
453 print (info->stream, dis_style_address_offset, "%d",
454 (int)EXTRACT_STYPE_IMM (l));
e23eba97
NC
455 break;
456
3b374308 457 case 'f':
49d31dc9
AB
458 print (info->stream, dis_style_address_offset, "%d",
459 (int)EXTRACT_STYPE_IMM (l));
3b374308
TO
460 break;
461
e23eba97 462 case 'a':
5a9f5403 463 info->target = EXTRACT_JTYPE_IMM (l) + pc;
e23eba97
NC
464 (*info->print_address_func) (info->target, info);
465 break;
466
467 case 'p':
5a9f5403 468 info->target = EXTRACT_BTYPE_IMM (l) + pc;
e23eba97
NC
469 (*info->print_address_func) (info->target, info);
470 break;
471
472 case 'd':
473 if ((l & MASK_AUIPC) == MATCH_AUIPC)
474 pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l);
475 else if ((l & MASK_LUI) == MATCH_LUI)
476 pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l);
477 else if ((l & MASK_C_LUI) == MATCH_C_LUI)
5a9f5403 478 pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l);
49d31dc9 479 print (info->stream, dis_style_register, "%s", riscv_gpr_names[rd]);
e23eba97
NC
480 break;
481
3d1cafa0 482 case 'y':
49d31dc9
AB
483 print (info->stream, dis_style_text, "0x%x",
484 (int)EXTRACT_OPERAND (BS, l));
3d1cafa0 485 break;
486
e23eba97 487 case 'z':
49d31dc9 488 print (info->stream, dis_style_register, "%s", riscv_gpr_names[0]);
e23eba97
NC
489 break;
490
491 case '>':
49d31dc9
AB
492 print (info->stream, dis_style_immediate, "0x%x",
493 (int)EXTRACT_OPERAND (SHAMT, l));
e23eba97
NC
494 break;
495
496 case '<':
49d31dc9
AB
497 print (info->stream, dis_style_immediate, "0x%x",
498 (int)EXTRACT_OPERAND (SHAMTW, l));
e23eba97
NC
499 break;
500
501 case 'S':
502 case 'U':
49d31dc9 503 print (info->stream, dis_style_register, "%s", riscv_fpr_names[rs1]);
e23eba97
NC
504 break;
505
506 case 'T':
49d31dc9
AB
507 print (info->stream, dis_style_register, "%s",
508 riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]);
e23eba97
NC
509 break;
510
511 case 'D':
49d31dc9 512 print (info->stream, dis_style_register, "%s", riscv_fpr_names[rd]);
e23eba97
NC
513 break;
514
515 case 'R':
49d31dc9
AB
516 print (info->stream, dis_style_register, "%s",
517 riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]);
e23eba97
NC
518 break;
519
520 case 'E':
521 {
dcd709e0 522 static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs. */
78933a4a 523 static bool init_csr = false;
e23eba97 524 unsigned int csr = EXTRACT_OPERAND (CSR, l);
8f595e9b
NC
525
526 if (!init_csr)
e23eba97 527 {
8f595e9b
NC
528 unsigned int i;
529 for (i = 0; i < 4096; i++)
530 riscv_csr_hash[i] = NULL;
531
dcd709e0 532 /* Set to the newest privileged version. */
8f595e9b
NC
533 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
534 default_priv_spec = PRIV_SPEC_CLASS_DRAFT - 1;
535
08ccfccf
NC
536#define DECLARE_CSR(name, num, class, define_version, abort_version) \
537 if (riscv_csr_hash[num] == NULL \
538 && ((define_version == PRIV_SPEC_CLASS_NONE \
539 && abort_version == PRIV_SPEC_CLASS_NONE) \
540 || (default_priv_spec >= define_version \
541 && default_priv_spec < abort_version))) \
8f595e9b
NC
542 riscv_csr_hash[num] = #name;
543#define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
544 DECLARE_CSR (name, num, class, define_version, abort_version)
e23eba97
NC
545#include "opcode/riscv-opc.h"
546#undef DECLARE_CSR
547 }
8f595e9b
NC
548
549 if (riscv_csr_hash[csr] != NULL)
e840e61c
AB
550 print (info->stream, dis_style_register, "%s",
551 riscv_csr_hash[csr]);
e23eba97 552 else
49d31dc9 553 print (info->stream, dis_style_text, "0x%x", csr);
e23eba97
NC
554 break;
555 }
556
3d1cafa0 557 case 'Y':
49d31dc9
AB
558 print (info->stream, dis_style_text, "0x%x",
559 (int) EXTRACT_OPERAND (RNUM, l));
3d1cafa0 560 break;
561
e23eba97 562 case 'Z':
49d31dc9 563 print (info->stream, dis_style_text, "%d", rs1);
e23eba97
NC
564 break;
565
8b7419c4
CM
566 case 'X': /* Integer immediate. */
567 {
568 size_t n;
569 size_t s;
570 bool sign;
571
572 switch (*++oparg)
573 {
25236d63
CM
574 case 'l': /* Literal. */
575 oparg++;
576 while (*oparg && *oparg != ',')
577 {
578 print (info->stream, dis_style_text, "%c", *oparg);
579 oparg++;
580 }
581 oparg--;
582 break;
8b7419c4
CM
583 case 's': /* 'XsN@S' ... N-bit signed immediate at bit S. */
584 sign = true;
585 goto print_imm;
586 case 'u': /* 'XuN@S' ... N-bit unsigned immediate at bit S. */
587 sign = false;
588 goto print_imm;
589 print_imm:
b0423163 590 n = strtol (oparg + 1, (char **)&oparg, 10);
8b7419c4
CM
591 if (*oparg != '@')
592 goto undefined_modifier;
b0423163 593 s = strtol (oparg + 1, (char **)&oparg, 10);
8b7419c4
CM
594 oparg--;
595
596 if (!sign)
597 print (info->stream, dis_style_immediate, "%u",
598 (unsigned)EXTRACT_U_IMM (n, s, l));
599 else
600 print (info->stream, dis_style_immediate, "%i",
601 (unsigned)EXTRACT_S_IMM (n, s, l));
602 break;
603 default:
604 goto undefined_modifier;
605 }
606 }
607 break;
e23eba97 608 default:
8b7419c4 609 undefined_modifier:
e23eba97 610 /* xgettext:c-format */
49d31dc9
AB
611 print (info->stream, dis_style_text,
612 _("# internal error, undefined modifier (%c)"),
437e2ff1 613 *opargStart);
e23eba97
NC
614 return;
615 }
616 }
617}
618
619/* Print the RISC-V instruction at address MEMADDR in debugged memory,
620 on using INFO. Returns length of the instruction, in bytes.
621 BIGENDIAN must be 1 if this is big-endian code, 0 if
622 this is little-endian code. */
623
624static int
625riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info)
626{
627 const struct riscv_opcode *op;
78933a4a 628 static bool init = 0;
e23eba97
NC
629 static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
630 struct riscv_private_data *pd;
631 int insnlen;
632
633#define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
634
635 /* Build a hash table to shorten the search time. */
636 if (! init)
637 {
638 for (op = riscv_opcodes; op->name; op++)
639 if (!riscv_hash[OP_HASH_IDX (op->match)])
640 riscv_hash[OP_HASH_IDX (op->match)] = op;
641
642 init = 1;
643 }
644
645 if (info->private_data == NULL)
646 {
647 int i;
648
649 pd = info->private_data = xcalloc (1, sizeof (struct riscv_private_data));
8fe1be5f
TO
650 pd->gp = 0;
651 pd->print_addr = 0;
e23eba97
NC
652 for (i = 0; i < (int)ARRAY_SIZE (pd->hi_addr); i++)
653 pd->hi_addr[i] = -1;
8fe1be5f
TO
654 pd->to_print_addr = false;
655 pd->has_gp = false;
e23eba97
NC
656
657 for (i = 0; i < info->symtab_size; i++)
b5292032 658 if (strcmp (bfd_asymbol_name (info->symtab[i]), RISCV_GP_SYMBOL) == 0)
8fe1be5f
TO
659 {
660 pd->gp = bfd_asymbol_value (info->symtab[i]);
661 pd->has_gp = true;
662 }
e23eba97
NC
663 }
664 else
665 pd = info->private_data;
666
667 insnlen = riscv_insn_length (word);
668
d7560e2d
JW
669 /* RISC-V instructions are always little-endian. */
670 info->endian_code = BFD_ENDIAN_LITTLE;
671
e23eba97
NC
672 info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2;
673 info->bytes_per_line = 8;
d7560e2d
JW
674 /* We don't support constant pools, so this must be code. */
675 info->display_endian = info->endian_code;
e23eba97
NC
676 info->insn_info_valid = 1;
677 info->branch_delay_insns = 0;
678 info->data_size = 0;
679 info->insn_type = dis_nonbranch;
680 info->target = 0;
681 info->target2 = 0;
682
683 op = riscv_hash[OP_HASH_IDX (word)];
684 if (op != NULL)
685 {
2922d21d
AW
686 /* If XLEN is not known, get its value from the ELF class. */
687 if (info->mach == bfd_mach_riscv64)
688 xlen = 64;
689 else if (info->mach == bfd_mach_riscv32)
690 xlen = 32;
691 else if (info->section != NULL)
e23eba97
NC
692 {
693 Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
694 xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
695 }
696
de83e514 697 /* If arch has ZFINX flags, use gpr for disassemble. */
698 if(riscv_subset_supports (&riscv_rps_dis, "zfinx"))
3d5d6bd5 699 riscv_fpr_names = riscv_gpr_names;
de83e514 700
e23eba97
NC
701 for (; op->name; op++)
702 {
703 /* Does the opcode match? */
704 if (! (op->match_func) (op, word))
705 continue;
706 /* Is this a pseudo-instruction and may we print it as such? */
707 if (no_aliases && (op->pinfo & INSN_ALIAS))
708 continue;
709 /* Is this instruction restricted to a certain value of XLEN? */
43135d3b 710 if ((op->xlen_requirement != 0) && (op->xlen_requirement != xlen))
e23eba97
NC
711 continue;
712
f786c359
NC
713 if (!riscv_multi_subset_supports (&riscv_rps_dis, op->insn_class))
714 continue;
715
e23eba97 716 /* It's a match. */
49d31dc9
AB
717 (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic,
718 "%s", op->name);
e23eba97
NC
719 print_insn_args (op->args, word, memaddr, info);
720
721 /* Try to disassemble multi-instruction addressing sequences. */
8fe1be5f 722 if (pd->to_print_addr)
e23eba97
NC
723 {
724 info->target = pd->print_addr;
49d31dc9
AB
725 (*info->fprintf_styled_func)
726 (info->stream, dis_style_comment_start, " # ");
e23eba97 727 (*info->print_address_func) (info->target, info);
8fe1be5f 728 pd->to_print_addr = false;
e23eba97
NC
729 }
730
eb41b248
JW
731 /* Finish filling out insn_info fields. */
732 switch (op->pinfo & INSN_TYPE)
733 {
734 case INSN_BRANCH:
735 info->insn_type = dis_branch;
736 break;
737 case INSN_CONDBRANCH:
738 info->insn_type = dis_condbranch;
739 break;
740 case INSN_JSR:
741 info->insn_type = dis_jsr;
742 break;
743 case INSN_DREF:
744 info->insn_type = dis_dref;
745 break;
746 default:
747 break;
748 }
749
750 if (op->pinfo & INSN_DATA_SIZE)
751 {
752 int size = ((op->pinfo & INSN_DATA_SIZE)
753 >> INSN_DATA_SIZE_SHIFT);
754 info->data_size = 1 << (size - 1);
755 }
756
e23eba97
NC
757 return insnlen;
758 }
759 }
760
761 /* We did not find a match, so just print the instruction bits. */
762 info->insn_type = dis_noninsn;
6a7f5766
AB
763 switch (insnlen)
764 {
765 case 2:
766 case 4:
767 case 8:
49d31dc9
AB
768 (*info->fprintf_styled_func)
769 (info->stream, dis_style_assembler_directive, ".%dbyte\t", insnlen);
770 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
771 "0x%llx", (unsigned long long) word);
6a7f5766
AB
772 break;
773 default:
774 {
775 int i;
49d31dc9
AB
776 (*info->fprintf_styled_func)
777 (info->stream, dis_style_assembler_directive, ".byte\t");
6a7f5766
AB
778 for (i = 0; i < insnlen; ++i)
779 {
780 if (i > 0)
49d31dc9
AB
781 (*info->fprintf_styled_func) (info->stream, dis_style_text,
782 ", ");
783 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
784 "0x%02x",
785 (unsigned int) (word & 0xff));
6a7f5766
AB
786 word >>= 8;
787 }
788 }
789 break;
790 }
e23eba97
NC
791 return insnlen;
792}
793
9b9b1092
NC
794/* Return true if we find the suitable mapping symbol,
795 and also update the STATE. Otherwise, return false. */
796
797static bool
798riscv_get_map_state (int n,
799 enum riscv_seg_mstate *state,
800 struct disassemble_info *info)
801{
802 const char *name;
803
804 /* If the symbol is in a different section, ignore it. */
805 if (info->section != NULL
806 && info->section != info->symtab[n]->section)
807 return false;
808
809 name = bfd_asymbol_name(info->symtab[n]);
810 if (strcmp (name, "$x") == 0)
811 *state = MAP_INSN;
812 else if (strcmp (name, "$d") == 0)
813 *state = MAP_DATA;
814 else
815 return false;
816
817 return true;
818}
819
820/* Check the sorted symbol table (sorted by the symbol value), find the
821 suitable mapping symbols. */
822
823static enum riscv_seg_mstate
824riscv_search_mapping_symbol (bfd_vma memaddr,
825 struct disassemble_info *info)
826{
827 enum riscv_seg_mstate mstate;
828 bool from_last_map_symbol;
829 bool found = false;
830 int symbol = -1;
831 int n;
832
833 /* Decide whether to print the data or instruction by default, in case
834 we can not find the corresponding mapping symbols. */
835 mstate = MAP_DATA;
836 if ((info->section
837 && info->section->flags & SEC_CODE)
838 || !info->section)
839 mstate = MAP_INSN;
840
841 if (info->symtab_size == 0
842 || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour)
843 return mstate;
844
845 /* Reset the last_map_symbol if we start to dump a new section. */
846 if (memaddr <= 0)
847 last_map_symbol = -1;
848
849 /* If the last stop offset is different from the current one, then
850 don't use the last_map_symbol to search. We usually reset the
851 info->stop_offset when handling a new section. */
852 from_last_map_symbol = (last_map_symbol >= 0
853 && info->stop_offset == last_stop_offset);
854
855 /* Start scanning at the start of the function, or wherever
856 we finished last time. */
857 n = info->symtab_pos + 1;
858 if (from_last_map_symbol && n >= last_map_symbol)
859 n = last_map_symbol;
860
861 /* Find the suitable mapping symbol to dump. */
862 for (; n < info->symtab_size; n++)
863 {
864 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
865 /* We have searched all possible symbols in the range. */
866 if (addr > memaddr)
867 break;
868 if (riscv_get_map_state (n, &mstate, info))
869 {
870 symbol = n;
871 found = true;
872 /* Do not stop searching, in case there are some mapping
873 symbols have the same value, but have different names.
874 Use the last one. */
875 }
876 }
877
878 /* We can not find the suitable mapping symbol above. Therefore, we
879 look forwards and try to find it again, but don't go pass the start
880 of the section. Otherwise a data section without mapping symbols
881 can pick up a text mapping symbol of a preceeding section. */
882 if (!found)
883 {
884 n = info->symtab_pos;
885 if (from_last_map_symbol && n >= last_map_symbol)
886 n = last_map_symbol;
887
888 for (; n >= 0; n--)
889 {
890 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
891 /* We have searched all possible symbols in the range. */
892 if (addr < (info->section ? info->section->vma : 0))
893 break;
894 /* Stop searching once we find the closed mapping symbol. */
895 if (riscv_get_map_state (n, &mstate, info))
896 {
897 symbol = n;
898 found = true;
899 break;
900 }
901 }
902 }
903
904 /* Save the information for next use. */
905 last_map_symbol = symbol;
906 last_stop_offset = info->stop_offset;
907
908 return mstate;
909}
910
911/* Decide which data size we should print. */
912
913static bfd_vma
914riscv_data_length (bfd_vma memaddr,
915 disassemble_info *info)
916{
917 bfd_vma length;
918 bool found = false;
919
920 length = 4;
921 if (info->symtab_size != 0
922 && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour
923 && last_map_symbol >= 0)
924 {
925 int n;
926 enum riscv_seg_mstate m = MAP_NONE;
927 for (n = last_map_symbol + 1; n < info->symtab_size; n++)
928 {
929 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
930 if (addr > memaddr
931 && riscv_get_map_state (n, &m, info))
932 {
933 if (addr - memaddr < length)
934 length = addr - memaddr;
935 found = true;
936 break;
937 }
938 }
939 }
940 if (!found)
941 {
942 /* Do not set the length which exceeds the section size. */
943 bfd_vma offset = info->section->vma + info->section->size;
944 offset -= memaddr;
945 length = (offset < length) ? offset : length;
946 }
947 length = length == 3 ? 2 : length;
948 return length;
949}
950
951/* Dump the data contents. */
952
953static int
954riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED,
955 insn_t data,
956 disassemble_info *info)
957{
958 info->display_endian = info->endian;
959
960 switch (info->bytes_per_chunk)
961 {
962 case 1:
963 info->bytes_per_line = 6;
49d31dc9
AB
964 (*info->fprintf_styled_func)
965 (info->stream, dis_style_assembler_directive, ".byte\t");
966 (*info->fprintf_styled_func)
967 (info->stream, dis_style_assembler_directive, "0x%02llx",
968 (unsigned long long) data);
9b9b1092
NC
969 break;
970 case 2:
971 info->bytes_per_line = 8;
49d31dc9
AB
972 (*info->fprintf_styled_func)
973 (info->stream, dis_style_assembler_directive, ".short\t");
974 (*info->fprintf_styled_func)
975 (info->stream, dis_style_immediate, "0x%04llx",
976 (unsigned long long) data);
9b9b1092
NC
977 break;
978 case 4:
979 info->bytes_per_line = 8;
49d31dc9
AB
980 (*info->fprintf_styled_func)
981 (info->stream, dis_style_assembler_directive, ".word\t");
982 (*info->fprintf_styled_func)
983 (info->stream, dis_style_immediate, "0x%08llx",
984 (unsigned long long) data);
9b9b1092
NC
985 break;
986 case 8:
987 info->bytes_per_line = 8;
49d31dc9
AB
988 (*info->fprintf_styled_func)
989 (info->stream, dis_style_assembler_directive, ".dword\t");
990 (*info->fprintf_styled_func)
991 (info->stream, dis_style_immediate, "0x%016llx",
992 (unsigned long long) data);
9b9b1092
NC
993 break;
994 default:
995 abort ();
996 }
997 return info->bytes_per_chunk;
998}
999
e23eba97
NC
1000int
1001print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
1002{
9b9b1092 1003 bfd_byte packet[8];
e23eba97 1004 insn_t insn = 0;
9b9b1092 1005 bfd_vma dump_size;
e23eba97 1006 int status;
9b9b1092
NC
1007 enum riscv_seg_mstate mstate;
1008 int (*riscv_disassembler) (bfd_vma, insn_t, struct disassemble_info *);
e23eba97
NC
1009
1010 if (info->disassembler_options != NULL)
1011 {
1012 parse_riscv_dis_options (info->disassembler_options);
1013 /* Avoid repeatedly parsing the options. */
1014 info->disassembler_options = NULL;
1015 }
1016 else if (riscv_gpr_names == NULL)
1017 set_default_riscv_dis_options ();
1018
9b9b1092
NC
1019 mstate = riscv_search_mapping_symbol (memaddr, info);
1020 /* Save the last mapping state. */
1021 last_map_state = mstate;
1022
1023 /* Set the size to dump. */
1024 if (mstate == MAP_DATA
1025 && (info->flags & DISASSEMBLE_DATA) == 0)
1026 {
1027 dump_size = riscv_data_length (memaddr, info);
1028 info->bytes_per_chunk = dump_size;
1029 riscv_disassembler = riscv_disassemble_data;
1030 }
1031 else
e23eba97 1032 {
9b9b1092
NC
1033 /* Get the first 2-bytes to check the lenghth of instruction. */
1034 status = (*info->read_memory_func) (memaddr, packet, 2, info);
e23eba97
NC
1035 if (status != 0)
1036 {
e23eba97 1037 (*info->memory_error_func) (status, memaddr, info);
685bb4e8 1038 return status;
e23eba97 1039 }
9b9b1092
NC
1040 insn = (insn_t) bfd_getl16 (packet);
1041 dump_size = riscv_insn_length (insn);
1042 riscv_disassembler = riscv_disassemble_insn;
1043 }
e23eba97 1044
9b9b1092
NC
1045 /* Fetch the instruction to dump. */
1046 status = (*info->read_memory_func) (memaddr, packet, dump_size, info);
1047 if (status != 0)
1048 {
1049 (*info->memory_error_func) (status, memaddr, info);
685bb4e8 1050 return status;
e23eba97 1051 }
9b9b1092 1052 insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false);
e23eba97 1053
9b9b1092 1054 return (*riscv_disassembler) (memaddr, insn, info);
e23eba97
NC
1055}
1056
8152e040
NC
1057disassembler_ftype
1058riscv_get_disassembler (bfd *abfd)
1059{
f786c359
NC
1060 const char *default_arch = "rv64gc";
1061
16089f32 1062 if (abfd && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
8152e040 1063 {
16089f32
TH
1064 const char *sec_name = get_elf_backend_data (abfd)->obj_attrs_section;
1065 if (bfd_get_section_by_name (abfd, sec_name) != NULL)
f786c359 1066 {
16089f32
TH
1067 obj_attribute *attr = elf_known_obj_attributes_proc (abfd);
1068 unsigned int Tag_a = Tag_RISCV_priv_spec;
1069 unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
1070 unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
1071 riscv_get_priv_spec_class_from_numbers (attr[Tag_a].i,
1072 attr[Tag_b].i,
1073 attr[Tag_c].i,
1074 &default_priv_spec);
1075 default_arch = attr[Tag_RISCV_arch].s;
f786c359 1076 }
8152e040 1077 }
f786c359
NC
1078
1079 riscv_release_subset_list (&riscv_subsets);
1080 riscv_parse_subset (&riscv_rps_dis, default_arch);
1081 return print_insn_riscv;
8152e040
NC
1082}
1083
884b49e3
AB
1084/* Prevent use of the fake labels that are generated as part of the DWARF
1085 and for relaxable relocations in the assembler. */
1086
78933a4a 1087bool
884b49e3
AB
1088riscv_symbol_is_valid (asymbol * sym,
1089 struct disassemble_info * info ATTRIBUTE_UNUSED)
1090{
1091 const char * name;
1092
1093 if (sym == NULL)
78933a4a 1094 return false;
884b49e3
AB
1095
1096 name = bfd_asymbol_name (sym);
1097
9b9b1092
NC
1098 return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0
1099 && !riscv_elf_is_mapping_symbols (name));
884b49e3 1100}
3a337a86
AB
1101\f
1102
1103/* Indices into option argument vector for options accepting an argument.
1104 Use RISCV_OPTION_ARG_NONE for options accepting no argument. */
1105
1106typedef enum
1107{
1108 RISCV_OPTION_ARG_NONE = -1,
1109 RISCV_OPTION_ARG_PRIV_SPEC,
1110
1111 RISCV_OPTION_ARG_COUNT
1112} riscv_option_arg_t;
1113
1114/* Valid RISCV disassembler options. */
1115
1116static struct
1117{
1118 const char *name;
1119 const char *description;
1120 riscv_option_arg_t arg;
1121} riscv_options[] =
1122{
1123 { "numeric",
1124 N_("Print numeric register names, rather than ABI names."),
1125 RISCV_OPTION_ARG_NONE },
1126 { "no-aliases",
1127 N_("Disassemble only into canonical instructions."),
1128 RISCV_OPTION_ARG_NONE },
1129 { "priv-spec=",
1130 N_("Print the CSR according to the chosen privilege spec."),
1131 RISCV_OPTION_ARG_PRIV_SPEC }
1132};
1133
1134/* Build the structure representing valid RISCV disassembler options.
1135 This is done dynamically for maintenance ease purpose; a static
1136 initializer would be unreadable. */
1137
1138const disasm_options_and_args_t *
1139disassembler_options_riscv (void)
1140{
1141 static disasm_options_and_args_t *opts_and_args;
1142
1143 if (opts_and_args == NULL)
1144 {
1145 size_t num_options = ARRAY_SIZE (riscv_options);
1146 size_t num_args = RISCV_OPTION_ARG_COUNT;
1147 disasm_option_arg_t *args;
1148 disasm_options_t *opts;
1149 size_t i, priv_spec_count;
1150
1151 args = XNEWVEC (disasm_option_arg_t, num_args + 1);
1152
1153 args[RISCV_OPTION_ARG_PRIV_SPEC].name = "SPEC";
1154 priv_spec_count = PRIV_SPEC_CLASS_DRAFT - PRIV_SPEC_CLASS_NONE - 1;
1155 args[RISCV_OPTION_ARG_PRIV_SPEC].values
1156 = XNEWVEC (const char *, priv_spec_count + 1);
1157 for (i = 0; i < priv_spec_count; i++)
1158 args[RISCV_OPTION_ARG_PRIV_SPEC].values[i]
1159 = riscv_priv_specs[i].name;
1160 /* The array we return must be NULL terminated. */
1161 args[RISCV_OPTION_ARG_PRIV_SPEC].values[i] = NULL;
1162
1163 /* The array we return must be NULL terminated. */
1164 args[num_args].name = NULL;
1165 args[num_args].values = NULL;
1166
1167 opts_and_args = XNEW (disasm_options_and_args_t);
1168 opts_and_args->args = args;
1169
1170 opts = &opts_and_args->options;
1171 opts->name = XNEWVEC (const char *, num_options + 1);
1172 opts->description = XNEWVEC (const char *, num_options + 1);
1173 opts->arg = XNEWVEC (const disasm_option_arg_t *, num_options + 1);
1174 for (i = 0; i < num_options; i++)
1175 {
1176 opts->name[i] = riscv_options[i].name;
1177 opts->description[i] = _(riscv_options[i].description);
1178 if (riscv_options[i].arg != RISCV_OPTION_ARG_NONE)
1179 opts->arg[i] = &args[riscv_options[i].arg];
1180 else
1181 opts->arg[i] = NULL;
1182 }
1183 /* The array we return must be NULL terminated. */
1184 opts->name[i] = NULL;
1185 opts->description[i] = NULL;
1186 opts->arg[i] = NULL;
1187 }
1188
1189 return opts_and_args;
1190}
884b49e3 1191
e23eba97
NC
1192void
1193print_riscv_disassembler_options (FILE *stream)
1194{
3a337a86
AB
1195 const disasm_options_and_args_t *opts_and_args;
1196 const disasm_option_arg_t *args;
1197 const disasm_options_t *opts;
1198 size_t max_len = 0;
1199 size_t i;
1200 size_t j;
1201
1202 opts_and_args = disassembler_options_riscv ();
1203 opts = &opts_and_args->options;
1204 args = opts_and_args->args;
1205
e23eba97 1206 fprintf (stream, _("\n\
3a337a86 1207The following RISC-V specific disassembler options are supported for use\n\
e23eba97 1208with the -M switch (multiple options should be separated by commas):\n"));
3a337a86 1209 fprintf (stream, "\n");
e23eba97 1210
3a337a86
AB
1211 /* Compute the length of the longest option name. */
1212 for (i = 0; opts->name[i] != NULL; i++)
1213 {
1214 size_t len = strlen (opts->name[i]);
8f595e9b 1215
3a337a86
AB
1216 if (opts->arg[i] != NULL)
1217 len += strlen (opts->arg[i]->name);
1218 if (max_len < len)
1219 max_len = len;
1220 }
e23eba97 1221
3a337a86
AB
1222 for (i = 0, max_len++; opts->name[i] != NULL; i++)
1223 {
1224 fprintf (stream, " %s", opts->name[i]);
1225 if (opts->arg[i] != NULL)
1226 fprintf (stream, "%s", opts->arg[i]->name);
1227 if (opts->description[i] != NULL)
1228 {
1229 size_t len = strlen (opts->name[i]);
1230
1231 if (opts->arg != NULL && opts->arg[i] != NULL)
1232 len += strlen (opts->arg[i]->name);
1233 fprintf (stream, "%*c %s", (int) (max_len - len), ' ',
1234 opts->description[i]);
1235 }
1236 fprintf (stream, "\n");
1237 }
1238
1239 for (i = 0; args[i].name != NULL; i++)
1240 {
9869e2e5
TO
1241 if (args[i].values == NULL)
1242 continue;
3a337a86
AB
1243 fprintf (stream, _("\n\
1244 For the options above, the following values are supported for \"%s\":\n "),
1245 args[i].name);
1246 for (j = 0; args[i].values[j] != NULL; j++)
1247 fprintf (stream, " %s", args[i].values[j]);
1248 fprintf (stream, _("\n"));
1249 }
e23eba97
NC
1250
1251 fprintf (stream, _("\n"));
1252}