Interpreter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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(Realm& realm)
  24. : m_vm(realm.vm())
  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, RegisterWindow* in_frame)
  36. {
  37. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable);
  38. TemporaryChange restore_executable { m_current_executable, &executable };
  39. TemporaryChange restore_saved_jump { m_scheduled_jump, static_cast<BasicBlock const*>(nullptr) };
  40. TemporaryChange restore_saved_exception { m_saved_exception, {} };
  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 = &m_realm->global_object();
  46. static DeprecatedFlyString 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. execution_context.is_strict_mode = executable.is_strict_mode;
  52. vm().push_execution_context(execution_context);
  53. pushed_execution_context = true;
  54. }
  55. TemporaryChange restore_current_block { m_current_block, entry_point ?: executable.basic_blocks.first() };
  56. if (in_frame)
  57. m_register_windows.append(in_frame);
  58. else
  59. m_register_windows.append(make<RegisterWindow>(MarkedVector<Value>(vm().heap()), MarkedVector<GCPtr<Environment>>(vm().heap()), MarkedVector<GCPtr<Environment>>(vm().heap()), Vector<UnwindInfo> {}));
  60. registers().resize(executable.number_of_registers);
  61. for (;;) {
  62. Bytecode::InstructionStreamIterator pc(m_current_block->instruction_stream());
  63. TemporaryChange temp_change { m_pc, &pc };
  64. // FIXME: This is getting kinda spaghetti-y
  65. bool will_jump = false;
  66. bool will_return = false;
  67. bool will_yield = false;
  68. while (!pc.at_end()) {
  69. auto& instruction = *pc;
  70. auto ran_or_error = instruction.execute(*this);
  71. if (ran_or_error.is_error()) {
  72. auto exception_value = *ran_or_error.throw_completion().value();
  73. m_saved_exception = make_handle(exception_value);
  74. if (unwind_contexts().is_empty())
  75. break;
  76. auto& unwind_context = unwind_contexts().last();
  77. if (unwind_context.executable != m_current_executable)
  78. break;
  79. if (unwind_context.handler) {
  80. vm().running_execution_context().lexical_environment = unwind_context.lexical_environment;
  81. vm().running_execution_context().variable_environment = unwind_context.variable_environment;
  82. m_current_block = unwind_context.handler;
  83. unwind_context.handler = nullptr;
  84. accumulator() = exception_value;
  85. m_saved_exception = {};
  86. will_jump = true;
  87. break;
  88. }
  89. if (unwind_context.finalizer) {
  90. m_current_block = unwind_context.finalizer;
  91. will_jump = true;
  92. break;
  93. }
  94. // 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.
  95. // If you run into this, you probably forgot to remove the current unwind_context somewhere.
  96. VERIFY_NOT_REACHED();
  97. }
  98. if (m_pending_jump.has_value()) {
  99. m_current_block = m_pending_jump.release_value();
  100. will_jump = true;
  101. break;
  102. }
  103. if (!m_return_value.is_empty()) {
  104. will_return = true;
  105. // Note: A `yield` statement will not go through a finally statement,
  106. // hence we need to set a flag to not do so,
  107. // but we generate a Yield Operation in the case of returns in
  108. // generators as well, so we need to check if it will actually
  109. // continue or is a `return` in disguise
  110. will_yield = instruction.type() == Instruction::Type::Yield && static_cast<Op::Yield const&>(instruction).continuation().has_value();
  111. break;
  112. }
  113. ++pc;
  114. }
  115. if (will_jump)
  116. continue;
  117. if (!unwind_contexts().is_empty() && !will_yield) {
  118. auto& unwind_context = unwind_contexts().last();
  119. if (unwind_context.executable == m_current_executable && unwind_context.finalizer) {
  120. m_saved_return_value = make_handle(m_return_value);
  121. m_return_value = {};
  122. m_current_block = unwind_context.finalizer;
  123. // the unwind_context will be pop'ed when entering the finally block
  124. continue;
  125. }
  126. }
  127. if (pc.at_end())
  128. break;
  129. if (!m_saved_exception.is_null())
  130. break;
  131. if (will_return)
  132. break;
  133. }
  134. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable);
  135. if constexpr (JS_BYTECODE_DEBUG) {
  136. for (size_t i = 0; i < registers().size(); ++i) {
  137. String value_string;
  138. if (registers()[i].is_empty())
  139. value_string = MUST("(empty)"_string);
  140. else
  141. value_string = MUST(registers()[i].to_string_without_side_effects());
  142. dbgln("[{:3}] {}", i, value_string);
  143. }
  144. }
  145. auto frame = m_register_windows.take_last();
  146. Value return_value = js_undefined();
  147. if (!m_return_value.is_empty()) {
  148. return_value = m_return_value;
  149. m_return_value = {};
  150. } else if (!m_saved_return_value.is_null() && m_saved_exception.is_null()) {
  151. return_value = m_saved_return_value.value();
  152. m_saved_return_value = {};
  153. }
  154. // NOTE: The return value from a called function is put into $0 in the caller context.
  155. if (!m_register_windows.is_empty())
  156. window().registers[0] = return_value;
  157. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  158. // in which case this is a no-op.
  159. vm().run_queued_promise_jobs();
  160. if (pushed_execution_context) {
  161. VERIFY(&vm().running_execution_context() == &execution_context);
  162. vm().pop_execution_context();
  163. }
  164. vm().finish_execution_generation();
  165. if (!m_saved_exception.is_null()) {
  166. Value thrown_value = m_saved_exception.value();
  167. m_saved_exception = {};
  168. m_saved_return_value = {};
  169. if (auto* register_window = frame.get_pointer<NonnullOwnPtr<RegisterWindow>>())
  170. return { throw_completion(thrown_value), move(*register_window) };
  171. return { throw_completion(thrown_value), nullptr };
  172. }
  173. if (auto* register_window = frame.get_pointer<NonnullOwnPtr<RegisterWindow>>())
  174. return { return_value, move(*register_window) };
  175. return { return_value, nullptr };
  176. }
  177. void Interpreter::enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target)
  178. {
  179. unwind_contexts().empend(
  180. m_current_executable,
  181. handler_target.has_value() ? &handler_target->block() : nullptr,
  182. finalizer_target.has_value() ? &finalizer_target->block() : nullptr,
  183. vm().running_execution_context().lexical_environment,
  184. vm().running_execution_context().variable_environment);
  185. }
  186. void Interpreter::leave_unwind_context()
  187. {
  188. unwind_contexts().take_last();
  189. }
  190. ThrowCompletionOr<void> Interpreter::continue_pending_unwind(Label const& resume_label)
  191. {
  192. if (!m_saved_exception.is_null()) {
  193. auto result = throw_completion(m_saved_exception.value());
  194. m_saved_exception = {};
  195. return result;
  196. }
  197. if (!m_saved_return_value.is_null()) {
  198. do_return(m_saved_return_value.value());
  199. m_saved_return_value = {};
  200. return {};
  201. }
  202. if (m_scheduled_jump) {
  203. // FIXME: If we `break` or `continue` in the finally, we need to clear
  204. // this field
  205. jump(Label { *m_scheduled_jump });
  206. m_scheduled_jump = nullptr;
  207. } else {
  208. jump(resume_label);
  209. }
  210. return {};
  211. }
  212. VM::InterpreterExecutionScope Interpreter::ast_interpreter_scope()
  213. {
  214. if (!m_ast_interpreter)
  215. m_ast_interpreter = JS::Interpreter::create_with_existing_realm(m_realm);
  216. return { *m_ast_interpreter };
  217. }
  218. AK::Array<OwnPtr<PassManager>, static_cast<UnderlyingType<Interpreter::OptimizationLevel>>(Interpreter::OptimizationLevel::__Count)> Interpreter::s_optimization_pipelines {};
  219. Bytecode::PassManager& Interpreter::optimization_pipeline(Interpreter::OptimizationLevel level)
  220. {
  221. auto underlying_level = to_underlying(level);
  222. VERIFY(underlying_level <= to_underlying(Interpreter::OptimizationLevel::__Count));
  223. auto& entry = s_optimization_pipelines[underlying_level];
  224. if (entry)
  225. return *entry;
  226. auto pm = make<PassManager>();
  227. if (level == OptimizationLevel::None) {
  228. // No optimization.
  229. } else if (level == OptimizationLevel::Optimize) {
  230. pm->add<Passes::GenerateCFG>();
  231. pm->add<Passes::UnifySameBlocks>();
  232. pm->add<Passes::GenerateCFG>();
  233. pm->add<Passes::MergeBlocks>();
  234. pm->add<Passes::GenerateCFG>();
  235. pm->add<Passes::UnifySameBlocks>();
  236. pm->add<Passes::GenerateCFG>();
  237. pm->add<Passes::MergeBlocks>();
  238. pm->add<Passes::GenerateCFG>();
  239. pm->add<Passes::PlaceBlocks>();
  240. pm->add<Passes::EliminateLoads>();
  241. } else {
  242. VERIFY_NOT_REACHED();
  243. }
  244. auto& passes = *pm;
  245. entry = move(pm);
  246. return passes;
  247. }
  248. }