blob: beeba5d5dca2255d76136b126c20af4245fa2541 [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
86spv_result_t spvBinaryHeaderGet(const spv_binary binary,
87 const spv_endianness_t endian,
88 spv_header_t *pHeader) {
Lei Zhang40056702015-09-11 14:31:27 -040089 if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
90 if (!pHeader) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010091
92 // TODO: Validation checking?
93 pHeader->magic = spvFixWord(binary->code[SPV_INDEX_MAGIC_NUMBER], endian);
94 pHeader->version = spvFixWord(binary->code[SPV_INDEX_VERSION_NUMBER], endian);
95 pHeader->generator =
96 spvFixWord(binary->code[SPV_INDEX_GENERATOR_NUMBER], endian);
97 pHeader->bound = spvFixWord(binary->code[SPV_INDEX_BOUND], endian);
98 pHeader->schema = spvFixWord(binary->code[SPV_INDEX_SCHEMA], endian);
99 pHeader->instructions = &binary->code[SPV_INDEX_INSTRUCTION];
100
101 return SPV_SUCCESS;
102}
103
104spv_result_t spvBinaryHeaderSet(spv_binary_t *binary, const uint32_t bound) {
Lei Zhang40056702015-09-11 14:31:27 -0400105 if (!binary) return SPV_ERROR_INVALID_BINARY;
106 if (!binary->code || !binary->wordCount) return SPV_ERROR_INVALID_BINARY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100107
108 binary->code[SPV_INDEX_MAGIC_NUMBER] = SPV_MAGIC_NUMBER;
109 binary->code[SPV_INDEX_VERSION_NUMBER] = SPV_VERSION_NUMBER;
Kenneth Benzie (Benie)81d7d492015-06-01 09:50:46 -0700110 binary->code[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100111 binary->code[SPV_INDEX_BOUND] = bound;
112 binary->code[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
113
114 return SPV_SUCCESS;
115}
116
117spv_result_t spvBinaryEncodeU32(const uint32_t value, spv_instruction_t *pInst,
118 const spv_position position,
119 spv_diagnostic *pDiagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -0400120 if (pInst->wordCount + 1 > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
121 DIAGNOSTIC << "Instruction word count '"
122 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "' exceeded.";
123 return SPV_ERROR_INVALID_TEXT;
124 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100125
126 pInst->words[pInst->wordCount++] = (uint32_t)value;
127 return SPV_SUCCESS;
128}
129
130spv_result_t spvBinaryEncodeU64(const uint64_t value, spv_instruction_t *pInst,
131 const spv_position position,
132 spv_diagnostic *pDiagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -0400133 if (pInst->wordCount + 2 > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
134 DIAGNOSTIC << "Instruction word count '"
135 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "' exceeded.";
136 return SPV_ERROR_INVALID_TEXT;
137 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100138
139 uint32_t low = (uint32_t)(0x00000000ffffffff & value);
140 uint32_t high = (uint32_t)((0xffffffff00000000 & value) >> 32);
141 pInst->words[pInst->wordCount++] = low;
142 pInst->words[pInst->wordCount++] = high;
143 return SPV_SUCCESS;
144}
145
146spv_result_t spvBinaryEncodeString(const char *str, spv_instruction_t *pInst,
147 const spv_position position,
148 spv_diagnostic *pDiagnostic) {
149 size_t length = strlen(str);
150 size_t wordCount = (length / 4) + 1;
Lei Zhang40056702015-09-11 14:31:27 -0400151 if ((sizeof(uint32_t) * pInst->wordCount) + length >
152 sizeof(uint32_t) * SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
153 DIAGNOSTIC << "Instruction word count '"
154 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << "'exceeded.";
155 return SPV_ERROR_INVALID_TEXT;
156 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100157
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) {
Lei Zhang40056702015-09-11 14:31:27 -0400212 if (!words || !position) return SPV_ERROR_INVALID_POINTER;
213 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100214
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
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100219 switch (type) {
David Neto78c3b432015-08-27 13:03:52 -0400220 case SPV_OPERAND_TYPE_ID:
221 case SPV_OPERAND_TYPE_RESULT_ID:
222 case SPV_OPERAND_TYPE_OPTIONAL_ID:
223 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE: {
224 if (color) {
Pyry Haulos26b3b002015-09-09 13:35:53 -0700225 if (type == SPV_OPERAND_TYPE_RESULT_ID) {
226 stream.get() << clr::blue();
227 } else {
228 stream.get() << clr::yellow();
229 }
David Neto78c3b432015-08-27 13:03:52 -0400230 }
Lei Zhang97afd5c2015-09-14 15:26:12 -0400231 stream.get() << "%" << spvFixWord(words[0], endian);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100232 stream.get() << ((color) ? clr::reset() : "");
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100233 position->index++;
234 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100235 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
236 // NOTE: Special case for extended instruction use
237 if (OpExtInst == opcode) {
238 spv_ext_inst_desc extInst;
Lei Zhang40056702015-09-11 14:31:27 -0400239 if (spvExtInstTableValueLookup(extInstTable, *pExtInstType, words[0],
240 &extInst)) {
241 DIAGNOSTIC << "Invalid extended instruction '" << words[0] << "'.";
242 return SPV_ERROR_INVALID_BINARY;
243 }
David Neto78c3b432015-08-27 13:03:52 -0400244 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
Andrew Woloszyn0d350b52015-08-21 14:23:42 -0400245 stream.get() << (color ? clr::red() : "");
246 stream.get() << extInst->name;
247 stream.get() << (color ? clr::reset() : "");
Lei Zhang41bf0732015-09-14 12:26:15 -0400248 position->index++;
249 break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100250 }
Lei Zhang41bf0732015-09-14 12:26:15 -0400251 } // Fall through for the general case.
252 case SPV_OPERAND_TYPE_LITERAL:
253 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL:
254 case SPV_OPERAND_TYPE_LITERAL_IN_OPTIONAL_TUPLE: {
255 // TODO: Need to support multiple word literals
256 stream.get() << (color ? clr::red() : "");
Lei Zhang97afd5c2015-09-14 15:26:12 -0400257 stream.get() << spvFixWord(words[0], endian);
Lei Zhang41bf0732015-09-14 12:26:15 -0400258 stream.get() << (color ? clr::reset() : "");
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100259 position->index++;
260 } break;
David Neto78c3b432015-08-27 13:03:52 -0400261 case SPV_OPERAND_TYPE_LITERAL_STRING:
262 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang97afd5c2015-09-14 15:26:12 -0400263 const char *string = (const char *)words;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100264 uint64_t stringOperandCount = (strlen(string) / 4) + 1;
265
266 // NOTE: Special case for extended instruction import
267 if (OpExtInstImport == opcode) {
268 *pExtInstType = spvExtInstImportTypeGet(string);
Lei Zhang40056702015-09-11 14:31:27 -0400269 if (SPV_EXT_INST_TYPE_NONE == *pExtInstType) {
270 DIAGNOSTIC << "Invalid extended instruction import'" << string
271 << "'.";
272 return SPV_ERROR_INVALID_BINARY;
273 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100274 }
275
276 stream.get() << "\"";
277 stream.get() << (color ? clr::green() : "");
278 stream.get() << string;
279 stream.get() << (color ? clr::reset() : "");
280 stream.get() << "\"";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100281 position->index += stringOperandCount;
282 } break;
283 case SPV_OPERAND_TYPE_CAPABILITY:
284 case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
285 case SPV_OPERAND_TYPE_EXECUTION_MODEL:
286 case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
287 case SPV_OPERAND_TYPE_MEMORY_MODEL:
288 case SPV_OPERAND_TYPE_EXECUTION_MODE:
David Neto78c3b432015-08-27 13:03:52 -0400289 case SPV_OPERAND_TYPE_OPTIONAL_EXECUTION_MODE:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100290 case SPV_OPERAND_TYPE_STORAGE_CLASS:
291 case SPV_OPERAND_TYPE_DIMENSIONALITY:
292 case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
293 case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
294 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
295 case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
296 case SPV_OPERAND_TYPE_LINKAGE_TYPE:
297 case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
298 case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
299 case SPV_OPERAND_TYPE_DECORATION:
300 case SPV_OPERAND_TYPE_BUILT_IN:
301 case SPV_OPERAND_TYPE_SELECTION_CONTROL:
302 case SPV_OPERAND_TYPE_LOOP_CONTROL:
303 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
304 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
David Neto78c3b432015-08-27 13:03:52 -0400305 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100306 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
307 case SPV_OPERAND_TYPE_GROUP_OPERATION:
308 case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
David Neto47994822015-08-27 13:11:01 -0400309 case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100310 spv_operand_desc entry;
Lei Zhang97afd5c2015-09-14 15:26:12 -0400311 if (spvOperandTableValueLookup(operandTable, type,
312 spvFixWord(words[0], endian), &entry)) {
Lei Zhang40056702015-09-11 14:31:27 -0400313 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " operand '"
Lei Zhang97afd5c2015-09-14 15:26:12 -0400314 << words[0] << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400315 return SPV_ERROR_INVALID_TEXT;
316 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100317 stream.get() << entry->name;
David Neto78c3b432015-08-27 13:03:52 -0400318 // Prepare to accept operands to this operand, if needed.
319 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100320 position->index++;
321 } break;
322 default: {
323 DIAGNOSTIC << "Invalid binary operand '" << type << "'";
324 return SPV_ERROR_INVALID_BINARY;
325 }
326 }
327
328 return SPV_SUCCESS;
329}
330
331spv_result_t spvBinaryDecodeOpcode(
332 spv_instruction_t *pInst, const spv_endianness_t endian,
333 const uint32_t options, const spv_opcode_table opcodeTable,
334 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Lei Zhang29e667e2015-09-11 11:01:59 -0400335 spv_assembly_syntax_format_t format, out_stream &stream,
336 spv_position position, spv_diagnostic *pDiagnostic) {
Lei Zhang40056702015-09-11 14:31:27 -0400337 if (!pInst || !position) return SPV_ERROR_INVALID_POINTER;
338 if (!opcodeTable || !operandTable || !extInstTable)
339 return SPV_ERROR_INVALID_TABLE;
340 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100341
David Neto78c3b432015-08-27 13:03:52 -0400342 spv_position_t instructionStart = *position;
343
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100344 uint16_t wordCount;
345 Op opcode;
346 spvOpcodeSplit(spvFixWord(pInst->words[0], endian), &wordCount, &opcode);
347
348 spv_opcode_desc opcodeEntry;
Lei Zhang40056702015-09-11 14:31:27 -0400349 if (spvOpcodeTableValueLookup(opcodeTable, opcode, &opcodeEntry)) {
350 DIAGNOSTIC << "Invalid Opcode '" << opcode << "'.";
351 return SPV_ERROR_INVALID_BINARY;
352 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100353
David Neto78c3b432015-08-27 13:03:52 -0400354 // See if there are enough required words.
355 // Some operands in the operand types are optional or could be zero length.
356 // The optional and zero length opeands must be at the end of the list.
357 if (opcodeEntry->numTypes > wordCount &&
358 !spvOperandIsOptional(opcodeEntry->operandTypes[wordCount])) {
359 uint16_t numRequired;
Lei Zhange78a7c12015-09-10 17:07:21 -0400360 for (numRequired = 0;
361 numRequired < opcodeEntry->numTypes &&
362 !spvOperandIsOptional(opcodeEntry->operandTypes[numRequired]);
363 numRequired++)
David Neto78c3b432015-08-27 13:03:52 -0400364 ;
365 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
Lei Zhange78a7c12015-09-10 17:07:21 -0400366 << " word count '" << wordCount << "', expected at least '"
367 << numRequired << "'.";
David Neto78c3b432015-08-27 13:03:52 -0400368 return SPV_ERROR_INVALID_BINARY;
369 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100370
Lei Zhang29e667e2015-09-11 11:01:59 -0400371 const bool isAssigmentFormat =
372 SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format;
373
374 // For Canonical Assembly Format, all words are written to stream in order.
375 // For Assignment Assembly Format, <result-id> and the equal sign are written
376 // to stream first, while the rest are written to no_result_id_stream. After
377 // processing all words, all words in no_result_id_stream are transcribed to
378 // stream.
379
Lei Zhang8a375202015-08-24 15:52:26 -0400380 std::stringstream no_result_id_strstream;
381 out_stream no_result_id_stream(no_result_id_strstream);
Lei Zhang29e667e2015-09-11 11:01:59 -0400382 (isAssigmentFormat ? no_result_id_stream.get() : stream.get())
383 << "Op" << opcodeEntry->name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100384
Lei Zhang29e667e2015-09-11 11:01:59 -0400385 const int16_t result_id_index = spvOpcodeResultIdIndex(opcodeEntry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100386 position->index++;
387
David Neto78c3b432015-08-27 13:03:52 -0400388 // Maintains the ordered list of expected operand types.
389 // For many instructions we only need the {numTypes, operandTypes}
390 // entries in opcodeEntry. However, sometimes we need to modify
391 // the list as we parse the operands. This occurs when an operand
392 // has its own logical operands (such as the LocalSize operand for
393 // ExecutionMode), or for extended instructions that may have their
394 // own operands depending on the selected extended instruction.
395 spv_operand_pattern_t expectedOperands(
396 opcodeEntry->operandTypes,
397 opcodeEntry->operandTypes + opcodeEntry->numTypes);
398
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100399 for (uint16_t index = 1; index < wordCount; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100400 const uint64_t currentPosIndex = position->index;
Lei Zhang29e667e2015-09-11 11:01:59 -0400401 const bool currentIsResultId = result_id_index == index - 1;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100402
Lei Zhang40056702015-09-11 14:31:27 -0400403 if (expectedOperands.empty()) {
404 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
405 << " starting at word " << instructionStart.index
406 << ": expected no more operands after " << index
407 << " words, but word count is " << wordCount << ".";
408 return SPV_ERROR_INVALID_BINARY;
409 }
David Neto78c3b432015-08-27 13:03:52 -0400410
411 spv_operand_type_t type = spvTakeFirstMatchableOperand(&expectedOperands);
412
Lei Zhang29e667e2015-09-11 11:01:59 -0400413 if (isAssigmentFormat) {
414 if (!currentIsResultId) no_result_id_stream.get() << " ";
415 } else {
416 stream.get() << " ";
417 }
Lei Zhang40056702015-09-11 14:31:27 -0400418 if (spvBinaryDecodeOperand(
David Neto78c3b432015-08-27 13:03:52 -0400419 opcodeEntry->opcode, type, pInst->words + index, endian, options,
420 operandTable, extInstTable, &expectedOperands, &pInst->extInstType,
Lei Zhang29e667e2015-09-11 11:01:59 -0400421 (isAssigmentFormat && !currentIsResultId ? no_result_id_stream
422 : stream),
Lei Zhang40056702015-09-11 14:31:27 -0400423 position, pDiagnostic)) {
424 DIAGNOSTIC << "UNEXPLAINED ERROR";
425 return SPV_ERROR_INVALID_BINARY;
426 }
Lei Zhang29e667e2015-09-11 11:01:59 -0400427 if (isAssigmentFormat && currentIsResultId) stream.get() << " = ";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100428 index += (uint16_t)(position->index - currentPosIndex - 1);
429 }
David Neto78c3b432015-08-27 13:03:52 -0400430 // TODO(dneto): There's an opportunity for a more informative message.
Lei Zhang40056702015-09-11 14:31:27 -0400431 if (!expectedOperands.empty() &&
432 !spvOperandIsOptional(expectedOperands.front())) {
433 DIAGNOSTIC << "Invalid instruction Op" << opcodeEntry->name
434 << " starting at word " << instructionStart.index
435 << ": expected more operands after " << wordCount << " words.";
436 return SPV_ERROR_INVALID_BINARY;
437 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100438
Lei Zhang8a375202015-08-24 15:52:26 -0400439 stream.get() << no_result_id_strstream.str();
440
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100441 return SPV_SUCCESS;
442}
443
Lei Zhange78a7c12015-09-10 17:07:21 -0400444spv_result_t spvBinaryToText(uint32_t *code, const uint64_t wordCount,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400445 const uint32_t options,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100446 const spv_opcode_table opcodeTable,
447 const spv_operand_table operandTable,
448 const spv_ext_inst_table extInstTable,
449 spv_text *pText, spv_diagnostic *pDiagnostic) {
Lei Zhang29e667e2015-09-11 11:01:59 -0400450 return spvBinaryToTextWithFormat(
451 code, wordCount, options, opcodeTable, operandTable, extInstTable,
452 SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT, pText, pDiagnostic);
453}
454
455spv_result_t spvBinaryToTextWithFormat(
456 uint32_t *code, const uint64_t wordCount, const uint32_t options,
457 const spv_opcode_table opcodeTable, const spv_operand_table operandTable,
458 const spv_ext_inst_table extInstTable, spv_assembly_syntax_format_t format,
459 spv_text *pText, spv_diagnostic *pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400460 spv_binary_t binary = {code, wordCount};
461
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400462 spv_position_t position = {};
Lei Zhang40056702015-09-11 14:31:27 -0400463 if (!binary.code || !binary.wordCount) {
464 DIAGNOSTIC << "Binary stream is empty.";
465 return SPV_ERROR_INVALID_BINARY;
466 }
467 if (!opcodeTable || !operandTable || !extInstTable)
468 return SPV_ERROR_INVALID_TABLE;
469 if (pText && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options))
470 return SPV_ERROR_INVALID_POINTER;
471 if (!pText && !spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options))
472 return SPV_ERROR_INVALID_POINTER;
473 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100474
475 spv_endianness_t endian;
Lei Zhang40056702015-09-11 14:31:27 -0400476 if (spvBinaryEndianness(&binary, &endian)) {
477 DIAGNOSTIC << "Invalid SPIR-V magic number '" << std::hex << binary.code[0]
478 << "'.";
479 return SPV_ERROR_INVALID_BINARY;
480 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100481
482 spv_header_t header;
Lei Zhang40056702015-09-11 14:31:27 -0400483 if (spvBinaryHeaderGet(&binary, endian, &header)) {
484 DIAGNOSTIC << "Invalid SPIR-V header.";
485 return SPV_ERROR_INVALID_BINARY;
486 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100487
488 bool print = spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_PRINT, options);
489 bool color =
490 print && spvIsInBitfield(SPV_BINARY_TO_TEXT_OPTION_COLOR, options);
491
492 std::stringstream sstream;
493 out_stream stream(sstream);
494 if (print) {
495 stream = out_stream();
496 }
497
498 if (color) {
499 stream.get() << clr::grey();
500 }
501 stream.get() << "; SPIR-V\n"
502 << "; Version: " << header.version << "\n"
503 << "; Generator: " << spvGeneratorStr(header.generator) << "\n"
504 << "; Bound: " << header.bound << "\n"
505 << "; Schema: " << header.schema << "\n";
506 if (color) {
507 stream.get() << clr::reset();
508 }
509
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400510 const uint32_t *words = binary.code;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100511 position.index = SPV_INDEX_INSTRUCTION;
512 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400513 while (position.index < binary.wordCount) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100514 uint64_t index = position.index;
515 uint16_t wordCount;
516 Op opcode;
517 spvOpcodeSplit(spvFixWord(words[position.index], endian), &wordCount,
518 &opcode);
519
520 spv_instruction_t inst = {};
521 inst.extInstType = extInstType;
522 spvInstructionCopy(&words[position.index], opcode, wordCount, endian,
523 &inst);
524
Lei Zhang40056702015-09-11 14:31:27 -0400525 if (spvBinaryDecodeOpcode(&inst, endian, options, opcodeTable, operandTable,
526 extInstTable, format, stream, &position,
527 pDiagnostic))
528 return SPV_ERROR_INVALID_BINARY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100529 extInstType = inst.extInstType;
530
Lei Zhang40056702015-09-11 14:31:27 -0400531 if ((index + wordCount) != position.index) {
532 DIAGNOSTIC << "Invalid word count.";
533 return SPV_ERROR_INVALID_BINARY;
534 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100535
536 stream.get() << "\n";
537 }
538
539 if (!print) {
540 size_t length = sstream.str().size();
541 char *str = new char[length + 1];
Lei Zhang40056702015-09-11 14:31:27 -0400542 if (!str) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100543 strncpy(str, sstream.str().c_str(), length + 1);
544 spv_text text = new spv_text_t();
Lei Zhang40056702015-09-11 14:31:27 -0400545 if (!text) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100546 text->str = str;
547 text->length = length;
548 *pText = text;
549 }
550
551 return SPV_SUCCESS;
552}
553
554void spvBinaryDestroy(spv_binary binary) {
Lei Zhang40056702015-09-11 14:31:27 -0400555 if (!binary) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100556 if (binary->code) {
557 delete[] binary->code;
558 }
559 delete binary;
560}