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