Interpreter.cpp 3.2 KB

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