InstEmitFlow.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using static ARMeilleure.Instructions.InstEmitFlowHelper;
  6. using static ARMeilleure.Instructions.InstEmitHelper;
  7. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  8. namespace ARMeilleure.Instructions
  9. {
  10. static partial class InstEmit
  11. {
  12. public static void B(ArmEmitterContext context)
  13. {
  14. OpCodeBImmAl op = (OpCodeBImmAl)context.CurrOp;
  15. if (context.CurrBlock.Branch != null)
  16. {
  17. context.Branch(context.GetLabel((ulong)op.Immediate));
  18. }
  19. else
  20. {
  21. EmitTailContinue(context, Const(op.Immediate), context.CurrBlock.TailCall);
  22. }
  23. }
  24. public static void B_Cond(ArmEmitterContext context)
  25. {
  26. OpCodeBImmCond op = (OpCodeBImmCond)context.CurrOp;
  27. EmitBranch(context, op.Cond);
  28. }
  29. public static void Bl(ArmEmitterContext context)
  30. {
  31. OpCodeBImmAl op = (OpCodeBImmAl)context.CurrOp;
  32. context.Copy(GetIntOrZR(context, RegisterAlias.Lr), Const(op.Address + 4));
  33. EmitCall(context, (ulong)op.Immediate);
  34. }
  35. public static void Blr(ArmEmitterContext context)
  36. {
  37. OpCodeBReg op = (OpCodeBReg)context.CurrOp;
  38. Operand n = context.Copy(GetIntOrZR(context, op.Rn));
  39. context.Copy(GetIntOrZR(context, RegisterAlias.Lr), Const(op.Address + 4));
  40. EmitVirtualCall(context, n);
  41. }
  42. public static void Br(ArmEmitterContext context)
  43. {
  44. OpCodeBReg op = (OpCodeBReg)context.CurrOp;
  45. EmitVirtualJump(context, GetIntOrZR(context, op.Rn), op.Rn == RegisterAlias.Lr);
  46. }
  47. public static void Cbnz(ArmEmitterContext context) => EmitCb(context, onNotZero: true);
  48. public static void Cbz(ArmEmitterContext context) => EmitCb(context, onNotZero: false);
  49. private static void EmitCb(ArmEmitterContext context, bool onNotZero)
  50. {
  51. OpCodeBImmCmp op = (OpCodeBImmCmp)context.CurrOp;
  52. EmitBranch(context, GetIntOrZR(context, op.Rt), onNotZero);
  53. }
  54. public static void Ret(ArmEmitterContext context)
  55. {
  56. OpCodeBReg op = (OpCodeBReg)context.CurrOp;
  57. context.Return(GetIntOrZR(context, op.Rn));
  58. }
  59. public static void Tbnz(ArmEmitterContext context) => EmitTb(context, onNotZero: true);
  60. public static void Tbz(ArmEmitterContext context) => EmitTb(context, onNotZero: false);
  61. private static void EmitTb(ArmEmitterContext context, bool onNotZero)
  62. {
  63. OpCodeBImmTest op = (OpCodeBImmTest)context.CurrOp;
  64. Operand value = context.BitwiseAnd(GetIntOrZR(context, op.Rt), Const(1L << op.Bit));
  65. EmitBranch(context, value, onNotZero);
  66. }
  67. private static void EmitBranch(ArmEmitterContext context, Condition cond)
  68. {
  69. OpCodeBImm op = (OpCodeBImm)context.CurrOp;
  70. if (context.CurrBlock.Branch != null)
  71. {
  72. EmitCondBranch(context, context.GetLabel((ulong)op.Immediate), cond);
  73. if (context.CurrBlock.Next == null)
  74. {
  75. EmitTailContinue(context, Const(op.Address + 4));
  76. }
  77. }
  78. else
  79. {
  80. Operand lblTaken = Label();
  81. EmitCondBranch(context, lblTaken, cond);
  82. EmitTailContinue(context, Const(op.Address + 4));
  83. context.MarkLabel(lblTaken);
  84. EmitTailContinue(context, Const(op.Immediate));
  85. }
  86. }
  87. private static void EmitBranch(ArmEmitterContext context, Operand value, bool onNotZero)
  88. {
  89. OpCodeBImm op = (OpCodeBImm)context.CurrOp;
  90. if (context.CurrBlock.Branch != null)
  91. {
  92. Operand lblTarget = context.GetLabel((ulong)op.Immediate);
  93. if (onNotZero)
  94. {
  95. context.BranchIfTrue(lblTarget, value);
  96. }
  97. else
  98. {
  99. context.BranchIfFalse(lblTarget, value);
  100. }
  101. if (context.CurrBlock.Next == null)
  102. {
  103. EmitTailContinue(context, Const(op.Address + 4));
  104. }
  105. }
  106. else
  107. {
  108. Operand lblTaken = Label();
  109. if (onNotZero)
  110. {
  111. context.BranchIfTrue(lblTaken, value);
  112. }
  113. else
  114. {
  115. context.BranchIfFalse(lblTaken, value);
  116. }
  117. EmitTailContinue(context, Const(op.Address + 4));
  118. context.MarkLabel(lblTaken);
  119. EmitTailContinue(context, Const(op.Immediate));
  120. }
  121. }
  122. }
  123. }