Move AssemblyGrammar into its own source files

This commit is contained in:
David Neto 2015-10-27 15:31:10 -04:00
parent e5e99b57cc
commit fcc7d58353
8 changed files with 264 additions and 187 deletions

View File

@ -103,6 +103,7 @@ include_directories(
set(SPIRV_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/include/libspirv/libspirv.h
${CMAKE_CURRENT_SOURCE_DIR}/include/util/bitutils.h
${CMAKE_CURRENT_SOURCE_DIR}/source/assembly_grammar.h
${CMAKE_CURRENT_SOURCE_DIR}/source/binary.h
${CMAKE_CURRENT_SOURCE_DIR}/source/diagnostic.h
${CMAKE_CURRENT_SOURCE_DIR}/source/ext_inst.h
@ -113,6 +114,7 @@ set(SPIRV_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/source/text.h
${CMAKE_CURRENT_SOURCE_DIR}/source/text_handler.h
${CMAKE_CURRENT_SOURCE_DIR}/source/validate.h
${CMAKE_CURRENT_SOURCE_DIR}/source/assembly_grammar.cpp
${CMAKE_CURRENT_SOURCE_DIR}/source/binary.cpp
${CMAKE_CURRENT_SOURCE_DIR}/source/diagnostic.cpp
${CMAKE_CURRENT_SOURCE_DIR}/source/ext_inst.cpp

137
source/assembly_grammar.cpp Normal file
View File

@ -0,0 +1,137 @@
// 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 <cstring>
#include "ext_inst.h"
#include "opcode.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;
}
} // anonymous namespace
namespace libspirv {
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(Op 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

114
source/assembly_grammar.h Normal file
View File

@ -0,0 +1,114 @@
// 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.
#ifndef _LIBSPIRV_UTIL_ASSEMBLY_GRAMMAR_H_
#define _LIBSPIRV_UTIL_ASSEMBLY_GRAMMAR_H_
#include <libspirv/libspirv.h>
#include "operand.h"
namespace libspirv {
// Encapsulates the grammar to use for SPIR-V assembly.
// Contains methods to query for valid instructions and operands.
class AssemblyGrammar {
public:
AssemblyGrammar(const spv_operand_table operand_table,
const spv_opcode_table opcode_table,
const spv_ext_inst_table ext_inst_table)
: operandTable_(operand_table),
opcodeTable_(opcode_table),
extInstTable_(ext_inst_table) {}
// Returns true if the compilation_data has been initialized with valid data.
bool isValid() const;
// Fills in the desc parameter with the information about the opcode
// of the given name. Returns SPV_SUCCESS if the opcode was found, and
// SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
spv_result_t lookupOpcode(const char *name, spv_opcode_desc *desc) const;
// Fills in the desc parameter with the information about the opcode
// of the valid. Returns SPV_SUCCESS if the opcode was found, and
// SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
spv_result_t lookupOpcode(Op opcode, spv_opcode_desc *desc) const;
// Fills in the desc parameter with the information about the given
// operand. Returns SPV_SUCCESS if the operand was found, and
// SPV_ERROR_INVALID_LOOKUP otherwise.
spv_result_t lookupOperand(spv_operand_type_t type, const char *name,
size_t name_len, spv_operand_desc *desc) const;
// Fills in the desc parameter with the information about the given
// operand. Returns SPV_SUCCESS if the operand was found, and
// SPV_ERROR_INVALID_LOOKUP otherwise.
spv_result_t lookupOperand(spv_operand_type_t type, uint32_t operand,
spv_operand_desc *desc) const;
// 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 is a named enum value for a given type. No whitespace
// is permitted.
//
// On success, the value is written to pValue, and SPV_SUCCESS is returend.
// The operand type is defined by the type parameter, and the text to be
// parsed is defined by the textValue parameter.
spv_result_t parseMaskOperand(const spv_operand_type_t type,
const char *textValue, uint32_t *pValue) const;
// Writes the extended operand with the given type and text to the *extInst
// parameter.
// Returns SPV_SUCCESS if the value could be found.
spv_result_t lookupExtInst(spv_ext_inst_type_t type, const char *textValue,
spv_ext_inst_desc *extInst) const;
// Writes the extended operand with the given type and first encoded word
// to the *extInst parameter.
// Returns SPV_SUCCESS if the value could be found.
spv_result_t lookupExtInst(spv_ext_inst_type_t type, uint32_t firstWord,
spv_ext_inst_desc *extInst) const;
// Inserts the operands expected after the given typed mask onto the front
// of the given pattern.
//
// Each set bit in the mask represents zero or more operand types that should
// be prepended onto the pattern. Opearnds for a less significant bit always
// appear before operands for a more significatn bit.
//
// If a set bit is unknown, then we assume it has no operands.
void prependOperandTypesForMask(const spv_operand_type_t type,
const uint32_t mask,
spv_operand_pattern_t *pattern) const;
private:
const spv_operand_table operandTable_;
const spv_opcode_table opcodeTable_;
const spv_ext_inst_table extInstTable_;
};
}
#endif // _LIBSPIRV_UTIL_ASSEMBLY_GRAMMAR_H_

View File

@ -24,8 +24,15 @@
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
#include <libspirv/libspirv.h>
#include "binary.h"
#include <cassert>
#include <cstring>
#include <sstream>
#include <unordered_map>
#include <libspirv/libspirv.h>
#include "assembly_grammar.h"
#include "diagnostic.h"
#include "ext_inst.h"
#include "instruction.h"
@ -33,12 +40,6 @@
#include "operand.h"
#include "text_handler.h"
#include <assert.h>
#include <string.h>
#include <sstream>
#include <unordered_map>
// Binary API
enum {

View File

@ -38,6 +38,7 @@
#include <unordered_map>
#include <vector>
#include "assembly_grammar.h"
#include "binary.h"
#include "diagnostic.h"
#include "ext_inst.h"

View File

@ -26,12 +26,12 @@
#include "text_handler.h"
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <tuple>
#include "assembly_grammar.h"
#include "binary.h"
#include "ext_inst.h"
#include "instruction.h"
@ -163,110 +163,12 @@ bool startsWithOp(spv_text text, spv_position position) {
return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
}
/// @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;
}
} // anonymous namespace
namespace libspirv {
const IdType kUnknownType = {0, false, IdTypeClass::kBottom};
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(Op 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);
}
// TODO(dneto): Reorder AssemblyContext definitions to match declaration order.
// This represents all of the data that is only valid for the duration of

View File

@ -36,7 +36,6 @@
#include <libspirv/libspirv.h>
#include "diagnostic.h"
#include "instruction.h"
#include "operand.h"
#include "text.h"
namespace libspirv {
@ -102,86 +101,6 @@ inline int assumedBitWidth(const IdType& type) {
return 0;
}
// Encapsulates the grammar to use for SPIR-V assembly.
// Contains methods to query for valid instructions and operands.
class AssemblyGrammar {
public:
AssemblyGrammar(const spv_operand_table operand_table,
const spv_opcode_table opcode_table,
const spv_ext_inst_table ext_inst_table)
: operandTable_(operand_table),
opcodeTable_(opcode_table),
extInstTable_(ext_inst_table) {}
// Returns true if the compilation_data has been initialized with valid data.
bool isValid() const;
// Fills in the desc parameter with the information about the opcode
// of the given name. Returns SPV_SUCCESS if the opcode was found, and
// SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
spv_result_t lookupOpcode(const char *name, spv_opcode_desc *desc) const;
// Fills in the desc parameter with the information about the opcode
// of the valid. Returns SPV_SUCCESS if the opcode was found, and
// SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
spv_result_t lookupOpcode(Op opcode, spv_opcode_desc *desc) const;
// Fills in the desc parameter with the information about the given
// operand. Returns SPV_SUCCESS if the operand was found, and
// SPV_ERROR_INVALID_LOOKUP otherwise.
spv_result_t lookupOperand(spv_operand_type_t type, const char *name,
size_t name_len, spv_operand_desc *desc) const;
// Fills in the desc parameter with the information about the given
// operand. Returns SPV_SUCCESS if the operand was found, and
// SPV_ERROR_INVALID_LOOKUP otherwise.
spv_result_t lookupOperand(spv_operand_type_t type, uint32_t operand,
spv_operand_desc *desc) const;
// 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 is a named enum value for a given type. No whitespace
// is permitted.
//
// On success, the value is written to pValue, and SPV_SUCCESS is returend.
// The operand type is defined by the type parameter, and the text to be
// parsed is defined by the textValue parameter.
spv_result_t parseMaskOperand(const spv_operand_type_t type,
const char *textValue, uint32_t *pValue) const;
// Writes the extended operand with the given type and text to the *extInst
// parameter.
// Returns SPV_SUCCESS if the value could be found.
spv_result_t lookupExtInst(spv_ext_inst_type_t type, const char *textValue,
spv_ext_inst_desc *extInst) const;
// Writes the extended operand with the given type and first encoded word
// to the *extInst parameter.
// Returns SPV_SUCCESS if the value could be found.
spv_result_t lookupExtInst(spv_ext_inst_type_t type,
uint32_t firstWord,
spv_ext_inst_desc *extInst) const;
// Inserts the operands expected after the given typed mask onto the front
// of the given pattern.
//
// Each set bit in the mask represents zero or more operand types that should
// be prepended onto the pattern. Opearnds for a less significant bit always
// appear before operands for a more significatn bit.
//
// If a set bit is unknown, then we assume it has no operands.
void prependOperandTypesForMask(const spv_operand_type_t type,
const uint32_t mask,
spv_operand_pattern_t *pattern) const;
private:
const spv_operand_table operandTable_;
const spv_opcode_table opcodeTable_;
const spv_ext_inst_table extInstTable_;
};
// Encapsulates the data used during the assembly of a SPIR-V module.
class AssemblyContext {
public:

View File

@ -28,6 +28,7 @@
#define _CODEPLAY_UNITBIL_H_
#include <libspirv/libspirv.h>
#include "../source/assembly_grammar.h"
#include "../source/binary.h"
#include "../source/diagnostic.h"
#include "../source/opcode.h"