SPV_OPERAND_TYPE_NONE = 0,
SPV_OPERAND_TYPE_ID,
SPV_OPERAND_TYPE_RESULT_ID,
- SPV_OPERAND_TYPE_LITERAL, // Either a literal number or literal string
+ SPV_OPERAND_TYPE_LITERAL, // Either a literal number or literal string
SPV_OPERAND_TYPE_LITERAL_NUMBER,
SPV_OPERAND_TYPE_LITERAL_STRING,
SPV_OPERAND_TYPE_SOURCE_LANGUAGE,
typedef struct spv_opcode_desc_t {
const char *name;
const Op opcode;
- const uint32_t flags; // Bitfield of spv_opcode_flags_t
- const uint32_t capabilities; // spv_language_capabilities_t
+ const uint32_t flags; // Bitfield of spv_opcode_flags_t
+ const uint32_t capabilities; // spv_language_capabilities_t
// operandTypes[0..numTypes-1] describe logical operands for the instruction.
// The operand types include result id and result-type id, followed by
// the types of arguments.
uint16_t numTypes;
spv_operand_type_t operandTypes[16]; // TODO: Smaller/larger?
- const bool hasResult; // Does the instruction have a result ID operand?
- const bool hasType; // Does the instruction have a type ID operand?
+ const bool hasResult; // Does the instruction have a result ID operand?
+ const bool hasType; // Does the instruction have a type ID operand?
// The operand class for each logical argument. This does *not* include
// the result Id or type ID. The list is terminated by SPV_OPERAND_TYPE_NONE.
const OperandClass operandClass[16];
/// @param[out] pDiagnostic contains diagnostic on failure
///
/// @return result code
-spv_result_t spvBinaryToText(uint32_t* binary,
- const uint64_t wordCount,
+spv_result_t spvBinaryToText(uint32_t *binary, const uint64_t wordCount,
const uint32_t options,
const spv_opcode_table opcodeTable,
const spv_operand_table operandTable,
if (opcodeEntry->numTypes > wordCount &&
!spvOperandIsOptional(opcodeEntry->operandTypes[wordCount])) {
uint16_t numRequired;
- for (numRequired = 0; numRequired < opcodeEntry->numTypes &&
- !spvOperandIsOptional(opcodeEntry->operandTypes[numRequired]) ; numRequired++ )
+ for (numRequired = 0;
+ numRequired < opcodeEntry->numTypes &&
+ !spvOperandIsOptional(opcodeEntry->operandTypes[numRequired]);
+ numRequired++)
;
DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
- << " word count '" << wordCount
- << "', expected at least '" << numRequired
- << "'.";
+ << " word count '" << wordCount << "', expected at least '"
+ << numRequired << "'.";
return SPV_ERROR_INVALID_BINARY;
}
return SPV_SUCCESS;
}
-spv_result_t spvBinaryToText(uint32_t* code,
- const uint64_t wordCount,
+spv_result_t spvBinaryToText(uint32_t *code, const uint64_t wordCount,
const uint32_t options,
const spv_opcode_table opcodeTable,
const spv_operand_table operandTable,
spv_binary_t binary = {code, wordCount};
spv_position_t position = {};
- spvCheck(!binary.code || !binary.wordCount,
- DIAGNOSTIC << "Binary stream is empty.";
+ spvCheck(!binary.code || !binary.wordCount, DIAGNOSTIC
+ << "Binary stream is empty.";
return SPV_ERROR_INVALID_BINARY);
spvCheck(!opcodeTable || !operandTable || !extInstTable,
return SPV_ERROR_INVALID_TABLE);
case '\t':
case '\n':
if (escaping || quoting) break;
- // Fall through.
+ // Fall through.
case '\0': { // NOTE: End of word found!
word.assign(text->str + startPosition->index,
(size_t)(endPosition->index - startPosition->index));
return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
}
-} // anonymous namespace
+} // 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) {
+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;
break;
default:
isString = true;
- index = len; // break out of the loop too.
+ index = len; // break out of the loop too.
break;
}
}
pLiteral->type = spv_literal_type_t(99);
- if (isString || numPeriods > 1 || (isSigned && len==1)) {
+ if (isString || numPeriods > 1 || (isSigned && len == 1)) {
// TODO(dneto): Allow escaping.
if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
return SPV_FAILED_MATCH;
pLiteral->type = SPV_LITERAL_TYPE_STRING;
// Need room for the null-terminator.
if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY;
- strncpy(pLiteral->value.str, textValue+1, len-2);
- pLiteral->value.str[len-2] = 0;
+ strncpy(pLiteral->value.str, textValue + 1, len - 2);
+ pLiteral->value.str[len - 2] = 0;
} else if (numPeriods == 1) {
double d = std::strtod(textValue, nullptr);
float f = (float)d;
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,
+ spv_operand_pattern_t *pExpectedOperands, uint32_t *pBound,
const spv_position position, spv_diagnostic *pDiagnostic) {
// NOTE: Handle immediate int in the stream
if ('!' == textValue[0]) {
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
size_t len = strlen(textValue);
spvCheck('"' != textValue[0] && '"' != textValue[len - 1],
- if (spvOperandIsOptional(type))
- return SPV_FAILED_MATCH;
+ if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
DIAGNOSTIC << "Invalid literal string '" << textValue
<< "', expected quotes.";
return SPV_ERROR_INVALID_TEXT;);
expectedOperands.pop_front();
// Expand optional tuples lazily.
- if (spvExpandOperandSequenceOnce(type, &expectedOperands))
- continue;
+ if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
// 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);
+ error = spvTextEncodeOperand(SPV_OPERAND_TYPE_RESULT_ID,
+ result_id.c_str(), operandTable,
+ extInstTable, namedIdTable, pInst, nullptr,
+ pBound, &result_id_position, pDiagnostic);
spvCheck(error, return error);
} else {
// Find the next word.
error = spvTextAdvance(text, position);
if (error == SPV_END_OF_STREAM) {
if (spvOperandIsOptional(type)) {
- // This would have been the last potential operand for the instruction,
+ // This would have been the last potential operand for the
+ // instruction,
// and we didn't find one. We're finished parsing this instruction.
break;
} else {
spvCheck(error, DIAGNOSTIC << "Internal Error"; return error);
error = spvTextEncodeOperand(
- type, operandValue.c_str(),
- operandTable, extInstTable, namedIdTable, pInst, &expectedOperands,
- pBound, position, pDiagnostic);
+ type, operandValue.c_str(), operandTable, extInstTable, namedIdTable,
+ pInst, &expectedOperands, pBound, position, pDiagnostic);
if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
return SPV_SUCCESS;
return SPV_SUCCESS;
}
-} // anonymous namespace
+} // anonymous namespace
spv_result_t spvTextToBinary(const char *input_text,
const uint64_t input_text_size,
namespace {
-using test_fixture::TextToBinaryTest;
+using test_fixture::TextToBinaryTest;
TEST_F(TextToBinaryTest, Whitespace) {
SetText(R"(
namespace {
-TEST(Macros, BitShiftInnerParens) {
- ASSERT_EQ(65536, SPV_BIT(2 << 3));
-}
+TEST(Macros, BitShiftInnerParens) { ASSERT_EQ(65536, SPV_BIT(2 << 3)); }
-TEST(Macros, BitShiftOuterParens) {
- ASSERT_EQ(15, SPV_BIT(4)-1);
-}
+TEST(Macros, BitShiftOuterParens) { ASSERT_EQ(15, SPV_BIT(4) - 1); }
} // anonymous namespace
VariableOperandExpansionTest,
::testing::ValuesIn(allOperandTypes()));
-
} // anonymous namespace
TEST(TextStartsWithOp, NoRegular) {
spv_position_t startPosition = {};
- EXPECT_FALSE(spvTextIsStartOfNewInst(AutoText("Fee Fi Fo Fum"), &startPosition));
+ 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));
TEST(TextStartsWithOp, YesForValueGenerationForm) {
spv_position_t startPosition = {};
- EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText("\%foo = OpAdd"), &startPosition));
- EXPECT_TRUE(spvTextIsStartOfNewInst(AutoText("\%foo = OpAdd"), &startPosition));
+ EXPECT_TRUE(
+ spvTextIsStartOfNewInst(AutoText("\%foo = OpAdd"), &startPosition));
+ EXPECT_TRUE(
+ spvTextIsStartOfNewInst(AutoText("\%foo = OpAdd"), &startPosition));
}
TEST(TextStartsWithOp, NoForNearlyValueGeneration) {
spv_binary binary;
spv_diagnostic diagnostic = nullptr;
- spv_result_t error = spvTextToBinary(textStr, strlen(textStr), opcodeTable, operandTable,
- extInstTable, &binary, &diagnostic);
+ spv_result_t error =
+ spvTextToBinary(textStr, strlen(textStr), opcodeTable, operandTable,
+ extInstTable, &binary, &diagnostic);
if (error) {
spvDiagnosticPrint(diagnostic);
}
TEST_F(TextToBinaryTest, InvalidPointer) {
- SetText("OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
+ SetText(
+ "OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
spvTextToBinary(text.str, text.length, opcodeTable, operandTable,
extInstTable, nullptr, &diagnostic));
}
TEST_F(TextToBinaryTest, InvalidDiagnostic) {
- SetText("OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
+ SetText(
+ "OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
spv_binary binary;
ASSERT_EQ(SPV_ERROR_INVALID_DIAGNOSTIC,
spvTextToBinary(text.str, text.length, opcodeTable, operandTable,
)");
// Minimal check: The OpSwitch opcode word is correct.
- EXPECT_EQ(int(spv::OpSwitch) || (7<<16) , code[14]);
+ EXPECT_EQ(int(spv::OpSwitch) || (7 << 16), code[14]);
}
TEST_F(TextToBinaryTest, GoodSwitchZeroCasesOneDefault) {
)");
// Minimal check: The OpSwitch opcode word is correct.
- EXPECT_EQ(int(spv::OpSwitch) || (3<<16) , code[10]);
+ EXPECT_EQ(int(spv::OpSwitch) || (3 << 16), code[10]);
}
TEST_F(TextToBinaryTest, BadSwitchTruncatedCase) {
extInstTable, &binary, &diagnostic));
EXPECT_EQ(6, diagnostic->position.line + 1);
EXPECT_EQ(1, diagnostic->position.column + 1);
- EXPECT_STREQ("Expected operand, found next instruction instead.", diagnostic->error);
+ EXPECT_STREQ("Expected operand, found next instruction instead.",
+ diagnostic->error);
}
using TextToBinaryFloatValueTest = test_fixture::TextToBinaryTestBase<
spv_binary binary;
};
-#define CHECK(str, expected) \
- spv_diagnostic diagnostic; \
- spv_result_t error = spvTextToBinary(str, strlen(str), opcodeTable, operandTable, \
- extInstTable, &binary, &diagnostic); \
- if (error) { \
- spvDiagnosticPrint(diagnostic); \
- spvDiagnosticDestroy(diagnostic); \
- ASSERT_EQ(SPV_SUCCESS, error); \
- } \
- spv_result_t result = \
- spvValidate(binary, opcodeTable, operandTable, extInstTable, \
- SPV_VALIDATE_ID_BIT, &diagnostic); \
- if (SPV_SUCCESS != result) { \
- spvDiagnosticPrint(diagnostic); \
- spvDiagnosticDestroy(diagnostic); \
- } \
+#define CHECK(str, expected) \
+ spv_diagnostic diagnostic; \
+ spv_result_t error = \
+ spvTextToBinary(str, strlen(str), opcodeTable, operandTable, \
+ extInstTable, &binary, &diagnostic); \
+ if (error) { \
+ spvDiagnosticPrint(diagnostic); \
+ spvDiagnosticDestroy(diagnostic); \
+ ASSERT_EQ(SPV_SUCCESS, error); \
+ } \
+ spv_result_t result = \
+ spvValidate(binary, opcodeTable, operandTable, extInstTable, \
+ SPV_VALIDATE_ID_BIT, &diagnostic); \
+ if (SPV_SUCCESS != result) { \
+ spvDiagnosticPrint(diagnostic); \
+ spvDiagnosticDestroy(diagnostic); \
+ } \
ASSERT_EQ(expected, result);
// TODO: OpUndef
}
TEST_F(ValidateID, OpLineGood) {
- // TODO(dneto): OpLine changed after Rev31. It no longer has a first argument.
+ // TODO(dneto): OpLine changed after Rev31. It no longer has a first argument.
// The following is the Rev31 form.
const char *spirv = R"(
%1 = OpString "/path/to/source.file"
CHECK(spirv, SPV_SUCCESS);
}
TEST_F(ValidateID, OpLineFileBad) {
- // TODO(dneto): OpLine changed after Rev31. It no longer has a first argument.
+ // TODO(dneto): OpLine changed after Rev31. It no longer has a first argument.
// The following is the Rev31 form.
const char *spirv = R"(
OpLine %4 %2 0 0