BasicBlock.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/Badge.h>
  8. #include <AK/NonnullOwnPtrVector.h>
  9. #include <AK/String.h>
  10. #include <LibJS/Forward.h>
  11. namespace JS::Bytecode {
  12. struct UnwindInfo {
  13. Executable const* executable;
  14. BasicBlock const* handler;
  15. BasicBlock const* finalizer;
  16. };
  17. class BasicBlock {
  18. AK_MAKE_NONCOPYABLE(BasicBlock);
  19. public:
  20. static NonnullOwnPtr<BasicBlock> create(String name, size_t size = 4 * KiB);
  21. ~BasicBlock();
  22. void seal();
  23. void dump(Executable const&) const;
  24. ReadonlyBytes instruction_stream() const { return ReadonlyBytes { m_buffer, m_buffer_size }; }
  25. size_t size() const { return m_buffer_size; }
  26. void* next_slot() { return m_buffer + m_buffer_size; }
  27. bool can_grow(size_t additional_size) const { return m_buffer_size + additional_size <= m_buffer_capacity; }
  28. void grow(size_t additional_size);
  29. void terminate(Badge<Generator>) { m_is_terminated = true; }
  30. bool is_terminated() const { return m_is_terminated; }
  31. String const& name() const { return m_name; }
  32. private:
  33. BasicBlock(String name, size_t size);
  34. u8* m_buffer { nullptr };
  35. size_t m_buffer_capacity { 0 };
  36. size_t m_buffer_size { 0 };
  37. bool m_is_terminated { false };
  38. String m_name;
  39. };
  40. }