Block.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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/Bytecode/Register.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 Block {
  33. public:
  34. static NonnullOwnPtr<Block> create();
  35. ~Block();
  36. void dump() const;
  37. ReadonlyBytes instruction_stream() const { return m_buffer.span(); }
  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. Vector<u8>& buffer() { return m_buffer; }
  41. private:
  42. Block() = default;
  43. size_t m_register_count { 0 };
  44. Vector<u8> m_buffer;
  45. };
  46. }