ATranslator.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.Events;
  3. using ChocolArm64.Instruction;
  4. using ChocolArm64.Memory;
  5. using ChocolArm64.Translation;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Reflection.Emit;
  10. using System.Threading;
  11. namespace ChocolArm64
  12. {
  13. public class ATranslator
  14. {
  15. private HashSet<long> SubBlocks;
  16. private ConcurrentDictionary<long, ATranslatedSub> CachedSubs;
  17. private ConcurrentDictionary<long, string> SymbolTable;
  18. public event EventHandler<ACpuTraceEventArgs> CpuTrace;
  19. public bool EnableCpuTrace { get; set; }
  20. private bool KeepRunning;
  21. public ATranslator(IReadOnlyDictionary<long, string> SymbolTable = null)
  22. {
  23. SubBlocks = new HashSet<long>();
  24. CachedSubs = new ConcurrentDictionary<long, ATranslatedSub>();
  25. if (SymbolTable != null)
  26. {
  27. this.SymbolTable = new ConcurrentDictionary<long, string>(SymbolTable);
  28. }
  29. else
  30. {
  31. this.SymbolTable = new ConcurrentDictionary<long, string>();
  32. }
  33. KeepRunning = true;
  34. }
  35. internal void StopExecution() => KeepRunning = false;
  36. internal void ExecuteSubroutine(AThread Thread, long Position)
  37. {
  38. do
  39. {
  40. if (EnableCpuTrace)
  41. {
  42. if (!SymbolTable.TryGetValue(Position, out string SubName))
  43. {
  44. SubName = string.Empty;
  45. }
  46. CpuTrace?.Invoke(this, new ACpuTraceEventArgs(Position, SubName));
  47. }
  48. if (!CachedSubs.TryGetValue(Position, out ATranslatedSub Sub))
  49. {
  50. Sub = TranslateTier0(Thread.Memory, Position);
  51. }
  52. if (Sub.ShouldReJit())
  53. {
  54. TranslateTier1(Thread.Memory, Position);
  55. }
  56. Position = Sub.Execute(Thread.ThreadState, Thread.Memory);
  57. }
  58. while (Position != 0 && KeepRunning);
  59. }
  60. internal bool TryGetCachedSub(AOpCode OpCode, out ATranslatedSub Sub)
  61. {
  62. if (OpCode.Emitter != AInstEmit.Bl)
  63. {
  64. Sub = null;
  65. return false;
  66. }
  67. return TryGetCachedSub(((AOpCodeBImmAl)OpCode).Imm, out Sub);
  68. }
  69. internal bool TryGetCachedSub(long Position, out ATranslatedSub Sub)
  70. {
  71. return CachedSubs.TryGetValue(Position, out Sub);
  72. }
  73. internal bool HasCachedSub(long Position)
  74. {
  75. return CachedSubs.ContainsKey(Position);
  76. }
  77. private ATranslatedSub TranslateTier0(AMemory Memory, long Position)
  78. {
  79. ABlock Block = ADecoder.DecodeBasicBlock(this, Memory, Position);
  80. ABlock[] Graph = new ABlock[] { Block };
  81. string SubName = GetSubName(Position);
  82. AILEmitterCtx Context = new AILEmitterCtx(this, Graph, Block, SubName);
  83. do
  84. {
  85. Context.EmitOpCode();
  86. }
  87. while (Context.AdvanceOpCode());
  88. ATranslatedSub Subroutine = Context.GetSubroutine();
  89. if (SubBlocks.Contains(Position))
  90. {
  91. SubBlocks.Remove(Position);
  92. Subroutine.SetType(ATranslatedSubType.SubBlock);
  93. }
  94. else
  95. {
  96. Subroutine.SetType(ATranslatedSubType.SubTier0);
  97. }
  98. CachedSubs.AddOrUpdate(Position, Subroutine, (Key, OldVal) => Subroutine);
  99. AOpCode LastOp = Block.GetLastOp();
  100. if (LastOp.Emitter != AInstEmit.Ret &&
  101. LastOp.Emitter != AInstEmit.Br)
  102. {
  103. SubBlocks.Add(LastOp.Position + 4);
  104. }
  105. return Subroutine;
  106. }
  107. private void TranslateTier1(AMemory Memory, long Position)
  108. {
  109. (ABlock[] Graph, ABlock Root) Cfg = ADecoder.DecodeSubroutine(this, Memory, Position);
  110. string SubName = GetSubName(Position);
  111. PropagateName(Cfg.Graph, SubName);
  112. AILEmitterCtx Context = new AILEmitterCtx(this, Cfg.Graph, Cfg.Root, SubName);
  113. if (Context.CurrBlock.Position != Position)
  114. {
  115. Context.Emit(OpCodes.Br, Context.GetLabel(Position));
  116. }
  117. do
  118. {
  119. Context.EmitOpCode();
  120. }
  121. while (Context.AdvanceOpCode());
  122. //Mark all methods that calls this method for ReJiting,
  123. //since we can now call it directly which is faster.
  124. foreach (ATranslatedSub TS in CachedSubs.Values)
  125. {
  126. if (TS.HasCallee(Position))
  127. {
  128. TS.MarkForReJit();
  129. }
  130. }
  131. ATranslatedSub Subroutine = Context.GetSubroutine();
  132. Subroutine.SetType(ATranslatedSubType.SubTier1);
  133. CachedSubs.AddOrUpdate(Position, Subroutine, (Key, OldVal) => Subroutine);
  134. }
  135. private string GetSubName(long Position)
  136. {
  137. return SymbolTable.GetOrAdd(Position, $"Sub{Position:x16}");
  138. }
  139. private void PropagateName(ABlock[] Graph, string Name)
  140. {
  141. foreach (ABlock Block in Graph)
  142. {
  143. AOpCode LastOp = Block.GetLastOp();
  144. if (LastOp != null &&
  145. (LastOp.Emitter == AInstEmit.Bl ||
  146. LastOp.Emitter == AInstEmit.Blr))
  147. {
  148. SymbolTable.TryAdd(LastOp.Position + 4, Name);
  149. }
  150. }
  151. }
  152. }
  153. }