Interpreter.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/TemporaryChange.h>
  8. #include <LibJS/Bytecode/BasicBlock.h>
  9. #include <LibJS/Bytecode/Instruction.h>
  10. #include <LibJS/Bytecode/Interpreter.h>
  11. #include <LibJS/Bytecode/Op.h>
  12. #include <LibJS/Runtime/GlobalEnvironment.h>
  13. #include <LibJS/Runtime/GlobalObject.h>
  14. namespace JS::Bytecode {
  15. static Interpreter* s_current;
  16. Interpreter* Interpreter::current()
  17. {
  18. return s_current;
  19. }
  20. Interpreter::Interpreter(GlobalObject& global_object, Realm& realm)
  21. : m_vm(global_object.vm())
  22. , m_global_object(global_object)
  23. , m_realm(realm)
  24. {
  25. VERIFY(!s_current);
  26. s_current = this;
  27. }
  28. Interpreter::~Interpreter()
  29. {
  30. VERIFY(s_current == this);
  31. s_current = nullptr;
  32. }
  33. Value Interpreter::run(Executable const& executable, BasicBlock const* entry_point)
  34. {
  35. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable);
  36. TemporaryChange restore_executable { m_current_executable, &executable };
  37. vm().set_last_value(Badge<Interpreter> {}, {});
  38. ExecutionContext execution_context(vm().heap());
  39. if (vm().execution_context_stack().is_empty()) {
  40. execution_context.this_value = &global_object();
  41. static FlyString global_execution_context_name = "(*BC* global execution context)";
  42. execution_context.function_name = global_execution_context_name;
  43. execution_context.lexical_environment = &global_object().environment();
  44. execution_context.variable_environment = &global_object().environment();
  45. VERIFY(!vm().exception());
  46. // FIXME: How do we know if we're in strict mode? Maybe the Bytecode::Block should know this?
  47. // execution_context.is_strict_mode = ???;
  48. vm().push_execution_context(execution_context, global_object());
  49. VERIFY(!vm().exception());
  50. }
  51. auto block = entry_point ?: &executable.basic_blocks.first();
  52. if (m_manually_entered_frames) {
  53. VERIFY(registers().size() >= executable.number_of_registers);
  54. } else {
  55. m_register_windows.append(make<RegisterWindow>());
  56. registers().resize(executable.number_of_registers);
  57. registers()[Register::global_object_index] = Value(&global_object());
  58. }
  59. for (;;) {
  60. Bytecode::InstructionStreamIterator pc(block->instruction_stream());
  61. bool will_jump = false;
  62. bool will_return = false;
  63. while (!pc.at_end()) {
  64. auto& instruction = *pc;
  65. instruction.execute(*this);
  66. if (vm().exception()) {
  67. m_saved_exception = {};
  68. if (m_unwind_contexts.is_empty())
  69. break;
  70. auto& unwind_context = m_unwind_contexts.last();
  71. if (unwind_context.handler) {
  72. block = unwind_context.handler;
  73. unwind_context.handler = nullptr;
  74. accumulator() = vm().exception()->value();
  75. vm().clear_exception();
  76. will_jump = true;
  77. } else if (unwind_context.finalizer) {
  78. block = unwind_context.finalizer;
  79. m_unwind_contexts.take_last();
  80. will_jump = true;
  81. m_saved_exception = Handle<Exception>::create(vm().exception());
  82. vm().clear_exception();
  83. }
  84. }
  85. if (m_pending_jump.has_value()) {
  86. block = m_pending_jump.release_value();
  87. will_jump = true;
  88. break;
  89. }
  90. if (!m_return_value.is_empty()) {
  91. will_return = true;
  92. break;
  93. }
  94. ++pc;
  95. }
  96. if (will_return)
  97. break;
  98. if (pc.at_end() && !will_jump)
  99. break;
  100. if (vm().exception())
  101. break;
  102. }
  103. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable);
  104. if constexpr (JS_BYTECODE_DEBUG) {
  105. for (size_t i = 0; i < registers().size(); ++i) {
  106. String value_string;
  107. if (registers()[i].is_empty())
  108. value_string = "(empty)";
  109. else
  110. value_string = registers()[i].to_string_without_side_effects();
  111. dbgln("[{:3}] {}", i, value_string);
  112. }
  113. }
  114. vm().set_last_value(Badge<Interpreter> {}, accumulator());
  115. if (!m_manually_entered_frames)
  116. m_register_windows.take_last();
  117. auto return_value = m_return_value.value_or(js_undefined());
  118. m_return_value = {};
  119. // NOTE: The return value from a called function is put into $0 in the caller context.
  120. if (!m_register_windows.is_empty())
  121. m_register_windows.last()[0] = return_value;
  122. if (vm().execution_context_stack().size() == 1)
  123. vm().pop_execution_context();
  124. vm().finish_execution_generation();
  125. return return_value;
  126. }
  127. void Interpreter::enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target)
  128. {
  129. m_unwind_contexts.empend(handler_target.has_value() ? &handler_target->block() : nullptr, finalizer_target.has_value() ? &finalizer_target->block() : nullptr);
  130. }
  131. void Interpreter::leave_unwind_context()
  132. {
  133. m_unwind_contexts.take_last();
  134. }
  135. void Interpreter::continue_pending_unwind(Label const& resume_label)
  136. {
  137. if (!m_saved_exception.is_null()) {
  138. vm().set_exception(*m_saved_exception.cell());
  139. m_saved_exception = {};
  140. } else {
  141. jump(resume_label);
  142. }
  143. }
  144. AK::Array<OwnPtr<PassManager>, static_cast<UnderlyingType<Interpreter::OptimizationLevel>>(Interpreter::OptimizationLevel::__Count)> Interpreter::s_optimization_pipelines {};
  145. Bytecode::PassManager& Interpreter::optimization_pipeline(Interpreter::OptimizationLevel level)
  146. {
  147. auto underlying_level = to_underlying(level);
  148. VERIFY(underlying_level <= to_underlying(Interpreter::OptimizationLevel::__Count));
  149. auto& entry = s_optimization_pipelines[underlying_level];
  150. if (entry)
  151. return *entry;
  152. auto pm = make<PassManager>();
  153. if (level == OptimizationLevel::Default) {
  154. pm->add<Passes::GenerateCFG>();
  155. pm->add<Passes::UnifySameBlocks>();
  156. pm->add<Passes::GenerateCFG>();
  157. pm->add<Passes::MergeBlocks>();
  158. pm->add<Passes::GenerateCFG>();
  159. pm->add<Passes::UnifySameBlocks>();
  160. pm->add<Passes::GenerateCFG>();
  161. pm->add<Passes::MergeBlocks>();
  162. pm->add<Passes::GenerateCFG>();
  163. pm->add<Passes::PlaceBlocks>();
  164. } else {
  165. VERIFY_NOT_REACHED();
  166. }
  167. auto& passes = *pm;
  168. entry = move(pm);
  169. return passes;
  170. }
  171. }