2020-03-17 19:30:19 +00:00
|
|
|
// Copyright (c) 2015-2020 The Khronos Group Inc.
|
|
|
|
// Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights
|
|
|
|
// reserved.
|
2015-10-27 20:56:14 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-10-27 20:56:14 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-10-27 20:56:14 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-10-27 20:56:14 +00:00
|
|
|
|
|
|
|
// This file contains a disassembler: It converts a SPIR-V binary
|
|
|
|
// to text.
|
|
|
|
|
2021-12-14 14:46:59 +00:00
|
|
|
#include "source/disassemble.h"
|
|
|
|
|
2016-07-08 18:29:52 +00:00
|
|
|
#include <algorithm>
|
2015-10-30 20:06:15 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
2016-01-18 20:29:15 +00:00
|
|
|
#include <iomanip>
|
2016-07-08 18:29:52 +00:00
|
|
|
#include <memory>
|
2015-10-30 20:06:15 +00:00
|
|
|
#include <unordered_map>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <utility>
|
2015-10-30 20:06:15 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/assembly_grammar.h"
|
|
|
|
#include "source/binary.h"
|
|
|
|
#include "source/diagnostic.h"
|
|
|
|
#include "source/ext_inst.h"
|
|
|
|
#include "source/opcode.h"
|
|
|
|
#include "source/parsed_operand.h"
|
|
|
|
#include "source/print.h"
|
|
|
|
#include "source/spirv_constant.h"
|
|
|
|
#include "source/spirv_endian.h"
|
|
|
|
#include "source/util/hex_float.h"
|
2018-08-14 19:01:50 +00:00
|
|
|
#include "source/util/make_unique.h"
|
2016-02-17 19:44:00 +00:00
|
|
|
#include "spirv-tools/libspirv.h"
|
2015-10-30 20:06:15 +00:00
|
|
|
|
2021-12-14 14:46:59 +00:00
|
|
|
namespace spvtools {
|
2015-10-30 20:06:15 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// A Disassembler instance converts a SPIR-V binary to its assembly
|
|
|
|
// representation.
|
|
|
|
class Disassembler {
|
|
|
|
public:
|
2021-12-14 14:46:59 +00:00
|
|
|
Disassembler(const AssemblyGrammar& grammar, uint32_t options,
|
|
|
|
NameMapper name_mapper)
|
|
|
|
: print_(spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options)),
|
2015-10-30 20:06:15 +00:00
|
|
|
text_(),
|
|
|
|
out_(print_ ? out_stream() : out_stream(text_)),
|
2021-12-14 14:46:59 +00:00
|
|
|
instruction_disassembler_(grammar, out_.get(), options, name_mapper),
|
2016-06-27 19:06:41 +00:00
|
|
|
header_(!spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER, options)),
|
2021-12-14 14:46:59 +00:00
|
|
|
byte_offset_(0) {}
|
2015-10-30 20:06:15 +00:00
|
|
|
|
|
|
|
// Emits the assembly header for the module, and sets up internal state
|
|
|
|
// so subsequent callbacks can handle the cases where the entire module
|
|
|
|
// is either big-endian or little-endian.
|
|
|
|
spv_result_t HandleHeader(spv_endianness_t endian, uint32_t version,
|
|
|
|
uint32_t generator, uint32_t id_bound,
|
|
|
|
uint32_t schema);
|
|
|
|
// Emits the assembly text for the given instruction.
|
|
|
|
spv_result_t HandleInstruction(const spv_parsed_instruction_t& inst);
|
|
|
|
|
|
|
|
// If not printing, populates text_result with the accumulated text.
|
|
|
|
// Returns SPV_SUCCESS on success.
|
|
|
|
spv_result_t SaveTextResult(spv_text* text_result) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const bool print_; // Should we also print to the standard output stream?
|
|
|
|
spv_endianness_t endian_; // The detected endianness of the binary.
|
|
|
|
std::stringstream text_; // Captures the text, if not printing.
|
|
|
|
out_stream out_; // The Output stream. Either to text_ or standard output.
|
2021-12-14 14:46:59 +00:00
|
|
|
disassemble::InstructionDisassembler instruction_disassembler_;
|
|
|
|
const bool header_; // Should we output header as the leading comment?
|
|
|
|
size_t byte_offset_; // The number of bytes processed so far.
|
2020-09-28 14:59:50 +00:00
|
|
|
bool inserted_decoration_space_ = false;
|
|
|
|
bool inserted_debug_space_ = false;
|
|
|
|
bool inserted_type_space_ = false;
|
2015-10-30 20:06:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
spv_result_t Disassembler::HandleHeader(spv_endianness_t endian,
|
|
|
|
uint32_t version, uint32_t generator,
|
|
|
|
uint32_t id_bound, uint32_t schema) {
|
|
|
|
endian_ = endian;
|
|
|
|
|
2016-06-27 19:06:41 +00:00
|
|
|
if (header_) {
|
2021-12-14 14:46:59 +00:00
|
|
|
instruction_disassembler_.EmitHeaderSpirv();
|
|
|
|
instruction_disassembler_.EmitHeaderVersion(version);
|
|
|
|
instruction_disassembler_.EmitHeaderGenerator(generator);
|
|
|
|
instruction_disassembler_.EmitHeaderIdBound(id_bound);
|
|
|
|
instruction_disassembler_.EmitHeaderSchema(schema);
|
2015-11-12 23:33:47 +00:00
|
|
|
}
|
2015-10-30 20:06:15 +00:00
|
|
|
|
2016-01-18 20:29:15 +00:00
|
|
|
byte_offset_ = SPV_INDEX_INSTRUCTION * sizeof(uint32_t);
|
|
|
|
|
2015-10-30 20:06:15 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t Disassembler::HandleInstruction(
|
|
|
|
const spv_parsed_instruction_t& inst) {
|
2021-12-14 14:46:59 +00:00
|
|
|
instruction_disassembler_.EmitSectionComment(inst, inserted_decoration_space_,
|
|
|
|
inserted_debug_space_,
|
|
|
|
inserted_type_space_);
|
|
|
|
|
|
|
|
instruction_disassembler_.EmitInstruction(inst, byte_offset_);
|
|
|
|
|
|
|
|
byte_offset_ += inst.num_words * sizeof(uint32_t);
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t Disassembler::SaveTextResult(spv_text* text_result) const {
|
|
|
|
if (!print_) {
|
|
|
|
size_t length = text_.str().size();
|
|
|
|
char* str = new char[length + 1];
|
|
|
|
if (!str) return SPV_ERROR_OUT_OF_MEMORY;
|
|
|
|
strncpy(str, text_.str().c_str(), length + 1);
|
|
|
|
spv_text text = new spv_text_t();
|
|
|
|
if (!text) {
|
|
|
|
delete[] str;
|
|
|
|
return SPV_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
text->str = str;
|
|
|
|
text->length = length;
|
|
|
|
*text_result = text;
|
2020-09-28 14:59:50 +00:00
|
|
|
}
|
2021-12-14 14:46:59 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t DisassembleHeader(void* user_data, spv_endianness_t endian,
|
|
|
|
uint32_t /* magic */, uint32_t version,
|
|
|
|
uint32_t generator, uint32_t id_bound,
|
|
|
|
uint32_t schema) {
|
|
|
|
assert(user_data);
|
|
|
|
auto disassembler = static_cast<Disassembler*>(user_data);
|
|
|
|
return disassembler->HandleHeader(endian, version, generator, id_bound,
|
|
|
|
schema);
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t DisassembleInstruction(
|
|
|
|
void* user_data, const spv_parsed_instruction_t* parsed_instruction) {
|
|
|
|
assert(user_data);
|
|
|
|
auto disassembler = static_cast<Disassembler*>(user_data);
|
|
|
|
return disassembler->HandleInstruction(*parsed_instruction);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple wrapper class to provide extra data necessary for targeted
|
|
|
|
// instruction disassembly.
|
|
|
|
class WrappedDisassembler {
|
|
|
|
public:
|
|
|
|
WrappedDisassembler(Disassembler* dis, const uint32_t* binary, size_t wc)
|
|
|
|
: disassembler_(dis), inst_binary_(binary), word_count_(wc) {}
|
|
|
|
|
|
|
|
Disassembler* disassembler() { return disassembler_; }
|
|
|
|
const uint32_t* inst_binary() const { return inst_binary_; }
|
|
|
|
size_t word_count() const { return word_count_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Disassembler* disassembler_;
|
|
|
|
const uint32_t* inst_binary_;
|
|
|
|
const size_t word_count_;
|
|
|
|
};
|
|
|
|
|
|
|
|
spv_result_t DisassembleTargetHeader(void* user_data, spv_endianness_t endian,
|
|
|
|
uint32_t /* magic */, uint32_t version,
|
|
|
|
uint32_t generator, uint32_t id_bound,
|
|
|
|
uint32_t schema) {
|
|
|
|
assert(user_data);
|
|
|
|
auto wrapped = static_cast<WrappedDisassembler*>(user_data);
|
|
|
|
return wrapped->disassembler()->HandleHeader(endian, version, generator,
|
|
|
|
id_bound, schema);
|
|
|
|
}
|
|
|
|
|
|
|
|
spv_result_t DisassembleTargetInstruction(
|
|
|
|
void* user_data, const spv_parsed_instruction_t* parsed_instruction) {
|
|
|
|
assert(user_data);
|
|
|
|
auto wrapped = static_cast<WrappedDisassembler*>(user_data);
|
|
|
|
// Check if this is the instruction we want to disassemble.
|
|
|
|
if (wrapped->word_count() == parsed_instruction->num_words &&
|
|
|
|
std::equal(wrapped->inst_binary(),
|
|
|
|
wrapped->inst_binary() + wrapped->word_count(),
|
|
|
|
parsed_instruction->words)) {
|
|
|
|
// Found the target instruction. Disassemble it and signal that we should
|
|
|
|
// stop searching so we don't output the same instruction again.
|
|
|
|
if (auto error =
|
|
|
|
wrapped->disassembler()->HandleInstruction(*parsed_instruction))
|
|
|
|
return error;
|
|
|
|
return SPV_REQUESTED_TERMINATION;
|
2020-09-28 14:59:50 +00:00
|
|
|
}
|
2021-12-14 14:46:59 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int kStandardIndent = 15;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace disassemble {
|
|
|
|
InstructionDisassembler::InstructionDisassembler(const AssemblyGrammar& grammar,
|
|
|
|
std::ostream& stream,
|
|
|
|
uint32_t options,
|
|
|
|
NameMapper name_mapper)
|
|
|
|
: grammar_(grammar),
|
|
|
|
stream_(stream),
|
|
|
|
print_(spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options)),
|
|
|
|
color_(spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COLOR, options)),
|
|
|
|
indent_(spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_INDENT, options)
|
|
|
|
? kStandardIndent
|
|
|
|
: 0),
|
|
|
|
comment_(spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COMMENT, options)),
|
|
|
|
show_byte_offset_(
|
|
|
|
spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET, options)),
|
|
|
|
name_mapper_(std::move(name_mapper)) {}
|
|
|
|
|
|
|
|
void InstructionDisassembler::EmitHeaderSpirv() { stream_ << "; SPIR-V\n"; }
|
|
|
|
|
|
|
|
void InstructionDisassembler::EmitHeaderVersion(uint32_t version) {
|
|
|
|
stream_ << "; Version: " << SPV_SPIRV_VERSION_MAJOR_PART(version) << "."
|
|
|
|
<< SPV_SPIRV_VERSION_MINOR_PART(version) << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionDisassembler::EmitHeaderGenerator(uint32_t generator) {
|
|
|
|
const char* generator_tool =
|
|
|
|
spvGeneratorStr(SPV_GENERATOR_TOOL_PART(generator));
|
|
|
|
stream_ << "; Generator: " << generator_tool;
|
|
|
|
// For unknown tools, print the numeric tool value.
|
|
|
|
if (0 == strcmp("Unknown", generator_tool)) {
|
|
|
|
stream_ << "(" << SPV_GENERATOR_TOOL_PART(generator) << ")";
|
2020-09-28 14:59:50 +00:00
|
|
|
}
|
2021-12-14 14:46:59 +00:00
|
|
|
// Print the miscellaneous part of the generator word on the same
|
|
|
|
// line as the tool name.
|
|
|
|
stream_ << "; " << SPV_GENERATOR_MISC_PART(generator) << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionDisassembler::EmitHeaderIdBound(uint32_t id_bound) {
|
|
|
|
stream_ << "; Bound: " << id_bound << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionDisassembler::EmitHeaderSchema(uint32_t schema) {
|
|
|
|
stream_ << "; Schema: " << schema << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionDisassembler::EmitInstruction(
|
|
|
|
const spv_parsed_instruction_t& inst, size_t inst_byte_offset) {
|
|
|
|
auto opcode = static_cast<SpvOp>(inst.opcode);
|
2020-09-28 14:59:50 +00:00
|
|
|
|
2015-11-05 22:45:09 +00:00
|
|
|
if (inst.result_id) {
|
2015-10-30 20:06:15 +00:00
|
|
|
SetBlue();
|
2016-07-08 18:29:52 +00:00
|
|
|
const std::string id_name = name_mapper_(inst.result_id);
|
|
|
|
if (indent_)
|
|
|
|
stream_ << std::setw(std::max(0, indent_ - 3 - int(id_name.size())));
|
|
|
|
stream_ << "%" << id_name;
|
2015-10-30 20:06:15 +00:00
|
|
|
ResetColor();
|
2015-11-12 18:53:27 +00:00
|
|
|
stream_ << " = ";
|
|
|
|
} else {
|
|
|
|
stream_ << std::string(indent_, ' ');
|
2015-10-30 20:06:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 14:59:50 +00:00
|
|
|
stream_ << "Op" << spvOpcodeString(opcode);
|
2015-10-30 20:06:15 +00:00
|
|
|
|
|
|
|
for (uint16_t i = 0; i < inst.num_operands; i++) {
|
|
|
|
const spv_operand_type_t type = inst.operands[i].type;
|
|
|
|
assert(type != SPV_OPERAND_TYPE_NONE);
|
2015-11-05 22:45:09 +00:00
|
|
|
if (type == SPV_OPERAND_TYPE_RESULT_ID) continue;
|
2015-10-30 20:06:15 +00:00
|
|
|
stream_ << " ";
|
|
|
|
EmitOperand(inst, i);
|
|
|
|
}
|
|
|
|
|
2020-09-28 14:59:50 +00:00
|
|
|
if (comment_ && opcode == SpvOpName) {
|
|
|
|
const spv_parsed_operand_t& operand = inst.operands[0];
|
|
|
|
const uint32_t word = inst.words[operand.offset];
|
|
|
|
stream_ << " ; id %" << word;
|
|
|
|
}
|
|
|
|
|
2016-01-18 20:29:15 +00:00
|
|
|
if (show_byte_offset_) {
|
|
|
|
SetGrey();
|
|
|
|
auto saved_flags = stream_.flags();
|
|
|
|
auto saved_fill = stream_.fill();
|
|
|
|
stream_ << " ; 0x" << std::setw(8) << std::hex << std::setfill('0')
|
2021-12-14 14:46:59 +00:00
|
|
|
<< inst_byte_offset;
|
2016-01-18 20:29:15 +00:00
|
|
|
stream_.flags(saved_flags);
|
|
|
|
stream_.fill(saved_fill);
|
|
|
|
ResetColor();
|
|
|
|
}
|
2015-10-30 20:06:15 +00:00
|
|
|
stream_ << "\n";
|
|
|
|
}
|
|
|
|
|
2021-12-14 14:46:59 +00:00
|
|
|
void InstructionDisassembler::EmitSectionComment(
|
|
|
|
const spv_parsed_instruction_t& inst, bool& inserted_decoration_space,
|
|
|
|
bool& inserted_debug_space, bool& inserted_type_space) {
|
|
|
|
auto opcode = static_cast<SpvOp>(inst.opcode);
|
|
|
|
if (comment_ && opcode == SpvOpFunction) {
|
|
|
|
stream_ << std::endl;
|
|
|
|
stream_ << std::string(indent_, ' ');
|
|
|
|
stream_ << "; Function " << name_mapper_(inst.result_id) << std::endl;
|
|
|
|
}
|
|
|
|
if (comment_ && !inserted_decoration_space && spvOpcodeIsDecoration(opcode)) {
|
|
|
|
inserted_decoration_space = true;
|
|
|
|
stream_ << std::endl;
|
|
|
|
stream_ << std::string(indent_, ' ');
|
|
|
|
stream_ << "; Annotations" << std::endl;
|
|
|
|
}
|
|
|
|
if (comment_ && !inserted_debug_space && spvOpcodeIsDebug(opcode)) {
|
|
|
|
inserted_debug_space = true;
|
|
|
|
stream_ << std::endl;
|
|
|
|
stream_ << std::string(indent_, ' ');
|
|
|
|
stream_ << "; Debug Information" << std::endl;
|
|
|
|
}
|
|
|
|
if (comment_ && !inserted_type_space && spvOpcodeGeneratesType(opcode)) {
|
|
|
|
inserted_type_space = true;
|
|
|
|
stream_ << std::endl;
|
|
|
|
stream_ << std::string(indent_, ' ');
|
|
|
|
stream_ << "; Types, variables and constants" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionDisassembler::EmitOperand(const spv_parsed_instruction_t& inst,
|
|
|
|
const uint16_t operand_index) {
|
2015-10-30 20:06:15 +00:00
|
|
|
assert(operand_index < inst.num_operands);
|
|
|
|
const spv_parsed_operand_t& operand = inst.operands[operand_index];
|
2015-11-20 19:21:10 +00:00
|
|
|
const uint32_t word = inst.words[operand.offset];
|
2015-10-30 20:06:15 +00:00
|
|
|
switch (operand.type) {
|
|
|
|
case SPV_OPERAND_TYPE_RESULT_ID:
|
2015-11-05 22:45:09 +00:00
|
|
|
assert(false && "<result-id> is not supposed to be handled here");
|
2015-10-30 20:06:15 +00:00
|
|
|
SetBlue();
|
2016-07-08 18:29:52 +00:00
|
|
|
stream_ << "%" << name_mapper_(word);
|
2015-10-30 20:06:15 +00:00
|
|
|
break;
|
|
|
|
case SPV_OPERAND_TYPE_ID:
|
|
|
|
case SPV_OPERAND_TYPE_TYPE_ID:
|
2015-11-18 20:48:32 +00:00
|
|
|
case SPV_OPERAND_TYPE_SCOPE_ID:
|
|
|
|
case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
|
2015-10-30 20:06:15 +00:00
|
|
|
SetYellow();
|
2016-07-08 18:29:52 +00:00
|
|
|
stream_ << "%" << name_mapper_(word);
|
2015-10-30 20:06:15 +00:00
|
|
|
break;
|
|
|
|
case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
|
|
|
|
spv_ext_inst_desc ext_inst;
|
|
|
|
SetRed();
|
2019-12-18 23:10:29 +00:00
|
|
|
if (grammar_.lookupExtInst(inst.ext_inst_type, word, &ext_inst) ==
|
|
|
|
SPV_SUCCESS) {
|
|
|
|
stream_ << ext_inst->name;
|
|
|
|
} else {
|
|
|
|
if (!spvExtInstIsNonSemantic(inst.ext_inst_type)) {
|
|
|
|
assert(false && "should have caught this earlier");
|
|
|
|
} else {
|
|
|
|
// for non-semantic instruction sets we can just print the number
|
|
|
|
stream_ << word;
|
|
|
|
}
|
|
|
|
}
|
2015-10-30 20:06:15 +00:00
|
|
|
} break;
|
2015-11-11 07:45:45 +00:00
|
|
|
case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
|
|
|
|
spv_opcode_desc opcode_desc;
|
|
|
|
if (grammar_.lookupOpcode(SpvOp(word), &opcode_desc))
|
|
|
|
assert(false && "should have caught this earlier");
|
|
|
|
SetRed();
|
|
|
|
stream_ << opcode_desc->name;
|
|
|
|
} break;
|
2015-10-30 20:06:15 +00:00
|
|
|
case SPV_OPERAND_TYPE_LITERAL_INTEGER:
|
2015-11-04 22:38:17 +00:00
|
|
|
case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: {
|
2015-10-30 20:06:15 +00:00
|
|
|
SetRed();
|
2021-12-14 14:46:59 +00:00
|
|
|
EmitNumericLiteral(&stream_, inst, operand);
|
2016-10-13 19:17:11 +00:00
|
|
|
ResetColor();
|
2015-10-30 20:06:15 +00:00
|
|
|
} break;
|
|
|
|
case SPV_OPERAND_TYPE_LITERAL_STRING: {
|
|
|
|
stream_ << "\"";
|
|
|
|
SetGreen();
|
2021-12-08 17:01:26 +00:00
|
|
|
|
|
|
|
std::string str = spvDecodeLiteralStringOperand(inst, operand_index);
|
|
|
|
for (char const& c : str) {
|
|
|
|
if (c == '"' || c == '\\') stream_ << '\\';
|
|
|
|
stream_ << c;
|
2015-10-30 20:06:15 +00:00
|
|
|
}
|
|
|
|
ResetColor();
|
|
|
|
stream_ << '"';
|
|
|
|
} break;
|
|
|
|
case SPV_OPERAND_TYPE_CAPABILITY:
|
|
|
|
case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
|
|
|
|
case SPV_OPERAND_TYPE_EXECUTION_MODEL:
|
|
|
|
case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
|
|
|
|
case SPV_OPERAND_TYPE_MEMORY_MODEL:
|
|
|
|
case SPV_OPERAND_TYPE_EXECUTION_MODE:
|
|
|
|
case SPV_OPERAND_TYPE_STORAGE_CLASS:
|
|
|
|
case SPV_OPERAND_TYPE_DIMENSIONALITY:
|
|
|
|
case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
|
|
|
|
case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
|
2015-11-24 23:37:24 +00:00
|
|
|
case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT:
|
2015-10-30 20:06:15 +00:00
|
|
|
case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
|
|
|
|
case SPV_OPERAND_TYPE_LINKAGE_TYPE:
|
|
|
|
case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
|
|
|
|
case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
|
|
|
|
case SPV_OPERAND_TYPE_DECORATION:
|
|
|
|
case SPV_OPERAND_TYPE_BUILT_IN:
|
|
|
|
case SPV_OPERAND_TYPE_GROUP_OPERATION:
|
|
|
|
case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
|
2017-12-03 17:30:08 +00:00
|
|
|
case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO:
|
2020-03-17 19:30:19 +00:00
|
|
|
case SPV_OPERAND_TYPE_RAY_FLAGS:
|
|
|
|
case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION:
|
|
|
|
case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE:
|
|
|
|
case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE:
|
2017-12-03 17:30:08 +00:00
|
|
|
case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
|
|
|
|
case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE:
|
|
|
|
case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER:
|
2019-12-19 22:16:26 +00:00
|
|
|
case SPV_OPERAND_TYPE_DEBUG_OPERATION:
|
|
|
|
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING:
|
|
|
|
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE:
|
|
|
|
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER:
|
|
|
|
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION:
|
2021-01-20 14:39:51 +00:00
|
|
|
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY:
|
|
|
|
case SPV_OPERAND_TYPE_FPDENORM_MODE:
|
2021-06-09 20:20:39 +00:00
|
|
|
case SPV_OPERAND_TYPE_FPOPERATION_MODE:
|
|
|
|
case SPV_OPERAND_TYPE_QUANTIZATION_MODES:
|
|
|
|
case SPV_OPERAND_TYPE_OVERFLOW_MODES: {
|
2015-10-30 20:06:15 +00:00
|
|
|
spv_operand_desc entry;
|
|
|
|
if (grammar_.lookupOperand(operand.type, word, &entry))
|
|
|
|
assert(false && "should have caught this earlier");
|
|
|
|
stream_ << entry->name;
|
|
|
|
} break;
|
|
|
|
case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
|
|
|
|
case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
|
|
|
|
case SPV_OPERAND_TYPE_LOOP_CONTROL:
|
2015-11-04 22:38:17 +00:00
|
|
|
case SPV_OPERAND_TYPE_IMAGE:
|
|
|
|
case SPV_OPERAND_TYPE_MEMORY_ACCESS:
|
2015-10-30 20:06:15 +00:00
|
|
|
case SPV_OPERAND_TYPE_SELECTION_CONTROL:
|
2017-12-03 17:30:08 +00:00
|
|
|
case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS:
|
2019-12-19 22:16:26 +00:00
|
|
|
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS:
|
2015-10-30 20:06:15 +00:00
|
|
|
EmitMaskOperand(operand.type, word);
|
|
|
|
break;
|
|
|
|
default:
|
2021-06-23 17:32:24 +00:00
|
|
|
if (spvOperandIsConcreteMask(operand.type)) {
|
|
|
|
EmitMaskOperand(operand.type, word);
|
|
|
|
} else if (spvOperandIsConcrete(operand.type)) {
|
|
|
|
spv_operand_desc entry;
|
|
|
|
if (grammar_.lookupOperand(operand.type, word, &entry))
|
|
|
|
assert(false && "should have caught this earlier");
|
|
|
|
stream_ << entry->name;
|
|
|
|
} else {
|
|
|
|
assert(false && "unhandled or invalid case");
|
|
|
|
}
|
|
|
|
break;
|
2015-10-30 20:06:15 +00:00
|
|
|
}
|
|
|
|
ResetColor();
|
|
|
|
}
|
|
|
|
|
2021-12-14 14:46:59 +00:00
|
|
|
void InstructionDisassembler::EmitMaskOperand(const spv_operand_type_t type,
|
|
|
|
const uint32_t word) {
|
2015-10-30 20:06:15 +00:00
|
|
|
// Scan the mask from least significant bit to most significant bit. For each
|
|
|
|
// set bit, emit the name of that bit. Separate multiple names with '|'.
|
|
|
|
uint32_t remaining_word = word;
|
|
|
|
uint32_t mask;
|
|
|
|
int num_emitted = 0;
|
|
|
|
for (mask = 1; remaining_word; mask <<= 1) {
|
|
|
|
if (remaining_word & mask) {
|
|
|
|
remaining_word ^= mask;
|
|
|
|
spv_operand_desc entry;
|
|
|
|
if (grammar_.lookupOperand(type, mask, &entry))
|
|
|
|
assert(false && "should have caught this earlier");
|
|
|
|
if (num_emitted) stream_ << "|";
|
|
|
|
stream_ << entry->name;
|
|
|
|
num_emitted++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!num_emitted) {
|
|
|
|
// An operand value of 0 was provided, so represent it by the name
|
|
|
|
// of the 0 value. In many cases, that's "None".
|
|
|
|
spv_operand_desc entry;
|
|
|
|
if (SPV_SUCCESS == grammar_.lookupOperand(type, 0, &entry))
|
|
|
|
stream_ << entry->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 14:46:59 +00:00
|
|
|
void InstructionDisassembler::ResetColor() {
|
|
|
|
if (color_) stream_ << spvtools::clr::reset{print_};
|
2015-10-30 20:06:15 +00:00
|
|
|
}
|
2021-12-14 14:46:59 +00:00
|
|
|
void InstructionDisassembler::SetGrey() {
|
|
|
|
if (color_) stream_ << spvtools::clr::grey{print_};
|
2015-10-30 20:06:15 +00:00
|
|
|
}
|
2021-12-14 14:46:59 +00:00
|
|
|
void InstructionDisassembler::SetBlue() {
|
|
|
|
if (color_) stream_ << spvtools::clr::blue{print_};
|
2015-10-30 20:06:15 +00:00
|
|
|
}
|
2021-12-14 14:46:59 +00:00
|
|
|
void InstructionDisassembler::SetYellow() {
|
|
|
|
if (color_) stream_ << spvtools::clr::yellow{print_};
|
|
|
|
}
|
|
|
|
void InstructionDisassembler::SetRed() {
|
|
|
|
if (color_) stream_ << spvtools::clr::red{print_};
|
|
|
|
}
|
|
|
|
void InstructionDisassembler::SetGreen() {
|
|
|
|
if (color_) stream_ << spvtools::clr::green{print_};
|
|
|
|
}
|
|
|
|
} // namespace disassemble
|
|
|
|
|
|
|
|
std::string spvInstructionBinaryToText(const spv_target_env env,
|
|
|
|
const uint32_t* instCode,
|
|
|
|
const size_t instWordCount,
|
|
|
|
const uint32_t* code,
|
|
|
|
const size_t wordCount,
|
|
|
|
const uint32_t options) {
|
|
|
|
spv_context context = spvContextCreate(env);
|
|
|
|
const AssemblyGrammar grammar(context);
|
|
|
|
if (!grammar.isValid()) {
|
|
|
|
spvContextDestroy(context);
|
|
|
|
return "";
|
|
|
|
}
|
2015-10-30 20:06:15 +00:00
|
|
|
|
2021-12-14 14:46:59 +00:00
|
|
|
// Generate friendly names for Ids if requested.
|
|
|
|
std::unique_ptr<FriendlyNameMapper> friendly_mapper;
|
|
|
|
NameMapper name_mapper = GetTrivialNameMapper();
|
|
|
|
if (options & SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES) {
|
|
|
|
friendly_mapper = MakeUnique<FriendlyNameMapper>(context, code, wordCount);
|
|
|
|
name_mapper = friendly_mapper->GetNameMapper();
|
|
|
|
}
|
2018-01-10 19:23:47 +00:00
|
|
|
|
2021-12-14 14:46:59 +00:00
|
|
|
// Now disassemble!
|
|
|
|
Disassembler disassembler(grammar, options, name_mapper);
|
|
|
|
WrappedDisassembler wrapped(&disassembler, instCode, instWordCount);
|
|
|
|
spvBinaryParse(context, &wrapped, code, wordCount, DisassembleTargetHeader,
|
|
|
|
DisassembleTargetInstruction, nullptr);
|
2018-01-10 19:23:47 +00:00
|
|
|
|
2021-12-14 14:46:59 +00:00
|
|
|
spv_text text = nullptr;
|
|
|
|
std::string output;
|
|
|
|
if (disassembler.SaveTextResult(&text) == SPV_SUCCESS) {
|
|
|
|
output.assign(text->str, text->str + text->length);
|
|
|
|
// Drop trailing newline characters.
|
|
|
|
while (!output.empty() && output.back() == '\n') output.pop_back();
|
2018-01-10 19:23:47 +00:00
|
|
|
}
|
2021-12-14 14:46:59 +00:00
|
|
|
spvTextDestroy(text);
|
|
|
|
spvContextDestroy(context);
|
2018-01-10 19:23:47 +00:00
|
|
|
|
2021-12-14 14:46:59 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
} // namespace spvtools
|
2015-10-27 20:56:14 +00:00
|
|
|
|
2015-11-12 18:48:30 +00:00
|
|
|
spv_result_t spvBinaryToText(const spv_const_context context,
|
|
|
|
const uint32_t* code, const size_t wordCount,
|
2015-11-11 16:33:26 +00:00
|
|
|
const uint32_t options, spv_text* pText,
|
|
|
|
spv_diagnostic* pDiagnostic) {
|
2016-09-02 22:06:18 +00:00
|
|
|
spv_context_t hijack_context = *context;
|
|
|
|
if (pDiagnostic) {
|
|
|
|
*pDiagnostic = nullptr;
|
2018-07-07 13:38:00 +00:00
|
|
|
spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
|
2016-09-02 22:06:18 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
const spvtools::AssemblyGrammar grammar(&hijack_context);
|
2015-10-30 20:06:15 +00:00
|
|
|
if (!grammar.isValid()) return SPV_ERROR_INVALID_TABLE;
|
|
|
|
|
2016-07-08 18:29:52 +00:00
|
|
|
// Generate friendly names for Ids if requested.
|
2018-07-07 13:38:00 +00:00
|
|
|
std::unique_ptr<spvtools::FriendlyNameMapper> friendly_mapper;
|
|
|
|
spvtools::NameMapper name_mapper = spvtools::GetTrivialNameMapper();
|
2016-07-08 18:29:52 +00:00
|
|
|
if (options & SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES) {
|
2018-08-14 19:01:50 +00:00
|
|
|
friendly_mapper = spvtools::MakeUnique<spvtools::FriendlyNameMapper>(
|
|
|
|
&hijack_context, code, wordCount);
|
2016-07-08 18:29:52 +00:00
|
|
|
name_mapper = friendly_mapper->GetNameMapper();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now disassemble!
|
2021-12-14 14:46:59 +00:00
|
|
|
spvtools::Disassembler disassembler(grammar, options, name_mapper);
|
|
|
|
if (auto error =
|
|
|
|
spvBinaryParse(&hijack_context, &disassembler, code, wordCount,
|
|
|
|
spvtools::DisassembleHeader,
|
|
|
|
spvtools::DisassembleInstruction, pDiagnostic)) {
|
2015-10-30 20:06:15 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return disassembler.SaveTextResult(pText);
|
|
|
|
}
|