Skip to content

Commit

Permalink
Simplify binary translation mappings, avoid some allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Oct 22, 2023
1 parent c56f852 commit f76774f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/libriscv/decoded_exec_segment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace riscv
#ifdef RISCV_BINARY_TRANSLATION
bool is_binary_translated() const noexcept { return m_bintr_dl != nullptr; }
void set_binary_translated(void* dl) const { m_bintr_dl = dl; }
void reserve_mappings(size_t mappings) { m_translator_mappings.reserve(mappings); }
void add_mapping(instruction_handler<W> handler) { m_translator_mappings.push_back(handler); }
instruction_handler<W> mapping_at(unsigned i) const { return m_translator_mappings.at(i); }
#else
Expand Down
15 changes: 7 additions & 8 deletions lib/libriscv/decoder_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ namespace riscv
// When compressed instructions are enabled, many decoder
// entries are illegal because they between instructions.
bool was_full_instruction = true;
[[maybe_unused]] const auto block_ending_bytecode =
CPU<W>::computed_index_for(RV32_INSTR_BLOCK_END);

/* Generate all instruction pointers for executable code.
Cannot step outside of this area when pregen is enabled,
Expand All @@ -283,14 +285,11 @@ namespace riscv
rv32i_instruction rewritten = instruction;

#ifdef RISCV_BINARY_TRANSLATION
if (exec.is_binary_translated()) {
if (entry.isset()) {
// With translator ops we pretend the original opcode is JAL,
// which breaks the block-finding loop. In all cases, continue.
entry.set_bytecode(CPU<W>::computed_index_for(RV32_INSTR_BLOCK_END));
dst += 4;
continue;
}
// With translator ops we pretend the original opcode is JAL,
// which ends the block-finding loop.
if (entry.get_bytecode() == block_ending_bytecode) {
dst += 4;
continue;
}
#endif // RISCV_BINARY_TRANSLATION

Expand Down
3 changes: 2 additions & 1 deletion lib/libriscv/tr_translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,13 @@ void CPU<W>::activate_dylib(DecodedExecuteSegment<W>& exec, void* dylib) const

// Apply mappings to decoder cache
const auto nmappings = *no_mappings;
exec.reserve_mappings(nmappings);
for (size_t i = 0; i < nmappings; i++) {
exec.add_mapping(mappings[i].handler);
if (mappings[i].handler != nullptr) {
auto& entry = decoder_entry_at(exec, mappings[i].addr);
entry.m_handler = 0xFF;
entry.instr = i;
entry.set_bytecode(CPU<W>::computed_index_for(RV32_INSTR_BLOCK_END));
}
}

Expand Down

0 comments on commit f76774f

Please sign in to comment.