blob: f504a018814c0120b71fca60f56ca8bbbac44626 [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
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010027#include "binary.h"
David Netofcc7d582015-10-27 15:31:10 -040028
David Neto7bff3eb2015-11-20 14:21:10 -050029#include <algorithm>
David Netofcc7d582015-10-27 15:31:10 -040030#include <cassert>
31#include <cstring>
David Neto0ca6b592015-10-30 16:06:15 -040032#include <limits>
David Netofcc7d582015-10-27 15:31:10 -040033#include <unordered_map>
34
David Netofcc7d582015-10-27 15:31:10 -040035#include "assembly_grammar.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010036#include "diagnostic.h"
37#include "ext_inst.h"
Lei Zhang923f6c12015-11-11 12:45:23 -050038#include "libspirv/libspirv.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010039#include "opcode.h"
40#include "operand.h"
Lei Zhangaa056cd2015-11-11 14:24:04 -050041#include "spirv_constant.h"
David Neto4c215712015-12-22 15:08:41 -050042#include "spirv_endian.h"
Andrew Woloszyn157e41b2015-10-16 15:11:00 -040043
Andrew Woloszyn55ecc2e2015-11-11 11:05:07 -050044spv_result_t spvBinaryHeaderGet(const spv_const_binary binary,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010045 const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -050046 spv_header_t* pHeader) {
David Neto0ca6b592015-10-30 16:06:15 -040047 if (!binary->code) return SPV_ERROR_INVALID_BINARY;
48 if (binary->wordCount < SPV_INDEX_INSTRUCTION)
49 return SPV_ERROR_INVALID_BINARY;
Lei Zhang40056702015-09-11 14:31:27 -040050 if (!pHeader) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010051
52 // TODO: Validation checking?
53 pHeader->magic = spvFixWord(binary->code[SPV_INDEX_MAGIC_NUMBER], endian);
54 pHeader->version = spvFixWord(binary->code[SPV_INDEX_VERSION_NUMBER], endian);
55 pHeader->generator =
56 spvFixWord(binary->code[SPV_INDEX_GENERATOR_NUMBER], endian);
57 pHeader->bound = spvFixWord(binary->code[SPV_INDEX_BOUND], endian);
58 pHeader->schema = spvFixWord(binary->code[SPV_INDEX_SCHEMA], endian);
59 pHeader->instructions = &binary->code[SPV_INDEX_INSTRUCTION];
60
61 return SPV_SUCCESS;
62}
63
David Neto78c3b432015-08-27 13:03:52 -040064// TODO(dneto): This API is not powerful enough in the case that the
65// number and type of operands are not known until partway through parsing
66// the operation. This happens when enum operands might have different number
67// of operands, or with extended instructions.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010068spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
69 const uint16_t operandIndex,
70 const spv_opcode_desc opcodeEntry,
71 const spv_operand_table operandTable,
Lei Zhang1a0334e2015-11-02 09:41:20 -050072 spv_operand_desc* pOperandEntry) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073 spv_operand_type_t type;
David Neto78c3b432015-08-27 13:03:52 -040074 if (operandIndex < opcodeEntry->numTypes) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010075 // NOTE: Do operand table lookup to set operandEntry if successful
David Netoba73a7c2016-01-06 13:08:39 -050076 const int index = operandIndex - 1;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010077 type = opcodeEntry->operandTypes[index];
78 spv_operand_desc entry = nullptr;
79 if (!spvOperandTableValueLookup(operandTable, type, word, &entry)) {
80 if (SPV_OPERAND_TYPE_NONE != entry->operandTypes[0]) {
81 *pOperandEntry = entry;
82 }
83 }
84 } else if (*pOperandEntry) {
85 // NOTE: Use specified operand entry operand type for this word
David Netoba73a7c2016-01-06 13:08:39 -050086 const int index = operandIndex - opcodeEntry->numTypes;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010087 type = (*pOperandEntry)->operandTypes[index];
Lei Zhangb36e7042015-10-28 13:40:52 -040088 } else if (SpvOpSwitch == opcodeEntry->opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010089 // NOTE: OpSwitch is a special case which expects a list of paired extra
90 // operands
91 assert(0 &&
92 "This case is previously untested, remove this assert and ensure it "
93 "is behaving correctly!");
David Netoba73a7c2016-01-06 13:08:39 -050094 const int lastIndex = opcodeEntry->numTypes - 1;
95 const int index = lastIndex + ((operandIndex - lastIndex) % 2);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010096 type = opcodeEntry->operandTypes[index];
97 } else {
98 // NOTE: Default to last operand type in opcode entry
David Netoba73a7c2016-01-06 13:08:39 -050099 const int index = opcodeEntry->numTypes - 1;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100100 type = opcodeEntry->operandTypes[index];
101 }
102 return type;
103}
104
David Neto0ca6b592015-10-30 16:06:15 -0400105namespace {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100106
David Neto0ca6b592015-10-30 16:06:15 -0400107// A SPIR-V binary parser. A parser instance communicates detailed parse
108// results via callbacks.
109class Parser {
110 public:
111 // The user_data value is provided to the callbacks as context.
Lei Zhang972788b2015-11-12 13:48:30 -0500112 Parser(const spv_const_context context, void* user_data,
113 spv_parsed_header_fn_t parsed_header_fn,
David Neto0ca6b592015-10-30 16:06:15 -0400114 spv_parsed_instruction_fn_t parsed_instruction_fn)
Lei Zhang972788b2015-11-12 13:48:30 -0500115 : grammar_(context),
116 user_data_(user_data),
David Neto0ca6b592015-10-30 16:06:15 -0400117 parsed_header_fn_(parsed_header_fn),
118 parsed_instruction_fn_(parsed_instruction_fn) {}
119
120 // Parses the specified binary SPIR-V module, issuing callbacks on a parsed
121 // header and for each parsed instruction. Returns SPV_SUCCESS on success.
122 // Otherwise returns an error code and issues a diagnostic.
123 spv_result_t parse(const uint32_t* words, size_t num_words,
124 spv_diagnostic* diagnostic);
125
126 private:
127 // All remaining methods work on the current module parse state.
128
129 // Like the parse method, but works on the current module parse state.
130 spv_result_t parseModule();
131
132 // Parses an instruction at the current position of the binary. Assumes
133 // the header has been parsed, the endian has been set, and the word index is
134 // still in range. Advances the parsing position past the instruction, and
135 // updates other parsing state for the current module.
136 // On success, returns SPV_SUCCESS and issues the parsed-instruction callback.
137 // On failure, returns an error code and issues a diagnostic.
138 spv_result_t parseInstruction();
139
David Neto7bff3eb2015-11-20 14:21:10 -0500140 // Parses an instruction operand with the given type, for an instruction
141 // starting at inst_offset words into the SPIR-V binary.
142 // If the SPIR-V binary is the same endianness as the host, then the
143 // endian_converted_inst_words parameter is ignored. Otherwise, this method
144 // appends the words for this operand, converted to host native endianness,
145 // to the end of endian_converted_inst_words. This method also updates the
146 // expected_operands parameter, and the scalar members of the inst parameter.
147 // On success, returns SPV_SUCCESS, advances past the operand, and pushes a
148 // new entry on to the operands vector. Otherwise returns an error code and
149 // issues a diagnostic.
150 spv_result_t parseOperand(size_t inst_offset, spv_parsed_instruction_t* inst,
David Neto0ca6b592015-10-30 16:06:15 -0400151 const spv_operand_type_t type,
David Neto7bff3eb2015-11-20 14:21:10 -0500152 std::vector<uint32_t>* endian_converted_inst_words,
David Neto0ca6b592015-10-30 16:06:15 -0400153 std::vector<spv_parsed_operand_t>* operands,
154 spv_operand_pattern_t* expected_operands);
155
156 // Records the numeric type for an operand according to the type information
157 // associated with the given non-zero type Id. This can fail if the type Id
158 // is not a type Id, or if the type Id does not reference a scalar numeric
159 // type. On success, return SPV_SUCCESS and populates the num_words,
160 // number_kind, and number_bit_width fields of parsed_operand.
161 spv_result_t setNumericTypeInfoForType(spv_parsed_operand_t* parsed_operand,
162 uint32_t type_id);
163
David Neto7bff3eb2015-11-20 14:21:10 -0500164 // Records the number type for an instruction at the given offset, if that
165 // instruction generates a type. For types that aren't scalar numbers,
166 // record something with number kind SPV_NUMBER_NONE.
167 void recordNumberType(size_t inst_offset,
168 const spv_parsed_instruction_t* inst);
David Neto0ca6b592015-10-30 16:06:15 -0400169
170 // Returns a diagnostic stream object initialized with current position in
171 // the input stream, and for the given error code. Any data written to the
172 // returned object will be propagated to the current parse's diagnostic
173 // object.
David Neto01656362015-11-20 10:44:41 -0500174 libspirv::DiagnosticStream diagnostic(spv_result_t error) {
David Neto7bff3eb2015-11-20 14:21:10 -0500175 return libspirv::DiagnosticStream({0, 0, _.word_index}, _.diagnostic,
176 error);
David Neto0ca6b592015-10-30 16:06:15 -0400177 }
178
179 // Returns a diagnostic stream object with the default parse error code.
David Neto01656362015-11-20 10:44:41 -0500180 libspirv::DiagnosticStream diagnostic() {
David Neto0ca6b592015-10-30 16:06:15 -0400181 // The default failure for parsing is invalid binary.
182 return diagnostic(SPV_ERROR_INVALID_BINARY);
183 }
184
David Netod9ad0502015-11-24 18:37:24 -0500185 // Issues a diagnostic describing an exhaustion of input condition when
186 // trying to decode an instruction operand, and returns
187 // SPV_ERROR_INVALID_BINARY.
188 spv_result_t exhaustedInputDiagnostic(size_t inst_offset, SpvOp opcode,
189 spv_operand_type_t type) {
190 return diagnostic() << "End of input reached while decoding Op"
191 << spvOpcodeString(opcode) << " starting at word "
192 << inst_offset
193 << ((_.word_index < _.num_words) ? ": truncated "
194 : ": missing ")
195 << spvOperandTypeStr(type) << " operand at word offset "
196 << _.word_index - inst_offset << ".";
197 }
198
David Neto0ca6b592015-10-30 16:06:15 -0400199 // Returns the endian-corrected word at the current position.
200 uint32_t peek() const { return peekAt(_.word_index); }
201
202 // Returns the endian-corrected word at the given position.
203 uint32_t peekAt(size_t index) const {
204 assert(index < _.num_words);
205 return spvFixWord(_.words[index], _.endian);
206 }
207
208 // Data members
209
210 const libspirv::AssemblyGrammar grammar_; // SPIR-V syntax utility.
211 void* const user_data_; // Context for the callbacks
212 const spv_parsed_header_fn_t parsed_header_fn_; // Parsed header callback
213 const spv_parsed_instruction_fn_t
214 parsed_instruction_fn_; // Parsed instruction callback
215
216 // Describes the format of a typed literal number.
217 struct NumberType {
218 spv_number_kind_t type;
219 uint32_t bit_width;
220 };
221
222 // The state used to parse a single SPIR-V binary module.
223 struct State {
224 State(const uint32_t* words_arg, size_t num_words_arg,
225 spv_diagnostic* diagnostic_arg)
226 : words(words_arg),
227 num_words(num_words_arg),
228 diagnostic(diagnostic_arg),
229 word_index(0),
230 endian() {}
231 State() : State(0, 0, nullptr) {}
232 const uint32_t* words; // Words in the binary SPIR-V module.
233 size_t num_words; // Number of words in the module.
234 spv_diagnostic* diagnostic; // Where diagnostics go.
235 size_t word_index; // The current position in words.
236 spv_endianness_t endian; // The endianness of the binary.
David Neto7bff3eb2015-11-20 14:21:10 -0500237 // Is the SPIR-V binary in a different endiannes from the host native
238 // endianness?
239 bool requires_endian_conversion;
David Neto0ca6b592015-10-30 16:06:15 -0400240
241 // Maps a result ID to its type ID. By convention:
242 // - a result ID that is a type definition maps to itself.
243 // - a result ID without a type maps to 0. (E.g. for OpLabel)
244 std::unordered_map<uint32_t, uint32_t> id_to_type_id;
245 // Maps a type ID to its number type description.
246 std::unordered_map<uint32_t, NumberType> type_id_to_number_type_info;
247 // Maps an ExtInstImport id to the extended instruction type.
248 std::unordered_map<uint32_t, spv_ext_inst_type_t>
249 import_id_to_ext_inst_type;
250 } _;
251};
252
253spv_result_t Parser::parse(const uint32_t* words, size_t num_words,
254 spv_diagnostic* diagnostic_arg) {
255 _ = State(words, num_words, diagnostic_arg);
256
257 const spv_result_t result = parseModule();
258
259 // Clear the module state. The tables might be big.
260 _ = State();
261
262 return result;
263}
264
265spv_result_t Parser::parseModule() {
266 if (!_.words) return diagnostic() << "Missing module.";
267
268 if (_.num_words < SPV_INDEX_INSTRUCTION)
269 return diagnostic() << "Module has incomplete header: only " << _.num_words
270 << " words instead of " << SPV_INDEX_INSTRUCTION;
271
272 // Check the magic number and detect the module's endianness.
Andrew Woloszyn55ecc2e2015-11-11 11:05:07 -0500273 spv_const_binary_t binary{_.words, _.num_words};
David Neto0ca6b592015-10-30 16:06:15 -0400274 if (spvBinaryEndianness(&binary, &_.endian)) {
275 return diagnostic() << "Invalid SPIR-V magic number '" << std::hex
276 << _.words[0] << "'.";
277 }
David Neto7bff3eb2015-11-20 14:21:10 -0500278 _.requires_endian_conversion = !spvIsHostEndian(_.endian);
David Neto0ca6b592015-10-30 16:06:15 -0400279
280 // Process the header.
281 spv_header_t header;
282 if (spvBinaryHeaderGet(&binary, _.endian, &header)) {
283 // It turns out there is no way to trigger this error since the only
284 // failure cases are already handled above, with better messages.
285 return diagnostic(SPV_ERROR_INTERNAL)
286 << "Internal error: unhandled header parse failure";
287 }
288 if (parsed_header_fn_) {
289 if (auto error = parsed_header_fn_(user_data_, _.endian, header.magic,
290 header.version, header.generator,
291 header.bound, header.schema)) {
292 return error;
293 }
294 }
295
296 // Process the instructions.
297 _.word_index = SPV_INDEX_INSTRUCTION;
298 while (_.word_index < _.num_words)
299 if (auto error = parseInstruction()) return error;
300
301 // Running off the end should already have been reported earlier.
302 assert(_.word_index == _.num_words);
303
304 return SPV_SUCCESS;
305}
306
307spv_result_t Parser::parseInstruction() {
308 // The zero values for all members except for opcode are the
309 // correct initial values.
310 spv_parsed_instruction_t inst = {};
David Neto7bff3eb2015-11-20 14:21:10 -0500311
312 const uint32_t first_word = peek();
313
314 // TODO(dneto): If it's too expensive to construct the following "words"
315 // and "operands" vectors for each instruction, each instruction, then make
316 // them class data members instead, and clear them here.
317
318 // If the module's endianness is different from the host native endianness,
319 // then converted_words contains the the endian-translated words in the
320 // instruction.
321 std::vector<uint32_t> endian_converted_words = {first_word};
322 if (_.requires_endian_conversion) {
323 // Most instructions have fewer than 25 words.
324 endian_converted_words.reserve(25);
325 }
David Neto0ca6b592015-10-30 16:06:15 -0400326
327 // After a successful parse of the instruction, the inst.operands member
328 // will point to this vector's storage.
David Neto0ca6b592015-10-30 16:06:15 -0400329 std::vector<spv_parsed_operand_t> operands;
330 // Most instructions have fewer than 25 logical operands.
331 operands.reserve(25);
332
333 assert(_.word_index < _.num_words);
334 // Decompose and check the first word.
335 uint16_t inst_word_count = 0;
David Neto7bff3eb2015-11-20 14:21:10 -0500336 spvOpcodeSplit(first_word, &inst_word_count, &inst.opcode);
David Neto0ca6b592015-10-30 16:06:15 -0400337 if (inst_word_count < 1) {
338 return diagnostic() << "Invalid instruction word count: "
339 << inst_word_count;
340 }
341 spv_opcode_desc opcode_desc;
342 if (grammar_.lookupOpcode(inst.opcode, &opcode_desc))
343 return diagnostic() << "Invalid opcode: " << int(inst.opcode);
344
David Neto7bff3eb2015-11-20 14:21:10 -0500345 // Advance past the opcode word. But remember the of the start
346 // of the instruction.
347 const size_t inst_offset = _.word_index;
David Neto0ca6b592015-10-30 16:06:15 -0400348 _.word_index++;
349
350 // Maintains the ordered list of expected operand types.
351 // For many instructions we only need the {numTypes, operandTypes}
352 // entries in opcode_desc. However, sometimes we need to modify
353 // the list as we parse the operands. This occurs when an operand
354 // has its own logical operands (such as the LocalSize operand for
355 // ExecutionMode), or for extended instructions that may have their
356 // own operands depending on the selected extended instruction.
357 spv_operand_pattern_t expected_operands(
358 opcode_desc->operandTypes,
359 opcode_desc->operandTypes + opcode_desc->numTypes);
360
David Neto7bff3eb2015-11-20 14:21:10 -0500361 while (_.word_index < inst_offset + inst_word_count) {
362 const uint16_t inst_word_index = uint16_t(_.word_index - inst_offset);
David Neto0ca6b592015-10-30 16:06:15 -0400363 if (expected_operands.empty()) {
364 return diagnostic() << "Invalid instruction Op" << opcode_desc->name
David Neto7bff3eb2015-11-20 14:21:10 -0500365 << " starting at word " << inst_offset
David Neto0ca6b592015-10-30 16:06:15 -0400366 << ": expected no more operands after "
367 << inst_word_index
368 << " words, but stated word count is "
369 << inst_word_count << ".";
370 }
371
372 spv_operand_type_t type = spvTakeFirstMatchableOperand(&expected_operands);
373
David Neto7bff3eb2015-11-20 14:21:10 -0500374 if (auto error =
375 parseOperand(inst_offset, &inst, type, &endian_converted_words,
David Netod9ad0502015-11-24 18:37:24 -0500376 &operands, &expected_operands)) {
David Neto0ca6b592015-10-30 16:06:15 -0400377 return error;
David Netod9ad0502015-11-24 18:37:24 -0500378 }
David Neto0ca6b592015-10-30 16:06:15 -0400379 }
380
381 if (!expected_operands.empty() &&
382 !spvOperandIsOptional(expected_operands.front())) {
383 return diagnostic() << "End of input reached while decoding Op"
384 << opcode_desc->name << " starting at word "
David Neto7bff3eb2015-11-20 14:21:10 -0500385 << inst_offset << ": expected more operands after "
David Neto0ca6b592015-10-30 16:06:15 -0400386 << inst_word_count << " words.";
387 }
388
David Neto7bff3eb2015-11-20 14:21:10 -0500389 if ((inst_offset + inst_word_count) != _.word_index) {
David Netod9ad0502015-11-24 18:37:24 -0500390 return diagnostic() << "Invalid word count: Op" << opcode_desc->name
391 << " starting at word " << inst_offset
392 << " says it has " << inst_word_count
David Neto7bff3eb2015-11-20 14:21:10 -0500393 << " words, but found " << _.word_index - inst_offset
David Neto0ca6b592015-10-30 16:06:15 -0400394 << " words instead.";
395 }
David Neto15afbf92015-11-23 14:17:35 -0500396
397 // Check the computed length of the endian-converted words vector against
398 // the declared number of words in the instruction. If endian conversion
399 // is required, then they should match. If no endian conversion was
400 // performed, then the vector only contains the initial opcode/word-count
401 // word.
402 assert(!_.requires_endian_conversion ||
403 (inst_word_count == endian_converted_words.size()));
404 assert(_.requires_endian_conversion || (endian_converted_words.size() == 1));
David Neto0ca6b592015-10-30 16:06:15 -0400405
David Neto7bff3eb2015-11-20 14:21:10 -0500406 recordNumberType(inst_offset, &inst);
David Neto0ca6b592015-10-30 16:06:15 -0400407
David Neto7bff3eb2015-11-20 14:21:10 -0500408 if (_.requires_endian_conversion) {
409 // We must wait until here to set this pointer, because the vector might
410 // have been be resized while we accumulated its elements.
411 inst.words = endian_converted_words.data();
412 } else {
413 // If no conversion is required, then just point to the underlying binary.
414 // This saves time and space.
415 inst.words = _.words + inst_offset;
416 }
417 inst.num_words = inst_word_count;
418
419 // We must wait until here to set this pointer, because the vector might
420 // have been be resized while we accumulated its elements.
David Neto0ca6b592015-10-30 16:06:15 -0400421 inst.operands = operands.data();
Andrew Woloszyn3a4bc7e2015-11-19 09:22:53 -0500422 inst.num_operands = uint16_t(operands.size());
David Neto0ca6b592015-10-30 16:06:15 -0400423
424 // Issue the callback. The callee should know that all the storage in inst
425 // is transient, and will disappear immediately afterward.
426 if (parsed_instruction_fn_) {
427 if (auto error = parsed_instruction_fn_(user_data_, &inst)) return error;
428 }
429
430 return SPV_SUCCESS;
431}
432
David Neto7bff3eb2015-11-20 14:21:10 -0500433spv_result_t Parser::parseOperand(size_t inst_offset,
434 spv_parsed_instruction_t* inst,
David Neto0ca6b592015-10-30 16:06:15 -0400435 const spv_operand_type_t type,
David Neto7bff3eb2015-11-20 14:21:10 -0500436 std::vector<uint32_t>* words,
David Neto0ca6b592015-10-30 16:06:15 -0400437 std::vector<spv_parsed_operand_t>* operands,
438 spv_operand_pattern_t* expected_operands) {
439 // We'll fill in this result as we go along.
440 spv_parsed_operand_t parsed_operand;
David Neto7bff3eb2015-11-20 14:21:10 -0500441 parsed_operand.offset = uint16_t(_.word_index - inst_offset);
David Neto0ca6b592015-10-30 16:06:15 -0400442 // Most operands occupy one word. This might be be adjusted later.
443 parsed_operand.num_words = 1;
444 // The type argument is the one used by the grammar to parse the instruction.
445 // But it can exposes internal parser details such as whether an operand is
446 // optional or actually represents a variable-length sequence of operands.
447 // The resulting type should be adjusted to avoid those internal details.
448 // In most cases, the resulting operand type is the same as the grammar type.
449 parsed_operand.type = type;
450
451 // Assume non-numeric values. This will be updated for literal numbers.
452 parsed_operand.number_kind = SPV_NUMBER_NONE;
453 parsed_operand.number_bit_width = 0;
454
David Netod9ad0502015-11-24 18:37:24 -0500455 if (_.word_index >= _.num_words)
456 return exhaustedInputDiagnostic(inst_offset, inst->opcode, type);
457
David Neto0ca6b592015-10-30 16:06:15 -0400458 const uint32_t word = peek();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100459
David Neto7bff3eb2015-11-20 14:21:10 -0500460 // Do the words in this operand have to be converted to native endianness?
461 // True for all but literal strings.
462 bool convert_operand_endianness = true;
463
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100464 switch (type) {
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400465 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto677e0c72016-01-05 14:56:02 -0500466 if (!word)
467 return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Type Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400468 inst->type_id = word;
469 break;
470
471 case SPV_OPERAND_TYPE_RESULT_ID:
David Neto677e0c72016-01-05 14:56:02 -0500472 if (!word)
473 return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Result Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400474 inst->result_id = word;
475 // Save the result ID to type ID mapping.
476 // In the grammar, type ID always appears before result ID.
477 if (_.id_to_type_id.find(inst->result_id) != _.id_to_type_id.end())
Umar Arshadf76e0f52015-11-18 15:43:43 -0500478 return diagnostic(SPV_ERROR_INVALID_ID) << "Id " << inst->result_id
David Neto677e0c72016-01-05 14:56:02 -0500479 << " is defined more than once";
David Neto0ca6b592015-10-30 16:06:15 -0400480 // Record it.
481 // A regular value maps to its type. Some instructions (e.g. OpLabel)
482 // have no type Id, and will map to 0. The result Id for a
483 // type-generating instruction (e.g. OpTypeInt) maps to itself.
484 _.id_to_type_id[inst->result_id] = spvOpcodeGeneratesType(inst->opcode)
485 ? inst->result_id
486 : inst->type_id;
487 break;
488
489 case SPV_OPERAND_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400490 case SPV_OPERAND_TYPE_OPTIONAL_ID:
Umar Arshadf76e0f52015-11-18 15:43:43 -0500491 if (!word) return diagnostic(SPV_ERROR_INVALID_ID) << "Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400492 parsed_operand.type = SPV_OPERAND_TYPE_ID;
493
494 if (inst->opcode == SpvOpExtInst && parsed_operand.offset == 3) {
495 // The current word is the extended instruction set Id.
496 // Set the extended instruction set type for the current instruction.
497 auto ext_inst_type_iter = _.import_id_to_ext_inst_type.find(word);
498 if (ext_inst_type_iter == _.import_id_to_ext_inst_type.end()) {
Umar Arshadf76e0f52015-11-18 15:43:43 -0500499 return diagnostic(SPV_ERROR_INVALID_ID)
David Neto0ca6b592015-10-30 16:06:15 -0400500 << "OpExtInst set Id " << word
501 << " does not reference an OpExtInstImport result Id";
502 }
503 inst->ext_inst_type = ext_inst_type_iter->second;
504 }
505 break;
506
David Neto64a9be92015-11-18 15:48:32 -0500507 case SPV_OPERAND_TYPE_SCOPE_ID:
508 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
David Netod9ad0502015-11-24 18:37:24 -0500509 // Check for trivially invalid values. The operand descriptions already
510 // have the word "ID" in them.
511 if (!word) return diagnostic() << spvOperandTypeStr(type) << " is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400512 break;
513
David Neto445ce442015-10-15 15:22:06 -0400514 case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
David Neto0ca6b592015-10-30 16:06:15 -0400515 assert(SpvOpExtInst == inst->opcode);
516 assert(inst->ext_inst_type != SPV_EXT_INST_TYPE_NONE);
517 spv_ext_inst_desc ext_inst;
518 if (grammar_.lookupExtInst(inst->ext_inst_type, word, &ext_inst))
519 return diagnostic() << "Invalid extended instruction number: " << word;
520 spvPrependOperandTypes(ext_inst->operandTypes, expected_operands);
David Neto445ce442015-10-15 15:22:06 -0400521 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400522
David Neto21196942015-11-11 02:45:45 -0500523 case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
524 assert(SpvOpSpecConstantOp == inst->opcode);
525 if (grammar_.lookupSpecConstantOpcode(SpvOp(word))) {
526 return diagnostic() << "Invalid " << spvOperandTypeStr(type) << ": "
527 << word;
528 }
529 spv_opcode_desc opcode_entry = nullptr;
530 if (grammar_.lookupOpcode(SpvOp(word), &opcode_entry)) {
531 return diagnostic(SPV_ERROR_INTERNAL)
532 << "OpSpecConstant opcode table out of sync";
533 }
534 // OpSpecConstant opcodes must have a type and result. We've already
535 // processed them, so skip them when preparing to parse the other
536 // operants for the opcode.
537 assert(opcode_entry->hasType);
538 assert(opcode_entry->hasResult);
539 assert(opcode_entry->numTypes >= 2);
540 spvPrependOperandTypes(opcode_entry->operandTypes + 2, expected_operands);
541 } break;
542
David Neto445ce442015-10-15 15:22:06 -0400543 case SPV_OPERAND_TYPE_LITERAL_INTEGER:
Lei Zhang6483bd72015-10-14 17:02:39 -0400544 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER:
David Neto201caf72015-11-04 17:38:17 -0500545 // These are regular single-word literal integer operands.
546 // Post-parsing validation should check the range of the parsed value.
David Neto0ca6b592015-10-30 16:06:15 -0400547 parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_INTEGER;
David Neto201caf72015-11-04 17:38:17 -0500548 // It turns out they are always unsigned integers!
549 parsed_operand.number_kind = SPV_NUMBER_UNSIGNED_INT;
550 parsed_operand.number_bit_width = 32;
551 break;
552
553 case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER:
554 case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
Lei Zhangaa3cd5a2015-11-10 14:29:35 -0500555 parsed_operand.type = SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER;
David Neto0ca6b592015-10-30 16:06:15 -0400556 if (inst->opcode == SpvOpSwitch) {
557 // The literal operands have the same type as the value
558 // referenced by the selector Id.
David Neto7bff3eb2015-11-20 14:21:10 -0500559 const uint32_t selector_id = peekAt(inst_offset + 1);
David Neto3664bd52015-12-23 13:21:43 -0500560 const auto type_id_iter = _.id_to_type_id.find(selector_id);
561 if (type_id_iter == _.id_to_type_id.end() ||
562 type_id_iter->second == 0) {
David Neto0ca6b592015-10-30 16:06:15 -0400563 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
564 << " has no type";
565 }
566 uint32_t type_id = type_id_iter->second;
567
568 if (selector_id == type_id) {
569 // Recall that by convention, a result ID that is a type definition
570 // maps to itself.
571 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
572 << " is a type, not a value";
573 }
574 if (auto error = setNumericTypeInfoForType(&parsed_operand, type_id))
575 return error;
576 if (parsed_operand.number_kind != SPV_NUMBER_UNSIGNED_INT &&
577 parsed_operand.number_kind != SPV_NUMBER_SIGNED_INT) {
578 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
579 << " is not a scalar integer";
580 }
Lei Zhangb41d1502015-09-14 15:22:23 -0400581 } else {
David Neto201caf72015-11-04 17:38:17 -0500582 assert(inst->opcode == SpvOpConstant ||
583 inst->opcode == SpvOpSpecConstant);
584 // The literal number type is determined by the type Id for the
585 // constant.
586 assert(inst->type_id);
587 if (auto error =
588 setNumericTypeInfoForType(&parsed_operand, inst->type_id))
589 return error;
Lei Zhangb41d1502015-09-14 15:22:23 -0400590 }
David Neto0ca6b592015-10-30 16:06:15 -0400591 break;
592
David Neto78c3b432015-08-27 13:03:52 -0400593 case SPV_OPERAND_TYPE_LITERAL_STRING:
594 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
David Neto7bff3eb2015-11-20 14:21:10 -0500595 convert_operand_endianness = false;
David Neto0ca6b592015-10-30 16:06:15 -0400596 const char* string =
597 reinterpret_cast<const char*>(_.words + _.word_index);
David Netod9ad0502015-11-24 18:37:24 -0500598 // Compute the length of the string, but make sure we don't run off the
599 // end of the input.
600 const size_t remaining_input_bytes =
601 sizeof(uint32_t) * (_.num_words - _.word_index);
602 const size_t string_num_content_bytes =
603 strnlen(string, remaining_input_bytes);
604 // If there was no terminating null byte, then that's an end-of-input
605 // error.
606 if (string_num_content_bytes == remaining_input_bytes)
607 return exhaustedInputDiagnostic(inst_offset, inst->opcode, type);
608 // Account for null in the word length, so add 1 for null, then add 3 to
609 // make sure we round up. The following is equivalent to:
610 // (string_num_content_bytes + 1 + 3) / 4
611 const size_t string_num_words = string_num_content_bytes / 4 + 1;
David Neto0ca6b592015-10-30 16:06:15 -0400612 // Make sure we can record the word count without overflow.
David Netod9ad0502015-11-24 18:37:24 -0500613 //
614 // This error can't currently be triggered because of validity
615 // checks elsewhere.
David Neto0ca6b592015-10-30 16:06:15 -0400616 if (string_num_words > std::numeric_limits<uint16_t>::max()) {
617 return diagnostic() << "Literal string is longer than "
618 << std::numeric_limits<uint16_t>::max()
619 << " words: " << string_num_words << " words long";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100620 }
Andrew Woloszyn3a4bc7e2015-11-19 09:22:53 -0500621 parsed_operand.num_words = uint16_t(string_num_words);
David Neto0ca6b592015-10-30 16:06:15 -0400622 parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_STRING;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100623
David Neto0ca6b592015-10-30 16:06:15 -0400624 if (SpvOpExtInstImport == inst->opcode) {
625 // Record the extended instruction type for the ID for this import.
626 // There is only one string literal argument to OpExtInstImport,
627 // so it's sufficient to guard this just on the opcode.
628 const spv_ext_inst_type_t ext_inst_type =
629 spvExtInstImportTypeGet(string);
630 if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
631 return diagnostic() << "Invalid extended instruction import '"
632 << string << "'";
Andrew Woloszyne59e6b72015-10-14 14:18:43 -0400633 }
David Neto0ca6b592015-10-30 16:06:15 -0400634 // We must have parsed a valid result ID. It's a condition
635 // of the grammar, and we only accept non-zero result Ids.
636 assert(inst->result_id);
637 _.import_id_to_ext_inst_type[inst->result_id] = ext_inst_type;
Andrew Woloszyne59e6b72015-10-14 14:18:43 -0400638 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100639 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400640
641 case SPV_OPERAND_TYPE_OPTIONAL_EXECUTION_MODE:
642 parsed_operand.type = SPV_OPERAND_TYPE_EXECUTION_MODE;
643 // Fall through
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100644 case SPV_OPERAND_TYPE_CAPABILITY:
645 case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
646 case SPV_OPERAND_TYPE_EXECUTION_MODEL:
647 case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
648 case SPV_OPERAND_TYPE_MEMORY_MODEL:
649 case SPV_OPERAND_TYPE_EXECUTION_MODE:
650 case SPV_OPERAND_TYPE_STORAGE_CLASS:
651 case SPV_OPERAND_TYPE_DIMENSIONALITY:
652 case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
653 case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
David Netod9ad0502015-11-24 18:37:24 -0500654 case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100655 case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
656 case SPV_OPERAND_TYPE_LINKAGE_TYPE:
657 case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
658 case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
659 case SPV_OPERAND_TYPE_DECORATION:
660 case SPV_OPERAND_TYPE_BUILT_IN:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100661 case SPV_OPERAND_TYPE_GROUP_OPERATION:
662 case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
David Neto47994822015-08-27 13:11:01 -0400663 case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: {
David Neto0ca6b592015-10-30 16:06:15 -0400664 // A single word that is a plain enum value.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100665 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400666 if (grammar_.lookupOperand(type, word, &entry)) {
David Neto201caf72015-11-04 17:38:17 -0500667 return diagnostic() << "Invalid "
668 << spvOperandTypeStr(parsed_operand.type)
David Neto0ca6b592015-10-30 16:06:15 -0400669 << " operand: " << word;
Lei Zhang40056702015-09-11 14:31:27 -0400670 }
David Neto78c3b432015-08-27 13:03:52 -0400671 // Prepare to accept operands to this operand, if needed.
David Neto0ca6b592015-10-30 16:06:15 -0400672 spvPrependOperandTypes(entry->operandTypes, expected_operands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100673 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400674
David Neto619db262015-09-25 12:43:37 -0400675 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
676 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
677 case SPV_OPERAND_TYPE_LOOP_CONTROL:
678 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
679 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
680 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
681 // This operand is a mask.
David Neto201caf72015-11-04 17:38:17 -0500682
683 // Map an optional operand type to its corresponding concrete type.
684 if (type == SPV_OPERAND_TYPE_OPTIONAL_IMAGE)
685 parsed_operand.type = SPV_OPERAND_TYPE_IMAGE;
686 else if (type == SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS)
687 parsed_operand.type = SPV_OPERAND_TYPE_MEMORY_ACCESS;
688
David Neto0ca6b592015-10-30 16:06:15 -0400689 // Check validity of set mask bits. Also prepare for operands for those
690 // masks if they have any. To get operand order correct, scan from
691 // MSB to LSB since we can only prepend operands to a pattern.
692 // The only case in the grammar where you have more than one mask bit
693 // having an operand is for image operands. See SPIR-V 3.14 Image
694 // Operands.
695 uint32_t remaining_word = word;
696 for (uint32_t mask = (1u << 31); remaining_word; mask >>= 1) {
David Neto619db262015-09-25 12:43:37 -0400697 if (remaining_word & mask) {
David Neto619db262015-09-25 12:43:37 -0400698 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400699 if (grammar_.lookupOperand(type, mask, &entry)) {
David Neto201caf72015-11-04 17:38:17 -0500700 return diagnostic()
701 << "Invalid " << spvOperandTypeStr(parsed_operand.type)
702 << " operand: " << word << " has invalid mask component "
703 << mask;
David Neto619db262015-09-25 12:43:37 -0400704 }
David Neto0ca6b592015-10-30 16:06:15 -0400705 remaining_word ^= mask;
706 spvPrependOperandTypes(entry->operandTypes, expected_operands);
David Neto619db262015-09-25 12:43:37 -0400707 }
708 }
David Neto0ca6b592015-10-30 16:06:15 -0400709 if (word == 0) {
710 // An all-zeroes mask *might* also be valid.
David Neto619db262015-09-25 12:43:37 -0400711 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400712 if (SPV_SUCCESS == grammar_.lookupOperand(type, 0, &entry)) {
David Neto619db262015-09-25 12:43:37 -0400713 // Prepare for its operands, if any.
David Neto0ca6b592015-10-30 16:06:15 -0400714 spvPrependOperandTypes(entry->operandTypes, expected_operands);
David Neto619db262015-09-25 12:43:37 -0400715 }
716 }
David Neto619db262015-09-25 12:43:37 -0400717 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400718 default:
719 return diagnostic() << "Internal error: Unhandled operand type: " << type;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100720 }
721
David Neto0ca6b592015-10-30 16:06:15 -0400722 assert(int(SPV_OPERAND_TYPE_FIRST_CONCRETE_TYPE) <= int(parsed_operand.type));
723 assert(int(SPV_OPERAND_TYPE_LAST_CONCRETE_TYPE) >= int(parsed_operand.type));
724
725 operands->push_back(parsed_operand);
726
David Neto7bff3eb2015-11-20 14:21:10 -0500727 const size_t index_after_operand = _.word_index + parsed_operand.num_words;
David Netod9ad0502015-11-24 18:37:24 -0500728
729 // Avoid buffer overrun for the cases where the operand has more than one
730 // word, and where it isn't a string. (Those other cases have already been
731 // handled earlier.) For example, this error can occur for a multi-word
732 // argument to OpConstant, or a multi-word case literal operand for OpSwitch.
733 if (_.num_words < index_after_operand)
734 return exhaustedInputDiagnostic(inst_offset, inst->opcode, type);
735
David Neto7bff3eb2015-11-20 14:21:10 -0500736 if (_.requires_endian_conversion) {
737 // Copy instruction words. Translate to native endianness as needed.
738 if (convert_operand_endianness) {
739 const spv_endianness_t endianness = _.endian;
740 std::transform(_.words + _.word_index, _.words + index_after_operand,
David Neto677e0c72016-01-05 14:56:02 -0500741 words->end(), [endianness](const uint32_t raw_word) {
742 return spvFixWord(raw_word, endianness);
David Neto7bff3eb2015-11-20 14:21:10 -0500743 });
744 } else {
745 words->insert(words->end(), _.words + _.word_index,
746 _.words + index_after_operand);
747 }
748 }
749
750 // Advance past the operand.
751 _.word_index = index_after_operand;
David Neto0ca6b592015-10-30 16:06:15 -0400752
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100753 return SPV_SUCCESS;
754}
755
David Neto0ca6b592015-10-30 16:06:15 -0400756spv_result_t Parser::setNumericTypeInfoForType(
757 spv_parsed_operand_t* parsed_operand, uint32_t type_id) {
David Neto201caf72015-11-04 17:38:17 -0500758 assert(type_id != 0);
David Neto0ca6b592015-10-30 16:06:15 -0400759 auto type_info_iter = _.type_id_to_number_type_info.find(type_id);
760 if (type_info_iter == _.type_id_to_number_type_info.end()) {
761 return diagnostic() << "Type Id " << type_id << " is not a type";
762 }
763 const NumberType& info = type_info_iter->second;
764 if (info.type == SPV_NUMBER_NONE) {
765 // This is a valid type, but for something other than a scalar number.
766 return diagnostic() << "Type Id " << type_id
767 << " is not a scalar numeric type";
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400768 }
769
David Neto0ca6b592015-10-30 16:06:15 -0400770 parsed_operand->number_kind = info.type;
771 parsed_operand->number_bit_width = info.bit_width;
David Neto066bd522016-01-05 14:57:58 -0500772 // Round up the word count.
773 parsed_operand->num_words = static_cast<uint16_t>((info.bit_width + 31) / 32);
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400774 return SPV_SUCCESS;
775}
776
David Neto7bff3eb2015-11-20 14:21:10 -0500777void Parser::recordNumberType(size_t inst_offset,
778 const spv_parsed_instruction_t* inst) {
David Neto0ca6b592015-10-30 16:06:15 -0400779 if (spvOpcodeGeneratesType(inst->opcode)) {
780 NumberType info = {SPV_NUMBER_NONE, 0};
781 if (SpvOpTypeInt == inst->opcode) {
David Neto7bff3eb2015-11-20 14:21:10 -0500782 const bool is_signed = peekAt(inst_offset + 3) != 0;
David Neto0ca6b592015-10-30 16:06:15 -0400783 info.type = is_signed ? SPV_NUMBER_SIGNED_INT : SPV_NUMBER_UNSIGNED_INT;
David Neto7bff3eb2015-11-20 14:21:10 -0500784 info.bit_width = peekAt(inst_offset + 2);
David Neto0ca6b592015-10-30 16:06:15 -0400785 } else if (SpvOpTypeFloat == inst->opcode) {
786 info.type = SPV_NUMBER_FLOATING;
David Neto7bff3eb2015-11-20 14:21:10 -0500787 info.bit_width = peekAt(inst_offset + 2);
Lei Zhang40056702015-09-11 14:31:27 -0400788 }
David Neto0ca6b592015-10-30 16:06:15 -0400789 // The *result* Id of a type generating instruction is the type Id.
790 _.type_id_to_number_type_info[inst->result_id] = info;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100791 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100792}
793
David Neto0ca6b592015-10-30 16:06:15 -0400794} // anonymous namespace
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400795
Lei Zhang972788b2015-11-12 13:48:30 -0500796spv_result_t spvBinaryParse(const spv_const_context context, void* user_data,
797 const uint32_t* code, const size_t num_words,
David Neto0ca6b592015-10-30 16:06:15 -0400798 spv_parsed_header_fn_t parsed_header,
799 spv_parsed_instruction_fn_t parsed_instruction,
800 spv_diagnostic* diagnostic) {
Lei Zhang972788b2015-11-12 13:48:30 -0500801 Parser parser(context, user_data, parsed_header, parsed_instruction);
David Neto0ca6b592015-10-30 16:06:15 -0400802 return parser.parse(code, num_words, diagnostic);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100803}
804
David Neto0ca6b592015-10-30 16:06:15 -0400805// TODO(dneto): This probably belongs in text.cpp since that's the only place
806// that a spv_binary_t value is created.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100807void spvBinaryDestroy(spv_binary binary) {
Lei Zhang40056702015-09-11 14:31:27 -0400808 if (!binary) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100809 if (binary->code) {
810 delete[] binary->code;
811 }
812 delete binary;
813}