1 // Copyright (c) 2018 Google 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 #ifndef SOURCE_DISASSEMBLE_H_ 16 #define SOURCE_DISASSEMBLE_H_ 17 18 #include <iosfwd> 19 #include <string> 20 21 #include "source/name_mapper.h" 22 #include "spirv-tools/libspirv.h" 23 24 namespace spvtools { 25 26 // Decodes the given SPIR-V instruction binary representation to its assembly 27 // text. The context is inferred from the provided module binary. The options 28 // parameter is a bit field of spv_binary_to_text_options_t (note: the option 29 // SPV_BINARY_TO_TEXT_OPTION_PRINT will be ignored). Decoded text will be 30 // stored into *text. Any error will be written into *diagnostic if diagnostic 31 // is non-null. 32 std::string spvInstructionBinaryToText(const spv_target_env env, 33 const uint32_t* inst_binary, 34 const size_t inst_word_count, 35 const uint32_t* binary, 36 const size_t word_count, 37 const uint32_t options); 38 39 class AssemblyGrammar; 40 namespace disassemble { 41 42 // Shared code with other tools (than the disassembler) that might need to 43 // output disassembly. An InstructionDisassembler instance converts SPIR-V 44 // binary for an instruction to its assembly representation. 45 class InstructionDisassembler { 46 public: 47 InstructionDisassembler(const AssemblyGrammar& grammar, std::ostream& stream, 48 uint32_t options, NameMapper name_mapper); 49 50 // Emits the assembly header for the module. 51 void EmitHeaderSpirv(); 52 void EmitHeaderVersion(uint32_t version); 53 void EmitHeaderGenerator(uint32_t generator); 54 void EmitHeaderIdBound(uint32_t id_bound); 55 void EmitHeaderSchema(uint32_t schema); 56 57 // Emits the assembly text for the given instruction. 58 void EmitInstruction(const spv_parsed_instruction_t& inst, 59 size_t inst_byte_offset); 60 61 // Emits a comment between different sections of the module. 62 void EmitSectionComment(const spv_parsed_instruction_t& inst, 63 bool& inserted_decoration_space, 64 bool& inserted_debug_space, 65 bool& inserted_type_space); 66 67 // Resets the output color, if color is turned on. 68 void ResetColor(); 69 // Set the output color, if color is turned on. 70 void SetGrey(); 71 void SetBlue(); 72 void SetYellow(); 73 void SetRed(); 74 void SetGreen(); 75 76 private: 77 // Emits an operand for the given instruction, where the instruction 78 // is at offset words from the start of the binary. 79 void EmitOperand(const spv_parsed_instruction_t& inst, 80 const uint16_t operand_index); 81 82 // Emits a mask expression for the given mask word of the specified type. 83 void EmitMaskOperand(const spv_operand_type_t type, const uint32_t word); 84 85 const spvtools::AssemblyGrammar& grammar_; 86 std::ostream& stream_; 87 const bool print_; // Should we also print to the standard output stream? 88 const bool color_; // Should we print in colour? 89 const int indent_; // How much to indent. 0 means don't indent 90 const int comment_; // Should we comment the source 91 const bool show_byte_offset_; // Should we print byte offset, in hex? 92 spvtools::NameMapper name_mapper_; 93 }; 94 95 } // namespace disassemble 96 } // namespace spvtools 97 98 #endif // SOURCE_DISASSEMBLE_H_ 99