InstEmitAlu32.cs 25 KB

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