Skip to content

Commit 577ae16

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 986ebb5 commit 577ae16

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
@@ -838,6 +838,9 @@ print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info
838838
static const struct riscv_opcode **riscv_hash[OP_HASH_LEN + 1];
839839
static const struct riscv_opcode **riscv_opcodes_sorted;
840840

841+
/* Whether the fallback should be used. */
842+
static bool is_riscv_hash_fallback = false;
843+
841844
/* Compare two riscv_opcode* objects to sort by hash index. */
842845

843846
static int
@@ -868,15 +871,25 @@ build_riscv_opcodes_hash_table (void)
868871

869872
/* Sort riscv_opcodes entry pointers (except macros). */
870873
for (op = riscv_opcodes; op->name; op++)
871-
if (op->pinfo != INSN_MACRO)
874+
{
875+
if (op->pinfo == INSN_MACRO)
876+
continue;
872877
len++;
878+
if (is_riscv_hash_fallback)
879+
continue;
880+
if (OP_HASH_IDX (op->match) < OP_MASK_OP2
881+
? (op->mask & OP_MASK_OP2) != OP_MASK_OP2
882+
: (op->mask & OP_MASK_OP) != OP_MASK_OP)
883+
is_riscv_hash_fallback = true;
884+
}
873885
riscv_opcodes_sorted = xcalloc (len, sizeof (struct riscv_opcode *));
874886
pop_end = riscv_opcodes_sorted;
875887
for (op = riscv_opcodes; op->name; op++)
876888
if (op->pinfo != INSN_MACRO)
877889
*pop_end++ = op;
878-
qsort (riscv_opcodes_sorted, len, sizeof (struct riscv_opcode *),
879-
compare_opcodes);
890+
if (!is_riscv_hash_fallback)
891+
qsort (riscv_opcodes_sorted, len, sizeof (struct riscv_opcode *),
892+
compare_opcodes);
880893

881894
/* Initialize faster hash table. */
882895
pop = riscv_opcodes_sorted;
@@ -919,8 +932,16 @@ riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info)
919932
info->target2 = 0;
920933

921934
matched_op = NULL;
922-
pop = riscv_hash[OP_HASH_IDX (word)];
923-
pop_end = riscv_hash[OP_HASH_IDX (word) + 1];
935+
if (!is_riscv_hash_fallback)
936+
{
937+
pop = riscv_hash[OP_HASH_IDX (word)];
938+
pop_end = riscv_hash[OP_HASH_IDX (word) + 1];
939+
}
940+
else
941+
{
942+
pop = riscv_hash[0];
943+
pop_end = riscv_hash[OP_HASH_LEN];
944+
}
924945
for (; pop != pop_end; pop++)
925946
{
926947
op = *pop;

0 commit comments

Comments
 (0)