disassemble.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #ifndef SOURCE_DISASSEMBLE_H_
  15. #define SOURCE_DISASSEMBLE_H_
  16. #include <ios>
  17. #include <sstream>
  18. #include <string>
  19. #include "source/name_mapper.h"
  20. #include "spirv-tools/libspirv.h"
  21. namespace spvtools {
  22. // Decodes the given SPIR-V instruction binary representation to its assembly
  23. // text. The context is inferred from the provided module binary. The options
  24. // parameter is a bit field of spv_binary_to_text_options_t (note: the option
  25. // SPV_BINARY_TO_TEXT_OPTION_PRINT will be ignored). Decoded text will be
  26. // stored into *text. Any error will be written into *diagnostic if diagnostic
  27. // is non-null.
  28. std::string spvInstructionBinaryToText(const spv_target_env env,
  29. const uint32_t* inst_binary,
  30. const size_t inst_word_count,
  31. const uint32_t* binary,
  32. const size_t word_count,
  33. const uint32_t options);
  34. class AssemblyGrammar;
  35. namespace disassemble {
  36. // Shared code with other tools (than the disassembler) that might need to
  37. // output disassembly. An InstructionDisassembler instance converts SPIR-V
  38. // binary for an instruction to its assembly representation.
  39. class InstructionDisassembler {
  40. public:
  41. InstructionDisassembler(const AssemblyGrammar& grammar, std::ostream& stream,
  42. uint32_t options, NameMapper name_mapper);
  43. // Emits the assembly header for the module.
  44. void EmitHeaderSpirv();
  45. void EmitHeaderVersion(uint32_t version);
  46. void EmitHeaderGenerator(uint32_t generator);
  47. void EmitHeaderIdBound(uint32_t id_bound);
  48. void EmitHeaderSchema(uint32_t schema);
  49. // Emits the assembly text for the given instruction.
  50. void EmitInstruction(const spv_parsed_instruction_t& inst,
  51. size_t inst_byte_offset);
  52. // Same as EmitInstruction, but only for block instructions (including
  53. // OpLabel) and useful for nested indentation. If nested indentation is not
  54. // desired, EmitInstruction can still be used for block instructions.
  55. void EmitInstructionInBlock(const spv_parsed_instruction_t& inst,
  56. size_t inst_byte_offset, uint32_t block_indent);
  57. // Emits a comment between different sections of the module.
  58. void EmitSectionComment(const spv_parsed_instruction_t& inst,
  59. bool& inserted_decoration_space,
  60. bool& inserted_debug_space,
  61. bool& inserted_type_space);
  62. // Resets the output color, if color is turned on.
  63. void ResetColor();
  64. // Set the output color, if color is turned on.
  65. void SetGrey();
  66. void SetBlue();
  67. void SetYellow();
  68. void SetRed();
  69. void SetGreen();
  70. private:
  71. void ResetColor(std::ostream& stream) const;
  72. void SetGrey(std::ostream& stream) const;
  73. void SetBlue(std::ostream& stream) const;
  74. void SetYellow(std::ostream& stream) const;
  75. void SetRed(std::ostream& stream) const;
  76. void SetGreen(std::ostream& stream) const;
  77. void EmitInstructionImpl(const spv_parsed_instruction_t& inst,
  78. size_t inst_byte_offset, uint32_t block_indent,
  79. bool is_in_block);
  80. // Emits an operand for the given instruction, where the instruction
  81. // is at offset words from the start of the binary.
  82. void EmitOperand(std::ostream& stream, const spv_parsed_instruction_t& inst,
  83. uint16_t operand_index) const;
  84. // Emits a mask expression for the given mask word of the specified type.
  85. void EmitMaskOperand(std::ostream& stream, spv_operand_type_t type,
  86. uint32_t word) const;
  87. // Generate part of the instruction as a comment to be added to
  88. // |id_comments_|.
  89. void GenerateCommentForDecoratedId(const spv_parsed_instruction_t& inst);
  90. const spvtools::AssemblyGrammar& grammar_;
  91. std::ostream& stream_;
  92. const bool print_; // Should we also print to the standard output stream?
  93. const bool color_; // Should we print in colour?
  94. const int indent_; // How much to indent. 0 means don't indent
  95. const bool nested_indent_; // Whether indentation should indicate nesting
  96. const int comment_; // Should we comment the source
  97. const bool show_byte_offset_; // Should we print byte offset, in hex?
  98. spvtools::NameMapper name_mapper_;
  99. // Some comments are generated as instructions (such as OpDecorate) are
  100. // visited so that when the instruction with that result id is visited, the
  101. // comment can be output.
  102. std::unordered_map<uint32_t, std::ostringstream> id_comments_;
  103. // Align the comments in consecutive lines for more readability.
  104. uint32_t last_instruction_comment_alignment_;
  105. };
  106. } // namespace disassemble
  107. } // namespace spvtools
  108. #endif // SOURCE_DISASSEMBLE_H_