ATranslatedSub.cs 3.7 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 ATranslatedSub
  12. {
  13. private delegate long AA64Subroutine(AThreadState Register, AMemory 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<ARegister> Params { get; private set; }
  21. private HashSet<long> Callers;
  22. private ATranslatedSubType Type;
  23. private int CallCount;
  24. private bool NeedsReJit;
  25. public ATranslatedSub(DynamicMethod Method, List<ARegister> 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. this.Method = Method;
  36. this.Params = Params.AsReadOnly();
  37. Callers = new HashSet<long>();
  38. PrepareDelegate();
  39. }
  40. static ATranslatedSub()
  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(AThreadState))
  50. {
  51. StateArgIdx = Index;
  52. }
  53. else if (ParamType == typeof(AMemory))
  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 (ARegister 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(AThreadState ThreadState, AMemory 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(ATranslatedSubType Type)
  102. {
  103. this.Type = Type;
  104. if (Type == ATranslatedSubType.SubTier0)
  105. {
  106. NeedsReJit = true;
  107. }
  108. }
  109. public void MarkForReJit() => NeedsReJit = true;
  110. }
  111. }