NativeContext.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 unsafe ulong GetX(int index)
  31. {
  32. if ((uint)index >= RegisterConsts.IntRegsCount)
  33. {
  34. throw new ArgumentOutOfRangeException(nameof(index));
  35. }
  36. return GetStorage().X[index];
  37. }
  38. public unsafe void SetX(int index, ulong value)
  39. {
  40. if ((uint)index >= RegisterConsts.IntRegsCount)
  41. {
  42. throw new ArgumentOutOfRangeException(nameof(index));
  43. }
  44. GetStorage().X[index] = value;
  45. }
  46. public unsafe V128 GetV(int index)
  47. {
  48. if ((uint)index >= RegisterConsts.VecRegsCount)
  49. {
  50. throw new ArgumentOutOfRangeException(nameof(index));
  51. }
  52. return new V128(GetStorage().V[index * 2 + 0], GetStorage().V[index * 2 + 1]);
  53. }
  54. public unsafe void SetV(int index, V128 value)
  55. {
  56. if ((uint)index >= RegisterConsts.VecRegsCount)
  57. {
  58. throw new ArgumentOutOfRangeException(nameof(index));
  59. }
  60. GetStorage().V[index * 2 + 0] = value.Extract<ulong>(0);
  61. GetStorage().V[index * 2 + 1] = value.Extract<ulong>(1);
  62. }
  63. public unsafe bool GetPstateFlag(PState flag)
  64. {
  65. if ((uint)flag >= RegisterConsts.FlagsCount)
  66. {
  67. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  68. }
  69. return GetStorage().Flags[(int)flag] != 0;
  70. }
  71. public unsafe void SetPstateFlag(PState flag, bool value)
  72. {
  73. if ((uint)flag >= RegisterConsts.FlagsCount)
  74. {
  75. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  76. }
  77. GetStorage().Flags[(int)flag] = value ? 1u : 0u;
  78. }
  79. public unsafe uint GetPstate()
  80. {
  81. uint value = 0;
  82. for (int flag = 0; flag < RegisterConsts.FlagsCount; flag++)
  83. {
  84. value |= GetStorage().Flags[flag] != 0 ? 1u << flag : 0u;
  85. }
  86. return value;
  87. }
  88. public unsafe void SetPstate(uint value)
  89. {
  90. for (int flag = 0; flag < RegisterConsts.FlagsCount; flag++)
  91. {
  92. uint bit = 1u << flag;
  93. GetStorage().Flags[flag] = (value & bit) == bit ? 1u : 0u;
  94. }
  95. }
  96. public unsafe bool GetFPStateFlag(FPState flag)
  97. {
  98. if ((uint)flag >= RegisterConsts.FpFlagsCount)
  99. {
  100. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  101. }
  102. return GetStorage().FpFlags[(int)flag] != 0;
  103. }
  104. public unsafe void SetFPStateFlag(FPState flag, bool value)
  105. {
  106. if ((uint)flag >= RegisterConsts.FpFlagsCount)
  107. {
  108. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  109. }
  110. GetStorage().FpFlags[(int)flag] = value ? 1u : 0u;
  111. }
  112. public int GetCounter() => GetStorage().Counter;
  113. public void SetCounter(int value) => GetStorage().Counter = value;
  114. public bool GetRunning() => GetStorage().Running != 0;
  115. public void SetRunning(bool value) => GetStorage().Running = value ? 1 : 0;
  116. public unsafe static int GetRegisterOffset(Register reg)
  117. {
  118. if (reg.Type == RegisterType.Integer)
  119. {
  120. if ((uint)reg.Index >= RegisterConsts.IntRegsCount)
  121. {
  122. throw new ArgumentException("Invalid register.");
  123. }
  124. return StorageOffset(ref _dummyStorage, ref _dummyStorage.X[reg.Index]);
  125. }
  126. else if (reg.Type == RegisterType.Vector)
  127. {
  128. if ((uint)reg.Index >= RegisterConsts.VecRegsCount)
  129. {
  130. throw new ArgumentException("Invalid register.");
  131. }
  132. return StorageOffset(ref _dummyStorage, ref _dummyStorage.V[reg.Index * 2]);
  133. }
  134. else if (reg.Type == RegisterType.Flag)
  135. {
  136. if ((uint)reg.Index >= RegisterConsts.FlagsCount)
  137. {
  138. throw new ArgumentException("Invalid register.");
  139. }
  140. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Flags[reg.Index]);
  141. }
  142. else /* if (reg.Type == RegisterType.FpFlag) */
  143. {
  144. if ((uint)reg.Index >= RegisterConsts.FpFlagsCount)
  145. {
  146. throw new ArgumentException("Invalid register.");
  147. }
  148. return StorageOffset(ref _dummyStorage, ref _dummyStorage.FpFlags[reg.Index]);
  149. }
  150. }
  151. public static int GetCounterOffset()
  152. {
  153. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Counter);
  154. }
  155. public static int GetDispatchAddressOffset()
  156. {
  157. return StorageOffset(ref _dummyStorage, ref _dummyStorage.DispatchAddress);
  158. }
  159. public static int GetExclusiveAddressOffset()
  160. {
  161. return StorageOffset(ref _dummyStorage, ref _dummyStorage.ExclusiveAddress);
  162. }
  163. public static int GetExclusiveValueOffset()
  164. {
  165. return StorageOffset(ref _dummyStorage, ref _dummyStorage.ExclusiveValueLow);
  166. }
  167. public static int GetRunningOffset()
  168. {
  169. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Running);
  170. }
  171. private static int StorageOffset<T>(ref NativeCtxStorage storage, ref T target)
  172. {
  173. return (int)Unsafe.ByteOffset(ref Unsafe.As<NativeCtxStorage, T>(ref storage), ref target);
  174. }
  175. private unsafe ref NativeCtxStorage GetStorage() => ref Unsafe.AsRef<NativeCtxStorage>((void*)_block.Pointer);
  176. public void Dispose() => _block.Dispose();
  177. }
  178. }