ATranslator.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 ConcurrentDictionary<long, ATranslatedSub> CachedSubs;
  15. private ConcurrentDictionary<long, string> SymbolTable;
  16. public event EventHandler<ACpuTraceEventArgs> CpuTrace;
  17. public bool EnableCpuTrace { get; set; }
  18. private bool KeepRunning;
  19. public ATranslator(IReadOnlyDictionary<long, string> SymbolTable = null)
  20. {
  21. CachedSubs = new ConcurrentDictionary<long, ATranslatedSub>();
  22. if (SymbolTable != null)
  23. {
  24. this.SymbolTable = new ConcurrentDictionary<long, string>(SymbolTable);
  25. }
  26. else
  27. {
  28. this.SymbolTable = new ConcurrentDictionary<long, string>();
  29. }
  30. KeepRunning = true;
  31. }
  32. public void StopExecution() => KeepRunning = false;
  33. public void ExecuteSubroutine(AThread Thread, long Position)
  34. {
  35. do
  36. {
  37. if (EnableCpuTrace)
  38. {
  39. if (!SymbolTable.TryGetValue(Position, out string SubName))
  40. {
  41. SubName = string.Empty;
  42. }
  43. CpuTrace?.Invoke(this, new ACpuTraceEventArgs(Position, SubName));
  44. }
  45. if (!CachedSubs.TryGetValue(Position, out ATranslatedSub Sub) || Sub.NeedsReJit)
  46. {
  47. Sub = TranslateSubroutine(Thread.Memory, Position);
  48. }
  49. Position = Sub.Execute(Thread.ThreadState, Thread.Memory);
  50. }
  51. while (Position != 0 && KeepRunning);
  52. }
  53. internal bool TryGetCachedSub(AOpCode OpCode, out ATranslatedSub Sub)
  54. {
  55. if (OpCode.Emitter != AInstEmit.Bl)
  56. {
  57. Sub = null;
  58. return false;
  59. }
  60. return TryGetCachedSub(((AOpCodeBImmAl)OpCode).Imm, out Sub);
  61. }
  62. internal bool TryGetCachedSub(long Position, out ATranslatedSub Sub)
  63. {
  64. return CachedSubs.TryGetValue(Position, out Sub);
  65. }
  66. internal bool HasCachedSub(long Position)
  67. {
  68. return CachedSubs.ContainsKey(Position);
  69. }
  70. private ATranslatedSub TranslateSubroutine(AMemory Memory, long Position)
  71. {
  72. (ABlock[] Graph, ABlock Root) Cfg = ADecoder.DecodeSubroutine(this, Memory, Position);
  73. string SubName = SymbolTable.GetOrAdd(Position, $"Sub{Position:x16}");
  74. PropagateName(Cfg.Graph, SubName);
  75. AILEmitterCtx Context = new AILEmitterCtx(
  76. this,
  77. Cfg.Graph,
  78. Cfg.Root,
  79. SubName);
  80. if (Context.CurrBlock.Position != Position)
  81. {
  82. Context.Emit(OpCodes.Br, Context.GetLabel(Position));
  83. }
  84. do
  85. {
  86. Context.EmitOpCode();
  87. }
  88. while (Context.AdvanceOpCode());
  89. //Mark all methods that calls this method for ReJiting,
  90. //since we can now call it directly which is faster.
  91. foreach (ATranslatedSub TS in CachedSubs.Values)
  92. {
  93. if (TS.SubCalls.Contains(Position))
  94. {
  95. TS.MarkForReJit();
  96. }
  97. }
  98. ATranslatedSub Subroutine = Context.GetSubroutine();
  99. CachedSubs.AddOrUpdate(Position, Subroutine, (Key, OldVal) => Subroutine);
  100. return Subroutine;
  101. }
  102. private void PropagateName(ABlock[] Graph, string Name)
  103. {
  104. foreach (ABlock Block in Graph)
  105. {
  106. AOpCode LastOp = Block.GetLastOp();
  107. if (LastOp != null &&
  108. (LastOp.Emitter == AInstEmit.Bl ||
  109. LastOp.Emitter == AInstEmit.Blr))
  110. {
  111. SymbolTable.TryAdd(LastOp.Position + 4, Name);
  112. }
  113. }
  114. }
  115. }
  116. }