Block.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 <LibJS/Forward.h>
  10. namespace JS::Bytecode {
  11. class InstructionStreamIterator {
  12. public:
  13. explicit InstructionStreamIterator(ReadonlyBytes bytes)
  14. : m_bytes(bytes)
  15. {
  16. }
  17. size_t offset() const { return m_offset; }
  18. bool at_end() const { return m_offset >= m_bytes.size(); }
  19. void jump(size_t offset)
  20. {
  21. VERIFY(offset <= m_bytes.size());
  22. m_offset = offset;
  23. }
  24. Instruction const& operator*() const { return dereference(); }
  25. void operator++();
  26. private:
  27. Instruction const& dereference() const { return *reinterpret_cast<Instruction const*>(m_bytes.data() + offset()); }
  28. ReadonlyBytes m_bytes;
  29. size_t m_offset { 0 };
  30. };
  31. class Block {
  32. public:
  33. static NonnullOwnPtr<Block> create();
  34. ~Block();
  35. void seal();
  36. void dump() const;
  37. ReadonlyBytes instruction_stream() const { return ReadonlyBytes { m_buffer, m_buffer_size }; }
  38. size_t register_count() const { return m_register_count; }
  39. void set_register_count(Badge<Bytecode::Generator>, size_t count) { m_register_count = count; }
  40. void* next_slot() { return m_buffer + m_buffer_size; }
  41. void grow(size_t additional_size);
  42. private:
  43. Block();
  44. size_t m_register_count { 0 };
  45. u8* m_buffer { nullptr };
  46. size_t m_buffer_capacity { 0 };
  47. size_t m_buffer_size { 0 };
  48. };
  49. }