Label.h 1004 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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/Format.h>
  8. #include <LibJS/Bytecode/BasicBlock.h>
  9. namespace JS::Bytecode {
  10. class Label {
  11. public:
  12. explicit Label(BasicBlock const& block)
  13. : m_block(&block)
  14. {
  15. }
  16. // Used while compiling.
  17. BasicBlock const& block() const { return *m_block; }
  18. // Used after compiling.
  19. size_t address() const { return m_address; }
  20. void set_address(size_t address) { m_address = address; }
  21. private:
  22. union {
  23. // Relevant while compiling.
  24. BasicBlock const* m_block { nullptr };
  25. // Relevant after compiling.
  26. size_t m_address;
  27. };
  28. };
  29. }
  30. template<>
  31. struct AK::Formatter<JS::Bytecode::Label> : AK::Formatter<FormatString> {
  32. ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Label const& label)
  33. {
  34. return AK::Formatter<FormatString>::format(builder, "@{:x}"sv, label.address());
  35. }
  36. };