ILMethodBuilder.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using ChocolArm64.State;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection.Emit;
  5. using System.Runtime.Intrinsics;
  6. namespace ChocolArm64.Translation
  7. {
  8. class ILMethodBuilder
  9. {
  10. public LocalAlloc LocalAlloc { get; private set; }
  11. public ILGenerator Generator { get; private set; }
  12. private Dictionary<Register, int> _locals;
  13. private ILBlock[] _ilBlocks;
  14. private string _subName;
  15. private int _localsCount;
  16. public ILMethodBuilder(ILBlock[] ilBlocks, string subName)
  17. {
  18. _ilBlocks = ilBlocks;
  19. _subName = subName;
  20. }
  21. public TranslatedSub GetSubroutine()
  22. {
  23. LocalAlloc = new LocalAlloc(_ilBlocks, _ilBlocks[0]);
  24. List<Register> subArgs = new List<Register>();
  25. void SetArgs(long inputs, RegisterType baseType)
  26. {
  27. for (int bit = 0; bit < 64; bit++)
  28. {
  29. long mask = 1L << bit;
  30. if ((inputs & mask) != 0)
  31. {
  32. subArgs.Add(GetRegFromBit(bit, baseType));
  33. }
  34. }
  35. }
  36. SetArgs(LocalAlloc.GetIntInputs(_ilBlocks[0]), RegisterType.Int);
  37. SetArgs(LocalAlloc.GetVecInputs(_ilBlocks[0]), RegisterType.Vector);
  38. DynamicMethod method = new DynamicMethod(_subName, typeof(long), GetArgumentTypes(subArgs));
  39. Generator = method.GetILGenerator();
  40. TranslatedSub subroutine = new TranslatedSub(method, subArgs);
  41. int argsStart = TranslatedSub.FixedArgTypes.Length;
  42. _locals = new Dictionary<Register, int>();
  43. _localsCount = 0;
  44. for (int index = 0; index < subroutine.SubArgs.Count; index++)
  45. {
  46. Register reg = subroutine.SubArgs[index];
  47. Generator.EmitLdarg(index + argsStart);
  48. Generator.EmitStloc(GetLocalIndex(reg));
  49. }
  50. foreach (ILBlock ilBlock in _ilBlocks)
  51. {
  52. ilBlock.Emit(this);
  53. }
  54. return subroutine;
  55. }
  56. private Type[] GetArgumentTypes(IList<Register> Params)
  57. {
  58. Type[] fixedArgs = TranslatedSub.FixedArgTypes;
  59. Type[] output = new Type[Params.Count + fixedArgs.Length];
  60. fixedArgs.CopyTo(output, 0);
  61. int typeIdx = fixedArgs.Length;
  62. for (int index = 0; index < Params.Count; index++)
  63. {
  64. output[typeIdx++] = GetFieldType(Params[index].Type);
  65. }
  66. return output;
  67. }
  68. public int GetLocalIndex(Register reg)
  69. {
  70. if (!_locals.TryGetValue(reg, out int index))
  71. {
  72. Generator.DeclareLocal(GetFieldType(reg.Type));
  73. index = _localsCount++;
  74. _locals.Add(reg, index);
  75. }
  76. return index;
  77. }
  78. private static Type GetFieldType(RegisterType regType)
  79. {
  80. switch (regType)
  81. {
  82. case RegisterType.Flag: return typeof(bool);
  83. case RegisterType.Int: return typeof(ulong);
  84. case RegisterType.Vector: return typeof(Vector128<float>);
  85. }
  86. throw new ArgumentException(nameof(regType));
  87. }
  88. public static Register GetRegFromBit(int bit, RegisterType baseType)
  89. {
  90. if (bit < 32)
  91. {
  92. return new Register(bit, baseType);
  93. }
  94. else if (baseType == RegisterType.Int)
  95. {
  96. return new Register(bit & 0x1f, RegisterType.Flag);
  97. }
  98. else
  99. {
  100. throw new ArgumentOutOfRangeException(nameof(bit));
  101. }
  102. }
  103. public static bool IsRegIndex(int index)
  104. {
  105. return (uint)index < 32;
  106. }
  107. }
  108. }