mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +00:00
LibJS/Bytecode: Keep instruction source mappings in Executable
Instead of storing source offsets with each instruction, we now keep them in a side table in Executable. This shrinks each instruction by 8 bytes, further improving locality.
This commit is contained in:
parent
4cf4ea92a7
commit
5a08544138
Notes:
sideshowbarker
2024-07-17 02:28:18 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/5a08544138 Pull-request: https://github.com/SerenityOS/serenity/pull/24240 Reviewed-by: https://github.com/Hendiadyoin1 Reviewed-by: https://github.com/trflynn89 ✅
8 changed files with 27 additions and 14 deletions
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include <AK/Badge.h>
|
#include <AK/Badge.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
|
#include <LibJS/Bytecode/Executable.h>
|
||||||
#include <LibJS/Forward.h>
|
#include <LibJS/Forward.h>
|
||||||
#include <LibJS/Heap/Handle.h>
|
#include <LibJS/Heap/Handle.h>
|
||||||
|
|
||||||
|
@ -45,6 +46,9 @@ public:
|
||||||
BasicBlock const* handler() const { return m_handler; }
|
BasicBlock const* handler() const { return m_handler; }
|
||||||
BasicBlock const* finalizer() const { return m_finalizer; }
|
BasicBlock const* finalizer() const { return m_finalizer; }
|
||||||
|
|
||||||
|
auto const& source_map() const { return m_source_map; }
|
||||||
|
void add_source_map_entry(size_t bytecode_offset, SourceRecord const& source_record) { m_source_map.set(bytecode_offset, source_record); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit BasicBlock(String name);
|
explicit BasicBlock(String name);
|
||||||
|
|
||||||
|
@ -53,6 +57,8 @@ private:
|
||||||
BasicBlock const* m_finalizer { nullptr };
|
BasicBlock const* m_finalizer { nullptr };
|
||||||
String m_name;
|
String m_name;
|
||||||
bool m_terminated { false };
|
bool m_terminated { false };
|
||||||
|
|
||||||
|
HashMap<size_t, SourceRecord> m_source_map;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,11 +106,13 @@ UnrealizedSourceRange Executable::source_range_at(size_t offset) const
|
||||||
return {};
|
return {};
|
||||||
auto it = InstructionStreamIterator(bytecode.span().slice(offset), this);
|
auto it = InstructionStreamIterator(bytecode.span().slice(offset), this);
|
||||||
VERIFY(!it.at_end());
|
VERIFY(!it.at_end());
|
||||||
auto& instruction = *it;
|
auto mapping = source_map.get(offset);
|
||||||
|
if (!mapping.has_value())
|
||||||
|
return {};
|
||||||
return UnrealizedSourceRange {
|
return UnrealizedSourceRange {
|
||||||
.source_code = source_code,
|
.source_code = source_code,
|
||||||
.start_offset = instruction.source_record().source_start_offset,
|
.start_offset = mapping->source_start_offset,
|
||||||
.end_offset = instruction.source_record().source_end_offset,
|
.end_offset = mapping->source_end_offset,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -84,6 +84,8 @@ public:
|
||||||
Vector<ExceptionHandlers> exception_handlers;
|
Vector<ExceptionHandlers> exception_handlers;
|
||||||
Vector<size_t> basic_block_start_offsets;
|
Vector<size_t> basic_block_start_offsets;
|
||||||
|
|
||||||
|
HashMap<size_t, SourceRecord> source_map;
|
||||||
|
|
||||||
ByteString const& get_string(StringTableIndex index) const { return string_table->get(index); }
|
ByteString const& get_string(StringTableIndex index) const { return string_table->get(index); }
|
||||||
DeprecatedFlyString const& get_identifier(IdentifierTableIndex index) const { return identifier_table->get(index); }
|
DeprecatedFlyString const& get_identifier(IdentifierTableIndex index) const { return identifier_table->get(index); }
|
||||||
|
|
||||||
|
|
|
@ -96,6 +96,8 @@ CodeGenerationErrorOr<NonnullGCPtr<Executable>> Generator::generate(VM& vm, ASTN
|
||||||
};
|
};
|
||||||
Vector<UnlinkedExceptionHandlers> unlinked_exception_handlers;
|
Vector<UnlinkedExceptionHandlers> unlinked_exception_handlers;
|
||||||
|
|
||||||
|
HashMap<size_t, SourceRecord> source_map;
|
||||||
|
|
||||||
for (auto& block : generator.m_root_basic_blocks) {
|
for (auto& block : generator.m_root_basic_blocks) {
|
||||||
basic_block_start_offsets.append(bytecode.size());
|
basic_block_start_offsets.append(bytecode.size());
|
||||||
if (block->handler() || block->finalizer()) {
|
if (block->handler() || block->finalizer()) {
|
||||||
|
@ -108,6 +110,11 @@ CodeGenerationErrorOr<NonnullGCPtr<Executable>> Generator::generate(VM& vm, ASTN
|
||||||
}
|
}
|
||||||
|
|
||||||
block_offsets.set(block.ptr(), bytecode.size());
|
block_offsets.set(block.ptr(), bytecode.size());
|
||||||
|
|
||||||
|
for (auto& [offset, source_record] : block->source_map()) {
|
||||||
|
source_map.set(bytecode.size() + offset, source_record);
|
||||||
|
}
|
||||||
|
|
||||||
Bytecode::InstructionStreamIterator it(block->instruction_stream());
|
Bytecode::InstructionStreamIterator it(block->instruction_stream());
|
||||||
while (!it.at_end()) {
|
while (!it.at_end()) {
|
||||||
auto& instruction = const_cast<Instruction&>(*it);
|
auto& instruction = const_cast<Instruction&>(*it);
|
||||||
|
@ -161,6 +168,7 @@ CodeGenerationErrorOr<NonnullGCPtr<Executable>> Generator::generate(VM& vm, ASTN
|
||||||
|
|
||||||
executable->exception_handlers = move(linked_exception_handlers);
|
executable->exception_handlers = move(linked_exception_handlers);
|
||||||
executable->basic_block_start_offsets = move(basic_block_start_offsets);
|
executable->basic_block_start_offsets = move(basic_block_start_offsets);
|
||||||
|
executable->source_map = move(source_map);
|
||||||
|
|
||||||
return executable;
|
return executable;
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,8 +77,7 @@ public:
|
||||||
new (slot) OpType(forward<Args>(args)...);
|
new (slot) OpType(forward<Args>(args)...);
|
||||||
if constexpr (OpType::IsTerminator)
|
if constexpr (OpType::IsTerminator)
|
||||||
m_current_basic_block->terminate({});
|
m_current_basic_block->terminate({});
|
||||||
auto* op = static_cast<OpType*>(slot);
|
m_current_basic_block->add_source_map_entry(slot_offset, { m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
|
||||||
op->set_source_record({ m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename OpType, typename ExtraSlotType, typename... Args>
|
template<typename OpType, typename ExtraSlotType, typename... Args>
|
||||||
|
@ -93,8 +92,7 @@ public:
|
||||||
new (slot) OpType(forward<Args>(args)...);
|
new (slot) OpType(forward<Args>(args)...);
|
||||||
if constexpr (OpType::IsTerminator)
|
if constexpr (OpType::IsTerminator)
|
||||||
m_current_basic_block->terminate({});
|
m_current_basic_block->terminate({});
|
||||||
auto* op = static_cast<OpType*>(slot);
|
m_current_basic_block->add_source_map_entry(slot_offset, { m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
|
||||||
op->set_source_record({ m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename OpType, typename... Args>
|
template<typename OpType, typename... Args>
|
||||||
|
|
|
@ -52,7 +52,7 @@ void Instruction::visit_labels(Function<void(JS::Bytecode::Label&)> visitor)
|
||||||
UnrealizedSourceRange InstructionStreamIterator::source_range() const
|
UnrealizedSourceRange InstructionStreamIterator::source_range() const
|
||||||
{
|
{
|
||||||
VERIFY(m_executable);
|
VERIFY(m_executable);
|
||||||
auto record = dereference().source_record();
|
auto record = m_executable->source_map.get(offset()).value();
|
||||||
return {
|
return {
|
||||||
.source_code = m_executable->source_code,
|
.source_code = m_executable->source_code,
|
||||||
.start_offset = record.source_start_offset,
|
.start_offset = record.source_start_offset,
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Forward.h>
|
#include <AK/Forward.h>
|
||||||
|
#include <AK/Function.h>
|
||||||
#include <AK/Span.h>
|
#include <AK/Span.h>
|
||||||
#include <LibJS/Bytecode/Executable.h>
|
#include <LibJS/Bytecode/Executable.h>
|
||||||
#include <LibJS/Forward.h>
|
#include <LibJS/Forward.h>
|
||||||
|
@ -140,17 +141,12 @@ public:
|
||||||
void visit_labels(Function<void(Label&)> visitor);
|
void visit_labels(Function<void(Label&)> visitor);
|
||||||
static void destroy(Instruction&);
|
static void destroy(Instruction&);
|
||||||
|
|
||||||
// FIXME: Find a better way to organize this information
|
|
||||||
void set_source_record(SourceRecord rec) { m_source_record = rec; }
|
|
||||||
SourceRecord source_record() const { return m_source_record; }
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Instruction(Type, size_t length);
|
Instruction(Type, size_t length);
|
||||||
|
|
||||||
void visit_labels_impl(Function<void(Label&)>) { }
|
void visit_labels_impl(Function<void(Label&)>) { }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SourceRecord m_source_record {};
|
|
||||||
Type m_type {};
|
Type m_type {};
|
||||||
u32 m_length {};
|
u32 m_length {};
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,10 +7,11 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Format.h>
|
#include <AK/Format.h>
|
||||||
#include <LibJS/Bytecode/BasicBlock.h>
|
|
||||||
|
|
||||||
namespace JS::Bytecode {
|
namespace JS::Bytecode {
|
||||||
|
|
||||||
|
class BasicBlock;
|
||||||
|
|
||||||
class Label {
|
class Label {
|
||||||
public:
|
public:
|
||||||
explicit Label(BasicBlock const& block)
|
explicit Label(BasicBlock const& block)
|
||||||
|
|
Loading…
Reference in a new issue