Skip to content

Commit 76c9090

Browse files
committed
RISC-V: Fallback on faster hash table
Although it does not have a problem on current GNU Binutils implementation, if the custom vendor implements an instruction which spans across multiple major opcodes (e.g. uses both CUSTOM_0 and CUSTOM_1 in a *single* custom instruction), the original assumption of the sorted hash table breaks. In this case, this commit enables the fallback mode to disable all optimizations except filtering macros out. Note that, if a such instruction (that disables this disassembler optimization) is upstreamed to Binutils, a separate solution will be required to avoid major performance degradation when such instruction is not used. The intent of this commit is to make a room for custom vendors to implement such instructions in *their* tree without causing disassembler problems. opcodes/ChangeLog: * riscv-dis.c (is_riscv_hash_fallback) New. (build_riscv_opcodes_hash_table): If an instruction spans across multiple major opcodes, enable fallback mode and disable sorting. (riscv_disassemble_insn): If the fallback mode is enabled, scan through all instructions instead of scanning only instruction entries matching the hash value.
1 parent c81481a commit 76c9090

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

opcodes/riscv-dis.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,9 @@ print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info
912912
static const struct riscv_opcode **riscv_hash[OP_HASH_LEN + 1];
913913
static const struct riscv_opcode **riscv_opcodes_sorted;
914914

915+
/* Whether the fallback should be used. */
916+
static bool is_riscv_hash_fallback = false;
917+
915918
/* Compare two riscv_opcode* objects to sort by hash index. */
916919

917920
static int
@@ -942,15 +945,25 @@ build_riscv_opcodes_hash_table (void)
942945

943946
/* Sort riscv_opcodes entry pointers (except macros). */
944947
for (op = riscv_opcodes; op->name; op++)
945-
if (op->pinfo != INSN_MACRO)
948+
{
949+
if (op->pinfo == INSN_MACRO)
950+
continue;
946951
len++;
952+
if (is_riscv_hash_fallback)
953+
continue;
954+
if (OP_HASH_IDX (op->match) < OP_MASK_OP2
955+
? (op->mask & OP_MASK_OP2) != OP_MASK_OP2
956+
: (op->mask & OP_MASK_OP) != OP_MASK_OP)
957+
is_riscv_hash_fallback = true;
958+
}
947959
riscv_opcodes_sorted = xcalloc (len, sizeof (struct riscv_opcode *));
948960
pop_end = riscv_opcodes_sorted;
949961
for (op = riscv_opcodes; op->name; op++)
950962
if (op->pinfo != INSN_MACRO)
951963
*pop_end++ = op;
952-
qsort (riscv_opcodes_sorted, len, sizeof (struct riscv_opcode *),
953-
compare_opcodes);
964+
if (!is_riscv_hash_fallback)
965+
qsort (riscv_opcodes_sorted, len, sizeof (struct riscv_opcode *),
966+
compare_opcodes);
954967

955968
/* Initialize faster hash table. */
956969
pop = riscv_opcodes_sorted;
@@ -997,8 +1010,16 @@ riscv_disassemble_insn (bfd_vma memaddr,
9971010
info->target2 = 0;
9981011

9991012
matched_op = NULL;
1000-
pop = riscv_hash[OP_HASH_IDX (word)];
1001-
pop_end = riscv_hash[OP_HASH_IDX (word) + 1];
1013+
if (!is_riscv_hash_fallback)
1014+
{
1015+
pop = riscv_hash[OP_HASH_IDX (word)];
1016+
pop_end = riscv_hash[OP_HASH_IDX (word) + 1];
1017+
}
1018+
else
1019+
{
1020+
pop = riscv_hash[0];
1021+
pop_end = riscv_hash[OP_HASH_LEN];
1022+
}
10021023
for (; pop != pop_end; pop++)
10031024
{
10041025
op = *pop;

0 commit comments

Comments
 (0)