Skip to content

Commit 43c3bf5

Browse files
committed
RISC-V: Reorganize disassembler state initialization
The current disassembler repeatedly checks current state per instruction: - Whether riscv_gpr_names is initialized to a non-NULL value - Whether the Zfinx extension is available - Whether the hash table is initialized ... but they are not frequently changed. riscv_gpr_names is initialized to a non-NULL value when - The first disassembler option is specified, or - Not initialized with disassembler options (in the first print_insn_riscv function call). We can safely initialize the default disassembler options prior to the print_insn_riscv function call and this per-instruction checking of riscv_gpr_names can be safely removed. The opcode hash table initialization will be taken care with a later commit. To group disassembler state initialization, this commit utilizes the disassemble_init_for_target function. As a side effect, this will also fix a potential issue when disassembler options is set and then unset on GDB. This idea is based on opcodes/ppc-dis.c and opcodes/wasm32-dis.c. New callback function init_riscv_dis_state_for_arch_and_options is called when either the architecture string or an option is possibly changed. We can now group the disassembler state initialization together. It makes state initialization clearer and makes further changes easier. In performance perspective, this commit has various effects (-5% to 6%). However, this commit makes implementing large optimizations easier as well as "RISC-V: Split match/print steps on disassembler". include/ChangeLog: * dis-asm.h (disassemble_init_riscv): Add declaration of disassemble_init_riscv. opcodes/ChangeLog: * disassemble.c (disassemble_init_for_target): Call disassemble_init_riscv to group state initialization together. * riscv-dis.c (xlen_by_mach, xlen_by_elf): New variables to store environment-inferred XLEN. (is_numeric): New. Instead of directly setting riscv_{gpr,fpr}_names, use this to store an option. (update_riscv_dis_xlen): New function to set actual XLEN from xlen_by_mach and xlen_by_elf variables. (init_riscv_dis_state_for_arch_and_options): New callback function called when either the architecture or an option is changed. Set riscv_{gpr,fpr}_names here. (set_default_riscv_dis_options): Initialize is_numeric instead of riscv_gpr_names and riscv_fpr_names. (parse_riscv_dis_option_without_args): When the "numeric" option is specified, write to is_numeric instead of register names. (parse_riscv_dis_options): Suppress setting the default options here and let disassemble_init_riscv to initialize them. (riscv_disassemble_insn): Move probing Zfinx and setting XLEN portions to init_riscv_dis_state_for_arch_and_options and update_riscv_dis_xlen. (riscv_get_map_state): If a mapping symbol with ISA string is suitable, call init_riscv_dis_state_for_arch_and_options function to update disassembler state. (print_insn_riscv): Update XLEN only if we haven't guessed correct XLEN for the disassembler. Stop checking disassembler options for every instruction and let disassemble_init_riscv to parse options. (riscv_get_disassembler): Call init_riscv_dis_state_for_arch_and_options because the architecture string is possibly updated here. (disassemble_init_riscv): New function to initialize the structure, reset/guess correct XLEN and reset/parse disassembler options.
1 parent a7a665b commit 43c3bf5

File tree

3 files changed

+79
-36
lines changed

3 files changed

+79
-36
lines changed

include/dis-asm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ extern bool arm_symbol_is_valid (asymbol *, struct disassemble_info *);
395395
extern bool csky_symbol_is_valid (asymbol *, struct disassemble_info *);
396396
extern bool riscv_symbol_is_valid (asymbol *, struct disassemble_info *);
397397
extern void disassemble_init_powerpc (struct disassemble_info *);
398+
extern void disassemble_init_riscv (struct disassemble_info *);
398399
extern void disassemble_init_s390 (struct disassemble_info *);
399400
extern void disassemble_init_wasm32 (struct disassemble_info *);
400401
extern void disassemble_init_nds32 (struct disassemble_info *);

opcodes/disassemble.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ disassemble_init_for_target (struct disassemble_info * info)
710710
#endif
711711
#ifdef ARCH_riscv
712712
case bfd_arch_riscv:
713-
info->symbol_is_valid = riscv_symbol_is_valid;
713+
disassemble_init_riscv (info);
714714
info->created_styled_output = true;
715715
break;
716716
#endif

opcodes/riscv-dis.c

Lines changed: 77 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
/* Current XLEN for the disassembler. */
3636
static unsigned xlen = 0;
3737

38+
/* XLEN as inferred by the machine architecture. */
39+
static unsigned xlen_by_mach = 0;
40+
41+
/* XLEN as inferred by ELF header. */
42+
static unsigned xlen_by_elf = 0;
43+
3844
/* Default ISA specification version (constant as of now). */
3945
static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1;
4046

@@ -75,16 +81,57 @@ static const char (*riscv_fpr_names)[NRC];
7581

7682
/* If set, disassemble as most general instruction. */
7783
static bool no_aliases = false;
84+
85+
/* If set, disassemble with numeric register names. */
86+
static bool is_numeric = false;
87+
88+
89+
/* Guess and update current XLEN. */
90+
91+
static void
92+
update_riscv_dis_xlen (struct disassemble_info *info)
93+
{
94+
/* Set XLEN with following precedence rules:
95+
1. BFD machine architecture set by either:
96+
a. -m riscv:rv[32|64] option (GDB: set arch riscv:rv[32|64])
97+
b. ELF class in actual ELF header (only on RISC-V ELF)
98+
This is only effective if XLEN-specific BFD machine architecture is
99+
chosen. If XLEN-neutral (like riscv), BFD machine architecture is
100+
ignored on XLEN selection.
101+
2. ELF class in dummy ELF header. */
102+
if (xlen_by_mach != 0)
103+
xlen = xlen_by_mach;
104+
else if (xlen_by_elf != 0)
105+
xlen = xlen_by_elf;
106+
else if (info != NULL && info->section != NULL)
107+
{
108+
Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
109+
xlen = xlen_by_elf = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
110+
}
111+
}
112+
113+
/* Initialization (for arch and options). */
114+
115+
static void
116+
init_riscv_dis_state_for_arch_and_options (void)
117+
{
118+
/* Set GPR register names to disassemble. */
119+
riscv_gpr_names = is_numeric ? riscv_gpr_names_numeric : riscv_gpr_names_abi;
120+
/* Set FPR register names to disassemble. */
121+
riscv_fpr_names
122+
= !riscv_subset_supports (&riscv_rps_dis, "zfinx")
123+
? (is_numeric ? riscv_fpr_names_numeric : riscv_fpr_names_abi)
124+
: riscv_gpr_names;
125+
}
78126

79127

80128
/* Set default RISC-V disassembler options. */
81129

82130
static void
83131
set_default_riscv_dis_options (void)
84132
{
85-
riscv_gpr_names = riscv_gpr_names_abi;
86-
riscv_fpr_names = riscv_fpr_names_abi;
87133
no_aliases = false;
134+
is_numeric = false;
88135
}
89136

90137
/* Parse RISC-V disassembler option (without arguments). */
@@ -95,10 +142,7 @@ parse_riscv_dis_option_without_args (const char *option)
95142
if (strcmp (option, "no-aliases") == 0)
96143
no_aliases = true;
97144
else if (strcmp (option, "numeric") == 0)
98-
{
99-
riscv_gpr_names = riscv_gpr_names_numeric;
100-
riscv_fpr_names = riscv_fpr_names_numeric;
101-
}
145+
is_numeric = true;
102146
else
103147
return false;
104148
return true;
@@ -166,8 +210,6 @@ parse_riscv_dis_options (const char *opts_in)
166210
{
167211
char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts;
168212

169-
set_default_riscv_dis_options ();
170-
171213
for ( ; opt_end != NULL; opt = opt_end + 1)
172214
{
173215
if ((opt_end = strchr (opt, ',')) != NULL)
@@ -745,25 +787,6 @@ riscv_disassemble_insn (bfd_vma memaddr,
745787
matched_op = NULL;
746788
op = riscv_hash[OP_HASH_IDX (word)];
747789

748-
/* If XLEN is not known, get its value from the ELF class. */
749-
if (info->mach == bfd_mach_riscv64)
750-
xlen = 64;
751-
else if (info->mach == bfd_mach_riscv32)
752-
xlen = 32;
753-
else if (info->section != NULL)
754-
{
755-
Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
756-
xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
757-
}
758-
759-
/* If arch has the Zfinx extension, replace FPR with GPR. */
760-
if (riscv_subset_supports (&riscv_rps_dis, "zfinx"))
761-
riscv_fpr_names = riscv_gpr_names;
762-
else
763-
riscv_fpr_names = riscv_gpr_names == riscv_gpr_names_abi
764-
? riscv_fpr_names_abi
765-
: riscv_fpr_names_numeric;
766-
767790
for (; op && op->name; op++)
768791
{
769792
/* Does the opcode match? */
@@ -897,6 +920,7 @@ riscv_get_map_state (int n,
897920
{
898921
riscv_release_subset_list (&riscv_subsets);
899922
riscv_parse_subset (&riscv_rps_dis, arch);
923+
init_riscv_dis_state_for_arch_and_options ();
900924
}
901925
return true;
902926
}
@@ -1161,14 +1185,9 @@ print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
11611185
int (*riscv_disassembler) (bfd_vma, insn_t, const bfd_byte *,
11621186
struct disassemble_info *);
11631187

1164-
if (info->disassembler_options != NULL)
1165-
{
1166-
parse_riscv_dis_options (info->disassembler_options);
1167-
/* Avoid repeatedly parsing the options. */
1168-
info->disassembler_options = NULL;
1169-
}
1170-
else if (riscv_gpr_names == NULL)
1171-
set_default_riscv_dis_options ();
1188+
/* Guess and update XLEN if we haven't determined it yet. */
1189+
if (xlen == 0)
1190+
update_riscv_dis_xlen (info);
11721191

11731192
if (info->private_data == NULL && !riscv_init_disasm_info (info))
11741193
return -1;
@@ -1235,9 +1254,32 @@ riscv_get_disassembler (bfd *abfd)
12351254

12361255
riscv_release_subset_list (&riscv_subsets);
12371256
riscv_parse_subset (&riscv_rps_dis, default_arch);
1257+
init_riscv_dis_state_for_arch_and_options ();
12381258
return print_insn_riscv;
12391259
}
12401260

1261+
/* Initialize disassemble_info and parse options. */
1262+
1263+
void
1264+
disassemble_init_riscv (struct disassemble_info *info)
1265+
{
1266+
info->symbol_is_valid = riscv_symbol_is_valid;
1267+
/* Clear previous XLEN and guess by mach. */
1268+
xlen = 0;
1269+
xlen_by_mach = 0;
1270+
xlen_by_elf = 0;
1271+
if (info->mach == bfd_mach_riscv64)
1272+
xlen_by_mach = 64;
1273+
else if (info->mach == bfd_mach_riscv32)
1274+
xlen_by_mach = 32;
1275+
update_riscv_dis_xlen (info);
1276+
/* Parse disassembler options. */
1277+
set_default_riscv_dis_options ();
1278+
if (info->disassembler_options != NULL)
1279+
parse_riscv_dis_options (info->disassembler_options);
1280+
init_riscv_dis_state_for_arch_and_options ();
1281+
}
1282+
12411283
/* Prevent use of the fake labels that are generated as part of the DWARF
12421284
and for relaxable relocations in the assembler. */
12431285

0 commit comments

Comments
 (0)