NativeContext.cs 8.0 KB

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