From 234ed2d466f02185d4b937118b37227b9f12a315 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 5 Nov 2023 14:19:42 +0100 Subject: [PATCH] LibJS/JIT: Resolve the GetGlobal identifier at JIT time --- .../LibJS/Bytecode/CommonImplementations.cpp | 17 ++++++++--------- .../LibJS/Bytecode/CommonImplementations.h | 2 +- .../Libraries/LibJS/Bytecode/Interpreter.cpp | 2 +- Userland/Libraries/LibJS/JIT/Compiler.cpp | 4 ++-- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp index eae170d082..69ed663fbd 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp @@ -94,7 +94,7 @@ ThrowCompletionOr get_by_value(Bytecode::Interpreter& interpreter, Value return TRY(object->internal_get(property_key, base_value)); } -ThrowCompletionOr get_global(Bytecode::Interpreter& interpreter, IdentifierTableIndex identifier, u32 cache_index) +ThrowCompletionOr get_global(Bytecode::Interpreter& interpreter, DeprecatedFlyString const& identifier, u32 cache_index) { auto& vm = interpreter.vm(); auto& realm = *vm.current_realm(); @@ -114,25 +114,24 @@ ThrowCompletionOr get_global(Bytecode::Interpreter& interpreter, Identifi cache.environment_serial_number = declarative_record.environment_serial_number(); - auto const& name = interpreter.current_executable().get_identifier(identifier); if (vm.running_execution_context().script_or_module.has>()) { // NOTE: GetGlobal is used to access variables stored in the module environment and global environment. // The module environment is checked first since it precedes the global environment in the environment chain. auto& module_environment = *vm.running_execution_context().script_or_module.get>()->environment(); - if (TRY(module_environment.has_binding(name))) { + if (TRY(module_environment.has_binding(identifier))) { // TODO: Cache offset of binding value - return TRY(module_environment.get_binding_value(vm, name, vm.in_strict_mode())); + return TRY(module_environment.get_binding_value(vm, identifier, vm.in_strict_mode())); } } - if (TRY(declarative_record.has_binding(name))) { + if (TRY(declarative_record.has_binding(identifier))) { // TODO: Cache offset of binding value - return TRY(declarative_record.get_binding_value(vm, name, vm.in_strict_mode())); + return TRY(declarative_record.get_binding_value(vm, identifier, vm.in_strict_mode())); } - if (TRY(binding_object.has_property(name))) { + if (TRY(binding_object.has_property(identifier))) { CacheablePropertyMetadata cacheable_metadata; - auto value = TRY(binding_object.internal_get(name, js_undefined(), &cacheable_metadata)); + auto value = TRY(binding_object.internal_get(identifier, js_undefined(), &cacheable_metadata)); if (cacheable_metadata.type == CacheablePropertyMetadata::Type::OwnProperty) { cache.shape = shape; cache.property_offset = cacheable_metadata.property_offset.value(); @@ -141,7 +140,7 @@ ThrowCompletionOr get_global(Bytecode::Interpreter& interpreter, Identifi return value; } - return vm.throw_completion(ErrorType::UnknownIdentifier, name); + return vm.throw_completion(ErrorType::UnknownIdentifier, identifier); } ThrowCompletionOr put_by_property_key(VM& vm, Value base, Value this_value, Value value, PropertyKey name, Op::PropertyKind kind) diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h index 1b14c79336..355a5486d1 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h @@ -15,7 +15,7 @@ namespace JS::Bytecode { ThrowCompletionOr> base_object_for_get(Bytecode::Interpreter&, Value base_value); ThrowCompletionOr get_by_id(Bytecode::Interpreter&, DeprecatedFlyString const& property, Value base_value, Value this_value, u32 cache_index); ThrowCompletionOr get_by_value(Bytecode::Interpreter&, Value base_value, Value property_key_value); -ThrowCompletionOr get_global(Bytecode::Interpreter&, IdentifierTableIndex, u32 cache_index); +ThrowCompletionOr get_global(Bytecode::Interpreter&, DeprecatedFlyString const& identifier, u32 cache_index); ThrowCompletionOr put_by_property_key(VM&, Value base, Value this_value, Value value, PropertyKey name, Op::PropertyKind kind); ThrowCompletionOr perform_call(Interpreter&, Value this_value, Op::CallType, Value callee, MarkedVector argument_values); ThrowCompletionOr throw_if_needed_for_call(Interpreter&, Value callee, Op::CallType, Optional const& expression_string); diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 2003bf7750..c26c199885 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -697,7 +697,7 @@ ThrowCompletionOr GetCalleeAndThisFromEnvironment::execute_impl(Bytecode:: ThrowCompletionOr GetGlobal::execute_impl(Bytecode::Interpreter& interpreter) const { - interpreter.accumulator() = TRY(get_global(interpreter, m_identifier, m_cache_index)); + interpreter.accumulator() = TRY(get_global(interpreter, interpreter.current_executable().get_identifier(m_identifier), m_cache_index)); return {}; } diff --git a/Userland/Libraries/LibJS/JIT/Compiler.cpp b/Userland/Libraries/LibJS/JIT/Compiler.cpp index cb59a78a0b..00a3c498a3 100644 --- a/Userland/Libraries/LibJS/JIT/Compiler.cpp +++ b/Userland/Libraries/LibJS/JIT/Compiler.cpp @@ -952,7 +952,7 @@ void Compiler::compile_get_by_value(Bytecode::Op::GetByValue const& op) check_exception(); } -static Value cxx_get_global(VM& vm, Bytecode::IdentifierTableIndex identifier, u32 cache_index) +static Value cxx_get_global(VM& vm, DeprecatedFlyString const& identifier, u32 cache_index) { return TRY_OR_SET_EXCEPTION(Bytecode::get_global(vm.bytecode_interpreter(), identifier, cache_index)); } @@ -961,7 +961,7 @@ void Compiler::compile_get_global(Bytecode::Op::GetGlobal const& op) { m_assembler.mov( Assembler::Operand::Register(ARG1), - Assembler::Operand::Imm(op.identifier().value())); + Assembler::Operand::Imm(bit_cast(&m_bytecode_executable.get_identifier(op.identifier())))); m_assembler.mov( Assembler::Operand::Register(ARG2), Assembler::Operand::Imm(op.cache_index()));