NativeContext.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using ARMeilleure.IntermediateRepresentation;
  2. using ARMeilleure.Memory;
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace ARMeilleure.State
  6. {
  7. class NativeContext : IDisposable
  8. {
  9. private unsafe struct NativeCtxStorage
  10. {
  11. public fixed ulong X[RegisterConsts.IntRegsCount];
  12. public fixed ulong V[RegisterConsts.VecRegsCount * 2];
  13. public fixed uint Flags[RegisterConsts.FlagsCount];
  14. public fixed uint FpFlags[RegisterConsts.FpFlagsCount];
  15. public long TpidrEl0;
  16. public long TpidrroEl0;
  17. public int Counter;
  18. public ulong DispatchAddress;
  19. public ulong ExclusiveAddress;
  20. public ulong ExclusiveValueLow;
  21. public ulong ExclusiveValueHigh;
  22. public int Running;
  23. public long Tpidr2El0;
  24. }
  25. private static NativeCtxStorage _dummyStorage = new();
  26. private readonly IJitMemoryBlock _block;
  27. public nint BasePtr => _block.Pointer;
  28. public NativeContext(IJitMemoryAllocator allocator)
  29. {
  30. _block = allocator.Allocate((ulong)Unsafe.SizeOf<NativeCtxStorage>());
  31. GetStorage().ExclusiveAddress = ulong.MaxValue;
  32. }
  33. public ulong GetPc()
  34. {
  35. // TODO: More precise tracking of PC value.
  36. return GetStorage().DispatchAddress;
  37. }
  38. public unsafe ulong GetX(int index)
  39. {
  40. if ((uint)index >= RegisterConsts.IntRegsCount)
  41. {
  42. throw new ArgumentOutOfRangeException(nameof(index));
  43. }
  44. return GetStorage().X[index];
  45. }
  46. public unsafe void SetX(int index, ulong value)
  47. {
  48. if ((uint)index >= RegisterConsts.IntRegsCount)
  49. {
  50. throw new ArgumentOutOfRangeException(nameof(index));
  51. }
  52. GetStorage().X[index] = value;
  53. }
  54. public unsafe V128 GetV(int index)
  55. {
  56. if ((uint)index >= RegisterConsts.VecRegsCount)
  57. {
  58. throw new ArgumentOutOfRangeException(nameof(index));
  59. }
  60. return new V128(GetStorage().V[index * 2 + 0], GetStorage().V[index * 2 + 1]);
  61. }
  62. public unsafe void SetV(int index, V128 value)
  63. {
  64. if ((uint)index >= RegisterConsts.VecRegsCount)
  65. {
  66. throw new ArgumentOutOfRangeException(nameof(index));
  67. }
  68. GetStorage().V[index * 2 + 0] = value.Extract<ulong>(0);
  69. GetStorage().V[index * 2 + 1] = value.Extract<ulong>(1);
  70. }
  71. public unsafe bool GetPstateFlag(PState flag)
  72. {
  73. if ((uint)flag >= RegisterConsts.FlagsCount)
  74. {
  75. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  76. }
  77. return GetStorage().Flags[(int)flag] != 0;
  78. }
  79. public unsafe void SetPstateFlag(PState flag, bool value)
  80. {
  81. if ((uint)flag >= RegisterConsts.FlagsCount)
  82. {
  83. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  84. }
  85. GetStorage().Flags[(int)flag] = value ? 1u : 0u;
  86. }
  87. public unsafe uint GetPstate()
  88. {
  89. uint value = 0;
  90. for (int flag = 0; flag < RegisterConsts.FlagsCount; flag++)
  91. {
  92. value |= GetStorage().Flags[flag] != 0 ? 1u << flag : 0u;
  93. }
  94. return value;
  95. }
  96. public unsafe void SetPstate(uint value)
  97. {
  98. for (int flag = 0; flag < RegisterConsts.FlagsCount; flag++)
  99. {
  100. uint bit = 1u << flag;
  101. GetStorage().Flags[flag] = (value & bit) == bit ? 1u : 0u;
  102. }
  103. }
  104. public unsafe bool GetFPStateFlag(FPState flag)
  105. {
  106. if ((uint)flag >= RegisterConsts.FpFlagsCount)
  107. {
  108. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  109. }
  110. return GetStorage().FpFlags[(int)flag] != 0;
  111. }
  112. public unsafe void SetFPStateFlag(FPState flag, bool value)
  113. {
  114. if ((uint)flag >= RegisterConsts.FpFlagsCount)
  115. {
  116. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  117. }
  118. GetStorage().FpFlags[(int)flag] = value ? 1u : 0u;
  119. }
  120. public unsafe uint GetFPState(uint mask = uint.MaxValue)
  121. {
  122. uint value = 0;
  123. for (int flag = 0; flag < RegisterConsts.FpFlagsCount; flag++)
  124. {
  125. uint bit = 1u << flag;
  126. if ((mask & bit) == bit)
  127. {
  128. value |= GetStorage().FpFlags[flag] != 0 ? bit : 0u;
  129. }
  130. }
  131. return value;
  132. }
  133. public unsafe void SetFPState(uint value, uint mask = uint.MaxValue)
  134. {
  135. for (int flag = 0; flag < RegisterConsts.FpFlagsCount; flag++)
  136. {
  137. uint bit = 1u << flag;
  138. if ((mask & bit) == bit)
  139. {
  140. GetStorage().FpFlags[flag] = (value & bit) == bit ? 1u : 0u;
  141. }
  142. }
  143. }
  144. public long GetTpidrEl0() => GetStorage().TpidrEl0;
  145. public void SetTpidrEl0(long value) => GetStorage().TpidrEl0 = value;
  146. public long GetTpidrroEl0() => GetStorage().TpidrroEl0;
  147. public void SetTpidrroEl0(long value) => GetStorage().TpidrroEl0 = value;
  148. public long GetTpidr2El0() => GetStorage().Tpidr2El0;
  149. public void SetTpidr2El0(long value) => GetStorage().Tpidr2El0 = value;
  150. public int GetCounter() => GetStorage().Counter;
  151. public void SetCounter(int value) => GetStorage().Counter = value;
  152. public bool GetRunning() => GetStorage().Running != 0;
  153. public void SetRunning(bool value) => GetStorage().Running = value ? 1 : 0;
  154. public unsafe static int GetRegisterOffset(Register reg)
  155. {
  156. if (reg.Type == RegisterType.Integer)
  157. {
  158. if ((uint)reg.Index >= RegisterConsts.IntRegsCount)
  159. {
  160. throw new ArgumentException("Invalid register.");
  161. }
  162. return StorageOffset(ref _dummyStorage, ref _dummyStorage.X[reg.Index]);
  163. }
  164. else if (reg.Type == RegisterType.Vector)
  165. {
  166. if ((uint)reg.Index >= RegisterConsts.VecRegsCount)
  167. {
  168. throw new ArgumentException("Invalid register.");
  169. }
  170. return StorageOffset(ref _dummyStorage, ref _dummyStorage.V[reg.Index * 2]);
  171. }
  172. else if (reg.Type == RegisterType.Flag)
  173. {
  174. if ((uint)reg.Index >= RegisterConsts.FlagsCount)
  175. {
  176. throw new ArgumentException("Invalid register.");
  177. }
  178. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Flags[reg.Index]);
  179. }
  180. else /* if (reg.Type == RegisterType.FpFlag) */
  181. {
  182. if ((uint)reg.Index >= RegisterConsts.FpFlagsCount)
  183. {
  184. throw new ArgumentException("Invalid register.");
  185. }
  186. return StorageOffset(ref _dummyStorage, ref _dummyStorage.FpFlags[reg.Index]);
  187. }
  188. }
  189. public static int GetTpidrEl0Offset()
  190. {
  191. return StorageOffset(ref _dummyStorage, ref _dummyStorage.TpidrEl0);
  192. }
  193. public static int GetTpidrroEl0Offset()
  194. {
  195. return StorageOffset(ref _dummyStorage, ref _dummyStorage.TpidrroEl0);
  196. }
  197. public static int GetTpidr2El0Offset()
  198. {
  199. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Tpidr2El0);
  200. }
  201. public static int GetCounterOffset()
  202. {
  203. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Counter);
  204. }
  205. public static int GetDispatchAddressOffset()
  206. {
  207. return StorageOffset(ref _dummyStorage, ref _dummyStorage.DispatchAddress);
  208. }
  209. public static int GetExclusiveAddressOffset()
  210. {
  211. return StorageOffset(ref _dummyStorage, ref _dummyStorage.ExclusiveAddress);
  212. }
  213. public static int GetExclusiveValueOffset()
  214. {
  215. return StorageOffset(ref _dummyStorage, ref _dummyStorage.ExclusiveValueLow);
  216. }
  217. public static int GetRunningOffset()
  218. {
  219. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Running);
  220. }
  221. private static int StorageOffset<T>(ref NativeCtxStorage storage, ref T target)
  222. {
  223. return (int)Unsafe.ByteOffset(ref Unsafe.As<NativeCtxStorage, T>(ref storage), ref target);
  224. }
  225. private unsafe ref NativeCtxStorage GetStorage() => ref Unsafe.AsRef<NativeCtxStorage>((void*)_block.Pointer);
  226. public void Dispose() => _block.Dispose();
  227. }
  228. }