ATranslatedSub.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using ChocolArm64.Memory;
  2. using ChocolArm64.State;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Reflection;
  7. using System.Reflection.Emit;
  8. namespace ChocolArm64
  9. {
  10. class ATranslatedSub
  11. {
  12. private delegate long AA64Subroutine(AThreadState Register, AMemory Memory);
  13. private AA64Subroutine ExecDelegate;
  14. public static int StateArgIdx { get; private set; }
  15. public static int MemoryArgIdx { get; private set; }
  16. public static Type[] FixedArgTypes { get; private set; }
  17. public DynamicMethod Method { get; private set; }
  18. public ReadOnlyCollection<ARegister> Params { get; private set; }
  19. private HashSet<long> Callees;
  20. private ATranslatedSubType Type;
  21. private int CallCount;
  22. private bool NeedsReJit;
  23. private int MinCallCountForReJit = 250;
  24. public ATranslatedSub(DynamicMethod Method, List<ARegister> Params, HashSet<long> Callees)
  25. {
  26. if (Method == null)
  27. {
  28. throw new ArgumentNullException(nameof(Method));
  29. }
  30. if (Params == null)
  31. {
  32. throw new ArgumentNullException(nameof(Params));
  33. }
  34. if (Callees == null)
  35. {
  36. throw new ArgumentNullException(nameof(Callees));
  37. }
  38. this.Method = Method;
  39. this.Params = Params.AsReadOnly();
  40. this.Callees = Callees;
  41. PrepareDelegate();
  42. }
  43. static ATranslatedSub()
  44. {
  45. MethodInfo MthdInfo = typeof(AA64Subroutine).GetMethod("Invoke");
  46. ParameterInfo[] Params = MthdInfo.GetParameters();
  47. FixedArgTypes = new Type[Params.Length];
  48. for (int Index = 0; Index < Params.Length; Index++)
  49. {
  50. Type ParamType = Params[Index].ParameterType;
  51. FixedArgTypes[Index] = ParamType;
  52. if (ParamType == typeof(AThreadState))
  53. {
  54. StateArgIdx = Index;
  55. }
  56. else if (ParamType == typeof(AMemory))
  57. {
  58. MemoryArgIdx = Index;
  59. }
  60. }
  61. }
  62. private void PrepareDelegate()
  63. {
  64. string Name = $"{Method.Name}_Dispatch";
  65. DynamicMethod Mthd = new DynamicMethod(Name, typeof(long), FixedArgTypes);
  66. ILGenerator Generator = Mthd.GetILGenerator();
  67. Generator.EmitLdargSeq(FixedArgTypes.Length);
  68. foreach (ARegister Reg in Params)
  69. {
  70. Generator.EmitLdarg(StateArgIdx);
  71. Generator.Emit(OpCodes.Ldfld, Reg.GetField());
  72. }
  73. Generator.Emit(OpCodes.Call, Method);
  74. Generator.Emit(OpCodes.Ret);
  75. ExecDelegate = (AA64Subroutine)Mthd.CreateDelegate(typeof(AA64Subroutine));
  76. }
  77. public bool ShouldReJit()
  78. {
  79. if (Type == ATranslatedSubType.SubTier0)
  80. {
  81. if (CallCount < MinCallCountForReJit)
  82. {
  83. CallCount++;
  84. }
  85. return CallCount == MinCallCountForReJit;
  86. }
  87. return Type == ATranslatedSubType.SubTier1 && NeedsReJit;
  88. }
  89. public long Execute(AThreadState ThreadState, AMemory Memory)
  90. {
  91. return ExecDelegate(ThreadState, Memory);
  92. }
  93. public void SetType(ATranslatedSubType Type) => this.Type = Type;
  94. public bool HasCallee(long Position) => Callees.Contains(Position);
  95. public void MarkForReJit() => NeedsReJit = true;
  96. }
  97. }