2018-10-31 02:43:02 +01:00
|
|
|
using ChocolArm64.State;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
using System.Runtime.Intrinsics;
|
|
|
|
|
|
|
|
namespace ChocolArm64.Translation
|
|
|
|
{
|
2018-12-11 01:58:52 +01:00
|
|
|
class ILMethodBuilder
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
|
|
|
public LocalAlloc LocalAlloc { get; private set; }
|
|
|
|
|
|
|
|
public ILGenerator Generator { get; private set; }
|
|
|
|
|
|
|
|
private Dictionary<Register, int> _locals;
|
|
|
|
|
|
|
|
private ILBlock[] _ilBlocks;
|
|
|
|
|
|
|
|
private string _subName;
|
|
|
|
|
|
|
|
private int _localsCount;
|
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
public ILMethodBuilder(ILBlock[] ilBlocks, string subName)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2018-12-11 01:58:52 +01:00
|
|
|
_ilBlocks = ilBlocks;
|
|
|
|
_subName = subName;
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public TranslatedSub GetSubroutine()
|
|
|
|
{
|
2018-12-11 01:58:52 +01:00
|
|
|
LocalAlloc = new LocalAlloc(_ilBlocks, _ilBlocks[0]);
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
List<Register> subArgs = new List<Register>();
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
void SetArgs(long inputs, RegisterType baseType)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
|
|
|
for (int bit = 0; bit < 64; bit++)
|
|
|
|
{
|
|
|
|
long mask = 1L << bit;
|
|
|
|
|
|
|
|
if ((inputs & mask) != 0)
|
|
|
|
{
|
2018-12-11 01:58:52 +01:00
|
|
|
subArgs.Add(GetRegFromBit(bit, baseType));
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
SetArgs(LocalAlloc.GetIntInputs(_ilBlocks[0]), RegisterType.Int);
|
|
|
|
SetArgs(LocalAlloc.GetVecInputs(_ilBlocks[0]), RegisterType.Vector);
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
DynamicMethod method = new DynamicMethod(_subName, typeof(long), GetArgumentTypes(subArgs));
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
Generator = method.GetILGenerator();
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
TranslatedSub subroutine = new TranslatedSub(method, subArgs);
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
int argsStart = TranslatedSub.FixedArgTypes.Length;
|
2018-10-31 02:43:02 +01:00
|
|
|
|
|
|
|
_locals = new Dictionary<Register, int>();
|
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
_localsCount = 0;
|
|
|
|
|
|
|
|
for (int index = 0; index < subroutine.SubArgs.Count; index++)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2018-12-11 01:58:52 +01:00
|
|
|
Register reg = subroutine.SubArgs[index];
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
Generator.EmitLdarg(index + argsStart);
|
2018-10-31 02:43:02 +01:00
|
|
|
Generator.EmitStloc(GetLocalIndex(reg));
|
|
|
|
}
|
2018-12-11 01:58:52 +01:00
|
|
|
|
|
|
|
foreach (ILBlock ilBlock in _ilBlocks)
|
|
|
|
{
|
|
|
|
ilBlock.Emit(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return subroutine;
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
private Type[] GetArgumentTypes(IList<Register> Params)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
|
|
|
Type[] fixedArgs = TranslatedSub.FixedArgTypes;
|
|
|
|
|
|
|
|
Type[] output = new Type[Params.Count + fixedArgs.Length];
|
|
|
|
|
|
|
|
fixedArgs.CopyTo(output, 0);
|
|
|
|
|
|
|
|
int typeIdx = fixedArgs.Length;
|
|
|
|
|
|
|
|
for (int index = 0; index < Params.Count; index++)
|
|
|
|
{
|
|
|
|
output[typeIdx++] = GetFieldType(Params[index].Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int GetLocalIndex(Register reg)
|
|
|
|
{
|
|
|
|
if (!_locals.TryGetValue(reg, out int index))
|
|
|
|
{
|
2018-12-11 01:58:52 +01:00
|
|
|
Generator.DeclareLocal(GetFieldType(reg.Type));
|
2018-10-31 02:43:02 +01:00
|
|
|
|
|
|
|
index = _localsCount++;
|
|
|
|
|
|
|
|
_locals.Add(reg, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
private static Type GetFieldType(RegisterType regType)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
|
|
|
switch (regType)
|
|
|
|
{
|
|
|
|
case RegisterType.Flag: return typeof(bool);
|
|
|
|
case RegisterType.Int: return typeof(ulong);
|
|
|
|
case RegisterType.Vector: return typeof(Vector128<float>);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException(nameof(regType));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Register GetRegFromBit(int bit, RegisterType baseType)
|
|
|
|
{
|
|
|
|
if (bit < 32)
|
|
|
|
{
|
|
|
|
return new Register(bit, baseType);
|
|
|
|
}
|
|
|
|
else if (baseType == RegisterType.Int)
|
|
|
|
{
|
|
|
|
return new Register(bit & 0x1f, RegisterType.Flag);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(bit));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool IsRegIndex(int index)
|
|
|
|
{
|
2018-12-11 01:58:52 +01:00
|
|
|
return (uint)index < 32;
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|