AInstEmitSimdMove.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. Context.EmitLdvec(Op.Rd);
  49. Context.EmitStvectmp();
  50. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  51. int Position = Op.Imm4;
  52. for (int Index = 0; Index < Bytes; Index++)
  53. {
  54. int Reg = Op.Imm4 + Index < Bytes ? Op.Rn : Op.Rm;
  55. if (Position == Bytes)
  56. {
  57. Position = 0;
  58. }
  59. EmitVectorExtractZx(Context, Reg, Position++, 0);
  60. EmitVectorInsertTmp(Context, Index, 0);
  61. }
  62. Context.EmitLdvectmp();
  63. Context.EmitStvec(Op.Rd);
  64. if (Op.RegisterSize == ARegisterSize.SIMD64)
  65. {
  66. EmitVectorZeroUpper(Context, Op.Rd);
  67. }
  68. }
  69. public static void Fcsel_S(AILEmitterCtx Context)
  70. {
  71. AOpCodeSimdFcond Op = (AOpCodeSimdFcond)Context.CurrOp;
  72. AILLabel LblTrue = new AILLabel();
  73. AILLabel LblEnd = new AILLabel();
  74. Context.EmitCondBranch(LblTrue, Op.Cond);
  75. EmitVectorExtractF(Context, Op.Rm, 0, Op.Size);
  76. Context.Emit(OpCodes.Br_S, LblEnd);
  77. Context.MarkLabel(LblTrue);
  78. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  79. Context.MarkLabel(LblEnd);
  80. EmitScalarSetF(Context, Op.Rd, Op.Size);
  81. }
  82. public static void Fmov_Ftoi(AILEmitterCtx Context)
  83. {
  84. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  85. EmitVectorExtractZx(Context, Op.Rn, 0, 3);
  86. EmitIntZeroUpperIfNeeded(Context);
  87. Context.EmitStintzr(Op.Rd);
  88. }
  89. public static void Fmov_Ftoi1(AILEmitterCtx Context)
  90. {
  91. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  92. EmitVectorExtractZx(Context, Op.Rn, 1, 3);
  93. EmitIntZeroUpperIfNeeded(Context);
  94. Context.EmitStintzr(Op.Rd);
  95. }
  96. public static void Fmov_Itof(AILEmitterCtx Context)
  97. {
  98. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  99. Context.EmitLdintzr(Op.Rn);
  100. EmitIntZeroUpperIfNeeded(Context);
  101. EmitScalarSet(Context, Op.Rd, 3);
  102. }
  103. public static void Fmov_Itof1(AILEmitterCtx Context)
  104. {
  105. AOpCodeSimdCvt Op = (AOpCodeSimdCvt)Context.CurrOp;
  106. Context.EmitLdintzr(Op.Rn);
  107. EmitIntZeroUpperIfNeeded(Context);
  108. EmitVectorInsert(Context, Op.Rd, 1, 3);
  109. }
  110. public static void Fmov_S(AILEmitterCtx Context)
  111. {
  112. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  113. EmitVectorExtractF(Context, Op.Rn, 0, Op.Size);
  114. EmitScalarSetF(Context, Op.Rd, Op.Size);
  115. }
  116. public static void Fmov_Si(AILEmitterCtx Context)
  117. {
  118. AOpCodeSimdFmov Op = (AOpCodeSimdFmov)Context.CurrOp;
  119. Context.EmitLdc_I8(Op.Imm);
  120. EmitScalarSet(Context, Op.Rd, Op.Size + 2);
  121. }
  122. public static void Fmov_V(AILEmitterCtx Context)
  123. {
  124. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  125. int Elems = Op.RegisterSize == ARegisterSize.SIMD128 ? 4 : 2;
  126. for (int Index = 0; Index < (Elems >> Op.Size); Index++)
  127. {
  128. Context.EmitLdc_I8(Op.Imm);
  129. EmitVectorInsert(Context, Op.Rd, Index, Op.Size + 2);
  130. }
  131. if (Op.RegisterSize == ARegisterSize.SIMD64)
  132. {
  133. EmitVectorZeroUpper(Context, Op.Rd);
  134. }
  135. }
  136. public static void Ins_Gp(AILEmitterCtx Context)
  137. {
  138. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  139. Context.EmitLdintzr(Op.Rn);
  140. EmitVectorInsert(Context, Op.Rd, Op.DstIndex, Op.Size);
  141. }
  142. public static void Ins_V(AILEmitterCtx Context)
  143. {
  144. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  145. EmitVectorExtractZx(Context, Op.Rn, Op.SrcIndex, Op.Size);
  146. EmitVectorInsert(Context, Op.Rd, Op.DstIndex, Op.Size);
  147. }
  148. public static void Movi_V(AILEmitterCtx Context)
  149. {
  150. EmitVectorImmUnaryOp(Context, () => { });
  151. }
  152. public static void Mvni_V(AILEmitterCtx Context)
  153. {
  154. EmitVectorImmUnaryOp(Context, () => Context.Emit(OpCodes.Not));
  155. }
  156. public static void Tbl_V(AILEmitterCtx Context)
  157. {
  158. AOpCodeSimdTbl Op = (AOpCodeSimdTbl)Context.CurrOp;
  159. Context.EmitLdvec(Op.Rm);
  160. for (int Index = 0; Index < Op.Size; Index++)
  161. {
  162. Context.EmitLdvec((Op.Rn + Index) & 0x1f);
  163. }
  164. switch (Op.Size)
  165. {
  166. case 1: AVectorHelper.EmitCall(Context,
  167. nameof(AVectorHelper.Tbl1_V64),
  168. nameof(AVectorHelper.Tbl1_V128)); break;
  169. case 2: AVectorHelper.EmitCall(Context,
  170. nameof(AVectorHelper.Tbl2_V64),
  171. nameof(AVectorHelper.Tbl2_V128)); break;
  172. case 3: AVectorHelper.EmitCall(Context,
  173. nameof(AVectorHelper.Tbl3_V64),
  174. nameof(AVectorHelper.Tbl3_V128)); break;
  175. case 4: AVectorHelper.EmitCall(Context,
  176. nameof(AVectorHelper.Tbl4_V64),
  177. nameof(AVectorHelper.Tbl4_V128)); break;
  178. default: throw new InvalidOperationException();
  179. }
  180. Context.EmitStvec(Op.Rd);
  181. }
  182. public static void Trn1_V(AILEmitterCtx Context)
  183. {
  184. EmitVectorTranspose(Context, Part: 0);
  185. }
  186. public static void Trn2_V(AILEmitterCtx Context)
  187. {
  188. EmitVectorTranspose(Context, Part: 1);
  189. }
  190. public static void Umov_S(AILEmitterCtx Context)
  191. {
  192. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  193. EmitVectorExtractZx(Context, Op.Rn, Op.DstIndex, Op.Size);
  194. Context.EmitStintzr(Op.Rd);
  195. }
  196. public static void Uzp1_V(AILEmitterCtx Context)
  197. {
  198. EmitVectorUnzip(Context, Part: 0);
  199. }
  200. public static void Uzp2_V(AILEmitterCtx Context)
  201. {
  202. EmitVectorUnzip(Context, Part: 1);
  203. }
  204. public static void Xtn_V(AILEmitterCtx Context)
  205. {
  206. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  207. int Elems = 8 >> Op.Size;
  208. int Part = Op.RegisterSize == ARegisterSize.SIMD128 ? Elems : 0;
  209. for (int Index = 0; Index < Elems; Index++)
  210. {
  211. EmitVectorExtractZx(Context, Op.Rn, Index, Op.Size + 1);
  212. EmitVectorInsert(Context, Op.Rd, Part + Index, Op.Size);
  213. }
  214. if (Part == 0)
  215. {
  216. EmitVectorZeroUpper(Context, Op.Rd);
  217. }
  218. }
  219. public static void Zip1_V(AILEmitterCtx Context)
  220. {
  221. EmitVectorZip(Context, Part: 0);
  222. }
  223. public static void Zip2_V(AILEmitterCtx Context)
  224. {
  225. EmitVectorZip(Context, Part: 1);
  226. }
  227. private static void EmitIntZeroUpperIfNeeded(AILEmitterCtx Context)
  228. {
  229. if (Context.CurrOp.RegisterSize == ARegisterSize.Int32)
  230. {
  231. Context.Emit(OpCodes.Conv_U4);
  232. Context.Emit(OpCodes.Conv_U8);
  233. }
  234. }
  235. private static void EmitVectorTranspose(AILEmitterCtx Context, int Part)
  236. {
  237. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  238. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  239. int Elems = Bytes >> Op.Size;
  240. for (int Index = 0; Index < Elems; Index++)
  241. {
  242. int Elem = (Index & ~1) + Part;
  243. EmitVectorExtractZx(Context, (Index & 1) == 0 ? Op.Rn : Op.Rm, Elem, Op.Size);
  244. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  245. }
  246. if (Op.RegisterSize == ARegisterSize.SIMD64)
  247. {
  248. EmitVectorZeroUpper(Context, Op.Rd);
  249. }
  250. }
  251. private static void EmitVectorUnzip(AILEmitterCtx Context, int Part)
  252. {
  253. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  254. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  255. int Elems = Bytes >> Op.Size;
  256. int Half = Elems >> 1;
  257. for (int Index = 0; Index < Elems; Index++)
  258. {
  259. int Elem = Part + ((Index & (Half - 1)) << 1);
  260. EmitVectorExtractZx(Context, Index < Half ? Op.Rn : Op.Rm, Elem, Op.Size);
  261. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  262. }
  263. if (Op.RegisterSize == ARegisterSize.SIMD64)
  264. {
  265. EmitVectorZeroUpper(Context, Op.Rd);
  266. }
  267. }
  268. private static void EmitVectorZip(AILEmitterCtx Context, int Part)
  269. {
  270. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  271. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  272. int Elems = Bytes >> Op.Size;
  273. int Half = Elems >> 1;
  274. for (int Index = 0; Index < Elems; Index++)
  275. {
  276. int Elem = Part * Half + (Index >> 1);
  277. EmitVectorExtractZx(Context, (Index & 1) == 0 ? Op.Rn : Op.Rm, Elem, Op.Size);
  278. EmitVectorInsert(Context, Op.Rd, Index, Op.Size);
  279. }
  280. if (Op.RegisterSize == ARegisterSize.SIMD64)
  281. {
  282. EmitVectorZeroUpper(Context, Op.Rd);
  283. }
  284. }
  285. }
  286. }