AILEmitter.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reflection.Emit;
  6. namespace ChocolArm64.Translation
  7. {
  8. class AILEmitter
  9. {
  10. public ALocalAlloc LocalAlloc { get; private set; }
  11. public ILGenerator Generator { get; private set; }
  12. private Dictionary<ARegister, int> Locals;
  13. private AILBlock[] ILBlocks;
  14. private AILBlock Root;
  15. private ATranslatedSub Subroutine;
  16. private string SubName;
  17. private int LocalsCount;
  18. public AILEmitter(ABlock[] Graph, ABlock Root, string SubName)
  19. {
  20. this.SubName = SubName;
  21. Locals = new Dictionary<ARegister, int>();
  22. ILBlocks = new AILBlock[Graph.Length];
  23. AILBlock GetBlock(int Index)
  24. {
  25. if (Index < 0 || Index >= ILBlocks.Length)
  26. {
  27. return null;
  28. }
  29. if (ILBlocks[Index] == null)
  30. {
  31. ILBlocks[Index] = new AILBlock();
  32. }
  33. return ILBlocks[Index];
  34. }
  35. for (int Index = 0; Index < ILBlocks.Length; Index++)
  36. {
  37. AILBlock Block = GetBlock(Index);
  38. Block.Next = GetBlock(Array.IndexOf(Graph, Graph[Index].Next));
  39. Block.Branch = GetBlock(Array.IndexOf(Graph, Graph[Index].Branch));
  40. }
  41. this.Root = ILBlocks[Array.IndexOf(Graph, Root)];
  42. }
  43. public ATranslatedSub GetSubroutine()
  44. {
  45. LocalAlloc = new ALocalAlloc(ILBlocks, Root);
  46. InitSubroutine();
  47. InitLocals();
  48. foreach (AILBlock ILBlock in ILBlocks)
  49. {
  50. ILBlock.Emit(this);
  51. }
  52. return Subroutine;
  53. }
  54. public AILBlock GetILBlock(int Index) => ILBlocks[Index];
  55. private void InitLocals()
  56. {
  57. int ParamsStart = ATranslatedSub.FixedArgTypes.Length;
  58. Locals = new Dictionary<ARegister, int>();
  59. for (int Index = 0; Index < Subroutine.Params.Count; Index++)
  60. {
  61. ARegister Reg = Subroutine.Params[Index];
  62. Generator.EmitLdarg(Index + ParamsStart);
  63. Generator.EmitStloc(GetLocalIndex(Reg));
  64. }
  65. }
  66. private void InitSubroutine()
  67. {
  68. List<ARegister> Params = new List<ARegister>();
  69. void SetParams(long Inputs, ARegisterType BaseType)
  70. {
  71. for (int Bit = 0; Bit < 64; Bit++)
  72. {
  73. long Mask = 1L << Bit;
  74. if ((Inputs & Mask) != 0)
  75. {
  76. Params.Add(GetRegFromBit(Bit, BaseType));
  77. }
  78. }
  79. }
  80. SetParams(LocalAlloc.GetIntInputs(Root), ARegisterType.Int);
  81. SetParams(LocalAlloc.GetVecInputs(Root), ARegisterType.Vector);
  82. DynamicMethod Mthd = new DynamicMethod(SubName, typeof(long), GetParamTypes(Params));
  83. Generator = Mthd.GetILGenerator();
  84. Subroutine = new ATranslatedSub(Mthd, Params);
  85. }
  86. private Type[] GetParamTypes(IList<ARegister> Params)
  87. {
  88. Type[] FixedArgs = ATranslatedSub.FixedArgTypes;
  89. Type[] Output = new Type[Params.Count + FixedArgs.Length];
  90. FixedArgs.CopyTo(Output, 0);
  91. int TypeIdx = FixedArgs.Length;
  92. for (int Index = 0; Index < Params.Count; Index++)
  93. {
  94. Output[TypeIdx++] = GetFieldType(Params[Index].Type);
  95. }
  96. return Output;
  97. }
  98. public int GetLocalIndex(ARegister Reg)
  99. {
  100. if (!Locals.TryGetValue(Reg, out int Index))
  101. {
  102. Generator.DeclareLocal(GetLocalType(Reg));
  103. Index = LocalsCount++;
  104. Locals.Add(Reg, Index);
  105. }
  106. return Index;
  107. }
  108. public Type GetLocalType(ARegister Reg) => GetFieldType(Reg.Type);
  109. public Type GetFieldType(ARegisterType RegType)
  110. {
  111. switch (RegType)
  112. {
  113. case ARegisterType.Flag: return typeof(bool);
  114. case ARegisterType.Int: return typeof(ulong);
  115. case ARegisterType.Vector: return typeof(AVec);
  116. }
  117. throw new ArgumentException(nameof(RegType));
  118. }
  119. public static ARegister GetRegFromBit(int Bit, ARegisterType BaseType)
  120. {
  121. if (Bit < 32)
  122. {
  123. return new ARegister(Bit, BaseType);
  124. }
  125. else if (BaseType == ARegisterType.Int)
  126. {
  127. return new ARegister(Bit & 0x1f, ARegisterType.Flag);
  128. }
  129. else
  130. {
  131. throw new ArgumentOutOfRangeException(nameof(Bit));
  132. }
  133. }
  134. public static bool IsRegIndex(int Index)
  135. {
  136. return Index >= 0 && Index < 32;
  137. }
  138. }
  139. }