InstEmitSimdMemory.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection.Emit;
  6. using static ChocolArm64.Instructions.InstEmitMemoryHelper;
  7. using static ChocolArm64.Instructions.InstEmitSimdHelper;
  8. namespace ChocolArm64.Instructions
  9. {
  10. static partial class InstEmit
  11. {
  12. public static void Ld__Vms(ILEmitterCtx context)
  13. {
  14. EmitSimdMemMs(context, isLoad: true);
  15. }
  16. public static void Ld__Vss(ILEmitterCtx context)
  17. {
  18. EmitSimdMemSs(context, isLoad: true);
  19. }
  20. public static void St__Vms(ILEmitterCtx context)
  21. {
  22. EmitSimdMemMs(context, isLoad: false);
  23. }
  24. public static void St__Vss(ILEmitterCtx context)
  25. {
  26. EmitSimdMemSs(context, isLoad: false);
  27. }
  28. private static void EmitSimdMemMs(ILEmitterCtx context, bool isLoad)
  29. {
  30. OpCodeSimdMemMs64 op = (OpCodeSimdMemMs64)context.CurrOp;
  31. int offset = 0;
  32. for (int rep = 0; rep < op.Reps; rep++)
  33. for (int elem = 0; elem < op.Elems; elem++)
  34. for (int sElem = 0; sElem < op.SElems; sElem++)
  35. {
  36. int rtt = (op.Rt + rep + sElem) & 0x1f;
  37. if (isLoad)
  38. {
  39. context.EmitLdint(op.Rn);
  40. context.EmitLdc_I8(offset);
  41. context.Emit(OpCodes.Add);
  42. EmitReadZxCall(context, op.Size);
  43. EmitVectorInsert(context, rtt, elem, op.Size);
  44. if (op.RegisterSize == RegisterSize.Simd64 && elem == op.Elems - 1)
  45. {
  46. EmitVectorZeroUpper(context, rtt);
  47. }
  48. }
  49. else
  50. {
  51. context.EmitLdint(op.Rn);
  52. context.EmitLdc_I8(offset);
  53. context.Emit(OpCodes.Add);
  54. EmitVectorExtractZx(context, rtt, elem, op.Size);
  55. EmitWriteCall(context, op.Size);
  56. }
  57. offset += 1 << op.Size;
  58. }
  59. if (op.WBack)
  60. {
  61. EmitSimdMemWBack(context, offset);
  62. }
  63. }
  64. private static void EmitSimdMemSs(ILEmitterCtx context, bool isLoad)
  65. {
  66. OpCodeSimdMemSs64 op = (OpCodeSimdMemSs64)context.CurrOp;
  67. int offset = 0;
  68. void EmitMemAddress()
  69. {
  70. context.EmitLdint(op.Rn);
  71. context.EmitLdc_I8(offset);
  72. context.Emit(OpCodes.Add);
  73. }
  74. if (op.Replicate)
  75. {
  76. // Only loads uses the replicate mode.
  77. if (!isLoad)
  78. {
  79. throw new InvalidOperationException();
  80. }
  81. int bytes = op.GetBitsCount() >> 3;
  82. int elems = bytes >> op.Size;
  83. for (int sElem = 0; sElem < op.SElems; sElem++)
  84. {
  85. int rt = (op.Rt + sElem) & 0x1f;
  86. for (int index = 0; index < elems; index++)
  87. {
  88. EmitMemAddress();
  89. EmitReadZxCall(context, op.Size);
  90. EmitVectorInsert(context, rt, index, op.Size);
  91. }
  92. if (op.RegisterSize == RegisterSize.Simd64)
  93. {
  94. EmitVectorZeroUpper(context, rt);
  95. }
  96. offset += 1 << op.Size;
  97. }
  98. }
  99. else
  100. {
  101. for (int sElem = 0; sElem < op.SElems; sElem++)
  102. {
  103. int rt = (op.Rt + sElem) & 0x1f;
  104. if (isLoad)
  105. {
  106. EmitMemAddress();
  107. EmitReadZxCall(context, op.Size);
  108. EmitVectorInsert(context, rt, op.Index, op.Size);
  109. }
  110. else
  111. {
  112. EmitMemAddress();
  113. EmitVectorExtractZx(context, rt, op.Index, op.Size);
  114. EmitWriteCall(context, op.Size);
  115. }
  116. offset += 1 << op.Size;
  117. }
  118. }
  119. if (op.WBack)
  120. {
  121. EmitSimdMemWBack(context, offset);
  122. }
  123. }
  124. private static void EmitSimdMemWBack(ILEmitterCtx context, int offset)
  125. {
  126. OpCodeMemReg64 op = (OpCodeMemReg64)context.CurrOp;
  127. context.EmitLdint(op.Rn);
  128. if (op.Rm != RegisterAlias.Zr)
  129. {
  130. context.EmitLdint(op.Rm);
  131. }
  132. else
  133. {
  134. context.EmitLdc_I8(offset);
  135. }
  136. context.Emit(OpCodes.Add);
  137. context.EmitStint(op.Rn);
  138. }
  139. }
  140. }