AInstEmitFlow.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System.Reflection.Emit;
  5. namespace ChocolArm64.Instruction
  6. {
  7. static partial class AInstEmit
  8. {
  9. public static void B(AILEmitterCtx Context)
  10. {
  11. AOpCodeBImmAl Op = (AOpCodeBImmAl)Context.CurrOp;
  12. if (Context.CurrBlock.Branch != null)
  13. {
  14. Context.Emit(OpCodes.Br, Context.GetLabel(Op.Imm));
  15. }
  16. else
  17. {
  18. Context.EmitStoreState();
  19. Context.EmitLdc_I8(Op.Imm);
  20. Context.Emit(OpCodes.Ret);
  21. }
  22. }
  23. public static void B_Cond(AILEmitterCtx Context)
  24. {
  25. AOpCodeBImmCond Op = (AOpCodeBImmCond)Context.CurrOp;
  26. EmitBranch(Context, Op.Cond);
  27. }
  28. public static void Bl(AILEmitterCtx Context)
  29. {
  30. AOpCodeBImmAl Op = (AOpCodeBImmAl)Context.CurrOp;
  31. Context.EmitLdc_I(Op.Position + 4);
  32. Context.EmitStint(AThreadState.LRIndex);
  33. Context.EmitStoreState();
  34. if (Context.TryOptEmitSubroutineCall())
  35. {
  36. //Note: the return value of the called method will be placed
  37. //at the Stack, the return value is always a Int64 with the
  38. //return address of the function. We check if the address is
  39. //correct, if it isn't we keep returning until we reach the dispatcher.
  40. Context.Emit(OpCodes.Dup);
  41. Context.EmitLdc_I8(Op.Position + 4);
  42. AILLabel LblContinue = new AILLabel();
  43. Context.Emit(OpCodes.Beq_S, LblContinue);
  44. Context.Emit(OpCodes.Ret);
  45. Context.MarkLabel(LblContinue);
  46. Context.Emit(OpCodes.Pop);
  47. Context.EmitLoadState(Context.CurrBlock.Next);
  48. }
  49. else
  50. {
  51. Context.EmitLdc_I8(Op.Imm);
  52. Context.Emit(OpCodes.Ret);
  53. }
  54. }
  55. public static void Blr(AILEmitterCtx Context)
  56. {
  57. AOpCodeBReg Op = (AOpCodeBReg)Context.CurrOp;
  58. Context.EmitLdc_I(Op.Position + 4);
  59. Context.EmitStint(AThreadState.LRIndex);
  60. Context.EmitStoreState();
  61. Context.EmitLdintzr(Op.Rn);
  62. Context.Emit(OpCodes.Ret);
  63. }
  64. public static void Br(AILEmitterCtx Context)
  65. {
  66. AOpCodeBReg Op = (AOpCodeBReg)Context.CurrOp;
  67. Context.EmitStoreState();
  68. Context.EmitLdintzr(Op.Rn);
  69. Context.Emit(OpCodes.Ret);
  70. }
  71. public static void Cbnz(AILEmitterCtx Context) => EmitCb(Context, OpCodes.Bne_Un);
  72. public static void Cbz(AILEmitterCtx Context) => EmitCb(Context, OpCodes.Beq);
  73. private static void EmitCb(AILEmitterCtx Context, OpCode ILOp)
  74. {
  75. AOpCodeBImmCmp Op = (AOpCodeBImmCmp)Context.CurrOp;
  76. Context.EmitLdintzr(Op.Rt);
  77. Context.EmitLdc_I(0);
  78. EmitBranch(Context, ILOp);
  79. }
  80. public static void Ret(AILEmitterCtx Context)
  81. {
  82. Context.EmitStoreState();
  83. Context.EmitLdint(AThreadState.LRIndex);
  84. Context.Emit(OpCodes.Ret);
  85. }
  86. public static void Tbnz(AILEmitterCtx Context) => EmitTb(Context, OpCodes.Bne_Un);
  87. public static void Tbz(AILEmitterCtx Context) => EmitTb(Context, OpCodes.Beq);
  88. private static void EmitTb(AILEmitterCtx Context, OpCode ILOp)
  89. {
  90. AOpCodeBImmTest Op = (AOpCodeBImmTest)Context.CurrOp;
  91. Context.EmitLdintzr(Op.Rt);
  92. Context.EmitLdc_I(1L << Op.Pos);
  93. Context.Emit(OpCodes.And);
  94. Context.EmitLdc_I(0);
  95. EmitBranch(Context, ILOp);
  96. }
  97. private static void EmitBranch(AILEmitterCtx Context, ACond Cond)
  98. {
  99. AOpCodeBImm Op = (AOpCodeBImm)Context.CurrOp;
  100. if (Context.CurrBlock.Next != null &&
  101. Context.CurrBlock.Branch != null)
  102. {
  103. Context.EmitCondBranch(Context.GetLabel(Op.Imm), Cond);
  104. }
  105. else
  106. {
  107. Context.EmitStoreState();
  108. AILLabel LblTaken = new AILLabel();
  109. Context.EmitCondBranch(LblTaken, Cond);
  110. Context.EmitLdc_I8(Op.Position + 4);
  111. Context.Emit(OpCodes.Ret);
  112. Context.MarkLabel(LblTaken);
  113. Context.EmitLdc_I8(Op.Imm);
  114. Context.Emit(OpCodes.Ret);
  115. }
  116. }
  117. private static void EmitBranch(AILEmitterCtx Context, OpCode ILOp)
  118. {
  119. AOpCodeBImm Op = (AOpCodeBImm)Context.CurrOp;
  120. if (Context.CurrBlock.Next != null &&
  121. Context.CurrBlock.Branch != null)
  122. {
  123. Context.Emit(ILOp, Context.GetLabel(Op.Imm));
  124. }
  125. else
  126. {
  127. Context.EmitStoreState();
  128. AILLabel LblTaken = new AILLabel();
  129. Context.Emit(ILOp, LblTaken);
  130. Context.EmitLdc_I8(Op.Position + 4);
  131. Context.Emit(OpCodes.Ret);
  132. Context.MarkLabel(LblTaken);
  133. Context.EmitLdc_I8(Op.Imm);
  134. Context.Emit(OpCodes.Ret);
  135. }
  136. }
  137. }
  138. }