0
0
Fork 0
mirror of https://github.com/GreemDev/Ryujinx.git synced 2024-12-23 09:15:46 +00:00
Ryujinx/ChocolArm64/ATranslator.cs

165 lines
4.8 KiB
C#
Raw Normal View History

2018-02-04 23:08:20 +00:00
using ChocolArm64.Decoder;
using ChocolArm64.Events;
using ChocolArm64.Memory;
using ChocolArm64.State;
2018-02-04 23:08:20 +00:00
using ChocolArm64.Translation;
using System;
2018-02-04 23:08:20 +00:00
using System.Reflection.Emit;
namespace ChocolArm64
{
public class ATranslator
2018-02-04 23:08:20 +00:00
{
private ATranslatorCache Cache;
public event EventHandler<ACpuTraceEventArgs> CpuTrace;
public bool EnableCpuTrace { get; set; }
2018-02-18 04:57:33 +00:00
public ATranslator()
2018-02-04 23:08:20 +00:00
{
Cache = new ATranslatorCache();
2018-02-04 23:08:20 +00:00
}
internal void ExecuteSubroutine(AThread Thread, long Position)
{
//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-04 23:08:20 +00:00
{
do
{
if (EnableCpuTrace)
2018-02-04 23:08:20 +00:00
{
CpuTrace?.Invoke(this, new ACpuTraceEventArgs(Position));
2018-02-04 23:08:20 +00:00
}
if (!Cache.TryGetSubroutine(Position, out ATranslatedSub Sub))
{
Sub = TranslateTier0(State, Memory, Position);
}
if (Sub.ShouldReJit())
2018-02-04 23:08:20 +00:00
{
TranslateTier1(State, Memory, Position);
2018-02-04 23:08:20 +00:00
}
Position = Sub.Execute(State, Memory);
2018-02-04 23:08:20 +00:00
}
while (Position != 0 && State.Running);
2018-02-04 23:08:20 +00:00
}
internal bool HasCachedSub(long Position)
2018-02-04 23:08:20 +00:00
{
return Cache.HasSubroutine(Position);
2018-02-04 23:08:20 +00:00
}
private ATranslatedSub TranslateTier0(AThreadState State, AMemory Memory, long Position)
{
ABlock Block = ADecoder.DecodeBasicBlock(State, Memory, Position);
ABlock[] Graph = new ABlock[] { Block };
string SubName = GetSubroutineName(Position);
AILEmitterCtx Context = new AILEmitterCtx(Cache, Graph, Block, SubName);
do
{
Context.EmitOpCode();
}
while (Context.AdvanceOpCode());
ATranslatedSub Subroutine = Context.GetSubroutine();
Subroutine.SetType(ATranslatedSubType.SubTier0);
Cache.AddOrUpdate(Position, Subroutine, Block.OpCodes.Count);
AOpCode LastOp = Block.GetLastOp();
return Subroutine;
}
private void TranslateTier1(AThreadState State, AMemory Memory, long Position)
2018-02-04 23:08:20 +00:00
{
(ABlock[] Graph, ABlock Root) = ADecoder.DecodeSubroutine(Cache, State, Memory, Position);
string SubName = GetSubroutineName(Position);
AILEmitterCtx Context = new AILEmitterCtx(Cache, Graph, Root, SubName);
2018-02-04 23:08:20 +00: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.
if (Cache.TryGetSubroutine(Position, out ATranslatedSub OldSub))
2018-02-04 23:08:20 +00:00
{
foreach (long CallerPos in OldSub.GetCallerPositions())
2018-02-04 23:08:20 +00:00
{
if (Cache.TryGetSubroutine(Position, out ATranslatedSub CallerSub))
{
CallerSub.MarkForReJit();
}
2018-02-04 23:08:20 +00:00
}
}
ATranslatedSub Subroutine = Context.GetSubroutine();
Subroutine.SetType(ATranslatedSubType.SubTier1);
Cache.AddOrUpdate(Position, Subroutine, GetGraphInstCount(Graph));
}
2018-02-04 23:08:20 +00:00
private string GetSubroutineName(long Position)
{
return $"Sub{Position:x16}";
2018-02-04 23:08:20 +00:00
}
private int GetGraphInstCount(ABlock[] Graph)
{
int Size = 0;
foreach (ABlock Block in Graph)
{
Size += Block.OpCodes.Count;
}
return Size;
}
2018-02-04 23:08:20 +00:00
}
}