InstEmitAluHelper.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using System.Diagnostics;
  7. using static ARMeilleure.Instructions.InstEmitHelper;
  8. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  9. namespace ARMeilleure.Instructions
  10. {
  11. static class InstEmitAluHelper
  12. {
  13. public static bool ShouldSetFlags(ArmEmitterContext context)
  14. {
  15. IOpCode32HasSetFlags op = (IOpCode32HasSetFlags)context.CurrOp;
  16. if (op.SetFlags == null)
  17. {
  18. return !context.IsInIfThenBlock;
  19. }
  20. return op.SetFlags.Value;
  21. }
  22. public static void EmitNZFlagsCheck(ArmEmitterContext context, Operand d)
  23. {
  24. SetFlag(context, PState.NFlag, context.ICompareLess (d, Const(d.Type, 0)));
  25. SetFlag(context, PState.ZFlag, context.ICompareEqual(d, Const(d.Type, 0)));
  26. }
  27. public static void EmitAdcsCCheck(ArmEmitterContext context, Operand n, Operand d)
  28. {
  29. // C = (Rd == Rn && CIn) || Rd < Rn
  30. Operand cIn = GetFlag(PState.CFlag);
  31. Operand cOut = context.BitwiseAnd(context.ICompareEqual(d, n), cIn);
  32. cOut = context.BitwiseOr(cOut, context.ICompareLessUI(d, n));
  33. SetFlag(context, PState.CFlag, cOut);
  34. }
  35. public static void EmitAddsCCheck(ArmEmitterContext context, Operand n, Operand d)
  36. {
  37. // C = Rd < Rn
  38. SetFlag(context, PState.CFlag, context.ICompareLessUI(d, n));
  39. }
  40. public static void EmitAddsVCheck(ArmEmitterContext context, Operand n, Operand m, Operand d)
  41. {
  42. // V = (Rd ^ Rn) & ~(Rn ^ Rm) < 0
  43. Operand vOut = context.BitwiseExclusiveOr(d, n);
  44. vOut = context.BitwiseAnd(vOut, context.BitwiseNot(context.BitwiseExclusiveOr(n, m)));
  45. vOut = context.ICompareLess(vOut, Const(vOut.Type, 0));
  46. SetFlag(context, PState.VFlag, vOut);
  47. }
  48. public static void EmitSbcsCCheck(ArmEmitterContext context, Operand n, Operand m)
  49. {
  50. // C = (Rn == Rm && CIn) || Rn > Rm
  51. Operand cIn = GetFlag(PState.CFlag);
  52. Operand cOut = context.BitwiseAnd(context.ICompareEqual(n, m), cIn);
  53. cOut = context.BitwiseOr(cOut, context.ICompareGreaterUI(n, m));
  54. SetFlag(context, PState.CFlag, cOut);
  55. }
  56. public static void EmitSubsCCheck(ArmEmitterContext context, Operand n, Operand m)
  57. {
  58. // C = Rn >= Rm
  59. SetFlag(context, PState.CFlag, context.ICompareGreaterOrEqualUI(n, m));
  60. }
  61. public static void EmitSubsVCheck(ArmEmitterContext context, Operand n, Operand m, Operand d)
  62. {
  63. // V = (Rd ^ Rn) & (Rn ^ Rm) < 0
  64. Operand vOut = context.BitwiseExclusiveOr(d, n);
  65. vOut = context.BitwiseAnd(vOut, context.BitwiseExclusiveOr(n, m));
  66. vOut = context.ICompareLess(vOut, Const(vOut.Type, 0));
  67. SetFlag(context, PState.VFlag, vOut);
  68. }
  69. public static Operand EmitReverseBits32Op(ArmEmitterContext context, Operand op)
  70. {
  71. Debug.Assert(op.Type == OperandType.I32);
  72. Operand val = context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(op, Const(0xaaaaaaaau)), Const(1)),
  73. context.ShiftLeft(context.BitwiseAnd(op, Const(0x55555555u)), Const(1)));
  74. val = context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(val, Const(0xccccccccu)), Const(2)),
  75. context.ShiftLeft(context.BitwiseAnd(val, Const(0x33333333u)), Const(2)));
  76. val = context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(val, Const(0xf0f0f0f0u)), Const(4)),
  77. context.ShiftLeft(context.BitwiseAnd(val, Const(0x0f0f0f0fu)), Const(4)));
  78. val = context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(val, Const(0xff00ff00u)), Const(8)),
  79. context.ShiftLeft(context.BitwiseAnd(val, Const(0x00ff00ffu)), Const(8)));
  80. return context.BitwiseOr(context.ShiftRightUI(val, Const(16)), context.ShiftLeft(val, Const(16)));
  81. }
  82. public static Operand EmitReverseBytes16_64Op(ArmEmitterContext context, Operand op)
  83. {
  84. Debug.Assert(op.Type == OperandType.I64);
  85. return context.BitwiseOr(context.ShiftRightUI(context.BitwiseAnd(op, Const(0xff00ff00ff00ff00ul)), Const(8)),
  86. context.ShiftLeft(context.BitwiseAnd(op, Const(0x00ff00ff00ff00fful)), Const(8)));
  87. }
  88. public static Operand EmitReverseBytes16_32Op(ArmEmitterContext context, Operand op)
  89. {
  90. Debug.Assert(op.Type == OperandType.I32);
  91. Operand val = EmitReverseBytes16_64Op(context, context.ZeroExtend32(OperandType.I64, op));
  92. return context.ConvertI64ToI32(val);
  93. }
  94. private static void EmitAluWritePc(ArmEmitterContext context, Operand value)
  95. {
  96. Debug.Assert(value.Type == OperandType.I32);
  97. if (((OpCode32)context.CurrOp).IsThumb)
  98. {
  99. bool isReturn = IsA32Return(context);
  100. if (!isReturn)
  101. {
  102. context.StoreToContext();
  103. }
  104. InstEmitFlowHelper.EmitVirtualJump(context, value, isReturn);
  105. }
  106. else
  107. {
  108. EmitBxWritePc(context, value);
  109. }
  110. }
  111. public static void EmitGenericAluStoreA32(ArmEmitterContext context, int rd, bool setFlags, Operand value)
  112. {
  113. Debug.Assert(value.Type == OperandType.I32);
  114. if (rd == RegisterAlias.Aarch32Pc && setFlags)
  115. {
  116. if (setFlags)
  117. {
  118. // TODO: Load SPSR etc.
  119. EmitBxWritePc(context, value);
  120. }
  121. else
  122. {
  123. EmitAluWritePc(context, value);
  124. }
  125. }
  126. else
  127. {
  128. SetIntA32(context, rd, value);
  129. }
  130. }
  131. public static Operand GetAluN(ArmEmitterContext context)
  132. {
  133. if (context.CurrOp is IOpCodeAlu op)
  134. {
  135. if (op.DataOp == DataOp.Logical || op is IOpCodeAluRs)
  136. {
  137. return GetIntOrZR(context, op.Rn);
  138. }
  139. else
  140. {
  141. return GetIntOrSP(context, op.Rn);
  142. }
  143. }
  144. else if (context.CurrOp is IOpCode32Alu op32)
  145. {
  146. return GetIntA32(context, op32.Rn);
  147. }
  148. else
  149. {
  150. throw InvalidOpCodeType(context.CurrOp);
  151. }
  152. }
  153. public static Operand GetAluM(ArmEmitterContext context, bool setCarry = true)
  154. {
  155. switch (context.CurrOp)
  156. {
  157. // ARM32.
  158. case IOpCode32AluImm op:
  159. {
  160. if (ShouldSetFlags(context) && op.IsRotated && setCarry)
  161. {
  162. SetFlag(context, PState.CFlag, Const((uint)op.Immediate >> 31));
  163. }
  164. return Const(op.Immediate);
  165. }
  166. case IOpCode32AluImm16 op: return Const(op.Immediate);
  167. case IOpCode32AluRsImm op: return GetMShiftedByImmediate(context, op, setCarry);
  168. case IOpCode32AluRsReg op: return GetMShiftedByReg(context, op, setCarry);
  169. case IOpCode32AluReg op: return GetIntA32(context, op.Rm);
  170. // ARM64.
  171. case IOpCodeAluImm op:
  172. {
  173. if (op.GetOperandType() == OperandType.I32)
  174. {
  175. return Const((int)op.Immediate);
  176. }
  177. else
  178. {
  179. return Const(op.Immediate);
  180. }
  181. }
  182. case IOpCodeAluRs op:
  183. {
  184. Operand value = GetIntOrZR(context, op.Rm);
  185. switch (op.ShiftType)
  186. {
  187. case ShiftType.Lsl: value = context.ShiftLeft (value, Const(op.Shift)); break;
  188. case ShiftType.Lsr: value = context.ShiftRightUI(value, Const(op.Shift)); break;
  189. case ShiftType.Asr: value = context.ShiftRightSI(value, Const(op.Shift)); break;
  190. case ShiftType.Ror: value = context.RotateRight (value, Const(op.Shift)); break;
  191. }
  192. return value;
  193. }
  194. case IOpCodeAluRx op:
  195. {
  196. Operand value = GetExtendedM(context, op.Rm, op.IntType);
  197. value = context.ShiftLeft(value, Const(op.Shift));
  198. return value;
  199. }
  200. default: throw InvalidOpCodeType(context.CurrOp);
  201. }
  202. }
  203. private static Exception InvalidOpCodeType(OpCode opCode)
  204. {
  205. return new InvalidOperationException($"Invalid OpCode type \"{opCode?.GetType().Name ?? "null"}\".");
  206. }
  207. // ARM32 helpers.
  208. public static Operand GetMShiftedByImmediate(ArmEmitterContext context, IOpCode32AluRsImm op, bool setCarry)
  209. {
  210. Operand m = GetIntA32(context, op.Rm);
  211. int shift = op.Immediate;
  212. if (shift == 0)
  213. {
  214. switch (op.ShiftType)
  215. {
  216. case ShiftType.Lsr: shift = 32; break;
  217. case ShiftType.Asr: shift = 32; break;
  218. case ShiftType.Ror: shift = 1; break;
  219. }
  220. }
  221. if (shift != 0)
  222. {
  223. setCarry &= ShouldSetFlags(context);
  224. switch (op.ShiftType)
  225. {
  226. case ShiftType.Lsl: m = GetLslC(context, m, setCarry, shift); break;
  227. case ShiftType.Lsr: m = GetLsrC(context, m, setCarry, shift); break;
  228. case ShiftType.Asr: m = GetAsrC(context, m, setCarry, shift); break;
  229. case ShiftType.Ror:
  230. if (op.Immediate != 0)
  231. {
  232. m = GetRorC(context, m, setCarry, shift);
  233. }
  234. else
  235. {
  236. m = GetRrxC(context, m, setCarry);
  237. }
  238. break;
  239. }
  240. }
  241. return m;
  242. }
  243. public static int DecodeImmShift(ShiftType shiftType, int shift)
  244. {
  245. if (shift == 0)
  246. {
  247. switch (shiftType)
  248. {
  249. case ShiftType.Lsr: shift = 32; break;
  250. case ShiftType.Asr: shift = 32; break;
  251. case ShiftType.Ror: shift = 1; break;
  252. }
  253. }
  254. return shift;
  255. }
  256. public static Operand GetMShiftedByReg(ArmEmitterContext context, IOpCode32AluRsReg op, bool setCarry)
  257. {
  258. Operand m = GetIntA32(context, op.Rm);
  259. Operand s = context.ZeroExtend8(OperandType.I32, GetIntA32(context, op.Rs));
  260. Operand shiftIsZero = context.ICompareEqual(s, Const(0));
  261. Operand zeroResult = m;
  262. Operand shiftResult = m;
  263. setCarry &= ShouldSetFlags(context);
  264. switch (op.ShiftType)
  265. {
  266. case ShiftType.Lsl: shiftResult = EmitLslC(context, m, setCarry, s, shiftIsZero); break;
  267. case ShiftType.Lsr: shiftResult = EmitLsrC(context, m, setCarry, s, shiftIsZero); break;
  268. case ShiftType.Asr: shiftResult = EmitAsrC(context, m, setCarry, s, shiftIsZero); break;
  269. case ShiftType.Ror: shiftResult = EmitRorC(context, m, setCarry, s, shiftIsZero); break;
  270. }
  271. return context.ConditionalSelect(shiftIsZero, zeroResult, shiftResult);
  272. }
  273. public static void EmitIfHelper(ArmEmitterContext context, Operand boolValue, Action action, bool expected = true)
  274. {
  275. Debug.Assert(boolValue.Type == OperandType.I32);
  276. Operand endLabel = Label();
  277. if (expected)
  278. {
  279. context.BranchIfFalse(endLabel, boolValue);
  280. }
  281. else
  282. {
  283. context.BranchIfTrue(endLabel, boolValue);
  284. }
  285. action();
  286. context.MarkLabel(endLabel);
  287. }
  288. public static Operand EmitLslC(ArmEmitterContext context, Operand m, bool setCarry, Operand shift, Operand shiftIsZero)
  289. {
  290. Debug.Assert(m.Type == OperandType.I32 && shift.Type == OperandType.I32 && shiftIsZero.Type == OperandType.I32);
  291. Operand shiftLarge = context.ICompareGreaterOrEqual(shift, Const(32));
  292. Operand result = context.ShiftLeft(m, shift);
  293. if (setCarry)
  294. {
  295. EmitIfHelper(context, shiftIsZero, () =>
  296. {
  297. Operand cOut = context.ShiftRightUI(m, context.Subtract(Const(32), shift));
  298. cOut = context.BitwiseAnd(cOut, Const(1));
  299. cOut = context.ConditionalSelect(context.ICompareGreater(shift, Const(32)), Const(0), cOut);
  300. SetFlag(context, PState.CFlag, cOut);
  301. }, false);
  302. }
  303. return context.ConditionalSelect(shiftLarge, Const(0), result);
  304. }
  305. public static Operand GetLslC(ArmEmitterContext context, Operand m, bool setCarry, int shift)
  306. {
  307. Debug.Assert(m.Type == OperandType.I32);
  308. if ((uint)shift > 32)
  309. {
  310. return GetShiftByMoreThan32(context, setCarry);
  311. }
  312. else if (shift == 32)
  313. {
  314. if (setCarry)
  315. {
  316. SetCarryMLsb(context, m);
  317. }
  318. return Const(0);
  319. }
  320. else
  321. {
  322. if (setCarry)
  323. {
  324. Operand cOut = context.ShiftRightUI(m, Const(32 - shift));
  325. cOut = context.BitwiseAnd(cOut, Const(1));
  326. SetFlag(context, PState.CFlag, cOut);
  327. }
  328. return context.ShiftLeft(m, Const(shift));
  329. }
  330. }
  331. public static Operand EmitLsrC(ArmEmitterContext context, Operand m, bool setCarry, Operand shift, Operand shiftIsZero)
  332. {
  333. Debug.Assert(m.Type == OperandType.I32 && shift.Type == OperandType.I32 && shiftIsZero.Type == OperandType.I32);
  334. Operand shiftLarge = context.ICompareGreaterOrEqual(shift, Const(32));
  335. Operand result = context.ShiftRightUI(m, shift);
  336. if (setCarry)
  337. {
  338. EmitIfHelper(context, shiftIsZero, () =>
  339. {
  340. Operand cOut = context.ShiftRightUI(m, context.Subtract(shift, Const(1)));
  341. cOut = context.BitwiseAnd(cOut, Const(1));
  342. cOut = context.ConditionalSelect(context.ICompareGreater(shift, Const(32)), Const(0), cOut);
  343. SetFlag(context, PState.CFlag, cOut);
  344. }, false);
  345. }
  346. return context.ConditionalSelect(shiftLarge, Const(0), result);
  347. }
  348. public static Operand GetLsrC(ArmEmitterContext context, Operand m, bool setCarry, int shift)
  349. {
  350. Debug.Assert(m.Type == OperandType.I32);
  351. if ((uint)shift > 32)
  352. {
  353. return GetShiftByMoreThan32(context, setCarry);
  354. }
  355. else if (shift == 32)
  356. {
  357. if (setCarry)
  358. {
  359. SetCarryMMsb(context, m);
  360. }
  361. return Const(0);
  362. }
  363. else
  364. {
  365. if (setCarry)
  366. {
  367. SetCarryMShrOut(context, m, shift);
  368. }
  369. return context.ShiftRightUI(m, Const(shift));
  370. }
  371. }
  372. private static Operand GetShiftByMoreThan32(ArmEmitterContext context, bool setCarry)
  373. {
  374. if (setCarry)
  375. {
  376. SetFlag(context, PState.CFlag, Const(0));
  377. }
  378. return Const(0);
  379. }
  380. public static Operand EmitAsrC(ArmEmitterContext context, Operand m, bool setCarry, Operand shift, Operand shiftIsZero)
  381. {
  382. Debug.Assert(m.Type == OperandType.I32 && shift.Type == OperandType.I32 && shiftIsZero.Type == OperandType.I32);
  383. Operand l32Result;
  384. Operand ge32Result;
  385. Operand less32 = context.ICompareLess(shift, Const(32));
  386. ge32Result = context.ShiftRightSI(m, Const(31));
  387. if (setCarry)
  388. {
  389. EmitIfHelper(context, context.BitwiseOr(less32, shiftIsZero), () =>
  390. {
  391. SetCarryMLsb(context, ge32Result);
  392. }, false);
  393. }
  394. l32Result = context.ShiftRightSI(m, shift);
  395. if (setCarry)
  396. {
  397. EmitIfHelper(context, context.BitwiseAnd(less32, context.BitwiseNot(shiftIsZero)), () =>
  398. {
  399. Operand cOut = context.ShiftRightUI(m, context.Subtract(shift, Const(1)));
  400. cOut = context.BitwiseAnd(cOut, Const(1));
  401. SetFlag(context, PState.CFlag, cOut);
  402. });
  403. }
  404. return context.ConditionalSelect(less32, l32Result, ge32Result);
  405. }
  406. public static Operand GetAsrC(ArmEmitterContext context, Operand m, bool setCarry, int shift)
  407. {
  408. Debug.Assert(m.Type == OperandType.I32);
  409. if ((uint)shift >= 32)
  410. {
  411. m = context.ShiftRightSI(m, Const(31));
  412. if (setCarry)
  413. {
  414. SetCarryMLsb(context, m);
  415. }
  416. return m;
  417. }
  418. else
  419. {
  420. if (setCarry)
  421. {
  422. SetCarryMShrOut(context, m, shift);
  423. }
  424. return context.ShiftRightSI(m, Const(shift));
  425. }
  426. }
  427. public static Operand EmitRorC(ArmEmitterContext context, Operand m, bool setCarry, Operand shift, Operand shiftIsZero)
  428. {
  429. Debug.Assert(m.Type == OperandType.I32 && shift.Type == OperandType.I32 && shiftIsZero.Type == OperandType.I32);
  430. shift = context.BitwiseAnd(shift, Const(0x1f));
  431. m = context.RotateRight(m, shift);
  432. if (setCarry)
  433. {
  434. EmitIfHelper(context, shiftIsZero, () =>
  435. {
  436. SetCarryMMsb(context, m);
  437. }, false);
  438. }
  439. return m;
  440. }
  441. public static Operand GetRorC(ArmEmitterContext context, Operand m, bool setCarry, int shift)
  442. {
  443. Debug.Assert(m.Type == OperandType.I32);
  444. shift &= 0x1f;
  445. m = context.RotateRight(m, Const(shift));
  446. if (setCarry)
  447. {
  448. SetCarryMMsb(context, m);
  449. }
  450. return m;
  451. }
  452. public static Operand GetRrxC(ArmEmitterContext context, Operand m, bool setCarry)
  453. {
  454. Debug.Assert(m.Type == OperandType.I32);
  455. // Rotate right by 1 with carry.
  456. Operand cIn = context.Copy(GetFlag(PState.CFlag));
  457. if (setCarry)
  458. {
  459. SetCarryMLsb(context, m);
  460. }
  461. m = context.ShiftRightUI(m, Const(1));
  462. m = context.BitwiseOr(m, context.ShiftLeft(cIn, Const(31)));
  463. return m;
  464. }
  465. private static void SetCarryMLsb(ArmEmitterContext context, Operand m)
  466. {
  467. Debug.Assert(m.Type == OperandType.I32);
  468. SetFlag(context, PState.CFlag, context.BitwiseAnd(m, Const(1)));
  469. }
  470. private static void SetCarryMMsb(ArmEmitterContext context, Operand m)
  471. {
  472. Debug.Assert(m.Type == OperandType.I32);
  473. SetFlag(context, PState.CFlag, context.ShiftRightUI(m, Const(31)));
  474. }
  475. private static void SetCarryMShrOut(ArmEmitterContext context, Operand m, int shift)
  476. {
  477. Debug.Assert(m.Type == OperandType.I32);
  478. Operand cOut = context.ShiftRightUI(m, Const(shift - 1));
  479. cOut = context.BitwiseAnd(cOut, Const(1));
  480. SetFlag(context, PState.CFlag, cOut);
  481. }
  482. }
  483. }