From c95dde971b7f0f5071ce778dc20da83cb15a6627 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 24 Oct 2021 13:34:46 +0200 Subject: [PATCH] LibJS: Move global "should dump bytecode" flag into LibJS This will allow us to trigger bytecode executable dumps when generating bytecode inside LibJS as well, not just in clients like js and test-js. --- .../Libraries/LibJS/Bytecode/Interpreter.cpp | 1 + .../Libraries/LibJS/Bytecode/Interpreter.h | 2 ++ .../Libraries/LibTest/JavaScriptTestRunner.h | 5 ++--- .../LibTest/JavaScriptTestRunnerMain.cpp | 5 ++--- Userland/Utilities/js.cpp | 21 +++++++------------ 5 files changed, 14 insertions(+), 20 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 7f0943c7ac..49ef47c048 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -17,6 +17,7 @@ namespace JS::Bytecode { static Interpreter* s_current; +bool g_dump_bytecode = false; Interpreter* Interpreter::current() { diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.h b/Userland/Libraries/LibJS/Bytecode/Interpreter.h index f8faea9029..d32735fa6b 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.h +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.h @@ -85,4 +85,6 @@ private: Handle m_saved_exception; }; +extern bool g_dump_bytecode; + } diff --git a/Userland/Libraries/LibTest/JavaScriptTestRunner.h b/Userland/Libraries/LibTest/JavaScriptTestRunner.h index 6efbc63d0d..436c327d48 100644 --- a/Userland/Libraries/LibTest/JavaScriptTestRunner.h +++ b/Userland/Libraries/LibTest/JavaScriptTestRunner.h @@ -117,7 +117,6 @@ extern bool g_collect_on_every_allocation; extern bool g_zombify_dead_cells; #endif extern bool g_run_bytecode; -extern bool g_dump_bytecode; extern String g_currently_running_test; struct FunctionWithLength { JS::ThrowCompletionOr (*function)(JS::VM&, JS::GlobalObject&); @@ -338,7 +337,7 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path) if (g_run_bytecode) { auto executable = JS::Bytecode::Generator::generate(m_test_script->parse_node()); - if (g_dump_bytecode) + if (JS::Bytecode::g_dump_bytecode) executable.dump(); JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object(), interpreter->realm()); bytecode_interpreter.run(executable); @@ -353,7 +352,7 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path) return { test_path, file_script.error() }; if (g_run_bytecode) { auto executable = JS::Bytecode::Generator::generate(file_script.value()->parse_node()); - if (g_dump_bytecode) + if (JS::Bytecode::g_dump_bytecode) executable.dump(); JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object(), interpreter->realm()); bytecode_interpreter.run(executable); diff --git a/Userland/Libraries/LibTest/JavaScriptTestRunnerMain.cpp b/Userland/Libraries/LibTest/JavaScriptTestRunnerMain.cpp index 7fb9dbe8b1..a733dd1e60 100644 --- a/Userland/Libraries/LibTest/JavaScriptTestRunnerMain.cpp +++ b/Userland/Libraries/LibTest/JavaScriptTestRunnerMain.cpp @@ -22,7 +22,6 @@ bool g_collect_on_every_allocation = false; bool g_zombify_dead_cells = false; #endif bool g_run_bytecode = false; -bool g_dump_bytecode = false; String g_currently_running_test; HashMap s_exposed_global_functions; Function g_main_hook; @@ -116,7 +115,7 @@ int main(int argc, char** argv) args_parser.add_option(g_zombify_dead_cells, "Zombify dead cells (to catch missing GC marks)", "zombify-dead-cells", 'z'); #endif args_parser.add_option(g_run_bytecode, "Use the bytecode interpreter", "run-bytecode", 'b'); - args_parser.add_option(g_dump_bytecode, "Dump the bytecode", "dump-bytecode", 'd'); + args_parser.add_option(JS::Bytecode::g_dump_bytecode, "Dump the bytecode", "dump-bytecode", 'd'); args_parser.add_option(test_glob, "Only run tests matching the given glob", "filter", 'f', "glob"); for (auto& entry : g_extra_args) args_parser.add_option(*entry.key, entry.value.get<0>().characters(), entry.value.get<1>().characters(), entry.value.get<2>()); @@ -130,7 +129,7 @@ int main(int argc, char** argv) AK::set_debug_enabled(false); } - if (g_dump_bytecode && !g_run_bytecode) { + if (JS::Bytecode::g_dump_bytecode && !g_run_bytecode) { warnln("--dump-bytecode can only be used when --run-bytecode is specified."); return 1; } diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index 3ae0a81986..18acb52f92 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -98,7 +98,6 @@ private: }; static bool s_dump_ast = false; -static bool s_dump_bytecode = false; static bool s_run_bytecode = false; static bool s_opt_bytecode = false; static bool s_as_module = false; @@ -824,26 +823,20 @@ static bool parse_and_run(JS::Interpreter& interpreter, StringView const& source outln("{}", hint); vm->throw_exception(interpreter.global_object(), error.to_string()); } else { - if (s_dump_bytecode || s_run_bytecode) { - auto unit = JS::Bytecode::Generator::generate(*program); + if (JS::Bytecode::g_dump_bytecode || s_run_bytecode) { + auto executable = JS::Bytecode::Generator::generate(*program); if (s_opt_bytecode) { auto& passes = JS::Bytecode::Interpreter::optimization_pipeline(); - passes.perform(unit); + passes.perform(executable); dbgln("Optimisation passes took {}us", passes.elapsed()); } - if (s_dump_bytecode) { - for (auto& block : unit.basic_blocks) - block.dump(unit); - if (!unit.string_table->is_empty()) { - outln(); - unit.string_table->dump(); - } - } + if (JS::Bytecode::g_dump_bytecode) + executable.dump(); if (s_run_bytecode) { JS::Bytecode::Interpreter bytecode_interpreter(interpreter.global_object(), interpreter.realm()); - bytecode_interpreter.run(unit); + bytecode_interpreter.run(executable); } else { return true; } @@ -1121,7 +1114,7 @@ int main(int argc, char** argv) Core::ArgsParser args_parser; args_parser.set_general_help("This is a JavaScript interpreter."); args_parser.add_option(s_dump_ast, "Dump the AST", "dump-ast", 'A'); - args_parser.add_option(s_dump_bytecode, "Dump the bytecode", "dump-bytecode", 'd'); + args_parser.add_option(JS::Bytecode::g_dump_bytecode, "Dump the bytecode", "dump-bytecode", 'd'); args_parser.add_option(s_run_bytecode, "Run the bytecode", "run-bytecode", 'b'); args_parser.add_option(s_opt_bytecode, "Optimize the bytecode", "optimize-bytecode", 'p'); args_parser.add_option(s_as_module, "Treat as module", "as-module", 'm');