SPIRV-Tools/source/assembly_grammar.cpp
David Neto 0ca6b59bfd Refactor the binary parser.
The binary parser has a C API, described in binary.h.
Eventually we will make it public in libspirv.h.
The API is event-driven in the sense that a callback is called
when a valid header is parsed, and for each parsed instruction.

Classify some operand types as "concrete".  The binary parser uses
only concrete operand types to describe parsed instructions.

The old disassembler APIs are moved into disassemble.cpp

TODO: Add unit tests for spvBinaryParse.
2015-11-10 15:57:33 -05:00

171 lines
6.2 KiB
C++

// Copyright (c) 2015 The Khronos Group Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and/or associated documentation files (the
// "Materials"), to deal in the Materials without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Materials, and to
// permit persons to whom the Materials are furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Materials.
//
// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
// https://www.khronos.org/registry/
//
// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
#include "assembly_grammar.h"
#include <algorithm>
#include <cassert>
#include <cstring>
#include "ext_inst.h"
#include "opcode.h"
#include "operand.h"
namespace {
/// @brief Parses a mask expression string for the given operand type.
///
/// A mask expression is a sequence of one or more terms separated by '|',
/// where each term a named enum value for the given type. No whitespace
/// is permitted.
///
/// On success, the value is written to pValue.
///
/// @param[in] operandTable operand lookup table
/// @param[in] type of the operand
/// @param[in] textValue word of text to be parsed
/// @param[out] pValue where the resulting value is written
///
/// @return result code
spv_result_t spvTextParseMaskOperand(const spv_operand_table operandTable,
const spv_operand_type_t type,
const char* textValue, uint32_t* pValue) {
if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT;
size_t text_length = strlen(textValue);
if (text_length == 0) return SPV_ERROR_INVALID_TEXT;
const char* text_end = textValue + text_length;
// We only support mask expressions in ASCII, so the separator value is a
// char.
const char separator = '|';
// Accumulate the result by interpreting one word at a time, scanning
// from left to right.
uint32_t value = 0;
const char* begin = textValue; // The left end of the current word.
const char* end = nullptr; // One character past the end of the current word.
do {
end = std::find(begin, text_end, separator);
spv_operand_desc entry = nullptr;
if (spvOperandTableNameLookup(operandTable, type, begin, end - begin,
&entry)) {
return SPV_ERROR_INVALID_TEXT;
}
value |= entry->value;
// Advance to the next word by skipping over the separator.
begin = end + 1;
} while (end != text_end);
*pValue = value;
return SPV_SUCCESS;
}
// Returns the operand table.
spv_operand_table GetDefaultOperandTable() {
spv_operand_table result = nullptr;
spvOperandTableGet(&result);
assert(result);
return result;
}
// Returns the opcode table.
spv_opcode_table GetDefaultOpcodeTable() {
spv_opcode_table result = nullptr;
spvOpcodeTableGet(&result);
assert(result);
return result;
}
// Returns the extended instruction table.
spv_ext_inst_table GetDefaultExtInstTable() {
spv_ext_inst_table result = nullptr;
spvExtInstTableGet(&result);
assert(result);
return result;
}
} // anonymous namespace
namespace libspirv {
AssemblyGrammar::AssemblyGrammar()
: AssemblyGrammar(GetDefaultOperandTable(), GetDefaultOpcodeTable(),
GetDefaultExtInstTable()) {
assert(isValid());
}
bool AssemblyGrammar::isValid() const {
return operandTable_ && opcodeTable_ && extInstTable_;
}
spv_result_t AssemblyGrammar::lookupOpcode(const char* name,
spv_opcode_desc* desc) const {
return spvOpcodeTableNameLookup(opcodeTable_, name, desc);
}
spv_result_t AssemblyGrammar::lookupOpcode(SpvOp opcode,
spv_opcode_desc* desc) const {
return spvOpcodeTableValueLookup(opcodeTable_, opcode, desc);
}
spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type,
const char* name, size_t name_len,
spv_operand_desc* desc) const {
return spvOperandTableNameLookup(operandTable_, type, name, name_len, desc);
}
spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type,
uint32_t operand,
spv_operand_desc* desc) const {
return spvOperandTableValueLookup(operandTable_, type, operand, desc);
}
spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type,
const char* textValue,
uint32_t* pValue) const {
return spvTextParseMaskOperand(operandTable_, type, textValue, pValue);
}
spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type,
const char* textValue,
spv_ext_inst_desc* extInst) const {
return spvExtInstTableNameLookup(extInstTable_, type, textValue, extInst);
}
spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type,
uint32_t firstWord,
spv_ext_inst_desc* extInst) const {
return spvExtInstTableValueLookup(extInstTable_, type, firstWord, extInst);
}
void AssemblyGrammar::prependOperandTypesForMask(
const spv_operand_type_t type, const uint32_t mask,
spv_operand_pattern_t* pattern) const {
spvPrependOperandTypesForMask(operandTable_, type, mask, pattern);
}
} // namespace libspirv