ADecoder.cs 6.8 KB

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