2018-02-05 00:08:20 +01:00
|
|
|
using ChocolArm64.Decoder;
|
2018-02-26 02:14:58 +01:00
|
|
|
using ChocolArm64.Events;
|
2018-02-05 00:08:20 +01:00
|
|
|
using ChocolArm64.Instruction;
|
2018-02-26 02:14:58 +01:00
|
|
|
using ChocolArm64.Memory;
|
2018-02-05 00:08:20 +01:00
|
|
|
using ChocolArm64.Translation;
|
2018-02-26 02:14:58 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
2018-02-05 00:08:20 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
|
|
|
|
namespace ChocolArm64
|
|
|
|
{
|
2018-02-26 02:14:58 +01:00
|
|
|
public class ATranslator
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-04 18:09:59 +01:00
|
|
|
private HashSet<long> SubBlocks;
|
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
private ConcurrentDictionary<long, ATranslatedSub> CachedSubs;
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
private ConcurrentDictionary<long, string> SymbolTable;
|
|
|
|
|
|
|
|
public event EventHandler<ACpuTraceEventArgs> CpuTrace;
|
|
|
|
|
|
|
|
public bool EnableCpuTrace { get; set; }
|
2018-02-18 05:57:33 +01:00
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
public ATranslator(IReadOnlyDictionary<long, string> SymbolTable = null)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-04 18:09:59 +01:00
|
|
|
SubBlocks = new HashSet<long>();
|
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
CachedSubs = new ConcurrentDictionary<long, ATranslatedSub>();
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
if (SymbolTable != null)
|
|
|
|
{
|
|
|
|
this.SymbolTable = new ConcurrentDictionary<long, string>(SymbolTable);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.SymbolTable = new ConcurrentDictionary<long, string>();
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 18:09:59 +01:00
|
|
|
internal void ExecuteSubroutine(AThread Thread, long Position)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2018-02-26 02:14:58 +01:00
|
|
|
if (EnableCpuTrace)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-26 02:14:58 +01:00
|
|
|
if (!SymbolTable.TryGetValue(Position, out string SubName))
|
|
|
|
{
|
|
|
|
SubName = string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
CpuTrace?.Invoke(this, new ACpuTraceEventArgs(Position, SubName));
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-02-26 02:14:58 +01:00
|
|
|
|
2018-03-04 18:09:59 +01:00
|
|
|
if (!CachedSubs.TryGetValue(Position, out ATranslatedSub Sub))
|
|
|
|
{
|
|
|
|
Sub = TranslateTier0(Thread.Memory, Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Sub.ShouldReJit())
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-03-04 18:09:59 +01:00
|
|
|
TranslateTier1(Thread.Memory, Position);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-02-26 02:14:58 +01:00
|
|
|
|
|
|
|
Position = Sub.Execute(Thread.ThreadState, Thread.Memory);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-03-12 05:04:52 +01:00
|
|
|
while (Position != 0 && Thread.ThreadState.Running);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
internal bool TryGetCachedSub(AOpCode OpCode, out ATranslatedSub Sub)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
|
|
|
if (OpCode.Emitter != AInstEmit.Bl)
|
|
|
|
{
|
|
|
|
Sub = null;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TryGetCachedSub(((AOpCodeBImmAl)OpCode).Imm, out Sub);
|
|
|
|
}
|
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
internal bool TryGetCachedSub(long Position, out ATranslatedSub Sub)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
|
|
|
return CachedSubs.TryGetValue(Position, out Sub);
|
|
|
|
}
|
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
internal bool HasCachedSub(long Position)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
|
|
|
return CachedSubs.ContainsKey(Position);
|
|
|
|
}
|
|
|
|
|
2018-03-04 18:09:59 +01:00
|
|
|
private ATranslatedSub TranslateTier0(AMemory Memory, long Position)
|
|
|
|
{
|
|
|
|
ABlock Block = ADecoder.DecodeBasicBlock(this, Memory, Position);
|
|
|
|
|
|
|
|
ABlock[] Graph = new ABlock[] { Block };
|
|
|
|
|
|
|
|
string SubName = GetSubName(Position);
|
|
|
|
|
|
|
|
AILEmitterCtx Context = new AILEmitterCtx(this, Graph, Block, SubName);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Context.EmitOpCode();
|
|
|
|
}
|
|
|
|
while (Context.AdvanceOpCode());
|
|
|
|
|
|
|
|
ATranslatedSub Subroutine = Context.GetSubroutine();
|
|
|
|
|
2018-04-04 23:17:37 +02:00
|
|
|
lock (SubBlocks)
|
2018-03-04 18:09:59 +01:00
|
|
|
{
|
2018-04-04 23:17:37 +02:00
|
|
|
if (SubBlocks.Contains(Position))
|
|
|
|
{
|
|
|
|
SubBlocks.Remove(Position);
|
2018-03-04 18:09:59 +01:00
|
|
|
|
2018-04-04 23:17:37 +02:00
|
|
|
Subroutine.SetType(ATranslatedSubType.SubBlock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Subroutine.SetType(ATranslatedSubType.SubTier0);
|
|
|
|
}
|
2018-03-04 18:09:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CachedSubs.AddOrUpdate(Position, Subroutine, (Key, OldVal) => Subroutine);
|
|
|
|
|
|
|
|
AOpCode LastOp = Block.GetLastOp();
|
|
|
|
|
2018-04-04 23:17:37 +02:00
|
|
|
lock (SubBlocks)
|
2018-03-04 18:09:59 +01:00
|
|
|
{
|
2018-04-04 23:17:37 +02:00
|
|
|
if (LastOp.Emitter != AInstEmit.Ret &&
|
|
|
|
LastOp.Emitter != AInstEmit.Br)
|
|
|
|
{
|
|
|
|
SubBlocks.Add(LastOp.Position + 4);
|
|
|
|
}
|
2018-03-04 18:09:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Subroutine;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void TranslateTier1(AMemory Memory, long Position)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-02-26 02:14:58 +01:00
|
|
|
(ABlock[] Graph, ABlock Root) Cfg = ADecoder.DecodeSubroutine(this, Memory, Position);
|
|
|
|
|
2018-03-04 18:09:59 +01:00
|
|
|
string SubName = GetSubName(Position);
|
2018-02-26 02:14:58 +01:00
|
|
|
|
|
|
|
PropagateName(Cfg.Graph, SubName);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-04 18:09:59 +01:00
|
|
|
AILEmitterCtx Context = new AILEmitterCtx(this, Cfg.Graph, Cfg.Root, SubName);
|
2018-02-05 00:08:20 +01:00
|
|
|
|
|
|
|
if (Context.CurrBlock.Position != Position)
|
|
|
|
{
|
|
|
|
Context.Emit(OpCodes.Br, Context.GetLabel(Position));
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Context.EmitOpCode();
|
|
|
|
}
|
|
|
|
while (Context.AdvanceOpCode());
|
|
|
|
|
|
|
|
//Mark all methods that calls this method for ReJiting,
|
|
|
|
//since we can now call it directly which is faster.
|
2018-04-11 19:44:03 +02:00
|
|
|
if (CachedSubs.TryGetValue(Position, out ATranslatedSub OldSub))
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-04-11 19:44:03 +02:00
|
|
|
foreach (long CallerPos in OldSub.GetCallerPositions())
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-04-11 19:44:03 +02:00
|
|
|
if (CachedSubs.TryGetValue(Position, out ATranslatedSub CallerSub))
|
|
|
|
{
|
|
|
|
CallerSub.MarkForReJit();
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ATranslatedSub Subroutine = Context.GetSubroutine();
|
|
|
|
|
2018-03-04 18:09:59 +01:00
|
|
|
Subroutine.SetType(ATranslatedSubType.SubTier1);
|
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
CachedSubs.AddOrUpdate(Position, Subroutine, (Key, OldVal) => Subroutine);
|
2018-03-04 18:09:59 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-03-04 18:09:59 +01:00
|
|
|
private string GetSubName(long Position)
|
|
|
|
{
|
|
|
|
return SymbolTable.GetOrAdd(Position, $"Sub{Position:x16}");
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-02-26 02:14:58 +01:00
|
|
|
|
|
|
|
private void PropagateName(ABlock[] Graph, string Name)
|
|
|
|
{
|
|
|
|
foreach (ABlock Block in Graph)
|
|
|
|
{
|
|
|
|
AOpCode LastOp = Block.GetLastOp();
|
|
|
|
|
|
|
|
if (LastOp != null &&
|
|
|
|
(LastOp.Emitter == AInstEmit.Bl ||
|
|
|
|
LastOp.Emitter == AInstEmit.Blr))
|
|
|
|
{
|
|
|
|
SymbolTable.TryAdd(LastOp.Position + 4, Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|