From d1c701f79f6c151c21ba4096912c63f0dace542c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 27 Oct 2023 13:58:55 +0200 Subject: [PATCH] LibJS/JIT: Compile the Create/LeaveLexicalEnvironment instructions --- Userland/Libraries/LibJS/JIT/Compiler.cpp | 33 +++++++++++++++++++++++ Userland/Libraries/LibJS/JIT/Compiler.h | 2 ++ 2 files changed, 35 insertions(+) diff --git a/Userland/Libraries/LibJS/JIT/Compiler.cpp b/Userland/Libraries/LibJS/JIT/Compiler.cpp index 92d7f5033f..f443c714ae 100644 --- a/Userland/Libraries/LibJS/JIT/Compiler.cpp +++ b/Userland/Libraries/LibJS/JIT/Compiler.cpp @@ -9,7 +9,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -867,6 +869,31 @@ void Compiler::compile_set_variable(Bytecode::Op::SetVariable const& op) check_exception(); } +static void cxx_create_lexical_environment(VM& vm) +{ + auto make_and_swap_envs = [&](auto& old_environment) { + GCPtr environment = new_declarative_environment(*old_environment).ptr(); + swap(old_environment, environment); + return environment; + }; + vm.bytecode_interpreter().saved_lexical_environment_stack().append(make_and_swap_envs(vm.running_execution_context().lexical_environment)); +} + +void Compiler::compile_create_lexical_environment(Bytecode::Op::CreateLexicalEnvironment const&) +{ + m_assembler.native_call((void*)cxx_create_lexical_environment); +} + +static void cxx_leave_lexical_environment(VM& vm) +{ + vm.running_execution_context().lexical_environment = vm.bytecode_interpreter().saved_lexical_environment_stack().take_last(); +} + +void Compiler::compile_leave_lexical_environment(Bytecode::Op::LeaveLexicalEnvironment const&) +{ + m_assembler.native_call((void*)cxx_leave_lexical_environment); +} + OwnPtr Compiler::compile(Bytecode::Executable& bytecode_executable) { if (!getenv("LIBJS_JIT")) @@ -931,6 +958,12 @@ OwnPtr Compiler::compile(Bytecode::Executable& bytecode_execut case Bytecode::Instruction::Type::Return: compiler.compile_return(static_cast(op)); break; + case Bytecode::Instruction::Type::CreateLexicalEnvironment: + compiler.compile_create_lexical_environment(static_cast(op)); + break; + case Bytecode::Instruction::Type::LeaveLexicalEnvironment: + compiler.compile_leave_lexical_environment(static_cast(op)); + break; case Bytecode::Instruction::Type::NewString: compiler.compile_new_string(static_cast(op)); break; diff --git a/Userland/Libraries/LibJS/JIT/Compiler.h b/Userland/Libraries/LibJS/JIT/Compiler.h index 846e006a66..b6a92a776b 100644 --- a/Userland/Libraries/LibJS/JIT/Compiler.h +++ b/Userland/Libraries/LibJS/JIT/Compiler.h @@ -46,6 +46,8 @@ private: void compile_enter_unwind_context(Bytecode::Op::EnterUnwindContext const&); void compile_leave_unwind_context(Bytecode::Op::LeaveUnwindContext const&); void compile_throw(Bytecode::Op::Throw const&); + void compile_create_lexical_environment(Bytecode::Op::CreateLexicalEnvironment const&); + void compile_leave_lexical_environment(Bytecode::Op::LeaveLexicalEnvironment const&); void compile_to_numeric(Bytecode::Op::ToNumeric const&); void compile_resolve_this_binding(Bytecode::Op::ResolveThisBinding const&);