Register.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 static u32 saved_return_value_index = 1;
  17. static constexpr Register saved_return_value()
  18. {
  19. return Register(saved_return_value_index);
  20. }
  21. static constexpr u32 exception_index = 2;
  22. static constexpr Register exception()
  23. {
  24. return Register(exception_index);
  25. }
  26. static constexpr Register this_value()
  27. {
  28. constexpr u32 this_value_index = 3;
  29. return Register(this_value_index);
  30. }
  31. static constexpr Register return_value()
  32. {
  33. constexpr u32 return_value_index = 4;
  34. return Register(return_value_index);
  35. }
  36. static constexpr u32 reserved_register_count = 5;
  37. constexpr explicit Register(u32 index)
  38. : m_index(index)
  39. {
  40. }
  41. constexpr bool operator==(Register reg) const { return m_index == reg.index(); }
  42. constexpr u32 index() const { return m_index; }
  43. private:
  44. u32 m_index;
  45. };
  46. }
  47. template<>
  48. struct AK::Formatter<JS::Bytecode::Register> : AK::Formatter<FormatString> {
  49. ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Register const& value)
  50. {
  51. if (value.index() == JS::Bytecode::Register::accumulator_index)
  52. return builder.put_string("acc"sv);
  53. return AK::Formatter<FormatString>::format(builder, "${}"sv, value.index());
  54. }
  55. };