mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:37:34 +00:00
LibJS: Generate bytecode for do...while statements :^)
This was quite straightforward using the same label/jump machinery that we added for while statements. The main addition here is a new JumpIfTrue bytecode instruction.
This commit is contained in:
parent
bd1a5e282a
commit
f2863b5a89
4 changed files with 45 additions and 0 deletions
|
@ -391,6 +391,7 @@ public:
|
||||||
|
|
||||||
virtual Value execute(Interpreter&, GlobalObject&) const override;
|
virtual Value execute(Interpreter&, GlobalObject&) const override;
|
||||||
virtual void dump(int indent) const override;
|
virtual void dump(int indent) const override;
|
||||||
|
virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
NonnullRefPtr<Expression> m_test;
|
NonnullRefPtr<Expression> m_test;
|
||||||
|
|
|
@ -105,4 +105,14 @@ Optional<Bytecode::Register> WhileStatement::generate_bytecode(Bytecode::Generat
|
||||||
return body_result_reg;
|
return body_result_reg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<Bytecode::Register> DoWhileStatement::generate_bytecode(Bytecode::Generator& generator) const
|
||||||
|
{
|
||||||
|
auto head_label = generator.make_label();
|
||||||
|
auto body_result_reg = m_body->generate_bytecode(generator);
|
||||||
|
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);
|
||||||
|
return body_result_reg;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,6 +64,14 @@ void JumpIfFalse::execute(Bytecode::Interpreter& interpreter) const
|
||||||
interpreter.jump(m_target.value());
|
interpreter.jump(m_target.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void JumpIfTrue::execute(Bytecode::Interpreter& interpreter) const
|
||||||
|
{
|
||||||
|
VERIFY(m_target.has_value());
|
||||||
|
auto result = interpreter.reg(m_result);
|
||||||
|
if (result.as_bool())
|
||||||
|
interpreter.jump(m_target.value());
|
||||||
|
}
|
||||||
|
|
||||||
String Load::to_string() const
|
String Load::to_string() const
|
||||||
{
|
{
|
||||||
return String::formatted("Load dst:{}, value:{}", m_dst, m_value.to_string_without_side_effects());
|
return String::formatted("Load dst:{}, value:{}", m_dst, m_value.to_string_without_side_effects());
|
||||||
|
@ -116,4 +124,11 @@ String JumpIfFalse::to_string() const
|
||||||
return String::formatted("JumpIfFalse result:{}, target:<empty>", m_result);
|
return String::formatted("JumpIfFalse result:{}, target:<empty>", m_result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String JumpIfTrue::to_string() const
|
||||||
|
{
|
||||||
|
if (m_target.has_value())
|
||||||
|
return String::formatted("JumpIfTrue result:{}, target:{}", m_result, m_target.value());
|
||||||
|
return String::formatted("JumpIfTrue result:{}, target:<empty>", m_result);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,4 +193,23 @@ private:
|
||||||
Optional<Label> m_target;
|
Optional<Label> m_target;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class JumpIfTrue final : public Instruction {
|
||||||
|
public:
|
||||||
|
explicit JumpIfTrue(Register result, Optional<Label> target = {})
|
||||||
|
: m_result(result)
|
||||||
|
, m_target(move(target))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_target(Optional<Label> target) { m_target = move(target); }
|
||||||
|
|
||||||
|
virtual ~JumpIfTrue() override { }
|
||||||
|
virtual void execute(Bytecode::Interpreter&) const override;
|
||||||
|
virtual String to_string() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Register m_result;
|
||||||
|
Optional<Label> m_target;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue