AInstEmitSimdMove.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.AInstEmitSimdHelper;
  7. namespace ChocolArm64.Instruction
  8. {
  9. static partial class AInstEmit
  10. {
  11. public static void Dup_Gp(AILEmitterCtx Context)
  12. {
  13. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  14. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  15. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  16. {
  17. Context.EmitLdintzr(Op.Rn);
  18. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  19. }
  20. if (Op.RegisterSize == ARegisterSize.SIMD64)
  21. {
  22. EmitVectorZeroUpper(Context, Op.Rd);
  23. }
  24. }
  25. public static void Dup_S(AILEmitterCtx Context)
  26. {
  27. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  28. EmitVectorExtractZx(Context, Op.Rn, Op.DstIndex, Op.Size);
  29. EmitScalarSet(Context, Op.Rd, Op.Size);
  30. }
  31. public static void Dup_V(AILEmitterCtx Context)
  32. {
  33. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  34. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  35. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  36. {
  37. EmitVectorExtractZx(Context, Op.Rn, Op.DstIndex, Op.Size);
  38. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  39. }
  40. if (Op.RegisterSize == ARegisterSize.SIMD64)
  41. {
  42. EmitVectorZeroUpper(Context, Op.Rd);
  43. }
  44. }
  45. public static void Ext_V(AILEmitterCtx Context)
  46. {
  47. AOpCodeSimdExt Op = (AOpCodeSimdExt)Context.CurrOp;
  48. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  49. int Position = Op.Imm4;
  50. for (int Index = 0; Index < Bytes; Index++)
  51. {
  52. int Reg = Op.Imm4 + Index < Bytes ? Op.Rn : Op.Rm;
  53. if (Position == Bytes)
  54. {
  55. Position = 0;
  56. }
  57. EmitVectorExtractZx(Context, Reg, Position++, 0);
  58. EmitVectorInsert(Context, Op.Rd, Index, 0);
  59. }
  60. if (Op.RegisterSize == ARegisterSize.SIMD64)
  61. {
  62. EmitVectorZeroUpper(Context, Op.Rd);
  63. }
  64. }
  65. public static void Fcsel_S(AILEmitterCtx Context)
  66. {
  67. AOpCodeSimdFcond Op = (AOpCodeSimdFcond)Context.CurrOp;
  68. AILLabel LblTrue = new AILLabel();
  69. AILLabel LblEnd = new AILLabel();
  70. Context.EmitCondBranch(LblTrue, Op.Cond);
  71. EmitVectorExtractF(Context, Op.Rm, 0, Op.Size);
  72. Context.Emit(OpCodes.Br_S, LblEnd);
  73. Context.MarkLabel(LblTrue);
  74. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  75. Context.MarkLabel(LblEnd);
  76. EmitScalarSetF(Context, Op.Rd, Op.Size);
  77. }
  78. public static void Fmov_Ftoi(AILEmitterCtx Context)
  79. {
  80. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  81. EmitVectorExtractZx(Context, Op.Rn, 0, 3);
  82. EmitIntZeroHigherIfNeeded(Context);
  83. Context.EmitStintzr(Op.Rd);
  84. }
  85. public static void Fmov_Ftoi1(AILEmitterCtx Context)
  86. {
  87. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  88. EmitVectorExtractZx(Context, Op.Rn, 1, 3);
  89. EmitIntZeroHigherIfNeeded(Context);
  90. Context.EmitStintzr(Op.Rd);
  91. }
  92. public static void Fmov_Itof(AILEmitterCtx Context)
  93. {
  94. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  95. Context.EmitLdintzr(Op.Rn);
  96. EmitIntZeroHigherIfNeeded(Context);
  97. EmitScalarSet(Context, Op.Rd, 3);
  98. }
  99. public static void Fmov_Itof1(AILEmitterCtx Context)
  100. {
  101. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  102. Context.EmitLdintzr(Op.Rn);
  103. EmitIntZeroHigherIfNeeded(Context);
  104. EmitVectorInsert(Context, Op.Rd, 1, 3);
  105. }
  106. public static void Fmov_S(AILEmitterCtx Context)
  107. {
  108. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  109. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  110. EmitScalarSetF(Context, Op.Rd, Op.Size);
  111. }
  112. public static void Fmov_Si(AILEmitterCtx Context)
  113. {
  114. AOpCodeSimdFmov Op = (AOpCodeSimdFmov)Context.CurrOp;
  115. Context.EmitLdc_I8(Op.Imm);
  116. EmitScalarSet(Context, Op.Rd, Op.Size + 2);
  117. }
  118. public static void Fmov_V(AILEmitterCtx Context)
  119. {
  120. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  121. int Elems = Op.RegisterSize == ARegisterSize.SIMD128 ? 4 : 2;
  122. for (int Index = 0; Index < (Elems >> Op.Size); Index++)
  123. {
  124. Context.EmitLdc_I8(Op.Imm);
  125. EmitVectorInsert(Context, Op.Rd, Index, Op.Size + 2);
  126. }
  127. if (Op.RegisterSize == ARegisterSize.SIMD64)
  128. {
  129. EmitVectorZeroUpper(Context, Op.Rd);
  130. }
  131. }
  132. public static void Ins_Gp(AILEmitterCtx Context)
  133. {
  134. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  135. Context.EmitLdintzr(Op.Rn);
  136. EmitVectorInsert(Context, Op.Rd, Op.DstIndex, Op.Size);
  137. }
  138. public static void Ins_V(AILEmitterCtx Context)
  139. {
  140. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  141. EmitVectorExtractZx(Context, Op.Rn, Op.SrcIndex, Op.Size);
  142. EmitVectorInsert(Context, Op.Rd, Op.DstIndex, Op.Size);
  143. }
  144. public static void Movi_V(AILEmitterCtx Context)
  145. {
  146. EmitVectorImmUnaryOp(Context, () => { });
  147. }
  148. public static void Mvni_V(AILEmitterCtx Context)
  149. {
  150. EmitVectorImmUnaryOp(Context, () => Context.Emit(OpCodes.Not));
  151. }
  152. public static void Tbl_V(AILEmitterCtx Context)
  153. {
  154. AOpCodeSimdTbl Op = (AOpCodeSimdTbl)Context.CurrOp;
  155. Context.EmitLdvec(Op.Rm);
  156. for (int Index = 0; Index < Op.Size; Index++)
  157. {
  158. Context.EmitLdvec((Op.Rn + Index) & 0x1f);
  159. }
  160. switch (Op.Size)
  161. {
  162. case 1: ASoftFallback.EmitCall(Context,
  163. nameof(ASoftFallback.Tbl1_V64),
  164. nameof(ASoftFallback.Tbl1_V128)); break;
  165. case 2: ASoftFallback.EmitCall(Context,
  166. nameof(ASoftFallback.Tbl2_V64),
  167. nameof(ASoftFallback.Tbl2_V128)); break;
  168. case 3: ASoftFallback.EmitCall(Context,
  169. nameof(ASoftFallback.Tbl3_V64),
  170. nameof(ASoftFallback.Tbl3_V128)); break;
  171. case 4: ASoftFallback.EmitCall(Context,
  172. nameof(ASoftFallback.Tbl4_V64),
  173. nameof(ASoftFallback.Tbl4_V128)); break;
  174. default: throw new InvalidOperationException();
  175. }
  176. Context.EmitStvec(Op.Rd);
  177. }
  178. public static void Umov_S(AILEmitterCtx Context)
  179. {
  180. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  181. EmitVectorExtractZx(Context, Op.Rn, Op.DstIndex, Op.Size);
  182. Context.EmitStintzr(Op.Rd);
  183. }
  184. public static void Uzp1_V(AILEmitterCtx Context)
  185. {
  186. EmitVectorUnzip(Context, Part: 0);
  187. }
  188. public static void Uzp2_V(AILEmitterCtx Context)
  189. {
  190. EmitVectorUnzip(Context, Part: 1);
  191. }
  192. public static void Xtn_V(AILEmitterCtx Context)
  193. {
  194. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  195. int Elems = 8 >> Op.Size;
  196. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  197. for (int Index = 0; Index < Elems; Index++)
  198. {
  199. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size + 1);
  200. EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size);
  201. }
  202. if (Part == 0)
  203. {
  204. EmitVectorZeroUpper(Context, Op.Rd);
  205. }
  206. }
  207. public static void Zip1_V(AILEmitterCtx Context)
  208. {
  209. EmitVectorZip(Context, Part: 0);
  210. }
  211. public static void Zip2_V(AILEmitterCtx Context)
  212. {
  213. EmitVectorZip(Context, Part: 1);
  214. }
  215. private static void EmitIntZeroHigherIfNeeded(AILEmitterCtx Context)
  216. {
  217. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  218. {
  219. Context.Emit(OpCodes.Conv_U4);
  220. Context.Emit(OpCodes.Conv_U8);
  221. }
  222. }
  223. private static void EmitVectorUnzip(AILEmitterCtx Context, int Part)
  224. {
  225. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  226. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  227. int Elems = Bytes >> Op.Size;
  228. int Half = Elems >> 1;
  229. for (int Index = 0; Index < Elems; Index++)
  230. {
  231. int Elem = Part + ((Index & (Half - 1)) << 1);
  232. EmitVectorExtractZx(Context, Index < Half ? Op.Rn : Op.Rm, Elem, Op.Size);
  233. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  234. }
  235. if (Op.RegisterSize == ARegisterSize.SIMD64)
  236. {
  237. EmitVectorZeroUpper(Context, Op.Rd);
  238. }
  239. }
  240. private static void EmitVectorZip(AILEmitterCtx Context, int Part)
  241. {
  242. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  243. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  244. int Elems = Bytes >> Op.Size;
  245. int Half = Elems >> 1;
  246. for (int Index = 0; Index < Elems; Index++)
  247. {
  248. int Elem = Part * Half + (Index >> 1);
  249. EmitVectorExtractZx(Context, (Index & 1) == 0 ? Op.Rn : Op.Rm, Elem, Op.Size);
  250. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  251. }
  252. if (Op.RegisterSize == ARegisterSize.SIMD64)
  253. {
  254. EmitVectorZeroUpper(Context, Op.Rd);
  255. }
  256. }
  257. }
  258. }