InstEmitAlu32.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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.Operand.Factory;
  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 (ShouldSetFlags(context))
  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 (ShouldSetFlags(context))
  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 (ShouldSetFlags(context))
  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 (ShouldSetFlags(context))
  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. Operand n = GetAluN(context);
  96. Operand m = GetAluM(context, setCarry: false);
  97. Operand res = context.Subtract(n, m);
  98. EmitNZFlagsCheck(context, res);
  99. EmitSubsCCheck(context, n, res);
  100. EmitSubsVCheck(context, n, m, res);
  101. }
  102. public static void Cmn(ArmEmitterContext context)
  103. {
  104. Operand n = GetAluN(context);
  105. Operand m = GetAluM(context, setCarry: false);
  106. Operand res = context.Add(n, m);
  107. EmitNZFlagsCheck(context, res);
  108. EmitAddsCCheck(context, n, res);
  109. EmitAddsVCheck(context, n, m, res);
  110. }
  111. public static void Eor(ArmEmitterContext context)
  112. {
  113. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  114. Operand n = GetAluN(context);
  115. Operand m = GetAluM(context);
  116. Operand res = context.BitwiseExclusiveOr(n, m);
  117. if (ShouldSetFlags(context))
  118. {
  119. EmitNZFlagsCheck(context, res);
  120. }
  121. EmitAluStore(context, res);
  122. }
  123. public static void Mov(ArmEmitterContext context)
  124. {
  125. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  126. Operand m = GetAluM(context);
  127. if (ShouldSetFlags(context))
  128. {
  129. EmitNZFlagsCheck(context, m);
  130. }
  131. EmitAluStore(context, m);
  132. }
  133. public static void Movt(ArmEmitterContext context)
  134. {
  135. OpCode32AluImm16 op = (OpCode32AluImm16)context.CurrOp;
  136. Operand d = GetIntA32(context, op.Rd);
  137. Operand imm = Const(op.Immediate << 16); // Immeditate value as top halfword.
  138. Operand res = context.BitwiseAnd(d, Const(0x0000ffff));
  139. res = context.BitwiseOr(res, imm);
  140. EmitAluStore(context, res);
  141. }
  142. public static void Mul(ArmEmitterContext context)
  143. {
  144. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  145. Operand n = GetAluN(context);
  146. Operand m = GetAluM(context);
  147. Operand res = context.Multiply(n, m);
  148. if (ShouldSetFlags(context))
  149. {
  150. EmitNZFlagsCheck(context, res);
  151. }
  152. EmitAluStore(context, res);
  153. }
  154. public static void Mvn(ArmEmitterContext context)
  155. {
  156. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  157. Operand m = GetAluM(context);
  158. Operand res = context.BitwiseNot(m);
  159. if (ShouldSetFlags(context))
  160. {
  161. EmitNZFlagsCheck(context, res);
  162. }
  163. EmitAluStore(context, res);
  164. }
  165. public static void Orr(ArmEmitterContext context)
  166. {
  167. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  168. Operand n = GetAluN(context);
  169. Operand m = GetAluM(context);
  170. Operand res = context.BitwiseOr(n, m);
  171. if (ShouldSetFlags(context))
  172. {
  173. EmitNZFlagsCheck(context, res);
  174. }
  175. EmitAluStore(context, res);
  176. }
  177. public static void Orn(ArmEmitterContext context)
  178. {
  179. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  180. Operand n = GetAluN(context);
  181. Operand m = GetAluM(context);
  182. Operand res = context.BitwiseOr(n, context.BitwiseNot(m));
  183. if (ShouldSetFlags(context))
  184. {
  185. EmitNZFlagsCheck(context, res);
  186. }
  187. EmitAluStore(context, res);
  188. }
  189. public static void Pkh(ArmEmitterContext context)
  190. {
  191. OpCode32AluRsImm op = (OpCode32AluRsImm)context.CurrOp;
  192. Operand n = GetAluN(context);
  193. Operand m = GetAluM(context);
  194. Operand res;
  195. bool tbform = op.ShiftType == ShiftType.Asr;
  196. if (tbform)
  197. {
  198. res = context.BitwiseOr(context.BitwiseAnd(n, Const(0xFFFF0000)), context.BitwiseAnd(m, Const(0xFFFF)));
  199. }
  200. else
  201. {
  202. res = context.BitwiseOr(context.BitwiseAnd(m, Const(0xFFFF0000)), context.BitwiseAnd(n, Const(0xFFFF)));
  203. }
  204. EmitAluStore(context, res);
  205. }
  206. public static void Rbit(ArmEmitterContext context)
  207. {
  208. Operand m = GetAluM(context);
  209. Operand res = EmitReverseBits32Op(context, m);
  210. EmitAluStore(context, res);
  211. }
  212. public static void Rev(ArmEmitterContext context)
  213. {
  214. Operand m = GetAluM(context);
  215. Operand res = context.ByteSwap(m);
  216. EmitAluStore(context, res);
  217. }
  218. public static void Rev16(ArmEmitterContext context)
  219. {
  220. Operand m = GetAluM(context);
  221. Operand res = EmitReverseBytes16_32Op(context, m);
  222. EmitAluStore(context, res);
  223. }
  224. public static void Revsh(ArmEmitterContext context)
  225. {
  226. Operand m = GetAluM(context);
  227. Operand res = EmitReverseBytes16_32Op(context, m);
  228. EmitAluStore(context, context.SignExtend16(OperandType.I32, res));
  229. }
  230. public static void Rsc(ArmEmitterContext context)
  231. {
  232. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  233. Operand n = GetAluN(context);
  234. Operand m = GetAluM(context, setCarry: false);
  235. Operand res = context.Subtract(m, n);
  236. Operand borrow = context.BitwiseExclusiveOr(GetFlag(PState.CFlag), Const(1));
  237. res = context.Subtract(res, borrow);
  238. if (ShouldSetFlags(context))
  239. {
  240. EmitNZFlagsCheck(context, res);
  241. EmitSbcsCCheck(context, m, n);
  242. EmitSubsVCheck(context, m, n, res);
  243. }
  244. EmitAluStore(context, res);
  245. }
  246. public static void Rsb(ArmEmitterContext context)
  247. {
  248. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  249. Operand n = GetAluN(context);
  250. Operand m = GetAluM(context, setCarry: false);
  251. Operand res = context.Subtract(m, n);
  252. if (ShouldSetFlags(context))
  253. {
  254. EmitNZFlagsCheck(context, res);
  255. EmitSubsCCheck(context, m, res);
  256. EmitSubsVCheck(context, m, n, res);
  257. }
  258. EmitAluStore(context, res);
  259. }
  260. public static void Sbc(ArmEmitterContext context)
  261. {
  262. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  263. Operand n = GetAluN(context);
  264. Operand m = GetAluM(context, setCarry: false);
  265. Operand res = context.Subtract(n, m);
  266. Operand borrow = context.BitwiseExclusiveOr(GetFlag(PState.CFlag), Const(1));
  267. res = context.Subtract(res, borrow);
  268. if (ShouldSetFlags(context))
  269. {
  270. EmitNZFlagsCheck(context, res);
  271. EmitSbcsCCheck(context, n, m);
  272. EmitSubsVCheck(context, n, m, res);
  273. }
  274. EmitAluStore(context, res);
  275. }
  276. public static void Sbfx(ArmEmitterContext context)
  277. {
  278. OpCode32AluBf op = (OpCode32AluBf)context.CurrOp;
  279. var msb = op.Lsb + op.Msb; // For this instruction, the msb is actually a width.
  280. Operand n = GetIntA32(context, op.Rn);
  281. Operand res = context.ShiftRightSI(context.ShiftLeft(n, Const(31 - msb)), Const(31 - op.Msb));
  282. SetIntA32(context, op.Rd, res);
  283. }
  284. public static void Sdiv(ArmEmitterContext context)
  285. {
  286. EmitDiv(context, false);
  287. }
  288. public static void Shadd8(ArmEmitterContext context)
  289. {
  290. EmitHadd8(context, false);
  291. }
  292. public static void Shsub8(ArmEmitterContext context)
  293. {
  294. EmitHsub8(context, false);
  295. }
  296. public static void Ssat(ArmEmitterContext context)
  297. {
  298. OpCode32Sat op = (OpCode32Sat)context.CurrOp;
  299. EmitSat(context, -(1 << op.SatImm), (1 << op.SatImm) - 1);
  300. }
  301. public static void Ssat16(ArmEmitterContext context)
  302. {
  303. OpCode32Sat16 op = (OpCode32Sat16)context.CurrOp;
  304. EmitSat16(context, -(1 << op.SatImm), (1 << op.SatImm) - 1);
  305. }
  306. public static void Sub(ArmEmitterContext context)
  307. {
  308. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  309. Operand n = GetAluN(context);
  310. Operand m = GetAluM(context, setCarry: false);
  311. Operand res = context.Subtract(n, m);
  312. if (ShouldSetFlags(context))
  313. {
  314. EmitNZFlagsCheck(context, res);
  315. EmitSubsCCheck(context, n, res);
  316. EmitSubsVCheck(context, n, m, res);
  317. }
  318. EmitAluStore(context, res);
  319. }
  320. public static void Sxtb(ArmEmitterContext context)
  321. {
  322. EmitSignExtend(context, true, 8);
  323. }
  324. public static void Sxtb16(ArmEmitterContext context)
  325. {
  326. EmitExtend16(context, true);
  327. }
  328. public static void Sxth(ArmEmitterContext context)
  329. {
  330. EmitSignExtend(context, true, 16);
  331. }
  332. public static void Teq(ArmEmitterContext context)
  333. {
  334. Operand n = GetAluN(context);
  335. Operand m = GetAluM(context);
  336. Operand res = context.BitwiseExclusiveOr(n, m);
  337. EmitNZFlagsCheck(context, res);
  338. }
  339. public static void Tst(ArmEmitterContext context)
  340. {
  341. Operand n = GetAluN(context);
  342. Operand m = GetAluM(context);
  343. Operand res = context.BitwiseAnd(n, m);
  344. EmitNZFlagsCheck(context, res);
  345. }
  346. public static void Ubfx(ArmEmitterContext context)
  347. {
  348. OpCode32AluBf op = (OpCode32AluBf)context.CurrOp;
  349. var msb = op.Lsb + op.Msb; // For this instruction, the msb is actually a width.
  350. Operand n = GetIntA32(context, op.Rn);
  351. Operand res = context.ShiftRightUI(context.ShiftLeft(n, Const(31 - msb)), Const(31 - op.Msb));
  352. SetIntA32(context, op.Rd, res);
  353. }
  354. public static void Udiv(ArmEmitterContext context)
  355. {
  356. EmitDiv(context, true);
  357. }
  358. public static void Uhadd8(ArmEmitterContext context)
  359. {
  360. EmitHadd8(context, true);
  361. }
  362. public static void Uhsub8(ArmEmitterContext context)
  363. {
  364. EmitHsub8(context, true);
  365. }
  366. public static void Usat(ArmEmitterContext context)
  367. {
  368. OpCode32Sat op = (OpCode32Sat)context.CurrOp;
  369. EmitSat(context, 0, op.SatImm == 32 ? (int)(~0) : (1 << op.SatImm) - 1);
  370. }
  371. public static void Usat16(ArmEmitterContext context)
  372. {
  373. OpCode32Sat16 op = (OpCode32Sat16)context.CurrOp;
  374. EmitSat16(context, 0, (1 << op.SatImm) - 1);
  375. }
  376. public static void Uxtb(ArmEmitterContext context)
  377. {
  378. EmitSignExtend(context, false, 8);
  379. }
  380. public static void Uxtb16(ArmEmitterContext context)
  381. {
  382. EmitExtend16(context, false);
  383. }
  384. public static void Uxth(ArmEmitterContext context)
  385. {
  386. EmitSignExtend(context, false, 16);
  387. }
  388. private static void EmitSignExtend(ArmEmitterContext context, bool signed, int bits)
  389. {
  390. IOpCode32AluUx op = (IOpCode32AluUx)context.CurrOp;
  391. Operand m = GetAluM(context);
  392. Operand res;
  393. if (op.RotateBits == 0)
  394. {
  395. res = m;
  396. }
  397. else
  398. {
  399. Operand rotate = Const(op.RotateBits);
  400. res = context.RotateRight(m, rotate);
  401. }
  402. switch (bits)
  403. {
  404. case 8:
  405. res = (signed) ? context.SignExtend8(OperandType.I32, res) : context.ZeroExtend8(OperandType.I32, res);
  406. break;
  407. case 16:
  408. res = (signed) ? context.SignExtend16(OperandType.I32, res) : context.ZeroExtend16(OperandType.I32, res);
  409. break;
  410. }
  411. if (op.Add)
  412. {
  413. res = context.Add(res, GetAluN(context));
  414. }
  415. EmitAluStore(context, res);
  416. }
  417. private static void EmitExtend16(ArmEmitterContext context, bool signed)
  418. {
  419. IOpCode32AluUx op = (IOpCode32AluUx)context.CurrOp;
  420. Operand m = GetAluM(context);
  421. Operand res;
  422. if (op.RotateBits == 0)
  423. {
  424. res = m;
  425. }
  426. else
  427. {
  428. Operand rotate = Const(op.RotateBits);
  429. res = context.RotateRight(m, rotate);
  430. }
  431. Operand low16, high16;
  432. if (signed)
  433. {
  434. low16 = context.SignExtend8(OperandType.I32, res);
  435. high16 = context.SignExtend8(OperandType.I32, context.ShiftRightUI(res, Const(16)));
  436. }
  437. else
  438. {
  439. low16 = context.ZeroExtend8(OperandType.I32, res);
  440. high16 = context.ZeroExtend8(OperandType.I32, context.ShiftRightUI(res, Const(16)));
  441. }
  442. if (op.Add)
  443. {
  444. Operand n = GetAluN(context);
  445. Operand lowAdd, highAdd;
  446. if (signed)
  447. {
  448. lowAdd = context.SignExtend16(OperandType.I32, n);
  449. highAdd = context.SignExtend16(OperandType.I32, context.ShiftRightUI(n, Const(16)));
  450. }
  451. else
  452. {
  453. lowAdd = context.ZeroExtend16(OperandType.I32, n);
  454. highAdd = context.ZeroExtend16(OperandType.I32, context.ShiftRightUI(n, Const(16)));
  455. }
  456. low16 = context.Add(low16, lowAdd);
  457. high16 = context.Add(high16, highAdd);
  458. }
  459. res = context.BitwiseOr(
  460. context.ZeroExtend16(OperandType.I32, low16),
  461. context.ShiftLeft(context.ZeroExtend16(OperandType.I32, high16), Const(16)));
  462. EmitAluStore(context, res);
  463. }
  464. private static void EmitDiv(ArmEmitterContext context, bool unsigned)
  465. {
  466. Operand n = GetAluN(context);
  467. Operand m = GetAluM(context);
  468. Operand zero = Const(m.Type, 0);
  469. Operand divisorIsZero = context.ICompareEqual(m, zero);
  470. Operand lblBadDiv = Label();
  471. Operand lblEnd = Label();
  472. context.BranchIfTrue(lblBadDiv, divisorIsZero);
  473. if (!unsigned)
  474. {
  475. // ARM64 behaviour: If Rn == INT_MIN && Rm == -1, Rd = INT_MIN (overflow).
  476. // TODO: tests to ensure A32 works the same
  477. Operand intMin = Const(int.MinValue);
  478. Operand minus1 = Const(-1);
  479. Operand nIsIntMin = context.ICompareEqual(n, intMin);
  480. Operand mIsMinus1 = context.ICompareEqual(m, minus1);
  481. Operand lblGoodDiv = Label();
  482. context.BranchIfFalse(lblGoodDiv, context.BitwiseAnd(nIsIntMin, mIsMinus1));
  483. EmitAluStore(context, intMin);
  484. context.Branch(lblEnd);
  485. context.MarkLabel(lblGoodDiv);
  486. }
  487. Operand res = unsigned
  488. ? context.DivideUI(n, m)
  489. : context.Divide(n, m);
  490. EmitAluStore(context, res);
  491. context.Branch(lblEnd);
  492. context.MarkLabel(lblBadDiv);
  493. EmitAluStore(context, zero);
  494. context.MarkLabel(lblEnd);
  495. }
  496. private static void EmitHadd8(ArmEmitterContext context, bool unsigned)
  497. {
  498. OpCode32AluReg op = (OpCode32AluReg)context.CurrOp;
  499. Operand m = GetIntA32(context, op.Rm);
  500. Operand n = GetIntA32(context, op.Rn);
  501. Operand xor, res, carry;
  502. // This relies on the equality x+y == ((x&y) << 1) + (x^y).
  503. // Note that x^y always contains the LSB of the result.
  504. // Since we want to calculate (x+y)/2, we can instead calculate (x&y) + ((x^y)>>1).
  505. // We mask by 0x7F to remove the LSB so that it doesn't leak into the field below.
  506. res = context.BitwiseAnd(m, n);
  507. carry = context.BitwiseExclusiveOr(m, n);
  508. xor = context.ShiftRightUI(carry, Const(1));
  509. xor = context.BitwiseAnd(xor, Const(0x7F7F7F7Fu));
  510. res = context.Add(res, xor);
  511. if (!unsigned)
  512. {
  513. // Propagates the sign bit from (x^y)>>1 upwards by one.
  514. carry = context.BitwiseAnd(carry, Const(0x80808080u));
  515. res = context.BitwiseExclusiveOr(res, carry);
  516. }
  517. SetIntA32(context, op.Rd, res);
  518. }
  519. private static void EmitHsub8(ArmEmitterContext context, bool unsigned)
  520. {
  521. OpCode32AluReg op = (OpCode32AluReg)context.CurrOp;
  522. Operand m = GetIntA32(context, op.Rm);
  523. Operand n = GetIntA32(context, op.Rn);
  524. Operand left, right, carry, res;
  525. // This relies on the equality x-y == (x^y) - (((x^y)&y) << 1).
  526. // Note that x^y always contains the LSB of the result.
  527. // Since we want to calculate (x+y)/2, we can instead calculate ((x^y)>>1) - ((x^y)&y).
  528. carry = context.BitwiseExclusiveOr(m, n);
  529. left = context.ShiftRightUI(carry, Const(1));
  530. right = context.BitwiseAnd(carry, m);
  531. // We must now perform a partitioned subtraction.
  532. // We can do this because minuend contains 7 bit fields.
  533. // We use the extra bit in minuend as a bit to borrow from; we set this bit.
  534. // We invert this bit at the end as this tells us if that bit was borrowed from.
  535. res = context.BitwiseOr(left, Const(0x80808080));
  536. res = context.Subtract(res, right);
  537. res = context.BitwiseExclusiveOr(res, Const(0x80808080));
  538. if (!unsigned)
  539. {
  540. // We then sign extend the result into this bit.
  541. carry = context.BitwiseAnd(carry, Const(0x80808080));
  542. res = context.BitwiseExclusiveOr(res, carry);
  543. }
  544. SetIntA32(context, op.Rd, res);
  545. }
  546. private static void EmitSat(ArmEmitterContext context, int intMin, int intMax)
  547. {
  548. OpCode32Sat op = (OpCode32Sat)context.CurrOp;
  549. Operand n = GetIntA32(context, op.Rn);
  550. int shift = DecodeImmShift(op.ShiftType, op.Imm5);
  551. switch (op.ShiftType)
  552. {
  553. case ShiftType.Lsl:
  554. if (shift == 32)
  555. {
  556. n = Const(0);
  557. }
  558. else
  559. {
  560. n = context.ShiftLeft(n, Const(shift));
  561. }
  562. break;
  563. case ShiftType.Asr:
  564. if (shift == 32)
  565. {
  566. n = context.ShiftRightSI(n, Const(31));
  567. }
  568. else
  569. {
  570. n = context.ShiftRightSI(n, Const(shift));
  571. }
  572. break;
  573. }
  574. Operand lblCheckLtIntMin = Label();
  575. Operand lblNoSat = Label();
  576. Operand lblEnd = Label();
  577. context.BranchIfFalse(lblCheckLtIntMin, context.ICompareGreater(n, Const(intMax)));
  578. SetFlag(context, PState.QFlag, Const(1));
  579. SetIntA32(context, op.Rd, Const(intMax));
  580. context.Branch(lblEnd);
  581. context.MarkLabel(lblCheckLtIntMin);
  582. context.BranchIfFalse(lblNoSat, context.ICompareLess(n, Const(intMin)));
  583. SetFlag(context, PState.QFlag, Const(1));
  584. SetIntA32(context, op.Rd, Const(intMin));
  585. context.Branch(lblEnd);
  586. context.MarkLabel(lblNoSat);
  587. SetIntA32(context, op.Rd, n);
  588. context.MarkLabel(lblEnd);
  589. }
  590. private static void EmitSat16(ArmEmitterContext context, int intMin, int intMax)
  591. {
  592. OpCode32Sat16 op = (OpCode32Sat16)context.CurrOp;
  593. void SetD(int part, Operand value)
  594. {
  595. if (part == 0)
  596. {
  597. SetIntA32(context, op.Rd, context.ZeroExtend16(OperandType.I32, value));
  598. }
  599. else
  600. {
  601. SetIntA32(context, op.Rd, context.BitwiseOr(GetIntA32(context, op.Rd), context.ShiftLeft(value, Const(16))));
  602. }
  603. }
  604. Operand n = GetIntA32(context, op.Rn);
  605. Operand nLow = context.SignExtend16(OperandType.I32, n);
  606. Operand nHigh = context.ShiftRightSI(n, Const(16));
  607. for (int part = 0; part < 2; part++)
  608. {
  609. Operand nPart = part == 0 ? nLow : nHigh;
  610. Operand lblCheckLtIntMin = Label();
  611. Operand lblNoSat = Label();
  612. Operand lblEnd = Label();
  613. context.BranchIfFalse(lblCheckLtIntMin, context.ICompareGreater(nPart, Const(intMax)));
  614. SetFlag(context, PState.QFlag, Const(1));
  615. SetD(part, Const(intMax));
  616. context.Branch(lblEnd);
  617. context.MarkLabel(lblCheckLtIntMin);
  618. context.BranchIfFalse(lblNoSat, context.ICompareLess(nPart, Const(intMin)));
  619. SetFlag(context, PState.QFlag, Const(1));
  620. SetD(part, Const(intMin));
  621. context.Branch(lblEnd);
  622. context.MarkLabel(lblNoSat);
  623. SetD(part, nPart);
  624. context.MarkLabel(lblEnd);
  625. }
  626. }
  627. private static void EmitAluStore(ArmEmitterContext context, Operand value)
  628. {
  629. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  630. EmitGenericAluStoreA32(context, op.Rd, ShouldSetFlags(context), value);
  631. }
  632. }
  633. }