Generator.h 4.2 KB

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