summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorGunnar Beutner <gbeutner@serenityos.org>2021-06-08 11:28:27 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-08 11:38:48 +0200
commitef83872f62183501e212e66db2feed0382fa8e05 (patch)
tree6605c78b0ca2bf45276c8a2f9aeaed6ccc0709fc /Userland/Libraries/LibJS
parentafbbcde15045b1881efb0e1ade3995865bf6765c (diff)
downloadserenity-ef83872f62183501e212e66db2feed0382fa8e05.zip
LibJS: Make JumpIf{True,False,Nullish} inherit from Jump
This saves a few lines in LogicalExpression::generate_bytecode.
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp17
-rw-r--r--Userland/Libraries/LibJS/Bytecode/Op.h34
2 files changed, 16 insertions, 35 deletions
diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
index db7763d0a7..c789f4d36a 100644
--- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
+++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
@@ -126,7 +126,7 @@ Optional<Bytecode::Register> LogicalExpression::generate_bytecode(Bytecode::Gene
auto result_reg = generator.allocate_register();
auto lhs_reg = m_lhs->generate_bytecode(generator);
- Bytecode::Instruction* test_instr;
+ Bytecode::Op::Jump* test_instr;
switch (m_op) {
case LogicalOp::And:
test_instr = &generator.emit<Bytecode::Op::JumpIfTrue>(*lhs_reg);
@@ -145,20 +145,7 @@ Optional<Bytecode::Register> LogicalExpression::generate_bytecode(Bytecode::Gene
auto& end_jump = generator.emit<Bytecode::Op::Jump>();
auto rhs_label = generator.make_label();
-
- switch (m_op) {
- case LogicalOp::And:
- static_cast<Bytecode::Op::JumpIfTrue*>(test_instr)->set_target(rhs_label);
- break;
- case LogicalOp::Or:
- static_cast<Bytecode::Op::JumpIfFalse*>(test_instr)->set_target(rhs_label);
- break;
- case LogicalOp::NullishCoalescing:
- static_cast<Bytecode::Op::JumpIfNullish*>(test_instr)->set_target(rhs_label);
- break;
- default:
- VERIFY_NOT_REACHED();
- }
+ test_instr->set_target(rhs_label);
auto rhs_reg = m_rhs->generate_bytecode(generator);
generator.emit<Bytecode::Op::LoadRegister>(result_reg, *rhs_reg);
diff --git a/Userland/Libraries/LibJS/Bytecode/Op.h b/Userland/Libraries/LibJS/Bytecode/Op.h
index d60138196c..b80b744a77 100644
--- a/Userland/Libraries/LibJS/Bytecode/Op.h
+++ b/Userland/Libraries/LibJS/Bytecode/Op.h
@@ -229,8 +229,14 @@ private:
Register m_src;
};
-class Jump final : public Instruction {
+class Jump : public Instruction {
public:
+ explicit Jump(Type type, Optional<Label> target = {})
+ : Instruction(type)
+ , m_target(move(target))
+ {
+ }
+
explicit Jump(Optional<Label> target = {})
: Instruction(Type::Jump)
, m_target(move(target))
@@ -242,65 +248,53 @@ public:
void execute(Bytecode::Interpreter&) const;
String to_string() const;
-private:
+protected:
Optional<Label> m_target;
};
-class JumpIfFalse final : public Instruction {
+class JumpIfFalse final : public Jump {
public:
explicit JumpIfFalse(Register result, Optional<Label> target = {})
- : Instruction(Type::JumpIfFalse)
+ : Jump(Type::JumpIfFalse, move(target))
, m_result(result)
- , m_target(move(target))
{
}
- void set_target(Optional<Label> target) { m_target = move(target); }
-
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_result;
- Optional<Label> m_target;
};
-class JumpIfTrue final : public Instruction {
+class JumpIfTrue : public Jump {
public:
explicit JumpIfTrue(Register result, Optional<Label> target = {})
- : Instruction(Type::JumpIfTrue)
+ : Jump(Type::JumpIfTrue, move(target))
, m_result(result)
- , m_target(move(target))
{
}
- void set_target(Optional<Label> target) { m_target = move(target); }
-
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_result;
- Optional<Label> m_target;
};
-class JumpIfNullish final : public Instruction {
+class JumpIfNullish final : public Jump {
public:
explicit JumpIfNullish(Register result, Optional<Label> target = {})
- : Instruction(Type::JumpIfNullish)
+ : Jump(Type::JumpIfNullish, move(target))
, m_result(result)
- , m_target(move(target))
{
}
- void set_target(Optional<Label> target) { m_target = move(target); }
-
void execute(Bytecode::Interpreter&) const;
String to_string() const;
private:
Register m_result;
- Optional<Label> m_target;
};
// NOTE: This instruction is variable-width depending on the number of arguments!