AILEmitter.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using ChocolArm64.Decoder;
  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 AILEmitter
  10. {
  11. public ALocalAlloc LocalAlloc { get; private set; }
  12. public ILGenerator Generator { get; private set; }
  13. private Dictionary<ARegister, int> Locals;
  14. private AILBlock[] ILBlocks;
  15. private AILBlock Root;
  16. private ATranslatedSub Subroutine;
  17. private string SubName;
  18. private int LocalsCount;
  19. public AILEmitter(ABlock[] Graph, ABlock Root, string SubName)
  20. {
  21. this.SubName = SubName;
  22. Locals = new Dictionary<ARegister, int>();
  23. ILBlocks = new AILBlock[Graph.Length];
  24. AILBlock 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 AILBlock();
  33. }
  34. return ILBlocks[Index];
  35. }
  36. for (int Index = 0; Index < ILBlocks.Length; Index++)
  37. {
  38. AILBlock 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. this.Root = ILBlocks[Array.IndexOf(Graph, Root)];
  43. }
  44. public AILBlock GetILBlock(int Index) => ILBlocks[Index];
  45. public ATranslatedSub GetSubroutine()
  46. {
  47. LocalAlloc = new ALocalAlloc(ILBlocks, Root);
  48. InitSubroutine();
  49. InitLocals();
  50. foreach (AILBlock ILBlock in ILBlocks)
  51. {
  52. ILBlock.Emit(this);
  53. }
  54. return Subroutine;
  55. }
  56. private void InitSubroutine()
  57. {
  58. List<ARegister> Params = new List<ARegister>();
  59. void SetParams(long Inputs, ARegisterType 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), ARegisterType.Int);
  71. SetParams(LocalAlloc.GetVecInputs(Root), ARegisterType.Vector);
  72. DynamicMethod Mthd = new DynamicMethod(SubName, typeof(long), GetParamTypes(Params));
  73. Generator = Mthd.GetILGenerator();
  74. Subroutine = new ATranslatedSub(Mthd, Params);
  75. }
  76. private void InitLocals()
  77. {
  78. int ParamsStart = ATranslatedSub.FixedArgTypes.Length;
  79. Locals = new Dictionary<ARegister, int>();
  80. for (int Index = 0; Index < Subroutine.Params.Count; Index++)
  81. {
  82. ARegister Reg = Subroutine.Params[Index];
  83. Generator.EmitLdarg(Index + ParamsStart);
  84. Generator.EmitStloc(GetLocalIndex(Reg));
  85. }
  86. }
  87. private Type[] GetParamTypes(IList<ARegister> Params)
  88. {
  89. Type[] FixedArgs = ATranslatedSub.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(ARegister 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(ARegister Reg) => GetFieldType(Reg.Type);
  110. public Type GetFieldType(ARegisterType RegType)
  111. {
  112. switch (RegType)
  113. {
  114. case ARegisterType.Flag: return typeof(bool);
  115. case ARegisterType.Int: return typeof(ulong);
  116. case ARegisterType.Vector: return typeof(Vector128<float>);
  117. }
  118. throw new ArgumentException(nameof(RegType));
  119. }
  120. public static ARegister GetRegFromBit(int Bit, ARegisterType BaseType)
  121. {
  122. if (Bit < 32)
  123. {
  124. return new ARegister(Bit, BaseType);
  125. }
  126. else if (BaseType == ARegisterType.Int)
  127. {
  128. return new ARegister(Bit & 0x1f, ARegisterType.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. }