Interpreter.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Bytecode/Executable.h>
  8. #include <LibJS/Bytecode/Label.h>
  9. #include <LibJS/Bytecode/Register.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibJS/Heap/Cell.h>
  12. #include <LibJS/Runtime/FunctionKind.h>
  13. #include <LibJS/Runtime/VM.h>
  14. #include <LibJS/Runtime/Value.h>
  15. namespace JS::Bytecode {
  16. class InstructionStreamIterator;
  17. class Interpreter {
  18. public:
  19. explicit Interpreter(VM&);
  20. ~Interpreter();
  21. [[nodiscard]] Realm& realm() { return *m_realm; }
  22. [[nodiscard]] Object& global_object() { return *m_global_object; }
  23. [[nodiscard]] DeclarativeEnvironment& global_declarative_environment() { return *m_global_declarative_environment; }
  24. VM& vm() { return m_vm; }
  25. VM const& vm() const { return m_vm; }
  26. ThrowCompletionOr<Value> run(Script&, JS::GCPtr<Environment> lexical_environment_override = nullptr);
  27. ThrowCompletionOr<Value> run(SourceTextModule&);
  28. ThrowCompletionOr<Value> run(Bytecode::Executable& executable, Bytecode::BasicBlock const* entry_point = nullptr)
  29. {
  30. auto result_and_return_register = run_executable(executable, entry_point);
  31. return move(result_and_return_register.value);
  32. }
  33. struct ResultAndReturnRegister {
  34. ThrowCompletionOr<Value> value;
  35. Value return_register_value;
  36. };
  37. ResultAndReturnRegister run_executable(Bytecode::Executable&, Bytecode::BasicBlock const* entry_point);
  38. ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
  39. ALWAYS_INLINE Value& saved_return_value() { return reg(Register::saved_return_value()); }
  40. Value& reg(Register const& r)
  41. {
  42. return vm().running_execution_context().registers[r.index()];
  43. }
  44. Value reg(Register const& r) const
  45. {
  46. return vm().running_execution_context().registers[r.index()];
  47. }
  48. [[nodiscard]] Value get(Operand) const;
  49. void set(Operand, Value);
  50. void do_return(Value value)
  51. {
  52. reg(Register::return_value()) = value;
  53. reg(Register::exception()) = {};
  54. }
  55. void enter_unwind_context();
  56. void leave_unwind_context();
  57. void catch_exception(Operand dst);
  58. void restore_scheduled_jump();
  59. void leave_finally();
  60. void enter_object_environment(Object&);
  61. Executable& current_executable() { return *m_current_executable; }
  62. Executable const& current_executable() const { return *m_current_executable; }
  63. BasicBlock const& current_block() const { return *m_current_block; }
  64. Optional<InstructionStreamIterator const&> instruction_stream_iterator() const { return m_pc; }
  65. Vector<Value>& registers() { return vm().running_execution_context().registers; }
  66. Vector<Value> const& registers() const { return vm().running_execution_context().registers; }
  67. private:
  68. void run_bytecode();
  69. VM& m_vm;
  70. BasicBlock const* m_scheduled_jump { nullptr };
  71. GCPtr<Executable> m_current_executable { nullptr };
  72. BasicBlock const* m_current_block { nullptr };
  73. GCPtr<Realm> m_realm { nullptr };
  74. GCPtr<Object> m_global_object { nullptr };
  75. GCPtr<DeclarativeEnvironment> m_global_declarative_environment { nullptr };
  76. Optional<InstructionStreamIterator&> m_pc {};
  77. };
  78. extern bool g_dump_bytecode;
  79. ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM&, ASTNode const&, ReadonlySpan<FunctionParameter>, JS::FunctionKind kind, DeprecatedFlyString const& name);
  80. }