Generator.h 4.1 KB

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