typed_address.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <compare>
  5. #include <type_traits>
  6. #include <fmt/format.h>
  7. #include "common/common_types.h"
  8. namespace Common {
  9. template <bool Virtual, typename T>
  10. class TypedAddress {
  11. public:
  12. // Constructors.
  13. constexpr inline TypedAddress() : m_address(0) {}
  14. constexpr inline TypedAddress(uint64_t a) : m_address(a) {}
  15. template <typename U>
  16. constexpr inline explicit TypedAddress(const U* ptr)
  17. : m_address(reinterpret_cast<uint64_t>(ptr)) {}
  18. // Copy constructor.
  19. constexpr inline TypedAddress(const TypedAddress& rhs) = default;
  20. // Assignment operator.
  21. constexpr inline TypedAddress& operator=(const TypedAddress& rhs) = default;
  22. // Arithmetic operators.
  23. template <typename I>
  24. constexpr inline TypedAddress operator+(I rhs) const {
  25. static_assert(std::is_integral_v<I>);
  26. return m_address + rhs;
  27. }
  28. constexpr inline TypedAddress operator+(TypedAddress rhs) const {
  29. return m_address + rhs.m_address;
  30. }
  31. constexpr inline TypedAddress operator++() {
  32. return ++m_address;
  33. }
  34. constexpr inline TypedAddress operator++(int) {
  35. return m_address++;
  36. }
  37. template <typename I>
  38. constexpr inline TypedAddress operator-(I rhs) const {
  39. static_assert(std::is_integral_v<I>);
  40. return m_address - rhs;
  41. }
  42. constexpr inline ptrdiff_t operator-(TypedAddress rhs) const {
  43. return m_address - rhs.m_address;
  44. }
  45. constexpr inline TypedAddress operator--() {
  46. return --m_address;
  47. }
  48. constexpr inline TypedAddress operator--(int) {
  49. return m_address--;
  50. }
  51. template <typename I>
  52. constexpr inline TypedAddress operator+=(I rhs) {
  53. static_assert(std::is_integral_v<I>);
  54. m_address += rhs;
  55. return *this;
  56. }
  57. template <typename I>
  58. constexpr inline TypedAddress operator-=(I rhs) {
  59. static_assert(std::is_integral_v<I>);
  60. m_address -= rhs;
  61. return *this;
  62. }
  63. // Logical operators.
  64. constexpr inline uint64_t operator&(uint64_t mask) const {
  65. return m_address & mask;
  66. }
  67. constexpr inline uint64_t operator|(uint64_t mask) const {
  68. return m_address | mask;
  69. }
  70. template <typename I>
  71. constexpr inline TypedAddress operator|=(I rhs) {
  72. static_assert(std::is_integral_v<I>);
  73. m_address |= rhs;
  74. return *this;
  75. }
  76. constexpr inline uint64_t operator<<(int shift) const {
  77. return m_address << shift;
  78. }
  79. constexpr inline uint64_t operator>>(int shift) const {
  80. return m_address >> shift;
  81. }
  82. template <typename U>
  83. constexpr inline size_t operator/(U size) const {
  84. return m_address / size;
  85. }
  86. constexpr explicit operator bool() const {
  87. return m_address != 0;
  88. }
  89. // constexpr inline uint64_t operator%(U align) const { return m_address % align; }
  90. // Comparison operators.
  91. constexpr bool operator==(const TypedAddress&) const = default;
  92. constexpr auto operator<=>(const TypedAddress&) const = default;
  93. // For convenience, also define comparison operators versus uint64_t.
  94. constexpr inline bool operator==(uint64_t rhs) const {
  95. return m_address == rhs;
  96. }
  97. // Allow getting the address explicitly, for use in accessors.
  98. constexpr inline uint64_t GetValue() const {
  99. return m_address;
  100. }
  101. private:
  102. uint64_t m_address{};
  103. };
  104. struct PhysicalAddressTag {};
  105. struct VirtualAddressTag {};
  106. struct ProcessAddressTag {};
  107. using PhysicalAddress = TypedAddress<false, PhysicalAddressTag>;
  108. using VirtualAddress = TypedAddress<true, VirtualAddressTag>;
  109. using ProcessAddress = TypedAddress<true, ProcessAddressTag>;
  110. // Define accessors.
  111. template <typename T>
  112. concept IsTypedAddress = std::same_as<T, PhysicalAddress> || std::same_as<T, VirtualAddress> ||
  113. std::same_as<T, ProcessAddress>;
  114. template <typename T>
  115. constexpr inline T Null = [] {
  116. if constexpr (std::is_same<T, uint64_t>::value) {
  117. return 0;
  118. } else {
  119. static_assert(std::is_same<T, PhysicalAddress>::value ||
  120. std::is_same<T, VirtualAddress>::value ||
  121. std::is_same<T, ProcessAddress>::value);
  122. return T(0);
  123. }
  124. }();
  125. // Basic type validations.
  126. static_assert(sizeof(PhysicalAddress) == sizeof(uint64_t));
  127. static_assert(sizeof(VirtualAddress) == sizeof(uint64_t));
  128. static_assert(sizeof(ProcessAddress) == sizeof(uint64_t));
  129. static_assert(std::is_trivially_copyable_v<PhysicalAddress>);
  130. static_assert(std::is_trivially_copyable_v<VirtualAddress>);
  131. static_assert(std::is_trivially_copyable_v<ProcessAddress>);
  132. static_assert(std::is_trivially_copy_constructible_v<PhysicalAddress>);
  133. static_assert(std::is_trivially_copy_constructible_v<VirtualAddress>);
  134. static_assert(std::is_trivially_copy_constructible_v<ProcessAddress>);
  135. static_assert(std::is_trivially_move_constructible_v<PhysicalAddress>);
  136. static_assert(std::is_trivially_move_constructible_v<VirtualAddress>);
  137. static_assert(std::is_trivially_move_constructible_v<ProcessAddress>);
  138. static_assert(std::is_trivially_copy_assignable_v<PhysicalAddress>);
  139. static_assert(std::is_trivially_copy_assignable_v<VirtualAddress>);
  140. static_assert(std::is_trivially_copy_assignable_v<ProcessAddress>);
  141. static_assert(std::is_trivially_move_assignable_v<PhysicalAddress>);
  142. static_assert(std::is_trivially_move_assignable_v<VirtualAddress>);
  143. static_assert(std::is_trivially_move_assignable_v<ProcessAddress>);
  144. static_assert(std::is_trivially_destructible_v<PhysicalAddress>);
  145. static_assert(std::is_trivially_destructible_v<VirtualAddress>);
  146. static_assert(std::is_trivially_destructible_v<ProcessAddress>);
  147. static_assert(Null<uint64_t> == 0);
  148. static_assert(Null<PhysicalAddress> == Null<uint64_t>);
  149. static_assert(Null<VirtualAddress> == Null<uint64_t>);
  150. static_assert(Null<ProcessAddress> == Null<uint64_t>);
  151. // Constructor/assignment validations.
  152. static_assert([] {
  153. const PhysicalAddress a(5);
  154. PhysicalAddress b(a);
  155. return b;
  156. }() == PhysicalAddress(5));
  157. static_assert([] {
  158. const PhysicalAddress a(5);
  159. PhysicalAddress b(10);
  160. b = a;
  161. return b;
  162. }() == PhysicalAddress(5));
  163. // Arithmetic validations.
  164. static_assert(PhysicalAddress(10) + 5 == PhysicalAddress(15));
  165. static_assert(PhysicalAddress(10) - 5 == PhysicalAddress(5));
  166. static_assert([] {
  167. PhysicalAddress v(10);
  168. v += 5;
  169. return v;
  170. }() == PhysicalAddress(15));
  171. static_assert([] {
  172. PhysicalAddress v(10);
  173. v -= 5;
  174. return v;
  175. }() == PhysicalAddress(5));
  176. static_assert(PhysicalAddress(10)++ == PhysicalAddress(10));
  177. static_assert(++PhysicalAddress(10) == PhysicalAddress(11));
  178. static_assert(PhysicalAddress(10)-- == PhysicalAddress(10));
  179. static_assert(--PhysicalAddress(10) == PhysicalAddress(9));
  180. // Logical validations.
  181. static_assert((PhysicalAddress(0b11111111) >> 1) == 0b01111111);
  182. static_assert((PhysicalAddress(0b10101010) >> 1) == 0b01010101);
  183. static_assert((PhysicalAddress(0b11111111) << 1) == 0b111111110);
  184. static_assert((PhysicalAddress(0b01010101) << 1) == 0b10101010);
  185. static_assert((PhysicalAddress(0b11111111) & 0b01010101) == 0b01010101);
  186. static_assert((PhysicalAddress(0b11111111) & 0b10101010) == 0b10101010);
  187. static_assert((PhysicalAddress(0b01010101) & 0b10101010) == 0b00000000);
  188. static_assert((PhysicalAddress(0b00000000) | 0b01010101) == 0b01010101);
  189. static_assert((PhysicalAddress(0b11111111) | 0b01010101) == 0b11111111);
  190. static_assert((PhysicalAddress(0b10101010) | 0b01010101) == 0b11111111);
  191. // Comparisons.
  192. static_assert(PhysicalAddress(0) == PhysicalAddress(0));
  193. static_assert(PhysicalAddress(0) != PhysicalAddress(1));
  194. static_assert(PhysicalAddress(0) < PhysicalAddress(1));
  195. static_assert(PhysicalAddress(0) <= PhysicalAddress(1));
  196. static_assert(PhysicalAddress(1) > PhysicalAddress(0));
  197. static_assert(PhysicalAddress(1) >= PhysicalAddress(0));
  198. static_assert(!(PhysicalAddress(0) == PhysicalAddress(1)));
  199. static_assert(!(PhysicalAddress(0) != PhysicalAddress(0)));
  200. static_assert(!(PhysicalAddress(1) < PhysicalAddress(0)));
  201. static_assert(!(PhysicalAddress(1) <= PhysicalAddress(0)));
  202. static_assert(!(PhysicalAddress(0) > PhysicalAddress(1)));
  203. static_assert(!(PhysicalAddress(0) >= PhysicalAddress(1)));
  204. } // namespace Common
  205. template <bool Virtual, typename T>
  206. constexpr inline uint64_t GetInteger(Common::TypedAddress<Virtual, T> address) {
  207. return address.GetValue();
  208. }
  209. template <>
  210. struct fmt::formatter<Common::PhysicalAddress> {
  211. constexpr auto parse(fmt::format_parse_context& ctx) {
  212. return ctx.begin();
  213. }
  214. template <typename FormatContext>
  215. auto format(const Common::PhysicalAddress& addr, FormatContext& ctx) {
  216. return fmt::format_to(ctx.out(), "{:#x}", static_cast<u64>(addr.GetValue()));
  217. }
  218. };
  219. template <>
  220. struct fmt::formatter<Common::ProcessAddress> {
  221. constexpr auto parse(fmt::format_parse_context& ctx) {
  222. return ctx.begin();
  223. }
  224. template <typename FormatContext>
  225. auto format(const Common::ProcessAddress& addr, FormatContext& ctx) {
  226. return fmt::format_to(ctx.out(), "{:#x}", static_cast<u64>(addr.GetValue()));
  227. }
  228. };
  229. template <>
  230. struct fmt::formatter<Common::VirtualAddress> {
  231. constexpr auto parse(fmt::format_parse_context& ctx) {
  232. return ctx.begin();
  233. }
  234. template <typename FormatContext>
  235. auto format(const Common::VirtualAddress& addr, FormatContext& ctx) {
  236. return fmt::format_to(ctx.out(), "{:#x}", static_cast<u64>(addr.GetValue()));
  237. }
  238. };
  239. namespace std {
  240. template <>
  241. struct hash<Common::PhysicalAddress> {
  242. size_t operator()(const Common::PhysicalAddress& k) const noexcept {
  243. return k.GetValue();
  244. }
  245. };
  246. template <>
  247. struct hash<Common::ProcessAddress> {
  248. size_t operator()(const Common::ProcessAddress& k) const noexcept {
  249. return k.GetValue();
  250. }
  251. };
  252. template <>
  253. struct hash<Common::VirtualAddress> {
  254. size_t operator()(const Common::VirtualAddress& k) const noexcept {
  255. return k.GetValue();
  256. }
  257. };
  258. } // namespace std