Register.h 998 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. namespace JS::Bytecode {
  9. class Register {
  10. public:
  11. constexpr static u32 accumulator_index = 0;
  12. static constexpr Register accumulator()
  13. {
  14. return Register(accumulator_index);
  15. }
  16. constexpr explicit Register(u32 index)
  17. : m_index(index)
  18. {
  19. }
  20. constexpr bool operator==(Register reg) const { return m_index == reg.index(); }
  21. constexpr u32 index() const { return m_index; }
  22. private:
  23. u32 m_index;
  24. };
  25. }
  26. template<>
  27. struct AK::Formatter<JS::Bytecode::Register> : AK::Formatter<FormatString> {
  28. ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Register const& value)
  29. {
  30. if (value.index() == JS::Bytecode::Register::accumulator_index)
  31. return builder.put_string("acc"sv);
  32. return AK::Formatter<FormatString>::format(builder, "${}"sv, value.index());
  33. }
  34. };