diff --git a/Userland/Libraries/LibJS/Bytecode/Executable.cpp b/Userland/Libraries/LibJS/Bytecode/Executable.cpp new file mode 100644 index 0000000000..caa34ba450 --- /dev/null +++ b/Userland/Libraries/LibJS/Bytecode/Executable.cpp @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace JS::Bytecode { + +void Executable::dump() const +{ + for (auto& block : basic_blocks) + block.dump(*this); + if (!string_table->is_empty()) { + outln(); + string_table->dump(); + } +} + +} diff --git a/Userland/Libraries/LibJS/Bytecode/Executable.h b/Userland/Libraries/LibJS/Bytecode/Executable.h new file mode 100644 index 0000000000..7b20e8004d --- /dev/null +++ b/Userland/Libraries/LibJS/Bytecode/Executable.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace JS::Bytecode { + +struct Executable { + NonnullOwnPtrVector basic_blocks; + NonnullOwnPtr string_table; + size_t number_of_registers { 0 }; + + String const& get_string(StringTableIndex index) const { return string_table->get(index); } + + void dump() const; +}; + +} diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.h b/Userland/Libraries/LibJS/Bytecode/Generator.h index 49b34e138a..a0491deaaf 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.h +++ b/Userland/Libraries/LibJS/Bytecode/Generator.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -18,14 +19,6 @@ namespace JS::Bytecode { -struct Executable { - NonnullOwnPtrVector basic_blocks; - NonnullOwnPtr string_table; - size_t number_of_registers { 0 }; - - String const& get_string(StringTableIndex index) const { return string_table->get(index); } -}; - class Generator { public: static Executable generate(ASTNode const&, bool is_in_generator_function = false); diff --git a/Userland/Libraries/LibJS/CMakeLists.txt b/Userland/Libraries/LibJS/CMakeLists.txt index 2f17d0421f..ecfaeb72ad 100644 --- a/Userland/Libraries/LibJS/CMakeLists.txt +++ b/Userland/Libraries/LibJS/CMakeLists.txt @@ -2,6 +2,7 @@ set(SOURCES AST.cpp Bytecode/ASTCodegen.cpp Bytecode/BasicBlock.cpp + Bytecode/Executable.cpp Bytecode/Generator.cpp Bytecode/Instruction.cpp Bytecode/Interpreter.cpp diff --git a/Userland/Libraries/LibTest/JavaScriptTestRunner.h b/Userland/Libraries/LibTest/JavaScriptTestRunner.h index 10dfd62264..6efbc63d0d 100644 --- a/Userland/Libraries/LibTest/JavaScriptTestRunner.h +++ b/Userland/Libraries/LibTest/JavaScriptTestRunner.h @@ -337,18 +337,11 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path) } if (g_run_bytecode) { - auto unit = JS::Bytecode::Generator::generate(m_test_script->parse_node()); - if (g_dump_bytecode) { - for (auto& block : unit.basic_blocks) - block.dump(unit); - if (!unit.string_table->is_empty()) { - outln(); - unit.string_table->dump(); - } - } - + auto executable = JS::Bytecode::Generator::generate(m_test_script->parse_node()); + if (g_dump_bytecode) + executable.dump(); JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object(), interpreter->realm()); - bytecode_interpreter.run(unit); + bytecode_interpreter.run(executable); } else { interpreter->run(interpreter->global_object(), m_test_script->parse_node()); } @@ -359,18 +352,11 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path) if (file_script.is_error()) return { test_path, file_script.error() }; if (g_run_bytecode) { - auto unit = JS::Bytecode::Generator::generate(file_script.value()->parse_node()); - if (g_dump_bytecode) { - for (auto& block : unit.basic_blocks) - block.dump(unit); - if (!unit.string_table->is_empty()) { - outln(); - unit.string_table->dump(); - } - } - + auto executable = JS::Bytecode::Generator::generate(file_script.value()->parse_node()); + if (g_dump_bytecode) + executable.dump(); JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object(), interpreter->realm()); - bytecode_interpreter.run(unit); + bytecode_interpreter.run(executable); } else { interpreter->run(interpreter->global_object(), file_script.value()->parse_node()); }