Generator.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/Register.h>
  13. #include <LibJS/Bytecode/StringTable.h>
  14. #include <LibJS/Forward.h>
  15. namespace JS::Bytecode {
  16. struct Executable {
  17. NonnullOwnPtrVector<BasicBlock> basic_blocks;
  18. NonnullOwnPtr<StringTable> string_table;
  19. size_t number_of_registers { 0 };
  20. String const& get_string(StringTableIndex index) const { return string_table->get(index); }
  21. };
  22. class Generator {
  23. public:
  24. static Executable generate(ASTNode const&);
  25. Register allocate_register();
  26. template<typename OpType, typename... Args>
  27. OpType& emit(Args&&... args)
  28. {
  29. VERIFY(!is_current_block_terminated());
  30. void* slot = next_slot();
  31. grow(sizeof(OpType));
  32. new (slot) OpType(forward<Args>(args)...);
  33. if constexpr (OpType::IsTerminator)
  34. m_current_basic_block->terminate({});
  35. return *static_cast<OpType*>(slot);
  36. }
  37. template<typename OpType, typename... Args>
  38. OpType& emit_with_extra_register_slots(size_t extra_register_slots, Args&&... args)
  39. {
  40. VERIFY(!is_current_block_terminated());
  41. void* slot = next_slot();
  42. grow(sizeof(OpType) + extra_register_slots * sizeof(Register));
  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. void begin_continuable_scope(Label continue_target);
  49. void end_continuable_scope();
  50. void begin_breakable_scope(Label breakable_target);
  51. void end_breakable_scope();
  52. [[nodiscard]] Label nearest_continuable_scope() const;
  53. [[nodiscard]] Label nearest_breakable_scope() const;
  54. void switch_to_basic_block(BasicBlock& block)
  55. {
  56. m_current_basic_block = &block;
  57. }
  58. [[nodiscard]] BasicBlock& current_block() { return *m_current_basic_block; }
  59. BasicBlock& make_block(String name = {})
  60. {
  61. if (name.is_empty())
  62. name = String::number(m_next_block++);
  63. m_root_basic_blocks.append(BasicBlock::create(name));
  64. return m_root_basic_blocks.last();
  65. }
  66. bool is_current_block_terminated() const
  67. {
  68. return m_current_basic_block->is_terminated();
  69. }
  70. StringTableIndex intern_string(StringView const& string)
  71. {
  72. return m_string_table->insert(string);
  73. }
  74. private:
  75. Generator();
  76. ~Generator();
  77. void grow(size_t);
  78. void* next_slot();
  79. BasicBlock* m_current_basic_block { nullptr };
  80. NonnullOwnPtrVector<BasicBlock> m_root_basic_blocks;
  81. NonnullOwnPtr<StringTable> m_string_table;
  82. u32 m_next_register { 2 };
  83. u32 m_next_block { 1 };
  84. Vector<Label> m_continuable_scopes;
  85. Vector<Label> m_breakable_scopes;
  86. };
  87. }