Interpreter.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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>());
  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. accumulator() = exception_value;
  84. m_saved_exception = {};
  85. will_jump = true;
  86. break;
  87. }
  88. if (unwind_context.finalizer) {
  89. block = unwind_context.finalizer;
  90. m_unwind_contexts.take_last();
  91. will_jump = true;
  92. break;
  93. }
  94. }
  95. if (m_pending_jump.has_value()) {
  96. block = m_pending_jump.release_value();
  97. will_jump = true;
  98. break;
  99. }
  100. if (!m_return_value.is_empty()) {
  101. will_return = true;
  102. break;
  103. }
  104. ++pc;
  105. }
  106. if (will_return)
  107. break;
  108. if (pc.at_end() && !will_jump)
  109. break;
  110. if (!m_saved_exception.is_null())
  111. break;
  112. }
  113. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable);
  114. if constexpr (JS_BYTECODE_DEBUG) {
  115. for (size_t i = 0; i < registers().size(); ++i) {
  116. String value_string;
  117. if (registers()[i].is_empty())
  118. value_string = "(empty)";
  119. else
  120. value_string = registers()[i].to_string_without_side_effects();
  121. dbgln("[{:3}] {}", i, value_string);
  122. }
  123. }
  124. OwnPtr<RegisterWindow> frame;
  125. if (!m_manually_entered_frames.last()) {
  126. frame = m_register_windows.take_last();
  127. m_manually_entered_frames.take_last();
  128. }
  129. auto return_value = m_return_value.value_or(js_undefined());
  130. m_return_value = {};
  131. // NOTE: The return value from a called function is put into $0 in the caller context.
  132. if (!m_register_windows.is_empty())
  133. m_register_windows.last()[0] = return_value;
  134. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  135. // in which case this is a no-op.
  136. vm().run_queued_promise_jobs();
  137. if (pushed_execution_context) {
  138. VERIFY(&vm().running_execution_context() == &execution_context);
  139. vm().pop_execution_context();
  140. }
  141. vm().finish_execution_generation();
  142. if (!m_saved_exception.is_null()) {
  143. Value thrown_value = m_saved_exception.value();
  144. m_saved_exception = {};
  145. return { throw_completion(thrown_value), move(frame) };
  146. }
  147. return { return_value, move(frame) };
  148. }
  149. void Interpreter::enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target)
  150. {
  151. m_unwind_contexts.empend(m_current_executable, handler_target.has_value() ? &handler_target->block() : nullptr, finalizer_target.has_value() ? &finalizer_target->block() : nullptr);
  152. }
  153. void Interpreter::leave_unwind_context()
  154. {
  155. m_unwind_contexts.take_last();
  156. }
  157. ThrowCompletionOr<void> Interpreter::continue_pending_unwind(Label const& resume_label)
  158. {
  159. if (!m_saved_exception.is_null()) {
  160. auto result = throw_completion(m_saved_exception.value());
  161. m_saved_exception = {};
  162. return result;
  163. }
  164. jump(resume_label);
  165. return {};
  166. }
  167. VM::InterpreterExecutionScope Interpreter::ast_interpreter_scope()
  168. {
  169. if (!m_ast_interpreter)
  170. m_ast_interpreter = JS::Interpreter::create_with_existing_realm(m_realm);
  171. return { *m_ast_interpreter };
  172. }
  173. AK::Array<OwnPtr<PassManager>, static_cast<UnderlyingType<Interpreter::OptimizationLevel>>(Interpreter::OptimizationLevel::__Count)> Interpreter::s_optimization_pipelines {};
  174. Bytecode::PassManager& Interpreter::optimization_pipeline(Interpreter::OptimizationLevel level)
  175. {
  176. auto underlying_level = to_underlying(level);
  177. VERIFY(underlying_level <= to_underlying(Interpreter::OptimizationLevel::__Count));
  178. auto& entry = s_optimization_pipelines[underlying_level];
  179. if (entry)
  180. return *entry;
  181. auto pm = make<PassManager>();
  182. if (level == OptimizationLevel::Default) {
  183. pm->add<Passes::GenerateCFG>();
  184. pm->add<Passes::UnifySameBlocks>();
  185. pm->add<Passes::GenerateCFG>();
  186. pm->add<Passes::MergeBlocks>();
  187. pm->add<Passes::GenerateCFG>();
  188. pm->add<Passes::UnifySameBlocks>();
  189. pm->add<Passes::GenerateCFG>();
  190. pm->add<Passes::MergeBlocks>();
  191. pm->add<Passes::GenerateCFG>();
  192. pm->add<Passes::PlaceBlocks>();
  193. } else {
  194. VERIFY_NOT_REACHED();
  195. }
  196. auto& passes = *pm;
  197. entry = move(pm);
  198. return passes;
  199. }
  200. }