InstEmitAlu32.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using static ARMeilleure.Instructions.InstEmitAluHelper;
  6. using static ARMeilleure.Instructions.InstEmitHelper;
  7. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  8. namespace ARMeilleure.Instructions
  9. {
  10. static partial class InstEmit32
  11. {
  12. public static void Add(ArmEmitterContext context)
  13. {
  14. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  15. Operand n = GetAluN(context);
  16. Operand m = GetAluM(context, setCarry: false);
  17. Operand res = context.Add(n, m);
  18. if (op.SetFlags)
  19. {
  20. EmitNZFlagsCheck(context, res);
  21. EmitAddsCCheck(context, n, res);
  22. EmitAddsVCheck(context, n, m, res);
  23. }
  24. EmitAluStore(context, res);
  25. }
  26. public static void Adc(ArmEmitterContext context)
  27. {
  28. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  29. Operand n = GetAluN(context);
  30. Operand m = GetAluM(context, setCarry: false);
  31. Operand res = context.Add(n, m);
  32. Operand carry = GetFlag(PState.CFlag);
  33. res = context.Add(res, carry);
  34. if (op.SetFlags)
  35. {
  36. EmitNZFlagsCheck(context, res);
  37. EmitAdcsCCheck(context, n, res);
  38. EmitAddsVCheck(context, n, m, res);
  39. }
  40. EmitAluStore(context, res);
  41. }
  42. public static void And(ArmEmitterContext context)
  43. {
  44. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  45. Operand n = GetAluN(context);
  46. Operand m = GetAluM(context);
  47. Operand res = context.BitwiseAnd(n, m);
  48. if (op.SetFlags)
  49. {
  50. EmitNZFlagsCheck(context, res);
  51. }
  52. EmitAluStore(context, res);
  53. }
  54. public static void Bfc(ArmEmitterContext context)
  55. {
  56. OpCode32AluBf op = (OpCode32AluBf)context.CurrOp;
  57. Operand d = GetIntA32(context, op.Rd);
  58. Operand res = context.BitwiseAnd(d, Const(~op.DestMask));
  59. SetIntA32(context, op.Rd, res);
  60. }
  61. public static void Bfi(ArmEmitterContext context)
  62. {
  63. OpCode32AluBf op = (OpCode32AluBf)context.CurrOp;
  64. Operand n = GetIntA32(context, op.Rn);
  65. Operand d = GetIntA32(context, op.Rd);
  66. Operand part = context.BitwiseAnd(n, Const(op.SourceMask));
  67. if (op.Lsb != 0)
  68. {
  69. part = context.ShiftLeft(part, Const(op.Lsb));
  70. }
  71. Operand res = context.BitwiseAnd(d, Const(~op.DestMask));
  72. res = context.BitwiseOr(res, context.BitwiseAnd(part, Const(op.DestMask)));
  73. SetIntA32(context, op.Rd, res);
  74. }
  75. public static void Bic(ArmEmitterContext context)
  76. {
  77. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  78. Operand n = GetAluN(context);
  79. Operand m = GetAluM(context);
  80. Operand res = context.BitwiseAnd(n, context.BitwiseNot(m));
  81. if (op.SetFlags)
  82. {
  83. EmitNZFlagsCheck(context, res);
  84. }
  85. EmitAluStore(context, res);
  86. }
  87. public static void Clz(ArmEmitterContext context)
  88. {
  89. Operand m = GetAluM(context, setCarry: false);
  90. Operand res = context.CountLeadingZeros(m);
  91. EmitAluStore(context, res);
  92. }
  93. public static void Cmp(ArmEmitterContext context)
  94. {
  95. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  96. Operand n = GetAluN(context);
  97. Operand m = GetAluM(context, setCarry: false);
  98. Operand res = context.Subtract(n, m);
  99. EmitNZFlagsCheck(context, res);
  100. EmitSubsCCheck(context, n, res);
  101. EmitSubsVCheck(context, n, m, res);
  102. }
  103. public static void Cmn(ArmEmitterContext context)
  104. {
  105. Operand n = GetAluN(context);
  106. Operand m = GetAluM(context, setCarry: false);
  107. Operand res = context.Add(n, m);
  108. EmitNZFlagsCheck(context, res);
  109. EmitAddsCCheck(context, n, res);
  110. EmitAddsVCheck(context, n, m, res);
  111. }
  112. public static void Eor(ArmEmitterContext context)
  113. {
  114. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  115. Operand n = GetAluN(context);
  116. Operand m = GetAluM(context);
  117. Operand res = context.BitwiseExclusiveOr(n, m);
  118. if (op.SetFlags)
  119. {
  120. EmitNZFlagsCheck(context, res);
  121. }
  122. EmitAluStore(context, res);
  123. }
  124. public static void Mov(ArmEmitterContext context)
  125. {
  126. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  127. Operand m = GetAluM(context);
  128. if (op.SetFlags)
  129. {
  130. EmitNZFlagsCheck(context, m);
  131. }
  132. EmitAluStore(context, m);
  133. }
  134. public static void Movt(ArmEmitterContext context)
  135. {
  136. OpCode32AluImm16 op = (OpCode32AluImm16)context.CurrOp;
  137. Operand d = GetIntA32(context, op.Rd);
  138. Operand imm = Const(op.Immediate << 16); // Immeditate value as top halfword.
  139. Operand res = context.BitwiseAnd(d, Const(0x0000ffff));
  140. res = context.BitwiseOr(res, imm);
  141. EmitAluStore(context, res);
  142. }
  143. public static void Mul(ArmEmitterContext context)
  144. {
  145. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  146. Operand n = GetAluN(context);
  147. Operand m = GetAluM(context);
  148. Operand res = context.Multiply(n, m);
  149. if (op.SetFlags)
  150. {
  151. EmitNZFlagsCheck(context, res);
  152. }
  153. EmitAluStore(context, res);
  154. }
  155. public static void Mvn(ArmEmitterContext context)
  156. {
  157. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  158. Operand m = GetAluM(context);
  159. Operand res = context.BitwiseNot(m);
  160. if (op.SetFlags)
  161. {
  162. EmitNZFlagsCheck(context, res);
  163. }
  164. EmitAluStore(context, res);
  165. }
  166. public static void Orr(ArmEmitterContext context)
  167. {
  168. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  169. Operand n = GetAluN(context);
  170. Operand m = GetAluM(context);
  171. Operand res = context.BitwiseOr(n, m);
  172. if (op.SetFlags)
  173. {
  174. EmitNZFlagsCheck(context, res);
  175. }
  176. EmitAluStore(context, res);
  177. }
  178. public static void Pkh(ArmEmitterContext context)
  179. {
  180. OpCode32AluRsImm op = (OpCode32AluRsImm)context.CurrOp;
  181. Operand n = GetAluN(context);
  182. Operand m = GetAluM(context);
  183. Operand res;
  184. bool tbform = op.ShiftType == ShiftType.Asr;
  185. if (tbform)
  186. {
  187. res = context.BitwiseOr(context.BitwiseAnd(n, Const(0xFFFF0000)), context.BitwiseAnd(m, Const(0xFFFF)));
  188. }
  189. else
  190. {
  191. res = context.BitwiseOr(context.BitwiseAnd(m, Const(0xFFFF0000)), context.BitwiseAnd(n, Const(0xFFFF)));
  192. }
  193. EmitAluStore(context, res);
  194. }
  195. public static void Rbit(ArmEmitterContext context)
  196. {
  197. Operand m = GetAluM(context);
  198. Operand res = EmitReverseBits32Op(context, m);
  199. EmitAluStore(context, res);
  200. }
  201. public static void Rev(ArmEmitterContext context)
  202. {
  203. Operand m = GetAluM(context);
  204. Operand res = context.ByteSwap(m);
  205. EmitAluStore(context, res);
  206. }
  207. public static void Rev16(ArmEmitterContext context)
  208. {
  209. Operand m = GetAluM(context);
  210. Operand res = EmitReverseBytes16_32Op(context, m);
  211. EmitAluStore(context, res);
  212. }
  213. public static void Revsh(ArmEmitterContext context)
  214. {
  215. Operand m = GetAluM(context);
  216. Operand res = EmitReverseBytes16_32Op(context, m);
  217. EmitAluStore(context, context.SignExtend16(OperandType.I32, res));
  218. }
  219. public static void Rsc(ArmEmitterContext context)
  220. {
  221. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  222. Operand n = GetAluN(context);
  223. Operand m = GetAluM(context, setCarry: false);
  224. Operand res = context.Subtract(m, n);
  225. Operand borrow = context.BitwiseExclusiveOr(GetFlag(PState.CFlag), Const(1));
  226. res = context.Subtract(res, borrow);
  227. if (op.SetFlags)
  228. {
  229. EmitNZFlagsCheck(context, res);
  230. EmitSbcsCCheck(context, m, n);
  231. EmitSubsVCheck(context, m, n, res);
  232. }
  233. EmitAluStore(context, res);
  234. }
  235. public static void Rsb(ArmEmitterContext context)
  236. {
  237. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  238. Operand n = GetAluN(context);
  239. Operand m = GetAluM(context, setCarry: false);
  240. Operand res = context.Subtract(m, n);
  241. if (op.SetFlags)
  242. {
  243. EmitNZFlagsCheck(context, res);
  244. EmitSubsCCheck(context, m, res);
  245. EmitSubsVCheck(context, m, n, res);
  246. }
  247. EmitAluStore(context, res);
  248. }
  249. public static void Sbc(ArmEmitterContext context)
  250. {
  251. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  252. Operand n = GetAluN(context);
  253. Operand m = GetAluM(context, setCarry: false);
  254. Operand res = context.Subtract(n, m);
  255. Operand borrow = context.BitwiseExclusiveOr(GetFlag(PState.CFlag), Const(1));
  256. res = context.Subtract(res, borrow);
  257. if (op.SetFlags)
  258. {
  259. EmitNZFlagsCheck(context, res);
  260. EmitSbcsCCheck(context, n, m);
  261. EmitSubsVCheck(context, n, m, res);
  262. }
  263. EmitAluStore(context, res);
  264. }
  265. public static void Sbfx(ArmEmitterContext context)
  266. {
  267. OpCode32AluBf op = (OpCode32AluBf)context.CurrOp;
  268. var msb = op.Lsb + op.Msb; // For this instruction, the msb is actually a width.
  269. Operand n = GetIntA32(context, op.Rn);
  270. Operand res = context.ShiftRightSI(context.ShiftLeft(n, Const(31 - msb)), Const(31 - op.Msb));
  271. SetIntA32(context, op.Rd, res);
  272. }
  273. public static void Sdiv(ArmEmitterContext context)
  274. {
  275. EmitDiv(context, false);
  276. }
  277. public static void Sub(ArmEmitterContext context)
  278. {
  279. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  280. Operand n = GetAluN(context);
  281. Operand m = GetAluM(context, setCarry: false);
  282. Operand res = context.Subtract(n, m);
  283. if (op.SetFlags)
  284. {
  285. EmitNZFlagsCheck(context, res);
  286. EmitSubsCCheck(context, n, res);
  287. EmitSubsVCheck(context, n, m, res);
  288. }
  289. EmitAluStore(context, res);
  290. }
  291. public static void Sxtb(ArmEmitterContext context)
  292. {
  293. EmitSignExtend(context, true, 8);
  294. }
  295. public static void Sxtb16(ArmEmitterContext context)
  296. {
  297. EmitExtend16(context, true);
  298. }
  299. public static void Sxth(ArmEmitterContext context)
  300. {
  301. EmitSignExtend(context, true, 16);
  302. }
  303. public static void Teq(ArmEmitterContext context)
  304. {
  305. Operand n = GetAluN(context);
  306. Operand m = GetAluM(context);
  307. Operand res = context.BitwiseExclusiveOr(n, m);
  308. EmitNZFlagsCheck(context, res);
  309. }
  310. public static void Tst(ArmEmitterContext context)
  311. {
  312. Operand n = GetAluN(context);
  313. Operand m = GetAluM(context);
  314. Operand res = context.BitwiseAnd(n, m);
  315. EmitNZFlagsCheck(context, res);
  316. }
  317. public static void Ubfx(ArmEmitterContext context)
  318. {
  319. OpCode32AluBf op = (OpCode32AluBf)context.CurrOp;
  320. var msb = op.Lsb + op.Msb; // For this instruction, the msb is actually a width.
  321. Operand n = GetIntA32(context, op.Rn);
  322. Operand res = context.ShiftRightUI(context.ShiftLeft(n, Const(31 - msb)), Const(31 - op.Msb));
  323. SetIntA32(context, op.Rd, res);
  324. }
  325. public static void Udiv(ArmEmitterContext context)
  326. {
  327. EmitDiv(context, true);
  328. }
  329. public static void Uxtb(ArmEmitterContext context)
  330. {
  331. EmitSignExtend(context, false, 8);
  332. }
  333. public static void Uxtb16(ArmEmitterContext context)
  334. {
  335. EmitExtend16(context, false);
  336. }
  337. public static void Uxth(ArmEmitterContext context)
  338. {
  339. EmitSignExtend(context, false, 16);
  340. }
  341. private static void EmitSignExtend(ArmEmitterContext context, bool signed, int bits)
  342. {
  343. IOpCode32AluUx op = (IOpCode32AluUx)context.CurrOp;
  344. Operand m = GetAluM(context);
  345. Operand res;
  346. if (op.RotateBits == 0)
  347. {
  348. res = m;
  349. }
  350. else
  351. {
  352. Operand rotate = Const(op.RotateBits);
  353. res = context.RotateRight(m, rotate);
  354. }
  355. switch (bits)
  356. {
  357. case 8:
  358. res = (signed) ? context.SignExtend8(OperandType.I32, res) : context.ZeroExtend8(OperandType.I32, res);
  359. break;
  360. case 16:
  361. res = (signed) ? context.SignExtend16(OperandType.I32, res) : context.ZeroExtend16(OperandType.I32, res);
  362. break;
  363. }
  364. if (op.Add)
  365. {
  366. res = context.Add(res, GetAluN(context));
  367. }
  368. EmitAluStore(context, res);
  369. }
  370. private static void EmitExtend16(ArmEmitterContext context, bool signed)
  371. {
  372. IOpCode32AluUx op = (IOpCode32AluUx)context.CurrOp;
  373. Operand m = GetAluM(context);
  374. Operand res;
  375. if (op.RotateBits == 0)
  376. {
  377. res = m;
  378. }
  379. else
  380. {
  381. Operand rotate = Const(op.RotateBits);
  382. res = context.RotateRight(m, rotate);
  383. }
  384. Operand low16, high16;
  385. if (signed)
  386. {
  387. low16 = context.SignExtend8(OperandType.I32, res);
  388. high16 = context.SignExtend8(OperandType.I32, context.ShiftRightUI(res, Const(16)));
  389. }
  390. else
  391. {
  392. low16 = context.ZeroExtend8(OperandType.I32, res);
  393. high16 = context.ZeroExtend8(OperandType.I32, context.ShiftRightUI(res, Const(16)));
  394. }
  395. if (op.Add)
  396. {
  397. Operand n = GetAluN(context);
  398. Operand lowAdd, highAdd;
  399. if (signed)
  400. {
  401. lowAdd = context.SignExtend16(OperandType.I32, n);
  402. highAdd = context.SignExtend16(OperandType.I32, context.ShiftRightUI(n, Const(16)));
  403. }
  404. else
  405. {
  406. lowAdd = context.ZeroExtend16(OperandType.I32, n);
  407. highAdd = context.ZeroExtend16(OperandType.I32, context.ShiftRightUI(n, Const(16)));
  408. }
  409. low16 = context.Add(low16, lowAdd);
  410. high16 = context.Add(high16, highAdd);
  411. }
  412. res = context.BitwiseOr(
  413. context.ZeroExtend16(OperandType.I32, low16),
  414. context.ShiftLeft(context.ZeroExtend16(OperandType.I32, high16), Const(16)));
  415. EmitAluStore(context, res);
  416. }
  417. public static void EmitDiv(ArmEmitterContext context, bool unsigned)
  418. {
  419. Operand n = GetAluN(context);
  420. Operand m = GetAluM(context);
  421. Operand zero = Const(m.Type, 0);
  422. Operand divisorIsZero = context.ICompareEqual(m, zero);
  423. Operand lblBadDiv = Label();
  424. Operand lblEnd = Label();
  425. context.BranchIfTrue(lblBadDiv, divisorIsZero);
  426. if (!unsigned)
  427. {
  428. // ARM64 behaviour: If Rn == INT_MIN && Rm == -1, Rd = INT_MIN (overflow).
  429. // TODO: tests to ensure A32 works the same
  430. Operand intMin = Const(int.MinValue);
  431. Operand minus1 = Const(-1);
  432. Operand nIsIntMin = context.ICompareEqual(n, intMin);
  433. Operand mIsMinus1 = context.ICompareEqual(m, minus1);
  434. Operand lblGoodDiv = Label();
  435. context.BranchIfFalse(lblGoodDiv, context.BitwiseAnd(nIsIntMin, mIsMinus1));
  436. EmitAluStore(context, intMin);
  437. context.Branch(lblEnd);
  438. context.MarkLabel(lblGoodDiv);
  439. }
  440. Operand res = unsigned
  441. ? context.DivideUI(n, m)
  442. : context.Divide(n, m);
  443. EmitAluStore(context, res);
  444. context.Branch(lblEnd);
  445. context.MarkLabel(lblBadDiv);
  446. EmitAluStore(context, zero);
  447. context.MarkLabel(lblEnd);
  448. }
  449. private static void EmitAluStore(ArmEmitterContext context, Operand value)
  450. {
  451. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  452. EmitGenericAluStoreA32(context, op.Rd, op.SetFlags, value);
  453. }
  454. }
  455. }