AInstEmitSimd.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection;
  6. using System.Reflection.Emit;
  7. using static ChocolArm64.Instruction.AInstEmitMemoryHelper;
  8. namespace ChocolArm64.Instruction
  9. {
  10. static partial class AInstEmit
  11. {
  12. public static void Add_V(AILEmitterCtx Context) => EmitVectorBinaryZx(Context, OpCodes.Add);
  13. public static void Addp_V(AILEmitterCtx Context)
  14. {
  15. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  16. Context.EmitLdvec(Op.Rn);
  17. Context.EmitLdvec(Op.Rm);
  18. Context.EmitLdc_I4(Op.Size);
  19. ASoftFallback.EmitCall(Context,
  20. nameof(ASoftFallback.Addp64),
  21. nameof(ASoftFallback.Addp128));
  22. Context.EmitStvec(Op.Rd);
  23. }
  24. public static void Addv_V(AILEmitterCtx Context) => EmitVectorAddv(Context);
  25. public static void And_V(AILEmitterCtx Context) => EmitVectorBinaryZx(Context, OpCodes.And);
  26. public static void Bic_V(AILEmitterCtx Context) => EmitVectorBic(Context);
  27. public static void Bic_Vi(AILEmitterCtx Context)
  28. {
  29. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  30. Context.EmitLdvec(Op.Rd);
  31. Context.EmitLdc_I8(Op.Imm);
  32. Context.EmitLdc_I4(Op.Size);
  33. ASoftFallback.EmitCall(Context,
  34. nameof(ASoftFallback.Bic_Vi64),
  35. nameof(ASoftFallback.Bic_Vi128));
  36. Context.EmitStvec(Op.Rd);
  37. }
  38. public static void Bsl_V(AILEmitterCtx Context) => EmitVectorBsl(Context);
  39. public static void Cmeq_V(AILEmitterCtx Context) => EmitVectorCmp(Context, OpCodes.Beq_S);
  40. public static void Cmge_V(AILEmitterCtx Context) => EmitVectorCmp(Context, OpCodes.Bge_S);
  41. public static void Cmgt_V(AILEmitterCtx Context) => EmitVectorCmp(Context, OpCodes.Bgt_S);
  42. public static void Cmhi_V(AILEmitterCtx Context) => EmitVectorCmp(Context, OpCodes.Bgt_Un_S);
  43. public static void Cmhs_V(AILEmitterCtx Context) => EmitVectorCmp(Context, OpCodes.Bge_Un_S);
  44. public static void Cmle_V(AILEmitterCtx Context) => EmitVectorCmp(Context, OpCodes.Ble_S);
  45. public static void Cmlt_V(AILEmitterCtx Context) => EmitVectorCmp(Context, OpCodes.Blt_S);
  46. public static void Cnt_V(AILEmitterCtx Context)
  47. {
  48. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  49. Context.EmitLdvec(Op.Rn);
  50. ASoftFallback.EmitCall(Context,
  51. nameof(ASoftFallback.Cnt64),
  52. nameof(ASoftFallback.Cnt128));
  53. Context.EmitStvec(Op.Rd);
  54. }
  55. public static void Dup_Gp(AILEmitterCtx Context)
  56. {
  57. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  58. Context.EmitLdintzr(Op.Rn);
  59. Context.EmitLdc_I4(Op.Size);
  60. ASoftFallback.EmitCall(Context,
  61. nameof(ASoftFallback.Dup_Gp64),
  62. nameof(ASoftFallback.Dup_Gp128));
  63. Context.EmitStvec(Op.Rd);
  64. }
  65. public static void Dup_V(AILEmitterCtx Context)
  66. {
  67. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  68. Context.EmitLdvec(Op.Rn);
  69. Context.EmitLdc_I4(Op.DstIndex);
  70. Context.EmitLdc_I4(Op.Size);
  71. ASoftFallback.EmitCall(Context,
  72. nameof(ASoftFallback.Dup_V64),
  73. nameof(ASoftFallback.Dup_V128));
  74. Context.EmitStvec(Op.Rd);
  75. }
  76. public static void Eor_V(AILEmitterCtx Context) => EmitVectorBinaryZx(Context, OpCodes.Xor);
  77. public static void Fadd_V(AILEmitterCtx Context)
  78. {
  79. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  80. Context.EmitLdvec(Op.Rn);
  81. Context.EmitLdvec(Op.Rm);
  82. Context.EmitLdc_I4(Op.SizeF);
  83. ASoftFallback.EmitCall(Context,
  84. nameof(ASoftFallback.Fadd64),
  85. nameof(ASoftFallback.Fadd128));
  86. Context.EmitStvec(Op.Rd);
  87. }
  88. public static void Fcvtzs_V(AILEmitterCtx Context)
  89. {
  90. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  91. Context.EmitLdvec(Op.Rn);
  92. Context.EmitLdc_I4(Op.SizeF);
  93. ASoftFallback.EmitCall(Context,
  94. nameof(ASoftFallback.Fcvtzs_V64),
  95. nameof(ASoftFallback.Fcvtzs_V128));
  96. Context.EmitStvec(Op.Rd);
  97. }
  98. public static void Fcvtzu_V(AILEmitterCtx Context)
  99. {
  100. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  101. Context.EmitLdvec(Op.Rn);
  102. Context.EmitLdc_I4(0);
  103. Context.EmitLdc_I4(Op.SizeF);
  104. ASoftFallback.EmitCall(Context,
  105. nameof(ASoftFallback.Fcvtzu_V_64),
  106. nameof(ASoftFallback.Fcvtzu_V_128));
  107. Context.EmitStvec(Op.Rd);
  108. }
  109. public static void Fcvtzu_V_Fix(AILEmitterCtx Context)
  110. {
  111. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  112. Context.EmitLdvec(Op.Rn);
  113. Context.EmitLdc_I4((8 << (Op.Size + 1)) - Op.Imm);
  114. Context.EmitLdc_I4(Op.Size - 2);
  115. ASoftFallback.EmitCall(Context,
  116. nameof(ASoftFallback.Fcvtzu_V_64),
  117. nameof(ASoftFallback.Fcvtzu_V_128));
  118. Context.EmitStvec(Op.Rd);
  119. }
  120. public static void Fmla_V(AILEmitterCtx Context)
  121. {
  122. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  123. Context.EmitLdvec(Op.Rd);
  124. Context.EmitLdvec(Op.Rn);
  125. Context.EmitLdvec(Op.Rm);
  126. Context.EmitLdc_I4(Op.SizeF);
  127. ASoftFallback.EmitCall(Context,
  128. nameof(ASoftFallback.Fmla64),
  129. nameof(ASoftFallback.Fmla128));
  130. Context.EmitStvec(Op.Rd);
  131. }
  132. public static void Fmla_Vs(AILEmitterCtx Context)
  133. {
  134. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  135. Context.EmitLdvec(Op.Rd);
  136. Context.EmitLdvec(Op.Rn);
  137. Context.EmitLdvec(Op.Rm);
  138. Context.EmitLdc_I4(Op.Index);
  139. Context.EmitLdc_I4(Op.SizeF);
  140. ASoftFallback.EmitCall(Context,
  141. nameof(ASoftFallback.Fmla_Ve64),
  142. nameof(ASoftFallback.Fmla_Ve128));
  143. Context.EmitStvec(Op.Rd);
  144. }
  145. public static void Fmov_V(AILEmitterCtx Context)
  146. {
  147. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  148. Context.EmitLdc_I8(Op.Imm);
  149. Context.EmitLdc_I4(Op.Size + 2);
  150. ASoftFallback.EmitCall(Context,
  151. nameof(ASoftFallback.Dup_Gp64),
  152. nameof(ASoftFallback.Dup_Gp128));
  153. Context.EmitStvec(Op.Rd);
  154. }
  155. public static void Fmul_V(AILEmitterCtx Context)
  156. {
  157. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  158. Context.EmitLdvec(Op.Rn);
  159. Context.EmitLdvec(Op.Rm);
  160. Context.EmitLdc_I4(Op.SizeF);
  161. ASoftFallback.EmitCall(Context,
  162. nameof(ASoftFallback.Fmul64),
  163. nameof(ASoftFallback.Fmul128));
  164. Context.EmitStvec(Op.Rd);
  165. }
  166. public static void Fmul_Vs(AILEmitterCtx Context)
  167. {
  168. AOpCodeSimdRegElem Op = (AOpCodeSimdRegElem)Context.CurrOp;
  169. Context.EmitLdvec(Op.Rn);
  170. Context.EmitLdvec(Op.Rm);
  171. Context.EmitLdc_I4(Op.Index);
  172. Context.EmitLdc_I4(Op.SizeF);
  173. ASoftFallback.EmitCall(Context,
  174. nameof(ASoftFallback.Fmul_Ve64),
  175. nameof(ASoftFallback.Fmul_Ve128));
  176. Context.EmitStvec(Op.Rd);
  177. }
  178. public static void Fsub_V(AILEmitterCtx Context)
  179. {
  180. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  181. Context.EmitLdvec(Op.Rn);
  182. Context.EmitLdvec(Op.Rm);
  183. Context.EmitLdc_I4(Op.SizeF);
  184. ASoftFallback.EmitCall(Context,
  185. nameof(ASoftFallback.Fsub64),
  186. nameof(ASoftFallback.Fsub128));
  187. Context.EmitStvec(Op.Rd);
  188. }
  189. public static void Ins_Gp(AILEmitterCtx Context)
  190. {
  191. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  192. Context.EmitLdvec(Op.Rd);
  193. Context.EmitLdintzr(Op.Rn);
  194. Context.EmitLdc_I4(Op.DstIndex);
  195. Context.EmitLdc_I4(Op.Size);
  196. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Ins_Gp));
  197. Context.EmitStvec(Op.Rd);
  198. }
  199. public static void Ins_V(AILEmitterCtx Context)
  200. {
  201. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  202. Context.EmitLdvec(Op.Rd);
  203. Context.EmitLdintzr(Op.Rn);
  204. Context.EmitLdc_I4(Op.SrcIndex);
  205. Context.EmitLdc_I4(Op.DstIndex);
  206. Context.EmitLdc_I4(Op.Size);
  207. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Ins_V));
  208. Context.EmitStvec(Op.Rd);
  209. }
  210. public static void Ld__V(AILEmitterCtx Context) => EmitSimdMultLdSt(Context, IsLoad: true);
  211. public static void Mla_V(AILEmitterCtx Context) => EmitVectorMla(Context);
  212. public static void Movi_V(AILEmitterCtx Context) => EmitMovi_V(Context, false);
  213. public static void Mul_V(AILEmitterCtx Context) => EmitVectorBinaryZx(Context, OpCodes.Mul);
  214. public static void Mvni_V(AILEmitterCtx Context) => EmitMovi_V(Context, true);
  215. private static void EmitMovi_V(AILEmitterCtx Context, bool Not)
  216. {
  217. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  218. Context.EmitLdc_I8(Not ? ~Op.Imm : Op.Imm);
  219. Context.EmitLdc_I4(Op.Size);
  220. ASoftFallback.EmitCall(Context,
  221. nameof(ASoftFallback.Dup_Gp64),
  222. nameof(ASoftFallback.Dup_Gp128));
  223. Context.EmitStvec(Op.Rd);
  224. }
  225. public static void Neg_V(AILEmitterCtx Context) => EmitVectorUnarySx(Context, OpCodes.Neg);
  226. public static void Not_V(AILEmitterCtx Context) => EmitVectorUnaryZx(Context, OpCodes.Not);
  227. public static void Orr_V(AILEmitterCtx Context) => EmitVectorBinaryZx(Context, OpCodes.Or);
  228. public static void Orr_Vi(AILEmitterCtx Context)
  229. {
  230. AOpCodeSimdImm Op = (AOpCodeSimdImm)Context.CurrOp;
  231. Context.EmitLdvec(Op.Rd);
  232. Context.EmitLdc_I8(Op.Imm);
  233. Context.EmitLdc_I4(Op.Size);
  234. ASoftFallback.EmitCall(Context,
  235. nameof(ASoftFallback.Orr_Vi64),
  236. nameof(ASoftFallback.Orr_Vi128));
  237. Context.EmitStvec(Op.Rd);
  238. }
  239. public static void Saddw_V(AILEmitterCtx Context)
  240. {
  241. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  242. Context.EmitLdvec(Op.Rn);
  243. Context.EmitLdvec(Op.Rm);
  244. Context.EmitLdc_I4(Op.Size);
  245. ASoftFallback.EmitCall(Context,
  246. nameof(ASoftFallback.Saddw),
  247. nameof(ASoftFallback.Saddw2));
  248. Context.EmitStvec(Op.Rd);
  249. }
  250. public static void Scvtf_V(AILEmitterCtx Context)
  251. {
  252. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  253. Context.EmitLdvec(Op.Rn);
  254. Context.EmitLdc_I4(Op.SizeF);
  255. ASoftFallback.EmitCall(Context,
  256. nameof(ASoftFallback.Scvtf_V64),
  257. nameof(ASoftFallback.Scvtf_V128));
  258. Context.EmitStvec(Op.Rd);
  259. }
  260. public static void Shl_V(AILEmitterCtx Context)
  261. {
  262. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  263. Context.EmitLdvec(Op.Rn);
  264. Context.EmitLdc_I4(Op.Imm - (8 << Op.Size));
  265. Context.EmitLdc_I4(Op.Size);
  266. ASoftFallback.EmitCall(Context,
  267. nameof(ASoftFallback.Shl64),
  268. nameof(ASoftFallback.Shl128));
  269. Context.EmitStvec(Op.Rd);
  270. }
  271. public static void Smax_V(AILEmitterCtx Context) => EmitVectorSmax(Context);
  272. public static void Smin_V(AILEmitterCtx Context) => EmitVectorSmin(Context);
  273. public static void Sshll_V(AILEmitterCtx Context)
  274. {
  275. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  276. Context.EmitLdvec(Op.Rn);
  277. Context.EmitLdc_I4(Op.Imm - (8 << Op.Size));
  278. Context.EmitLdc_I4(Op.Size);
  279. ASoftFallback.EmitCall(Context,
  280. nameof(ASoftFallback.Sshll),
  281. nameof(ASoftFallback.Sshll2));
  282. Context.EmitStvec(Op.Rd);
  283. }
  284. public static void Sshr_V(AILEmitterCtx Context)
  285. {
  286. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  287. Context.EmitLdvec(Op.Rn);
  288. Context.EmitLdc_I4((8 << (Op.Size + 1)) - Op.Imm);
  289. Context.EmitLdc_I4(Op.Size);
  290. ASoftFallback.EmitCall(Context,
  291. nameof(ASoftFallback.Sshr64),
  292. nameof(ASoftFallback.Sshr128));
  293. Context.EmitStvec(Op.Rd);
  294. }
  295. public static void St__V(AILEmitterCtx Context) => EmitSimdMultLdSt(Context, IsLoad: false);
  296. public static void Sub_V(AILEmitterCtx Context) => EmitVectorBinaryZx(Context, OpCodes.Sub);
  297. public static void Tbl_V(AILEmitterCtx Context)
  298. {
  299. AOpCodeSimdTbl Op = (AOpCodeSimdTbl)Context.CurrOp;
  300. Context.EmitLdvec(Op.Rm);
  301. for (int Index = 0; Index < Op.Size; Index++)
  302. {
  303. Context.EmitLdvec((Op.Rn + Index) & 0x1f);
  304. }
  305. switch (Op.Size)
  306. {
  307. case 1: ASoftFallback.EmitCall(Context,
  308. nameof(ASoftFallback.Tbl1_V64),
  309. nameof(ASoftFallback.Tbl1_V128)); break;
  310. case 2: ASoftFallback.EmitCall(Context,
  311. nameof(ASoftFallback.Tbl2_V64),
  312. nameof(ASoftFallback.Tbl2_V128)); break;
  313. case 3: ASoftFallback.EmitCall(Context,
  314. nameof(ASoftFallback.Tbl3_V64),
  315. nameof(ASoftFallback.Tbl3_V128)); break;
  316. case 4: ASoftFallback.EmitCall(Context,
  317. nameof(ASoftFallback.Tbl4_V64),
  318. nameof(ASoftFallback.Tbl4_V128)); break;
  319. default: throw new InvalidOperationException();
  320. }
  321. Context.EmitStvec(Op.Rd);
  322. }
  323. public static void Uaddlv_V(AILEmitterCtx Context)
  324. {
  325. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  326. Context.EmitLdvec(Op.Rn);
  327. Context.EmitLdc_I4(Op.Size);
  328. ASoftFallback.EmitCall(Context,
  329. nameof(ASoftFallback.Uaddlv64),
  330. nameof(ASoftFallback.Uaddlv128));
  331. Context.EmitStvec(Op.Rd);
  332. }
  333. public static void Uaddw_V(AILEmitterCtx Context)
  334. {
  335. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  336. Context.EmitLdvec(Op.Rn);
  337. Context.EmitLdvec(Op.Rm);
  338. Context.EmitLdc_I4(Op.Size);
  339. ASoftFallback.EmitCall(Context,
  340. nameof(ASoftFallback.Uaddw),
  341. nameof(ASoftFallback.Uaddw2));
  342. Context.EmitStvec(Op.Rd);
  343. }
  344. public static void Ucvtf_V(AILEmitterCtx Context)
  345. {
  346. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  347. Context.EmitLdvec(Op.Rn);
  348. if (Op.Size == 0)
  349. {
  350. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Ucvtf_V_F));
  351. }
  352. else if (Op.Size == 1)
  353. {
  354. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Ucvtf_V_D));
  355. }
  356. else
  357. {
  358. throw new InvalidOperationException();
  359. }
  360. Context.EmitStvec(Op.Rd);
  361. }
  362. public static void Umov_S(AILEmitterCtx Context)
  363. {
  364. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  365. Context.EmitLdvec(Op.Rn);
  366. Context.EmitLdc_I4(Op.DstIndex);
  367. Context.EmitLdc_I4(Op.Size);
  368. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.ExtractVec));
  369. Context.EmitStintzr(Op.Rd);
  370. }
  371. public static void Ushl_V(AILEmitterCtx Context) => EmitVectorUshl(Context);
  372. public static void Ushll_V(AILEmitterCtx Context)
  373. {
  374. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  375. Context.EmitLdvec(Op.Rn);
  376. Context.EmitLdc_I4(Op.Imm - (8 << Op.Size));
  377. Context.EmitLdc_I4(Op.Size);
  378. ASoftFallback.EmitCall(Context,
  379. nameof(ASoftFallback.Ushll),
  380. nameof(ASoftFallback.Ushll2));
  381. Context.EmitStvec(Op.Rd);
  382. }
  383. public static void Ushr_V(AILEmitterCtx Context)
  384. {
  385. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  386. Context.EmitLdvec(Op.Rn);
  387. Context.EmitLdc_I4((8 << (Op.Size + 1)) - Op.Imm);
  388. Context.EmitLdc_I4(Op.Size);
  389. ASoftFallback.EmitCall(Context,
  390. nameof(ASoftFallback.Ushr64),
  391. nameof(ASoftFallback.Ushr128));
  392. Context.EmitStvec(Op.Rd);
  393. }
  394. public static void Usra_V(AILEmitterCtx Context)
  395. {
  396. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  397. Context.EmitLdvec(Op.Rd);
  398. Context.EmitLdvec(Op.Rn);
  399. Context.EmitLdc_I4((8 << (Op.Size + 1)) - Op.Imm);
  400. Context.EmitLdc_I4(Op.Size);
  401. ASoftFallback.EmitCall(Context,
  402. nameof(ASoftFallback.Usra64),
  403. nameof(ASoftFallback.Usra128));
  404. Context.EmitStvec(Op.Rd);
  405. }
  406. public static void Uzp1_V(AILEmitterCtx Context)
  407. {
  408. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  409. Context.EmitLdvec(Op.Rn);
  410. Context.EmitLdvec(Op.Rm);
  411. Context.EmitLdc_I4(Op.Size);
  412. ASoftFallback.EmitCall(Context,
  413. nameof(ASoftFallback.Uzp1_V64),
  414. nameof(ASoftFallback.Uzp1_V128));
  415. Context.EmitStvec(Op.Rd);
  416. }
  417. public static void Xtn_V(AILEmitterCtx Context)
  418. {
  419. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  420. Context.EmitLdvec(Op.Rn);
  421. Context.EmitLdc_I4(Op.Size);
  422. ASoftFallback.EmitCall(Context,
  423. nameof(ASoftFallback.Xtn),
  424. nameof(ASoftFallback.Xtn2));
  425. Context.EmitStvec(Op.Rd);
  426. }
  427. private static void EmitSimdMultLdSt(AILEmitterCtx Context, bool IsLoad)
  428. {
  429. AOpCodeSimdMemMult Op = (AOpCodeSimdMemMult)Context.CurrOp;
  430. int Offset = 0;
  431. for (int Rep = 0; Rep < Op.Reps; Rep++)
  432. for (int Elem = 0; Elem < Op.Elems; Elem++)
  433. for (int SElem = 0; SElem < Op.SElems; SElem++)
  434. {
  435. int Rtt = (Op.Rt + Rep + SElem) & 0x1f;
  436. if (IsLoad)
  437. {
  438. Context.EmitLdvec(Rtt);
  439. Context.EmitLdc_I4(Elem);
  440. Context.EmitLdc_I4(Op.Size);
  441. Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
  442. Context.EmitLdint(Op.Rn);
  443. Context.EmitLdc_I8(Offset);
  444. Context.Emit(OpCodes.Add);
  445. EmitReadZxCall(Context, Op.Size);
  446. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.InsertVec));
  447. Context.EmitStvec(Rtt);
  448. if (Op.RegisterSize == ARegisterSize.SIMD64 && Elem == Op.Elems - 1)
  449. {
  450. EmitVectorZeroUpper(Context, Rtt);
  451. }
  452. }
  453. else
  454. {
  455. Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
  456. Context.EmitLdint(Op.Rn);
  457. Context.EmitLdc_I8(Offset);
  458. Context.Emit(OpCodes.Add);
  459. Context.EmitLdvec(Rtt);
  460. Context.EmitLdc_I4(Elem);
  461. Context.EmitLdc_I4(Op.Size);
  462. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.ExtractVec));
  463. EmitWriteCall(Context, Op.Size);
  464. }
  465. Offset += 1 << Op.Size;
  466. }
  467. if (Op.WBack)
  468. {
  469. Context.EmitLdint(Op.Rn);
  470. if (Op.Rm != ARegisters.ZRIndex)
  471. {
  472. Context.EmitLdint(Op.Rm);
  473. }
  474. else
  475. {
  476. Context.EmitLdc_I8(Offset);
  477. }
  478. Context.Emit(OpCodes.Add);
  479. Context.EmitStint(Op.Rn);
  480. }
  481. }
  482. private static void EmitVectorAddv(AILEmitterCtx Context)
  483. {
  484. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  485. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  486. EmitVectorZeroLower(Context, Op.Rd);
  487. EmitVectorZeroUpper(Context, Op.Rd);
  488. Context.EmitLdvec(Op.Rd);
  489. Context.EmitLdc_I4(0);
  490. Context.EmitLdc_I4(Op.Size);
  491. EmitVectorExtractZx(Context, Op.Rn, 0);
  492. for (int Index = 1; Index < (Bytes >> Op.Size); Index++)
  493. {
  494. EmitVectorExtractZx(Context, Op.Rn, Index);
  495. Context.Emit(OpCodes.Add);
  496. }
  497. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.InsertVec));
  498. Context.EmitStvec(Op.Rd);
  499. }
  500. private static void EmitVectorBic(AILEmitterCtx Context)
  501. {
  502. EmitVectorBinaryZx(Context, () =>
  503. {
  504. Context.Emit(OpCodes.Not);
  505. Context.Emit(OpCodes.And);
  506. });
  507. }
  508. private static void EmitVectorBsl(AILEmitterCtx Context)
  509. {
  510. EmitVectorTernaryZx(Context, () =>
  511. {
  512. Context.EmitSttmp();
  513. Context.EmitLdtmp();
  514. Context.Emit(OpCodes.Xor);
  515. Context.Emit(OpCodes.And);
  516. Context.EmitLdtmp();
  517. Context.Emit(OpCodes.Xor);
  518. });
  519. }
  520. private static void EmitVectorMla(AILEmitterCtx Context)
  521. {
  522. EmitVectorTernaryZx(Context, () =>
  523. {
  524. Context.Emit(OpCodes.Mul);
  525. Context.Emit(OpCodes.Add);
  526. });
  527. }
  528. private static void EmitVectorSmax(AILEmitterCtx Context)
  529. {
  530. Type[] Types = new Type[] { typeof(long), typeof(long) };
  531. MethodInfo MthdInfo = typeof(Math).GetMethod(nameof(Math.Max), Types);
  532. EmitVectorBinarySx(Context, () => Context.EmitCall(MthdInfo));
  533. }
  534. private static void EmitVectorSmin(AILEmitterCtx Context)
  535. {
  536. Type[] Types = new Type[] { typeof(long), typeof(long) };
  537. MethodInfo MthdInfo = typeof(Math).GetMethod(nameof(Math.Min), Types);
  538. EmitVectorBinarySx(Context, () => Context.EmitCall(MthdInfo));
  539. }
  540. private static void EmitVectorUshl(AILEmitterCtx Context)
  541. {
  542. //This instruction shifts the value on vector A by the number of bits
  543. //specified on the signed, lower 8 bits of vector B. If the shift value
  544. //is greater or equal to the data size of each lane, then the result is zero.
  545. //Additionally, negative shifts produces right shifts by the negated shift value.
  546. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  547. int MaxShift = 8 << Op.Size;
  548. EmitVectorBinaryZx(Context, () =>
  549. {
  550. AILLabel LblShl = new AILLabel();
  551. AILLabel LblZero = new AILLabel();
  552. AILLabel LblEnd = new AILLabel();
  553. void EmitShift(OpCode ILOp)
  554. {
  555. Context.Emit(OpCodes.Dup);
  556. Context.EmitLdc_I4(MaxShift);
  557. Context.Emit(OpCodes.Bge_S, LblZero);
  558. Context.Emit(ILOp);
  559. Context.Emit(OpCodes.Br_S, LblEnd);
  560. }
  561. Context.Emit(OpCodes.Conv_I1);
  562. Context.Emit(OpCodes.Dup);
  563. Context.EmitLdc_I4(0);
  564. Context.Emit(OpCodes.Bge_S, LblShl);
  565. Context.Emit(OpCodes.Neg);
  566. EmitShift(OpCodes.Shr_Un);
  567. Context.MarkLabel(LblShl);
  568. EmitShift(OpCodes.Shl);
  569. Context.MarkLabel(LblZero);
  570. Context.Emit(OpCodes.Pop);
  571. Context.Emit(OpCodes.Pop);
  572. Context.EmitLdc_I8(0);
  573. Context.MarkLabel(LblEnd);
  574. });
  575. }
  576. private static void EmitVectorUnarySx(AILEmitterCtx Context, OpCode ILOp)
  577. {
  578. EmitVectorUnarySx(Context, () => Context.Emit(ILOp));
  579. }
  580. private static void EmitVectorUnaryZx(AILEmitterCtx Context, OpCode ILOp)
  581. {
  582. EmitVectorUnaryZx(Context, () => Context.Emit(ILOp));
  583. }
  584. private static void EmitVectorBinaryZx(AILEmitterCtx Context, OpCode ILOp)
  585. {
  586. EmitVectorBinaryZx(Context, () => Context.Emit(ILOp));
  587. }
  588. private static void EmitVectorUnarySx(AILEmitterCtx Context, Action Emit)
  589. {
  590. EmitVectorOp(Context, Emit, 1, true);
  591. }
  592. private static void EmitVectorBinarySx(AILEmitterCtx Context, Action Emit)
  593. {
  594. EmitVectorOp(Context, Emit, 2, true);
  595. }
  596. private static void EmitVectorUnaryZx(AILEmitterCtx Context, Action Emit)
  597. {
  598. EmitVectorOp(Context, Emit, 1, false);
  599. }
  600. private static void EmitVectorBinaryZx(AILEmitterCtx Context, Action Emit)
  601. {
  602. EmitVectorOp(Context, Emit, 2, false);
  603. }
  604. private static void EmitVectorTernaryZx(AILEmitterCtx Context, Action Emit)
  605. {
  606. EmitVectorOp(Context, Emit, 3, false);
  607. }
  608. private static void EmitVectorOp(AILEmitterCtx Context, Action Emit, int Opers, bool Signed)
  609. {
  610. if (Opers < 1 || Opers > 3)
  611. {
  612. throw new ArgumentOutOfRangeException(nameof(Opers));
  613. }
  614. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  615. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  616. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  617. {
  618. Context.EmitLdvec(Op.Rd);
  619. Context.EmitLdc_I4(Index);
  620. Context.EmitLdc_I4(Op.Size);
  621. if (Opers == 3)
  622. {
  623. EmitVectorExtract(Context, Op.Rd, Index, Signed);
  624. }
  625. if (Opers >= 1)
  626. {
  627. EmitVectorExtract(Context, Op.Rn, Index, Signed);
  628. }
  629. if (Opers >= 2)
  630. {
  631. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Signed);
  632. }
  633. Emit();
  634. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.InsertVec));
  635. Context.EmitStvec(Op.Rd);
  636. }
  637. if (Op.RegisterSize == ARegisterSize.SIMD64)
  638. {
  639. EmitVectorZeroUpper(Context, Op.Rd);
  640. }
  641. }
  642. private static void EmitVectorCmp(AILEmitterCtx Context, OpCode ILOp)
  643. {
  644. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  645. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  646. ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size));
  647. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  648. {
  649. EmitVectorExtractSx(Context, Op.Rn, Index);
  650. if (Op is AOpCodeSimdReg BinOp)
  651. {
  652. EmitVectorExtractSx(Context, BinOp.Rm, Index);
  653. }
  654. else
  655. {
  656. Context.EmitLdc_I8(0);
  657. }
  658. AILLabel LblTrue = new AILLabel();
  659. AILLabel LblEnd = new AILLabel();
  660. Context.Emit(ILOp, LblTrue);
  661. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, 0);
  662. Context.Emit(OpCodes.Br_S, LblEnd);
  663. Context.MarkLabel(LblTrue);
  664. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, (long)SzMask);
  665. Context.MarkLabel(LblEnd);
  666. }
  667. if (Op.RegisterSize == ARegisterSize.SIMD64)
  668. {
  669. EmitVectorZeroUpper(Context, Op.Rd);
  670. }
  671. }
  672. private static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index)
  673. {
  674. EmitVectorExtract(Context, Reg, Index, true);
  675. }
  676. private static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index)
  677. {
  678. EmitVectorExtract(Context, Reg, Index, false);
  679. }
  680. private static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, bool Signed)
  681. {
  682. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  683. Context.EmitLdvec(Reg);
  684. Context.EmitLdc_I4(Index);
  685. Context.EmitLdc_I4(Op.Size);
  686. ASoftFallback.EmitCall(Context, Signed
  687. ? nameof(ASoftFallback.ExtractSVec)
  688. : nameof(ASoftFallback.ExtractVec));
  689. }
  690. private static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
  691. {
  692. EmitVectorInsert(Context, Rd, 0, 3, 0);
  693. }
  694. private static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
  695. {
  696. EmitVectorInsert(Context, Rd, 1, 3, 0);
  697. }
  698. private static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
  699. {
  700. Context.EmitLdvec(Reg);
  701. Context.EmitLdc_I4(Index);
  702. Context.EmitLdc_I4(Size);
  703. Context.EmitLdc_I8(Value);
  704. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.InsertVec));
  705. Context.EmitStvec(Reg);
  706. }
  707. }
  708. }