InstEmitHelper.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 GetIntOrSP(context, GetRegisterAlias(context.Mode, regIndex));
  39. }
  40. }
  41. public static void SetIntA32(ArmEmitterContext context, int regIndex, Operand value)
  42. {
  43. if (regIndex == RegisterAlias.Aarch32Pc)
  44. {
  45. context.StoreToContext();
  46. EmitBxWritePc(context, value);
  47. }
  48. else
  49. {
  50. SetIntOrSP(context, GetRegisterAlias(context.Mode, regIndex), value);
  51. }
  52. }
  53. public static int GetRegisterAlias(Aarch32Mode mode, int regIndex)
  54. {
  55. // Only registers >= 8 are banked,
  56. // with registers in the range [8, 12] being
  57. // banked for the FIQ mode, and registers
  58. // 13 and 14 being banked for all modes.
  59. if ((uint)regIndex < 8)
  60. {
  61. return regIndex;
  62. }
  63. return GetBankedRegisterAlias(mode, regIndex);
  64. }
  65. public static int GetBankedRegisterAlias(Aarch32Mode mode, int regIndex)
  66. {
  67. switch (regIndex)
  68. {
  69. case 8: return mode == Aarch32Mode.Fiq
  70. ? RegisterAlias.R8Fiq
  71. : RegisterAlias.R8Usr;
  72. case 9: return mode == Aarch32Mode.Fiq
  73. ? RegisterAlias.R9Fiq
  74. : RegisterAlias.R9Usr;
  75. case 10: return mode == Aarch32Mode.Fiq
  76. ? RegisterAlias.R10Fiq
  77. : RegisterAlias.R10Usr;
  78. case 11: return mode == Aarch32Mode.Fiq
  79. ? RegisterAlias.R11Fiq
  80. : RegisterAlias.R11Usr;
  81. case 12: return mode == Aarch32Mode.Fiq
  82. ? RegisterAlias.R12Fiq
  83. : RegisterAlias.R12Usr;
  84. case 13:
  85. switch (mode)
  86. {
  87. case Aarch32Mode.User:
  88. case Aarch32Mode.System: return RegisterAlias.SpUsr;
  89. case Aarch32Mode.Fiq: return RegisterAlias.SpFiq;
  90. case Aarch32Mode.Irq: return RegisterAlias.SpIrq;
  91. case Aarch32Mode.Supervisor: return RegisterAlias.SpSvc;
  92. case Aarch32Mode.Abort: return RegisterAlias.SpAbt;
  93. case Aarch32Mode.Hypervisor: return RegisterAlias.SpHyp;
  94. case Aarch32Mode.Undefined: return RegisterAlias.SpUnd;
  95. default: throw new ArgumentException(nameof(mode));
  96. }
  97. case 14:
  98. switch (mode)
  99. {
  100. case Aarch32Mode.User:
  101. case Aarch32Mode.Hypervisor:
  102. case Aarch32Mode.System: return RegisterAlias.LrUsr;
  103. case Aarch32Mode.Fiq: return RegisterAlias.LrFiq;
  104. case Aarch32Mode.Irq: return RegisterAlias.LrIrq;
  105. case Aarch32Mode.Supervisor: return RegisterAlias.LrSvc;
  106. case Aarch32Mode.Abort: return RegisterAlias.LrAbt;
  107. case Aarch32Mode.Undefined: return RegisterAlias.LrUnd;
  108. default: throw new ArgumentException(nameof(mode));
  109. }
  110. default: throw new ArgumentOutOfRangeException(nameof(regIndex));
  111. }
  112. }
  113. public static void EmitBxWritePc(ArmEmitterContext context, Operand pc)
  114. {
  115. Operand mode = context.BitwiseAnd(pc, Const(1));
  116. SetFlag(context, PState.TFlag, mode);
  117. Operand lblArmMode = Label();
  118. context.BranchIfTrue(lblArmMode, mode);
  119. context.Return(context.ZeroExtend32(OperandType.I64, context.BitwiseAnd(pc, Const(~1))));
  120. context.MarkLabel(lblArmMode);
  121. context.Return(context.ZeroExtend32(OperandType.I64, context.BitwiseAnd(pc, Const(~3))));
  122. }
  123. public static Operand GetIntOrZR(ArmEmitterContext context, int regIndex)
  124. {
  125. if (regIndex == RegisterConsts.ZeroIndex)
  126. {
  127. OperandType type = context.CurrOp.GetOperandType();
  128. return type == OperandType.I32 ? Const(0) : Const(0L);
  129. }
  130. else
  131. {
  132. return GetIntOrSP(context, regIndex);
  133. }
  134. }
  135. public static void SetIntOrZR(ArmEmitterContext context, int regIndex, Operand value)
  136. {
  137. if (regIndex == RegisterConsts.ZeroIndex)
  138. {
  139. return;
  140. }
  141. SetIntOrSP(context, regIndex, value);
  142. }
  143. public static Operand GetIntOrSP(ArmEmitterContext context, int regIndex)
  144. {
  145. Operand value = Register(regIndex, RegisterType.Integer, OperandType.I64);
  146. if (context.CurrOp.RegisterSize == RegisterSize.Int32)
  147. {
  148. value = context.ConvertI64ToI32(value);
  149. }
  150. return value;
  151. }
  152. public static void SetIntOrSP(ArmEmitterContext context, int regIndex, Operand value)
  153. {
  154. Operand reg = Register(regIndex, RegisterType.Integer, OperandType.I64);
  155. if (value.Type == OperandType.I32)
  156. {
  157. value = context.ZeroExtend32(OperandType.I64, value);
  158. }
  159. context.Copy(reg, value);
  160. }
  161. public static Operand GetVec(int regIndex)
  162. {
  163. return Register(regIndex, RegisterType.Vector, OperandType.V128);
  164. }
  165. public static Operand GetFlag(PState stateFlag)
  166. {
  167. return Register((int)stateFlag, RegisterType.Flag, OperandType.I32);
  168. }
  169. public static void SetFlag(ArmEmitterContext context, PState stateFlag, Operand value)
  170. {
  171. context.Copy(GetFlag(stateFlag), value);
  172. context.MarkFlagSet(stateFlag);
  173. }
  174. }
  175. }