OpCodeTable.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using Ryujinx.Graphics.Shader.Instructions;
  2. using System;
  3. using System.Reflection.Emit;
  4. namespace Ryujinx.Graphics.Shader.Decoders
  5. {
  6. static class OpCodeTable
  7. {
  8. public delegate object OpActivator(InstEmitter emitter, ulong address, long opCode);
  9. private const int EncodingBits = 14;
  10. private class TableEntry
  11. {
  12. public InstEmitter Emitter { get; }
  13. public Type OpCodeType { get; }
  14. public int XBits { get; }
  15. public OpActivator OpActivator { get; }
  16. public TableEntry(InstEmitter emitter, Type opCodeType, int xBits)
  17. {
  18. Emitter = emitter;
  19. OpCodeType = opCodeType;
  20. XBits = xBits;
  21. OpActivator = CacheOpActivator(opCodeType);
  22. }
  23. private static OpActivator CacheOpActivator(Type type)
  24. {
  25. Type[] argTypes = new Type[] { typeof(InstEmitter), typeof(ulong), typeof(long) };
  26. DynamicMethod mthd = new DynamicMethod($"Make{type.Name}", type, argTypes);
  27. ILGenerator generator = mthd.GetILGenerator();
  28. generator.Emit(OpCodes.Ldarg_0);
  29. generator.Emit(OpCodes.Ldarg_1);
  30. generator.Emit(OpCodes.Ldarg_2);
  31. generator.Emit(OpCodes.Newobj, type.GetConstructor(argTypes));
  32. generator.Emit(OpCodes.Ret);
  33. return (OpActivator)mthd.CreateDelegate(typeof(OpActivator));
  34. }
  35. }
  36. private static TableEntry[] _opCodes;
  37. static OpCodeTable()
  38. {
  39. _opCodes = new TableEntry[1 << EncodingBits];
  40. #region Instructions
  41. Set("1110111111011x", InstEmit.Ald, typeof(OpCodeAttribute));
  42. Set("1110111111110x", InstEmit.Ast, typeof(OpCodeAttribute));
  43. Set("11101100xxxxxx", InstEmit.Atoms, typeof(OpCodeAtom));
  44. Set("1111000010101x", InstEmit.Bar, typeof(OpCodeBarrier));
  45. Set("0100110000000x", InstEmit.Bfe, typeof(OpCodeAluCbuf));
  46. Set("0011100x00000x", InstEmit.Bfe, typeof(OpCodeAluImm));
  47. Set("0101110000000x", InstEmit.Bfe, typeof(OpCodeAluReg));
  48. Set("0100101111110x", InstEmit.Bfi, typeof(OpCodeAluCbuf));
  49. Set("0011011x11110x", InstEmit.Bfi, typeof(OpCodeAluImm));
  50. Set("0101001111110x", InstEmit.Bfi, typeof(OpCodeAluRegCbuf));
  51. Set("0101101111110x", InstEmit.Bfi, typeof(OpCodeAluReg));
  52. Set("111000100100xx", InstEmit.Bra, typeof(OpCodeBranch));
  53. Set("111000110100xx", InstEmit.Brk, typeof(OpCodeBranchPop));
  54. Set("111000100101xx", InstEmit.Brx, typeof(OpCodeBranchIndir));
  55. Set("0101000010100x", InstEmit.Csetp, typeof(OpCodePset));
  56. Set("0100110001110x", InstEmit.Dadd, typeof(OpCodeFArithCbuf));
  57. Set("0011100x01110x", InstEmit.Dadd, typeof(OpCodeDArithImm));
  58. Set("0101110001110x", InstEmit.Dadd, typeof(OpCodeFArithReg));
  59. Set("1111000011110x", InstEmit.Depbar, typeof(OpCode));
  60. Set("010010110111xx", InstEmit.Dfma, typeof(OpCodeFArithCbuf));
  61. Set("0011011x0111xx", InstEmit.Dfma, typeof(OpCodeDArithImm));
  62. Set("010100110111xx", InstEmit.Dfma, typeof(OpCodeFArithRegCbuf));
  63. Set("010110110111xx", InstEmit.Dfma, typeof(OpCodeFArithReg));
  64. Set("0100110010000x", InstEmit.Dmul, typeof(OpCodeFArithCbuf));
  65. Set("0011100x10000x", InstEmit.Dmul, typeof(OpCodeDArithImm));
  66. Set("0101110010000x", InstEmit.Dmul, typeof(OpCodeFArithReg));
  67. Set("111000110000xx", InstEmit.Exit, typeof(OpCodeExit));
  68. Set("0100110010101x", InstEmit.F2F, typeof(OpCodeFArithCbuf));
  69. Set("0011100x10101x", InstEmit.F2F, typeof(OpCodeFArithImm));
  70. Set("0101110010101x", InstEmit.F2F, typeof(OpCodeFArithReg));
  71. Set("0100110010110x", InstEmit.F2I, typeof(OpCodeFArithCbuf));
  72. Set("0011100x10110x", InstEmit.F2I, typeof(OpCodeFArithImm));
  73. Set("0101110010110x", InstEmit.F2I, typeof(OpCodeFArithReg));
  74. Set("0100110001011x", InstEmit.Fadd, typeof(OpCodeFArithCbuf));
  75. Set("0011100x01011x", InstEmit.Fadd, typeof(OpCodeFArithImm));
  76. Set("000010xxxxxxxx", InstEmit.Fadd, typeof(OpCodeFArithImm32));
  77. Set("0101110001011x", InstEmit.Fadd, typeof(OpCodeFArithReg));
  78. Set("010010111010xx", InstEmit.Fcmp, typeof(OpCodeFArithCbuf));
  79. Set("0011011x1010xx", InstEmit.Fcmp, typeof(OpCodeFArithImm));
  80. Set("010110111010xx", InstEmit.Fcmp, typeof(OpCodeFArithReg));
  81. Set("010100111010xx", InstEmit.Fcmp, typeof(OpCodeFArithRegCbuf));
  82. Set("010010011xxxxx", InstEmit.Ffma, typeof(OpCodeFArithCbuf));
  83. Set("0011001x1xxxxx", InstEmit.Ffma, typeof(OpCodeFArithImm));
  84. Set("000011xxxxxxxx", InstEmit.Ffma32i, typeof(OpCodeFArithImm32));
  85. Set("010100011xxxxx", InstEmit.Ffma, typeof(OpCodeFArithRegCbuf));
  86. Set("010110011xxxxx", InstEmit.Ffma, typeof(OpCodeFArithReg));
  87. Set("0100110000110x", InstEmit.Flo, typeof(OpCodeAluCbuf));
  88. Set("0011100x00110x", InstEmit.Flo, typeof(OpCodeAluImm));
  89. Set("0101110000110x", InstEmit.Flo, typeof(OpCodeAluReg));
  90. Set("0100110001100x", InstEmit.Fmnmx, typeof(OpCodeFArithCbuf));
  91. Set("0011100x01100x", InstEmit.Fmnmx, typeof(OpCodeFArithImm));
  92. Set("0101110001100x", InstEmit.Fmnmx, typeof(OpCodeFArithReg));
  93. Set("0100110001101x", InstEmit.Fmul, typeof(OpCodeFArithCbuf));
  94. Set("0011100x01101x", InstEmit.Fmul, typeof(OpCodeFArithImm));
  95. Set("00011110xxxxxx", InstEmit.Fmul, typeof(OpCodeFArithImm32));
  96. Set("0101110001101x", InstEmit.Fmul, typeof(OpCodeFArithReg));
  97. Set("0100100xxxxxxx", InstEmit.Fset, typeof(OpCodeSetCbuf));
  98. Set("0011000xxxxxxx", InstEmit.Fset, typeof(OpCodeFsetImm));
  99. Set("01011000xxxxxx", InstEmit.Fset, typeof(OpCodeSetReg));
  100. Set("010010111011xx", InstEmit.Fsetp, typeof(OpCodeSetCbuf));
  101. Set("0011011x1011xx", InstEmit.Fsetp, typeof(OpCodeFsetImm));
  102. Set("010110111011xx", InstEmit.Fsetp, typeof(OpCodeSetReg));
  103. Set("0101000011111x", InstEmit.Fswzadd, typeof(OpCodeAluReg));
  104. Set("0111101x1xxxxx", InstEmit.Hadd2, typeof(OpCodeAluCbuf));
  105. Set("0111101x0xxxxx", InstEmit.Hadd2, typeof(OpCodeAluImm2x10));
  106. Set("0010110xxxxxxx", InstEmit.Hadd2, typeof(OpCodeAluImm32));
  107. Set("0101110100010x", InstEmit.Hadd2, typeof(OpCodeAluReg));
  108. Set("01110xxx1xxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaCbuf));
  109. Set("01110xxx0xxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaImm2x10));
  110. Set("0010100xxxxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaImm32));
  111. Set("0101110100000x", InstEmit.Hfma2, typeof(OpCodeHfmaReg));
  112. Set("01100xxx1xxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaRegCbuf));
  113. Set("0111100x1xxxxx", InstEmit.Hmul2, typeof(OpCodeAluCbuf));
  114. Set("0111100x0xxxxx", InstEmit.Hmul2, typeof(OpCodeAluImm2x10));
  115. Set("0010101xxxxxxx", InstEmit.Hmul2, typeof(OpCodeAluImm32));
  116. Set("0101110100001x", InstEmit.Hmul2, typeof(OpCodeAluReg));
  117. Set("0111110x1xxxxx", InstEmit.Hset2, typeof(OpCodeSetCbuf));
  118. Set("0111110x0xxxxx", InstEmit.Hset2, typeof(OpCodeHsetImm2x10));
  119. Set("0101110100011x", InstEmit.Hset2, typeof(OpCodeSetReg));
  120. Set("0111111x1xxxxx", InstEmit.Hsetp2, typeof(OpCodeSetCbuf));
  121. Set("0111111x0xxxxx", InstEmit.Hsetp2, typeof(OpCodeHsetImm2x10));
  122. Set("0101110100100x", InstEmit.Hsetp2, typeof(OpCodeSetReg));
  123. Set("0100110010111x", InstEmit.I2F, typeof(OpCodeAluCbuf));
  124. Set("0011100x10111x", InstEmit.I2F, typeof(OpCodeAluImm));
  125. Set("0101110010111x", InstEmit.I2F, typeof(OpCodeAluReg));
  126. Set("0100110011100x", InstEmit.I2I, typeof(OpCodeAluCbuf));
  127. Set("0011100x11100x", InstEmit.I2I, typeof(OpCodeAluImm));
  128. Set("0101110011100x", InstEmit.I2I, typeof(OpCodeAluReg));
  129. Set("0100110000010x", InstEmit.Iadd, typeof(OpCodeAluCbuf));
  130. Set("0011100x00010x", InstEmit.Iadd, typeof(OpCodeAluImm));
  131. Set("0001110x0xxxxx", InstEmit.Iadd, typeof(OpCodeAluImm32));
  132. Set("0101110000010x", InstEmit.Iadd, typeof(OpCodeAluReg));
  133. Set("010011001100xx", InstEmit.Iadd3, typeof(OpCodeAluCbuf));
  134. Set("0011100x1100xx", InstEmit.Iadd3, typeof(OpCodeAluImm));
  135. Set("010111001100xx", InstEmit.Iadd3, typeof(OpCodeAluReg));
  136. Set("010010110100xx", InstEmit.Icmp, typeof(OpCodeAluCbuf));
  137. Set("0011011x0100xx", InstEmit.Icmp, typeof(OpCodeAluImm));
  138. Set("010110110100xx", InstEmit.Icmp, typeof(OpCodeAluReg));
  139. Set("010100110100xx", InstEmit.Icmp, typeof(OpCodeAluRegCbuf));
  140. Set("010010100xxxxx", InstEmit.Imad, typeof(OpCodeAluCbuf));
  141. Set("0011010x0xxxxx", InstEmit.Imad, typeof(OpCodeAluImm));
  142. Set("010110100xxxxx", InstEmit.Imad, typeof(OpCodeAluReg));
  143. Set("010100100xxxxx", InstEmit.Imad, typeof(OpCodeAluRegCbuf));
  144. Set("0100110000100x", InstEmit.Imnmx, typeof(OpCodeAluCbuf));
  145. Set("0011100x00100x", InstEmit.Imnmx, typeof(OpCodeAluImm));
  146. Set("0101110000100x", InstEmit.Imnmx, typeof(OpCodeAluReg));
  147. Set("11100000xxxxxx", InstEmit.Ipa, typeof(OpCodeIpa));
  148. Set("1110111111010x", InstEmit.Isberd, typeof(OpCodeAlu));
  149. Set("0100110000011x", InstEmit.Iscadd, typeof(OpCodeAluCbuf));
  150. Set("0011100x00011x", InstEmit.Iscadd, typeof(OpCodeAluImm));
  151. Set("000101xxxxxxxx", InstEmit.Iscadd, typeof(OpCodeAluImm32));
  152. Set("0101110000011x", InstEmit.Iscadd, typeof(OpCodeAluReg));
  153. Set("010010110101xx", InstEmit.Iset, typeof(OpCodeSetCbuf));
  154. Set("0011011x0101xx", InstEmit.Iset, typeof(OpCodeSetImm));
  155. Set("010110110101xx", InstEmit.Iset, typeof(OpCodeSetReg));
  156. Set("010010110110xx", InstEmit.Isetp, typeof(OpCodeSetCbuf));
  157. Set("0011011x0110xx", InstEmit.Isetp, typeof(OpCodeSetImm));
  158. Set("010110110110xx", InstEmit.Isetp, typeof(OpCodeSetReg));
  159. Set("111000110011xx", InstEmit.Kil, typeof(OpCodeExit));
  160. Set("1110111101000x", InstEmit.Ld, typeof(OpCodeMemory));
  161. Set("1110111110010x", InstEmit.Ldc, typeof(OpCodeLdc));
  162. Set("1110111011010x", InstEmit.Ldg, typeof(OpCodeMemory));
  163. Set("1110111101001x", InstEmit.Lds, typeof(OpCodeMemory));
  164. Set("010010111101xx", InstEmit.Lea, typeof(OpCodeAluCbuf));
  165. Set("0011011x11010x", InstEmit.Lea, typeof(OpCodeAluImm));
  166. Set("0101101111010x", InstEmit.Lea, typeof(OpCodeAluReg));
  167. Set("0100110001000x", InstEmit.Lop, typeof(OpCodeLopCbuf));
  168. Set("0011100001000x", InstEmit.Lop, typeof(OpCodeLopImm));
  169. Set("000001xxxxxxxx", InstEmit.Lop, typeof(OpCodeLopImm32));
  170. Set("0101110001000x", InstEmit.Lop, typeof(OpCodeLopReg));
  171. Set("0010000xxxxxxx", InstEmit.Lop3, typeof(OpCodeLopCbuf));
  172. Set("001111xxxxxxxx", InstEmit.Lop3, typeof(OpCodeLopImm));
  173. Set("0101101111100x", InstEmit.Lop3, typeof(OpCodeLopReg));
  174. Set("1110111110011x", InstEmit.Membar, typeof(OpCodeMemoryBarrier));
  175. Set("0100110010011x", InstEmit.Mov, typeof(OpCodeAluCbuf));
  176. Set("0011100x10011x", InstEmit.Mov, typeof(OpCodeAluImm));
  177. Set("000000010000xx", InstEmit.Mov, typeof(OpCodeAluImm32));
  178. Set("0101110010011x", InstEmit.Mov, typeof(OpCodeAluReg));
  179. Set("0101000010000x", InstEmit.Mufu, typeof(OpCodeFArith));
  180. Set("0101000010110x", InstEmit.Nop, typeof(OpCode));
  181. Set("1111101111100x", InstEmit.Out, typeof(OpCode));
  182. Set("111000101010xx", InstEmit.Pbk, typeof(OpCodePush));
  183. Set("0100110000001x", InstEmit.Popc, typeof(OpCodeAluCbuf));
  184. Set("0011100x00001x", InstEmit.Popc, typeof(OpCodeAluImm));
  185. Set("0101110000001x", InstEmit.Popc, typeof(OpCodeAluReg));
  186. Set("0101000010001x", InstEmit.Pset, typeof(OpCodePset));
  187. Set("0101000010010x", InstEmit.Psetp, typeof(OpCodePset));
  188. Set("0100110011110x", InstEmit.R2p, typeof(OpCodeAluCbuf));
  189. Set("0011100x11110x", InstEmit.R2p, typeof(OpCodeAluImm));
  190. Set("0101110011110x", InstEmit.R2p, typeof(OpCodeAluReg));
  191. Set("1110101111111x", InstEmit.Red, typeof(OpCodeRed));
  192. Set("0100110010010x", InstEmit.Rro, typeof(OpCodeFArithCbuf));
  193. Set("0011100x10010x", InstEmit.Rro, typeof(OpCodeFArithImm));
  194. Set("0101110010010x", InstEmit.Rro, typeof(OpCodeFArithReg));
  195. Set("1111000011001x", InstEmit.S2r, typeof(OpCodeAlu));
  196. Set("0100110010100x", InstEmit.Sel, typeof(OpCodeAluCbuf));
  197. Set("0011100x10100x", InstEmit.Sel, typeof(OpCodeAluImm));
  198. Set("0101110010100x", InstEmit.Sel, typeof(OpCodeAluReg));
  199. Set("1110111100010x", InstEmit.Shfl, typeof(OpCodeShuffle));
  200. Set("0100110001001x", InstEmit.Shl, typeof(OpCodeAluCbuf));
  201. Set("0011100x01001x", InstEmit.Shl, typeof(OpCodeAluImm));
  202. Set("0101110001001x", InstEmit.Shl, typeof(OpCodeAluReg));
  203. Set("0100110000101x", InstEmit.Shr, typeof(OpCodeAluCbuf));
  204. Set("0011100x00101x", InstEmit.Shr, typeof(OpCodeAluImm));
  205. Set("0101110000101x", InstEmit.Shr, typeof(OpCodeAluReg));
  206. Set("111000101001xx", InstEmit.Ssy, typeof(OpCodePush));
  207. Set("1110111101010x", InstEmit.St, typeof(OpCodeMemory));
  208. Set("1110111011011x", InstEmit.Stg, typeof(OpCodeMemory));
  209. Set("1110111101011x", InstEmit.Sts, typeof(OpCodeMemory));
  210. Set("11101011000xxx", InstEmit.Suld, typeof(OpCodeImage));
  211. Set("11101011001xxx", InstEmit.Sust, typeof(OpCodeImage));
  212. Set("1111000011111x", InstEmit.Sync, typeof(OpCodeBranchPop));
  213. Set("110000xxxx111x", InstEmit.Tex, typeof(OpCodeTex));
  214. Set("1101111010111x", InstEmit.TexB, typeof(OpCodeTexB));
  215. Set("1101x00xxxxxxx", InstEmit.Texs, typeof(OpCodeTexs));
  216. Set("1101x01xxxxxxx", InstEmit.Texs, typeof(OpCodeTlds));
  217. Set("11011111x0xxxx", InstEmit.Texs, typeof(OpCodeTld4s));
  218. Set("11011100xx111x", InstEmit.Tld, typeof(OpCodeTld));
  219. Set("11011101xx111x", InstEmit.TldB, typeof(OpCodeTld));
  220. Set("110010xxxx111x", InstEmit.Tld4, typeof(OpCodeTld4));
  221. Set("1101111011111x", InstEmit.Tld4, typeof(OpCodeTld4B));
  222. Set("11011111011000", InstEmit.TmmlB, typeof(OpCodeTexture));
  223. Set("11011111010110", InstEmit.Tmml, typeof(OpCodeTexture));
  224. Set("110111100x1110", InstEmit.Txd, typeof(OpCodeTxd));
  225. Set("1101111101001x", InstEmit.Txq, typeof(OpCodeTex));
  226. Set("1101111101010x", InstEmit.TxqB, typeof(OpCodeTex));
  227. Set("01011111xxxxxx", InstEmit.Vmad, typeof(OpCodeVideo));
  228. Set("0011101xxxxxxx", InstEmit.Vmnmx, typeof(OpCodeVideo));
  229. Set("0101000011011x", InstEmit.Vote, typeof(OpCodeVote));
  230. Set("0100111xxxxxxx", InstEmit.Xmad, typeof(OpCodeAluCbuf));
  231. Set("0011011x00xxxx", InstEmit.Xmad, typeof(OpCodeAluImm));
  232. Set("010100010xxxxx", InstEmit.Xmad, typeof(OpCodeAluRegCbuf));
  233. Set("0101101100xxxx", InstEmit.Xmad, typeof(OpCodeAluReg));
  234. #endregion
  235. }
  236. private static void Set(string encoding, InstEmitter emitter, Type opCodeType)
  237. {
  238. if (encoding.Length != EncodingBits)
  239. {
  240. throw new ArgumentException(nameof(encoding));
  241. }
  242. int bit = encoding.Length - 1;
  243. int value = 0;
  244. int xMask = 0;
  245. int xBits = 0;
  246. int[] xPos = new int[encoding.Length];
  247. for (int index = 0; index < encoding.Length; index++, bit--)
  248. {
  249. char chr = encoding[index];
  250. if (chr == '1')
  251. {
  252. value |= 1 << bit;
  253. }
  254. else if (chr == 'x')
  255. {
  256. xMask |= 1 << bit;
  257. xPos[xBits++] = bit;
  258. }
  259. }
  260. xMask = ~xMask;
  261. TableEntry entry = new TableEntry(emitter, opCodeType, xBits);
  262. for (int index = 0; index < (1 << xBits); index++)
  263. {
  264. value &= xMask;
  265. for (int x = 0; x < xBits; x++)
  266. {
  267. value |= ((index >> x) & 1) << xPos[x];
  268. }
  269. if (_opCodes[value] == null || _opCodes[value].XBits > xBits)
  270. {
  271. _opCodes[value] = entry;
  272. }
  273. }
  274. }
  275. public static (InstEmitter emitter, OpActivator opActivator) GetEmitter(long opCode)
  276. {
  277. TableEntry entry = _opCodes[(ulong)opCode >> (64 - EncodingBits)];
  278. if (entry != null)
  279. {
  280. return (entry.Emitter, entry.OpActivator);
  281. }
  282. return (null, null);
  283. }
  284. }
  285. }