shader_bytecode.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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 <cstring>
  7. #include <map>
  8. #include <string>
  9. #include <vector>
  10. #include <boost/optional.hpp>
  11. #include "common/assert.h"
  12. #include "common/bit_field.h"
  13. #include "common/common_types.h"
  14. namespace Tegra::Shader {
  15. struct Register {
  16. /// Number of registers
  17. static constexpr size_t NumRegisters = 256;
  18. /// Register 255 is special cased to always be 0
  19. static constexpr size_t ZeroIndex = 255;
  20. enum class Size : u64 {
  21. Byte = 0,
  22. Short = 1,
  23. Word = 2,
  24. Long = 3,
  25. };
  26. constexpr Register() = default;
  27. constexpr Register(u64 value) : value(value) {}
  28. constexpr operator u64() const {
  29. return value;
  30. }
  31. template <typename T>
  32. constexpr u64 operator-(const T& oth) const {
  33. return value - oth;
  34. }
  35. template <typename T>
  36. constexpr u64 operator&(const T& oth) const {
  37. return value & oth;
  38. }
  39. constexpr u64 operator&(const Register& oth) const {
  40. return value & oth.value;
  41. }
  42. constexpr u64 operator~() const {
  43. return ~value;
  44. }
  45. u64 GetSwizzledIndex(u64 elem) const {
  46. elem = (value + elem) & 3;
  47. return (value & ~3) + elem;
  48. }
  49. private:
  50. u64 value{};
  51. };
  52. union Attribute {
  53. Attribute() = default;
  54. constexpr explicit Attribute(u64 value) : value(value) {}
  55. enum class Index : u64 {
  56. Position = 7,
  57. Attribute_0 = 8,
  58. Attribute_31 = 39,
  59. // This attribute contains a tuple of (~, ~, InstanceId, VertexId) when inside a vertex
  60. // shader, and a tuple of (TessCoord.x, TessCoord.y, TessCoord.z, ~) when inside a Tess Eval
  61. // shader.
  62. TessCoordInstanceIDVertexID = 47,
  63. // This attribute contains a tuple of (Unk, Unk, Unk, gl_FrontFacing) when inside a fragment
  64. // shader. It is unknown what the other values contain.
  65. FrontFacing = 63,
  66. };
  67. union {
  68. BitField<22, 2, u64> element;
  69. BitField<24, 6, Index> index;
  70. BitField<47, 3, u64> size;
  71. } fmt20;
  72. union {
  73. BitField<30, 2, u64> element;
  74. BitField<32, 6, Index> index;
  75. } fmt28;
  76. BitField<39, 8, u64> reg;
  77. u64 value{};
  78. };
  79. union Sampler {
  80. Sampler() = default;
  81. constexpr explicit Sampler(u64 value) : value(value) {}
  82. enum class Index : u64 {
  83. Sampler_0 = 8,
  84. };
  85. BitField<36, 13, Index> index;
  86. u64 value{};
  87. };
  88. } // namespace Tegra::Shader
  89. namespace std {
  90. // TODO(bunnei): The below is forbidden by the C++ standard, but works fine. See #330.
  91. template <>
  92. struct make_unsigned<Tegra::Shader::Attribute> {
  93. using type = Tegra::Shader::Attribute;
  94. };
  95. template <>
  96. struct make_unsigned<Tegra::Shader::Register> {
  97. using type = Tegra::Shader::Register;
  98. };
  99. } // namespace std
  100. namespace Tegra::Shader {
  101. enum class Pred : u64 {
  102. UnusedIndex = 0x7,
  103. NeverExecute = 0xF,
  104. };
  105. enum class PredCondition : u64 {
  106. LessThan = 1,
  107. Equal = 2,
  108. LessEqual = 3,
  109. GreaterThan = 4,
  110. NotEqual = 5,
  111. GreaterEqual = 6,
  112. LessThanWithNan = 9,
  113. GreaterThanWithNan = 12,
  114. NotEqualWithNan = 13,
  115. GreaterEqualWithNan = 14,
  116. // TODO(Subv): Other condition types
  117. };
  118. enum class PredOperation : u64 {
  119. And = 0,
  120. Or = 1,
  121. Xor = 2,
  122. };
  123. enum class LogicOperation : u64 {
  124. And = 0,
  125. Or = 1,
  126. Xor = 2,
  127. PassB = 3,
  128. };
  129. enum class SubOp : u64 {
  130. Cos = 0x0,
  131. Sin = 0x1,
  132. Ex2 = 0x2,
  133. Lg2 = 0x3,
  134. Rcp = 0x4,
  135. Rsq = 0x5,
  136. Sqrt = 0x8,
  137. };
  138. enum class F2iRoundingOp : u64 {
  139. None = 0,
  140. Floor = 1,
  141. Ceil = 2,
  142. Trunc = 3,
  143. };
  144. enum class F2fRoundingOp : u64 {
  145. None = 0,
  146. Pass = 3,
  147. Round = 8,
  148. Floor = 9,
  149. Ceil = 10,
  150. Trunc = 11,
  151. };
  152. enum class UniformType : u64 {
  153. UnsignedByte = 0,
  154. SignedByte = 1,
  155. UnsignedShort = 2,
  156. SignedShort = 3,
  157. Single = 4,
  158. Double = 5,
  159. };
  160. enum class IMinMaxExchange : u64 {
  161. None = 0,
  162. XLo = 1,
  163. XMed = 2,
  164. XHi = 3,
  165. };
  166. enum class XmadMode : u64 {
  167. None = 0,
  168. CLo = 1,
  169. CHi = 2,
  170. CSfu = 3,
  171. CBcc = 4,
  172. };
  173. enum class IAdd3Mode : u64 {
  174. None = 0,
  175. RightShift = 1,
  176. LeftShift = 2,
  177. };
  178. enum class IAdd3Height : u64 {
  179. None = 0,
  180. LowerHalfWord = 1,
  181. UpperHalfWord = 2,
  182. };
  183. enum class FlowCondition : u64 {
  184. Always = 0xF,
  185. Fcsm_Tr = 0x1C, // TODO(bunnei): What is this used for?
  186. };
  187. enum class PredicateResultMode : u64 {
  188. None = 0x0,
  189. NotZero = 0x3,
  190. };
  191. enum class TextureType : u64 {
  192. Texture1D = 0,
  193. Texture2D = 1,
  194. Texture3D = 2,
  195. TextureCube = 3,
  196. };
  197. enum class IpaMode : u64 { Pass = 0, None = 1, Constant = 2, Sc = 3 };
  198. union Instruction {
  199. Instruction& operator=(const Instruction& instr) {
  200. value = instr.value;
  201. return *this;
  202. }
  203. constexpr Instruction(u64 value) : value{value} {}
  204. BitField<0, 8, Register> gpr0;
  205. BitField<8, 8, Register> gpr8;
  206. union {
  207. BitField<16, 4, Pred> full_pred;
  208. BitField<16, 3, u64> pred_index;
  209. } pred;
  210. BitField<19, 1, u64> negate_pred;
  211. BitField<20, 8, Register> gpr20;
  212. BitField<20, 4, SubOp> sub_op;
  213. BitField<28, 8, Register> gpr28;
  214. BitField<39, 8, Register> gpr39;
  215. BitField<48, 16, u64> opcode;
  216. union {
  217. BitField<20, 19, u64> imm20_19;
  218. BitField<20, 32, s64> imm20_32;
  219. BitField<45, 1, u64> negate_b;
  220. BitField<46, 1, u64> abs_a;
  221. BitField<48, 1, u64> negate_a;
  222. BitField<49, 1, u64> abs_b;
  223. BitField<50, 1, u64> saturate_d;
  224. BitField<56, 1, u64> negate_imm;
  225. union {
  226. BitField<39, 3, u64> pred;
  227. BitField<42, 1, u64> negate_pred;
  228. } fmnmx;
  229. union {
  230. BitField<39, 1, u64> invert_a;
  231. BitField<40, 1, u64> invert_b;
  232. BitField<41, 2, LogicOperation> operation;
  233. BitField<44, 2, PredicateResultMode> pred_result_mode;
  234. BitField<48, 3, Pred> pred48;
  235. } lop;
  236. union {
  237. BitField<53, 2, LogicOperation> operation;
  238. BitField<55, 1, u64> invert_a;
  239. BitField<56, 1, u64> invert_b;
  240. } lop32i;
  241. union {
  242. BitField<28, 8, u64> imm_lut28;
  243. BitField<48, 8, u64> imm_lut48;
  244. u32 GetImmLut28() const {
  245. return static_cast<u32>(imm_lut28);
  246. }
  247. u32 GetImmLut48() const {
  248. return static_cast<u32>(imm_lut48);
  249. }
  250. } lop3;
  251. u32 GetImm20_19() const {
  252. u32 imm{static_cast<u32>(imm20_19)};
  253. imm <<= 12;
  254. imm |= negate_imm ? 0x80000000 : 0;
  255. return imm;
  256. }
  257. u32 GetImm20_32() const {
  258. return static_cast<u32>(imm20_32);
  259. }
  260. s32 GetSignedImm20_20() const {
  261. u32 immediate = static_cast<u32>(imm20_19 | (negate_imm << 19));
  262. // Sign extend the 20-bit value.
  263. u32 mask = 1U << (20 - 1);
  264. return static_cast<s32>((immediate ^ mask) - mask);
  265. }
  266. } alu;
  267. union {
  268. BitField<54, 3, IpaMode> mode;
  269. } ipa;
  270. union {
  271. BitField<39, 2, u64> tab5cb8_2;
  272. BitField<41, 3, u64> tab5c68_1;
  273. BitField<44, 2, u64> tab5c68_0;
  274. BitField<47, 1, u64> cc;
  275. BitField<48, 1, u64> negate_b;
  276. } fmul;
  277. union {
  278. BitField<48, 1, u64> is_signed;
  279. } shift;
  280. union {
  281. BitField<39, 5, u64> shift_amount;
  282. BitField<48, 1, u64> negate_b;
  283. BitField<49, 1, u64> negate_a;
  284. } alu_integer;
  285. union {
  286. BitField<40, 1, u64> invert;
  287. } popc;
  288. union {
  289. BitField<39, 3, u64> pred;
  290. BitField<42, 1, u64> neg_pred;
  291. } sel;
  292. union {
  293. BitField<39, 3, u64> pred;
  294. BitField<42, 1, u64> negate_pred;
  295. BitField<43, 2, IMinMaxExchange> exchange;
  296. BitField<48, 1, u64> is_signed;
  297. } imnmx;
  298. union {
  299. BitField<31, 2, IAdd3Height> height_c;
  300. BitField<33, 2, IAdd3Height> height_b;
  301. BitField<35, 2, IAdd3Height> height_a;
  302. BitField<37, 2, IAdd3Mode> mode;
  303. BitField<49, 1, u64> neg_c;
  304. BitField<50, 1, u64> neg_b;
  305. BitField<51, 1, u64> neg_a;
  306. } iadd3;
  307. union {
  308. BitField<54, 1, u64> saturate;
  309. BitField<56, 1, u64> negate_a;
  310. } iadd32i;
  311. union {
  312. BitField<53, 1, u64> negate_b;
  313. BitField<54, 1, u64> abs_a;
  314. BitField<56, 1, u64> negate_a;
  315. BitField<57, 1, u64> abs_b;
  316. } fadd32i;
  317. union {
  318. BitField<20, 8, u64> shift_position;
  319. BitField<28, 8, u64> shift_length;
  320. BitField<48, 1, u64> negate_b;
  321. BitField<49, 1, u64> negate_a;
  322. u64 GetLeftShiftValue() const {
  323. return 32 - (shift_position + shift_length);
  324. }
  325. } bfe;
  326. union {
  327. BitField<0, 5, FlowCondition> cond;
  328. } flow;
  329. union {
  330. BitField<48, 1, u64> negate_b;
  331. BitField<49, 1, u64> negate_c;
  332. } ffma;
  333. union {
  334. BitField<48, 3, UniformType> type;
  335. BitField<44, 2, u64> unknown;
  336. } ld_c;
  337. union {
  338. BitField<0, 3, u64> pred0;
  339. BitField<3, 3, u64> pred3;
  340. BitField<7, 1, u64> abs_a;
  341. BitField<39, 3, u64> pred39;
  342. BitField<42, 1, u64> neg_pred;
  343. BitField<43, 1, u64> neg_a;
  344. BitField<44, 1, u64> abs_b;
  345. BitField<45, 2, PredOperation> op;
  346. BitField<47, 1, u64> ftz;
  347. BitField<48, 4, PredCondition> cond;
  348. BitField<56, 1, u64> neg_b;
  349. } fsetp;
  350. union {
  351. BitField<0, 3, u64> pred0;
  352. BitField<3, 3, u64> pred3;
  353. BitField<39, 3, u64> pred39;
  354. BitField<42, 1, u64> neg_pred;
  355. BitField<45, 2, PredOperation> op;
  356. BitField<48, 1, u64> is_signed;
  357. BitField<49, 3, PredCondition> cond;
  358. } isetp;
  359. union {
  360. BitField<0, 3, u64> pred0;
  361. BitField<3, 3, u64> pred3;
  362. BitField<12, 3, u64> pred12;
  363. BitField<15, 1, u64> neg_pred12;
  364. BitField<24, 2, PredOperation> cond;
  365. BitField<29, 3, u64> pred29;
  366. BitField<32, 1, u64> neg_pred29;
  367. BitField<39, 3, u64> pred39;
  368. BitField<42, 1, u64> neg_pred39;
  369. BitField<45, 2, PredOperation> op;
  370. } psetp;
  371. union {
  372. BitField<39, 3, u64> pred39;
  373. BitField<42, 1, u64> neg_pred;
  374. BitField<43, 1, u64> neg_a;
  375. BitField<44, 1, u64> abs_b;
  376. BitField<45, 2, PredOperation> op;
  377. BitField<48, 4, PredCondition> cond;
  378. BitField<52, 1, u64> bf;
  379. BitField<53, 1, u64> neg_b;
  380. BitField<54, 1, u64> abs_a;
  381. BitField<55, 1, u64> ftz;
  382. BitField<56, 1, u64> neg_imm;
  383. } fset;
  384. union {
  385. BitField<39, 3, u64> pred39;
  386. BitField<42, 1, u64> neg_pred;
  387. BitField<44, 1, u64> bf;
  388. BitField<45, 2, PredOperation> op;
  389. BitField<48, 1, u64> is_signed;
  390. BitField<49, 3, PredCondition> cond;
  391. } iset;
  392. union {
  393. BitField<8, 2, Register::Size> dest_size;
  394. BitField<10, 2, Register::Size> src_size;
  395. BitField<12, 1, u64> is_output_signed;
  396. BitField<13, 1, u64> is_input_signed;
  397. BitField<41, 2, u64> selector;
  398. BitField<45, 1, u64> negate_a;
  399. BitField<49, 1, u64> abs_a;
  400. union {
  401. BitField<39, 2, F2iRoundingOp> rounding;
  402. } f2i;
  403. union {
  404. BitField<39, 4, F2fRoundingOp> rounding;
  405. } f2f;
  406. } conversion;
  407. union {
  408. BitField<28, 1, u64> array;
  409. BitField<29, 2, TextureType> texture_type;
  410. BitField<31, 4, u64> component_mask;
  411. bool IsComponentEnabled(size_t component) const {
  412. return ((1ull << component) & component_mask) != 0;
  413. }
  414. } tex;
  415. union {
  416. BitField<28, 1, u64> array;
  417. BitField<29, 2, TextureType> texture_type;
  418. BitField<56, 2, u64> component;
  419. } tld4;
  420. union {
  421. BitField<52, 2, u64> component;
  422. } tld4s;
  423. union {
  424. BitField<0, 8, Register> gpr0;
  425. BitField<28, 8, Register> gpr28;
  426. BitField<50, 3, u64> component_mask_selector;
  427. BitField<53, 4, u64> texture_info;
  428. TextureType GetTextureType() const {
  429. // The TEXS instruction has a weird encoding for the texture type.
  430. if (texture_info == 0)
  431. return TextureType::Texture1D;
  432. if (texture_info >= 1 && texture_info <= 9)
  433. return TextureType::Texture2D;
  434. if (texture_info >= 10 && texture_info <= 11)
  435. return TextureType::Texture3D;
  436. if (texture_info >= 12 && texture_info <= 13)
  437. return TextureType::TextureCube;
  438. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}",
  439. static_cast<u32>(texture_info.Value()));
  440. UNREACHABLE();
  441. }
  442. bool IsArrayTexture() const {
  443. // TEXS only supports Texture2D arrays.
  444. return texture_info >= 7 && texture_info <= 9;
  445. }
  446. bool HasTwoDestinations() const {
  447. return gpr28.Value() != Register::ZeroIndex;
  448. }
  449. bool IsComponentEnabled(size_t component) const {
  450. static constexpr std::array<std::array<u32, 8>, 4> mask_lut{{
  451. {},
  452. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  453. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  454. {0x7, 0xb, 0xd, 0xe, 0xf},
  455. }};
  456. size_t index{gpr0.Value() != Register::ZeroIndex ? 1U : 0U};
  457. index |= gpr28.Value() != Register::ZeroIndex ? 2 : 0;
  458. u32 mask = mask_lut[index][component_mask_selector];
  459. // A mask of 0 means this instruction uses an unimplemented mask.
  460. ASSERT(mask != 0);
  461. return ((1ull << component) & mask) != 0;
  462. }
  463. } texs;
  464. union {
  465. BitField<53, 4, u64> texture_info;
  466. TextureType GetTextureType() const {
  467. // The TLDS instruction has a weird encoding for the texture type.
  468. if (texture_info >= 0 && texture_info <= 1) {
  469. return TextureType::Texture1D;
  470. }
  471. if (texture_info == 2 || texture_info == 8 || texture_info == 12 ||
  472. (texture_info >= 4 && texture_info <= 6)) {
  473. return TextureType::Texture2D;
  474. }
  475. if (texture_info == 7) {
  476. return TextureType::Texture3D;
  477. }
  478. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}",
  479. static_cast<u32>(texture_info.Value()));
  480. UNREACHABLE();
  481. }
  482. bool IsArrayTexture() const {
  483. // TEXS only supports Texture2D arrays.
  484. return texture_info == 8;
  485. }
  486. } tlds;
  487. union {
  488. BitField<20, 24, u64> target;
  489. BitField<5, 1, u64> constant_buffer;
  490. s32 GetBranchTarget() const {
  491. // Sign extend the branch target offset
  492. u32 mask = 1U << (24 - 1);
  493. u32 value = static_cast<u32>(target);
  494. // The branch offset is relative to the next instruction and is stored in bytes, so
  495. // divide it by the size of an instruction and add 1 to it.
  496. return static_cast<s32>((value ^ mask) - mask) / sizeof(Instruction) + 1;
  497. }
  498. } bra;
  499. union {
  500. BitField<20, 16, u64> imm20_16;
  501. BitField<36, 1, u64> product_shift_left;
  502. BitField<37, 1, u64> merge_37;
  503. BitField<48, 1, u64> sign_a;
  504. BitField<49, 1, u64> sign_b;
  505. BitField<50, 3, XmadMode> mode;
  506. BitField<52, 1, u64> high_b;
  507. BitField<53, 1, u64> high_a;
  508. BitField<56, 1, u64> merge_56;
  509. } xmad;
  510. union {
  511. BitField<20, 14, u64> offset;
  512. BitField<34, 5, u64> index;
  513. } cbuf34;
  514. union {
  515. BitField<20, 16, s64> offset;
  516. BitField<36, 5, u64> index;
  517. } cbuf36;
  518. BitField<61, 1, u64> is_b_imm;
  519. BitField<60, 1, u64> is_b_gpr;
  520. BitField<59, 1, u64> is_c_gpr;
  521. Attribute attribute;
  522. Sampler sampler;
  523. u64 value;
  524. };
  525. static_assert(sizeof(Instruction) == 0x8, "Incorrect structure size");
  526. static_assert(std::is_standard_layout_v<Instruction>, "Instruction is not standard layout");
  527. class OpCode {
  528. public:
  529. enum class Id {
  530. KIL,
  531. SSY,
  532. SYNC,
  533. DEPBAR,
  534. BFE_C,
  535. BFE_R,
  536. BFE_IMM,
  537. BRA,
  538. LD_A,
  539. LD_C,
  540. ST_A,
  541. LDG, // Load from global memory
  542. STG, // Store in global memory
  543. TEX,
  544. TEXQ, // Texture Query
  545. TEXS, // Texture Fetch with scalar/non-vec4 source/destinations
  546. TLDS, // Texture Load with scalar/non-vec4 source/destinations
  547. TLD4, // Texture Load 4
  548. TLD4S, // Texture Load 4 with scalar / non - vec4 source / destinations
  549. EXIT,
  550. IPA,
  551. FFMA_IMM, // Fused Multiply and Add
  552. FFMA_CR,
  553. FFMA_RC,
  554. FFMA_RR,
  555. FADD_C,
  556. FADD_R,
  557. FADD_IMM,
  558. FADD32I,
  559. FMUL_C,
  560. FMUL_R,
  561. FMUL_IMM,
  562. FMUL32_IMM,
  563. IADD_C,
  564. IADD_R,
  565. IADD_IMM,
  566. IADD3_C, // Add 3 Integers
  567. IADD3_R,
  568. IADD3_IMM,
  569. IADD32I,
  570. ISCADD_C, // Scale and Add
  571. ISCADD_R,
  572. ISCADD_IMM,
  573. POPC_C,
  574. POPC_R,
  575. POPC_IMM,
  576. SEL_C,
  577. SEL_R,
  578. SEL_IMM,
  579. MUFU, // Multi-Function Operator
  580. RRO_C, // Range Reduction Operator
  581. RRO_R,
  582. RRO_IMM,
  583. F2F_C,
  584. F2F_R,
  585. F2F_IMM,
  586. F2I_C,
  587. F2I_R,
  588. F2I_IMM,
  589. I2F_C,
  590. I2F_R,
  591. I2F_IMM,
  592. I2I_C,
  593. I2I_R,
  594. I2I_IMM,
  595. LOP_C,
  596. LOP_R,
  597. LOP_IMM,
  598. LOP32I,
  599. LOP3_C,
  600. LOP3_R,
  601. LOP3_IMM,
  602. MOV_C,
  603. MOV_R,
  604. MOV_IMM,
  605. MOV32_IMM,
  606. SHL_C,
  607. SHL_R,
  608. SHL_IMM,
  609. SHR_C,
  610. SHR_R,
  611. SHR_IMM,
  612. FMNMX_C,
  613. FMNMX_R,
  614. FMNMX_IMM,
  615. IMNMX_C,
  616. IMNMX_R,
  617. IMNMX_IMM,
  618. FSETP_C, // Set Predicate
  619. FSETP_R,
  620. FSETP_IMM,
  621. FSET_C,
  622. FSET_R,
  623. FSET_IMM,
  624. ISETP_C,
  625. ISETP_IMM,
  626. ISETP_R,
  627. ISET_R,
  628. ISET_C,
  629. ISET_IMM,
  630. PSETP,
  631. XMAD_IMM,
  632. XMAD_CR,
  633. XMAD_RC,
  634. XMAD_RR,
  635. };
  636. enum class Type {
  637. Trivial,
  638. Arithmetic,
  639. ArithmeticImmediate,
  640. ArithmeticInteger,
  641. ArithmeticIntegerImmediate,
  642. Bfe,
  643. Shift,
  644. Ffma,
  645. Flow,
  646. Synch,
  647. Memory,
  648. FloatSet,
  649. FloatSetPredicate,
  650. IntegerSet,
  651. IntegerSetPredicate,
  652. PredicateSetPredicate,
  653. Conversion,
  654. Xmad,
  655. Unknown,
  656. };
  657. /// Returns whether an opcode has an execution predicate field or not (ie, whether it can be
  658. /// conditionally executed).
  659. static bool IsPredicatedInstruction(Id opcode) {
  660. // TODO(Subv): Add the rest of unpredicated instructions.
  661. return opcode != Id::SSY;
  662. }
  663. class Matcher {
  664. public:
  665. Matcher(const char* const name, u16 mask, u16 expected, OpCode::Id id, OpCode::Type type)
  666. : name{name}, mask{mask}, expected{expected}, id{id}, type{type} {}
  667. const char* GetName() const {
  668. return name;
  669. }
  670. u16 GetMask() const {
  671. return mask;
  672. }
  673. Id GetId() const {
  674. return id;
  675. }
  676. Type GetType() const {
  677. return type;
  678. }
  679. /**
  680. * Tests to see if the given instruction is the instruction this matcher represents.
  681. * @param instruction The instruction to test
  682. * @returns true if the given instruction matches.
  683. */
  684. bool Matches(u16 instruction) const {
  685. return (instruction & mask) == expected;
  686. }
  687. private:
  688. const char* name;
  689. u16 mask;
  690. u16 expected;
  691. Id id;
  692. Type type;
  693. };
  694. static boost::optional<const Matcher&> Decode(Instruction instr) {
  695. static const auto table{GetDecodeTable()};
  696. const auto matches_instruction = [instr](const auto& matcher) {
  697. return matcher.Matches(static_cast<u16>(instr.opcode));
  698. };
  699. auto iter = std::find_if(table.begin(), table.end(), matches_instruction);
  700. return iter != table.end() ? boost::optional<const Matcher&>(*iter) : boost::none;
  701. }
  702. private:
  703. struct Detail {
  704. private:
  705. static constexpr size_t opcode_bitsize = 16;
  706. /**
  707. * Generates the mask and the expected value after masking from a given bitstring.
  708. * A '0' in a bitstring indicates that a zero must be present at that bit position.
  709. * A '1' in a bitstring indicates that a one must be present at that bit position.
  710. */
  711. static auto GetMaskAndExpect(const char* const bitstring) {
  712. u16 mask = 0, expect = 0;
  713. for (size_t i = 0; i < opcode_bitsize; i++) {
  714. const size_t bit_position = opcode_bitsize - i - 1;
  715. switch (bitstring[i]) {
  716. case '0':
  717. mask |= 1 << bit_position;
  718. break;
  719. case '1':
  720. expect |= 1 << bit_position;
  721. mask |= 1 << bit_position;
  722. break;
  723. default:
  724. // Ignore
  725. break;
  726. }
  727. }
  728. return std::make_tuple(mask, expect);
  729. }
  730. public:
  731. /// Creates a matcher that can match and parse instructions based on bitstring.
  732. static auto GetMatcher(const char* const bitstring, OpCode::Id op, OpCode::Type type,
  733. const char* const name) {
  734. const auto mask_expect = GetMaskAndExpect(bitstring);
  735. return Matcher(name, std::get<0>(mask_expect), std::get<1>(mask_expect), op, type);
  736. }
  737. };
  738. static std::vector<Matcher> GetDecodeTable() {
  739. std::vector<Matcher> table = {
  740. #define INST(bitstring, op, type, name) Detail::GetMatcher(bitstring, op, type, name)
  741. INST("111000110011----", Id::KIL, Type::Flow, "KIL"),
  742. INST("111000101001----", Id::SSY, Type::Flow, "SSY"),
  743. INST("111000100100----", Id::BRA, Type::Flow, "BRA"),
  744. INST("1111000011110---", Id::DEPBAR, Type::Synch, "DEPBAR"),
  745. INST("1111000011111---", Id::SYNC, Type::Synch, "SYNC"),
  746. INST("1110111111011---", Id::LD_A, Type::Memory, "LD_A"),
  747. INST("1110111110010---", Id::LD_C, Type::Memory, "LD_C"),
  748. INST("1110111111110---", Id::ST_A, Type::Memory, "ST_A"),
  749. INST("1110111011010---", Id::LDG, Type::Memory, "LDG"),
  750. INST("1110111011011---", Id::STG, Type::Memory, "STG"),
  751. INST("110000----111---", Id::TEX, Type::Memory, "TEX"),
  752. INST("1101111101001---", Id::TEXQ, Type::Memory, "TEXQ"),
  753. INST("1101100---------", Id::TEXS, Type::Memory, "TEXS"),
  754. INST("1101101---------", Id::TLDS, Type::Memory, "TLDS"),
  755. INST("110010----111---", Id::TLD4, Type::Memory, "TLD4"),
  756. INST("1101111100------", Id::TLD4S, Type::Memory, "TLD4S"),
  757. INST("111000110000----", Id::EXIT, Type::Trivial, "EXIT"),
  758. INST("11100000--------", Id::IPA, Type::Trivial, "IPA"),
  759. INST("0011001-1-------", Id::FFMA_IMM, Type::Ffma, "FFMA_IMM"),
  760. INST("010010011-------", Id::FFMA_CR, Type::Ffma, "FFMA_CR"),
  761. INST("010100011-------", Id::FFMA_RC, Type::Ffma, "FFMA_RC"),
  762. INST("010110011-------", Id::FFMA_RR, Type::Ffma, "FFMA_RR"),
  763. INST("0100110001011---", Id::FADD_C, Type::Arithmetic, "FADD_C"),
  764. INST("0101110001011---", Id::FADD_R, Type::Arithmetic, "FADD_R"),
  765. INST("0011100-01011---", Id::FADD_IMM, Type::Arithmetic, "FADD_IMM"),
  766. INST("000010----------", Id::FADD32I, Type::ArithmeticImmediate, "FADD32I"),
  767. INST("0100110001101---", Id::FMUL_C, Type::Arithmetic, "FMUL_C"),
  768. INST("0101110001101---", Id::FMUL_R, Type::Arithmetic, "FMUL_R"),
  769. INST("0011100-01101---", Id::FMUL_IMM, Type::Arithmetic, "FMUL_IMM"),
  770. INST("00011110--------", Id::FMUL32_IMM, Type::ArithmeticImmediate, "FMUL32_IMM"),
  771. INST("0100110000010---", Id::IADD_C, Type::ArithmeticInteger, "IADD_C"),
  772. INST("0101110000010---", Id::IADD_R, Type::ArithmeticInteger, "IADD_R"),
  773. INST("0011100-00010---", Id::IADD_IMM, Type::ArithmeticInteger, "IADD_IMM"),
  774. INST("010011001100----", Id::IADD3_C, Type::ArithmeticInteger, "IADD3_C"),
  775. INST("010111001100----", Id::IADD3_R, Type::ArithmeticInteger, "IADD3_R"),
  776. INST("0011100-1100----", Id::IADD3_IMM, Type::ArithmeticInteger, "IADD3_IMM"),
  777. INST("0001110---------", Id::IADD32I, Type::ArithmeticIntegerImmediate, "IADD32I"),
  778. INST("0100110000011---", Id::ISCADD_C, Type::ArithmeticInteger, "ISCADD_C"),
  779. INST("0101110000011---", Id::ISCADD_R, Type::ArithmeticInteger, "ISCADD_R"),
  780. INST("0011100-00011---", Id::ISCADD_IMM, Type::ArithmeticInteger, "ISCADD_IMM"),
  781. INST("0100110000001---", Id::POPC_C, Type::ArithmeticInteger, "POPC_C"),
  782. INST("0101110000001---", Id::POPC_R, Type::ArithmeticInteger, "POPC_R"),
  783. INST("0011100-00001---", Id::POPC_IMM, Type::ArithmeticInteger, "POPC_IMM"),
  784. INST("0100110010100---", Id::SEL_C, Type::ArithmeticInteger, "SEL_C"),
  785. INST("0101110010100---", Id::SEL_R, Type::ArithmeticInteger, "SEL_R"),
  786. INST("0011100-10100---", Id::SEL_IMM, Type::ArithmeticInteger, "SEL_IMM"),
  787. INST("0101000010000---", Id::MUFU, Type::Arithmetic, "MUFU"),
  788. INST("0100110010010---", Id::RRO_C, Type::Arithmetic, "RRO_C"),
  789. INST("0101110010010---", Id::RRO_R, Type::Arithmetic, "RRO_R"),
  790. INST("0011100-10010---", Id::RRO_IMM, Type::Arithmetic, "RRO_IMM"),
  791. INST("0100110010101---", Id::F2F_C, Type::Conversion, "F2F_C"),
  792. INST("0101110010101---", Id::F2F_R, Type::Conversion, "F2F_R"),
  793. INST("0011100-10101---", Id::F2F_IMM, Type::Conversion, "F2F_IMM"),
  794. INST("0100110010110---", Id::F2I_C, Type::Conversion, "F2I_C"),
  795. INST("0101110010110---", Id::F2I_R, Type::Conversion, "F2I_R"),
  796. INST("0011100-10110---", Id::F2I_IMM, Type::Conversion, "F2I_IMM"),
  797. INST("0100110010011---", Id::MOV_C, Type::Arithmetic, "MOV_C"),
  798. INST("0101110010011---", Id::MOV_R, Type::Arithmetic, "MOV_R"),
  799. INST("0011100-10011---", Id::MOV_IMM, Type::Arithmetic, "MOV_IMM"),
  800. INST("000000010000----", Id::MOV32_IMM, Type::ArithmeticImmediate, "MOV32_IMM"),
  801. INST("0100110001100---", Id::FMNMX_C, Type::Arithmetic, "FMNMX_C"),
  802. INST("0101110001100---", Id::FMNMX_R, Type::Arithmetic, "FMNMX_R"),
  803. INST("0011100-01100---", Id::FMNMX_IMM, Type::Arithmetic, "FMNMX_IMM"),
  804. INST("0100110000100---", Id::IMNMX_C, Type::ArithmeticInteger, "IMNMX_C"),
  805. INST("0101110000100---", Id::IMNMX_R, Type::ArithmeticInteger, "IMNMX_R"),
  806. INST("0011100-00100---", Id::IMNMX_IMM, Type::ArithmeticInteger, "IMNMX_IMM"),
  807. INST("0100110000000---", Id::BFE_C, Type::Bfe, "BFE_C"),
  808. INST("0101110000000---", Id::BFE_R, Type::Bfe, "BFE_R"),
  809. INST("0011100-00000---", Id::BFE_IMM, Type::Bfe, "BFE_IMM"),
  810. INST("0100110001000---", Id::LOP_C, Type::ArithmeticInteger, "LOP_C"),
  811. INST("0101110001000---", Id::LOP_R, Type::ArithmeticInteger, "LOP_R"),
  812. INST("0011100001000---", Id::LOP_IMM, Type::ArithmeticInteger, "LOP_IMM"),
  813. INST("000001----------", Id::LOP32I, Type::ArithmeticIntegerImmediate, "LOP32I"),
  814. INST("0000001---------", Id::LOP3_C, Type::ArithmeticInteger, "LOP3_C"),
  815. INST("0101101111100---", Id::LOP3_R, Type::ArithmeticInteger, "LOP3_R"),
  816. INST("0011110---------", Id::LOP3_IMM, Type::ArithmeticInteger, "LOP3_IMM"),
  817. INST("0100110001001---", Id::SHL_C, Type::Shift, "SHL_C"),
  818. INST("0101110001001---", Id::SHL_R, Type::Shift, "SHL_R"),
  819. INST("0011100-01001---", Id::SHL_IMM, Type::Shift, "SHL_IMM"),
  820. INST("0100110000101---", Id::SHR_C, Type::Shift, "SHR_C"),
  821. INST("0101110000101---", Id::SHR_R, Type::Shift, "SHR_R"),
  822. INST("0011100-00101---", Id::SHR_IMM, Type::Shift, "SHR_IMM"),
  823. INST("0100110011100---", Id::I2I_C, Type::Conversion, "I2I_C"),
  824. INST("0101110011100---", Id::I2I_R, Type::Conversion, "I2I_R"),
  825. INST("01110001-1000---", Id::I2I_IMM, Type::Conversion, "I2I_IMM"),
  826. INST("0100110010111---", Id::I2F_C, Type::Conversion, "I2F_C"),
  827. INST("0101110010111---", Id::I2F_R, Type::Conversion, "I2F_R"),
  828. INST("0011100-10111---", Id::I2F_IMM, Type::Conversion, "I2F_IMM"),
  829. INST("01011000--------", Id::FSET_R, Type::FloatSet, "FSET_R"),
  830. INST("0100100---------", Id::FSET_C, Type::FloatSet, "FSET_C"),
  831. INST("0011000---------", Id::FSET_IMM, Type::FloatSet, "FSET_IMM"),
  832. INST("010010111011----", Id::FSETP_C, Type::FloatSetPredicate, "FSETP_C"),
  833. INST("010110111011----", Id::FSETP_R, Type::FloatSetPredicate, "FSETP_R"),
  834. INST("0011011-1011----", Id::FSETP_IMM, Type::FloatSetPredicate, "FSETP_IMM"),
  835. INST("010010110110----", Id::ISETP_C, Type::IntegerSetPredicate, "ISETP_C"),
  836. INST("010110110110----", Id::ISETP_R, Type::IntegerSetPredicate, "ISETP_R"),
  837. INST("0011011-0110----", Id::ISETP_IMM, Type::IntegerSetPredicate, "ISETP_IMM"),
  838. INST("010110110101----", Id::ISET_R, Type::IntegerSet, "ISET_R"),
  839. INST("010010110101----", Id::ISET_C, Type::IntegerSet, "ISET_C"),
  840. INST("0011011-0101----", Id::ISET_IMM, Type::IntegerSet, "ISET_IMM"),
  841. INST("0101000010010---", Id::PSETP, Type::PredicateSetPredicate, "PSETP"),
  842. INST("0011011-00------", Id::XMAD_IMM, Type::Xmad, "XMAD_IMM"),
  843. INST("0100111---------", Id::XMAD_CR, Type::Xmad, "XMAD_CR"),
  844. INST("010100010-------", Id::XMAD_RC, Type::Xmad, "XMAD_RC"),
  845. INST("0101101100------", Id::XMAD_RR, Type::Xmad, "XMAD_RR"),
  846. };
  847. #undef INST
  848. std::stable_sort(table.begin(), table.end(), [](const auto& a, const auto& b) {
  849. // If a matcher has more bits in its mask it is more specific, so it
  850. // should come first.
  851. return std::bitset<16>(a.GetMask()).count() > std::bitset<16>(b.GetMask()).count();
  852. });
  853. return table;
  854. }
  855. };
  856. } // namespace Tegra::Shader