InstEmitAluHelper.cs 20 KB

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