TranslatedSub.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using ChocolArm64.Memory;
  2. using ChocolArm64.State;
  3. using System;
  4. using System.Reflection;
  5. using System.Reflection.Emit;
  6. namespace ChocolArm64.Translation
  7. {
  8. delegate long ArmSubroutine(CpuThreadState state, MemoryManager memory);
  9. class TranslatedSub
  10. {
  11. public ArmSubroutine Delegate { get; private set; }
  12. public static int StateArgIdx { get; private set; }
  13. public static int MemoryArgIdx { get; private set; }
  14. public static Type[] FixedArgTypes { get; private set; }
  15. public DynamicMethod Method { get; private set; }
  16. public TranslationTier Tier { get; private set; }
  17. public TranslatedSub(DynamicMethod method, TranslationTier tier)
  18. {
  19. Method = method ?? throw new ArgumentNullException(nameof(method));;
  20. Tier = tier;
  21. }
  22. static TranslatedSub()
  23. {
  24. MethodInfo mthdInfo = typeof(ArmSubroutine).GetMethod("Invoke");
  25. ParameterInfo[] Params = mthdInfo.GetParameters();
  26. FixedArgTypes = new Type[Params.Length];
  27. for (int index = 0; index < Params.Length; index++)
  28. {
  29. Type argType = Params[index].ParameterType;
  30. FixedArgTypes[index] = argType;
  31. if (argType == typeof(CpuThreadState))
  32. {
  33. StateArgIdx = index;
  34. }
  35. else if (argType == typeof(MemoryManager))
  36. {
  37. MemoryArgIdx = index;
  38. }
  39. }
  40. }
  41. public void PrepareMethod()
  42. {
  43. Delegate = (ArmSubroutine)Method.CreateDelegate(typeof(ArmSubroutine));
  44. }
  45. public long Execute(CpuThreadState threadState, MemoryManager memory)
  46. {
  47. return Delegate(threadState, memory);
  48. }
  49. }
  50. }