ADecoder.cs 7.6 KB

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