InstEmitAlu32.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using System.Diagnostics.CodeAnalysis;
  6. using static ARMeilleure.Instructions.InstEmitAluHelper;
  7. using static ARMeilleure.Instructions.InstEmitHelper;
  8. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  9. namespace ARMeilleure.Instructions
  10. {
  11. [SuppressMessage("Style", "IDE0059: Remove unnecessary value assignment")]
  12. static partial class InstEmit32
  13. {
  14. public static void Add(ArmEmitterContext context)
  15. {
  16. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  17. Operand n = GetAluN(context);
  18. Operand m = GetAluM(context, setCarry: false);
  19. if (op.Rn == RegisterAlias.Aarch32Pc && op is OpCodeT32AluImm12)
  20. {
  21. // For ADR, PC is always 4 bytes aligned, even in Thumb mode.
  22. n = context.BitwiseAnd(n, Const(~3u));
  23. }
  24. Operand res = context.Add(n, m);
  25. if (ShouldSetFlags(context))
  26. {
  27. EmitNZFlagsCheck(context, res);
  28. EmitAddsCCheck(context, n, res);
  29. EmitAddsVCheck(context, n, m, res);
  30. }
  31. EmitAluStore(context, res);
  32. }
  33. public static void Adc(ArmEmitterContext context)
  34. {
  35. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  36. Operand n = GetAluN(context);
  37. Operand m = GetAluM(context, setCarry: false);
  38. Operand res = context.Add(n, m);
  39. Operand carry = GetFlag(PState.CFlag);
  40. res = context.Add(res, carry);
  41. if (ShouldSetFlags(context))
  42. {
  43. EmitNZFlagsCheck(context, res);
  44. EmitAdcsCCheck(context, n, res);
  45. EmitAddsVCheck(context, n, m, res);
  46. }
  47. EmitAluStore(context, res);
  48. }
  49. public static void And(ArmEmitterContext context)
  50. {
  51. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  52. Operand n = GetAluN(context);
  53. Operand m = GetAluM(context);
  54. Operand res = context.BitwiseAnd(n, m);
  55. if (ShouldSetFlags(context))
  56. {
  57. EmitNZFlagsCheck(context, res);
  58. }
  59. EmitAluStore(context, res);
  60. }
  61. public static void Bfc(ArmEmitterContext context)
  62. {
  63. IOpCode32AluBf op = (IOpCode32AluBf)context.CurrOp;
  64. Operand d = GetIntA32(context, op.Rd);
  65. Operand res = context.BitwiseAnd(d, Const(~op.DestMask));
  66. SetIntA32(context, op.Rd, res);
  67. }
  68. public static void Bfi(ArmEmitterContext context)
  69. {
  70. IOpCode32AluBf op = (IOpCode32AluBf)context.CurrOp;
  71. Operand n = GetIntA32(context, op.Rn);
  72. Operand d = GetIntA32(context, op.Rd);
  73. Operand part = context.BitwiseAnd(n, Const(op.SourceMask));
  74. if (op.Lsb != 0)
  75. {
  76. part = context.ShiftLeft(part, Const(op.Lsb));
  77. }
  78. Operand res = context.BitwiseAnd(d, Const(~op.DestMask));
  79. res = context.BitwiseOr(res, context.BitwiseAnd(part, Const(op.DestMask)));
  80. SetIntA32(context, op.Rd, res);
  81. }
  82. public static void Bic(ArmEmitterContext context)
  83. {
  84. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  85. Operand n = GetAluN(context);
  86. Operand m = GetAluM(context);
  87. Operand res = context.BitwiseAnd(n, context.BitwiseNot(m));
  88. if (ShouldSetFlags(context))
  89. {
  90. EmitNZFlagsCheck(context, res);
  91. }
  92. EmitAluStore(context, res);
  93. }
  94. public static void Clz(ArmEmitterContext context)
  95. {
  96. Operand m = GetAluM(context, setCarry: false);
  97. Operand res = context.CountLeadingZeros(m);
  98. EmitAluStore(context, res);
  99. }
  100. public static void Cmp(ArmEmitterContext context)
  101. {
  102. Operand n = GetAluN(context);
  103. Operand m = GetAluM(context, setCarry: false);
  104. Operand res = context.Subtract(n, m);
  105. EmitNZFlagsCheck(context, res);
  106. EmitSubsCCheck(context, n, res);
  107. EmitSubsVCheck(context, n, m, res);
  108. }
  109. public static void Cmn(ArmEmitterContext context)
  110. {
  111. Operand n = GetAluN(context);
  112. Operand m = GetAluM(context, setCarry: false);
  113. Operand res = context.Add(n, m);
  114. EmitNZFlagsCheck(context, res);
  115. EmitAddsCCheck(context, n, res);
  116. EmitAddsVCheck(context, n, m, res);
  117. }
  118. public static void Eor(ArmEmitterContext context)
  119. {
  120. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  121. Operand n = GetAluN(context);
  122. Operand m = GetAluM(context);
  123. Operand res = context.BitwiseExclusiveOr(n, m);
  124. if (ShouldSetFlags(context))
  125. {
  126. EmitNZFlagsCheck(context, res);
  127. }
  128. EmitAluStore(context, res);
  129. }
  130. public static void Mov(ArmEmitterContext context)
  131. {
  132. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  133. Operand m = GetAluM(context);
  134. if (ShouldSetFlags(context))
  135. {
  136. EmitNZFlagsCheck(context, m);
  137. }
  138. EmitAluStore(context, m);
  139. }
  140. public static void Movt(ArmEmitterContext context)
  141. {
  142. IOpCode32AluImm16 op = (IOpCode32AluImm16)context.CurrOp;
  143. Operand d = GetIntA32(context, op.Rd);
  144. Operand imm = Const(op.Immediate << 16); // Immeditate value as top halfword.
  145. Operand res = context.BitwiseAnd(d, Const(0x0000ffff));
  146. res = context.BitwiseOr(res, imm);
  147. EmitAluStore(context, res);
  148. }
  149. public static void Mul(ArmEmitterContext context)
  150. {
  151. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  152. Operand n = GetAluN(context);
  153. Operand m = GetAluM(context);
  154. Operand res = context.Multiply(n, m);
  155. if (ShouldSetFlags(context))
  156. {
  157. EmitNZFlagsCheck(context, res);
  158. }
  159. EmitAluStore(context, res);
  160. }
  161. public static void Mvn(ArmEmitterContext context)
  162. {
  163. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  164. Operand m = GetAluM(context);
  165. Operand res = context.BitwiseNot(m);
  166. if (ShouldSetFlags(context))
  167. {
  168. EmitNZFlagsCheck(context, res);
  169. }
  170. EmitAluStore(context, res);
  171. }
  172. public static void Orr(ArmEmitterContext context)
  173. {
  174. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  175. Operand n = GetAluN(context);
  176. Operand m = GetAluM(context);
  177. Operand res = context.BitwiseOr(n, m);
  178. if (ShouldSetFlags(context))
  179. {
  180. EmitNZFlagsCheck(context, res);
  181. }
  182. EmitAluStore(context, res);
  183. }
  184. public static void Orn(ArmEmitterContext context)
  185. {
  186. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  187. Operand n = GetAluN(context);
  188. Operand m = GetAluM(context);
  189. Operand res = context.BitwiseOr(n, context.BitwiseNot(m));
  190. if (ShouldSetFlags(context))
  191. {
  192. EmitNZFlagsCheck(context, res);
  193. }
  194. EmitAluStore(context, res);
  195. }
  196. public static void Pkh(ArmEmitterContext context)
  197. {
  198. OpCode32AluRsImm op = (OpCode32AluRsImm)context.CurrOp;
  199. Operand n = GetAluN(context);
  200. Operand m = GetAluM(context);
  201. Operand res;
  202. bool tbform = op.ShiftType == ShiftType.Asr;
  203. if (tbform)
  204. {
  205. res = context.BitwiseOr(context.BitwiseAnd(n, Const(0xFFFF0000)), context.BitwiseAnd(m, Const(0xFFFF)));
  206. }
  207. else
  208. {
  209. res = context.BitwiseOr(context.BitwiseAnd(m, Const(0xFFFF0000)), context.BitwiseAnd(n, Const(0xFFFF)));
  210. }
  211. EmitAluStore(context, res);
  212. }
  213. public static void Rbit(ArmEmitterContext context)
  214. {
  215. Operand m = GetAluM(context);
  216. Operand res = EmitReverseBits32Op(context, m);
  217. EmitAluStore(context, res);
  218. }
  219. public static void Rev(ArmEmitterContext context)
  220. {
  221. Operand m = GetAluM(context);
  222. Operand res = context.ByteSwap(m);
  223. EmitAluStore(context, res);
  224. }
  225. public static void Rev16(ArmEmitterContext context)
  226. {
  227. Operand m = GetAluM(context);
  228. Operand res = EmitReverseBytes16_32Op(context, m);
  229. EmitAluStore(context, res);
  230. }
  231. public static void Revsh(ArmEmitterContext context)
  232. {
  233. Operand m = GetAluM(context);
  234. Operand res = EmitReverseBytes16_32Op(context, m);
  235. EmitAluStore(context, context.SignExtend16(OperandType.I32, res));
  236. }
  237. public static void Rsc(ArmEmitterContext context)
  238. {
  239. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  240. Operand n = GetAluN(context);
  241. Operand m = GetAluM(context, setCarry: false);
  242. Operand res = context.Subtract(m, n);
  243. Operand borrow = context.BitwiseExclusiveOr(GetFlag(PState.CFlag), Const(1));
  244. res = context.Subtract(res, borrow);
  245. if (ShouldSetFlags(context))
  246. {
  247. EmitNZFlagsCheck(context, res);
  248. EmitSbcsCCheck(context, m, n);
  249. EmitSubsVCheck(context, m, n, res);
  250. }
  251. EmitAluStore(context, res);
  252. }
  253. public static void Rsb(ArmEmitterContext context)
  254. {
  255. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  256. Operand n = GetAluN(context);
  257. Operand m = GetAluM(context, setCarry: false);
  258. Operand res = context.Subtract(m, n);
  259. if (ShouldSetFlags(context))
  260. {
  261. EmitNZFlagsCheck(context, res);
  262. EmitSubsCCheck(context, m, res);
  263. EmitSubsVCheck(context, m, n, res);
  264. }
  265. EmitAluStore(context, res);
  266. }
  267. public static void Sadd8(ArmEmitterContext context)
  268. {
  269. EmitAddSub8(context, add: true, unsigned: false);
  270. }
  271. public static void Sbc(ArmEmitterContext context)
  272. {
  273. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  274. Operand n = GetAluN(context);
  275. Operand m = GetAluM(context, setCarry: false);
  276. Operand res = context.Subtract(n, m);
  277. Operand borrow = context.BitwiseExclusiveOr(GetFlag(PState.CFlag), Const(1));
  278. res = context.Subtract(res, borrow);
  279. if (ShouldSetFlags(context))
  280. {
  281. EmitNZFlagsCheck(context, res);
  282. EmitSbcsCCheck(context, n, m);
  283. EmitSubsVCheck(context, n, m, res);
  284. }
  285. EmitAluStore(context, res);
  286. }
  287. public static void Sbfx(ArmEmitterContext context)
  288. {
  289. IOpCode32AluBf op = (IOpCode32AluBf)context.CurrOp;
  290. var msb = op.Lsb + op.Msb; // For this instruction, the msb is actually a width.
  291. Operand n = GetIntA32(context, op.Rn);
  292. Operand res = context.ShiftRightSI(context.ShiftLeft(n, Const(31 - msb)), Const(31 - op.Msb));
  293. SetIntA32(context, op.Rd, res);
  294. }
  295. public static void Sdiv(ArmEmitterContext context)
  296. {
  297. EmitDiv(context, unsigned: false);
  298. }
  299. public static void Sel(ArmEmitterContext context)
  300. {
  301. IOpCode32AluReg op = (IOpCode32AluReg)context.CurrOp;
  302. Operand n = GetIntA32(context, op.Rn);
  303. Operand m = GetIntA32(context, op.Rm);
  304. Operand ge0 = context.ZeroExtend8(OperandType.I32, context.Negate(GetFlag(PState.GE0Flag)));
  305. Operand ge1 = context.ZeroExtend8(OperandType.I32, context.Negate(GetFlag(PState.GE1Flag)));
  306. Operand ge2 = context.ZeroExtend8(OperandType.I32, context.Negate(GetFlag(PState.GE2Flag)));
  307. Operand ge3 = context.Negate(GetFlag(PState.GE3Flag));
  308. Operand mask = context.BitwiseOr(ge0, context.ShiftLeft(ge1, Const(8)));
  309. mask = context.BitwiseOr(mask, context.ShiftLeft(ge2, Const(16)));
  310. mask = context.BitwiseOr(mask, context.ShiftLeft(ge3, Const(24)));
  311. Operand res = context.BitwiseOr(context.BitwiseAnd(n, mask), context.BitwiseAnd(m, context.BitwiseNot(mask)));
  312. SetIntA32(context, op.Rd, res);
  313. }
  314. public static void Shadd8(ArmEmitterContext context)
  315. {
  316. EmitHadd8(context, unsigned: false);
  317. }
  318. public static void Shsub8(ArmEmitterContext context)
  319. {
  320. EmitHsub8(context, unsigned: false);
  321. }
  322. public static void Ssat(ArmEmitterContext context)
  323. {
  324. OpCode32Sat op = (OpCode32Sat)context.CurrOp;
  325. EmitSat(context, -(1 << op.SatImm), (1 << op.SatImm) - 1);
  326. }
  327. public static void Ssat16(ArmEmitterContext context)
  328. {
  329. OpCode32Sat16 op = (OpCode32Sat16)context.CurrOp;
  330. EmitSat16(context, -(1 << op.SatImm), (1 << op.SatImm) - 1);
  331. }
  332. public static void Ssub8(ArmEmitterContext context)
  333. {
  334. EmitAddSub8(context, add: false, unsigned: false);
  335. }
  336. public static void Sub(ArmEmitterContext context)
  337. {
  338. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  339. Operand n = GetAluN(context);
  340. Operand m = GetAluM(context, setCarry: false);
  341. if (op.Rn == RegisterAlias.Aarch32Pc && op is OpCodeT32AluImm12)
  342. {
  343. // For ADR, PC is always 4 bytes aligned, even in Thumb mode.
  344. n = context.BitwiseAnd(n, Const(~3u));
  345. }
  346. Operand res = context.Subtract(n, m);
  347. if (ShouldSetFlags(context))
  348. {
  349. EmitNZFlagsCheck(context, res);
  350. EmitSubsCCheck(context, n, res);
  351. EmitSubsVCheck(context, n, m, res);
  352. }
  353. EmitAluStore(context, res);
  354. }
  355. public static void Sxtb(ArmEmitterContext context)
  356. {
  357. EmitSignExtend(context, true, 8);
  358. }
  359. public static void Sxtb16(ArmEmitterContext context)
  360. {
  361. EmitExtend16(context, true);
  362. }
  363. public static void Sxth(ArmEmitterContext context)
  364. {
  365. EmitSignExtend(context, true, 16);
  366. }
  367. public static void Teq(ArmEmitterContext context)
  368. {
  369. Operand n = GetAluN(context);
  370. Operand m = GetAluM(context);
  371. Operand res = context.BitwiseExclusiveOr(n, m);
  372. EmitNZFlagsCheck(context, res);
  373. }
  374. public static void Tst(ArmEmitterContext context)
  375. {
  376. Operand n = GetAluN(context);
  377. Operand m = GetAluM(context);
  378. Operand res = context.BitwiseAnd(n, m);
  379. EmitNZFlagsCheck(context, res);
  380. }
  381. public static void Uadd8(ArmEmitterContext context)
  382. {
  383. EmitAddSub8(context, add: true, unsigned: true);
  384. }
  385. public static void Ubfx(ArmEmitterContext context)
  386. {
  387. IOpCode32AluBf op = (IOpCode32AluBf)context.CurrOp;
  388. var msb = op.Lsb + op.Msb; // For this instruction, the msb is actually a width.
  389. Operand n = GetIntA32(context, op.Rn);
  390. Operand res = context.ShiftRightUI(context.ShiftLeft(n, Const(31 - msb)), Const(31 - op.Msb));
  391. SetIntA32(context, op.Rd, res);
  392. }
  393. public static void Udiv(ArmEmitterContext context)
  394. {
  395. EmitDiv(context, unsigned: true);
  396. }
  397. public static void Uhadd8(ArmEmitterContext context)
  398. {
  399. EmitHadd8(context, unsigned: true);
  400. }
  401. public static void Uhsub8(ArmEmitterContext context)
  402. {
  403. EmitHsub8(context, unsigned: true);
  404. }
  405. public static void Usat(ArmEmitterContext context)
  406. {
  407. OpCode32Sat op = (OpCode32Sat)context.CurrOp;
  408. EmitSat(context, 0, op.SatImm == 32 ? (int)(~0) : (1 << op.SatImm) - 1);
  409. }
  410. public static void Usat16(ArmEmitterContext context)
  411. {
  412. OpCode32Sat16 op = (OpCode32Sat16)context.CurrOp;
  413. EmitSat16(context, 0, (1 << op.SatImm) - 1);
  414. }
  415. public static void Usub8(ArmEmitterContext context)
  416. {
  417. EmitAddSub8(context, add: false, unsigned: true);
  418. }
  419. public static void Uxtb(ArmEmitterContext context)
  420. {
  421. EmitSignExtend(context, false, 8);
  422. }
  423. public static void Uxtb16(ArmEmitterContext context)
  424. {
  425. EmitExtend16(context, false);
  426. }
  427. public static void Uxth(ArmEmitterContext context)
  428. {
  429. EmitSignExtend(context, false, 16);
  430. }
  431. private static void EmitSignExtend(ArmEmitterContext context, bool signed, int bits)
  432. {
  433. IOpCode32AluUx op = (IOpCode32AluUx)context.CurrOp;
  434. Operand m = GetAluM(context);
  435. Operand res;
  436. if (op.RotateBits == 0)
  437. {
  438. res = m;
  439. }
  440. else
  441. {
  442. Operand rotate = Const(op.RotateBits);
  443. res = context.RotateRight(m, rotate);
  444. }
  445. switch (bits)
  446. {
  447. case 8:
  448. res = (signed) ? context.SignExtend8(OperandType.I32, res) : context.ZeroExtend8(OperandType.I32, res);
  449. break;
  450. case 16:
  451. res = (signed) ? context.SignExtend16(OperandType.I32, res) : context.ZeroExtend16(OperandType.I32, res);
  452. break;
  453. }
  454. if (op.Add)
  455. {
  456. res = context.Add(res, GetAluN(context));
  457. }
  458. EmitAluStore(context, res);
  459. }
  460. private static void EmitExtend16(ArmEmitterContext context, bool signed)
  461. {
  462. IOpCode32AluUx op = (IOpCode32AluUx)context.CurrOp;
  463. Operand m = GetAluM(context);
  464. Operand res;
  465. if (op.RotateBits == 0)
  466. {
  467. res = m;
  468. }
  469. else
  470. {
  471. Operand rotate = Const(op.RotateBits);
  472. res = context.RotateRight(m, rotate);
  473. }
  474. Operand low16, high16;
  475. if (signed)
  476. {
  477. low16 = context.SignExtend8(OperandType.I32, res);
  478. high16 = context.SignExtend8(OperandType.I32, context.ShiftRightUI(res, Const(16)));
  479. }
  480. else
  481. {
  482. low16 = context.ZeroExtend8(OperandType.I32, res);
  483. high16 = context.ZeroExtend8(OperandType.I32, context.ShiftRightUI(res, Const(16)));
  484. }
  485. if (op.Add)
  486. {
  487. Operand n = GetAluN(context);
  488. Operand lowAdd, highAdd;
  489. if (signed)
  490. {
  491. lowAdd = context.SignExtend16(OperandType.I32, n);
  492. highAdd = context.SignExtend16(OperandType.I32, context.ShiftRightUI(n, Const(16)));
  493. }
  494. else
  495. {
  496. lowAdd = context.ZeroExtend16(OperandType.I32, n);
  497. highAdd = context.ZeroExtend16(OperandType.I32, context.ShiftRightUI(n, Const(16)));
  498. }
  499. low16 = context.Add(low16, lowAdd);
  500. high16 = context.Add(high16, highAdd);
  501. }
  502. res = context.BitwiseOr(
  503. context.ZeroExtend16(OperandType.I32, low16),
  504. context.ShiftLeft(context.ZeroExtend16(OperandType.I32, high16), Const(16)));
  505. EmitAluStore(context, res);
  506. }
  507. private static void EmitDiv(ArmEmitterContext context, bool unsigned)
  508. {
  509. Operand n = GetAluN(context);
  510. Operand m = GetAluM(context);
  511. Operand zero = Const(m.Type, 0);
  512. Operand divisorIsZero = context.ICompareEqual(m, zero);
  513. Operand lblBadDiv = Label();
  514. Operand lblEnd = Label();
  515. context.BranchIfTrue(lblBadDiv, divisorIsZero);
  516. if (!unsigned)
  517. {
  518. // ARM64 behaviour: If Rn == INT_MIN && Rm == -1, Rd = INT_MIN (overflow).
  519. // TODO: tests to ensure A32 works the same
  520. Operand intMin = Const(int.MinValue);
  521. Operand minus1 = Const(-1);
  522. Operand nIsIntMin = context.ICompareEqual(n, intMin);
  523. Operand mIsMinus1 = context.ICompareEqual(m, minus1);
  524. Operand lblGoodDiv = Label();
  525. context.BranchIfFalse(lblGoodDiv, context.BitwiseAnd(nIsIntMin, mIsMinus1));
  526. EmitAluStore(context, intMin);
  527. context.Branch(lblEnd);
  528. context.MarkLabel(lblGoodDiv);
  529. }
  530. Operand res = unsigned
  531. ? context.DivideUI(n, m)
  532. : context.Divide(n, m);
  533. EmitAluStore(context, res);
  534. context.Branch(lblEnd);
  535. context.MarkLabel(lblBadDiv);
  536. EmitAluStore(context, zero);
  537. context.MarkLabel(lblEnd);
  538. }
  539. private static void EmitAddSub8(ArmEmitterContext context, bool add, bool unsigned)
  540. {
  541. IOpCode32AluReg op = (IOpCode32AluReg)context.CurrOp;
  542. Operand n = GetIntA32(context, op.Rn);
  543. Operand m = GetIntA32(context, op.Rm);
  544. Operand res = Const(0);
  545. for (int byteSel = 0; byteSel < 4; byteSel++)
  546. {
  547. Operand shift = Const(byteSel * 8);
  548. Operand nByte = context.ShiftRightUI(n, shift);
  549. Operand mByte = context.ShiftRightUI(m, shift);
  550. nByte = unsigned ? context.ZeroExtend8(OperandType.I32, nByte) : context.SignExtend8(OperandType.I32, nByte);
  551. mByte = unsigned ? context.ZeroExtend8(OperandType.I32, mByte) : context.SignExtend8(OperandType.I32, mByte);
  552. Operand resByte = add ? context.Add(nByte, mByte) : context.Subtract(nByte, mByte);
  553. res = context.BitwiseOr(res, context.ShiftLeft(context.ZeroExtend8(OperandType.I32, resByte), shift));
  554. SetFlag(context, PState.GE0Flag + byteSel, unsigned && add
  555. ? context.ShiftRightUI(resByte, Const(8))
  556. : context.ShiftRightUI(context.BitwiseNot(resByte), Const(31)));
  557. }
  558. SetIntA32(context, op.Rd, res);
  559. }
  560. private static void EmitHadd8(ArmEmitterContext context, bool unsigned)
  561. {
  562. IOpCode32AluReg op = (IOpCode32AluReg)context.CurrOp;
  563. Operand m = GetIntA32(context, op.Rm);
  564. Operand n = GetIntA32(context, op.Rn);
  565. Operand xor, res, carry;
  566. // This relies on the equality x+y == ((x&y) << 1) + (x^y).
  567. // Note that x^y always contains the LSB of the result.
  568. // Since we want to calculate (x+y)/2, we can instead calculate (x&y) + ((x^y)>>1).
  569. // We mask by 0x7F to remove the LSB so that it doesn't leak into the field below.
  570. res = context.BitwiseAnd(m, n);
  571. carry = context.BitwiseExclusiveOr(m, n);
  572. xor = context.ShiftRightUI(carry, Const(1));
  573. xor = context.BitwiseAnd(xor, Const(0x7F7F7F7Fu));
  574. res = context.Add(res, xor);
  575. if (!unsigned)
  576. {
  577. // Propagates the sign bit from (x^y)>>1 upwards by one.
  578. carry = context.BitwiseAnd(carry, Const(0x80808080u));
  579. res = context.BitwiseExclusiveOr(res, carry);
  580. }
  581. SetIntA32(context, op.Rd, res);
  582. }
  583. private static void EmitHsub8(ArmEmitterContext context, bool unsigned)
  584. {
  585. IOpCode32AluReg op = (IOpCode32AluReg)context.CurrOp;
  586. Operand m = GetIntA32(context, op.Rm);
  587. Operand n = GetIntA32(context, op.Rn);
  588. Operand left, right, carry, res;
  589. // This relies on the equality x-y == (x^y) - (((x^y)&y) << 1).
  590. // Note that x^y always contains the LSB of the result.
  591. // Since we want to calculate (x+y)/2, we can instead calculate ((x^y)>>1) - ((x^y)&y).
  592. carry = context.BitwiseExclusiveOr(m, n);
  593. left = context.ShiftRightUI(carry, Const(1));
  594. right = context.BitwiseAnd(carry, m);
  595. // We must now perform a partitioned subtraction.
  596. // We can do this because minuend contains 7 bit fields.
  597. // We use the extra bit in minuend as a bit to borrow from; we set this bit.
  598. // We invert this bit at the end as this tells us if that bit was borrowed from.
  599. res = context.BitwiseOr(left, Const(0x80808080));
  600. res = context.Subtract(res, right);
  601. res = context.BitwiseExclusiveOr(res, Const(0x80808080));
  602. if (!unsigned)
  603. {
  604. // We then sign extend the result into this bit.
  605. carry = context.BitwiseAnd(carry, Const(0x80808080));
  606. res = context.BitwiseExclusiveOr(res, carry);
  607. }
  608. SetIntA32(context, op.Rd, res);
  609. }
  610. private static void EmitSat(ArmEmitterContext context, int intMin, int intMax)
  611. {
  612. OpCode32Sat op = (OpCode32Sat)context.CurrOp;
  613. Operand n = GetIntA32(context, op.Rn);
  614. int shift = DecodeImmShift(op.ShiftType, op.Imm5);
  615. switch (op.ShiftType)
  616. {
  617. case ShiftType.Lsl:
  618. if (shift == 32)
  619. {
  620. n = Const(0);
  621. }
  622. else
  623. {
  624. n = context.ShiftLeft(n, Const(shift));
  625. }
  626. break;
  627. case ShiftType.Asr:
  628. if (shift == 32)
  629. {
  630. n = context.ShiftRightSI(n, Const(31));
  631. }
  632. else
  633. {
  634. n = context.ShiftRightSI(n, Const(shift));
  635. }
  636. break;
  637. }
  638. Operand lblCheckLtIntMin = Label();
  639. Operand lblNoSat = Label();
  640. Operand lblEnd = Label();
  641. context.BranchIfFalse(lblCheckLtIntMin, context.ICompareGreater(n, Const(intMax)));
  642. SetFlag(context, PState.QFlag, Const(1));
  643. SetIntA32(context, op.Rd, Const(intMax));
  644. context.Branch(lblEnd);
  645. context.MarkLabel(lblCheckLtIntMin);
  646. context.BranchIfFalse(lblNoSat, context.ICompareLess(n, Const(intMin)));
  647. SetFlag(context, PState.QFlag, Const(1));
  648. SetIntA32(context, op.Rd, Const(intMin));
  649. context.Branch(lblEnd);
  650. context.MarkLabel(lblNoSat);
  651. SetIntA32(context, op.Rd, n);
  652. context.MarkLabel(lblEnd);
  653. }
  654. private static void EmitSat16(ArmEmitterContext context, int intMin, int intMax)
  655. {
  656. OpCode32Sat16 op = (OpCode32Sat16)context.CurrOp;
  657. void SetD(int part, Operand value)
  658. {
  659. if (part == 0)
  660. {
  661. SetIntA32(context, op.Rd, context.ZeroExtend16(OperandType.I32, value));
  662. }
  663. else
  664. {
  665. SetIntA32(context, op.Rd, context.BitwiseOr(GetIntA32(context, op.Rd), context.ShiftLeft(value, Const(16))));
  666. }
  667. }
  668. Operand n = GetIntA32(context, op.Rn);
  669. Operand nLow = context.SignExtend16(OperandType.I32, n);
  670. Operand nHigh = context.ShiftRightSI(n, Const(16));
  671. for (int part = 0; part < 2; part++)
  672. {
  673. Operand nPart = part == 0 ? nLow : nHigh;
  674. Operand lblCheckLtIntMin = Label();
  675. Operand lblNoSat = Label();
  676. Operand lblEnd = Label();
  677. context.BranchIfFalse(lblCheckLtIntMin, context.ICompareGreater(nPart, Const(intMax)));
  678. SetFlag(context, PState.QFlag, Const(1));
  679. SetD(part, Const(intMax));
  680. context.Branch(lblEnd);
  681. context.MarkLabel(lblCheckLtIntMin);
  682. context.BranchIfFalse(lblNoSat, context.ICompareLess(nPart, Const(intMin)));
  683. SetFlag(context, PState.QFlag, Const(1));
  684. SetD(part, Const(intMin));
  685. context.Branch(lblEnd);
  686. context.MarkLabel(lblNoSat);
  687. SetD(part, nPart);
  688. context.MarkLabel(lblEnd);
  689. }
  690. }
  691. private static void EmitAluStore(ArmEmitterContext context, Operand value)
  692. {
  693. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  694. EmitGenericAluStoreA32(context, op.Rd, ShouldSetFlags(context), value);
  695. }
  696. }
  697. }