AInstEmitFlow.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. if (AOptimizations.GenerateCallStack)
  32. {
  33. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  34. Context.EmitLdc_I8(Op.Imm);
  35. Context.EmitPrivateCall(typeof(AThreadState), nameof(AThreadState.EnterMethod));
  36. }
  37. Context.EmitLdc_I(Op.Position + 4);
  38. Context.EmitStint(AThreadState.LRIndex);
  39. Context.EmitStoreState();
  40. if (Context.TryOptEmitSubroutineCall())
  41. {
  42. //Note: the return value of the called method will be placed
  43. //at the Stack, the return value is always a Int64 with the
  44. //return address of the function. We check if the address is
  45. //correct, if it isn't we keep returning until we reach the dispatcher.
  46. Context.Emit(OpCodes.Dup);
  47. Context.EmitLdc_I8(Op.Position + 4);
  48. AILLabel LblContinue = new AILLabel();
  49. Context.Emit(OpCodes.Beq_S, LblContinue);
  50. Context.Emit(OpCodes.Ret);
  51. Context.MarkLabel(LblContinue);
  52. Context.Emit(OpCodes.Pop);
  53. Context.EmitLoadState(Context.CurrBlock.Next);
  54. }
  55. else
  56. {
  57. Context.EmitLdc_I8(Op.Imm);
  58. Context.Emit(OpCodes.Ret);
  59. }
  60. }
  61. public static void Blr(AILEmitterCtx Context)
  62. {
  63. AOpCodeBReg Op = (AOpCodeBReg)Context.CurrOp;
  64. if (AOptimizations.GenerateCallStack)
  65. {
  66. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  67. Context.EmitLdintzr(Op.Rn);
  68. Context.EmitPrivateCall(typeof(AThreadState), nameof(AThreadState.EnterMethod));
  69. }
  70. Context.EmitLdc_I(Op.Position + 4);
  71. Context.EmitStint(AThreadState.LRIndex);
  72. Context.EmitStoreState();
  73. Context.EmitLdintzr(Op.Rn);
  74. Context.Emit(OpCodes.Ret);
  75. }
  76. public static void Br(AILEmitterCtx Context)
  77. {
  78. AOpCodeBReg Op = (AOpCodeBReg)Context.CurrOp;
  79. if (AOptimizations.GenerateCallStack)
  80. {
  81. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  82. Context.EmitLdintzr(Op.Rn);
  83. Context.EmitPrivateCall(typeof(AThreadState), nameof(AThreadState.JumpMethod));
  84. }
  85. Context.EmitStoreState();
  86. Context.EmitLdintzr(Op.Rn);
  87. Context.Emit(OpCodes.Ret);
  88. }
  89. public static void Cbnz(AILEmitterCtx Context) => EmitCb(Context, OpCodes.Bne_Un);
  90. public static void Cbz(AILEmitterCtx Context) => EmitCb(Context, OpCodes.Beq);
  91. private static void EmitCb(AILEmitterCtx Context, OpCode ILOp)
  92. {
  93. AOpCodeBImmCmp Op = (AOpCodeBImmCmp)Context.CurrOp;
  94. Context.EmitLdintzr(Op.Rt);
  95. Context.EmitLdc_I(0);
  96. EmitBranch(Context, ILOp);
  97. }
  98. public static void Ret(AILEmitterCtx Context)
  99. {
  100. if (AOptimizations.GenerateCallStack)
  101. {
  102. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  103. Context.EmitPrivateCall(typeof(AThreadState), nameof(AThreadState.ExitMethod));
  104. }
  105. Context.EmitStoreState();
  106. Context.EmitLdint(AThreadState.LRIndex);
  107. Context.Emit(OpCodes.Ret);
  108. }
  109. public static void Tbnz(AILEmitterCtx Context) => EmitTb(Context, OpCodes.Bne_Un);
  110. public static void Tbz(AILEmitterCtx Context) => EmitTb(Context, OpCodes.Beq);
  111. private static void EmitTb(AILEmitterCtx Context, OpCode ILOp)
  112. {
  113. AOpCodeBImmTest Op = (AOpCodeBImmTest)Context.CurrOp;
  114. Context.EmitLdintzr(Op.Rt);
  115. Context.EmitLdc_I(1L << Op.Pos);
  116. Context.Emit(OpCodes.And);
  117. Context.EmitLdc_I(0);
  118. EmitBranch(Context, ILOp);
  119. }
  120. private static void EmitBranch(AILEmitterCtx Context, ACond Cond)
  121. {
  122. AOpCodeBImm Op = (AOpCodeBImm)Context.CurrOp;
  123. if (Context.CurrBlock.Next != null &&
  124. Context.CurrBlock.Branch != null)
  125. {
  126. Context.EmitCondBranch(Context.GetLabel(Op.Imm), Cond);
  127. }
  128. else
  129. {
  130. Context.EmitStoreState();
  131. AILLabel LblTaken = new AILLabel();
  132. Context.EmitCondBranch(LblTaken, Cond);
  133. Context.EmitLdc_I8(Op.Position + 4);
  134. Context.Emit(OpCodes.Ret);
  135. Context.MarkLabel(LblTaken);
  136. Context.EmitLdc_I8(Op.Imm);
  137. Context.Emit(OpCodes.Ret);
  138. }
  139. }
  140. private static void EmitBranch(AILEmitterCtx Context, OpCode ILOp)
  141. {
  142. AOpCodeBImm Op = (AOpCodeBImm)Context.CurrOp;
  143. if (Context.CurrBlock.Next != null &&
  144. Context.CurrBlock.Branch != null)
  145. {
  146. Context.Emit(ILOp, Context.GetLabel(Op.Imm));
  147. }
  148. else
  149. {
  150. Context.EmitStoreState();
  151. AILLabel LblTaken = new AILLabel();
  152. Context.Emit(ILOp, LblTaken);
  153. Context.EmitLdc_I8(Op.Position + 4);
  154. Context.Emit(OpCodes.Ret);
  155. Context.MarkLabel(LblTaken);
  156. Context.EmitLdc_I8(Op.Imm);
  157. Context.Emit(OpCodes.Ret);
  158. }
  159. }
  160. }
  161. }