ADecoder.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using ChocolArm64.Instruction;
  2. using ChocolArm64.Memory;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reflection.Emit;
  6. namespace ChocolArm64.Decoder
  7. {
  8. static class ADecoder
  9. {
  10. public static (ABlock[] Graph, ABlock Root) DecodeSubroutine(ATranslator Translator, long Start)
  11. {
  12. Dictionary<long, ABlock> Visited = new Dictionary<long, ABlock>();
  13. Dictionary<long, ABlock> VisitedEnd = new Dictionary<long, ABlock>();
  14. Queue<ABlock> Blocks = new Queue<ABlock>();
  15. ABlock Enqueue(long Position)
  16. {
  17. if (!Visited.TryGetValue(Position, out ABlock Output))
  18. {
  19. Output = new ABlock(Position);
  20. Blocks.Enqueue(Output);
  21. Visited.Add(Position, Output);
  22. }
  23. return Output;
  24. }
  25. ABlock Root = Enqueue(Start);
  26. while (Blocks.Count > 0)
  27. {
  28. ABlock Current = Blocks.Dequeue();
  29. FillBlock(Translator.Thread.Memory, Current);
  30. //Set child blocks. "Branch" is the block the branch instruction
  31. //points to (when taken), "Next" is the block at the next address,
  32. //executed when the branch is not taken. For Unconditional Branches
  33. //(except BL/BLR that are sub calls) or end of executable, Next is null.
  34. if (Current.OpCodes.Count > 0)
  35. {
  36. bool HasCachedSub = false;
  37. AOpCode LastOp = Current.GetLastOp();
  38. if (LastOp is AOpCodeBImm Op)
  39. {
  40. if (Op.Emitter == AInstEmit.Bl)
  41. {
  42. HasCachedSub = Translator.HasCachedSub(Op.Imm);
  43. }
  44. else
  45. {
  46. Current.Branch = Enqueue(Op.Imm);
  47. }
  48. }
  49. if ((!(LastOp is AOpCodeBImmAl) &&
  50. !(LastOp is AOpCodeBReg)) || HasCachedSub)
  51. {
  52. Current.Next = Enqueue(Current.EndPosition);
  53. }
  54. }
  55. //If we have on the tree two blocks with the same end position,
  56. //then we need to split the bigger block and have two small blocks,
  57. //the end position of the bigger "Current" block should then be == to
  58. //the position of the "Smaller" block.
  59. while (VisitedEnd.TryGetValue(Current.EndPosition, out ABlock Smaller))
  60. {
  61. if (Current.Position > Smaller.Position)
  62. {
  63. ABlock Temp = Smaller;
  64. Smaller = Current;
  65. Current = Temp;
  66. }
  67. Current.EndPosition = Smaller.Position;
  68. Current.Next = Smaller;
  69. Current.Branch = null;
  70. Current.OpCodes.RemoveRange(
  71. Current.OpCodes.Count - Smaller.OpCodes.Count,
  72. Smaller.OpCodes.Count);
  73. VisitedEnd[Smaller.EndPosition] = Smaller;
  74. }
  75. VisitedEnd.Add(Current.EndPosition, Current);
  76. }
  77. //Make and sort Graph blocks array by position.
  78. ABlock[] Graph = new ABlock[Visited.Count];
  79. while (Visited.Count > 0)
  80. {
  81. ulong FirstPos = ulong.MaxValue;
  82. foreach (ABlock Block in Visited.Values)
  83. {
  84. if (FirstPos > (ulong)Block.Position)
  85. FirstPos = (ulong)Block.Position;
  86. }
  87. ABlock Current = Visited[(long)FirstPos];
  88. do
  89. {
  90. Graph[Graph.Length - Visited.Count] = Current;
  91. Visited.Remove(Current.Position);
  92. Current = Current.Next;
  93. }
  94. while (Current != null);
  95. }
  96. return (Graph, Root);
  97. }
  98. private static void FillBlock(AMemory Memory, ABlock Block)
  99. {
  100. long Position = Block.Position;
  101. AOpCode OpCode;
  102. do
  103. {
  104. OpCode = DecodeOpCode(Memory, Position);
  105. Block.OpCodes.Add(OpCode);
  106. Position += 4;
  107. }
  108. while (!(IsBranch(OpCode) || IsException(OpCode)));
  109. Block.EndPosition = Position;
  110. }
  111. private static bool IsBranch(AOpCode OpCode)
  112. {
  113. return OpCode is AOpCodeBImm ||
  114. OpCode is AOpCodeBReg;
  115. }
  116. private static bool IsException(AOpCode OpCode)
  117. {
  118. return OpCode.Emitter == AInstEmit.Brk ||
  119. OpCode.Emitter == AInstEmit.Svc ||
  120. OpCode.Emitter == AInstEmit.Und;
  121. }
  122. public static AOpCode DecodeOpCode(AMemory Memory, long Position)
  123. {
  124. int OpCode = Memory.ReadInt32(Position);
  125. AInst Inst = AOpCodeTable.GetInst(OpCode);
  126. AOpCode DecodedOpCode = new AOpCode(AInst.Undefined, Position, OpCode);
  127. if (Inst.Type != null)
  128. {
  129. DecodedOpCode = CreateOpCode(Inst.Type, Inst, Position, OpCode);
  130. }
  131. return DecodedOpCode;
  132. }
  133. private delegate object OpActivator(AInst Inst, long Position, int OpCode);
  134. private static Dictionary<Type, OpActivator> Activators = new Dictionary<Type, OpActivator>();
  135. private static AOpCode CreateOpCode(Type Type, AInst Inst, long Position, int OpCode)
  136. {
  137. if (Type == null)
  138. {
  139. throw new ArgumentNullException(nameof(Type));
  140. }
  141. if (!Activators.TryGetValue(Type, out OpActivator CreateInstance))
  142. {
  143. Type[] ArgTypes = new Type[] { typeof(AInst), typeof(long), typeof(int) };
  144. DynamicMethod Mthd = new DynamicMethod($"{Type.Name}_Create", Type, ArgTypes);
  145. ILGenerator Generator = Mthd.GetILGenerator();
  146. Generator.Emit(OpCodes.Ldarg_0);
  147. Generator.Emit(OpCodes.Ldarg_1);
  148. Generator.Emit(OpCodes.Ldarg_2);
  149. Generator.Emit(OpCodes.Newobj, Type.GetConstructor(ArgTypes));
  150. Generator.Emit(OpCodes.Ret);
  151. CreateInstance = (OpActivator)Mthd.CreateDelegate(typeof(OpActivator));
  152. Activators.Add(Type, CreateInstance);
  153. }
  154. return (AOpCode)CreateInstance(Inst, Position, OpCode);
  155. }
  156. }
  157. }