shader_bytecode.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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<48, 1, u64> negate_b;
  272. } fmul;
  273. union {
  274. BitField<48, 1, u64> is_signed;
  275. } shift;
  276. union {
  277. BitField<39, 5, u64> shift_amount;
  278. BitField<48, 1, u64> negate_b;
  279. BitField<49, 1, u64> negate_a;
  280. } alu_integer;
  281. union {
  282. BitField<40, 1, u64> invert;
  283. } popc;
  284. union {
  285. BitField<39, 3, u64> pred;
  286. BitField<42, 1, u64> neg_pred;
  287. } sel;
  288. union {
  289. BitField<39, 3, u64> pred;
  290. BitField<42, 1, u64> negate_pred;
  291. BitField<43, 2, IMinMaxExchange> exchange;
  292. BitField<48, 1, u64> is_signed;
  293. } imnmx;
  294. union {
  295. BitField<31, 2, IAdd3Height> height_c;
  296. BitField<33, 2, IAdd3Height> height_b;
  297. BitField<35, 2, IAdd3Height> height_a;
  298. BitField<37, 2, IAdd3Mode> mode;
  299. BitField<49, 1, u64> neg_c;
  300. BitField<50, 1, u64> neg_b;
  301. BitField<51, 1, u64> neg_a;
  302. } iadd3;
  303. union {
  304. BitField<54, 1, u64> saturate;
  305. BitField<56, 1, u64> negate_a;
  306. } iadd32i;
  307. union {
  308. BitField<53, 1, u64> negate_b;
  309. BitField<54, 1, u64> abs_a;
  310. BitField<56, 1, u64> negate_a;
  311. BitField<57, 1, u64> abs_b;
  312. } fadd32i;
  313. union {
  314. BitField<20, 8, u64> shift_position;
  315. BitField<28, 8, u64> shift_length;
  316. BitField<48, 1, u64> negate_b;
  317. BitField<49, 1, u64> negate_a;
  318. u64 GetLeftShiftValue() const {
  319. return 32 - (shift_position + shift_length);
  320. }
  321. } bfe;
  322. union {
  323. BitField<0, 5, FlowCondition> cond;
  324. } flow;
  325. union {
  326. BitField<48, 1, u64> negate_b;
  327. BitField<49, 1, u64> negate_c;
  328. } ffma;
  329. union {
  330. BitField<48, 3, UniformType> type;
  331. BitField<44, 2, u64> unknown;
  332. } ld_c;
  333. union {
  334. BitField<0, 3, u64> pred0;
  335. BitField<3, 3, u64> pred3;
  336. BitField<7, 1, u64> abs_a;
  337. BitField<39, 3, u64> pred39;
  338. BitField<42, 1, u64> neg_pred;
  339. BitField<43, 1, u64> neg_a;
  340. BitField<44, 1, u64> abs_b;
  341. BitField<45, 2, PredOperation> op;
  342. BitField<47, 1, u64> ftz;
  343. BitField<48, 4, PredCondition> cond;
  344. BitField<56, 1, u64> neg_b;
  345. } fsetp;
  346. union {
  347. BitField<0, 3, u64> pred0;
  348. BitField<3, 3, u64> pred3;
  349. BitField<39, 3, u64> pred39;
  350. BitField<42, 1, u64> neg_pred;
  351. BitField<45, 2, PredOperation> op;
  352. BitField<48, 1, u64> is_signed;
  353. BitField<49, 3, PredCondition> cond;
  354. } isetp;
  355. union {
  356. BitField<0, 3, u64> pred0;
  357. BitField<3, 3, u64> pred3;
  358. BitField<12, 3, u64> pred12;
  359. BitField<15, 1, u64> neg_pred12;
  360. BitField<24, 2, PredOperation> cond;
  361. BitField<29, 3, u64> pred29;
  362. BitField<32, 1, u64> neg_pred29;
  363. BitField<39, 3, u64> pred39;
  364. BitField<42, 1, u64> neg_pred39;
  365. BitField<45, 2, PredOperation> op;
  366. } psetp;
  367. union {
  368. BitField<39, 3, u64> pred39;
  369. BitField<42, 1, u64> neg_pred;
  370. BitField<43, 1, u64> neg_a;
  371. BitField<44, 1, u64> abs_b;
  372. BitField<45, 2, PredOperation> op;
  373. BitField<48, 4, PredCondition> cond;
  374. BitField<52, 1, u64> bf;
  375. BitField<53, 1, u64> neg_b;
  376. BitField<54, 1, u64> abs_a;
  377. BitField<55, 1, u64> ftz;
  378. BitField<56, 1, u64> neg_imm;
  379. } fset;
  380. union {
  381. BitField<39, 3, u64> pred39;
  382. BitField<42, 1, u64> neg_pred;
  383. BitField<44, 1, u64> bf;
  384. BitField<45, 2, PredOperation> op;
  385. BitField<48, 1, u64> is_signed;
  386. BitField<49, 3, PredCondition> cond;
  387. } iset;
  388. union {
  389. BitField<8, 2, Register::Size> dest_size;
  390. BitField<10, 2, Register::Size> src_size;
  391. BitField<12, 1, u64> is_output_signed;
  392. BitField<13, 1, u64> is_input_signed;
  393. BitField<41, 2, u64> selector;
  394. BitField<45, 1, u64> negate_a;
  395. BitField<49, 1, u64> abs_a;
  396. union {
  397. BitField<39, 2, F2iRoundingOp> rounding;
  398. } f2i;
  399. union {
  400. BitField<39, 4, F2fRoundingOp> rounding;
  401. } f2f;
  402. } conversion;
  403. union {
  404. BitField<28, 1, u64> array;
  405. BitField<29, 2, TextureType> texture_type;
  406. BitField<31, 4, u64> component_mask;
  407. bool IsComponentEnabled(size_t component) const {
  408. return ((1ull << component) & component_mask) != 0;
  409. }
  410. } tex;
  411. union {
  412. BitField<28, 1, u64> array;
  413. BitField<29, 2, TextureType> texture_type;
  414. BitField<56, 2, u64> component;
  415. } tld4;
  416. union {
  417. BitField<52, 2, u64> component;
  418. } tld4s;
  419. union {
  420. BitField<0, 8, Register> gpr0;
  421. BitField<28, 8, Register> gpr28;
  422. BitField<50, 3, u64> component_mask_selector;
  423. BitField<53, 4, u64> texture_info;
  424. TextureType GetTextureType() const {
  425. // The TEXS instruction has a weird encoding for the texture type.
  426. if (texture_info == 0)
  427. return TextureType::Texture1D;
  428. if (texture_info >= 1 && texture_info <= 9)
  429. return TextureType::Texture2D;
  430. if (texture_info >= 10 && texture_info <= 11)
  431. return TextureType::Texture3D;
  432. if (texture_info >= 12 && texture_info <= 13)
  433. return TextureType::TextureCube;
  434. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}",
  435. static_cast<u32>(texture_info.Value()));
  436. UNREACHABLE();
  437. }
  438. bool IsArrayTexture() const {
  439. // TEXS only supports Texture2D arrays.
  440. return texture_info >= 7 && texture_info <= 9;
  441. }
  442. bool HasTwoDestinations() const {
  443. return gpr28.Value() != Register::ZeroIndex;
  444. }
  445. bool IsComponentEnabled(size_t component) const {
  446. static constexpr std::array<std::array<u32, 8>, 4> mask_lut{{
  447. {},
  448. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  449. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  450. {0x7, 0xb, 0xd, 0xe, 0xf},
  451. }};
  452. size_t index{gpr0.Value() != Register::ZeroIndex ? 1U : 0U};
  453. index |= gpr28.Value() != Register::ZeroIndex ? 2 : 0;
  454. u32 mask = mask_lut[index][component_mask_selector];
  455. // A mask of 0 means this instruction uses an unimplemented mask.
  456. ASSERT(mask != 0);
  457. return ((1ull << component) & mask) != 0;
  458. }
  459. } texs;
  460. union {
  461. BitField<53, 4, u64> texture_info;
  462. TextureType GetTextureType() const {
  463. // The TLDS instruction has a weird encoding for the texture type.
  464. if (texture_info >= 0 && texture_info <= 1) {
  465. return TextureType::Texture1D;
  466. }
  467. if (texture_info == 2 || texture_info == 8 || texture_info == 12 ||
  468. (texture_info >= 4 && texture_info <= 6)) {
  469. return TextureType::Texture2D;
  470. }
  471. if (texture_info == 7) {
  472. return TextureType::Texture3D;
  473. }
  474. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}",
  475. static_cast<u32>(texture_info.Value()));
  476. UNREACHABLE();
  477. }
  478. bool IsArrayTexture() const {
  479. // TEXS only supports Texture2D arrays.
  480. return texture_info == 8;
  481. }
  482. } tlds;
  483. union {
  484. BitField<20, 24, u64> target;
  485. BitField<5, 1, u64> constant_buffer;
  486. s32 GetBranchTarget() const {
  487. // Sign extend the branch target offset
  488. u32 mask = 1U << (24 - 1);
  489. u32 value = static_cast<u32>(target);
  490. // The branch offset is relative to the next instruction and is stored in bytes, so
  491. // divide it by the size of an instruction and add 1 to it.
  492. return static_cast<s32>((value ^ mask) - mask) / sizeof(Instruction) + 1;
  493. }
  494. } bra;
  495. union {
  496. BitField<20, 16, u64> imm20_16;
  497. BitField<36, 1, u64> product_shift_left;
  498. BitField<37, 1, u64> merge_37;
  499. BitField<48, 1, u64> sign_a;
  500. BitField<49, 1, u64> sign_b;
  501. BitField<50, 3, XmadMode> mode;
  502. BitField<52, 1, u64> high_b;
  503. BitField<53, 1, u64> high_a;
  504. BitField<56, 1, u64> merge_56;
  505. } xmad;
  506. union {
  507. BitField<20, 14, u64> offset;
  508. BitField<34, 5, u64> index;
  509. } cbuf34;
  510. union {
  511. BitField<20, 16, s64> offset;
  512. BitField<36, 5, u64> index;
  513. } cbuf36;
  514. BitField<61, 1, u64> is_b_imm;
  515. BitField<60, 1, u64> is_b_gpr;
  516. BitField<59, 1, u64> is_c_gpr;
  517. Attribute attribute;
  518. Sampler sampler;
  519. u64 value;
  520. };
  521. static_assert(sizeof(Instruction) == 0x8, "Incorrect structure size");
  522. static_assert(std::is_standard_layout_v<Instruction>, "Instruction is not standard layout");
  523. class OpCode {
  524. public:
  525. enum class Id {
  526. KIL,
  527. SSY,
  528. SYNC,
  529. DEPBAR,
  530. BFE_C,
  531. BFE_R,
  532. BFE_IMM,
  533. BRA,
  534. LD_A,
  535. LD_C,
  536. ST_A,
  537. LDG, // Load from global memory
  538. STG, // Store in global memory
  539. TEX,
  540. TEXQ, // Texture Query
  541. TEXS, // Texture Fetch with scalar/non-vec4 source/destinations
  542. TLDS, // Texture Load with scalar/non-vec4 source/destinations
  543. TLD4, // Texture Load 4
  544. TLD4S, // Texture Load 4 with scalar / non - vec4 source / destinations
  545. EXIT,
  546. IPA,
  547. FFMA_IMM, // Fused Multiply and Add
  548. FFMA_CR,
  549. FFMA_RC,
  550. FFMA_RR,
  551. FADD_C,
  552. FADD_R,
  553. FADD_IMM,
  554. FADD32I,
  555. FMUL_C,
  556. FMUL_R,
  557. FMUL_IMM,
  558. FMUL32_IMM,
  559. IADD_C,
  560. IADD_R,
  561. IADD_IMM,
  562. IADD3_C, // Add 3 Integers
  563. IADD3_R,
  564. IADD3_IMM,
  565. IADD32I,
  566. ISCADD_C, // Scale and Add
  567. ISCADD_R,
  568. ISCADD_IMM,
  569. POPC_C,
  570. POPC_R,
  571. POPC_IMM,
  572. SEL_C,
  573. SEL_R,
  574. SEL_IMM,
  575. MUFU, // Multi-Function Operator
  576. RRO_C, // Range Reduction Operator
  577. RRO_R,
  578. RRO_IMM,
  579. F2F_C,
  580. F2F_R,
  581. F2F_IMM,
  582. F2I_C,
  583. F2I_R,
  584. F2I_IMM,
  585. I2F_C,
  586. I2F_R,
  587. I2F_IMM,
  588. I2I_C,
  589. I2I_R,
  590. I2I_IMM,
  591. LOP_C,
  592. LOP_R,
  593. LOP_IMM,
  594. LOP32I,
  595. LOP3_C,
  596. LOP3_R,
  597. LOP3_IMM,
  598. MOV_C,
  599. MOV_R,
  600. MOV_IMM,
  601. MOV32_IMM,
  602. SHL_C,
  603. SHL_R,
  604. SHL_IMM,
  605. SHR_C,
  606. SHR_R,
  607. SHR_IMM,
  608. FMNMX_C,
  609. FMNMX_R,
  610. FMNMX_IMM,
  611. IMNMX_C,
  612. IMNMX_R,
  613. IMNMX_IMM,
  614. FSETP_C, // Set Predicate
  615. FSETP_R,
  616. FSETP_IMM,
  617. FSET_C,
  618. FSET_R,
  619. FSET_IMM,
  620. ISETP_C,
  621. ISETP_IMM,
  622. ISETP_R,
  623. ISET_R,
  624. ISET_C,
  625. ISET_IMM,
  626. PSETP,
  627. XMAD_IMM,
  628. XMAD_CR,
  629. XMAD_RC,
  630. XMAD_RR,
  631. };
  632. enum class Type {
  633. Trivial,
  634. Arithmetic,
  635. ArithmeticImmediate,
  636. ArithmeticInteger,
  637. ArithmeticIntegerImmediate,
  638. Bfe,
  639. Shift,
  640. Ffma,
  641. Flow,
  642. Synch,
  643. Memory,
  644. FloatSet,
  645. FloatSetPredicate,
  646. IntegerSet,
  647. IntegerSetPredicate,
  648. PredicateSetPredicate,
  649. Conversion,
  650. Xmad,
  651. Unknown,
  652. };
  653. /// Returns whether an opcode has an execution predicate field or not (ie, whether it can be
  654. /// conditionally executed).
  655. static bool IsPredicatedInstruction(Id opcode) {
  656. // TODO(Subv): Add the rest of unpredicated instructions.
  657. return opcode != Id::SSY;
  658. }
  659. class Matcher {
  660. public:
  661. Matcher(const char* const name, u16 mask, u16 expected, OpCode::Id id, OpCode::Type type)
  662. : name{name}, mask{mask}, expected{expected}, id{id}, type{type} {}
  663. const char* GetName() const {
  664. return name;
  665. }
  666. u16 GetMask() const {
  667. return mask;
  668. }
  669. Id GetId() const {
  670. return id;
  671. }
  672. Type GetType() const {
  673. return type;
  674. }
  675. /**
  676. * Tests to see if the given instruction is the instruction this matcher represents.
  677. * @param instruction The instruction to test
  678. * @returns true if the given instruction matches.
  679. */
  680. bool Matches(u16 instruction) const {
  681. return (instruction & mask) == expected;
  682. }
  683. private:
  684. const char* name;
  685. u16 mask;
  686. u16 expected;
  687. Id id;
  688. Type type;
  689. };
  690. static boost::optional<const Matcher&> Decode(Instruction instr) {
  691. static const auto table{GetDecodeTable()};
  692. const auto matches_instruction = [instr](const auto& matcher) {
  693. return matcher.Matches(static_cast<u16>(instr.opcode));
  694. };
  695. auto iter = std::find_if(table.begin(), table.end(), matches_instruction);
  696. return iter != table.end() ? boost::optional<const Matcher&>(*iter) : boost::none;
  697. }
  698. private:
  699. struct Detail {
  700. private:
  701. static constexpr size_t opcode_bitsize = 16;
  702. /**
  703. * Generates the mask and the expected value after masking from a given bitstring.
  704. * A '0' in a bitstring indicates that a zero must be present at that bit position.
  705. * A '1' in a bitstring indicates that a one must be present at that bit position.
  706. */
  707. static auto GetMaskAndExpect(const char* const bitstring) {
  708. u16 mask = 0, expect = 0;
  709. for (size_t i = 0; i < opcode_bitsize; i++) {
  710. const size_t bit_position = opcode_bitsize - i - 1;
  711. switch (bitstring[i]) {
  712. case '0':
  713. mask |= 1 << bit_position;
  714. break;
  715. case '1':
  716. expect |= 1 << bit_position;
  717. mask |= 1 << bit_position;
  718. break;
  719. default:
  720. // Ignore
  721. break;
  722. }
  723. }
  724. return std::make_tuple(mask, expect);
  725. }
  726. public:
  727. /// Creates a matcher that can match and parse instructions based on bitstring.
  728. static auto GetMatcher(const char* const bitstring, OpCode::Id op, OpCode::Type type,
  729. const char* const name) {
  730. const auto mask_expect = GetMaskAndExpect(bitstring);
  731. return Matcher(name, std::get<0>(mask_expect), std::get<1>(mask_expect), op, type);
  732. }
  733. };
  734. static std::vector<Matcher> GetDecodeTable() {
  735. std::vector<Matcher> table = {
  736. #define INST(bitstring, op, type, name) Detail::GetMatcher(bitstring, op, type, name)
  737. INST("111000110011----", Id::KIL, Type::Flow, "KIL"),
  738. INST("111000101001----", Id::SSY, Type::Flow, "SSY"),
  739. INST("111000100100----", Id::BRA, Type::Flow, "BRA"),
  740. INST("1111000011110---", Id::DEPBAR, Type::Synch, "DEPBAR"),
  741. INST("1111000011111---", Id::SYNC, Type::Synch, "SYNC"),
  742. INST("1110111111011---", Id::LD_A, Type::Memory, "LD_A"),
  743. INST("1110111110010---", Id::LD_C, Type::Memory, "LD_C"),
  744. INST("1110111111110---", Id::ST_A, Type::Memory, "ST_A"),
  745. INST("1110111011010---", Id::LDG, Type::Memory, "LDG"),
  746. INST("1110111011011---", Id::STG, Type::Memory, "STG"),
  747. INST("110000----111---", Id::TEX, Type::Memory, "TEX"),
  748. INST("1101111101001---", Id::TEXQ, Type::Memory, "TEXQ"),
  749. INST("1101100---------", Id::TEXS, Type::Memory, "TEXS"),
  750. INST("1101101---------", Id::TLDS, Type::Memory, "TLDS"),
  751. INST("110010----111---", Id::TLD4, Type::Memory, "TLD4"),
  752. INST("1101111100------", Id::TLD4S, Type::Memory, "TLD4S"),
  753. INST("111000110000----", Id::EXIT, Type::Trivial, "EXIT"),
  754. INST("11100000--------", Id::IPA, Type::Trivial, "IPA"),
  755. INST("0011001-1-------", Id::FFMA_IMM, Type::Ffma, "FFMA_IMM"),
  756. INST("010010011-------", Id::FFMA_CR, Type::Ffma, "FFMA_CR"),
  757. INST("010100011-------", Id::FFMA_RC, Type::Ffma, "FFMA_RC"),
  758. INST("010110011-------", Id::FFMA_RR, Type::Ffma, "FFMA_RR"),
  759. INST("0100110001011---", Id::FADD_C, Type::Arithmetic, "FADD_C"),
  760. INST("0101110001011---", Id::FADD_R, Type::Arithmetic, "FADD_R"),
  761. INST("0011100-01011---", Id::FADD_IMM, Type::Arithmetic, "FADD_IMM"),
  762. INST("000010----------", Id::FADD32I, Type::ArithmeticImmediate, "FADD32I"),
  763. INST("0100110001101---", Id::FMUL_C, Type::Arithmetic, "FMUL_C"),
  764. INST("0101110001101---", Id::FMUL_R, Type::Arithmetic, "FMUL_R"),
  765. INST("0011100-01101---", Id::FMUL_IMM, Type::Arithmetic, "FMUL_IMM"),
  766. INST("00011110--------", Id::FMUL32_IMM, Type::ArithmeticImmediate, "FMUL32_IMM"),
  767. INST("0100110000010---", Id::IADD_C, Type::ArithmeticInteger, "IADD_C"),
  768. INST("0101110000010---", Id::IADD_R, Type::ArithmeticInteger, "IADD_R"),
  769. INST("0011100-00010---", Id::IADD_IMM, Type::ArithmeticInteger, "IADD_IMM"),
  770. INST("010011001100----", Id::IADD3_C, Type::ArithmeticInteger, "IADD3_C"),
  771. INST("010111001100----", Id::IADD3_R, Type::ArithmeticInteger, "IADD3_R"),
  772. INST("0011100-1100----", Id::IADD3_IMM, Type::ArithmeticInteger, "IADD3_IMM"),
  773. INST("0001110---------", Id::IADD32I, Type::ArithmeticIntegerImmediate, "IADD32I"),
  774. INST("0100110000011---", Id::ISCADD_C, Type::ArithmeticInteger, "ISCADD_C"),
  775. INST("0101110000011---", Id::ISCADD_R, Type::ArithmeticInteger, "ISCADD_R"),
  776. INST("0011100-00011---", Id::ISCADD_IMM, Type::ArithmeticInteger, "ISCADD_IMM"),
  777. INST("0100110000001---", Id::POPC_C, Type::ArithmeticInteger, "POPC_C"),
  778. INST("0101110000001---", Id::POPC_R, Type::ArithmeticInteger, "POPC_R"),
  779. INST("0011100-00001---", Id::POPC_IMM, Type::ArithmeticInteger, "POPC_IMM"),
  780. INST("0100110010100---", Id::SEL_C, Type::ArithmeticInteger, "SEL_C"),
  781. INST("0101110010100---", Id::SEL_R, Type::ArithmeticInteger, "SEL_R"),
  782. INST("0011100-10100---", Id::SEL_IMM, Type::ArithmeticInteger, "SEL_IMM"),
  783. INST("0101000010000---", Id::MUFU, Type::Arithmetic, "MUFU"),
  784. INST("0100110010010---", Id::RRO_C, Type::Arithmetic, "RRO_C"),
  785. INST("0101110010010---", Id::RRO_R, Type::Arithmetic, "RRO_R"),
  786. INST("0011100-10010---", Id::RRO_IMM, Type::Arithmetic, "RRO_IMM"),
  787. INST("0100110010101---", Id::F2F_C, Type::Conversion, "F2F_C"),
  788. INST("0101110010101---", Id::F2F_R, Type::Conversion, "F2F_R"),
  789. INST("0011100-10101---", Id::F2F_IMM, Type::Conversion, "F2F_IMM"),
  790. INST("0100110010110---", Id::F2I_C, Type::Conversion, "F2I_C"),
  791. INST("0101110010110---", Id::F2I_R, Type::Conversion, "F2I_R"),
  792. INST("0011100-10110---", Id::F2I_IMM, Type::Conversion, "F2I_IMM"),
  793. INST("0100110010011---", Id::MOV_C, Type::Arithmetic, "MOV_C"),
  794. INST("0101110010011---", Id::MOV_R, Type::Arithmetic, "MOV_R"),
  795. INST("0011100-10011---", Id::MOV_IMM, Type::Arithmetic, "MOV_IMM"),
  796. INST("000000010000----", Id::MOV32_IMM, Type::ArithmeticImmediate, "MOV32_IMM"),
  797. INST("0100110001100---", Id::FMNMX_C, Type::Arithmetic, "FMNMX_C"),
  798. INST("0101110001100---", Id::FMNMX_R, Type::Arithmetic, "FMNMX_R"),
  799. INST("0011100-01100---", Id::FMNMX_IMM, Type::Arithmetic, "FMNMX_IMM"),
  800. INST("0100110000100---", Id::IMNMX_C, Type::ArithmeticInteger, "IMNMX_C"),
  801. INST("0101110000100---", Id::IMNMX_R, Type::ArithmeticInteger, "IMNMX_R"),
  802. INST("0011100-00100---", Id::IMNMX_IMM, Type::ArithmeticInteger, "IMNMX_IMM"),
  803. INST("0100110000000---", Id::BFE_C, Type::Bfe, "BFE_C"),
  804. INST("0101110000000---", Id::BFE_R, Type::Bfe, "BFE_R"),
  805. INST("0011100-00000---", Id::BFE_IMM, Type::Bfe, "BFE_IMM"),
  806. INST("0100110001000---", Id::LOP_C, Type::ArithmeticInteger, "LOP_C"),
  807. INST("0101110001000---", Id::LOP_R, Type::ArithmeticInteger, "LOP_R"),
  808. INST("0011100001000---", Id::LOP_IMM, Type::ArithmeticInteger, "LOP_IMM"),
  809. INST("000001----------", Id::LOP32I, Type::ArithmeticIntegerImmediate, "LOP32I"),
  810. INST("0000001---------", Id::LOP3_C, Type::ArithmeticInteger, "LOP3_C"),
  811. INST("0101101111100---", Id::LOP3_R, Type::ArithmeticInteger, "LOP3_R"),
  812. INST("0011110---------", Id::LOP3_IMM, Type::ArithmeticInteger, "LOP3_IMM"),
  813. INST("0100110001001---", Id::SHL_C, Type::Shift, "SHL_C"),
  814. INST("0101110001001---", Id::SHL_R, Type::Shift, "SHL_R"),
  815. INST("0011100-01001---", Id::SHL_IMM, Type::Shift, "SHL_IMM"),
  816. INST("0100110000101---", Id::SHR_C, Type::Shift, "SHR_C"),
  817. INST("0101110000101---", Id::SHR_R, Type::Shift, "SHR_R"),
  818. INST("0011100-00101---", Id::SHR_IMM, Type::Shift, "SHR_IMM"),
  819. INST("0100110011100---", Id::I2I_C, Type::Conversion, "I2I_C"),
  820. INST("0101110011100---", Id::I2I_R, Type::Conversion, "I2I_R"),
  821. INST("01110001-1000---", Id::I2I_IMM, Type::Conversion, "I2I_IMM"),
  822. INST("0100110010111---", Id::I2F_C, Type::Conversion, "I2F_C"),
  823. INST("0101110010111---", Id::I2F_R, Type::Conversion, "I2F_R"),
  824. INST("0011100-10111---", Id::I2F_IMM, Type::Conversion, "I2F_IMM"),
  825. INST("01011000--------", Id::FSET_R, Type::FloatSet, "FSET_R"),
  826. INST("0100100---------", Id::FSET_C, Type::FloatSet, "FSET_C"),
  827. INST("0011000---------", Id::FSET_IMM, Type::FloatSet, "FSET_IMM"),
  828. INST("010010111011----", Id::FSETP_C, Type::FloatSetPredicate, "FSETP_C"),
  829. INST("010110111011----", Id::FSETP_R, Type::FloatSetPredicate, "FSETP_R"),
  830. INST("0011011-1011----", Id::FSETP_IMM, Type::FloatSetPredicate, "FSETP_IMM"),
  831. INST("010010110110----", Id::ISETP_C, Type::IntegerSetPredicate, "ISETP_C"),
  832. INST("010110110110----", Id::ISETP_R, Type::IntegerSetPredicate, "ISETP_R"),
  833. INST("0011011-0110----", Id::ISETP_IMM, Type::IntegerSetPredicate, "ISETP_IMM"),
  834. INST("010110110101----", Id::ISET_R, Type::IntegerSet, "ISET_R"),
  835. INST("010010110101----", Id::ISET_C, Type::IntegerSet, "ISET_C"),
  836. INST("0011011-0101----", Id::ISET_IMM, Type::IntegerSet, "ISET_IMM"),
  837. INST("0101000010010---", Id::PSETP, Type::PredicateSetPredicate, "PSETP"),
  838. INST("0011011-00------", Id::XMAD_IMM, Type::Xmad, "XMAD_IMM"),
  839. INST("0100111---------", Id::XMAD_CR, Type::Xmad, "XMAD_CR"),
  840. INST("010100010-------", Id::XMAD_RC, Type::Xmad, "XMAD_RC"),
  841. INST("0101101100------", Id::XMAD_RR, Type::Xmad, "XMAD_RR"),
  842. };
  843. #undef INST
  844. std::stable_sort(table.begin(), table.end(), [](const auto& a, const auto& b) {
  845. // If a matcher has more bits in its mask it is more specific, so it
  846. // should come first.
  847. return std::bitset<16>(a.GetMask()).count() > std::bitset<16>(b.GetMask()).count();
  848. });
  849. return table;
  850. }
  851. };
  852. } // namespace Tegra::Shader