2018-10-30 21:43:02 -04:00
|
|
|
using ChocolArm64.State;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
using System.Runtime.Intrinsics;
|
|
|
|
|
|
|
|
namespace ChocolArm64.Translation
|
|
|
|
{
|
2018-12-10 19:58:52 -05:00
|
|
|
class ILMethodBuilder
|
2018-10-30 21:43:02 -04:00
|
|
|
{
|
2019-02-27 21:03:31 -05:00
|
|
|
private const int RegsCount = 32;
|
|
|
|
private const int RegsMask = RegsCount - 1;
|
|
|
|
|
|
|
|
public RegisterUsage RegUsage { get; private set; }
|
2018-10-30 21:43:02 -04:00
|
|
|
|
|
|
|
public ILGenerator Generator { get; private set; }
|
|
|
|
|
|
|
|
private Dictionary<Register, int> _locals;
|
|
|
|
|
|
|
|
private ILBlock[] _ilBlocks;
|
|
|
|
|
|
|
|
private string _subName;
|
|
|
|
|
2019-02-27 21:03:31 -05:00
|
|
|
public bool IsAarch64 { get; }
|
|
|
|
|
|
|
|
public bool IsSubComplete { get; }
|
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
private int _localsCount;
|
|
|
|
|
2019-02-27 21:03:31 -05:00
|
|
|
public ILMethodBuilder(
|
|
|
|
ILBlock[] ilBlocks,
|
|
|
|
string subName,
|
|
|
|
bool isAarch64,
|
|
|
|
bool isSubComplete = false)
|
2018-10-30 21:43:02 -04:00
|
|
|
{
|
2019-02-27 21:03:31 -05:00
|
|
|
_ilBlocks = ilBlocks;
|
|
|
|
_subName = subName;
|
|
|
|
IsAarch64 = isAarch64;
|
|
|
|
IsSubComplete = isSubComplete;
|
2018-10-30 21:43:02 -04:00
|
|
|
}
|
|
|
|
|
2019-02-27 21:03:31 -05:00
|
|
|
public TranslatedSub GetSubroutine(TranslationTier tier, bool isWorthOptimizing)
|
2018-10-30 21:43:02 -04:00
|
|
|
{
|
2019-02-27 21:03:31 -05:00
|
|
|
RegUsage = new RegisterUsage();
|
|
|
|
|
|
|
|
RegUsage.BuildUses(_ilBlocks[0]);
|
2018-10-30 21:43:02 -04:00
|
|
|
|
2019-02-04 16:26:05 -05:00
|
|
|
DynamicMethod method = new DynamicMethod(_subName, typeof(long), TranslatedSub.FixedArgTypes);
|
2018-10-30 21:43:02 -04:00
|
|
|
|
2019-02-27 21:03:31 -05:00
|
|
|
long intNiRegsMask = RegUsage.GetIntNotInputs(_ilBlocks[0]);
|
|
|
|
long vecNiRegsMask = RegUsage.GetVecNotInputs(_ilBlocks[0]);
|
2018-10-30 21:43:02 -04:00
|
|
|
|
2019-02-27 21:03:31 -05:00
|
|
|
TranslatedSub subroutine = new TranslatedSub(
|
|
|
|
method,
|
|
|
|
intNiRegsMask,
|
|
|
|
vecNiRegsMask,
|
|
|
|
tier,
|
|
|
|
isWorthOptimizing);
|
2018-10-30 21:43:02 -04:00
|
|
|
|
|
|
|
_locals = new Dictionary<Register, int>();
|
|
|
|
|
2018-12-10 19:58:52 -05:00
|
|
|
_localsCount = 0;
|
|
|
|
|
2019-02-27 21:03:31 -05:00
|
|
|
Generator = method.GetILGenerator();
|
2018-12-10 19:58:52 -05:00
|
|
|
|
|
|
|
foreach (ILBlock ilBlock in _ilBlocks)
|
|
|
|
{
|
|
|
|
ilBlock.Emit(this);
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:26:05 -05:00
|
|
|
subroutine.PrepareMethod();
|
2018-10-30 21:43:02 -04:00
|
|
|
|
2019-02-04 16:26:05 -05:00
|
|
|
return subroutine;
|
2018-10-30 21:43:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public int GetLocalIndex(Register reg)
|
|
|
|
{
|
|
|
|
if (!_locals.TryGetValue(reg, out int index))
|
|
|
|
{
|
2018-12-10 19:58:52 -05:00
|
|
|
Generator.DeclareLocal(GetFieldType(reg.Type));
|
2018-10-30 21:43:02 -04:00
|
|
|
|
|
|
|
index = _localsCount++;
|
|
|
|
|
|
|
|
_locals.Add(reg, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2018-12-10 19:58:52 -05:00
|
|
|
private static Type GetFieldType(RegisterType regType)
|
2018-10-30 21:43:02 -04: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)
|
|
|
|
{
|
2019-02-27 21:03:31 -05:00
|
|
|
if (bit < RegsCount)
|
2018-10-30 21:43:02 -04:00
|
|
|
{
|
|
|
|
return new Register(bit, baseType);
|
|
|
|
}
|
|
|
|
else if (baseType == RegisterType.Int)
|
|
|
|
{
|
2019-02-27 21:03:31 -05:00
|
|
|
return new Register(bit & RegsMask, RegisterType.Flag);
|
2018-10-30 21:43:02 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(bit));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool IsRegIndex(int index)
|
|
|
|
{
|
2019-02-27 21:03:31 -05:00
|
|
|
return (uint)index < RegsCount;
|
2018-10-30 21:43:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|