ADecoder.cs 7.1 KB

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