ILEmitter.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reflection.Emit;
  6. using System.Runtime.Intrinsics;
  7. namespace ChocolArm64.Translation
  8. {
  9. class ILEmitter
  10. {
  11. public LocalAlloc LocalAlloc { get; private set; }
  12. public ILGenerator Generator { get; private set; }
  13. private Dictionary<Register, int> _locals;
  14. private ILBlock[] _ilBlocks;
  15. private ILBlock _root;
  16. private TranslatedSub _subroutine;
  17. private string _subName;
  18. private int _localsCount;
  19. public ILEmitter(Block[] graph, Block root, string subName)
  20. {
  21. _subName = subName;
  22. _locals = new Dictionary<Register, int>();
  23. _ilBlocks = new ILBlock[graph.Length];
  24. ILBlock GetBlock(int index)
  25. {
  26. if (index < 0 || index >= _ilBlocks.Length)
  27. {
  28. return null;
  29. }
  30. if (_ilBlocks[index] == null)
  31. {
  32. _ilBlocks[index] = new ILBlock();
  33. }
  34. return _ilBlocks[index];
  35. }
  36. for (int index = 0; index < _ilBlocks.Length; index++)
  37. {
  38. ILBlock block = GetBlock(index);
  39. block.Next = GetBlock(Array.IndexOf(graph, graph[index].Next));
  40. block.Branch = GetBlock(Array.IndexOf(graph, graph[index].Branch));
  41. }
  42. _root = _ilBlocks[Array.IndexOf(graph, root)];
  43. }
  44. public ILBlock GetIlBlock(int index) => _ilBlocks[index];
  45. public TranslatedSub GetSubroutine()
  46. {
  47. LocalAlloc = new LocalAlloc(_ilBlocks, _root);
  48. InitSubroutine();
  49. InitLocals();
  50. foreach (ILBlock ilBlock in _ilBlocks)
  51. {
  52. ilBlock.Emit(this);
  53. }
  54. return _subroutine;
  55. }
  56. private void InitSubroutine()
  57. {
  58. List<Register> Params = new List<Register>();
  59. void SetParams(long inputs, RegisterType baseType)
  60. {
  61. for (int bit = 0; bit < 64; bit++)
  62. {
  63. long mask = 1L << bit;
  64. if ((inputs & mask) != 0)
  65. {
  66. Params.Add(GetRegFromBit(bit, baseType));
  67. }
  68. }
  69. }
  70. SetParams(LocalAlloc.GetIntInputs(_root), RegisterType.Int);
  71. SetParams(LocalAlloc.GetVecInputs(_root), RegisterType.Vector);
  72. DynamicMethod mthd = new DynamicMethod(_subName, typeof(long), GetParamTypes(Params));
  73. Generator = mthd.GetILGenerator();
  74. _subroutine = new TranslatedSub(mthd, Params);
  75. }
  76. private void InitLocals()
  77. {
  78. int paramsStart = TranslatedSub.FixedArgTypes.Length;
  79. _locals = new Dictionary<Register, int>();
  80. for (int index = 0; index < _subroutine.Params.Count; index++)
  81. {
  82. Register reg = _subroutine.Params[index];
  83. Generator.EmitLdarg(index + paramsStart);
  84. Generator.EmitStloc(GetLocalIndex(reg));
  85. }
  86. }
  87. private Type[] GetParamTypes(IList<Register> Params)
  88. {
  89. Type[] fixedArgs = TranslatedSub.FixedArgTypes;
  90. Type[] output = new Type[Params.Count + fixedArgs.Length];
  91. fixedArgs.CopyTo(output, 0);
  92. int typeIdx = fixedArgs.Length;
  93. for (int index = 0; index < Params.Count; index++)
  94. {
  95. output[typeIdx++] = GetFieldType(Params[index].Type);
  96. }
  97. return output;
  98. }
  99. public int GetLocalIndex(Register reg)
  100. {
  101. if (!_locals.TryGetValue(reg, out int index))
  102. {
  103. Generator.DeclareLocal(GetLocalType(reg));
  104. index = _localsCount++;
  105. _locals.Add(reg, index);
  106. }
  107. return index;
  108. }
  109. public Type GetLocalType(Register reg) => GetFieldType(reg.Type);
  110. public Type GetFieldType(RegisterType regType)
  111. {
  112. switch (regType)
  113. {
  114. case RegisterType.Flag: return typeof(bool);
  115. case RegisterType.Int: return typeof(ulong);
  116. case RegisterType.Vector: return typeof(Vector128<float>);
  117. }
  118. throw new ArgumentException(nameof(regType));
  119. }
  120. public static Register GetRegFromBit(int bit, RegisterType baseType)
  121. {
  122. if (bit < 32)
  123. {
  124. return new Register(bit, baseType);
  125. }
  126. else if (baseType == RegisterType.Int)
  127. {
  128. return new Register(bit & 0x1f, RegisterType.Flag);
  129. }
  130. else
  131. {
  132. throw new ArgumentOutOfRangeException(nameof(bit));
  133. }
  134. }
  135. public static bool IsRegIndex(int index)
  136. {
  137. return index >= 0 && index < 32;
  138. }
  139. }
  140. }