Skip to content

Commit 63c1ad7

Browse files
committed
RISC-V: Move disassembler private data initialization
Because disassemble_info.private_data could be used not only by riscv_disassemble_insn, this commit splits the initialization of the private data to a separate function. This commit now allows storing mapping symbol and/or section-related information to riscv_private_data. In performance perspective, it also has a penalty. However, it can be easily paid back by other optimizations and it makes implementing some optimizations easier. opcodes/ChangeLog: * riscv-dis.c (init_riscv_dis_private_data): New. (riscv_disassemble_insn): Move private data initialization to init_riscv_dis_private_data. (print_insn_riscv): Start initializing the private data instead of instruction only riscv_disassemble_insn function.
1 parent fd57795 commit 63c1ad7

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

opcodes/riscv-dis.c

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,29 @@ init_riscv_dis_state_for_arch_and_options (void)
219219
is_arch_changed = false;
220220
}
221221

222+
/* Initialize private data of the disassemble_info. */
223+
224+
static void
225+
init_riscv_dis_private_data (struct disassemble_info *info)
226+
{
227+
struct riscv_private_data *pd;
228+
229+
pd = info->private_data = xcalloc (1, sizeof (struct riscv_private_data));
230+
pd->gp = 0;
231+
pd->print_addr = 0;
232+
for (int i = 0; i < (int)ARRAY_SIZE (pd->hi_addr); i++)
233+
pd->hi_addr[i] = -1;
234+
pd->to_print_addr = false;
235+
pd->has_gp = false;
236+
237+
for (int i = 0; i < info->symtab_size; i++)
238+
if (strcmp (bfd_asymbol_name (info->symtab[i]), RISCV_GP_SYMBOL) == 0)
239+
{
240+
pd->gp = bfd_asymbol_value (info->symtab[i]);
241+
pd->has_gp = true;
242+
}
243+
}
244+
222245
/* Update architecture for disassembler with its context.
223246
Call initialization functions if either:
224247
- the architecture for current context is changed or
@@ -806,7 +829,7 @@ riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info)
806829
const struct riscv_opcode *op, *matched_op;
807830
static bool init = false;
808831
static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
809-
struct riscv_private_data *pd;
832+
struct riscv_private_data *pd = info->private_data;
810833
int insnlen;
811834

812835
#define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
@@ -821,28 +844,6 @@ riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info)
821844
init = true;
822845
}
823846

824-
if (info->private_data == NULL)
825-
{
826-
int i;
827-
828-
pd = info->private_data = xcalloc (1, sizeof (struct riscv_private_data));
829-
pd->gp = 0;
830-
pd->print_addr = 0;
831-
for (i = 0; i < (int)ARRAY_SIZE (pd->hi_addr); i++)
832-
pd->hi_addr[i] = -1;
833-
pd->to_print_addr = false;
834-
pd->has_gp = false;
835-
836-
for (i = 0; i < info->symtab_size; i++)
837-
if (strcmp (bfd_asymbol_name (info->symtab[i]), RISCV_GP_SYMBOL) == 0)
838-
{
839-
pd->gp = bfd_asymbol_value (info->symtab[i]);
840-
pd->has_gp = true;
841-
}
842-
}
843-
else
844-
pd = info->private_data;
845-
846847
insnlen = riscv_insn_length (word);
847848

848849
/* RISC-V instructions are always little-endian. */
@@ -1215,6 +1216,10 @@ print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
12151216
enum riscv_seg_mstate mstate;
12161217
int (*riscv_disassembler) (bfd_vma, insn_t, struct disassemble_info *);
12171218

1219+
/* Initialize the private data. */
1220+
if (info->private_data == NULL)
1221+
init_riscv_dis_private_data (info);
1222+
12181223
/* Guess and update XLEN if we haven't determined it yet. */
12191224
if (xlen == 0)
12201225
update_riscv_dis_xlen (info);

0 commit comments

Comments
 (0)