InstEmitAlu32.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. IOpCode32AluBf op = (IOpCode32AluBf)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. IOpCode32AluBf op = (IOpCode32AluBf)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. IOpCode32AluImm16 op = (IOpCode32AluImm16)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 Sadd8(ArmEmitterContext context)
  261. {
  262. EmitAddSub8(context, add: true, unsigned: false);
  263. }
  264. public static void Sbc(ArmEmitterContext context)
  265. {
  266. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  267. Operand n = GetAluN(context);
  268. Operand m = GetAluM(context, setCarry: false);
  269. Operand res = context.Subtract(n, m);
  270. Operand borrow = context.BitwiseExclusiveOr(GetFlag(PState.CFlag), Const(1));
  271. res = context.Subtract(res, borrow);
  272. if (ShouldSetFlags(context))
  273. {
  274. EmitNZFlagsCheck(context, res);
  275. EmitSbcsCCheck(context, n, m);
  276. EmitSubsVCheck(context, n, m, res);
  277. }
  278. EmitAluStore(context, res);
  279. }
  280. public static void Sbfx(ArmEmitterContext context)
  281. {
  282. IOpCode32AluBf op = (IOpCode32AluBf)context.CurrOp;
  283. var msb = op.Lsb + op.Msb; // For this instruction, the msb is actually a width.
  284. Operand n = GetIntA32(context, op.Rn);
  285. Operand res = context.ShiftRightSI(context.ShiftLeft(n, Const(31 - msb)), Const(31 - op.Msb));
  286. SetIntA32(context, op.Rd, res);
  287. }
  288. public static void Sdiv(ArmEmitterContext context)
  289. {
  290. EmitDiv(context, unsigned: false);
  291. }
  292. public static void Sel(ArmEmitterContext context)
  293. {
  294. IOpCode32AluReg op = (IOpCode32AluReg)context.CurrOp;
  295. Operand n = GetIntA32(context, op.Rn);
  296. Operand m = GetIntA32(context, op.Rm);
  297. Operand ge0 = context.ZeroExtend8(OperandType.I32, context.Negate(GetFlag(PState.GE0Flag)));
  298. Operand ge1 = context.ZeroExtend8(OperandType.I32, context.Negate(GetFlag(PState.GE1Flag)));
  299. Operand ge2 = context.ZeroExtend8(OperandType.I32, context.Negate(GetFlag(PState.GE2Flag)));
  300. Operand ge3 = context.Negate(GetFlag(PState.GE3Flag));
  301. Operand mask = context.BitwiseOr(ge0, context.ShiftLeft(ge1, Const(8)));
  302. mask = context.BitwiseOr(mask, context.ShiftLeft(ge2, Const(16)));
  303. mask = context.BitwiseOr(mask, context.ShiftLeft(ge3, Const(24)));
  304. Operand res = context.BitwiseOr(context.BitwiseAnd(n, mask), context.BitwiseAnd(m, context.BitwiseNot(mask)));
  305. SetIntA32(context, op.Rd, res);
  306. }
  307. public static void Shadd8(ArmEmitterContext context)
  308. {
  309. EmitHadd8(context, unsigned: false);
  310. }
  311. public static void Shsub8(ArmEmitterContext context)
  312. {
  313. EmitHsub8(context, unsigned: false);
  314. }
  315. public static void Ssat(ArmEmitterContext context)
  316. {
  317. OpCode32Sat op = (OpCode32Sat)context.CurrOp;
  318. EmitSat(context, -(1 << op.SatImm), (1 << op.SatImm) - 1);
  319. }
  320. public static void Ssat16(ArmEmitterContext context)
  321. {
  322. OpCode32Sat16 op = (OpCode32Sat16)context.CurrOp;
  323. EmitSat16(context, -(1 << op.SatImm), (1 << op.SatImm) - 1);
  324. }
  325. public static void Ssub8(ArmEmitterContext context)
  326. {
  327. EmitAddSub8(context, add: false, unsigned: false);
  328. }
  329. public static void Sub(ArmEmitterContext context)
  330. {
  331. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  332. Operand n = GetAluN(context);
  333. Operand m = GetAluM(context, setCarry: false);
  334. Operand res = context.Subtract(n, m);
  335. if (ShouldSetFlags(context))
  336. {
  337. EmitNZFlagsCheck(context, res);
  338. EmitSubsCCheck(context, n, res);
  339. EmitSubsVCheck(context, n, m, res);
  340. }
  341. EmitAluStore(context, res);
  342. }
  343. public static void Sxtb(ArmEmitterContext context)
  344. {
  345. EmitSignExtend(context, true, 8);
  346. }
  347. public static void Sxtb16(ArmEmitterContext context)
  348. {
  349. EmitExtend16(context, true);
  350. }
  351. public static void Sxth(ArmEmitterContext context)
  352. {
  353. EmitSignExtend(context, true, 16);
  354. }
  355. public static void Teq(ArmEmitterContext context)
  356. {
  357. Operand n = GetAluN(context);
  358. Operand m = GetAluM(context);
  359. Operand res = context.BitwiseExclusiveOr(n, m);
  360. EmitNZFlagsCheck(context, res);
  361. }
  362. public static void Tst(ArmEmitterContext context)
  363. {
  364. Operand n = GetAluN(context);
  365. Operand m = GetAluM(context);
  366. Operand res = context.BitwiseAnd(n, m);
  367. EmitNZFlagsCheck(context, res);
  368. }
  369. public static void Uadd8(ArmEmitterContext context)
  370. {
  371. EmitAddSub8(context, add: true, unsigned: true);
  372. }
  373. public static void Ubfx(ArmEmitterContext context)
  374. {
  375. IOpCode32AluBf op = (IOpCode32AluBf)context.CurrOp;
  376. var msb = op.Lsb + op.Msb; // For this instruction, the msb is actually a width.
  377. Operand n = GetIntA32(context, op.Rn);
  378. Operand res = context.ShiftRightUI(context.ShiftLeft(n, Const(31 - msb)), Const(31 - op.Msb));
  379. SetIntA32(context, op.Rd, res);
  380. }
  381. public static void Udiv(ArmEmitterContext context)
  382. {
  383. EmitDiv(context, unsigned: true);
  384. }
  385. public static void Uhadd8(ArmEmitterContext context)
  386. {
  387. EmitHadd8(context, unsigned: true);
  388. }
  389. public static void Uhsub8(ArmEmitterContext context)
  390. {
  391. EmitHsub8(context, unsigned: true);
  392. }
  393. public static void Usat(ArmEmitterContext context)
  394. {
  395. OpCode32Sat op = (OpCode32Sat)context.CurrOp;
  396. EmitSat(context, 0, op.SatImm == 32 ? (int)(~0) : (1 << op.SatImm) - 1);
  397. }
  398. public static void Usat16(ArmEmitterContext context)
  399. {
  400. OpCode32Sat16 op = (OpCode32Sat16)context.CurrOp;
  401. EmitSat16(context, 0, (1 << op.SatImm) - 1);
  402. }
  403. public static void Usub8(ArmEmitterContext context)
  404. {
  405. EmitAddSub8(context, add: false, unsigned: true);
  406. }
  407. public static void Uxtb(ArmEmitterContext context)
  408. {
  409. EmitSignExtend(context, false, 8);
  410. }
  411. public static void Uxtb16(ArmEmitterContext context)
  412. {
  413. EmitExtend16(context, false);
  414. }
  415. public static void Uxth(ArmEmitterContext context)
  416. {
  417. EmitSignExtend(context, false, 16);
  418. }
  419. private static void EmitSignExtend(ArmEmitterContext context, bool signed, int bits)
  420. {
  421. IOpCode32AluUx op = (IOpCode32AluUx)context.CurrOp;
  422. Operand m = GetAluM(context);
  423. Operand res;
  424. if (op.RotateBits == 0)
  425. {
  426. res = m;
  427. }
  428. else
  429. {
  430. Operand rotate = Const(op.RotateBits);
  431. res = context.RotateRight(m, rotate);
  432. }
  433. switch (bits)
  434. {
  435. case 8:
  436. res = (signed) ? context.SignExtend8(OperandType.I32, res) : context.ZeroExtend8(OperandType.I32, res);
  437. break;
  438. case 16:
  439. res = (signed) ? context.SignExtend16(OperandType.I32, res) : context.ZeroExtend16(OperandType.I32, res);
  440. break;
  441. }
  442. if (op.Add)
  443. {
  444. res = context.Add(res, GetAluN(context));
  445. }
  446. EmitAluStore(context, res);
  447. }
  448. private static void EmitExtend16(ArmEmitterContext context, bool signed)
  449. {
  450. IOpCode32AluUx op = (IOpCode32AluUx)context.CurrOp;
  451. Operand m = GetAluM(context);
  452. Operand res;
  453. if (op.RotateBits == 0)
  454. {
  455. res = m;
  456. }
  457. else
  458. {
  459. Operand rotate = Const(op.RotateBits);
  460. res = context.RotateRight(m, rotate);
  461. }
  462. Operand low16, high16;
  463. if (signed)
  464. {
  465. low16 = context.SignExtend8(OperandType.I32, res);
  466. high16 = context.SignExtend8(OperandType.I32, context.ShiftRightUI(res, Const(16)));
  467. }
  468. else
  469. {
  470. low16 = context.ZeroExtend8(OperandType.I32, res);
  471. high16 = context.ZeroExtend8(OperandType.I32, context.ShiftRightUI(res, Const(16)));
  472. }
  473. if (op.Add)
  474. {
  475. Operand n = GetAluN(context);
  476. Operand lowAdd, highAdd;
  477. if (signed)
  478. {
  479. lowAdd = context.SignExtend16(OperandType.I32, n);
  480. highAdd = context.SignExtend16(OperandType.I32, context.ShiftRightUI(n, Const(16)));
  481. }
  482. else
  483. {
  484. lowAdd = context.ZeroExtend16(OperandType.I32, n);
  485. highAdd = context.ZeroExtend16(OperandType.I32, context.ShiftRightUI(n, Const(16)));
  486. }
  487. low16 = context.Add(low16, lowAdd);
  488. high16 = context.Add(high16, highAdd);
  489. }
  490. res = context.BitwiseOr(
  491. context.ZeroExtend16(OperandType.I32, low16),
  492. context.ShiftLeft(context.ZeroExtend16(OperandType.I32, high16), Const(16)));
  493. EmitAluStore(context, res);
  494. }
  495. private static void EmitDiv(ArmEmitterContext context, bool unsigned)
  496. {
  497. Operand n = GetAluN(context);
  498. Operand m = GetAluM(context);
  499. Operand zero = Const(m.Type, 0);
  500. Operand divisorIsZero = context.ICompareEqual(m, zero);
  501. Operand lblBadDiv = Label();
  502. Operand lblEnd = Label();
  503. context.BranchIfTrue(lblBadDiv, divisorIsZero);
  504. if (!unsigned)
  505. {
  506. // ARM64 behaviour: If Rn == INT_MIN && Rm == -1, Rd = INT_MIN (overflow).
  507. // TODO: tests to ensure A32 works the same
  508. Operand intMin = Const(int.MinValue);
  509. Operand minus1 = Const(-1);
  510. Operand nIsIntMin = context.ICompareEqual(n, intMin);
  511. Operand mIsMinus1 = context.ICompareEqual(m, minus1);
  512. Operand lblGoodDiv = Label();
  513. context.BranchIfFalse(lblGoodDiv, context.BitwiseAnd(nIsIntMin, mIsMinus1));
  514. EmitAluStore(context, intMin);
  515. context.Branch(lblEnd);
  516. context.MarkLabel(lblGoodDiv);
  517. }
  518. Operand res = unsigned
  519. ? context.DivideUI(n, m)
  520. : context.Divide(n, m);
  521. EmitAluStore(context, res);
  522. context.Branch(lblEnd);
  523. context.MarkLabel(lblBadDiv);
  524. EmitAluStore(context, zero);
  525. context.MarkLabel(lblEnd);
  526. }
  527. private static void EmitAddSub8(ArmEmitterContext context, bool add, bool unsigned)
  528. {
  529. IOpCode32AluReg op = (IOpCode32AluReg)context.CurrOp;
  530. Operand n = GetIntA32(context, op.Rn);
  531. Operand m = GetIntA32(context, op.Rm);
  532. Operand res = Const(0);
  533. for (int byteSel = 0; byteSel < 4; byteSel++)
  534. {
  535. Operand shift = Const(byteSel * 8);
  536. Operand nByte = context.ShiftRightUI(n, shift);
  537. Operand mByte = context.ShiftRightUI(m, shift);
  538. nByte = unsigned ? context.ZeroExtend8(OperandType.I32, nByte) : context.SignExtend8(OperandType.I32, nByte);
  539. mByte = unsigned ? context.ZeroExtend8(OperandType.I32, mByte) : context.SignExtend8(OperandType.I32, mByte);
  540. Operand resByte = add ? context.Add(nByte, mByte) : context.Subtract(nByte, mByte);
  541. res = context.BitwiseOr(res, context.ShiftLeft(context.ZeroExtend8(OperandType.I32, resByte), shift));
  542. SetFlag(context, PState.GE0Flag + byteSel, unsigned && add
  543. ? context.ShiftRightUI(resByte, Const(8))
  544. : context.ShiftRightUI(context.BitwiseNot(resByte), Const(31)));
  545. }
  546. SetIntA32(context, op.Rd, res);
  547. }
  548. private static void EmitHadd8(ArmEmitterContext context, bool unsigned)
  549. {
  550. IOpCode32AluReg op = (IOpCode32AluReg)context.CurrOp;
  551. Operand m = GetIntA32(context, op.Rm);
  552. Operand n = GetIntA32(context, op.Rn);
  553. Operand xor, res, carry;
  554. // This relies on the equality x+y == ((x&y) << 1) + (x^y).
  555. // Note that x^y always contains the LSB of the result.
  556. // Since we want to calculate (x+y)/2, we can instead calculate (x&y) + ((x^y)>>1).
  557. // We mask by 0x7F to remove the LSB so that it doesn't leak into the field below.
  558. res = context.BitwiseAnd(m, n);
  559. carry = context.BitwiseExclusiveOr(m, n);
  560. xor = context.ShiftRightUI(carry, Const(1));
  561. xor = context.BitwiseAnd(xor, Const(0x7F7F7F7Fu));
  562. res = context.Add(res, xor);
  563. if (!unsigned)
  564. {
  565. // Propagates the sign bit from (x^y)>>1 upwards by one.
  566. carry = context.BitwiseAnd(carry, Const(0x80808080u));
  567. res = context.BitwiseExclusiveOr(res, carry);
  568. }
  569. SetIntA32(context, op.Rd, res);
  570. }
  571. private static void EmitHsub8(ArmEmitterContext context, bool unsigned)
  572. {
  573. IOpCode32AluReg op = (IOpCode32AluReg)context.CurrOp;
  574. Operand m = GetIntA32(context, op.Rm);
  575. Operand n = GetIntA32(context, op.Rn);
  576. Operand left, right, carry, res;
  577. // This relies on the equality x-y == (x^y) - (((x^y)&y) << 1).
  578. // Note that x^y always contains the LSB of the result.
  579. // Since we want to calculate (x+y)/2, we can instead calculate ((x^y)>>1) - ((x^y)&y).
  580. carry = context.BitwiseExclusiveOr(m, n);
  581. left = context.ShiftRightUI(carry, Const(1));
  582. right = context.BitwiseAnd(carry, m);
  583. // We must now perform a partitioned subtraction.
  584. // We can do this because minuend contains 7 bit fields.
  585. // We use the extra bit in minuend as a bit to borrow from; we set this bit.
  586. // We invert this bit at the end as this tells us if that bit was borrowed from.
  587. res = context.BitwiseOr(left, Const(0x80808080));
  588. res = context.Subtract(res, right);
  589. res = context.BitwiseExclusiveOr(res, Const(0x80808080));
  590. if (!unsigned)
  591. {
  592. // We then sign extend the result into this bit.
  593. carry = context.BitwiseAnd(carry, Const(0x80808080));
  594. res = context.BitwiseExclusiveOr(res, carry);
  595. }
  596. SetIntA32(context, op.Rd, res);
  597. }
  598. private static void EmitSat(ArmEmitterContext context, int intMin, int intMax)
  599. {
  600. OpCode32Sat op = (OpCode32Sat)context.CurrOp;
  601. Operand n = GetIntA32(context, op.Rn);
  602. int shift = DecodeImmShift(op.ShiftType, op.Imm5);
  603. switch (op.ShiftType)
  604. {
  605. case ShiftType.Lsl:
  606. if (shift == 32)
  607. {
  608. n = Const(0);
  609. }
  610. else
  611. {
  612. n = context.ShiftLeft(n, Const(shift));
  613. }
  614. break;
  615. case ShiftType.Asr:
  616. if (shift == 32)
  617. {
  618. n = context.ShiftRightSI(n, Const(31));
  619. }
  620. else
  621. {
  622. n = context.ShiftRightSI(n, Const(shift));
  623. }
  624. break;
  625. }
  626. Operand lblCheckLtIntMin = Label();
  627. Operand lblNoSat = Label();
  628. Operand lblEnd = Label();
  629. context.BranchIfFalse(lblCheckLtIntMin, context.ICompareGreater(n, Const(intMax)));
  630. SetFlag(context, PState.QFlag, Const(1));
  631. SetIntA32(context, op.Rd, Const(intMax));
  632. context.Branch(lblEnd);
  633. context.MarkLabel(lblCheckLtIntMin);
  634. context.BranchIfFalse(lblNoSat, context.ICompareLess(n, Const(intMin)));
  635. SetFlag(context, PState.QFlag, Const(1));
  636. SetIntA32(context, op.Rd, Const(intMin));
  637. context.Branch(lblEnd);
  638. context.MarkLabel(lblNoSat);
  639. SetIntA32(context, op.Rd, n);
  640. context.MarkLabel(lblEnd);
  641. }
  642. private static void EmitSat16(ArmEmitterContext context, int intMin, int intMax)
  643. {
  644. OpCode32Sat16 op = (OpCode32Sat16)context.CurrOp;
  645. void SetD(int part, Operand value)
  646. {
  647. if (part == 0)
  648. {
  649. SetIntA32(context, op.Rd, context.ZeroExtend16(OperandType.I32, value));
  650. }
  651. else
  652. {
  653. SetIntA32(context, op.Rd, context.BitwiseOr(GetIntA32(context, op.Rd), context.ShiftLeft(value, Const(16))));
  654. }
  655. }
  656. Operand n = GetIntA32(context, op.Rn);
  657. Operand nLow = context.SignExtend16(OperandType.I32, n);
  658. Operand nHigh = context.ShiftRightSI(n, Const(16));
  659. for (int part = 0; part < 2; part++)
  660. {
  661. Operand nPart = part == 0 ? nLow : nHigh;
  662. Operand lblCheckLtIntMin = Label();
  663. Operand lblNoSat = Label();
  664. Operand lblEnd = Label();
  665. context.BranchIfFalse(lblCheckLtIntMin, context.ICompareGreater(nPart, Const(intMax)));
  666. SetFlag(context, PState.QFlag, Const(1));
  667. SetD(part, Const(intMax));
  668. context.Branch(lblEnd);
  669. context.MarkLabel(lblCheckLtIntMin);
  670. context.BranchIfFalse(lblNoSat, context.ICompareLess(nPart, Const(intMin)));
  671. SetFlag(context, PState.QFlag, Const(1));
  672. SetD(part, Const(intMin));
  673. context.Branch(lblEnd);
  674. context.MarkLabel(lblNoSat);
  675. SetD(part, nPart);
  676. context.MarkLabel(lblEnd);
  677. }
  678. }
  679. private static void EmitAluStore(ArmEmitterContext context, Operand value)
  680. {
  681. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  682. EmitGenericAluStoreA32(context, op.Rd, ShouldSetFlags(context), value);
  683. }
  684. }
  685. }