disassemble.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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. idDescriptor[resultId] = "ptr";
  363. break;
  364. case Op::OpTypeVector:
  365. if (idDescriptor[stream[word]].size() > 0) {
  366. if (idDescriptor[stream[word]].substr(0,2) == "bf") {
  367. idDescriptor[resultId].append(idDescriptor[stream[word]].begin(), idDescriptor[stream[word]].begin() + 2);
  368. } else {
  369. idDescriptor[resultId].append(idDescriptor[stream[word]].begin(), idDescriptor[stream[word]].begin() + 1);
  370. }
  371. if (strstr(idDescriptor[stream[word]].c_str(), "8")) {
  372. idDescriptor[resultId].append("8");
  373. }
  374. if (strstr(idDescriptor[stream[word]].c_str(), "16")) {
  375. idDescriptor[resultId].append("16");
  376. }
  377. if (strstr(idDescriptor[stream[word]].c_str(), "64")) {
  378. idDescriptor[resultId].append("64");
  379. }
  380. }
  381. idDescriptor[resultId].append("vec");
  382. switch (stream[word + 1]) {
  383. case 2: idDescriptor[resultId].append("2"); break;
  384. case 3: idDescriptor[resultId].append("3"); break;
  385. case 4: idDescriptor[resultId].append("4"); break;
  386. case 8: idDescriptor[resultId].append("8"); break;
  387. case 16: idDescriptor[resultId].append("16"); break;
  388. case 32: idDescriptor[resultId].append("32"); break;
  389. default: break;
  390. }
  391. break;
  392. default:
  393. break;
  394. }
  395. }
  396. }
  397. // Process the operands. Note, a new context-dependent set could be
  398. // swapped in mid-traversal.
  399. // Handle images specially, so can put out helpful strings.
  400. if (opCode == Op::OpTypeImage) {
  401. out << " ";
  402. disassembleIds(1);
  403. out << " " << DimensionString((int)(Dim)stream[word++]);
  404. out << (stream[word++] != 0 ? " depth" : "");
  405. out << (stream[word++] != 0 ? " array" : "");
  406. out << (stream[word++] != 0 ? " multi-sampled" : "");
  407. switch (stream[word++]) {
  408. case 0: out << " runtime"; break;
  409. case 1: out << " sampled"; break;
  410. case 2: out << " nonsampled"; break;
  411. }
  412. out << " format:" << ImageFormatString((int)(ImageFormat)stream[word++]);
  413. if (numOperands == 8) {
  414. out << " " << AccessQualifierString(stream[word++]);
  415. }
  416. return;
  417. }
  418. // Handle all the parameterized operands
  419. for (int op = 0; op < InstructionDesc[enumCast(opCode)].operands.getNum() && numOperands > 0; ++op) {
  420. out << " ";
  421. OperandClass operandClass = InstructionDesc[enumCast(opCode)].operands.getClass(op);
  422. switch (operandClass) {
  423. case OperandId:
  424. case OperandScope:
  425. case OperandMemorySemantics:
  426. disassembleIds(1);
  427. --numOperands;
  428. // Get names for printing "(XXX)" for readability, *after* this id
  429. if (opCode == Op::OpName)
  430. idDescriptor[stream[word - 1]] = decodeString().second;
  431. break;
  432. case OperandVariableIds:
  433. disassembleIds(numOperands);
  434. return;
  435. case OperandImageOperands:
  436. outputMask(OperandImageOperands, stream[word++]);
  437. --numOperands;
  438. disassembleIds(numOperands);
  439. return;
  440. case OperandOptionalLiteral:
  441. case OperandVariableLiterals:
  442. if ((opCode == Op::OpDecorate && stream[word - 1] == Decoration::BuiltIn) ||
  443. (opCode == Op::OpMemberDecorate && stream[word - 1] == Decoration::BuiltIn)) {
  444. out << BuiltInString(stream[word++]);
  445. --numOperands;
  446. ++op;
  447. }
  448. disassembleImmediates(numOperands);
  449. return;
  450. case OperandVariableIdLiteral:
  451. while (numOperands > 0) {
  452. out << std::endl;
  453. outputResultId(0);
  454. outputTypeId(0);
  455. outputIndent();
  456. out << " Type ";
  457. disassembleIds(1);
  458. out << ", member ";
  459. disassembleImmediates(1);
  460. numOperands -= 2;
  461. }
  462. return;
  463. case OperandVariableLiteralId:
  464. while (numOperands > 0) {
  465. out << std::endl;
  466. outputResultId(0);
  467. outputTypeId(0);
  468. outputIndent();
  469. out << " case ";
  470. disassembleImmediates(1);
  471. out << ": ";
  472. disassembleIds(1);
  473. numOperands -= 2;
  474. }
  475. return;
  476. case OperandLiteralNumber:
  477. disassembleImmediates(1);
  478. --numOperands;
  479. if (opCode == Op::OpExtInst) {
  480. ExtInstSet extInstSet = GLSL450Inst;
  481. const char* name = idDescriptor[stream[word - 2]].c_str();
  482. if (strcmp("OpenCL.std", name) == 0) {
  483. extInstSet = OpenCLExtInst;
  484. } else if (strcmp("OpenCL.DebugInfo.100", name) == 0) {
  485. extInstSet = OpenCLExtInst;
  486. } else if (strcmp("NonSemantic.DebugPrintf", name) == 0) {
  487. extInstSet = NonSemanticDebugPrintfExtInst;
  488. } else if (strcmp("NonSemantic.DebugBreak", name) == 0) {
  489. extInstSet = NonSemanticDebugBreakExtInst;
  490. } else if (strcmp("NonSemantic.Shader.DebugInfo.100", name) == 0) {
  491. extInstSet = NonSemanticShaderDebugInfo100;
  492. } else if (strcmp(spv::E_SPV_AMD_shader_ballot, name) == 0 ||
  493. strcmp(spv::E_SPV_AMD_shader_trinary_minmax, name) == 0 ||
  494. strcmp(spv::E_SPV_AMD_shader_explicit_vertex_parameter, name) == 0 ||
  495. strcmp(spv::E_SPV_AMD_gcn_shader, name) == 0) {
  496. extInstSet = GLSLextAMDInst;
  497. } else if (strcmp(spv::E_SPV_NV_sample_mask_override_coverage, name) == 0 ||
  498. strcmp(spv::E_SPV_NV_geometry_shader_passthrough, name) == 0 ||
  499. strcmp(spv::E_SPV_NV_viewport_array2, name) == 0 ||
  500. strcmp(spv::E_SPV_NVX_multiview_per_view_attributes, name) == 0 ||
  501. strcmp(spv::E_SPV_NV_fragment_shader_barycentric, name) == 0 ||
  502. strcmp(spv::E_SPV_NV_mesh_shader, name) == 0) {
  503. extInstSet = GLSLextNVInst;
  504. }
  505. unsigned entrypoint = stream[word - 1];
  506. if (extInstSet == GLSL450Inst) {
  507. if (entrypoint < GLSLstd450Count) {
  508. out << "(" << GlslStd450DebugNames[entrypoint] << ")";
  509. }
  510. } else if (extInstSet == GLSLextAMDInst) {
  511. out << "(" << GLSLextAMDGetDebugNames(name, entrypoint) << ")";
  512. }
  513. else if (extInstSet == GLSLextNVInst) {
  514. out << "(" << GLSLextNVGetDebugNames(name, entrypoint) << ")";
  515. } else if (extInstSet == NonSemanticDebugPrintfExtInst) {
  516. out << "(DebugPrintf)";
  517. } else if (extInstSet == NonSemanticDebugBreakExtInst) {
  518. out << "(DebugBreak)";
  519. } else if (extInstSet == NonSemanticShaderDebugInfo100) {
  520. out << "(" << NonSemanticShaderDebugInfo100GetDebugNames(entrypoint) << ")";
  521. }
  522. }
  523. break;
  524. case OperandOptionalLiteralString:
  525. case OperandLiteralString:
  526. numOperands -= disassembleString();
  527. break;
  528. case OperandVariableLiteralStrings:
  529. while (numOperands > 0)
  530. numOperands -= disassembleString();
  531. return;
  532. case OperandMemoryAccess:
  533. {
  534. outputMask(OperandMemoryAccess, stream[word++]);
  535. --numOperands;
  536. // Put a space after "None" if there are any remaining operands
  537. if (numOperands && stream[word-1] == 0) {
  538. out << " ";
  539. }
  540. uint32_t mask = stream[word-1];
  541. // Aligned is the only memory access operand that uses an immediate
  542. // value, and it is also the first operand that uses a value at all.
  543. if (mask & (uint32_t)MemoryAccessMask::Aligned) {
  544. disassembleImmediates(1);
  545. numOperands--;
  546. if (numOperands)
  547. out << " ";
  548. }
  549. uint32_t bitCount = popcount(mask & (uint32_t)(MemoryAccessMask::MakePointerAvailable | MemoryAccessMask::MakePointerVisible));
  550. disassembleIds(bitCount);
  551. numOperands -= bitCount;
  552. }
  553. break;
  554. case OperandTensorAddressingOperands:
  555. {
  556. outputMask(OperandTensorAddressingOperands, stream[word++]);
  557. --numOperands;
  558. // Put a space after "None" if there are any remaining operands
  559. if (numOperands && stream[word-1] == 0) {
  560. out << " ";
  561. }
  562. uint32_t bitCount = popcount(stream[word-1]);
  563. disassembleIds(bitCount);
  564. numOperands -= bitCount;
  565. }
  566. break;
  567. default:
  568. assert(operandClass >= OperandSource && operandClass < OperandOpcode);
  569. if (OperandClassParams[operandClass].bitmask)
  570. outputMask(operandClass, stream[word++]);
  571. else
  572. out << OperandClassParams[operandClass].getName(stream[word++]);
  573. --numOperands;
  574. break;
  575. }
  576. }
  577. return;
  578. }
  579. static void GLSLstd450GetDebugNames(const char** names)
  580. {
  581. for (int i = 0; i < GLSLstd450Count; ++i)
  582. names[i] = "Unknown";
  583. names[GLSLstd450Round] = "Round";
  584. names[GLSLstd450RoundEven] = "RoundEven";
  585. names[GLSLstd450Trunc] = "Trunc";
  586. names[GLSLstd450FAbs] = "FAbs";
  587. names[GLSLstd450SAbs] = "SAbs";
  588. names[GLSLstd450FSign] = "FSign";
  589. names[GLSLstd450SSign] = "SSign";
  590. names[GLSLstd450Floor] = "Floor";
  591. names[GLSLstd450Ceil] = "Ceil";
  592. names[GLSLstd450Fract] = "Fract";
  593. names[GLSLstd450Radians] = "Radians";
  594. names[GLSLstd450Degrees] = "Degrees";
  595. names[GLSLstd450Sin] = "Sin";
  596. names[GLSLstd450Cos] = "Cos";
  597. names[GLSLstd450Tan] = "Tan";
  598. names[GLSLstd450Asin] = "Asin";
  599. names[GLSLstd450Acos] = "Acos";
  600. names[GLSLstd450Atan] = "Atan";
  601. names[GLSLstd450Sinh] = "Sinh";
  602. names[GLSLstd450Cosh] = "Cosh";
  603. names[GLSLstd450Tanh] = "Tanh";
  604. names[GLSLstd450Asinh] = "Asinh";
  605. names[GLSLstd450Acosh] = "Acosh";
  606. names[GLSLstd450Atanh] = "Atanh";
  607. names[GLSLstd450Atan2] = "Atan2";
  608. names[GLSLstd450Pow] = "Pow";
  609. names[GLSLstd450Exp] = "Exp";
  610. names[GLSLstd450Log] = "Log";
  611. names[GLSLstd450Exp2] = "Exp2";
  612. names[GLSLstd450Log2] = "Log2";
  613. names[GLSLstd450Sqrt] = "Sqrt";
  614. names[GLSLstd450InverseSqrt] = "InverseSqrt";
  615. names[GLSLstd450Determinant] = "Determinant";
  616. names[GLSLstd450MatrixInverse] = "MatrixInverse";
  617. names[GLSLstd450Modf] = "Modf";
  618. names[GLSLstd450ModfStruct] = "ModfStruct";
  619. names[GLSLstd450FMin] = "FMin";
  620. names[GLSLstd450SMin] = "SMin";
  621. names[GLSLstd450UMin] = "UMin";
  622. names[GLSLstd450FMax] = "FMax";
  623. names[GLSLstd450SMax] = "SMax";
  624. names[GLSLstd450UMax] = "UMax";
  625. names[GLSLstd450FClamp] = "FClamp";
  626. names[GLSLstd450SClamp] = "SClamp";
  627. names[GLSLstd450UClamp] = "UClamp";
  628. names[GLSLstd450FMix] = "FMix";
  629. names[GLSLstd450Step] = "Step";
  630. names[GLSLstd450SmoothStep] = "SmoothStep";
  631. names[GLSLstd450Fma] = "Fma";
  632. names[GLSLstd450Frexp] = "Frexp";
  633. names[GLSLstd450FrexpStruct] = "FrexpStruct";
  634. names[GLSLstd450Ldexp] = "Ldexp";
  635. names[GLSLstd450PackSnorm4x8] = "PackSnorm4x8";
  636. names[GLSLstd450PackUnorm4x8] = "PackUnorm4x8";
  637. names[GLSLstd450PackSnorm2x16] = "PackSnorm2x16";
  638. names[GLSLstd450PackUnorm2x16] = "PackUnorm2x16";
  639. names[GLSLstd450PackHalf2x16] = "PackHalf2x16";
  640. names[GLSLstd450PackDouble2x32] = "PackDouble2x32";
  641. names[GLSLstd450UnpackSnorm2x16] = "UnpackSnorm2x16";
  642. names[GLSLstd450UnpackUnorm2x16] = "UnpackUnorm2x16";
  643. names[GLSLstd450UnpackHalf2x16] = "UnpackHalf2x16";
  644. names[GLSLstd450UnpackSnorm4x8] = "UnpackSnorm4x8";
  645. names[GLSLstd450UnpackUnorm4x8] = "UnpackUnorm4x8";
  646. names[GLSLstd450UnpackDouble2x32] = "UnpackDouble2x32";
  647. names[GLSLstd450Length] = "Length";
  648. names[GLSLstd450Distance] = "Distance";
  649. names[GLSLstd450Cross] = "Cross";
  650. names[GLSLstd450Normalize] = "Normalize";
  651. names[GLSLstd450FaceForward] = "FaceForward";
  652. names[GLSLstd450Reflect] = "Reflect";
  653. names[GLSLstd450Refract] = "Refract";
  654. names[GLSLstd450FindILsb] = "FindILsb";
  655. names[GLSLstd450FindSMsb] = "FindSMsb";
  656. names[GLSLstd450FindUMsb] = "FindUMsb";
  657. names[GLSLstd450InterpolateAtCentroid] = "InterpolateAtCentroid";
  658. names[GLSLstd450InterpolateAtSample] = "InterpolateAtSample";
  659. names[GLSLstd450InterpolateAtOffset] = "InterpolateAtOffset";
  660. names[GLSLstd450NMin] = "NMin";
  661. names[GLSLstd450NMax] = "NMax";
  662. names[GLSLstd450NClamp] = "NClamp";
  663. }
  664. static const char* GLSLextAMDGetDebugNames(const char* name, unsigned entrypoint)
  665. {
  666. if (strcmp(name, spv::E_SPV_AMD_shader_ballot) == 0) {
  667. switch (entrypoint) {
  668. case SwizzleInvocationsAMD: return "SwizzleInvocationsAMD";
  669. case SwizzleInvocationsMaskedAMD: return "SwizzleInvocationsMaskedAMD";
  670. case WriteInvocationAMD: return "WriteInvocationAMD";
  671. case MbcntAMD: return "MbcntAMD";
  672. default: return "Bad";
  673. }
  674. } else if (strcmp(name, spv::E_SPV_AMD_shader_trinary_minmax) == 0) {
  675. switch (entrypoint) {
  676. case FMin3AMD: return "FMin3AMD";
  677. case UMin3AMD: return "UMin3AMD";
  678. case SMin3AMD: return "SMin3AMD";
  679. case FMax3AMD: return "FMax3AMD";
  680. case UMax3AMD: return "UMax3AMD";
  681. case SMax3AMD: return "SMax3AMD";
  682. case FMid3AMD: return "FMid3AMD";
  683. case UMid3AMD: return "UMid3AMD";
  684. case SMid3AMD: return "SMid3AMD";
  685. default: return "Bad";
  686. }
  687. } else if (strcmp(name, spv::E_SPV_AMD_shader_explicit_vertex_parameter) == 0) {
  688. switch (entrypoint) {
  689. case InterpolateAtVertexAMD: return "InterpolateAtVertexAMD";
  690. default: return "Bad";
  691. }
  692. }
  693. else if (strcmp(name, spv::E_SPV_AMD_gcn_shader) == 0) {
  694. switch (entrypoint) {
  695. case CubeFaceIndexAMD: return "CubeFaceIndexAMD";
  696. case CubeFaceCoordAMD: return "CubeFaceCoordAMD";
  697. case TimeAMD: return "TimeAMD";
  698. default:
  699. break;
  700. }
  701. }
  702. return "Bad";
  703. }
  704. static const char* GLSLextNVGetDebugNames(const char* name, unsigned entrypoint)
  705. {
  706. if (strcmp(name, spv::E_SPV_NV_sample_mask_override_coverage) == 0 ||
  707. strcmp(name, spv::E_SPV_NV_geometry_shader_passthrough) == 0 ||
  708. strcmp(name, spv::E_ARB_shader_viewport_layer_array) == 0 ||
  709. strcmp(name, spv::E_SPV_NV_viewport_array2) == 0 ||
  710. strcmp(name, spv::E_SPV_NVX_multiview_per_view_attributes) == 0 ||
  711. strcmp(name, spv::E_SPV_NV_fragment_shader_barycentric) == 0 ||
  712. strcmp(name, spv::E_SPV_NV_mesh_shader) == 0 ||
  713. strcmp(name, spv::E_SPV_NV_shader_image_footprint) == 0) {
  714. switch (entrypoint) {
  715. // NV builtins
  716. case (unsigned)BuiltIn::ViewportMaskNV: return "ViewportMaskNV";
  717. case (unsigned)BuiltIn::SecondaryPositionNV: return "SecondaryPositionNV";
  718. case (unsigned)BuiltIn::SecondaryViewportMaskNV: return "SecondaryViewportMaskNV";
  719. case (unsigned)BuiltIn::PositionPerViewNV: return "PositionPerViewNV";
  720. case (unsigned)BuiltIn::ViewportMaskPerViewNV: return "ViewportMaskPerViewNV";
  721. case (unsigned)BuiltIn::BaryCoordNV: return "BaryCoordNV";
  722. case (unsigned)BuiltIn::BaryCoordNoPerspNV: return "BaryCoordNoPerspNV";
  723. case (unsigned)BuiltIn::TaskCountNV: return "TaskCountNV";
  724. case (unsigned)BuiltIn::PrimitiveCountNV: return "PrimitiveCountNV";
  725. case (unsigned)BuiltIn::PrimitiveIndicesNV: return "PrimitiveIndicesNV";
  726. case (unsigned)BuiltIn::ClipDistancePerViewNV: return "ClipDistancePerViewNV";
  727. case (unsigned)BuiltIn::CullDistancePerViewNV: return "CullDistancePerViewNV";
  728. case (unsigned)BuiltIn::LayerPerViewNV: return "LayerPerViewNV";
  729. case (unsigned)BuiltIn::MeshViewCountNV: return "MeshViewCountNV";
  730. case (unsigned)BuiltIn::MeshViewIndicesNV: return "MeshViewIndicesNV";
  731. // NV Capabilities
  732. case (unsigned)Capability::GeometryShaderPassthroughNV: return "GeometryShaderPassthroughNV";
  733. case (unsigned)Capability::ShaderViewportMaskNV: return "ShaderViewportMaskNV";
  734. case (unsigned)Capability::ShaderStereoViewNV: return "ShaderStereoViewNV";
  735. case (unsigned)Capability::PerViewAttributesNV: return "PerViewAttributesNV";
  736. case (unsigned)Capability::FragmentBarycentricNV: return "FragmentBarycentricNV";
  737. case (unsigned)Capability::MeshShadingNV: return "MeshShadingNV";
  738. case (unsigned)Capability::ImageFootprintNV: return "ImageFootprintNV";
  739. case (unsigned)Capability::SampleMaskOverrideCoverageNV:return "SampleMaskOverrideCoverageNV";
  740. // NV Decorations
  741. case (unsigned)Decoration::OverrideCoverageNV: return "OverrideCoverageNV";
  742. case (unsigned)Decoration::PassthroughNV: return "PassthroughNV";
  743. case (unsigned)Decoration::ViewportRelativeNV: return "ViewportRelativeNV";
  744. case (unsigned)Decoration::SecondaryViewportRelativeNV: return "SecondaryViewportRelativeNV";
  745. case (unsigned)Decoration::PerVertexNV: return "PerVertexNV";
  746. case (unsigned)Decoration::PerPrimitiveNV: return "PerPrimitiveNV";
  747. case (unsigned)Decoration::PerViewNV: return "PerViewNV";
  748. case (unsigned)Decoration::PerTaskNV: return "PerTaskNV";
  749. default: return "Bad";
  750. }
  751. }
  752. return "Bad";
  753. }
  754. static const char* NonSemanticShaderDebugInfo100GetDebugNames(unsigned entrypoint)
  755. {
  756. switch (entrypoint) {
  757. case NonSemanticShaderDebugInfo100DebugInfoNone: return "DebugInfoNone";
  758. case NonSemanticShaderDebugInfo100DebugCompilationUnit: return "DebugCompilationUnit";
  759. case NonSemanticShaderDebugInfo100DebugTypeBasic: return "DebugTypeBasic";
  760. case NonSemanticShaderDebugInfo100DebugTypePointer: return "DebugTypePointer";
  761. case NonSemanticShaderDebugInfo100DebugTypeQualifier: return "DebugTypeQualifier";
  762. case NonSemanticShaderDebugInfo100DebugTypeArray: return "DebugTypeArray";
  763. case NonSemanticShaderDebugInfo100DebugTypeVector: return "DebugTypeVector";
  764. case NonSemanticShaderDebugInfo100DebugTypedef: return "DebugTypedef";
  765. case NonSemanticShaderDebugInfo100DebugTypeFunction: return "DebugTypeFunction";
  766. case NonSemanticShaderDebugInfo100DebugTypeEnum: return "DebugTypeEnum";
  767. case NonSemanticShaderDebugInfo100DebugTypeComposite: return "DebugTypeComposite";
  768. case NonSemanticShaderDebugInfo100DebugTypeMember: return "DebugTypeMember";
  769. case NonSemanticShaderDebugInfo100DebugTypeInheritance: return "DebugTypeInheritance";
  770. case NonSemanticShaderDebugInfo100DebugTypePtrToMember: return "DebugTypePtrToMember";
  771. case NonSemanticShaderDebugInfo100DebugTypeTemplate: return "DebugTypeTemplate";
  772. case NonSemanticShaderDebugInfo100DebugTypeTemplateParameter: return "DebugTypeTemplateParameter";
  773. case NonSemanticShaderDebugInfo100DebugTypeTemplateTemplateParameter: return "DebugTypeTemplateTemplateParameter";
  774. case NonSemanticShaderDebugInfo100DebugTypeTemplateParameterPack: return "DebugTypeTemplateParameterPack";
  775. case NonSemanticShaderDebugInfo100DebugGlobalVariable: return "DebugGlobalVariable";
  776. case NonSemanticShaderDebugInfo100DebugFunctionDeclaration: return "DebugFunctionDeclaration";
  777. case NonSemanticShaderDebugInfo100DebugFunction: return "DebugFunction";
  778. case NonSemanticShaderDebugInfo100DebugLexicalBlock: return "DebugLexicalBlock";
  779. case NonSemanticShaderDebugInfo100DebugLexicalBlockDiscriminator: return "DebugLexicalBlockDiscriminator";
  780. case NonSemanticShaderDebugInfo100DebugScope: return "DebugScope";
  781. case NonSemanticShaderDebugInfo100DebugNoScope: return "DebugNoScope";
  782. case NonSemanticShaderDebugInfo100DebugInlinedAt: return "DebugInlinedAt";
  783. case NonSemanticShaderDebugInfo100DebugLocalVariable: return "DebugLocalVariable";
  784. case NonSemanticShaderDebugInfo100DebugInlinedVariable: return "DebugInlinedVariable";
  785. case NonSemanticShaderDebugInfo100DebugDeclare: return "DebugDeclare";
  786. case NonSemanticShaderDebugInfo100DebugValue: return "DebugValue";
  787. case NonSemanticShaderDebugInfo100DebugOperation: return "DebugOperation";
  788. case NonSemanticShaderDebugInfo100DebugExpression: return "DebugExpression";
  789. case NonSemanticShaderDebugInfo100DebugMacroDef: return "DebugMacroDef";
  790. case NonSemanticShaderDebugInfo100DebugMacroUndef: return "DebugMacroUndef";
  791. case NonSemanticShaderDebugInfo100DebugImportedEntity: return "DebugImportedEntity";
  792. case NonSemanticShaderDebugInfo100DebugSource: return "DebugSource";
  793. case NonSemanticShaderDebugInfo100DebugFunctionDefinition: return "DebugFunctionDefinition";
  794. case NonSemanticShaderDebugInfo100DebugSourceContinued: return "DebugSourceContinued";
  795. case NonSemanticShaderDebugInfo100DebugLine: return "DebugLine";
  796. case NonSemanticShaderDebugInfo100DebugNoLine: return "DebugNoLine";
  797. case NonSemanticShaderDebugInfo100DebugBuildIdentifier: return "DebugBuildIdentifier";
  798. case NonSemanticShaderDebugInfo100DebugStoragePath: return "DebugStoragePath";
  799. case NonSemanticShaderDebugInfo100DebugEntryPoint: return "DebugEntryPoint";
  800. case NonSemanticShaderDebugInfo100DebugTypeMatrix: return "DebugTypeMatrix";
  801. default: return "Bad";
  802. }
  803. return "Bad";
  804. }
  805. void Disassemble(std::ostream& out, const std::vector<unsigned int>& stream)
  806. {
  807. SpirvStream SpirvStream(out, stream);
  808. spv::Parameterize();
  809. GLSLstd450GetDebugNames(GlslStd450DebugNames);
  810. SpirvStream.validate();
  811. SpirvStream.processInstructions();
  812. }
  813. } // end namespace spv