InstEmitSystem32.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using System.Reflection;
  7. using static ARMeilleure.Instructions.InstEmitHelper;
  8. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  9. namespace ARMeilleure.Instructions
  10. {
  11. static partial class InstEmit32
  12. {
  13. public static void Mcr(ArmEmitterContext context)
  14. {
  15. OpCode32System op = (OpCode32System)context.CurrOp;
  16. if (op.Coproc != 15)
  17. {
  18. InstEmit.Und(context);
  19. return;
  20. }
  21. if (op.Opc1 != 0)
  22. {
  23. throw new NotImplementedException($"Unknown MRC Opc1 0x{op.Opc1:X16} at 0x{op.Address:X16}.");
  24. }
  25. MethodInfo info;
  26. switch (op.CRn)
  27. {
  28. case 13: // Process and Thread Info.
  29. if (op.CRm != 0)
  30. {
  31. throw new NotImplementedException($"Unknown MRC CRm 0x{op.CRm:X16} at 0x{op.Address:X16}.");
  32. }
  33. switch (op.Opc2)
  34. {
  35. case 2:
  36. info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetTpidrEl032)); break;
  37. default:
  38. throw new NotImplementedException($"Unknown MRC Opc2 0x{op.Opc2:X16} at 0x{op.Address:X16}.");
  39. }
  40. break;
  41. case 7:
  42. switch (op.CRm) // Cache and Memory barrier.
  43. {
  44. case 10:
  45. switch (op.Opc2)
  46. {
  47. case 5: // Data Memory Barrier Register.
  48. return; // No-op.
  49. default:
  50. throw new NotImplementedException($"Unknown MRC Opc2 0x{op.Opc2:X16} at 0x{op.Address:X16}.");
  51. }
  52. default:
  53. throw new NotImplementedException($"Unknown MRC CRm 0x{op.CRm:X16} at 0x{op.Address:X16}.");
  54. }
  55. default:
  56. throw new NotImplementedException($"Unknown MRC 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  57. }
  58. context.Call(info, GetIntA32(context, op.Rt));
  59. }
  60. public static void Mrc(ArmEmitterContext context)
  61. {
  62. OpCode32System op = (OpCode32System)context.CurrOp;
  63. if (op.Coproc != 15)
  64. {
  65. InstEmit.Und(context);
  66. return;
  67. }
  68. if (op.Opc1 != 0)
  69. {
  70. throw new NotImplementedException($"Unknown MRC Opc1 0x{op.Opc1:X16} at 0x{op.Address:X16}.");
  71. }
  72. MethodInfo info;
  73. switch (op.CRn)
  74. {
  75. case 13: // Process and Thread Info.
  76. if (op.CRm != 0)
  77. {
  78. throw new NotImplementedException($"Unknown MRC CRm 0x{op.CRm:X16} at 0x{op.Address:X16}.");
  79. }
  80. switch (op.Opc2)
  81. {
  82. case 2:
  83. info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetTpidrEl032)); break;
  84. case 3:
  85. info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetTpidr32)); break;
  86. default:
  87. throw new NotImplementedException($"Unknown MRC Opc2 0x{op.Opc2:X16} at 0x{op.Address:X16}.");
  88. }
  89. break;
  90. default:
  91. throw new NotImplementedException($"Unknown MRC 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  92. }
  93. if (op.Rt == RegisterAlias.Aarch32Pc)
  94. {
  95. // Special behavior: copy NZCV flags into APSR.
  96. EmitSetNzcv(context, context.Call(info));
  97. return;
  98. }
  99. else
  100. {
  101. SetIntA32(context, op.Rt, context.Call(info));
  102. }
  103. }
  104. public static void Mrrc(ArmEmitterContext context)
  105. {
  106. OpCode32System op = (OpCode32System)context.CurrOp;
  107. if (op.Coproc != 15)
  108. {
  109. InstEmit.Und(context);
  110. return;
  111. }
  112. int opc = op.MrrcOp;
  113. MethodInfo info;
  114. switch (op.CRm)
  115. {
  116. case 14: // Timer.
  117. switch (opc)
  118. {
  119. case 0:
  120. info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetCntpctEl0)); break;
  121. default:
  122. throw new NotImplementedException($"Unknown MRRC Opc1 0x{opc:X16} at 0x{op.Address:X16}.");
  123. }
  124. break;
  125. default:
  126. throw new NotImplementedException($"Unknown MRRC 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  127. }
  128. Operand result = context.Call(info);
  129. SetIntA32(context, op.Rt, context.ConvertI64ToI32(result));
  130. SetIntA32(context, op.CRn, context.ConvertI64ToI32(context.ShiftRightUI(result, Const(32))));
  131. }
  132. public static void Msr(ArmEmitterContext context)
  133. {
  134. OpCode32MsrReg op = (OpCode32MsrReg)context.CurrOp;
  135. if (op.R)
  136. {
  137. throw new NotImplementedException("SPSR");
  138. }
  139. else
  140. {
  141. if ((op.Mask & 8) != 0)
  142. {
  143. Operand value = GetIntA32(context, op.Rn);
  144. EmitSetNzcv(context, value);
  145. Operand q = context.ShiftRightUI(value, Const((int)PState.QFlag));
  146. q = context.BitwiseAnd(q, Const(1));
  147. SetFlag(context, PState.QFlag, q);
  148. }
  149. if ((op.Mask & 4) != 0)
  150. {
  151. throw new NotImplementedException("APSR_g");
  152. }
  153. if ((op.Mask & 2) != 0)
  154. {
  155. throw new NotImplementedException("CPSR_x");
  156. }
  157. if ((op.Mask & 1) != 0)
  158. {
  159. throw new NotImplementedException("CPSR_c");
  160. }
  161. }
  162. }
  163. public static void Nop(ArmEmitterContext context) { }
  164. public static void Vmrs(ArmEmitterContext context)
  165. {
  166. OpCode32SimdSpecial op = (OpCode32SimdSpecial)context.CurrOp;
  167. if (op.Rt == RegisterAlias.Aarch32Pc && op.Sreg == 0b0001)
  168. {
  169. // Special behavior: copy NZCV flags into APSR.
  170. SetFlag(context, PState.VFlag, GetFpFlag(FPState.VFlag));
  171. SetFlag(context, PState.CFlag, GetFpFlag(FPState.CFlag));
  172. SetFlag(context, PState.ZFlag, GetFpFlag(FPState.ZFlag));
  173. SetFlag(context, PState.NFlag, GetFpFlag(FPState.NFlag));
  174. return;
  175. }
  176. switch (op.Sreg)
  177. {
  178. case 0b0000: // FPSID
  179. throw new NotImplementedException("Supervisor Only");
  180. case 0b0001: // FPSCR
  181. EmitGetFpscr(context); return;
  182. case 0b0101: // MVFR2
  183. throw new NotImplementedException("MVFR2");
  184. case 0b0110: // MVFR1
  185. throw new NotImplementedException("MVFR1");
  186. case 0b0111: // MVFR0
  187. throw new NotImplementedException("MVFR0");
  188. case 0b1000: // FPEXC
  189. throw new NotImplementedException("Supervisor Only");
  190. default:
  191. throw new NotImplementedException($"Unknown VMRS 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  192. }
  193. }
  194. public static void Vmsr(ArmEmitterContext context)
  195. {
  196. OpCode32SimdSpecial op = (OpCode32SimdSpecial)context.CurrOp;
  197. switch (op.Sreg)
  198. {
  199. case 0b0000: // FPSID
  200. throw new NotImplementedException("Supervisor Only");
  201. case 0b0001: // FPSCR
  202. EmitSetFpscr(context); return;
  203. case 0b0101: // MVFR2
  204. throw new NotImplementedException("MVFR2");
  205. case 0b0110: // MVFR1
  206. throw new NotImplementedException("MVFR1");
  207. case 0b0111: // MVFR0
  208. throw new NotImplementedException("MVFR0");
  209. case 0b1000: // FPEXC
  210. throw new NotImplementedException("Supervisor Only");
  211. default:
  212. throw new NotImplementedException($"Unknown VMSR 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  213. }
  214. }
  215. private static void EmitSetNzcv(ArmEmitterContext context, Operand t)
  216. {
  217. Operand v = context.ShiftRightUI(t, Const((int)PState.VFlag));
  218. v = context.BitwiseAnd(v, Const(1));
  219. Operand c = context.ShiftRightUI(t, Const((int)PState.CFlag));
  220. c = context.BitwiseAnd(c, Const(1));
  221. Operand z = context.ShiftRightUI(t, Const((int)PState.ZFlag));
  222. z = context.BitwiseAnd(z, Const(1));
  223. Operand n = context.ShiftRightUI(t, Const((int)PState.NFlag));
  224. n = context.BitwiseAnd(n, Const(1));
  225. SetFlag(context, PState.VFlag, v);
  226. SetFlag(context, PState.CFlag, c);
  227. SetFlag(context, PState.ZFlag, z);
  228. SetFlag(context, PState.NFlag, n);
  229. }
  230. private static void EmitGetFpscr(ArmEmitterContext context)
  231. {
  232. OpCode32SimdSpecial op = (OpCode32SimdSpecial)context.CurrOp;
  233. Operand vSh = context.ShiftLeft(GetFpFlag(FPState.VFlag), Const((int)FPState.VFlag));
  234. Operand cSh = context.ShiftLeft(GetFpFlag(FPState.CFlag), Const((int)FPState.CFlag));
  235. Operand zSh = context.ShiftLeft(GetFpFlag(FPState.ZFlag), Const((int)FPState.ZFlag));
  236. Operand nSh = context.ShiftLeft(GetFpFlag(FPState.NFlag), Const((int)FPState.NFlag));
  237. Operand nzcvSh = context.BitwiseOr(context.BitwiseOr(nSh, zSh), context.BitwiseOr(cSh, vSh));
  238. Operand fpscr = context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFpscr)));
  239. SetIntA32(context, op.Rt, context.BitwiseOr(nzcvSh, fpscr));
  240. }
  241. private static void EmitSetFpscr(ArmEmitterContext context)
  242. {
  243. OpCode32SimdSpecial op = (OpCode32SimdSpecial)context.CurrOp;
  244. Operand t = GetIntA32(context, op.Rt);
  245. Operand v = context.ShiftRightUI(t, Const((int)FPState.VFlag));
  246. v = context.BitwiseAnd(v, Const(1));
  247. Operand c = context.ShiftRightUI(t, Const((int)FPState.CFlag));
  248. c = context.BitwiseAnd(c, Const(1));
  249. Operand z = context.ShiftRightUI(t, Const((int)FPState.ZFlag));
  250. z = context.BitwiseAnd(z, Const(1));
  251. Operand n = context.ShiftRightUI(t, Const((int)FPState.NFlag));
  252. n = context.BitwiseAnd(n, Const(1));
  253. SetFpFlag(context, FPState.VFlag, v);
  254. SetFpFlag(context, FPState.CFlag, c);
  255. SetFpFlag(context, FPState.ZFlag, z);
  256. SetFpFlag(context, FPState.NFlag, n);
  257. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpscr)), t);
  258. }
  259. }
  260. }