Translator.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.Diagnostics;
  3. using ARMeilleure.Instructions;
  4. using ARMeilleure.IntermediateRepresentation;
  5. using ARMeilleure.Memory;
  6. using ARMeilleure.State;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Threading;
  10. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  11. using static ARMeilleure.IntermediateRepresentation.OperationHelper;
  12. namespace ARMeilleure.Translation
  13. {
  14. public class Translator
  15. {
  16. private const ulong CallFlag = InstEmitFlowHelper.CallFlag;
  17. private const bool AlwaysTranslateFunctions = true; // If false, only translates a single block for lowCq.
  18. private readonly IMemoryManager _memory;
  19. private readonly ConcurrentDictionary<ulong, TranslatedFunction> _funcs;
  20. private readonly JumpTable _jumpTable;
  21. private readonly PriorityQueue<RejitRequest> _backgroundQueue;
  22. private readonly AutoResetEvent _backgroundTranslatorEvent;
  23. private volatile int _threadCount;
  24. public Translator(IJitMemoryAllocator allocator, IMemoryManager memory)
  25. {
  26. _memory = memory;
  27. _funcs = new ConcurrentDictionary<ulong, TranslatedFunction>();
  28. _jumpTable = new JumpTable(allocator);
  29. _backgroundQueue = new PriorityQueue<RejitRequest>(2);
  30. _backgroundTranslatorEvent = new AutoResetEvent(false);
  31. JitCache.Initialize(allocator);
  32. DirectCallStubs.InitializeStubs();
  33. }
  34. private void TranslateQueuedSubs()
  35. {
  36. while (_threadCount != 0)
  37. {
  38. if (_backgroundQueue.TryDequeue(out RejitRequest request))
  39. {
  40. TranslatedFunction func = Translate(request.Address, request.Mode, highCq: true);
  41. _funcs.AddOrUpdate(request.Address, func, (key, oldFunc) => func);
  42. _jumpTable.RegisterFunction(request.Address, func);
  43. }
  44. else
  45. {
  46. _backgroundTranslatorEvent.WaitOne();
  47. }
  48. }
  49. _backgroundTranslatorEvent.Set(); // Wake up any other background translator threads, to encourage them to exit.
  50. }
  51. public void Execute(State.ExecutionContext context, ulong address)
  52. {
  53. if (Interlocked.Increment(ref _threadCount) == 1)
  54. {
  55. // Simple heuristic, should be user configurable in future. (1 for 4 core/ht or less, 2 for 6 core+ht etc).
  56. // All threads are normal priority except from the last, which just fills as much of the last core as the os lets it with a low priority.
  57. // If we only have one rejit thread, it should be normal priority as highCq code is performance critical.
  58. // TODO: Use physical cores rather than logical. This only really makes sense for processors with hyperthreading. Requires OS specific code.
  59. int unboundedThreadCount = Math.Max(1, (Environment.ProcessorCount - 6) / 3);
  60. int threadCount = Math.Min(4, unboundedThreadCount);
  61. for (int i = 0; i < threadCount; i++)
  62. {
  63. bool last = i != 0 && i == unboundedThreadCount - 1;
  64. Thread backgroundTranslatorThread = new Thread(TranslateQueuedSubs)
  65. {
  66. Name = "CPU.BackgroundTranslatorThread." + i,
  67. Priority = last ? ThreadPriority.Lowest : ThreadPriority.Normal
  68. };
  69. backgroundTranslatorThread.Start();
  70. }
  71. }
  72. Statistics.InitializeTimer();
  73. NativeInterface.RegisterThread(context, _memory, this);
  74. do
  75. {
  76. address = ExecuteSingle(context, address);
  77. }
  78. while (context.Running && (address & ~1UL) != 0);
  79. NativeInterface.UnregisterThread();
  80. if (Interlocked.Decrement(ref _threadCount) == 0)
  81. {
  82. _backgroundTranslatorEvent.Set();
  83. }
  84. }
  85. public ulong ExecuteSingle(State.ExecutionContext context, ulong address)
  86. {
  87. TranslatedFunction func = GetOrTranslate(address, context.ExecutionMode);
  88. Statistics.StartTimer();
  89. ulong nextAddr = func.Execute(context);
  90. Statistics.StopTimer(address);
  91. return nextAddr;
  92. }
  93. internal TranslatedFunction GetOrTranslate(ulong address, ExecutionMode mode)
  94. {
  95. // TODO: Investigate how we should handle code at unaligned addresses.
  96. // Currently, those low bits are used to store special flags.
  97. bool isCallTarget = (address & CallFlag) != 0;
  98. address &= ~CallFlag;
  99. if (!_funcs.TryGetValue(address, out TranslatedFunction func))
  100. {
  101. func = Translate(address, mode, highCq: false);
  102. _funcs.TryAdd(address, func);
  103. }
  104. else if (isCallTarget && func.ShouldRejit())
  105. {
  106. _backgroundQueue.Enqueue(0, new RejitRequest(address, mode));
  107. _backgroundTranslatorEvent.Set();
  108. }
  109. return func;
  110. }
  111. private TranslatedFunction Translate(ulong address, ExecutionMode mode, bool highCq)
  112. {
  113. ArmEmitterContext context = new ArmEmitterContext(_memory, _jumpTable, (long)address, highCq, Aarch32Mode.User);
  114. PrepareOperandPool(highCq);
  115. PrepareOperationPool(highCq);
  116. Logger.StartPass(PassName.Decoding);
  117. Block[] blocks = AlwaysTranslateFunctions
  118. ? Decoder.DecodeFunction (_memory, address, mode, highCq)
  119. : Decoder.DecodeBasicBlock(_memory, address, mode);
  120. Logger.EndPass(PassName.Decoding);
  121. Logger.StartPass(PassName.Translation);
  122. EmitSynchronization(context);
  123. if (blocks[0].Address != address)
  124. {
  125. context.Branch(context.GetLabel(address));
  126. }
  127. ControlFlowGraph cfg = EmitAndGetCFG(context, blocks);
  128. Logger.EndPass(PassName.Translation);
  129. Logger.StartPass(PassName.RegisterUsage);
  130. RegisterUsage.RunPass(cfg, mode, isCompleteFunction: false);
  131. Logger.EndPass(PassName.RegisterUsage);
  132. OperandType[] argTypes = new OperandType[] { OperandType.I64 };
  133. CompilerOptions options = highCq ? CompilerOptions.HighCq : CompilerOptions.None;
  134. GuestFunction func = Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, options);
  135. ResetOperandPool(highCq);
  136. ResetOperationPool(highCq);
  137. return new TranslatedFunction(func, rejit: !highCq);
  138. }
  139. private static ControlFlowGraph EmitAndGetCFG(ArmEmitterContext context, Block[] blocks)
  140. {
  141. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  142. {
  143. Block block = blocks[blkIndex];
  144. context.CurrBlock = block;
  145. context.MarkLabel(context.GetLabel(block.Address));
  146. for (int opcIndex = 0; opcIndex < block.OpCodes.Count; opcIndex++)
  147. {
  148. OpCode opCode = block.OpCodes[opcIndex];
  149. context.CurrOp = opCode;
  150. bool isLastOp = opcIndex == block.OpCodes.Count - 1;
  151. if (isLastOp && block.Branch != null && block.Branch.Address <= block.Address)
  152. {
  153. EmitSynchronization(context);
  154. }
  155. Operand lblPredicateSkip = null;
  156. if (opCode is OpCode32 op && op.Cond < Condition.Al)
  157. {
  158. lblPredicateSkip = Label();
  159. InstEmitFlowHelper.EmitCondBranch(context, lblPredicateSkip, op.Cond.Invert());
  160. }
  161. if (opCode.Instruction.Emitter != null)
  162. {
  163. opCode.Instruction.Emitter(context);
  164. }
  165. else
  166. {
  167. throw new InvalidOperationException($"Invalid instruction \"{opCode.Instruction.Name}\".");
  168. }
  169. if (lblPredicateSkip != null)
  170. {
  171. context.MarkLabel(lblPredicateSkip);
  172. // If this is the last op on the block, and there's no "next" block
  173. // after this one, then we have to return right now, with the address
  174. // of the next instruction to be executed (in the case that the condition
  175. // is false, and the branch was not taken, as all basic blocks should end
  176. // with some kind of branch).
  177. if (isLastOp && block.Next == null)
  178. {
  179. InstEmitFlowHelper.EmitTailContinue(context, Const(opCode.Address + (ulong)opCode.OpCodeSizeInBytes));
  180. }
  181. }
  182. }
  183. }
  184. return context.GetControlFlowGraph();
  185. }
  186. private static void EmitSynchronization(EmitterContext context)
  187. {
  188. long countOffs = NativeContext.GetCounterOffset();
  189. Operand countAddr = context.Add(context.LoadArgument(OperandType.I64, 0), Const(countOffs));
  190. Operand count = context.Load(OperandType.I32, countAddr);
  191. Operand lblNonZero = Label();
  192. Operand lblExit = Label();
  193. context.BranchIfTrue(lblNonZero, count);
  194. Operand running = context.Call(new _Bool(NativeInterface.CheckSynchronization));
  195. context.BranchIfTrue(lblExit, running);
  196. context.Return(Const(0L));
  197. context.Branch(lblExit);
  198. context.MarkLabel(lblNonZero);
  199. count = context.Subtract(count, Const(1));
  200. context.Store(countAddr, count);
  201. context.MarkLabel(lblExit);
  202. }
  203. }
  204. }