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       argv0, argv0);
61 }
62 
63 static const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_5;
64 
main(int argc,char ** argv)65 int main(int argc, char** argv) {
66   const char* inFile = nullptr;
67   const char* outFile = nullptr;
68 
69   bool color_is_possible =
70 #if SPIRV_COLOR_TERMINAL
71       true;
72 #else
73       false;
74 #endif
75   bool force_color = false;
76   bool force_no_color = false;
77 
78   bool allow_indent = true;
79   bool show_byte_offsets = false;
80   bool no_header = false;
81   bool friendly_names = true;
82 
83   for (int argi = 1; argi < argc; ++argi) {
84     if ('-' == argv[argi][0]) {
85       switch (argv[argi][1]) {
86         case 'h':
87           print_usage(argv[0]);
88           return 0;
89         case 'o': {
90           if (!outFile && argi + 1 < argc) {
91             outFile = argv[++argi];
92           } else {
93             print_usage(argv[0]);
94             return 1;
95           }
96         } break;
97         case '-': {
98           // Long options
99           if (0 == strcmp(argv[argi], "--no-color")) {
100             force_no_color = true;
101             force_color = false;
102           } else if (0 == strcmp(argv[argi], "--color")) {
103             force_no_color = false;
104             force_color = true;
105           } else if (0 == strcmp(argv[argi], "--no-indent")) {
106             allow_indent = false;
107           } else if (0 == strcmp(argv[argi], "--offsets")) {
108             show_byte_offsets = true;
109           } else if (0 == strcmp(argv[argi], "--no-header")) {
110             no_header = true;
111           } else if (0 == strcmp(argv[argi], "--raw-id")) {
112             friendly_names = false;
113           } else if (0 == strcmp(argv[argi], "--help")) {
114             print_usage(argv[0]);
115             return 0;
116           } else if (0 == strcmp(argv[argi], "--version")) {
117             printf("%s\n", spvSoftwareVersionDetailsString());
118             printf("Target: %s\n",
119                    spvTargetEnvDescription(kDefaultEnvironment));
120             return 0;
121           } else {
122             print_usage(argv[0]);
123             return 1;
124           }
125         } break;
126         case 0: {
127           // Setting a filename of "-" to indicate stdin.
128           if (!inFile) {
129             inFile = argv[argi];
130           } else {
131             fprintf(stderr, "error: More than one input file specified\n");
132             return 1;
133           }
134         } break;
135         default:
136           print_usage(argv[0]);
137           return 1;
138       }
139     } else {
140       if (!inFile) {
141         inFile = argv[argi];
142       } else {
143         fprintf(stderr, "error: More than one input file specified\n");
144         return 1;
145       }
146     }
147   }
148 
149   uint32_t options = SPV_BINARY_TO_TEXT_OPTION_NONE;
150 
151   if (allow_indent) options |= SPV_BINARY_TO_TEXT_OPTION_INDENT;
152 
153   if (show_byte_offsets) options |= SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET;
154 
155   if (no_header) options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER;
156 
157   if (friendly_names) options |= SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES;
158 
159   if (!outFile || (0 == strcmp("-", outFile))) {
160     // Print to standard output.
161     options |= SPV_BINARY_TO_TEXT_OPTION_PRINT;
162 
163     if (color_is_possible && !force_no_color) {
164       bool output_is_tty = true;
165 #if defined(_POSIX_VERSION)
166       output_is_tty = isatty(fileno(stdout));
167 #endif
168       if (output_is_tty || force_color) {
169         options |= SPV_BINARY_TO_TEXT_OPTION_COLOR;
170       }
171     }
172   }
173 
174   // Read the input binary.
175   std::vector<uint32_t> contents;
176   if (!ReadFile<uint32_t>(inFile, "rb", &contents)) return 1;
177 
178   // If printing to standard output, then spvBinaryToText should
179   // do the printing.  In particular, colour printing on Windows is
180   // controlled by modifying console objects synchronously while
181   // outputting to the stream rather than by injecting escape codes
182   // into the output stream.
183   // If the printing option is off, then save the text in memory, so
184   // it can be emitted later in this function.
185   const bool print_to_stdout = SPV_BINARY_TO_TEXT_OPTION_PRINT & options;
186   spv_text text = nullptr;
187   spv_text* textOrNull = print_to_stdout ? nullptr : &text;
188   spv_diagnostic diagnostic = nullptr;
189   spv_context context = spvContextCreate(kDefaultEnvironment);
190   spv_result_t error =
191       spvBinaryToText(context, contents.data(), contents.size(), options,
192                       textOrNull, &diagnostic);
193   spvContextDestroy(context);
194   if (error) {
195     spvDiagnosticPrint(diagnostic);
196     spvDiagnosticDestroy(diagnostic);
197     return error;
198   }
199 
200   if (!print_to_stdout) {
201     if (!WriteFile<char>(outFile, "w", text->str, text->length)) {
202       spvTextDestroy(text);
203       return 1;
204     }
205   }
206   spvTextDestroy(text);
207 
208   return 0;
209 }
210