mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibJS: Move AST bytecode generation virtuals to separate cpp file
This will hopefully make it a bit more pleasant to edit this, as things will just get larger and larger.
This commit is contained in:
parent
2b9fbd10ed
commit
0553e0b048
Notes:
sideshowbarker
2024-07-18 12:42:38 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/0553e0b0488
3 changed files with 91 additions and 78 deletions
|
@ -13,9 +13,6 @@
|
|||
#include <AK/TemporaryChange.h>
|
||||
#include <LibCrypto/BigInt/SignedBigInteger.h>
|
||||
#include <LibJS/AST.h>
|
||||
#include <LibJS/Bytecode/Generator.h>
|
||||
#include <LibJS/Bytecode/Op.h>
|
||||
#include <LibJS/Bytecode/Register.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/Runtime/Accessor.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
|
@ -2237,79 +2234,4 @@ void ScopeNode::add_functions(NonnullRefPtrVector<FunctionDeclaration> functions
|
|||
m_functions.append(move(functions));
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> ASTNode::generate_bytecode(Bytecode::Generator&) const
|
||||
{
|
||||
dbgln("Missing generate_bytecode()");
|
||||
TODO();
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> ScopeNode::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
for (auto& child : children()) {
|
||||
[[maybe_unused]] auto reg = child.generate_bytecode(generator);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> ExpressionStatement::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
return m_expression->generate_bytecode(generator);
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> BinaryExpression::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
auto lhs_reg = m_lhs->generate_bytecode(generator);
|
||||
auto rhs_reg = m_rhs->generate_bytecode(generator);
|
||||
|
||||
VERIFY(lhs_reg.has_value());
|
||||
VERIFY(rhs_reg.has_value());
|
||||
|
||||
auto dst_reg = generator.allocate_register();
|
||||
|
||||
switch (m_op) {
|
||||
case BinaryOp::Addition:
|
||||
generator.emit<Bytecode::Op::Add>(dst_reg, *lhs_reg, *rhs_reg);
|
||||
return dst_reg;
|
||||
case BinaryOp::Subtraction:
|
||||
generator.emit<Bytecode::Op::Sub>(dst_reg, *lhs_reg, *rhs_reg);
|
||||
return dst_reg;
|
||||
default:
|
||||
TODO();
|
||||
}
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> NumericLiteral::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
auto dst = generator.allocate_register();
|
||||
generator.emit<Bytecode::Op::Load>(dst, m_value);
|
||||
return dst;
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> StringLiteral::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
auto dst = generator.allocate_register();
|
||||
generator.emit<Bytecode::Op::NewString>(dst, m_value);
|
||||
return dst;
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> Identifier::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
auto reg = generator.allocate_register();
|
||||
generator.emit<Bytecode::Op::GetVariable>(reg, m_string);
|
||||
return reg;
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> AssignmentExpression::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
if (is<Identifier>(*m_lhs)) {
|
||||
auto& identifier = static_cast<Identifier const&>(*m_lhs);
|
||||
auto rhs_reg = m_rhs->generate_bytecode(generator);
|
||||
VERIFY(rhs_reg.has_value());
|
||||
generator.emit<Bytecode::Op::SetVariable>(identifier.string(), *rhs_reg);
|
||||
return rhs_reg;
|
||||
}
|
||||
|
||||
TODO();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
90
Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
Normal file
90
Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
Normal file
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/AST.h>
|
||||
#include <LibJS/Bytecode/Generator.h>
|
||||
#include <LibJS/Bytecode/Instruction.h>
|
||||
#include <LibJS/Bytecode/Op.h>
|
||||
#include <LibJS/Bytecode/Register.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
Optional<Bytecode::Register> ASTNode::generate_bytecode(Bytecode::Generator&) const
|
||||
{
|
||||
dbgln("Missing generate_bytecode()");
|
||||
TODO();
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> ScopeNode::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
for (auto& child : children()) {
|
||||
[[maybe_unused]] auto reg = child.generate_bytecode(generator);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> ExpressionStatement::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
return m_expression->generate_bytecode(generator);
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> BinaryExpression::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
auto lhs_reg = m_lhs->generate_bytecode(generator);
|
||||
auto rhs_reg = m_rhs->generate_bytecode(generator);
|
||||
|
||||
VERIFY(lhs_reg.has_value());
|
||||
VERIFY(rhs_reg.has_value());
|
||||
|
||||
auto dst_reg = generator.allocate_register();
|
||||
|
||||
switch (m_op) {
|
||||
case BinaryOp::Addition:
|
||||
generator.emit<Bytecode::Op::Add>(dst_reg, *lhs_reg, *rhs_reg);
|
||||
return dst_reg;
|
||||
case BinaryOp::Subtraction:
|
||||
generator.emit<Bytecode::Op::Sub>(dst_reg, *lhs_reg, *rhs_reg);
|
||||
return dst_reg;
|
||||
default:
|
||||
TODO();
|
||||
}
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> NumericLiteral::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
auto dst = generator.allocate_register();
|
||||
generator.emit<Bytecode::Op::Load>(dst, m_value);
|
||||
return dst;
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> StringLiteral::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
auto dst = generator.allocate_register();
|
||||
generator.emit<Bytecode::Op::NewString>(dst, m_value);
|
||||
return dst;
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> Identifier::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
auto reg = generator.allocate_register();
|
||||
generator.emit<Bytecode::Op::GetVariable>(reg, m_string);
|
||||
return reg;
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> AssignmentExpression::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
if (is<Identifier>(*m_lhs)) {
|
||||
auto& identifier = static_cast<Identifier const&>(*m_lhs);
|
||||
auto rhs_reg = m_rhs->generate_bytecode(generator);
|
||||
VERIFY(rhs_reg.has_value());
|
||||
generator.emit<Bytecode::Op::SetVariable>(identifier.string(), *rhs_reg);
|
||||
return rhs_reg;
|
||||
}
|
||||
|
||||
TODO();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
set(SOURCES
|
||||
AST.cpp
|
||||
Bytecode/ASTCodegen.cpp
|
||||
Bytecode/Block.cpp
|
||||
Bytecode/Generator.cpp
|
||||
Bytecode/Instruction.cpp
|
||||
|
|
Loading…
Reference in a new issue