typed_address.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 bool operator!=(const TypedAddress&) const = default;
  93. constexpr auto operator<=>(const TypedAddress&) const = default;
  94. // For convenience, also define comparison operators versus uint64_t.
  95. constexpr inline bool operator==(uint64_t rhs) const {
  96. return m_address == rhs;
  97. }
  98. constexpr inline bool operator!=(uint64_t rhs) const {
  99. return m_address != rhs;
  100. }
  101. // Allow getting the address explicitly, for use in accessors.
  102. constexpr inline uint64_t GetValue() const {
  103. return m_address;
  104. }
  105. private:
  106. uint64_t m_address{};
  107. };
  108. struct PhysicalAddressTag {};
  109. struct VirtualAddressTag {};
  110. struct ProcessAddressTag {};
  111. using PhysicalAddress = TypedAddress<false, PhysicalAddressTag>;
  112. using VirtualAddress = TypedAddress<true, VirtualAddressTag>;
  113. using ProcessAddress = TypedAddress<true, ProcessAddressTag>;
  114. // Define accessors.
  115. template <typename T>
  116. concept IsTypedAddress = std::same_as<T, PhysicalAddress> || std::same_as<T, VirtualAddress> ||
  117. std::same_as<T, ProcessAddress>;
  118. template <typename T>
  119. constexpr inline T Null = [] {
  120. if constexpr (std::is_same<T, uint64_t>::value) {
  121. return 0;
  122. } else {
  123. static_assert(std::is_same<T, PhysicalAddress>::value ||
  124. std::is_same<T, VirtualAddress>::value ||
  125. std::is_same<T, ProcessAddress>::value);
  126. return T(0);
  127. }
  128. }();
  129. // Basic type validations.
  130. static_assert(sizeof(PhysicalAddress) == sizeof(uint64_t));
  131. static_assert(sizeof(VirtualAddress) == sizeof(uint64_t));
  132. static_assert(sizeof(ProcessAddress) == sizeof(uint64_t));
  133. static_assert(std::is_trivially_copyable_v<PhysicalAddress>);
  134. static_assert(std::is_trivially_copyable_v<VirtualAddress>);
  135. static_assert(std::is_trivially_copyable_v<ProcessAddress>);
  136. static_assert(std::is_trivially_copy_constructible_v<PhysicalAddress>);
  137. static_assert(std::is_trivially_copy_constructible_v<VirtualAddress>);
  138. static_assert(std::is_trivially_copy_constructible_v<ProcessAddress>);
  139. static_assert(std::is_trivially_move_constructible_v<PhysicalAddress>);
  140. static_assert(std::is_trivially_move_constructible_v<VirtualAddress>);
  141. static_assert(std::is_trivially_move_constructible_v<ProcessAddress>);
  142. static_assert(std::is_trivially_copy_assignable_v<PhysicalAddress>);
  143. static_assert(std::is_trivially_copy_assignable_v<VirtualAddress>);
  144. static_assert(std::is_trivially_copy_assignable_v<ProcessAddress>);
  145. static_assert(std::is_trivially_move_assignable_v<PhysicalAddress>);
  146. static_assert(std::is_trivially_move_assignable_v<VirtualAddress>);
  147. static_assert(std::is_trivially_move_assignable_v<ProcessAddress>);
  148. static_assert(std::is_trivially_destructible_v<PhysicalAddress>);
  149. static_assert(std::is_trivially_destructible_v<VirtualAddress>);
  150. static_assert(std::is_trivially_destructible_v<ProcessAddress>);
  151. static_assert(Null<uint64_t> == 0);
  152. static_assert(Null<PhysicalAddress> == Null<uint64_t>);
  153. static_assert(Null<VirtualAddress> == Null<uint64_t>);
  154. static_assert(Null<ProcessAddress> == Null<uint64_t>);
  155. // Constructor/assignment validations.
  156. static_assert([] {
  157. const PhysicalAddress a(5);
  158. PhysicalAddress b(a);
  159. return b;
  160. }() == PhysicalAddress(5));
  161. static_assert([] {
  162. const PhysicalAddress a(5);
  163. PhysicalAddress b(10);
  164. b = a;
  165. return b;
  166. }() == PhysicalAddress(5));
  167. // Arithmetic validations.
  168. static_assert(PhysicalAddress(10) + 5 == PhysicalAddress(15));
  169. static_assert(PhysicalAddress(10) - 5 == PhysicalAddress(5));
  170. static_assert([] {
  171. PhysicalAddress v(10);
  172. v += 5;
  173. return v;
  174. }() == PhysicalAddress(15));
  175. static_assert([] {
  176. PhysicalAddress v(10);
  177. v -= 5;
  178. return v;
  179. }() == PhysicalAddress(5));
  180. static_assert(PhysicalAddress(10)++ == PhysicalAddress(10));
  181. static_assert(++PhysicalAddress(10) == PhysicalAddress(11));
  182. static_assert(PhysicalAddress(10)-- == PhysicalAddress(10));
  183. static_assert(--PhysicalAddress(10) == PhysicalAddress(9));
  184. // Logical validations.
  185. static_assert((PhysicalAddress(0b11111111) >> 1) == 0b01111111);
  186. static_assert((PhysicalAddress(0b10101010) >> 1) == 0b01010101);
  187. static_assert((PhysicalAddress(0b11111111) << 1) == 0b111111110);
  188. static_assert((PhysicalAddress(0b01010101) << 1) == 0b10101010);
  189. static_assert((PhysicalAddress(0b11111111) & 0b01010101) == 0b01010101);
  190. static_assert((PhysicalAddress(0b11111111) & 0b10101010) == 0b10101010);
  191. static_assert((PhysicalAddress(0b01010101) & 0b10101010) == 0b00000000);
  192. static_assert((PhysicalAddress(0b00000000) | 0b01010101) == 0b01010101);
  193. static_assert((PhysicalAddress(0b11111111) | 0b01010101) == 0b11111111);
  194. static_assert((PhysicalAddress(0b10101010) | 0b01010101) == 0b11111111);
  195. // Comparisons.
  196. static_assert(PhysicalAddress(0) == PhysicalAddress(0));
  197. static_assert(PhysicalAddress(0) != PhysicalAddress(1));
  198. static_assert(PhysicalAddress(0) < PhysicalAddress(1));
  199. static_assert(PhysicalAddress(0) <= PhysicalAddress(1));
  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(0)));
  204. static_assert(!(PhysicalAddress(1) < PhysicalAddress(0)));
  205. static_assert(!(PhysicalAddress(1) <= PhysicalAddress(0)));
  206. static_assert(!(PhysicalAddress(0) > PhysicalAddress(1)));
  207. static_assert(!(PhysicalAddress(0) >= PhysicalAddress(1)));
  208. } // namespace Common
  209. template <bool Virtual, typename T>
  210. constexpr inline uint64_t GetInteger(Common::TypedAddress<Virtual, T> address) {
  211. return address.GetValue();
  212. }
  213. template <>
  214. struct fmt::formatter<Common::PhysicalAddress> {
  215. constexpr auto parse(fmt::format_parse_context& ctx) {
  216. return ctx.begin();
  217. }
  218. template <typename FormatContext>
  219. auto format(const Common::PhysicalAddress& addr, FormatContext& ctx) {
  220. return fmt::format_to(ctx.out(), "{:#x}", static_cast<u64>(addr.GetValue()));
  221. }
  222. };
  223. template <>
  224. struct fmt::formatter<Common::ProcessAddress> {
  225. constexpr auto parse(fmt::format_parse_context& ctx) {
  226. return ctx.begin();
  227. }
  228. template <typename FormatContext>
  229. auto format(const Common::ProcessAddress& addr, FormatContext& ctx) {
  230. return fmt::format_to(ctx.out(), "{:#x}", static_cast<u64>(addr.GetValue()));
  231. }
  232. };
  233. template <>
  234. struct fmt::formatter<Common::VirtualAddress> {
  235. constexpr auto parse(fmt::format_parse_context& ctx) {
  236. return ctx.begin();
  237. }
  238. template <typename FormatContext>
  239. auto format(const Common::VirtualAddress& addr, FormatContext& ctx) {
  240. return fmt::format_to(ctx.out(), "{:#x}", static_cast<u64>(addr.GetValue()));
  241. }
  242. };
  243. namespace std {
  244. template <>
  245. struct hash<Common::PhysicalAddress> {
  246. size_t operator()(const Common::PhysicalAddress& k) const noexcept {
  247. return k.GetValue();
  248. }
  249. };
  250. template <>
  251. struct hash<Common::ProcessAddress> {
  252. size_t operator()(const Common::ProcessAddress& k) const noexcept {
  253. return k.GetValue();
  254. }
  255. };
  256. template <>
  257. struct hash<Common::VirtualAddress> {
  258. size_t operator()(const Common::VirtualAddress& k) const noexcept {
  259. return k.GetValue();
  260. }
  261. };
  262. } // namespace std