shader_bytecode.h 29 KB

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