Generator.h 790 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. private:
  25. Generator();
  26. ~Generator();
  27. void append(NonnullOwnPtr<Instruction>);
  28. OwnPtr<Block> m_block;
  29. u32 m_next_register { 1 };
  30. };
  31. }