blob: 5dfa11d0c58a7663aa1ab900cb447294efebc18a [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"
David Netob5dc8fc2015-10-06 16:22:00 -040031#include "instruction.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010032#include "opcode.h"
33#include "operand.h"
34
35#include <assert.h>
36#include <string.h>
37
38#include <sstream>
39
40// Binary API
41
42enum {
43 I32_ENDIAN_LITTLE = 0x03020100ul,
44 I32_ENDIAN_BIG = 0x00010203ul,
45};
46
47static const union {
48 unsigned char bytes[4];
49 uint32_t value;
50} o32_host_order = {{0, 1, 2, 3}};
51
52#define I32_ENDIAN_HOST (o32_host_order.value)
53
54spv_result_t spvBinaryEndianness(const spv_binary binary,
55 spv_endianness_t *pEndian) {
Lei Zhang40056702015-09-11 14:31:27 -040056 if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
57 if (!pEndian) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010058
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
Lei Zhangb41d1502015-09-14 15:22:23 -040087uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high,
88 const spv_endianness_t endian) {
89 return (uint64_t(spvFixWord(high, endian)) << 32) | spvFixWord(low, endian);
90}
91
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010092spv_result_t spvBinaryHeaderGet(const spv_binary binary,
93 const spv_endianness_t endian,
94 spv_header_t *pHeader) {
Lei Zhang40056702015-09-11 14:31:27 -040095 if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
96 if (!pHeader) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010097
98 // TODO: Validation checking?
99 pHeader->magic = spvFixWord(binary->code[SPV_INDEX_MAGIC_NUMBER], endian);
100 pHeader->version = spvFixWord(binary->code[SPV_INDEX_VERSION_NUMBER], endian);
101 pHeader->generator =
102 spvFixWord(binary->code[SPV_INDEX_GENERATOR_NUMBER], endian);
103 pHeader->bound = spvFixWord(binary->code[SPV_INDEX_BOUND], endian);
104 pHeader->schema = spvFixWord(binary->code[SPV_INDEX_SCHEMA], endian);
105 pHeader->instructions = &binary->code[SPV_INDEX_INSTRUCTION];
106
107 return SPV_SUCCESS;
108}
109
110spv_result_t spvBinaryHeaderSet(spv_binary_t *binary, const uint32_t bound) {
Lei Zhang40056702015-09-11 14:31:27 -0400111 if (!binary) return SPV_ERROR_INVALID_BINARY;
112 if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100113
114 binary->code[SPV_INDEX_MAGIC_NUMBER] = SPV_MAGIC_NUMBER;
115 binary->code[SPV_INDEX_VERSION_NUMBER] = SPV_VERSION_NUMBER;
Kenneth Benzie (Benie)81d7d492015-06-01 09:50:46 -0700116 binary->code[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100117 binary->code[SPV_INDEX_BOUND] = bound;
118 binary->code[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
119
120 return SPV_SUCCESS;
121}
122
David Neto78c3b432015-08-27 13:03:52 -0400123// TODO(dneto): This API is not powerful enough in the case that the
124// number and type of operands are not known until partway through parsing
125// the operation. This happens when enum operands might have different number
126// of operands, or with extended instructions.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100127spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
128 const uint16_t operandIndex,
129 const spv_opcode_desc opcodeEntry,
130 const spv_operand_table operandTable,
131 spv_operand_desc *pOperandEntry) {
132 spv_operand_type_t type;
David Neto78c3b432015-08-27 13:03:52 -0400133 if (operandIndex < opcodeEntry->numTypes) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100134 // NOTE: Do operand table lookup to set operandEntry if successful
135 uint16_t index = operandIndex - 1;
136 type = opcodeEntry->operandTypes[index];
137 spv_operand_desc entry = nullptr;
138 if (!spvOperandTableValueLookup(operandTable, type, word, &entry)) {
139 if (SPV_OPERAND_TYPE_NONE != entry->operandTypes[0]) {
140 *pOperandEntry = entry;
141 }
142 }
143 } else if (*pOperandEntry) {
144 // NOTE: Use specified operand entry operand type for this word
David Neto78c3b432015-08-27 13:03:52 -0400145 uint16_t index = operandIndex - opcodeEntry->numTypes;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100146 type = (*pOperandEntry)->operandTypes[index];
147 } else if (OpSwitch == opcodeEntry->opcode) {
148 // NOTE: OpSwitch is a special case which expects a list of paired extra
149 // operands
150 assert(0 &&
151 "This case is previously untested, remove this assert and ensure it "
152 "is behaving correctly!");
David Neto78c3b432015-08-27 13:03:52 -0400153 uint16_t lastIndex = opcodeEntry->numTypes - 1;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100154 uint16_t index = lastIndex + ((operandIndex - lastIndex) % 2);
155 type = opcodeEntry->operandTypes[index];
156 } else {
157 // NOTE: Default to last operand type in opcode entry
David Neto78c3b432015-08-27 13:03:52 -0400158 uint16_t index = opcodeEntry->numTypes - 1;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100159 type = opcodeEntry->operandTypes[index];
160 }
161 return type;
162}
163
164spv_result_t spvBinaryDecodeOperand(
165 const Op opcode, const spv_operand_type_t type, const uint32_t *words,
Lei Zhangb41d1502015-09-14 15:22:23 -0400166 uint16_t numWords, const spv_endianness_t endian, const uint32_t options,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100167 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
David Neto78c3b432015-08-27 13:03:52 -0400168 spv_operand_pattern_t *pExpectedOperands, spv_ext_inst_type_t *pExtInstType,
169 out_stream &stream, spv_position position, spv_diagnostic *pDiagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -0400170 if (!words || !position) return SPV_ERROR_INVALID_POINTER;
171 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100172
173 bool print = spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options);
174 bool color =
175 print && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COLOR, options);
176
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100177 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400178 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Neto78c3b432015-08-27 13:03:52 -0400179 case SPV_OPERAND_TYPE_ID:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400180 case SPV_OPERAND_TYPE_TYPE_ID:
David Netob14a7272015-09-25 13:56:09 -0400181 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
David Neto78c3b432015-08-27 13:03:52 -0400182 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netob14a7272015-09-25 13:56:09 -0400183 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
184 case SPV_OPERAND_TYPE_RESULT_ID: {
David Neto78c3b432015-08-27 13:03:52 -0400185 if (color) {
Pyry Haulos26b3b002015-09-09 13:35:53 -0700186 if (type == SPV_OPERAND_TYPE_RESULT_ID) {
187 stream.get() << clr::blue();
188 } else {
189 stream.get() << clr::yellow();
190 }
David Neto78c3b432015-08-27 13:03:52 -0400191 }
Lei Zhang97afd5c2015-09-14 15:26:12 -0400192 stream.get() << "%" << spvFixWord(words[0], endian);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100193 stream.get() << ((color) ? clr::reset() : "");
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100194 position->index++;
195 } break;
Lei Zhang6483bd72015-10-14 17:02:39 -0400196 case SPV_OPERAND_TYPE_LITERAL_INTEGER: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100197 // NOTE: Special case for extended instruction use
198 if (OpExtInst == opcode) {
199 spv_ext_inst_desc extInst;
Lei Zhang40056702015-09-11 14:31:27 -0400200 if (spvExtInstTableValueLookup(extInstTable, *pExtInstType, words[0],
201 &extInst)) {
202 DIAGNOSTIC << "Invalid extended instruction '" << words[0] << "'.";
203 return SPV_ERROR_INVALID_BINARY;
204 }
David Neto78c3b432015-08-27 13:03:52 -0400205 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
Andrew Woloszyn0d350b52015-08-21 14:23:42 -0400206 stream.get() << (color ? clr::red() : "");
207 stream.get() << extInst->name;
208 stream.get() << (color ? clr::reset() : "");
Lei Zhang41bf0732015-09-14 12:26:15 -0400209 position->index++;
210 break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100211 }
Lei Zhang41bf0732015-09-14 12:26:15 -0400212 } // Fall through for the general case.
Lei Zhangb41d1502015-09-14 15:22:23 -0400213 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
Lei Zhang6483bd72015-10-14 17:02:39 -0400214 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER:
215 case SPV_OPERAND_TYPE_LITERAL_INTEGER_IN_OPTIONAL_TUPLE: {
Lei Zhang41bf0732015-09-14 12:26:15 -0400216 // TODO: Need to support multiple word literals
217 stream.get() << (color ? clr::red() : "");
Lei Zhangb41d1502015-09-14 15:22:23 -0400218 if (numWords > 2) {
219 DIAGNOSTIC << "Literal numbers larger than 64-bit not supported yet.";
220 return SPV_UNSUPPORTED;
221 } else if (numWords == 2) {
222 stream.get() << spvFixDoubleWord(words[0], words[1], endian);
223 position->index += 2;
224 } else {
225 stream.get() << spvFixWord(words[0], endian);
226 position->index++;
227 }
Lei Zhang41bf0732015-09-14 12:26:15 -0400228 stream.get() << (color ? clr::reset() : "");
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100229 } break;
David Neto78c3b432015-08-27 13:03:52 -0400230 case SPV_OPERAND_TYPE_LITERAL_STRING:
231 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang97afd5c2015-09-14 15:26:12 -0400232 const char *string = (const char *)words;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100233 uint64_t stringOperandCount = (strlen(string) / 4) + 1;
234
235 // NOTE: Special case for extended instruction import
236 if (OpExtInstImport == opcode) {
237 *pExtInstType = spvExtInstImportTypeGet(string);
Lei Zhang40056702015-09-11 14:31:27 -0400238 if (SPV_EXT_INST_TYPE_NONE == *pExtInstType) {
239 DIAGNOSTIC << "Invalid extended instruction import'" << string
240 << "'.";
241 return SPV_ERROR_INVALID_BINARY;
242 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100243 }
244
245 stream.get() << "\"";
246 stream.get() << (color ? clr::green() : "");
David Neto980b7cb2015-10-15 16:40:04 -0400247 for (const char* p = string; *p; ++p) {
248 if(*p == '"' || *p == '\\') {
Andrew Woloszyne59e6b72015-10-14 14:18:43 -0400249 stream.get() << '\\';
250 }
David Neto980b7cb2015-10-15 16:40:04 -0400251 stream.get() << *p;
Andrew Woloszyne59e6b72015-10-14 14:18:43 -0400252 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100253 stream.get() << (color ? clr::reset() : "");
254 stream.get() << "\"";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100255 position->index += stringOperandCount;
256 } break;
257 case SPV_OPERAND_TYPE_CAPABILITY:
258 case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
259 case SPV_OPERAND_TYPE_EXECUTION_MODEL:
260 case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
261 case SPV_OPERAND_TYPE_MEMORY_MODEL:
262 case SPV_OPERAND_TYPE_EXECUTION_MODE:
David Neto78c3b432015-08-27 13:03:52 -0400263 case SPV_OPERAND_TYPE_OPTIONAL_EXECUTION_MODE:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100264 case SPV_OPERAND_TYPE_STORAGE_CLASS:
265 case SPV_OPERAND_TYPE_DIMENSIONALITY:
266 case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
267 case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100268 case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
269 case SPV_OPERAND_TYPE_LINKAGE_TYPE:
270 case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
271 case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
272 case SPV_OPERAND_TYPE_DECORATION:
273 case SPV_OPERAND_TYPE_BUILT_IN:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100274 case SPV_OPERAND_TYPE_GROUP_OPERATION:
275 case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
David Neto47994822015-08-27 13:11:01 -0400276 case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100277 spv_operand_desc entry;
Lei Zhang97afd5c2015-09-14 15:26:12 -0400278 if (spvOperandTableValueLookup(operandTable, type,
279 spvFixWord(words[0], endian), &entry)) {
Lei Zhang40056702015-09-11 14:31:27 -0400280 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " operand '"
Lei Zhang97afd5c2015-09-14 15:26:12 -0400281 << words[0] << "'.";
David Neto619db262015-09-25 12:43:37 -0400282 return SPV_ERROR_INVALID_TEXT; // TODO(dneto): Surely this is invalid binary.
Lei Zhang40056702015-09-11 14:31:27 -0400283 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100284 stream.get() << entry->name;
David Neto78c3b432015-08-27 13:03:52 -0400285 // Prepare to accept operands to this operand, if needed.
286 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100287 position->index++;
288 } break;
David Neto619db262015-09-25 12:43:37 -0400289 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
290 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
291 case SPV_OPERAND_TYPE_LOOP_CONTROL:
292 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
293 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
294 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
295 // This operand is a mask.
296 // Scan it from least significant bit to most significant bit. For each
297 // set bit, emit the name of that bit and prepare to parse its operands,
298 // if any.
299 uint32_t remaining_word = spvFixWord(words[0], endian);
300 uint32_t mask;
301 int num_emitted = 0;
302 for (mask = 1; remaining_word; mask <<= 1) {
303 if (remaining_word & mask) {
304 remaining_word ^= mask;
305 spv_operand_desc entry;
306 if (spvOperandTableValueLookup(operandTable, type, mask, &entry)) {
307 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " operand '"
308 << words[0] << "'.";
309 return SPV_ERROR_INVALID_BINARY;
310 }
311 if (num_emitted) stream.get() << "|";
312 stream.get() << entry->name;
313 num_emitted++;
314 }
315 }
316 if (!num_emitted) {
317 // An operand value of 0 was provided, so represent it by the name
318 // of the 0 value. In many cases, that's "None".
319 spv_operand_desc entry;
320 if (SPV_SUCCESS ==
321 spvOperandTableValueLookup(operandTable, type, 0, &entry)) {
322 stream.get() << entry->name;
323 // Prepare for its operands, if any.
324 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
325 }
326 }
327 // Prepare for subsequent operands, if any.
328 // Scan from MSB to LSB since we can only prepend operands to a pattern.
329 remaining_word = spvFixWord(words[0], endian);
330 for (mask = (1u << 31); remaining_word; mask >>= 1) {
331 if (remaining_word & mask) {
332 remaining_word ^= mask;
333 spv_operand_desc entry;
334 if (SPV_SUCCESS ==
335 spvOperandTableValueLookup(operandTable, type, mask, &entry)) {
336 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
337 }
338 }
339 }
340 position->index++;
341 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100342 default: {
343 DIAGNOSTIC << "Invalid binary operand '" << type << "'";
344 return SPV_ERROR_INVALID_BINARY;
345 }
346 }
347
348 return SPV_SUCCESS;
349}
350
351spv_result_t spvBinaryDecodeOpcode(
352 spv_instruction_t *pInst, const spv_endianness_t endian,
353 const uint32_t options, const spv_opcode_table opcodeTable,
354 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Lei Zhang29e667e2015-09-11 11:01:59 -0400355 spv_assembly_syntax_format_t format, out_stream &stream,
356 spv_position position, spv_diagnostic *pDiagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -0400357 if (!pInst || !position) return SPV_ERROR_INVALID_POINTER;
358 if (!opcodeTable || !operandTable || !extInstTable)
359 return SPV_ERROR_INVALID_TABLE;
360 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100361
David Neto78c3b432015-08-27 13:03:52 -0400362 spv_position_t instructionStart = *position;
363
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100364 uint16_t wordCount;
365 Op opcode;
366 spvOpcodeSplit(spvFixWord(pInst->words[0], endian), &wordCount, &opcode);
367
368 spv_opcode_desc opcodeEntry;
Lei Zhang40056702015-09-11 14:31:27 -0400369 if (spvOpcodeTableValueLookup(opcodeTable, opcode, &opcodeEntry)) {
370 DIAGNOSTIC << "Invalid Opcode '" << opcode << "'.";
371 return SPV_ERROR_INVALID_BINARY;
372 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100373
David Neto78c3b432015-08-27 13:03:52 -0400374 // See if there are enough required words.
375 // Some operands in the operand types are optional or could be zero length.
376 // The optional and zero length opeands must be at the end of the list.
377 if (opcodeEntry->numTypes > wordCount &&
378 !spvOperandIsOptional(opcodeEntry->operandTypes[wordCount])) {
379 uint16_t numRequired;
Lei Zhange78a7c12015-09-10 17:07:21 -0400380 for (numRequired = 0;
381 numRequired < opcodeEntry->numTypes &&
382 !spvOperandIsOptional(opcodeEntry->operandTypes[numRequired]);
383 numRequired++)
David Neto78c3b432015-08-27 13:03:52 -0400384 ;
385 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
Lei Zhange78a7c12015-09-10 17:07:21 -0400386 << " word count '" << wordCount << "', expected at least '"
387 << numRequired << "'.";
David Neto78c3b432015-08-27 13:03:52 -0400388 return SPV_ERROR_INVALID_BINARY;
389 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100390
Lei Zhang29e667e2015-09-11 11:01:59 -0400391 const bool isAssigmentFormat =
392 SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format;
393
394 // For Canonical Assembly Format, all words are written to stream in order.
395 // For Assignment Assembly Format, <result-id> and the equal sign are written
396 // to stream first, while the rest are written to no_result_id_stream. After
397 // processing all words, all words in no_result_id_stream are transcribed to
398 // stream.
399
Lei Zhang8a375202015-08-24 15:52:26 -0400400 std::stringstream no_result_id_strstream;
401 out_stream no_result_id_stream(no_result_id_strstream);
Lei Zhang29e667e2015-09-11 11:01:59 -0400402 (isAssigmentFormat ? no_result_id_stream.get() : stream.get())
403 << "Op" << opcodeEntry->name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100404
Lei Zhang29e667e2015-09-11 11:01:59 -0400405 const int16_t result_id_index = spvOpcodeResultIdIndex(opcodeEntry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100406 position->index++;
407
David Neto78c3b432015-08-27 13:03:52 -0400408 // Maintains the ordered list of expected operand types.
409 // For many instructions we only need the {numTypes, operandTypes}
410 // entries in opcodeEntry. However, sometimes we need to modify
411 // the list as we parse the operands. This occurs when an operand
412 // has its own logical operands (such as the LocalSize operand for
413 // ExecutionMode), or for extended instructions that may have their
414 // own operands depending on the selected extended instruction.
415 spv_operand_pattern_t expectedOperands(
416 opcodeEntry->operandTypes,
417 opcodeEntry->operandTypes + opcodeEntry->numTypes);
418
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100419 for (uint16_t index = 1; index < wordCount; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100420 const uint64_t currentPosIndex = position->index;
Lei Zhang29e667e2015-09-11 11:01:59 -0400421 const bool currentIsResultId = result_id_index == index - 1;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100422
Lei Zhang40056702015-09-11 14:31:27 -0400423 if (expectedOperands.empty()) {
424 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
425 << " starting at word " << instructionStart.index
426 << ": expected no more operands after " << index
427 << " words, but word count is " << wordCount << ".";
428 return SPV_ERROR_INVALID_BINARY;
429 }
David Neto78c3b432015-08-27 13:03:52 -0400430
431 spv_operand_type_t type = spvTakeFirstMatchableOperand(&expectedOperands);
432
Lei Zhang29e667e2015-09-11 11:01:59 -0400433 if (isAssigmentFormat) {
434 if (!currentIsResultId) no_result_id_stream.get() << " ";
435 } else {
436 stream.get() << " ";
437 }
Lei Zhangb41d1502015-09-14 15:22:23 -0400438
439 uint16_t numWords = 1;
440 if (type == SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER) {
441 // Make sure this is the last operand for this instruction.
442 if (expectedOperands.empty()) {
443 numWords = wordCount - index;
444 } else {
445 // TODO(antiagainst): This may not be an error. The exact design has not
446 // been settled yet.
447 DIAGNOSTIC << "Multiple word literal numbers can only appear as the "
448 "last operand of an instruction.";
449 return SPV_ERROR_INVALID_BINARY;
450 }
451 }
452
Lei Zhang40056702015-09-11 14:31:27 -0400453 if (spvBinaryDecodeOperand(
David Netob5dc8fc2015-10-06 16:22:00 -0400454 opcodeEntry->opcode, type, &pInst->words[index], numWords, endian,
Lei Zhangb41d1502015-09-14 15:22:23 -0400455 options, operandTable, extInstTable, &expectedOperands,
456 &pInst->extInstType,
Lei Zhang29e667e2015-09-11 11:01:59 -0400457 (isAssigmentFormat && !currentIsResultId ? no_result_id_stream
458 : stream),
Lei Zhang40056702015-09-11 14:31:27 -0400459 position, pDiagnostic)) {
460 DIAGNOSTIC << "UNEXPLAINED ERROR";
461 return SPV_ERROR_INVALID_BINARY;
462 }
Lei Zhang29e667e2015-09-11 11:01:59 -0400463 if (isAssigmentFormat && currentIsResultId) stream.get() << " = ";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100464 index += (uint16_t)(position->index - currentPosIndex - 1);
465 }
David Neto78c3b432015-08-27 13:03:52 -0400466 // TODO(dneto): There's an opportunity for a more informative message.
Lei Zhang40056702015-09-11 14:31:27 -0400467 if (!expectedOperands.empty() &&
468 !spvOperandIsOptional(expectedOperands.front())) {
469 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
470 << " starting at word " << instructionStart.index
471 << ": expected more operands after " << wordCount << " words.";
472 return SPV_ERROR_INVALID_BINARY;
473 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100474
Lei Zhang8a375202015-08-24 15:52:26 -0400475 stream.get() << no_result_id_strstream.str();
476
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100477 return SPV_SUCCESS;
478}
479
Lei Zhange78a7c12015-09-10 17:07:21 -0400480spv_result_t spvBinaryToText(uint32_t *code, const uint64_t wordCount,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400481 const uint32_t options,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100482 const spv_opcode_table opcodeTable,
483 const spv_operand_table operandTable,
484 const spv_ext_inst_table extInstTable,
485 spv_text *pText, spv_diagnostic *pDiagnostic) {
Lei Zhang29e667e2015-09-11 11:01:59 -0400486 return spvBinaryToTextWithFormat(
487 code, wordCount, options, opcodeTable, operandTable, extInstTable,
488 SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT, pText, pDiagnostic);
489}
490
491spv_result_t spvBinaryToTextWithFormat(
492 uint32_t *code, const uint64_t wordCount, const uint32_t options,
493 const spv_opcode_table opcodeTable, const spv_operand_table operandTable,
494 const spv_ext_inst_table extInstTable, spv_assembly_syntax_format_t format,
495 spv_text *pText, spv_diagnostic *pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400496 spv_binary_t binary = {code, wordCount};
497
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400498 spv_position_t position = {};
Lei Zhang40056702015-09-11 14:31:27 -0400499 if (!binary.code || !binary.wordCount) {
500 DIAGNOSTIC << "Binary stream is empty.";
501 return SPV_ERROR_INVALID_BINARY;
502 }
503 if (!opcodeTable || !operandTable || !extInstTable)
504 return SPV_ERROR_INVALID_TABLE;
505 if (pText && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options))
506 return SPV_ERROR_INVALID_POINTER;
507 if (!pText && !spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options))
508 return SPV_ERROR_INVALID_POINTER;
509 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100510
511 spv_endianness_t endian;
Lei Zhang40056702015-09-11 14:31:27 -0400512 if (spvBinaryEndianness(&binary, &endian)) {
513 DIAGNOSTIC << "Invalid SPIR-V magic number '" << std::hex << binary.code[0]
514 << "'.";
515 return SPV_ERROR_INVALID_BINARY;
516 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100517
518 spv_header_t header;
Lei Zhang40056702015-09-11 14:31:27 -0400519 if (spvBinaryHeaderGet(&binary, endian, &header)) {
520 DIAGNOSTIC << "Invalid SPIR-V header.";
521 return SPV_ERROR_INVALID_BINARY;
522 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100523
524 bool print = spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options);
525 bool color =
526 print && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COLOR, options);
527
528 std::stringstream sstream;
529 out_stream stream(sstream);
530 if (print) {
531 stream = out_stream();
532 }
533
534 if (color) {
535 stream.get() << clr::grey();
536 }
537 stream.get() << "; SPIR-V\n"
538 << "; Version: " << header.version << "\n"
539 << "; Generator: " << spvGeneratorStr(header.generator) << "\n"
540 << "; Bound: " << header.bound << "\n"
541 << "; Schema: " << header.schema << "\n";
542 if (color) {
543 stream.get() << clr::reset();
544 }
545
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400546 const uint32_t *words = binary.code;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100547 position.index = SPV_INDEX_INSTRUCTION;
548 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400549 while (position.index < binary.wordCount) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100550 uint64_t index = position.index;
551 uint16_t wordCount;
552 Op opcode;
553 spvOpcodeSplit(spvFixWord(words[position.index], endian), &wordCount,
554 &opcode);
555
556 spv_instruction_t inst = {};
557 inst.extInstType = extInstType;
558 spvInstructionCopy(&words[position.index], opcode, wordCount, endian,
559 &inst);
560
Lei Zhang40056702015-09-11 14:31:27 -0400561 if (spvBinaryDecodeOpcode(&inst, endian, options, opcodeTable, operandTable,
562 extInstTable, format, stream, &position,
563 pDiagnostic))
564 return SPV_ERROR_INVALID_BINARY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100565 extInstType = inst.extInstType;
566
Lei Zhang40056702015-09-11 14:31:27 -0400567 if ((index + wordCount) != position.index) {
568 DIAGNOSTIC << "Invalid word count.";
569 return SPV_ERROR_INVALID_BINARY;
570 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100571
572 stream.get() << "\n";
573 }
574
575 if (!print) {
576 size_t length = sstream.str().size();
577 char *str = new char[length + 1];
Lei Zhang40056702015-09-11 14:31:27 -0400578 if (!str) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100579 strncpy(str, sstream.str().c_str(), length + 1);
580 spv_text text = new spv_text_t();
Lei Zhang40056702015-09-11 14:31:27 -0400581 if (!text) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100582 text->str = str;
583 text->length = length;
584 *pText = text;
585 }
586
587 return SPV_SUCCESS;
588}
589
590void spvBinaryDestroy(spv_binary binary) {
Lei Zhang40056702015-09-11 14:31:27 -0400591 if (!binary) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100592 if (binary->code) {
593 delete[] binary->code;
594 }
595 delete binary;
596}