BasicBlock.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. struct UnwindInfo {
  33. Executable const* executable;
  34. BasicBlock const* handler;
  35. BasicBlock const* finalizer;
  36. };
  37. class BasicBlock {
  38. AK_MAKE_NONCOPYABLE(BasicBlock);
  39. public:
  40. static NonnullOwnPtr<BasicBlock> create(String name, size_t size = 4 * KiB);
  41. ~BasicBlock();
  42. void seal();
  43. void dump(Executable const&) const;
  44. ReadonlyBytes instruction_stream() const { return ReadonlyBytes { m_buffer, m_buffer_size }; }
  45. size_t size() const { return m_buffer_size; }
  46. void* next_slot() { return m_buffer + m_buffer_size; }
  47. bool can_grow(size_t additional_size) const { return m_buffer_size + additional_size <= m_buffer_capacity; }
  48. void grow(size_t additional_size);
  49. void terminate(Badge<Generator>) { m_is_terminated = true; }
  50. bool is_terminated() const { return m_is_terminated; }
  51. String const& name() const { return m_name; }
  52. private:
  53. BasicBlock(String name, size_t size);
  54. u8* m_buffer { nullptr };
  55. size_t m_buffer_capacity { 0 };
  56. size_t m_buffer_size { 0 };
  57. bool m_is_terminated { false };
  58. String m_name;
  59. };
  60. }