disassemble.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. //
  2. // Copyright (C) 2014-2015 LunarG, Inc.
  3. //
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. //
  10. // Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. //
  13. // Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following
  15. // disclaimer in the documentation and/or other materials provided
  16. // with the distribution.
  17. //
  18. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  19. // contributors may be used to endorse or promote products derived
  20. // from this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. // POSSIBILITY OF SUCH DAMAGE.
  34. //
  35. // Disassembler for SPIR-V.
  36. //
  37. #include <cstdint>
  38. #include <cstdlib>
  39. #include <cstring>
  40. #include <cassert>
  41. #include <iomanip>
  42. #include <stack>
  43. #include <sstream>
  44. #include <cstring>
  45. #include <utility>
  46. #include "disassemble.h"
  47. #include "doc.h"
  48. #include "spvUtil.h"
  49. namespace spv {
  50. extern "C" {
  51. // Include C-based headers that don't have a namespace
  52. #include "GLSL.std.450.h"
  53. #include "GLSL.ext.AMD.h"
  54. #include "GLSL.ext.NV.h"
  55. #include "GLSL.ext.ARM.h"
  56. #include "NonSemanticShaderDebugInfo100.h"
  57. #include "GLSL.ext.QCOM.h"
  58. }
  59. }
  60. static const char* GlslStd450DebugNames[spv::GLSLstd450Count];
  61. namespace spv {
  62. static const char* GLSLextAMDGetDebugNames(const char*, unsigned);
  63. static const char* GLSLextNVGetDebugNames(const char*, unsigned);
  64. static const char* NonSemanticShaderDebugInfo100GetDebugNames(unsigned);
  65. static void Kill(std::ostream& out, const char* message)
  66. {
  67. out << std::endl << "Disassembly failed: " << message << std::endl;
  68. exit(1);
  69. }
  70. // used to identify the extended instruction library imported when printing
  71. enum ExtInstSet {
  72. GLSL450Inst,
  73. GLSLextAMDInst,
  74. GLSLextNVInst,
  75. OpenCLExtInst,
  76. NonSemanticDebugPrintfExtInst,
  77. NonSemanticDebugBreakExtInst,
  78. NonSemanticShaderDebugInfo100
  79. };
  80. // Container class for a single instance of a SPIR-V stream, with methods for disassembly.
  81. class SpirvStream {
  82. public:
  83. SpirvStream(std::ostream& out, const std::vector<unsigned int>& stream) : out(out), stream(stream), word(0), nextNestedControl(0) { }
  84. virtual ~SpirvStream() { }
  85. void validate();
  86. void processInstructions();
  87. protected:
  88. SpirvStream(const SpirvStream&);
  89. SpirvStream& operator=(const SpirvStream&);
  90. Op getOpCode(int id) const { return idInstruction[id] ? (Op)(stream[idInstruction[id]] & OpCodeMask) : Op::OpNop; }
  91. // Output methods
  92. void outputIndent();
  93. void formatId(Id id, std::stringstream&);
  94. void outputResultId(Id id);
  95. void outputTypeId(Id id);
  96. void outputId(Id id);
  97. void outputMask(OperandClass operandClass, unsigned mask);
  98. void disassembleImmediates(int numOperands);
  99. void disassembleIds(int numOperands);
  100. std::pair<int, std::string> decodeString();
  101. int disassembleString();
  102. void disassembleInstruction(Id resultId, Id typeId, Op opCode, int numOperands);
  103. // Data
  104. std::ostream& out; // where to write the disassembly
  105. const std::vector<unsigned int>& stream; // the actual word stream
  106. int size; // the size of the word stream
  107. int word; // the next word of the stream to read
  108. // map each <id> to the instruction that created it
  109. Id bound;
  110. std::vector<unsigned int> idInstruction; // the word offset into the stream where the instruction for result [id] starts; 0 if not yet seen (forward reference or function parameter)
  111. std::vector<std::string> idDescriptor; // the best text string known for explaining the <id>
  112. // schema
  113. unsigned int schema;
  114. // stack of structured-merge points
  115. std::stack<Id> nestedControl;
  116. Id nextNestedControl; // need a slight delay for when we are nested
  117. };
  118. void SpirvStream::validate()
  119. {
  120. size = (int)stream.size();
  121. if (size < 4)
  122. Kill(out, "stream is too short");
  123. // Magic number
  124. if (stream[word++] != MagicNumber) {
  125. out << "Bad magic number";
  126. return;
  127. }
  128. // Version
  129. out << "// Module Version " << std::hex << stream[word++] << std::endl;
  130. // Generator's magic number
  131. out << "// Generated by (magic number): " << std::hex << stream[word++] << std::dec << std::endl;
  132. // Result <id> bound
  133. bound = stream[word++];
  134. idInstruction.resize(bound);
  135. idDescriptor.resize(bound);
  136. out << "// Id's are bound by " << bound << std::endl;
  137. out << std::endl;
  138. // Reserved schema, must be 0 for now
  139. schema = stream[word++];
  140. if (schema != 0)
  141. Kill(out, "bad schema, must be 0");
  142. }
  143. // Loop over all the instructions, in order, processing each.
  144. // Boiler plate for each is handled here directly, the rest is dispatched.
  145. void SpirvStream::processInstructions()
  146. {
  147. // Instructions
  148. while (word < size) {
  149. int instructionStart = word;
  150. // Instruction wordCount and opcode
  151. unsigned int firstWord = stream[word];
  152. unsigned wordCount = firstWord >> WordCountShift;
  153. Op opCode = (Op)(firstWord & OpCodeMask);
  154. int nextInst = word + wordCount;
  155. ++word;
  156. // Presence of full instruction
  157. if (nextInst > size)
  158. Kill(out, "stream instruction terminated too early");
  159. // Base for computing number of operands; will be updated as more is learned
  160. unsigned numOperands = wordCount - 1;
  161. // Type <id>
  162. Id typeId = 0;
  163. if (InstructionDesc[enumCast(opCode)].hasType()) {
  164. typeId = stream[word++];
  165. --numOperands;
  166. }
  167. // Result <id>
  168. Id resultId = 0;
  169. if (InstructionDesc[enumCast(opCode)].hasResult()) {
  170. resultId = stream[word++];
  171. --numOperands;
  172. // save instruction for future reference
  173. idInstruction[resultId] = instructionStart;
  174. }
  175. outputResultId(resultId);
  176. outputTypeId(typeId);
  177. outputIndent();
  178. // Hand off the Op and all its operands
  179. disassembleInstruction(resultId, typeId, opCode, numOperands);
  180. if (word != nextInst) {
  181. out << " ERROR, incorrect number of operands consumed. At " << word << " instead of " << nextInst << " instruction start was " << instructionStart;
  182. word = nextInst;
  183. }
  184. out << std::endl;
  185. }
  186. }
  187. void SpirvStream::outputIndent()
  188. {
  189. for (int i = 0; i < (int)nestedControl.size(); ++i)
  190. out << " ";
  191. }
  192. void SpirvStream::formatId(Id id, std::stringstream& idStream)
  193. {
  194. if (id != 0) {
  195. // On instructions with no IDs, this is called with "0", which does not
  196. // have to be within ID bounds on null shaders.
  197. if (id >= bound)
  198. Kill(out, "Bad <id>");
  199. idStream << id;
  200. if (idDescriptor[id].size() > 0)
  201. idStream << "(" << idDescriptor[id] << ")";
  202. }
  203. }
  204. void SpirvStream::outputResultId(Id id)
  205. {
  206. const int width = 16;
  207. std::stringstream idStream;
  208. formatId(id, idStream);
  209. out << std::setw(width) << std::right << idStream.str();
  210. if (id != 0)
  211. out << ":";
  212. else
  213. out << " ";
  214. if (nestedControl.size() && id == nestedControl.top())
  215. nestedControl.pop();
  216. }
  217. void SpirvStream::outputTypeId(Id id)
  218. {
  219. const int width = 12;
  220. std::stringstream idStream;
  221. formatId(id, idStream);
  222. out << std::setw(width) << std::right << idStream.str() << " ";
  223. }
  224. void SpirvStream::outputId(Id id)
  225. {
  226. if (id >= bound)
  227. Kill(out, "Bad <id>");
  228. out << id;
  229. if (idDescriptor[id].size() > 0)
  230. out << "(" << idDescriptor[id] << ")";
  231. }
  232. void SpirvStream::outputMask(OperandClass operandClass, unsigned mask)
  233. {
  234. if (mask == 0)
  235. out << "None";
  236. else {
  237. for (int m = 0; m < OperandClassParams[operandClass].ceiling; ++m) {
  238. if (mask & (1 << m))
  239. out << OperandClassParams[operandClass].getName(m) << " ";
  240. }
  241. }
  242. }
  243. void SpirvStream::disassembleImmediates(int numOperands)
  244. {
  245. for (int i = 0; i < numOperands; ++i) {
  246. out << stream[word++];
  247. if (i < numOperands - 1)
  248. out << " ";
  249. }
  250. }
  251. void SpirvStream::disassembleIds(int numOperands)
  252. {
  253. for (int i = 0; i < numOperands; ++i) {
  254. outputId(stream[word++]);
  255. if (i < numOperands - 1)
  256. out << " ";
  257. }
  258. }
  259. // decode string from words at current position (non-consuming)
  260. std::pair<int, std::string> SpirvStream::decodeString()
  261. {
  262. std::string res;
  263. int wordPos = word;
  264. char c;
  265. bool done = false;
  266. do {
  267. unsigned int content = stream[wordPos];
  268. for (int charCount = 0; charCount < 4; ++charCount) {
  269. c = content & 0xff;
  270. content >>= 8;
  271. if (c == '\0') {
  272. done = true;
  273. break;
  274. }
  275. res += c;
  276. }
  277. ++wordPos;
  278. } while(! done);
  279. return std::make_pair(wordPos - word, res);
  280. }
  281. // return the number of operands consumed by the string
  282. int SpirvStream::disassembleString()
  283. {
  284. out << " \"";
  285. std::pair<int, std::string> decoderes = decodeString();
  286. out << decoderes.second;
  287. out << "\"";
  288. word += decoderes.first;
  289. return decoderes.first;
  290. }
  291. static uint32_t popcount(uint32_t mask)
  292. {
  293. uint32_t count = 0;
  294. while (mask) {
  295. if (mask & 1) {
  296. count++;
  297. }
  298. mask >>= 1;
  299. }
  300. return count;
  301. }
  302. void SpirvStream::disassembleInstruction(Id resultId, Id /*typeId*/, Op opCode, int numOperands)
  303. {
  304. // Process the opcode
  305. out << (OpcodeString((int)opCode) + 2); // leave out the "Op"
  306. if (opCode == Op::OpLoopMerge || opCode == Op::OpSelectionMerge)
  307. nextNestedControl = stream[word];
  308. else if (opCode == Op::OpBranchConditional || opCode == Op::OpSwitch) {
  309. if (nextNestedControl) {
  310. nestedControl.push(nextNestedControl);
  311. nextNestedControl = 0;
  312. }
  313. } else if (opCode == Op::OpExtInstImport) {
  314. idDescriptor[resultId] = decodeString().second;
  315. }
  316. else {
  317. if (resultId != 0 && idDescriptor[resultId].size() == 0) {
  318. switch (opCode) {
  319. case Op::OpTypeInt:
  320. switch (stream[word]) {
  321. case 8: idDescriptor[resultId] = "int8_t"; break;
  322. case 16: idDescriptor[resultId] = "int16_t"; break;
  323. default: assert(0); [[fallthrough]];
  324. case 32: idDescriptor[resultId] = "int"; break;
  325. case 64: idDescriptor[resultId] = "int64_t"; break;
  326. }
  327. break;
  328. case Op::OpTypeFloat:
  329. switch (stream[word]) {
  330. case 8:
  331. case 16:
  332. if (numOperands > 1) {
  333. switch (stream[word+1]) {
  334. default:
  335. assert(0); [[fallthrough]];
  336. case (int)spv::FPEncoding::BFloat16KHR:
  337. idDescriptor[resultId] = "bfloat16_t";
  338. break;
  339. case (int)spv::FPEncoding::Float8E4M3EXT:
  340. idDescriptor[resultId] = "floate4m3_t";
  341. break;
  342. case (int)spv::FPEncoding::Float8E5M2EXT:
  343. idDescriptor[resultId] = "floate5m2_t";
  344. break;
  345. }
  346. } else {
  347. idDescriptor[resultId] = "float16_t";
  348. }
  349. break;
  350. default: assert(0); [[fallthrough]];
  351. case 32: idDescriptor[resultId] = "float"; break;
  352. case 64: idDescriptor[resultId] = "float64_t"; break;
  353. }
  354. break;
  355. case Op::OpTypeBool:
  356. idDescriptor[resultId] = "bool";
  357. break;
  358. case Op::OpTypeStruct:
  359. idDescriptor[resultId] = "struct";
  360. break;
  361. case Op::OpTypePointer:
  362. case Op::OpTypeUntypedPointerKHR:
  363. idDescriptor[resultId] = "ptr";
  364. break;
  365. case Op::OpTypeVector:
  366. if (idDescriptor[stream[word]].size() > 0) {
  367. if (idDescriptor[stream[word]].substr(0,2) == "bf") {
  368. idDescriptor[resultId].append(idDescriptor[stream[word]].begin(), idDescriptor[stream[word]].begin() + 2);
  369. } else {
  370. idDescriptor[resultId].append(idDescriptor[stream[word]].begin(), idDescriptor[stream[word]].begin() + 1);
  371. }
  372. if (strstr(idDescriptor[stream[word]].c_str(), "8")) {
  373. idDescriptor[resultId].append("8");
  374. }
  375. if (strstr(idDescriptor[stream[word]].c_str(), "16")) {
  376. idDescriptor[resultId].append("16");
  377. }
  378. if (strstr(idDescriptor[stream[word]].c_str(), "64")) {
  379. idDescriptor[resultId].append("64");
  380. }
  381. }
  382. idDescriptor[resultId].append("vec");
  383. switch (stream[word + 1]) {
  384. case 2: idDescriptor[resultId].append("2"); break;
  385. case 3: idDescriptor[resultId].append("3"); break;
  386. case 4: idDescriptor[resultId].append("4"); break;
  387. case 8: idDescriptor[resultId].append("8"); break;
  388. case 16: idDescriptor[resultId].append("16"); break;
  389. case 32: idDescriptor[resultId].append("32"); break;
  390. default: break;
  391. }
  392. break;
  393. default:
  394. break;
  395. }
  396. }
  397. }
  398. // Process the operands. Note, a new context-dependent set could be
  399. // swapped in mid-traversal.
  400. // Handle images specially, so can put out helpful strings.
  401. if (opCode == Op::OpTypeImage) {
  402. out << " ";
  403. disassembleIds(1);
  404. out << " " << DimensionString((int)(Dim)stream[word++]);
  405. out << (stream[word++] != 0 ? " depth" : "");
  406. out << (stream[word++] != 0 ? " array" : "");
  407. out << (stream[word++] != 0 ? " multi-sampled" : "");
  408. switch (stream[word++]) {
  409. case 0: out << " runtime"; break;
  410. case 1: out << " sampled"; break;
  411. case 2: out << " nonsampled"; break;
  412. }
  413. out << " format:" << ImageFormatString((int)(ImageFormat)stream[word++]);
  414. if (numOperands == 8) {
  415. out << " " << AccessQualifierString(stream[word++]);
  416. }
  417. return;
  418. }
  419. // Handle all the parameterized operands
  420. for (int op = 0; op < InstructionDesc[enumCast(opCode)].operands.getNum() && numOperands > 0; ++op) {
  421. out << " ";
  422. OperandClass operandClass = InstructionDesc[enumCast(opCode)].operands.getClass(op);
  423. switch (operandClass) {
  424. case OperandId:
  425. case OperandScope:
  426. case OperandMemorySemantics:
  427. disassembleIds(1);
  428. --numOperands;
  429. // Get names for printing "(XXX)" for readability, *after* this id
  430. if (opCode == Op::OpName)
  431. idDescriptor[stream[word - 1]] = decodeString().second;
  432. break;
  433. case OperandVariableIds:
  434. disassembleIds(numOperands);
  435. return;
  436. case OperandImageOperands:
  437. outputMask(OperandImageOperands, stream[word++]);
  438. --numOperands;
  439. disassembleIds(numOperands);
  440. return;
  441. case OperandOptionalLiteral:
  442. case OperandVariableLiterals:
  443. if ((opCode == Op::OpDecorate && stream[word - 1] == Decoration::BuiltIn) ||
  444. (opCode == Op::OpMemberDecorate && stream[word - 1] == Decoration::BuiltIn)) {
  445. out << BuiltInString(stream[word++]);
  446. --numOperands;
  447. ++op;
  448. }
  449. disassembleImmediates(numOperands);
  450. return;
  451. case OperandVariableIdLiteral:
  452. while (numOperands > 0) {
  453. out << std::endl;
  454. outputResultId(0);
  455. outputTypeId(0);
  456. outputIndent();
  457. out << " Type ";
  458. disassembleIds(1);
  459. out << ", member ";
  460. disassembleImmediates(1);
  461. numOperands -= 2;
  462. }
  463. return;
  464. case OperandVariableLiteralId:
  465. while (numOperands > 0) {
  466. out << std::endl;
  467. outputResultId(0);
  468. outputTypeId(0);
  469. outputIndent();
  470. out << " case ";
  471. disassembleImmediates(1);
  472. out << ": ";
  473. disassembleIds(1);
  474. numOperands -= 2;
  475. }
  476. return;
  477. case OperandLiteralNumber:
  478. disassembleImmediates(1);
  479. --numOperands;
  480. if (opCode == Op::OpExtInst) {
  481. ExtInstSet extInstSet = GLSL450Inst;
  482. const char* name = idDescriptor[stream[word - 2]].c_str();
  483. if (strcmp("OpenCL.std", name) == 0) {
  484. extInstSet = OpenCLExtInst;
  485. } else if (strcmp("OpenCL.DebugInfo.100", name) == 0) {
  486. extInstSet = OpenCLExtInst;
  487. } else if (strcmp("NonSemantic.DebugPrintf", name) == 0) {
  488. extInstSet = NonSemanticDebugPrintfExtInst;
  489. } else if (strcmp("NonSemantic.DebugBreak", name) == 0) {
  490. extInstSet = NonSemanticDebugBreakExtInst;
  491. } else if (strcmp("NonSemantic.Shader.DebugInfo.100", name) == 0) {
  492. extInstSet = NonSemanticShaderDebugInfo100;
  493. } else if (strcmp(spv::E_SPV_AMD_shader_ballot, name) == 0 ||
  494. strcmp(spv::E_SPV_AMD_shader_trinary_minmax, name) == 0 ||
  495. strcmp(spv::E_SPV_AMD_shader_explicit_vertex_parameter, name) == 0 ||
  496. strcmp(spv::E_SPV_AMD_gcn_shader, name) == 0) {
  497. extInstSet = GLSLextAMDInst;
  498. } else if (strcmp(spv::E_SPV_NV_sample_mask_override_coverage, name) == 0 ||
  499. strcmp(spv::E_SPV_NV_geometry_shader_passthrough, name) == 0 ||
  500. strcmp(spv::E_SPV_NV_viewport_array2, name) == 0 ||
  501. strcmp(spv::E_SPV_NVX_multiview_per_view_attributes, name) == 0 ||
  502. strcmp(spv::E_SPV_NV_fragment_shader_barycentric, name) == 0 ||
  503. strcmp(spv::E_SPV_NV_mesh_shader, name) == 0) {
  504. extInstSet = GLSLextNVInst;
  505. }
  506. unsigned entrypoint = stream[word - 1];
  507. if (extInstSet == GLSL450Inst) {
  508. if (entrypoint < GLSLstd450Count) {
  509. out << "(" << GlslStd450DebugNames[entrypoint] << ")";
  510. }
  511. } else if (extInstSet == GLSLextAMDInst) {
  512. out << "(" << GLSLextAMDGetDebugNames(name, entrypoint) << ")";
  513. }
  514. else if (extInstSet == GLSLextNVInst) {
  515. out << "(" << GLSLextNVGetDebugNames(name, entrypoint) << ")";
  516. } else if (extInstSet == NonSemanticDebugPrintfExtInst) {
  517. out << "(DebugPrintf)";
  518. } else if (extInstSet == NonSemanticDebugBreakExtInst) {
  519. out << "(DebugBreak)";
  520. } else if (extInstSet == NonSemanticShaderDebugInfo100) {
  521. out << "(" << NonSemanticShaderDebugInfo100GetDebugNames(entrypoint) << ")";
  522. }
  523. }
  524. break;
  525. case OperandOptionalLiteralString:
  526. case OperandLiteralString:
  527. numOperands -= disassembleString();
  528. break;
  529. case OperandVariableLiteralStrings:
  530. while (numOperands > 0)
  531. numOperands -= disassembleString();
  532. return;
  533. case OperandMemoryAccess:
  534. {
  535. outputMask(OperandMemoryAccess, stream[word++]);
  536. --numOperands;
  537. // Put a space after "None" if there are any remaining operands
  538. if (numOperands && stream[word-1] == 0) {
  539. out << " ";
  540. }
  541. uint32_t mask = stream[word-1];
  542. // Aligned is the only memory access operand that uses an immediate
  543. // value, and it is also the first operand that uses a value at all.
  544. if (mask & (uint32_t)MemoryAccessMask::Aligned) {
  545. disassembleImmediates(1);
  546. numOperands--;
  547. if (numOperands)
  548. out << " ";
  549. }
  550. uint32_t bitCount = popcount(mask & (uint32_t)(MemoryAccessMask::MakePointerAvailable | MemoryAccessMask::MakePointerVisible));
  551. disassembleIds(bitCount);
  552. numOperands -= bitCount;
  553. }
  554. break;
  555. case OperandTensorAddressingOperands:
  556. {
  557. outputMask(OperandTensorAddressingOperands, stream[word++]);
  558. --numOperands;
  559. // Put a space after "None" if there are any remaining operands
  560. if (numOperands && stream[word-1] == 0) {
  561. out << " ";
  562. }
  563. uint32_t bitCount = popcount(stream[word-1]);
  564. disassembleIds(bitCount);
  565. numOperands -= bitCount;
  566. }
  567. break;
  568. default:
  569. assert(operandClass >= OperandSource && operandClass < OperandOpcode);
  570. if (OperandClassParams[operandClass].bitmask)
  571. outputMask(operandClass, stream[word++]);
  572. else
  573. out << OperandClassParams[operandClass].getName(stream[word++]);
  574. --numOperands;
  575. break;
  576. }
  577. }
  578. return;
  579. }
  580. static void GLSLstd450GetDebugNames(const char** names)
  581. {
  582. for (int i = 0; i < GLSLstd450Count; ++i)
  583. names[i] = "Unknown";
  584. names[GLSLstd450Round] = "Round";
  585. names[GLSLstd450RoundEven] = "RoundEven";
  586. names[GLSLstd450Trunc] = "Trunc";
  587. names[GLSLstd450FAbs] = "FAbs";
  588. names[GLSLstd450SAbs] = "SAbs";
  589. names[GLSLstd450FSign] = "FSign";
  590. names[GLSLstd450SSign] = "SSign";
  591. names[GLSLstd450Floor] = "Floor";
  592. names[GLSLstd450Ceil] = "Ceil";
  593. names[GLSLstd450Fract] = "Fract";
  594. names[GLSLstd450Radians] = "Radians";
  595. names[GLSLstd450Degrees] = "Degrees";
  596. names[GLSLstd450Sin] = "Sin";
  597. names[GLSLstd450Cos] = "Cos";
  598. names[GLSLstd450Tan] = "Tan";
  599. names[GLSLstd450Asin] = "Asin";
  600. names[GLSLstd450Acos] = "Acos";
  601. names[GLSLstd450Atan] = "Atan";
  602. names[GLSLstd450Sinh] = "Sinh";
  603. names[GLSLstd450Cosh] = "Cosh";
  604. names[GLSLstd450Tanh] = "Tanh";
  605. names[GLSLstd450Asinh] = "Asinh";
  606. names[GLSLstd450Acosh] = "Acosh";
  607. names[GLSLstd450Atanh] = "Atanh";
  608. names[GLSLstd450Atan2] = "Atan2";
  609. names[GLSLstd450Pow] = "Pow";
  610. names[GLSLstd450Exp] = "Exp";
  611. names[GLSLstd450Log] = "Log";
  612. names[GLSLstd450Exp2] = "Exp2";
  613. names[GLSLstd450Log2] = "Log2";
  614. names[GLSLstd450Sqrt] = "Sqrt";
  615. names[GLSLstd450InverseSqrt] = "InverseSqrt";
  616. names[GLSLstd450Determinant] = "Determinant";
  617. names[GLSLstd450MatrixInverse] = "MatrixInverse";
  618. names[GLSLstd450Modf] = "Modf";
  619. names[GLSLstd450ModfStruct] = "ModfStruct";
  620. names[GLSLstd450FMin] = "FMin";
  621. names[GLSLstd450SMin] = "SMin";
  622. names[GLSLstd450UMin] = "UMin";
  623. names[GLSLstd450FMax] = "FMax";
  624. names[GLSLstd450SMax] = "SMax";
  625. names[GLSLstd450UMax] = "UMax";
  626. names[GLSLstd450FClamp] = "FClamp";
  627. names[GLSLstd450SClamp] = "SClamp";
  628. names[GLSLstd450UClamp] = "UClamp";
  629. names[GLSLstd450FMix] = "FMix";
  630. names[GLSLstd450Step] = "Step";
  631. names[GLSLstd450SmoothStep] = "SmoothStep";
  632. names[GLSLstd450Fma] = "Fma";
  633. names[GLSLstd450Frexp] = "Frexp";
  634. names[GLSLstd450FrexpStruct] = "FrexpStruct";
  635. names[GLSLstd450Ldexp] = "Ldexp";
  636. names[GLSLstd450PackSnorm4x8] = "PackSnorm4x8";
  637. names[GLSLstd450PackUnorm4x8] = "PackUnorm4x8";
  638. names[GLSLstd450PackSnorm2x16] = "PackSnorm2x16";
  639. names[GLSLstd450PackUnorm2x16] = "PackUnorm2x16";
  640. names[GLSLstd450PackHalf2x16] = "PackHalf2x16";
  641. names[GLSLstd450PackDouble2x32] = "PackDouble2x32";
  642. names[GLSLstd450UnpackSnorm2x16] = "UnpackSnorm2x16";
  643. names[GLSLstd450UnpackUnorm2x16] = "UnpackUnorm2x16";
  644. names[GLSLstd450UnpackHalf2x16] = "UnpackHalf2x16";
  645. names[GLSLstd450UnpackSnorm4x8] = "UnpackSnorm4x8";
  646. names[GLSLstd450UnpackUnorm4x8] = "UnpackUnorm4x8";
  647. names[GLSLstd450UnpackDouble2x32] = "UnpackDouble2x32";
  648. names[GLSLstd450Length] = "Length";
  649. names[GLSLstd450Distance] = "Distance";
  650. names[GLSLstd450Cross] = "Cross";
  651. names[GLSLstd450Normalize] = "Normalize";
  652. names[GLSLstd450FaceForward] = "FaceForward";
  653. names[GLSLstd450Reflect] = "Reflect";
  654. names[GLSLstd450Refract] = "Refract";
  655. names[GLSLstd450FindILsb] = "FindILsb";
  656. names[GLSLstd450FindSMsb] = "FindSMsb";
  657. names[GLSLstd450FindUMsb] = "FindUMsb";
  658. names[GLSLstd450InterpolateAtCentroid] = "InterpolateAtCentroid";
  659. names[GLSLstd450InterpolateAtSample] = "InterpolateAtSample";
  660. names[GLSLstd450InterpolateAtOffset] = "InterpolateAtOffset";
  661. names[GLSLstd450NMin] = "NMin";
  662. names[GLSLstd450NMax] = "NMax";
  663. names[GLSLstd450NClamp] = "NClamp";
  664. }
  665. static const char* GLSLextAMDGetDebugNames(const char* name, unsigned entrypoint)
  666. {
  667. if (strcmp(name, spv::E_SPV_AMD_shader_ballot) == 0) {
  668. switch (entrypoint) {
  669. case SwizzleInvocationsAMD: return "SwizzleInvocationsAMD";
  670. case SwizzleInvocationsMaskedAMD: return "SwizzleInvocationsMaskedAMD";
  671. case WriteInvocationAMD: return "WriteInvocationAMD";
  672. case MbcntAMD: return "MbcntAMD";
  673. default: return "Bad";
  674. }
  675. } else if (strcmp(name, spv::E_SPV_AMD_shader_trinary_minmax) == 0) {
  676. switch (entrypoint) {
  677. case FMin3AMD: return "FMin3AMD";
  678. case UMin3AMD: return "UMin3AMD";
  679. case SMin3AMD: return "SMin3AMD";
  680. case FMax3AMD: return "FMax3AMD";
  681. case UMax3AMD: return "UMax3AMD";
  682. case SMax3AMD: return "SMax3AMD";
  683. case FMid3AMD: return "FMid3AMD";
  684. case UMid3AMD: return "UMid3AMD";
  685. case SMid3AMD: return "SMid3AMD";
  686. default: return "Bad";
  687. }
  688. } else if (strcmp(name, spv::E_SPV_AMD_shader_explicit_vertex_parameter) == 0) {
  689. switch (entrypoint) {
  690. case InterpolateAtVertexAMD: return "InterpolateAtVertexAMD";
  691. default: return "Bad";
  692. }
  693. }
  694. else if (strcmp(name, spv::E_SPV_AMD_gcn_shader) == 0) {
  695. switch (entrypoint) {
  696. case CubeFaceIndexAMD: return "CubeFaceIndexAMD";
  697. case CubeFaceCoordAMD: return "CubeFaceCoordAMD";
  698. case TimeAMD: return "TimeAMD";
  699. default:
  700. break;
  701. }
  702. }
  703. return "Bad";
  704. }
  705. static const char* GLSLextNVGetDebugNames(const char* name, unsigned entrypoint)
  706. {
  707. if (strcmp(name, spv::E_SPV_NV_sample_mask_override_coverage) == 0 ||
  708. strcmp(name, spv::E_SPV_NV_geometry_shader_passthrough) == 0 ||
  709. strcmp(name, spv::E_ARB_shader_viewport_layer_array) == 0 ||
  710. strcmp(name, spv::E_SPV_NV_viewport_array2) == 0 ||
  711. strcmp(name, spv::E_SPV_NVX_multiview_per_view_attributes) == 0 ||
  712. strcmp(name, spv::E_SPV_NV_fragment_shader_barycentric) == 0 ||
  713. strcmp(name, spv::E_SPV_NV_mesh_shader) == 0 ||
  714. strcmp(name, spv::E_SPV_NV_shader_image_footprint) == 0) {
  715. switch (entrypoint) {
  716. // NV builtins
  717. case (unsigned)BuiltIn::ViewportMaskNV: return "ViewportMaskNV";
  718. case (unsigned)BuiltIn::SecondaryPositionNV: return "SecondaryPositionNV";
  719. case (unsigned)BuiltIn::SecondaryViewportMaskNV: return "SecondaryViewportMaskNV";
  720. case (unsigned)BuiltIn::PositionPerViewNV: return "PositionPerViewNV";
  721. case (unsigned)BuiltIn::ViewportMaskPerViewNV: return "ViewportMaskPerViewNV";
  722. case (unsigned)BuiltIn::BaryCoordNV: return "BaryCoordNV";
  723. case (unsigned)BuiltIn::BaryCoordNoPerspNV: return "BaryCoordNoPerspNV";
  724. case (unsigned)BuiltIn::TaskCountNV: return "TaskCountNV";
  725. case (unsigned)BuiltIn::PrimitiveCountNV: return "PrimitiveCountNV";
  726. case (unsigned)BuiltIn::PrimitiveIndicesNV: return "PrimitiveIndicesNV";
  727. case (unsigned)BuiltIn::ClipDistancePerViewNV: return "ClipDistancePerViewNV";
  728. case (unsigned)BuiltIn::CullDistancePerViewNV: return "CullDistancePerViewNV";
  729. case (unsigned)BuiltIn::LayerPerViewNV: return "LayerPerViewNV";
  730. case (unsigned)BuiltIn::MeshViewCountNV: return "MeshViewCountNV";
  731. case (unsigned)BuiltIn::MeshViewIndicesNV: return "MeshViewIndicesNV";
  732. // NV Capabilities
  733. case (unsigned)Capability::GeometryShaderPassthroughNV: return "GeometryShaderPassthroughNV";
  734. case (unsigned)Capability::ShaderViewportMaskNV: return "ShaderViewportMaskNV";
  735. case (unsigned)Capability::ShaderStereoViewNV: return "ShaderStereoViewNV";
  736. case (unsigned)Capability::PerViewAttributesNV: return "PerViewAttributesNV";
  737. case (unsigned)Capability::FragmentBarycentricNV: return "FragmentBarycentricNV";
  738. case (unsigned)Capability::MeshShadingNV: return "MeshShadingNV";
  739. case (unsigned)Capability::ImageFootprintNV: return "ImageFootprintNV";
  740. case (unsigned)Capability::SampleMaskOverrideCoverageNV:return "SampleMaskOverrideCoverageNV";
  741. // NV Decorations
  742. case (unsigned)Decoration::OverrideCoverageNV: return "OverrideCoverageNV";
  743. case (unsigned)Decoration::PassthroughNV: return "PassthroughNV";
  744. case (unsigned)Decoration::ViewportRelativeNV: return "ViewportRelativeNV";
  745. case (unsigned)Decoration::SecondaryViewportRelativeNV: return "SecondaryViewportRelativeNV";
  746. case (unsigned)Decoration::PerVertexNV: return "PerVertexNV";
  747. case (unsigned)Decoration::PerPrimitiveNV: return "PerPrimitiveNV";
  748. case (unsigned)Decoration::PerViewNV: return "PerViewNV";
  749. case (unsigned)Decoration::PerTaskNV: return "PerTaskNV";
  750. default: return "Bad";
  751. }
  752. }
  753. return "Bad";
  754. }
  755. static const char* NonSemanticShaderDebugInfo100GetDebugNames(unsigned entrypoint)
  756. {
  757. switch (entrypoint) {
  758. case NonSemanticShaderDebugInfo100DebugInfoNone: return "DebugInfoNone";
  759. case NonSemanticShaderDebugInfo100DebugCompilationUnit: return "DebugCompilationUnit";
  760. case NonSemanticShaderDebugInfo100DebugTypeBasic: return "DebugTypeBasic";
  761. case NonSemanticShaderDebugInfo100DebugTypePointer: return "DebugTypePointer";
  762. case NonSemanticShaderDebugInfo100DebugTypeQualifier: return "DebugTypeQualifier";
  763. case NonSemanticShaderDebugInfo100DebugTypeArray: return "DebugTypeArray";
  764. case NonSemanticShaderDebugInfo100DebugTypeVector: return "DebugTypeVector";
  765. case NonSemanticShaderDebugInfo100DebugTypedef: return "DebugTypedef";
  766. case NonSemanticShaderDebugInfo100DebugTypeFunction: return "DebugTypeFunction";
  767. case NonSemanticShaderDebugInfo100DebugTypeEnum: return "DebugTypeEnum";
  768. case NonSemanticShaderDebugInfo100DebugTypeComposite: return "DebugTypeComposite";
  769. case NonSemanticShaderDebugInfo100DebugTypeMember: return "DebugTypeMember";
  770. case NonSemanticShaderDebugInfo100DebugTypeInheritance: return "DebugTypeInheritance";
  771. case NonSemanticShaderDebugInfo100DebugTypePtrToMember: return "DebugTypePtrToMember";
  772. case NonSemanticShaderDebugInfo100DebugTypeTemplate: return "DebugTypeTemplate";
  773. case NonSemanticShaderDebugInfo100DebugTypeTemplateParameter: return "DebugTypeTemplateParameter";
  774. case NonSemanticShaderDebugInfo100DebugTypeTemplateTemplateParameter: return "DebugTypeTemplateTemplateParameter";
  775. case NonSemanticShaderDebugInfo100DebugTypeTemplateParameterPack: return "DebugTypeTemplateParameterPack";
  776. case NonSemanticShaderDebugInfo100DebugGlobalVariable: return "DebugGlobalVariable";
  777. case NonSemanticShaderDebugInfo100DebugFunctionDeclaration: return "DebugFunctionDeclaration";
  778. case NonSemanticShaderDebugInfo100DebugFunction: return "DebugFunction";
  779. case NonSemanticShaderDebugInfo100DebugLexicalBlock: return "DebugLexicalBlock";
  780. case NonSemanticShaderDebugInfo100DebugLexicalBlockDiscriminator: return "DebugLexicalBlockDiscriminator";
  781. case NonSemanticShaderDebugInfo100DebugScope: return "DebugScope";
  782. case NonSemanticShaderDebugInfo100DebugNoScope: return "DebugNoScope";
  783. case NonSemanticShaderDebugInfo100DebugInlinedAt: return "DebugInlinedAt";
  784. case NonSemanticShaderDebugInfo100DebugLocalVariable: return "DebugLocalVariable";
  785. case NonSemanticShaderDebugInfo100DebugInlinedVariable: return "DebugInlinedVariable";
  786. case NonSemanticShaderDebugInfo100DebugDeclare: return "DebugDeclare";
  787. case NonSemanticShaderDebugInfo100DebugValue: return "DebugValue";
  788. case NonSemanticShaderDebugInfo100DebugOperation: return "DebugOperation";
  789. case NonSemanticShaderDebugInfo100DebugExpression: return "DebugExpression";
  790. case NonSemanticShaderDebugInfo100DebugMacroDef: return "DebugMacroDef";
  791. case NonSemanticShaderDebugInfo100DebugMacroUndef: return "DebugMacroUndef";
  792. case NonSemanticShaderDebugInfo100DebugImportedEntity: return "DebugImportedEntity";
  793. case NonSemanticShaderDebugInfo100DebugSource: return "DebugSource";
  794. case NonSemanticShaderDebugInfo100DebugFunctionDefinition: return "DebugFunctionDefinition";
  795. case NonSemanticShaderDebugInfo100DebugSourceContinued: return "DebugSourceContinued";
  796. case NonSemanticShaderDebugInfo100DebugLine: return "DebugLine";
  797. case NonSemanticShaderDebugInfo100DebugNoLine: return "DebugNoLine";
  798. case NonSemanticShaderDebugInfo100DebugBuildIdentifier: return "DebugBuildIdentifier";
  799. case NonSemanticShaderDebugInfo100DebugStoragePath: return "DebugStoragePath";
  800. case NonSemanticShaderDebugInfo100DebugEntryPoint: return "DebugEntryPoint";
  801. case NonSemanticShaderDebugInfo100DebugTypeMatrix: return "DebugTypeMatrix";
  802. default: return "Bad";
  803. }
  804. return "Bad";
  805. }
  806. void Disassemble(std::ostream& out, const std::vector<unsigned int>& stream)
  807. {
  808. SpirvStream SpirvStream(out, stream);
  809. spv::Parameterize();
  810. GLSLstd450GetDebugNames(GlslStd450DebugNames);
  811. SpirvStream.validate();
  812. SpirvStream.processInstructions();
  813. }
  814. } // end namespace spv