InstEmitSimdMemory.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.EmitLdarg(TranslatedSub.MemoryArgIdx);
  40. context.EmitLdint(op.Rn);
  41. context.EmitLdc_I8(offset);
  42. context.Emit(OpCodes.Add);
  43. EmitReadZxCall(context, op.Size);
  44. EmitVectorInsert(context, rtt, elem, op.Size);
  45. if (op.RegisterSize == RegisterSize.Simd64 && elem == op.Elems - 1)
  46. {
  47. EmitVectorZeroUpper(context, rtt);
  48. }
  49. }
  50. else
  51. {
  52. context.EmitLdarg(TranslatedSub.MemoryArgIdx);
  53. context.EmitLdint(op.Rn);
  54. context.EmitLdc_I8(offset);
  55. context.Emit(OpCodes.Add);
  56. EmitVectorExtractZx(context, rtt, elem, op.Size);
  57. EmitWriteCall(context, op.Size);
  58. }
  59. offset += 1 << op.Size;
  60. }
  61. if (op.WBack)
  62. {
  63. EmitSimdMemWBack(context, offset);
  64. }
  65. }
  66. private static void EmitSimdMemSs(ILEmitterCtx context, bool isLoad)
  67. {
  68. OpCodeSimdMemSs64 op = (OpCodeSimdMemSs64)context.CurrOp;
  69. int offset = 0;
  70. void EmitMemAddress()
  71. {
  72. context.EmitLdarg(TranslatedSub.MemoryArgIdx);
  73. context.EmitLdint(op.Rn);
  74. context.EmitLdc_I8(offset);
  75. context.Emit(OpCodes.Add);
  76. }
  77. if (op.Replicate)
  78. {
  79. //Only loads uses the replicate mode.
  80. if (!isLoad)
  81. {
  82. throw new InvalidOperationException();
  83. }
  84. int bytes = op.GetBitsCount() >> 3;
  85. int elems = bytes >> op.Size;
  86. for (int sElem = 0; sElem < op.SElems; sElem++)
  87. {
  88. int rt = (op.Rt + sElem) & 0x1f;
  89. for (int index = 0; index < elems; index++)
  90. {
  91. EmitMemAddress();
  92. EmitReadZxCall(context, op.Size);
  93. EmitVectorInsert(context, rt, index, op.Size);
  94. }
  95. if (op.RegisterSize == RegisterSize.Simd64)
  96. {
  97. EmitVectorZeroUpper(context, rt);
  98. }
  99. offset += 1 << op.Size;
  100. }
  101. }
  102. else
  103. {
  104. for (int sElem = 0; sElem < op.SElems; sElem++)
  105. {
  106. int rt = (op.Rt + sElem) & 0x1f;
  107. if (isLoad)
  108. {
  109. EmitMemAddress();
  110. EmitReadZxCall(context, op.Size);
  111. EmitVectorInsert(context, rt, op.Index, op.Size);
  112. }
  113. else
  114. {
  115. EmitMemAddress();
  116. EmitVectorExtractZx(context, rt, op.Index, op.Size);
  117. EmitWriteCall(context, op.Size);
  118. }
  119. offset += 1 << op.Size;
  120. }
  121. }
  122. if (op.WBack)
  123. {
  124. EmitSimdMemWBack(context, offset);
  125. }
  126. }
  127. private static void EmitSimdMemWBack(ILEmitterCtx context, int offset)
  128. {
  129. OpCodeMemReg64 op = (OpCodeMemReg64)context.CurrOp;
  130. context.EmitLdint(op.Rn);
  131. if (op.Rm != CpuThreadState.ZrIndex)
  132. {
  133. context.EmitLdint(op.Rm);
  134. }
  135. else
  136. {
  137. context.EmitLdc_I8(offset);
  138. }
  139. context.Emit(OpCodes.Add);
  140. context.EmitStint(op.Rn);
  141. }
  142. }
  143. }