blob: 007d96663e10b5125b1e5bd33a5857ce6352f0e3 [file] [log] [blame]
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01001// 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
41enum {
42 I32_ENDIAN_LITTLE = 0x03020100ul,
43 I32_ENDIAN_BIG = 0x00010203ul,
44};
45
46static 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
53spv_result_t spvBinaryEndianness(const spv_binary binary,
54 spv_endianness_t *pEndian) {
55 spvCheck(!binary->code || !binary->wordCount,
56 return SPV_ERROR_INVALID_BINARY);
57 spvCheck(!pEndian, return SPV_ERROR_INVALID_POINTER);
58
59 uint8_t bytes[4];
60 memcpy(bytes, binary->code, sizeof(uint32_t));
61
62 if (0x03 == bytes[0] && 0x02 == bytes[1] && 0x23 == bytes[2] &&
63 0x07 == bytes[3]) {
64 *pEndian = SPV_ENDIANNESS_LITTLE;
65 return SPV_SUCCESS;
66 }
67
68 if (0x07 == bytes[0] && 0x23 == bytes[1] && 0x02 == bytes[2] &&
69 0x03 == bytes[3]) {
70 *pEndian = SPV_ENDIANNESS_BIG;
71 return SPV_SUCCESS;
72 }
73
74 return SPV_ERROR_INVALID_BINARY;
75}
76
77uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian) {
78 if ((SPV_ENDIANNESS_LITTLE == endian && I32_ENDIAN_HOST == I32_ENDIAN_BIG) ||
79 (SPV_ENDIANNESS_BIG == endian && I32_ENDIAN_HOST == I32_ENDIAN_LITTLE)) {
80 return (word & 0x000000ff) << 24 | (word & 0x0000ff00) << 8 |
81 (word & 0x00ff0000) >> 8 | (word & 0xff000000) >> 24;
82 }
83
84 return word;
85}
86
87spv_result_t spvBinaryHeaderGet(const spv_binary binary,
88 const spv_endianness_t endian,
89 spv_header_t *pHeader) {
90 spvCheck(!binary->code || !binary->wordCount,
91 return SPV_ERROR_INVALID_BINARY);
92 spvCheck(!pHeader, return SPV_ERROR_INVALID_POINTER);
93
94 // TODO: Validation checking?
95 pHeader->magic = spvFixWord(binary->code[SPV_INDEX_MAGIC_NUMBER], endian);
96 pHeader->version = spvFixWord(binary->code[SPV_INDEX_VERSION_NUMBER], endian);
97 pHeader->generator =
98 spvFixWord(binary->code[SPV_INDEX_GENERATOR_NUMBER], endian);
99 pHeader->bound = spvFixWord(binary->code[SPV_INDEX_BOUND], endian);
100 pHeader->schema = spvFixWord(binary->code[SPV_INDEX_SCHEMA], endian);
101 pHeader->instructions = &binary->code[SPV_INDEX_INSTRUCTION];
102
103 return SPV_SUCCESS;
104}
105
106spv_result_t spvBinaryHeaderSet(spv_binary_t *binary, const uint32_t bound) {
107 spvCheck(!binary, return SPV_ERROR_INVALID_BINARY);
108 spvCheck(!binary->code || !binary->wordCount,
109 return SPV_ERROR_INVALID_BINARY);
110
111 binary->code[SPV_INDEX_MAGIC_NUMBER] = SPV_MAGIC_NUMBER;
112 binary->code[SPV_INDEX_VERSION_NUMBER] = SPV_VERSION_NUMBER;
Kenneth Benzie (Benie)81d7d492015-06-01 09:50:46 -0700113 binary->code[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100114 binary->code[SPV_INDEX_BOUND] = bound;
115 binary->code[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
116
117 return SPV_SUCCESS;
118}
119
120spv_result_t spvBinaryEncodeU32(const uint32_t value, spv_instruction_t *pInst,
121 const spv_position position,
122 spv_diagnostic *pDiagnostic) {
123 spvCheck(pInst->wordCount + 1 > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX,
124 DIAGNOSTIC << "Instruction word count '"
125 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "' exceeded.";
126 return SPV_ERROR_INVALID_TEXT);
127
128 pInst->words[pInst->wordCount++] = (uint32_t)value;
129 return SPV_SUCCESS;
130}
131
132spv_result_t spvBinaryEncodeU64(const uint64_t value, spv_instruction_t *pInst,
133 const spv_position position,
134 spv_diagnostic *pDiagnostic) {
135 spvCheck(pInst->wordCount + 2 > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX,
136 DIAGNOSTIC << "Instruction word count '"
137 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "' exceeded.";
138 return SPV_ERROR_INVALID_TEXT);
139
140 uint32_t low = (uint32_t)(0x00000000ffffffff & value);
141 uint32_t high = (uint32_t)((0xffffffff00000000 & value) >> 32);
142 pInst->words[pInst->wordCount++] = low;
143 pInst->words[pInst->wordCount++] = high;
144 return SPV_SUCCESS;
145}
146
147spv_result_t spvBinaryEncodeString(const char *str, spv_instruction_t *pInst,
148 const spv_position position,
149 spv_diagnostic *pDiagnostic) {
150 size_t length = strlen(str);
151 size_t wordCount = (length / 4) + 1;
152 spvCheck((sizeof(uint32_t) * pInst->wordCount) + length >
153 sizeof(uint32_t) * SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX,
154 DIAGNOSTIC << "Instruction word count '"
155 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "'exceeded.";
156 return SPV_ERROR_INVALID_TEXT);
157
158 char *dest = (char *)&pInst->words[pInst->wordCount];
159 strncpy(dest, str, length);
160 pInst->wordCount += (uint16_t)wordCount;
161
162 return SPV_SUCCESS;
163}
164
David Neto78c3b432015-08-27 13:03:52 -0400165// TODO(dneto): This API is not powerful enough in the case that the
166// number and type of operands are not known until partway through parsing
167// the operation. This happens when enum operands might have different number
168// of operands, or with extended instructions.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100169spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
170 const uint16_t operandIndex,
171 const spv_opcode_desc opcodeEntry,
172 const spv_operand_table operandTable,
173 spv_operand_desc *pOperandEntry) {
174 spv_operand_type_t type;
David Neto78c3b432015-08-27 13:03:52 -0400175 if (operandIndex < opcodeEntry->numTypes) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100176 // NOTE: Do operand table lookup to set operandEntry if successful
177 uint16_t index = operandIndex - 1;
178 type = opcodeEntry->operandTypes[index];
179 spv_operand_desc entry = nullptr;
180 if (!spvOperandTableValueLookup(operandTable, type, word, &entry)) {
181 if (SPV_OPERAND_TYPE_NONE != entry->operandTypes[0]) {
182 *pOperandEntry = entry;
183 }
184 }
185 } else if (*pOperandEntry) {
186 // NOTE: Use specified operand entry operand type for this word
David Neto78c3b432015-08-27 13:03:52 -0400187 uint16_t index = operandIndex - opcodeEntry->numTypes;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100188 type = (*pOperandEntry)->operandTypes[index];
189 } else if (OpSwitch == opcodeEntry->opcode) {
190 // NOTE: OpSwitch is a special case which expects a list of paired extra
191 // operands
192 assert(0 &&
193 "This case is previously untested, remove this assert and ensure it "
194 "is behaving correctly!");
David Neto78c3b432015-08-27 13:03:52 -0400195 uint16_t lastIndex = opcodeEntry->numTypes - 1;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100196 uint16_t index = lastIndex + ((operandIndex - lastIndex) % 2);
197 type = opcodeEntry->operandTypes[index];
198 } else {
199 // NOTE: Default to last operand type in opcode entry
David Neto78c3b432015-08-27 13:03:52 -0400200 uint16_t index = opcodeEntry->numTypes - 1;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100201 type = opcodeEntry->operandTypes[index];
202 }
203 return type;
204}
205
206spv_result_t spvBinaryDecodeOperand(
207 const Op opcode, const spv_operand_type_t type, const uint32_t *words,
208 const spv_endianness_t endian, const uint32_t options,
209 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
David Neto78c3b432015-08-27 13:03:52 -0400210 spv_operand_pattern_t *pExpectedOperands, spv_ext_inst_type_t *pExtInstType,
211 out_stream &stream, spv_position position, spv_diagnostic *pDiagnostic) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100212 spvCheck(!words || !position, return SPV_ERROR_INVALID_POINTER);
213 spvCheck(!pDiagnostic, return SPV_ERROR_INVALID_DIAGNOSTIC);
214
215 bool print = spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options);
216 bool color =
217 print && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COLOR, options);
218
219 uint64_t index = 0;
220 switch (type) {
David Neto78c3b432015-08-27 13:03:52 -0400221 case SPV_OPERAND_TYPE_ID:
222 case SPV_OPERAND_TYPE_RESULT_ID:
223 case SPV_OPERAND_TYPE_OPTIONAL_ID:
224 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE: {
225 if (color) {
Pyry Haulos26b3b002015-09-09 13:35:53 -0700226 if (type == SPV_OPERAND_TYPE_RESULT_ID) {
227 stream.get() << clr::blue();
228 } else {
229 stream.get() << clr::yellow();
230 }
David Neto78c3b432015-08-27 13:03:52 -0400231 }
Lei Zhangabafd5e2015-08-21 11:52:29 -0400232 stream.get() << "%" << spvFixWord(words[index], endian);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100233 stream.get() << ((color) ? clr::reset() : "");
234 index++;
235 position->index++;
236 } break;
David Neto78c3b432015-08-27 13:03:52 -0400237 case SPV_OPERAND_TYPE_LITERAL:
238 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL:
239 case SPV_OPERAND_TYPE_LITERAL_IN_OPTIONAL_TUPLE: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100240 // TODO: Need to support multiple word literals
241 stream.get() << (color ? clr::red() : "");
242 stream.get() << spvFixWord(words[index], endian);
243 stream.get() << (color ? clr::reset() : "");
244 index++;
245 position->index++;
246 } break;
247 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
248 // NOTE: Special case for extended instruction use
249 if (OpExtInst == opcode) {
250 spv_ext_inst_desc extInst;
251 spvCheck(spvExtInstTableValueLookup(extInstTable, *pExtInstType,
252 words[0], &extInst),
253 DIAGNOSTIC << "Invalid extended instruction '" << words[0]
254 << "'.";
255 return SPV_ERROR_INVALID_BINARY);
David Neto78c3b432015-08-27 13:03:52 -0400256 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
Andrew Woloszyn0d350b52015-08-21 14:23:42 -0400257 stream.get() << (color ? clr::red() : "");
258 stream.get() << extInst->name;
259 stream.get() << (color ? clr::reset() : "");
260 } else {
261 stream.get() << (color ? clr::red() : "");
262 stream.get() << spvFixWord(words[index], endian);
263 stream.get() << (color ? clr::reset() : "");
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100264 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100265 index++;
266 position->index++;
267 } break;
David Neto78c3b432015-08-27 13:03:52 -0400268 case SPV_OPERAND_TYPE_LITERAL_STRING:
269 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100270 const char *string = (const char *)&words[index];
271 uint64_t stringOperandCount = (strlen(string) / 4) + 1;
272
273 // NOTE: Special case for extended instruction import
274 if (OpExtInstImport == opcode) {
275 *pExtInstType = spvExtInstImportTypeGet(string);
276 spvCheck(SPV_EXT_INST_TYPE_NONE == *pExtInstType,
277 DIAGNOSTIC << "Invalid extended instruction import'" << string
278 << "'.";
279 return SPV_ERROR_INVALID_BINARY);
280 }
281
282 stream.get() << "\"";
283 stream.get() << (color ? clr::green() : "");
284 stream.get() << string;
285 stream.get() << (color ? clr::reset() : "");
286 stream.get() << "\"";
287 index += stringOperandCount;
288 position->index += stringOperandCount;
289 } break;
290 case SPV_OPERAND_TYPE_CAPABILITY:
291 case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
292 case SPV_OPERAND_TYPE_EXECUTION_MODEL:
293 case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
294 case SPV_OPERAND_TYPE_MEMORY_MODEL:
295 case SPV_OPERAND_TYPE_EXECUTION_MODE:
David Neto78c3b432015-08-27 13:03:52 -0400296 case SPV_OPERAND_TYPE_OPTIONAL_EXECUTION_MODE:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100297 case SPV_OPERAND_TYPE_STORAGE_CLASS:
298 case SPV_OPERAND_TYPE_DIMENSIONALITY:
299 case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
300 case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
301 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
302 case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
303 case SPV_OPERAND_TYPE_LINKAGE_TYPE:
304 case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
305 case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
306 case SPV_OPERAND_TYPE_DECORATION:
307 case SPV_OPERAND_TYPE_BUILT_IN:
308 case SPV_OPERAND_TYPE_SELECTION_CONTROL:
309 case SPV_OPERAND_TYPE_LOOP_CONTROL:
310 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
311 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
David Neto78c3b432015-08-27 13:03:52 -0400312 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100313 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
314 case SPV_OPERAND_TYPE_GROUP_OPERATION:
315 case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
David Neto47994822015-08-27 13:11:01 -0400316 case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100317 spv_operand_desc entry;
318 spvCheck(
319 spvOperandTableValueLookup(operandTable, type,
320 spvFixWord(words[index], endian), &entry),
321 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " operand '"
322 << words[index] << "'.";
323 return SPV_ERROR_INVALID_TEXT);
324 stream.get() << entry->name;
David Neto78c3b432015-08-27 13:03:52 -0400325 // Prepare to accept operands to this operand, if needed.
326 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100327 index++;
328 position->index++;
329 } break;
330 default: {
331 DIAGNOSTIC << "Invalid binary operand '" << type << "'";
332 return SPV_ERROR_INVALID_BINARY;
333 }
334 }
335
336 return SPV_SUCCESS;
337}
338
339spv_result_t spvBinaryDecodeOpcode(
340 spv_instruction_t *pInst, const spv_endianness_t endian,
341 const uint32_t options, const spv_opcode_table opcodeTable,
342 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
343 out_stream &stream, spv_position position, spv_diagnostic *pDiagnostic) {
344 spvCheck(!pInst || !position, return SPV_ERROR_INVALID_POINTER);
345 spvCheck(!opcodeTable || !operandTable || !extInstTable,
346 return SPV_ERROR_INVALID_TABLE);
347 spvCheck(!pDiagnostic, return SPV_ERROR_INVALID_DIAGNOSTIC);
348
David Neto78c3b432015-08-27 13:03:52 -0400349 spv_position_t instructionStart = *position;
350
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100351 uint16_t wordCount;
352 Op opcode;
353 spvOpcodeSplit(spvFixWord(pInst->words[0], endian), &wordCount, &opcode);
354
355 spv_opcode_desc opcodeEntry;
356 spvCheck(spvOpcodeTableValueLookup(opcodeTable, opcode, &opcodeEntry),
357 DIAGNOSTIC << "Invalid Opcode '" << opcode << "'.";
358 return SPV_ERROR_INVALID_BINARY);
359
David Neto78c3b432015-08-27 13:03:52 -0400360 // See if there are enough required words.
361 // Some operands in the operand types are optional or could be zero length.
362 // The optional and zero length opeands must be at the end of the list.
363 if (opcodeEntry->numTypes > wordCount &&
364 !spvOperandIsOptional(opcodeEntry->operandTypes[wordCount])) {
365 uint16_t numRequired;
366 for (numRequired = 0; numRequired < opcodeEntry->numTypes &&
367 !spvOperandIsOptional(opcodeEntry->operandTypes[numRequired]) ; numRequired++ )
368 ;
369 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
370 << " word count '" << wordCount
371 << "', expected at least '" << numRequired
372 << "'.";
373 return SPV_ERROR_INVALID_BINARY;
374 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100375
Lei Zhang8a375202015-08-24 15:52:26 -0400376 std::stringstream no_result_id_strstream;
377 out_stream no_result_id_stream(no_result_id_strstream);
378 const int16_t result_id_index = spvOpcodeResultIdIndex(opcodeEntry);
379 no_result_id_stream.get() << "Op" << opcodeEntry->name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100380
381 position->index++;
382
David Neto78c3b432015-08-27 13:03:52 -0400383 // Maintains the ordered list of expected operand types.
384 // For many instructions we only need the {numTypes, operandTypes}
385 // entries in opcodeEntry. However, sometimes we need to modify
386 // the list as we parse the operands. This occurs when an operand
387 // has its own logical operands (such as the LocalSize operand for
388 // ExecutionMode), or for extended instructions that may have their
389 // own operands depending on the selected extended instruction.
390 spv_operand_pattern_t expectedOperands(
391 opcodeEntry->operandTypes,
392 opcodeEntry->operandTypes + opcodeEntry->numTypes);
393
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100394 for (uint16_t index = 1; index < wordCount; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100395 const uint64_t currentPosIndex = position->index;
396
David Neto78c3b432015-08-27 13:03:52 -0400397 spvCheck(expectedOperands.empty(),
398 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
399 << " starting at word " << instructionStart.index
400 << ": "
401 << " expected no more operands after " << index
402 << " words, but word count is " << wordCount << ".";
403 return SPV_ERROR_INVALID_BINARY;);
404
405 spv_operand_type_t type = spvTakeFirstMatchableOperand(&expectedOperands);
406
Lei Zhang8a375202015-08-24 15:52:26 -0400407 if (result_id_index != index - 1) no_result_id_strstream << " ";
David Neto78c3b432015-08-27 13:03:52 -0400408 spvCheck(
409 spvBinaryDecodeOperand(
410 opcodeEntry->opcode, type, pInst->words + index, endian, options,
411 operandTable, extInstTable, &expectedOperands, &pInst->extInstType,
412 (result_id_index == index - 1 ? stream : no_result_id_stream),
413 position, pDiagnostic),
414 DIAGNOSTIC << "UNEXPLAINED ERROR";
415 return SPV_ERROR_INVALID_BINARY);
Lei Zhang8a375202015-08-24 15:52:26 -0400416 if (result_id_index == index - 1) stream.get() << " = ";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100417 index += (uint16_t)(position->index - currentPosIndex - 1);
418 }
David Neto78c3b432015-08-27 13:03:52 -0400419 // TODO(dneto): There's an opportunity for a more informative message.
420 spvCheck(!expectedOperands.empty() &&
421 !spvOperandIsOptional(expectedOperands.front()),
422 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
423 << " starting at word " << instructionStart.index << ": "
424 << " expected more operands after " << wordCount
425 << " words.";
426 return SPV_ERROR_INVALID_BINARY;);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100427
Lei Zhang8a375202015-08-24 15:52:26 -0400428 stream.get() << no_result_id_strstream.str();
429
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100430 return SPV_SUCCESS;
431}
432
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400433spv_result_t spvBinaryToText(uint32_t* code,
434 const uint64_t wordCount,
435 const uint32_t options,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100436 const spv_opcode_table opcodeTable,
437 const spv_operand_table operandTable,
438 const spv_ext_inst_table extInstTable,
439 spv_text *pText, spv_diagnostic *pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400440 spv_binary_t binary = {code, wordCount};
441
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400442 spv_position_t position = {};
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400443 spvCheck(!binary.code || !binary.wordCount,
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400444 DIAGNOSTIC << "Binary stream is empty.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100445 return SPV_ERROR_INVALID_BINARY);
446 spvCheck(!opcodeTable || !operandTable || !extInstTable,
447 return SPV_ERROR_INVALID_TABLE);
448 spvCheck(pText && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options),
449 return SPV_ERROR_INVALID_POINTER);
450 spvCheck(!pText && !spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options),
451 return SPV_ERROR_INVALID_POINTER);
452 spvCheck(!pDiagnostic, return SPV_ERROR_INVALID_DIAGNOSTIC);
453
454 spv_endianness_t endian;
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400455 spvCheck(spvBinaryEndianness(&binary, &endian),
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100456 DIAGNOSTIC << "Invalid SPIR-V magic number '" << std::hex
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400457 << binary.code[0] << "'.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100458 return SPV_ERROR_INVALID_BINARY);
459
460 spv_header_t header;
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400461 spvCheck(spvBinaryHeaderGet(&binary, endian, &header),
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100462 DIAGNOSTIC << "Invalid SPIR-V header.";
463 return SPV_ERROR_INVALID_BINARY);
464
465 bool print = spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options);
466 bool color =
467 print && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COLOR, options);
468
469 std::stringstream sstream;
470 out_stream stream(sstream);
471 if (print) {
472 stream = out_stream();
473 }
474
475 if (color) {
476 stream.get() << clr::grey();
477 }
478 stream.get() << "; SPIR-V\n"
479 << "; Version: " << header.version << "\n"
480 << "; Generator: " << spvGeneratorStr(header.generator) << "\n"
481 << "; Bound: " << header.bound << "\n"
482 << "; Schema: " << header.schema << "\n";
483 if (color) {
484 stream.get() << clr::reset();
485 }
486
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400487 const uint32_t *words = binary.code;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100488 position.index = SPV_INDEX_INSTRUCTION;
489 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400490 while (position.index < binary.wordCount) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100491 uint64_t index = position.index;
492 uint16_t wordCount;
493 Op opcode;
494 spvOpcodeSplit(spvFixWord(words[position.index], endian), &wordCount,
495 &opcode);
496
497 spv_instruction_t inst = {};
498 inst.extInstType = extInstType;
499 spvInstructionCopy(&words[position.index], opcode, wordCount, endian,
500 &inst);
501
502 spvCheck(
503 spvBinaryDecodeOpcode(&inst, endian, options, opcodeTable, operandTable,
504 extInstTable, stream, &position, pDiagnostic),
505 return SPV_ERROR_INVALID_BINARY);
506 extInstType = inst.extInstType;
507
508 spvCheck((index + wordCount) != position.index,
509 DIAGNOSTIC << "Invalid word count.";
510 return SPV_ERROR_INVALID_BINARY);
511
512 stream.get() << "\n";
513 }
514
515 if (!print) {
516 size_t length = sstream.str().size();
517 char *str = new char[length + 1];
518 spvCheck(!str, return SPV_ERROR_OUT_OF_MEMORY);
519 strncpy(str, sstream.str().c_str(), length + 1);
520 spv_text text = new spv_text_t();
521 spvCheck(!text, return SPV_ERROR_OUT_OF_MEMORY);
522 text->str = str;
523 text->length = length;
524 *pText = text;
525 }
526
527 return SPV_SUCCESS;
528}
529
530void spvBinaryDestroy(spv_binary binary) {
531 spvCheck(!binary, return );
532 if (binary->code) {
533 delete[] binary->code;
534 }
535 delete binary;
536}