AInstEmitMemoryHelper.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection.Emit;
  6. namespace ChocolArm64.Instruction
  7. {
  8. static class AInstEmitMemoryHelper
  9. {
  10. private enum Extension
  11. {
  12. Zx,
  13. Sx32,
  14. Sx64
  15. }
  16. public static void EmitReadZxCall(AILEmitterCtx Context, int Size)
  17. {
  18. EmitReadCall(Context, Extension.Zx, Size);
  19. }
  20. public static void EmitReadSx32Call(AILEmitterCtx Context, int Size)
  21. {
  22. EmitReadCall(Context, Extension.Sx32, Size);
  23. }
  24. public static void EmitReadSx64Call(AILEmitterCtx Context, int Size)
  25. {
  26. EmitReadCall(Context, Extension.Sx64, Size);
  27. }
  28. private static void EmitReadCall(AILEmitterCtx Context, Extension Ext, int Size)
  29. {
  30. bool IsSimd = GetIsSimd(Context);
  31. string Name = null;
  32. if (Size < 0 || Size > (IsSimd ? 4 : 3))
  33. {
  34. throw new ArgumentOutOfRangeException(nameof(Size));
  35. }
  36. if (IsSimd)
  37. {
  38. switch (Size)
  39. {
  40. case 0: Name = AOptimizations.DisableMemoryChecks
  41. ? nameof(AMemory.ReadVector8Unchecked)
  42. : nameof(AMemory.ReadVector8); break;
  43. case 1: Name = AOptimizations.DisableMemoryChecks
  44. ? nameof(AMemory.ReadVector16Unchecked)
  45. : nameof(AMemory.ReadVector16); break;
  46. case 2: Name = AOptimizations.DisableMemoryChecks
  47. ? nameof(AMemory.ReadVector32Unchecked)
  48. : nameof(AMemory.ReadVector32); break;
  49. case 3: Name = AOptimizations.DisableMemoryChecks
  50. ? nameof(AMemory.ReadVector64Unchecked)
  51. : nameof(AMemory.ReadVector64); break;
  52. case 4: Name = AOptimizations.DisableMemoryChecks
  53. ? nameof(AMemory.ReadVector128Unchecked)
  54. : nameof(AMemory.ReadVector128); break;
  55. }
  56. }
  57. else
  58. {
  59. switch (Size)
  60. {
  61. case 0: Name = AOptimizations.DisableMemoryChecks
  62. ? nameof(AMemory.ReadByteUnchecked)
  63. : nameof(AMemory.ReadByte); break;
  64. case 1: Name = AOptimizations.DisableMemoryChecks
  65. ? nameof(AMemory.ReadUInt16Unchecked)
  66. : nameof(AMemory.ReadUInt16); break;
  67. case 2: Name = AOptimizations.DisableMemoryChecks
  68. ? nameof(AMemory.ReadUInt32Unchecked)
  69. : nameof(AMemory.ReadUInt32); break;
  70. case 3: Name = AOptimizations.DisableMemoryChecks
  71. ? nameof(AMemory.ReadUInt64Unchecked)
  72. : nameof(AMemory.ReadUInt64); break;
  73. }
  74. }
  75. Context.EmitCall(typeof(AMemory), Name);
  76. if (!IsSimd)
  77. {
  78. if (Ext == Extension.Sx32 ||
  79. Ext == Extension.Sx64)
  80. {
  81. switch (Size)
  82. {
  83. case 0: Context.Emit(OpCodes.Conv_I1); break;
  84. case 1: Context.Emit(OpCodes.Conv_I2); break;
  85. case 2: Context.Emit(OpCodes.Conv_I4); break;
  86. }
  87. }
  88. if (Size < 3)
  89. {
  90. Context.Emit(Ext == Extension.Sx64
  91. ? OpCodes.Conv_I8
  92. : OpCodes.Conv_U8);
  93. }
  94. }
  95. }
  96. public static void EmitWriteCall(AILEmitterCtx Context, int Size)
  97. {
  98. bool IsSimd = GetIsSimd(Context);
  99. string Name = null;
  100. if (Size < 0 || Size > (IsSimd ? 4 : 3))
  101. {
  102. throw new ArgumentOutOfRangeException(nameof(Size));
  103. }
  104. if (Size < 3 && !IsSimd)
  105. {
  106. Context.Emit(OpCodes.Conv_I4);
  107. }
  108. if (IsSimd)
  109. {
  110. switch (Size)
  111. {
  112. case 0: Name = AOptimizations.DisableMemoryChecks
  113. ? nameof(AMemory.WriteVector8Unchecked)
  114. : nameof(AMemory.WriteVector8); break;
  115. case 1: Name = AOptimizations.DisableMemoryChecks
  116. ? nameof(AMemory.WriteVector16Unchecked)
  117. : nameof(AMemory.WriteVector16); break;
  118. case 2: Name = AOptimizations.DisableMemoryChecks
  119. ? nameof(AMemory.WriteVector32Unchecked)
  120. : nameof(AMemory.WriteVector32); break;
  121. case 3: Name = AOptimizations.DisableMemoryChecks
  122. ? nameof(AMemory.WriteVector64Unchecked)
  123. : nameof(AMemory.WriteVector64); break;
  124. case 4: Name = AOptimizations.DisableMemoryChecks
  125. ? nameof(AMemory.WriteVector128Unchecked)
  126. : nameof(AMemory.WriteVector128); break;
  127. }
  128. }
  129. else
  130. {
  131. switch (Size)
  132. {
  133. case 0: Name = AOptimizations.DisableMemoryChecks
  134. ? nameof(AMemory.WriteByteUnchecked)
  135. : nameof(AMemory.WriteByte); break;
  136. case 1: Name = AOptimizations.DisableMemoryChecks
  137. ? nameof(AMemory.WriteUInt16Unchecked)
  138. : nameof(AMemory.WriteUInt16); break;
  139. case 2: Name = AOptimizations.DisableMemoryChecks
  140. ? nameof(AMemory.WriteUInt32Unchecked)
  141. : nameof(AMemory.WriteUInt32); break;
  142. case 3: Name = AOptimizations.DisableMemoryChecks
  143. ? nameof(AMemory.WriteUInt64Unchecked)
  144. : nameof(AMemory.WriteUInt64); break;
  145. }
  146. }
  147. Context.EmitCall(typeof(AMemory), Name);
  148. }
  149. private static bool GetIsSimd(AILEmitterCtx Context)
  150. {
  151. return Context.CurrOp is IAOpCodeSimd &&
  152. !(Context.CurrOp is AOpCodeSimdMemMs ||
  153. Context.CurrOp is AOpCodeSimdMemSs);
  154. }
  155. }
  156. }