InstEmitFlowHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using ChocolArm64.IntermediateRepresentation;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System.Reflection;
  5. using System.Reflection.Emit;
  6. namespace ChocolArm64.Instructions
  7. {
  8. static class InstEmitFlowHelper
  9. {
  10. public static void EmitCall(ILEmitterCtx context, long imm)
  11. {
  12. if (context.Tier == TranslationTier.Tier0)
  13. {
  14. context.EmitStoreContext();
  15. context.TranslateAhead(imm);
  16. context.EmitLdc_I8(imm);
  17. context.Emit(OpCodes.Ret);
  18. return;
  19. }
  20. if (!context.TryOptEmitSubroutineCall())
  21. {
  22. context.HasSlowCall = true;
  23. context.EmitStoreContext();
  24. context.TranslateAhead(imm);
  25. context.EmitLdarg(TranslatedSub.StateArgIdx);
  26. context.EmitLdfld(typeof(CpuThreadState).GetField(nameof(CpuThreadState.CurrentTranslator),
  27. BindingFlags.Instance |
  28. BindingFlags.NonPublic));
  29. context.EmitLdarg(TranslatedSub.StateArgIdx);
  30. context.EmitLdc_I8(imm);
  31. context.EmitLdc_I4((int)CallType.Call);
  32. context.EmitPrivateCall(typeof(Translator), nameof(Translator.GetOrTranslateSubroutine));
  33. context.EmitLdarg(TranslatedSub.StateArgIdx);
  34. context.EmitLdarg(TranslatedSub.MemoryArgIdx);
  35. context.EmitCall(typeof(TranslatedSub), nameof(TranslatedSub.Execute));
  36. }
  37. EmitContinueOrReturnCheck(context);
  38. }
  39. public static void EmitVirtualCall(ILEmitterCtx context)
  40. {
  41. EmitVirtualCallOrJump(context, isJump: false);
  42. }
  43. public static void EmitVirtualJump(ILEmitterCtx context)
  44. {
  45. EmitVirtualCallOrJump(context, isJump: true);
  46. }
  47. private static void EmitVirtualCallOrJump(ILEmitterCtx context, bool isJump)
  48. {
  49. if (context.Tier == TranslationTier.Tier0)
  50. {
  51. context.Emit(OpCodes.Ret);
  52. }
  53. else
  54. {
  55. context.EmitSttmp();
  56. context.EmitLdarg(TranslatedSub.StateArgIdx);
  57. context.EmitLdfld(typeof(CpuThreadState).GetField(nameof(CpuThreadState.CurrentTranslator),
  58. BindingFlags.Instance |
  59. BindingFlags.NonPublic));
  60. context.EmitLdarg(TranslatedSub.StateArgIdx);
  61. context.EmitLdtmp();
  62. context.EmitLdc_I4(isJump
  63. ? (int)CallType.VirtualJump
  64. : (int)CallType.VirtualCall);
  65. context.EmitPrivateCall(typeof(Translator), nameof(Translator.GetOrTranslateSubroutine));
  66. context.EmitLdarg(TranslatedSub.StateArgIdx);
  67. context.EmitLdarg(TranslatedSub.MemoryArgIdx);
  68. if (isJump)
  69. {
  70. // The tail prefix allows the JIT to jump to the next function,
  71. // while releasing the stack space used by the current one.
  72. // This is ideal for BR ARM instructions, which are
  73. // basically indirect tail calls.
  74. context.Emit(OpCodes.Tailcall);
  75. }
  76. MethodInfo mthdInfo = typeof(ArmSubroutine).GetMethod("Invoke");
  77. context.EmitCall(mthdInfo, isVirtual: true);
  78. if (!isJump)
  79. {
  80. EmitContinueOrReturnCheck(context);
  81. }
  82. else
  83. {
  84. context.Emit(OpCodes.Ret);
  85. }
  86. }
  87. }
  88. private static void EmitContinueOrReturnCheck(ILEmitterCtx context)
  89. {
  90. // Note: The return value of the called method will be placed
  91. // at the Stack, the return value is always a Int64 with the
  92. // return address of the function. We check if the address is
  93. // correct, if it isn't we keep returning until we reach the dispatcher.
  94. if (context.CurrBlock.Next != null)
  95. {
  96. context.Emit(OpCodes.Dup);
  97. context.EmitLdc_I8(context.CurrOp.Position + 4);
  98. ILLabel lblContinue = new ILLabel();
  99. context.Emit(OpCodes.Beq_S, lblContinue);
  100. context.Emit(OpCodes.Ret);
  101. context.MarkLabel(lblContinue);
  102. context.Emit(OpCodes.Pop);
  103. context.EmitLoadContext();
  104. }
  105. else
  106. {
  107. context.Emit(OpCodes.Ret);
  108. }
  109. }
  110. }
  111. }