ATranslator.cs 5.9 KB

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