2018-02-05 00:08:20 +01:00
|
|
|
using ChocolArm64.Decoder;
|
2018-02-26 02:14:58 +01:00
|
|
|
using ChocolArm64.Events;
|
|
|
|
using ChocolArm64.Memory;
|
2018-05-26 22:49:21 +02:00
|
|
|
using ChocolArm64.State;
|
2018-02-05 00:08:20 +01:00
|
|
|
using ChocolArm64.Translation;
|
2018-02-26 02:14:58 +01:00
|
|
|
using System;
|
2018-02-05 00:08:20 +01:00
|
|
|
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-09-19 22:07:56 +02:00
|
|
|
private ATranslatorCache Cache;
|
2018-02-26 02:14:58 +01:00
|
|
|
|
|
|
|
public event EventHandler<ACpuTraceEventArgs> CpuTrace;
|
|
|
|
|
|
|
|
public bool EnableCpuTrace { get; set; }
|
2018-02-18 05:57:33 +01:00
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
public ATranslator()
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-09-19 22:07:56 +02:00
|
|
|
Cache = new ATranslatorCache();
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 18:09:59 +01:00
|
|
|
internal void ExecuteSubroutine(AThread Thread, long Position)
|
2018-05-26 22:49:21 +02:00
|
|
|
{
|
|
|
|
//TODO: Both the execute A32/A64 methods should be merged on the future,
|
|
|
|
//when both ISAs are implemented with the interpreter and JIT.
|
|
|
|
//As of now, A32 only has a interpreter and A64 a JIT.
|
|
|
|
AThreadState State = Thread.ThreadState;
|
|
|
|
AMemory Memory = Thread.Memory;
|
|
|
|
|
|
|
|
if (State.ExecutionMode == AExecutionMode.AArch32)
|
|
|
|
{
|
|
|
|
ExecuteSubroutineA32(State, Memory);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ExecuteSubroutineA64(State, Memory, Position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ExecuteSubroutineA32(AThreadState State, AMemory Memory)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
AOpCode OpCode = ADecoder.DecodeOpCode(State, Memory, State.R15);
|
|
|
|
|
|
|
|
OpCode.Interpreter(State, Memory, OpCode);
|
|
|
|
}
|
|
|
|
while (State.R15 != 0 && State.Running);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ExecuteSubroutineA64(AThreadState State, AMemory Memory, 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-09-19 22:07:56 +02:00
|
|
|
CpuTrace?.Invoke(this, new ACpuTraceEventArgs(Position));
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-02-26 02:14:58 +01:00
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
if (!Cache.TryGetSubroutine(Position, out ATranslatedSub Sub))
|
2018-03-04 18:09:59 +01:00
|
|
|
{
|
2018-05-26 22:49:21 +02:00
|
|
|
Sub = TranslateTier0(State, Memory, Position);
|
2018-03-04 18:09:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Sub.ShouldReJit())
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-05-26 22:49:21 +02:00
|
|
|
TranslateTier1(State, Memory, Position);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-02-26 02:14:58 +01:00
|
|
|
|
2018-05-26 22:49:21 +02:00
|
|
|
Position = Sub.Execute(State, Memory);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-05-26 22:49:21 +02:00
|
|
|
while (Position != 0 && State.Running);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
internal bool HasCachedSub(long Position)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-09-19 22:07:56 +02:00
|
|
|
return Cache.HasSubroutine(Position);
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
|
2018-05-26 22:49:21 +02:00
|
|
|
private ATranslatedSub TranslateTier0(AThreadState State, AMemory Memory, long Position)
|
2018-03-04 18:09:59 +01:00
|
|
|
{
|
2018-09-19 22:07:56 +02:00
|
|
|
ABlock Block = ADecoder.DecodeBasicBlock(State, Memory, Position);
|
2018-03-04 18:09:59 +01:00
|
|
|
|
|
|
|
ABlock[] Graph = new ABlock[] { Block };
|
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
string SubName = GetSubroutineName(Position);
|
2018-03-04 18:09:59 +01:00
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
AILEmitterCtx Context = new AILEmitterCtx(Cache, Graph, Block, SubName);
|
2018-03-04 18:09:59 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Context.EmitOpCode();
|
|
|
|
}
|
|
|
|
while (Context.AdvanceOpCode());
|
|
|
|
|
|
|
|
ATranslatedSub Subroutine = Context.GetSubroutine();
|
|
|
|
|
2018-05-26 22:49:21 +02:00
|
|
|
Subroutine.SetType(ATranslatedSubType.SubTier0);
|
2018-03-04 18:09:59 +01:00
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
Cache.AddOrUpdate(Position, Subroutine, Block.OpCodes.Count);
|
2018-03-04 18:09:59 +01:00
|
|
|
|
|
|
|
AOpCode LastOp = Block.GetLastOp();
|
|
|
|
|
|
|
|
return Subroutine;
|
|
|
|
}
|
|
|
|
|
2018-05-26 22:49:21 +02:00
|
|
|
private void TranslateTier1(AThreadState State, AMemory Memory, long Position)
|
2018-02-05 00:08:20 +01:00
|
|
|
{
|
2018-09-19 22:07:56 +02:00
|
|
|
(ABlock[] Graph, ABlock Root) = ADecoder.DecodeSubroutine(Cache, State, Memory, Position);
|
2018-02-26 02:14:58 +01:00
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
string SubName = GetSubroutineName(Position);
|
2018-02-26 02:14:58 +01:00
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
AILEmitterCtx Context = new AILEmitterCtx(Cache, Graph, 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-09-19 22:07:56 +02:00
|
|
|
if (Cache.TryGetSubroutine(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-09-19 22:07:56 +02:00
|
|
|
if (Cache.TryGetSubroutine(Position, out ATranslatedSub CallerSub))
|
2018-04-11 19:44:03 +02:00
|
|
|
{
|
|
|
|
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-09-19 22:07:56 +02:00
|
|
|
Cache.AddOrUpdate(Position, Subroutine, GetGraphInstCount(Graph));
|
2018-03-04 18:09:59 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
private string GetSubroutineName(long Position)
|
2018-03-04 18:09:59 +01:00
|
|
|
{
|
2018-09-19 22:07:56 +02:00
|
|
|
return $"Sub{Position:x16}";
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
2018-02-26 02:14:58 +01:00
|
|
|
|
2018-09-19 22:07:56 +02:00
|
|
|
private int GetGraphInstCount(ABlock[] Graph)
|
2018-02-26 02:14:58 +01:00
|
|
|
{
|
2018-09-19 22:07:56 +02:00
|
|
|
int Size = 0;
|
|
|
|
|
2018-02-26 02:14:58 +01:00
|
|
|
foreach (ABlock Block in Graph)
|
|
|
|
{
|
2018-09-19 22:07:56 +02:00
|
|
|
Size += Block.OpCodes.Count;
|
2018-02-26 02:14:58 +01:00
|
|
|
}
|
2018-09-19 22:07:56 +02:00
|
|
|
|
|
|
|
return Size;
|
2018-02-26 02:14:58 +01:00
|
|
|
}
|
2018-02-05 00:08:20 +01:00
|
|
|
}
|
|
|
|
}
|