From 068754fec523fc0bc506293db6f8121bfae844dd Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sun, 25 Feb 2018 22:14:58 -0300 Subject: [PATCH] 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 --- AThread.cs | 6 +- ATranslator.cs | 82 ++++++++++++++----- Decoder/ADecoder.cs | 7 +- Events/ACpuTraceEventArgs.cs | 17 ++++ Events/AInstExceptionEventArgs.cs | 14 ++++ .../AInstUndefinedEventArgs.cs | 6 +- Instruction/AInstEmitCsel.cs | 5 +- State/AInstExceptEventArgs.cs | 14 ---- State/AThreadState.cs | 13 +-- Translation/AILBarrier.cs | 7 ++ Translation/AILBlock.cs | 27 ++++-- Translation/AILEmitterCtx.cs | 10 ++- Translation/AILOpCodeLoad.cs | 6 +- Translation/AILOpCodeStore.cs | 4 +- Translation/ALocalAlloc.cs | 8 +- 15 files changed, 151 insertions(+), 75 deletions(-) create mode 100644 Events/ACpuTraceEventArgs.cs create mode 100644 Events/AInstExceptionEventArgs.cs rename State/AInstUndEventArgs.cs => Events/AInstUndefinedEventArgs.cs (60%) delete mode 100644 State/AInstExceptEventArgs.cs create mode 100644 Translation/AILBarrier.cs diff --git a/AThread.cs b/AThread.cs index 5c03228..6e018db 100644 --- a/AThread.cs +++ b/AThread.cs @@ -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); diff --git a/ATranslator.cs b/ATranslator.cs index 96bbc89..2daf7bb 100644 --- a/ATranslator.cs +++ b/ATranslator.cs @@ -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 CachedSubs; - private Dictionary CachedSubs; + private ConcurrentDictionary SymbolTable; + + public event EventHandler CpuTrace; + + public bool EnableCpuTrace { get; set; } private bool KeepRunning; - public ATranslator(AThread Parent) + public ATranslator(IReadOnlyDictionary SymbolTable = null) { - this.Thread = Parent; + CachedSubs = new ConcurrentDictionary(); - CachedSubs = new Dictionary(); + if (SymbolTable != null) + { + this.SymbolTable = new ConcurrentDictionary(SymbolTable); + } + else + { + this.SymbolTable = new ConcurrentDictionary(); + } 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); + } + } + } } } \ No newline at end of file diff --git a/Decoder/ADecoder.cs b/Decoder/ADecoder.cs index a3f44e4..4430229 100644 --- a/Decoder/ADecoder.cs +++ b/Decoder/ADecoder.cs @@ -18,7 +18,10 @@ namespace ChocolArm64.Decoder OpActivators = new ConcurrentDictionary(); } - 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 Visited = new Dictionary(); Dictionary VisitedEnd = new Dictionary(); @@ -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, diff --git a/Events/ACpuTraceEventArgs.cs b/Events/ACpuTraceEventArgs.cs new file mode 100644 index 0000000..fedf386 --- /dev/null +++ b/Events/ACpuTraceEventArgs.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Events/AInstExceptionEventArgs.cs b/Events/AInstExceptionEventArgs.cs new file mode 100644 index 0000000..34f90c8 --- /dev/null +++ b/Events/AInstExceptionEventArgs.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/State/AInstUndEventArgs.cs b/Events/AInstUndefinedEventArgs.cs similarity index 60% rename from State/AInstUndEventArgs.cs rename to Events/AInstUndefinedEventArgs.cs index 53de65a..cdc1728 100644 --- a/State/AInstUndEventArgs.cs +++ b/Events/AInstUndefinedEventArgs.cs @@ -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; diff --git a/Instruction/AInstEmitCsel.cs b/Instruction/AInstEmitCsel.cs index 3308098..2187675 100644 --- a/Instruction/AInstEmitCsel.cs +++ b/Instruction/AInstEmitCsel.cs @@ -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); } } } \ No newline at end of file diff --git a/State/AInstExceptEventArgs.cs b/State/AInstExceptEventArgs.cs deleted file mode 100644 index f2ee039..0000000 --- a/State/AInstExceptEventArgs.cs +++ /dev/null @@ -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; - } - } -} \ No newline at end of file diff --git a/State/AThreadState.cs b/State/AThreadState.cs index cdab403..2d988a6 100644 --- a/State/AThreadState.cs +++ b/State/AThreadState.cs @@ -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 Break; - public event EventHandler SvcCall; - public event EventHandler Undefined; + public event EventHandler Break; + public event EventHandler SvcCall; + public event EventHandler 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)); } } } \ No newline at end of file diff --git a/Translation/AILBarrier.cs b/Translation/AILBarrier.cs new file mode 100644 index 0000000..25b08de --- /dev/null +++ b/Translation/AILBarrier.cs @@ -0,0 +1,7 @@ +namespace ChocolArm64.Translation +{ + struct AILBarrier : IAILEmit + { + public void Emit(AILEmitter Context) { } + } +} \ No newline at end of file diff --git a/Translation/AILBlock.cs b/Translation/AILBlock.cs index bed195a..e580e09 100644 --- a/Translation/AILBlock.cs +++ b/Translation/AILBlock.cs @@ -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) diff --git a/Translation/AILEmitterCtx.cs b/Translation/AILEmitterCtx.cs index 9199edd..ffcfa85 100644 --- a/Translation/AILEmitterCtx.cs +++ b/Translation/AILEmitterCtx.cs @@ -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(); Emitter = new AILEmitter(Graph, Root, SubName); @@ -92,6 +94,8 @@ namespace ChocolArm64.Translation } CurrOp.Emitter(this); + + ILBlock.Add(new AILBarrier()); } public bool TryOptEmitSubroutineCall() diff --git a/Translation/AILOpCodeLoad.cs b/Translation/AILOpCodeLoad.cs index 7cb431e..d60ce53 100644 --- a/Translation/AILOpCodeLoad.cs +++ b/Translation/AILOpCodeLoad.cs @@ -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; } diff --git a/Translation/AILOpCodeStore.cs b/Translation/AILOpCodeStore.cs index c4ea53a..a0feb43 100644 --- a/Translation/AILOpCodeStore.cs +++ b/Translation/AILOpCodeStore.cs @@ -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; } diff --git a/Translation/ALocalAlloc.cs b/Translation/ALocalAlloc.cs index 0661ddc..f23af9c 100644 --- a/Translation/ALocalAlloc.cs +++ b/Translation/ALocalAlloc.cs @@ -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 {