From 200e89788775ebaa1b23d261dc6ce96badff4b16 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 27 Jun 2016 15:06:41 -0400 Subject: [PATCH] Add an option to spvBinaryToText() to ignore header output. --- include/spirv-tools/libspirv.h | 2 ++ source/disassemble.cpp | 41 +++++++++++++++++++--------------- test/TestFixture.h | 6 ++--- tools/dis/dis.cpp | 7 ++++++ 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/include/spirv-tools/libspirv.h b/include/spirv-tools/libspirv.h index 5dcf4ff2..5263c78a 100644 --- a/include/spirv-tools/libspirv.h +++ b/include/spirv-tools/libspirv.h @@ -244,6 +244,8 @@ typedef enum spv_binary_to_text_options_t { SPV_BINARY_TO_TEXT_OPTION_COLOR = SPV_BIT(2), SPV_BINARY_TO_TEXT_OPTION_INDENT = SPV_BIT(3), SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET = SPV_BIT(4), + // Do not output the module header as leading comments in the assembly. + SPV_BINARY_TO_TEXT_OPTION_NO_HEADER = SPV_BIT(5), SPV_FORCE_32_BIT_ENUM(spv_binary_to_text_options_t) } spv_binary_to_text_options_t; diff --git a/source/disassemble.cpp b/source/disassemble.cpp index 53e07cb5..72d94c8d 100644 --- a/source/disassemble.cpp +++ b/source/disassemble.cpp @@ -60,6 +60,7 @@ class Disassembler { text_(), out_(print_ ? out_stream() : out_stream(text_)), stream_(out_.get()), + header_(!spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER, options)), show_byte_offset_(spvIsInBitfield( SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET, options)), byte_offset_(0) {} @@ -123,8 +124,9 @@ class Disassembler { std::stringstream text_; // Captures the text, if not printing. out_stream out_; // The Output stream. Either to text_ or standard output. std::ostream& stream_; // The output std::stream. + const bool header_; // Should we output header as the leading comment? const bool show_byte_offset_; // Should we print byte offset, in hex? - size_t byte_offset_; // The number of bytes processed so far. + size_t byte_offset_; // The number of bytes processed so far. }; spv_result_t Disassembler::HandleHeader(spv_endianness_t endian, @@ -132,23 +134,25 @@ spv_result_t Disassembler::HandleHeader(spv_endianness_t endian, uint32_t id_bound, uint32_t schema) { endian_ = endian; - SetGrey(); - const char* generator_tool = - spvGeneratorStr(SPV_GENERATOR_TOOL_PART(generator)); - stream_ << "; SPIR-V\n" - << "; Version: " << SPV_SPIRV_VERSION_MAJOR_PART(version) << "." - << SPV_SPIRV_VERSION_MINOR_PART(version) << "\n" - << "; Generator: " << generator_tool; - // For unknown tools, print the numeric tool value. - if (0 == strcmp("Unknown", generator_tool)) { - stream_ << "(" << SPV_GENERATOR_TOOL_PART(generator) << ")"; + if (header_) { + SetGrey(); + const char* generator_tool = + spvGeneratorStr(SPV_GENERATOR_TOOL_PART(generator)); + stream_ << "; SPIR-V\n" + << "; Version: " << SPV_SPIRV_VERSION_MAJOR_PART(version) << "." + << SPV_SPIRV_VERSION_MINOR_PART(version) << "\n" + << "; Generator: " << generator_tool; + // For unknown tools, print the numeric tool value. + if (0 == strcmp("Unknown", generator_tool)) { + stream_ << "(" << SPV_GENERATOR_TOOL_PART(generator) << ")"; + } + // Print the miscellaneous part of the generator word on the same + // line as the tool name. + stream_ << "; " << SPV_GENERATOR_MISC_PART(generator) << "\n" + << "; Bound: " << id_bound << "\n" + << "; Schema: " << schema << "\n"; + ResetColor(); } - // Print the miscellaneous part of the generator word on the same - // line as the tool name. - stream_ << "; " << SPV_GENERATOR_MISC_PART(generator) << "\n" - << "; Bound: " << id_bound << "\n" - << "; Schema: " << schema << "\n"; - ResetColor(); byte_offset_ = SPV_INDEX_INSTRUCTION * sizeof(uint32_t); @@ -254,7 +258,8 @@ void Disassembler::EmitOperand(const spv_parsed_instruction_t& inst, break; case SPV_NUMBER_FLOATING: if (operand.number_bit_width == 16) { - stream_ << spvutils::FloatProxy(uint16_t(word & 0xFFFF)); + stream_ << spvutils::FloatProxy( + uint16_t(word & 0xFFFF)); } else { // Assume 32-bit floats. stream_ << spvutils::FloatProxy(word); diff --git a/test/TestFixture.h b/test/TestFixture.h index 40203818..9b2a1f15 100644 --- a/test/TestFixture.h +++ b/test/TestFixture.h @@ -104,6 +104,7 @@ class TextToBinaryTestBase : public T { spv_target_env env = SPV_ENV_UNIVERSAL_1_0) { DestroyBinary(); ScopedContext context(env); + disassemble_options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER; spv_result_t error = spvTextToBinary(context.context, txt.c_str(), txt.size(), &binary, &diagnostic); if (error) { @@ -125,10 +126,7 @@ class TextToBinaryTestBase : public T { const std::string decoded_string = decoded_text->str; spvTextDestroy(decoded_text); - // Remove the preamble comments generated by disassembler. - const std::string schema0 = "Schema: 0\n"; - std::string::size_type preamble_end = decoded_string.find(schema0); - return decoded_string.substr(preamble_end + schema0.size()); + return decoded_string; } // Encodes SPIR-V text into binary. This is expected to succeed. diff --git a/tools/dis/dis.cpp b/tools/dis/dis.cpp index 675f77ff..103d9baf 100644 --- a/tools/dis/dis.cpp +++ b/tools/dis/dis.cpp @@ -54,6 +54,8 @@ Options: --no-indent Don't indent instructions. + --no-header Don't output the header as leading comments. + --offsets Show byte offsets for each instruction. )", argv0, argv0); @@ -69,6 +71,7 @@ int main(int argc, char** argv) { #endif bool allow_indent = true; bool show_byte_offsets = false; + bool no_header = false; for (int argi = 1; argi < argc; ++argi) { if ('-' == argv[argi][0]) { @@ -92,6 +95,8 @@ int main(int argc, char** argv) { allow_indent = false; } else if (0 == strcmp(argv[argi], "--offsets")) { show_byte_offsets = true; + } else if (0 == strcmp(argv[argi], "--no-header")) { + no_header = true; } else if (0 == strcmp(argv[argi], "--help")) { print_usage(argv[0]); return 0; @@ -134,6 +139,8 @@ int main(int argc, char** argv) { if (show_byte_offsets) options |= SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET; + if (no_header) options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER; + if (!outFile || (0 == strcmp("-", outFile))) { // Print to standard output. options |= SPV_BINARY_TO_TEXT_OPTION_PRINT;