AInstEmitSimd.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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 Sshl_V(AILEmitterCtx Context) => EmitVectorSshl(Context);
  274. public static void Sshll_V(AILEmitterCtx Context)
  275. {
  276. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  277. Context.EmitLdvec(Op.Rn);
  278. Context.EmitLdc_I4(Op.Imm - (8 << Op.Size));
  279. Context.EmitLdc_I4(Op.Size);
  280. ASoftFallback.EmitCall(Context,
  281. nameof(ASoftFallback.Sshll),
  282. nameof(ASoftFallback.Sshll2));
  283. Context.EmitStvec(Op.Rd);
  284. }
  285. public static void Sshr_V(AILEmitterCtx Context)
  286. {
  287. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  288. Context.EmitLdvec(Op.Rn);
  289. Context.EmitLdc_I4((8 << (Op.Size + 1)) - Op.Imm);
  290. Context.EmitLdc_I4(Op.Size);
  291. ASoftFallback.EmitCall(Context,
  292. nameof(ASoftFallback.Sshr64),
  293. nameof(ASoftFallback.Sshr128));
  294. Context.EmitStvec(Op.Rd);
  295. }
  296. public static void St__V(AILEmitterCtx Context) => EmitSimdMultLdSt(Context, IsLoad: false);
  297. public static void Sub_V(AILEmitterCtx Context) => EmitVectorBinaryZx(Context, OpCodes.Sub);
  298. public static void Tbl_V(AILEmitterCtx Context)
  299. {
  300. AOpCodeSimdTbl Op = (AOpCodeSimdTbl)Context.CurrOp;
  301. Context.EmitLdvec(Op.Rm);
  302. for (int Index = 0; Index < Op.Size; Index++)
  303. {
  304. Context.EmitLdvec((Op.Rn + Index) & 0x1f);
  305. }
  306. switch (Op.Size)
  307. {
  308. case 1: ASoftFallback.EmitCall(Context,
  309. nameof(ASoftFallback.Tbl1_V64),
  310. nameof(ASoftFallback.Tbl1_V128)); break;
  311. case 2: ASoftFallback.EmitCall(Context,
  312. nameof(ASoftFallback.Tbl2_V64),
  313. nameof(ASoftFallback.Tbl2_V128)); break;
  314. case 3: ASoftFallback.EmitCall(Context,
  315. nameof(ASoftFallback.Tbl3_V64),
  316. nameof(ASoftFallback.Tbl3_V128)); break;
  317. case 4: ASoftFallback.EmitCall(Context,
  318. nameof(ASoftFallback.Tbl4_V64),
  319. nameof(ASoftFallback.Tbl4_V128)); break;
  320. default: throw new InvalidOperationException();
  321. }
  322. Context.EmitStvec(Op.Rd);
  323. }
  324. public static void Uaddlv_V(AILEmitterCtx Context)
  325. {
  326. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  327. Context.EmitLdvec(Op.Rn);
  328. Context.EmitLdc_I4(Op.Size);
  329. ASoftFallback.EmitCall(Context,
  330. nameof(ASoftFallback.Uaddlv64),
  331. nameof(ASoftFallback.Uaddlv128));
  332. Context.EmitStvec(Op.Rd);
  333. }
  334. public static void Uaddw_V(AILEmitterCtx Context)
  335. {
  336. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  337. Context.EmitLdvec(Op.Rn);
  338. Context.EmitLdvec(Op.Rm);
  339. Context.EmitLdc_I4(Op.Size);
  340. ASoftFallback.EmitCall(Context,
  341. nameof(ASoftFallback.Uaddw),
  342. nameof(ASoftFallback.Uaddw2));
  343. Context.EmitStvec(Op.Rd);
  344. }
  345. public static void Ucvtf_V(AILEmitterCtx Context)
  346. {
  347. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  348. Context.EmitLdvec(Op.Rn);
  349. if (Op.Size == 0)
  350. {
  351. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Ucvtf_V_F));
  352. }
  353. else if (Op.Size == 1)
  354. {
  355. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.Ucvtf_V_D));
  356. }
  357. else
  358. {
  359. throw new InvalidOperationException();
  360. }
  361. Context.EmitStvec(Op.Rd);
  362. }
  363. public static void Umov_S(AILEmitterCtx Context)
  364. {
  365. AOpCodeSimdIns Op = (AOpCodeSimdIns)Context.CurrOp;
  366. Context.EmitLdvec(Op.Rn);
  367. Context.EmitLdc_I4(Op.DstIndex);
  368. Context.EmitLdc_I4(Op.Size);
  369. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.ExtractVec));
  370. Context.EmitStintzr(Op.Rd);
  371. }
  372. public static void Ushl_V(AILEmitterCtx Context) => EmitVectorUshl(Context);
  373. public static void Ushll_V(AILEmitterCtx Context)
  374. {
  375. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  376. Context.EmitLdvec(Op.Rn);
  377. Context.EmitLdc_I4(Op.Imm - (8 << Op.Size));
  378. Context.EmitLdc_I4(Op.Size);
  379. ASoftFallback.EmitCall(Context,
  380. nameof(ASoftFallback.Ushll),
  381. nameof(ASoftFallback.Ushll2));
  382. Context.EmitStvec(Op.Rd);
  383. }
  384. public static void Ushr_V(AILEmitterCtx Context)
  385. {
  386. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  387. Context.EmitLdvec(Op.Rn);
  388. Context.EmitLdc_I4((8 << (Op.Size + 1)) - Op.Imm);
  389. Context.EmitLdc_I4(Op.Size);
  390. ASoftFallback.EmitCall(Context,
  391. nameof(ASoftFallback.Ushr64),
  392. nameof(ASoftFallback.Ushr128));
  393. Context.EmitStvec(Op.Rd);
  394. }
  395. public static void Usra_V(AILEmitterCtx Context)
  396. {
  397. AOpCodeSimdShImm Op = (AOpCodeSimdShImm)Context.CurrOp;
  398. Context.EmitLdvec(Op.Rd);
  399. Context.EmitLdvec(Op.Rn);
  400. Context.EmitLdc_I4((8 << (Op.Size + 1)) - Op.Imm);
  401. Context.EmitLdc_I4(Op.Size);
  402. ASoftFallback.EmitCall(Context,
  403. nameof(ASoftFallback.Usra64),
  404. nameof(ASoftFallback.Usra128));
  405. Context.EmitStvec(Op.Rd);
  406. }
  407. public static void Uzp1_V(AILEmitterCtx Context)
  408. {
  409. AOpCodeSimdReg Op = (AOpCodeSimdReg)Context.CurrOp;
  410. Context.EmitLdvec(Op.Rn);
  411. Context.EmitLdvec(Op.Rm);
  412. Context.EmitLdc_I4(Op.Size);
  413. ASoftFallback.EmitCall(Context,
  414. nameof(ASoftFallback.Uzp1_V64),
  415. nameof(ASoftFallback.Uzp1_V128));
  416. Context.EmitStvec(Op.Rd);
  417. }
  418. public static void Xtn_V(AILEmitterCtx Context)
  419. {
  420. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  421. Context.EmitLdvec(Op.Rn);
  422. Context.EmitLdc_I4(Op.Size);
  423. ASoftFallback.EmitCall(Context,
  424. nameof(ASoftFallback.Xtn),
  425. nameof(ASoftFallback.Xtn2));
  426. Context.EmitStvec(Op.Rd);
  427. }
  428. private static void EmitSimdMultLdSt(AILEmitterCtx Context, bool IsLoad)
  429. {
  430. AOpCodeSimdMemMult Op = (AOpCodeSimdMemMult)Context.CurrOp;
  431. int Offset = 0;
  432. for (int Rep = 0; Rep < Op.Reps; Rep++)
  433. for (int Elem = 0; Elem < Op.Elems; Elem++)
  434. for (int SElem = 0; SElem < Op.SElems; SElem++)
  435. {
  436. int Rtt = (Op.Rt + Rep + SElem) & 0x1f;
  437. if (IsLoad)
  438. {
  439. Context.EmitLdvec(Rtt);
  440. Context.EmitLdc_I4(Elem);
  441. Context.EmitLdc_I4(Op.Size);
  442. Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
  443. Context.EmitLdint(Op.Rn);
  444. Context.EmitLdc_I8(Offset);
  445. Context.Emit(OpCodes.Add);
  446. EmitReadZxCall(Context, Op.Size);
  447. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.InsertVec));
  448. Context.EmitStvec(Rtt);
  449. if (Op.RegisterSize == ARegisterSize.SIMD64 && Elem == Op.Elems - 1)
  450. {
  451. EmitVectorZeroUpper(Context, Rtt);
  452. }
  453. }
  454. else
  455. {
  456. Context.EmitLdarg(ATranslatedSub.MemoryArgIdx);
  457. Context.EmitLdint(Op.Rn);
  458. Context.EmitLdc_I8(Offset);
  459. Context.Emit(OpCodes.Add);
  460. Context.EmitLdvec(Rtt);
  461. Context.EmitLdc_I4(Elem);
  462. Context.EmitLdc_I4(Op.Size);
  463. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.ExtractVec));
  464. EmitWriteCall(Context, Op.Size);
  465. }
  466. Offset += 1 << Op.Size;
  467. }
  468. if (Op.WBack)
  469. {
  470. Context.EmitLdint(Op.Rn);
  471. if (Op.Rm != ARegisters.ZRIndex)
  472. {
  473. Context.EmitLdint(Op.Rm);
  474. }
  475. else
  476. {
  477. Context.EmitLdc_I8(Offset);
  478. }
  479. Context.Emit(OpCodes.Add);
  480. Context.EmitStint(Op.Rn);
  481. }
  482. }
  483. private static void EmitVectorAddv(AILEmitterCtx Context)
  484. {
  485. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  486. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  487. EmitVectorZeroLower(Context, Op.Rd);
  488. EmitVectorZeroUpper(Context, Op.Rd);
  489. Context.EmitLdvec(Op.Rd);
  490. Context.EmitLdc_I4(0);
  491. Context.EmitLdc_I4(Op.Size);
  492. EmitVectorExtractZx(Context, Op.Rn, 0);
  493. for (int Index = 1; Index < (Bytes >> Op.Size); Index++)
  494. {
  495. EmitVectorExtractZx(Context, Op.Rn, Index);
  496. Context.Emit(OpCodes.Add);
  497. }
  498. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.InsertVec));
  499. Context.EmitStvec(Op.Rd);
  500. }
  501. private static void EmitVectorBic(AILEmitterCtx Context)
  502. {
  503. EmitVectorBinaryZx(Context, () =>
  504. {
  505. Context.Emit(OpCodes.Not);
  506. Context.Emit(OpCodes.And);
  507. });
  508. }
  509. private static void EmitVectorBsl(AILEmitterCtx Context)
  510. {
  511. EmitVectorTernaryZx(Context, () =>
  512. {
  513. Context.EmitSttmp();
  514. Context.EmitLdtmp();
  515. Context.Emit(OpCodes.Xor);
  516. Context.Emit(OpCodes.And);
  517. Context.EmitLdtmp();
  518. Context.Emit(OpCodes.Xor);
  519. });
  520. }
  521. private static void EmitVectorMla(AILEmitterCtx Context)
  522. {
  523. EmitVectorTernaryZx(Context, () =>
  524. {
  525. Context.Emit(OpCodes.Mul);
  526. Context.Emit(OpCodes.Add);
  527. });
  528. }
  529. private static void EmitVectorSmax(AILEmitterCtx Context)
  530. {
  531. Type[] Types = new Type[] { typeof(long), typeof(long) };
  532. MethodInfo MthdInfo = typeof(Math).GetMethod(nameof(Math.Max), Types);
  533. EmitVectorBinarySx(Context, () => Context.EmitCall(MthdInfo));
  534. }
  535. private static void EmitVectorSmin(AILEmitterCtx Context)
  536. {
  537. Type[] Types = new Type[] { typeof(long), typeof(long) };
  538. MethodInfo MthdInfo = typeof(Math).GetMethod(nameof(Math.Min), Types);
  539. EmitVectorBinarySx(Context, () => Context.EmitCall(MthdInfo));
  540. }
  541. private static void EmitVectorSshl(AILEmitterCtx Context) => EmitVectorShl(Context, true);
  542. private static void EmitVectorUshl(AILEmitterCtx Context) => EmitVectorShl(Context, false);
  543. private static void EmitVectorShl(AILEmitterCtx Context, bool Signed)
  544. {
  545. //This instruction shifts the value on vector A by the number of bits
  546. //specified on the signed, lower 8 bits of vector B. If the shift value
  547. //is greater or equal to the data size of each lane, then the result is zero.
  548. //Additionally, negative shifts produces right shifts by the negated shift value.
  549. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  550. int MaxShift = 8 << Op.Size;
  551. EmitVectorBinaryZx(Context, () =>
  552. {
  553. AILLabel LblShl = new AILLabel();
  554. AILLabel LblZero = new AILLabel();
  555. AILLabel LblEnd = new AILLabel();
  556. void EmitShift(OpCode ILOp)
  557. {
  558. Context.Emit(OpCodes.Dup);
  559. Context.EmitLdc_I4(MaxShift);
  560. Context.Emit(OpCodes.Bge_S, LblZero);
  561. Context.Emit(ILOp);
  562. Context.Emit(OpCodes.Br_S, LblEnd);
  563. }
  564. Context.Emit(OpCodes.Conv_I1);
  565. Context.Emit(OpCodes.Dup);
  566. Context.EmitLdc_I4(0);
  567. Context.Emit(OpCodes.Bge_S, LblShl);
  568. Context.Emit(OpCodes.Neg);
  569. EmitShift(Signed
  570. ? OpCodes.Shr
  571. : OpCodes.Shr_Un);
  572. Context.MarkLabel(LblShl);
  573. EmitShift(OpCodes.Shl);
  574. Context.MarkLabel(LblZero);
  575. Context.Emit(OpCodes.Pop);
  576. Context.Emit(OpCodes.Pop);
  577. Context.EmitLdc_I8(0);
  578. Context.MarkLabel(LblEnd);
  579. });
  580. }
  581. private static void EmitVectorUnarySx(AILEmitterCtx Context, OpCode ILOp)
  582. {
  583. EmitVectorUnarySx(Context, () => Context.Emit(ILOp));
  584. }
  585. private static void EmitVectorUnaryZx(AILEmitterCtx Context, OpCode ILOp)
  586. {
  587. EmitVectorUnaryZx(Context, () => Context.Emit(ILOp));
  588. }
  589. private static void EmitVectorBinaryZx(AILEmitterCtx Context, OpCode ILOp)
  590. {
  591. EmitVectorBinaryZx(Context, () => Context.Emit(ILOp));
  592. }
  593. private static void EmitVectorUnarySx(AILEmitterCtx Context, Action Emit)
  594. {
  595. EmitVectorOp(Context, Emit, 1, true);
  596. }
  597. private static void EmitVectorBinarySx(AILEmitterCtx Context, Action Emit)
  598. {
  599. EmitVectorOp(Context, Emit, 2, true);
  600. }
  601. private static void EmitVectorUnaryZx(AILEmitterCtx Context, Action Emit)
  602. {
  603. EmitVectorOp(Context, Emit, 1, false);
  604. }
  605. private static void EmitVectorBinaryZx(AILEmitterCtx Context, Action Emit)
  606. {
  607. EmitVectorOp(Context, Emit, 2, false);
  608. }
  609. private static void EmitVectorTernaryZx(AILEmitterCtx Context, Action Emit)
  610. {
  611. EmitVectorOp(Context, Emit, 3, false);
  612. }
  613. private static void EmitVectorOp(AILEmitterCtx Context, Action Emit, int Opers, bool Signed)
  614. {
  615. if (Opers < 1 || Opers > 3)
  616. {
  617. throw new ArgumentOutOfRangeException(nameof(Opers));
  618. }
  619. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  620. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  621. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  622. {
  623. Context.EmitLdvec(Op.Rd);
  624. Context.EmitLdc_I4(Index);
  625. Context.EmitLdc_I4(Op.Size);
  626. if (Opers == 3)
  627. {
  628. EmitVectorExtract(Context, Op.Rd, Index, Signed);
  629. }
  630. if (Opers >= 1)
  631. {
  632. EmitVectorExtract(Context, Op.Rn, Index, Signed);
  633. }
  634. if (Opers >= 2)
  635. {
  636. EmitVectorExtract(Context, ((AOpCodeSimdReg)Op).Rm, Index, Signed);
  637. }
  638. Emit();
  639. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.InsertVec));
  640. Context.EmitStvec(Op.Rd);
  641. }
  642. if (Op.RegisterSize == ARegisterSize.SIMD64)
  643. {
  644. EmitVectorZeroUpper(Context, Op.Rd);
  645. }
  646. }
  647. private static void EmitVectorCmp(AILEmitterCtx Context, OpCode ILOp)
  648. {
  649. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  650. int Bytes = Context.CurrOp.GetBitsCount() >> 3;
  651. ulong SzMask = ulong.MaxValue >> (64 - (8 << Op.Size));
  652. for (int Index = 0; Index < (Bytes >> Op.Size); Index++)
  653. {
  654. EmitVectorExtractSx(Context, Op.Rn, Index);
  655. if (Op is AOpCodeSimdReg BinOp)
  656. {
  657. EmitVectorExtractSx(Context, BinOp.Rm, Index);
  658. }
  659. else
  660. {
  661. Context.EmitLdc_I8(0);
  662. }
  663. AILLabel LblTrue = new AILLabel();
  664. AILLabel LblEnd = new AILLabel();
  665. Context.Emit(ILOp, LblTrue);
  666. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, 0);
  667. Context.Emit(OpCodes.Br_S, LblEnd);
  668. Context.MarkLabel(LblTrue);
  669. EmitVectorInsert(Context, Op.Rd, Index, Op.Size, (long)SzMask);
  670. Context.MarkLabel(LblEnd);
  671. }
  672. if (Op.RegisterSize == ARegisterSize.SIMD64)
  673. {
  674. EmitVectorZeroUpper(Context, Op.Rd);
  675. }
  676. }
  677. private static void EmitVectorExtractSx(AILEmitterCtx Context, int Reg, int Index)
  678. {
  679. EmitVectorExtract(Context, Reg, Index, true);
  680. }
  681. private static void EmitVectorExtractZx(AILEmitterCtx Context, int Reg, int Index)
  682. {
  683. EmitVectorExtract(Context, Reg, Index, false);
  684. }
  685. private static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, bool Signed)
  686. {
  687. AOpCodeSimd Op = (AOpCodeSimd)Context.CurrOp;
  688. Context.EmitLdvec(Reg);
  689. Context.EmitLdc_I4(Index);
  690. Context.EmitLdc_I4(Op.Size);
  691. ASoftFallback.EmitCall(Context, Signed
  692. ? nameof(ASoftFallback.ExtractSVec)
  693. : nameof(ASoftFallback.ExtractVec));
  694. }
  695. private static void EmitVectorZeroLower(AILEmitterCtx Context, int Rd)
  696. {
  697. EmitVectorInsert(Context, Rd, 0, 3, 0);
  698. }
  699. private static void EmitVectorZeroUpper(AILEmitterCtx Context, int Rd)
  700. {
  701. EmitVectorInsert(Context, Rd, 1, 3, 0);
  702. }
  703. private static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
  704. {
  705. Context.EmitLdvec(Reg);
  706. Context.EmitLdc_I4(Index);
  707. Context.EmitLdc_I4(Size);
  708. Context.EmitLdc_I8(Value);
  709. ASoftFallback.EmitCall(Context, nameof(ASoftFallback.InsertVec));
  710. Context.EmitStvec(Reg);
  711. }
  712. }
  713. }