Browse Source

LibJS/Bytecode: Simplify creating/leaving lexical environment

Since we no longer need to create or leave var environments directly
in bytecode, we can streamline the two instructions by making them
always operate on the lexical environment.
Andreas Kling 2 years ago
parent
commit
dbfe1311ef

+ 2 - 2
Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp

@@ -2226,7 +2226,7 @@ Bytecode::CodeGenerationErrorOr<void> WithStatement::generate_bytecode(Bytecode:
     generator.end_boundary(Bytecode::Generator::BlockBoundaryType::LeaveLexicalEnvironment);
     generator.end_boundary(Bytecode::Generator::BlockBoundaryType::LeaveLexicalEnvironment);
 
 
     if (!generator.is_current_block_terminated())
     if (!generator.is_current_block_terminated())
-        generator.emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
+        generator.emit<Bytecode::Op::LeaveLexicalEnvironment>();
 
 
     return {};
     return {};
 }
 }
@@ -2282,7 +2282,7 @@ static Bytecode::CodeGenerationErrorOr<ForInOfHeadEvaluationResult> for_in_of_he
                 generator.emit<Bytecode::Op::CreateVariable>(identifier, Bytecode::Op::EnvironmentMode::Lexical, false);
                 generator.emit<Bytecode::Op::CreateVariable>(identifier, Bytecode::Op::EnvironmentMode::Lexical, false);
             }));
             }));
             // d. Set the running execution context's LexicalEnvironment to newEnv.
             // d. Set the running execution context's LexicalEnvironment to newEnv.
-            // NOTE: Done by CreateEnvironment.
+            // NOTE: Done by CreateLexicalEnvironment.
         }
         }
     } else {
     } else {
         // Runtime Semantics: ForInOfLoopEvaluation, for any of:
         // Runtime Semantics: ForInOfLoopEvaluation, for any of:

+ 6 - 6
Userland/Libraries/LibJS/Bytecode/Generator.cpp

@@ -96,7 +96,7 @@ void Generator::block_declaration_instantiation(ScopeNode const& scope_node)
 void Generator::begin_variable_scope()
 void Generator::begin_variable_scope()
 {
 {
     start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
     start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
-    emit<Bytecode::Op::CreateEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
+    emit<Bytecode::Op::CreateLexicalEnvironment>();
 }
 }
 
 
 void Generator::end_variable_scope()
 void Generator::end_variable_scope()
@@ -104,7 +104,7 @@ void Generator::end_variable_scope()
     end_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
     end_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
 
 
     if (!m_current_basic_block->is_terminated()) {
     if (!m_current_basic_block->is_terminated()) {
-        emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
+        emit<Bytecode::Op::LeaveLexicalEnvironment>();
     }
     }
 }
 }
 
 
@@ -313,7 +313,7 @@ void Generator::generate_break()
             last_was_finally = false;
             last_was_finally = false;
             break;
             break;
         case LeaveLexicalEnvironment:
         case LeaveLexicalEnvironment:
-            emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
+            emit<Bytecode::Op::LeaveLexicalEnvironment>();
             break;
             break;
         case Continue:
         case Continue:
             break;
             break;
@@ -341,7 +341,7 @@ void Generator::generate_break(DeprecatedFlyString const& break_label)
                     emit<Bytecode::Op::LeaveUnwindContext>();
                     emit<Bytecode::Op::LeaveUnwindContext>();
                 last_was_finally = false;
                 last_was_finally = false;
             } else if (boundary == BlockBoundaryType::LeaveLexicalEnvironment) {
             } else if (boundary == BlockBoundaryType::LeaveLexicalEnvironment) {
-                emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
+                emit<Bytecode::Op::LeaveLexicalEnvironment>();
             } else if (boundary == BlockBoundaryType::ReturnToFinally) {
             } else if (boundary == BlockBoundaryType::ReturnToFinally) {
                 auto& block = make_block(DeprecatedString::formatted("{}.break", current_block().name()));
                 auto& block = make_block(DeprecatedString::formatted("{}.break", current_block().name()));
                 emit<Op::ScheduleJump>(Label { block });
                 emit<Op::ScheduleJump>(Label { block });
@@ -381,7 +381,7 @@ void Generator::generate_continue()
             last_was_finally = false;
             last_was_finally = false;
             break;
             break;
         case LeaveLexicalEnvironment:
         case LeaveLexicalEnvironment:
-            emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
+            emit<Bytecode::Op::LeaveLexicalEnvironment>();
             break;
             break;
         case Break:
         case Break:
             break;
             break;
@@ -409,7 +409,7 @@ void Generator::generate_continue(DeprecatedFlyString const& continue_label)
                     emit<Bytecode::Op::LeaveUnwindContext>();
                     emit<Bytecode::Op::LeaveUnwindContext>();
                 last_was_finally = false;
                 last_was_finally = false;
             } else if (boundary == BlockBoundaryType::LeaveLexicalEnvironment) {
             } else if (boundary == BlockBoundaryType::LeaveLexicalEnvironment) {
-                emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
+                emit<Bytecode::Op::LeaveLexicalEnvironment>();
             } else if (boundary == BlockBoundaryType::ReturnToFinally) {
             } else if (boundary == BlockBoundaryType::ReturnToFinally) {
                 auto& block = make_block(DeprecatedString::formatted("{}.continue", current_block().name()));
                 auto& block = make_block(DeprecatedString::formatted("{}.continue", current_block().name()));
                 emit<Op::ScheduleJump>(Label { block });
                 emit<Op::ScheduleJump>(Label { block });

+ 1 - 1
Userland/Libraries/LibJS/Bytecode/Generator.h

@@ -164,7 +164,7 @@ public:
                 emit<Bytecode::Op::LeaveUnwindContext>();
                 emit<Bytecode::Op::LeaveUnwindContext>();
                 break;
                 break;
             case LeaveLexicalEnvironment:
             case LeaveLexicalEnvironment:
-                emit<Bytecode::Op::LeaveEnvironment>(Bytecode::Op::EnvironmentMode::Lexical);
+                emit<Bytecode::Op::LeaveLexicalEnvironment>();
                 break;
                 break;
             case Break:
             case Break:
             case Continue:
             case Continue:

+ 2 - 2
Userland/Libraries/LibJS/Bytecode/Instruction.h

@@ -22,7 +22,7 @@
     O(ConcatString)                  \
     O(ConcatString)                  \
     O(ContinuePendingUnwind)         \
     O(ContinuePendingUnwind)         \
     O(CopyObjectExcludingProperties) \
     O(CopyObjectExcludingProperties) \
-    O(CreateEnvironment)             \
+    O(CreateLexicalEnvironment)      \
     O(CreateVariable)                \
     O(CreateVariable)                \
     O(Decrement)                     \
     O(Decrement)                     \
     O(DeleteById)                    \
     O(DeleteById)                    \
@@ -53,7 +53,7 @@
     O(JumpConditional)               \
     O(JumpConditional)               \
     O(JumpNullish)                   \
     O(JumpNullish)                   \
     O(JumpUndefined)                 \
     O(JumpUndefined)                 \
-    O(LeaveEnvironment)              \
+    O(LeaveLexicalEnvironment)       \
     O(LeaveUnwindContext)            \
     O(LeaveUnwindContext)            \
     O(LeftShift)                     \
     O(LeftShift)                     \
     O(LessThan)                      \
     O(LessThan)                      \

+ 8 - 20
Userland/Libraries/LibJS/Bytecode/Op.cpp

@@ -411,17 +411,14 @@ ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& inte
     return {};
     return {};
 }
 }
 
 
-ThrowCompletionOr<void> CreateEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
+ThrowCompletionOr<void> CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
 {
 {
     auto make_and_swap_envs = [&](auto& old_environment) {
     auto make_and_swap_envs = [&](auto& old_environment) {
         GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
         GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
         swap(old_environment, environment);
         swap(old_environment, environment);
         return environment;
         return environment;
     };
     };
-    if (m_mode == EnvironmentMode::Lexical)
-        interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment));
-    else if (m_mode == EnvironmentMode::Var)
-        interpreter.saved_variable_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().variable_environment));
+    interpreter.saved_lexical_environment_stack().append(make_and_swap_envs(interpreter.vm().running_execution_context().lexical_environment));
     return {};
     return {};
 }
 }
 
 
@@ -831,12 +828,9 @@ ThrowCompletionOr<void> ScheduleJump::execute_impl(Bytecode::Interpreter& interp
     return {};
     return {};
 }
 }
 
 
-ThrowCompletionOr<void> LeaveEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
+ThrowCompletionOr<void> LeaveLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
 {
 {
-    if (m_mode == EnvironmentMode::Lexical)
-        interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
-    if (m_mode == EnvironmentMode::Var)
-        interpreter.vm().running_execution_context().variable_environment = interpreter.saved_variable_environment_stack().take_last();
+    interpreter.vm().running_execution_context().lexical_environment = interpreter.saved_lexical_environment_stack().take_last();
     return {};
     return {};
 }
 }
 
 
@@ -1191,12 +1185,9 @@ DeprecatedString DeleteVariable::to_deprecated_string_impl(Bytecode::Executable
     return DeprecatedString::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
     return DeprecatedString::formatted("DeleteVariable {} ({})", m_identifier, executable.identifier_table->get(m_identifier));
 }
 }
 
 
-DeprecatedString CreateEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
+DeprecatedString CreateLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
 {
 {
-    auto mode_string = m_mode == EnvironmentMode::Lexical
-        ? "Lexical"
-        : "Variable";
-    return DeprecatedString::formatted("CreateEnvironment mode:{}", mode_string);
+    return "CreateLexicalEnvironment"sv;
 }
 }
 
 
 DeprecatedString CreateVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
 DeprecatedString CreateVariable::to_deprecated_string_impl(Bytecode::Executable const& executable) const
@@ -1344,12 +1335,9 @@ DeprecatedString ScheduleJump::to_deprecated_string_impl(Bytecode::Executable co
     return DeprecatedString::formatted("ScheduleJump {}", m_target);
     return DeprecatedString::formatted("ScheduleJump {}", m_target);
 }
 }
 
 
-DeprecatedString LeaveEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
+DeprecatedString LeaveLexicalEnvironment::to_deprecated_string_impl(Bytecode::Executable const&) const
 {
 {
-    auto mode_string = m_mode == EnvironmentMode::Lexical
-        ? "Lexical"
-        : "Variable";
-    return DeprecatedString::formatted("LeaveEnvironment env:{}", mode_string);
+    return "LeaveLexicalEnvironment"sv;
 }
 }
 
 
 DeprecatedString LeaveUnwindContext::to_deprecated_string_impl(Bytecode::Executable const&) const
 DeprecatedString LeaveUnwindContext::to_deprecated_string_impl(Bytecode::Executable const&) const

+ 6 - 14
Userland/Libraries/LibJS/Bytecode/Op.h

@@ -383,11 +383,10 @@ enum class EnvironmentMode {
     Var,
     Var,
 };
 };
 
 
-class CreateEnvironment final : public Instruction {
+class CreateLexicalEnvironment final : public Instruction {
 public:
 public:
-    explicit CreateEnvironment(EnvironmentMode mode)
-        : Instruction(Type::CreateEnvironment)
-        , m_mode(mode)
+    explicit CreateLexicalEnvironment()
+        : Instruction(Type::CreateLexicalEnvironment)
     {
     {
     }
     }
 
 
@@ -395,9 +394,6 @@ public:
     DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
     DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
     void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
     void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
     void replace_references_impl(Register, Register) { }
     void replace_references_impl(Register, Register) { }
-
-private:
-    EnvironmentMode m_mode { EnvironmentMode::Lexical };
 };
 };
 
 
 class EnterObjectEnvironment final : public Instruction {
 class EnterObjectEnvironment final : public Instruction {
@@ -955,11 +951,10 @@ private:
     Label m_target;
     Label m_target;
 };
 };
 
 
-class LeaveEnvironment final : public Instruction {
+class LeaveLexicalEnvironment final : public Instruction {
 public:
 public:
-    LeaveEnvironment(EnvironmentMode mode)
-        : Instruction(Type::LeaveEnvironment)
-        , m_mode(mode)
+    LeaveLexicalEnvironment()
+        : Instruction(Type::LeaveLexicalEnvironment)
     {
     {
     }
     }
 
 
@@ -967,9 +962,6 @@ public:
     DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
     DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
     void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
     void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
     void replace_references_impl(Register, Register) { }
     void replace_references_impl(Register, Register) { }
-
-private:
-    EnvironmentMode m_mode { EnvironmentMode::Lexical };
 };
 };
 
 
 class LeaveUnwindContext final : public Instruction {
 class LeaveUnwindContext final : public Instruction {