NativeContext.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 bool GetFPStateFlag(FPState flag)
  80. {
  81. if ((uint)flag >= RegisterConsts.FpFlagsCount)
  82. {
  83. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  84. }
  85. return GetStorage().FpFlags[(int)flag] != 0;
  86. }
  87. public unsafe void SetFPStateFlag(FPState flag, bool value)
  88. {
  89. if ((uint)flag >= RegisterConsts.FpFlagsCount)
  90. {
  91. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  92. }
  93. GetStorage().FpFlags[(int)flag] = value ? 1u : 0u;
  94. }
  95. public int GetCounter() => GetStorage().Counter;
  96. public void SetCounter(int value) => GetStorage().Counter = value;
  97. public bool GetRunning() => GetStorage().Running != 0;
  98. public void SetRunning(bool value) => GetStorage().Running = value ? 1 : 0;
  99. public unsafe static int GetRegisterOffset(Register reg)
  100. {
  101. if (reg.Type == RegisterType.Integer)
  102. {
  103. if ((uint)reg.Index >= RegisterConsts.IntRegsCount)
  104. {
  105. throw new ArgumentException("Invalid register.");
  106. }
  107. return StorageOffset(ref _dummyStorage, ref _dummyStorage.X[reg.Index]);
  108. }
  109. else if (reg.Type == RegisterType.Vector)
  110. {
  111. if ((uint)reg.Index >= RegisterConsts.VecRegsCount)
  112. {
  113. throw new ArgumentException("Invalid register.");
  114. }
  115. return StorageOffset(ref _dummyStorage, ref _dummyStorage.V[reg.Index * 2]);
  116. }
  117. else if (reg.Type == RegisterType.Flag)
  118. {
  119. if ((uint)reg.Index >= RegisterConsts.FlagsCount)
  120. {
  121. throw new ArgumentException("Invalid register.");
  122. }
  123. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Flags[reg.Index]);
  124. }
  125. else /* if (reg.Type == RegisterType.FpFlag) */
  126. {
  127. if ((uint)reg.Index >= RegisterConsts.FpFlagsCount)
  128. {
  129. throw new ArgumentException("Invalid register.");
  130. }
  131. return StorageOffset(ref _dummyStorage, ref _dummyStorage.FpFlags[reg.Index]);
  132. }
  133. }
  134. public static int GetCounterOffset()
  135. {
  136. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Counter);
  137. }
  138. public static int GetDispatchAddressOffset()
  139. {
  140. return StorageOffset(ref _dummyStorage, ref _dummyStorage.DispatchAddress);
  141. }
  142. public static int GetExclusiveAddressOffset()
  143. {
  144. return StorageOffset(ref _dummyStorage, ref _dummyStorage.ExclusiveAddress);
  145. }
  146. public static int GetExclusiveValueOffset()
  147. {
  148. return StorageOffset(ref _dummyStorage, ref _dummyStorage.ExclusiveValueLow);
  149. }
  150. public static int GetRunningOffset()
  151. {
  152. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Running);
  153. }
  154. private static int StorageOffset<T>(ref NativeCtxStorage storage, ref T target)
  155. {
  156. return (int)Unsafe.ByteOffset(ref Unsafe.As<NativeCtxStorage, T>(ref storage), ref target);
  157. }
  158. private unsafe ref NativeCtxStorage GetStorage() => ref Unsafe.AsRef<NativeCtxStorage>((void*)_block.Pointer);
  159. public void Dispose() => _block.Dispose();
  160. }
  161. }