LibJS/Bytecode: Flatten bytecode to a contiguous representation

Instead of keeping bytecode as a set of disjoint basic blocks on the
malloc heap, bytecode is now a contiguous sequence of bytes(!)

The transformation happens at the end of Bytecode::Generator::generate()
and the only really hairy part is rerouting jump labels.

This required solving a few problems:

- The interpreter execution loop had to change quite a bit, since we
  were storing BasicBlock pointers all over the place, and control
  transfer was done by redirecting the interpreter's current block.

- Exception handlers & finalizers are now stored per-bytecode-range
  in a side table in Executable.

- The interpreter now has a plain program counter instead of a stream
  iterator. This actually makes error stack generation a bit nicer
  since we just have to deal with a number instead of reaching into
  the iterator.

This yields a 25% performance improvement on this microbenchmark:

    for (let i = 0; i < 1_000_000; ++i) { }

But basically everything gets faster. :^)
This commit is contained in:
Andreas Kling 2024-05-06 06:44:08 +02:00
commit f6aee2b9e8
Notes: sideshowbarker 2024-07-17 02:22:23 +09:00
21 changed files with 392 additions and 172 deletions

View file

@ -30,27 +30,6 @@ BasicBlock::~BasicBlock()
}
}
void BasicBlock::dump(Bytecode::Executable const& executable) const
{
Bytecode::InstructionStreamIterator it(instruction_stream());
if (!m_name.is_empty())
warn("{}", m_name);
if (m_handler || m_finalizer) {
warn(" [");
if (m_handler)
warn(" Handler: {}", Label { *m_handler });
if (m_finalizer)
warn(" Finalizer: {}", Label { *m_finalizer });
warn(" ]");
}
warnln(":");
while (!it.at_end()) {
warnln("[{:4x}] {}", it.offset(), (*it).to_byte_string(executable));
++it;
}
}
void BasicBlock::grow(size_t additional_size)
{
m_buffer.grow_capacity(m_buffer.size() + additional_size);