BasicBlock.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. class InstructionStreamIterator {
  13. public:
  14. explicit InstructionStreamIterator(ReadonlyBytes bytes)
  15. : m_bytes(bytes)
  16. {
  17. }
  18. size_t offset() const { return m_offset; }
  19. bool at_end() const { return m_offset >= m_bytes.size(); }
  20. void jump(size_t offset)
  21. {
  22. VERIFY(offset <= m_bytes.size());
  23. m_offset = offset;
  24. }
  25. Instruction const& operator*() const { return dereference(); }
  26. void operator++();
  27. private:
  28. Instruction const& dereference() const { return *reinterpret_cast<Instruction const*>(m_bytes.data() + offset()); }
  29. ReadonlyBytes m_bytes;
  30. size_t m_offset { 0 };
  31. };
  32. class BasicBlock {
  33. public:
  34. static NonnullOwnPtr<BasicBlock> create(String name);
  35. ~BasicBlock();
  36. void seal();
  37. void dump() const;
  38. ReadonlyBytes instruction_stream() const { return ReadonlyBytes { m_buffer, m_buffer_size }; }
  39. void* next_slot() { return m_buffer + m_buffer_size; }
  40. void grow(size_t additional_size);
  41. void terminate(Badge<Generator>) { m_is_terminated = true; }
  42. bool is_terminated() const { return m_is_terminated; }
  43. String const& name() const { return m_name; }
  44. private:
  45. BasicBlock(String name);
  46. u8* m_buffer { nullptr };
  47. size_t m_buffer_capacity { 0 };
  48. size_t m_buffer_size { 0 };
  49. bool m_is_terminated { false };
  50. String m_name;
  51. };
  52. }