Interpreter.cpp 7.3 KB

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