InstEmitAluHelper.cs 20 KB

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