InstEmitAluHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection.Emit;
  6. namespace ChocolArm64.Instructions
  7. {
  8. static class InstEmitAluHelper
  9. {
  10. public static void EmitAdcsCCheck(ILEmitterCtx context)
  11. {
  12. // C = (Rd == Rn && CIn) || Rd < Rn
  13. context.EmitSttmp();
  14. context.EmitLdtmp();
  15. context.EmitLdtmp();
  16. EmitAluLoadRn(context);
  17. context.Emit(OpCodes.Ceq);
  18. context.EmitLdflg((int)PState.CBit);
  19. context.Emit(OpCodes.And);
  20. context.EmitLdtmp();
  21. EmitAluLoadRn(context);
  22. context.Emit(OpCodes.Clt_Un);
  23. context.Emit(OpCodes.Or);
  24. context.EmitStflg((int)PState.CBit);
  25. }
  26. public static void EmitAddsCCheck(ILEmitterCtx context)
  27. {
  28. // C = Rd < Rn
  29. context.Emit(OpCodes.Dup);
  30. EmitAluLoadRn(context);
  31. context.Emit(OpCodes.Clt_Un);
  32. context.EmitStflg((int)PState.CBit);
  33. }
  34. public static void EmitAddsVCheck(ILEmitterCtx context)
  35. {
  36. // V = (Rd ^ Rn) & ~(Rn ^ Rm) < 0
  37. context.Emit(OpCodes.Dup);
  38. EmitAluLoadRn(context);
  39. context.Emit(OpCodes.Xor);
  40. EmitAluLoadOpers(context);
  41. context.Emit(OpCodes.Xor);
  42. context.Emit(OpCodes.Not);
  43. context.Emit(OpCodes.And);
  44. context.EmitLdc_I(0);
  45. context.Emit(OpCodes.Clt);
  46. context.EmitStflg((int)PState.VBit);
  47. }
  48. public static void EmitSbcsCCheck(ILEmitterCtx context)
  49. {
  50. // C = (Rn == Rm && CIn) || Rn > Rm
  51. EmitAluLoadOpers(context);
  52. context.Emit(OpCodes.Ceq);
  53. context.EmitLdflg((int)PState.CBit);
  54. context.Emit(OpCodes.And);
  55. EmitAluLoadOpers(context);
  56. context.Emit(OpCodes.Cgt_Un);
  57. context.Emit(OpCodes.Or);
  58. context.EmitStflg((int)PState.CBit);
  59. }
  60. public static void EmitSubsCCheck(ILEmitterCtx context)
  61. {
  62. // C = Rn == Rm || Rn > Rm = !(Rn < Rm)
  63. EmitAluLoadOpers(context);
  64. context.Emit(OpCodes.Clt_Un);
  65. context.EmitLdc_I4(1);
  66. context.Emit(OpCodes.Xor);
  67. context.EmitStflg((int)PState.CBit);
  68. }
  69. public static void EmitSubsVCheck(ILEmitterCtx context)
  70. {
  71. // V = (Rd ^ Rn) & (Rn ^ Rm) < 0
  72. context.Emit(OpCodes.Dup);
  73. EmitAluLoadRn(context);
  74. context.Emit(OpCodes.Xor);
  75. EmitAluLoadOpers(context);
  76. context.Emit(OpCodes.Xor);
  77. context.Emit(OpCodes.And);
  78. context.EmitLdc_I(0);
  79. context.Emit(OpCodes.Clt);
  80. context.EmitStflg((int)PState.VBit);
  81. }
  82. public static void EmitAluLoadRm(ILEmitterCtx context)
  83. {
  84. if (context.CurrOp is IOpCodeAluRs64 op)
  85. {
  86. context.EmitLdintzr(op.Rm);
  87. }
  88. else if (context.CurrOp is OpCode32AluRsImm op32)
  89. {
  90. InstEmit32Helper.EmitLoadFromRegister(context, op32.Rm);
  91. }
  92. else
  93. {
  94. throw new InvalidOperationException();
  95. }
  96. }
  97. public static void EmitAluLoadOpers(ILEmitterCtx context, bool setCarry = true)
  98. {
  99. EmitAluLoadRn(context);
  100. EmitAluLoadOper2(context, setCarry);
  101. }
  102. public static void EmitAluLoadRn(ILEmitterCtx context)
  103. {
  104. if (context.CurrOp is IOpCodeAlu64 op)
  105. {
  106. if (op.DataOp == DataOp.Logical || op is IOpCodeAluRs64)
  107. {
  108. context.EmitLdintzr(op.Rn);
  109. }
  110. else
  111. {
  112. context.EmitLdint(op.Rn);
  113. }
  114. }
  115. else if (context.CurrOp is IOpCode32Alu op32)
  116. {
  117. InstEmit32Helper.EmitLoadFromRegister(context, op32.Rn);
  118. }
  119. else
  120. {
  121. throw new InvalidOperationException();
  122. }
  123. }
  124. public static void EmitAluLoadOper2(ILEmitterCtx context, bool setCarry = true)
  125. {
  126. switch (context.CurrOp)
  127. {
  128. // ARM32.
  129. case OpCode32AluImm op:
  130. context.EmitLdc_I4(op.Imm);
  131. if (op.SetFlags && op.IsRotated)
  132. {
  133. context.EmitLdc_I4((int)((uint)op.Imm >> 31));
  134. context.EmitStflg((int)PState.CBit);
  135. }
  136. break;
  137. case OpCode32AluRsImm op:
  138. EmitLoadRmShiftedByImmediate(context, op, setCarry);
  139. break;
  140. case OpCodeT16AluImm8 op:
  141. context.EmitLdc_I4(op.Imm);
  142. break;
  143. // ARM64.
  144. case IOpCodeAluImm64 op:
  145. context.EmitLdc_I(op.Imm);
  146. break;
  147. case IOpCodeAluRs64 op:
  148. context.EmitLdintzr(op.Rm);
  149. switch (op.ShiftType)
  150. {
  151. case ShiftType.Lsl: context.EmitLsl(op.Shift); break;
  152. case ShiftType.Lsr: context.EmitLsr(op.Shift); break;
  153. case ShiftType.Asr: context.EmitAsr(op.Shift); break;
  154. case ShiftType.Ror: context.EmitRor(op.Shift); break;
  155. }
  156. break;
  157. case IOpCodeAluRx64 op:
  158. context.EmitLdintzr(op.Rm);
  159. context.EmitCast(op.IntType);
  160. context.EmitLsl(op.Shift);
  161. break;
  162. default: throw new InvalidOperationException();
  163. }
  164. }
  165. public static void EmitSetNzcv(ILEmitterCtx context)
  166. {
  167. context.Emit(OpCodes.Dup);
  168. context.Emit(OpCodes.Ldc_I4_1);
  169. context.Emit(OpCodes.And);
  170. context.EmitStflg((int)PState.VBit);
  171. context.Emit(OpCodes.Ldc_I4_1);
  172. context.Emit(OpCodes.Shr);
  173. context.Emit(OpCodes.Dup);
  174. context.Emit(OpCodes.Ldc_I4_1);
  175. context.Emit(OpCodes.And);
  176. context.EmitStflg((int)PState.CBit);
  177. context.Emit(OpCodes.Ldc_I4_1);
  178. context.Emit(OpCodes.Shr);
  179. context.Emit(OpCodes.Dup);
  180. context.Emit(OpCodes.Ldc_I4_1);
  181. context.Emit(OpCodes.And);
  182. context.EmitStflg((int)PState.ZBit);
  183. context.Emit(OpCodes.Ldc_I4_1);
  184. context.Emit(OpCodes.Shr);
  185. context.Emit(OpCodes.Ldc_I4_1);
  186. context.Emit(OpCodes.And);
  187. context.EmitStflg((int)PState.NBit);
  188. }
  189. // ARM32 helpers.
  190. private static void EmitLoadRmShiftedByImmediate(ILEmitterCtx context, OpCode32AluRsImm op, bool setCarry)
  191. {
  192. int shift = op.Imm;
  193. if (shift == 0)
  194. {
  195. switch (op.ShiftType)
  196. {
  197. case ShiftType.Lsr: shift = 32; break;
  198. case ShiftType.Asr: shift = 32; break;
  199. case ShiftType.Ror: shift = 1; break;
  200. }
  201. }
  202. context.EmitLdint(op.Rm);
  203. if (shift != 0)
  204. {
  205. setCarry &= op.SetFlags;
  206. switch (op.ShiftType)
  207. {
  208. case ShiftType.Lsl: EmitLslC(context, setCarry, shift); break;
  209. case ShiftType.Lsr: EmitLsrC(context, setCarry, shift); break;
  210. case ShiftType.Asr: EmitAsrC(context, setCarry, shift); break;
  211. case ShiftType.Ror:
  212. if (op.Imm != 0)
  213. {
  214. EmitRorC(context, setCarry, shift);
  215. }
  216. else
  217. {
  218. EmitRrxC(context, setCarry);
  219. }
  220. break;
  221. }
  222. }
  223. }
  224. private static void EmitLslC(ILEmitterCtx context, bool setCarry, int shift)
  225. {
  226. if ((uint)shift > 32)
  227. {
  228. EmitShiftByMoreThan32(context, setCarry);
  229. }
  230. else if (shift == 32)
  231. {
  232. if (setCarry)
  233. {
  234. context.EmitLdc_I4(1);
  235. context.Emit(OpCodes.And);
  236. context.EmitStflg((int)PState.CBit);
  237. }
  238. else
  239. {
  240. context.Emit(OpCodes.Pop);
  241. }
  242. context.EmitLdc_I4(0);
  243. }
  244. else
  245. {
  246. if (setCarry)
  247. {
  248. context.Emit(OpCodes.Dup);
  249. context.EmitLsr(32 - shift);
  250. context.EmitLdc_I4(1);
  251. context.Emit(OpCodes.And);
  252. context.EmitStflg((int)PState.CBit);
  253. }
  254. context.EmitLsl(shift);
  255. }
  256. }
  257. private static void EmitLsrC(ILEmitterCtx context, bool setCarry, int shift)
  258. {
  259. if ((uint)shift > 32)
  260. {
  261. EmitShiftByMoreThan32(context, setCarry);
  262. }
  263. else if (shift == 32)
  264. {
  265. if (setCarry)
  266. {
  267. context.EmitLsr(31);
  268. context.EmitStflg((int)PState.CBit);
  269. }
  270. else
  271. {
  272. context.Emit(OpCodes.Pop);
  273. }
  274. context.EmitLdc_I4(0);
  275. }
  276. else
  277. {
  278. context.Emit(OpCodes.Dup);
  279. context.EmitLsr(shift - 1);
  280. context.EmitLdc_I4(1);
  281. context.Emit(OpCodes.And);
  282. context.EmitStflg((int)PState.CBit);
  283. context.EmitLsr(shift);
  284. }
  285. }
  286. private static void EmitShiftByMoreThan32(ILEmitterCtx context, bool setCarry)
  287. {
  288. context.Emit(OpCodes.Pop);
  289. context.EmitLdc_I4(0);
  290. if (setCarry)
  291. {
  292. context.Emit(OpCodes.Dup);
  293. context.EmitStflg((int)PState.CBit);
  294. }
  295. }
  296. private static void EmitAsrC(ILEmitterCtx context, bool setCarry, int shift)
  297. {
  298. if ((uint)shift >= 32)
  299. {
  300. context.EmitAsr(31);
  301. if (setCarry)
  302. {
  303. context.Emit(OpCodes.Dup);
  304. context.EmitLdc_I4(1);
  305. context.Emit(OpCodes.And);
  306. context.EmitStflg((int)PState.CBit);
  307. }
  308. }
  309. else
  310. {
  311. if (setCarry)
  312. {
  313. context.Emit(OpCodes.Dup);
  314. context.EmitLsr(shift - 1);
  315. context.EmitLdc_I4(1);
  316. context.Emit(OpCodes.And);
  317. context.EmitStflg((int)PState.CBit);
  318. }
  319. context.EmitAsr(shift);
  320. }
  321. }
  322. private static void EmitRorC(ILEmitterCtx context, bool setCarry, int shift)
  323. {
  324. shift &= 0x1f;
  325. context.EmitRor(shift);
  326. if (setCarry)
  327. {
  328. context.Emit(OpCodes.Dup);
  329. context.EmitLsr(31);
  330. context.EmitStflg((int)PState.CBit);
  331. }
  332. }
  333. private static void EmitRrxC(ILEmitterCtx context, bool setCarry)
  334. {
  335. // Rotate right by 1 with carry.
  336. if (setCarry)
  337. {
  338. context.Emit(OpCodes.Dup);
  339. context.EmitLdc_I4(1);
  340. context.Emit(OpCodes.And);
  341. context.EmitSttmp();
  342. }
  343. context.EmitLsr(1);
  344. context.EmitLdflg((int)PState.CBit);
  345. context.EmitLsl(31);
  346. context.Emit(OpCodes.Or);
  347. if (setCarry)
  348. {
  349. context.EmitLdtmp();
  350. context.EmitStflg((int)PState.CBit);
  351. }
  352. }
  353. }
  354. }