ATranslator.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.Instruction;
  3. using ChocolArm64.Translation;
  4. using System.Collections.Generic;
  5. using System.Reflection.Emit;
  6. namespace ChocolArm64
  7. {
  8. class ATranslator
  9. {
  10. public AThread Thread { get; private set; }
  11. private Dictionary<long, ATranslatedSub> CachedSubs;
  12. private bool KeepRunning;
  13. public ATranslator(AThread Parent)
  14. {
  15. this.Thread = Parent;
  16. CachedSubs = new Dictionary<long, ATranslatedSub>();
  17. KeepRunning = true;
  18. }
  19. public void StopExecution() => KeepRunning = false;
  20. public void ExecuteSubroutine(long Position)
  21. {
  22. do
  23. {
  24. if (CachedSubs.TryGetValue(Position, out ATranslatedSub Sub) && !Sub.NeedsReJit)
  25. {
  26. Position = Sub.Execute(Thread.ThreadState, Thread.Memory);
  27. }
  28. else
  29. {
  30. Position = TranslateSubroutine(Position).Execute(Thread.ThreadState, Thread.Memory);
  31. }
  32. }
  33. while (Position != 0 && KeepRunning);
  34. }
  35. public bool TryGetCachedSub(AOpCode OpCode, out ATranslatedSub Sub)
  36. {
  37. if (OpCode.Emitter != AInstEmit.Bl)
  38. {
  39. Sub = null;
  40. return false;
  41. }
  42. return TryGetCachedSub(((AOpCodeBImmAl)OpCode).Imm, out Sub);
  43. }
  44. public bool TryGetCachedSub(long Position, out ATranslatedSub Sub)
  45. {
  46. return CachedSubs.TryGetValue(Position, out Sub);
  47. }
  48. public bool HasCachedSub(long Position)
  49. {
  50. return CachedSubs.ContainsKey(Position);
  51. }
  52. private ATranslatedSub TranslateSubroutine(long Position)
  53. {
  54. (ABlock[] Graph, ABlock Root) Cfg = ADecoder.DecodeSubroutine(this, Position);
  55. AILEmitterCtx Context = new AILEmitterCtx(
  56. this,
  57. Cfg.Graph,
  58. Cfg.Root);
  59. if (Context.CurrBlock.Position != Position)
  60. {
  61. Context.Emit(OpCodes.Br, Context.GetLabel(Position));
  62. }
  63. do
  64. {
  65. Context.EmitOpCode();
  66. }
  67. while (Context.AdvanceOpCode());
  68. //Mark all methods that calls this method for ReJiting,
  69. //since we can now call it directly which is faster.
  70. foreach (ATranslatedSub TS in CachedSubs.Values)
  71. {
  72. if (TS.SubCalls.Contains(Position))
  73. {
  74. TS.MarkForReJit();
  75. }
  76. }
  77. ATranslatedSub Subroutine = Context.GetSubroutine();
  78. if (!CachedSubs.TryAdd(Position, Subroutine))
  79. {
  80. CachedSubs[Position] = Subroutine;
  81. }
  82. return Subroutine;
  83. }
  84. }
  85. }