TranslatedSub.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using ChocolArm64.Memory;
  2. using ChocolArm64.State;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Reflection.Emit;
  9. namespace ChocolArm64
  10. {
  11. class TranslatedSub
  12. {
  13. private delegate long Aa64Subroutine(CpuThreadState register, MemoryManager memory);
  14. private const int MinCallCountForReJit = 250;
  15. private Aa64Subroutine _execDelegate;
  16. public static int StateArgIdx { get; private set; }
  17. public static int MemoryArgIdx { get; private set; }
  18. public static Type[] FixedArgTypes { get; private set; }
  19. public DynamicMethod Method { get; private set; }
  20. public ReadOnlyCollection<Register> Params { get; private set; }
  21. private HashSet<long> _callers;
  22. private TranslatedSubType _type;
  23. private int _callCount;
  24. private bool _needsReJit;
  25. public TranslatedSub(DynamicMethod method, List<Register> Params)
  26. {
  27. if (method == null)
  28. {
  29. throw new ArgumentNullException(nameof(method));
  30. }
  31. if (Params == null)
  32. {
  33. throw new ArgumentNullException(nameof(Params));
  34. }
  35. Method = method;
  36. this.Params = Params.AsReadOnly();
  37. _callers = new HashSet<long>();
  38. PrepareDelegate();
  39. }
  40. static TranslatedSub()
  41. {
  42. MethodInfo mthdInfo = typeof(Aa64Subroutine).GetMethod("Invoke");
  43. ParameterInfo[] Params = mthdInfo.GetParameters();
  44. FixedArgTypes = new Type[Params.Length];
  45. for (int index = 0; index < Params.Length; index++)
  46. {
  47. Type paramType = Params[index].ParameterType;
  48. FixedArgTypes[index] = paramType;
  49. if (paramType == typeof(CpuThreadState))
  50. {
  51. StateArgIdx = index;
  52. }
  53. else if (paramType == typeof(MemoryManager))
  54. {
  55. MemoryArgIdx = index;
  56. }
  57. }
  58. }
  59. private void PrepareDelegate()
  60. {
  61. string name = $"{Method.Name}_Dispatch";
  62. DynamicMethod mthd = new DynamicMethod(name, typeof(long), FixedArgTypes);
  63. ILGenerator generator = mthd.GetILGenerator();
  64. generator.EmitLdargSeq(FixedArgTypes.Length);
  65. foreach (Register reg in Params)
  66. {
  67. generator.EmitLdarg(StateArgIdx);
  68. generator.Emit(OpCodes.Ldfld, reg.GetField());
  69. }
  70. generator.Emit(OpCodes.Call, Method);
  71. generator.Emit(OpCodes.Ret);
  72. _execDelegate = (Aa64Subroutine)mthd.CreateDelegate(typeof(Aa64Subroutine));
  73. }
  74. public bool ShouldReJit()
  75. {
  76. if (_needsReJit && _callCount < MinCallCountForReJit)
  77. {
  78. _callCount++;
  79. return false;
  80. }
  81. return _needsReJit;
  82. }
  83. public long Execute(CpuThreadState threadState, MemoryManager memory)
  84. {
  85. return _execDelegate(threadState, memory);
  86. }
  87. public void AddCaller(long position)
  88. {
  89. lock (_callers)
  90. {
  91. _callers.Add(position);
  92. }
  93. }
  94. public long[] GetCallerPositions()
  95. {
  96. lock (_callers)
  97. {
  98. return _callers.ToArray();
  99. }
  100. }
  101. public void SetType(TranslatedSubType type)
  102. {
  103. _type = type;
  104. if (type == TranslatedSubType.SubTier0)
  105. {
  106. _needsReJit = true;
  107. }
  108. }
  109. public void MarkForReJit() => _needsReJit = true;
  110. }
  111. }