Interpreter.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Bytecode/Block.h>
  7. #include <LibJS/Bytecode/Instruction.h>
  8. #include <LibJS/Bytecode/Interpreter.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. namespace JS::Bytecode {
  11. Interpreter::Interpreter(GlobalObject& global_object)
  12. : m_vm(global_object.vm())
  13. , m_global_object(global_object)
  14. {
  15. }
  16. Interpreter::~Interpreter()
  17. {
  18. }
  19. void Interpreter::run(Bytecode::Block const& block)
  20. {
  21. dbgln("Bytecode::Interpreter will run block {:p}", &block);
  22. m_registers.resize(block.register_count());
  23. size_t pc = 0;
  24. while (pc < block.instructions().size()) {
  25. auto& instruction = block.instructions()[pc];
  26. instruction.execute(*this);
  27. if (m_pending_jump.has_value()) {
  28. pc = m_pending_jump.release_value();
  29. continue;
  30. }
  31. ++pc;
  32. }
  33. dbgln("Bytecode::Interpreter did run block {:p}", &block);
  34. for (size_t i = 0; i < m_registers.size(); ++i) {
  35. String value_string;
  36. if (m_registers[i].is_empty())
  37. value_string = "(empty)";
  38. else
  39. value_string = m_registers[i].to_string_without_side_effects();
  40. dbgln("[{:3}] {}", i, value_string);
  41. }
  42. }
  43. }