shader_bytecode.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <bitset>
  6. #include <string>
  7. #include <tuple>
  8. #include <vector>
  9. #include <boost/optional.hpp>
  10. #include "common/assert.h"
  11. #include "common/bit_field.h"
  12. #include "common/common_types.h"
  13. namespace Tegra::Shader {
  14. struct Register {
  15. /// Number of registers
  16. static constexpr std::size_t NumRegisters = 256;
  17. /// Register 255 is special cased to always be 0
  18. static constexpr std::size_t ZeroIndex = 255;
  19. enum class Size : u64 {
  20. Byte = 0,
  21. Short = 1,
  22. Word = 2,
  23. Long = 3,
  24. };
  25. constexpr Register() = default;
  26. constexpr Register(u64 value) : value(value) {}
  27. constexpr operator u64() const {
  28. return value;
  29. }
  30. template <typename T>
  31. constexpr u64 operator-(const T& oth) const {
  32. return value - oth;
  33. }
  34. template <typename T>
  35. constexpr u64 operator&(const T& oth) const {
  36. return value & oth;
  37. }
  38. constexpr u64 operator&(const Register& oth) const {
  39. return value & oth.value;
  40. }
  41. constexpr u64 operator~() const {
  42. return ~value;
  43. }
  44. u64 GetSwizzledIndex(u64 elem) const {
  45. elem = (value + elem) & 3;
  46. return (value & ~3) + elem;
  47. }
  48. private:
  49. u64 value{};
  50. };
  51. enum class AttributeSize : u64 {
  52. Word = 0,
  53. DoubleWord = 1,
  54. TripleWord = 2,
  55. QuadWord = 3,
  56. };
  57. union Attribute {
  58. Attribute() = default;
  59. constexpr explicit Attribute(u64 value) : value(value) {}
  60. enum class Index : u64 {
  61. Position = 7,
  62. Attribute_0 = 8,
  63. Attribute_31 = 39,
  64. PointCoord = 46,
  65. // This attribute contains a tuple of (~, ~, InstanceId, VertexId) when inside a vertex
  66. // shader, and a tuple of (TessCoord.x, TessCoord.y, TessCoord.z, ~) when inside a Tess Eval
  67. // shader.
  68. TessCoordInstanceIDVertexID = 47,
  69. // This attribute contains a tuple of (Unk, Unk, Unk, gl_FrontFacing) when inside a fragment
  70. // shader. It is unknown what the other values contain.
  71. FrontFacing = 63,
  72. };
  73. union {
  74. BitField<20, 10, u64> immediate;
  75. BitField<22, 2, u64> element;
  76. BitField<24, 6, Index> index;
  77. BitField<47, 3, AttributeSize> size;
  78. } fmt20;
  79. union {
  80. BitField<30, 2, u64> element;
  81. BitField<32, 6, Index> index;
  82. } fmt28;
  83. BitField<39, 8, u64> reg;
  84. u64 value{};
  85. };
  86. union Sampler {
  87. Sampler() = default;
  88. constexpr explicit Sampler(u64 value) : value(value) {}
  89. enum class Index : u64 {
  90. Sampler_0 = 8,
  91. };
  92. BitField<36, 13, Index> index;
  93. u64 value{};
  94. };
  95. } // namespace Tegra::Shader
  96. namespace std {
  97. // TODO(bunnei): The below is forbidden by the C++ standard, but works fine. See #330.
  98. template <>
  99. struct make_unsigned<Tegra::Shader::Attribute> {
  100. using type = Tegra::Shader::Attribute;
  101. };
  102. template <>
  103. struct make_unsigned<Tegra::Shader::Register> {
  104. using type = Tegra::Shader::Register;
  105. };
  106. } // namespace std
  107. namespace Tegra::Shader {
  108. enum class Pred : u64 {
  109. UnusedIndex = 0x7,
  110. NeverExecute = 0xF,
  111. };
  112. enum class PredCondition : u64 {
  113. LessThan = 1,
  114. Equal = 2,
  115. LessEqual = 3,
  116. GreaterThan = 4,
  117. NotEqual = 5,
  118. GreaterEqual = 6,
  119. LessThanWithNan = 9,
  120. GreaterThanWithNan = 12,
  121. NotEqualWithNan = 13,
  122. GreaterEqualWithNan = 14,
  123. // TODO(Subv): Other condition types
  124. };
  125. enum class PredOperation : u64 {
  126. And = 0,
  127. Or = 1,
  128. Xor = 2,
  129. };
  130. enum class LogicOperation : u64 {
  131. And = 0,
  132. Or = 1,
  133. Xor = 2,
  134. PassB = 3,
  135. };
  136. enum class SubOp : u64 {
  137. Cos = 0x0,
  138. Sin = 0x1,
  139. Ex2 = 0x2,
  140. Lg2 = 0x3,
  141. Rcp = 0x4,
  142. Rsq = 0x5,
  143. Sqrt = 0x8,
  144. };
  145. enum class F2iRoundingOp : u64 {
  146. None = 0,
  147. Floor = 1,
  148. Ceil = 2,
  149. Trunc = 3,
  150. };
  151. enum class F2fRoundingOp : u64 {
  152. None = 0,
  153. Pass = 3,
  154. Round = 8,
  155. Floor = 9,
  156. Ceil = 10,
  157. Trunc = 11,
  158. };
  159. enum class UniformType : u64 {
  160. UnsignedByte = 0,
  161. SignedByte = 1,
  162. UnsignedShort = 2,
  163. SignedShort = 3,
  164. Single = 4,
  165. Double = 5,
  166. };
  167. enum class IMinMaxExchange : u64 {
  168. None = 0,
  169. XLo = 1,
  170. XMed = 2,
  171. XHi = 3,
  172. };
  173. enum class VmadType : u64 {
  174. Size16_Low = 0,
  175. Size16_High = 1,
  176. Size32 = 2,
  177. Invalid = 3,
  178. };
  179. enum class VmadShr : u64 {
  180. Shr7 = 1,
  181. Shr15 = 2,
  182. };
  183. enum class XmadMode : u64 {
  184. None = 0,
  185. CLo = 1,
  186. CHi = 2,
  187. CSfu = 3,
  188. CBcc = 4,
  189. };
  190. enum class IAdd3Mode : u64 {
  191. None = 0,
  192. RightShift = 1,
  193. LeftShift = 2,
  194. };
  195. enum class IAdd3Height : u64 {
  196. None = 0,
  197. LowerHalfWord = 1,
  198. UpperHalfWord = 2,
  199. };
  200. enum class FlowCondition : u64 {
  201. Always = 0xF,
  202. Fcsm_Tr = 0x1C, // TODO(bunnei): What is this used for?
  203. };
  204. enum class ControlCode : u64 {
  205. F = 0,
  206. LT = 1,
  207. EQ = 2,
  208. LE = 3,
  209. GT = 4,
  210. NE = 5,
  211. GE = 6,
  212. Num = 7,
  213. Nan = 8,
  214. LTU = 9,
  215. EQU = 10,
  216. LEU = 11,
  217. GTU = 12,
  218. NEU = 13,
  219. GEU = 14,
  220. //
  221. OFF = 16,
  222. LO = 17,
  223. SFF = 18,
  224. LS = 19,
  225. HI = 20,
  226. SFT = 21,
  227. HS = 22,
  228. OFT = 23,
  229. CSM_TA = 24,
  230. CSM_TR = 25,
  231. CSM_MX = 26,
  232. FCSM_TA = 27,
  233. FCSM_TR = 28,
  234. FCSM_MX = 29,
  235. RLE = 30,
  236. RGT = 31,
  237. };
  238. enum class PredicateResultMode : u64 {
  239. None = 0x0,
  240. NotZero = 0x3,
  241. };
  242. enum class TextureType : u64 {
  243. Texture1D = 0,
  244. Texture2D = 1,
  245. Texture3D = 2,
  246. TextureCube = 3,
  247. };
  248. enum class TextureQueryType : u64 {
  249. Dimension = 1,
  250. TextureType = 2,
  251. SamplePosition = 5,
  252. Filter = 16,
  253. LevelOfDetail = 18,
  254. Wrap = 20,
  255. BorderColor = 22,
  256. };
  257. enum class TextureProcessMode : u64 {
  258. None = 0,
  259. LZ = 1, // Unknown, appears to be the same as none.
  260. LB = 2, // Load Bias.
  261. LL = 3, // Load LOD (LevelOfDetail)
  262. LBA = 6, // Load Bias. The A is unknown, does not appear to differ with LB
  263. LLA = 7 // Load LOD. The A is unknown, does not appear to differ with LL
  264. };
  265. enum class TextureMiscMode : u64 {
  266. DC,
  267. AOFFI, // Uses Offset
  268. NDV,
  269. NODEP,
  270. MZ,
  271. PTP,
  272. };
  273. enum class IsberdMode : u64 {
  274. None = 0,
  275. Patch = 1,
  276. Prim = 2,
  277. Attr = 3,
  278. };
  279. enum class IsberdShift : u64 { None = 0, U16 = 1, B32 = 2 };
  280. enum class HalfType : u64 {
  281. H0_H1 = 0,
  282. F32 = 1,
  283. H0_H0 = 2,
  284. H1_H1 = 3,
  285. };
  286. enum class HalfMerge : u64 {
  287. H0_H1 = 0,
  288. F32 = 1,
  289. Mrg_H0 = 2,
  290. Mrg_H1 = 3,
  291. };
  292. enum class HalfPrecision : u64 {
  293. None = 0,
  294. FTZ = 1,
  295. FMZ = 2,
  296. };
  297. enum class IpaInterpMode : u64 {
  298. Linear = 0,
  299. Perspective = 1,
  300. Flat = 2,
  301. Sc = 3,
  302. };
  303. enum class IpaSampleMode : u64 {
  304. Default = 0,
  305. Centroid = 1,
  306. Offset = 2,
  307. };
  308. struct IpaMode {
  309. IpaInterpMode interpolation_mode;
  310. IpaSampleMode sampling_mode;
  311. bool operator==(const IpaMode& a) const {
  312. return std::tie(interpolation_mode, sampling_mode) ==
  313. std::tie(a.interpolation_mode, a.sampling_mode);
  314. }
  315. bool operator!=(const IpaMode& a) const {
  316. return !operator==(a);
  317. }
  318. };
  319. enum class SystemVariable : u64 {
  320. LaneId = 0x00,
  321. VirtCfg = 0x02,
  322. VirtId = 0x03,
  323. Pm0 = 0x04,
  324. Pm1 = 0x05,
  325. Pm2 = 0x06,
  326. Pm3 = 0x07,
  327. Pm4 = 0x08,
  328. Pm5 = 0x09,
  329. Pm6 = 0x0a,
  330. Pm7 = 0x0b,
  331. OrderingTicket = 0x0f,
  332. PrimType = 0x10,
  333. InvocationId = 0x11,
  334. Ydirection = 0x12,
  335. ThreadKill = 0x13,
  336. ShaderType = 0x14,
  337. DirectBeWriteAddressLow = 0x15,
  338. DirectBeWriteAddressHigh = 0x16,
  339. DirectBeWriteEnabled = 0x17,
  340. MachineId0 = 0x18,
  341. MachineId1 = 0x19,
  342. MachineId2 = 0x1a,
  343. MachineId3 = 0x1b,
  344. Affinity = 0x1c,
  345. InvocationInfo = 0x1d,
  346. WscaleFactorXY = 0x1e,
  347. WscaleFactorZ = 0x1f,
  348. Tid = 0x20,
  349. TidX = 0x21,
  350. TidY = 0x22,
  351. TidZ = 0x23,
  352. CtaParam = 0x24,
  353. CtaIdX = 0x25,
  354. CtaIdY = 0x26,
  355. CtaIdZ = 0x27,
  356. NtId = 0x28,
  357. CirQueueIncrMinusOne = 0x29,
  358. Nlatc = 0x2a,
  359. SmSpaVersion = 0x2c,
  360. MultiPassShaderInfo = 0x2d,
  361. LwinHi = 0x2e,
  362. SwinHi = 0x2f,
  363. SwinLo = 0x30,
  364. SwinSz = 0x31,
  365. SmemSz = 0x32,
  366. SmemBanks = 0x33,
  367. LwinLo = 0x34,
  368. LwinSz = 0x35,
  369. LmemLosz = 0x36,
  370. LmemHioff = 0x37,
  371. EqMask = 0x38,
  372. LtMask = 0x39,
  373. LeMask = 0x3a,
  374. GtMask = 0x3b,
  375. GeMask = 0x3c,
  376. RegAlloc = 0x3d,
  377. CtxAddr = 0x3e, // .fmask = F_SM50
  378. BarrierAlloc = 0x3e, // .fmask = F_SM60
  379. GlobalErrorStatus = 0x40,
  380. WarpErrorStatus = 0x42,
  381. WarpErrorStatusClear = 0x43,
  382. PmHi0 = 0x48,
  383. PmHi1 = 0x49,
  384. PmHi2 = 0x4a,
  385. PmHi3 = 0x4b,
  386. PmHi4 = 0x4c,
  387. PmHi5 = 0x4d,
  388. PmHi6 = 0x4e,
  389. PmHi7 = 0x4f,
  390. ClockLo = 0x50,
  391. ClockHi = 0x51,
  392. GlobalTimerLo = 0x52,
  393. GlobalTimerHi = 0x53,
  394. HwTaskId = 0x60,
  395. CircularQueueEntryIndex = 0x61,
  396. CircularQueueEntryAddressLow = 0x62,
  397. CircularQueueEntryAddressHigh = 0x63,
  398. };
  399. union Instruction {
  400. Instruction& operator=(const Instruction& instr) {
  401. value = instr.value;
  402. return *this;
  403. }
  404. constexpr Instruction(u64 value) : value{value} {}
  405. BitField<0, 8, Register> gpr0;
  406. BitField<8, 8, Register> gpr8;
  407. union {
  408. BitField<16, 4, Pred> full_pred;
  409. BitField<16, 3, u64> pred_index;
  410. } pred;
  411. BitField<19, 1, u64> negate_pred;
  412. BitField<20, 8, Register> gpr20;
  413. BitField<20, 4, SubOp> sub_op;
  414. BitField<28, 8, Register> gpr28;
  415. BitField<39, 8, Register> gpr39;
  416. BitField<48, 16, u64> opcode;
  417. union {
  418. BitField<20, 16, u64> imm20_16;
  419. BitField<20, 19, u64> imm20_19;
  420. BitField<20, 32, s64> imm20_32;
  421. BitField<45, 1, u64> negate_b;
  422. BitField<46, 1, u64> abs_a;
  423. BitField<48, 1, u64> negate_a;
  424. BitField<49, 1, u64> abs_b;
  425. BitField<50, 1, u64> saturate_d;
  426. BitField<56, 1, u64> negate_imm;
  427. union {
  428. BitField<39, 3, u64> pred;
  429. BitField<42, 1, u64> negate_pred;
  430. } fmnmx;
  431. union {
  432. BitField<39, 1, u64> invert_a;
  433. BitField<40, 1, u64> invert_b;
  434. BitField<41, 2, LogicOperation> operation;
  435. BitField<44, 2, PredicateResultMode> pred_result_mode;
  436. BitField<48, 3, Pred> pred48;
  437. } lop;
  438. union {
  439. BitField<53, 2, LogicOperation> operation;
  440. BitField<55, 1, u64> invert_a;
  441. BitField<56, 1, u64> invert_b;
  442. } lop32i;
  443. union {
  444. BitField<28, 8, u64> imm_lut28;
  445. BitField<48, 8, u64> imm_lut48;
  446. u32 GetImmLut28() const {
  447. return static_cast<u32>(imm_lut28);
  448. }
  449. u32 GetImmLut48() const {
  450. return static_cast<u32>(imm_lut48);
  451. }
  452. } lop3;
  453. u16 GetImm20_16() const {
  454. return static_cast<u16>(imm20_16);
  455. }
  456. u32 GetImm20_19() const {
  457. u32 imm{static_cast<u32>(imm20_19)};
  458. imm <<= 12;
  459. imm |= negate_imm ? 0x80000000 : 0;
  460. return imm;
  461. }
  462. u32 GetImm20_32() const {
  463. return static_cast<u32>(imm20_32);
  464. }
  465. s32 GetSignedImm20_20() const {
  466. u32 immediate = static_cast<u32>(imm20_19 | (negate_imm << 19));
  467. // Sign extend the 20-bit value.
  468. u32 mask = 1U << (20 - 1);
  469. return static_cast<s32>((immediate ^ mask) - mask);
  470. }
  471. } alu;
  472. union {
  473. BitField<51, 1, u64> saturate;
  474. BitField<52, 2, IpaSampleMode> sample_mode;
  475. BitField<54, 2, IpaInterpMode> interp_mode;
  476. } ipa;
  477. union {
  478. BitField<39, 2, u64> tab5cb8_2;
  479. BitField<41, 3, u64> tab5c68_1;
  480. BitField<44, 2, u64> tab5c68_0;
  481. BitField<47, 1, u64> cc;
  482. BitField<48, 1, u64> negate_b;
  483. } fmul;
  484. union {
  485. BitField<48, 1, u64> is_signed;
  486. } shift;
  487. union {
  488. BitField<39, 5, u64> shift_amount;
  489. BitField<48, 1, u64> negate_b;
  490. BitField<49, 1, u64> negate_a;
  491. } alu_integer;
  492. union {
  493. BitField<39, 1, u64> ftz;
  494. BitField<32, 1, u64> saturate;
  495. BitField<49, 2, HalfMerge> merge;
  496. BitField<43, 1, u64> negate_a;
  497. BitField<44, 1, u64> abs_a;
  498. BitField<47, 2, HalfType> type_a;
  499. BitField<31, 1, u64> negate_b;
  500. BitField<30, 1, u64> abs_b;
  501. BitField<47, 2, HalfType> type_b;
  502. BitField<35, 2, HalfType> type_c;
  503. } alu_half;
  504. union {
  505. BitField<39, 2, HalfPrecision> precision;
  506. BitField<39, 1, u64> ftz;
  507. BitField<52, 1, u64> saturate;
  508. BitField<49, 2, HalfMerge> merge;
  509. BitField<43, 1, u64> negate_a;
  510. BitField<44, 1, u64> abs_a;
  511. BitField<47, 2, HalfType> type_a;
  512. } alu_half_imm;
  513. union {
  514. BitField<29, 1, u64> first_negate;
  515. BitField<20, 9, u64> first;
  516. BitField<56, 1, u64> second_negate;
  517. BitField<30, 9, u64> second;
  518. u32 PackImmediates() const {
  519. // Immediates are half floats shifted.
  520. constexpr u32 imm_shift = 6;
  521. return static_cast<u32>((first << imm_shift) | (second << (16 + imm_shift)));
  522. }
  523. } half_imm;
  524. union {
  525. union {
  526. BitField<37, 2, HalfPrecision> precision;
  527. BitField<32, 1, u64> saturate;
  528. BitField<30, 1, u64> negate_c;
  529. BitField<35, 2, HalfType> type_c;
  530. } rr;
  531. BitField<57, 2, HalfPrecision> precision;
  532. BitField<52, 1, u64> saturate;
  533. BitField<49, 2, HalfMerge> merge;
  534. BitField<47, 2, HalfType> type_a;
  535. BitField<56, 1, u64> negate_b;
  536. BitField<28, 2, HalfType> type_b;
  537. BitField<51, 1, u64> negate_c;
  538. BitField<53, 2, HalfType> type_reg39;
  539. } hfma2;
  540. union {
  541. BitField<40, 1, u64> invert;
  542. } popc;
  543. union {
  544. BitField<39, 3, u64> pred;
  545. BitField<42, 1, u64> neg_pred;
  546. } sel;
  547. union {
  548. BitField<39, 3, u64> pred;
  549. BitField<42, 1, u64> negate_pred;
  550. BitField<43, 2, IMinMaxExchange> exchange;
  551. BitField<48, 1, u64> is_signed;
  552. } imnmx;
  553. union {
  554. BitField<31, 2, IAdd3Height> height_c;
  555. BitField<33, 2, IAdd3Height> height_b;
  556. BitField<35, 2, IAdd3Height> height_a;
  557. BitField<37, 2, IAdd3Mode> mode;
  558. BitField<49, 1, u64> neg_c;
  559. BitField<50, 1, u64> neg_b;
  560. BitField<51, 1, u64> neg_a;
  561. } iadd3;
  562. union {
  563. BitField<54, 1, u64> saturate;
  564. BitField<56, 1, u64> negate_a;
  565. } iadd32i;
  566. union {
  567. BitField<53, 1, u64> negate_b;
  568. BitField<54, 1, u64> abs_a;
  569. BitField<56, 1, u64> negate_a;
  570. BitField<57, 1, u64> abs_b;
  571. } fadd32i;
  572. union {
  573. BitField<20, 8, u64> shift_position;
  574. BitField<28, 8, u64> shift_length;
  575. BitField<48, 1, u64> negate_b;
  576. BitField<49, 1, u64> negate_a;
  577. u64 GetLeftShiftValue() const {
  578. return 32 - (shift_position + shift_length);
  579. }
  580. } bfe;
  581. union {
  582. BitField<48, 3, u64> pred48;
  583. union {
  584. BitField<20, 20, u64> entry_a;
  585. BitField<39, 5, u64> entry_b;
  586. BitField<45, 1, u64> neg;
  587. BitField<46, 1, u64> uses_cc;
  588. } imm;
  589. union {
  590. BitField<20, 14, u64> cb_index;
  591. BitField<34, 5, u64> cb_offset;
  592. BitField<56, 1, u64> neg;
  593. BitField<57, 1, u64> uses_cc;
  594. } hi;
  595. union {
  596. BitField<20, 14, u64> cb_index;
  597. BitField<34, 5, u64> cb_offset;
  598. BitField<39, 5, u64> entry_a;
  599. BitField<45, 1, u64> neg;
  600. BitField<46, 1, u64> uses_cc;
  601. } rz;
  602. union {
  603. BitField<39, 5, u64> entry_a;
  604. BitField<45, 1, u64> neg;
  605. BitField<46, 1, u64> uses_cc;
  606. } r1;
  607. union {
  608. BitField<28, 8, u64> entry_a;
  609. BitField<37, 1, u64> neg;
  610. BitField<38, 1, u64> uses_cc;
  611. } r2;
  612. } lea;
  613. union {
  614. BitField<0, 5, FlowCondition> cond;
  615. } flow;
  616. union {
  617. BitField<47, 1, u64> cc;
  618. BitField<48, 1, u64> negate_b;
  619. BitField<49, 1, u64> negate_c;
  620. BitField<51, 2, u64> tab5980_1;
  621. BitField<53, 2, u64> tab5980_0;
  622. } ffma;
  623. union {
  624. BitField<48, 3, UniformType> type;
  625. BitField<44, 2, u64> unknown;
  626. } ld_c;
  627. union {
  628. BitField<0, 3, u64> pred0;
  629. BitField<3, 3, u64> pred3;
  630. BitField<7, 1, u64> abs_a;
  631. BitField<39, 3, u64> pred39;
  632. BitField<42, 1, u64> neg_pred;
  633. BitField<43, 1, u64> neg_a;
  634. BitField<44, 1, u64> abs_b;
  635. BitField<45, 2, PredOperation> op;
  636. BitField<47, 1, u64> ftz;
  637. BitField<48, 4, PredCondition> cond;
  638. BitField<56, 1, u64> neg_b;
  639. } fsetp;
  640. union {
  641. BitField<0, 3, u64> pred0;
  642. BitField<3, 3, u64> pred3;
  643. BitField<39, 3, u64> pred39;
  644. BitField<42, 1, u64> neg_pred;
  645. BitField<45, 2, PredOperation> op;
  646. BitField<48, 1, u64> is_signed;
  647. BitField<49, 3, PredCondition> cond;
  648. } isetp;
  649. union {
  650. BitField<0, 3, u64> pred0;
  651. BitField<3, 3, u64> pred3;
  652. BitField<12, 3, u64> pred12;
  653. BitField<15, 1, u64> neg_pred12;
  654. BitField<24, 2, PredOperation> cond;
  655. BitField<29, 3, u64> pred29;
  656. BitField<32, 1, u64> neg_pred29;
  657. BitField<39, 3, u64> pred39;
  658. BitField<42, 1, u64> neg_pred39;
  659. BitField<45, 2, PredOperation> op;
  660. } psetp;
  661. union {
  662. BitField<12, 3, u64> pred12;
  663. BitField<15, 1, u64> neg_pred12;
  664. BitField<24, 2, PredOperation> cond;
  665. BitField<29, 3, u64> pred29;
  666. BitField<32, 1, u64> neg_pred29;
  667. BitField<39, 3, u64> pred39;
  668. BitField<42, 1, u64> neg_pred39;
  669. BitField<44, 1, u64> bf;
  670. BitField<45, 2, PredOperation> op;
  671. } pset;
  672. union {
  673. BitField<0, 3, u64> pred0;
  674. BitField<3, 3, u64> pred3;
  675. BitField<8, 5, ControlCode> cc; // flag in cc
  676. BitField<39, 3, u64> pred39;
  677. BitField<42, 1, u64> neg_pred39;
  678. BitField<45, 4, PredOperation> op; // op with pred39
  679. } csetp;
  680. union {
  681. BitField<39, 3, u64> pred39;
  682. BitField<42, 1, u64> neg_pred;
  683. BitField<43, 1, u64> neg_a;
  684. BitField<44, 1, u64> abs_b;
  685. BitField<45, 2, PredOperation> op;
  686. BitField<48, 4, PredCondition> cond;
  687. BitField<52, 1, u64> bf;
  688. BitField<53, 1, u64> neg_b;
  689. BitField<54, 1, u64> abs_a;
  690. BitField<55, 1, u64> ftz;
  691. BitField<56, 1, u64> neg_imm;
  692. } fset;
  693. union {
  694. BitField<39, 3, u64> pred39;
  695. BitField<42, 1, u64> neg_pred;
  696. BitField<44, 1, u64> bf;
  697. BitField<45, 2, PredOperation> op;
  698. BitField<48, 1, u64> is_signed;
  699. BitField<49, 3, PredCondition> cond;
  700. } iset;
  701. union {
  702. BitField<8, 2, Register::Size> dest_size;
  703. BitField<10, 2, Register::Size> src_size;
  704. BitField<12, 1, u64> is_output_signed;
  705. BitField<13, 1, u64> is_input_signed;
  706. BitField<41, 2, u64> selector;
  707. BitField<45, 1, u64> negate_a;
  708. BitField<49, 1, u64> abs_a;
  709. union {
  710. BitField<39, 2, F2iRoundingOp> rounding;
  711. } f2i;
  712. union {
  713. BitField<39, 4, F2fRoundingOp> rounding;
  714. } f2f;
  715. } conversion;
  716. union {
  717. BitField<28, 1, u64> array;
  718. BitField<29, 2, TextureType> texture_type;
  719. BitField<31, 4, u64> component_mask;
  720. BitField<49, 1, u64> nodep_flag;
  721. BitField<50, 1, u64> dc_flag;
  722. BitField<54, 1, u64> aoffi_flag;
  723. BitField<55, 3, TextureProcessMode> process_mode;
  724. bool IsComponentEnabled(std::size_t component) const {
  725. return ((1ull << component) & component_mask) != 0;
  726. }
  727. TextureProcessMode GetTextureProcessMode() const {
  728. return process_mode;
  729. }
  730. bool UsesMiscMode(TextureMiscMode mode) const {
  731. switch (mode) {
  732. case TextureMiscMode::DC:
  733. return dc_flag != 0;
  734. case TextureMiscMode::NODEP:
  735. return nodep_flag != 0;
  736. case TextureMiscMode::AOFFI:
  737. return aoffi_flag != 0;
  738. default:
  739. break;
  740. }
  741. return false;
  742. }
  743. } tex;
  744. union {
  745. BitField<22, 6, TextureQueryType> query_type;
  746. BitField<31, 4, u64> component_mask;
  747. BitField<49, 1, u64> nodep_flag;
  748. bool UsesMiscMode(TextureMiscMode mode) const {
  749. switch (mode) {
  750. case TextureMiscMode::NODEP:
  751. return nodep_flag != 0;
  752. default:
  753. break;
  754. }
  755. return false;
  756. }
  757. } txq;
  758. union {
  759. BitField<28, 1, u64> array;
  760. BitField<29, 2, TextureType> texture_type;
  761. BitField<31, 4, u64> component_mask;
  762. BitField<35, 1, u64> ndv_flag;
  763. BitField<49, 1, u64> nodep_flag;
  764. bool IsComponentEnabled(std::size_t component) const {
  765. return ((1ull << component) & component_mask) != 0;
  766. }
  767. bool UsesMiscMode(TextureMiscMode mode) const {
  768. switch (mode) {
  769. case TextureMiscMode::NDV:
  770. return (ndv_flag != 0);
  771. case TextureMiscMode::NODEP:
  772. return (nodep_flag != 0);
  773. default:
  774. break;
  775. }
  776. return false;
  777. }
  778. } tmml;
  779. union {
  780. BitField<28, 1, u64> array;
  781. BitField<29, 2, TextureType> texture_type;
  782. BitField<35, 1, u64> ndv_flag;
  783. BitField<49, 1, u64> nodep_flag;
  784. BitField<50, 1, u64> dc_flag;
  785. BitField<54, 2, u64> info;
  786. BitField<56, 2, u64> component;
  787. bool UsesMiscMode(TextureMiscMode mode) const {
  788. switch (mode) {
  789. case TextureMiscMode::NDV:
  790. return ndv_flag != 0;
  791. case TextureMiscMode::NODEP:
  792. return nodep_flag != 0;
  793. case TextureMiscMode::DC:
  794. return dc_flag != 0;
  795. case TextureMiscMode::AOFFI:
  796. return info == 1;
  797. case TextureMiscMode::PTP:
  798. return info == 2;
  799. default:
  800. break;
  801. }
  802. return false;
  803. }
  804. } tld4;
  805. union {
  806. BitField<49, 1, u64> nodep_flag;
  807. BitField<50, 1, u64> dc_flag;
  808. BitField<51, 1, u64> aoffi_flag;
  809. BitField<52, 2, u64> component;
  810. bool UsesMiscMode(TextureMiscMode mode) const {
  811. switch (mode) {
  812. case TextureMiscMode::DC:
  813. return dc_flag != 0;
  814. case TextureMiscMode::NODEP:
  815. return nodep_flag != 0;
  816. case TextureMiscMode::AOFFI:
  817. return aoffi_flag != 0;
  818. default:
  819. break;
  820. }
  821. return false;
  822. }
  823. } tld4s;
  824. union {
  825. BitField<0, 8, Register> gpr0;
  826. BitField<28, 8, Register> gpr28;
  827. BitField<49, 1, u64> nodep_flag;
  828. BitField<50, 3, u64> component_mask_selector;
  829. BitField<53, 4, u64> texture_info;
  830. TextureType GetTextureType() const {
  831. // The TEXS instruction has a weird encoding for the texture type.
  832. if (texture_info == 0)
  833. return TextureType::Texture1D;
  834. if (texture_info >= 1 && texture_info <= 9)
  835. return TextureType::Texture2D;
  836. if (texture_info >= 10 && texture_info <= 11)
  837. return TextureType::Texture3D;
  838. if (texture_info >= 12 && texture_info <= 13)
  839. return TextureType::TextureCube;
  840. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}",
  841. static_cast<u32>(texture_info.Value()));
  842. UNREACHABLE();
  843. }
  844. TextureProcessMode GetTextureProcessMode() const {
  845. switch (texture_info) {
  846. case 0:
  847. case 2:
  848. case 6:
  849. case 8:
  850. case 9:
  851. case 11:
  852. return TextureProcessMode::LZ;
  853. case 3:
  854. case 5:
  855. case 13:
  856. return TextureProcessMode::LL;
  857. default:
  858. break;
  859. }
  860. return TextureProcessMode::None;
  861. }
  862. bool UsesMiscMode(TextureMiscMode mode) const {
  863. switch (mode) {
  864. case TextureMiscMode::DC:
  865. return (texture_info >= 4 && texture_info <= 6) || texture_info == 9;
  866. case TextureMiscMode::NODEP:
  867. return nodep_flag != 0;
  868. default:
  869. break;
  870. }
  871. return false;
  872. }
  873. bool IsArrayTexture() const {
  874. // TEXS only supports Texture2D arrays.
  875. return texture_info >= 7 && texture_info <= 9;
  876. }
  877. bool HasTwoDestinations() const {
  878. return gpr28.Value() != Register::ZeroIndex;
  879. }
  880. bool IsComponentEnabled(std::size_t component) const {
  881. static constexpr std::array<std::array<u32, 8>, 4> mask_lut{{
  882. {},
  883. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  884. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  885. {0x7, 0xb, 0xd, 0xe, 0xf},
  886. }};
  887. std::size_t index{gpr0.Value() != Register::ZeroIndex ? 1U : 0U};
  888. index |= gpr28.Value() != Register::ZeroIndex ? 2 : 0;
  889. u32 mask = mask_lut[index][component_mask_selector];
  890. // A mask of 0 means this instruction uses an unimplemented mask.
  891. ASSERT(mask != 0);
  892. return ((1ull << component) & mask) != 0;
  893. }
  894. } texs;
  895. union {
  896. BitField<49, 1, u64> nodep_flag;
  897. BitField<53, 4, u64> texture_info;
  898. TextureType GetTextureType() const {
  899. // The TLDS instruction has a weird encoding for the texture type.
  900. if (texture_info >= 0 && texture_info <= 1) {
  901. return TextureType::Texture1D;
  902. }
  903. if (texture_info == 2 || texture_info == 8 || texture_info == 12 ||
  904. (texture_info >= 4 && texture_info <= 6)) {
  905. return TextureType::Texture2D;
  906. }
  907. if (texture_info == 7) {
  908. return TextureType::Texture3D;
  909. }
  910. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}",
  911. static_cast<u32>(texture_info.Value()));
  912. UNREACHABLE();
  913. }
  914. TextureProcessMode GetTextureProcessMode() const {
  915. if (texture_info == 1 || texture_info == 5 || texture_info == 12)
  916. return TextureProcessMode::LL;
  917. return TextureProcessMode::LZ;
  918. }
  919. bool UsesMiscMode(TextureMiscMode mode) const {
  920. switch (mode) {
  921. case TextureMiscMode::AOFFI:
  922. return texture_info == 12 || texture_info == 4;
  923. case TextureMiscMode::MZ:
  924. return texture_info == 5;
  925. case TextureMiscMode::NODEP:
  926. return nodep_flag != 0;
  927. default:
  928. break;
  929. }
  930. return false;
  931. }
  932. bool IsArrayTexture() const {
  933. // TEXS only supports Texture2D arrays.
  934. return texture_info == 8;
  935. }
  936. } tlds;
  937. union {
  938. BitField<20, 24, u64> target;
  939. BitField<5, 1, u64> constant_buffer;
  940. s32 GetBranchTarget() const {
  941. // Sign extend the branch target offset
  942. u32 mask = 1U << (24 - 1);
  943. u32 value = static_cast<u32>(target);
  944. // The branch offset is relative to the next instruction and is stored in bytes, so
  945. // divide it by the size of an instruction and add 1 to it.
  946. return static_cast<s32>((value ^ mask) - mask) / sizeof(Instruction) + 1;
  947. }
  948. } bra;
  949. union {
  950. BitField<39, 1, u64> emit; // EmitVertex
  951. BitField<40, 1, u64> cut; // EndPrimitive
  952. } out;
  953. union {
  954. BitField<31, 1, u64> skew;
  955. BitField<32, 1, u64> o;
  956. BitField<33, 2, IsberdMode> mode;
  957. BitField<47, 2, IsberdShift> shift;
  958. } isberd;
  959. union {
  960. BitField<48, 1, u64> signed_a;
  961. BitField<38, 1, u64> is_byte_chunk_a;
  962. BitField<36, 2, VmadType> type_a;
  963. BitField<36, 2, u64> byte_height_a;
  964. BitField<49, 1, u64> signed_b;
  965. BitField<50, 1, u64> use_register_b;
  966. BitField<30, 1, u64> is_byte_chunk_b;
  967. BitField<28, 2, VmadType> type_b;
  968. BitField<28, 2, u64> byte_height_b;
  969. BitField<51, 2, VmadShr> shr;
  970. BitField<55, 1, u64> saturate; // Saturates the result (a * b + c)
  971. BitField<47, 1, u64> cc;
  972. } vmad;
  973. union {
  974. BitField<20, 16, u64> imm20_16;
  975. BitField<36, 1, u64> product_shift_left;
  976. BitField<37, 1, u64> merge_37;
  977. BitField<48, 1, u64> sign_a;
  978. BitField<49, 1, u64> sign_b;
  979. BitField<50, 3, XmadMode> mode;
  980. BitField<52, 1, u64> high_b;
  981. BitField<53, 1, u64> high_a;
  982. BitField<56, 1, u64> merge_56;
  983. } xmad;
  984. union {
  985. BitField<20, 14, u64> offset;
  986. BitField<34, 5, u64> index;
  987. } cbuf34;
  988. union {
  989. BitField<20, 16, s64> offset;
  990. BitField<36, 5, u64> index;
  991. } cbuf36;
  992. // Unsure about the size of this one.
  993. // It's always used with a gpr0, so any size should be fine.
  994. BitField<20, 8, SystemVariable> sys20;
  995. BitField<47, 1, u64> generates_cc;
  996. BitField<61, 1, u64> is_b_imm;
  997. BitField<60, 1, u64> is_b_gpr;
  998. BitField<59, 1, u64> is_c_gpr;
  999. Attribute attribute;
  1000. Sampler sampler;
  1001. u64 value;
  1002. };
  1003. static_assert(sizeof(Instruction) == 0x8, "Incorrect structure size");
  1004. static_assert(std::is_standard_layout_v<Instruction>, "Instruction is not standard layout");
  1005. class OpCode {
  1006. public:
  1007. enum class Id {
  1008. KIL,
  1009. SSY,
  1010. SYNC,
  1011. DEPBAR,
  1012. BFE_C,
  1013. BFE_R,
  1014. BFE_IMM,
  1015. BRA,
  1016. LD_A,
  1017. LD_C,
  1018. ST_A,
  1019. LDG, // Load from global memory
  1020. STG, // Store in global memory
  1021. TEX,
  1022. TXQ, // Texture Query
  1023. TEXS, // Texture Fetch with scalar/non-vec4 source/destinations
  1024. TLDS, // Texture Load with scalar/non-vec4 source/destinations
  1025. TLD4, // Texture Load 4
  1026. TLD4S, // Texture Load 4 with scalar / non - vec4 source / destinations
  1027. TMML_B, // Texture Mip Map Level
  1028. TMML, // Texture Mip Map Level
  1029. EXIT,
  1030. IPA,
  1031. OUT_R, // Emit vertex/primitive
  1032. ISBERD,
  1033. VMAD,
  1034. FFMA_IMM, // Fused Multiply and Add
  1035. FFMA_CR,
  1036. FFMA_RC,
  1037. FFMA_RR,
  1038. FADD_C,
  1039. FADD_R,
  1040. FADD_IMM,
  1041. FADD32I,
  1042. FMUL_C,
  1043. FMUL_R,
  1044. FMUL_IMM,
  1045. FMUL32_IMM,
  1046. IADD_C,
  1047. IADD_R,
  1048. IADD_IMM,
  1049. IADD3_C, // Add 3 Integers
  1050. IADD3_R,
  1051. IADD3_IMM,
  1052. IADD32I,
  1053. ISCADD_C, // Scale and Add
  1054. ISCADD_R,
  1055. ISCADD_IMM,
  1056. LEA_R1,
  1057. LEA_R2,
  1058. LEA_RZ,
  1059. LEA_IMM,
  1060. LEA_HI,
  1061. HADD2_C,
  1062. HADD2_R,
  1063. HADD2_IMM,
  1064. HMUL2_C,
  1065. HMUL2_R,
  1066. HMUL2_IMM,
  1067. HFMA2_CR,
  1068. HFMA2_RC,
  1069. HFMA2_RR,
  1070. HFMA2_IMM_R,
  1071. POPC_C,
  1072. POPC_R,
  1073. POPC_IMM,
  1074. SEL_C,
  1075. SEL_R,
  1076. SEL_IMM,
  1077. MUFU, // Multi-Function Operator
  1078. RRO_C, // Range Reduction Operator
  1079. RRO_R,
  1080. RRO_IMM,
  1081. F2F_C,
  1082. F2F_R,
  1083. F2F_IMM,
  1084. F2I_C,
  1085. F2I_R,
  1086. F2I_IMM,
  1087. I2F_C,
  1088. I2F_R,
  1089. I2F_IMM,
  1090. I2I_C,
  1091. I2I_R,
  1092. I2I_IMM,
  1093. LOP_C,
  1094. LOP_R,
  1095. LOP_IMM,
  1096. LOP32I,
  1097. LOP3_C,
  1098. LOP3_R,
  1099. LOP3_IMM,
  1100. MOV_C,
  1101. MOV_R,
  1102. MOV_IMM,
  1103. MOV_SYS,
  1104. MOV32_IMM,
  1105. SHL_C,
  1106. SHL_R,
  1107. SHL_IMM,
  1108. SHR_C,
  1109. SHR_R,
  1110. SHR_IMM,
  1111. FMNMX_C,
  1112. FMNMX_R,
  1113. FMNMX_IMM,
  1114. IMNMX_C,
  1115. IMNMX_R,
  1116. IMNMX_IMM,
  1117. FSETP_C, // Set Predicate
  1118. FSETP_R,
  1119. FSETP_IMM,
  1120. FSET_C,
  1121. FSET_R,
  1122. FSET_IMM,
  1123. ISETP_C,
  1124. ISETP_IMM,
  1125. ISETP_R,
  1126. ISET_R,
  1127. ISET_C,
  1128. ISET_IMM,
  1129. PSETP,
  1130. PSET,
  1131. CSETP,
  1132. XMAD_IMM,
  1133. XMAD_CR,
  1134. XMAD_RC,
  1135. XMAD_RR,
  1136. };
  1137. enum class Type {
  1138. Trivial,
  1139. Arithmetic,
  1140. ArithmeticImmediate,
  1141. ArithmeticInteger,
  1142. ArithmeticIntegerImmediate,
  1143. ArithmeticHalf,
  1144. ArithmeticHalfImmediate,
  1145. Bfe,
  1146. Shift,
  1147. Ffma,
  1148. Hfma2,
  1149. Flow,
  1150. Synch,
  1151. Memory,
  1152. FloatSet,
  1153. FloatSetPredicate,
  1154. IntegerSet,
  1155. IntegerSetPredicate,
  1156. PredicateSetPredicate,
  1157. PredicateSetRegister,
  1158. Conversion,
  1159. Xmad,
  1160. Unknown,
  1161. };
  1162. /// Returns whether an opcode has an execution predicate field or not (ie, whether it can be
  1163. /// conditionally executed).
  1164. static bool IsPredicatedInstruction(Id opcode) {
  1165. // TODO(Subv): Add the rest of unpredicated instructions.
  1166. return opcode != Id::SSY;
  1167. }
  1168. class Matcher {
  1169. public:
  1170. Matcher(const char* const name, u16 mask, u16 expected, OpCode::Id id, OpCode::Type type)
  1171. : name{name}, mask{mask}, expected{expected}, id{id}, type{type} {}
  1172. const char* GetName() const {
  1173. return name;
  1174. }
  1175. u16 GetMask() const {
  1176. return mask;
  1177. }
  1178. Id GetId() const {
  1179. return id;
  1180. }
  1181. Type GetType() const {
  1182. return type;
  1183. }
  1184. /**
  1185. * Tests to see if the given instruction is the instruction this matcher represents.
  1186. * @param instruction The instruction to test
  1187. * @returns true if the given instruction matches.
  1188. */
  1189. bool Matches(u16 instruction) const {
  1190. return (instruction & mask) == expected;
  1191. }
  1192. private:
  1193. const char* name;
  1194. u16 mask;
  1195. u16 expected;
  1196. Id id;
  1197. Type type;
  1198. };
  1199. static boost::optional<const Matcher&> Decode(Instruction instr) {
  1200. static const auto table{GetDecodeTable()};
  1201. const auto matches_instruction = [instr](const auto& matcher) {
  1202. return matcher.Matches(static_cast<u16>(instr.opcode));
  1203. };
  1204. auto iter = std::find_if(table.begin(), table.end(), matches_instruction);
  1205. return iter != table.end() ? boost::optional<const Matcher&>(*iter) : boost::none;
  1206. }
  1207. private:
  1208. struct Detail {
  1209. private:
  1210. static constexpr std::size_t opcode_bitsize = 16;
  1211. /**
  1212. * Generates the mask and the expected value after masking from a given bitstring.
  1213. * A '0' in a bitstring indicates that a zero must be present at that bit position.
  1214. * A '1' in a bitstring indicates that a one must be present at that bit position.
  1215. */
  1216. static auto GetMaskAndExpect(const char* const bitstring) {
  1217. u16 mask = 0, expect = 0;
  1218. for (std::size_t i = 0; i < opcode_bitsize; i++) {
  1219. const std::size_t bit_position = opcode_bitsize - i - 1;
  1220. switch (bitstring[i]) {
  1221. case '0':
  1222. mask |= 1 << bit_position;
  1223. break;
  1224. case '1':
  1225. expect |= 1 << bit_position;
  1226. mask |= 1 << bit_position;
  1227. break;
  1228. default:
  1229. // Ignore
  1230. break;
  1231. }
  1232. }
  1233. return std::make_tuple(mask, expect);
  1234. }
  1235. public:
  1236. /// Creates a matcher that can match and parse instructions based on bitstring.
  1237. static auto GetMatcher(const char* const bitstring, OpCode::Id op, OpCode::Type type,
  1238. const char* const name) {
  1239. const auto mask_expect = GetMaskAndExpect(bitstring);
  1240. return Matcher(name, std::get<0>(mask_expect), std::get<1>(mask_expect), op, type);
  1241. }
  1242. };
  1243. static std::vector<Matcher> GetDecodeTable() {
  1244. std::vector<Matcher> table = {
  1245. #define INST(bitstring, op, type, name) Detail::GetMatcher(bitstring, op, type, name)
  1246. INST("111000110011----", Id::KIL, Type::Flow, "KIL"),
  1247. INST("111000101001----", Id::SSY, Type::Flow, "SSY"),
  1248. INST("111000100100----", Id::BRA, Type::Flow, "BRA"),
  1249. INST("1111000011110---", Id::DEPBAR, Type::Synch, "DEPBAR"),
  1250. INST("1111000011111---", Id::SYNC, Type::Synch, "SYNC"),
  1251. INST("1110111111011---", Id::LD_A, Type::Memory, "LD_A"),
  1252. INST("1110111110010---", Id::LD_C, Type::Memory, "LD_C"),
  1253. INST("1110111111110---", Id::ST_A, Type::Memory, "ST_A"),
  1254. INST("1110111011010---", Id::LDG, Type::Memory, "LDG"),
  1255. INST("1110111011011---", Id::STG, Type::Memory, "STG"),
  1256. INST("110000----111---", Id::TEX, Type::Memory, "TEX"),
  1257. INST("1101111101001---", Id::TXQ, Type::Memory, "TXQ"),
  1258. INST("1101100---------", Id::TEXS, Type::Memory, "TEXS"),
  1259. INST("1101101---------", Id::TLDS, Type::Memory, "TLDS"),
  1260. INST("110010----111---", Id::TLD4, Type::Memory, "TLD4"),
  1261. INST("1101111100------", Id::TLD4S, Type::Memory, "TLD4S"),
  1262. INST("110111110110----", Id::TMML_B, Type::Memory, "TMML_B"),
  1263. INST("1101111101011---", Id::TMML, Type::Memory, "TMML"),
  1264. INST("111000110000----", Id::EXIT, Type::Trivial, "EXIT"),
  1265. INST("11100000--------", Id::IPA, Type::Trivial, "IPA"),
  1266. INST("1111101111100---", Id::OUT_R, Type::Trivial, "OUT_R"),
  1267. INST("1110111111010---", Id::ISBERD, Type::Trivial, "ISBERD"),
  1268. INST("01011111--------", Id::VMAD, Type::Trivial, "VMAD"),
  1269. INST("0011001-1-------", Id::FFMA_IMM, Type::Ffma, "FFMA_IMM"),
  1270. INST("010010011-------", Id::FFMA_CR, Type::Ffma, "FFMA_CR"),
  1271. INST("010100011-------", Id::FFMA_RC, Type::Ffma, "FFMA_RC"),
  1272. INST("010110011-------", Id::FFMA_RR, Type::Ffma, "FFMA_RR"),
  1273. INST("0100110001011---", Id::FADD_C, Type::Arithmetic, "FADD_C"),
  1274. INST("0101110001011---", Id::FADD_R, Type::Arithmetic, "FADD_R"),
  1275. INST("0011100-01011---", Id::FADD_IMM, Type::Arithmetic, "FADD_IMM"),
  1276. INST("000010----------", Id::FADD32I, Type::ArithmeticImmediate, "FADD32I"),
  1277. INST("0100110001101---", Id::FMUL_C, Type::Arithmetic, "FMUL_C"),
  1278. INST("0101110001101---", Id::FMUL_R, Type::Arithmetic, "FMUL_R"),
  1279. INST("0011100-01101---", Id::FMUL_IMM, Type::Arithmetic, "FMUL_IMM"),
  1280. INST("00011110--------", Id::FMUL32_IMM, Type::ArithmeticImmediate, "FMUL32_IMM"),
  1281. INST("0100110000010---", Id::IADD_C, Type::ArithmeticInteger, "IADD_C"),
  1282. INST("0101110000010---", Id::IADD_R, Type::ArithmeticInteger, "IADD_R"),
  1283. INST("0011100-00010---", Id::IADD_IMM, Type::ArithmeticInteger, "IADD_IMM"),
  1284. INST("010011001100----", Id::IADD3_C, Type::ArithmeticInteger, "IADD3_C"),
  1285. INST("010111001100----", Id::IADD3_R, Type::ArithmeticInteger, "IADD3_R"),
  1286. INST("0011100-1100----", Id::IADD3_IMM, Type::ArithmeticInteger, "IADD3_IMM"),
  1287. INST("0001110---------", Id::IADD32I, Type::ArithmeticIntegerImmediate, "IADD32I"),
  1288. INST("0100110000011---", Id::ISCADD_C, Type::ArithmeticInteger, "ISCADD_C"),
  1289. INST("0101110000011---", Id::ISCADD_R, Type::ArithmeticInteger, "ISCADD_R"),
  1290. INST("0011100-00011---", Id::ISCADD_IMM, Type::ArithmeticInteger, "ISCADD_IMM"),
  1291. INST("0100110000001---", Id::POPC_C, Type::ArithmeticInteger, "POPC_C"),
  1292. INST("0101110000001---", Id::POPC_R, Type::ArithmeticInteger, "POPC_R"),
  1293. INST("0011100-00001---", Id::POPC_IMM, Type::ArithmeticInteger, "POPC_IMM"),
  1294. INST("0100110010100---", Id::SEL_C, Type::ArithmeticInteger, "SEL_C"),
  1295. INST("0101110010100---", Id::SEL_R, Type::ArithmeticInteger, "SEL_R"),
  1296. INST("0011100-10100---", Id::SEL_IMM, Type::ArithmeticInteger, "SEL_IMM"),
  1297. INST("0101101111011---", Id::LEA_R2, Type::ArithmeticInteger, "LEA_R2"),
  1298. INST("0101101111010---", Id::LEA_R1, Type::ArithmeticInteger, "LEA_R1"),
  1299. INST("001101101101----", Id::LEA_IMM, Type::ArithmeticInteger, "LEA_IMM"),
  1300. INST("010010111101----", Id::LEA_RZ, Type::ArithmeticInteger, "LEA_RZ"),
  1301. INST("00011000--------", Id::LEA_HI, Type::ArithmeticInteger, "LEA_HI"),
  1302. INST("0111101-1-------", Id::HADD2_C, Type::ArithmeticHalf, "HADD2_C"),
  1303. INST("0101110100010---", Id::HADD2_R, Type::ArithmeticHalf, "HADD2_R"),
  1304. INST("0111101-0-------", Id::HADD2_IMM, Type::ArithmeticHalfImmediate, "HADD2_IMM"),
  1305. INST("0111100-1-------", Id::HMUL2_C, Type::ArithmeticHalf, "HMUL2_C"),
  1306. INST("0101110100001---", Id::HMUL2_R, Type::ArithmeticHalf, "HMUL2_R"),
  1307. INST("0111100-0-------", Id::HMUL2_IMM, Type::ArithmeticHalfImmediate, "HMUL2_IMM"),
  1308. INST("01110---1-------", Id::HFMA2_CR, Type::Hfma2, "HFMA2_CR"),
  1309. INST("01100---1-------", Id::HFMA2_RC, Type::Hfma2, "HFMA2_RC"),
  1310. INST("0101110100000---", Id::HFMA2_RR, Type::Hfma2, "HFMA2_RR"),
  1311. INST("01110---0-------", Id::HFMA2_IMM_R, Type::Hfma2, "HFMA2_R_IMM"),
  1312. INST("0101000010000---", Id::MUFU, Type::Arithmetic, "MUFU"),
  1313. INST("0100110010010---", Id::RRO_C, Type::Arithmetic, "RRO_C"),
  1314. INST("0101110010010---", Id::RRO_R, Type::Arithmetic, "RRO_R"),
  1315. INST("0011100-10010---", Id::RRO_IMM, Type::Arithmetic, "RRO_IMM"),
  1316. INST("0100110010101---", Id::F2F_C, Type::Conversion, "F2F_C"),
  1317. INST("0101110010101---", Id::F2F_R, Type::Conversion, "F2F_R"),
  1318. INST("0011100-10101---", Id::F2F_IMM, Type::Conversion, "F2F_IMM"),
  1319. INST("0100110010110---", Id::F2I_C, Type::Conversion, "F2I_C"),
  1320. INST("0101110010110---", Id::F2I_R, Type::Conversion, "F2I_R"),
  1321. INST("0011100-10110---", Id::F2I_IMM, Type::Conversion, "F2I_IMM"),
  1322. INST("0100110010011---", Id::MOV_C, Type::Arithmetic, "MOV_C"),
  1323. INST("0101110010011---", Id::MOV_R, Type::Arithmetic, "MOV_R"),
  1324. INST("0011100-10011---", Id::MOV_IMM, Type::Arithmetic, "MOV_IMM"),
  1325. INST("1111000011001---", Id::MOV_SYS, Type::Trivial, "MOV_SYS"),
  1326. INST("000000010000----", Id::MOV32_IMM, Type::ArithmeticImmediate, "MOV32_IMM"),
  1327. INST("0100110001100---", Id::FMNMX_C, Type::Arithmetic, "FMNMX_C"),
  1328. INST("0101110001100---", Id::FMNMX_R, Type::Arithmetic, "FMNMX_R"),
  1329. INST("0011100-01100---", Id::FMNMX_IMM, Type::Arithmetic, "FMNMX_IMM"),
  1330. INST("0100110000100---", Id::IMNMX_C, Type::ArithmeticInteger, "IMNMX_C"),
  1331. INST("0101110000100---", Id::IMNMX_R, Type::ArithmeticInteger, "IMNMX_R"),
  1332. INST("0011100-00100---", Id::IMNMX_IMM, Type::ArithmeticInteger, "IMNMX_IMM"),
  1333. INST("0100110000000---", Id::BFE_C, Type::Bfe, "BFE_C"),
  1334. INST("0101110000000---", Id::BFE_R, Type::Bfe, "BFE_R"),
  1335. INST("0011100-00000---", Id::BFE_IMM, Type::Bfe, "BFE_IMM"),
  1336. INST("0100110001000---", Id::LOP_C, Type::ArithmeticInteger, "LOP_C"),
  1337. INST("0101110001000---", Id::LOP_R, Type::ArithmeticInteger, "LOP_R"),
  1338. INST("0011100001000---", Id::LOP_IMM, Type::ArithmeticInteger, "LOP_IMM"),
  1339. INST("000001----------", Id::LOP32I, Type::ArithmeticIntegerImmediate, "LOP32I"),
  1340. INST("0000001---------", Id::LOP3_C, Type::ArithmeticInteger, "LOP3_C"),
  1341. INST("0101101111100---", Id::LOP3_R, Type::ArithmeticInteger, "LOP3_R"),
  1342. INST("0011110---------", Id::LOP3_IMM, Type::ArithmeticInteger, "LOP3_IMM"),
  1343. INST("0100110001001---", Id::SHL_C, Type::Shift, "SHL_C"),
  1344. INST("0101110001001---", Id::SHL_R, Type::Shift, "SHL_R"),
  1345. INST("0011100-01001---", Id::SHL_IMM, Type::Shift, "SHL_IMM"),
  1346. INST("0100110000101---", Id::SHR_C, Type::Shift, "SHR_C"),
  1347. INST("0101110000101---", Id::SHR_R, Type::Shift, "SHR_R"),
  1348. INST("0011100-00101---", Id::SHR_IMM, Type::Shift, "SHR_IMM"),
  1349. INST("0100110011100---", Id::I2I_C, Type::Conversion, "I2I_C"),
  1350. INST("0101110011100---", Id::I2I_R, Type::Conversion, "I2I_R"),
  1351. INST("01110001-1000---", Id::I2I_IMM, Type::Conversion, "I2I_IMM"),
  1352. INST("0100110010111---", Id::I2F_C, Type::Conversion, "I2F_C"),
  1353. INST("0101110010111---", Id::I2F_R, Type::Conversion, "I2F_R"),
  1354. INST("0011100-10111---", Id::I2F_IMM, Type::Conversion, "I2F_IMM"),
  1355. INST("01011000--------", Id::FSET_R, Type::FloatSet, "FSET_R"),
  1356. INST("0100100---------", Id::FSET_C, Type::FloatSet, "FSET_C"),
  1357. INST("0011000---------", Id::FSET_IMM, Type::FloatSet, "FSET_IMM"),
  1358. INST("010010111011----", Id::FSETP_C, Type::FloatSetPredicate, "FSETP_C"),
  1359. INST("010110111011----", Id::FSETP_R, Type::FloatSetPredicate, "FSETP_R"),
  1360. INST("0011011-1011----", Id::FSETP_IMM, Type::FloatSetPredicate, "FSETP_IMM"),
  1361. INST("010010110110----", Id::ISETP_C, Type::IntegerSetPredicate, "ISETP_C"),
  1362. INST("010110110110----", Id::ISETP_R, Type::IntegerSetPredicate, "ISETP_R"),
  1363. INST("0011011-0110----", Id::ISETP_IMM, Type::IntegerSetPredicate, "ISETP_IMM"),
  1364. INST("010110110101----", Id::ISET_R, Type::IntegerSet, "ISET_R"),
  1365. INST("010010110101----", Id::ISET_C, Type::IntegerSet, "ISET_C"),
  1366. INST("0011011-0101----", Id::ISET_IMM, Type::IntegerSet, "ISET_IMM"),
  1367. INST("0101000010001---", Id::PSET, Type::PredicateSetRegister, "PSET"),
  1368. INST("0101000010010---", Id::PSETP, Type::PredicateSetPredicate, "PSETP"),
  1369. INST("010100001010----", Id::CSETP, Type::PredicateSetPredicate, "CSETP"),
  1370. INST("0011011-00------", Id::XMAD_IMM, Type::Xmad, "XMAD_IMM"),
  1371. INST("0100111---------", Id::XMAD_CR, Type::Xmad, "XMAD_CR"),
  1372. INST("010100010-------", Id::XMAD_RC, Type::Xmad, "XMAD_RC"),
  1373. INST("0101101100------", Id::XMAD_RR, Type::Xmad, "XMAD_RR"),
  1374. };
  1375. #undef INST
  1376. std::stable_sort(table.begin(), table.end(), [](const auto& a, const auto& b) {
  1377. // If a matcher has more bits in its mask it is more specific, so it
  1378. // should come first.
  1379. return std::bitset<16>(a.GetMask()).count() > std::bitset<16>(b.GetMask()).count();
  1380. });
  1381. return table;
  1382. }
  1383. };
  1384. } // namespace Tegra::Shader