InstEmitAlu.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using static ARMeilleure.Instructions.InstEmitAluHelper;
  6. using static ARMeilleure.Instructions.InstEmitHelper;
  7. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  8. namespace ARMeilleure.Instructions
  9. {
  10. static partial class InstEmit
  11. {
  12. public static void Adc(ArmEmitterContext context) => EmitAdc(context, setFlags: false);
  13. public static void Adcs(ArmEmitterContext context) => EmitAdc(context, setFlags: true);
  14. private static void EmitAdc(ArmEmitterContext context, bool setFlags)
  15. {
  16. Operand n = GetAluN(context);
  17. Operand m = GetAluM(context);
  18. Operand d = context.Add(n, m);
  19. Operand carry = GetFlag(PState.CFlag);
  20. if (context.CurrOp.RegisterSize == RegisterSize.Int64)
  21. {
  22. carry = context.ZeroExtend32(OperandType.I64, carry);
  23. }
  24. d = context.Add(d, carry);
  25. if (setFlags)
  26. {
  27. EmitNZFlagsCheck(context, d);
  28. EmitAdcsCCheck(context, n, d);
  29. EmitAddsVCheck(context, n, m, d);
  30. }
  31. SetAluDOrZR(context, d);
  32. }
  33. public static void Add(ArmEmitterContext context)
  34. {
  35. SetAluD(context, context.Add(GetAluN(context), GetAluM(context)));
  36. }
  37. public static void Adds(ArmEmitterContext context)
  38. {
  39. Operand n = GetAluN(context);
  40. Operand m = GetAluM(context);
  41. context.MarkComparison(n, m);
  42. Operand d = context.Add(n, m);
  43. EmitNZFlagsCheck(context, d);
  44. EmitAddsCCheck(context, n, d);
  45. EmitAddsVCheck(context, n, m, d);
  46. SetAluDOrZR(context, d);
  47. }
  48. public static void And(ArmEmitterContext context)
  49. {
  50. SetAluD(context, context.BitwiseAnd(GetAluN(context), GetAluM(context)));
  51. }
  52. public static void Ands(ArmEmitterContext context)
  53. {
  54. Operand n = GetAluN(context);
  55. Operand m = GetAluM(context);
  56. Operand d = context.BitwiseAnd(n, m);
  57. EmitNZFlagsCheck(context, d);
  58. EmitCVFlagsClear(context);
  59. SetAluDOrZR(context, d);
  60. }
  61. public static void Asrv(ArmEmitterContext context)
  62. {
  63. SetAluDOrZR(context, context.ShiftRightSI(GetAluN(context), GetAluMShift(context)));
  64. }
  65. public static void Bic(ArmEmitterContext context) => EmitBic(context, setFlags: false);
  66. public static void Bics(ArmEmitterContext context) => EmitBic(context, setFlags: true);
  67. private static void EmitBic(ArmEmitterContext context, bool setFlags)
  68. {
  69. Operand n = GetAluN(context);
  70. Operand m = GetAluM(context);
  71. Operand d = context.BitwiseAnd(n, context.BitwiseNot(m));
  72. if (setFlags)
  73. {
  74. EmitNZFlagsCheck(context, d);
  75. EmitCVFlagsClear(context);
  76. }
  77. SetAluD(context, d, setFlags);
  78. }
  79. public static void Cls(ArmEmitterContext context)
  80. {
  81. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  82. Operand n = GetIntOrZR(context, op.Rn);
  83. Operand nHigh = context.ShiftRightUI(n, Const(1));
  84. bool is32Bits = op.RegisterSize == RegisterSize.Int32;
  85. Operand mask = is32Bits ? Const(int.MaxValue) : Const(long.MaxValue);
  86. Operand nLow = context.BitwiseAnd(n, mask);
  87. Operand res = context.CountLeadingZeros(context.BitwiseExclusiveOr(nHigh, nLow));
  88. res = context.Subtract(res, Const(res.Type, 1));
  89. SetAluDOrZR(context, res);
  90. }
  91. public static void Clz(ArmEmitterContext context)
  92. {
  93. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  94. Operand n = GetIntOrZR(context, op.Rn);
  95. Operand d = context.CountLeadingZeros(n);
  96. SetAluDOrZR(context, d);
  97. }
  98. public static void Eon(ArmEmitterContext context)
  99. {
  100. Operand n = GetAluN(context);
  101. Operand m = GetAluM(context);
  102. Operand d = context.BitwiseExclusiveOr(n, context.BitwiseNot(m));
  103. SetAluD(context, d);
  104. }
  105. public static void Eor(ArmEmitterContext context)
  106. {
  107. SetAluD(context, context.BitwiseExclusiveOr(GetAluN(context), GetAluM(context)));
  108. }
  109. public static void Extr(ArmEmitterContext context)
  110. {
  111. OpCodeAluRs op = (OpCodeAluRs)context.CurrOp;
  112. Operand res = GetIntOrZR(context, op.Rm);
  113. if (op.Shift != 0)
  114. {
  115. if (op.Rn == op.Rm)
  116. {
  117. res = context.RotateRight(res, Const(op.Shift));
  118. }
  119. else
  120. {
  121. res = context.ShiftRightUI(res, Const(op.Shift));
  122. Operand n = GetIntOrZR(context, op.Rn);
  123. int invShift = op.GetBitsCount() - op.Shift;
  124. res = context.BitwiseOr(res, context.ShiftLeft(n, Const(invShift)));
  125. }
  126. }
  127. SetAluDOrZR(context, res);
  128. }
  129. public static void Lslv(ArmEmitterContext context)
  130. {
  131. SetAluDOrZR(context, context.ShiftLeft(GetAluN(context), GetAluMShift(context)));
  132. }
  133. public static void Lsrv(ArmEmitterContext context)
  134. {
  135. SetAluDOrZR(context, context.ShiftRightUI(GetAluN(context), GetAluMShift(context)));
  136. }
  137. public static void Sbc(ArmEmitterContext context) => EmitSbc(context, setFlags: false);
  138. public static void Sbcs(ArmEmitterContext context) => EmitSbc(context, setFlags: true);
  139. private static void EmitSbc(ArmEmitterContext context, bool setFlags)
  140. {
  141. Operand n = GetAluN(context);
  142. Operand m = GetAluM(context);
  143. Operand d = context.Subtract(n, m);
  144. Operand borrow = context.BitwiseExclusiveOr(GetFlag(PState.CFlag), Const(1));
  145. if (context.CurrOp.RegisterSize == RegisterSize.Int64)
  146. {
  147. borrow = context.ZeroExtend32(OperandType.I64, borrow);
  148. }
  149. d = context.Subtract(d, borrow);
  150. if (setFlags)
  151. {
  152. EmitNZFlagsCheck(context, d);
  153. EmitSbcsCCheck(context, n, m);
  154. EmitSubsVCheck(context, n, m, d);
  155. }
  156. SetAluDOrZR(context, d);
  157. }
  158. public static void Sub(ArmEmitterContext context)
  159. {
  160. SetAluD(context, context.Subtract(GetAluN(context), GetAluM(context)));
  161. }
  162. public static void Subs(ArmEmitterContext context)
  163. {
  164. Operand n = GetAluN(context);
  165. Operand m = GetAluM(context);
  166. context.MarkComparison(n, m);
  167. Operand d = context.Subtract(n, m);
  168. EmitNZFlagsCheck(context, d);
  169. EmitSubsCCheck(context, n, m);
  170. EmitSubsVCheck(context, n, m, d);
  171. SetAluDOrZR(context, d);
  172. }
  173. public static void Orn(ArmEmitterContext context)
  174. {
  175. Operand n = GetAluN(context);
  176. Operand m = GetAluM(context);
  177. Operand d = context.BitwiseOr(n, context.BitwiseNot(m));
  178. SetAluD(context, d);
  179. }
  180. public static void Orr(ArmEmitterContext context)
  181. {
  182. SetAluD(context, context.BitwiseOr(GetAluN(context), GetAluM(context)));
  183. }
  184. public static void Rbit(ArmEmitterContext context)
  185. {
  186. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  187. Operand n = GetIntOrZR(context, op.Rn);
  188. Operand d;
  189. if (op.RegisterSize == RegisterSize.Int32)
  190. {
  191. d = context.Call(new _U32_U32(SoftFallback.ReverseBits32), n);
  192. }
  193. else
  194. {
  195. d = context.Call(new _U64_U64(SoftFallback.ReverseBits64), n);
  196. }
  197. SetAluDOrZR(context, d);
  198. }
  199. public static void Rev16(ArmEmitterContext context)
  200. {
  201. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  202. Operand n = GetIntOrZR(context, op.Rn);
  203. Operand d;
  204. if (op.RegisterSize == RegisterSize.Int32)
  205. {
  206. d = context.Call(new _U32_U32(SoftFallback.ReverseBytes16_32), n);
  207. }
  208. else
  209. {
  210. d = context.Call(new _U64_U64(SoftFallback.ReverseBytes16_64), n);
  211. }
  212. SetAluDOrZR(context, d);
  213. }
  214. public static void Rev32(ArmEmitterContext context)
  215. {
  216. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  217. Operand n = GetIntOrZR(context, op.Rn);
  218. if (op.RegisterSize == RegisterSize.Int32)
  219. {
  220. SetAluDOrZR(context, context.ByteSwap(n));
  221. }
  222. else
  223. {
  224. Operand d = context.Call(new _U64_U64(SoftFallback.ReverseBytes32_64), n);
  225. SetAluDOrZR(context, d);
  226. }
  227. }
  228. public static void Rev64(ArmEmitterContext context)
  229. {
  230. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  231. SetAluDOrZR(context, context.ByteSwap(GetIntOrZR(context, op.Rn)));
  232. }
  233. public static void Rorv(ArmEmitterContext context)
  234. {
  235. SetAluDOrZR(context, context.RotateRight(GetAluN(context), GetAluMShift(context)));
  236. }
  237. private static Operand GetAluMShift(ArmEmitterContext context)
  238. {
  239. IOpCodeAluRs op = (IOpCodeAluRs)context.CurrOp;
  240. Operand m = GetIntOrZR(context, op.Rm);
  241. if (op.RegisterSize == RegisterSize.Int64)
  242. {
  243. m = context.ConvertI64ToI32(m);
  244. }
  245. return context.BitwiseAnd(m, Const(context.CurrOp.GetBitsCount() - 1));
  246. }
  247. private static void EmitCVFlagsClear(ArmEmitterContext context)
  248. {
  249. SetFlag(context, PState.CFlag, Const(0));
  250. SetFlag(context, PState.VFlag, Const(0));
  251. }
  252. public static void SetAluD(ArmEmitterContext context, Operand d)
  253. {
  254. SetAluD(context, d, x31IsZR: false);
  255. }
  256. public static void SetAluDOrZR(ArmEmitterContext context, Operand d)
  257. {
  258. SetAluD(context, d, x31IsZR: true);
  259. }
  260. public static void SetAluD(ArmEmitterContext context, Operand d, bool x31IsZR)
  261. {
  262. IOpCodeAlu op = (IOpCodeAlu)context.CurrOp;
  263. if ((x31IsZR || op is IOpCodeAluRs) && op.Rd == RegisterConsts.ZeroIndex)
  264. {
  265. return;
  266. }
  267. SetIntOrSP(context, op.Rd, d);
  268. }
  269. }
  270. }