Generator.h 4.4 KB

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