Added initial support for function names from symbol table on the cpu with tracing, fix wrong ImageEnd on executables with MOD0, fix issue on the CPU on input elimination for instruction with more than one register store

This commit is contained in:
gdkchan 2018-02-25 22:14:58 -03:00
parent d2f3bd3526
commit 068754fec5
15 changed files with 151 additions and 75 deletions

View File

@ -28,14 +28,14 @@ namespace ChocolArm64
private object ExecuteLock;
public AThread(AMemory Memory, ThreadPriority Priority, long EntryPoint)
public AThread(ATranslator Translator, AMemory Memory, ThreadPriority Priority, long EntryPoint)
{
this.Translator = Translator;
this.Memory = Memory;
this.Priority = Priority;
this.EntryPoint = EntryPoint;
ThreadState = new AThreadState();
Translator = new ATranslator(this);
ExecuteLock = new object();
}
@ -55,7 +55,7 @@ namespace ChocolArm64
Work = new Thread(delegate()
{
Translator.ExecuteSubroutine(EntryPoint);
Translator.ExecuteSubroutine(this, EntryPoint);
Memory.RemoveMonitor(ThreadId);

View File

@ -1,47 +1,70 @@
using ChocolArm64.Decoder;
using ChocolArm64.Events;
using ChocolArm64.Instruction;
using ChocolArm64.Memory;
using ChocolArm64.Translation;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection.Emit;
namespace ChocolArm64
{
class ATranslator
public class ATranslator
{
public AThread Thread { get; private set; }
private ConcurrentDictionary<long, ATranslatedSub> CachedSubs;
private Dictionary<long, ATranslatedSub> CachedSubs;
private ConcurrentDictionary<long, string> SymbolTable;
public event EventHandler<ACpuTraceEventArgs> CpuTrace;
public bool EnableCpuTrace { get; set; }
private bool KeepRunning;
public ATranslator(AThread Parent)
public ATranslator(IReadOnlyDictionary<long, string> SymbolTable = null)
{
this.Thread = Parent;
CachedSubs = new ConcurrentDictionary<long, ATranslatedSub>();
CachedSubs = new Dictionary<long, ATranslatedSub>();
if (SymbolTable != null)
{
this.SymbolTable = new ConcurrentDictionary<long, string>(SymbolTable);
}
else
{
this.SymbolTable = new ConcurrentDictionary<long, string>();
}
KeepRunning = true;
}
public void StopExecution() => KeepRunning = false;
public void ExecuteSubroutine(long Position)
public void ExecuteSubroutine(AThread Thread, long Position)
{
do
{
if (CachedSubs.TryGetValue(Position, out ATranslatedSub Sub) && !Sub.NeedsReJit)
if (EnableCpuTrace)
{
Position = Sub.Execute(Thread.ThreadState, Thread.Memory);
if (!SymbolTable.TryGetValue(Position, out string SubName))
{
SubName = string.Empty;
}
CpuTrace?.Invoke(this, new ACpuTraceEventArgs(Position, SubName));
}
else
if (!CachedSubs.TryGetValue(Position, out ATranslatedSub Sub) || Sub.NeedsReJit)
{
Position = TranslateSubroutine(Position).Execute(Thread.ThreadState, Thread.Memory);
Sub = TranslateSubroutine(Thread.Memory, Position);
}
Position = Sub.Execute(Thread.ThreadState, Thread.Memory);
}
while (Position != 0 && KeepRunning);
}
public bool TryGetCachedSub(AOpCode OpCode, out ATranslatedSub Sub)
internal bool TryGetCachedSub(AOpCode OpCode, out ATranslatedSub Sub)
{
if (OpCode.Emitter != AInstEmit.Bl)
{
@ -53,24 +76,29 @@ namespace ChocolArm64
return TryGetCachedSub(((AOpCodeBImmAl)OpCode).Imm, out Sub);
}
public bool TryGetCachedSub(long Position, out ATranslatedSub Sub)
internal bool TryGetCachedSub(long Position, out ATranslatedSub Sub)
{
return CachedSubs.TryGetValue(Position, out Sub);
}
public bool HasCachedSub(long Position)
internal bool HasCachedSub(long Position)
{
return CachedSubs.ContainsKey(Position);
}
private ATranslatedSub TranslateSubroutine(long Position)
private ATranslatedSub TranslateSubroutine(AMemory Memory, long Position)
{
(ABlock[] Graph, ABlock Root) Cfg = ADecoder.DecodeSubroutine(this, Position);
(ABlock[] Graph, ABlock Root) Cfg = ADecoder.DecodeSubroutine(this, Memory, Position);
string SubName = SymbolTable.GetOrAdd(Position, $"Sub{Position:x16}");
PropagateName(Cfg.Graph, SubName);
AILEmitterCtx Context = new AILEmitterCtx(
this,
Cfg.Graph,
Cfg.Root);
Cfg.Root,
SubName);
if (Context.CurrBlock.Position != Position)
{
@ -95,12 +123,24 @@ namespace ChocolArm64
ATranslatedSub Subroutine = Context.GetSubroutine();
if (!CachedSubs.TryAdd(Position, Subroutine))
{
CachedSubs[Position] = Subroutine;
}
CachedSubs.AddOrUpdate(Position, Subroutine, (Key, OldVal) => Subroutine);
return Subroutine;
}
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);
}
}
}
}
}

View File

@ -18,7 +18,10 @@ namespace ChocolArm64.Decoder
OpActivators = new ConcurrentDictionary<Type, OpActivator>();
}
public static (ABlock[] Graph, ABlock Root) DecodeSubroutine(ATranslator Translator, long Start)
public static (ABlock[] Graph, ABlock Root) DecodeSubroutine(
ATranslator Translator,
AMemory Memory,
long Start)
{
Dictionary<long, ABlock> Visited = new Dictionary<long, ABlock>();
Dictionary<long, ABlock> VisitedEnd = new Dictionary<long, ABlock>();
@ -45,7 +48,7 @@ namespace ChocolArm64.Decoder
{
ABlock Current = Blocks.Dequeue();
FillBlock(Translator.Thread.Memory, Current);
FillBlock(Memory, Current);
//Set child blocks. "Branch" is the block the branch instruction
//points to (when taken), "Next" is the block at the next address,

View File

@ -0,0 +1,17 @@
using System;
namespace ChocolArm64.Events
{
public class ACpuTraceEventArgs : EventArgs
{
public long Position { get; private set; }
public string SubName { get; private set; }
public ACpuTraceEventArgs(long Position, string SubName)
{
this.Position = Position;
this.SubName = SubName;
}
}
}

View File

@ -0,0 +1,14 @@
using System;
namespace ChocolArm64.Events
{
public class AInstExceptionEventArgs : EventArgs
{
public int Id { get; private set; }
public AInstExceptionEventArgs(int Id)
{
this.Id = Id;
}
}
}

View File

@ -1,13 +1,13 @@
using System;
namespace ChocolArm64.State
namespace ChocolArm64.Events
{
public class AInstUndEventArgs : EventArgs
public class AInstUndefinedEventArgs : EventArgs
{
public long Position { get; private set; }
public int RawOpCode { get; private set; }
public AInstUndEventArgs(long Position, int RawOpCode)
public AInstUndefinedEventArgs(long Position, int RawOpCode)
{
this.Position = Position;
this.RawOpCode = RawOpCode;

View File

@ -44,16 +44,15 @@ namespace ChocolArm64.Instruction
Context.Emit(OpCodes.Neg);
}
Context.EmitStintzr(Op.Rd);
Context.Emit(OpCodes.Br_S, LblEnd);
Context.MarkLabel(LblTrue);
Context.EmitLdintzr(Op.Rn);
Context.EmitStintzr(Op.Rd);
Context.MarkLabel(LblEnd);
Context.EmitStintzr(Op.Rd);
}
}
}

View File

@ -1,14 +0,0 @@
using System;
namespace ChocolArm64.State
{
public class AInstExceptEventArgs : EventArgs
{
public int Id { get; private set; }
public AInstExceptEventArgs(int Id)
{
this.Id = Id;
}
}
}

View File

@ -1,3 +1,4 @@
using ChocolArm64.Events;
using System;
namespace ChocolArm64.State
@ -42,23 +43,23 @@ namespace ChocolArm64.State
public long CntpctEl0 => Environment.TickCount * TicksPerMS;
public event EventHandler<AInstExceptEventArgs> Break;
public event EventHandler<AInstExceptEventArgs> SvcCall;
public event EventHandler<AInstUndEventArgs> Undefined;
public event EventHandler<AInstExceptionEventArgs> Break;
public event EventHandler<AInstExceptionEventArgs> SvcCall;
public event EventHandler<AInstUndefinedEventArgs> Undefined;
internal void OnBreak(int Imm)
{
Break?.Invoke(this, new AInstExceptEventArgs(Imm));
Break?.Invoke(this, new AInstExceptionEventArgs(Imm));
}
internal void OnSvcCall(int Imm)
{
SvcCall?.Invoke(this, new AInstExceptEventArgs(Imm));
SvcCall?.Invoke(this, new AInstExceptionEventArgs(Imm));
}
internal void OnUndefined(long Position, int RawOpCode)
{
Undefined?.Invoke(this, new AInstUndEventArgs(Position, RawOpCode));
Undefined?.Invoke(this, new AInstUndefinedEventArgs(Position, RawOpCode));
}
}
}

View File

@ -0,0 +1,7 @@
namespace ChocolArm64.Translation
{
struct AILBarrier : IAILEmit
{
public void Emit(AILEmitter Context) { }
}
}

View File

@ -4,11 +4,13 @@ namespace ChocolArm64.Translation
{
class AILBlock : IAILEmit
{
public long IntInputs { get; private set; }
public long IntOutputs { get; private set; }
public long IntInputs { get; private set; }
public long IntOutputs { get; private set; }
public long IntAwOutputs { get; private set; }
public long VecInputs { get; private set; }
public long VecOutputs { get; private set; }
public long VecInputs { get; private set; }
public long VecOutputs { get; private set; }
public long VecAwOutputs { get; private set; }
public bool HasStateStore { get; private set; }
@ -24,13 +26,22 @@ namespace ChocolArm64.Translation
public void Add(IAILEmit ILEmitter)
{
if (ILEmitter is AILOpCodeLoad Ld && AILEmitter.IsRegIndex(Ld.Index))
if (ILEmitter is AILBarrier)
{
//Those barriers are used to separate the groups of CIL
//opcodes emitted by each ARM instruction.
//We can only consider the new outputs for doing input elimination
//after all the CIL opcodes used by the instruction being emitted.
IntAwOutputs = IntOutputs;
VecAwOutputs = VecOutputs;
}
else if (ILEmitter is AILOpCodeLoad Ld && AILEmitter.IsRegIndex(Ld.Index))
{
switch (Ld.IoType)
{
case AIoType.Flag: IntInputs |= ((1L << Ld.Index) << 32) & ~IntOutputs; break;
case AIoType.Int: IntInputs |= (1L << Ld.Index) & ~IntOutputs; break;
case AIoType.Vector: VecInputs |= (1L << Ld.Index) & ~VecOutputs; break;
case AIoType.Flag: IntInputs |= ((1L << Ld.Index) << 32) & ~IntAwOutputs; break;
case AIoType.Int: IntInputs |= (1L << Ld.Index) & ~IntAwOutputs; break;
case AIoType.Vector: VecInputs |= (1L << Ld.Index) & ~VecAwOutputs; break;
}
}
else if (ILEmitter is AILOpCodeStore St)

View File

@ -39,14 +39,16 @@ namespace ChocolArm64.Translation
private const int Tmp4Index = -4;
private const int Tmp5Index = -5;
public AILEmitterCtx(ATranslator Translator, ABlock[] Graph, ABlock Root)
public AILEmitterCtx(
ATranslator Translator,
ABlock[] Graph,
ABlock Root,
string SubName)
{
this.Translator = Translator;
this.Graph = Graph;
this.Root = Root;
string SubName = $"Sub{Root.Position:X16}";
Labels = new Dictionary<long, AILLabel>();
Emitter = new AILEmitter(Graph, Root, SubName);
@ -92,6 +94,8 @@ namespace ChocolArm64.Translation
}
CurrOp.Emitter(this);
ILBlock.Add(new AILBarrier());
}
public bool TryOptEmitSubroutineCall()

View File

@ -11,12 +11,10 @@ namespace ChocolArm64.Translation
public ARegisterSize RegisterSize { get; private set; }
public AILOpCodeLoad(int Index, AIoType IoType) : this(Index, IoType, ARegisterSize.Int64) { }
public AILOpCodeLoad(int Index, AIoType IoType, ARegisterSize RegisterSize)
public AILOpCodeLoad(int Index, AIoType IoType, ARegisterSize RegisterSize = 0)
{
this.IoType = IoType;
this.Index = Index;
this.IoType = IoType;
this.RegisterSize = RegisterSize;
}

View File

@ -11,10 +11,10 @@ namespace ChocolArm64.Translation
public ARegisterSize RegisterSize { get; private set; }
public AILOpCodeStore(int Index, AIoType IoType, ARegisterSize RegisterSize = ARegisterSize.Int64)
public AILOpCodeStore(int Index, AIoType IoType, ARegisterSize RegisterSize = 0)
{
this.IoType = IoType;
this.Index = Index;
this.IoType = IoType;
this.RegisterSize = RegisterSize;
}

View File

@ -67,7 +67,7 @@ namespace ChocolArm64.Translation
public long VecOutputs;
}
private const int MaxOptGraphLength = 120;
private const int MaxOptGraphLength = 55;
public ALocalAlloc(AILBlock[] Graph, AILBlock Root)
{
@ -149,11 +149,7 @@ namespace ChocolArm64.Translation
if (RetTarget)
{
BlkIO.Entry = Block;
BlkIO.IntInputs = 0;
BlkIO.VecInputs = 0;
BlkIO.IntOutputs = 0;
BlkIO.VecOutputs = 0;
BlkIO.Entry = Block;
}
else
{