• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2015-2016 The Khronos Group Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
16 #include <stdio.h>  // Need fileno
17 #include <unistd.h>
18 #endif
19 
20 #include <cstdio>
21 #include <cstring>
22 #include <string>
23 #include <vector>
24 
25 #include "spirv-tools/libspirv.h"
26 #include "tools/io.h"
27 
print_usage(char * argv0)28 static void print_usage(char* argv0) {
29   printf(
30       R"(%s - Disassemble a SPIR-V binary module
31 
32 Usage: %s [options] [<filename>]
33 
34 The SPIR-V binary is read from <filename>. If no file is specified,
35 or if the filename is "-", then the binary is read from standard input.
36 
37 Options:
38 
39   -h, --help      Print this help.
40   --version       Display disassembler version information.
41 
42   -o <filename>   Set the output filename.
43                   Output goes to standard output if this option is
44                   not specified, or if the filename is "-".
45 
46   --color         Force color output.  The default when printing to a terminal.
47                   Overrides a previous --no-color option.
48   --no-color      Don't print in color.  Overrides a previous --color option.
49                   The default when output goes to something other than a
50                   terminal (e.g. a file, a pipe, or a shell redirection).
51 
52   --no-indent     Don't indent instructions.
53 
54   --no-header     Don't output the header as leading comments.
55 
56   --raw-id        Show raw Id values instead of friendly names.
57 
58   --offsets       Show byte offsets for each instruction.
59 
60   --comment       Add comments to make reading easier
61 )",
62       argv0, argv0);
63 }
64 
65 static const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_5;
66 
main(int argc,char ** argv)67 int main(int argc, char** argv) {
68   const char* inFile = nullptr;
69   const char* outFile = nullptr;
70 
71   bool color_is_possible =
72 #if SPIRV_COLOR_TERMINAL
73       true;
74 #else
75       false;
76 #endif
77   bool force_color = false;
78   bool force_no_color = false;
79 
80   bool allow_indent = true;
81   bool show_byte_offsets = false;
82   bool no_header = false;
83   bool friendly_names = true;
84   bool comments = false;
85 
86   for (int argi = 1; argi < argc; ++argi) {
87     if ('-' == argv[argi][0]) {
88       switch (argv[argi][1]) {
89         case 'h':
90           print_usage(argv[0]);
91           return 0;
92         case 'o': {
93           if (!outFile && argi + 1 < argc) {
94             outFile = argv[++argi];
95           } else {
96             print_usage(argv[0]);
97             return 1;
98           }
99         } break;
100         case '-': {
101           // Long options
102           if (0 == strcmp(argv[argi], "--no-color")) {
103             force_no_color = true;
104             force_color = false;
105           } else if (0 == strcmp(argv[argi], "--color")) {
106             force_no_color = false;
107             force_color = true;
108           } else if (0 == strcmp(argv[argi], "--comment")) {
109             comments = true;
110           } else if (0 == strcmp(argv[argi], "--no-indent")) {
111             allow_indent = false;
112           } else if (0 == strcmp(argv[argi], "--offsets")) {
113             show_byte_offsets = true;
114           } else if (0 == strcmp(argv[argi], "--no-header")) {
115             no_header = true;
116           } else if (0 == strcmp(argv[argi], "--raw-id")) {
117             friendly_names = false;
118           } else if (0 == strcmp(argv[argi], "--help")) {
119             print_usage(argv[0]);
120             return 0;
121           } else if (0 == strcmp(argv[argi], "--version")) {
122             printf("%s\n", spvSoftwareVersionDetailsString());
123             printf("Target: %s\n",
124                    spvTargetEnvDescription(kDefaultEnvironment));
125             return 0;
126           } else {
127             print_usage(argv[0]);
128             return 1;
129           }
130         } break;
131         case 0: {
132           // Setting a filename of "-" to indicate stdin.
133           if (!inFile) {
134             inFile = argv[argi];
135           } else {
136             fprintf(stderr, "error: More than one input file specified\n");
137             return 1;
138           }
139         } break;
140         default:
141           print_usage(argv[0]);
142           return 1;
143       }
144     } else {
145       if (!inFile) {
146         inFile = argv[argi];
147       } else {
148         fprintf(stderr, "error: More than one input file specified\n");
149         return 1;
150       }
151     }
152   }
153 
154   uint32_t options = SPV_BINARY_TO_TEXT_OPTION_NONE;
155 
156   if (allow_indent) options |= SPV_BINARY_TO_TEXT_OPTION_INDENT;
157 
158   if (show_byte_offsets) options |= SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET;
159 
160   if (no_header) options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER;
161 
162   if (friendly_names) options |= SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES;
163 
164   if (comments) options |= SPV_BINARY_TO_TEXT_OPTION_COMMENT;
165 
166   if (!outFile || (0 == strcmp("-", outFile))) {
167     // Print to standard output.
168     options |= SPV_BINARY_TO_TEXT_OPTION_PRINT;
169 
170     if (color_is_possible && !force_no_color) {
171       bool output_is_tty = true;
172 #if defined(_POSIX_VERSION)
173       output_is_tty = isatty(fileno(stdout));
174 #endif
175       if (output_is_tty || force_color) {
176         options |= SPV_BINARY_TO_TEXT_OPTION_COLOR;
177       }
178     }
179   }
180 
181   // Read the input binary.
182   std::vector<uint32_t> contents;
183   if (!ReadBinaryFile<uint32_t>(inFile, &contents)) return 1;
184 
185   // If printing to standard output, then spvBinaryToText should
186   // do the printing.  In particular, colour printing on Windows is
187   // controlled by modifying console objects synchronously while
188   // outputting to the stream rather than by injecting escape codes
189   // into the output stream.
190   // If the printing option is off, then save the text in memory, so
191   // it can be emitted later in this function.
192   const bool print_to_stdout = SPV_BINARY_TO_TEXT_OPTION_PRINT & options;
193   spv_text text = nullptr;
194   spv_text* textOrNull = print_to_stdout ? nullptr : &text;
195   spv_diagnostic diagnostic = nullptr;
196   spv_context context = spvContextCreate(kDefaultEnvironment);
197   spv_result_t error =
198       spvBinaryToText(context, contents.data(), contents.size(), options,
199                       textOrNull, &diagnostic);
200   spvContextDestroy(context);
201   if (error) {
202     spvDiagnosticPrint(diagnostic);
203     spvDiagnosticDestroy(diagnostic);
204     return error;
205   }
206 
207   if (!print_to_stdout) {
208     if (!WriteFile<char>(outFile, "w", text->str, text->length)) {
209       spvTextDestroy(text);
210       return 1;
211     }
212   }
213   spvTextDestroy(text);
214 
215   return 0;
216 }
217