Interpreter.cpp 8.7 KB

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