ADecoder.cs 6.8 KB

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