Przeglądaj źródła

LibJS/Bytecode: Add constants table to Bytecode::Executable

Andreas Kling 1 rok temu
rodzic
commit
e46de4eb59

+ 2 - 0
Userland/Libraries/LibJS/Bytecode/Executable.cpp

@@ -17,6 +17,7 @@ Executable::Executable(
     NonnullOwnPtr<IdentifierTable> identifier_table,
     NonnullOwnPtr<StringTable> string_table,
     NonnullOwnPtr<RegexTable> regex_table,
+    Vector<Value> constants,
     NonnullRefPtr<SourceCode const> source_code,
     size_t number_of_property_lookup_caches,
     size_t number_of_global_variable_caches,
@@ -28,6 +29,7 @@ Executable::Executable(
     , string_table(move(string_table))
     , identifier_table(move(identifier_table))
     , regex_table(move(regex_table))
+    , constants(move(constants))
     , source_code(move(source_code))
     , number_of_registers(number_of_registers)
     , is_strict_mode(is_strict_mode)

+ 2 - 0
Userland/Libraries/LibJS/Bytecode/Executable.h

@@ -51,6 +51,7 @@ public:
         NonnullOwnPtr<IdentifierTable>,
         NonnullOwnPtr<StringTable>,
         NonnullOwnPtr<RegexTable>,
+        Vector<Value> constants,
         NonnullRefPtr<SourceCode const>,
         size_t number_of_property_lookup_caches,
         size_t number_of_global_variable_caches,
@@ -69,6 +70,7 @@ public:
     NonnullOwnPtr<StringTable> string_table;
     NonnullOwnPtr<IdentifierTable> identifier_table;
     NonnullOwnPtr<RegexTable> regex_table;
+    Vector<Value> constants;
 
     NonnullRefPtr<SourceCode const> source_code;
     size_t number_of_registers { 0 };

+ 1 - 0
Userland/Libraries/LibJS/Bytecode/Generator.cpp

@@ -62,6 +62,7 @@ CodeGenerationErrorOr<NonnullGCPtr<Executable>> Generator::generate(VM& vm, ASTN
         move(generator.m_identifier_table),
         move(generator.m_string_table),
         move(generator.m_regex_table),
+        move(generator.m_constants),
         node.source_code(),
         generator.m_next_property_lookup_cache,
         generator.m_next_global_variable_cache,

+ 11 - 0
Userland/Libraries/LibJS/Bytecode/Generator.h

@@ -241,6 +241,16 @@ public:
     [[nodiscard]] size_t next_environment_variable_cache() { return m_next_environment_variable_cache++; }
     [[nodiscard]] size_t next_property_lookup_cache() { return m_next_property_lookup_cache++; }
 
+    [[nodiscard]] Operand add_constant(Value value)
+    {
+        for (size_t i = 0; i < m_constants.size(); ++i) {
+            if (m_constants[i] == value)
+                return Operand(Operand::Type::Constant, i);
+        }
+        m_constants.append(value);
+        return Operand(Operand::Type::Constant, m_constants.size() - 1);
+    }
+
 private:
     enum class JumpType {
         Continue,
@@ -267,6 +277,7 @@ private:
     NonnullOwnPtr<StringTable> m_string_table;
     NonnullOwnPtr<IdentifierTable> m_identifier_table;
     NonnullOwnPtr<RegexTable> m_regex_table;
+    Vector<Value> m_constants;
 
     u32 m_next_register { Register::reserved_register_count };
     u32 m_next_block { 1 };