InstEmitCcmp.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System;
  5. using System.Reflection.Emit;
  6. using static ChocolArm64.Instructions.InstEmitAluHelper;
  7. namespace ChocolArm64.Instructions
  8. {
  9. static partial class InstEmit
  10. {
  11. private enum CcmpOp
  12. {
  13. Cmp,
  14. Cmn
  15. }
  16. public static void Ccmn(ILEmitterCtx context) => EmitCcmp(context, CcmpOp.Cmn);
  17. public static void Ccmp(ILEmitterCtx context) => EmitCcmp(context, CcmpOp.Cmp);
  18. private static void EmitCcmp(ILEmitterCtx context, CcmpOp cmpOp)
  19. {
  20. OpCodeCcmp64 op = (OpCodeCcmp64)context.CurrOp;
  21. ILLabel lblTrue = new ILLabel();
  22. ILLabel lblEnd = new ILLabel();
  23. context.EmitCondBranch(lblTrue, op.Cond);
  24. context.EmitLdc_I4((op.Nzcv >> 0) & 1);
  25. context.EmitStflg((int)PState.VBit);
  26. context.EmitLdc_I4((op.Nzcv >> 1) & 1);
  27. context.EmitStflg((int)PState.CBit);
  28. context.EmitLdc_I4((op.Nzcv >> 2) & 1);
  29. context.EmitStflg((int)PState.ZBit);
  30. context.EmitLdc_I4((op.Nzcv >> 3) & 1);
  31. context.EmitStflg((int)PState.NBit);
  32. context.Emit(OpCodes.Br_S, lblEnd);
  33. context.MarkLabel(lblTrue);
  34. EmitAluLoadOpers(context);
  35. if (cmpOp == CcmpOp.Cmp)
  36. {
  37. context.Emit(OpCodes.Sub);
  38. context.EmitZnFlagCheck();
  39. EmitSubsCCheck(context);
  40. EmitSubsVCheck(context);
  41. }
  42. else if (cmpOp == CcmpOp.Cmn)
  43. {
  44. context.Emit(OpCodes.Add);
  45. context.EmitZnFlagCheck();
  46. EmitAddsCCheck(context);
  47. EmitAddsVCheck(context);
  48. }
  49. else
  50. {
  51. throw new ArgumentException(nameof(cmpOp));
  52. }
  53. context.Emit(OpCodes.Pop);
  54. context.MarkLabel(lblEnd);
  55. }
  56. }
  57. }