blob: 2ade2546b1679ffca688f8164077f249aaa8b82d [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>
Lei Zhangdc6e4832016-09-21 17:16:31 -040023#include <vector>
David Netofcc7d582015-10-27 15:31:10 -040024
David Netofcc7d582015-10-27 15:31:10 -040025#include "assembly_grammar.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010026#include "diagnostic.h"
27#include "ext_inst.h"
28#include "opcode.h"
29#include "operand.h"
David Netodbc20492017-03-14 12:43:41 -040030#include "spirv/1.2/spirv.h"
Lei Zhangaa056cd2015-11-11 14:24:04 -050031#include "spirv_constant.h"
David Neto4c215712015-12-22 15:08:41 -050032#include "spirv_endian.h"
Andrew Woloszyn157e41b2015-10-16 15:11:00 -040033
Andrew Woloszyn55ecc2e2015-11-11 11:05:07 -050034spv_result_t spvBinaryHeaderGet(const spv_const_binary binary,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010035 const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -050036 spv_header_t* pHeader) {
David Neto0ca6b592015-10-30 16:06:15 -040037 if (!binary->code) return SPV_ERROR_INVALID_BINARY;
38 if (binary->wordCount < SPV_INDEX_INSTRUCTION)
39 return SPV_ERROR_INVALID_BINARY;
Lei Zhang40056702015-09-11 14:31:27 -040040 if (!pHeader) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010041
42 // TODO: Validation checking?
43 pHeader->magic = spvFixWord(binary->code[SPV_INDEX_MAGIC_NUMBER], endian);
44 pHeader->version = spvFixWord(binary->code[SPV_INDEX_VERSION_NUMBER], endian);
45 pHeader->generator =
46 spvFixWord(binary->code[SPV_INDEX_GENERATOR_NUMBER], endian);
47 pHeader->bound = spvFixWord(binary->code[SPV_INDEX_BOUND], endian);
48 pHeader->schema = spvFixWord(binary->code[SPV_INDEX_SCHEMA], endian);
49 pHeader->instructions = &binary->code[SPV_INDEX_INSTRUCTION];
50
51 return SPV_SUCCESS;
52}
53
David Neto0ca6b592015-10-30 16:06:15 -040054namespace {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010055
David Neto0ca6b592015-10-30 16:06:15 -040056// A SPIR-V binary parser. A parser instance communicates detailed parse
57// results via callbacks.
58class Parser {
59 public:
60 // The user_data value is provided to the callbacks as context.
Lei Zhang972788b2015-11-12 13:48:30 -050061 Parser(const spv_const_context context, void* user_data,
62 spv_parsed_header_fn_t parsed_header_fn,
David Neto0ca6b592015-10-30 16:06:15 -040063 spv_parsed_instruction_fn_t parsed_instruction_fn)
Lei Zhang972788b2015-11-12 13:48:30 -050064 : grammar_(context),
Lei Zhang755f97f2016-09-02 18:06:18 -040065 consumer_(context->consumer),
Lei Zhang972788b2015-11-12 13:48:30 -050066 user_data_(user_data),
David Neto0ca6b592015-10-30 16:06:15 -040067 parsed_header_fn_(parsed_header_fn),
68 parsed_instruction_fn_(parsed_instruction_fn) {}
69
70 // Parses the specified binary SPIR-V module, issuing callbacks on a parsed
71 // header and for each parsed instruction. Returns SPV_SUCCESS on success.
72 // Otherwise returns an error code and issues a diagnostic.
73 spv_result_t parse(const uint32_t* words, size_t num_words,
74 spv_diagnostic* diagnostic);
75
76 private:
77 // All remaining methods work on the current module parse state.
78
79 // Like the parse method, but works on the current module parse state.
80 spv_result_t parseModule();
81
82 // Parses an instruction at the current position of the binary. Assumes
83 // the header has been parsed, the endian has been set, and the word index is
84 // still in range. Advances the parsing position past the instruction, and
85 // updates other parsing state for the current module.
86 // On success, returns SPV_SUCCESS and issues the parsed-instruction callback.
87 // On failure, returns an error code and issues a diagnostic.
88 spv_result_t parseInstruction();
89
David Neto7bff3eb2015-11-20 14:21:10 -050090 // Parses an instruction operand with the given type, for an instruction
91 // starting at inst_offset words into the SPIR-V binary.
92 // If the SPIR-V binary is the same endianness as the host, then the
93 // endian_converted_inst_words parameter is ignored. Otherwise, this method
94 // appends the words for this operand, converted to host native endianness,
95 // to the end of endian_converted_inst_words. This method also updates the
96 // expected_operands parameter, and the scalar members of the inst parameter.
97 // On success, returns SPV_SUCCESS, advances past the operand, and pushes a
98 // new entry on to the operands vector. Otherwise returns an error code and
99 // issues a diagnostic.
100 spv_result_t parseOperand(size_t inst_offset, spv_parsed_instruction_t* inst,
David Neto0ca6b592015-10-30 16:06:15 -0400101 const spv_operand_type_t type,
David Neto7bff3eb2015-11-20 14:21:10 -0500102 std::vector<uint32_t>* endian_converted_inst_words,
David Neto0ca6b592015-10-30 16:06:15 -0400103 std::vector<spv_parsed_operand_t>* operands,
104 spv_operand_pattern_t* expected_operands);
105
106 // Records the numeric type for an operand according to the type information
107 // associated with the given non-zero type Id. This can fail if the type Id
108 // is not a type Id, or if the type Id does not reference a scalar numeric
109 // type. On success, return SPV_SUCCESS and populates the num_words,
110 // number_kind, and number_bit_width fields of parsed_operand.
111 spv_result_t setNumericTypeInfoForType(spv_parsed_operand_t* parsed_operand,
112 uint32_t type_id);
113
David Neto7bff3eb2015-11-20 14:21:10 -0500114 // Records the number type for an instruction at the given offset, if that
115 // instruction generates a type. For types that aren't scalar numbers,
116 // record something with number kind SPV_NUMBER_NONE.
117 void recordNumberType(size_t inst_offset,
118 const spv_parsed_instruction_t* inst);
David Neto0ca6b592015-10-30 16:06:15 -0400119
120 // Returns a diagnostic stream object initialized with current position in
121 // the input stream, and for the given error code. Any data written to the
122 // returned object will be propagated to the current parse's diagnostic
123 // object.
David Neto01656362015-11-20 10:44:41 -0500124 libspirv::DiagnosticStream diagnostic(spv_result_t error) {
Lei Zhang755f97f2016-09-02 18:06:18 -0400125 return libspirv::DiagnosticStream({0, 0, _.word_index}, consumer_, error);
David Neto0ca6b592015-10-30 16:06:15 -0400126 }
127
128 // Returns a diagnostic stream object with the default parse error code.
David Neto01656362015-11-20 10:44:41 -0500129 libspirv::DiagnosticStream diagnostic() {
David Neto0ca6b592015-10-30 16:06:15 -0400130 // The default failure for parsing is invalid binary.
131 return diagnostic(SPV_ERROR_INVALID_BINARY);
132 }
133
David Netod9ad0502015-11-24 18:37:24 -0500134 // Issues a diagnostic describing an exhaustion of input condition when
135 // trying to decode an instruction operand, and returns
136 // SPV_ERROR_INVALID_BINARY.
137 spv_result_t exhaustedInputDiagnostic(size_t inst_offset, SpvOp opcode,
138 spv_operand_type_t type) {
139 return diagnostic() << "End of input reached while decoding Op"
140 << spvOpcodeString(opcode) << " starting at word "
141 << inst_offset
142 << ((_.word_index < _.num_words) ? ": truncated "
143 : ": missing ")
144 << spvOperandTypeStr(type) << " operand at word offset "
145 << _.word_index - inst_offset << ".";
146 }
147
David Neto0ca6b592015-10-30 16:06:15 -0400148 // Returns the endian-corrected word at the current position.
149 uint32_t peek() const { return peekAt(_.word_index); }
150
151 // Returns the endian-corrected word at the given position.
152 uint32_t peekAt(size_t index) const {
153 assert(index < _.num_words);
154 return spvFixWord(_.words[index], _.endian);
155 }
156
157 // Data members
158
159 const libspirv::AssemblyGrammar grammar_; // SPIR-V syntax utility.
Lei Zhang755f97f2016-09-02 18:06:18 -0400160 const spvtools::MessageConsumer& consumer_; // Message consumer callback.
David Neto0ca6b592015-10-30 16:06:15 -0400161 void* const user_data_; // Context for the callbacks
162 const spv_parsed_header_fn_t parsed_header_fn_; // Parsed header callback
163 const spv_parsed_instruction_fn_t
164 parsed_instruction_fn_; // Parsed instruction callback
165
166 // Describes the format of a typed literal number.
167 struct NumberType {
168 spv_number_kind_t type;
169 uint32_t bit_width;
170 };
171
172 // The state used to parse a single SPIR-V binary module.
173 struct State {
174 State(const uint32_t* words_arg, size_t num_words_arg,
175 spv_diagnostic* diagnostic_arg)
176 : words(words_arg),
177 num_words(num_words_arg),
178 diagnostic(diagnostic_arg),
179 word_index(0),
Lei Zhang712bed02016-02-25 16:11:16 -0500180 endian(),
Chris Forbes78338d52017-06-27 16:28:22 -0700181 requires_endian_conversion(false) {
182
183 // Temporary storage for parser state within a single instruction.
184 // Most instructions require fewer than 25 words or operands.
185 operands.reserve(25);
186 endian_converted_words.reserve(25);
187 expected_operands.reserve(25);
188 }
David Neto0ca6b592015-10-30 16:06:15 -0400189 State() : State(0, 0, nullptr) {}
190 const uint32_t* words; // Words in the binary SPIR-V module.
191 size_t num_words; // Number of words in the module.
192 spv_diagnostic* diagnostic; // Where diagnostics go.
193 size_t word_index; // The current position in words.
194 spv_endianness_t endian; // The endianness of the binary.
David Neto7bff3eb2015-11-20 14:21:10 -0500195 // Is the SPIR-V binary in a different endiannes from the host native
196 // endianness?
197 bool requires_endian_conversion;
David Neto0ca6b592015-10-30 16:06:15 -0400198
199 // Maps a result ID to its type ID. By convention:
200 // - a result ID that is a type definition maps to itself.
201 // - a result ID without a type maps to 0. (E.g. for OpLabel)
202 std::unordered_map<uint32_t, uint32_t> id_to_type_id;
203 // Maps a type ID to its number type description.
204 std::unordered_map<uint32_t, NumberType> type_id_to_number_type_info;
205 // Maps an ExtInstImport id to the extended instruction type.
206 std::unordered_map<uint32_t, spv_ext_inst_type_t>
207 import_id_to_ext_inst_type;
Chris Forbesfcd991f2017-06-27 11:00:06 -0700208
209 // Used by parseOperand
210 std::vector<spv_parsed_operand_t> operands;
211 std::vector<uint32_t> endian_converted_words;
Chris Forbes78338d52017-06-27 16:28:22 -0700212 spv_operand_pattern_t expected_operands;
David Neto0ca6b592015-10-30 16:06:15 -0400213 } _;
214};
215
216spv_result_t Parser::parse(const uint32_t* words, size_t num_words,
217 spv_diagnostic* diagnostic_arg) {
218 _ = State(words, num_words, diagnostic_arg);
219
220 const spv_result_t result = parseModule();
221
222 // Clear the module state. The tables might be big.
223 _ = State();
224
225 return result;
226}
227
228spv_result_t Parser::parseModule() {
229 if (!_.words) return diagnostic() << "Missing module.";
230
231 if (_.num_words < SPV_INDEX_INSTRUCTION)
232 return diagnostic() << "Module has incomplete header: only " << _.num_words
233 << " words instead of " << SPV_INDEX_INSTRUCTION;
234
235 // Check the magic number and detect the module's endianness.
Andrew Woloszyn55ecc2e2015-11-11 11:05:07 -0500236 spv_const_binary_t binary{_.words, _.num_words};
David Neto0ca6b592015-10-30 16:06:15 -0400237 if (spvBinaryEndianness(&binary, &_.endian)) {
238 return diagnostic() << "Invalid SPIR-V magic number '" << std::hex
239 << _.words[0] << "'.";
240 }
David Neto7bff3eb2015-11-20 14:21:10 -0500241 _.requires_endian_conversion = !spvIsHostEndian(_.endian);
David Neto0ca6b592015-10-30 16:06:15 -0400242
243 // Process the header.
244 spv_header_t header;
245 if (spvBinaryHeaderGet(&binary, _.endian, &header)) {
246 // It turns out there is no way to trigger this error since the only
247 // failure cases are already handled above, with better messages.
248 return diagnostic(SPV_ERROR_INTERNAL)
249 << "Internal error: unhandled header parse failure";
250 }
251 if (parsed_header_fn_) {
252 if (auto error = parsed_header_fn_(user_data_, _.endian, header.magic,
253 header.version, header.generator,
254 header.bound, header.schema)) {
255 return error;
256 }
257 }
258
259 // Process the instructions.
260 _.word_index = SPV_INDEX_INSTRUCTION;
261 while (_.word_index < _.num_words)
262 if (auto error = parseInstruction()) return error;
263
264 // Running off the end should already have been reported earlier.
265 assert(_.word_index == _.num_words);
266
267 return SPV_SUCCESS;
268}
269
270spv_result_t Parser::parseInstruction() {
271 // The zero values for all members except for opcode are the
272 // correct initial values.
273 spv_parsed_instruction_t inst = {};
David Neto7bff3eb2015-11-20 14:21:10 -0500274
275 const uint32_t first_word = peek();
276
David Neto7bff3eb2015-11-20 14:21:10 -0500277 // If the module's endianness is different from the host native endianness,
278 // then converted_words contains the the endian-translated words in the
279 // instruction.
Chris Forbesfcd991f2017-06-27 11:00:06 -0700280 _.endian_converted_words.clear();
281 _.endian_converted_words.push_back(first_word);
David Neto0ca6b592015-10-30 16:06:15 -0400282
283 // After a successful parse of the instruction, the inst.operands member
284 // will point to this vector's storage.
Chris Forbesfcd991f2017-06-27 11:00:06 -0700285 _.operands.clear();
David Neto0ca6b592015-10-30 16:06:15 -0400286
287 assert(_.word_index < _.num_words);
288 // Decompose and check the first word.
289 uint16_t inst_word_count = 0;
David Neto7bff3eb2015-11-20 14:21:10 -0500290 spvOpcodeSplit(first_word, &inst_word_count, &inst.opcode);
David Neto0ca6b592015-10-30 16:06:15 -0400291 if (inst_word_count < 1) {
292 return diagnostic() << "Invalid instruction word count: "
293 << inst_word_count;
294 }
295 spv_opcode_desc opcode_desc;
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400296 if (grammar_.lookupOpcode(static_cast<SpvOp>(inst.opcode), &opcode_desc))
297 return diagnostic() << "Invalid opcode: " << inst.opcode;
David Neto0ca6b592015-10-30 16:06:15 -0400298
David Neto7bff3eb2015-11-20 14:21:10 -0500299 // Advance past the opcode word. But remember the of the start
300 // of the instruction.
301 const size_t inst_offset = _.word_index;
David Neto0ca6b592015-10-30 16:06:15 -0400302 _.word_index++;
303
304 // Maintains the ordered list of expected operand types.
305 // For many instructions we only need the {numTypes, operandTypes}
306 // entries in opcode_desc. However, sometimes we need to modify
307 // the list as we parse the operands. This occurs when an operand
308 // has its own logical operands (such as the LocalSize operand for
309 // ExecutionMode), or for extended instructions that may have their
310 // own operands depending on the selected extended instruction.
Chris Forbes78338d52017-06-27 16:28:22 -0700311 _.expected_operands.clear();
312 for (auto i = 0; i < opcode_desc->numTypes; i++)
313 _.expected_operands.push_back(opcode_desc->operandTypes[opcode_desc->numTypes - i - 1]);
David Neto0ca6b592015-10-30 16:06:15 -0400314
David Neto7bff3eb2015-11-20 14:21:10 -0500315 while (_.word_index < inst_offset + inst_word_count) {
316 const uint16_t inst_word_index = uint16_t(_.word_index - inst_offset);
Chris Forbes78338d52017-06-27 16:28:22 -0700317 if (_.expected_operands.empty()) {
David Neto0ca6b592015-10-30 16:06:15 -0400318 return diagnostic() << "Invalid instruction Op" << opcode_desc->name
David Neto7bff3eb2015-11-20 14:21:10 -0500319 << " starting at word " << inst_offset
David Neto0ca6b592015-10-30 16:06:15 -0400320 << ": expected no more operands after "
321 << inst_word_index
322 << " words, but stated word count is "
323 << inst_word_count << ".";
324 }
325
Chris Forbes78338d52017-06-27 16:28:22 -0700326 spv_operand_type_t type = spvTakeFirstMatchableOperand(&_.expected_operands);
David Neto0ca6b592015-10-30 16:06:15 -0400327
David Neto7bff3eb2015-11-20 14:21:10 -0500328 if (auto error =
Chris Forbesfcd991f2017-06-27 11:00:06 -0700329 parseOperand(inst_offset, &inst, type, &_.endian_converted_words,
Chris Forbes78338d52017-06-27 16:28:22 -0700330 &_.operands, &_.expected_operands)) {
David Neto0ca6b592015-10-30 16:06:15 -0400331 return error;
David Netod9ad0502015-11-24 18:37:24 -0500332 }
David Neto0ca6b592015-10-30 16:06:15 -0400333 }
334
Chris Forbes78338d52017-06-27 16:28:22 -0700335 if (!_.expected_operands.empty() &&
336 !spvOperandIsOptional(_.expected_operands.back())) {
David Neto0ca6b592015-10-30 16:06:15 -0400337 return diagnostic() << "End of input reached while decoding Op"
338 << opcode_desc->name << " starting at word "
David Neto7bff3eb2015-11-20 14:21:10 -0500339 << inst_offset << ": expected more operands after "
David Neto0ca6b592015-10-30 16:06:15 -0400340 << inst_word_count << " words.";
341 }
342
David Neto7bff3eb2015-11-20 14:21:10 -0500343 if ((inst_offset + inst_word_count) != _.word_index) {
David Netod9ad0502015-11-24 18:37:24 -0500344 return diagnostic() << "Invalid word count: Op" << opcode_desc->name
345 << " starting at word " << inst_offset
346 << " says it has " << inst_word_count
David Neto7bff3eb2015-11-20 14:21:10 -0500347 << " words, but found " << _.word_index - inst_offset
David Neto0ca6b592015-10-30 16:06:15 -0400348 << " words instead.";
349 }
David Neto15afbf92015-11-23 14:17:35 -0500350
351 // Check the computed length of the endian-converted words vector against
352 // the declared number of words in the instruction. If endian conversion
353 // is required, then they should match. If no endian conversion was
354 // performed, then the vector only contains the initial opcode/word-count
355 // word.
356 assert(!_.requires_endian_conversion ||
Chris Forbesfcd991f2017-06-27 11:00:06 -0700357 (inst_word_count == _.endian_converted_words.size()));
358 assert(_.requires_endian_conversion || (_.endian_converted_words.size() == 1));
David Neto0ca6b592015-10-30 16:06:15 -0400359
David Neto7bff3eb2015-11-20 14:21:10 -0500360 recordNumberType(inst_offset, &inst);
David Neto0ca6b592015-10-30 16:06:15 -0400361
David Neto7bff3eb2015-11-20 14:21:10 -0500362 if (_.requires_endian_conversion) {
363 // We must wait until here to set this pointer, because the vector might
364 // have been be resized while we accumulated its elements.
Chris Forbesfcd991f2017-06-27 11:00:06 -0700365 inst.words = _.endian_converted_words.data();
David Neto7bff3eb2015-11-20 14:21:10 -0500366 } else {
367 // If no conversion is required, then just point to the underlying binary.
368 // This saves time and space.
369 inst.words = _.words + inst_offset;
370 }
371 inst.num_words = inst_word_count;
372
373 // We must wait until here to set this pointer, because the vector might
374 // have been be resized while we accumulated its elements.
Chris Forbesfcd991f2017-06-27 11:00:06 -0700375 inst.operands = _.operands.data();
376 inst.num_operands = uint16_t(_.operands.size());
David Neto0ca6b592015-10-30 16:06:15 -0400377
378 // Issue the callback. The callee should know that all the storage in inst
379 // is transient, and will disappear immediately afterward.
380 if (parsed_instruction_fn_) {
381 if (auto error = parsed_instruction_fn_(user_data_, &inst)) return error;
382 }
383
384 return SPV_SUCCESS;
385}
386
David Neto7bff3eb2015-11-20 14:21:10 -0500387spv_result_t Parser::parseOperand(size_t inst_offset,
388 spv_parsed_instruction_t* inst,
David Neto0ca6b592015-10-30 16:06:15 -0400389 const spv_operand_type_t type,
David Neto7bff3eb2015-11-20 14:21:10 -0500390 std::vector<uint32_t>* words,
David Neto0ca6b592015-10-30 16:06:15 -0400391 std::vector<spv_parsed_operand_t>* operands,
392 spv_operand_pattern_t* expected_operands) {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400393 const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
David Neto0ca6b592015-10-30 16:06:15 -0400394 // We'll fill in this result as we go along.
395 spv_parsed_operand_t parsed_operand;
David Neto7bff3eb2015-11-20 14:21:10 -0500396 parsed_operand.offset = uint16_t(_.word_index - inst_offset);
David Neto0ca6b592015-10-30 16:06:15 -0400397 // Most operands occupy one word. This might be be adjusted later.
398 parsed_operand.num_words = 1;
399 // The type argument is the one used by the grammar to parse the instruction.
400 // But it can exposes internal parser details such as whether an operand is
401 // optional or actually represents a variable-length sequence of operands.
402 // The resulting type should be adjusted to avoid those internal details.
403 // In most cases, the resulting operand type is the same as the grammar type.
404 parsed_operand.type = type;
405
406 // Assume non-numeric values. This will be updated for literal numbers.
407 parsed_operand.number_kind = SPV_NUMBER_NONE;
408 parsed_operand.number_bit_width = 0;
409
David Netod9ad0502015-11-24 18:37:24 -0500410 if (_.word_index >= _.num_words)
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400411 return exhaustedInputDiagnostic(inst_offset, opcode, type);
David Netod9ad0502015-11-24 18:37:24 -0500412
David Neto0ca6b592015-10-30 16:06:15 -0400413 const uint32_t word = peek();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100414
David Neto7bff3eb2015-11-20 14:21:10 -0500415 // Do the words in this operand have to be converted to native endianness?
416 // True for all but literal strings.
417 bool convert_operand_endianness = true;
418
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100419 switch (type) {
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400420 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto677e0c72016-01-05 14:56:02 -0500421 if (!word)
422 return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Type Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400423 inst->type_id = word;
424 break;
425
426 case SPV_OPERAND_TYPE_RESULT_ID:
David Neto677e0c72016-01-05 14:56:02 -0500427 if (!word)
428 return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Result Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400429 inst->result_id = word;
430 // Save the result ID to type ID mapping.
431 // In the grammar, type ID always appears before result ID.
432 if (_.id_to_type_id.find(inst->result_id) != _.id_to_type_id.end())
Umar Arshadf76e0f52015-11-18 15:43:43 -0500433 return diagnostic(SPV_ERROR_INVALID_ID) << "Id " << inst->result_id
David Neto677e0c72016-01-05 14:56:02 -0500434 << " is defined more than once";
David Neto0ca6b592015-10-30 16:06:15 -0400435 // Record it.
436 // A regular value maps to its type. Some instructions (e.g. OpLabel)
437 // have no type Id, and will map to 0. The result Id for a
438 // type-generating instruction (e.g. OpTypeInt) maps to itself.
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400439 _.id_to_type_id[inst->result_id] =
440 spvOpcodeGeneratesType(opcode) ? inst->result_id : inst->type_id;
David Neto0ca6b592015-10-30 16:06:15 -0400441 break;
442
443 case SPV_OPERAND_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400444 case SPV_OPERAND_TYPE_OPTIONAL_ID:
Umar Arshadf76e0f52015-11-18 15:43:43 -0500445 if (!word) return diagnostic(SPV_ERROR_INVALID_ID) << "Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400446 parsed_operand.type = SPV_OPERAND_TYPE_ID;
447
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400448 if (opcode == SpvOpExtInst && parsed_operand.offset == 3) {
David Neto0ca6b592015-10-30 16:06:15 -0400449 // The current word is the extended instruction set Id.
450 // Set the extended instruction set type for the current instruction.
451 auto ext_inst_type_iter = _.import_id_to_ext_inst_type.find(word);
452 if (ext_inst_type_iter == _.import_id_to_ext_inst_type.end()) {
Umar Arshadf76e0f52015-11-18 15:43:43 -0500453 return diagnostic(SPV_ERROR_INVALID_ID)
David Neto0ca6b592015-10-30 16:06:15 -0400454 << "OpExtInst set Id " << word
455 << " does not reference an OpExtInstImport result Id";
456 }
457 inst->ext_inst_type = ext_inst_type_iter->second;
458 }
459 break;
460
David Neto64a9be92015-11-18 15:48:32 -0500461 case SPV_OPERAND_TYPE_SCOPE_ID:
462 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
David Netod9ad0502015-11-24 18:37:24 -0500463 // Check for trivially invalid values. The operand descriptions already
464 // have the word "ID" in them.
465 if (!word) return diagnostic() << spvOperandTypeStr(type) << " is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400466 break;
467
David Neto445ce442015-10-15 15:22:06 -0400468 case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400469 assert(SpvOpExtInst == opcode);
David Neto0ca6b592015-10-30 16:06:15 -0400470 assert(inst->ext_inst_type != SPV_EXT_INST_TYPE_NONE);
471 spv_ext_inst_desc ext_inst;
472 if (grammar_.lookupExtInst(inst->ext_inst_type, word, &ext_inst))
473 return diagnostic() << "Invalid extended instruction number: " << word;
Chris Forbes78338d52017-06-27 16:28:22 -0700474 spvPushOperandTypes(ext_inst->operandTypes, expected_operands);
David Neto445ce442015-10-15 15:22:06 -0400475 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400476
David Neto21196942015-11-11 02:45:45 -0500477 case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400478 assert(SpvOpSpecConstantOp == opcode);
David Neto21196942015-11-11 02:45:45 -0500479 if (grammar_.lookupSpecConstantOpcode(SpvOp(word))) {
480 return diagnostic() << "Invalid " << spvOperandTypeStr(type) << ": "
481 << word;
482 }
483 spv_opcode_desc opcode_entry = nullptr;
484 if (grammar_.lookupOpcode(SpvOp(word), &opcode_entry)) {
485 return diagnostic(SPV_ERROR_INTERNAL)
486 << "OpSpecConstant opcode table out of sync";
487 }
488 // OpSpecConstant opcodes must have a type and result. We've already
489 // processed them, so skip them when preparing to parse the other
490 // operants for the opcode.
491 assert(opcode_entry->hasType);
492 assert(opcode_entry->hasResult);
493 assert(opcode_entry->numTypes >= 2);
Chris Forbes78338d52017-06-27 16:28:22 -0700494 spvPushOperandTypes(opcode_entry->operandTypes + 2, expected_operands);
David Neto21196942015-11-11 02:45:45 -0500495 } break;
496
David Neto445ce442015-10-15 15:22:06 -0400497 case SPV_OPERAND_TYPE_LITERAL_INTEGER:
Lei Zhang6483bd72015-10-14 17:02:39 -0400498 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER:
David Neto201caf72015-11-04 17:38:17 -0500499 // These are regular single-word literal integer operands.
500 // Post-parsing validation should check the range of the parsed value.
David Neto0ca6b592015-10-30 16:06:15 -0400501 parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_INTEGER;
David Neto201caf72015-11-04 17:38:17 -0500502 // It turns out they are always unsigned integers!
503 parsed_operand.number_kind = SPV_NUMBER_UNSIGNED_INT;
504 parsed_operand.number_bit_width = 32;
505 break;
506
507 case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER:
508 case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
Lei Zhangaa3cd5a2015-11-10 14:29:35 -0500509 parsed_operand.type = SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER;
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400510 if (opcode == SpvOpSwitch) {
David Neto0ca6b592015-10-30 16:06:15 -0400511 // The literal operands have the same type as the value
512 // referenced by the selector Id.
David Neto7bff3eb2015-11-20 14:21:10 -0500513 const uint32_t selector_id = peekAt(inst_offset + 1);
David Neto3664bd52015-12-23 13:21:43 -0500514 const auto type_id_iter = _.id_to_type_id.find(selector_id);
515 if (type_id_iter == _.id_to_type_id.end() ||
516 type_id_iter->second == 0) {
David Neto0ca6b592015-10-30 16:06:15 -0400517 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
518 << " has no type";
519 }
520 uint32_t type_id = type_id_iter->second;
521
522 if (selector_id == type_id) {
523 // Recall that by convention, a result ID that is a type definition
524 // maps to itself.
525 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
526 << " is a type, not a value";
527 }
528 if (auto error = setNumericTypeInfoForType(&parsed_operand, type_id))
529 return error;
530 if (parsed_operand.number_kind != SPV_NUMBER_UNSIGNED_INT &&
531 parsed_operand.number_kind != SPV_NUMBER_SIGNED_INT) {
532 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
533 << " is not a scalar integer";
534 }
Lei Zhangb41d1502015-09-14 15:22:23 -0400535 } else {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400536 assert(opcode == SpvOpConstant || opcode == SpvOpSpecConstant);
David Neto201caf72015-11-04 17:38:17 -0500537 // The literal number type is determined by the type Id for the
538 // constant.
539 assert(inst->type_id);
540 if (auto error =
541 setNumericTypeInfoForType(&parsed_operand, inst->type_id))
542 return error;
Lei Zhangb41d1502015-09-14 15:22:23 -0400543 }
David Neto0ca6b592015-10-30 16:06:15 -0400544 break;
545
David Neto78c3b432015-08-27 13:03:52 -0400546 case SPV_OPERAND_TYPE_LITERAL_STRING:
547 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
David Neto7bff3eb2015-11-20 14:21:10 -0500548 convert_operand_endianness = false;
David Neto0ca6b592015-10-30 16:06:15 -0400549 const char* string =
550 reinterpret_cast<const char*>(_.words + _.word_index);
David Netod9ad0502015-11-24 18:37:24 -0500551 // Compute the length of the string, but make sure we don't run off the
552 // end of the input.
553 const size_t remaining_input_bytes =
554 sizeof(uint32_t) * (_.num_words - _.word_index);
555 const size_t string_num_content_bytes =
David Neto37422e92016-12-19 13:26:42 -0500556 spv_strnlen_s(string, remaining_input_bytes);
David Netod9ad0502015-11-24 18:37:24 -0500557 // If there was no terminating null byte, then that's an end-of-input
558 // error.
559 if (string_num_content_bytes == remaining_input_bytes)
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400560 return exhaustedInputDiagnostic(inst_offset, opcode, type);
David Netod9ad0502015-11-24 18:37:24 -0500561 // Account for null in the word length, so add 1 for null, then add 3 to
562 // make sure we round up. The following is equivalent to:
563 // (string_num_content_bytes + 1 + 3) / 4
564 const size_t string_num_words = string_num_content_bytes / 4 + 1;
David Neto0ca6b592015-10-30 16:06:15 -0400565 // Make sure we can record the word count without overflow.
David Netod9ad0502015-11-24 18:37:24 -0500566 //
567 // This error can't currently be triggered because of validity
568 // checks elsewhere.
David Neto0ca6b592015-10-30 16:06:15 -0400569 if (string_num_words > std::numeric_limits<uint16_t>::max()) {
570 return diagnostic() << "Literal string is longer than "
571 << std::numeric_limits<uint16_t>::max()
572 << " words: " << string_num_words << " words long";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100573 }
Andrew Woloszyn3a4bc7e2015-11-19 09:22:53 -0500574 parsed_operand.num_words = uint16_t(string_num_words);
David Neto0ca6b592015-10-30 16:06:15 -0400575 parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_STRING;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100576
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400577 if (SpvOpExtInstImport == opcode) {
David Neto0ca6b592015-10-30 16:06:15 -0400578 // Record the extended instruction type for the ID for this import.
579 // There is only one string literal argument to OpExtInstImport,
580 // so it's sufficient to guard this just on the opcode.
581 const spv_ext_inst_type_t ext_inst_type =
582 spvExtInstImportTypeGet(string);
583 if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
584 return diagnostic() << "Invalid extended instruction import '"
585 << string << "'";
Andrew Woloszyne59e6b72015-10-14 14:18:43 -0400586 }
David Neto0ca6b592015-10-30 16:06:15 -0400587 // We must have parsed a valid result ID. It's a condition
588 // of the grammar, and we only accept non-zero result Ids.
589 assert(inst->result_id);
590 _.import_id_to_ext_inst_type[inst->result_id] = ext_inst_type;
Andrew Woloszyne59e6b72015-10-14 14:18:43 -0400591 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100592 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400593
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100594 case SPV_OPERAND_TYPE_CAPABILITY:
595 case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
596 case SPV_OPERAND_TYPE_EXECUTION_MODEL:
597 case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
598 case SPV_OPERAND_TYPE_MEMORY_MODEL:
599 case SPV_OPERAND_TYPE_EXECUTION_MODE:
600 case SPV_OPERAND_TYPE_STORAGE_CLASS:
601 case SPV_OPERAND_TYPE_DIMENSIONALITY:
602 case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
603 case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
David Netod9ad0502015-11-24 18:37:24 -0500604 case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100605 case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
606 case SPV_OPERAND_TYPE_LINKAGE_TYPE:
607 case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
David Neto2889a0c2016-02-15 13:50:00 -0500608 case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100609 case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
610 case SPV_OPERAND_TYPE_DECORATION:
611 case SPV_OPERAND_TYPE_BUILT_IN:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100612 case SPV_OPERAND_TYPE_GROUP_OPERATION:
613 case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
David Neto47994822015-08-27 13:11:01 -0400614 case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: {
David Neto0ca6b592015-10-30 16:06:15 -0400615 // A single word that is a plain enum value.
David Neto2889a0c2016-02-15 13:50:00 -0500616
617 // Map an optional operand type to its corresponding concrete type.
618 if (type == SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER)
619 parsed_operand.type = SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
620
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100621 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400622 if (grammar_.lookupOperand(type, word, &entry)) {
David Neto201caf72015-11-04 17:38:17 -0500623 return diagnostic() << "Invalid "
624 << spvOperandTypeStr(parsed_operand.type)
David Neto0ca6b592015-10-30 16:06:15 -0400625 << " operand: " << word;
Lei Zhang40056702015-09-11 14:31:27 -0400626 }
David Neto78c3b432015-08-27 13:03:52 -0400627 // Prepare to accept operands to this operand, if needed.
Chris Forbes78338d52017-06-27 16:28:22 -0700628 spvPushOperandTypes(entry->operandTypes, expected_operands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100629 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400630
David Neto619db262015-09-25 12:43:37 -0400631 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
632 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
633 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Neto2889a0c2016-02-15 13:50:00 -0500634 case SPV_OPERAND_TYPE_IMAGE:
David Neto619db262015-09-25 12:43:37 -0400635 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
636 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
637 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
638 // This operand is a mask.
David Neto201caf72015-11-04 17:38:17 -0500639
640 // Map an optional operand type to its corresponding concrete type.
641 if (type == SPV_OPERAND_TYPE_OPTIONAL_IMAGE)
642 parsed_operand.type = SPV_OPERAND_TYPE_IMAGE;
643 else if (type == SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS)
644 parsed_operand.type = SPV_OPERAND_TYPE_MEMORY_ACCESS;
645
David Neto0ca6b592015-10-30 16:06:15 -0400646 // Check validity of set mask bits. Also prepare for operands for those
647 // masks if they have any. To get operand order correct, scan from
648 // MSB to LSB since we can only prepend operands to a pattern.
649 // The only case in the grammar where you have more than one mask bit
650 // having an operand is for image operands. See SPIR-V 3.14 Image
651 // Operands.
652 uint32_t remaining_word = word;
653 for (uint32_t mask = (1u << 31); remaining_word; mask >>= 1) {
David Neto619db262015-09-25 12:43:37 -0400654 if (remaining_word & mask) {
David Neto619db262015-09-25 12:43:37 -0400655 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400656 if (grammar_.lookupOperand(type, mask, &entry)) {
David Neto201caf72015-11-04 17:38:17 -0500657 return diagnostic()
658 << "Invalid " << spvOperandTypeStr(parsed_operand.type)
659 << " operand: " << word << " has invalid mask component "
660 << mask;
David Neto619db262015-09-25 12:43:37 -0400661 }
David Neto0ca6b592015-10-30 16:06:15 -0400662 remaining_word ^= mask;
Chris Forbes78338d52017-06-27 16:28:22 -0700663 spvPushOperandTypes(entry->operandTypes, expected_operands);
David Neto619db262015-09-25 12:43:37 -0400664 }
665 }
David Neto0ca6b592015-10-30 16:06:15 -0400666 if (word == 0) {
667 // An all-zeroes mask *might* also be valid.
David Neto619db262015-09-25 12:43:37 -0400668 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400669 if (SPV_SUCCESS == grammar_.lookupOperand(type, 0, &entry)) {
David Neto619db262015-09-25 12:43:37 -0400670 // Prepare for its operands, if any.
Chris Forbes78338d52017-06-27 16:28:22 -0700671 spvPushOperandTypes(entry->operandTypes, expected_operands);
David Neto619db262015-09-25 12:43:37 -0400672 }
673 }
David Neto619db262015-09-25 12:43:37 -0400674 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400675 default:
676 return diagnostic() << "Internal error: Unhandled operand type: " << type;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100677 }
678
David Neto0ca6b592015-10-30 16:06:15 -0400679 assert(int(SPV_OPERAND_TYPE_FIRST_CONCRETE_TYPE) <= int(parsed_operand.type));
680 assert(int(SPV_OPERAND_TYPE_LAST_CONCRETE_TYPE) >= int(parsed_operand.type));
681
682 operands->push_back(parsed_operand);
683
David Neto7bff3eb2015-11-20 14:21:10 -0500684 const size_t index_after_operand = _.word_index + parsed_operand.num_words;
David Netod9ad0502015-11-24 18:37:24 -0500685
686 // Avoid buffer overrun for the cases where the operand has more than one
687 // word, and where it isn't a string. (Those other cases have already been
688 // handled earlier.) For example, this error can occur for a multi-word
689 // argument to OpConstant, or a multi-word case literal operand for OpSwitch.
690 if (_.num_words < index_after_operand)
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400691 return exhaustedInputDiagnostic(inst_offset, opcode, type);
David Netod9ad0502015-11-24 18:37:24 -0500692
David Neto7bff3eb2015-11-20 14:21:10 -0500693 if (_.requires_endian_conversion) {
694 // Copy instruction words. Translate to native endianness as needed.
695 if (convert_operand_endianness) {
696 const spv_endianness_t endianness = _.endian;
697 std::transform(_.words + _.word_index, _.words + index_after_operand,
Andrew Woloszyn3b69d052016-01-11 13:54:30 -0500698 std::back_inserter(*words),
699 [endianness](const uint32_t raw_word) {
David Neto677e0c72016-01-05 14:56:02 -0500700 return spvFixWord(raw_word, endianness);
David Neto7bff3eb2015-11-20 14:21:10 -0500701 });
702 } else {
703 words->insert(words->end(), _.words + _.word_index,
704 _.words + index_after_operand);
705 }
706 }
707
708 // Advance past the operand.
709 _.word_index = index_after_operand;
David Neto0ca6b592015-10-30 16:06:15 -0400710
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100711 return SPV_SUCCESS;
712}
713
David Neto0ca6b592015-10-30 16:06:15 -0400714spv_result_t Parser::setNumericTypeInfoForType(
715 spv_parsed_operand_t* parsed_operand, uint32_t type_id) {
David Neto201caf72015-11-04 17:38:17 -0500716 assert(type_id != 0);
David Neto0ca6b592015-10-30 16:06:15 -0400717 auto type_info_iter = _.type_id_to_number_type_info.find(type_id);
718 if (type_info_iter == _.type_id_to_number_type_info.end()) {
719 return diagnostic() << "Type Id " << type_id << " is not a type";
720 }
721 const NumberType& info = type_info_iter->second;
722 if (info.type == SPV_NUMBER_NONE) {
723 // This is a valid type, but for something other than a scalar number.
724 return diagnostic() << "Type Id " << type_id
725 << " is not a scalar numeric type";
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400726 }
727
David Neto0ca6b592015-10-30 16:06:15 -0400728 parsed_operand->number_kind = info.type;
729 parsed_operand->number_bit_width = info.bit_width;
David Neto066bd522016-01-05 14:57:58 -0500730 // Round up the word count.
731 parsed_operand->num_words = static_cast<uint16_t>((info.bit_width + 31) / 32);
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400732 return SPV_SUCCESS;
733}
734
David Neto7bff3eb2015-11-20 14:21:10 -0500735void Parser::recordNumberType(size_t inst_offset,
736 const spv_parsed_instruction_t* inst) {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400737 const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
738 if (spvOpcodeGeneratesType(opcode)) {
David Neto0ca6b592015-10-30 16:06:15 -0400739 NumberType info = {SPV_NUMBER_NONE, 0};
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400740 if (SpvOpTypeInt == opcode) {
David Neto7bff3eb2015-11-20 14:21:10 -0500741 const bool is_signed = peekAt(inst_offset + 3) != 0;
David Neto0ca6b592015-10-30 16:06:15 -0400742 info.type = is_signed ? SPV_NUMBER_SIGNED_INT : SPV_NUMBER_UNSIGNED_INT;
David Neto7bff3eb2015-11-20 14:21:10 -0500743 info.bit_width = peekAt(inst_offset + 2);
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400744 } else if (SpvOpTypeFloat == opcode) {
David Neto0ca6b592015-10-30 16:06:15 -0400745 info.type = SPV_NUMBER_FLOATING;
David Neto7bff3eb2015-11-20 14:21:10 -0500746 info.bit_width = peekAt(inst_offset + 2);
Lei Zhang40056702015-09-11 14:31:27 -0400747 }
David Neto0ca6b592015-10-30 16:06:15 -0400748 // The *result* Id of a type generating instruction is the type Id.
749 _.type_id_to_number_type_info[inst->result_id] = info;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100750 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100751}
752
David Neto0ca6b592015-10-30 16:06:15 -0400753} // anonymous namespace
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400754
Lei Zhang972788b2015-11-12 13:48:30 -0500755spv_result_t spvBinaryParse(const spv_const_context context, void* user_data,
756 const uint32_t* code, const size_t num_words,
David Neto0ca6b592015-10-30 16:06:15 -0400757 spv_parsed_header_fn_t parsed_header,
758 spv_parsed_instruction_fn_t parsed_instruction,
759 spv_diagnostic* diagnostic) {
Lei Zhang755f97f2016-09-02 18:06:18 -0400760 spv_context_t hijack_context = *context;
761 if (diagnostic) {
762 *diagnostic = nullptr;
763 libspirv::UseDiagnosticAsMessageConsumer(&hijack_context, diagnostic);
764 }
765 Parser parser(&hijack_context, user_data, parsed_header, parsed_instruction);
David Neto0ca6b592015-10-30 16:06:15 -0400766 return parser.parse(code, num_words, diagnostic);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100767}
768
David Neto0ca6b592015-10-30 16:06:15 -0400769// TODO(dneto): This probably belongs in text.cpp since that's the only place
770// that a spv_binary_t value is created.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100771void spvBinaryDestroy(spv_binary binary) {
Lei Zhang40056702015-09-11 14:31:27 -0400772 if (!binary) return;
Eric Engestromeb6ae972016-02-18 23:41:16 +0000773 delete[] binary->code;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100774 delete binary;
775}
David Neto37422e92016-12-19 13:26:42 -0500776
777size_t spv_strnlen_s(const char* str, size_t strsz) {
778 if (!str) return 0;
779 for (size_t i = 0; i < strsz; i++) {
780 if (!str[i]) return i;
781 }
782 return strsz;
783}