InstEmitAluHelper.cs 21 KB

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