Преглед изворни кода

LibJS/JIT: Compile the NewRegExp bytecode instruction

Andreas Kling пре 1 година
родитељ
комит
c1551a64dc

+ 20 - 0
Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp

@@ -11,6 +11,7 @@
 #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
 #include <LibJS/Runtime/GlobalEnvironment.h>
 #include <LibJS/Runtime/ObjectEnvironment.h>
+#include <LibJS/Runtime/RegExpObject.h>
 
 namespace JS::Bytecode {
 
@@ -375,4 +376,23 @@ ThrowCompletionOr<CalleeAndThis> get_callee_and_this_from_environment(Bytecode::
     };
 }
 
+// 13.2.7.3 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-regular-expression-literals-runtime-semantics-evaluation
+Value new_regexp(VM& vm, ParsedRegex const& parsed_regex, DeprecatedString const& pattern, DeprecatedString const& flags)
+{
+    // 1. Let pattern be CodePointsToString(BodyText of RegularExpressionLiteral).
+    // 2. Let flags be CodePointsToString(FlagText of RegularExpressionLiteral).
+
+    // 3. Return ! RegExpCreate(pattern, flags).
+    auto& realm = *vm.current_realm();
+    Regex<ECMA262> regex(parsed_regex.regex, parsed_regex.pattern, parsed_regex.flags);
+    // NOTE: We bypass RegExpCreate and subsequently RegExpAlloc as an optimization to use the already parsed values.
+    auto regexp_object = RegExpObject::create(realm, move(regex), pattern, flags);
+    // RegExpAlloc has these two steps from the 'Legacy RegExp features' proposal.
+    regexp_object->set_realm(realm);
+    // We don't need to check 'If SameValue(newTarget, thisRealm.[[Intrinsics]].[[%RegExp%]]) is true'
+    // here as we know RegExpCreate calls RegExpAlloc with %RegExp% for newTarget.
+    regexp_object->set_legacy_features_enabled(true);
+    return regexp_object;
+}
+
 }

+ 1 - 0
Userland/Libraries/LibJS/Bytecode/CommonImplementations.h

@@ -30,5 +30,6 @@ struct CalleeAndThis {
     Value this_value;
 };
 ThrowCompletionOr<CalleeAndThis> get_callee_and_this_from_environment(Bytecode::Interpreter&, DeprecatedFlyString const& name, u32 cache_index);
+Value new_regexp(VM&, ParsedRegex const&, DeprecatedString const& pattern, DeprecatedString const& flags);
 
 }

+ 5 - 22
Userland/Libraries/LibJS/Bytecode/Interpreter.cpp

@@ -708,30 +708,13 @@ ThrowCompletionOr<void> NewObject::execute_impl(Bytecode::Interpreter& interpret
     return {};
 }
 
-// 13.2.7.3 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-regular-expression-literals-runtime-semantics-evaluation
 ThrowCompletionOr<void> NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
 {
-    auto& vm = interpreter.vm();
-
-    auto& realm = *vm.current_realm();
-
-    // 1. Let pattern be CodePointsToString(BodyText of RegularExpressionLiteral).
-    auto pattern = interpreter.current_executable().get_string(m_source_index);
-
-    // 2. Let flags be CodePointsToString(FlagText of RegularExpressionLiteral).
-    auto flags = interpreter.current_executable().get_string(m_flags_index);
-
-    // 3. Return ! RegExpCreate(pattern, flags).
-    auto& parsed_regex = interpreter.current_executable().regex_table->get(m_regex_index);
-    Regex<ECMA262> regex(parsed_regex.regex, parsed_regex.pattern, parsed_regex.flags);
-    // NOTE: We bypass RegExpCreate and subsequently RegExpAlloc as an optimization to use the already parsed values.
-    auto regexp_object = RegExpObject::create(realm, move(regex), move(pattern), move(flags));
-    // RegExpAlloc has these two steps from the 'Legacy RegExp features' proposal.
-    regexp_object->set_realm(*vm.current_realm());
-    // We don't need to check 'If SameValue(newTarget, thisRealm.[[Intrinsics]].[[%RegExp%]]) is true'
-    // here as we know RegExpCreate calls RegExpAlloc with %RegExp% for newTarget.
-    regexp_object->set_legacy_features_enabled(true);
-    interpreter.accumulator() = regexp_object;
+    interpreter.accumulator() = new_regexp(
+        interpreter.vm(),
+        interpreter.current_executable().regex_table->get(m_regex_index),
+        interpreter.current_executable().get_string(m_source_index),
+        interpreter.current_executable().get_string(m_flags_index));
     return {};
 }
 

+ 4 - 0
Userland/Libraries/LibJS/Bytecode/Op.h

@@ -187,6 +187,10 @@ public:
     ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
     DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
 
+    StringTableIndex source_index() const { return m_source_index; }
+    StringTableIndex flags_index() const { return m_flags_index; }
+    RegexTableIndex regex_index() const { return m_regex_index; }
+
 private:
     StringTableIndex m_source_index;
     StringTableIndex m_flags_index;

+ 29 - 0
Userland/Libraries/LibJS/JIT/Compiler.cpp

@@ -9,6 +9,7 @@
 #include <LibJS/Bytecode/CommonImplementations.h>
 #include <LibJS/Bytecode/Instruction.h>
 #include <LibJS/Bytecode/Interpreter.h>
+#include <LibJS/Bytecode/RegexTable.h>
 #include <LibJS/JIT/Compiler.h>
 #include <LibJS/Runtime/AbstractOperations.h>
 #include <LibJS/Runtime/Array.h>
@@ -583,6 +584,31 @@ void Compiler::compile_new_string(Bytecode::Op::NewString const& op)
     store_vm_register(Bytecode::Register::accumulator(), RET);
 }
 
+static Value cxx_new_regexp(VM& vm, Bytecode::ParsedRegex const& parsed_regex, DeprecatedString const& pattern, DeprecatedString const& flags)
+{
+    return Bytecode::new_regexp(vm, parsed_regex, pattern, flags);
+}
+
+void Compiler::compile_new_regexp(Bytecode::Op::NewRegExp const& op)
+{
+    auto const& parsed_regex = m_bytecode_executable.regex_table->get(op.regex_index());
+    auto const& pattern = m_bytecode_executable.string_table->get(op.source_index());
+    auto const& flags = m_bytecode_executable.string_table->get(op.flags_index());
+
+    m_assembler.mov(
+        Assembler::Operand::Register(ARG1),
+        Assembler::Operand::Imm64(bit_cast<u64>(&parsed_regex)));
+    m_assembler.mov(
+        Assembler::Operand::Register(ARG2),
+        Assembler::Operand::Imm64(bit_cast<u64>(&pattern)));
+    m_assembler.mov(
+        Assembler::Operand::Register(ARG3),
+        Assembler::Operand::Imm64(bit_cast<u64>(&flags)));
+
+    m_assembler.native_call((void*)cxx_new_regexp);
+    store_vm_register(Bytecode::Register::accumulator(), RET);
+}
+
 static Value cxx_new_object(VM& vm)
 {
     auto& realm = *vm.current_realm();
@@ -1017,6 +1043,9 @@ OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_execut
             case Bytecode::Instruction::Type::NewFunction:
                 compiler.compile_new_function(static_cast<Bytecode::Op::NewFunction const&>(op));
                 break;
+            case Bytecode::Instruction::Type::NewRegExp:
+                compiler.compile_new_regexp(static_cast<Bytecode::Op::NewRegExp const&>(op));
+                break;
             case Bytecode::Instruction::Type::GetById:
                 compiler.compile_get_by_id(static_cast<Bytecode::Op::GetById const&>(op));
                 break;

+ 1 - 0
Userland/Libraries/LibJS/JIT/Compiler.h

@@ -98,6 +98,7 @@ private:
     void compile_new_object(Bytecode::Op::NewObject const&);
     void compile_new_array(Bytecode::Op::NewArray const&);
     void compile_new_function(Bytecode::Op::NewFunction const&);
+    void compile_new_regexp(Bytecode::Op::NewRegExp const&);
 
     void compile_get_by_id(Bytecode::Op::GetById const&);
     void compile_get_by_value(Bytecode::Op::GetByValue const&);