blob: e2f0135997e495a4717db83cc3e8cc8f5a20e1d3 [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) {
Lei Zhang40056702015-09-11 14:31:27 -040055 if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
56 if (!pEndian) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010057
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
76uint32_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 Zhangb41d1502015-09-14 15:22:23 -040086uint64_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)83e5a292015-05-22 18:26:19 +010091spv_result_t spvBinaryHeaderGet(const spv_binary binary,
92 const spv_endianness_t endian,
93 spv_header_t *pHeader) {
Lei Zhang40056702015-09-11 14:31:27 -040094 if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
95 if (!pHeader) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010096
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
109spv_result_t spvBinaryHeaderSet(spv_binary_t *binary, const uint32_t bound) {
Lei Zhang40056702015-09-11 14:31:27 -0400110 if (!binary) return SPV_ERROR_INVALID_BINARY;
111 if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100112
113 binary->code[SPV_INDEX_MAGIC_NUMBER] = SPV_MAGIC_NUMBER;
114 binary->code[SPV_INDEX_VERSION_NUMBER] = SPV_VERSION_NUMBER;
Kenneth Benzie (Benie)81d7d492015-06-01 09:50:46 -0700115 binary->code[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100116 binary->code[SPV_INDEX_BOUND] = bound;
117 binary->code[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
118
119 return SPV_SUCCESS;
120}
121
David Neto78c3b432015-08-27 13:03:52 -0400122// TODO(dneto): This API is not powerful enough in the case that the
123// number and type of operands are not known until partway through parsing
124// the operation. This happens when enum operands might have different number
125// of operands, or with extended instructions.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100126spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
127 const uint16_t operandIndex,
128 const spv_opcode_desc opcodeEntry,
129 const spv_operand_table operandTable,
130 spv_operand_desc *pOperandEntry) {
131 spv_operand_type_t type;
David Neto78c3b432015-08-27 13:03:52 -0400132 if (operandIndex < opcodeEntry->numTypes) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100133 // NOTE: Do operand table lookup to set operandEntry if successful
134 uint16_t index = operandIndex - 1;
135 type = opcodeEntry->operandTypes[index];
136 spv_operand_desc entry = nullptr;
137 if (!spvOperandTableValueLookup(operandTable, type, word, &entry)) {
138 if (SPV_OPERAND_TYPE_NONE != entry->operandTypes[0]) {
139 *pOperandEntry = entry;
140 }
141 }
142 } else if (*pOperandEntry) {
143 // NOTE: Use specified operand entry operand type for this word
David Neto78c3b432015-08-27 13:03:52 -0400144 uint16_t index = operandIndex - opcodeEntry->numTypes;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100145 type = (*pOperandEntry)->operandTypes[index];
146 } else if (OpSwitch == opcodeEntry->opcode) {
147 // NOTE: OpSwitch is a special case which expects a list of paired extra
148 // operands
149 assert(0 &&
150 "This case is previously untested, remove this assert and ensure it "
151 "is behaving correctly!");
David Neto78c3b432015-08-27 13:03:52 -0400152 uint16_t lastIndex = opcodeEntry->numTypes - 1;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100153 uint16_t index = lastIndex + ((operandIndex - lastIndex) % 2);
154 type = opcodeEntry->operandTypes[index];
155 } else {
156 // NOTE: Default to last operand type in opcode entry
David Neto78c3b432015-08-27 13:03:52 -0400157 uint16_t index = opcodeEntry->numTypes - 1;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100158 type = opcodeEntry->operandTypes[index];
159 }
160 return type;
161}
162
163spv_result_t spvBinaryDecodeOperand(
164 const Op opcode, const spv_operand_type_t type, const uint32_t *words,
Lei Zhangb41d1502015-09-14 15:22:23 -0400165 uint16_t numWords, const spv_endianness_t endian, const uint32_t options,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100166 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
David Neto78c3b432015-08-27 13:03:52 -0400167 spv_operand_pattern_t *pExpectedOperands, spv_ext_inst_type_t *pExtInstType,
168 out_stream &stream, spv_position position, spv_diagnostic *pDiagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -0400169 if (!words || !position) return SPV_ERROR_INVALID_POINTER;
170 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100171
172 bool print = spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options);
173 bool color =
174 print && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COLOR, options);
175
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100176 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400177 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Neto78c3b432015-08-27 13:03:52 -0400178 case SPV_OPERAND_TYPE_ID:
David Netob14a7272015-09-25 13:56:09 -0400179 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
David Neto78c3b432015-08-27 13:03:52 -0400180 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netob14a7272015-09-25 13:56:09 -0400181 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
182 case SPV_OPERAND_TYPE_RESULT_ID: {
David Neto78c3b432015-08-27 13:03:52 -0400183 if (color) {
Pyry Haulos26b3b002015-09-09 13:35:53 -0700184 if (type == SPV_OPERAND_TYPE_RESULT_ID) {
185 stream.get() << clr::blue();
186 } else {
187 stream.get() << clr::yellow();
188 }
David Neto78c3b432015-08-27 13:03:52 -0400189 }
Lei Zhang97afd5c2015-09-14 15:26:12 -0400190 stream.get() << "%" << spvFixWord(words[0], endian);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100191 stream.get() << ((color) ? clr::reset() : "");
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100192 position->index++;
193 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100194 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
195 // NOTE: Special case for extended instruction use
196 if (OpExtInst == opcode) {
197 spv_ext_inst_desc extInst;
Lei Zhang40056702015-09-11 14:31:27 -0400198 if (spvExtInstTableValueLookup(extInstTable, *pExtInstType, words[0],
199 &extInst)) {
200 DIAGNOSTIC << "Invalid extended instruction '" << words[0] << "'.";
201 return SPV_ERROR_INVALID_BINARY;
202 }
David Neto78c3b432015-08-27 13:03:52 -0400203 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
Andrew Woloszyn0d350b52015-08-21 14:23:42 -0400204 stream.get() << (color ? clr::red() : "");
205 stream.get() << extInst->name;
206 stream.get() << (color ? clr::reset() : "");
Lei Zhang41bf0732015-09-14 12:26:15 -0400207 position->index++;
208 break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100209 }
Lei Zhang41bf0732015-09-14 12:26:15 -0400210 } // Fall through for the general case.
Lei Zhangb41d1502015-09-14 15:22:23 -0400211 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
David Neto561dc4e2015-09-25 14:23:29 -0400212 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
213 case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE: {
Lei Zhang41bf0732015-09-14 12:26:15 -0400214 // TODO: Need to support multiple word literals
215 stream.get() << (color ? clr::red() : "");
Lei Zhangb41d1502015-09-14 15:22:23 -0400216 if (numWords > 2) {
217 DIAGNOSTIC << "Literal numbers larger than 64-bit not supported yet.";
218 return SPV_UNSUPPORTED;
219 } else if (numWords == 2) {
220 stream.get() << spvFixDoubleWord(words[0], words[1], endian);
221 position->index += 2;
222 } else {
223 stream.get() << spvFixWord(words[0], endian);
224 position->index++;
225 }
Lei Zhang41bf0732015-09-14 12:26:15 -0400226 stream.get() << (color ? clr::reset() : "");
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100227 } break;
David Neto78c3b432015-08-27 13:03:52 -0400228 case SPV_OPERAND_TYPE_LITERAL_STRING:
229 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang97afd5c2015-09-14 15:26:12 -0400230 const char *string = (const char *)words;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100231 uint64_t stringOperandCount = (strlen(string) / 4) + 1;
232
233 // NOTE: Special case for extended instruction import
234 if (OpExtInstImport == opcode) {
235 *pExtInstType = spvExtInstImportTypeGet(string);
Lei Zhang40056702015-09-11 14:31:27 -0400236 if (SPV_EXT_INST_TYPE_NONE == *pExtInstType) {
237 DIAGNOSTIC << "Invalid extended instruction import'" << string
238 << "'.";
239 return SPV_ERROR_INVALID_BINARY;
240 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100241 }
242
243 stream.get() << "\"";
244 stream.get() << (color ? clr::green() : "");
245 stream.get() << string;
246 stream.get() << (color ? clr::reset() : "");
247 stream.get() << "\"";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100248 position->index += stringOperandCount;
249 } break;
250 case SPV_OPERAND_TYPE_CAPABILITY:
251 case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
252 case SPV_OPERAND_TYPE_EXECUTION_MODEL:
253 case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
254 case SPV_OPERAND_TYPE_MEMORY_MODEL:
255 case SPV_OPERAND_TYPE_EXECUTION_MODE:
David Neto78c3b432015-08-27 13:03:52 -0400256 case SPV_OPERAND_TYPE_OPTIONAL_EXECUTION_MODE:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100257 case SPV_OPERAND_TYPE_STORAGE_CLASS:
258 case SPV_OPERAND_TYPE_DIMENSIONALITY:
259 case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
260 case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100261 case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
262 case SPV_OPERAND_TYPE_LINKAGE_TYPE:
263 case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
264 case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
265 case SPV_OPERAND_TYPE_DECORATION:
266 case SPV_OPERAND_TYPE_BUILT_IN:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100267 case SPV_OPERAND_TYPE_GROUP_OPERATION:
268 case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
David Neto47994822015-08-27 13:11:01 -0400269 case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100270 spv_operand_desc entry;
Lei Zhang97afd5c2015-09-14 15:26:12 -0400271 if (spvOperandTableValueLookup(operandTable, type,
272 spvFixWord(words[0], endian), &entry)) {
Lei Zhang40056702015-09-11 14:31:27 -0400273 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " operand '"
Lei Zhang97afd5c2015-09-14 15:26:12 -0400274 << words[0] << "'.";
David Neto619db262015-09-25 12:43:37 -0400275 return SPV_ERROR_INVALID_TEXT; // TODO(dneto): Surely this is invalid binary.
Lei Zhang40056702015-09-11 14:31:27 -0400276 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100277 stream.get() << entry->name;
David Neto78c3b432015-08-27 13:03:52 -0400278 // Prepare to accept operands to this operand, if needed.
279 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100280 position->index++;
281 } break;
David Neto619db262015-09-25 12:43:37 -0400282 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
283 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
284 case SPV_OPERAND_TYPE_LOOP_CONTROL:
285 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
286 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
287 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
288 // This operand is a mask.
289 // Scan it from least significant bit to most significant bit. For each
290 // set bit, emit the name of that bit and prepare to parse its operands,
291 // if any.
292 uint32_t remaining_word = spvFixWord(words[0], endian);
293 uint32_t mask;
294 int num_emitted = 0;
295 for (mask = 1; remaining_word; mask <<= 1) {
296 if (remaining_word & mask) {
297 remaining_word ^= mask;
298 spv_operand_desc entry;
299 if (spvOperandTableValueLookup(operandTable, type, mask, &entry)) {
300 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " operand '"
301 << words[0] << "'.";
302 return SPV_ERROR_INVALID_BINARY;
303 }
304 if (num_emitted) stream.get() << "|";
305 stream.get() << entry->name;
306 num_emitted++;
307 }
308 }
309 if (!num_emitted) {
310 // An operand value of 0 was provided, so represent it by the name
311 // of the 0 value. In many cases, that's "None".
312 spv_operand_desc entry;
313 if (SPV_SUCCESS ==
314 spvOperandTableValueLookup(operandTable, type, 0, &entry)) {
315 stream.get() << entry->name;
316 // Prepare for its operands, if any.
317 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
318 }
319 }
320 // Prepare for subsequent operands, if any.
321 // Scan from MSB to LSB since we can only prepend operands to a pattern.
322 remaining_word = spvFixWord(words[0], endian);
323 for (mask = (1u << 31); remaining_word; mask >>= 1) {
324 if (remaining_word & mask) {
325 remaining_word ^= mask;
326 spv_operand_desc entry;
327 if (SPV_SUCCESS ==
328 spvOperandTableValueLookup(operandTable, type, mask, &entry)) {
329 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
330 }
331 }
332 }
333 position->index++;
334 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100335 default: {
336 DIAGNOSTIC << "Invalid binary operand '" << type << "'";
337 return SPV_ERROR_INVALID_BINARY;
338 }
339 }
340
341 return SPV_SUCCESS;
342}
343
344spv_result_t spvBinaryDecodeOpcode(
345 spv_instruction_t *pInst, const spv_endianness_t endian,
346 const uint32_t options, const spv_opcode_table opcodeTable,
347 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Lei Zhang29e667e2015-09-11 11:01:59 -0400348 spv_assembly_syntax_format_t format, out_stream &stream,
349 spv_position position, spv_diagnostic *pDiagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -0400350 if (!pInst || !position) return SPV_ERROR_INVALID_POINTER;
351 if (!opcodeTable || !operandTable || !extInstTable)
352 return SPV_ERROR_INVALID_TABLE;
353 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100354
David Neto78c3b432015-08-27 13:03:52 -0400355 spv_position_t instructionStart = *position;
356
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100357 uint16_t wordCount;
358 Op opcode;
359 spvOpcodeSplit(spvFixWord(pInst->words[0], endian), &wordCount, &opcode);
360
361 spv_opcode_desc opcodeEntry;
Lei Zhang40056702015-09-11 14:31:27 -0400362 if (spvOpcodeTableValueLookup(opcodeTable, opcode, &opcodeEntry)) {
363 DIAGNOSTIC << "Invalid Opcode '" << opcode << "'.";
364 return SPV_ERROR_INVALID_BINARY;
365 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100366
David Neto78c3b432015-08-27 13:03:52 -0400367 // See if there are enough required words.
368 // Some operands in the operand types are optional or could be zero length.
369 // The optional and zero length opeands must be at the end of the list.
370 if (opcodeEntry->numTypes > wordCount &&
371 !spvOperandIsOptional(opcodeEntry->operandTypes[wordCount])) {
372 uint16_t numRequired;
Lei Zhange78a7c12015-09-10 17:07:21 -0400373 for (numRequired = 0;
374 numRequired < opcodeEntry->numTypes &&
375 !spvOperandIsOptional(opcodeEntry->operandTypes[numRequired]);
376 numRequired++)
David Neto78c3b432015-08-27 13:03:52 -0400377 ;
378 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
Lei Zhange78a7c12015-09-10 17:07:21 -0400379 << " word count '" << wordCount << "', expected at least '"
380 << numRequired << "'.";
David Neto78c3b432015-08-27 13:03:52 -0400381 return SPV_ERROR_INVALID_BINARY;
382 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100383
Lei Zhang29e667e2015-09-11 11:01:59 -0400384 const bool isAssigmentFormat =
385 SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format;
386
387 // For Canonical Assembly Format, all words are written to stream in order.
388 // For Assignment Assembly Format, <result-id> and the equal sign are written
389 // to stream first, while the rest are written to no_result_id_stream. After
390 // processing all words, all words in no_result_id_stream are transcribed to
391 // stream.
392
Lei Zhang8a375202015-08-24 15:52:26 -0400393 std::stringstream no_result_id_strstream;
394 out_stream no_result_id_stream(no_result_id_strstream);
Lei Zhang29e667e2015-09-11 11:01:59 -0400395 (isAssigmentFormat ? no_result_id_stream.get() : stream.get())
396 << "Op" << opcodeEntry->name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100397
Lei Zhang29e667e2015-09-11 11:01:59 -0400398 const int16_t result_id_index = spvOpcodeResultIdIndex(opcodeEntry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100399 position->index++;
400
David Neto78c3b432015-08-27 13:03:52 -0400401 // Maintains the ordered list of expected operand types.
402 // For many instructions we only need the {numTypes, operandTypes}
403 // entries in opcodeEntry. However, sometimes we need to modify
404 // the list as we parse the operands. This occurs when an operand
405 // has its own logical operands (such as the LocalSize operand for
406 // ExecutionMode), or for extended instructions that may have their
407 // own operands depending on the selected extended instruction.
408 spv_operand_pattern_t expectedOperands(
409 opcodeEntry->operandTypes,
410 opcodeEntry->operandTypes + opcodeEntry->numTypes);
411
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100412 for (uint16_t index = 1; index < wordCount; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100413 const uint64_t currentPosIndex = position->index;
Lei Zhang29e667e2015-09-11 11:01:59 -0400414 const bool currentIsResultId = result_id_index == index - 1;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100415
Lei Zhang40056702015-09-11 14:31:27 -0400416 if (expectedOperands.empty()) {
417 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
418 << " starting at word " << instructionStart.index
419 << ": expected no more operands after " << index
420 << " words, but word count is " << wordCount << ".";
421 return SPV_ERROR_INVALID_BINARY;
422 }
David Neto78c3b432015-08-27 13:03:52 -0400423
424 spv_operand_type_t type = spvTakeFirstMatchableOperand(&expectedOperands);
425
Lei Zhang29e667e2015-09-11 11:01:59 -0400426 if (isAssigmentFormat) {
427 if (!currentIsResultId) no_result_id_stream.get() << " ";
428 } else {
429 stream.get() << " ";
430 }
Lei Zhangb41d1502015-09-14 15:22:23 -0400431
432 uint16_t numWords = 1;
433 if (type == SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER) {
434 // Make sure this is the last operand for this instruction.
435 if (expectedOperands.empty()) {
436 numWords = wordCount - index;
437 } else {
438 // TODO(antiagainst): This may not be an error. The exact design has not
439 // been settled yet.
440 DIAGNOSTIC << "Multiple word literal numbers can only appear as the "
441 "last operand of an instruction.";
442 return SPV_ERROR_INVALID_BINARY;
443 }
444 }
445
Lei Zhang40056702015-09-11 14:31:27 -0400446 if (spvBinaryDecodeOperand(
Lei Zhangb41d1502015-09-14 15:22:23 -0400447 opcodeEntry->opcode, type, pInst->words + index, numWords, endian,
448 options, operandTable, extInstTable, &expectedOperands,
449 &pInst->extInstType,
Lei Zhang29e667e2015-09-11 11:01:59 -0400450 (isAssigmentFormat && !currentIsResultId ? no_result_id_stream
451 : stream),
Lei Zhang40056702015-09-11 14:31:27 -0400452 position, pDiagnostic)) {
453 DIAGNOSTIC << "UNEXPLAINED ERROR";
454 return SPV_ERROR_INVALID_BINARY;
455 }
Lei Zhang29e667e2015-09-11 11:01:59 -0400456 if (isAssigmentFormat && currentIsResultId) stream.get() << " = ";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100457 index += (uint16_t)(position->index - currentPosIndex - 1);
458 }
David Neto78c3b432015-08-27 13:03:52 -0400459 // TODO(dneto): There's an opportunity for a more informative message.
Lei Zhang40056702015-09-11 14:31:27 -0400460 if (!expectedOperands.empty() &&
461 !spvOperandIsOptional(expectedOperands.front())) {
462 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
463 << " starting at word " << instructionStart.index
464 << ": expected more operands after " << wordCount << " words.";
465 return SPV_ERROR_INVALID_BINARY;
466 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100467
Lei Zhang8a375202015-08-24 15:52:26 -0400468 stream.get() << no_result_id_strstream.str();
469
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100470 return SPV_SUCCESS;
471}
472
Lei Zhange78a7c12015-09-10 17:07:21 -0400473spv_result_t spvBinaryToText(uint32_t *code, const uint64_t wordCount,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400474 const uint32_t options,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100475 const spv_opcode_table opcodeTable,
476 const spv_operand_table operandTable,
477 const spv_ext_inst_table extInstTable,
478 spv_text *pText, spv_diagnostic *pDiagnostic) {
Lei Zhang29e667e2015-09-11 11:01:59 -0400479 return spvBinaryToTextWithFormat(
480 code, wordCount, options, opcodeTable, operandTable, extInstTable,
481 SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT, pText, pDiagnostic);
482}
483
484spv_result_t spvBinaryToTextWithFormat(
485 uint32_t *code, const uint64_t wordCount, const uint32_t options,
486 const spv_opcode_table opcodeTable, const spv_operand_table operandTable,
487 const spv_ext_inst_table extInstTable, spv_assembly_syntax_format_t format,
488 spv_text *pText, spv_diagnostic *pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400489 spv_binary_t binary = {code, wordCount};
490
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400491 spv_position_t position = {};
Lei Zhang40056702015-09-11 14:31:27 -0400492 if (!binary.code || !binary.wordCount) {
493 DIAGNOSTIC << "Binary stream is empty.";
494 return SPV_ERROR_INVALID_BINARY;
495 }
496 if (!opcodeTable || !operandTable || !extInstTable)
497 return SPV_ERROR_INVALID_TABLE;
498 if (pText && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options))
499 return SPV_ERROR_INVALID_POINTER;
500 if (!pText && !spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options))
501 return SPV_ERROR_INVALID_POINTER;
502 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100503
504 spv_endianness_t endian;
Lei Zhang40056702015-09-11 14:31:27 -0400505 if (spvBinaryEndianness(&binary, &endian)) {
506 DIAGNOSTIC << "Invalid SPIR-V magic number '" << std::hex << binary.code[0]
507 << "'.";
508 return SPV_ERROR_INVALID_BINARY;
509 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100510
511 spv_header_t header;
Lei Zhang40056702015-09-11 14:31:27 -0400512 if (spvBinaryHeaderGet(&binary, endian, &header)) {
513 DIAGNOSTIC << "Invalid SPIR-V header.";
514 return SPV_ERROR_INVALID_BINARY;
515 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100516
517 bool print = spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options);
518 bool color =
519 print && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COLOR, options);
520
521 std::stringstream sstream;
522 out_stream stream(sstream);
523 if (print) {
524 stream = out_stream();
525 }
526
527 if (color) {
528 stream.get() << clr::grey();
529 }
530 stream.get() << "; SPIR-V\n"
531 << "; Version: " << header.version << "\n"
532 << "; Generator: " << spvGeneratorStr(header.generator) << "\n"
533 << "; Bound: " << header.bound << "\n"
534 << "; Schema: " << header.schema << "\n";
535 if (color) {
536 stream.get() << clr::reset();
537 }
538
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400539 const uint32_t *words = binary.code;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100540 position.index = SPV_INDEX_INSTRUCTION;
541 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400542 while (position.index < binary.wordCount) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100543 uint64_t index = position.index;
544 uint16_t wordCount;
545 Op opcode;
546 spvOpcodeSplit(spvFixWord(words[position.index], endian), &wordCount,
547 &opcode);
548
549 spv_instruction_t inst = {};
550 inst.extInstType = extInstType;
551 spvInstructionCopy(&words[position.index], opcode, wordCount, endian,
552 &inst);
553
Lei Zhang40056702015-09-11 14:31:27 -0400554 if (spvBinaryDecodeOpcode(&inst, endian, options, opcodeTable, operandTable,
555 extInstTable, format, stream, &position,
556 pDiagnostic))
557 return SPV_ERROR_INVALID_BINARY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100558 extInstType = inst.extInstType;
559
Lei Zhang40056702015-09-11 14:31:27 -0400560 if ((index + wordCount) != position.index) {
561 DIAGNOSTIC << "Invalid word count.";
562 return SPV_ERROR_INVALID_BINARY;
563 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100564
565 stream.get() << "\n";
566 }
567
568 if (!print) {
569 size_t length = sstream.str().size();
570 char *str = new char[length + 1];
Lei Zhang40056702015-09-11 14:31:27 -0400571 if (!str) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100572 strncpy(str, sstream.str().c_str(), length + 1);
573 spv_text text = new spv_text_t();
Lei Zhang40056702015-09-11 14:31:27 -0400574 if (!text) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100575 text->str = str;
576 text->length = length;
577 *pText = text;
578 }
579
580 return SPV_SUCCESS;
581}
582
583void spvBinaryDestroy(spv_binary binary) {
Lei Zhang40056702015-09-11 14:31:27 -0400584 if (!binary) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100585 if (binary->code) {
586 delete[] binary->code;
587 }
588 delete binary;
589}