2 Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
7 This file is part of the GNU opcodes library.
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)
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.
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/>. */
24 #include "disassemble.h"
25 #include "libiberty.h"
26 #include "opcode/riscv.h"
29 #include "elf/riscv.h"
30 #include "elfxx-riscv.h"
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
40 /* Current XLEN for the disassembler. */
41 static unsigned xlen
= 0;
43 /* Default ISA specification version (constant as of now). */
44 static enum riscv_spec_class default_isa_spec
= ISA_SPEC_CLASS_DRAFT
- 1;
46 /* Default privileged specification
47 (as specified by the ELF attributes or the `priv-spec' option). */
48 static enum riscv_spec_class default_priv_spec
= PRIV_SPEC_CLASS_NONE
;
50 static riscv_subset_list_t riscv_subsets
;
51 static riscv_parse_subset_t riscv_rps_dis
=
53 &riscv_subsets
, /* subset_list. */
54 opcodes_error_handler
,/* error_handler. */
56 &default_isa_spec
, /* isa_spec. */
57 false, /* check_unknown_prefixed_ext. */
60 struct riscv_private_data
64 bfd_vma hi_addr
[OP_MASK_RD
+ 1];
69 /* Used for mapping symbols. */
70 static int last_map_symbol
= -1;
71 static bfd_vma last_stop_offset
= 0;
72 static bfd_vma last_map_symbol_boundary
= 0;
73 static enum riscv_seg_mstate last_map_state
= MAP_NONE
;
74 static asection
*last_map_section
= NULL
;
76 /* Register names as used by the disassembler. */
77 static const char (*riscv_gpr_names
)[NRC
];
78 static const char (*riscv_fpr_names
)[NRC
];
80 /* If set, disassemble as most general instruction. */
81 static bool no_aliases
= false;
83 /* If set, disassemble without checking architectire string, just like what
84 we did at the beginning. */
85 static bool all_ext
= false;
87 /* Set default RISC-V disassembler options. */
90 set_default_riscv_dis_options (void)
92 riscv_gpr_names
= riscv_gpr_names_abi
;
93 riscv_fpr_names
= riscv_fpr_names_abi
;
97 /* Parse RISC-V disassembler option (without arguments). */
100 parse_riscv_dis_option_without_args (const char *option
)
102 if (strcmp (option
, "no-aliases") == 0)
104 else if (strcmp (option
, "numeric") == 0)
106 riscv_gpr_names
= riscv_gpr_names_numeric
;
107 riscv_fpr_names
= riscv_fpr_names_numeric
;
109 else if (strcmp (option
, "max") == 0)
116 /* Parse RISC-V disassembler option (possibly with arguments). */
119 parse_riscv_dis_option (const char *option
)
123 if (parse_riscv_dis_option_without_args (option
))
126 equal
= strchr (option
, '=');
129 /* The option without '=' should be defined above. */
130 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
134 || *(equal
+ 1) == '\0')
136 /* Invalid options with '=', no option name before '=',
137 and no value after '='. */
138 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
145 if (strcmp (option
, "priv-spec") == 0)
147 enum riscv_spec_class priv_spec
= PRIV_SPEC_CLASS_NONE
;
148 const char *name
= NULL
;
150 RISCV_GET_PRIV_SPEC_CLASS (value
, priv_spec
);
151 if (priv_spec
== PRIV_SPEC_CLASS_NONE
)
152 opcodes_error_handler (_("unknown privileged spec set by %s=%s"),
154 else if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
155 default_priv_spec
= priv_spec
;
156 else if (default_priv_spec
!= priv_spec
)
158 RISCV_GET_PRIV_SPEC_NAME (name
, default_priv_spec
);
159 opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, "
160 "the elf privilege attribute is %s"),
161 option
, value
, name
);
166 /* xgettext:c-format */
167 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
171 /* Parse RISC-V disassembler options. */
174 parse_riscv_dis_options (const char *opts_in
)
176 char *opts
= xstrdup (opts_in
), *opt
= opts
, *opt_end
= opts
;
178 set_default_riscv_dis_options ();
180 for ( ; opt_end
!= NULL
; opt
= opt_end
+ 1)
182 if ((opt_end
= strchr (opt
, ',')) != NULL
)
184 parse_riscv_dis_option (opt
);
190 /* Print one argument from an array. */
193 arg_print (struct disassemble_info
*info
, unsigned long val
,
194 const char* const* array
, size_t size
)
196 const char *s
= val
>= size
|| array
[val
] == NULL
? "unknown" : array
[val
];
197 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "%s", s
);
200 /* If we need to print an address, set its value and state. */
203 maybe_print_address (struct riscv_private_data
*pd
, int base_reg
, int offset
,
206 if (pd
->hi_addr
[base_reg
] != (bfd_vma
)-1)
208 pd
->print_addr
= (base_reg
!= 0 ? pd
->hi_addr
[base_reg
] : 0) + offset
;
209 pd
->hi_addr
[base_reg
] = -1;
211 else if (base_reg
== X_GP
&& pd
->has_gp
)
212 pd
->print_addr
= pd
->gp
+ offset
;
213 else if (base_reg
== X_TP
|| base_reg
== 0)
214 pd
->print_addr
= offset
;
216 return; /* Don't print the address. */
217 pd
->to_print_addr
= true;
219 /* Sign-extend a 32-bit value to a 64-bit value. */
221 pd
->print_addr
= (bfd_vma
)(int32_t) pd
->print_addr
;
223 /* Fit into a 32-bit value on RV32. */
225 pd
->print_addr
= (bfd_vma
)(uint32_t)pd
->print_addr
;
228 /* Get Zcmp reg_list field. */
231 print_reg_list (disassemble_info
*info
, insn_t l
)
233 bool numeric
= riscv_gpr_names
== riscv_gpr_names_numeric
;
234 unsigned reg_list
= (int)EXTRACT_OPERAND (REG_LIST
, l
);
235 unsigned r_start
= numeric
? X_S2
: X_S0
;
236 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
237 "%s", riscv_gpr_names
[X_RA
]);
241 info
->fprintf_styled_func (info
->stream
, dis_style_text
, ",");
242 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
243 "%s", riscv_gpr_names
[X_S0
]);
245 else if (reg_list
== 6 || (numeric
&& reg_list
> 6))
247 info
->fprintf_styled_func (info
->stream
, dis_style_text
, ",");
248 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
249 "%s", riscv_gpr_names
[X_S0
]);
250 info
->fprintf_styled_func (info
->stream
, dis_style_text
, "-");
251 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
252 "%s", riscv_gpr_names
[X_S1
]);
257 info
->fprintf_styled_func (info
->stream
, dis_style_text
, ",");
258 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
259 "%s", riscv_gpr_names
[r_start
]);
260 info
->fprintf_styled_func (info
->stream
, dis_style_text
, "-");
261 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
262 "%s", riscv_gpr_names
[X_S11
]);
264 else if (reg_list
== 7 && numeric
)
266 info
->fprintf_styled_func (info
->stream
, dis_style_text
, ",");
267 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
268 "%s", riscv_gpr_names
[X_S2
]);
270 else if (reg_list
> 6)
272 info
->fprintf_styled_func (info
->stream
, dis_style_text
, ",");
273 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
274 "%s", riscv_gpr_names
[r_start
]);
275 info
->fprintf_styled_func (info
->stream
, dis_style_text
, "-");
276 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
277 "%s", riscv_gpr_names
[reg_list
+ 11]);
281 /* Get Zcmp sp adjustment immediate. */
284 riscv_get_spimm (insn_t l
)
286 int spimm
= riscv_get_sp_base(l
, *riscv_rps_dis
.xlen
);
287 spimm
+= EXTRACT_ZCMP_SPIMM (l
);
288 if (((l
^ MATCH_CM_PUSH
) & MASK_CM_PUSH
) == 0)
293 /* Get s-register regno by using sreg number.
294 e.g. the regno of s0 is 8, so
295 riscv_zcmp_get_sregno (0) equals 8. */
298 riscv_zcmp_get_sregno (unsigned sreg_idx
)
300 return sreg_idx
> 1 ?
301 sreg_idx
+ 16 : sreg_idx
+ 8;
304 /* Print insn arguments for 32/64-bit code. */
307 print_insn_args (const char *oparg
, insn_t l
, bfd_vma pc
, disassemble_info
*info
)
309 struct riscv_private_data
*pd
= info
->private_data
;
310 int rs1
= (l
>> OP_SH_RS1
) & OP_MASK_RS1
;
311 int rd
= (l
>> OP_SH_RD
) & OP_MASK_RD
;
312 fprintf_styled_ftype print
= info
->fprintf_styled_func
;
313 const char *opargStart
;
316 print (info
->stream
, dis_style_text
, "\t");
318 for (; *oparg
!= '\0'; oparg
++)
326 case 's': /* RS1 x8-x15. */
327 case 'w': /* RS1 x8-x15. */
328 print (info
->stream
, dis_style_register
, "%s",
329 riscv_gpr_names
[EXTRACT_OPERAND (CRS1S
, l
) + 8]);
331 case 't': /* RS2 x8-x15. */
332 case 'x': /* RS2 x8-x15. */
333 print (info
->stream
, dis_style_register
, "%s",
334 riscv_gpr_names
[EXTRACT_OPERAND (CRS2S
, l
) + 8]);
336 case 'U': /* RS1, constrained to equal RD. */
337 print (info
->stream
, dis_style_register
,
338 "%s", riscv_gpr_names
[rd
]);
340 case 'c': /* RS1, constrained to equal sp. */
341 print (info
->stream
, dis_style_register
, "%s",
342 riscv_gpr_names
[X_SP
]);
345 print (info
->stream
, dis_style_register
, "%s",
346 riscv_gpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
350 if (((l
& MASK_C_ADDI
) == MATCH_C_ADDI
) && rd
!= 0)
351 maybe_print_address (pd
, rd
, EXTRACT_CITYPE_IMM (l
), 0);
352 if (info
->mach
== bfd_mach_riscv64
353 && ((l
& MASK_C_ADDIW
) == MATCH_C_ADDIW
) && rd
!= 0)
354 maybe_print_address (pd
, rd
, EXTRACT_CITYPE_IMM (l
), 1);
355 print (info
->stream
, dis_style_immediate
, "%d",
356 (int)EXTRACT_CITYPE_IMM (l
));
359 print (info
->stream
, dis_style_address_offset
, "%d",
360 (int)EXTRACT_CLTYPE_LW_IMM (l
));
363 print (info
->stream
, dis_style_address_offset
, "%d",
364 (int)EXTRACT_CLTYPE_LD_IMM (l
));
367 print (info
->stream
, dis_style_address_offset
, "%d",
368 (int)EXTRACT_CITYPE_LWSP_IMM (l
));
371 print (info
->stream
, dis_style_address_offset
, "%d",
372 (int)EXTRACT_CITYPE_LDSP_IMM (l
));
375 print (info
->stream
, dis_style_immediate
, "%d",
376 (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l
));
379 print (info
->stream
, dis_style_immediate
, "%d",
380 (int)EXTRACT_CITYPE_ADDI16SP_IMM (l
));
383 print (info
->stream
, dis_style_address_offset
, "%d",
384 (int)EXTRACT_CSSTYPE_SWSP_IMM (l
));
387 print (info
->stream
, dis_style_address_offset
, "%d",
388 (int)EXTRACT_CSSTYPE_SDSP_IMM (l
));
391 info
->target
= EXTRACT_CBTYPE_IMM (l
) + pc
;
392 (*info
->print_address_func
) (info
->target
, info
);
395 info
->target
= EXTRACT_CJTYPE_IMM (l
) + pc
;
396 (*info
->print_address_func
) (info
->target
, info
);
399 print (info
->stream
, dis_style_immediate
, "0x%x",
400 (unsigned)(EXTRACT_CITYPE_IMM (l
) & (RISCV_BIGIMM_REACH
-1)));
403 print (info
->stream
, dis_style_immediate
, "0x%x",
404 (unsigned)EXTRACT_CITYPE_IMM (l
) & 0x3f);
407 print (info
->stream
, dis_style_immediate
, "0x%x",
408 (unsigned)EXTRACT_CITYPE_IMM (l
) & 0x1f);
410 case 'T': /* Floating-point RS2. */
411 print (info
->stream
, dis_style_register
, "%s",
412 riscv_fpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
414 case 'D': /* Floating-point RS2 x8-x15. */
415 print (info
->stream
, dis_style_register
, "%s",
416 riscv_fpr_names
[EXTRACT_OPERAND (CRS2S
, l
) + 8]);
426 print (info
->stream
, dis_style_register
, "%s",
427 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
430 if (!EXTRACT_OPERAND (VWD
, l
))
431 print (info
->stream
, dis_style_register
, "%s",
434 print (info
->stream
, dis_style_register
, "%s",
435 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
438 print (info
->stream
, dis_style_register
, "%s",
439 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VS1
, l
)]);
442 case 'u': /* VS1 == VS2 already verified at this point. */
443 case 'v': /* VD == VS1 == VS2 already verified at this point. */
444 print (info
->stream
, dis_style_register
, "%s",
445 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VS2
, l
)]);
448 print (info
->stream
, dis_style_register
, "%s",
449 riscv_vecr_names_numeric
[0]);
454 int imm
= (*oparg
== 'b') ? EXTRACT_RVV_VB_IMM (l
)
455 : EXTRACT_RVV_VC_IMM (l
);
456 unsigned int imm_vlmul
= EXTRACT_OPERAND (VLMUL
, imm
);
457 unsigned int imm_vsew
= EXTRACT_OPERAND (VSEW
, imm
);
458 unsigned int imm_vta
= EXTRACT_OPERAND (VTA
, imm
);
459 unsigned int imm_vma
= EXTRACT_OPERAND (VMA
, imm
);
460 unsigned int imm_vtype_res
= (imm
>> 8);
462 if (imm_vsew
< ARRAY_SIZE (riscv_vsew
)
463 && imm_vlmul
< ARRAY_SIZE (riscv_vlmul
)
464 && imm_vta
< ARRAY_SIZE (riscv_vta
)
465 && imm_vma
< ARRAY_SIZE (riscv_vma
)
467 && riscv_vsew
[imm_vsew
] != NULL
468 && riscv_vlmul
[imm_vlmul
] != NULL
)
469 print (info
->stream
, dis_style_text
, "%s,%s,%s,%s",
470 riscv_vsew
[imm_vsew
],
471 riscv_vlmul
[imm_vlmul
], riscv_vta
[imm_vta
],
474 print (info
->stream
, dis_style_immediate
, "%d", imm
);
478 print (info
->stream
, dis_style_immediate
, "%d",
479 (int)EXTRACT_RVV_VI_IMM (l
));
482 print (info
->stream
, dis_style_immediate
, "%d",
483 (int)EXTRACT_RVV_VI_UIMM (l
));
486 print (info
->stream
, dis_style_immediate
, "%d",
487 (int)EXTRACT_RVV_OFFSET (l
));
490 print (info
->stream
, dis_style_immediate
, "%d",
491 (int)EXTRACT_RVV_VI_UIMM6 (l
));
494 if (!EXTRACT_OPERAND (VMASK
, l
))
496 print (info
->stream
, dis_style_text
, ",");
497 print (info
->stream
, dis_style_register
, "%s",
498 riscv_vecm_names_numeric
[0]);
511 print (info
->stream
, dis_style_text
, "%c", *oparg
);
515 /* Only print constant 0 if it is the last argument. */
517 print (info
->stream
, dis_style_immediate
, "0");
521 if ((l
& MASK_JALR
) == MATCH_JALR
)
522 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
523 print (info
->stream
, dis_style_register
, "%s", riscv_gpr_names
[rs1
]);
527 print (info
->stream
, dis_style_register
, "%s",
528 riscv_gpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
532 print (info
->stream
, dis_style_immediate
, "0x%x",
533 (unsigned)EXTRACT_UTYPE_IMM (l
) >> RISCV_IMM_BITS
);
537 arg_print (info
, EXTRACT_OPERAND (RM
, l
),
538 riscv_rm
, ARRAY_SIZE (riscv_rm
));
542 arg_print (info
, EXTRACT_OPERAND (PRED
, l
),
543 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
547 arg_print (info
, EXTRACT_OPERAND (SUCC
, l
),
548 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
552 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
555 if (((l
& MASK_ADDI
) == MATCH_ADDI
&& rs1
!= 0)
556 || (l
& MASK_JALR
) == MATCH_JALR
)
557 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
558 if (info
->mach
== bfd_mach_riscv64
559 && ((l
& MASK_ADDIW
) == MATCH_ADDIW
) && rs1
!= 0)
560 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 1);
561 print (info
->stream
, dis_style_immediate
, "%d",
562 (int)EXTRACT_ITYPE_IMM (l
));
566 maybe_print_address (pd
, rs1
, EXTRACT_STYPE_IMM (l
), 0);
567 print (info
->stream
, dis_style_address_offset
, "%d",
568 (int)EXTRACT_STYPE_IMM (l
));
572 info
->target
= EXTRACT_JTYPE_IMM (l
) + pc
;
573 (*info
->print_address_func
) (info
->target
, info
);
577 info
->target
= EXTRACT_BTYPE_IMM (l
) + pc
;
578 (*info
->print_address_func
) (info
->target
, info
);
582 if ((l
& MASK_AUIPC
) == MATCH_AUIPC
)
583 pd
->hi_addr
[rd
] = pc
+ EXTRACT_UTYPE_IMM (l
);
584 else if ((l
& MASK_LUI
) == MATCH_LUI
)
585 pd
->hi_addr
[rd
] = EXTRACT_UTYPE_IMM (l
);
586 else if ((l
& MASK_C_LUI
) == MATCH_C_LUI
)
587 pd
->hi_addr
[rd
] = EXTRACT_CITYPE_LUI_IMM (l
);
588 print (info
->stream
, dis_style_register
, "%s", riscv_gpr_names
[rd
]);
592 print (info
->stream
, dis_style_immediate
, "0x%x",
593 EXTRACT_OPERAND (BS
, l
));
597 print (info
->stream
, dis_style_register
, "%s", riscv_gpr_names
[0]);
601 print (info
->stream
, dis_style_immediate
, "0x%x",
602 EXTRACT_OPERAND (SHAMT
, l
));
606 print (info
->stream
, dis_style_immediate
, "0x%x",
607 EXTRACT_OPERAND (SHAMTW
, l
));
612 print (info
->stream
, dis_style_register
, "%s", riscv_fpr_names
[rs1
]);
616 print (info
->stream
, dis_style_register
, "%s",
617 riscv_fpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
621 print (info
->stream
, dis_style_register
, "%s", riscv_fpr_names
[rd
]);
625 print (info
->stream
, dis_style_register
, "%s",
626 riscv_fpr_names
[EXTRACT_OPERAND (RS3
, l
)]);
631 static const char *riscv_csr_hash
[4096]; /* Total 2^12 CSRs. */
632 static bool init_csr
= false;
633 unsigned int csr
= EXTRACT_OPERAND (CSR
, l
);
638 for (i
= 0; i
< 4096; i
++)
639 riscv_csr_hash
[i
] = NULL
;
641 /* Set to the newest privileged version. */
642 if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
643 default_priv_spec
= PRIV_SPEC_CLASS_DRAFT
- 1;
645 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
646 if (riscv_csr_hash[num] == NULL \
647 && ((define_version == PRIV_SPEC_CLASS_NONE \
648 && abort_version == PRIV_SPEC_CLASS_NONE) \
649 || (default_priv_spec >= define_version \
650 && default_priv_spec < abort_version))) \
651 riscv_csr_hash[num] = #name;
652 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
653 DECLARE_CSR (name, num, class, define_version, abort_version)
654 #include "opcode/riscv-opc.h"
658 if (riscv_csr_hash
[csr
] != NULL
)
659 if (riscv_subset_supports (&riscv_rps_dis
, "xtheadvector")
660 && (csr
== CSR_VSTART
665 || csr
== CSR_VLENB
))
666 print (info
->stream
, dis_style_register
, "%s",
667 concat ("th.", riscv_csr_hash
[csr
], NULL
));
669 print (info
->stream
, dis_style_register
, "%s",
670 riscv_csr_hash
[csr
]);
672 print (info
->stream
, dis_style_immediate
, "0x%x", csr
);
677 print (info
->stream
, dis_style_immediate
, "0x%x",
678 EXTRACT_OPERAND (RNUM
, l
));
682 print (info
->stream
, dis_style_immediate
, "%d", rs1
);
685 case 'W': /* Various operands for standard z extensions. */
692 print (info
->stream
, dis_style_address_offset
, "%d",
693 (int) EXTRACT_STYPE_IMM (l
));
696 goto undefined_modifier
;
703 if (riscv_fli_symval
[rs1
])
704 print (info
->stream
, dis_style_text
, "%s",
705 riscv_fli_symval
[rs1
]);
707 print (info
->stream
, dis_style_immediate
, "%a",
708 riscv_fli_numval
[rs1
]);
711 goto undefined_modifier
;
714 case 'c': /* Zcb extension 16 bits length instruction fields. */
718 print (info
->stream
, dis_style_register
, "%s",
719 riscv_gpr_names
[riscv_zcmp_get_sregno (EXTRACT_OPERAND (SREG1
, l
))]);
722 print (info
->stream
, dis_style_register
, "%s",
723 riscv_gpr_names
[riscv_zcmp_get_sregno (EXTRACT_OPERAND (SREG2
, l
))]);
726 print (info
->stream
, dis_style_immediate
, "%d",
727 (int)EXTRACT_ZCB_BYTE_UIMM (l
));
730 print (info
->stream
, dis_style_immediate
, "%d",
731 (int)EXTRACT_ZCB_HALFWORD_UIMM (l
));
734 print_reg_list (info
, l
);
737 print (info
->stream
, dis_style_immediate
, "%d",
738 riscv_get_spimm (l
));
742 print (info
->stream
, dis_style_address_offset
,
743 "%lu", EXTRACT_ZCMT_INDEX (l
));
746 goto undefined_modifier
;
750 goto undefined_modifier
;
754 case 'X': /* Vendor-specific operands. */
757 case 't': /* Vendor-specific (T-head) operands. */
767 goto undefined_modifier
;
769 int imm
= (*oparg
== 'b') ? EXTRACT_RVV_VB_IMM (l
)
770 : EXTRACT_RVV_VC_IMM (l
);
771 unsigned int imm_vediv
= EXTRACT_OPERAND (XTHEADVEDIV
, imm
);
772 unsigned int imm_vlmul
= EXTRACT_OPERAND (XTHEADVLMUL
, imm
);
773 unsigned int imm_vsew
= EXTRACT_OPERAND (XTHEADVSEW
, imm
);
774 unsigned int imm_vtype_res
775 = EXTRACT_OPERAND (XTHEADVTYPE_RES
, imm
);
776 if (imm_vsew
< ARRAY_SIZE (riscv_vsew
)
777 && imm_vlmul
< ARRAY_SIZE (riscv_th_vlen
)
778 && imm_vediv
< ARRAY_SIZE (riscv_th_vediv
)
780 print (info
->stream
, dis_style_text
, "%s,%s,%s",
781 riscv_vsew
[imm_vsew
], riscv_th_vlen
[imm_vlmul
],
782 riscv_th_vediv
[imm_vediv
]);
784 print (info
->stream
, dis_style_immediate
, "%d", imm
);
786 case 'l': /* Integer immediate, literal. */
788 while (*oparg
&& *oparg
!= ',')
790 print (info
->stream
, dis_style_immediate
, "%c", *oparg
);
795 case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S. */
798 case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S. */
802 n
= strtol (oparg
+ 1, (char **)&oparg
, 10);
804 goto undefined_modifier
;
805 s
= strtol (oparg
+ 1, (char **)&oparg
, 10);
809 print (info
->stream
, dis_style_immediate
, "%lu",
810 (unsigned long)EXTRACT_U_IMM (n
, s
, l
));
812 print (info
->stream
, dis_style_immediate
, "%li",
813 (signed long)EXTRACT_S_IMM (n
, s
, l
));
816 goto undefined_modifier
;
820 case 'c': /* Vendor-specific (CORE-V) operands. */
824 print (info
->stream
, dis_style_immediate
, "%d",
825 ((int) EXTRACT_CV_IS2_UIMM5 (l
)));
828 print (info
->stream
, dis_style_immediate
, "%d",
829 ((int) EXTRACT_CV_IS3_UIMM5 (l
)));
832 print (info
->stream
, dis_style_immediate
, "%d",
833 ((int) EXTRACT_CV_BI_IMM5 (l
)));
836 print (info
->stream
, dis_style_immediate
, "%d",
837 ((int) EXTRACT_CV_SIMD_IMM6 (l
)));
840 print (info
->stream
, dis_style_immediate
, "%d",
841 ((int) EXTRACT_CV_BITMANIP_UIMM5 (l
)));
844 print (info
->stream
, dis_style_immediate
, "%d",
845 ((int) EXTRACT_CV_BITMANIP_UIMM2 (l
)));
848 print (info
->stream
, dis_style_immediate
, "%d",
849 ((int) EXTRACT_CV_SIMD_UIMM6 (l
)));
853 goto undefined_modifier
;
856 case 's': /* Vendor-specific (SiFive) operands. */
859 /* SiFive vector coprocessor interface. */
861 print (info
->stream
, dis_style_register
, "0x%x",
862 (unsigned) EXTRACT_OPERAND (RD
, l
));
865 print (info
->stream
, dis_style_register
, "0x%x",
866 (unsigned) EXTRACT_OPERAND (RS2
, l
));
872 print (info
->stream
, dis_style_register
, "0x%x",
873 (unsigned) EXTRACT_OPERAND (XSO2
, l
));
876 print (info
->stream
, dis_style_register
, "0x%x",
877 (unsigned) EXTRACT_OPERAND (XSO1
, l
));
884 goto undefined_modifier
;
890 /* xgettext:c-format */
891 print (info
->stream
, dis_style_text
,
892 _("# internal error, undefined modifier (%c)"),
899 /* Print the RISC-V instruction at address MEMADDR in debugged memory,
900 on using INFO. Returns length of the instruction, in bytes.
901 BIGENDIAN must be 1 if this is big-endian code, 0 if
902 this is little-endian code. */
905 riscv_disassemble_insn (bfd_vma memaddr
,
907 const bfd_byte
*packet
,
908 disassemble_info
*info
)
910 const struct riscv_opcode
*op
;
911 static bool init
= false;
912 static const struct riscv_opcode
*riscv_hash
[OP_MASK_OP
+ 1];
913 struct riscv_private_data
*pd
= info
->private_data
;
917 #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
919 /* Build a hash table to shorten the search time. */
922 for (op
= riscv_opcodes
; op
->name
; op
++)
923 if (!riscv_hash
[OP_HASH_IDX (op
->match
)])
924 riscv_hash
[OP_HASH_IDX (op
->match
)] = op
;
929 insnlen
= riscv_insn_length (word
);
931 /* RISC-V instructions are always little-endian. */
932 info
->endian_code
= BFD_ENDIAN_LITTLE
;
934 info
->bytes_per_chunk
= insnlen
% 4 == 0 ? 4 : 2;
935 info
->bytes_per_line
= 8;
936 /* We don't support constant pools, so this must be code. */
937 info
->display_endian
= info
->endian_code
;
938 info
->insn_info_valid
= 1;
939 info
->branch_delay_insns
= 0;
941 info
->insn_type
= dis_nonbranch
;
945 op
= riscv_hash
[OP_HASH_IDX (word
)];
948 /* If XLEN is not known, get its value from the ELF class. */
949 if (info
->mach
== bfd_mach_riscv64
)
951 else if (info
->mach
== bfd_mach_riscv32
)
953 else if (info
->section
!= NULL
)
955 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (info
->section
->owner
);
956 xlen
= ehdr
->e_ident
[EI_CLASS
] == ELFCLASS64
? 64 : 32;
959 /* If arch has the Zfinx extension, replace FPR with GPR. */
960 if (riscv_subset_supports (&riscv_rps_dis
, "zfinx"))
961 riscv_fpr_names
= riscv_gpr_names
;
963 riscv_fpr_names
= riscv_gpr_names
== riscv_gpr_names_abi
?
964 riscv_fpr_names_abi
: riscv_fpr_names_numeric
;
966 for (; op
->name
; op
++)
968 /* Ignore macro insns. */
969 if (op
->pinfo
== INSN_MACRO
)
971 /* Does the opcode match? */
972 if (! (op
->match_func
) (op
, word
))
974 /* Is this a pseudo-instruction and may we print it as such? */
975 if (no_aliases
&& (op
->pinfo
& INSN_ALIAS
))
977 /* Is this instruction restricted to a certain value of XLEN? */
978 if ((op
->xlen_requirement
!= 0) && (op
->xlen_requirement
!= xlen
))
980 /* Is this instruction supported by the current architecture? */
982 && !riscv_multi_subset_supports (&riscv_rps_dis
, op
->insn_class
))
986 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_mnemonic
,
988 print_insn_args (op
->args
, word
, memaddr
, info
);
990 /* Try to disassemble multi-instruction addressing sequences. */
991 if (pd
->to_print_addr
)
993 info
->target
= pd
->print_addr
;
994 (*info
->fprintf_styled_func
)
995 (info
->stream
, dis_style_comment_start
, " # ");
996 (*info
->print_address_func
) (info
->target
, info
);
997 pd
->to_print_addr
= false;
1000 /* Finish filling out insn_info fields. */
1001 switch (op
->pinfo
& INSN_TYPE
)
1004 info
->insn_type
= dis_branch
;
1006 case INSN_CONDBRANCH
:
1007 info
->insn_type
= dis_condbranch
;
1010 info
->insn_type
= dis_jsr
;
1013 info
->insn_type
= dis_dref
;
1019 if (op
->pinfo
& INSN_DATA_SIZE
)
1021 int size
= ((op
->pinfo
& INSN_DATA_SIZE
)
1022 >> INSN_DATA_SIZE_SHIFT
);
1023 info
->data_size
= 1 << (size
- 1);
1030 /* We did not find a match, so just print the instruction bits in
1031 the shape of an assembler .insn directive. */
1032 info
->insn_type
= dis_noninsn
;
1033 (*info
->fprintf_styled_func
)
1034 (info
->stream
, dis_style_assembler_directive
, ".insn");
1035 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1036 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1038 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, ", ");
1039 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
, "0x");
1040 for (i
= insnlen
, printed
= false; i
>= 2; )
1043 word
= bfd_get_bits (packet
+ i
, 16, false);
1044 if (!word
&& !printed
&& i
)
1047 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1048 "%04x", (unsigned int) word
);
1055 /* If we find the suitable mapping symbol update the STATE.
1056 Otherwise, do nothing. */
1059 riscv_update_map_state (int n
,
1060 enum riscv_seg_mstate
*state
,
1061 struct disassemble_info
*info
)
1065 /* If the symbol is in a different section, ignore it. */
1066 if (info
->section
!= NULL
1067 && info
->section
!= info
->symtab
[n
]->section
)
1070 name
= bfd_asymbol_name(info
->symtab
[n
]);
1071 if (strcmp (name
, "$x") == 0)
1073 else if (strcmp (name
, "$d") == 0)
1075 else if (strncmp (name
, "$xrv", 4) == 0)
1078 riscv_release_subset_list (&riscv_subsets
);
1080 /* ISA mapping string may be numbered, suffixed with '.n'. Do not
1081 consider this as part of the ISA string. */
1082 char *suffix
= strchr (name
, '.');
1085 int suffix_index
= (int)(suffix
- name
);
1086 char *name_substr
= xmalloc (suffix_index
+ 1);
1087 strncpy (name_substr
, name
, suffix_index
);
1088 name_substr
[suffix_index
] = '\0';
1089 riscv_parse_subset (&riscv_rps_dis
, name_substr
+ 2);
1093 riscv_parse_subset (&riscv_rps_dis
, name
+ 2);
1097 /* Return true if we find the suitable mapping symbol.
1098 Otherwise, return false. */
1101 riscv_is_valid_mapping_symbol (int n
,
1102 struct disassemble_info
*info
)
1106 /* If the symbol is in a different section, ignore it. */
1107 if (info
->section
!= NULL
1108 && info
->section
!= info
->symtab
[n
]->section
)
1111 name
= bfd_asymbol_name(info
->symtab
[n
]);
1112 return riscv_elf_is_mapping_symbols (name
);
1115 /* Check the sorted symbol table (sorted by the symbol value), find the
1116 suitable mapping symbols. */
1118 static enum riscv_seg_mstate
1119 riscv_search_mapping_symbol (bfd_vma memaddr
,
1120 struct disassemble_info
*info
)
1122 enum riscv_seg_mstate mstate
;
1123 bool from_last_map_symbol
;
1128 /* Return the last map state if the address is still within the range of the
1129 last mapping symbol. */
1130 if (last_map_section
== info
->section
1131 && (memaddr
< last_map_symbol_boundary
))
1132 return last_map_state
;
1134 last_map_section
= info
->section
;
1136 /* Decide whether to print the data or instruction by default, in case
1137 we can not find the corresponding mapping symbols. */
1140 && info
->section
->flags
& SEC_CODE
)
1144 if (info
->symtab_size
== 0
1145 || bfd_asymbol_flavour (*info
->symtab
) != bfd_target_elf_flavour
)
1148 /* Reset the last_map_symbol if we start to dump a new section. */
1150 last_map_symbol
= -1;
1152 /* If the last stop offset is different from the current one, then
1153 don't use the last_map_symbol to search. We usually reset the
1154 info->stop_offset when handling a new section. */
1155 from_last_map_symbol
= (last_map_symbol
>= 0
1156 && info
->stop_offset
== last_stop_offset
);
1158 /* Start scanning from wherever we finished last time, or the start
1160 n
= from_last_map_symbol
? last_map_symbol
: info
->symtab_pos
+ 1;
1162 /* Find the suitable mapping symbol to dump. */
1163 for (; n
< info
->symtab_size
; n
++)
1165 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
1166 /* We have searched all possible symbols in the range. */
1169 if (riscv_is_valid_mapping_symbol (n
, info
))
1173 /* Do not stop searching, in case there are some mapping
1174 symbols have the same value, but have different names.
1175 Use the last one. */
1179 /* We can not find the suitable mapping symbol above. Therefore, we
1180 look forwards and try to find it again, but don't go past the start
1181 of the section. Otherwise a data section without mapping symbols
1182 can pick up a text mapping symbol of a preceeding section. */
1185 n
= from_last_map_symbol
? last_map_symbol
: info
->symtab_pos
;
1189 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
1190 /* We have searched all possible symbols in the range. */
1191 if (addr
< (info
->section
? info
->section
->vma
: 0))
1193 /* Stop searching once we find the closed mapping symbol. */
1194 if (riscv_is_valid_mapping_symbol (n
, info
))
1205 riscv_update_map_state (symbol
, &mstate
, info
);
1207 /* Find the next mapping symbol to determine the boundary of this mapping
1210 bool found_next
= false;
1211 /* Try to found next mapping symbol. */
1212 for (n
= symbol
+ 1; n
< info
->symtab_size
; n
++)
1214 if (info
->symtab
[symbol
]->section
!= info
->symtab
[n
]->section
)
1217 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
1218 const char *sym_name
= bfd_asymbol_name(info
->symtab
[n
]);
1219 if (sym_name
[0] == '$' && (sym_name
[1] == 'x' || sym_name
[1] == 'd'))
1221 /* The next mapping symbol has been found, and it represents the
1222 boundary of this mapping symbol. */
1224 last_map_symbol_boundary
= addr
;
1229 /* No further mapping symbol has been found, indicating that the boundary
1230 of the current mapping symbol is the end of this section. */
1232 last_map_symbol_boundary
= info
->section
->vma
+ info
->section
->size
;
1235 /* Save the information for next use. */
1236 last_map_symbol
= symbol
;
1237 last_stop_offset
= info
->stop_offset
;
1242 /* Decide which data size we should print. */
1245 riscv_data_length (bfd_vma memaddr
,
1246 disassemble_info
*info
)
1252 if (info
->symtab_size
!= 0
1253 && bfd_asymbol_flavour (*info
->symtab
) == bfd_target_elf_flavour
1254 && last_map_symbol
>= 0)
1257 enum riscv_seg_mstate m
= MAP_NONE
;
1258 for (n
= last_map_symbol
+ 1; n
< info
->symtab_size
; n
++)
1260 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
1262 && riscv_is_valid_mapping_symbol (n
, info
))
1264 if (addr
- memaddr
< length
)
1265 length
= addr
- memaddr
;
1267 riscv_update_map_state (n
, &m
, info
);
1274 /* Do not set the length which exceeds the section size. */
1275 bfd_vma offset
= info
->section
->vma
+ info
->section
->size
;
1277 length
= (offset
< length
) ? offset
: length
;
1279 length
= length
== 3 ? 2 : length
;
1283 /* Dump the data contents. */
1286 riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED
,
1288 const bfd_byte
*packet ATTRIBUTE_UNUSED
,
1289 disassemble_info
*info
)
1291 info
->display_endian
= info
->endian
;
1293 switch (info
->bytes_per_chunk
)
1296 info
->bytes_per_line
= 6;
1297 (*info
->fprintf_styled_func
)
1298 (info
->stream
, dis_style_assembler_directive
, ".byte");
1299 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1300 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1301 "0x%02x", (unsigned)data
);
1304 info
->bytes_per_line
= 8;
1305 (*info
->fprintf_styled_func
)
1306 (info
->stream
, dis_style_assembler_directive
, ".short");
1307 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1308 (*info
->fprintf_styled_func
)
1309 (info
->stream
, dis_style_immediate
, "0x%04x", (unsigned) data
);
1312 info
->bytes_per_line
= 8;
1313 (*info
->fprintf_styled_func
)
1314 (info
->stream
, dis_style_assembler_directive
, ".word");
1315 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1316 (*info
->fprintf_styled_func
)
1317 (info
->stream
, dis_style_immediate
, "0x%08lx",
1318 (unsigned long) data
);
1321 info
->bytes_per_line
= 8;
1322 (*info
->fprintf_styled_func
)
1323 (info
->stream
, dis_style_assembler_directive
, ".dword");
1324 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1325 (*info
->fprintf_styled_func
)
1326 (info
->stream
, dis_style_immediate
, "0x%016llx",
1327 (unsigned long long) data
);
1332 return info
->bytes_per_chunk
;
1336 riscv_init_disasm_info (struct disassemble_info
*info
)
1340 struct riscv_private_data
*pd
=
1341 xcalloc (1, sizeof (struct riscv_private_data
));
1344 for (i
= 0; i
< (int) ARRAY_SIZE (pd
->hi_addr
); i
++)
1345 pd
->hi_addr
[i
] = -1;
1346 pd
->to_print_addr
= false;
1349 for (i
= 0; i
< info
->symtab_size
; i
++)
1351 asymbol
*sym
= info
->symtab
[i
];
1352 if (strcmp (bfd_asymbol_name (sym
), RISCV_GP_SYMBOL
) == 0)
1354 pd
->gp
= bfd_asymbol_value (sym
);
1359 info
->private_data
= pd
;
1364 print_insn_riscv (bfd_vma memaddr
, struct disassemble_info
*info
)
1366 bfd_byte packet
[RISCV_MAX_INSN_LEN
];
1370 enum riscv_seg_mstate mstate
;
1371 int (*riscv_disassembler
) (bfd_vma
, insn_t
, const bfd_byte
*,
1372 struct disassemble_info
*);
1374 if (info
->disassembler_options
!= NULL
)
1376 parse_riscv_dis_options (info
->disassembler_options
);
1377 /* Avoid repeatedly parsing the options. */
1378 info
->disassembler_options
= NULL
;
1380 else if (riscv_gpr_names
== NULL
)
1381 set_default_riscv_dis_options ();
1383 if (info
->private_data
== NULL
&& !riscv_init_disasm_info (info
))
1386 mstate
= riscv_search_mapping_symbol (memaddr
, info
);
1387 /* Save the last mapping state. */
1388 last_map_state
= mstate
;
1390 /* Set the size to dump. */
1391 if (mstate
== MAP_DATA
1392 && (info
->flags
& DISASSEMBLE_DATA
) == 0)
1394 dump_size
= riscv_data_length (memaddr
, info
);
1395 info
->bytes_per_chunk
= dump_size
;
1396 riscv_disassembler
= riscv_disassemble_data
;
1400 /* Get the first 2-bytes to check the lenghth of instruction. */
1401 status
= (*info
->read_memory_func
) (memaddr
, packet
, 2, info
);
1404 (*info
->memory_error_func
) (status
, memaddr
, info
);
1407 insn
= (insn_t
) bfd_getl16 (packet
);
1408 dump_size
= riscv_insn_length (insn
);
1409 riscv_disassembler
= riscv_disassemble_insn
;
1412 /* Fetch the instruction to dump. */
1413 status
= (*info
->read_memory_func
) (memaddr
, packet
, dump_size
, info
);
1416 (*info
->memory_error_func
) (status
, memaddr
, info
);
1419 insn
= (insn_t
) bfd_get_bits (packet
, dump_size
* 8, false);
1421 return (*riscv_disassembler
) (memaddr
, insn
, packet
, info
);
1425 riscv_get_disassembler (bfd
*abfd
)
1427 const char *default_arch
= "rv64gc";
1429 if (abfd
&& bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1431 const char *sec_name
= get_elf_backend_data (abfd
)->obj_attrs_section
;
1432 if (bfd_get_section_by_name (abfd
, sec_name
) != NULL
)
1434 obj_attribute
*attr
= elf_known_obj_attributes_proc (abfd
);
1435 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
1436 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
1437 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
1438 riscv_get_priv_spec_class_from_numbers (attr
[Tag_a
].i
,
1441 &default_priv_spec
);
1442 default_arch
= attr
[Tag_RISCV_arch
].s
;
1446 riscv_release_subset_list (&riscv_subsets
);
1447 riscv_parse_subset (&riscv_rps_dis
, default_arch
);
1448 return print_insn_riscv
;
1451 /* Prevent use of the fake labels that are generated as part of the DWARF
1452 and for relaxable relocations in the assembler. */
1455 riscv_symbol_is_valid (asymbol
* sym
,
1456 struct disassemble_info
* info ATTRIBUTE_UNUSED
)
1463 name
= bfd_asymbol_name (sym
);
1465 return (strcmp (name
, RISCV_FAKE_LABEL_NAME
) != 0
1466 && !riscv_elf_is_mapping_symbols (name
));
1470 /* Indices into option argument vector for options accepting an argument.
1471 Use RISCV_OPTION_ARG_NONE for options accepting no argument. */
1475 RISCV_OPTION_ARG_NONE
= -1,
1476 RISCV_OPTION_ARG_PRIV_SPEC
,
1478 RISCV_OPTION_ARG_COUNT
1479 } riscv_option_arg_t
;
1481 /* Valid RISCV disassembler options. */
1486 const char *description
;
1487 riscv_option_arg_t arg
;
1491 N_("Print numeric register names, rather than ABI names."),
1492 RISCV_OPTION_ARG_NONE
},
1494 N_("Disassemble only into canonical instructions."),
1495 RISCV_OPTION_ARG_NONE
},
1497 N_("Print the CSR according to the chosen privilege spec."),
1498 RISCV_OPTION_ARG_PRIV_SPEC
}
1501 /* Build the structure representing valid RISCV disassembler options.
1502 This is done dynamically for maintenance ease purpose; a static
1503 initializer would be unreadable. */
1505 const disasm_options_and_args_t
*
1506 disassembler_options_riscv (void)
1508 static disasm_options_and_args_t
*opts_and_args
;
1510 if (opts_and_args
== NULL
)
1512 size_t num_options
= ARRAY_SIZE (riscv_options
);
1513 size_t num_args
= RISCV_OPTION_ARG_COUNT
;
1514 disasm_option_arg_t
*args
;
1515 disasm_options_t
*opts
;
1516 size_t i
, priv_spec_count
;
1518 args
= XNEWVEC (disasm_option_arg_t
, num_args
+ 1);
1520 args
[RISCV_OPTION_ARG_PRIV_SPEC
].name
= "SPEC";
1521 priv_spec_count
= PRIV_SPEC_CLASS_DRAFT
- PRIV_SPEC_CLASS_NONE
- 1;
1522 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
1523 = XNEWVEC (const char *, priv_spec_count
+ 1);
1524 for (i
= 0; i
< priv_spec_count
; i
++)
1525 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
]
1526 = riscv_priv_specs
[i
].name
;
1527 /* The array we return must be NULL terminated. */
1528 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
] = NULL
;
1530 /* The array we return must be NULL terminated. */
1531 args
[num_args
].name
= NULL
;
1532 args
[num_args
].values
= NULL
;
1534 opts_and_args
= XNEW (disasm_options_and_args_t
);
1535 opts_and_args
->args
= args
;
1537 opts
= &opts_and_args
->options
;
1538 opts
->name
= XNEWVEC (const char *, num_options
+ 1);
1539 opts
->description
= XNEWVEC (const char *, num_options
+ 1);
1540 opts
->arg
= XNEWVEC (const disasm_option_arg_t
*, num_options
+ 1);
1541 for (i
= 0; i
< num_options
; i
++)
1543 opts
->name
[i
] = riscv_options
[i
].name
;
1544 opts
->description
[i
] = _(riscv_options
[i
].description
);
1545 if (riscv_options
[i
].arg
!= RISCV_OPTION_ARG_NONE
)
1546 opts
->arg
[i
] = &args
[riscv_options
[i
].arg
];
1548 opts
->arg
[i
] = NULL
;
1550 /* The array we return must be NULL terminated. */
1551 opts
->name
[i
] = NULL
;
1552 opts
->description
[i
] = NULL
;
1553 opts
->arg
[i
] = NULL
;
1556 return opts_and_args
;
1560 print_riscv_disassembler_options (FILE *stream
)
1562 const disasm_options_and_args_t
*opts_and_args
;
1563 const disasm_option_arg_t
*args
;
1564 const disasm_options_t
*opts
;
1569 opts_and_args
= disassembler_options_riscv ();
1570 opts
= &opts_and_args
->options
;
1571 args
= opts_and_args
->args
;
1573 fprintf (stream
, _("\n\
1574 The following RISC-V specific disassembler options are supported for use\n\
1575 with the -M switch (multiple options should be separated by commas):\n"));
1576 fprintf (stream
, "\n");
1578 /* Compute the length of the longest option name. */
1579 for (i
= 0; opts
->name
[i
] != NULL
; i
++)
1581 size_t len
= strlen (opts
->name
[i
]);
1583 if (opts
->arg
[i
] != NULL
)
1584 len
+= strlen (opts
->arg
[i
]->name
);
1589 for (i
= 0, max_len
++; opts
->name
[i
] != NULL
; i
++)
1591 fprintf (stream
, " %s", opts
->name
[i
]);
1592 if (opts
->arg
[i
] != NULL
)
1593 fprintf (stream
, "%s", opts
->arg
[i
]->name
);
1594 if (opts
->description
[i
] != NULL
)
1596 size_t len
= strlen (opts
->name
[i
]);
1598 if (opts
->arg
!= NULL
&& opts
->arg
[i
] != NULL
)
1599 len
+= strlen (opts
->arg
[i
]->name
);
1600 fprintf (stream
, "%*c %s", (int) (max_len
- len
), ' ',
1601 opts
->description
[i
]);
1603 fprintf (stream
, "\n");
1606 for (i
= 0; args
[i
].name
!= NULL
; i
++)
1608 if (args
[i
].values
== NULL
)
1610 fprintf (stream
, _("\n\
1611 For the options above, the following values are supported for \"%s\":\n "),
1613 for (j
= 0; args
[i
].values
[j
] != NULL
; j
++)
1614 fprintf (stream
, " %s", args
[i
].values
[j
]);
1615 fprintf (stream
, _("\n"));
1618 fprintf (stream
, _("\n"));
1621 void disassemble_free_riscv (struct disassemble_info
*info ATTRIBUTE_UNUSED
)
1623 riscv_release_subset_list (&riscv_subsets
);