Generator.h 947 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/OwnPtr.h>
  8. #include <LibJS/Bytecode/Label.h>
  9. #include <LibJS/Forward.h>
  10. namespace JS::Bytecode {
  11. class Generator {
  12. public:
  13. static OwnPtr<Block> generate(ASTNode const&);
  14. Register allocate_register();
  15. template<typename OpType, typename... Args>
  16. OpType& emit(Args&&... args)
  17. {
  18. auto instruction = make<OpType>(forward<Args>(args)...);
  19. auto* ptr = instruction.ptr();
  20. append(move(instruction));
  21. return *ptr;
  22. }
  23. Label make_label() const;
  24. void begin_continuable_scope();
  25. void end_continuable_scope();
  26. Label nearest_continuable_scope() const;
  27. private:
  28. Generator();
  29. ~Generator();
  30. void append(NonnullOwnPtr<Instruction>);
  31. OwnPtr<Block> m_block;
  32. u32 m_next_register { 1 };
  33. Vector<Label> m_continuable_scopes;
  34. };
  35. }