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
|
|
|
}
|
|
|
|
|
2019-02-04 22:26:05 +01:00
|
|
|
public TranslatedSub GetSubroutine(TranslationTier tier)
|
2018-10-31 02:43:02 +01:00
|
|
|
{
|
2018-12-11 01:58:52 +01:00
|
|
|
LocalAlloc = new LocalAlloc(_ilBlocks, _ilBlocks[0]);
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-02-04 22:26:05 +01:00
|
|
|
DynamicMethod method = new DynamicMethod(_subName, typeof(long), TranslatedSub.FixedArgTypes);
|
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
|
|
|
|
2019-02-04 22:26:05 +01:00
|
|
|
TranslatedSub subroutine = new TranslatedSub(method, tier);
|
2018-10-31 02:43:02 +01:00
|
|
|
|
|
|
|
_locals = new Dictionary<Register, int>();
|
|
|
|
|
2018-12-11 01:58:52 +01:00
|
|
|
_localsCount = 0;
|
|
|
|
|
2019-02-04 22:26:05 +01:00
|
|
|
new ILOpCodeLoadState(_ilBlocks[0]).Emit(this);
|
2018-12-11 01:58:52 +01:00
|
|
|
|
|
|
|
foreach (ILBlock ilBlock in _ilBlocks)
|
|
|
|
{
|
|
|
|
ilBlock.Emit(this);
|
|
|
|
}
|
|
|
|
|
2019-02-04 22:26:05 +01:00
|
|
|
subroutine.PrepareMethod();
|
2018-10-31 02:43:02 +01:00
|
|
|
|
2019-02-04 22:26:05 +01:00
|
|
|
return subroutine;
|
2018-10-31 02:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|