} spv_ext_inst_table_t;
typedef struct spv_binary_t {
- const uint32_t* code;
- uint64_t wordCount;
+ uint32_t* code;
+ size_t wordCount;
} spv_binary_t;
+typedef struct spv_const_binary_t {
+ const uint32_t* code;
+ const size_t wordCount;
+} spv_const_binary_t;
+
+
typedef struct spv_text_t {
const char* str;
- uint64_t length;
+ size_t length;
} spv_text_t;
typedef struct spv_position_t {
- uint64_t line;
- uint64_t column;
- uint64_t index;
+ size_t line;
+ size_t column;
+ size_t index;
} spv_position_t;
typedef struct spv_diagnostic_t {
typedef const spv_operand_table_t* spv_operand_table;
typedef const spv_ext_inst_desc_t* spv_ext_inst_desc;
typedef const spv_ext_inst_table_t* spv_ext_inst_table;
+typedef spv_const_binary_t* spv_const_binary;
typedef spv_binary_t* spv_binary;
typedef spv_text_t* spv_text;
typedef spv_position_t* spv_position;
/// @param[out] pDiagnostic contains diagnostic on failure
///
/// @return result code
-spv_result_t spvTextToBinary(const char* text, const uint64_t length,
+spv_result_t spvTextToBinary(const char* text, const size_t length,
const spv_opcode_table opcodeTable,
const spv_operand_table operandTable,
const spv_ext_inst_table extInstTable,
/// @param[out] pDiagnostic contains diagnostic on failure
///
/// @return result code
-spv_result_t spvValidate(const spv_binary binary,
+spv_result_t spvValidate(const spv_const_binary binary,
const spv_opcode_table opcodeTable,
const spv_operand_table operandTable,
const spv_ext_inst_table extInstTable,
#include "opcode.h"
#include "operand.h"
-spv_result_t spvBinaryHeaderGet(const spv_binary binary,
+spv_result_t spvBinaryHeaderGet(const spv_const_binary binary,
const spv_endianness_t endian,
spv_header_t* pHeader) {
if (!binary->code) return SPV_ERROR_INVALID_BINARY;
<< " words instead of " << SPV_INDEX_INSTRUCTION;
// Check the magic number and detect the module's endianness.
- spv_binary_t binary = {_.words, _.num_words}; // Can't make this const. :-(
+ spv_const_binary_t binary{_.words, _.num_words};
if (spvBinaryEndianness(&binary, &_.endian)) {
return diagnostic() << "Invalid SPIR-V magic number '" << std::hex
<< _.words[0] << "'.";
} // anonymous namespace
-spv_result_t spvBinaryParse(void* user_data, const uint32_t* const code,
+spv_result_t spvBinaryParse(void* user_data, const uint32_t* code,
const size_t num_words,
spv_parsed_header_fn_t parsed_header,
spv_parsed_instruction_fn_t parsed_instruction,
// returns SPV_ERROR_INVALID_BINARY and emits a diagnostic. If a callback
// returns anything other than SPV_SUCCESS, then that error code is returned
// and parsing terminates early.
-spv_result_t spvBinaryParse(void* user_data, const uint32_t* const words,
+spv_result_t spvBinaryParse(void* user_data, const uint32_t* words,
const size_t num_words,
spv_parsed_header_fn_t parse_header,
spv_parsed_instruction_fn_t parse_instruction,
/// @param[out] pHeader the returned header
///
/// @return result code
-spv_result_t spvBinaryHeaderGet(const spv_binary binary,
+spv_result_t spvBinaryHeaderGet(const spv_const_binary binary,
const spv_endianness_t endian,
spv_header_t* pHeader);
return (uint64_t(spvFixWord(high, endian)) << 32) | spvFixWord(low, endian);
}
-spv_result_t spvBinaryEndianness(const spv_binary binary,
+spv_result_t spvBinaryEndianness(spv_const_binary binary,
spv_endianness_t* pEndian) {
if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
if (!pEndian) return SPV_ERROR_INVALID_POINTER;
/// @param[out] pEndian return the endianness of the SPV module
///
/// @return result code
-spv_result_t spvBinaryEndianness(const spv_binary binary,
+spv_result_t spvBinaryEndianness(const spv_const_binary binary,
spv_endianness_t* pEndian);
#endif // LIBSPIRV_ENDIAN_H_
} // anonymous namespace
spv_result_t spvTextToBinary(const char* input_text,
- const uint64_t input_text_size,
+ const size_t input_text_size,
const spv_opcode_table opcodeTable,
const spv_operand_table operandTable,
const spv_ext_inst_table extInstTable,
return SPV_SUCCESS;
}
-spv_result_t spvValidate(const spv_binary binary,
+spv_result_t spvValidate(const spv_const_binary binary,
const spv_opcode_table opcodeTable,
const spv_operand_table operandTable,
const spv_ext_inst_table extInstTable,
TEST(BinaryEndianness, InvalidCode) {
uint32_t invalidMagicNumber[] = {0};
- spv_binary_t binary = {invalidMagicNumber, 1};
+ spv_const_binary_t binary = {invalidMagicNumber, 1};
spv_endianness_t endian;
ASSERT_EQ(SPV_ERROR_INVALID_BINARY, spvBinaryEndianness(&binary, &endian));
}
} else {
magicNumber = 0x03022307;
}
- spv_binary_t binary = {&magicNumber, 1};
+ spv_const_binary_t binary = {&magicNumber, 1};
spv_endianness_t endian;
ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&binary, &endian));
ASSERT_EQ(SPV_ENDIANNESS_LITTLE, endian);
} else {
magicNumber = 0x03022307;
}
- spv_binary_t binary = {&magicNumber, 1};
+ spv_const_binary_t binary = {&magicNumber, 1};
spv_endianness_t endian;
ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&binary, &endian));
ASSERT_EQ(SPV_ENDIANNESS_BIG, endian);
binary.code = code;
binary.wordCount = 6;
}
-
+ spv_const_binary_t get_const_binary() {
+ return spv_const_binary_t{binary.code, binary.wordCount};
+ }
virtual void TearDown() {}
uint32_t code[6];
TEST_F(BinaryHeaderGet, Default) {
spv_endianness_t endian;
- ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&binary, &endian));
+ spv_const_binary_t const_bin = get_const_binary();
+ ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&const_bin, &endian));
spv_header_t header;
- ASSERT_EQ(SPV_SUCCESS, spvBinaryHeaderGet(&binary, endian, &header));
+ ASSERT_EQ(SPV_SUCCESS, spvBinaryHeaderGet(&const_bin, endian, &header));
ASSERT_EQ(static_cast<uint32_t>(SpvMagicNumber), header.magic);
ASSERT_EQ(99u, header.version);
}
TEST_F(BinaryHeaderGet, InvalidCode) {
- spv_binary_t binary = {nullptr, 0};
+ spv_const_binary_t binary = {nullptr, 0};
spv_header_t header;
ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
spvBinaryHeaderGet(&binary, SPV_ENDIANNESS_LITTLE, &header));
}
TEST_F(BinaryHeaderGet, InvalidPointerHeader) {
+ spv_const_binary_t const_bin = get_const_binary();
ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
- spvBinaryHeaderGet(&binary, SPV_ENDIANNESS_LITTLE, nullptr));
+ spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
}
TEST_F(BinaryHeaderGet, TruncatedHeader) {
for (int i = 1; i < SPV_INDEX_INSTRUCTION; i++) {
binary.wordCount = i;
+ spv_const_binary_t const_bin = get_const_binary();
ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
- spvBinaryHeaderGet(&binary, SPV_ENDIANNESS_LITTLE, nullptr));
+ spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
}
}
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
+#ifdef _MSC_VER
+// We define this so that we can use sscanf in the tests without
+// MSVC warning us all the time.
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+
#include <cmath>
#include <cstdio>
#include <sstream>
}
virtual void TearDown() { spvBinaryDestroy(binary); }
+ spv_const_binary get_const_binary() {
+ return spv_const_binary(binary);
+ }
spv_binary binary;
spv_opcode_table opcodeTable;
spvTextToBinary(str, strlen(str), opcodeTable, operandTable,
extInstTable, &binary, &diagnostic));
ASSERT_EQ(SPV_SUCCESS,
- spvValidate(binary, opcodeTable, operandTable, extInstTable,
+ spvValidate(get_const_binary(), opcodeTable, operandTable, extInstTable,
SPV_VALIDATE_ALL, &diagnostic));
if (diagnostic) {
spvDiagnosticPrint(diagnostic);
spvTextToBinary(str, strlen(str), opcodeTable, operandTable,
extInstTable, &binary, &diagnostic));
ASSERT_EQ(SPV_ERROR_INVALID_ID,
- spvValidate(binary, opcodeTable, operandTable, extInstTable,
+ spvValidate(get_const_binary(), opcodeTable, operandTable, extInstTable,
SPV_VALIDATE_ALL, &diagnostic));
ASSERT_NE(nullptr, diagnostic);
spvDiagnosticPrint(diagnostic);
extInstTable, &binary, &diagnostic));
// TODO: Fix setting of bound in spvTextTo, then remove this!
ASSERT_EQ(SPV_ERROR_INVALID_ID,
- spvValidate(binary, opcodeTable, operandTable, extInstTable,
+ spvValidate(get_const_binary(), opcodeTable, operandTable, extInstTable,
SPV_VALIDATE_ALL, &diagnostic));
ASSERT_NE(nullptr, diagnostic);
spvDiagnosticPrint(diagnostic);
}
virtual void TearDown() { spvBinaryDestroy(binary); }
-
+ spv_const_binary get_const_binary() {
+ return spv_const_binary(binary);
+ }
spv_opcode_table opcodeTable;
spv_operand_table operandTable;
spv_ext_inst_table extInstTable;
ASSERT_EQ(SPV_SUCCESS, error); \
} \
spv_result_t result = \
- spvValidate(binary, opcodeTable, operandTable, extInstTable, \
- SPV_VALIDATE_ID_BIT, &diagnostic); \
+ spvValidate(get_const_binary(), opcodeTable, operandTable, \
+ extInstTable, SPV_VALIDATE_ID_BIT, &diagnostic); \
if (SPV_SUCCESS != result) { \
spvDiagnosticPrint(diagnostic); \
spvDiagnosticDestroy(diagnostic); \
return 1;
}
- spv_binary_t binary = {contents.data(), contents.size()};
+ spv_const_binary_t binary = {contents.data(), contents.size()};
spv_opcode_table opcodeTable;
spv_result_t error = spvOpcodeTableGet(&opcodeTable);