blob: c8b7c38c21a78979e2d770f21f54ae4aa0470a12 [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01002//
David Neto9fc86582016-09-01 15:33:59 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01008//
David Neto9fc86582016-09-01 15:33:59 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010014
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010015#include "binary.h"
David Netofcc7d582015-10-27 15:31:10 -040016
David Neto7bff3eb2015-11-20 14:21:10 -050017#include <algorithm>
David Netofcc7d582015-10-27 15:31:10 -040018#include <cassert>
19#include <cstring>
Andrew Woloszyn7ffd8ff2016-01-11 16:22:34 -050020#include <iterator>
David Neto0ca6b592015-10-30 16:06:15 -040021#include <limits>
David Netofcc7d582015-10-27 15:31:10 -040022#include <unordered_map>
23
David Netofcc7d582015-10-27 15:31:10 -040024#include "assembly_grammar.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010025#include "diagnostic.h"
26#include "ext_inst.h"
27#include "opcode.h"
28#include "operand.h"
David Neto5a703352016-02-17 14:44:00 -050029#include "spirv-tools/libspirv.h"
Lei Zhangaa056cd2015-11-11 14:24:04 -050030#include "spirv_constant.h"
David Neto4c215712015-12-22 15:08:41 -050031#include "spirv_endian.h"
Andrew Woloszyn157e41b2015-10-16 15:11:00 -040032
Andrew Woloszyn55ecc2e2015-11-11 11:05:07 -050033spv_result_t spvBinaryHeaderGet(const spv_const_binary binary,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010034 const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -050035 spv_header_t* pHeader) {
David Neto0ca6b592015-10-30 16:06:15 -040036 if (!binary->code) return SPV_ERROR_INVALID_BINARY;
37 if (binary->wordCount < SPV_INDEX_INSTRUCTION)
38 return SPV_ERROR_INVALID_BINARY;
Lei Zhang40056702015-09-11 14:31:27 -040039 if (!pHeader) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010040
41 // TODO: Validation checking?
42 pHeader->magic = spvFixWord(binary->code[SPV_INDEX_MAGIC_NUMBER], endian);
43 pHeader->version = spvFixWord(binary->code[SPV_INDEX_VERSION_NUMBER], endian);
44 pHeader->generator =
45 spvFixWord(binary->code[SPV_INDEX_GENERATOR_NUMBER], endian);
46 pHeader->bound = spvFixWord(binary->code[SPV_INDEX_BOUND], endian);
47 pHeader->schema = spvFixWord(binary->code[SPV_INDEX_SCHEMA], endian);
48 pHeader->instructions = &binary->code[SPV_INDEX_INSTRUCTION];
49
50 return SPV_SUCCESS;
51}
52
David Neto0ca6b592015-10-30 16:06:15 -040053namespace {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010054
David Neto0ca6b592015-10-30 16:06:15 -040055// A SPIR-V binary parser. A parser instance communicates detailed parse
56// results via callbacks.
57class Parser {
58 public:
59 // The user_data value is provided to the callbacks as context.
Lei Zhang972788b2015-11-12 13:48:30 -050060 Parser(const spv_const_context context, void* user_data,
61 spv_parsed_header_fn_t parsed_header_fn,
David Neto0ca6b592015-10-30 16:06:15 -040062 spv_parsed_instruction_fn_t parsed_instruction_fn)
Lei Zhang972788b2015-11-12 13:48:30 -050063 : grammar_(context),
Lei Zhang755f97f2016-09-02 18:06:18 -040064 consumer_(context->consumer),
Lei Zhang972788b2015-11-12 13:48:30 -050065 user_data_(user_data),
David Neto0ca6b592015-10-30 16:06:15 -040066 parsed_header_fn_(parsed_header_fn),
67 parsed_instruction_fn_(parsed_instruction_fn) {}
68
69 // Parses the specified binary SPIR-V module, issuing callbacks on a parsed
70 // header and for each parsed instruction. Returns SPV_SUCCESS on success.
71 // Otherwise returns an error code and issues a diagnostic.
72 spv_result_t parse(const uint32_t* words, size_t num_words,
73 spv_diagnostic* diagnostic);
74
75 private:
76 // All remaining methods work on the current module parse state.
77
78 // Like the parse method, but works on the current module parse state.
79 spv_result_t parseModule();
80
81 // Parses an instruction at the current position of the binary. Assumes
82 // the header has been parsed, the endian has been set, and the word index is
83 // still in range. Advances the parsing position past the instruction, and
84 // updates other parsing state for the current module.
85 // On success, returns SPV_SUCCESS and issues the parsed-instruction callback.
86 // On failure, returns an error code and issues a diagnostic.
87 spv_result_t parseInstruction();
88
David Neto7bff3eb2015-11-20 14:21:10 -050089 // Parses an instruction operand with the given type, for an instruction
90 // starting at inst_offset words into the SPIR-V binary.
91 // If the SPIR-V binary is the same endianness as the host, then the
92 // endian_converted_inst_words parameter is ignored. Otherwise, this method
93 // appends the words for this operand, converted to host native endianness,
94 // to the end of endian_converted_inst_words. This method also updates the
95 // expected_operands parameter, and the scalar members of the inst parameter.
96 // On success, returns SPV_SUCCESS, advances past the operand, and pushes a
97 // new entry on to the operands vector. Otherwise returns an error code and
98 // issues a diagnostic.
99 spv_result_t parseOperand(size_t inst_offset, spv_parsed_instruction_t* inst,
David Neto0ca6b592015-10-30 16:06:15 -0400100 const spv_operand_type_t type,
David Neto7bff3eb2015-11-20 14:21:10 -0500101 std::vector<uint32_t>* endian_converted_inst_words,
David Neto0ca6b592015-10-30 16:06:15 -0400102 std::vector<spv_parsed_operand_t>* operands,
103 spv_operand_pattern_t* expected_operands);
104
105 // Records the numeric type for an operand according to the type information
106 // associated with the given non-zero type Id. This can fail if the type Id
107 // is not a type Id, or if the type Id does not reference a scalar numeric
108 // type. On success, return SPV_SUCCESS and populates the num_words,
109 // number_kind, and number_bit_width fields of parsed_operand.
110 spv_result_t setNumericTypeInfoForType(spv_parsed_operand_t* parsed_operand,
111 uint32_t type_id);
112
David Neto7bff3eb2015-11-20 14:21:10 -0500113 // Records the number type for an instruction at the given offset, if that
114 // instruction generates a type. For types that aren't scalar numbers,
115 // record something with number kind SPV_NUMBER_NONE.
116 void recordNumberType(size_t inst_offset,
117 const spv_parsed_instruction_t* inst);
David Neto0ca6b592015-10-30 16:06:15 -0400118
119 // Returns a diagnostic stream object initialized with current position in
120 // the input stream, and for the given error code. Any data written to the
121 // returned object will be propagated to the current parse's diagnostic
122 // object.
David Neto01656362015-11-20 10:44:41 -0500123 libspirv::DiagnosticStream diagnostic(spv_result_t error) {
Lei Zhang755f97f2016-09-02 18:06:18 -0400124 return libspirv::DiagnosticStream({0, 0, _.word_index}, consumer_, error);
David Neto0ca6b592015-10-30 16:06:15 -0400125 }
126
127 // Returns a diagnostic stream object with the default parse error code.
David Neto01656362015-11-20 10:44:41 -0500128 libspirv::DiagnosticStream diagnostic() {
David Neto0ca6b592015-10-30 16:06:15 -0400129 // The default failure for parsing is invalid binary.
130 return diagnostic(SPV_ERROR_INVALID_BINARY);
131 }
132
David Netod9ad0502015-11-24 18:37:24 -0500133 // Issues a diagnostic describing an exhaustion of input condition when
134 // trying to decode an instruction operand, and returns
135 // SPV_ERROR_INVALID_BINARY.
136 spv_result_t exhaustedInputDiagnostic(size_t inst_offset, SpvOp opcode,
137 spv_operand_type_t type) {
138 return diagnostic() << "End of input reached while decoding Op"
139 << spvOpcodeString(opcode) << " starting at word "
140 << inst_offset
141 << ((_.word_index < _.num_words) ? ": truncated "
142 : ": missing ")
143 << spvOperandTypeStr(type) << " operand at word offset "
144 << _.word_index - inst_offset << ".";
145 }
146
David Neto0ca6b592015-10-30 16:06:15 -0400147 // Returns the endian-corrected word at the current position.
148 uint32_t peek() const { return peekAt(_.word_index); }
149
150 // Returns the endian-corrected word at the given position.
151 uint32_t peekAt(size_t index) const {
152 assert(index < _.num_words);
153 return spvFixWord(_.words[index], _.endian);
154 }
155
156 // Data members
157
158 const libspirv::AssemblyGrammar grammar_; // SPIR-V syntax utility.
Lei Zhang755f97f2016-09-02 18:06:18 -0400159 const spvtools::MessageConsumer& consumer_; // Message consumer callback.
David Neto0ca6b592015-10-30 16:06:15 -0400160 void* const user_data_; // Context for the callbacks
161 const spv_parsed_header_fn_t parsed_header_fn_; // Parsed header callback
162 const spv_parsed_instruction_fn_t
163 parsed_instruction_fn_; // Parsed instruction callback
164
165 // Describes the format of a typed literal number.
166 struct NumberType {
167 spv_number_kind_t type;
168 uint32_t bit_width;
169 };
170
171 // The state used to parse a single SPIR-V binary module.
172 struct State {
173 State(const uint32_t* words_arg, size_t num_words_arg,
174 spv_diagnostic* diagnostic_arg)
175 : words(words_arg),
176 num_words(num_words_arg),
177 diagnostic(diagnostic_arg),
178 word_index(0),
Lei Zhang712bed02016-02-25 16:11:16 -0500179 endian(),
180 requires_endian_conversion(false) {}
David Neto0ca6b592015-10-30 16:06:15 -0400181 State() : State(0, 0, nullptr) {}
182 const uint32_t* words; // Words in the binary SPIR-V module.
183 size_t num_words; // Number of words in the module.
184 spv_diagnostic* diagnostic; // Where diagnostics go.
185 size_t word_index; // The current position in words.
186 spv_endianness_t endian; // The endianness of the binary.
David Neto7bff3eb2015-11-20 14:21:10 -0500187 // Is the SPIR-V binary in a different endiannes from the host native
188 // endianness?
189 bool requires_endian_conversion;
David Neto0ca6b592015-10-30 16:06:15 -0400190
191 // Maps a result ID to its type ID. By convention:
192 // - a result ID that is a type definition maps to itself.
193 // - a result ID without a type maps to 0. (E.g. for OpLabel)
194 std::unordered_map<uint32_t, uint32_t> id_to_type_id;
195 // Maps a type ID to its number type description.
196 std::unordered_map<uint32_t, NumberType> type_id_to_number_type_info;
197 // Maps an ExtInstImport id to the extended instruction type.
198 std::unordered_map<uint32_t, spv_ext_inst_type_t>
199 import_id_to_ext_inst_type;
200 } _;
201};
202
203spv_result_t Parser::parse(const uint32_t* words, size_t num_words,
204 spv_diagnostic* diagnostic_arg) {
205 _ = State(words, num_words, diagnostic_arg);
206
207 const spv_result_t result = parseModule();
208
209 // Clear the module state. The tables might be big.
210 _ = State();
211
212 return result;
213}
214
215spv_result_t Parser::parseModule() {
216 if (!_.words) return diagnostic() << "Missing module.";
217
218 if (_.num_words < SPV_INDEX_INSTRUCTION)
219 return diagnostic() << "Module has incomplete header: only " << _.num_words
220 << " words instead of " << SPV_INDEX_INSTRUCTION;
221
222 // Check the magic number and detect the module's endianness.
Andrew Woloszyn55ecc2e2015-11-11 11:05:07 -0500223 spv_const_binary_t binary{_.words, _.num_words};
David Neto0ca6b592015-10-30 16:06:15 -0400224 if (spvBinaryEndianness(&binary, &_.endian)) {
225 return diagnostic() << "Invalid SPIR-V magic number '" << std::hex
226 << _.words[0] << "'.";
227 }
David Neto7bff3eb2015-11-20 14:21:10 -0500228 _.requires_endian_conversion = !spvIsHostEndian(_.endian);
David Neto0ca6b592015-10-30 16:06:15 -0400229
230 // Process the header.
231 spv_header_t header;
232 if (spvBinaryHeaderGet(&binary, _.endian, &header)) {
233 // It turns out there is no way to trigger this error since the only
234 // failure cases are already handled above, with better messages.
235 return diagnostic(SPV_ERROR_INTERNAL)
236 << "Internal error: unhandled header parse failure";
237 }
238 if (parsed_header_fn_) {
239 if (auto error = parsed_header_fn_(user_data_, _.endian, header.magic,
240 header.version, header.generator,
241 header.bound, header.schema)) {
242 return error;
243 }
244 }
245
246 // Process the instructions.
247 _.word_index = SPV_INDEX_INSTRUCTION;
248 while (_.word_index < _.num_words)
249 if (auto error = parseInstruction()) return error;
250
251 // Running off the end should already have been reported earlier.
252 assert(_.word_index == _.num_words);
253
254 return SPV_SUCCESS;
255}
256
257spv_result_t Parser::parseInstruction() {
258 // The zero values for all members except for opcode are the
259 // correct initial values.
260 spv_parsed_instruction_t inst = {};
David Neto7bff3eb2015-11-20 14:21:10 -0500261
262 const uint32_t first_word = peek();
263
264 // TODO(dneto): If it's too expensive to construct the following "words"
265 // and "operands" vectors for each instruction, each instruction, then make
266 // them class data members instead, and clear them here.
267
268 // If the module's endianness is different from the host native endianness,
269 // then converted_words contains the the endian-translated words in the
270 // instruction.
271 std::vector<uint32_t> endian_converted_words = {first_word};
272 if (_.requires_endian_conversion) {
273 // Most instructions have fewer than 25 words.
274 endian_converted_words.reserve(25);
275 }
David Neto0ca6b592015-10-30 16:06:15 -0400276
277 // After a successful parse of the instruction, the inst.operands member
278 // will point to this vector's storage.
David Neto0ca6b592015-10-30 16:06:15 -0400279 std::vector<spv_parsed_operand_t> operands;
280 // Most instructions have fewer than 25 logical operands.
281 operands.reserve(25);
282
283 assert(_.word_index < _.num_words);
284 // Decompose and check the first word.
285 uint16_t inst_word_count = 0;
David Neto7bff3eb2015-11-20 14:21:10 -0500286 spvOpcodeSplit(first_word, &inst_word_count, &inst.opcode);
David Neto0ca6b592015-10-30 16:06:15 -0400287 if (inst_word_count < 1) {
288 return diagnostic() << "Invalid instruction word count: "
289 << inst_word_count;
290 }
291 spv_opcode_desc opcode_desc;
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400292 if (grammar_.lookupOpcode(static_cast<SpvOp>(inst.opcode), &opcode_desc))
293 return diagnostic() << "Invalid opcode: " << inst.opcode;
David Neto0ca6b592015-10-30 16:06:15 -0400294
David Neto7bff3eb2015-11-20 14:21:10 -0500295 // Advance past the opcode word. But remember the of the start
296 // of the instruction.
297 const size_t inst_offset = _.word_index;
David Neto0ca6b592015-10-30 16:06:15 -0400298 _.word_index++;
299
300 // Maintains the ordered list of expected operand types.
301 // For many instructions we only need the {numTypes, operandTypes}
302 // entries in opcode_desc. However, sometimes we need to modify
303 // the list as we parse the operands. This occurs when an operand
304 // has its own logical operands (such as the LocalSize operand for
305 // ExecutionMode), or for extended instructions that may have their
306 // own operands depending on the selected extended instruction.
307 spv_operand_pattern_t expected_operands(
308 opcode_desc->operandTypes,
309 opcode_desc->operandTypes + opcode_desc->numTypes);
310
David Neto7bff3eb2015-11-20 14:21:10 -0500311 while (_.word_index < inst_offset + inst_word_count) {
312 const uint16_t inst_word_index = uint16_t(_.word_index - inst_offset);
David Neto0ca6b592015-10-30 16:06:15 -0400313 if (expected_operands.empty()) {
314 return diagnostic() << "Invalid instruction Op" << opcode_desc->name
David Neto7bff3eb2015-11-20 14:21:10 -0500315 << " starting at word " << inst_offset
David Neto0ca6b592015-10-30 16:06:15 -0400316 << ": expected no more operands after "
317 << inst_word_index
318 << " words, but stated word count is "
319 << inst_word_count << ".";
320 }
321
322 spv_operand_type_t type = spvTakeFirstMatchableOperand(&expected_operands);
323
David Neto7bff3eb2015-11-20 14:21:10 -0500324 if (auto error =
325 parseOperand(inst_offset, &inst, type, &endian_converted_words,
David Netod9ad0502015-11-24 18:37:24 -0500326 &operands, &expected_operands)) {
David Neto0ca6b592015-10-30 16:06:15 -0400327 return error;
David Netod9ad0502015-11-24 18:37:24 -0500328 }
David Neto0ca6b592015-10-30 16:06:15 -0400329 }
330
331 if (!expected_operands.empty() &&
332 !spvOperandIsOptional(expected_operands.front())) {
333 return diagnostic() << "End of input reached while decoding Op"
334 << opcode_desc->name << " starting at word "
David Neto7bff3eb2015-11-20 14:21:10 -0500335 << inst_offset << ": expected more operands after "
David Neto0ca6b592015-10-30 16:06:15 -0400336 << inst_word_count << " words.";
337 }
338
David Neto7bff3eb2015-11-20 14:21:10 -0500339 if ((inst_offset + inst_word_count) != _.word_index) {
David Netod9ad0502015-11-24 18:37:24 -0500340 return diagnostic() << "Invalid word count: Op" << opcode_desc->name
341 << " starting at word " << inst_offset
342 << " says it has " << inst_word_count
David Neto7bff3eb2015-11-20 14:21:10 -0500343 << " words, but found " << _.word_index - inst_offset
David Neto0ca6b592015-10-30 16:06:15 -0400344 << " words instead.";
345 }
David Neto15afbf92015-11-23 14:17:35 -0500346
347 // Check the computed length of the endian-converted words vector against
348 // the declared number of words in the instruction. If endian conversion
349 // is required, then they should match. If no endian conversion was
350 // performed, then the vector only contains the initial opcode/word-count
351 // word.
352 assert(!_.requires_endian_conversion ||
353 (inst_word_count == endian_converted_words.size()));
354 assert(_.requires_endian_conversion || (endian_converted_words.size() == 1));
David Neto0ca6b592015-10-30 16:06:15 -0400355
David Neto7bff3eb2015-11-20 14:21:10 -0500356 recordNumberType(inst_offset, &inst);
David Neto0ca6b592015-10-30 16:06:15 -0400357
David Neto7bff3eb2015-11-20 14:21:10 -0500358 if (_.requires_endian_conversion) {
359 // We must wait until here to set this pointer, because the vector might
360 // have been be resized while we accumulated its elements.
361 inst.words = endian_converted_words.data();
362 } else {
363 // If no conversion is required, then just point to the underlying binary.
364 // This saves time and space.
365 inst.words = _.words + inst_offset;
366 }
367 inst.num_words = inst_word_count;
368
369 // We must wait until here to set this pointer, because the vector might
370 // have been be resized while we accumulated its elements.
David Neto0ca6b592015-10-30 16:06:15 -0400371 inst.operands = operands.data();
Andrew Woloszyn3a4bc7e2015-11-19 09:22:53 -0500372 inst.num_operands = uint16_t(operands.size());
David Neto0ca6b592015-10-30 16:06:15 -0400373
374 // Issue the callback. The callee should know that all the storage in inst
375 // is transient, and will disappear immediately afterward.
376 if (parsed_instruction_fn_) {
377 if (auto error = parsed_instruction_fn_(user_data_, &inst)) return error;
378 }
379
380 return SPV_SUCCESS;
381}
382
David Neto7bff3eb2015-11-20 14:21:10 -0500383spv_result_t Parser::parseOperand(size_t inst_offset,
384 spv_parsed_instruction_t* inst,
David Neto0ca6b592015-10-30 16:06:15 -0400385 const spv_operand_type_t type,
David Neto7bff3eb2015-11-20 14:21:10 -0500386 std::vector<uint32_t>* words,
David Neto0ca6b592015-10-30 16:06:15 -0400387 std::vector<spv_parsed_operand_t>* operands,
388 spv_operand_pattern_t* expected_operands) {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400389 const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
David Neto0ca6b592015-10-30 16:06:15 -0400390 // We'll fill in this result as we go along.
391 spv_parsed_operand_t parsed_operand;
David Neto7bff3eb2015-11-20 14:21:10 -0500392 parsed_operand.offset = uint16_t(_.word_index - inst_offset);
David Neto0ca6b592015-10-30 16:06:15 -0400393 // Most operands occupy one word. This might be be adjusted later.
394 parsed_operand.num_words = 1;
395 // The type argument is the one used by the grammar to parse the instruction.
396 // But it can exposes internal parser details such as whether an operand is
397 // optional or actually represents a variable-length sequence of operands.
398 // The resulting type should be adjusted to avoid those internal details.
399 // In most cases, the resulting operand type is the same as the grammar type.
400 parsed_operand.type = type;
401
402 // Assume non-numeric values. This will be updated for literal numbers.
403 parsed_operand.number_kind = SPV_NUMBER_NONE;
404 parsed_operand.number_bit_width = 0;
405
David Netod9ad0502015-11-24 18:37:24 -0500406 if (_.word_index >= _.num_words)
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400407 return exhaustedInputDiagnostic(inst_offset, opcode, type);
David Netod9ad0502015-11-24 18:37:24 -0500408
David Neto0ca6b592015-10-30 16:06:15 -0400409 const uint32_t word = peek();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100410
David Neto7bff3eb2015-11-20 14:21:10 -0500411 // Do the words in this operand have to be converted to native endianness?
412 // True for all but literal strings.
413 bool convert_operand_endianness = true;
414
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100415 switch (type) {
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400416 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto677e0c72016-01-05 14:56:02 -0500417 if (!word)
418 return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Type Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400419 inst->type_id = word;
420 break;
421
422 case SPV_OPERAND_TYPE_RESULT_ID:
David Neto677e0c72016-01-05 14:56:02 -0500423 if (!word)
424 return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Result Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400425 inst->result_id = word;
426 // Save the result ID to type ID mapping.
427 // In the grammar, type ID always appears before result ID.
428 if (_.id_to_type_id.find(inst->result_id) != _.id_to_type_id.end())
Umar Arshadf76e0f52015-11-18 15:43:43 -0500429 return diagnostic(SPV_ERROR_INVALID_ID) << "Id " << inst->result_id
David Neto677e0c72016-01-05 14:56:02 -0500430 << " is defined more than once";
David Neto0ca6b592015-10-30 16:06:15 -0400431 // Record it.
432 // A regular value maps to its type. Some instructions (e.g. OpLabel)
433 // have no type Id, and will map to 0. The result Id for a
434 // type-generating instruction (e.g. OpTypeInt) maps to itself.
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400435 _.id_to_type_id[inst->result_id] =
436 spvOpcodeGeneratesType(opcode) ? inst->result_id : inst->type_id;
David Neto0ca6b592015-10-30 16:06:15 -0400437 break;
438
439 case SPV_OPERAND_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400440 case SPV_OPERAND_TYPE_OPTIONAL_ID:
Umar Arshadf76e0f52015-11-18 15:43:43 -0500441 if (!word) return diagnostic(SPV_ERROR_INVALID_ID) << "Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400442 parsed_operand.type = SPV_OPERAND_TYPE_ID;
443
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400444 if (opcode == SpvOpExtInst && parsed_operand.offset == 3) {
David Neto0ca6b592015-10-30 16:06:15 -0400445 // The current word is the extended instruction set Id.
446 // Set the extended instruction set type for the current instruction.
447 auto ext_inst_type_iter = _.import_id_to_ext_inst_type.find(word);
448 if (ext_inst_type_iter == _.import_id_to_ext_inst_type.end()) {
Umar Arshadf76e0f52015-11-18 15:43:43 -0500449 return diagnostic(SPV_ERROR_INVALID_ID)
David Neto0ca6b592015-10-30 16:06:15 -0400450 << "OpExtInst set Id " << word
451 << " does not reference an OpExtInstImport result Id";
452 }
453 inst->ext_inst_type = ext_inst_type_iter->second;
454 }
455 break;
456
David Neto64a9be92015-11-18 15:48:32 -0500457 case SPV_OPERAND_TYPE_SCOPE_ID:
458 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
David Netod9ad0502015-11-24 18:37:24 -0500459 // Check for trivially invalid values. The operand descriptions already
460 // have the word "ID" in them.
461 if (!word) return diagnostic() << spvOperandTypeStr(type) << " is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400462 break;
463
David Neto445ce442015-10-15 15:22:06 -0400464 case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400465 assert(SpvOpExtInst == opcode);
David Neto0ca6b592015-10-30 16:06:15 -0400466 assert(inst->ext_inst_type != SPV_EXT_INST_TYPE_NONE);
467 spv_ext_inst_desc ext_inst;
468 if (grammar_.lookupExtInst(inst->ext_inst_type, word, &ext_inst))
469 return diagnostic() << "Invalid extended instruction number: " << word;
470 spvPrependOperandTypes(ext_inst->operandTypes, expected_operands);
David Neto445ce442015-10-15 15:22:06 -0400471 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400472
David Neto21196942015-11-11 02:45:45 -0500473 case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400474 assert(SpvOpSpecConstantOp == opcode);
David Neto21196942015-11-11 02:45:45 -0500475 if (grammar_.lookupSpecConstantOpcode(SpvOp(word))) {
476 return diagnostic() << "Invalid " << spvOperandTypeStr(type) << ": "
477 << word;
478 }
479 spv_opcode_desc opcode_entry = nullptr;
480 if (grammar_.lookupOpcode(SpvOp(word), &opcode_entry)) {
481 return diagnostic(SPV_ERROR_INTERNAL)
482 << "OpSpecConstant opcode table out of sync";
483 }
484 // OpSpecConstant opcodes must have a type and result. We've already
485 // processed them, so skip them when preparing to parse the other
486 // operants for the opcode.
487 assert(opcode_entry->hasType);
488 assert(opcode_entry->hasResult);
489 assert(opcode_entry->numTypes >= 2);
490 spvPrependOperandTypes(opcode_entry->operandTypes + 2, expected_operands);
491 } break;
492
David Neto445ce442015-10-15 15:22:06 -0400493 case SPV_OPERAND_TYPE_LITERAL_INTEGER:
Lei Zhang6483bd72015-10-14 17:02:39 -0400494 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER:
David Neto201caf72015-11-04 17:38:17 -0500495 // These are regular single-word literal integer operands.
496 // Post-parsing validation should check the range of the parsed value.
David Neto0ca6b592015-10-30 16:06:15 -0400497 parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_INTEGER;
David Neto201caf72015-11-04 17:38:17 -0500498 // It turns out they are always unsigned integers!
499 parsed_operand.number_kind = SPV_NUMBER_UNSIGNED_INT;
500 parsed_operand.number_bit_width = 32;
501 break;
502
503 case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER:
504 case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
Lei Zhangaa3cd5a2015-11-10 14:29:35 -0500505 parsed_operand.type = SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER;
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400506 if (opcode == SpvOpSwitch) {
David Neto0ca6b592015-10-30 16:06:15 -0400507 // The literal operands have the same type as the value
508 // referenced by the selector Id.
David Neto7bff3eb2015-11-20 14:21:10 -0500509 const uint32_t selector_id = peekAt(inst_offset + 1);
David Neto3664bd52015-12-23 13:21:43 -0500510 const auto type_id_iter = _.id_to_type_id.find(selector_id);
511 if (type_id_iter == _.id_to_type_id.end() ||
512 type_id_iter->second == 0) {
David Neto0ca6b592015-10-30 16:06:15 -0400513 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
514 << " has no type";
515 }
516 uint32_t type_id = type_id_iter->second;
517
518 if (selector_id == type_id) {
519 // Recall that by convention, a result ID that is a type definition
520 // maps to itself.
521 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
522 << " is a type, not a value";
523 }
524 if (auto error = setNumericTypeInfoForType(&parsed_operand, type_id))
525 return error;
526 if (parsed_operand.number_kind != SPV_NUMBER_UNSIGNED_INT &&
527 parsed_operand.number_kind != SPV_NUMBER_SIGNED_INT) {
528 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
529 << " is not a scalar integer";
530 }
Lei Zhangb41d1502015-09-14 15:22:23 -0400531 } else {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400532 assert(opcode == SpvOpConstant || opcode == SpvOpSpecConstant);
David Neto201caf72015-11-04 17:38:17 -0500533 // The literal number type is determined by the type Id for the
534 // constant.
535 assert(inst->type_id);
536 if (auto error =
537 setNumericTypeInfoForType(&parsed_operand, inst->type_id))
538 return error;
Lei Zhangb41d1502015-09-14 15:22:23 -0400539 }
David Neto0ca6b592015-10-30 16:06:15 -0400540 break;
541
David Neto78c3b432015-08-27 13:03:52 -0400542 case SPV_OPERAND_TYPE_LITERAL_STRING:
543 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
David Neto7bff3eb2015-11-20 14:21:10 -0500544 convert_operand_endianness = false;
David Neto0ca6b592015-10-30 16:06:15 -0400545 const char* string =
546 reinterpret_cast<const char*>(_.words + _.word_index);
David Netod9ad0502015-11-24 18:37:24 -0500547 // Compute the length of the string, but make sure we don't run off the
548 // end of the input.
549 const size_t remaining_input_bytes =
550 sizeof(uint32_t) * (_.num_words - _.word_index);
551 const size_t string_num_content_bytes =
552 strnlen(string, remaining_input_bytes);
553 // If there was no terminating null byte, then that's an end-of-input
554 // error.
555 if (string_num_content_bytes == remaining_input_bytes)
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400556 return exhaustedInputDiagnostic(inst_offset, opcode, type);
David Netod9ad0502015-11-24 18:37:24 -0500557 // Account for null in the word length, so add 1 for null, then add 3 to
558 // make sure we round up. The following is equivalent to:
559 // (string_num_content_bytes + 1 + 3) / 4
560 const size_t string_num_words = string_num_content_bytes / 4 + 1;
David Neto0ca6b592015-10-30 16:06:15 -0400561 // Make sure we can record the word count without overflow.
David Netod9ad0502015-11-24 18:37:24 -0500562 //
563 // This error can't currently be triggered because of validity
564 // checks elsewhere.
David Neto0ca6b592015-10-30 16:06:15 -0400565 if (string_num_words > std::numeric_limits<uint16_t>::max()) {
566 return diagnostic() << "Literal string is longer than "
567 << std::numeric_limits<uint16_t>::max()
568 << " words: " << string_num_words << " words long";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100569 }
Andrew Woloszyn3a4bc7e2015-11-19 09:22:53 -0500570 parsed_operand.num_words = uint16_t(string_num_words);
David Neto0ca6b592015-10-30 16:06:15 -0400571 parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_STRING;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100572
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400573 if (SpvOpExtInstImport == opcode) {
David Neto0ca6b592015-10-30 16:06:15 -0400574 // Record the extended instruction type for the ID for this import.
575 // There is only one string literal argument to OpExtInstImport,
576 // so it's sufficient to guard this just on the opcode.
577 const spv_ext_inst_type_t ext_inst_type =
578 spvExtInstImportTypeGet(string);
579 if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
580 return diagnostic() << "Invalid extended instruction import '"
581 << string << "'";
Andrew Woloszyne59e6b72015-10-14 14:18:43 -0400582 }
David Neto0ca6b592015-10-30 16:06:15 -0400583 // We must have parsed a valid result ID. It's a condition
584 // of the grammar, and we only accept non-zero result Ids.
585 assert(inst->result_id);
586 _.import_id_to_ext_inst_type[inst->result_id] = ext_inst_type;
Andrew Woloszyne59e6b72015-10-14 14:18:43 -0400587 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100588 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400589
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100590 case SPV_OPERAND_TYPE_CAPABILITY:
591 case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
592 case SPV_OPERAND_TYPE_EXECUTION_MODEL:
593 case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
594 case SPV_OPERAND_TYPE_MEMORY_MODEL:
595 case SPV_OPERAND_TYPE_EXECUTION_MODE:
596 case SPV_OPERAND_TYPE_STORAGE_CLASS:
597 case SPV_OPERAND_TYPE_DIMENSIONALITY:
598 case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
599 case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
David Netod9ad0502015-11-24 18:37:24 -0500600 case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100601 case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
602 case SPV_OPERAND_TYPE_LINKAGE_TYPE:
603 case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
David Neto2889a0c2016-02-15 13:50:00 -0500604 case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100605 case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
606 case SPV_OPERAND_TYPE_DECORATION:
607 case SPV_OPERAND_TYPE_BUILT_IN:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100608 case SPV_OPERAND_TYPE_GROUP_OPERATION:
609 case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
David Neto47994822015-08-27 13:11:01 -0400610 case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: {
David Neto0ca6b592015-10-30 16:06:15 -0400611 // A single word that is a plain enum value.
David Neto2889a0c2016-02-15 13:50:00 -0500612
613 // Map an optional operand type to its corresponding concrete type.
614 if (type == SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER)
615 parsed_operand.type = SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
616
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100617 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400618 if (grammar_.lookupOperand(type, word, &entry)) {
David Neto201caf72015-11-04 17:38:17 -0500619 return diagnostic() << "Invalid "
620 << spvOperandTypeStr(parsed_operand.type)
David Neto0ca6b592015-10-30 16:06:15 -0400621 << " operand: " << word;
Lei Zhang40056702015-09-11 14:31:27 -0400622 }
David Neto78c3b432015-08-27 13:03:52 -0400623 // Prepare to accept operands to this operand, if needed.
David Neto0ca6b592015-10-30 16:06:15 -0400624 spvPrependOperandTypes(entry->operandTypes, expected_operands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100625 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400626
David Neto619db262015-09-25 12:43:37 -0400627 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
628 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
629 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Neto2889a0c2016-02-15 13:50:00 -0500630 case SPV_OPERAND_TYPE_IMAGE:
David Neto619db262015-09-25 12:43:37 -0400631 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
632 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
633 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
634 // This operand is a mask.
David Neto201caf72015-11-04 17:38:17 -0500635
636 // Map an optional operand type to its corresponding concrete type.
637 if (type == SPV_OPERAND_TYPE_OPTIONAL_IMAGE)
638 parsed_operand.type = SPV_OPERAND_TYPE_IMAGE;
639 else if (type == SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS)
640 parsed_operand.type = SPV_OPERAND_TYPE_MEMORY_ACCESS;
641
David Neto0ca6b592015-10-30 16:06:15 -0400642 // Check validity of set mask bits. Also prepare for operands for those
643 // masks if they have any. To get operand order correct, scan from
644 // MSB to LSB since we can only prepend operands to a pattern.
645 // The only case in the grammar where you have more than one mask bit
646 // having an operand is for image operands. See SPIR-V 3.14 Image
647 // Operands.
648 uint32_t remaining_word = word;
649 for (uint32_t mask = (1u << 31); remaining_word; mask >>= 1) {
David Neto619db262015-09-25 12:43:37 -0400650 if (remaining_word & mask) {
David Neto619db262015-09-25 12:43:37 -0400651 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400652 if (grammar_.lookupOperand(type, mask, &entry)) {
David Neto201caf72015-11-04 17:38:17 -0500653 return diagnostic()
654 << "Invalid " << spvOperandTypeStr(parsed_operand.type)
655 << " operand: " << word << " has invalid mask component "
656 << mask;
David Neto619db262015-09-25 12:43:37 -0400657 }
David Neto0ca6b592015-10-30 16:06:15 -0400658 remaining_word ^= mask;
659 spvPrependOperandTypes(entry->operandTypes, expected_operands);
David Neto619db262015-09-25 12:43:37 -0400660 }
661 }
David Neto0ca6b592015-10-30 16:06:15 -0400662 if (word == 0) {
663 // An all-zeroes mask *might* also be valid.
David Neto619db262015-09-25 12:43:37 -0400664 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400665 if (SPV_SUCCESS == grammar_.lookupOperand(type, 0, &entry)) {
David Neto619db262015-09-25 12:43:37 -0400666 // Prepare for its operands, if any.
David Neto0ca6b592015-10-30 16:06:15 -0400667 spvPrependOperandTypes(entry->operandTypes, expected_operands);
David Neto619db262015-09-25 12:43:37 -0400668 }
669 }
David Neto619db262015-09-25 12:43:37 -0400670 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400671 default:
672 return diagnostic() << "Internal error: Unhandled operand type: " << type;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100673 }
674
David Neto0ca6b592015-10-30 16:06:15 -0400675 assert(int(SPV_OPERAND_TYPE_FIRST_CONCRETE_TYPE) <= int(parsed_operand.type));
676 assert(int(SPV_OPERAND_TYPE_LAST_CONCRETE_TYPE) >= int(parsed_operand.type));
677
678 operands->push_back(parsed_operand);
679
David Neto7bff3eb2015-11-20 14:21:10 -0500680 const size_t index_after_operand = _.word_index + parsed_operand.num_words;
David Netod9ad0502015-11-24 18:37:24 -0500681
682 // Avoid buffer overrun for the cases where the operand has more than one
683 // word, and where it isn't a string. (Those other cases have already been
684 // handled earlier.) For example, this error can occur for a multi-word
685 // argument to OpConstant, or a multi-word case literal operand for OpSwitch.
686 if (_.num_words < index_after_operand)
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400687 return exhaustedInputDiagnostic(inst_offset, opcode, type);
David Netod9ad0502015-11-24 18:37:24 -0500688
David Neto7bff3eb2015-11-20 14:21:10 -0500689 if (_.requires_endian_conversion) {
690 // Copy instruction words. Translate to native endianness as needed.
691 if (convert_operand_endianness) {
692 const spv_endianness_t endianness = _.endian;
693 std::transform(_.words + _.word_index, _.words + index_after_operand,
Andrew Woloszyn3b69d052016-01-11 13:54:30 -0500694 std::back_inserter(*words),
695 [endianness](const uint32_t raw_word) {
David Neto677e0c72016-01-05 14:56:02 -0500696 return spvFixWord(raw_word, endianness);
David Neto7bff3eb2015-11-20 14:21:10 -0500697 });
698 } else {
699 words->insert(words->end(), _.words + _.word_index,
700 _.words + index_after_operand);
701 }
702 }
703
704 // Advance past the operand.
705 _.word_index = index_after_operand;
David Neto0ca6b592015-10-30 16:06:15 -0400706
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100707 return SPV_SUCCESS;
708}
709
David Neto0ca6b592015-10-30 16:06:15 -0400710spv_result_t Parser::setNumericTypeInfoForType(
711 spv_parsed_operand_t* parsed_operand, uint32_t type_id) {
David Neto201caf72015-11-04 17:38:17 -0500712 assert(type_id != 0);
David Neto0ca6b592015-10-30 16:06:15 -0400713 auto type_info_iter = _.type_id_to_number_type_info.find(type_id);
714 if (type_info_iter == _.type_id_to_number_type_info.end()) {
715 return diagnostic() << "Type Id " << type_id << " is not a type";
716 }
717 const NumberType& info = type_info_iter->second;
718 if (info.type == SPV_NUMBER_NONE) {
719 // This is a valid type, but for something other than a scalar number.
720 return diagnostic() << "Type Id " << type_id
721 << " is not a scalar numeric type";
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400722 }
723
David Neto0ca6b592015-10-30 16:06:15 -0400724 parsed_operand->number_kind = info.type;
725 parsed_operand->number_bit_width = info.bit_width;
David Neto066bd522016-01-05 14:57:58 -0500726 // Round up the word count.
727 parsed_operand->num_words = static_cast<uint16_t>((info.bit_width + 31) / 32);
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400728 return SPV_SUCCESS;
729}
730
David Neto7bff3eb2015-11-20 14:21:10 -0500731void Parser::recordNumberType(size_t inst_offset,
732 const spv_parsed_instruction_t* inst) {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400733 const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
734 if (spvOpcodeGeneratesType(opcode)) {
David Neto0ca6b592015-10-30 16:06:15 -0400735 NumberType info = {SPV_NUMBER_NONE, 0};
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400736 if (SpvOpTypeInt == opcode) {
David Neto7bff3eb2015-11-20 14:21:10 -0500737 const bool is_signed = peekAt(inst_offset + 3) != 0;
David Neto0ca6b592015-10-30 16:06:15 -0400738 info.type = is_signed ? SPV_NUMBER_SIGNED_INT : SPV_NUMBER_UNSIGNED_INT;
David Neto7bff3eb2015-11-20 14:21:10 -0500739 info.bit_width = peekAt(inst_offset + 2);
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400740 } else if (SpvOpTypeFloat == opcode) {
David Neto0ca6b592015-10-30 16:06:15 -0400741 info.type = SPV_NUMBER_FLOATING;
David Neto7bff3eb2015-11-20 14:21:10 -0500742 info.bit_width = peekAt(inst_offset + 2);
Lei Zhang40056702015-09-11 14:31:27 -0400743 }
David Neto0ca6b592015-10-30 16:06:15 -0400744 // The *result* Id of a type generating instruction is the type Id.
745 _.type_id_to_number_type_info[inst->result_id] = info;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100746 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100747}
748
David Neto0ca6b592015-10-30 16:06:15 -0400749} // anonymous namespace
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400750
Lei Zhang972788b2015-11-12 13:48:30 -0500751spv_result_t spvBinaryParse(const spv_const_context context, void* user_data,
752 const uint32_t* code, const size_t num_words,
David Neto0ca6b592015-10-30 16:06:15 -0400753 spv_parsed_header_fn_t parsed_header,
754 spv_parsed_instruction_fn_t parsed_instruction,
755 spv_diagnostic* diagnostic) {
Lei Zhang755f97f2016-09-02 18:06:18 -0400756 spv_context_t hijack_context = *context;
757 if (diagnostic) {
758 *diagnostic = nullptr;
759 libspirv::UseDiagnosticAsMessageConsumer(&hijack_context, diagnostic);
760 }
761 Parser parser(&hijack_context, user_data, parsed_header, parsed_instruction);
David Neto0ca6b592015-10-30 16:06:15 -0400762 return parser.parse(code, num_words, diagnostic);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100763}
764
David Neto0ca6b592015-10-30 16:06:15 -0400765// TODO(dneto): This probably belongs in text.cpp since that's the only place
766// that a spv_binary_t value is created.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100767void spvBinaryDestroy(spv_binary binary) {
Lei Zhang40056702015-09-11 14:31:27 -0400768 if (!binary) return;
Eric Engestromeb6ae972016-02-18 23:41:16 +0000769 delete[] binary->code;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100770 delete binary;
771}