diff --git a/CMakeLists.txt b/CMakeLists.txt index af4762c89..6f65fb486 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,6 +106,7 @@ set(SPIRV_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/source/assembly_grammar.h ${CMAKE_CURRENT_SOURCE_DIR}/source/binary.h ${CMAKE_CURRENT_SOURCE_DIR}/source/diagnostic.h + ${CMAKE_CURRENT_SOURCE_DIR}/source/endian.h ${CMAKE_CURRENT_SOURCE_DIR}/source/ext_inst.h ${CMAKE_CURRENT_SOURCE_DIR}/source/instruction.h ${CMAKE_CURRENT_SOURCE_DIR}/source/opcode.h @@ -117,6 +118,7 @@ set(SPIRV_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/source/assembly_grammar.cpp ${CMAKE_CURRENT_SOURCE_DIR}/source/binary.cpp ${CMAKE_CURRENT_SOURCE_DIR}/source/diagnostic.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/source/endian.cpp ${CMAKE_CURRENT_SOURCE_DIR}/source/ext_inst.cpp ${CMAKE_CURRENT_SOURCE_DIR}/source/opcode.cpp ${CMAKE_CURRENT_SOURCE_DIR}/source/operand.cpp diff --git a/source/binary.cpp b/source/binary.cpp index 4213bea16..128d27810 100644 --- a/source/binary.cpp +++ b/source/binary.cpp @@ -34,6 +34,7 @@ #include #include "assembly_grammar.h" #include "diagnostic.h" +#include "endian.h" #include "ext_inst.h" #include "instruction.h" #include "opcode.h" @@ -42,59 +43,9 @@ // Binary API -enum { - I32_ENDIAN_LITTLE = 0x03020100ul, - I32_ENDIAN_BIG = 0x00010203ul, -}; - -static const union { - unsigned char bytes[4]; - uint32_t value; -} o32_host_order = {{0, 1, 2, 3}}; - using id_to_type_id_map = std::unordered_map; using type_id_to_type_map = std::unordered_map; -#define I32_ENDIAN_HOST (o32_host_order.value) - -spv_result_t spvBinaryEndianness(const spv_binary binary, - spv_endianness_t *pEndian) { - if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY; - if (!pEndian) return SPV_ERROR_INVALID_POINTER; - - uint8_t bytes[4]; - memcpy(bytes, binary->code, sizeof(uint32_t)); - - if (0x03 == bytes[0] && 0x02 == bytes[1] && 0x23 == bytes[2] && - 0x07 == bytes[3]) { - *pEndian = SPV_ENDIANNESS_LITTLE; - return SPV_SUCCESS; - } - - if (0x07 == bytes[0] && 0x23 == bytes[1] && 0x02 == bytes[2] && - 0x03 == bytes[3]) { - *pEndian = SPV_ENDIANNESS_BIG; - return SPV_SUCCESS; - } - - return SPV_ERROR_INVALID_BINARY; -} - -uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian) { - if ((SPV_ENDIANNESS_LITTLE == endian && I32_ENDIAN_HOST == I32_ENDIAN_BIG) || - (SPV_ENDIANNESS_BIG == endian && I32_ENDIAN_HOST == I32_ENDIAN_LITTLE)) { - return (word & 0x000000ff) << 24 | (word & 0x0000ff00) << 8 | - (word & 0x00ff0000) >> 8 | (word & 0xff000000) >> 24; - } - - return word; -} - -uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high, - const spv_endianness_t endian) { - return (uint64_t(spvFixWord(high, endian)) << 32) | spvFixWord(low, endian); -} - spv_result_t spvBinaryHeaderGet(const spv_binary binary, const spv_endianness_t endian, spv_header_t *pHeader) { diff --git a/source/binary.h b/source/binary.h index ad542cf2e..05443953e 100644 --- a/source/binary.h +++ b/source/binary.h @@ -34,37 +34,6 @@ // Functions -/// @brief Fix the endianness of a word -/// -/// @param[in] word whos endianness should be fixed -/// @param[in] endian the desired endianness -/// -/// @return word with host endianness correction -uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian); - -/// @brief Fix the endianness of a double word -/// -/// @param[in] low the lower 32-bit of the double word -/// @param[in] high the higher 32-bit of the double word -/// @param[in] endian the desired endianness -/// -/// @return word with host endianness correction -uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high, - const spv_endianness_t endian); - -/// @brief Determine the endianness of the SPV binary -/// -/// Gets the endianness of the SPV source. Returns SPV_ENDIANNESS_UNKNOWN if -/// the -/// SPV magic number is invalid, otherwise the determined endianness. -/// -/// @param[in] binary the binary module -/// @param[out] pEndian return the endianness of the SPV module -/// -/// @return result code -spv_result_t spvBinaryEndianness(const spv_binary binary, - spv_endianness_t *pEndian); - /// @brief Grab the header from the SPV module /// /// @param[in] binary the binary module diff --git a/source/endian.cpp b/source/endian.cpp new file mode 100644 index 000000000..240718a5b --- /dev/null +++ b/source/endian.cpp @@ -0,0 +1,80 @@ +// 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 "endian.h" + +#include + +enum { + I32_ENDIAN_LITTLE = 0x03020100ul, + I32_ENDIAN_BIG = 0x00010203ul, +}; + +// TODO(dneto): This relies on undefined behaviour. Fix that. +static const union { + unsigned char bytes[4]; + uint32_t value; +} o32_host_order = {{0, 1, 2, 3}}; + +#define I32_ENDIAN_HOST (o32_host_order.value) + +uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian) { + if ((SPV_ENDIANNESS_LITTLE == endian && I32_ENDIAN_HOST == I32_ENDIAN_BIG) || + (SPV_ENDIANNESS_BIG == endian && I32_ENDIAN_HOST == I32_ENDIAN_LITTLE)) { + return (word & 0x000000ff) << 24 | (word & 0x0000ff00) << 8 | + (word & 0x00ff0000) >> 8 | (word & 0xff000000) >> 24; + } + + return word; +} + +uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high, + const spv_endianness_t endian) { + return (uint64_t(spvFixWord(high, endian)) << 32) | spvFixWord(low, endian); +} + +spv_result_t spvBinaryEndianness(const spv_binary binary, + spv_endianness_t *pEndian) { + if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY; + if (!pEndian) return SPV_ERROR_INVALID_POINTER; + + uint8_t bytes[4]; + memcpy(bytes, binary->code, sizeof(uint32_t)); + + if (0x03 == bytes[0] && 0x02 == bytes[1] && 0x23 == bytes[2] && + 0x07 == bytes[3]) { + *pEndian = SPV_ENDIANNESS_LITTLE; + return SPV_SUCCESS; + } + + if (0x07 == bytes[0] && 0x23 == bytes[1] && 0x02 == bytes[2] && + 0x03 == bytes[3]) { + *pEndian = SPV_ENDIANNESS_BIG; + return SPV_SUCCESS; + } + + return SPV_ERROR_INVALID_BINARY; +} diff --git a/source/endian.h b/source/endian.h new file mode 100644 index 000000000..f786b1933 --- /dev/null +++ b/source/endian.h @@ -0,0 +1,62 @@ +// 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_ENDIAN_H_ +#define _LIBSPIRV_UTIL_ENDIAN_H_ + +#include + +/// @brief Fix the endianness of a word +/// +/// @param[in] word whos endianness should be fixed +/// @param[in] endian the desired endianness +/// +/// @return word with host endianness correction +uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian); + +/// @brief Fix the endianness of a double word +/// +/// @param[in] low the lower 32-bit of the double word +/// @param[in] high the higher 32-bit of the double word +/// @param[in] endian the desired endianness +/// +/// @return word with host endianness correction +uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high, + const spv_endianness_t endian); + +/// @brief Determine the endianness of the SPV binary +/// +/// Gets the endianness of the SPV source. Returns SPV_ENDIANNESS_UNKNOWN if +/// the +/// SPV magic number is invalid, otherwise the determined endianness. +/// +/// @param[in] binary the binary module +/// @param[out] pEndian return the endianness of the SPV module +/// +/// @return result code +spv_result_t spvBinaryEndianness(const spv_binary binary, + spv_endianness_t *pEndian); +#endif diff --git a/source/opcode.cpp b/source/opcode.cpp index 32fe7fbd3..fa0159ca6 100644 --- a/source/opcode.cpp +++ b/source/opcode.cpp @@ -25,7 +25,7 @@ // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. #include -#include "binary.h" +#include "endian.h" #include "instruction.h" #include "opcode.h" diff --git a/source/validate.cpp b/source/validate.cpp index 341d618f8..ff66308c5 100644 --- a/source/validate.cpp +++ b/source/validate.cpp @@ -27,6 +27,7 @@ #include #include "binary.h" #include "diagnostic.h" +#include "endian.h" #include "instruction.h" #include "opcode.h" #include "operand.h" diff --git a/test/UnitSPIRV.h b/test/UnitSPIRV.h index 4686d257e..5a52be37c 100644 --- a/test/UnitSPIRV.h +++ b/test/UnitSPIRV.h @@ -31,6 +31,7 @@ #include "../source/assembly_grammar.h" #include "../source/binary.h" #include "../source/diagnostic.h" +#include "../source/endian.h" #include "../source/opcode.h" #include "../source/text.h" #include "../source/text_handler.h"