InstEmitFlow.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System.Reflection.Emit;
  5. using static ChocolArm64.Instructions.InstEmitFlowHelper;
  6. namespace ChocolArm64.Instructions
  7. {
  8. static partial class InstEmit
  9. {
  10. public static void B(ILEmitterCtx context)
  11. {
  12. OpCodeBImmAl64 op = (OpCodeBImmAl64)context.CurrOp;
  13. if (context.CurrBlock.Branch != null)
  14. {
  15. context.Emit(OpCodes.Br, context.GetLabel(op.Imm));
  16. }
  17. else
  18. {
  19. context.EmitStoreState();
  20. context.EmitLdc_I8(op.Imm);
  21. context.Emit(OpCodes.Ret);
  22. }
  23. }
  24. public static void B_Cond(ILEmitterCtx context)
  25. {
  26. OpCodeBImmCond64 op = (OpCodeBImmCond64)context.CurrOp;
  27. EmitBranch(context, op.Cond);
  28. }
  29. public static void Bl(ILEmitterCtx context)
  30. {
  31. OpCodeBImmAl64 op = (OpCodeBImmAl64)context.CurrOp;
  32. context.EmitLdc_I(op.Position + 4);
  33. context.EmitStint(RegisterAlias.Lr);
  34. EmitCall(context, op.Imm);
  35. }
  36. public static void Blr(ILEmitterCtx context)
  37. {
  38. OpCodeBReg64 op = (OpCodeBReg64)context.CurrOp;
  39. context.EmitLdintzr(op.Rn);
  40. context.EmitLdc_I(op.Position + 4);
  41. context.EmitStint(RegisterAlias.Lr);
  42. context.EmitStoreState();
  43. EmitVirtualCall(context);
  44. }
  45. public static void Br(ILEmitterCtx context)
  46. {
  47. OpCodeBReg64 op = (OpCodeBReg64)context.CurrOp;
  48. context.HasIndirectJump = true;
  49. context.EmitStoreState();
  50. context.EmitLdintzr(op.Rn);
  51. EmitVirtualJump(context);
  52. }
  53. public static void Cbnz(ILEmitterCtx context) => EmitCb(context, OpCodes.Bne_Un);
  54. public static void Cbz(ILEmitterCtx context) => EmitCb(context, OpCodes.Beq);
  55. private static void EmitCb(ILEmitterCtx context, OpCode ilOp)
  56. {
  57. OpCodeBImmCmp64 op = (OpCodeBImmCmp64)context.CurrOp;
  58. context.EmitLdintzr(op.Rt);
  59. context.EmitLdc_I(0);
  60. EmitBranch(context, ilOp);
  61. }
  62. public static void Ret(ILEmitterCtx context)
  63. {
  64. context.EmitStoreState();
  65. context.EmitLdint(RegisterAlias.Lr);
  66. context.Emit(OpCodes.Ret);
  67. }
  68. public static void Tbnz(ILEmitterCtx context) => EmitTb(context, OpCodes.Bne_Un);
  69. public static void Tbz(ILEmitterCtx context) => EmitTb(context, OpCodes.Beq);
  70. private static void EmitTb(ILEmitterCtx context, OpCode ilOp)
  71. {
  72. OpCodeBImmTest64 op = (OpCodeBImmTest64)context.CurrOp;
  73. context.EmitLdintzr(op.Rt);
  74. context.EmitLdc_I(1L << op.Pos);
  75. context.Emit(OpCodes.And);
  76. context.EmitLdc_I(0);
  77. EmitBranch(context, ilOp);
  78. }
  79. private static void EmitBranch(ILEmitterCtx context, Condition cond)
  80. {
  81. OpCodeBImm64 op = (OpCodeBImm64)context.CurrOp;
  82. if (context.CurrBlock.Branch != null)
  83. {
  84. context.EmitCondBranch(context.GetLabel(op.Imm), cond);
  85. if (context.CurrBlock.Next == null)
  86. {
  87. context.EmitStoreState();
  88. context.EmitLdc_I8(op.Position + 4);
  89. context.Emit(OpCodes.Ret);
  90. }
  91. }
  92. else
  93. {
  94. context.EmitStoreState();
  95. ILLabel lblTaken = new ILLabel();
  96. context.EmitCondBranch(lblTaken, cond);
  97. context.EmitLdc_I8(op.Position + 4);
  98. context.Emit(OpCodes.Ret);
  99. context.MarkLabel(lblTaken);
  100. context.EmitLdc_I8(op.Imm);
  101. context.Emit(OpCodes.Ret);
  102. }
  103. }
  104. private static void EmitBranch(ILEmitterCtx context, OpCode ilOp)
  105. {
  106. OpCodeBImm64 op = (OpCodeBImm64)context.CurrOp;
  107. if (context.CurrBlock.Branch != null)
  108. {
  109. context.Emit(ilOp, context.GetLabel(op.Imm));
  110. if (context.CurrBlock.Next == null)
  111. {
  112. context.EmitStoreState();
  113. context.EmitLdc_I8(op.Position + 4);
  114. context.Emit(OpCodes.Ret);
  115. }
  116. }
  117. else
  118. {
  119. context.EmitStoreState();
  120. ILLabel lblTaken = new ILLabel();
  121. context.Emit(ilOp, lblTaken);
  122. context.EmitLdc_I8(op.Position + 4);
  123. context.Emit(OpCodes.Ret);
  124. context.MarkLabel(lblTaken);
  125. context.EmitLdc_I8(op.Imm);
  126. context.Emit(OpCodes.Ret);
  127. }
  128. }
  129. }
  130. }