Interpreter.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. Value Interpreter::run(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) {
  55. VERIFY(registers().size() >= executable.number_of_registers);
  56. } else {
  57. m_register_windows.append(make<RegisterWindow>());
  58. registers().resize(executable.number_of_registers);
  59. registers()[Register::global_object_index] = Value(&global_object());
  60. }
  61. for (;;) {
  62. Bytecode::InstructionStreamIterator pc(block->instruction_stream());
  63. bool will_jump = false;
  64. bool will_return = false;
  65. while (!pc.at_end()) {
  66. auto& instruction = *pc;
  67. instruction.execute(*this);
  68. if (vm().exception()) {
  69. m_saved_exception = {};
  70. if (m_unwind_contexts.is_empty())
  71. break;
  72. auto& unwind_context = m_unwind_contexts.last();
  73. if (unwind_context.handler) {
  74. block = unwind_context.handler;
  75. unwind_context.handler = nullptr;
  76. accumulator() = vm().exception()->value();
  77. vm().clear_exception();
  78. will_jump = true;
  79. } else if (unwind_context.finalizer) {
  80. block = unwind_context.finalizer;
  81. m_unwind_contexts.take_last();
  82. will_jump = true;
  83. m_saved_exception = Handle<Exception>::create(vm().exception());
  84. vm().clear_exception();
  85. }
  86. }
  87. if (m_pending_jump.has_value()) {
  88. block = m_pending_jump.release_value();
  89. will_jump = true;
  90. break;
  91. }
  92. if (!m_return_value.is_empty()) {
  93. will_return = true;
  94. break;
  95. }
  96. ++pc;
  97. }
  98. if (will_return)
  99. break;
  100. if (pc.at_end() && !will_jump)
  101. break;
  102. if (vm().exception())
  103. break;
  104. }
  105. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable);
  106. if constexpr (JS_BYTECODE_DEBUG) {
  107. for (size_t i = 0; i < registers().size(); ++i) {
  108. String value_string;
  109. if (registers()[i].is_empty())
  110. value_string = "(empty)";
  111. else
  112. value_string = registers()[i].to_string_without_side_effects();
  113. dbgln("[{:3}] {}", i, value_string);
  114. }
  115. }
  116. vm().set_last_value(Badge<Interpreter> {}, accumulator());
  117. if (!m_manually_entered_frames)
  118. m_register_windows.take_last();
  119. auto return_value = m_return_value.value_or(js_undefined());
  120. m_return_value = {};
  121. // NOTE: The return value from a called function is put into $0 in the caller context.
  122. if (!m_register_windows.is_empty())
  123. m_register_windows.last()[0] = return_value;
  124. if (vm().execution_context_stack().size() == 1)
  125. vm().pop_execution_context();
  126. vm().finish_execution_generation();
  127. return return_value;
  128. }
  129. void Interpreter::enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target)
  130. {
  131. m_unwind_contexts.empend(handler_target.has_value() ? &handler_target->block() : nullptr, finalizer_target.has_value() ? &finalizer_target->block() : nullptr);
  132. }
  133. void Interpreter::leave_unwind_context()
  134. {
  135. m_unwind_contexts.take_last();
  136. }
  137. void Interpreter::continue_pending_unwind(Label const& resume_label)
  138. {
  139. if (!m_saved_exception.is_null()) {
  140. vm().set_exception(*m_saved_exception.cell());
  141. m_saved_exception = {};
  142. } else {
  143. jump(resume_label);
  144. }
  145. }
  146. AK::Array<OwnPtr<PassManager>, static_cast<UnderlyingType<Interpreter::OptimizationLevel>>(Interpreter::OptimizationLevel::__Count)> Interpreter::s_optimization_pipelines {};
  147. Bytecode::PassManager& Interpreter::optimization_pipeline(Interpreter::OptimizationLevel level)
  148. {
  149. auto underlying_level = to_underlying(level);
  150. VERIFY(underlying_level <= to_underlying(Interpreter::OptimizationLevel::__Count));
  151. auto& entry = s_optimization_pipelines[underlying_level];
  152. if (entry)
  153. return *entry;
  154. auto pm = make<PassManager>();
  155. if (level == OptimizationLevel::Default) {
  156. pm->add<Passes::GenerateCFG>();
  157. pm->add<Passes::UnifySameBlocks>();
  158. pm->add<Passes::GenerateCFG>();
  159. pm->add<Passes::MergeBlocks>();
  160. pm->add<Passes::GenerateCFG>();
  161. pm->add<Passes::UnifySameBlocks>();
  162. pm->add<Passes::GenerateCFG>();
  163. pm->add<Passes::MergeBlocks>();
  164. pm->add<Passes::GenerateCFG>();
  165. pm->add<Passes::PlaceBlocks>();
  166. } else {
  167. VERIFY_NOT_REACHED();
  168. }
  169. auto& passes = *pm;
  170. entry = move(pm);
  171. return passes;
  172. }
  173. }