NativeContext.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 CallAddress;
  17. public ulong ExclusiveAddress;
  18. public ulong ExclusiveValueLow;
  19. public ulong ExclusiveValueHigh;
  20. }
  21. private static NativeCtxStorage _dummyStorage = new NativeCtxStorage();
  22. private readonly IJitMemoryBlock _block;
  23. public IntPtr BasePtr => _block.Pointer;
  24. public NativeContext(IJitMemoryAllocator allocator)
  25. {
  26. _block = allocator.Allocate((ulong)Unsafe.SizeOf<NativeCtxStorage>());
  27. GetStorage().ExclusiveAddress = ulong.MaxValue;
  28. }
  29. public unsafe ulong GetX(int index)
  30. {
  31. if ((uint)index >= RegisterConsts.IntRegsCount)
  32. {
  33. throw new ArgumentOutOfRangeException(nameof(index));
  34. }
  35. return GetStorage().X[index];
  36. }
  37. public unsafe void SetX(int index, ulong value)
  38. {
  39. if ((uint)index >= RegisterConsts.IntRegsCount)
  40. {
  41. throw new ArgumentOutOfRangeException(nameof(index));
  42. }
  43. GetStorage().X[index] = value;
  44. }
  45. public unsafe V128 GetV(int index)
  46. {
  47. if ((uint)index >= RegisterConsts.VecRegsCount)
  48. {
  49. throw new ArgumentOutOfRangeException(nameof(index));
  50. }
  51. return new V128(GetStorage().V[index * 2 + 0], GetStorage().V[index * 2 + 1]);
  52. }
  53. public unsafe void SetV(int index, V128 value)
  54. {
  55. if ((uint)index >= RegisterConsts.VecRegsCount)
  56. {
  57. throw new ArgumentOutOfRangeException(nameof(index));
  58. }
  59. GetStorage().V[index * 2 + 0] = value.Extract<ulong>(0);
  60. GetStorage().V[index * 2 + 1] = value.Extract<ulong>(1);
  61. }
  62. public unsafe bool GetPstateFlag(PState flag)
  63. {
  64. if ((uint)flag >= RegisterConsts.FlagsCount)
  65. {
  66. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  67. }
  68. return GetStorage().Flags[(int)flag] != 0;
  69. }
  70. public unsafe void SetPstateFlag(PState flag, bool value)
  71. {
  72. if ((uint)flag >= RegisterConsts.FlagsCount)
  73. {
  74. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  75. }
  76. GetStorage().Flags[(int)flag] = value ? 1u : 0u;
  77. }
  78. public unsafe bool GetFPStateFlag(FPState flag)
  79. {
  80. if ((uint)flag >= RegisterConsts.FpFlagsCount)
  81. {
  82. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  83. }
  84. return GetStorage().FpFlags[(int)flag] != 0;
  85. }
  86. public unsafe void SetFPStateFlag(FPState flag, bool value)
  87. {
  88. if ((uint)flag >= RegisterConsts.FpFlagsCount)
  89. {
  90. throw new ArgumentException($"Invalid flag \"{flag}\" specified.");
  91. }
  92. GetStorage().FpFlags[(int)flag] = value ? 1u : 0u;
  93. }
  94. public int GetCounter() => GetStorage().Counter;
  95. public void SetCounter(int value) => GetStorage().Counter = value;
  96. public unsafe static int GetRegisterOffset(Register reg)
  97. {
  98. if (reg.Type == RegisterType.Integer)
  99. {
  100. if ((uint)reg.Index >= RegisterConsts.IntRegsCount)
  101. {
  102. throw new ArgumentException("Invalid register.");
  103. }
  104. return StorageOffset(ref _dummyStorage, ref _dummyStorage.X[reg.Index]);
  105. }
  106. else if (reg.Type == RegisterType.Vector)
  107. {
  108. if ((uint)reg.Index >= RegisterConsts.VecRegsCount)
  109. {
  110. throw new ArgumentException("Invalid register.");
  111. }
  112. return StorageOffset(ref _dummyStorage, ref _dummyStorage.V[reg.Index * 2]);
  113. }
  114. else if (reg.Type == RegisterType.Flag)
  115. {
  116. if ((uint)reg.Index >= RegisterConsts.FlagsCount)
  117. {
  118. throw new ArgumentException("Invalid register.");
  119. }
  120. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Flags[reg.Index]);
  121. }
  122. else /* if (reg.Type == RegisterType.FpFlag) */
  123. {
  124. if ((uint)reg.Index >= RegisterConsts.FpFlagsCount)
  125. {
  126. throw new ArgumentException("Invalid register.");
  127. }
  128. return StorageOffset(ref _dummyStorage, ref _dummyStorage.FpFlags[reg.Index]);
  129. }
  130. }
  131. public static int GetCounterOffset()
  132. {
  133. return StorageOffset(ref _dummyStorage, ref _dummyStorage.Counter);
  134. }
  135. public static int GetCallAddressOffset()
  136. {
  137. return StorageOffset(ref _dummyStorage, ref _dummyStorage.CallAddress);
  138. }
  139. public static int GetExclusiveAddressOffset()
  140. {
  141. return StorageOffset(ref _dummyStorage, ref _dummyStorage.ExclusiveAddress);
  142. }
  143. public static int GetExclusiveValueOffset()
  144. {
  145. return StorageOffset(ref _dummyStorage, ref _dummyStorage.ExclusiveValueLow);
  146. }
  147. private static int StorageOffset<T>(ref NativeCtxStorage storage, ref T target)
  148. {
  149. return (int)Unsafe.ByteOffset(ref Unsafe.As<NativeCtxStorage, T>(ref storage), ref target);
  150. }
  151. private unsafe ref NativeCtxStorage GetStorage() => ref Unsafe.AsRef<NativeCtxStorage>((void*)_block.Pointer);
  152. public void Dispose() => _block.Dispose();
  153. }
  154. }