InstEmitSystem.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 InstEmit
  12. {
  13. private const int DczSizeLog2 = 4; // Log2 size in words
  14. public const int DczSizeInBytes = 4 << DczSizeLog2;
  15. public static void Isb(ArmEmitterContext context)
  16. {
  17. // Execute as no-op.
  18. }
  19. public static void Mrs(ArmEmitterContext context)
  20. {
  21. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  22. MethodInfo info;
  23. switch (GetPackedId(op))
  24. {
  25. case 0b11_011_0000_0000_001: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetCtrEl0)); break;
  26. case 0b11_011_0000_0000_111: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetDczidEl0)); break;
  27. case 0b11_011_0100_0010_000: EmitGetNzcv(context); return;
  28. case 0b11_011_0100_0100_000: EmitGetFpcr(context); return;
  29. case 0b11_011_0100_0100_001: EmitGetFpsr(context); return;
  30. case 0b11_011_1101_0000_010: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetTpidrEl0)); break;
  31. case 0b11_011_1101_0000_011: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetTpidrroEl0)); break;
  32. case 0b11_011_1110_0000_000: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetCntfrqEl0)); break;
  33. case 0b11_011_1110_0000_001: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetCntpctEl0)); break;
  34. case 0b11_011_1110_0000_010: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetCntvctEl0)); break;
  35. default: throw new NotImplementedException($"Unknown MRS 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  36. }
  37. SetIntOrZR(context, op.Rt, context.Call(info));
  38. }
  39. public static void Msr(ArmEmitterContext context)
  40. {
  41. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  42. MethodInfo info;
  43. switch (GetPackedId(op))
  44. {
  45. case 0b11_011_0100_0010_000: EmitSetNzcv(context); return;
  46. case 0b11_011_0100_0100_000: EmitSetFpcr(context); return;
  47. case 0b11_011_0100_0100_001: EmitSetFpsr(context); return;
  48. case 0b11_011_1101_0000_010: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetTpidrEl0)); break;
  49. default: throw new NotImplementedException($"Unknown MSR 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  50. }
  51. context.Call(info, GetIntOrZR(context, op.Rt));
  52. }
  53. public static void Nop(ArmEmitterContext context)
  54. {
  55. // Do nothing.
  56. }
  57. public static void Sys(ArmEmitterContext context)
  58. {
  59. // This instruction is used to do some operations on the CPU like cache invalidation,
  60. // address translation and the like.
  61. // We treat it as no-op here since we don't have any cache being emulated anyway.
  62. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  63. switch (GetPackedId(op))
  64. {
  65. case 0b11_011_0111_0100_001:
  66. {
  67. // DC ZVA
  68. Operand t = GetIntOrZR(context, op.Rt);
  69. for (long offset = 0; offset < DczSizeInBytes; offset += 8)
  70. {
  71. Operand address = context.Add(t, Const(offset));
  72. InstEmitMemoryHelper.EmitStore(context, address, RegisterConsts.ZeroIndex, 3);
  73. }
  74. break;
  75. }
  76. // No-op
  77. case 0b11_011_0111_1110_001: // DC CIVAC
  78. break;
  79. case 0b11_011_0111_0101_001: // IC IVAU
  80. Operand target = Register(op.Rt, RegisterType.Integer, OperandType.I64);
  81. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.InvalidateCacheLine)), target);
  82. break;
  83. }
  84. }
  85. private static int GetPackedId(OpCodeSystem op)
  86. {
  87. int id;
  88. id = op.Op2 << 0;
  89. id |= op.CRm << 3;
  90. id |= op.CRn << 7;
  91. id |= op.Op1 << 11;
  92. id |= op.Op0 << 14;
  93. return id;
  94. }
  95. private static void EmitGetNzcv(ArmEmitterContext context)
  96. {
  97. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  98. Operand nzcv = context.ShiftLeft(GetFlag(PState.VFlag), Const((int)PState.VFlag));
  99. nzcv = context.BitwiseOr(nzcv, context.ShiftLeft(GetFlag(PState.CFlag), Const((int)PState.CFlag)));
  100. nzcv = context.BitwiseOr(nzcv, context.ShiftLeft(GetFlag(PState.ZFlag), Const((int)PState.ZFlag)));
  101. nzcv = context.BitwiseOr(nzcv, context.ShiftLeft(GetFlag(PState.NFlag), Const((int)PState.NFlag)));
  102. SetIntOrZR(context, op.Rt, nzcv);
  103. }
  104. private static void EmitGetFpcr(ArmEmitterContext context)
  105. {
  106. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  107. Operand fpcr = Const(0);
  108. for (int flag = 0; flag < RegisterConsts.FpFlagsCount; flag++)
  109. {
  110. if (FPCR.Mask.HasFlag((FPCR)(1u << flag)))
  111. {
  112. fpcr = context.BitwiseOr(fpcr, context.ShiftLeft(GetFpFlag((FPState)flag), Const(flag)));
  113. }
  114. }
  115. SetIntOrZR(context, op.Rt, fpcr);
  116. }
  117. private static void EmitGetFpsr(ArmEmitterContext context)
  118. {
  119. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  120. Operand fpsr = Const(0);
  121. for (int flag = 0; flag < RegisterConsts.FpFlagsCount; flag++)
  122. {
  123. if (FPSR.Mask.HasFlag((FPSR)(1u << flag)))
  124. {
  125. fpsr = context.BitwiseOr(fpsr, context.ShiftLeft(GetFpFlag((FPState)flag), Const(flag)));
  126. }
  127. }
  128. SetIntOrZR(context, op.Rt, fpsr);
  129. }
  130. private static void EmitSetNzcv(ArmEmitterContext context)
  131. {
  132. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  133. Operand nzcv = GetIntOrZR(context, op.Rt);
  134. nzcv = context.ConvertI64ToI32(nzcv);
  135. SetFlag(context, PState.VFlag, context.BitwiseAnd(context.ShiftRightUI(nzcv, Const((int)PState.VFlag)), Const(1)));
  136. SetFlag(context, PState.CFlag, context.BitwiseAnd(context.ShiftRightUI(nzcv, Const((int)PState.CFlag)), Const(1)));
  137. SetFlag(context, PState.ZFlag, context.BitwiseAnd(context.ShiftRightUI(nzcv, Const((int)PState.ZFlag)), Const(1)));
  138. SetFlag(context, PState.NFlag, context.BitwiseAnd(context.ShiftRightUI(nzcv, Const((int)PState.NFlag)), Const(1)));
  139. }
  140. private static void EmitSetFpcr(ArmEmitterContext context)
  141. {
  142. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  143. Operand fpcr = GetIntOrZR(context, op.Rt);
  144. fpcr = context.ConvertI64ToI32(fpcr);
  145. for (int flag = 0; flag < RegisterConsts.FpFlagsCount; flag++)
  146. {
  147. if (FPCR.Mask.HasFlag((FPCR)(1u << flag)))
  148. {
  149. SetFpFlag(context, (FPState)flag, context.BitwiseAnd(context.ShiftRightUI(fpcr, Const(flag)), Const(1)));
  150. }
  151. }
  152. }
  153. private static void EmitSetFpsr(ArmEmitterContext context)
  154. {
  155. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  156. Operand fpsr = GetIntOrZR(context, op.Rt);
  157. fpsr = context.ConvertI64ToI32(fpsr);
  158. for (int flag = 0; flag < RegisterConsts.FpFlagsCount; flag++)
  159. {
  160. if (FPSR.Mask.HasFlag((FPSR)(1u << flag)))
  161. {
  162. SetFpFlag(context, (FPState)flag, context.BitwiseAnd(context.ShiftRightUI(fpsr, Const(flag)), Const(1)));
  163. }
  164. }
  165. }
  166. }
  167. }