AInstEmitSimdMemory.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection.Emit;
  6. using static ChocolArm64.Instruction.AInstEmitMemoryHelper;
  7. using static ChocolArm64.Instruction.AInstEmitSimdHelper;
  8. namespace ChocolArm64.Instruction
  9. {
  10. static partial class AInstEmit
  11. {
  12. public static void Ld__Vms(AILEmitterCtx Context)
  13. {
  14. EmitSimdMemMs(Context, IsLoad: true);
  15. }
  16. public static void Ld__Vss(AILEmitterCtx Context)
  17. {
  18. EmitSimdMemSs(Context, IsLoad: true);
  19. }
  20. public static void St__Vms(AILEmitterCtx Context)
  21. {
  22. EmitSimdMemMs(Context, IsLoad: false);
  23. }
  24. public static void St__Vss(AILEmitterCtx Context)
  25. {
  26. EmitSimdMemSs(Context, IsLoad: false);
  27. }
  28. private static void EmitSimdMemMs(AILEmitterCtx Context, bool IsLoad)
  29. {
  30. AOpCodeSimdMemMs Op = (AOpCodeSimdMemMs)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(ATranslatedSub.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 == ARegisterSize.SIMD64 && Elem == Op.Elems - 1)
  46. {
  47. EmitVectorZeroUpper(Context, Rtt);
  48. }
  49. }
  50. else
  51. {
  52. Context.EmitLdarg(ATranslatedSub.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(AILEmitterCtx Context, bool IsLoad)
  67. {
  68. AOpCodeSimdMemSs Op = (AOpCodeSimdMemSs)Context.CurrOp;
  69. int Offset = 0;
  70. void EmitMemAddress()
  71. {
  72. Context.EmitLdarg(ATranslatedSub.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 == ARegisterSize.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(AILEmitterCtx Context, int Offset)
  128. {
  129. AOpCodeMemReg Op = (AOpCodeMemReg)Context.CurrOp;
  130. Context.EmitLdint(Op.Rn);
  131. if (Op.Rm != AThreadState.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. }