Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 1 | // Copyright (c) 2015 The Khronos Group Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | // copy of this software and/or associated documentation files (the |
| 5 | // "Materials"), to deal in the Materials without restriction, including |
| 6 | // without limitation the rights to use, copy, modify, merge, publish, |
| 7 | // distribute, sublicense, and/or sell copies of the Materials, and to |
| 8 | // permit persons to whom the Materials are furnished to do so, subject to |
| 9 | // the following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be included |
| 12 | // in all copies or substantial portions of the Materials. |
| 13 | // |
| 14 | // MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS |
| 15 | // KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS |
| 16 | // SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT |
| 17 | // https://www.khronos.org/registry/ |
| 18 | // |
| 19 | // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 | // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 26 | |
| 27 | #include <libspirv/libspirv.h> |
| 28 | #include "binary.h" |
| 29 | #include "diagnostic.h" |
| 30 | #include "ext_inst.h" |
| 31 | #include "opcode.h" |
| 32 | #include "operand.h" |
| 33 | |
| 34 | #include <assert.h> |
| 35 | #include <string.h> |
| 36 | |
| 37 | #include <sstream> |
| 38 | |
| 39 | // Binary API |
| 40 | |
| 41 | enum { |
| 42 | I32_ENDIAN_LITTLE = 0x03020100ul, |
| 43 | I32_ENDIAN_BIG = 0x00010203ul, |
| 44 | }; |
| 45 | |
| 46 | static const union { |
| 47 | unsigned char bytes[4]; |
| 48 | uint32_t value; |
| 49 | } o32_host_order = {{0, 1, 2, 3}}; |
| 50 | |
| 51 | #define I32_ENDIAN_HOST (o32_host_order.value) |
| 52 | |
| 53 | spv_result_t spvBinaryEndianness(const spv_binary binary, |
| 54 | spv_endianness_t *pEndian) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 55 | if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY; |
| 56 | if (!pEndian) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 57 | |
| 58 | uint8_t bytes[4]; |
| 59 | memcpy(bytes, binary->code, sizeof(uint32_t)); |
| 60 | |
| 61 | if (0x03 == bytes[0] && 0x02 == bytes[1] && 0x23 == bytes[2] && |
| 62 | 0x07 == bytes[3]) { |
| 63 | *pEndian = SPV_ENDIANNESS_LITTLE; |
| 64 | return SPV_SUCCESS; |
| 65 | } |
| 66 | |
| 67 | if (0x07 == bytes[0] && 0x23 == bytes[1] && 0x02 == bytes[2] && |
| 68 | 0x03 == bytes[3]) { |
| 69 | *pEndian = SPV_ENDIANNESS_BIG; |
| 70 | return SPV_SUCCESS; |
| 71 | } |
| 72 | |
| 73 | return SPV_ERROR_INVALID_BINARY; |
| 74 | } |
| 75 | |
| 76 | uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian) { |
| 77 | if ((SPV_ENDIANNESS_LITTLE == endian && I32_ENDIAN_HOST == I32_ENDIAN_BIG) || |
| 78 | (SPV_ENDIANNESS_BIG == endian && I32_ENDIAN_HOST == I32_ENDIAN_LITTLE)) { |
| 79 | return (word & 0x000000ff) << 24 | (word & 0x0000ff00) << 8 | |
| 80 | (word & 0x00ff0000) >> 8 | (word & 0xff000000) >> 24; |
| 81 | } |
| 82 | |
| 83 | return word; |
| 84 | } |
| 85 | |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 86 | uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high, |
| 87 | const spv_endianness_t endian) { |
| 88 | return (uint64_t(spvFixWord(high, endian)) << 32) | spvFixWord(low, endian); |
| 89 | } |
| 90 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 91 | spv_result_t spvBinaryHeaderGet(const spv_binary binary, |
| 92 | const spv_endianness_t endian, |
| 93 | spv_header_t *pHeader) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 94 | if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY; |
| 95 | if (!pHeader) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 96 | |
| 97 | // TODO: Validation checking? |
| 98 | pHeader->magic = spvFixWord(binary->code[SPV_INDEX_MAGIC_NUMBER], endian); |
| 99 | pHeader->version = spvFixWord(binary->code[SPV_INDEX_VERSION_NUMBER], endian); |
| 100 | pHeader->generator = |
| 101 | spvFixWord(binary->code[SPV_INDEX_GENERATOR_NUMBER], endian); |
| 102 | pHeader->bound = spvFixWord(binary->code[SPV_INDEX_BOUND], endian); |
| 103 | pHeader->schema = spvFixWord(binary->code[SPV_INDEX_SCHEMA], endian); |
| 104 | pHeader->instructions = &binary->code[SPV_INDEX_INSTRUCTION]; |
| 105 | |
| 106 | return SPV_SUCCESS; |
| 107 | } |
| 108 | |
| 109 | spv_result_t spvBinaryHeaderSet(spv_binary_t *binary, const uint32_t bound) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 110 | if (!binary) return SPV_ERROR_INVALID_BINARY; |
| 111 | if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 112 | |
| 113 | binary->code[SPV_INDEX_MAGIC_NUMBER] = SPV_MAGIC_NUMBER; |
| 114 | binary->code[SPV_INDEX_VERSION_NUMBER] = SPV_VERSION_NUMBER; |
Kenneth Benzie (Benie) | 81d7d49 | 2015-06-01 09:50:46 -0700 | [diff] [blame] | 115 | binary->code[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 116 | binary->code[SPV_INDEX_BOUND] = bound; |
| 117 | binary->code[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved |
| 118 | |
| 119 | return SPV_SUCCESS; |
| 120 | } |
| 121 | |
| 122 | spv_result_t spvBinaryEncodeU32(const uint32_t value, spv_instruction_t *pInst, |
| 123 | const spv_position position, |
| 124 | spv_diagnostic *pDiagnostic) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 125 | if (pInst->wordCount + 1 > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
| 126 | DIAGNOSTIC << "Instruction word count '" |
| 127 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "' exceeded."; |
| 128 | return SPV_ERROR_INVALID_TEXT; |
| 129 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 130 | |
| 131 | pInst->words[pInst->wordCount++] = (uint32_t)value; |
| 132 | return SPV_SUCCESS; |
| 133 | } |
| 134 | |
| 135 | spv_result_t spvBinaryEncodeU64(const uint64_t value, spv_instruction_t *pInst, |
| 136 | const spv_position position, |
| 137 | spv_diagnostic *pDiagnostic) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 138 | if (pInst->wordCount + 2 > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
| 139 | DIAGNOSTIC << "Instruction word count '" |
| 140 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "' exceeded."; |
| 141 | return SPV_ERROR_INVALID_TEXT; |
| 142 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 143 | |
| 144 | uint32_t low = (uint32_t)(0x00000000ffffffff & value); |
| 145 | uint32_t high = (uint32_t)((0xffffffff00000000 & value) >> 32); |
| 146 | pInst->words[pInst->wordCount++] = low; |
| 147 | pInst->words[pInst->wordCount++] = high; |
| 148 | return SPV_SUCCESS; |
| 149 | } |
| 150 | |
| 151 | spv_result_t spvBinaryEncodeString(const char *str, spv_instruction_t *pInst, |
| 152 | const spv_position position, |
| 153 | spv_diagnostic *pDiagnostic) { |
| 154 | size_t length = strlen(str); |
| 155 | size_t wordCount = (length / 4) + 1; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 156 | if ((sizeof(uint32_t) * pInst->wordCount) + length > |
| 157 | sizeof(uint32_t) * SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
| 158 | DIAGNOSTIC << "Instruction word count '" |
| 159 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "'exceeded."; |
| 160 | return SPV_ERROR_INVALID_TEXT; |
| 161 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 162 | |
| 163 | char *dest = (char *)&pInst->words[pInst->wordCount]; |
| 164 | strncpy(dest, str, length); |
| 165 | pInst->wordCount += (uint16_t)wordCount; |
| 166 | |
| 167 | return SPV_SUCCESS; |
| 168 | } |
| 169 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 170 | // TODO(dneto): This API is not powerful enough in the case that the |
| 171 | // number and type of operands are not known until partway through parsing |
| 172 | // the operation. This happens when enum operands might have different number |
| 173 | // of operands, or with extended instructions. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 174 | spv_operand_type_t spvBinaryOperandInfo(const uint32_t word, |
| 175 | const uint16_t operandIndex, |
| 176 | const spv_opcode_desc opcodeEntry, |
| 177 | const spv_operand_table operandTable, |
| 178 | spv_operand_desc *pOperandEntry) { |
| 179 | spv_operand_type_t type; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 180 | if (operandIndex < opcodeEntry->numTypes) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 181 | // NOTE: Do operand table lookup to set operandEntry if successful |
| 182 | uint16_t index = operandIndex - 1; |
| 183 | type = opcodeEntry->operandTypes[index]; |
| 184 | spv_operand_desc entry = nullptr; |
| 185 | if (!spvOperandTableValueLookup(operandTable, type, word, &entry)) { |
| 186 | if (SPV_OPERAND_TYPE_NONE != entry->operandTypes[0]) { |
| 187 | *pOperandEntry = entry; |
| 188 | } |
| 189 | } |
| 190 | } else if (*pOperandEntry) { |
| 191 | // NOTE: Use specified operand entry operand type for this word |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 192 | uint16_t index = operandIndex - opcodeEntry->numTypes; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 193 | type = (*pOperandEntry)->operandTypes[index]; |
| 194 | } else if (OpSwitch == opcodeEntry->opcode) { |
| 195 | // NOTE: OpSwitch is a special case which expects a list of paired extra |
| 196 | // operands |
| 197 | assert(0 && |
| 198 | "This case is previously untested, remove this assert and ensure it " |
| 199 | "is behaving correctly!"); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 200 | uint16_t lastIndex = opcodeEntry->numTypes - 1; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 201 | uint16_t index = lastIndex + ((operandIndex - lastIndex) % 2); |
| 202 | type = opcodeEntry->operandTypes[index]; |
| 203 | } else { |
| 204 | // NOTE: Default to last operand type in opcode entry |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 205 | uint16_t index = opcodeEntry->numTypes - 1; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 206 | type = opcodeEntry->operandTypes[index]; |
| 207 | } |
| 208 | return type; |
| 209 | } |
| 210 | |
| 211 | spv_result_t spvBinaryDecodeOperand( |
| 212 | const Op opcode, const spv_operand_type_t type, const uint32_t *words, |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 213 | uint16_t numWords, const spv_endianness_t endian, const uint32_t options, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 214 | const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 215 | spv_operand_pattern_t *pExpectedOperands, spv_ext_inst_type_t *pExtInstType, |
| 216 | out_stream &stream, spv_position position, spv_diagnostic *pDiagnostic) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 217 | if (!words || !position) return SPV_ERROR_INVALID_POINTER; |
| 218 | if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 219 | |
| 220 | bool print = spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options); |
| 221 | bool color = |
| 222 | print && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COLOR, options); |
| 223 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 224 | switch (type) { |
David Neto | b14a727 | 2015-09-25 13:56:09 -0400 | [diff] [blame] | 225 | case SPV_OPERAND_TYPE_EXECUTION_SCOPE: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 226 | case SPV_OPERAND_TYPE_ID: |
David Neto | b14a727 | 2015-09-25 13:56:09 -0400 | [diff] [blame] | 227 | case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 228 | case SPV_OPERAND_TYPE_OPTIONAL_ID: |
David Neto | b14a727 | 2015-09-25 13:56:09 -0400 | [diff] [blame] | 229 | case SPV_OPERAND_TYPE_MEMORY_SEMANTICS: |
| 230 | case SPV_OPERAND_TYPE_RESULT_ID: { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 231 | if (color) { |
Pyry Haulos | 26b3b00 | 2015-09-09 13:35:53 -0700 | [diff] [blame] | 232 | if (type == SPV_OPERAND_TYPE_RESULT_ID) { |
| 233 | stream.get() << clr::blue(); |
| 234 | } else { |
| 235 | stream.get() << clr::yellow(); |
| 236 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 237 | } |
Lei Zhang | 97afd5c | 2015-09-14 15:26:12 -0400 | [diff] [blame] | 238 | stream.get() << "%" << spvFixWord(words[0], endian); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 239 | stream.get() << ((color) ? clr::reset() : ""); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 240 | position->index++; |
| 241 | } break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 242 | case SPV_OPERAND_TYPE_LITERAL_NUMBER: { |
| 243 | // NOTE: Special case for extended instruction use |
| 244 | if (OpExtInst == opcode) { |
| 245 | spv_ext_inst_desc extInst; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 246 | if (spvExtInstTableValueLookup(extInstTable, *pExtInstType, words[0], |
| 247 | &extInst)) { |
| 248 | DIAGNOSTIC << "Invalid extended instruction '" << words[0] << "'."; |
| 249 | return SPV_ERROR_INVALID_BINARY; |
| 250 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 251 | spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands); |
Andrew Woloszyn | 0d350b5 | 2015-08-21 14:23:42 -0400 | [diff] [blame] | 252 | stream.get() << (color ? clr::red() : ""); |
| 253 | stream.get() << extInst->name; |
| 254 | stream.get() << (color ? clr::reset() : ""); |
Lei Zhang | 41bf073 | 2015-09-14 12:26:15 -0400 | [diff] [blame] | 255 | position->index++; |
| 256 | break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 257 | } |
Lei Zhang | 41bf073 | 2015-09-14 12:26:15 -0400 | [diff] [blame] | 258 | } // Fall through for the general case. |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 259 | case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER: |
David Neto | 561dc4e | 2015-09-25 14:23:29 -0400 | [diff] [blame^] | 260 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: |
| 261 | case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE: { |
Lei Zhang | 41bf073 | 2015-09-14 12:26:15 -0400 | [diff] [blame] | 262 | // TODO: Need to support multiple word literals |
| 263 | stream.get() << (color ? clr::red() : ""); |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 264 | if (numWords > 2) { |
| 265 | DIAGNOSTIC << "Literal numbers larger than 64-bit not supported yet."; |
| 266 | return SPV_UNSUPPORTED; |
| 267 | } else if (numWords == 2) { |
| 268 | stream.get() << spvFixDoubleWord(words[0], words[1], endian); |
| 269 | position->index += 2; |
| 270 | } else { |
| 271 | stream.get() << spvFixWord(words[0], endian); |
| 272 | position->index++; |
| 273 | } |
Lei Zhang | 41bf073 | 2015-09-14 12:26:15 -0400 | [diff] [blame] | 274 | stream.get() << (color ? clr::reset() : ""); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 275 | } break; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 276 | case SPV_OPERAND_TYPE_LITERAL_STRING: |
| 277 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: { |
Lei Zhang | 97afd5c | 2015-09-14 15:26:12 -0400 | [diff] [blame] | 278 | const char *string = (const char *)words; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 279 | uint64_t stringOperandCount = (strlen(string) / 4) + 1; |
| 280 | |
| 281 | // NOTE: Special case for extended instruction import |
| 282 | if (OpExtInstImport == opcode) { |
| 283 | *pExtInstType = spvExtInstImportTypeGet(string); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 284 | if (SPV_EXT_INST_TYPE_NONE == *pExtInstType) { |
| 285 | DIAGNOSTIC << "Invalid extended instruction import'" << string |
| 286 | << "'."; |
| 287 | return SPV_ERROR_INVALID_BINARY; |
| 288 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | stream.get() << "\""; |
| 292 | stream.get() << (color ? clr::green() : ""); |
| 293 | stream.get() << string; |
| 294 | stream.get() << (color ? clr::reset() : ""); |
| 295 | stream.get() << "\""; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 296 | position->index += stringOperandCount; |
| 297 | } break; |
| 298 | case SPV_OPERAND_TYPE_CAPABILITY: |
| 299 | case SPV_OPERAND_TYPE_SOURCE_LANGUAGE: |
| 300 | case SPV_OPERAND_TYPE_EXECUTION_MODEL: |
| 301 | case SPV_OPERAND_TYPE_ADDRESSING_MODEL: |
| 302 | case SPV_OPERAND_TYPE_MEMORY_MODEL: |
| 303 | case SPV_OPERAND_TYPE_EXECUTION_MODE: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 304 | case SPV_OPERAND_TYPE_OPTIONAL_EXECUTION_MODE: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 305 | case SPV_OPERAND_TYPE_STORAGE_CLASS: |
| 306 | case SPV_OPERAND_TYPE_DIMENSIONALITY: |
| 307 | case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE: |
| 308 | case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 309 | case SPV_OPERAND_TYPE_FP_ROUNDING_MODE: |
| 310 | case SPV_OPERAND_TYPE_LINKAGE_TYPE: |
| 311 | case SPV_OPERAND_TYPE_ACCESS_QUALIFIER: |
| 312 | case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE: |
| 313 | case SPV_OPERAND_TYPE_DECORATION: |
| 314 | case SPV_OPERAND_TYPE_BUILT_IN: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 315 | case SPV_OPERAND_TYPE_GROUP_OPERATION: |
| 316 | case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS: |
David Neto | 4799482 | 2015-08-27 13:11:01 -0400 | [diff] [blame] | 317 | case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 318 | spv_operand_desc entry; |
Lei Zhang | 97afd5c | 2015-09-14 15:26:12 -0400 | [diff] [blame] | 319 | if (spvOperandTableValueLookup(operandTable, type, |
| 320 | spvFixWord(words[0], endian), &entry)) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 321 | DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " operand '" |
Lei Zhang | 97afd5c | 2015-09-14 15:26:12 -0400 | [diff] [blame] | 322 | << words[0] << "'."; |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 323 | return SPV_ERROR_INVALID_TEXT; // TODO(dneto): Surely this is invalid binary. |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 324 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 325 | stream.get() << entry->name; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 326 | // Prepare to accept operands to this operand, if needed. |
| 327 | spvPrependOperandTypes(entry->operandTypes, pExpectedOperands); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 328 | position->index++; |
| 329 | } break; |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 330 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
| 331 | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
| 332 | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
| 333 | case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
| 334 | case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
| 335 | case SPV_OPERAND_TYPE_SELECTION_CONTROL: { |
| 336 | // This operand is a mask. |
| 337 | // Scan it from least significant bit to most significant bit. For each |
| 338 | // set bit, emit the name of that bit and prepare to parse its operands, |
| 339 | // if any. |
| 340 | uint32_t remaining_word = spvFixWord(words[0], endian); |
| 341 | uint32_t mask; |
| 342 | int num_emitted = 0; |
| 343 | for (mask = 1; remaining_word; mask <<= 1) { |
| 344 | if (remaining_word & mask) { |
| 345 | remaining_word ^= mask; |
| 346 | spv_operand_desc entry; |
| 347 | if (spvOperandTableValueLookup(operandTable, type, mask, &entry)) { |
| 348 | DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " operand '" |
| 349 | << words[0] << "'."; |
| 350 | return SPV_ERROR_INVALID_BINARY; |
| 351 | } |
| 352 | if (num_emitted) stream.get() << "|"; |
| 353 | stream.get() << entry->name; |
| 354 | num_emitted++; |
| 355 | } |
| 356 | } |
| 357 | if (!num_emitted) { |
| 358 | // An operand value of 0 was provided, so represent it by the name |
| 359 | // of the 0 value. In many cases, that's "None". |
| 360 | spv_operand_desc entry; |
| 361 | if (SPV_SUCCESS == |
| 362 | spvOperandTableValueLookup(operandTable, type, 0, &entry)) { |
| 363 | stream.get() << entry->name; |
| 364 | // Prepare for its operands, if any. |
| 365 | spvPrependOperandTypes(entry->operandTypes, pExpectedOperands); |
| 366 | } |
| 367 | } |
| 368 | // Prepare for subsequent operands, if any. |
| 369 | // Scan from MSB to LSB since we can only prepend operands to a pattern. |
| 370 | remaining_word = spvFixWord(words[0], endian); |
| 371 | for (mask = (1u << 31); remaining_word; mask >>= 1) { |
| 372 | if (remaining_word & mask) { |
| 373 | remaining_word ^= mask; |
| 374 | spv_operand_desc entry; |
| 375 | if (SPV_SUCCESS == |
| 376 | spvOperandTableValueLookup(operandTable, type, mask, &entry)) { |
| 377 | spvPrependOperandTypes(entry->operandTypes, pExpectedOperands); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | position->index++; |
| 382 | } break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 383 | default: { |
| 384 | DIAGNOSTIC << "Invalid binary operand '" << type << "'"; |
| 385 | return SPV_ERROR_INVALID_BINARY; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return SPV_SUCCESS; |
| 390 | } |
| 391 | |
| 392 | spv_result_t spvBinaryDecodeOpcode( |
| 393 | spv_instruction_t *pInst, const spv_endianness_t endian, |
| 394 | const uint32_t options, const spv_opcode_table opcodeTable, |
| 395 | const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, |
Lei Zhang | 29e667e | 2015-09-11 11:01:59 -0400 | [diff] [blame] | 396 | spv_assembly_syntax_format_t format, out_stream &stream, |
| 397 | spv_position position, spv_diagnostic *pDiagnostic) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 398 | if (!pInst || !position) return SPV_ERROR_INVALID_POINTER; |
| 399 | if (!opcodeTable || !operandTable || !extInstTable) |
| 400 | return SPV_ERROR_INVALID_TABLE; |
| 401 | if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 402 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 403 | spv_position_t instructionStart = *position; |
| 404 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 405 | uint16_t wordCount; |
| 406 | Op opcode; |
| 407 | spvOpcodeSplit(spvFixWord(pInst->words[0], endian), &wordCount, &opcode); |
| 408 | |
| 409 | spv_opcode_desc opcodeEntry; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 410 | if (spvOpcodeTableValueLookup(opcodeTable, opcode, &opcodeEntry)) { |
| 411 | DIAGNOSTIC << "Invalid Opcode '" << opcode << "'."; |
| 412 | return SPV_ERROR_INVALID_BINARY; |
| 413 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 414 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 415 | // See if there are enough required words. |
| 416 | // Some operands in the operand types are optional or could be zero length. |
| 417 | // The optional and zero length opeands must be at the end of the list. |
| 418 | if (opcodeEntry->numTypes > wordCount && |
| 419 | !spvOperandIsOptional(opcodeEntry->operandTypes[wordCount])) { |
| 420 | uint16_t numRequired; |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 421 | for (numRequired = 0; |
| 422 | numRequired < opcodeEntry->numTypes && |
| 423 | !spvOperandIsOptional(opcodeEntry->operandTypes[numRequired]); |
| 424 | numRequired++) |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 425 | ; |
| 426 | DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 427 | << " word count '" << wordCount << "', expected at least '" |
| 428 | << numRequired << "'."; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 429 | return SPV_ERROR_INVALID_BINARY; |
| 430 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 431 | |
Lei Zhang | 29e667e | 2015-09-11 11:01:59 -0400 | [diff] [blame] | 432 | const bool isAssigmentFormat = |
| 433 | SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format; |
| 434 | |
| 435 | // For Canonical Assembly Format, all words are written to stream in order. |
| 436 | // For Assignment Assembly Format, <result-id> and the equal sign are written |
| 437 | // to stream first, while the rest are written to no_result_id_stream. After |
| 438 | // processing all words, all words in no_result_id_stream are transcribed to |
| 439 | // stream. |
| 440 | |
Lei Zhang | 8a37520 | 2015-08-24 15:52:26 -0400 | [diff] [blame] | 441 | std::stringstream no_result_id_strstream; |
| 442 | out_stream no_result_id_stream(no_result_id_strstream); |
Lei Zhang | 29e667e | 2015-09-11 11:01:59 -0400 | [diff] [blame] | 443 | (isAssigmentFormat ? no_result_id_stream.get() : stream.get()) |
| 444 | << "Op" << opcodeEntry->name; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 445 | |
Lei Zhang | 29e667e | 2015-09-11 11:01:59 -0400 | [diff] [blame] | 446 | const int16_t result_id_index = spvOpcodeResultIdIndex(opcodeEntry); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 447 | position->index++; |
| 448 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 449 | // Maintains the ordered list of expected operand types. |
| 450 | // For many instructions we only need the {numTypes, operandTypes} |
| 451 | // entries in opcodeEntry. However, sometimes we need to modify |
| 452 | // the list as we parse the operands. This occurs when an operand |
| 453 | // has its own logical operands (such as the LocalSize operand for |
| 454 | // ExecutionMode), or for extended instructions that may have their |
| 455 | // own operands depending on the selected extended instruction. |
| 456 | spv_operand_pattern_t expectedOperands( |
| 457 | opcodeEntry->operandTypes, |
| 458 | opcodeEntry->operandTypes + opcodeEntry->numTypes); |
| 459 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 460 | for (uint16_t index = 1; index < wordCount; ++index) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 461 | const uint64_t currentPosIndex = position->index; |
Lei Zhang | 29e667e | 2015-09-11 11:01:59 -0400 | [diff] [blame] | 462 | const bool currentIsResultId = result_id_index == index - 1; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 463 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 464 | if (expectedOperands.empty()) { |
| 465 | DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name |
| 466 | << " starting at word " << instructionStart.index |
| 467 | << ": expected no more operands after " << index |
| 468 | << " words, but word count is " << wordCount << "."; |
| 469 | return SPV_ERROR_INVALID_BINARY; |
| 470 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 471 | |
| 472 | spv_operand_type_t type = spvTakeFirstMatchableOperand(&expectedOperands); |
| 473 | |
Lei Zhang | 29e667e | 2015-09-11 11:01:59 -0400 | [diff] [blame] | 474 | if (isAssigmentFormat) { |
| 475 | if (!currentIsResultId) no_result_id_stream.get() << " "; |
| 476 | } else { |
| 477 | stream.get() << " "; |
| 478 | } |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 479 | |
| 480 | uint16_t numWords = 1; |
| 481 | if (type == SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER) { |
| 482 | // Make sure this is the last operand for this instruction. |
| 483 | if (expectedOperands.empty()) { |
| 484 | numWords = wordCount - index; |
| 485 | } else { |
| 486 | // TODO(antiagainst): This may not be an error. The exact design has not |
| 487 | // been settled yet. |
| 488 | DIAGNOSTIC << "Multiple word literal numbers can only appear as the " |
| 489 | "last operand of an instruction."; |
| 490 | return SPV_ERROR_INVALID_BINARY; |
| 491 | } |
| 492 | } |
| 493 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 494 | if (spvBinaryDecodeOperand( |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 495 | opcodeEntry->opcode, type, pInst->words + index, numWords, endian, |
| 496 | options, operandTable, extInstTable, &expectedOperands, |
| 497 | &pInst->extInstType, |
Lei Zhang | 29e667e | 2015-09-11 11:01:59 -0400 | [diff] [blame] | 498 | (isAssigmentFormat && !currentIsResultId ? no_result_id_stream |
| 499 | : stream), |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 500 | position, pDiagnostic)) { |
| 501 | DIAGNOSTIC << "UNEXPLAINED ERROR"; |
| 502 | return SPV_ERROR_INVALID_BINARY; |
| 503 | } |
Lei Zhang | 29e667e | 2015-09-11 11:01:59 -0400 | [diff] [blame] | 504 | if (isAssigmentFormat && currentIsResultId) stream.get() << " = "; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 505 | index += (uint16_t)(position->index - currentPosIndex - 1); |
| 506 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 507 | // TODO(dneto): There's an opportunity for a more informative message. |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 508 | if (!expectedOperands.empty() && |
| 509 | !spvOperandIsOptional(expectedOperands.front())) { |
| 510 | DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name |
| 511 | << " starting at word " << instructionStart.index |
| 512 | << ": expected more operands after " << wordCount << " words."; |
| 513 | return SPV_ERROR_INVALID_BINARY; |
| 514 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 515 | |
Lei Zhang | 8a37520 | 2015-08-24 15:52:26 -0400 | [diff] [blame] | 516 | stream.get() << no_result_id_strstream.str(); |
| 517 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 518 | return SPV_SUCCESS; |
| 519 | } |
| 520 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 521 | spv_result_t spvBinaryToText(uint32_t *code, const uint64_t wordCount, |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 522 | const uint32_t options, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 523 | const spv_opcode_table opcodeTable, |
| 524 | const spv_operand_table operandTable, |
| 525 | const spv_ext_inst_table extInstTable, |
| 526 | spv_text *pText, spv_diagnostic *pDiagnostic) { |
Lei Zhang | 29e667e | 2015-09-11 11:01:59 -0400 | [diff] [blame] | 527 | return spvBinaryToTextWithFormat( |
| 528 | code, wordCount, options, opcodeTable, operandTable, extInstTable, |
| 529 | SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT, pText, pDiagnostic); |
| 530 | } |
| 531 | |
| 532 | spv_result_t spvBinaryToTextWithFormat( |
| 533 | uint32_t *code, const uint64_t wordCount, const uint32_t options, |
| 534 | const spv_opcode_table opcodeTable, const spv_operand_table operandTable, |
| 535 | const spv_ext_inst_table extInstTable, spv_assembly_syntax_format_t format, |
| 536 | spv_text *pText, spv_diagnostic *pDiagnostic) { |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 537 | spv_binary_t binary = {code, wordCount}; |
| 538 | |
Andrew Woloszyn | 4b4acde | 2015-09-10 10:28:22 -0400 | [diff] [blame] | 539 | spv_position_t position = {}; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 540 | if (!binary.code || !binary.wordCount) { |
| 541 | DIAGNOSTIC << "Binary stream is empty."; |
| 542 | return SPV_ERROR_INVALID_BINARY; |
| 543 | } |
| 544 | if (!opcodeTable || !operandTable || !extInstTable) |
| 545 | return SPV_ERROR_INVALID_TABLE; |
| 546 | if (pText && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options)) |
| 547 | return SPV_ERROR_INVALID_POINTER; |
| 548 | if (!pText && !spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options)) |
| 549 | return SPV_ERROR_INVALID_POINTER; |
| 550 | if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 551 | |
| 552 | spv_endianness_t endian; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 553 | if (spvBinaryEndianness(&binary, &endian)) { |
| 554 | DIAGNOSTIC << "Invalid SPIR-V magic number '" << std::hex << binary.code[0] |
| 555 | << "'."; |
| 556 | return SPV_ERROR_INVALID_BINARY; |
| 557 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 558 | |
| 559 | spv_header_t header; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 560 | if (spvBinaryHeaderGet(&binary, endian, &header)) { |
| 561 | DIAGNOSTIC << "Invalid SPIR-V header."; |
| 562 | return SPV_ERROR_INVALID_BINARY; |
| 563 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 564 | |
| 565 | bool print = spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options); |
| 566 | bool color = |
| 567 | print && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COLOR, options); |
| 568 | |
| 569 | std::stringstream sstream; |
| 570 | out_stream stream(sstream); |
| 571 | if (print) { |
| 572 | stream = out_stream(); |
| 573 | } |
| 574 | |
| 575 | if (color) { |
| 576 | stream.get() << clr::grey(); |
| 577 | } |
| 578 | stream.get() << "; SPIR-V\n" |
| 579 | << "; Version: " << header.version << "\n" |
| 580 | << "; Generator: " << spvGeneratorStr(header.generator) << "\n" |
| 581 | << "; Bound: " << header.bound << "\n" |
| 582 | << "; Schema: " << header.schema << "\n"; |
| 583 | if (color) { |
| 584 | stream.get() << clr::reset(); |
| 585 | } |
| 586 | |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 587 | const uint32_t *words = binary.code; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 588 | position.index = SPV_INDEX_INSTRUCTION; |
| 589 | spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE; |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 590 | while (position.index < binary.wordCount) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 591 | uint64_t index = position.index; |
| 592 | uint16_t wordCount; |
| 593 | Op opcode; |
| 594 | spvOpcodeSplit(spvFixWord(words[position.index], endian), &wordCount, |
| 595 | &opcode); |
| 596 | |
| 597 | spv_instruction_t inst = {}; |
| 598 | inst.extInstType = extInstType; |
| 599 | spvInstructionCopy(&words[position.index], opcode, wordCount, endian, |
| 600 | &inst); |
| 601 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 602 | if (spvBinaryDecodeOpcode(&inst, endian, options, opcodeTable, operandTable, |
| 603 | extInstTable, format, stream, &position, |
| 604 | pDiagnostic)) |
| 605 | return SPV_ERROR_INVALID_BINARY; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 606 | extInstType = inst.extInstType; |
| 607 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 608 | if ((index + wordCount) != position.index) { |
| 609 | DIAGNOSTIC << "Invalid word count."; |
| 610 | return SPV_ERROR_INVALID_BINARY; |
| 611 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 612 | |
| 613 | stream.get() << "\n"; |
| 614 | } |
| 615 | |
| 616 | if (!print) { |
| 617 | size_t length = sstream.str().size(); |
| 618 | char *str = new char[length + 1]; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 619 | if (!str) return SPV_ERROR_OUT_OF_MEMORY; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 620 | strncpy(str, sstream.str().c_str(), length + 1); |
| 621 | spv_text text = new spv_text_t(); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 622 | if (!text) return SPV_ERROR_OUT_OF_MEMORY; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 623 | text->str = str; |
| 624 | text->length = length; |
| 625 | *pText = text; |
| 626 | } |
| 627 | |
| 628 | return SPV_SUCCESS; |
| 629 | } |
| 630 | |
| 631 | void spvBinaryDestroy(spv_binary binary) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 632 | if (!binary) return; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 633 | if (binary->code) { |
| 634 | delete[] binary->code; |
| 635 | } |
| 636 | delete binary; |
| 637 | } |