ADecoder.cs 7.5 KB

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