Interpreter.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/GlobalObject.h>
  13. namespace JS::Bytecode {
  14. static Interpreter* s_current;
  15. Interpreter* Interpreter::current()
  16. {
  17. return s_current;
  18. }
  19. Interpreter::Interpreter(GlobalObject& global_object)
  20. : m_vm(global_object.vm())
  21. , m_global_object(global_object)
  22. {
  23. VERIFY(!s_current);
  24. s_current = this;
  25. }
  26. Interpreter::~Interpreter()
  27. {
  28. VERIFY(s_current == this);
  29. s_current = nullptr;
  30. }
  31. Value Interpreter::run(Executable const& executable, BasicBlock const* entry_point)
  32. {
  33. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable);
  34. TemporaryChange restore_executable { m_current_executable, &executable };
  35. vm().set_last_value(Badge<Interpreter> {}, {});
  36. CallFrame global_call_frame;
  37. if (vm().call_stack().is_empty()) {
  38. global_call_frame.this_value = &global_object();
  39. static FlyString global_execution_context_name = "(*BC* global execution context)";
  40. global_call_frame.function_name = global_execution_context_name;
  41. global_call_frame.scope = &global_object();
  42. VERIFY(!vm().exception());
  43. // FIXME: How do we know if we're in strict mode? Maybe the Bytecode::Block should know this?
  44. // global_call_frame.is_strict_mode = ???;
  45. vm().push_call_frame(global_call_frame, global_object());
  46. VERIFY(!vm().exception());
  47. }
  48. auto block = entry_point ?: &executable.basic_blocks.first();
  49. if (m_manually_entered_frames) {
  50. VERIFY(registers().size() >= executable.number_of_registers);
  51. } else {
  52. m_register_windows.append(make<RegisterWindow>());
  53. registers().resize(executable.number_of_registers);
  54. registers()[Register::global_object_index] = Value(&global_object());
  55. }
  56. for (;;) {
  57. Bytecode::InstructionStreamIterator pc(block->instruction_stream());
  58. bool will_jump = false;
  59. bool will_return = false;
  60. while (!pc.at_end()) {
  61. auto& instruction = *pc;
  62. instruction.execute(*this);
  63. if (vm().exception()) {
  64. m_saved_exception = {};
  65. if (m_unwind_contexts.is_empty())
  66. break;
  67. auto& unwind_context = m_unwind_contexts.last();
  68. if (unwind_context.handler) {
  69. block = unwind_context.handler;
  70. unwind_context.handler = nullptr;
  71. accumulator() = vm().exception()->value();
  72. vm().clear_exception();
  73. will_jump = true;
  74. } else if (unwind_context.finalizer) {
  75. block = unwind_context.finalizer;
  76. m_unwind_contexts.take_last();
  77. will_jump = true;
  78. m_saved_exception = Handle<Exception>::create(vm().exception());
  79. vm().clear_exception();
  80. }
  81. }
  82. if (m_pending_jump.has_value()) {
  83. block = m_pending_jump.release_value();
  84. will_jump = true;
  85. break;
  86. }
  87. if (!m_return_value.is_empty()) {
  88. will_return = true;
  89. break;
  90. }
  91. ++pc;
  92. }
  93. if (will_return)
  94. break;
  95. if (pc.at_end() && !will_jump)
  96. break;
  97. if (vm().exception())
  98. break;
  99. }
  100. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable);
  101. if constexpr (JS_BYTECODE_DEBUG) {
  102. for (size_t i = 0; i < registers().size(); ++i) {
  103. String value_string;
  104. if (registers()[i].is_empty())
  105. value_string = "(empty)";
  106. else
  107. value_string = registers()[i].to_string_without_side_effects();
  108. dbgln("[{:3}] {}", i, value_string);
  109. }
  110. }
  111. vm().set_last_value(Badge<Interpreter> {}, accumulator());
  112. if (!m_manually_entered_frames)
  113. m_register_windows.take_last();
  114. auto return_value = m_return_value.value_or(js_undefined());
  115. m_return_value = {};
  116. // NOTE: The return value from a called function is put into $0 in the caller context.
  117. if (!m_register_windows.is_empty())
  118. m_register_windows.last()[0] = return_value;
  119. if (vm().call_stack().size() == 1)
  120. vm().pop_call_frame();
  121. vm().finish_execution_generation();
  122. return return_value;
  123. }
  124. void Interpreter::enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target)
  125. {
  126. m_unwind_contexts.empend(handler_target.has_value() ? &handler_target->block() : nullptr, finalizer_target.has_value() ? &finalizer_target->block() : nullptr);
  127. }
  128. void Interpreter::leave_unwind_context()
  129. {
  130. m_unwind_contexts.take_last();
  131. }
  132. void Interpreter::continue_pending_unwind(Label const& resume_label)
  133. {
  134. if (!m_saved_exception.is_null()) {
  135. vm().set_exception(*m_saved_exception.cell());
  136. m_saved_exception = {};
  137. } else {
  138. jump(resume_label);
  139. }
  140. }
  141. }