Decoder.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using Ryujinx.Graphics.Shader.Instructions;
  2. using System;
  3. using System.Buffers.Binary;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  7. namespace Ryujinx.Graphics.Shader.Decoders
  8. {
  9. static class Decoder
  10. {
  11. public static Block[] Decode(ReadOnlySpan<byte> code, ulong headerSize)
  12. {
  13. List<Block> blocks = new List<Block>();
  14. Queue<Block> workQueue = new Queue<Block>();
  15. Dictionary<ulong, Block> visited = new Dictionary<ulong, Block>();
  16. ulong maxAddress = (ulong)code.Length - headerSize;
  17. Block GetBlock(ulong blkAddress)
  18. {
  19. if (!visited.TryGetValue(blkAddress, out Block block))
  20. {
  21. block = new Block(blkAddress);
  22. workQueue.Enqueue(block);
  23. visited.Add(blkAddress, block);
  24. }
  25. return block;
  26. }
  27. GetBlock(0);
  28. while (workQueue.TryDequeue(out Block currBlock))
  29. {
  30. // Check if the current block is inside another block.
  31. if (BinarySearch(blocks, currBlock.Address, out int nBlkIndex))
  32. {
  33. Block nBlock = blocks[nBlkIndex];
  34. if (nBlock.Address == currBlock.Address)
  35. {
  36. throw new InvalidOperationException("Found duplicate block address on the list.");
  37. }
  38. nBlock.Split(currBlock);
  39. blocks.Insert(nBlkIndex + 1, currBlock);
  40. continue;
  41. }
  42. // If we have a block after the current one, set the limit address.
  43. ulong limitAddress = maxAddress;
  44. if (nBlkIndex != blocks.Count)
  45. {
  46. Block nBlock = blocks[nBlkIndex];
  47. int nextIndex = nBlkIndex + 1;
  48. if (nBlock.Address < currBlock.Address && nextIndex < blocks.Count)
  49. {
  50. limitAddress = blocks[nextIndex].Address;
  51. }
  52. else if (nBlock.Address > currBlock.Address)
  53. {
  54. limitAddress = blocks[nBlkIndex].Address;
  55. }
  56. }
  57. FillBlock(code, currBlock, limitAddress, headerSize);
  58. if (currBlock.OpCodes.Count != 0)
  59. {
  60. // We should have blocks for all possible branch targets,
  61. // including those from SSY/PBK instructions.
  62. foreach (OpCodePush pushOp in currBlock.PushOpCodes)
  63. {
  64. if (pushOp.GetAbsoluteAddress() >= maxAddress)
  65. {
  66. return null;
  67. }
  68. GetBlock(pushOp.GetAbsoluteAddress());
  69. }
  70. // Set child blocks. "Branch" is the block the branch instruction
  71. // points to (when taken), "Next" is the block at the next address,
  72. // executed when the branch is not taken. For Unconditional Branches
  73. // or end of program, Next is null.
  74. OpCode lastOp = currBlock.GetLastOp();
  75. if (lastOp is OpCodeBranch opBr)
  76. {
  77. if (opBr.GetAbsoluteAddress() >= maxAddress)
  78. {
  79. return null;
  80. }
  81. currBlock.Branch = GetBlock(opBr.GetAbsoluteAddress());
  82. }
  83. else if (lastOp is OpCodeBranchIndir opBrIndir)
  84. {
  85. // An indirect branch could go anywhere, we don't know the target.
  86. // Those instructions are usually used on a switch to jump table
  87. // compiler optimization, and in those cases the possible targets
  88. // seems to be always right after the BRX itself. We can assume
  89. // that the possible targets are all the blocks in-between the
  90. // instruction right after the BRX, and the common target that
  91. // all the "cases" should eventually jump to, acting as the
  92. // switch break.
  93. Block firstTarget = GetBlock(currBlock.EndAddress);
  94. firstTarget.BrIndir = opBrIndir;
  95. opBrIndir.PossibleTargets.Add(firstTarget);
  96. }
  97. if (!IsUnconditionalBranch(lastOp))
  98. {
  99. currBlock.Next = GetBlock(currBlock.EndAddress);
  100. }
  101. }
  102. // Insert the new block on the list (sorted by address).
  103. if (blocks.Count != 0)
  104. {
  105. Block nBlock = blocks[nBlkIndex];
  106. blocks.Insert(nBlkIndex + (nBlock.Address < currBlock.Address ? 1 : 0), currBlock);
  107. }
  108. else
  109. {
  110. blocks.Add(currBlock);
  111. }
  112. // Do we have a block after the current one?
  113. if (!IsExit(currBlock.GetLastOp()) && currBlock.BrIndir != null && currBlock.EndAddress < maxAddress)
  114. {
  115. bool targetVisited = visited.ContainsKey(currBlock.EndAddress);
  116. Block possibleTarget = GetBlock(currBlock.EndAddress);
  117. currBlock.BrIndir.PossibleTargets.Add(possibleTarget);
  118. if (!targetVisited)
  119. {
  120. possibleTarget.BrIndir = currBlock.BrIndir;
  121. }
  122. }
  123. }
  124. foreach (Block block in blocks.Where(x => x.PushOpCodes.Count != 0))
  125. {
  126. for (int pushOpIndex = 0; pushOpIndex < block.PushOpCodes.Count; pushOpIndex++)
  127. {
  128. PropagatePushOp(visited, block, pushOpIndex);
  129. }
  130. }
  131. return blocks.ToArray();
  132. }
  133. private static bool BinarySearch(List<Block> blocks, ulong address, out int index)
  134. {
  135. index = 0;
  136. int left = 0;
  137. int right = blocks.Count - 1;
  138. while (left <= right)
  139. {
  140. int size = right - left;
  141. int middle = left + (size >> 1);
  142. Block block = blocks[middle];
  143. index = middle;
  144. if (address >= block.Address && address < block.EndAddress)
  145. {
  146. return true;
  147. }
  148. if (address < block.Address)
  149. {
  150. right = middle - 1;
  151. }
  152. else
  153. {
  154. left = middle + 1;
  155. }
  156. }
  157. return false;
  158. }
  159. private static void FillBlock(
  160. ReadOnlySpan<byte> code,
  161. Block block,
  162. ulong limitAddress,
  163. ulong startAddress)
  164. {
  165. ulong address = block.Address;
  166. do
  167. {
  168. if (address + 7 >= limitAddress)
  169. {
  170. break;
  171. }
  172. // Ignore scheduling instructions, which are written every 32 bytes.
  173. if ((address & 0x1f) == 0)
  174. {
  175. address += 8;
  176. continue;
  177. }
  178. uint word0 = BinaryPrimitives.ReadUInt32LittleEndian(code.Slice((int)(startAddress + address)));
  179. uint word1 = BinaryPrimitives.ReadUInt32LittleEndian(code.Slice((int)(startAddress + address + 4)));
  180. ulong opAddress = address;
  181. address += 8;
  182. long opCode = word0 | (long)word1 << 32;
  183. (InstEmitter emitter, OpCodeTable.OpActivator opActivator) = OpCodeTable.GetEmitter(opCode);
  184. if (emitter == null)
  185. {
  186. // TODO: Warning, illegal encoding.
  187. block.OpCodes.Add(new OpCode(null, opAddress, opCode));
  188. continue;
  189. }
  190. if (opActivator == null)
  191. {
  192. throw new ArgumentNullException(nameof(opActivator));
  193. }
  194. OpCode op = (OpCode)opActivator(emitter, opAddress, opCode);
  195. block.OpCodes.Add(op);
  196. }
  197. while (!IsBranch(block.GetLastOp()));
  198. block.EndAddress = address;
  199. block.UpdatePushOps();
  200. }
  201. private static bool IsUnconditionalBranch(OpCode opCode)
  202. {
  203. return IsUnconditional(opCode) && IsBranch(opCode);
  204. }
  205. private static bool IsUnconditional(OpCode opCode)
  206. {
  207. if (opCode is OpCodeExit op && op.Condition != Condition.Always)
  208. {
  209. return false;
  210. }
  211. return opCode.Predicate.Index == RegisterConsts.PredicateTrueIndex && !opCode.InvertPredicate;
  212. }
  213. private static bool IsBranch(OpCode opCode)
  214. {
  215. return (opCode is OpCodeBranch opBranch && !opBranch.PushTarget) ||
  216. opCode is OpCodeBranchIndir ||
  217. opCode is OpCodeBranchPop ||
  218. opCode is OpCodeExit;
  219. }
  220. private static bool IsExit(OpCode opCode)
  221. {
  222. return opCode is OpCodeExit;
  223. }
  224. private struct PathBlockState
  225. {
  226. public Block Block { get; }
  227. private enum RestoreType
  228. {
  229. None,
  230. PopPushOp,
  231. PushBranchOp
  232. }
  233. private RestoreType _restoreType;
  234. private ulong _restoreValue;
  235. public bool ReturningFromVisit => _restoreType != RestoreType.None;
  236. public PathBlockState(Block block)
  237. {
  238. Block = block;
  239. _restoreType = RestoreType.None;
  240. _restoreValue = 0;
  241. }
  242. public PathBlockState(int oldStackSize)
  243. {
  244. Block = null;
  245. _restoreType = RestoreType.PopPushOp;
  246. _restoreValue = (ulong)oldStackSize;
  247. }
  248. public PathBlockState(ulong syncAddress)
  249. {
  250. Block = null;
  251. _restoreType = RestoreType.PushBranchOp;
  252. _restoreValue = syncAddress;
  253. }
  254. public void RestoreStackState(Stack<ulong> branchStack)
  255. {
  256. if (_restoreType == RestoreType.PushBranchOp)
  257. {
  258. branchStack.Push(_restoreValue);
  259. }
  260. else if (_restoreType == RestoreType.PopPushOp)
  261. {
  262. while (branchStack.Count > (uint)_restoreValue)
  263. {
  264. branchStack.Pop();
  265. }
  266. }
  267. }
  268. }
  269. private static void PropagatePushOp(Dictionary<ulong, Block> blocks, Block currBlock, int pushOpIndex)
  270. {
  271. OpCodePush pushOp = currBlock.PushOpCodes[pushOpIndex];
  272. Stack<PathBlockState> workQueue = new Stack<PathBlockState>();
  273. HashSet<Block> visited = new HashSet<Block>();
  274. Stack<ulong> branchStack = new Stack<ulong>();
  275. void Push(PathBlockState pbs)
  276. {
  277. // When block is null, this means we are pushing a restore operation.
  278. // Restore operations are used to undo the work done inside a block
  279. // when we return from it, for example it pops addresses pushed by
  280. // SSY/PBK instructions inside the block, and pushes addresses poped
  281. // by SYNC/BRK.
  282. // For blocks, if it's already visited, we just ignore to avoid going
  283. // around in circles and getting stuck here.
  284. if (pbs.Block == null || !visited.Contains(pbs.Block))
  285. {
  286. workQueue.Push(pbs);
  287. }
  288. }
  289. Push(new PathBlockState(currBlock));
  290. while (workQueue.TryPop(out PathBlockState pbs))
  291. {
  292. if (pbs.ReturningFromVisit)
  293. {
  294. pbs.RestoreStackState(branchStack);
  295. continue;
  296. }
  297. Block current = pbs.Block;
  298. // If the block was already processed, we just ignore it, otherwise
  299. // we would push the same child blocks of an already processed block,
  300. // and go around in circles until memory is exhausted.
  301. if (!visited.Add(current))
  302. {
  303. continue;
  304. }
  305. int pushOpsCount = current.PushOpCodes.Count;
  306. if (pushOpsCount != 0)
  307. {
  308. Push(new PathBlockState(branchStack.Count));
  309. for (int index = pushOpIndex; index < pushOpsCount; index++)
  310. {
  311. branchStack.Push(current.PushOpCodes[index].GetAbsoluteAddress());
  312. }
  313. }
  314. pushOpIndex = 0;
  315. if (current.Next != null)
  316. {
  317. Push(new PathBlockState(current.Next));
  318. }
  319. if (current.Branch != null)
  320. {
  321. Push(new PathBlockState(current.Branch));
  322. }
  323. else if (current.GetLastOp() is OpCodeBranchIndir brIndir)
  324. {
  325. // By adding them in descending order (sorted by address), we process the blocks
  326. // in order (of ascending address), since we work with a LIFO.
  327. foreach (Block possibleTarget in brIndir.PossibleTargets.OrderByDescending(x => x.Address))
  328. {
  329. Push(new PathBlockState(possibleTarget));
  330. }
  331. }
  332. else if (current.GetLastOp() is OpCodeBranchPop op)
  333. {
  334. ulong targetAddress = branchStack.Pop();
  335. if (branchStack.Count == 0)
  336. {
  337. branchStack.Push(targetAddress);
  338. op.Targets.Add(pushOp, op.Targets.Count);
  339. pushOp.PopOps.TryAdd(op, Local());
  340. }
  341. else
  342. {
  343. // First we push the target address (this will be used to push the
  344. // address back into the SSY/PBK stack when we return from that block),
  345. // then we push the block itself into the work "queue" (well, it's a stack)
  346. // for processing.
  347. Push(new PathBlockState(targetAddress));
  348. Push(new PathBlockState(blocks[targetAddress]));
  349. }
  350. }
  351. }
  352. }
  353. }
  354. }