diff --git a/Userland/Libraries/LibJS/Bytecode/Executable.cpp b/Userland/Libraries/LibJS/Bytecode/Executable.cpp index 4ed17565051..ec33c920475 100644 --- a/Userland/Libraries/LibJS/Bytecode/Executable.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Executable.cpp @@ -17,6 +17,7 @@ Executable::Executable( NonnullOwnPtr identifier_table, NonnullOwnPtr string_table, NonnullOwnPtr regex_table, + Vector constants, NonnullRefPtr 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) diff --git a/Userland/Libraries/LibJS/Bytecode/Executable.h b/Userland/Libraries/LibJS/Bytecode/Executable.h index 965afc49111..b69b1167e39 100644 --- a/Userland/Libraries/LibJS/Bytecode/Executable.h +++ b/Userland/Libraries/LibJS/Bytecode/Executable.h @@ -51,6 +51,7 @@ public: NonnullOwnPtr, NonnullOwnPtr, NonnullOwnPtr, + Vector constants, NonnullRefPtr, size_t number_of_property_lookup_caches, size_t number_of_global_variable_caches, @@ -69,6 +70,7 @@ public: NonnullOwnPtr string_table; NonnullOwnPtr identifier_table; NonnullOwnPtr regex_table; + Vector constants; NonnullRefPtr source_code; size_t number_of_registers { 0 }; diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.cpp b/Userland/Libraries/LibJS/Bytecode/Generator.cpp index 2a11de594a8..abeab0f26eb 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Generator.cpp @@ -62,6 +62,7 @@ CodeGenerationErrorOr> 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, diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.h b/Userland/Libraries/LibJS/Bytecode/Generator.h index 1f1a913d1d5..c6a8966c810 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.h +++ b/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 m_string_table; NonnullOwnPtr m_identifier_table; NonnullOwnPtr m_regex_table; + Vector m_constants; u32 m_next_register { Register::reserved_register_count }; u32 m_next_block { 1 };