Interpreter.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <LibJS/Bytecode/BasicBlock.h>
  8. #include <LibJS/Bytecode/Instruction.h>
  9. #include <LibJS/Bytecode/Interpreter.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. namespace JS::Bytecode {
  12. static Interpreter* s_current;
  13. Interpreter* Interpreter::current()
  14. {
  15. return s_current;
  16. }
  17. Interpreter::Interpreter(GlobalObject& global_object)
  18. : m_vm(global_object.vm())
  19. , m_global_object(global_object)
  20. {
  21. VERIFY(!s_current);
  22. s_current = this;
  23. }
  24. Interpreter::~Interpreter()
  25. {
  26. VERIFY(s_current == this);
  27. s_current = nullptr;
  28. }
  29. Value Interpreter::run(ExecutionUnit const& execution_unit)
  30. {
  31. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &execution_unit);
  32. CallFrame global_call_frame;
  33. if (vm().call_stack().is_empty()) {
  34. global_call_frame.this_value = &global_object();
  35. static FlyString global_execution_context_name = "(*BC* global execution context)";
  36. global_call_frame.function_name = global_execution_context_name;
  37. global_call_frame.scope = &global_object();
  38. VERIFY(!vm().exception());
  39. // FIXME: How do we know if we're in strict mode? Maybe the Bytecode::Block should know this?
  40. // global_call_frame.is_strict_mode = ???;
  41. vm().push_call_frame(global_call_frame, global_object());
  42. VERIFY(!vm().exception());
  43. }
  44. auto block = &execution_unit.basic_blocks.first();
  45. m_register_windows.append(make<RegisterWindow>());
  46. registers().resize(execution_unit.number_of_registers);
  47. for (;;) {
  48. Bytecode::InstructionStreamIterator pc(block->instruction_stream());
  49. bool will_jump = false;
  50. bool will_return = false;
  51. while (!pc.at_end()) {
  52. auto& instruction = *pc;
  53. instruction.execute(*this);
  54. if (m_pending_jump.has_value()) {
  55. block = m_pending_jump.release_value();
  56. will_jump = true;
  57. break;
  58. }
  59. if (!m_return_value.is_empty()) {
  60. will_return = true;
  61. break;
  62. }
  63. ++pc;
  64. }
  65. if (will_return)
  66. break;
  67. if (pc.at_end() && !will_jump)
  68. break;
  69. }
  70. dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter did run unit {:p}", &execution_unit);
  71. if constexpr (JS_BYTECODE_DEBUG) {
  72. for (size_t i = 0; i < registers().size(); ++i) {
  73. String value_string;
  74. if (registers()[i].is_empty())
  75. value_string = "(empty)";
  76. else
  77. value_string = registers()[i].to_string_without_side_effects();
  78. dbgln("[{:3}] {}", i, value_string);
  79. }
  80. }
  81. m_register_windows.take_last();
  82. auto return_value = m_return_value.value_or(js_undefined());
  83. m_return_value = {};
  84. // NOTE: The return value from a called function is put into $0 in the caller context.
  85. if (!m_register_windows.is_empty())
  86. m_register_windows.last()[0] = return_value;
  87. if (vm().call_stack().size() == 1)
  88. vm().pop_call_frame();
  89. return return_value;
  90. }
  91. }