ILMethodBuilder.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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(TranslationTier tier)
  22. {
  23. LocalAlloc = new LocalAlloc(_ilBlocks, _ilBlocks[0]);
  24. DynamicMethod method = new DynamicMethod(_subName, typeof(long), TranslatedSub.FixedArgTypes);
  25. Generator = method.GetILGenerator();
  26. TranslatedSub subroutine = new TranslatedSub(method, tier);
  27. _locals = new Dictionary<Register, int>();
  28. _localsCount = 0;
  29. new ILOpCodeLoadState(_ilBlocks[0]).Emit(this);
  30. foreach (ILBlock ilBlock in _ilBlocks)
  31. {
  32. ilBlock.Emit(this);
  33. }
  34. subroutine.PrepareMethod();
  35. return subroutine;
  36. }
  37. public int GetLocalIndex(Register reg)
  38. {
  39. if (!_locals.TryGetValue(reg, out int index))
  40. {
  41. Generator.DeclareLocal(GetFieldType(reg.Type));
  42. index = _localsCount++;
  43. _locals.Add(reg, index);
  44. }
  45. return index;
  46. }
  47. private static Type GetFieldType(RegisterType regType)
  48. {
  49. switch (regType)
  50. {
  51. case RegisterType.Flag: return typeof(bool);
  52. case RegisterType.Int: return typeof(ulong);
  53. case RegisterType.Vector: return typeof(Vector128<float>);
  54. }
  55. throw new ArgumentException(nameof(regType));
  56. }
  57. public static Register GetRegFromBit(int bit, RegisterType baseType)
  58. {
  59. if (bit < 32)
  60. {
  61. return new Register(bit, baseType);
  62. }
  63. else if (baseType == RegisterType.Int)
  64. {
  65. return new Register(bit & 0x1f, RegisterType.Flag);
  66. }
  67. else
  68. {
  69. throw new ArgumentOutOfRangeException(nameof(bit));
  70. }
  71. }
  72. public static bool IsRegIndex(int index)
  73. {
  74. return (uint)index < 32;
  75. }
  76. }
  77. }