InstEmitAluHelper.cs 20 KB

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