InstEmitHelper.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  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 GetIntA32AlignedPC(ArmEmitterContext context, int regIndex)
  42. {
  43. if (regIndex == RegisterAlias.Aarch32Pc)
  44. {
  45. OpCode32 op = (OpCode32)context.CurrOp;
  46. return Const((int)(op.GetPc() & 0xfffffffc));
  47. }
  48. else
  49. {
  50. return Register(GetRegisterAlias(context.Mode, regIndex), RegisterType.Integer, OperandType.I32);
  51. }
  52. }
  53. public static Operand GetVecA32(int regIndex)
  54. {
  55. return Register(regIndex, RegisterType.Vector, OperandType.V128);
  56. }
  57. public static void SetIntA32(ArmEmitterContext context, int regIndex, Operand value)
  58. {
  59. if (regIndex == RegisterAlias.Aarch32Pc)
  60. {
  61. if (!IsA32Return(context))
  62. {
  63. context.StoreToContext();
  64. }
  65. EmitBxWritePc(context, value);
  66. }
  67. else
  68. {
  69. if (value.Type == OperandType.I64)
  70. {
  71. value = context.ConvertI64ToI32(value);
  72. }
  73. Operand reg = Register(GetRegisterAlias(context.Mode, regIndex), RegisterType.Integer, OperandType.I32);
  74. context.Copy(reg, value);
  75. }
  76. }
  77. public static int GetRegisterAlias(Aarch32Mode mode, int regIndex)
  78. {
  79. // Only registers >= 8 are banked,
  80. // with registers in the range [8, 12] being
  81. // banked for the FIQ mode, and registers
  82. // 13 and 14 being banked for all modes.
  83. if ((uint)regIndex < 8)
  84. {
  85. return regIndex;
  86. }
  87. return GetBankedRegisterAlias(mode, regIndex);
  88. }
  89. public static int GetBankedRegisterAlias(Aarch32Mode mode, int regIndex)
  90. {
  91. switch (regIndex)
  92. {
  93. case 8: return mode == Aarch32Mode.Fiq
  94. ? RegisterAlias.R8Fiq
  95. : RegisterAlias.R8Usr;
  96. case 9: return mode == Aarch32Mode.Fiq
  97. ? RegisterAlias.R9Fiq
  98. : RegisterAlias.R9Usr;
  99. case 10: return mode == Aarch32Mode.Fiq
  100. ? RegisterAlias.R10Fiq
  101. : RegisterAlias.R10Usr;
  102. case 11: return mode == Aarch32Mode.Fiq
  103. ? RegisterAlias.R11Fiq
  104. : RegisterAlias.R11Usr;
  105. case 12: return mode == Aarch32Mode.Fiq
  106. ? RegisterAlias.R12Fiq
  107. : RegisterAlias.R12Usr;
  108. case 13:
  109. switch (mode)
  110. {
  111. case Aarch32Mode.User:
  112. case Aarch32Mode.System: return RegisterAlias.SpUsr;
  113. case Aarch32Mode.Fiq: return RegisterAlias.SpFiq;
  114. case Aarch32Mode.Irq: return RegisterAlias.SpIrq;
  115. case Aarch32Mode.Supervisor: return RegisterAlias.SpSvc;
  116. case Aarch32Mode.Abort: return RegisterAlias.SpAbt;
  117. case Aarch32Mode.Hypervisor: return RegisterAlias.SpHyp;
  118. case Aarch32Mode.Undefined: return RegisterAlias.SpUnd;
  119. default: throw new ArgumentException(nameof(mode));
  120. }
  121. case 14:
  122. switch (mode)
  123. {
  124. case Aarch32Mode.User:
  125. case Aarch32Mode.Hypervisor:
  126. case Aarch32Mode.System: return RegisterAlias.LrUsr;
  127. case Aarch32Mode.Fiq: return RegisterAlias.LrFiq;
  128. case Aarch32Mode.Irq: return RegisterAlias.LrIrq;
  129. case Aarch32Mode.Supervisor: return RegisterAlias.LrSvc;
  130. case Aarch32Mode.Abort: return RegisterAlias.LrAbt;
  131. case Aarch32Mode.Undefined: return RegisterAlias.LrUnd;
  132. default: throw new ArgumentException(nameof(mode));
  133. }
  134. default: throw new ArgumentOutOfRangeException(nameof(regIndex));
  135. }
  136. }
  137. public static bool IsA32Return(ArmEmitterContext context)
  138. {
  139. switch (context.CurrOp)
  140. {
  141. case IOpCode32MemMult op:
  142. return true; // Setting PC using LDM is nearly always a return.
  143. case OpCode32AluRsImm op:
  144. return op.Rm == RegisterAlias.Aarch32Lr;
  145. case OpCode32AluRsReg op:
  146. return op.Rm == RegisterAlias.Aarch32Lr;
  147. case OpCode32AluReg op:
  148. return op.Rm == RegisterAlias.Aarch32Lr;
  149. case OpCode32Mem op:
  150. return op.Rn == RegisterAlias.Aarch32Sp && op.WBack && !op.Index; // Setting PC to an address stored on the stack is nearly always a return.
  151. }
  152. return false;
  153. }
  154. public static void EmitBxWritePc(ArmEmitterContext context, Operand pc, int sourceRegister = 0)
  155. {
  156. bool isReturn = sourceRegister == RegisterAlias.Aarch32Lr || IsA32Return(context);
  157. Operand mode = context.BitwiseAnd(pc, Const(1));
  158. SetFlag(context, PState.TFlag, mode);
  159. Operand addr = context.ConditionalSelect(mode, context.BitwiseAnd(pc, Const(~1)), context.BitwiseAnd(pc, Const(~3)));
  160. InstEmitFlowHelper.EmitVirtualJump(context, addr, isReturn);
  161. }
  162. public static Operand GetIntOrZR(ArmEmitterContext context, int regIndex)
  163. {
  164. if (regIndex == RegisterConsts.ZeroIndex)
  165. {
  166. OperandType type = context.CurrOp.GetOperandType();
  167. return type == OperandType.I32 ? Const(0) : Const(0L);
  168. }
  169. else
  170. {
  171. return GetIntOrSP(context, regIndex);
  172. }
  173. }
  174. public static void SetIntOrZR(ArmEmitterContext context, int regIndex, Operand value)
  175. {
  176. if (regIndex == RegisterConsts.ZeroIndex)
  177. {
  178. return;
  179. }
  180. SetIntOrSP(context, regIndex, value);
  181. }
  182. public static Operand GetIntOrSP(ArmEmitterContext context, int regIndex)
  183. {
  184. Operand value = Register(regIndex, RegisterType.Integer, OperandType.I64);
  185. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  186. {
  187. value = context.ConvertI64ToI32(value);
  188. }
  189. return value;
  190. }
  191. public static void SetIntOrSP(ArmEmitterContext context, int regIndex, Operand value)
  192. {
  193. Operand reg = Register(regIndex, RegisterType.Integer, OperandType.I64);
  194. if (value.Type == OperandType.I32)
  195. {
  196. value = context.ZeroExtend32(OperandType.I64, value);
  197. }
  198. context.Copy(reg, value);
  199. }
  200. public static Operand GetVec(int regIndex)
  201. {
  202. return Register(regIndex, RegisterType.Vector, OperandType.V128);
  203. }
  204. public static Operand GetFlag(PState stateFlag)
  205. {
  206. return Register((int)stateFlag, RegisterType.Flag, OperandType.I32);
  207. }
  208. public static Operand GetFpFlag(FPState stateFlag)
  209. {
  210. return Register((int)stateFlag, RegisterType.FpFlag, OperandType.I32);
  211. }
  212. public static void SetFlag(ArmEmitterContext context, PState stateFlag, Operand value)
  213. {
  214. context.Copy(GetFlag(stateFlag), value);
  215. context.MarkFlagSet(stateFlag);
  216. }
  217. public static void SetFpFlag(ArmEmitterContext context, FPState stateFlag, Operand value)
  218. {
  219. context.Copy(GetFpFlag(stateFlag), value);
  220. }
  221. }
  222. }