summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke <luke.wilde@live.co.uk>2021-06-08 04:54:34 +0100
committerLinus Groh <mail@linusgroh.de>2021-06-08 09:53:56 +0100
commitde3ee701cece9e357bc80bca1c3aeade1a5bd470 (patch)
tree19798e3bead0d363ae0f63f2937fd0dd0be68c71
parent6da587b59b5b96517adddef7b738d7092aec8783 (diff)
downloadserenity-de3ee701cece9e357bc80bca1c3aeade1a5bd470.zip
LibJS: Add conditional expression bytecode generation
Or, by its more common name, the ternary operator :^)
-rw-r--r--Userland/Libraries/LibJS/AST.h1
-rw-r--r--Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp19
2 files changed, 20 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/AST.h b/Userland/Libraries/LibJS/AST.h
index 8a10cd9fc4..582a083f01 100644
--- a/Userland/Libraries/LibJS/AST.h
+++ b/Userland/Libraries/LibJS/AST.h
@@ -1176,6 +1176,7 @@ public:
virtual void dump(int indent) const override;
virtual Value execute(Interpreter&, GlobalObject&) const override;
+ virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
private:
NonnullRefPtr<Expression> m_test;
diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
index 7c808aaee7..53adfbc530 100644
--- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
+++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
@@ -430,4 +430,23 @@ Optional<Bytecode::Register> DebuggerStatement::generate_bytecode(Bytecode::Gene
return {};
}
+Optional<Bytecode::Register> ConditionalExpression::generate_bytecode(Bytecode::Generator& generator) const
+{
+ auto result_reg = generator.allocate_register();
+ auto test_reg = m_test->generate_bytecode(generator);
+ auto& alternate_jump = generator.emit<Bytecode::Op::JumpIfFalse>(*test_reg);
+
+ auto consequent_reg = m_consequent->generate_bytecode(generator);
+ generator.emit<Bytecode::Op::LoadRegister>(result_reg, *consequent_reg);
+ auto& end_jump = generator.emit<Bytecode::Op::Jump>();
+
+ alternate_jump.set_target(generator.make_label());
+ auto alternative_reg = m_alternate->generate_bytecode(generator);
+ generator.emit<Bytecode::Op::LoadRegister>(result_reg, *alternative_reg);
+
+ end_jump.set_target(generator.make_label());
+
+ return result_reg;
+}
+
}