Generator.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/CodeGenerationError.h>
  12. #include <LibJS/Bytecode/Executable.h>
  13. #include <LibJS/Bytecode/IdentifierTable.h>
  14. #include <LibJS/Bytecode/Label.h>
  15. #include <LibJS/Bytecode/Op.h>
  16. #include <LibJS/Bytecode/Register.h>
  17. #include <LibJS/Bytecode/StringTable.h>
  18. #include <LibJS/Forward.h>
  19. #include <LibJS/Runtime/FunctionKind.h>
  20. namespace JS::Bytecode {
  21. class Generator {
  22. public:
  23. static CodeGenerationErrorOr<NonnullOwnPtr<Executable>> generate(ASTNode const&, FunctionKind = FunctionKind::Normal);
  24. Register allocate_register();
  25. void ensure_enough_space(size_t size)
  26. {
  27. // Make sure there's always enough space for a single jump at the end.
  28. if (!m_current_basic_block->can_grow(size + sizeof(Op::Jump))) {
  29. auto& new_block = make_block();
  30. emit<Op::Jump>().set_targets(
  31. Label { new_block },
  32. {});
  33. switch_to_basic_block(new_block);
  34. }
  35. }
  36. template<typename OpType, typename... Args>
  37. OpType& emit(Args&&... args)
  38. {
  39. VERIFY(!is_current_block_terminated());
  40. // If the block doesn't have enough space, switch to another block
  41. if constexpr (!OpType::IsTerminator)
  42. ensure_enough_space(sizeof(OpType));
  43. void* slot = next_slot();
  44. grow(sizeof(OpType));
  45. new (slot) OpType(forward<Args>(args)...);
  46. if constexpr (OpType::IsTerminator)
  47. m_current_basic_block->terminate({});
  48. return *static_cast<OpType*>(slot);
  49. }
  50. template<typename OpType, typename... Args>
  51. OpType& emit_with_extra_register_slots(size_t extra_register_slots, Args&&... args)
  52. {
  53. VERIFY(!is_current_block_terminated());
  54. // If the block doesn't have enough space, switch to another block
  55. if constexpr (!OpType::IsTerminator)
  56. ensure_enough_space(sizeof(OpType) + extra_register_slots * sizeof(Register));
  57. void* slot = next_slot();
  58. grow(sizeof(OpType) + extra_register_slots * sizeof(Register));
  59. new (slot) OpType(forward<Args>(args)...);
  60. if constexpr (OpType::IsTerminator)
  61. m_current_basic_block->terminate({});
  62. return *static_cast<OpType*>(slot);
  63. }
  64. CodeGenerationErrorOr<void> emit_load_from_reference(JS::ASTNode const&);
  65. CodeGenerationErrorOr<void> emit_store_to_reference(JS::ASTNode const&);
  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(String string)
  89. {
  90. return m_string_table->insert(move(string));
  91. }
  92. IdentifierTableIndex intern_identifier(FlyString string)
  93. {
  94. return m_identifier_table->insert(move(string));
  95. }
  96. bool is_in_generator_or_async_function() const { return m_enclosing_function_kind == FunctionKind::Async || m_enclosing_function_kind == FunctionKind::Generator; }
  97. bool is_in_generator_function() const { return m_enclosing_function_kind == FunctionKind::Generator; }
  98. bool is_in_async_function() const { return m_enclosing_function_kind == FunctionKind::Async; }
  99. private:
  100. Generator();
  101. ~Generator();
  102. void grow(size_t);
  103. void* next_slot();
  104. BasicBlock* m_current_basic_block { nullptr };
  105. NonnullOwnPtrVector<BasicBlock> m_root_basic_blocks;
  106. NonnullOwnPtr<StringTable> m_string_table;
  107. NonnullOwnPtr<IdentifierTable> m_identifier_table;
  108. u32 m_next_register { 2 };
  109. u32 m_next_block { 1 };
  110. FunctionKind m_enclosing_function_kind { FunctionKind::Normal };
  111. Vector<Label> m_continuable_scopes;
  112. Vector<Label> m_breakable_scopes;
  113. };
  114. }