mirror of
https://github.com/xenia-project/SPIRV-Tools.git
synced 2024-11-23 19:29:46 +00:00
Refactored dynamic and static state out of text processing.
This reduces the number of arguments required to be passed to every single function. This is in preparation for adding id tracking.
This commit is contained in:
parent
50babb2d00
commit
71fc05587b
@ -103,6 +103,7 @@ set(SPIRV_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/operand.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/print.h
|
||||
${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/binary.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/diagnostic.cpp
|
||||
@ -111,6 +112,7 @@ set(SPIRV_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/operand.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/print.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/text.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/text_handler.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/validate.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/source/validate_id.cpp)
|
||||
|
||||
|
@ -119,54 +119,6 @@ spv_result_t spvBinaryHeaderSet(spv_binary_t *binary, const uint32_t bound) {
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
|
||||
spv_result_t spvBinaryEncodeU32(const uint32_t value, spv_instruction_t *pInst,
|
||||
const spv_position position,
|
||||
spv_diagnostic *pDiagnostic) {
|
||||
if (pInst->wordCount + 1 > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
|
||||
DIAGNOSTIC << "Instruction word count '"
|
||||
<< SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "' exceeded.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
|
||||
pInst->words[pInst->wordCount++] = (uint32_t)value;
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
|
||||
spv_result_t spvBinaryEncodeU64(const uint64_t value, spv_instruction_t *pInst,
|
||||
const spv_position position,
|
||||
spv_diagnostic *pDiagnostic) {
|
||||
if (pInst->wordCount + 2 > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
|
||||
DIAGNOSTIC << "Instruction word count '"
|
||||
<< SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "' exceeded.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
|
||||
uint32_t low = (uint32_t)(0x00000000ffffffff & value);
|
||||
uint32_t high = (uint32_t)((0xffffffff00000000 & value) >> 32);
|
||||
pInst->words[pInst->wordCount++] = low;
|
||||
pInst->words[pInst->wordCount++] = high;
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
|
||||
spv_result_t spvBinaryEncodeString(const char *str, spv_instruction_t *pInst,
|
||||
const spv_position position,
|
||||
spv_diagnostic *pDiagnostic) {
|
||||
size_t length = strlen(str);
|
||||
size_t wordCount = (length / 4) + 1;
|
||||
if ((sizeof(uint32_t) * pInst->wordCount) + length >
|
||||
sizeof(uint32_t) * SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
|
||||
DIAGNOSTIC << "Instruction word count '"
|
||||
<< SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "'exceeded.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
|
||||
char *dest = (char *)&pInst->words[pInst->wordCount];
|
||||
strncpy(dest, str, length);
|
||||
pInst->wordCount += (uint16_t)wordCount;
|
||||
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
|
||||
// TODO(dneto): This API is not powerful enough in the case that the
|
||||
// number and type of operands are not known until partway through parsing
|
||||
// the operation. This happens when enum operands might have different number
|
||||
|
@ -83,42 +83,6 @@ spv_result_t spvBinaryHeaderGet(const spv_binary binary,
|
||||
/// @return result code
|
||||
spv_result_t spvBinaryHeaderSet(spv_binary binary, const uint32_t bound);
|
||||
|
||||
/// @brief Append a single word into a binary stream
|
||||
///
|
||||
/// @param[in] value the word to encode
|
||||
/// @param[in] pInst the stream to append to
|
||||
/// @param[in,out] position position in the binary
|
||||
/// @param[out] pDiagnostic contains diagnostic on failure
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t spvBinaryEncodeU32(const uint32_t value, spv_instruction_t *pInst,
|
||||
const spv_position position,
|
||||
spv_diagnostic *pDiagnostic);
|
||||
|
||||
/// @brief Append two related words into the binary stream
|
||||
///
|
||||
/// @param[in] value the two words to encode
|
||||
/// @param[in] pInst the stream to append to
|
||||
/// @param[in,out] position position in the binary
|
||||
/// @param[out] pDiagnostic contains diagnostic on failure
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t spvBinaryEncodeU64(const uint64_t value, spv_instruction_t *pInst,
|
||||
const spv_position position,
|
||||
spv_diagnostic *pDiagnostic);
|
||||
|
||||
/// @brief Append a string literal in the binary stream
|
||||
///
|
||||
/// @param[in] str the string to encode
|
||||
/// @param[in] pInst the stream to append to
|
||||
/// @param[in,out] position position in the binary
|
||||
/// @param[out] pDiagnostic contains diagnostic on failure
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t spvBinaryEncodeString(const char *str, spv_instruction_t *pInst,
|
||||
const spv_position position,
|
||||
spv_diagnostic *pDiagnostic);
|
||||
|
||||
/// @brief Determine the type of the desired operand
|
||||
///
|
||||
/// @param[in] word the operand value
|
||||
|
@ -79,3 +79,11 @@ spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic) {
|
||||
|
||||
return SPV_ERROR_INVALID_VALUE;
|
||||
}
|
||||
|
||||
|
||||
DiagnosticStream::~DiagnosticStream() {
|
||||
if (pDiagnostic_) {
|
||||
*pDiagnostic_ = spvDiagnosticCreate(position_, stream_.str().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,9 +29,8 @@
|
||||
|
||||
#include <libspirv/libspirv.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
class diagnostic_helper {
|
||||
public:
|
||||
@ -52,6 +51,38 @@ class diagnostic_helper {
|
||||
spv_diagnostic *pDiagnostic;
|
||||
};
|
||||
|
||||
// On destruction of the diagnostic stream, a diagnostic message will be
|
||||
// written to pDiagnostic containing all of the data written to the stream.
|
||||
// TODO(awoloszyn): This is very similar to diagnostic_helper, and hides
|
||||
// the data more easily. Replace diagnostic_helper elsewhere
|
||||
// eventually.
|
||||
class DiagnosticStream {
|
||||
public:
|
||||
DiagnosticStream(spv_position position, spv_diagnostic *pDiagnostic)
|
||||
: position_(position), pDiagnostic_(pDiagnostic) {}
|
||||
|
||||
DiagnosticStream(DiagnosticStream &&other) : position_(other.position_) {
|
||||
stream_.str(other.stream_.str());
|
||||
other.stream_.str("");
|
||||
pDiagnostic_ = other.pDiagnostic_;
|
||||
other.pDiagnostic_ = nullptr;
|
||||
}
|
||||
|
||||
~DiagnosticStream();
|
||||
|
||||
// Adds the given value to the diagnostic message to be written.
|
||||
template <typename T>
|
||||
DiagnosticStream &operator<<(const T &val) {
|
||||
stream_ << val;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
std::stringstream stream_;
|
||||
spv_position position_;
|
||||
spv_diagnostic *pDiagnostic_;
|
||||
};
|
||||
|
||||
#define DIAGNOSTIC \
|
||||
diagnostic_helper helper(position, pDiagnostic); \
|
||||
helper.stream
|
||||
|
@ -108,7 +108,7 @@ void spvPrependOperandTypes(const spv_operand_type_t* types,
|
||||
/// be prepended onto the pattern. Operands for a less significant bit always
|
||||
/// appear before operands for a more significant bit.
|
||||
///
|
||||
/// If the a set bit is unknown, then we assume it has no operands.
|
||||
/// If a set bit is unknown, then we assume it has no operands.
|
||||
///
|
||||
/// @param[in] operandTable the table of operand type definitions
|
||||
/// @param[in] type the type of operand
|
||||
|
565
source/text.cpp
565
source/text.cpp
@ -32,7 +32,9 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
@ -43,67 +45,18 @@
|
||||
#include <libspirv/libspirv.h>
|
||||
#include "opcode.h"
|
||||
#include "operand.h"
|
||||
#include "text_handler.h"
|
||||
|
||||
using spvutils::BitwiseCast;
|
||||
|
||||
// Structures
|
||||
|
||||
using spv_named_id_table = std::unordered_map<std::string, uint32_t>;
|
||||
|
||||
// Text API
|
||||
|
||||
std::string spvGetWord(const char* str) {
|
||||
size_t index = 0;
|
||||
while (true) {
|
||||
switch (str[index]) {
|
||||
case '\0':
|
||||
case '\t':
|
||||
case '\v':
|
||||
case '\r':
|
||||
case '\n':
|
||||
case ' ':
|
||||
return std::string(str, str + index);
|
||||
default:
|
||||
index++;
|
||||
}
|
||||
}
|
||||
assert(0 && "Unreachable");
|
||||
return ""; // Make certain compilers happy.
|
||||
}
|
||||
|
||||
uint32_t spvNamedIdAssignOrGet(spv_named_id_table* table, const char* textValue,
|
||||
uint32_t* pBound) {
|
||||
if (table->end() == table->find(textValue)) {
|
||||
(*table)[std::string(textValue)] = *pBound;
|
||||
}
|
||||
return (*table)[textValue];
|
||||
}
|
||||
|
||||
spv_result_t spvTextAdvanceLine(const spv_text text, spv_position position) {
|
||||
while (true) {
|
||||
switch (text->str[position->index]) {
|
||||
case '\0':
|
||||
return SPV_END_OF_STREAM;
|
||||
case '\n':
|
||||
position->column = 0;
|
||||
position->line++;
|
||||
position->index++;
|
||||
return SPV_SUCCESS;
|
||||
default:
|
||||
position->column++;
|
||||
position->index++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool spvIsValidIDCharacter(const char value) {
|
||||
return value == '_' || 0 != ::isalnum(value);
|
||||
}
|
||||
|
||||
// Returns true if the given string represents a valid ID name.
|
||||
bool spvIsValidID(const char* textValue) {
|
||||
const char* c = textValue;
|
||||
bool spvIsValidID(const char *textValue) {
|
||||
const char *c = textValue;
|
||||
for (; *c != '\0'; ++c) {
|
||||
if (!spvIsValidIDCharacter(*c)) {
|
||||
return false;
|
||||
@ -113,151 +66,7 @@ bool spvIsValidID(const char* textValue) {
|
||||
return c != textValue;
|
||||
}
|
||||
|
||||
spv_result_t spvTextAdvance(const spv_text text, spv_position position) {
|
||||
// NOTE: Consume white space, otherwise don't advance.
|
||||
switch (text->str[position->index]) {
|
||||
case '\0':
|
||||
return SPV_END_OF_STREAM;
|
||||
case ';':
|
||||
if (spv_result_t error = spvTextAdvanceLine(text, position)) return error;
|
||||
return spvTextAdvance(text, position);
|
||||
case ' ':
|
||||
case '\t':
|
||||
position->column++;
|
||||
position->index++;
|
||||
return spvTextAdvance(text, position);
|
||||
case '\n':
|
||||
position->column = 0;
|
||||
position->line++;
|
||||
position->index++;
|
||||
return spvTextAdvance(text, position);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
|
||||
spv_result_t spvTextWordGet(const spv_text text,
|
||||
const spv_position startPosition, std::string& word,
|
||||
spv_position endPosition) {
|
||||
if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
|
||||
if (!startPosition || !endPosition) return SPV_ERROR_INVALID_POINTER;
|
||||
|
||||
*endPosition = *startPosition;
|
||||
|
||||
bool quoting = false;
|
||||
bool escaping = false;
|
||||
|
||||
// NOTE: Assumes first character is not white space!
|
||||
while (true) {
|
||||
const char ch = text->str[endPosition->index];
|
||||
if (ch == '\\')
|
||||
escaping = !escaping;
|
||||
else {
|
||||
switch (ch) {
|
||||
case '"':
|
||||
if (!escaping) quoting = !quoting;
|
||||
break;
|
||||
case ' ':
|
||||
case ';':
|
||||
case '\t':
|
||||
case '\n':
|
||||
if (escaping || quoting) break;
|
||||
// Fall through.
|
||||
case '\0': { // NOTE: End of word found!
|
||||
word.assign(text->str + startPosition->index,
|
||||
(size_t)(endPosition->index - startPosition->index));
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
escaping = false;
|
||||
}
|
||||
|
||||
endPosition->column++;
|
||||
endPosition->index++;
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
// Returns true if the string at the given position in text starts with "Op".
|
||||
bool spvStartsWithOp(const spv_text text, const spv_position position) {
|
||||
if (text->length < position->index + 3) return false;
|
||||
char ch0 = text->str[position->index];
|
||||
char ch1 = text->str[position->index + 1];
|
||||
char ch2 = text->str[position->index + 2];
|
||||
return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
// Returns true if a new instruction begins at the given position in text.
|
||||
bool spvTextIsStartOfNewInst(const spv_text text, const spv_position position) {
|
||||
spv_position_t nextPosition = *position;
|
||||
if (spvTextAdvance(text, &nextPosition)) return false;
|
||||
if (spvStartsWithOp(text, &nextPosition)) return true;
|
||||
|
||||
std::string word;
|
||||
spv_position_t startPosition = *position;
|
||||
if (spvTextWordGet(text, &startPosition, word, &nextPosition)) return false;
|
||||
if ('%' != word.front()) return false;
|
||||
|
||||
if (spvTextAdvance(text, &nextPosition)) return false;
|
||||
startPosition = nextPosition;
|
||||
if (spvTextWordGet(text, &startPosition, word, &nextPosition)) return false;
|
||||
if ("=" != word) return false;
|
||||
|
||||
if (spvTextAdvance(text, &nextPosition)) return false;
|
||||
startPosition = nextPosition;
|
||||
if (spvStartsWithOp(text, &startPosition)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
spv_result_t spvTextStringGet(const spv_text text,
|
||||
const spv_position startPosition,
|
||||
std::string& string, spv_position endPosition) {
|
||||
if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
|
||||
if (!startPosition || !endPosition) return SPV_ERROR_INVALID_POINTER;
|
||||
|
||||
if ('"' != text->str[startPosition->index]) return SPV_ERROR_INVALID_TEXT;
|
||||
|
||||
*endPosition = *startPosition;
|
||||
|
||||
// NOTE: Assumes first character is not white space
|
||||
while (true) {
|
||||
endPosition->column++;
|
||||
endPosition->index++;
|
||||
|
||||
switch (text->str[endPosition->index]) {
|
||||
case '"': {
|
||||
endPosition->column++;
|
||||
endPosition->index++;
|
||||
|
||||
string.assign(text->str + startPosition->index,
|
||||
(size_t)(endPosition->index - startPosition->index));
|
||||
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
case '\n':
|
||||
case '\0':
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
spv_result_t spvTextToUInt32(const char* textValue, uint32_t* pValue) {
|
||||
char* endPtr = nullptr;
|
||||
*pValue = strtoul(textValue, &endPtr, 0);
|
||||
if (0 == *pValue && textValue == endPtr) {
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
// Text API
|
||||
|
||||
spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
|
||||
bool isSigned = false;
|
||||
@ -343,60 +152,22 @@ spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/// @brief Translate an Opcode operand to binary form
|
||||
///
|
||||
/// @param[in] grammar the grammar to use for compilation
|
||||
/// @param[in, out] context the dynamic compilation info
|
||||
/// @param[in] type of the operand
|
||||
/// @param[in] textValue word of text to be parsed
|
||||
/// @param[in] operandTable operand lookup table
|
||||
/// @param[in,out] namedIdTable table of named ID's
|
||||
/// @param[out] pInst return binary Opcode
|
||||
/// @param[in,out] pExpectedOperands the operand types expected
|
||||
/// @param[in,out] pBound current highest defined ID value
|
||||
/// @param[in] pPosition used in diagnostic on error
|
||||
/// @param[out] pDiagnostic populated on error
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t spvTextEncodeOperand(
|
||||
const spv_operand_type_t type, const char* textValue,
|
||||
const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
|
||||
spv_named_id_table* namedIdTable, spv_instruction_t* pInst,
|
||||
spv_operand_pattern_t* pExpectedOperands, uint32_t* pBound,
|
||||
const spv_position position, spv_diagnostic* pDiagnostic) {
|
||||
spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar &grammar,
|
||||
libspirv::AssemblyContext* context,
|
||||
const spv_operand_type_t type,
|
||||
const char* textValue,
|
||||
spv_instruction_t* pInst,
|
||||
spv_operand_pattern_t* pExpectedOperands) {
|
||||
// NOTE: Handle immediate int in the stream
|
||||
if ('!' == textValue[0]) {
|
||||
const char* begin = textValue + 1;
|
||||
@ -405,11 +176,10 @@ spv_result_t spvTextEncodeOperand(
|
||||
size_t size = strlen(textValue);
|
||||
size_t length = (end - begin);
|
||||
if (size - 1 != length) {
|
||||
DIAGNOSTIC << "Invalid immediate integer '" << textValue << "'.";
|
||||
context->diagnostic() << "Invalid immediate integer '" << textValue << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
position->column += size;
|
||||
position->index += size;
|
||||
context->seekForward(size);
|
||||
pInst->words[pInst->wordCount] = immediateInt;
|
||||
pInst->wordCount += 1;
|
||||
spvSwitchToAlternateParsingAfterImmediate(pExpectedOperands);
|
||||
@ -426,26 +196,24 @@ spv_result_t spvTextEncodeOperand(
|
||||
if ('%' == textValue[0]) {
|
||||
textValue++;
|
||||
} else {
|
||||
DIAGNOSTIC << "Expected id to start with %.";
|
||||
context->diagnostic() << "Expected id to start with %.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
if (!spvIsValidID(textValue)) {
|
||||
DIAGNOSTIC << "Invalid ID " << textValue;
|
||||
context->diagnostic() << "Invalid ID " << textValue;
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
const uint32_t id =
|
||||
spvNamedIdAssignOrGet(namedIdTable, textValue, pBound);
|
||||
const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
|
||||
pInst->words[pInst->wordCount++] = id;
|
||||
*pBound = std::max(*pBound, id + 1);
|
||||
} break;
|
||||
case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
|
||||
// NOTE: Special case for extension instruction lookup
|
||||
if (OpExtInst == pInst->opcode) {
|
||||
spv_ext_inst_desc extInst;
|
||||
if (spvExtInstTableNameLookup(extInstTable, pInst->extInstType,
|
||||
textValue, &extInst)) {
|
||||
DIAGNOSTIC << "Invalid extended instruction name '" << textValue
|
||||
<< "'.";
|
||||
if (grammar.lookupExtInst(pInst->extInstType, textValue,
|
||||
&extInst)) {
|
||||
context->diagnostic() << "Invalid extended instruction name '"
|
||||
<< textValue << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
pInst->words[pInst->wordCount++] = extInst->ext_inst;
|
||||
@ -464,49 +232,48 @@ spv_result_t spvTextEncodeOperand(
|
||||
if (error != SPV_SUCCESS) {
|
||||
if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
|
||||
if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
|
||||
DIAGNOSTIC << "Invalid literal number '" << textValue << "'.";
|
||||
context->diagnostic() << "Invalid literal number '" << textValue << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
switch (literal.type) {
|
||||
// We do not have to print diagnostics here because spvBinaryEncode*
|
||||
// prints diagnostic messages on failure.
|
||||
case SPV_LITERAL_TYPE_INT_32:
|
||||
if (spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32),
|
||||
pInst, position, pDiagnostic))
|
||||
if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32),
|
||||
pInst))
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
break;
|
||||
case SPV_LITERAL_TYPE_INT_64: {
|
||||
if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64),
|
||||
pInst, position, pDiagnostic))
|
||||
if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64),
|
||||
pInst))
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
} break;
|
||||
case SPV_LITERAL_TYPE_UINT_32: {
|
||||
if (spvBinaryEncodeU32(literal.value.u32, pInst, position,
|
||||
pDiagnostic))
|
||||
if (context->binaryEncodeU32(literal.value.u32, pInst))
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
} break;
|
||||
case SPV_LITERAL_TYPE_UINT_64: {
|
||||
if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64),
|
||||
pInst, position, pDiagnostic))
|
||||
if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64),
|
||||
pInst))
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
} break;
|
||||
case SPV_LITERAL_TYPE_FLOAT_32: {
|
||||
if (spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f), pInst,
|
||||
position, pDiagnostic))
|
||||
if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f),
|
||||
pInst))
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
} break;
|
||||
case SPV_LITERAL_TYPE_FLOAT_64: {
|
||||
if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d), pInst,
|
||||
position, pDiagnostic))
|
||||
if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d),
|
||||
pInst))
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
} break;
|
||||
case SPV_LITERAL_TYPE_STRING: {
|
||||
DIAGNOSTIC << "Expected literal number, found literal string '"
|
||||
<< textValue << "'.";
|
||||
context->diagnostic() << "Expected literal number, found literal string '"
|
||||
<< textValue << "'.";
|
||||
return SPV_FAILED_MATCH;
|
||||
} break;
|
||||
default:
|
||||
DIAGNOSTIC << "Invalid literal number '" << textValue << "'";
|
||||
context->diagnostic() << "Invalid literal number '" << textValue << "'";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
} break;
|
||||
@ -517,12 +284,12 @@ spv_result_t spvTextEncodeOperand(
|
||||
if (error != SPV_SUCCESS) {
|
||||
if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
|
||||
if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
|
||||
DIAGNOSTIC << "Invalid literal string '" << textValue << "'.";
|
||||
context->diagnostic() << "Invalid literal string '" << textValue << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
if (literal.type != SPV_LITERAL_TYPE_STRING) {
|
||||
DIAGNOSTIC << "Expected literal string, found literal number '"
|
||||
<< textValue << "'.";
|
||||
context->diagnostic() << "Expected literal string, found literal number '"
|
||||
<< textValue << "'.";
|
||||
return SPV_FAILED_MATCH;
|
||||
}
|
||||
|
||||
@ -531,8 +298,7 @@ spv_result_t spvTextEncodeOperand(
|
||||
pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
|
||||
}
|
||||
|
||||
if (spvBinaryEncodeString(literal.value.str, pInst, position,
|
||||
pDiagnostic))
|
||||
if (context->binaryEncodeString(literal.value.str, pInst))
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
} break;
|
||||
case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
|
||||
@ -542,38 +308,33 @@ spv_result_t spvTextEncodeOperand(
|
||||
case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
|
||||
case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
|
||||
uint32_t value;
|
||||
if (spvTextParseMaskOperand(operandTable, type, textValue, &value)) {
|
||||
DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
|
||||
<< "'.";
|
||||
if (grammar.parseMaskOperand(type, textValue, &value)) {
|
||||
context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
|
||||
<< textValue << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
if (auto error = spvBinaryEncodeU32(value, pInst, position, pDiagnostic))
|
||||
return error;
|
||||
if (auto error = context->binaryEncodeU32(value, pInst)) return error;
|
||||
// Prepare to parse the operands for this logical operand.
|
||||
spvPrependOperandTypesForMask(operandTable, type, value,
|
||||
pExpectedOperands);
|
||||
grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
|
||||
} break;
|
||||
case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
|
||||
auto error = spvTextEncodeOperand(
|
||||
SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue, operandTable,
|
||||
extInstTable, namedIdTable, pInst, pExpectedOperands, pBound,
|
||||
position, pDiagnostic);
|
||||
grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
|
||||
pInst, pExpectedOperands);
|
||||
if (error == SPV_FAILED_MATCH) {
|
||||
// It's not a literal number -- is it a literal string?
|
||||
error = spvTextEncodeOperand(SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
|
||||
textValue, operandTable, extInstTable,
|
||||
namedIdTable, pInst, pExpectedOperands,
|
||||
pBound, position, pDiagnostic);
|
||||
error = spvTextEncodeOperand(grammar, context,
|
||||
SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
|
||||
textValue, pInst, pExpectedOperands);
|
||||
}
|
||||
if (error == SPV_FAILED_MATCH) {
|
||||
// It's not a literal -- is it an ID?
|
||||
error = spvTextEncodeOperand(SPV_OPERAND_TYPE_OPTIONAL_ID, textValue,
|
||||
operandTable, extInstTable, namedIdTable,
|
||||
pInst, pExpectedOperands, pBound, position,
|
||||
pDiagnostic);
|
||||
error =
|
||||
spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
|
||||
textValue, pInst, pExpectedOperands);
|
||||
}
|
||||
if (error) {
|
||||
DIAGNOSTIC << "Invalid word following !<integer>: " << textValue;
|
||||
context->diagnostic() << "Invalid word following !<integer>: " << textValue;
|
||||
return error;
|
||||
}
|
||||
if (pExpectedOperands->empty()) {
|
||||
@ -584,15 +345,15 @@ spv_result_t spvTextEncodeOperand(
|
||||
// NOTE: All non literal operands are handled here using the operand
|
||||
// table.
|
||||
spv_operand_desc entry;
|
||||
if (spvOperandTableNameLookup(operandTable, type, textValue,
|
||||
strlen(textValue), &entry)) {
|
||||
DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
|
||||
<< "'.";
|
||||
if (grammar.lookupOperand(type, textValue, strlen(textValue),
|
||||
&entry)) {
|
||||
context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
|
||||
<< textValue << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
if (spvBinaryEncodeU32(entry->value, pInst, position, pDiagnostic)) {
|
||||
DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
|
||||
<< "'.";
|
||||
if (context->binaryEncodeU32(entry->value, pInst)) {
|
||||
context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
|
||||
<< textValue << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
|
||||
@ -611,15 +372,13 @@ namespace {
|
||||
/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
|
||||
/// leaves position pointing to the error in text.
|
||||
spv_result_t encodeInstructionStartingWithImmediate(
|
||||
const spv_text text, const spv_operand_table operandTable,
|
||||
const spv_ext_inst_table extInstTable, spv_named_id_table* namedIdTable,
|
||||
uint32_t* pBound, spv_instruction_t* pInst, spv_position position,
|
||||
spv_diagnostic* pDiagnostic) {
|
||||
const libspirv::AssemblyGrammar &grammar,
|
||||
libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
|
||||
std::string firstWord;
|
||||
spv_position_t nextPosition = {};
|
||||
auto error = spvTextWordGet(text, position, firstWord, &nextPosition);
|
||||
auto error = context->getWord(firstWord, &nextPosition);
|
||||
if (error) {
|
||||
DIAGNOSTIC << "Internal Error";
|
||||
context->diagnostic() << "Internal Error";
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -628,27 +387,26 @@ spv_result_t encodeInstructionStartingWithImmediate(
|
||||
char* end = nullptr;
|
||||
uint32_t immediateInt = strtoul(begin, &end, 0);
|
||||
if ((begin + firstWord.size() - 1) != end) {
|
||||
DIAGNOSTIC << "Invalid immediate integer '" << firstWord << "'.";
|
||||
context->diagnostic() << "Invalid immediate integer '" << firstWord << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
position->column += firstWord.size();
|
||||
position->index += firstWord.size();
|
||||
context->seekForward(firstWord.size());
|
||||
pInst->words[0] = immediateInt;
|
||||
pInst->wordCount = 1;
|
||||
while (spvTextAdvance(text, position) != SPV_END_OF_STREAM) {
|
||||
while (context->advance() != SPV_END_OF_STREAM) {
|
||||
// A beginning of a new instruction means we're done.
|
||||
if (spvTextIsStartOfNewInst(text, position)) return SPV_SUCCESS;
|
||||
if (context->isStartOfNewInst()) return SPV_SUCCESS;
|
||||
|
||||
// Otherwise, there must be an operand that's either a literal, an ID, or
|
||||
// an immediate.
|
||||
std::string operandValue;
|
||||
if ((error = spvTextWordGet(text, position, operandValue, &nextPosition))) {
|
||||
DIAGNOSTIC << "Internal Error";
|
||||
if ((error = context->getWord(operandValue, &nextPosition))) {
|
||||
context->diagnostic() << "Internal Error";
|
||||
return error;
|
||||
}
|
||||
|
||||
if (operandValue == "=") {
|
||||
DIAGNOSTIC << firstWord << " not allowed before =.";
|
||||
context->diagnostic() << firstWord << " not allowed before =.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
|
||||
@ -656,11 +414,10 @@ spv_result_t encodeInstructionStartingWithImmediate(
|
||||
// expanded.
|
||||
spv_operand_pattern_t dummyExpectedOperands;
|
||||
error = spvTextEncodeOperand(
|
||||
SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(), operandTable,
|
||||
extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound,
|
||||
position, pDiagnostic);
|
||||
grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
|
||||
pInst, &dummyExpectedOperands);
|
||||
if (error) return error;
|
||||
*position = nextPosition;
|
||||
context->setPosition(nextPosition);
|
||||
}
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
@ -669,28 +426,21 @@ spv_result_t encodeInstructionStartingWithImmediate(
|
||||
|
||||
/// @brief Translate single Opcode and operands to binary form
|
||||
///
|
||||
/// @param[in] grammar the grammar to use for compilation
|
||||
/// @param[in, out] context the dynamic compilation info
|
||||
/// @param[in] text stream to translate
|
||||
/// @param[in] format the assembly syntax format of text
|
||||
/// @param[in] opcodeTable Opcode lookup table
|
||||
/// @param[in] operandTable operand lookup table
|
||||
/// @param[in,out] namedIdTable table of named ID's
|
||||
/// @param[in,out] pBound current highest defined ID value
|
||||
/// @param[out] pInst returned binary Opcode
|
||||
/// @param[in,out] pPosition in the text stream
|
||||
/// @param[out] pDiagnostic populated on failure
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t spvTextEncodeOpcode(
|
||||
const spv_text text, spv_assembly_syntax_format_t format,
|
||||
const spv_opcode_table opcodeTable, const spv_operand_table operandTable,
|
||||
const spv_ext_inst_table extInstTable, spv_named_id_table* namedIdTable,
|
||||
uint32_t* pBound, spv_instruction_t* pInst, spv_position position,
|
||||
spv_diagnostic* pDiagnostic) {
|
||||
spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar &grammar,
|
||||
libspirv::AssemblyContext* context,
|
||||
spv_assembly_syntax_format_t format,
|
||||
spv_instruction_t* pInst) {
|
||||
// Check for !<integer> first.
|
||||
if ('!' == text->str[position->index]) {
|
||||
return encodeInstructionStartingWithImmediate(
|
||||
text, operandTable, extInstTable, namedIdTable, pBound, pInst, position,
|
||||
pDiagnostic);
|
||||
if ('!' == context->peek()) {
|
||||
return encodeInstructionStartingWithImmediate(grammar, context, pInst);
|
||||
}
|
||||
|
||||
// An assembly instruction has two possible formats:
|
||||
@ -699,22 +449,22 @@ spv_result_t spvTextEncodeOpcode(
|
||||
|
||||
std::string firstWord;
|
||||
spv_position_t nextPosition = {};
|
||||
spv_result_t error = spvTextWordGet(text, position, firstWord, &nextPosition);
|
||||
spv_result_t error = context->getWord(firstWord, &nextPosition);
|
||||
if (error) {
|
||||
DIAGNOSTIC << "Internal Error";
|
||||
context->diagnostic() << "Internal Error";
|
||||
return error;
|
||||
}
|
||||
|
||||
std::string opcodeName;
|
||||
std::string result_id;
|
||||
spv_position_t result_id_position = {};
|
||||
if (spvStartsWithOp(text, position)) {
|
||||
if (context->startsWithOp()) {
|
||||
opcodeName = firstWord;
|
||||
} else {
|
||||
// If the first word of this instruction is not an opcode, we must be
|
||||
// processing AAF now.
|
||||
if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
|
||||
DIAGNOSTIC
|
||||
context->diagnostic()
|
||||
<< "Expected <opcode> at the beginning of an instruction, found '"
|
||||
<< firstWord << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
@ -722,39 +472,39 @@ spv_result_t spvTextEncodeOpcode(
|
||||
|
||||
result_id = firstWord;
|
||||
if ('%' != result_id.front()) {
|
||||
DIAGNOSTIC << "Expected <opcode> or <result-id> at the beginning "
|
||||
"of an instruction, found '"
|
||||
<< result_id << "'.";
|
||||
context->diagnostic() << "Expected <opcode> or <result-id> at the beginning "
|
||||
"of an instruction, found '"
|
||||
<< result_id << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
result_id_position = *position;
|
||||
result_id_position = context->position();
|
||||
|
||||
// The '=' sign.
|
||||
*position = nextPosition;
|
||||
if (spvTextAdvance(text, position)) {
|
||||
DIAGNOSTIC << "Expected '=', found end of stream.";
|
||||
context->setPosition(nextPosition);
|
||||
if (context->advance()) {
|
||||
context->diagnostic() << "Expected '=', found end of stream.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
std::string equal_sign;
|
||||
error = spvTextWordGet(text, position, equal_sign, &nextPosition);
|
||||
error = context->getWord(equal_sign, &nextPosition);
|
||||
if ("=" != equal_sign) {
|
||||
DIAGNOSTIC << "'=' expected after result id.";
|
||||
context->diagnostic() << "'=' expected after result id.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
|
||||
// The <opcode> after the '=' sign.
|
||||
*position = nextPosition;
|
||||
if (spvTextAdvance(text, position)) {
|
||||
DIAGNOSTIC << "Expected opcode, found end of stream.";
|
||||
context->setPosition(nextPosition);
|
||||
if (context->advance()) {
|
||||
context->diagnostic() << "Expected opcode, found end of stream.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
error = spvTextWordGet(text, position, opcodeName, &nextPosition);
|
||||
error = context->getWord(opcodeName, &nextPosition);
|
||||
if (error) {
|
||||
DIAGNOSTIC << "Internal Error";
|
||||
context->diagnostic() << "Internal Error";
|
||||
return error;
|
||||
}
|
||||
if (!spvStartsWithOp(text, position)) {
|
||||
DIAGNOSTIC << "Invalid Opcode prefix '" << opcodeName << "'.";
|
||||
if (!context->startsWithOp()) {
|
||||
context->diagnostic() << "Invalid Opcode prefix '" << opcodeName << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
}
|
||||
@ -763,23 +513,22 @@ spv_result_t spvTextEncodeOpcode(
|
||||
const char* pInstName = opcodeName.data() + 2;
|
||||
|
||||
spv_opcode_desc opcodeEntry;
|
||||
error = spvOpcodeTableNameLookup(opcodeTable, pInstName, &opcodeEntry);
|
||||
error = grammar.lookupOpcode(pInstName, &opcodeEntry);
|
||||
if (error) {
|
||||
DIAGNOSTIC << "Invalid Opcode name '"
|
||||
<< spvGetWord(text->str + position->index) << "'";
|
||||
context->diagnostic() << "Invalid Opcode name '" << context->getWord() << "'";
|
||||
return error;
|
||||
}
|
||||
if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
|
||||
// If this instruction has <result-id>, check it follows AAF.
|
||||
if (opcodeEntry->hasResult && result_id.empty()) {
|
||||
DIAGNOSTIC << "Expected <result-id> at the beginning of an "
|
||||
"instruction, found '"
|
||||
<< firstWord << "'.";
|
||||
context->diagnostic() << "Expected <result-id> at the beginning of an "
|
||||
"instruction, found '"
|
||||
<< firstWord << "'.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
}
|
||||
pInst->opcode = opcodeEntry->opcode;
|
||||
*position = nextPosition;
|
||||
context->setPosition(nextPosition);
|
||||
pInst->wordCount++;
|
||||
|
||||
// Maintains the ordered list of expected operand types.
|
||||
@ -804,14 +553,16 @@ spv_result_t spvTextEncodeOpcode(
|
||||
// Handle the <result-id> for value generating instructions.
|
||||
// We've already consumed it from the text stream. Here
|
||||
// we inject its words into the instruction.
|
||||
error = spvTextEncodeOperand(SPV_OPERAND_TYPE_RESULT_ID,
|
||||
result_id.c_str(), operandTable,
|
||||
extInstTable, namedIdTable, pInst, nullptr,
|
||||
pBound, &result_id_position, pDiagnostic);
|
||||
spv_position_t temp_pos = context->position();
|
||||
error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
|
||||
result_id.c_str(), pInst, nullptr);
|
||||
result_id_position = context->position();
|
||||
// Because we are injecting we have to reset the position afterwards.
|
||||
context->setPosition(temp_pos);
|
||||
if (error) return error;
|
||||
} else {
|
||||
// Find the next word.
|
||||
error = spvTextAdvance(text, position);
|
||||
error = context->advance();
|
||||
if (error == SPV_END_OF_STREAM) {
|
||||
if (spvOperandIsOptional(type)) {
|
||||
// This would have been the last potential operand for the
|
||||
@ -819,38 +570,38 @@ spv_result_t spvTextEncodeOpcode(
|
||||
// and we didn't find one. We're finished parsing this instruction.
|
||||
break;
|
||||
} else {
|
||||
DIAGNOSTIC << "Expected operand, found end of stream.";
|
||||
context->diagnostic() << "Expected operand, found end of stream.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
}
|
||||
assert(error == SPV_SUCCESS && "Somebody added another way to fail");
|
||||
|
||||
if (spvTextIsStartOfNewInst(text, position)) {
|
||||
if (context->isStartOfNewInst()) {
|
||||
if (spvOperandIsOptional(type)) {
|
||||
break;
|
||||
} else {
|
||||
DIAGNOSTIC << "Expected operand, found next instruction instead.";
|
||||
context->diagnostic()
|
||||
<< "Expected operand, found next instruction instead.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
}
|
||||
|
||||
std::string operandValue;
|
||||
error = spvTextWordGet(text, position, operandValue, &nextPosition);
|
||||
error = context->getWord(operandValue, &nextPosition);
|
||||
if (error) {
|
||||
DIAGNOSTIC << "Internal Error";
|
||||
context->diagnostic() << "Internal Error";
|
||||
return error;
|
||||
}
|
||||
|
||||
error = spvTextEncodeOperand(
|
||||
type, operandValue.c_str(), operandTable, extInstTable, namedIdTable,
|
||||
pInst, &expectedOperands, pBound, position, pDiagnostic);
|
||||
error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
|
||||
pInst, &expectedOperands);
|
||||
|
||||
if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
|
||||
return SPV_SUCCESS;
|
||||
|
||||
if (error) return error;
|
||||
|
||||
*position = nextPosition;
|
||||
context->setPosition(nextPosition);
|
||||
}
|
||||
}
|
||||
|
||||
@ -864,55 +615,44 @@ namespace {
|
||||
// Translates a given assembly language module into binary form.
|
||||
// If a diagnostic is generated, it is not yet marked as being
|
||||
// for a text-based input.
|
||||
spv_result_t spvTextToBinaryInternal(const spv_text text,
|
||||
spv_assembly_syntax_format_t format,
|
||||
const spv_opcode_table opcodeTable,
|
||||
const spv_operand_table operandTable,
|
||||
const spv_ext_inst_table extInstTable,
|
||||
spv_binary* pBinary,
|
||||
spv_diagnostic* pDiagnostic) {
|
||||
spv_position_t position = {};
|
||||
spv_result_t spvTextToBinaryInternal(
|
||||
const libspirv::AssemblyGrammar &grammar, const spv_text text,
|
||||
spv_assembly_syntax_format_t format, spv_binary* pBinary,
|
||||
spv_diagnostic* pDiagnostic) {
|
||||
if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
|
||||
libspirv::AssemblyContext context(text, pDiagnostic);
|
||||
if (!text->str || !text->length) {
|
||||
DIAGNOSTIC << "Text stream is empty.";
|
||||
context.diagnostic() << "Text stream is empty.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
if (!opcodeTable || !operandTable || !extInstTable)
|
||||
if (!grammar.isValid()) {
|
||||
return SPV_ERROR_INVALID_TABLE;
|
||||
}
|
||||
if (!pBinary) return SPV_ERROR_INVALID_POINTER;
|
||||
if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
|
||||
|
||||
// NOTE: Ensure diagnostic is zero initialised
|
||||
*pDiagnostic = {};
|
||||
|
||||
uint32_t bound = 1;
|
||||
|
||||
std::vector<spv_instruction_t> instructions;
|
||||
|
||||
if (spvTextAdvance(text, &position)) {
|
||||
DIAGNOSTIC << "Text stream is empty.";
|
||||
if (context.advance()) {
|
||||
context.diagnostic() << "Text stream is empty.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
|
||||
// This causes namedIdTable to get cleaned up as soon as it is no
|
||||
// longer necessary.
|
||||
{
|
||||
spv_named_id_table namedIdTable;
|
||||
spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
|
||||
while (text->length > position.index) {
|
||||
spv_instruction_t inst = {};
|
||||
inst.extInstType = extInstType;
|
||||
spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
|
||||
while (context.hasText()) {
|
||||
spv_instruction_t inst = {};
|
||||
inst.extInstType = extInstType;
|
||||
|
||||
if (spvTextEncodeOpcode(text, format, opcodeTable, operandTable,
|
||||
extInstTable, &namedIdTable, &bound, &inst,
|
||||
&position, pDiagnostic)) {
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
extInstType = inst.extInstType;
|
||||
|
||||
instructions.push_back(inst);
|
||||
|
||||
if (spvTextAdvance(text, &position)) break;
|
||||
if (spvTextEncodeOpcode(grammar, &context, format, &inst)) {
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
extInstType = inst.extInstType;
|
||||
|
||||
instructions.push_back(inst);
|
||||
|
||||
if (context.advance()) break;
|
||||
}
|
||||
|
||||
size_t totalSize = SPV_INDEX_INSTRUCTION;
|
||||
@ -936,7 +676,7 @@ spv_result_t spvTextToBinaryInternal(const spv_text text,
|
||||
binary->code = data;
|
||||
binary->wordCount = totalSize;
|
||||
|
||||
spv_result_t error = spvBinaryHeaderSet(binary, bound);
|
||||
spv_result_t error = spvBinaryHeaderSet(binary, context.getBound());
|
||||
if (error) {
|
||||
spvBinaryDestroy(binary);
|
||||
return error;
|
||||
@ -967,9 +707,10 @@ spv_result_t spvTextWithFormatToBinary(
|
||||
spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
|
||||
spv_text_t text = {input_text, input_text_size};
|
||||
|
||||
libspirv::AssemblyGrammar grammar(operandTable, opcodeTable,
|
||||
extInstTable);
|
||||
spv_result_t result =
|
||||
spvTextToBinaryInternal(&text, format, opcodeTable, operandTable,
|
||||
extInstTable, pBinary, pDiagnostic);
|
||||
spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic);
|
||||
if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
|
||||
|
||||
return result;
|
||||
|
@ -61,80 +61,6 @@ typedef struct spv_literal_t {
|
||||
|
||||
// Functions
|
||||
|
||||
/// @brief Returns the word at the beginning of the given string.
|
||||
///
|
||||
/// A word ends at the first space, tab, form feed, carriage return, newline,
|
||||
/// or at the end of the string.
|
||||
///
|
||||
/// @param[in] str the source string
|
||||
///
|
||||
/// @return word as a string
|
||||
std::string spvGetWord(const char *str);
|
||||
|
||||
/// @brief Advance text to the start of the next line
|
||||
///
|
||||
/// @param[in] text to be parsed
|
||||
/// @param[in,out] pPosition position text has been advanced to
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t spvTextAdvanceLine(const spv_text text, spv_position_t *pPosition);
|
||||
|
||||
/// @brief Advance text to first non white space character
|
||||
///
|
||||
/// If a null terminator is found during the text advance SPV_END_OF_STREAM is
|
||||
/// returned, SPV_SUCCESS otherwise. No error checking is performed on the
|
||||
/// parameters, its the users responsibility to ensure these are non null.
|
||||
///
|
||||
/// @param[in] text to be parsed
|
||||
/// @param[in,out] pPosition position text has been advanced to
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t spvTextAdvance(const spv_text text, spv_position_t *pPosition);
|
||||
|
||||
/// @brief Fetch the next word from the text stream.
|
||||
///
|
||||
/// A word ends at the next comment or whitespace. However, double-quoted
|
||||
/// strings remain intact, and a backslash always escapes the next character.
|
||||
///
|
||||
/// @param[in] text stream to read from
|
||||
/// @param[in] startPosition current position in text stream
|
||||
/// @param[out] word returned word
|
||||
/// @param[out] endPosition one past the end of the returned word
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t spvTextWordGet(const spv_text text,
|
||||
const spv_position startPosition, std::string &word,
|
||||
spv_position endPosition);
|
||||
|
||||
/// @brief Returns true if the given text can start a new instruction.
|
||||
///
|
||||
/// @param[in] text stream to read from
|
||||
/// @param[in] startPosition current position in text stream
|
||||
///
|
||||
/// @return result code
|
||||
bool spvTextIsStartOfNewInst(const spv_text text,
|
||||
const spv_position startPosition);
|
||||
|
||||
/// @brief Fetch a string, including quotes, from the text stream
|
||||
///
|
||||
/// @param[in] text stream to read from
|
||||
/// @param[in] startPosition current position in text stream
|
||||
/// @param[out] string returned string
|
||||
/// @param[out] endPosition one past the end of the return string
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t spvTextStringGet(const spv_text text,
|
||||
const spv_position startPosition,
|
||||
std::string &string, spv_position endPosition);
|
||||
|
||||
/// @brief Convert the input text to a unsigned 32 bit integer
|
||||
///
|
||||
/// @param[in] textValue input text to parse
|
||||
/// @param[out] pValue the returned integer
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t spvTextToUInt32(const char *textValue, uint32_t *pValue);
|
||||
|
||||
/// @brief Convert the input text to one of the number types.
|
||||
///
|
||||
/// String literals must be surrounded by double-quotes ("), which are
|
||||
@ -146,21 +72,4 @@ spv_result_t spvTextToUInt32(const char *textValue, uint32_t *pValue);
|
||||
/// @return result code
|
||||
spv_result_t spvTextToLiteral(const char *textValue, spv_literal_t *pLiteral);
|
||||
|
||||
/// @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);
|
||||
#endif
|
||||
|
359
source/text_handler.cpp
Normal file
359
source/text_handler.cpp
Normal file
@ -0,0 +1,359 @@
|
||||
// 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 "text_handler.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
#include "binary.h"
|
||||
#include "ext_inst.h"
|
||||
#include "opcode.h"
|
||||
#include "text.h"
|
||||
|
||||
namespace {
|
||||
|
||||
/// @brief Advance text to the start of the next line
|
||||
///
|
||||
/// @param[in] text to be parsed
|
||||
/// @param[in,out] position position text has been advanced to
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t advanceLine(spv_text text, spv_position position) {
|
||||
while (true) {
|
||||
switch (text->str[position->index]) {
|
||||
case '\0':
|
||||
return SPV_END_OF_STREAM;
|
||||
case '\n':
|
||||
position->column = 0;
|
||||
position->line++;
|
||||
position->index++;
|
||||
return SPV_SUCCESS;
|
||||
default:
|
||||
position->column++;
|
||||
position->index++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Advance text to first non white space character
|
||||
/// If a null terminator is found during the text advance SPV_END_OF_STREAM is
|
||||
/// returned, SPV_SUCCESS otherwise. No error checking is performed on the
|
||||
/// parameters, its the users responsibility to ensure these are non null.
|
||||
///
|
||||
/// @param[in] text to be parsed
|
||||
/// @param[in,out] position text has been advanced to
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t advance(spv_text text, spv_position position) {
|
||||
// NOTE: Consume white space, otherwise don't advance.
|
||||
switch (text->str[position->index]) {
|
||||
case '\0':
|
||||
return SPV_END_OF_STREAM;
|
||||
case ';':
|
||||
if (spv_result_t error = advanceLine(text, position)) return error;
|
||||
return advance(text, position);
|
||||
case ' ':
|
||||
case '\t':
|
||||
position->column++;
|
||||
position->index++;
|
||||
return advance(text, position);
|
||||
case '\n':
|
||||
position->column = 0;
|
||||
position->line++;
|
||||
position->index++;
|
||||
return advance(text, position);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
|
||||
/// @brief Fetch the next word from the text stream.
|
||||
///
|
||||
/// A word ends at the next comment or whitespace. However, double-quoted
|
||||
/// strings remain intact, and a backslash always escapes the next character.
|
||||
///
|
||||
/// @param[in] text stream to read from
|
||||
/// @param[in] position current position in text stream
|
||||
/// @param[out] word returned word
|
||||
/// @param[out] endPosition one past the end of the returned word
|
||||
///
|
||||
/// @return result code
|
||||
spv_result_t getWord(spv_text text, spv_position position, std::string &word,
|
||||
spv_position endPosition) {
|
||||
if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
|
||||
if (!position || !endPosition) return SPV_ERROR_INVALID_POINTER;
|
||||
|
||||
*endPosition = *position;
|
||||
|
||||
bool quoting = false;
|
||||
bool escaping = false;
|
||||
|
||||
// NOTE: Assumes first character is not white space!
|
||||
while (true) {
|
||||
const char ch = text->str[endPosition->index];
|
||||
if (ch == '\\')
|
||||
escaping = !escaping;
|
||||
else {
|
||||
switch (ch) {
|
||||
case '"':
|
||||
if (!escaping) quoting = !quoting;
|
||||
break;
|
||||
case ' ':
|
||||
case ';':
|
||||
case '\t':
|
||||
case '\n':
|
||||
if (escaping || quoting) break;
|
||||
// Fall through.
|
||||
case '\0': { // NOTE: End of word found!
|
||||
word.assign(text->str + position->index,
|
||||
(size_t)(endPosition->index - position->index));
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
escaping = false;
|
||||
}
|
||||
|
||||
endPosition->column++;
|
||||
endPosition->index++;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if the characters in the text as position represent
|
||||
// the start of an Opcode.
|
||||
bool startsWithOp(spv_text text, spv_position position) {
|
||||
if (text->length < position->index + 3) return false;
|
||||
char ch0 = text->str[position->index];
|
||||
char ch1 = text->str[position->index + 1];
|
||||
char ch2 = text->str[position->index + 2];
|
||||
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 {
|
||||
|
||||
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::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::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);
|
||||
}
|
||||
|
||||
void AssemblyGrammar::prependOperandTypesForMask(
|
||||
const spv_operand_type_t type, const uint32_t mask,
|
||||
spv_operand_pattern_t *pattern) const {
|
||||
spvPrependOperandTypesForMask(operandTable_, type, mask, pattern);
|
||||
}
|
||||
|
||||
// This represents all of the data that is only valid for the duration of
|
||||
// a single compilation.
|
||||
uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char *textValue) {
|
||||
if (named_ids_.end() == named_ids_.find(textValue)) {
|
||||
named_ids_[std::string(textValue)] = bound_++;
|
||||
}
|
||||
return named_ids_[textValue];
|
||||
}
|
||||
uint32_t AssemblyContext::getBound() const { return bound_; }
|
||||
|
||||
spv_result_t AssemblyContext::advance() {
|
||||
return ::advance(text_, ¤t_position_);
|
||||
}
|
||||
|
||||
spv_result_t AssemblyContext::getWord(std::string &word,
|
||||
spv_position endPosition) {
|
||||
return ::getWord(text_, ¤t_position_, word, endPosition);
|
||||
}
|
||||
|
||||
bool AssemblyContext::startsWithOp() {
|
||||
return ::startsWithOp(text_, ¤t_position_);
|
||||
}
|
||||
|
||||
bool AssemblyContext::isStartOfNewInst() {
|
||||
spv_position_t nextPosition = current_position_;
|
||||
if (::advance(text_, &nextPosition)) return false;
|
||||
if (::startsWithOp(text_, &nextPosition)) return true;
|
||||
|
||||
std::string word;
|
||||
spv_position_t startPosition = current_position_;
|
||||
if (::getWord(text_, &startPosition, word, &nextPosition)) return false;
|
||||
if ('%' != word.front()) return false;
|
||||
|
||||
if (::advance(text_, &nextPosition)) return false;
|
||||
startPosition = nextPosition;
|
||||
if (::getWord(text_, &startPosition, word, &nextPosition)) return false;
|
||||
if ("=" != word) return false;
|
||||
|
||||
if (::advance(text_, &nextPosition)) return false;
|
||||
startPosition = nextPosition;
|
||||
if (::startsWithOp(text_, &startPosition)) return true;
|
||||
return false;
|
||||
}
|
||||
char AssemblyContext::peek() const {
|
||||
return text_->str[current_position_.index];
|
||||
}
|
||||
|
||||
bool AssemblyContext::hasText() const {
|
||||
return text_->length > current_position_.index;
|
||||
}
|
||||
std::string AssemblyContext::getWord() const {
|
||||
size_t index = current_position_.index;
|
||||
while (true) {
|
||||
switch (text_->str[index]) {
|
||||
case '\0':
|
||||
case '\t':
|
||||
case '\v':
|
||||
case '\r':
|
||||
case '\n':
|
||||
case ' ':
|
||||
return std::string(text_->str, text_->str + index);
|
||||
default:
|
||||
index++;
|
||||
}
|
||||
}
|
||||
assert(0 && "Unreachable");
|
||||
return ""; // Make certain compilers happy.
|
||||
}
|
||||
|
||||
void AssemblyContext::seekForward(uint32_t size) {
|
||||
current_position_.index += size;
|
||||
current_position_.column += size;
|
||||
}
|
||||
|
||||
spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value,
|
||||
spv_instruction_t *pInst) {
|
||||
if (pInst->wordCount + 1 > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
|
||||
diagnostic() << "Instruction word count '"
|
||||
<< SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "' exceeded.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
|
||||
pInst->words[pInst->wordCount++] = (uint32_t)value;
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
|
||||
spv_result_t AssemblyContext::binaryEncodeU64(const uint64_t value,
|
||||
spv_instruction_t *pInst) {
|
||||
uint32_t low = (uint32_t)(0x00000000ffffffff & value);
|
||||
uint32_t high = (uint32_t)((0xffffffff00000000 & value) >> 32);
|
||||
spv_result_t err = binaryEncodeU32(low, pInst);
|
||||
if (err != SPV_SUCCESS) {
|
||||
return err;
|
||||
}
|
||||
return binaryEncodeU32(high, pInst);
|
||||
}
|
||||
|
||||
spv_result_t AssemblyContext::binaryEncodeString(
|
||||
const char *value, spv_instruction_t *pInst) {
|
||||
size_t length = strlen(value);
|
||||
size_t wordCount = (length / 4) + 1;
|
||||
if ((sizeof(uint32_t) * pInst->wordCount) + length >
|
||||
sizeof(uint32_t) * SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
|
||||
diagnostic() << "Instruction word count '"
|
||||
<< SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "'exceeded.";
|
||||
return SPV_ERROR_INVALID_TEXT;
|
||||
}
|
||||
|
||||
char *dest = (char *)&pInst->words[pInst->wordCount];
|
||||
strncpy(dest, value, length);
|
||||
pInst->wordCount += (uint16_t)wordCount;
|
||||
|
||||
return SPV_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
210
source/text_handler.h
Normal file
210
source/text_handler.h
Normal file
@ -0,0 +1,210 @@
|
||||
// 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_TEXT_HANDLER_H_
|
||||
#define _LIBSPIRV_UTIL_TEXT_HANDLER_H_
|
||||
|
||||
#include <libspirv/libspirv.h>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "diagnostic.h"
|
||||
#include "operand.h"
|
||||
|
||||
namespace libspirv {
|
||||
// Structures
|
||||
|
||||
|
||||
// This is a lattice for tracking types.
|
||||
enum class IdTypeClass {
|
||||
kBottom, // We have no information yet.
|
||||
kScalarIntegerType,
|
||||
kScalarFloatType,
|
||||
kOtherType
|
||||
};
|
||||
|
||||
|
||||
// Contains ID type information that needs to be tracked across all Ids.
|
||||
// Bitwidth is only valid when type_class is kScalarIntegerType or
|
||||
// kScalarFloatType.
|
||||
struct IdType {
|
||||
uint32_t bitwidth; // Safe to assume that we will not have > 2^32 bits.
|
||||
IdTypeClass type_class;
|
||||
};
|
||||
|
||||
// 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 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;
|
||||
|
||||
// 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;
|
||||
|
||||
// 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:
|
||||
AssemblyContext(spv_text text, spv_diagnostic *diagnostic)
|
||||
: current_position_({}),
|
||||
pDiagnostic_(diagnostic),
|
||||
text_(text),
|
||||
bound_(1) {}
|
||||
|
||||
// Assigns a new integer value to the given text ID, or returns the previously
|
||||
// assigned integer value if the ID has been seen before.
|
||||
uint32_t spvNamedIdAssignOrGet(const char *textValue);
|
||||
|
||||
// Returns the largest largest numeric ID that has been assigned.
|
||||
uint32_t getBound() const;
|
||||
|
||||
// Advances position to point to the next word in the input stream.
|
||||
// Returns SPV_SUCCESS on success.
|
||||
spv_result_t advance();
|
||||
|
||||
// Sets word to the next word in the input text. Fills endPosition with
|
||||
// the next location past the end of the word.
|
||||
spv_result_t getWord(std::string &word, spv_position endPosition);
|
||||
|
||||
// Returns the next word in the input stream. It is invalid to call this
|
||||
// method if position has been set to a location in the stream that does not
|
||||
// exist. If there are no subsequent words, the empty string will be returend.
|
||||
std::string getWord() const;
|
||||
|
||||
// Returns true if the next word in the input is the start of a new Opcode.
|
||||
bool startsWithOp();
|
||||
|
||||
// Returns true if the next word in the input is the start of a new
|
||||
// instruction.
|
||||
bool isStartOfNewInst();
|
||||
|
||||
// Returns a diagnostic object initialized with current position in the input
|
||||
// stream. Any data written to this object will show up in pDiagnsotic on
|
||||
// destruction.
|
||||
DiagnosticStream diagnostic() {
|
||||
return DiagnosticStream(¤t_position_, pDiagnostic_);
|
||||
}
|
||||
|
||||
// Returns then next characted in the input stream.
|
||||
char peek() const;
|
||||
|
||||
// Returns true if there is more text in the input stream.
|
||||
bool hasText() const;
|
||||
|
||||
// Seeks the input stream forward by 'size' characters.
|
||||
void seekForward(uint32_t size);
|
||||
|
||||
// Sets the current position in the input stream to the given position.
|
||||
void setPosition(const spv_position_t &newPosition) {
|
||||
current_position_ = newPosition;
|
||||
}
|
||||
|
||||
// Returns the current position in the input stream.
|
||||
const spv_position_t &position() const { return current_position_; }
|
||||
|
||||
// Appends the given 32-bit value to the given instruction.
|
||||
// Returns SPV_SUCCESS if the value could be correctly inserted in the the
|
||||
// instruction.
|
||||
spv_result_t binaryEncodeU32(const uint32_t value, spv_instruction_t *pInst);
|
||||
// Appends the given 64-bit value to the given instruction.
|
||||
// Returns SPV_SUCCESS if the value could be correctly inserted in the the
|
||||
// instruction.
|
||||
spv_result_t binaryEncodeU64(const uint64_t value, spv_instruction_t *pInst);
|
||||
// Appends the given string to the given instruction.
|
||||
// Returns SPV_SUCCESS if the value could be correctly inserted in the the
|
||||
// instruction.
|
||||
spv_result_t binaryEncodeString(const char *value, spv_instruction_t *pInst);
|
||||
|
||||
private:
|
||||
// Maps ID names to their corresponding numerical ids.
|
||||
using spv_named_id_table = std::unordered_map<std::string, uint32_t>;
|
||||
// Maps type-defining IDs to their IdType.
|
||||
using spv_id_to_type_map = std::unordered_map<uint32_t, IdType>;
|
||||
// Maps Ids to the id of their type.
|
||||
using spv_id_to_type_id = std::unordered_map<uint32_t, uint32_t>;
|
||||
|
||||
spv_named_id_table named_ids_;
|
||||
spv_id_to_type_map types_;
|
||||
spv_id_to_type_id value_types_;
|
||||
spv_position_t current_position_;
|
||||
spv_diagnostic *pDiagnostic_;
|
||||
spv_text text_;
|
||||
uint32_t bound_;
|
||||
};
|
||||
}
|
||||
#endif // _LIBSPIRV_UTIL_TEXT_HANDLER_H_
|
||||
|
@ -28,68 +28,63 @@
|
||||
|
||||
namespace {
|
||||
|
||||
using libspirv::AssemblyContext;
|
||||
|
||||
TEST(TextAdvance, LeadingNewLines) {
|
||||
char textStr[] = "\n\nWord";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
spv_position_t position = {};
|
||||
ASSERT_EQ(SPV_SUCCESS, spvTextAdvance(&text, &position));
|
||||
ASSERT_EQ(0, position.column);
|
||||
ASSERT_EQ(2, position.line);
|
||||
ASSERT_EQ(2, position.index);
|
||||
AutoText input("\n\nWord");
|
||||
AssemblyContext data(input, nullptr);
|
||||
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
||||
ASSERT_EQ(0, data.position().column);
|
||||
ASSERT_EQ(2, data.position().line);
|
||||
ASSERT_EQ(2, data.position().index);
|
||||
}
|
||||
|
||||
TEST(TextAdvance, LeadingSpaces) {
|
||||
char textStr[] = " Word";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
spv_position_t position = {};
|
||||
ASSERT_EQ(SPV_SUCCESS, spvTextAdvance(&text, &position));
|
||||
ASSERT_EQ(4, position.column);
|
||||
ASSERT_EQ(0, position.line);
|
||||
ASSERT_EQ(4, position.index);
|
||||
AutoText input(" Word");
|
||||
AssemblyContext data(input, nullptr);
|
||||
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
||||
ASSERT_EQ(4, data.position().column);
|
||||
ASSERT_EQ(0, data.position().line);
|
||||
ASSERT_EQ(4, data.position().index);
|
||||
}
|
||||
|
||||
TEST(TextAdvance, LeadingTabs) {
|
||||
char textStr[] = "\t\t\tWord";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
spv_position_t position = {};
|
||||
ASSERT_EQ(SPV_SUCCESS, spvTextAdvance(&text, &position));
|
||||
ASSERT_EQ(3, position.column);
|
||||
ASSERT_EQ(0, position.line);
|
||||
ASSERT_EQ(3, position.index);
|
||||
AutoText input("\t\t\tWord");
|
||||
AssemblyContext data(input, nullptr);
|
||||
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
||||
ASSERT_EQ(3, data.position().column);
|
||||
ASSERT_EQ(0, data.position().line);
|
||||
ASSERT_EQ(3, data.position().index);
|
||||
}
|
||||
|
||||
TEST(TextAdvance, LeadingNewLinesSpacesAndTabs) {
|
||||
char textStr[] = "\n\n\t Word";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
spv_position_t position = {};
|
||||
ASSERT_EQ(SPV_SUCCESS, spvTextAdvance(&text, &position));
|
||||
ASSERT_EQ(3, position.column);
|
||||
ASSERT_EQ(2, position.line);
|
||||
ASSERT_EQ(5, position.index);
|
||||
AutoText input("\n\n\t Word");
|
||||
AssemblyContext data(input, nullptr);
|
||||
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
||||
ASSERT_EQ(3, data.position().column);
|
||||
ASSERT_EQ(2, data.position().line);
|
||||
ASSERT_EQ(5, data.position().index);
|
||||
}
|
||||
|
||||
TEST(TextAdvance, LeadingWhitespaceAfterCommentLine) {
|
||||
char textStr[] = "; comment\n \t \tWord";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
spv_position_t position = {};
|
||||
ASSERT_EQ(SPV_SUCCESS, spvTextAdvance(&text, &position));
|
||||
ASSERT_EQ(4, position.column);
|
||||
ASSERT_EQ(1, position.line);
|
||||
ASSERT_EQ(14, position.index);
|
||||
AutoText input("; comment\n \t \tWord");
|
||||
AssemblyContext data(input, nullptr);
|
||||
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
||||
ASSERT_EQ(4, data.position().column);
|
||||
ASSERT_EQ(1, data.position().line);
|
||||
ASSERT_EQ(14, data.position().index);
|
||||
}
|
||||
|
||||
TEST(TextAdvance, EOFAfterCommentLine) {
|
||||
char textStr[] = "; comment";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
spv_position_t position = {};
|
||||
ASSERT_EQ(SPV_END_OF_STREAM, spvTextAdvance(&text, &position));
|
||||
AutoText input("; comment");
|
||||
AssemblyContext data(input, nullptr);
|
||||
ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
|
||||
}
|
||||
|
||||
TEST(TextAdvance, NullTerminator) {
|
||||
char textStr[] = "";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
spv_position_t position = {};
|
||||
ASSERT_EQ(SPV_END_OF_STREAM, spvTextAdvance(&text, &position));
|
||||
AutoText input("");
|
||||
AssemblyContext data(input, nullptr);
|
||||
ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
@ -28,46 +28,59 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
using libspirv::AssemblyContext;
|
||||
|
||||
TEST(TextStartsWithOp, YesAtStart) {
|
||||
spv_position_t startPosition = {};
|
||||
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText("OpFoo"), &startPosition));
|
||||
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText("OpFoo"), &startPosition));
|
||||
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText("OpEnCL"), &startPosition));
|
||||
EXPECT_TRUE(
|
||||
AssemblyContext(AutoText("OpFoo"), nullptr).isStartOfNewInst());
|
||||
EXPECT_TRUE(
|
||||
AssemblyContext(AutoText("OpFoo"), nullptr).isStartOfNewInst());
|
||||
EXPECT_TRUE(
|
||||
AssemblyContext(AutoText("OpEnCL"), nullptr).isStartOfNewInst());
|
||||
}
|
||||
|
||||
TEST(TextStartsWithOp, YesAtMiddle) {
|
||||
spv_position_t startPosition = {};
|
||||
startPosition.index = 2;
|
||||
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText(" OpFoo"), &startPosition));
|
||||
EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText(" OpFoo"), &startPosition));
|
||||
{
|
||||
AssemblyContext dat(AutoText(" OpFoo"), nullptr);
|
||||
dat.seekForward(2);
|
||||
EXPECT_TRUE(dat.isStartOfNewInst());
|
||||
}
|
||||
{
|
||||
AssemblyContext dat(AutoText("xx OpFoo"), nullptr);
|
||||
dat.seekForward(2);
|
||||
EXPECT_TRUE(dat.isStartOfNewInst());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(TextStartsWithOp, NoIfTooFar) {
|
||||
spv_position_t startPosition = {};
|
||||
startPosition.index = 3;
|
||||
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText(" OpFoo"), &startPosition));
|
||||
AssemblyContext dat(AutoText(" OpFoo"), nullptr);
|
||||
dat.seekForward(3);
|
||||
EXPECT_FALSE(dat.isStartOfNewInst());
|
||||
}
|
||||
|
||||
TEST(TextStartsWithOp, NoRegular) {
|
||||
spv_position_t startPosition = {};
|
||||
EXPECT_FALSE(AssemblyContext(AutoText("Fee Fi Fo Fum"), nullptr)
|
||||
.isStartOfNewInst());
|
||||
EXPECT_FALSE(
|
||||
spvTextIsStartOfNewInst(AutoText("Fee Fi Fo Fum"), &startPosition));
|
||||
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("123456"), &startPosition));
|
||||
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("123456"), &startPosition));
|
||||
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("OpenCL"), &startPosition));
|
||||
AssemblyContext(AutoText("123456"), nullptr).isStartOfNewInst());
|
||||
EXPECT_FALSE(
|
||||
AssemblyContext(AutoText("123456"), nullptr).isStartOfNewInst());
|
||||
EXPECT_FALSE(
|
||||
AssemblyContext(AutoText("OpenCL"), nullptr).isStartOfNewInst());
|
||||
}
|
||||
|
||||
TEST(TextStartsWithOp, YesForValueGenerationForm) {
|
||||
spv_position_t startPosition = {};
|
||||
EXPECT_TRUE(
|
||||
spvTextIsStartOfNewInst(AutoText("%foo = OpAdd"), &startPosition));
|
||||
EXPECT_TRUE(
|
||||
spvTextIsStartOfNewInst(AutoText("%foo = OpAdd"), &startPosition));
|
||||
EXPECT_TRUE(AssemblyContext(AutoText("%foo = OpAdd"), nullptr)
|
||||
.isStartOfNewInst());
|
||||
EXPECT_TRUE(AssemblyContext(AutoText("%foo = OpAdd"), nullptr)
|
||||
.isStartOfNewInst());
|
||||
}
|
||||
|
||||
TEST(TextStartsWithOp, NoForNearlyValueGeneration) {
|
||||
spv_position_t startPosition = {};
|
||||
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("%foo = "), &startPosition));
|
||||
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("%foo "), &startPosition));
|
||||
EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("%foo"), &startPosition));
|
||||
EXPECT_FALSE(
|
||||
AssemblyContext(AutoText("%foo = "), nullptr).isStartOfNewInst());
|
||||
EXPECT_FALSE(
|
||||
AssemblyContext(AutoText("%foo "), nullptr).isStartOfNewInst());
|
||||
EXPECT_FALSE(
|
||||
AssemblyContext(AutoText("%foo"), nullptr).isStartOfNewInst());
|
||||
}
|
||||
|
@ -34,21 +34,27 @@
|
||||
namespace {
|
||||
|
||||
using spvtest::TextToBinaryTest;
|
||||
using libspirv::AssemblyContext;
|
||||
using libspirv::AssemblyGrammar;
|
||||
|
||||
TEST(GetWord, Simple) {
|
||||
EXPECT_EQ("", spvGetWord(""));
|
||||
EXPECT_EQ("", spvGetWord("\0a"));
|
||||
EXPECT_EQ("", spvGetWord(" a"));
|
||||
EXPECT_EQ("", spvGetWord("\ta"));
|
||||
EXPECT_EQ("", spvGetWord("\va"));
|
||||
EXPECT_EQ("", spvGetWord("\ra"));
|
||||
EXPECT_EQ("", spvGetWord("\na"));
|
||||
EXPECT_EQ("abc", spvGetWord("abc"));
|
||||
EXPECT_EQ("abc", spvGetWord("abc "));
|
||||
EXPECT_EQ("abc", spvGetWord("abc\t"));
|
||||
EXPECT_EQ("abc", spvGetWord("abc\r"));
|
||||
EXPECT_EQ("abc", spvGetWord("abc\v"));
|
||||
EXPECT_EQ("abc", spvGetWord("abc\n"));
|
||||
EXPECT_EQ("", AssemblyContext(AutoText(""), nullptr).getWord());
|
||||
EXPECT_EQ("", AssemblyContext(AutoText("\0a"), nullptr).getWord());
|
||||
EXPECT_EQ("", AssemblyContext(AutoText(" a"), nullptr).getWord());
|
||||
EXPECT_EQ("", AssemblyContext(AutoText("\ta"), nullptr).getWord());
|
||||
EXPECT_EQ("", AssemblyContext(AutoText("\va"), nullptr).getWord());
|
||||
EXPECT_EQ("", AssemblyContext(AutoText("\ra"), nullptr).getWord());
|
||||
EXPECT_EQ("", AssemblyContext(AutoText("\na"), nullptr).getWord());
|
||||
EXPECT_EQ("abc", AssemblyContext(AutoText("abc"), nullptr).getWord());
|
||||
EXPECT_EQ("abc", AssemblyContext(AutoText("abc "), nullptr).getWord());
|
||||
EXPECT_EQ("abc",
|
||||
AssemblyContext(AutoText("abc\t"), nullptr).getWord());
|
||||
EXPECT_EQ("abc",
|
||||
AssemblyContext(AutoText("abc\r"), nullptr).getWord());
|
||||
EXPECT_EQ("abc",
|
||||
AssemblyContext(AutoText("abc\v"), nullptr).getWord());
|
||||
EXPECT_EQ("abc",
|
||||
AssemblyContext(AutoText("abc\n"), nullptr).getWord());
|
||||
}
|
||||
|
||||
// An mask parsing test case.
|
||||
@ -65,9 +71,9 @@ TEST_P(GoodMaskParseTest, GoodMaskExpressions) {
|
||||
ASSERT_EQ(SPV_SUCCESS, spvOperandTableGet(&operandTable));
|
||||
|
||||
uint32_t value;
|
||||
EXPECT_EQ(SPV_SUCCESS,
|
||||
spvTextParseMaskOperand(operandTable, GetParam().which_enum,
|
||||
GetParam().expression, &value));
|
||||
EXPECT_EQ(SPV_SUCCESS, AssemblyGrammar(operandTable, nullptr, nullptr)
|
||||
.parseMaskOperand(GetParam().which_enum,
|
||||
GetParam().expression, &value));
|
||||
EXPECT_EQ(GetParam().expected_value, value);
|
||||
}
|
||||
|
||||
@ -109,9 +115,10 @@ TEST_P(BadFPFastMathMaskParseTest, BadMaskExpressions) {
|
||||
ASSERT_EQ(SPV_SUCCESS, spvOperandTableGet(&operandTable));
|
||||
|
||||
uint32_t value;
|
||||
EXPECT_NE(SPV_SUCCESS, spvTextParseMaskOperand(
|
||||
operandTable, SPV_OPERAND_TYPE_FP_FAST_MATH_MODE,
|
||||
GetParam(), &value));
|
||||
EXPECT_NE(SPV_SUCCESS,
|
||||
AssemblyGrammar(operandTable, nullptr, nullptr)
|
||||
.parseMaskOperand(SPV_OPERAND_TYPE_FP_FAST_MATH_MODE,
|
||||
GetParam(), &value));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(ParseMask, BadFPFastMathMaskParseTest,
|
||||
|
@ -28,19 +28,18 @@
|
||||
|
||||
namespace {
|
||||
|
||||
using libspirv::AssemblyContext;
|
||||
|
||||
#define TAB "\t"
|
||||
#define NEWLINE "\n"
|
||||
#define BACKSLASH R"(\)"
|
||||
#define QUOTE R"(")"
|
||||
|
||||
TEST(TextWordGet, NullTerminator) {
|
||||
char textStr[] = "Word";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
spv_position_t startPosition = {};
|
||||
std::string word;
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(&text, &startPosition, word, &endPosition));
|
||||
ASSERT_EQ(SPV_SUCCESS, AssemblyContext(AutoText("Word"), nullptr)
|
||||
.getWord(word, &endPosition));
|
||||
ASSERT_EQ(4, endPosition.column);
|
||||
ASSERT_EQ(0, endPosition.line);
|
||||
ASSERT_EQ(4, endPosition.index);
|
||||
@ -48,13 +47,10 @@ TEST(TextWordGet, NullTerminator) {
|
||||
}
|
||||
|
||||
TEST(TextWordGet, TabTerminator) {
|
||||
char textStr[] = "Word\t";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
spv_position_t startPosition = {};
|
||||
std::string word;
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(&text, &startPosition, word, &endPosition));
|
||||
ASSERT_EQ(SPV_SUCCESS, AssemblyContext(AutoText("Word\t"), nullptr)
|
||||
.getWord(word, &endPosition));
|
||||
ASSERT_EQ(4, endPosition.column);
|
||||
ASSERT_EQ(0, endPosition.line);
|
||||
ASSERT_EQ(4, endPosition.index);
|
||||
@ -62,13 +58,10 @@ TEST(TextWordGet, TabTerminator) {
|
||||
}
|
||||
|
||||
TEST(TextWordGet, SpaceTerminator) {
|
||||
char textStr[] = "Word ";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
spv_position_t startPosition = {};
|
||||
std::string word;
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(&text, &startPosition, word, &endPosition));
|
||||
ASSERT_EQ(SPV_SUCCESS, AssemblyContext(AutoText("Word "), nullptr)
|
||||
.getWord(word, &endPosition));
|
||||
ASSERT_EQ(4, endPosition.column);
|
||||
ASSERT_EQ(0, endPosition.line);
|
||||
ASSERT_EQ(4, endPosition.index);
|
||||
@ -76,13 +69,10 @@ TEST(TextWordGet, SpaceTerminator) {
|
||||
}
|
||||
|
||||
TEST(TextWordGet, SemicolonTerminator) {
|
||||
char textStr[] = "Wo;rd ";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
spv_position_t startPosition = {};
|
||||
std::string word;
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(&text, &startPosition, word, &endPosition));
|
||||
ASSERT_EQ(SPV_SUCCESS, AssemblyContext(AutoText("Wo;rd"), nullptr)
|
||||
.getWord(word, &endPosition));
|
||||
ASSERT_EQ(2, endPosition.column);
|
||||
ASSERT_EQ(0, endPosition.line);
|
||||
ASSERT_EQ(2, endPosition.index);
|
||||
@ -90,29 +80,28 @@ TEST(TextWordGet, SemicolonTerminator) {
|
||||
}
|
||||
|
||||
TEST(TextWordGet, MultipleWords) {
|
||||
char textStr[] = "Words in a sentence";
|
||||
spv_text_t text = {textStr, strlen(textStr)};
|
||||
const char *words[] = {"Words", "in", "a", "sentence"};
|
||||
AutoText input("Words in a sentence");
|
||||
AssemblyContext data(input, nullptr);
|
||||
|
||||
spv_position_t startPosition = {};
|
||||
spv_position_t endPosition = {};
|
||||
const char *words[] = {"Words", "in", "a", "sentence"};
|
||||
|
||||
std::string word;
|
||||
for (uint32_t wordIndex = 0; wordIndex < 4; ++wordIndex) {
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(&text, &startPosition, word, &endPosition));
|
||||
data.getWord(word, &endPosition));
|
||||
ASSERT_EQ(strlen(words[wordIndex]),
|
||||
endPosition.column - startPosition.column);
|
||||
endPosition.column - data.position().column);
|
||||
ASSERT_EQ(0, endPosition.line);
|
||||
ASSERT_EQ(strlen(words[wordIndex]),
|
||||
endPosition.index - startPosition.index);
|
||||
endPosition.index - data.position().index);
|
||||
ASSERT_STREQ(words[wordIndex], word.c_str());
|
||||
|
||||
startPosition = endPosition;
|
||||
data.setPosition(endPosition);
|
||||
if (3 != wordIndex) {
|
||||
ASSERT_EQ(SPV_SUCCESS, spvTextAdvance(&text, &startPosition));
|
||||
ASSERT_EQ(SPV_SUCCESS, data.advance());
|
||||
} else {
|
||||
ASSERT_EQ(SPV_END_OF_STREAM, spvTextAdvance(&text, &startPosition));
|
||||
ASSERT_EQ(SPV_END_OF_STREAM, data.advance());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -120,24 +109,23 @@ TEST(TextWordGet, MultipleWords) {
|
||||
TEST(TextWordGet, QuotesAreKept) {
|
||||
AutoText input(R"("quotes" "around words")");
|
||||
const char *expected[] = {R"("quotes")", R"("around words")"};
|
||||
AssemblyContext data(input, nullptr);
|
||||
|
||||
std::string word;
|
||||
spv_position_t startPosition = {};
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(input, &startPosition, word, &endPosition));
|
||||
data.getWord(word, &endPosition));
|
||||
EXPECT_EQ(8, endPosition.column);
|
||||
EXPECT_EQ(0, endPosition.line);
|
||||
EXPECT_EQ(8, endPosition.index);
|
||||
EXPECT_STREQ(expected[0], word.c_str());
|
||||
|
||||
// Move to the next word.
|
||||
startPosition = endPosition;
|
||||
startPosition.index++;
|
||||
startPosition.column++;
|
||||
data.setPosition(endPosition);
|
||||
data.seekForward(1);
|
||||
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(input, &startPosition, word, &endPosition));
|
||||
data.getWord(word, &endPosition));
|
||||
EXPECT_EQ(23, endPosition.column);
|
||||
EXPECT_EQ(0, endPosition.line);
|
||||
EXPECT_EQ(23, endPosition.index);
|
||||
@ -147,24 +135,23 @@ TEST(TextWordGet, QuotesAreKept) {
|
||||
TEST(TextWordGet, QuotesBetweenWordsActLikeGlue) {
|
||||
AutoText input(R"(quotes" "between words)");
|
||||
const char *expected[] = {R"(quotes" "between)", "words"};
|
||||
AssemblyContext data(input, nullptr);
|
||||
|
||||
std::string word;
|
||||
spv_position_t startPosition = {};
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(input, &startPosition, word, &endPosition));
|
||||
data.getWord(word, &endPosition));
|
||||
EXPECT_EQ(16, endPosition.column);
|
||||
EXPECT_EQ(0, endPosition.line);
|
||||
EXPECT_EQ(16, endPosition.index);
|
||||
EXPECT_STREQ(expected[0], word.c_str());
|
||||
|
||||
// Move to the next word.
|
||||
startPosition = endPosition;
|
||||
startPosition.index++;
|
||||
startPosition.column++;
|
||||
data.setPosition(endPosition);
|
||||
data.seekForward(1);
|
||||
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(input, &startPosition, word, &endPosition));
|
||||
data.getWord(word, &endPosition));
|
||||
EXPECT_EQ(22, endPosition.column);
|
||||
EXPECT_EQ(0, endPosition.line);
|
||||
EXPECT_EQ(22, endPosition.index);
|
||||
@ -172,13 +159,13 @@ TEST(TextWordGet, QuotesBetweenWordsActLikeGlue) {
|
||||
}
|
||||
|
||||
TEST(TextWordGet, QuotingWhitespace) {
|
||||
// Whitespace surrounded by quotes acts like glue.
|
||||
AutoText input(QUOTE "white " NEWLINE TAB " space" QUOTE);
|
||||
// Whitespace surrounded by quotes acts like glue.
|
||||
std::string word;
|
||||
spv_position_t startPosition = {};
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(input, &startPosition, word, &endPosition));
|
||||
AssemblyContext(input, nullptr).getWord(word, &endPosition));
|
||||
EXPECT_EQ(input.str.length(), endPosition.column);
|
||||
EXPECT_EQ(0, endPosition.line);
|
||||
EXPECT_EQ(input.str.length(), endPosition.index);
|
||||
@ -188,10 +175,9 @@ TEST(TextWordGet, QuotingWhitespace) {
|
||||
TEST(TextWordGet, QuoteAlone) {
|
||||
AutoText input(QUOTE);
|
||||
std::string word;
|
||||
spv_position_t startPosition = {};
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(input, &startPosition, word, &endPosition));
|
||||
AssemblyContext(input, nullptr).getWord(word, &endPosition));
|
||||
ASSERT_EQ(1, endPosition.column);
|
||||
ASSERT_EQ(0, endPosition.line);
|
||||
ASSERT_EQ(1, endPosition.index);
|
||||
@ -201,10 +187,9 @@ TEST(TextWordGet, QuoteAlone) {
|
||||
TEST(TextWordGet, EscapeAlone) {
|
||||
AutoText input(BACKSLASH);
|
||||
std::string word;
|
||||
spv_position_t startPosition = {};
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(input, &startPosition, word, &endPosition));
|
||||
AssemblyContext(input, nullptr).getWord(word, &endPosition));
|
||||
ASSERT_EQ(1, endPosition.column);
|
||||
ASSERT_EQ(0, endPosition.line);
|
||||
ASSERT_EQ(1, endPosition.index);
|
||||
@ -214,10 +199,9 @@ TEST(TextWordGet, EscapeAlone) {
|
||||
TEST(TextWordGet, EscapeAtEndOfInput) {
|
||||
AutoText input("word" BACKSLASH);
|
||||
std::string word;
|
||||
spv_position_t startPosition = {};
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(input, &startPosition, word, &endPosition));
|
||||
AssemblyContext(input, nullptr).getWord(word, &endPosition));
|
||||
ASSERT_EQ(5, endPosition.column);
|
||||
ASSERT_EQ(0, endPosition.line);
|
||||
ASSERT_EQ(5, endPosition.index);
|
||||
@ -227,10 +211,9 @@ TEST(TextWordGet, EscapeAtEndOfInput) {
|
||||
TEST(TextWordGet, Escaping) {
|
||||
AutoText input("w" BACKSLASH QUOTE "o" BACKSLASH NEWLINE "r" BACKSLASH ";d");
|
||||
std::string word;
|
||||
spv_position_t startPosition = {};
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(input, &startPosition, word, &endPosition));
|
||||
AssemblyContext(input, nullptr).getWord(word, &endPosition));
|
||||
ASSERT_EQ(10, endPosition.column);
|
||||
ASSERT_EQ(0, endPosition.line);
|
||||
ASSERT_EQ(10, endPosition.index);
|
||||
@ -240,10 +223,9 @@ TEST(TextWordGet, Escaping) {
|
||||
TEST(TextWordGet, EscapingEscape) {
|
||||
AutoText input("word" BACKSLASH BACKSLASH " abc");
|
||||
std::string word;
|
||||
spv_position_t startPosition = {};
|
||||
spv_position_t endPosition = {};
|
||||
ASSERT_EQ(SPV_SUCCESS,
|
||||
spvTextWordGet(input, &startPosition, word, &endPosition));
|
||||
AssemblyContext(input, nullptr).getWord(word, &endPosition));
|
||||
ASSERT_EQ(6, endPosition.column);
|
||||
ASSERT_EQ(0, endPosition.line);
|
||||
ASSERT_EQ(6, endPosition.index);
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include "../source/diagnostic.h"
|
||||
#include "../source/opcode.h"
|
||||
#include "../source/text.h"
|
||||
#include "../source/text_handler.h"
|
||||
#include "../source/validate.h"
|
||||
|
||||
#include <iomanip>
|
||||
|
Loading…
Reference in New Issue
Block a user