ATranslatedSub.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using ChocolArm64.Memory;
  2. using ChocolArm64.State;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using System.Reflection.Emit;
  7. namespace ChocolArm64
  8. {
  9. class ATranslatedSub
  10. {
  11. private delegate long AA64Subroutine(AThreadState Register, AMemory Memory);
  12. private AA64Subroutine ExecDelegate;
  13. private bool HasDelegate;
  14. public static Type[] FixedArgTypes { get; private set; }
  15. public static int StateArgIdx { get; private set; }
  16. public static int MemoryArgIdx { get; private set; }
  17. public DynamicMethod Method { get; private set; }
  18. public HashSet<long> SubCalls { get; private set; }
  19. public List<ARegister> Params { get; private set; }
  20. public bool NeedsReJit { get; private set; }
  21. public ATranslatedSub()
  22. {
  23. SubCalls = new HashSet<long>();
  24. }
  25. public ATranslatedSub(DynamicMethod Method, List<ARegister> Params) : this()
  26. {
  27. if (Params == null)
  28. {
  29. throw new ArgumentNullException(nameof(Params));
  30. }
  31. this.Method = Method;
  32. this.Params = Params;
  33. }
  34. static ATranslatedSub()
  35. {
  36. MethodInfo MthdInfo = typeof(AA64Subroutine).GetMethod("Invoke");
  37. ParameterInfo[] Params = MthdInfo.GetParameters();
  38. FixedArgTypes = new Type[Params.Length];
  39. for (int Index = 0; Index < Params.Length; Index++)
  40. {
  41. Type ParamType = Params[Index].ParameterType;
  42. FixedArgTypes[Index] = ParamType;
  43. if (ParamType == typeof(AThreadState))
  44. {
  45. StateArgIdx = Index;
  46. }
  47. else if (ParamType == typeof(AMemory))
  48. {
  49. MemoryArgIdx = Index;
  50. }
  51. }
  52. }
  53. public long Execute(AThreadState ThreadState, AMemory Memory)
  54. {
  55. if (!HasDelegate)
  56. {
  57. string Name = $"{Method.Name}_Dispatch";
  58. DynamicMethod Mthd = new DynamicMethod(Name, typeof(long), FixedArgTypes);
  59. ILGenerator Generator = Mthd.GetILGenerator();
  60. Generator.EmitLdargSeq(FixedArgTypes.Length);
  61. foreach (ARegister Reg in Params)
  62. {
  63. Generator.EmitLdarg(StateArgIdx);
  64. Generator.Emit(OpCodes.Ldfld, Reg.GetField());
  65. }
  66. Generator.Emit(OpCodes.Call, Method);
  67. Generator.Emit(OpCodes.Ret);
  68. ExecDelegate = (AA64Subroutine)Mthd.CreateDelegate(typeof(AA64Subroutine));
  69. HasDelegate = true;
  70. }
  71. return ExecDelegate(ThreadState, Memory);
  72. }
  73. public void MarkForReJit() => NeedsReJit = true;
  74. }
  75. }