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