Upgrade to 13.1.1(vulkan-sdk-1.3.268.0)
[platform/upstream/glslang.git] / SPIRV / disassemble.cpp
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 //
36 // Disassembler for SPIR-V.
37 //
38
39 #include <cstdlib>
40 #include <cstring>
41 #include <cassert>
42 #include <iomanip>
43 #include <stack>
44 #include <sstream>
45 #include <cstring>
46 #include <utility>
47
48 #include "disassemble.h"
49 #include "doc.h"
50
51 namespace spv {
52     extern "C" {
53         // Include C-based headers that don't have a namespace
54         #include "GLSL.std.450.h"
55         #include "GLSL.ext.AMD.h"
56         #include "GLSL.ext.NV.h"
57         #include "GLSL.ext.ARM.h"
58         #include "NonSemanticShaderDebugInfo100.h"
59         #include "GLSL.ext.QCOM.h"
60     }
61 }
62 const char* GlslStd450DebugNames[spv::GLSLstd450Count];
63
64 namespace spv {
65
66 static const char* GLSLextAMDGetDebugNames(const char*, unsigned);
67 static const char* GLSLextNVGetDebugNames(const char*, unsigned);
68 static const char* NonSemanticShaderDebugInfo100GetDebugNames(unsigned);
69
70 static void Kill(std::ostream& out, const char* message)
71 {
72     out << std::endl << "Disassembly failed: " << message << std::endl;
73     exit(1);
74 }
75
76 // used to identify the extended instruction library imported when printing
77 enum ExtInstSet {
78     GLSL450Inst,
79     GLSLextAMDInst,
80     GLSLextNVInst,
81     OpenCLExtInst,
82     NonSemanticDebugPrintfExtInst,
83     NonSemanticShaderDebugInfo100
84 };
85
86 // Container class for a single instance of a SPIR-V stream, with methods for disassembly.
87 class SpirvStream {
88 public:
89     SpirvStream(std::ostream& out, const std::vector<unsigned int>& stream) : out(out), stream(stream), word(0), nextNestedControl(0) { }
90     virtual ~SpirvStream() { }
91
92     void validate();
93     void processInstructions();
94
95 protected:
96     SpirvStream(const SpirvStream&);
97     SpirvStream& operator=(const SpirvStream&);
98     Op getOpCode(int id) const { return idInstruction[id] ? (Op)(stream[idInstruction[id]] & OpCodeMask) : OpNop; }
99
100     // Output methods
101     void outputIndent();
102     void formatId(Id id, std::stringstream&);
103     void outputResultId(Id id);
104     void outputTypeId(Id id);
105     void outputId(Id id);
106     void outputMask(OperandClass operandClass, unsigned mask);
107     void disassembleImmediates(int numOperands);
108     void disassembleIds(int numOperands);
109     std::pair<int, std::string> decodeString();
110     int disassembleString();
111     void disassembleInstruction(Id resultId, Id typeId, Op opCode, int numOperands);
112
113     // Data
114     std::ostream& out;                       // where to write the disassembly
115     const std::vector<unsigned int>& stream; // the actual word stream
116     int size;                                // the size of the word stream
117     int word;                                // the next word of the stream to read
118
119     // map each <id> to the instruction that created it
120     Id bound;
121     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)
122
123     std::vector<std::string> idDescriptor;    // the best text string known for explaining the <id>
124
125     // schema
126     unsigned int schema;
127
128     // stack of structured-merge points
129     std::stack<Id> nestedControl;
130     Id nextNestedControl;         // need a slight delay for when we are nested
131 };
132
133 void SpirvStream::validate()
134 {
135     size = (int)stream.size();
136     if (size < 4)
137         Kill(out, "stream is too short");
138
139     // Magic number
140     if (stream[word++] != MagicNumber) {
141         out << "Bad magic number";
142         return;
143     }
144
145     // Version
146     out << "// Module Version " << std::hex << stream[word++] << std::endl;
147
148     // Generator's magic number
149     out << "// Generated by (magic number): " << std::hex << stream[word++] << std::dec << std::endl;
150
151     // Result <id> bound
152     bound = stream[word++];
153     idInstruction.resize(bound);
154     idDescriptor.resize(bound);
155     out << "// Id's are bound by " << bound << std::endl;
156     out << std::endl;
157
158     // Reserved schema, must be 0 for now
159     schema = stream[word++];
160     if (schema != 0)
161         Kill(out, "bad schema, must be 0");
162 }
163
164 // Loop over all the instructions, in order, processing each.
165 // Boiler plate for each is handled here directly, the rest is dispatched.
166 void SpirvStream::processInstructions()
167 {
168     // Instructions
169     while (word < size) {
170         int instructionStart = word;
171
172         // Instruction wordCount and opcode
173         unsigned int firstWord = stream[word];
174         unsigned wordCount = firstWord >> WordCountShift;
175         Op opCode = (Op)(firstWord & OpCodeMask);
176         int nextInst = word + wordCount;
177         ++word;
178
179         // Presence of full instruction
180         if (nextInst > size)
181             Kill(out, "stream instruction terminated too early");
182
183         // Base for computing number of operands; will be updated as more is learned
184         unsigned numOperands = wordCount - 1;
185
186         // Type <id>
187         Id typeId = 0;
188         if (InstructionDesc[opCode].hasType()) {
189             typeId = stream[word++];
190             --numOperands;
191         }
192
193         // Result <id>
194         Id resultId = 0;
195         if (InstructionDesc[opCode].hasResult()) {
196             resultId = stream[word++];
197             --numOperands;
198
199             // save instruction for future reference
200             idInstruction[resultId] = instructionStart;
201         }
202
203         outputResultId(resultId);
204         outputTypeId(typeId);
205         outputIndent();
206
207         // Hand off the Op and all its operands
208         disassembleInstruction(resultId, typeId, opCode, numOperands);
209         if (word != nextInst) {
210             out << " ERROR, incorrect number of operands consumed.  At " << word << " instead of " << nextInst << " instruction start was " << instructionStart;
211             word = nextInst;
212         }
213         out << std::endl;
214     }
215 }
216
217 void SpirvStream::outputIndent()
218 {
219     for (int i = 0; i < (int)nestedControl.size(); ++i)
220         out << "  ";
221 }
222
223 void SpirvStream::formatId(Id id, std::stringstream& idStream)
224 {
225     if (id != 0) {
226         // On instructions with no IDs, this is called with "0", which does not
227         // have to be within ID bounds on null shaders.
228         if (id >= bound)
229             Kill(out, "Bad <id>");
230
231         idStream << id;
232         if (idDescriptor[id].size() > 0)
233             idStream << "(" << idDescriptor[id] << ")";
234     }
235 }
236
237 void SpirvStream::outputResultId(Id id)
238 {
239     const int width = 16;
240     std::stringstream idStream;
241     formatId(id, idStream);
242     out << std::setw(width) << std::right << idStream.str();
243     if (id != 0)
244         out << ":";
245     else
246         out << " ";
247
248     if (nestedControl.size() && id == nestedControl.top())
249         nestedControl.pop();
250 }
251
252 void SpirvStream::outputTypeId(Id id)
253 {
254     const int width = 12;
255     std::stringstream idStream;
256     formatId(id, idStream);
257     out << std::setw(width) << std::right << idStream.str() << " ";
258 }
259
260 void SpirvStream::outputId(Id id)
261 {
262     if (id >= bound)
263         Kill(out, "Bad <id>");
264
265     out << id;
266     if (idDescriptor[id].size() > 0)
267         out << "(" << idDescriptor[id] << ")";
268 }
269
270 void SpirvStream::outputMask(OperandClass operandClass, unsigned mask)
271 {
272     if (mask == 0)
273         out << "None";
274     else {
275         for (int m = 0; m < OperandClassParams[operandClass].ceiling; ++m) {
276             if (mask & (1 << m))
277                 out << OperandClassParams[operandClass].getName(m) << " ";
278         }
279     }
280 }
281
282 void SpirvStream::disassembleImmediates(int numOperands)
283 {
284     for (int i = 0; i < numOperands; ++i) {
285         out << stream[word++];
286         if (i < numOperands - 1)
287             out << " ";
288     }
289 }
290
291 void SpirvStream::disassembleIds(int numOperands)
292 {
293     for (int i = 0; i < numOperands; ++i) {
294         outputId(stream[word++]);
295         if (i < numOperands - 1)
296             out << " ";
297     }
298 }
299
300 // decode string from words at current position (non-consuming)
301 std::pair<int, std::string> SpirvStream::decodeString()
302 {
303     std::string res;
304     int wordPos = word;
305     char c;
306     bool done = false;
307
308     do {
309         unsigned int content = stream[wordPos];
310         for (int charCount = 0; charCount < 4; ++charCount) {
311             c = content & 0xff;
312             content >>= 8;
313             if (c == '\0') {
314                 done = true;
315                 break;
316             }
317             res += c;
318         }
319         ++wordPos;
320     } while(! done);
321
322     return std::make_pair(wordPos - word, res);
323 }
324
325 // return the number of operands consumed by the string
326 int SpirvStream::disassembleString()
327 {
328     out << " \"";
329
330     std::pair<int, std::string> decoderes = decodeString();
331
332     out << decoderes.second;
333     out << "\"";
334
335     word += decoderes.first;
336
337     return decoderes.first;
338 }
339
340 void SpirvStream::disassembleInstruction(Id resultId, Id /*typeId*/, Op opCode, int numOperands)
341 {
342     // Process the opcode
343
344     out << (OpcodeString(opCode) + 2);  // leave out the "Op"
345
346     if (opCode == OpLoopMerge || opCode == OpSelectionMerge)
347         nextNestedControl = stream[word];
348     else if (opCode == OpBranchConditional || opCode == OpSwitch) {
349         if (nextNestedControl) {
350             nestedControl.push(nextNestedControl);
351             nextNestedControl = 0;
352         }
353     } else if (opCode == OpExtInstImport) {
354         idDescriptor[resultId] = decodeString().second;
355     }
356     else {
357         if (resultId != 0 && idDescriptor[resultId].size() == 0) {
358             switch (opCode) {
359             case OpTypeInt:
360                 switch (stream[word]) {
361                 case 8:  idDescriptor[resultId] = "int8_t"; break;
362                 case 16: idDescriptor[resultId] = "int16_t"; break;
363                 default: assert(0); // fallthrough
364                 case 32: idDescriptor[resultId] = "int"; break;
365                 case 64: idDescriptor[resultId] = "int64_t"; break;
366                 }
367                 break;
368             case OpTypeFloat:
369                 switch (stream[word]) {
370                 case 16: idDescriptor[resultId] = "float16_t"; break;
371                 default: assert(0); // fallthrough
372                 case 32: idDescriptor[resultId] = "float"; break;
373                 case 64: idDescriptor[resultId] = "float64_t"; break;
374                 }
375                 break;
376             case OpTypeBool:
377                 idDescriptor[resultId] = "bool";
378                 break;
379             case OpTypeStruct:
380                 idDescriptor[resultId] = "struct";
381                 break;
382             case OpTypePointer:
383                 idDescriptor[resultId] = "ptr";
384                 break;
385             case OpTypeVector:
386                 if (idDescriptor[stream[word]].size() > 0) {
387                     idDescriptor[resultId].append(idDescriptor[stream[word]].begin(), idDescriptor[stream[word]].begin() + 1);
388                     if (strstr(idDescriptor[stream[word]].c_str(), "8")) {
389                         idDescriptor[resultId].append("8");
390                     }
391                     if (strstr(idDescriptor[stream[word]].c_str(), "16")) {
392                         idDescriptor[resultId].append("16");
393                     }
394                     if (strstr(idDescriptor[stream[word]].c_str(), "64")) {
395                         idDescriptor[resultId].append("64");
396                     }
397                 }
398                 idDescriptor[resultId].append("vec");
399                 switch (stream[word + 1]) {
400                 case 2:   idDescriptor[resultId].append("2");   break;
401                 case 3:   idDescriptor[resultId].append("3");   break;
402                 case 4:   idDescriptor[resultId].append("4");   break;
403                 case 8:   idDescriptor[resultId].append("8");   break;
404                 case 16:  idDescriptor[resultId].append("16");  break;
405                 case 32:  idDescriptor[resultId].append("32");  break;
406                 default: break;
407                 }
408                 break;
409             default:
410                 break;
411             }
412         }
413     }
414
415     // Process the operands.  Note, a new context-dependent set could be
416     // swapped in mid-traversal.
417
418     // Handle images specially, so can put out helpful strings.
419     if (opCode == OpTypeImage) {
420         out << " ";
421         disassembleIds(1);
422         out << " " << DimensionString((Dim)stream[word++]);
423         out << (stream[word++] != 0 ? " depth" : "");
424         out << (stream[word++] != 0 ? " array" : "");
425         out << (stream[word++] != 0 ? " multi-sampled" : "");
426         switch (stream[word++]) {
427         case 0: out << " runtime";    break;
428         case 1: out << " sampled";    break;
429         case 2: out << " nonsampled"; break;
430         }
431         out << " format:" << ImageFormatString((ImageFormat)stream[word++]);
432
433         if (numOperands == 8) {
434             out << " " << AccessQualifierString(stream[word++]);
435         }
436         return;
437     }
438
439     // Handle all the parameterized operands
440     for (int op = 0; op < InstructionDesc[opCode].operands.getNum() && numOperands > 0; ++op) {
441         out << " ";
442         OperandClass operandClass = InstructionDesc[opCode].operands.getClass(op);
443         switch (operandClass) {
444         case OperandId:
445         case OperandScope:
446         case OperandMemorySemantics:
447             disassembleIds(1);
448             --numOperands;
449             // Get names for printing "(XXX)" for readability, *after* this id
450             if (opCode == OpName)
451                 idDescriptor[stream[word - 1]] = decodeString().second;
452             break;
453         case OperandVariableIds:
454             disassembleIds(numOperands);
455             return;
456         case OperandImageOperands:
457             outputMask(OperandImageOperands, stream[word++]);
458             --numOperands;
459             disassembleIds(numOperands);
460             return;
461         case OperandOptionalLiteral:
462         case OperandVariableLiterals:
463             if ((opCode == OpDecorate && stream[word - 1] == DecorationBuiltIn) ||
464                 (opCode == OpMemberDecorate && stream[word - 1] == DecorationBuiltIn)) {
465                 out << BuiltInString(stream[word++]);
466                 --numOperands;
467                 ++op;
468             }
469             disassembleImmediates(numOperands);
470             return;
471         case OperandVariableIdLiteral:
472             while (numOperands > 0) {
473                 out << std::endl;
474                 outputResultId(0);
475                 outputTypeId(0);
476                 outputIndent();
477                 out << "     Type ";
478                 disassembleIds(1);
479                 out << ", member ";
480                 disassembleImmediates(1);
481                 numOperands -= 2;
482             }
483             return;
484         case OperandVariableLiteralId:
485             while (numOperands > 0) {
486                 out << std::endl;
487                 outputResultId(0);
488                 outputTypeId(0);
489                 outputIndent();
490                 out << "     case ";
491                 disassembleImmediates(1);
492                 out << ": ";
493                 disassembleIds(1);
494                 numOperands -= 2;
495             }
496             return;
497         case OperandLiteralNumber:
498             disassembleImmediates(1);
499             --numOperands;
500             if (opCode == OpExtInst) {
501                 ExtInstSet extInstSet = GLSL450Inst;
502                 const char* name = idDescriptor[stream[word - 2]].c_str();
503                 if (strcmp("OpenCL.std", name) == 0) {
504                     extInstSet = OpenCLExtInst;
505                 } else if (strcmp("OpenCL.DebugInfo.100", name) == 0) {
506                     extInstSet = OpenCLExtInst;
507                 } else if (strcmp("NonSemantic.DebugPrintf", name) == 0) {
508                     extInstSet = NonSemanticDebugPrintfExtInst;
509                 } else if (strcmp("NonSemantic.Shader.DebugInfo.100", name) == 0) {
510                     extInstSet = NonSemanticShaderDebugInfo100;
511                 } else if (strcmp(spv::E_SPV_AMD_shader_ballot, name) == 0 ||
512                            strcmp(spv::E_SPV_AMD_shader_trinary_minmax, name) == 0 ||
513                            strcmp(spv::E_SPV_AMD_shader_explicit_vertex_parameter, name) == 0 ||
514                            strcmp(spv::E_SPV_AMD_gcn_shader, name) == 0) {
515                     extInstSet = GLSLextAMDInst;
516                 } else if (strcmp(spv::E_SPV_NV_sample_mask_override_coverage, name) == 0 ||
517                           strcmp(spv::E_SPV_NV_geometry_shader_passthrough, name) == 0 ||
518                           strcmp(spv::E_SPV_NV_viewport_array2, name) == 0 ||
519                           strcmp(spv::E_SPV_NVX_multiview_per_view_attributes, name) == 0 ||
520                           strcmp(spv::E_SPV_NV_fragment_shader_barycentric, name) == 0 ||
521                           strcmp(spv::E_SPV_NV_mesh_shader, name) == 0) {
522                     extInstSet = GLSLextNVInst;
523                 }
524                 unsigned entrypoint = stream[word - 1];
525                 if (extInstSet == GLSL450Inst) {
526                     if (entrypoint < GLSLstd450Count) {
527                         out << "(" << GlslStd450DebugNames[entrypoint] << ")";
528                     }
529                 } else if (extInstSet == GLSLextAMDInst) {
530                     out << "(" << GLSLextAMDGetDebugNames(name, entrypoint) << ")";
531                 }
532                 else if (extInstSet == GLSLextNVInst) {
533                     out << "(" << GLSLextNVGetDebugNames(name, entrypoint) << ")";
534                 } else if (extInstSet == NonSemanticDebugPrintfExtInst) {
535                     out << "(DebugPrintf)";
536                 } else if (extInstSet == NonSemanticShaderDebugInfo100) {
537                     out << "(" << NonSemanticShaderDebugInfo100GetDebugNames(entrypoint) << ")";
538                 }
539             }
540             break;
541         case OperandOptionalLiteralString:
542         case OperandLiteralString:
543             numOperands -= disassembleString();
544             break;
545         case OperandVariableLiteralStrings:
546             while (numOperands > 0)
547                 numOperands -= disassembleString();
548             return;
549         case OperandMemoryAccess:
550             outputMask(OperandMemoryAccess, stream[word++]);
551             --numOperands;
552             // Aligned is the only memory access operand that uses an immediate
553             // value, and it is also the first operand that uses a value at all.
554             if (stream[word-1] & MemoryAccessAlignedMask) {
555                 disassembleImmediates(1);
556                 numOperands--;
557                 if (numOperands)
558                     out << " ";
559             }
560             disassembleIds(numOperands);
561             return;
562         default:
563             assert(operandClass >= OperandSource && operandClass < OperandOpcode);
564
565             if (OperandClassParams[operandClass].bitmask)
566                 outputMask(operandClass, stream[word++]);
567             else
568                 out << OperandClassParams[operandClass].getName(stream[word++]);
569             --numOperands;
570
571             break;
572         }
573     }
574
575     return;
576 }
577
578 static void GLSLstd450GetDebugNames(const char** names)
579 {
580     for (int i = 0; i < GLSLstd450Count; ++i)
581         names[i] = "Unknown";
582
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
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
704     return "Bad";
705 }
706
707 static const char* GLSLextNVGetDebugNames(const char* name, unsigned entrypoint)
708 {
709     if (strcmp(name, spv::E_SPV_NV_sample_mask_override_coverage) == 0 ||
710         strcmp(name, spv::E_SPV_NV_geometry_shader_passthrough) == 0 ||
711         strcmp(name, spv::E_ARB_shader_viewport_layer_array) == 0 ||
712         strcmp(name, spv::E_SPV_NV_viewport_array2) == 0 ||
713         strcmp(name, spv::E_SPV_NVX_multiview_per_view_attributes) == 0 ||
714         strcmp(name, spv::E_SPV_NV_fragment_shader_barycentric) == 0 ||
715         strcmp(name, spv::E_SPV_NV_mesh_shader) == 0 ||
716         strcmp(name, spv::E_SPV_NV_shader_image_footprint) == 0) {
717         switch (entrypoint) {
718         // NV builtins
719         case BuiltInViewportMaskNV:                 return "ViewportMaskNV";
720         case BuiltInSecondaryPositionNV:            return "SecondaryPositionNV";
721         case BuiltInSecondaryViewportMaskNV:        return "SecondaryViewportMaskNV";
722         case BuiltInPositionPerViewNV:              return "PositionPerViewNV";
723         case BuiltInViewportMaskPerViewNV:          return "ViewportMaskPerViewNV";
724         case BuiltInBaryCoordNV:                    return "BaryCoordNV";
725         case BuiltInBaryCoordNoPerspNV:             return "BaryCoordNoPerspNV";
726         case BuiltInTaskCountNV:                    return "TaskCountNV";
727         case BuiltInPrimitiveCountNV:               return "PrimitiveCountNV";
728         case BuiltInPrimitiveIndicesNV:             return "PrimitiveIndicesNV";
729         case BuiltInClipDistancePerViewNV:          return "ClipDistancePerViewNV";
730         case BuiltInCullDistancePerViewNV:          return "CullDistancePerViewNV";
731         case BuiltInLayerPerViewNV:                 return "LayerPerViewNV";
732         case BuiltInMeshViewCountNV:                return "MeshViewCountNV";
733         case BuiltInMeshViewIndicesNV:              return "MeshViewIndicesNV";
734
735         // NV Capabilities
736         case CapabilityGeometryShaderPassthroughNV: return "GeometryShaderPassthroughNV";
737         case CapabilityShaderViewportMaskNV:        return "ShaderViewportMaskNV";
738         case CapabilityShaderStereoViewNV:          return "ShaderStereoViewNV";
739         case CapabilityPerViewAttributesNV:         return "PerViewAttributesNV";
740         case CapabilityFragmentBarycentricNV:       return "FragmentBarycentricNV";
741         case CapabilityMeshShadingNV:               return "MeshShadingNV";
742         case CapabilityImageFootprintNV:            return "ImageFootprintNV";
743         case CapabilitySampleMaskOverrideCoverageNV:return "SampleMaskOverrideCoverageNV";
744
745         // NV Decorations
746         case DecorationOverrideCoverageNV:          return "OverrideCoverageNV";
747         case DecorationPassthroughNV:               return "PassthroughNV";
748         case DecorationViewportRelativeNV:          return "ViewportRelativeNV";
749         case DecorationSecondaryViewportRelativeNV: return "SecondaryViewportRelativeNV";
750         case DecorationPerVertexNV:                 return "PerVertexNV";
751         case DecorationPerPrimitiveNV:              return "PerPrimitiveNV";
752         case DecorationPerViewNV:                   return "PerViewNV";
753         case DecorationPerTaskNV:                   return "PerTaskNV";
754
755         default:                                    return "Bad";
756         }
757     }
758     return "Bad";
759 }
760
761 static const char* NonSemanticShaderDebugInfo100GetDebugNames(unsigned entrypoint)
762 {
763     switch (entrypoint) {
764         case NonSemanticShaderDebugInfo100DebugInfoNone:                        return "DebugInfoNone";
765         case NonSemanticShaderDebugInfo100DebugCompilationUnit:                 return "DebugCompilationUnit";
766         case NonSemanticShaderDebugInfo100DebugTypeBasic:                       return "DebugTypeBasic";
767         case NonSemanticShaderDebugInfo100DebugTypePointer:                     return "DebugTypePointer";
768         case NonSemanticShaderDebugInfo100DebugTypeQualifier:                   return "DebugTypeQualifier";
769         case NonSemanticShaderDebugInfo100DebugTypeArray:                       return "DebugTypeArray";
770         case NonSemanticShaderDebugInfo100DebugTypeVector:                      return "DebugTypeVector";
771         case NonSemanticShaderDebugInfo100DebugTypedef:                         return "DebugTypedef";
772         case NonSemanticShaderDebugInfo100DebugTypeFunction:                    return "DebugTypeFunction";
773         case NonSemanticShaderDebugInfo100DebugTypeEnum:                        return "DebugTypeEnum";
774         case NonSemanticShaderDebugInfo100DebugTypeComposite:                   return "DebugTypeComposite";
775         case NonSemanticShaderDebugInfo100DebugTypeMember:                      return "DebugTypeMember";
776         case NonSemanticShaderDebugInfo100DebugTypeInheritance:                 return "DebugTypeInheritance";
777         case NonSemanticShaderDebugInfo100DebugTypePtrToMember:                 return "DebugTypePtrToMember";
778         case NonSemanticShaderDebugInfo100DebugTypeTemplate:                    return "DebugTypeTemplate";
779         case NonSemanticShaderDebugInfo100DebugTypeTemplateParameter:           return "DebugTypeTemplateParameter";
780         case NonSemanticShaderDebugInfo100DebugTypeTemplateTemplateParameter:   return "DebugTypeTemplateTemplateParameter";
781         case NonSemanticShaderDebugInfo100DebugTypeTemplateParameterPack:       return "DebugTypeTemplateParameterPack";
782         case NonSemanticShaderDebugInfo100DebugGlobalVariable:                  return "DebugGlobalVariable";
783         case NonSemanticShaderDebugInfo100DebugFunctionDeclaration:             return "DebugFunctionDeclaration";
784         case NonSemanticShaderDebugInfo100DebugFunction:                        return "DebugFunction";
785         case NonSemanticShaderDebugInfo100DebugLexicalBlock:                    return "DebugLexicalBlock";
786         case NonSemanticShaderDebugInfo100DebugLexicalBlockDiscriminator:       return "DebugLexicalBlockDiscriminator";
787         case NonSemanticShaderDebugInfo100DebugScope:                           return "DebugScope";
788         case NonSemanticShaderDebugInfo100DebugNoScope:                         return "DebugNoScope";
789         case NonSemanticShaderDebugInfo100DebugInlinedAt:                       return "DebugInlinedAt";
790         case NonSemanticShaderDebugInfo100DebugLocalVariable:                   return "DebugLocalVariable";
791         case NonSemanticShaderDebugInfo100DebugInlinedVariable:                 return "DebugInlinedVariable";
792         case NonSemanticShaderDebugInfo100DebugDeclare:                         return "DebugDeclare";
793         case NonSemanticShaderDebugInfo100DebugValue:                           return "DebugValue";
794         case NonSemanticShaderDebugInfo100DebugOperation:                       return "DebugOperation";
795         case NonSemanticShaderDebugInfo100DebugExpression:                      return "DebugExpression";
796         case NonSemanticShaderDebugInfo100DebugMacroDef:                        return "DebugMacroDef";
797         case NonSemanticShaderDebugInfo100DebugMacroUndef:                      return "DebugMacroUndef";
798         case NonSemanticShaderDebugInfo100DebugImportedEntity:                  return "DebugImportedEntity";
799         case NonSemanticShaderDebugInfo100DebugSource:                          return "DebugSource";
800         case NonSemanticShaderDebugInfo100DebugFunctionDefinition:              return "DebugFunctionDefinition";
801         case NonSemanticShaderDebugInfo100DebugSourceContinued:                 return "DebugSourceContinued";
802         case NonSemanticShaderDebugInfo100DebugLine:                            return "DebugLine";
803         case NonSemanticShaderDebugInfo100DebugNoLine:                          return "DebugNoLine";
804         case NonSemanticShaderDebugInfo100DebugBuildIdentifier:                 return "DebugBuildIdentifier";
805         case NonSemanticShaderDebugInfo100DebugStoragePath:                     return "DebugStoragePath";
806         case NonSemanticShaderDebugInfo100DebugEntryPoint:                      return "DebugEntryPoint";
807         case NonSemanticShaderDebugInfo100DebugTypeMatrix:                      return "DebugTypeMatrix";
808         default:                                                                return "Bad";
809     }
810
811     return "Bad";
812 }
813
814 void Disassemble(std::ostream& out, const std::vector<unsigned int>& stream)
815 {
816     SpirvStream SpirvStream(out, stream);
817     spv::Parameterize();
818     GLSLstd450GetDebugNames(GlslStd450DebugNames);
819     SpirvStream.validate();
820     SpirvStream.processInstructions();
821 }
822
823 }; // end namespace spv