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 #include "tools/util/flags.h"
28
29 static const std::string kHelpText = R"(%s - Disassemble a SPIR-V binary module
30
31 Usage: %s [options] [<filename>]
32
33 The SPIR-V binary is read from <filename>. If no file is specified,
34 or if the filename is "-", then the binary is read from standard input.
35
36 Options:
37
38 -h, --help Print this help.
39 --version Display disassembler version information.
40
41 -o <filename> Set the output filename.
42 Output goes to standard output if this option is
43 not specified, or if the filename is "-".
44
45 --color Force color output. The default when printing to a terminal.
46 Overrides a previous --no-color option.
47 --no-color Don't print in color. Overrides a previous --color option.
48 The default when output goes to something other than a
49 terminal (e.g. a file, a pipe, or a shell redirection).
50
51 --no-indent Don't indent instructions.
52
53 --no-header Don't output the header as leading comments.
54
55 --raw-id Show raw Id values instead of friendly names.
56
57 --offsets Show byte offsets for each instruction.
58
59 --comment Add comments to make reading easier
60 )";
61
62 // clang-format off
63 FLAG_SHORT_bool (h, /* default_value= */ false, /* required= */ false);
64 FLAG_SHORT_string(o, /* default_value= */ "-", /* required= */ false);
65 FLAG_LONG_bool (help, /* default_value= */ false, /* required= */false);
66 FLAG_LONG_bool (version, /* default_value= */ false, /* required= */ false);
67 FLAG_LONG_bool (color, /* default_value= */ false, /* required= */ false);
68 FLAG_LONG_bool (no_color, /* default_value= */ false, /* required= */ false);
69 FLAG_LONG_bool (no_indent, /* default_value= */ false, /* required= */ false);
70 FLAG_LONG_bool (no_header, /* default_value= */ false, /* required= */ false);
71 FLAG_LONG_bool (raw_id, /* default_value= */ false, /* required= */ false);
72 FLAG_LONG_bool (offsets, /* default_value= */ false, /* required= */ false);
73 FLAG_LONG_bool (comment, /* default_value= */ false, /* required= */ false);
74 // clang-format on
75
76 static const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_5;
77
main(int,const char ** argv)78 int main(int, const char** argv) {
79 if (!flags::Parse(argv)) {
80 return 1;
81 }
82
83 if (flags::h.value() || flags::help.value()) {
84 printf(kHelpText.c_str(), argv[0], argv[0]);
85 return 0;
86 }
87
88 if (flags::version.value()) {
89 printf("%s\n", spvSoftwareVersionDetailsString());
90 printf("Target: %s\n", spvTargetEnvDescription(kDefaultEnvironment));
91 return 0;
92 }
93
94 if (flags::positional_arguments.size() > 1) {
95 fprintf(stderr, "error: more than one input file specified.\n");
96 return 1;
97 }
98
99 const std::string inFile = flags::positional_arguments.size() == 0
100 ? "-"
101 : flags::positional_arguments[0];
102 const std::string outFile = flags::o.value();
103
104 bool color_is_possible =
105 #if SPIRV_COLOR_TERMINAL
106 true;
107 #else
108 false;
109 #endif
110
111 uint32_t options = SPV_BINARY_TO_TEXT_OPTION_NONE;
112
113 if (!flags::no_indent.value()) options |= SPV_BINARY_TO_TEXT_OPTION_INDENT;
114
115 if (flags::offsets.value())
116 options |= SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET;
117
118 if (flags::no_header.value()) options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER;
119
120 if (!flags::raw_id.value())
121 options |= SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES;
122
123 if (flags::comment.value()) options |= SPV_BINARY_TO_TEXT_OPTION_COMMENT;
124
125 if (flags::o.value() == "-") {
126 // Print to standard output.
127 options |= SPV_BINARY_TO_TEXT_OPTION_PRINT;
128 if (color_is_possible && !flags::no_color.value()) {
129 bool output_is_tty = true;
130 #if defined(_POSIX_VERSION)
131 output_is_tty = isatty(fileno(stdout));
132 #endif
133 if (output_is_tty || flags::color.value()) {
134 options |= SPV_BINARY_TO_TEXT_OPTION_COLOR;
135 }
136 }
137 }
138
139 // Read the input binary.
140 std::vector<uint32_t> contents;
141 if (!ReadBinaryFile<uint32_t>(inFile.c_str(), &contents)) return 1;
142
143 // If printing to standard output, then spvBinaryToText should
144 // do the printing. In particular, colour printing on Windows is
145 // controlled by modifying console objects synchronously while
146 // outputting to the stream rather than by injecting escape codes
147 // into the output stream.
148 // If the printing option is off, then save the text in memory, so
149 // it can be emitted later in this function.
150 const bool print_to_stdout = SPV_BINARY_TO_TEXT_OPTION_PRINT & options;
151 spv_text text = nullptr;
152 spv_text* textOrNull = print_to_stdout ? nullptr : &text;
153 spv_diagnostic diagnostic = nullptr;
154 spv_context context = spvContextCreate(kDefaultEnvironment);
155 spv_result_t error =
156 spvBinaryToText(context, contents.data(), contents.size(), options,
157 textOrNull, &diagnostic);
158 spvContextDestroy(context);
159 if (error) {
160 spvDiagnosticPrint(diagnostic);
161 spvDiagnosticDestroy(diagnostic);
162 return error;
163 }
164
165 if (!print_to_stdout) {
166 if (!WriteFile<char>(outFile.c_str(), "w", text->str, text->length)) {
167 spvTextDestroy(text);
168 return 1;
169 }
170 }
171 spvTextDestroy(text);
172
173 return 0;
174 }
175