NativeContext.cs 5.6 KB

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