Interpreter.cpp 7.5 KB

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