InstEmitHelper.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 bool IsA32Return(ArmEmitterContext context)
  123. {
  124. switch (context.CurrOp)
  125. {
  126. case IOpCode32MemMult op:
  127. return true; // Setting PC using LDM is nearly always a return.
  128. case OpCode32AluRsImm op:
  129. return op.Rm == RegisterAlias.Aarch32Lr;
  130. case OpCode32AluRsReg op:
  131. return op.Rm == RegisterAlias.Aarch32Lr;
  132. case OpCode32AluReg op:
  133. return op.Rm == RegisterAlias.Aarch32Lr;
  134. case OpCode32Mem op:
  135. return op.Rn == RegisterAlias.Aarch32Sp && op.WBack && !op.Index; // Setting PC to an address stored on the stack is nearly always a return.
  136. }
  137. return false;
  138. }
  139. public static void EmitBxWritePc(ArmEmitterContext context, Operand pc, int sourceRegister = 0)
  140. {
  141. bool isReturn = sourceRegister == RegisterAlias.Aarch32Lr || IsA32Return(context);
  142. Operand mode = context.BitwiseAnd(pc, Const(1));
  143. SetFlag(context, PState.TFlag, mode);
  144. Operand addr = context.ConditionalSelect(mode, context.BitwiseOr(pc, Const((int)InstEmitFlowHelper.CallFlag)), context.BitwiseAnd(pc, Const(~3)));
  145. InstEmitFlowHelper.EmitVirtualJump(context, addr, isReturn);
  146. }
  147. public static Operand GetIntOrZR(ArmEmitterContext context, int regIndex)
  148. {
  149. if (regIndex == RegisterConsts.ZeroIndex)
  150. {
  151. OperandType type = context.CurrOp.GetOperandType();
  152. return type == OperandType.I32 ? Const(0) : Const(0L);
  153. }
  154. else
  155. {
  156. return GetIntOrSP(context, regIndex);
  157. }
  158. }
  159. public static void SetIntOrZR(ArmEmitterContext context, int regIndex, Operand value)
  160. {
  161. if (regIndex == RegisterConsts.ZeroIndex)
  162. {
  163. return;
  164. }
  165. SetIntOrSP(context, regIndex, value);
  166. }
  167. public static Operand GetIntOrSP(ArmEmitterContext context, int regIndex)
  168. {
  169. Operand value = Register(regIndex, RegisterType.Integer, OperandType.I64);
  170. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  171. {
  172. value = context.ConvertI64ToI32(value);
  173. }
  174. return value;
  175. }
  176. public static void SetIntOrSP(ArmEmitterContext context, int regIndex, Operand value)
  177. {
  178. Operand reg = Register(regIndex, RegisterType.Integer, OperandType.I64);
  179. if (value.Type == OperandType.I32)
  180. {
  181. value = context.ZeroExtend32(OperandType.I64, value);
  182. }
  183. context.Copy(reg, value);
  184. }
  185. public static Operand GetVec(int regIndex)
  186. {
  187. return Register(regIndex, RegisterType.Vector, OperandType.V128);
  188. }
  189. public static Operand GetFlag(PState stateFlag)
  190. {
  191. return Register((int)stateFlag, RegisterType.Flag, OperandType.I32);
  192. }
  193. public static Operand GetFpFlag(FPState stateFlag)
  194. {
  195. return Register((int)stateFlag, RegisterType.FpFlag, OperandType.I32);
  196. }
  197. public static void SetFlag(ArmEmitterContext context, PState stateFlag, Operand value)
  198. {
  199. context.Copy(GetFlag(stateFlag), value);
  200. context.MarkFlagSet(stateFlag);
  201. }
  202. public static void SetFpFlag(ArmEmitterContext context, FPState stateFlag, Operand value)
  203. {
  204. context.Copy(GetFpFlag(stateFlag), value);
  205. }
  206. }
  207. }