Generator.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/NonnullOwnPtrVector.h>
  8. #include <AK/OwnPtr.h>
  9. #include <AK/SinglyLinkedList.h>
  10. #include <LibJS/Bytecode/BasicBlock.h>
  11. #include <LibJS/Bytecode/Executable.h>
  12. #include <LibJS/Bytecode/Label.h>
  13. #include <LibJS/Bytecode/Op.h>
  14. #include <LibJS/Bytecode/Register.h>
  15. #include <LibJS/Bytecode/StringTable.h>
  16. #include <LibJS/Forward.h>
  17. namespace JS::Bytecode {
  18. class Generator {
  19. public:
  20. static Executable generate(ASTNode const&, bool is_in_generator_function = false);
  21. Register allocate_register();
  22. void ensure_enough_space(size_t size)
  23. {
  24. // Make sure there's always enough space for a single jump at the end.
  25. if (!m_current_basic_block->can_grow(size + sizeof(Op::Jump))) {
  26. auto& new_block = make_block();
  27. emit<Op::Jump>().set_targets(
  28. Label { new_block },
  29. {});
  30. switch_to_basic_block(new_block);
  31. }
  32. }
  33. template<typename OpType, typename... Args>
  34. OpType& emit(Args&&... args)
  35. {
  36. VERIFY(!is_current_block_terminated());
  37. // If the block doesn't have enough space, switch to another block
  38. if constexpr (!OpType::IsTerminator)
  39. ensure_enough_space(sizeof(OpType));
  40. void* slot = next_slot();
  41. grow(sizeof(OpType));
  42. new (slot) OpType(forward<Args>(args)...);
  43. if constexpr (OpType::IsTerminator)
  44. m_current_basic_block->terminate({});
  45. return *static_cast<OpType*>(slot);
  46. }
  47. template<typename OpType, typename... Args>
  48. OpType& emit_with_extra_register_slots(size_t extra_register_slots, Args&&... args)
  49. {
  50. VERIFY(!is_current_block_terminated());
  51. // If the block doesn't have enough space, switch to another block
  52. if constexpr (!OpType::IsTerminator)
  53. ensure_enough_space(sizeof(OpType) + extra_register_slots * sizeof(Register));
  54. void* slot = next_slot();
  55. grow(sizeof(OpType) + extra_register_slots * sizeof(Register));
  56. new (slot) OpType(forward<Args>(args)...);
  57. if constexpr (OpType::IsTerminator)
  58. m_current_basic_block->terminate({});
  59. return *static_cast<OpType*>(slot);
  60. }
  61. void begin_continuable_scope(Label continue_target);
  62. void end_continuable_scope();
  63. void begin_breakable_scope(Label breakable_target);
  64. void end_breakable_scope();
  65. [[nodiscard]] Label nearest_continuable_scope() const;
  66. [[nodiscard]] Label nearest_breakable_scope() const;
  67. void switch_to_basic_block(BasicBlock& block)
  68. {
  69. m_current_basic_block = &block;
  70. }
  71. [[nodiscard]] BasicBlock& current_block() { return *m_current_basic_block; }
  72. BasicBlock& make_block(String name = {})
  73. {
  74. if (name.is_empty())
  75. name = String::number(m_next_block++);
  76. m_root_basic_blocks.append(BasicBlock::create(name));
  77. return m_root_basic_blocks.last();
  78. }
  79. bool is_current_block_terminated() const
  80. {
  81. return m_current_basic_block->is_terminated();
  82. }
  83. StringTableIndex intern_string(StringView const& string)
  84. {
  85. return m_string_table->insert(string);
  86. }
  87. bool is_in_generator_function() const { return m_is_in_generator_function; }
  88. void enter_generator_context() { m_is_in_generator_function = true; }
  89. void leave_generator_context() { m_is_in_generator_function = false; }
  90. private:
  91. Generator();
  92. ~Generator();
  93. void grow(size_t);
  94. void* next_slot();
  95. BasicBlock* m_current_basic_block { nullptr };
  96. NonnullOwnPtrVector<BasicBlock> m_root_basic_blocks;
  97. NonnullOwnPtr<StringTable> m_string_table;
  98. u32 m_next_register { 2 };
  99. u32 m_next_block { 1 };
  100. bool m_is_in_generator_function { false };
  101. Vector<Label> m_continuable_scopes;
  102. Vector<Label> m_breakable_scopes;
  103. };
  104. }