Interpreter.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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)
  32. {
  33. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable);
  34. TemporaryChange restore_executable { m_current_executable, &executable };
  35. CallFrame global_call_frame;
  36. if (vm().call_stack().is_empty()) {
  37. global_call_frame.this_value = &global_object();
  38. static FlyString global_execution_context_name = "(*BC* global execution context)";
  39. global_call_frame.function_name = global_execution_context_name;
  40. global_call_frame.scope = &global_object();
  41. VERIFY(!vm().exception());
  42. // FIXME: How do we know if we're in strict mode? Maybe the Bytecode::Block should know this?
  43. // global_call_frame.is_strict_mode = ???;
  44. vm().push_call_frame(global_call_frame, global_object());
  45. VERIFY(!vm().exception());
  46. }
  47. auto block = &executable.basic_blocks.first();
  48. m_register_windows.append(make<RegisterWindow>());
  49. registers().resize(executable.number_of_registers);
  50. for (;;) {
  51. Bytecode::InstructionStreamIterator pc(block->instruction_stream());
  52. bool will_jump = false;
  53. bool will_return = false;
  54. while (!pc.at_end()) {
  55. auto& instruction = *pc;
  56. instruction.execute(*this);
  57. if (vm().exception())
  58. break;
  59. if (m_pending_jump.has_value()) {
  60. block = m_pending_jump.release_value();
  61. will_jump = true;
  62. break;
  63. }
  64. if (!m_return_value.is_empty()) {
  65. will_return = true;
  66. break;
  67. }
  68. ++pc;
  69. }
  70. if (will_return)
  71. break;
  72. if (pc.at_end() && !will_jump)
  73. break;
  74. if (vm().exception())
  75. break;
  76. }
  77. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &executable);
  78. if constexpr (JS_BYTECODE_DEBUG) {
  79. for (size_t i = 0; i < registers().size(); ++i) {
  80. String value_string;
  81. if (registers()[i].is_empty())
  82. value_string = "(empty)";
  83. else
  84. value_string = registers()[i].to_string_without_side_effects();
  85. dbgln("[{:3}] {}", i, value_string);
  86. }
  87. }
  88. m_register_windows.take_last();
  89. auto return_value = m_return_value.value_or(js_undefined());
  90. m_return_value = {};
  91. // NOTE: The return value from a called function is put into $0 in the caller context.
  92. if (!m_register_windows.is_empty())
  93. m_register_windows.last()[0] = return_value;
  94. if (vm().call_stack().size() == 1)
  95. vm().pop_call_frame();
  96. return return_value;
  97. }
  98. }