dis.cpp 6.6 KB

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