InstEmitAlu32.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System.Reflection.Emit;
  5. using static ChocolArm64.Instructions.InstEmit32Helper;
  6. using static ChocolArm64.Instructions.InstEmitAluHelper;
  7. namespace ChocolArm64.Instructions
  8. {
  9. static partial class InstEmit32
  10. {
  11. public static void Add(ILEmitterCtx context)
  12. {
  13. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  14. EmitAluLoadOpers(context, setCarry: false);
  15. context.Emit(OpCodes.Add);
  16. if (op.SetFlags)
  17. {
  18. context.EmitZnFlagCheck();
  19. EmitAddsCCheck(context);
  20. EmitAddsVCheck(context);
  21. }
  22. EmitAluStore(context);
  23. }
  24. public static void Cmp(ILEmitterCtx context)
  25. {
  26. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  27. EmitAluLoadOpers(context, setCarry: false);
  28. context.Emit(OpCodes.Sub);
  29. context.EmitZnFlagCheck();
  30. EmitSubsCCheck(context);
  31. EmitSubsVCheck(context);
  32. context.Emit(OpCodes.Pop);
  33. }
  34. public static void Mov(ILEmitterCtx context)
  35. {
  36. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  37. EmitAluLoadOper2(context);
  38. if (op.SetFlags)
  39. {
  40. context.EmitZnFlagCheck();
  41. }
  42. EmitAluStore(context);
  43. }
  44. public static void Sub(ILEmitterCtx context)
  45. {
  46. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  47. EmitAluLoadOpers(context, setCarry: false);
  48. context.Emit(OpCodes.Sub);
  49. if (op.SetFlags)
  50. {
  51. context.EmitZnFlagCheck();
  52. EmitSubsCCheck(context);
  53. EmitSubsVCheck(context);
  54. }
  55. EmitAluStore(context);
  56. }
  57. private static void EmitAluStore(ILEmitterCtx context)
  58. {
  59. IOpCode32Alu op = (IOpCode32Alu)context.CurrOp;
  60. if (op.Rd == RegisterAlias.Aarch32Pc)
  61. {
  62. if (op.SetFlags)
  63. {
  64. //TODO: Load SPSR etc.
  65. context.EmitLdflg((int)PState.TBit);
  66. ILLabel lblThumb = new ILLabel();
  67. ILLabel lblEnd = new ILLabel();
  68. context.Emit(OpCodes.Brtrue_S, lblThumb);
  69. context.EmitLdc_I4(~3);
  70. context.Emit(OpCodes.Br_S, lblEnd);
  71. context.MarkLabel(lblThumb);
  72. context.EmitLdc_I4(~1);
  73. context.MarkLabel(lblEnd);
  74. context.Emit(OpCodes.And);
  75. context.Emit(OpCodes.Conv_U8);
  76. context.Emit(OpCodes.Ret);
  77. }
  78. else
  79. {
  80. EmitAluWritePc(context);
  81. }
  82. }
  83. else
  84. {
  85. context.EmitStint(GetRegisterAlias(context.Mode, op.Rd));
  86. }
  87. }
  88. private static void EmitAluWritePc(ILEmitterCtx context)
  89. {
  90. context.EmitStoreState();
  91. if (IsThumb(context.CurrOp))
  92. {
  93. context.EmitLdc_I4(~1);
  94. context.Emit(OpCodes.And);
  95. context.Emit(OpCodes.Conv_U8);
  96. context.Emit(OpCodes.Ret);
  97. }
  98. else
  99. {
  100. EmitBxWritePc(context);
  101. }
  102. }
  103. }
  104. }