Generator.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/Block.h>
  9. #include <LibJS/Bytecode/Label.h>
  10. #include <LibJS/Forward.h>
  11. namespace JS::Bytecode {
  12. class Generator {
  13. public:
  14. static OwnPtr<Block> generate(ASTNode const&);
  15. Register allocate_register();
  16. template<typename OpType, typename... Args>
  17. InstructionHandle<OpType> emit(Args&&... args)
  18. {
  19. return make_instruction<OpType>(0, forward<Args>(args)...);
  20. }
  21. template<typename OpType, typename... Args>
  22. InstructionHandle<OpType> emit_with_extra_register_slots(size_t extra_register_slots, Args&&... args)
  23. {
  24. return make_instruction<OpType>(extra_register_slots, forward<Args>(args)...);
  25. }
  26. Label make_label() const;
  27. void begin_continuable_scope();
  28. void end_continuable_scope();
  29. Label nearest_continuable_scope() const;
  30. private:
  31. Generator();
  32. ~Generator();
  33. template<typename OpType, typename... Args>
  34. InstructionHandle<OpType> make_instruction(size_t extra_register_slots, Args&&... args)
  35. {
  36. auto& buffer = m_block->buffer();
  37. auto offset = buffer.size();
  38. buffer.resize(buffer.size() + sizeof(OpType) + extra_register_slots * sizeof(Register));
  39. new (buffer.data() + offset) OpType(forward<Args>(args)...);
  40. return InstructionHandle<OpType>(offset, m_block);
  41. }
  42. OwnPtr<Block> m_block;
  43. u32 m_next_register { 1 };
  44. Vector<Label> m_continuable_scopes;
  45. };
  46. }