InstEmitSystem32.cs 12 KB

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