Register.h 935 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 Register accumulator()
  13. {
  14. static Register accumulator(accumulator_index);
  15. return accumulator;
  16. }
  17. explicit Register(u32 index)
  18. : m_index(index)
  19. {
  20. }
  21. 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. void format(FormatBuilder& builder, JS::Bytecode::Register const& value)
  29. {
  30. if (value.index() == JS::Bytecode::Register::accumulator_index)
  31. return AK::Formatter<FormatString>::format(builder, "acc");
  32. return AK::Formatter<FormatString>::format(builder, "${}", value.index());
  33. }
  34. };