Block.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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() const;
  36. void unseal();
  37. void dump() const;
  38. ReadonlyBytes instruction_stream() const { return ReadonlyBytes { m_buffer, m_buffer_size }; }
  39. size_t register_count() const { return m_register_count; }
  40. void set_register_count(Badge<Bytecode::Generator>, size_t count) { m_register_count = count; }
  41. void* next_slot() { return m_buffer + m_buffer_size; }
  42. void grow(size_t additional_size);
  43. private:
  44. Block();
  45. size_t m_register_count { 0 };
  46. u8* m_buffer { nullptr };
  47. size_t m_buffer_capacity { 0 };
  48. size_t m_buffer_size { 0 };
  49. };
  50. }