InstEmitHelper.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  7. namespace ARMeilleure.Instructions
  8. {
  9. static class InstEmitHelper
  10. {
  11. public static bool IsThumb(OpCode op)
  12. {
  13. return op is OpCodeT16;
  14. }
  15. public static Operand GetExtendedM(ArmEmitterContext context, int rm, IntType type)
  16. {
  17. Operand value = GetIntOrZR(context, rm);
  18. switch (type)
  19. {
  20. case IntType.UInt8: value = context.ZeroExtend8 (value.Type, value); break;
  21. case IntType.UInt16: value = context.ZeroExtend16(value.Type, value); break;
  22. case IntType.UInt32: value = context.ZeroExtend32(value.Type, value); break;
  23. case IntType.Int8: value = context.SignExtend8 (value.Type, value); break;
  24. case IntType.Int16: value = context.SignExtend16(value.Type, value); break;
  25. case IntType.Int32: value = context.SignExtend32(value.Type, value); break;
  26. }
  27. return value;
  28. }
  29. public static Operand GetIntA32(ArmEmitterContext context, int regIndex)
  30. {
  31. if (regIndex == RegisterAlias.Aarch32Pc)
  32. {
  33. OpCode32 op = (OpCode32)context.CurrOp;
  34. return Const((int)op.GetPc());
  35. }
  36. else
  37. {
  38. return Register(GetRegisterAlias(context.Mode, regIndex), RegisterType.Integer, OperandType.I32);
  39. }
  40. }
  41. public static Operand GetVecA32(int regIndex)
  42. {
  43. return Register(regIndex, RegisterType.Vector, OperandType.V128);
  44. }
  45. public static void SetIntA32(ArmEmitterContext context, int regIndex, Operand value)
  46. {
  47. if (regIndex == RegisterAlias.Aarch32Pc)
  48. {
  49. context.StoreToContext();
  50. EmitBxWritePc(context, value);
  51. }
  52. else
  53. {
  54. if (value.Type == OperandType.I64)
  55. {
  56. value = context.ConvertI64ToI32(value);
  57. }
  58. Operand reg = Register(GetRegisterAlias(context.Mode, regIndex), RegisterType.Integer, OperandType.I32);
  59. context.Copy(reg, value);
  60. }
  61. }
  62. public static int GetRegisterAlias(Aarch32Mode mode, int regIndex)
  63. {
  64. // Only registers >= 8 are banked,
  65. // with registers in the range [8, 12] being
  66. // banked for the FIQ mode, and registers
  67. // 13 and 14 being banked for all modes.
  68. if ((uint)regIndex < 8)
  69. {
  70. return regIndex;
  71. }
  72. return GetBankedRegisterAlias(mode, regIndex);
  73. }
  74. public static int GetBankedRegisterAlias(Aarch32Mode mode, int regIndex)
  75. {
  76. switch (regIndex)
  77. {
  78. case 8: return mode == Aarch32Mode.Fiq
  79. ? RegisterAlias.R8Fiq
  80. : RegisterAlias.R8Usr;
  81. case 9: return mode == Aarch32Mode.Fiq
  82. ? RegisterAlias.R9Fiq
  83. : RegisterAlias.R9Usr;
  84. case 10: return mode == Aarch32Mode.Fiq
  85. ? RegisterAlias.R10Fiq
  86. : RegisterAlias.R10Usr;
  87. case 11: return mode == Aarch32Mode.Fiq
  88. ? RegisterAlias.R11Fiq
  89. : RegisterAlias.R11Usr;
  90. case 12: return mode == Aarch32Mode.Fiq
  91. ? RegisterAlias.R12Fiq
  92. : RegisterAlias.R12Usr;
  93. case 13:
  94. switch (mode)
  95. {
  96. case Aarch32Mode.User:
  97. case Aarch32Mode.System: return RegisterAlias.SpUsr;
  98. case Aarch32Mode.Fiq: return RegisterAlias.SpFiq;
  99. case Aarch32Mode.Irq: return RegisterAlias.SpIrq;
  100. case Aarch32Mode.Supervisor: return RegisterAlias.SpSvc;
  101. case Aarch32Mode.Abort: return RegisterAlias.SpAbt;
  102. case Aarch32Mode.Hypervisor: return RegisterAlias.SpHyp;
  103. case Aarch32Mode.Undefined: return RegisterAlias.SpUnd;
  104. default: throw new ArgumentException(nameof(mode));
  105. }
  106. case 14:
  107. switch (mode)
  108. {
  109. case Aarch32Mode.User:
  110. case Aarch32Mode.Hypervisor:
  111. case Aarch32Mode.System: return RegisterAlias.LrUsr;
  112. case Aarch32Mode.Fiq: return RegisterAlias.LrFiq;
  113. case Aarch32Mode.Irq: return RegisterAlias.LrIrq;
  114. case Aarch32Mode.Supervisor: return RegisterAlias.LrSvc;
  115. case Aarch32Mode.Abort: return RegisterAlias.LrAbt;
  116. case Aarch32Mode.Undefined: return RegisterAlias.LrUnd;
  117. default: throw new ArgumentException(nameof(mode));
  118. }
  119. default: throw new ArgumentOutOfRangeException(nameof(regIndex));
  120. }
  121. }
  122. public static void EmitBxWritePc(ArmEmitterContext context, Operand pc)
  123. {
  124. Operand mode = context.BitwiseAnd(pc, Const(1));
  125. SetFlag(context, PState.TFlag, mode);
  126. Operand lblArmMode = Label();
  127. context.BranchIfTrue(lblArmMode, mode);
  128. // Make this count as a call, the translator will ignore the low bit for the address.
  129. context.Return(context.ZeroExtend32(OperandType.I64, context.BitwiseOr(pc, Const((int)InstEmitFlowHelper.CallFlag))));
  130. context.MarkLabel(lblArmMode);
  131. context.Return(context.ZeroExtend32(OperandType.I64, context.BitwiseOr(context.BitwiseAnd(pc, Const(~3)), Const((int)InstEmitFlowHelper.CallFlag))));
  132. }
  133. public static Operand GetIntOrZR(ArmEmitterContext context, int regIndex)
  134. {
  135. if (regIndex == RegisterConsts.ZeroIndex)
  136. {
  137. OperandType type = context.CurrOp.GetOperandType();
  138. return type == OperandType.I32 ? Const(0) : Const(0L);
  139. }
  140. else
  141. {
  142. return GetIntOrSP(context, regIndex);
  143. }
  144. }
  145. public static void SetIntOrZR(ArmEmitterContext context, int regIndex, Operand value)
  146. {
  147. if (regIndex == RegisterConsts.ZeroIndex)
  148. {
  149. return;
  150. }
  151. SetIntOrSP(context, regIndex, value);
  152. }
  153. public static Operand GetIntOrSP(ArmEmitterContext context, int regIndex)
  154. {
  155. Operand value = Register(regIndex, RegisterType.Integer, OperandType.I64);
  156. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  157. {
  158. value = context.ConvertI64ToI32(value);
  159. }
  160. return value;
  161. }
  162. public static void SetIntOrSP(ArmEmitterContext context, int regIndex, Operand value)
  163. {
  164. Operand reg = Register(regIndex, RegisterType.Integer, OperandType.I64);
  165. if (value.Type == OperandType.I32)
  166. {
  167. value = context.ZeroExtend32(OperandType.I64, value);
  168. }
  169. context.Copy(reg, value);
  170. }
  171. public static Operand GetVec(int regIndex)
  172. {
  173. return Register(regIndex, RegisterType.Vector, OperandType.V128);
  174. }
  175. public static Operand GetFlag(PState stateFlag)
  176. {
  177. return Register((int)stateFlag, RegisterType.Flag, OperandType.I32);
  178. }
  179. public static Operand GetFpFlag(FPState stateFlag)
  180. {
  181. return Register((int)stateFlag, RegisterType.FpFlag, OperandType.I32);
  182. }
  183. public static void SetFlag(ArmEmitterContext context, PState stateFlag, Operand value)
  184. {
  185. context.Copy(GetFlag(stateFlag), value);
  186. context.MarkFlagSet(stateFlag);
  187. }
  188. public static void SetFpFlag(ArmEmitterContext context, FPState stateFlag, Operand value)
  189. {
  190. context.Copy(GetFpFlag(stateFlag), value);
  191. }
  192. }
  193. }