NativeContext.cs 8.6 KB

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