BasicBlock.h 1.9 KB

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