Fixed warnings on windows and constness of spv_binary.

Replaced uint64_t with size_t in the places that make sense and
added spv_const_binary{,_t} to allow the interface to accept non
modifiable spirv where appropriate.
This commit is contained in:
Andrew Woloszyn 2015-11-11 11:05:07 -05:00 committed by David Neto
parent 9049bb4877
commit 55ecc2e097
13 changed files with 56 additions and 33 deletions

23
include/libspirv/libspirv.h Normal file → Executable file
View File

@ -380,19 +380,25 @@ typedef struct spv_ext_inst_table_t {
} spv_ext_inst_table_t; } spv_ext_inst_table_t;
typedef struct spv_binary_t { typedef struct spv_binary_t {
const uint32_t* code; uint32_t* code;
uint64_t wordCount; size_t wordCount;
} spv_binary_t; } 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 { typedef struct spv_text_t {
const char* str; const char* str;
uint64_t length; size_t length;
} spv_text_t; } spv_text_t;
typedef struct spv_position_t { typedef struct spv_position_t {
uint64_t line; size_t line;
uint64_t column; size_t column;
uint64_t index; size_t index;
} spv_position_t; } spv_position_t;
typedef struct spv_diagnostic_t { typedef struct spv_diagnostic_t {
@ -409,6 +415,7 @@ typedef const spv_operand_desc_t* spv_operand_desc;
typedef const spv_operand_table_t* spv_operand_table; 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_desc_t* spv_ext_inst_desc;
typedef const spv_ext_inst_table_t* spv_ext_inst_table; 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_binary_t* spv_binary;
typedef spv_text_t* spv_text; typedef spv_text_t* spv_text;
typedef spv_position_t* spv_position; typedef spv_position_t* spv_position;
@ -452,7 +459,7 @@ spv_result_t spvExtInstTableGet(spv_ext_inst_table* pTable);
/// @param[out] pDiagnostic contains diagnostic on failure /// @param[out] pDiagnostic contains diagnostic on failure
/// ///
/// @return result code /// @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_opcode_table opcodeTable,
const spv_operand_table operandTable, const spv_operand_table operandTable,
const spv_ext_inst_table extInstTable, const spv_ext_inst_table extInstTable,
@ -505,7 +512,7 @@ void spvBinaryDestroy(spv_binary binary);
/// @param[out] pDiagnostic contains diagnostic on failure /// @param[out] pDiagnostic contains diagnostic on failure
/// ///
/// @return result code /// @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_opcode_table opcodeTable,
const spv_operand_table operandTable, const spv_operand_table operandTable,
const spv_ext_inst_table extInstTable, const spv_ext_inst_table extInstTable,

6
source/binary.cpp Normal file → Executable file
View File

@ -39,7 +39,7 @@
#include "opcode.h" #include "opcode.h"
#include "operand.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, const spv_endianness_t endian,
spv_header_t* pHeader) { spv_header_t* pHeader) {
if (!binary->code) return SPV_ERROR_INVALID_BINARY; if (!binary->code) return SPV_ERROR_INVALID_BINARY;
@ -241,7 +241,7 @@ spv_result_t Parser::parseModule() {
<< " words instead of " << SPV_INDEX_INSTRUCTION; << " words instead of " << SPV_INDEX_INSTRUCTION;
// Check the magic number and detect the module's endianness. // 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)) { if (spvBinaryEndianness(&binary, &_.endian)) {
return diagnostic() << "Invalid SPIR-V magic number '" << std::hex return diagnostic() << "Invalid SPIR-V magic number '" << std::hex
<< _.words[0] << "'."; << _.words[0] << "'.";
@ -671,7 +671,7 @@ void Parser::recordNumberType(const spv_parsed_instruction_t* inst) {
} // anonymous namespace } // 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, const size_t num_words,
spv_parsed_header_fn_t parsed_header, spv_parsed_header_fn_t parsed_header,
spv_parsed_instruction_fn_t parsed_instruction, spv_parsed_instruction_fn_t parsed_instruction,

4
source/binary.h Normal file → Executable file
View File

@ -107,7 +107,7 @@ typedef spv_result_t (*spv_parsed_instruction_fn_t)(
// returns SPV_ERROR_INVALID_BINARY and emits a diagnostic. If a callback // returns SPV_ERROR_INVALID_BINARY and emits a diagnostic. If a callback
// returns anything other than SPV_SUCCESS, then that error code is returned // returns anything other than SPV_SUCCESS, then that error code is returned
// and parsing terminates early. // 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, const size_t num_words,
spv_parsed_header_fn_t parse_header, spv_parsed_header_fn_t parse_header,
spv_parsed_instruction_fn_t parse_instruction, spv_parsed_instruction_fn_t parse_instruction,
@ -124,7 +124,7 @@ spv_result_t spvBinaryParse(void* user_data, const uint32_t* const words,
/// @param[out] pHeader the returned header /// @param[out] pHeader the returned header
/// ///
/// @return result code /// @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, const spv_endianness_t endian,
spv_header_t* pHeader); spv_header_t* pHeader);

2
source/endian.cpp Normal file → Executable file
View File

@ -56,7 +56,7 @@ uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high,
return (uint64_t(spvFixWord(high, endian)) << 32) | spvFixWord(low, endian); 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) { spv_endianness_t* pEndian) {
if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY; if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
if (!pEndian) return SPV_ERROR_INVALID_POINTER; if (!pEndian) return SPV_ERROR_INVALID_POINTER;

2
source/endian.h Normal file → Executable file
View File

@ -57,7 +57,7 @@ uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high,
/// @param[out] pEndian return the endianness of the SPV module /// @param[out] pEndian return the endianness of the SPV module
/// ///
/// @return result code /// @return result code
spv_result_t spvBinaryEndianness(const spv_binary binary, spv_result_t spvBinaryEndianness(const spv_const_binary binary,
spv_endianness_t* pEndian); spv_endianness_t* pEndian);
#endif // LIBSPIRV_ENDIAN_H_ #endif // LIBSPIRV_ENDIAN_H_

2
source/text.cpp Normal file → Executable file
View File

@ -737,7 +737,7 @@ spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
} // anonymous namespace } // anonymous namespace
spv_result_t spvTextToBinary(const char* input_text, 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_opcode_table opcodeTable,
const spv_operand_table operandTable, const spv_operand_table operandTable,
const spv_ext_inst_table extInstTable, const spv_ext_inst_table extInstTable,

2
source/validate.cpp Normal file → Executable file
View File

@ -259,7 +259,7 @@ spv_result_t spvValidateIDs(const spv_instruction_t* pInsts,
return SPV_SUCCESS; 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_opcode_table opcodeTable,
const spv_operand_table operandTable, const spv_operand_table operandTable,
const spv_ext_inst_table extInstTable, const spv_ext_inst_table extInstTable,

6
test/BinaryEndianness.cpp Normal file → Executable file
View File

@ -30,7 +30,7 @@ namespace {
TEST(BinaryEndianness, InvalidCode) { TEST(BinaryEndianness, InvalidCode) {
uint32_t invalidMagicNumber[] = {0}; uint32_t invalidMagicNumber[] = {0};
spv_binary_t binary = {invalidMagicNumber, 1}; spv_const_binary_t binary = {invalidMagicNumber, 1};
spv_endianness_t endian; spv_endianness_t endian;
ASSERT_EQ(SPV_ERROR_INVALID_BINARY, spvBinaryEndianness(&binary, &endian)); ASSERT_EQ(SPV_ERROR_INVALID_BINARY, spvBinaryEndianness(&binary, &endian));
} }
@ -42,7 +42,7 @@ TEST(BinaryEndianness, Little) {
} else { } else {
magicNumber = 0x03022307; magicNumber = 0x03022307;
} }
spv_binary_t binary = {&magicNumber, 1}; spv_const_binary_t binary = {&magicNumber, 1};
spv_endianness_t endian; spv_endianness_t endian;
ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&binary, &endian)); ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&binary, &endian));
ASSERT_EQ(SPV_ENDIANNESS_LITTLE, endian); ASSERT_EQ(SPV_ENDIANNESS_LITTLE, endian);
@ -55,7 +55,7 @@ TEST(BinaryEndianness, Big) {
} else { } else {
magicNumber = 0x03022307; magicNumber = 0x03022307;
} }
spv_binary_t binary = {&magicNumber, 1}; spv_const_binary_t binary = {&magicNumber, 1};
spv_endianness_t endian; spv_endianness_t endian;
ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&binary, &endian)); ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&binary, &endian));
ASSERT_EQ(SPV_ENDIANNESS_BIG, endian); ASSERT_EQ(SPV_ENDIANNESS_BIG, endian);

17
test/BinaryHeaderGet.cpp Normal file → Executable file
View File

@ -43,7 +43,9 @@ class BinaryHeaderGet : public ::testing::Test {
binary.code = code; binary.code = code;
binary.wordCount = 6; binary.wordCount = 6;
} }
spv_const_binary_t get_const_binary() {
return spv_const_binary_t{binary.code, binary.wordCount};
}
virtual void TearDown() {} virtual void TearDown() {}
uint32_t code[6]; uint32_t code[6];
@ -52,10 +54,11 @@ class BinaryHeaderGet : public ::testing::Test {
TEST_F(BinaryHeaderGet, Default) { TEST_F(BinaryHeaderGet, Default) {
spv_endianness_t endian; 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; 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(static_cast<uint32_t>(SpvMagicNumber), header.magic);
ASSERT_EQ(99u, header.version); ASSERT_EQ(99u, header.version);
@ -66,22 +69,24 @@ TEST_F(BinaryHeaderGet, Default) {
} }
TEST_F(BinaryHeaderGet, InvalidCode) { TEST_F(BinaryHeaderGet, InvalidCode) {
spv_binary_t binary = {nullptr, 0}; spv_const_binary_t binary = {nullptr, 0};
spv_header_t header; spv_header_t header;
ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
spvBinaryHeaderGet(&binary, SPV_ENDIANNESS_LITTLE, &header)); spvBinaryHeaderGet(&binary, SPV_ENDIANNESS_LITTLE, &header));
} }
TEST_F(BinaryHeaderGet, InvalidPointerHeader) { TEST_F(BinaryHeaderGet, InvalidPointerHeader) {
spv_const_binary_t const_bin = get_const_binary();
ASSERT_EQ(SPV_ERROR_INVALID_POINTER, ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
spvBinaryHeaderGet(&binary, SPV_ENDIANNESS_LITTLE, nullptr)); spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
} }
TEST_F(BinaryHeaderGet, TruncatedHeader) { TEST_F(BinaryHeaderGet, TruncatedHeader) {
for (int i = 1; i < SPV_INDEX_INSTRUCTION; i++) { for (int i = 1; i < SPV_INDEX_INSTRUCTION; i++) {
binary.wordCount = i; binary.wordCount = i;
spv_const_binary_t const_bin = get_const_binary();
ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
spvBinaryHeaderGet(&binary, SPV_ENDIANNESS_LITTLE, nullptr)); spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
} }
} }

6
test/HexFloat.cpp Normal file → Executable file
View File

@ -24,6 +24,12 @@
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. // 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 <cmath>
#include <cstdio> #include <cstdio>
#include <sstream> #include <sstream>

9
test/Validate.cpp Normal file → Executable file
View File

@ -39,6 +39,9 @@ class Validate : public ::testing::Test {
} }
virtual void TearDown() { spvBinaryDestroy(binary); } virtual void TearDown() { spvBinaryDestroy(binary); }
spv_const_binary get_const_binary() {
return spv_const_binary(binary);
}
spv_binary binary; spv_binary binary;
spv_opcode_table opcodeTable; spv_opcode_table opcodeTable;
@ -63,7 +66,7 @@ OpFunctionEnd
spvTextToBinary(str, strlen(str), opcodeTable, operandTable, spvTextToBinary(str, strlen(str), opcodeTable, operandTable,
extInstTable, &binary, &diagnostic)); extInstTable, &binary, &diagnostic));
ASSERT_EQ(SPV_SUCCESS, ASSERT_EQ(SPV_SUCCESS,
spvValidate(binary, opcodeTable, operandTable, extInstTable, spvValidate(get_const_binary(), opcodeTable, operandTable, extInstTable,
SPV_VALIDATE_ALL, &diagnostic)); SPV_VALIDATE_ALL, &diagnostic));
if (diagnostic) { if (diagnostic) {
spvDiagnosticPrint(diagnostic); spvDiagnosticPrint(diagnostic);
@ -88,7 +91,7 @@ OpFunctionEnd
spvTextToBinary(str, strlen(str), opcodeTable, operandTable, spvTextToBinary(str, strlen(str), opcodeTable, operandTable,
extInstTable, &binary, &diagnostic)); extInstTable, &binary, &diagnostic));
ASSERT_EQ(SPV_ERROR_INVALID_ID, ASSERT_EQ(SPV_ERROR_INVALID_ID,
spvValidate(binary, opcodeTable, operandTable, extInstTable, spvValidate(get_const_binary(), opcodeTable, operandTable, extInstTable,
SPV_VALIDATE_ALL, &diagnostic)); SPV_VALIDATE_ALL, &diagnostic));
ASSERT_NE(nullptr, diagnostic); ASSERT_NE(nullptr, diagnostic);
spvDiagnosticPrint(diagnostic); spvDiagnosticPrint(diagnostic);
@ -113,7 +116,7 @@ OpFunctionEnd
extInstTable, &binary, &diagnostic)); extInstTable, &binary, &diagnostic));
// TODO: Fix setting of bound in spvTextTo, then remove this! // TODO: Fix setting of bound in spvTextTo, then remove this!
ASSERT_EQ(SPV_ERROR_INVALID_ID, ASSERT_EQ(SPV_ERROR_INVALID_ID,
spvValidate(binary, opcodeTable, operandTable, extInstTable, spvValidate(get_const_binary(), opcodeTable, operandTable, extInstTable,
SPV_VALIDATE_ALL, &diagnostic)); SPV_VALIDATE_ALL, &diagnostic));
ASSERT_NE(nullptr, diagnostic); ASSERT_NE(nullptr, diagnostic);
spvDiagnosticPrint(diagnostic); spvDiagnosticPrint(diagnostic);

8
test/ValidateID.cpp Normal file → Executable file
View File

@ -45,7 +45,9 @@ class ValidateID : public ::testing::Test {
} }
virtual void TearDown() { spvBinaryDestroy(binary); } virtual void TearDown() { spvBinaryDestroy(binary); }
spv_const_binary get_const_binary() {
return spv_const_binary(binary);
}
spv_opcode_table opcodeTable; spv_opcode_table opcodeTable;
spv_operand_table operandTable; spv_operand_table operandTable;
spv_ext_inst_table extInstTable; spv_ext_inst_table extInstTable;
@ -63,8 +65,8 @@ class ValidateID : public ::testing::Test {
ASSERT_EQ(SPV_SUCCESS, error); \ ASSERT_EQ(SPV_SUCCESS, error); \
} \ } \
spv_result_t result = \ spv_result_t result = \
spvValidate(binary, opcodeTable, operandTable, extInstTable, \ spvValidate(get_const_binary(), opcodeTable, operandTable, \
SPV_VALIDATE_ID_BIT, &diagnostic); \ extInstTable, SPV_VALIDATE_ID_BIT, &diagnostic); \
if (SPV_SUCCESS != result) { \ if (SPV_SUCCESS != result) { \
spvDiagnosticPrint(diagnostic); \ spvDiagnosticPrint(diagnostic); \
spvDiagnosticDestroy(diagnostic); \ spvDiagnosticDestroy(diagnostic); \

2
tools/val/val.cpp Normal file → Executable file
View File

@ -96,7 +96,7 @@ int main(int argc, char** argv) {
return 1; return 1;
} }
spv_binary_t binary = {contents.data(), contents.size()}; spv_const_binary_t binary = {contents.data(), contents.size()};
spv_opcode_table opcodeTable; spv_opcode_table opcodeTable;
spv_result_t error = spvOpcodeTableGet(&opcodeTable); spv_result_t error = spvOpcodeTableGet(&opcodeTable);