Ryujinx/ChocolArm64/ATranslatedSub.cs

104 lines
2.8 KiB
C#
Raw Normal View History

2018-02-04 15:08:20 -08:00
using ChocolArm64.Memory;
using ChocolArm64.State;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
namespace ChocolArm64
{
class ATranslatedSub
{
2018-02-18 11:28:07 -08:00
private delegate long AA64Subroutine(AThreadState Register, AMemory Memory);
2018-02-04 15:08:20 -08:00
private AA64Subroutine ExecDelegate;
private bool HasDelegate;
public static Type[] FixedArgTypes { get; private set; }
2018-02-18 11:28:07 -08:00
public static int StateArgIdx { get; private set; }
public static int MemoryArgIdx { get; private set; }
2018-02-04 15:08:20 -08:00
public DynamicMethod Method { get; private set; }
public HashSet<long> SubCalls { get; private set; }
public List<ARegister> Params { get; private set; }
public bool NeedsReJit { get; private set; }
public ATranslatedSub()
{
SubCalls = new HashSet<long>();
}
public ATranslatedSub(DynamicMethod Method, List<ARegister> Params) : this()
{
if (Params == null)
{
throw new ArgumentNullException(nameof(Params));
}
this.Method = Method;
this.Params = Params;
}
static ATranslatedSub()
{
MethodInfo MthdInfo = typeof(AA64Subroutine).GetMethod("Invoke");
ParameterInfo[] Params = MthdInfo.GetParameters();
FixedArgTypes = new Type[Params.Length];
for (int Index = 0; Index < Params.Length; Index++)
{
Type ParamType = Params[Index].ParameterType;
FixedArgTypes[Index] = ParamType;
2018-02-18 11:28:07 -08:00
if (ParamType == typeof(AThreadState))
2018-02-04 15:08:20 -08:00
{
2018-02-18 11:28:07 -08:00
StateArgIdx = Index;
2018-02-04 15:08:20 -08:00
}
else if (ParamType == typeof(AMemory))
{
MemoryArgIdx = Index;
}
}
}
2018-02-18 11:28:07 -08:00
public long Execute(AThreadState ThreadState, AMemory Memory)
2018-02-04 15:08:20 -08:00
{
if (!HasDelegate)
{
string Name = $"{Method.Name}_Dispatch";
DynamicMethod Mthd = new DynamicMethod(Name, typeof(long), FixedArgTypes);
ILGenerator Generator = Mthd.GetILGenerator();
Generator.EmitLdargSeq(FixedArgTypes.Length);
foreach (ARegister Reg in Params)
{
2018-02-18 11:28:07 -08:00
Generator.EmitLdarg(StateArgIdx);
2018-02-04 15:08:20 -08:00
Generator.Emit(OpCodes.Ldfld, Reg.GetField());
}
Generator.Emit(OpCodes.Call, Method);
Generator.Emit(OpCodes.Ret);
ExecDelegate = (AA64Subroutine)Mthd.CreateDelegate(typeof(AA64Subroutine));
HasDelegate = true;
}
2018-02-18 11:28:07 -08:00
return ExecDelegate(ThreadState, Memory);
2018-02-04 15:08:20 -08:00
}
public void MarkForReJit() => NeedsReJit = true;
}
}