LibJS/JIT: Record machine code location to bytecode location mapping

This commit is contained in:
Simon Wanner 2023-10-30 19:03:09 +01:00 committed by Andreas Kling
parent cf93e56833
commit 9f78e56823
Notes: sideshowbarker 2024-07-17 00:57:24 +09:00
3 changed files with 37 additions and 4 deletions

View File

@ -1686,6 +1686,14 @@ OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_execut
Compiler compiler { bytecode_executable };
Vector<BytecodeMapping> mapping;
mapping.append({
.native_offset = compiler.m_output.size(),
.block_index = BytecodeMapping::EXECUTABLE,
.bytecode_offset = 0,
});
compiler.m_assembler.enter();
compiler.m_assembler.mov(
@ -1696,12 +1704,20 @@ OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_execut
Assembler::Operand::Register(LOCALS_ARRAY_BASE),
Assembler::Operand::Register(ARG2));
for (auto& block : bytecode_executable.basic_blocks) {
for (size_t block_index = 0; block_index < bytecode_executable.basic_blocks.size(); block_index++) {
auto& block = bytecode_executable.basic_blocks[block_index];
compiler.block_data_for(*block).start_offset = compiler.m_output.size();
compiler.set_current_block(*block);
auto it = Bytecode::InstructionStreamIterator(block->instruction_stream());
while (!it.at_end()) {
auto const& op = *it;
mapping.append({
.native_offset = compiler.m_output.size(),
.block_index = block_index,
.bytecode_offset = it.offset(),
});
switch (op.type()) {
# define CASE_BYTECODE_OP(OpTitleCase, op_snake_case, ...) \
case Bytecode::Instruction::Type::OpTitleCase: \
@ -1723,6 +1739,12 @@ OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_execut
compiler.jump_to_exit();
}
mapping.append({
.native_offset = compiler.m_output.size(),
.block_index = BytecodeMapping::EXECUTABLE,
.bytecode_offset = 1,
});
compiler.m_exit_label.link(compiler.m_assembler);
compiler.m_assembler.exit();
@ -1752,7 +1774,7 @@ OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_execut
dbgln("\033[32;1mJIT compilation succeeded!\033[0m {}", bytecode_executable.name);
}
auto executable = make<NativeExecutable>(executable_memory, compiler.m_output.size());
auto executable = make<NativeExecutable>(executable_memory, compiler.m_output.size(), mapping);
if constexpr (DUMP_JIT_DISASSEMBLY)
executable->dump_disassembly();
return executable;

View File

@ -12,9 +12,10 @@
namespace JS::JIT {
NativeExecutable::NativeExecutable(void* code, size_t size)
NativeExecutable::NativeExecutable(void* code, size_t size, Vector<BytecodeMapping> mapping)
: m_code(code)
, m_size(size)
, m_mapping(move(mapping))
{
}

View File

@ -12,12 +12,21 @@
namespace JS::JIT {
struct BytecodeMapping {
size_t native_offset;
size_t block_index;
size_t bytecode_offset;
// Special block index for labels outside any blocks.
static constexpr auto EXECUTABLE = NumericLimits<size_t>::max();
};
class NativeExecutable {
AK_MAKE_NONCOPYABLE(NativeExecutable);
AK_MAKE_NONMOVABLE(NativeExecutable);
public:
NativeExecutable(void* code, size_t size);
NativeExecutable(void* code, size_t size, Vector<BytecodeMapping>);
~NativeExecutable();
void run(VM&) const;
@ -26,6 +35,7 @@ public:
private:
void* m_code { nullptr };
size_t m_size { 0 };
Vector<BytecodeMapping> m_mapping;
};
}