1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-01 14:32:08 +00:00

LibJS: Add basic support for while loops in the bytecode engine

This introduces two new instructions: Jump and JumpIfFalse.
Jumps are made to a Bytecode::Label, which is a simple object that
represents a location in the bytecode stream.

Note that you may not always know the target of a jump when adding the
jump instruction itself, but we can just update the instruction later
on during codegen once we know where the jump target is.

The Bytecode::Interpreter now implements jumping via a jump slot that
gets checked after each instruction to see if a jump is pending.
If not, we just increment the PC as usual.
This commit is contained in:
Andreas Kling 2021-06-04 12:07:38 +02:00
parent 91640d0727
commit 6ae9346cd3
9 changed files with 128 additions and 1 deletions

View file

@ -27,8 +27,16 @@ void Interpreter::run(Bytecode::Block const& block)
m_registers.resize(block.register_count());
for (auto& instruction : block.instructions())
size_t pc = 0;
while (pc < block.instructions().size()) {
auto& instruction = block.instructions()[pc];
instruction.execute(*this);
if (m_pending_jump.has_value()) {
pc = m_pending_jump.release_value();
continue;
}
++pc;
}
dbgln("Bytecode::Interpreter did run block {:p}", &block);
for (size_t i = 0; i < m_registers.size(); ++i) {