diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 505cc5f8c11..fdf5fd651ff 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -13,9 +13,6 @@ #include #include #include -#include -#include -#include #include #include #include @@ -2237,79 +2234,4 @@ void ScopeNode::add_functions(NonnullRefPtrVector functions m_functions.append(move(functions)); } -Optional ASTNode::generate_bytecode(Bytecode::Generator&) const -{ - dbgln("Missing generate_bytecode()"); - TODO(); -} - -Optional ScopeNode::generate_bytecode(Bytecode::Generator& generator) const -{ - for (auto& child : children()) { - [[maybe_unused]] auto reg = child.generate_bytecode(generator); - } - return {}; -} - -Optional ExpressionStatement::generate_bytecode(Bytecode::Generator& generator) const -{ - return m_expression->generate_bytecode(generator); -} - -Optional 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(dst_reg, *lhs_reg, *rhs_reg); - return dst_reg; - case BinaryOp::Subtraction: - generator.emit(dst_reg, *lhs_reg, *rhs_reg); - return dst_reg; - default: - TODO(); - } -} - -Optional NumericLiteral::generate_bytecode(Bytecode::Generator& generator) const -{ - auto dst = generator.allocate_register(); - generator.emit(dst, m_value); - return dst; -} - -Optional StringLiteral::generate_bytecode(Bytecode::Generator& generator) const -{ - auto dst = generator.allocate_register(); - generator.emit(dst, m_value); - return dst; -} - -Optional Identifier::generate_bytecode(Bytecode::Generator& generator) const -{ - auto reg = generator.allocate_register(); - generator.emit(reg, m_string); - return reg; -} - -Optional AssignmentExpression::generate_bytecode(Bytecode::Generator& generator) const -{ - if (is(*m_lhs)) { - auto& identifier = static_cast(*m_lhs); - auto rhs_reg = m_rhs->generate_bytecode(generator); - VERIFY(rhs_reg.has_value()); - generator.emit(identifier.string(), *rhs_reg); - return rhs_reg; - } - - TODO(); -} - } diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp new file mode 100644 index 00000000000..5f349dd2ea0 --- /dev/null +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +namespace JS { + +Optional ASTNode::generate_bytecode(Bytecode::Generator&) const +{ + dbgln("Missing generate_bytecode()"); + TODO(); +} + +Optional ScopeNode::generate_bytecode(Bytecode::Generator& generator) const +{ + for (auto& child : children()) { + [[maybe_unused]] auto reg = child.generate_bytecode(generator); + } + return {}; +} + +Optional ExpressionStatement::generate_bytecode(Bytecode::Generator& generator) const +{ + return m_expression->generate_bytecode(generator); +} + +Optional 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(dst_reg, *lhs_reg, *rhs_reg); + return dst_reg; + case BinaryOp::Subtraction: + generator.emit(dst_reg, *lhs_reg, *rhs_reg); + return dst_reg; + default: + TODO(); + } +} + +Optional NumericLiteral::generate_bytecode(Bytecode::Generator& generator) const +{ + auto dst = generator.allocate_register(); + generator.emit(dst, m_value); + return dst; +} + +Optional StringLiteral::generate_bytecode(Bytecode::Generator& generator) const +{ + auto dst = generator.allocate_register(); + generator.emit(dst, m_value); + return dst; +} + +Optional Identifier::generate_bytecode(Bytecode::Generator& generator) const +{ + auto reg = generator.allocate_register(); + generator.emit(reg, m_string); + return reg; +} + +Optional AssignmentExpression::generate_bytecode(Bytecode::Generator& generator) const +{ + if (is(*m_lhs)) { + auto& identifier = static_cast(*m_lhs); + auto rhs_reg = m_rhs->generate_bytecode(generator); + VERIFY(rhs_reg.has_value()); + generator.emit(identifier.string(), *rhs_reg); + return rhs_reg; + } + + TODO(); +} + +} diff --git a/Userland/Libraries/LibJS/CMakeLists.txt b/Userland/Libraries/LibJS/CMakeLists.txt index da9a199f63e..a7fa1760d78 100644 --- a/Userland/Libraries/LibJS/CMakeLists.txt +++ b/Userland/Libraries/LibJS/CMakeLists.txt @@ -1,5 +1,6 @@ set(SOURCES AST.cpp + Bytecode/ASTCodegen.cpp Bytecode/Block.cpp Bytecode/Generator.cpp Bytecode/Instruction.cpp