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