InstEmitSystem32.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 Mrs(ArmEmitterContext context)
  133. {
  134. OpCode32Mrs op = (OpCode32Mrs)context.CurrOp;
  135. if (op.R)
  136. {
  137. throw new NotImplementedException("SPSR");
  138. }
  139. else
  140. {
  141. Operand vSh = context.ShiftLeft(GetFlag(PState.VFlag), Const((int)PState.VFlag));
  142. Operand cSh = context.ShiftLeft(GetFlag(PState.CFlag), Const((int)PState.CFlag));
  143. Operand zSh = context.ShiftLeft(GetFlag(PState.ZFlag), Const((int)PState.ZFlag));
  144. Operand nSh = context.ShiftLeft(GetFlag(PState.NFlag), Const((int)PState.NFlag));
  145. Operand qSh = context.ShiftLeft(GetFlag(PState.QFlag), Const((int)PState.QFlag));
  146. Operand spsr = context.BitwiseOr(context.BitwiseOr(nSh, zSh), context.BitwiseOr(cSh, vSh));
  147. spsr = context.BitwiseOr(spsr, qSh);
  148. // TODO: Remaining flags.
  149. SetIntA32(context, op.Rd, spsr);
  150. }
  151. }
  152. public static void Msr(ArmEmitterContext context)
  153. {
  154. OpCode32MsrReg op = (OpCode32MsrReg)context.CurrOp;
  155. if (op.R)
  156. {
  157. throw new NotImplementedException("SPSR");
  158. }
  159. else
  160. {
  161. if ((op.Mask & 8) != 0)
  162. {
  163. Operand value = GetIntA32(context, op.Rn);
  164. EmitSetNzcv(context, value);
  165. Operand q = context.ShiftRightUI(value, Const((int)PState.QFlag));
  166. q = context.BitwiseAnd(q, Const(1));
  167. SetFlag(context, PState.QFlag, q);
  168. }
  169. if ((op.Mask & 4) != 0)
  170. {
  171. throw new NotImplementedException("APSR_g");
  172. }
  173. if ((op.Mask & 2) != 0)
  174. {
  175. throw new NotImplementedException("CPSR_x");
  176. }
  177. if ((op.Mask & 1) != 0)
  178. {
  179. throw new NotImplementedException("CPSR_c");
  180. }
  181. }
  182. }
  183. public static void Nop(ArmEmitterContext context) { }
  184. public static void Vmrs(ArmEmitterContext context)
  185. {
  186. OpCode32SimdSpecial op = (OpCode32SimdSpecial)context.CurrOp;
  187. if (op.Rt == RegisterAlias.Aarch32Pc && op.Sreg == 0b0001)
  188. {
  189. // Special behavior: copy NZCV flags into APSR.
  190. SetFlag(context, PState.VFlag, GetFpFlag(FPState.VFlag));
  191. SetFlag(context, PState.CFlag, GetFpFlag(FPState.CFlag));
  192. SetFlag(context, PState.ZFlag, GetFpFlag(FPState.ZFlag));
  193. SetFlag(context, PState.NFlag, GetFpFlag(FPState.NFlag));
  194. return;
  195. }
  196. switch (op.Sreg)
  197. {
  198. case 0b0000: // FPSID
  199. throw new NotImplementedException("Supervisor Only");
  200. case 0b0001: // FPSCR
  201. EmitGetFpscr(context); return;
  202. case 0b0101: // MVFR2
  203. throw new NotImplementedException("MVFR2");
  204. case 0b0110: // MVFR1
  205. throw new NotImplementedException("MVFR1");
  206. case 0b0111: // MVFR0
  207. throw new NotImplementedException("MVFR0");
  208. case 0b1000: // FPEXC
  209. throw new NotImplementedException("Supervisor Only");
  210. default:
  211. throw new NotImplementedException($"Unknown VMRS 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  212. }
  213. }
  214. public static void Vmsr(ArmEmitterContext context)
  215. {
  216. OpCode32SimdSpecial op = (OpCode32SimdSpecial)context.CurrOp;
  217. switch (op.Sreg)
  218. {
  219. case 0b0000: // FPSID
  220. throw new NotImplementedException("Supervisor Only");
  221. case 0b0001: // FPSCR
  222. EmitSetFpscr(context); return;
  223. case 0b0101: // MVFR2
  224. throw new NotImplementedException("MVFR2");
  225. case 0b0110: // MVFR1
  226. throw new NotImplementedException("MVFR1");
  227. case 0b0111: // MVFR0
  228. throw new NotImplementedException("MVFR0");
  229. case 0b1000: // FPEXC
  230. throw new NotImplementedException("Supervisor Only");
  231. default:
  232. throw new NotImplementedException($"Unknown VMSR 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  233. }
  234. }
  235. private static void EmitSetNzcv(ArmEmitterContext context, Operand t)
  236. {
  237. Operand v = context.ShiftRightUI(t, Const((int)PState.VFlag));
  238. v = context.BitwiseAnd(v, Const(1));
  239. Operand c = context.ShiftRightUI(t, Const((int)PState.CFlag));
  240. c = context.BitwiseAnd(c, Const(1));
  241. Operand z = context.ShiftRightUI(t, Const((int)PState.ZFlag));
  242. z = context.BitwiseAnd(z, Const(1));
  243. Operand n = context.ShiftRightUI(t, Const((int)PState.NFlag));
  244. n = context.BitwiseAnd(n, Const(1));
  245. SetFlag(context, PState.VFlag, v);
  246. SetFlag(context, PState.CFlag, c);
  247. SetFlag(context, PState.ZFlag, z);
  248. SetFlag(context, PState.NFlag, n);
  249. }
  250. private static void EmitGetFpscr(ArmEmitterContext context)
  251. {
  252. OpCode32SimdSpecial op = (OpCode32SimdSpecial)context.CurrOp;
  253. Operand vSh = context.ShiftLeft(GetFpFlag(FPState.VFlag), Const((int)FPState.VFlag));
  254. Operand cSh = context.ShiftLeft(GetFpFlag(FPState.CFlag), Const((int)FPState.CFlag));
  255. Operand zSh = context.ShiftLeft(GetFpFlag(FPState.ZFlag), Const((int)FPState.ZFlag));
  256. Operand nSh = context.ShiftLeft(GetFpFlag(FPState.NFlag), Const((int)FPState.NFlag));
  257. Operand nzcvSh = context.BitwiseOr(context.BitwiseOr(nSh, zSh), context.BitwiseOr(cSh, vSh));
  258. Operand fpscr = context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFpscr)));
  259. SetIntA32(context, op.Rt, context.BitwiseOr(nzcvSh, fpscr));
  260. }
  261. private static void EmitSetFpscr(ArmEmitterContext context)
  262. {
  263. OpCode32SimdSpecial op = (OpCode32SimdSpecial)context.CurrOp;
  264. Operand t = GetIntA32(context, op.Rt);
  265. Operand v = context.ShiftRightUI(t, Const((int)FPState.VFlag));
  266. v = context.BitwiseAnd(v, Const(1));
  267. Operand c = context.ShiftRightUI(t, Const((int)FPState.CFlag));
  268. c = context.BitwiseAnd(c, Const(1));
  269. Operand z = context.ShiftRightUI(t, Const((int)FPState.ZFlag));
  270. z = context.BitwiseAnd(z, Const(1));
  271. Operand n = context.ShiftRightUI(t, Const((int)FPState.NFlag));
  272. n = context.BitwiseAnd(n, Const(1));
  273. SetFpFlag(context, FPState.VFlag, v);
  274. SetFpFlag(context, FPState.CFlag, c);
  275. SetFpFlag(context, FPState.ZFlag, z);
  276. SetFpFlag(context, FPState.NFlag, n);
  277. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpscr)), t);
  278. }
  279. }
  280. }