BasicBlock.h 1.5 KB

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