InstEmitFlowHelper.cs 4.5 KB

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