mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 20:28:11 +00:00
LibJS: Add basic support for "continue" in the bytecode VM
Unlike the convoluted unwind-until-scope-type mechanism in the AST interpreter, "continue" maps to a simple Bytecode::Op::Jump here. :^) We know where to jump based on a stack of "continuable scopes" that we now maintain on the Bytecode::Generator as we go. Note that this only supports bare "continue", not continue-with-label.
This commit is contained in:
parent
79eac08f5b
commit
4bdfe73895
4 changed files with 32 additions and 0 deletions
|
@ -110,6 +110,7 @@ Optional<Bytecode::Register> AssignmentExpression::generate_bytecode(Bytecode::G
|
|||
|
||||
Optional<Bytecode::Register> WhileStatement::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
generator.begin_continuable_scope();
|
||||
auto test_label = generator.make_label();
|
||||
auto test_result_reg = m_test->generate_bytecode(generator);
|
||||
VERIFY(test_result_reg.has_value());
|
||||
|
@ -117,13 +118,16 @@ Optional<Bytecode::Register> WhileStatement::generate_bytecode(Bytecode::Generat
|
|||
auto body_result_reg = m_body->generate_bytecode(generator);
|
||||
generator.emit<Bytecode::Op::Jump>(test_label);
|
||||
test_jump.set_target(generator.make_label());
|
||||
generator.end_continuable_scope();
|
||||
return body_result_reg;
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> DoWhileStatement::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
generator.begin_continuable_scope();
|
||||
auto head_label = generator.make_label();
|
||||
auto body_result_reg = m_body->generate_bytecode(generator);
|
||||
generator.end_continuable_scope();
|
||||
auto test_result_reg = m_test->generate_bytecode(generator);
|
||||
VERIFY(test_result_reg.has_value());
|
||||
generator.emit<Bytecode::Op::JumpIfTrue>(*test_result_reg, head_label);
|
||||
|
@ -209,4 +213,10 @@ Optional<Bytecode::Register> IfStatement::generate_bytecode(Bytecode::Generator&
|
|||
return {};
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> ContinueStatement::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
generator.emit<Bytecode::Op::Jump>(generator.nearest_continuable_scope());
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -47,4 +47,19 @@ Label Generator::make_label() const
|
|||
return Label { m_block->instructions().size() };
|
||||
}
|
||||
|
||||
Label Generator::nearest_continuable_scope() const
|
||||
{
|
||||
return m_continuable_scopes.last();
|
||||
}
|
||||
|
||||
void Generator::begin_continuable_scope()
|
||||
{
|
||||
m_continuable_scopes.append(make_label());
|
||||
}
|
||||
|
||||
void Generator::end_continuable_scope()
|
||||
{
|
||||
m_continuable_scopes.take_last();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,6 +29,11 @@ public:
|
|||
|
||||
Label make_label() const;
|
||||
|
||||
void begin_continuable_scope();
|
||||
void end_continuable_scope();
|
||||
|
||||
Label nearest_continuable_scope() const;
|
||||
|
||||
private:
|
||||
Generator();
|
||||
~Generator();
|
||||
|
@ -37,6 +42,7 @@ private:
|
|||
|
||||
OwnPtr<Block> m_block;
|
||||
u32 m_next_register { 1 };
|
||||
Vector<Label> m_continuable_scopes;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue