Interpreter.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. Interpreter* Interpreter::current()
  18. {
  19. return s_current;
  20. }
  21. Interpreter::Interpreter(GlobalObject& global_object, Realm& realm)
  22. : m_vm(global_object.vm())
  23. , m_global_object(global_object)
  24. , m_realm(realm)
  25. {
  26. VERIFY(!s_current);
  27. s_current = this;
  28. }
  29. Interpreter::~Interpreter()
  30. {
  31. VERIFY(s_current == this);
  32. s_current = nullptr;
  33. }
  34. Value Interpreter::run(Executable const& executable, BasicBlock const* entry_point)
  35. {
  36. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable);
  37. TemporaryChange restore_executable { m_current_executable, &executable };
  38. vm().set_last_value(Badge<Interpreter> {}, {});
  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. vm().push_execution_context(execution_context, global_object());
  50. VERIFY(!vm().exception());
  51. }
  52. auto block = entry_point ?: &executable.basic_blocks.first();
  53. if (m_manually_entered_frames) {
  54. VERIFY(registers().size() >= executable.number_of_registers);
  55. } else {
  56. m_register_windows.append(make<RegisterWindow>());
  57. registers().resize(executable.number_of_registers);
  58. registers()[Register::global_object_index] = Value(&global_object());
  59. }
  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.handler) {
  73. block = unwind_context.handler;
  74. unwind_context.handler = nullptr;
  75. accumulator() = vm().exception()->value();
  76. vm().clear_exception();
  77. will_jump = true;
  78. } else if (unwind_context.finalizer) {
  79. block = unwind_context.finalizer;
  80. m_unwind_contexts.take_last();
  81. will_jump = true;
  82. m_saved_exception = Handle<Exception>::create(vm().exception());
  83. vm().clear_exception();
  84. }
  85. }
  86. if (m_pending_jump.has_value()) {
  87. block = m_pending_jump.release_value();
  88. will_jump = true;
  89. break;
  90. }
  91. if (!m_return_value.is_empty()) {
  92. will_return = true;
  93. break;
  94. }
  95. ++pc;
  96. }
  97. if (will_return)
  98. break;
  99. if (pc.at_end() && !will_jump)
  100. break;
  101. if (vm().exception())
  102. break;
  103. }
  104. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable);
  105. if constexpr (JS_BYTECODE_DEBUG) {
  106. for (size_t i = 0; i < registers().size(); ++i) {
  107. String value_string;
  108. if (registers()[i].is_empty())
  109. value_string = "(empty)";
  110. else
  111. value_string = registers()[i].to_string_without_side_effects();
  112. dbgln("[{:3}] {}", i, value_string);
  113. }
  114. }
  115. vm().set_last_value(Badge<Interpreter> {}, accumulator());
  116. if (!m_manually_entered_frames)
  117. m_register_windows.take_last();
  118. auto return_value = m_return_value.value_or(js_undefined());
  119. m_return_value = {};
  120. // NOTE: The return value from a called function is put into $0 in the caller context.
  121. if (!m_register_windows.is_empty())
  122. m_register_windows.last()[0] = return_value;
  123. if (vm().execution_context_stack().size() == 1)
  124. vm().pop_execution_context();
  125. vm().finish_execution_generation();
  126. return return_value;
  127. }
  128. void Interpreter::enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target)
  129. {
  130. m_unwind_contexts.empend(handler_target.has_value() ? &handler_target->block() : nullptr, finalizer_target.has_value() ? &finalizer_target->block() : nullptr);
  131. }
  132. void Interpreter::leave_unwind_context()
  133. {
  134. m_unwind_contexts.take_last();
  135. }
  136. void Interpreter::continue_pending_unwind(Label const& resume_label)
  137. {
  138. if (!m_saved_exception.is_null()) {
  139. vm().set_exception(*m_saved_exception.cell());
  140. m_saved_exception = {};
  141. } else {
  142. jump(resume_label);
  143. }
  144. }
  145. AK::Array<OwnPtr<PassManager>, static_cast<UnderlyingType<Interpreter::OptimizationLevel>>(Interpreter::OptimizationLevel::__Count)> Interpreter::s_optimization_pipelines {};
  146. Bytecode::PassManager& Interpreter::optimization_pipeline(Interpreter::OptimizationLevel level)
  147. {
  148. auto underlying_level = to_underlying(level);
  149. VERIFY(underlying_level <= to_underlying(Interpreter::OptimizationLevel::__Count));
  150. auto& entry = s_optimization_pipelines[underlying_level];
  151. if (entry)
  152. return *entry;
  153. auto pm = make<PassManager>();
  154. if (level == OptimizationLevel::Default) {
  155. pm->add<Passes::GenerateCFG>();
  156. pm->add<Passes::UnifySameBlocks>();
  157. pm->add<Passes::GenerateCFG>();
  158. pm->add<Passes::MergeBlocks>();
  159. pm->add<Passes::GenerateCFG>();
  160. pm->add<Passes::UnifySameBlocks>();
  161. pm->add<Passes::GenerateCFG>();
  162. pm->add<Passes::MergeBlocks>();
  163. pm->add<Passes::GenerateCFG>();
  164. pm->add<Passes::PlaceBlocks>();
  165. } else {
  166. VERIFY_NOT_REACHED();
  167. }
  168. auto& passes = *pm;
  169. entry = move(pm);
  170. return passes;
  171. }
  172. }