InstEmitFlow.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. context.Return(Const(op.Immediate));
  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));
  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. context.Return(context.BitwiseOr(GetIntOrZR(context, RegisterAlias.Lr), Const(CallFlag)));
  57. }
  58. public static void Tbnz(ArmEmitterContext context) => EmitTb(context, onNotZero: true);
  59. public static void Tbz(ArmEmitterContext context) => EmitTb(context, onNotZero: false);
  60. private static void EmitTb(ArmEmitterContext context, bool onNotZero)
  61. {
  62. OpCodeBImmTest op = (OpCodeBImmTest)context.CurrOp;
  63. Operand value = context.BitwiseAnd(GetIntOrZR(context, op.Rt), Const(1L << op.Bit));
  64. EmitBranch(context, value, onNotZero);
  65. }
  66. private static void EmitBranch(ArmEmitterContext context, Condition cond)
  67. {
  68. OpCodeBImm op = (OpCodeBImm)context.CurrOp;
  69. if (context.CurrBlock.Branch != null)
  70. {
  71. EmitCondBranch(context, context.GetLabel((ulong)op.Immediate), cond);
  72. if (context.CurrBlock.Next == null)
  73. {
  74. context.Return(Const(op.Address + 4));
  75. }
  76. }
  77. else
  78. {
  79. Operand lblTaken = Label();
  80. EmitCondBranch(context, lblTaken, cond);
  81. context.Return(Const(op.Address + 4));
  82. context.MarkLabel(lblTaken);
  83. context.Return(Const(op.Immediate));
  84. }
  85. }
  86. private static void EmitBranch(ArmEmitterContext context, Operand value, bool onNotZero)
  87. {
  88. OpCodeBImm op = (OpCodeBImm)context.CurrOp;
  89. if (context.CurrBlock.Branch != null)
  90. {
  91. Operand lblTarget = context.GetLabel((ulong)op.Immediate);
  92. if (onNotZero)
  93. {
  94. context.BranchIfTrue(lblTarget, value);
  95. }
  96. else
  97. {
  98. context.BranchIfFalse(lblTarget, value);
  99. }
  100. if (context.CurrBlock.Next == null)
  101. {
  102. context.Return(Const(op.Address + 4));
  103. }
  104. }
  105. else
  106. {
  107. Operand lblTaken = Label();
  108. if (onNotZero)
  109. {
  110. context.BranchIfTrue(lblTaken, value);
  111. }
  112. else
  113. {
  114. context.BranchIfFalse(lblTaken, value);
  115. }
  116. context.Return(Const(op.Address + 4));
  117. context.MarkLabel(lblTaken);
  118. context.Return(Const(op.Immediate));
  119. }
  120. }
  121. }
  122. }