ATranslator.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. if (SubBlocks.Contains(Position))
  86. {
  87. SubBlocks.Remove(Position);
  88. Subroutine.SetType(ATranslatedSubType.SubBlock);
  89. }
  90. else
  91. {
  92. Subroutine.SetType(ATranslatedSubType.SubTier0);
  93. }
  94. CachedSubs.AddOrUpdate(Position, Subroutine, (Key, OldVal) => Subroutine);
  95. AOpCode LastOp = Block.GetLastOp();
  96. if (LastOp.Emitter != AInstEmit.Ret &&
  97. LastOp.Emitter != AInstEmit.Br)
  98. {
  99. SubBlocks.Add(LastOp.Position + 4);
  100. }
  101. return Subroutine;
  102. }
  103. private void TranslateTier1(AMemory Memory, long Position)
  104. {
  105. (ABlock[] Graph, ABlock Root) Cfg = ADecoder.DecodeSubroutine(this, Memory, Position);
  106. string SubName = GetSubName(Position);
  107. PropagateName(Cfg.Graph, SubName);
  108. AILEmitterCtx Context = new AILEmitterCtx(this, Cfg.Graph, Cfg.Root, SubName);
  109. if (Context.CurrBlock.Position != Position)
  110. {
  111. Context.Emit(OpCodes.Br, Context.GetLabel(Position));
  112. }
  113. do
  114. {
  115. Context.EmitOpCode();
  116. }
  117. while (Context.AdvanceOpCode());
  118. //Mark all methods that calls this method for ReJiting,
  119. //since we can now call it directly which is faster.
  120. foreach (ATranslatedSub TS in CachedSubs.Values)
  121. {
  122. if (TS.HasCallee(Position))
  123. {
  124. TS.MarkForReJit();
  125. }
  126. }
  127. ATranslatedSub Subroutine = Context.GetSubroutine();
  128. Subroutine.SetType(ATranslatedSubType.SubTier1);
  129. CachedSubs.AddOrUpdate(Position, Subroutine, (Key, OldVal) => Subroutine);
  130. }
  131. private string GetSubName(long Position)
  132. {
  133. return SymbolTable.GetOrAdd(Position, $"Sub{Position:x16}");
  134. }
  135. private void PropagateName(ABlock[] Graph, string Name)
  136. {
  137. foreach (ABlock Block in Graph)
  138. {
  139. AOpCode LastOp = Block.GetLastOp();
  140. if (LastOp != null &&
  141. (LastOp.Emitter == AInstEmit.Bl ||
  142. LastOp.Emitter == AInstEmit.Blr))
  143. {
  144. SymbolTable.TryAdd(LastOp.Position + 4, Name);
  145. }
  146. }
  147. }
  148. }
  149. }