1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 20:25:07 +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

@ -90,4 +90,16 @@ Optional<Bytecode::Register> AssignmentExpression::generate_bytecode(Bytecode::G
TODO();
}
Optional<Bytecode::Register> WhileStatement::generate_bytecode(Bytecode::Generator& generator) const
{
auto test_label = generator.make_label();
auto test_result_reg = m_test->generate_bytecode(generator);
VERIFY(test_result_reg.has_value());
auto& test_jump = generator.emit<Bytecode::Op::JumpIfFalse>(*test_result_reg);
auto body_result_reg = m_body->generate_bytecode(generator);
generator.emit<Bytecode::Op::Jump>(test_label);
test_jump.set_target(generator.make_label());
return body_result_reg;
}
}