A spv_binary_t points to const code words.
authorDavid Neto <dneto@google.com>
Wed, 28 Oct 2015 17:50:32 +0000 (13:50 -0400)
committerDavid Neto <dneto@google.com>
Tue, 10 Nov 2015 20:57:22 +0000 (15:57 -0500)
include/libspirv/libspirv.h
source/binary.cpp
source/disassemble.cpp
source/text.cpp

index 5713a57..300c0b2 100644 (file)
 #ifndef LIBSPIRV_LIBSPIRV_LIBSPIRV_H_
 #define LIBSPIRV_LIBSPIRV_LIBSPIRV_H_
 
-#include <headers/spirv.h>
-#include <headers/spirv_operands.hpp>
 #include <headers/GLSL.std.450.h>
 #include <headers/OpenCL.std.h>
+#include <headers/spirv.h>
+#include <headers/spirv_operands.hpp>
 
 #ifdef __cplusplus
 using namespace spv;
@@ -354,7 +354,7 @@ typedef struct spv_ext_inst_table_t {
 } spv_ext_inst_table_t;
 
 typedef struct spv_binary_t {
-  uint32_t* code;
+  uint32_t const* code;
   uint64_t wordCount;
 } spv_binary_t;
 
@@ -473,7 +473,7 @@ void spvTextDestroy(spv_text text);
 /// @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 const* binary, const uint64_t wordCount,
                              const uint32_t options,
                              const spv_opcode_table opcodeTable,
                              const spv_operand_table operandTable,
@@ -494,7 +494,7 @@ spv_result_t spvBinaryToText(uint32_t* binary, const uint64_t wordCount,
 ///
 /// @return result code
 spv_result_t spvBinaryToTextWithFormat(
-    uint32_t* binary, const uint64_t wordCount, const uint32_t options,
+    uint32_t const* binary, const uint64_t wordCount, const uint32_t options,
     const spv_opcode_table opcodeTable, const spv_operand_table operandTable,
     const spv_ext_inst_table extInstTable, spv_assembly_syntax_format_t format,
     spv_text* pText, spv_diagnostic* pDiagnostic);
index 330e692..8698b27 100644 (file)
@@ -502,7 +502,7 @@ spv_result_t spvBinaryDecodeOpcode(
 }
 
 spv_result_t spvBinaryToTextWithFormat(
-    uint32_t* code, const uint64_t wordCount, const uint32_t options,
+    uint32_t const* code, const uint64_t wordCount, const uint32_t options,
     const spv_opcode_table opcodeTable, const spv_operand_table operandTable,
     const spv_ext_inst_table extInstTable, spv_assembly_syntax_format_t format,
     spv_text* pText, spv_diagnostic* pDiagnostic) {
index 90a6181..8856924 100644 (file)
@@ -32,7 +32,7 @@
 
 #include "libspirv/libspirv.h"
 
-spv_result_t spvBinaryToText(uint32_t* code, const uint64_t wordCount,
+spv_result_t spvBinaryToText(uint32_t const* code, const uint64_t wordCount,
                              const uint32_t options,
                              const spv_opcode_table opcodeTable,
                              const spv_operand_table operandTable,
index a79699d..9e9104b 100644 (file)
@@ -613,21 +613,20 @@ spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
 
 namespace {
 
-/// @brief Populate a binary stream with this generator's header.
+/// @brief Populate a binary stream's words with this generator's header.
 ///
-/// @param[in,out] binary the binary stream
+/// @param[in,out] words the array of words
 /// @param[in] bound the upper ID bound
 ///
 /// @return result code
-spv_result_t SetHeader(spv_binary_t* binary, const uint32_t bound) {
-  if (!binary) return SPV_ERROR_INVALID_BINARY;
-  if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
+spv_result_t SetHeader(uint32_t* words, const uint32_t bound) {
+  if (!words) return SPV_ERROR_INVALID_BINARY;
 
-  binary->code[SPV_INDEX_MAGIC_NUMBER] = SPV_MAGIC_NUMBER;
-  binary->code[SPV_INDEX_VERSION_NUMBER] = SPV_VERSION_NUMBER;
-  binary->code[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS;
-  binary->code[SPV_INDEX_BOUND] = bound;
-  binary->code[SPV_INDEX_SCHEMA] = 0;  // NOTE: Reserved
+  words[SPV_INDEX_MAGIC_NUMBER] = SPV_MAGIC_NUMBER;
+  words[SPV_INDEX_VERSION_NUMBER] = SPV_VERSION_NUMBER;
+  words[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS;
+  words[SPV_INDEX_BOUND] = bound;
+  words[SPV_INDEX_SCHEMA] = 0;  // NOTE: Reserved
 
   return SPV_SUCCESS;
 }
@@ -686,6 +685,9 @@ spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
     currentIndex += inst.words.size();
   }
 
+  if (auto error = SetHeader(data, context.getBound()))
+    return error;
+
   spv_binary binary = new spv_binary_t();
   if (!binary) {
     delete[] data;
@@ -694,11 +696,6 @@ spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
   binary->code = data;
   binary->wordCount = totalSize;
 
-  if (auto error = SetHeader(binary, context.getBound())) {
-    spvBinaryDestroy(binary);
-    return error;
-  }
-
   *pBinary = binary;
 
   return SPV_SUCCESS;