blob: 5c6aa8fcec7a292b44e328b260224df82ed8b21c [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//
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>
Andrew Woloszyn7ffd8ff2016-01-11 16:22:34 -050032#include <iterator>
David Neto0ca6b592015-10-30 16:06:15 -040033#include <limits>
David Netofcc7d582015-10-27 15:31:10 -040034#include <unordered_map>
35
David Netofcc7d582015-10-27 15:31:10 -040036#include "assembly_grammar.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010037#include "diagnostic.h"
38#include "ext_inst.h"
39#include "opcode.h"
40#include "operand.h"
David Neto5a703352016-02-17 14:44:00 -050041#include "spirv-tools/libspirv.h"
Lei Zhangaa056cd2015-11-11 14:24:04 -050042#include "spirv_constant.h"
David Neto4c215712015-12-22 15:08:41 -050043#include "spirv_endian.h"
Andrew Woloszyn157e41b2015-10-16 15:11:00 -040044
Andrew Woloszyn55ecc2e2015-11-11 11:05:07 -050045spv_result_t spvBinaryHeaderGet(const spv_const_binary binary,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010046 const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -050047 spv_header_t* pHeader) {
David Neto0ca6b592015-10-30 16:06:15 -040048 if (!binary->code) return SPV_ERROR_INVALID_BINARY;
49 if (binary->wordCount < SPV_INDEX_INSTRUCTION)
50 return SPV_ERROR_INVALID_BINARY;
Lei Zhang40056702015-09-11 14:31:27 -040051 if (!pHeader) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010052
53 // TODO: Validation checking?
54 pHeader->magic = spvFixWord(binary->code[SPV_INDEX_MAGIC_NUMBER], endian);
55 pHeader->version = spvFixWord(binary->code[SPV_INDEX_VERSION_NUMBER], endian);
56 pHeader->generator =
57 spvFixWord(binary->code[SPV_INDEX_GENERATOR_NUMBER], endian);
58 pHeader->bound = spvFixWord(binary->code[SPV_INDEX_BOUND], endian);
59 pHeader->schema = spvFixWord(binary->code[SPV_INDEX_SCHEMA], endian);
60 pHeader->instructions = &binary->code[SPV_INDEX_INSTRUCTION];
61
62 return SPV_SUCCESS;
63}
64
David Neto0ca6b592015-10-30 16:06:15 -040065namespace {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010066
David Neto0ca6b592015-10-30 16:06:15 -040067// A SPIR-V binary parser. A parser instance communicates detailed parse
68// results via callbacks.
69class Parser {
70 public:
71 // The user_data value is provided to the callbacks as context.
Lei Zhang972788b2015-11-12 13:48:30 -050072 Parser(const spv_const_context context, void* user_data,
73 spv_parsed_header_fn_t parsed_header_fn,
David Neto0ca6b592015-10-30 16:06:15 -040074 spv_parsed_instruction_fn_t parsed_instruction_fn)
Lei Zhang972788b2015-11-12 13:48:30 -050075 : grammar_(context),
76 user_data_(user_data),
David Neto0ca6b592015-10-30 16:06:15 -040077 parsed_header_fn_(parsed_header_fn),
78 parsed_instruction_fn_(parsed_instruction_fn) {}
79
80 // Parses the specified binary SPIR-V module, issuing callbacks on a parsed
81 // header and for each parsed instruction. Returns SPV_SUCCESS on success.
82 // Otherwise returns an error code and issues a diagnostic.
83 spv_result_t parse(const uint32_t* words, size_t num_words,
84 spv_diagnostic* diagnostic);
85
86 private:
87 // All remaining methods work on the current module parse state.
88
89 // Like the parse method, but works on the current module parse state.
90 spv_result_t parseModule();
91
92 // Parses an instruction at the current position of the binary. Assumes
93 // the header has been parsed, the endian has been set, and the word index is
94 // still in range. Advances the parsing position past the instruction, and
95 // updates other parsing state for the current module.
96 // On success, returns SPV_SUCCESS and issues the parsed-instruction callback.
97 // On failure, returns an error code and issues a diagnostic.
98 spv_result_t parseInstruction();
99
David Neto7bff3eb2015-11-20 14:21:10 -0500100 // Parses an instruction operand with the given type, for an instruction
101 // starting at inst_offset words into the SPIR-V binary.
102 // If the SPIR-V binary is the same endianness as the host, then the
103 // endian_converted_inst_words parameter is ignored. Otherwise, this method
104 // appends the words for this operand, converted to host native endianness,
105 // to the end of endian_converted_inst_words. This method also updates the
106 // expected_operands parameter, and the scalar members of the inst parameter.
107 // On success, returns SPV_SUCCESS, advances past the operand, and pushes a
108 // new entry on to the operands vector. Otherwise returns an error code and
109 // issues a diagnostic.
110 spv_result_t parseOperand(size_t inst_offset, spv_parsed_instruction_t* inst,
David Neto0ca6b592015-10-30 16:06:15 -0400111 const spv_operand_type_t type,
David Neto7bff3eb2015-11-20 14:21:10 -0500112 std::vector<uint32_t>* endian_converted_inst_words,
David Neto0ca6b592015-10-30 16:06:15 -0400113 std::vector<spv_parsed_operand_t>* operands,
114 spv_operand_pattern_t* expected_operands);
115
116 // Records the numeric type for an operand according to the type information
117 // associated with the given non-zero type Id. This can fail if the type Id
118 // is not a type Id, or if the type Id does not reference a scalar numeric
119 // type. On success, return SPV_SUCCESS and populates the num_words,
120 // number_kind, and number_bit_width fields of parsed_operand.
121 spv_result_t setNumericTypeInfoForType(spv_parsed_operand_t* parsed_operand,
122 uint32_t type_id);
123
David Neto7bff3eb2015-11-20 14:21:10 -0500124 // Records the number type for an instruction at the given offset, if that
125 // instruction generates a type. For types that aren't scalar numbers,
126 // record something with number kind SPV_NUMBER_NONE.
127 void recordNumberType(size_t inst_offset,
128 const spv_parsed_instruction_t* inst);
David Neto0ca6b592015-10-30 16:06:15 -0400129
130 // Returns a diagnostic stream object initialized with current position in
131 // the input stream, and for the given error code. Any data written to the
132 // returned object will be propagated to the current parse's diagnostic
133 // object.
David Neto01656362015-11-20 10:44:41 -0500134 libspirv::DiagnosticStream diagnostic(spv_result_t error) {
David Neto7bff3eb2015-11-20 14:21:10 -0500135 return libspirv::DiagnosticStream({0, 0, _.word_index}, _.diagnostic,
136 error);
David Neto0ca6b592015-10-30 16:06:15 -0400137 }
138
139 // Returns a diagnostic stream object with the default parse error code.
David Neto01656362015-11-20 10:44:41 -0500140 libspirv::DiagnosticStream diagnostic() {
David Neto0ca6b592015-10-30 16:06:15 -0400141 // The default failure for parsing is invalid binary.
142 return diagnostic(SPV_ERROR_INVALID_BINARY);
143 }
144
David Netod9ad0502015-11-24 18:37:24 -0500145 // Issues a diagnostic describing an exhaustion of input condition when
146 // trying to decode an instruction operand, and returns
147 // SPV_ERROR_INVALID_BINARY.
148 spv_result_t exhaustedInputDiagnostic(size_t inst_offset, SpvOp opcode,
149 spv_operand_type_t type) {
150 return diagnostic() << "End of input reached while decoding Op"
151 << spvOpcodeString(opcode) << " starting at word "
152 << inst_offset
153 << ((_.word_index < _.num_words) ? ": truncated "
154 : ": missing ")
155 << spvOperandTypeStr(type) << " operand at word offset "
156 << _.word_index - inst_offset << ".";
157 }
158
David Neto0ca6b592015-10-30 16:06:15 -0400159 // Returns the endian-corrected word at the current position.
160 uint32_t peek() const { return peekAt(_.word_index); }
161
162 // Returns the endian-corrected word at the given position.
163 uint32_t peekAt(size_t index) const {
164 assert(index < _.num_words);
165 return spvFixWord(_.words[index], _.endian);
166 }
167
168 // Data members
169
170 const libspirv::AssemblyGrammar grammar_; // SPIR-V syntax utility.
171 void* const user_data_; // Context for the callbacks
172 const spv_parsed_header_fn_t parsed_header_fn_; // Parsed header callback
173 const spv_parsed_instruction_fn_t
174 parsed_instruction_fn_; // Parsed instruction callback
175
176 // Describes the format of a typed literal number.
177 struct NumberType {
178 spv_number_kind_t type;
179 uint32_t bit_width;
180 };
181
182 // The state used to parse a single SPIR-V binary module.
183 struct State {
184 State(const uint32_t* words_arg, size_t num_words_arg,
185 spv_diagnostic* diagnostic_arg)
186 : words(words_arg),
187 num_words(num_words_arg),
188 diagnostic(diagnostic_arg),
189 word_index(0),
Lei Zhang712bed02016-02-25 16:11:16 -0500190 endian(),
191 requires_endian_conversion(false) {}
David Neto0ca6b592015-10-30 16:06:15 -0400192 State() : State(0, 0, nullptr) {}
193 const uint32_t* words; // Words in the binary SPIR-V module.
194 size_t num_words; // Number of words in the module.
195 spv_diagnostic* diagnostic; // Where diagnostics go.
196 size_t word_index; // The current position in words.
197 spv_endianness_t endian; // The endianness of the binary.
David Neto7bff3eb2015-11-20 14:21:10 -0500198 // Is the SPIR-V binary in a different endiannes from the host native
199 // endianness?
200 bool requires_endian_conversion;
David Neto0ca6b592015-10-30 16:06:15 -0400201
202 // Maps a result ID to its type ID. By convention:
203 // - a result ID that is a type definition maps to itself.
204 // - a result ID without a type maps to 0. (E.g. for OpLabel)
205 std::unordered_map<uint32_t, uint32_t> id_to_type_id;
206 // Maps a type ID to its number type description.
207 std::unordered_map<uint32_t, NumberType> type_id_to_number_type_info;
208 // Maps an ExtInstImport id to the extended instruction type.
209 std::unordered_map<uint32_t, spv_ext_inst_type_t>
210 import_id_to_ext_inst_type;
211 } _;
212};
213
214spv_result_t Parser::parse(const uint32_t* words, size_t num_words,
215 spv_diagnostic* diagnostic_arg) {
216 _ = State(words, num_words, diagnostic_arg);
217
218 const spv_result_t result = parseModule();
219
220 // Clear the module state. The tables might be big.
221 _ = State();
222
223 return result;
224}
225
226spv_result_t Parser::parseModule() {
227 if (!_.words) return diagnostic() << "Missing module.";
228
229 if (_.num_words < SPV_INDEX_INSTRUCTION)
230 return diagnostic() << "Module has incomplete header: only " << _.num_words
231 << " words instead of " << SPV_INDEX_INSTRUCTION;
232
233 // Check the magic number and detect the module's endianness.
Andrew Woloszyn55ecc2e2015-11-11 11:05:07 -0500234 spv_const_binary_t binary{_.words, _.num_words};
David Neto0ca6b592015-10-30 16:06:15 -0400235 if (spvBinaryEndianness(&binary, &_.endian)) {
236 return diagnostic() << "Invalid SPIR-V magic number '" << std::hex
237 << _.words[0] << "'.";
238 }
David Neto7bff3eb2015-11-20 14:21:10 -0500239 _.requires_endian_conversion = !spvIsHostEndian(_.endian);
David Neto0ca6b592015-10-30 16:06:15 -0400240
241 // Process the header.
242 spv_header_t header;
243 if (spvBinaryHeaderGet(&binary, _.endian, &header)) {
244 // It turns out there is no way to trigger this error since the only
245 // failure cases are already handled above, with better messages.
246 return diagnostic(SPV_ERROR_INTERNAL)
247 << "Internal error: unhandled header parse failure";
248 }
249 if (parsed_header_fn_) {
250 if (auto error = parsed_header_fn_(user_data_, _.endian, header.magic,
251 header.version, header.generator,
252 header.bound, header.schema)) {
253 return error;
254 }
255 }
256
257 // Process the instructions.
258 _.word_index = SPV_INDEX_INSTRUCTION;
259 while (_.word_index < _.num_words)
260 if (auto error = parseInstruction()) return error;
261
262 // Running off the end should already have been reported earlier.
263 assert(_.word_index == _.num_words);
264
265 return SPV_SUCCESS;
266}
267
268spv_result_t Parser::parseInstruction() {
269 // The zero values for all members except for opcode are the
270 // correct initial values.
271 spv_parsed_instruction_t inst = {};
David Neto7bff3eb2015-11-20 14:21:10 -0500272
273 const uint32_t first_word = peek();
274
275 // TODO(dneto): If it's too expensive to construct the following "words"
276 // and "operands" vectors for each instruction, each instruction, then make
277 // them class data members instead, and clear them here.
278
279 // If the module's endianness is different from the host native endianness,
280 // then converted_words contains the the endian-translated words in the
281 // instruction.
282 std::vector<uint32_t> endian_converted_words = {first_word};
283 if (_.requires_endian_conversion) {
284 // Most instructions have fewer than 25 words.
285 endian_converted_words.reserve(25);
286 }
David Neto0ca6b592015-10-30 16:06:15 -0400287
288 // After a successful parse of the instruction, the inst.operands member
289 // will point to this vector's storage.
David Neto0ca6b592015-10-30 16:06:15 -0400290 std::vector<spv_parsed_operand_t> operands;
291 // Most instructions have fewer than 25 logical operands.
292 operands.reserve(25);
293
294 assert(_.word_index < _.num_words);
295 // Decompose and check the first word.
296 uint16_t inst_word_count = 0;
David Neto7bff3eb2015-11-20 14:21:10 -0500297 spvOpcodeSplit(first_word, &inst_word_count, &inst.opcode);
David Neto0ca6b592015-10-30 16:06:15 -0400298 if (inst_word_count < 1) {
299 return diagnostic() << "Invalid instruction word count: "
300 << inst_word_count;
301 }
302 spv_opcode_desc opcode_desc;
303 if (grammar_.lookupOpcode(inst.opcode, &opcode_desc))
304 return diagnostic() << "Invalid opcode: " << int(inst.opcode);
305
David Neto7bff3eb2015-11-20 14:21:10 -0500306 // Advance past the opcode word. But remember the of the start
307 // of the instruction.
308 const size_t inst_offset = _.word_index;
David Neto0ca6b592015-10-30 16:06:15 -0400309 _.word_index++;
310
311 // Maintains the ordered list of expected operand types.
312 // For many instructions we only need the {numTypes, operandTypes}
313 // entries in opcode_desc. However, sometimes we need to modify
314 // the list as we parse the operands. This occurs when an operand
315 // has its own logical operands (such as the LocalSize operand for
316 // ExecutionMode), or for extended instructions that may have their
317 // own operands depending on the selected extended instruction.
318 spv_operand_pattern_t expected_operands(
319 opcode_desc->operandTypes,
320 opcode_desc->operandTypes + opcode_desc->numTypes);
321
David Neto7bff3eb2015-11-20 14:21:10 -0500322 while (_.word_index < inst_offset + inst_word_count) {
323 const uint16_t inst_word_index = uint16_t(_.word_index - inst_offset);
David Neto0ca6b592015-10-30 16:06:15 -0400324 if (expected_operands.empty()) {
325 return diagnostic() << "Invalid instruction Op" << opcode_desc->name
David Neto7bff3eb2015-11-20 14:21:10 -0500326 << " starting at word " << inst_offset
David Neto0ca6b592015-10-30 16:06:15 -0400327 << ": expected no more operands after "
328 << inst_word_index
329 << " words, but stated word count is "
330 << inst_word_count << ".";
331 }
332
333 spv_operand_type_t type = spvTakeFirstMatchableOperand(&expected_operands);
334
David Neto7bff3eb2015-11-20 14:21:10 -0500335 if (auto error =
336 parseOperand(inst_offset, &inst, type, &endian_converted_words,
David Netod9ad0502015-11-24 18:37:24 -0500337 &operands, &expected_operands)) {
David Neto0ca6b592015-10-30 16:06:15 -0400338 return error;
David Netod9ad0502015-11-24 18:37:24 -0500339 }
David Neto0ca6b592015-10-30 16:06:15 -0400340 }
341
342 if (!expected_operands.empty() &&
343 !spvOperandIsOptional(expected_operands.front())) {
344 return diagnostic() << "End of input reached while decoding Op"
345 << opcode_desc->name << " starting at word "
David Neto7bff3eb2015-11-20 14:21:10 -0500346 << inst_offset << ": expected more operands after "
David Neto0ca6b592015-10-30 16:06:15 -0400347 << inst_word_count << " words.";
348 }
349
David Neto7bff3eb2015-11-20 14:21:10 -0500350 if ((inst_offset + inst_word_count) != _.word_index) {
David Netod9ad0502015-11-24 18:37:24 -0500351 return diagnostic() << "Invalid word count: Op" << opcode_desc->name
352 << " starting at word " << inst_offset
353 << " says it has " << inst_word_count
David Neto7bff3eb2015-11-20 14:21:10 -0500354 << " words, but found " << _.word_index - inst_offset
David Neto0ca6b592015-10-30 16:06:15 -0400355 << " words instead.";
356 }
David Neto15afbf92015-11-23 14:17:35 -0500357
358 // Check the computed length of the endian-converted words vector against
359 // the declared number of words in the instruction. If endian conversion
360 // is required, then they should match. If no endian conversion was
361 // performed, then the vector only contains the initial opcode/word-count
362 // word.
363 assert(!_.requires_endian_conversion ||
364 (inst_word_count == endian_converted_words.size()));
365 assert(_.requires_endian_conversion || (endian_converted_words.size() == 1));
David Neto0ca6b592015-10-30 16:06:15 -0400366
David Neto7bff3eb2015-11-20 14:21:10 -0500367 recordNumberType(inst_offset, &inst);
David Neto0ca6b592015-10-30 16:06:15 -0400368
David Neto7bff3eb2015-11-20 14:21:10 -0500369 if (_.requires_endian_conversion) {
370 // We must wait until here to set this pointer, because the vector might
371 // have been be resized while we accumulated its elements.
372 inst.words = endian_converted_words.data();
373 } else {
374 // If no conversion is required, then just point to the underlying binary.
375 // This saves time and space.
376 inst.words = _.words + inst_offset;
377 }
378 inst.num_words = inst_word_count;
379
380 // We must wait until here to set this pointer, because the vector might
381 // have been be resized while we accumulated its elements.
David Neto0ca6b592015-10-30 16:06:15 -0400382 inst.operands = operands.data();
Andrew Woloszyn3a4bc7e2015-11-19 09:22:53 -0500383 inst.num_operands = uint16_t(operands.size());
David Neto0ca6b592015-10-30 16:06:15 -0400384
385 // Issue the callback. The callee should know that all the storage in inst
386 // is transient, and will disappear immediately afterward.
387 if (parsed_instruction_fn_) {
388 if (auto error = parsed_instruction_fn_(user_data_, &inst)) return error;
389 }
390
391 return SPV_SUCCESS;
392}
393
David Neto7bff3eb2015-11-20 14:21:10 -0500394spv_result_t Parser::parseOperand(size_t inst_offset,
395 spv_parsed_instruction_t* inst,
David Neto0ca6b592015-10-30 16:06:15 -0400396 const spv_operand_type_t type,
David Neto7bff3eb2015-11-20 14:21:10 -0500397 std::vector<uint32_t>* words,
David Neto0ca6b592015-10-30 16:06:15 -0400398 std::vector<spv_parsed_operand_t>* operands,
399 spv_operand_pattern_t* expected_operands) {
400 // We'll fill in this result as we go along.
401 spv_parsed_operand_t parsed_operand;
David Neto7bff3eb2015-11-20 14:21:10 -0500402 parsed_operand.offset = uint16_t(_.word_index - inst_offset);
David Neto0ca6b592015-10-30 16:06:15 -0400403 // Most operands occupy one word. This might be be adjusted later.
404 parsed_operand.num_words = 1;
405 // The type argument is the one used by the grammar to parse the instruction.
406 // But it can exposes internal parser details such as whether an operand is
407 // optional or actually represents a variable-length sequence of operands.
408 // The resulting type should be adjusted to avoid those internal details.
409 // In most cases, the resulting operand type is the same as the grammar type.
410 parsed_operand.type = type;
411
412 // Assume non-numeric values. This will be updated for literal numbers.
413 parsed_operand.number_kind = SPV_NUMBER_NONE;
414 parsed_operand.number_bit_width = 0;
415
David Netod9ad0502015-11-24 18:37:24 -0500416 if (_.word_index >= _.num_words)
417 return exhaustedInputDiagnostic(inst_offset, inst->opcode, type);
418
David Neto0ca6b592015-10-30 16:06:15 -0400419 const uint32_t word = peek();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100420
David Neto7bff3eb2015-11-20 14:21:10 -0500421 // Do the words in this operand have to be converted to native endianness?
422 // True for all but literal strings.
423 bool convert_operand_endianness = true;
424
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100425 switch (type) {
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400426 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto677e0c72016-01-05 14:56:02 -0500427 if (!word)
428 return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Type Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400429 inst->type_id = word;
430 break;
431
432 case SPV_OPERAND_TYPE_RESULT_ID:
David Neto677e0c72016-01-05 14:56:02 -0500433 if (!word)
434 return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Result Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400435 inst->result_id = word;
436 // Save the result ID to type ID mapping.
437 // In the grammar, type ID always appears before result ID.
438 if (_.id_to_type_id.find(inst->result_id) != _.id_to_type_id.end())
Umar Arshadf76e0f52015-11-18 15:43:43 -0500439 return diagnostic(SPV_ERROR_INVALID_ID) << "Id " << inst->result_id
David Neto677e0c72016-01-05 14:56:02 -0500440 << " is defined more than once";
David Neto0ca6b592015-10-30 16:06:15 -0400441 // Record it.
442 // A regular value maps to its type. Some instructions (e.g. OpLabel)
443 // have no type Id, and will map to 0. The result Id for a
444 // type-generating instruction (e.g. OpTypeInt) maps to itself.
445 _.id_to_type_id[inst->result_id] = spvOpcodeGeneratesType(inst->opcode)
446 ? inst->result_id
447 : inst->type_id;
448 break;
449
450 case SPV_OPERAND_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400451 case SPV_OPERAND_TYPE_OPTIONAL_ID:
Umar Arshadf76e0f52015-11-18 15:43:43 -0500452 if (!word) return diagnostic(SPV_ERROR_INVALID_ID) << "Id is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400453 parsed_operand.type = SPV_OPERAND_TYPE_ID;
454
455 if (inst->opcode == SpvOpExtInst && parsed_operand.offset == 3) {
456 // The current word is the extended instruction set Id.
457 // Set the extended instruction set type for the current instruction.
458 auto ext_inst_type_iter = _.import_id_to_ext_inst_type.find(word);
459 if (ext_inst_type_iter == _.import_id_to_ext_inst_type.end()) {
Umar Arshadf76e0f52015-11-18 15:43:43 -0500460 return diagnostic(SPV_ERROR_INVALID_ID)
David Neto0ca6b592015-10-30 16:06:15 -0400461 << "OpExtInst set Id " << word
462 << " does not reference an OpExtInstImport result Id";
463 }
464 inst->ext_inst_type = ext_inst_type_iter->second;
465 }
466 break;
467
David Neto64a9be92015-11-18 15:48:32 -0500468 case SPV_OPERAND_TYPE_SCOPE_ID:
469 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
David Netod9ad0502015-11-24 18:37:24 -0500470 // Check for trivially invalid values. The operand descriptions already
471 // have the word "ID" in them.
472 if (!word) return diagnostic() << spvOperandTypeStr(type) << " is 0";
David Neto0ca6b592015-10-30 16:06:15 -0400473 break;
474
David Neto445ce442015-10-15 15:22:06 -0400475 case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
David Neto0ca6b592015-10-30 16:06:15 -0400476 assert(SpvOpExtInst == inst->opcode);
477 assert(inst->ext_inst_type != SPV_EXT_INST_TYPE_NONE);
478 spv_ext_inst_desc ext_inst;
479 if (grammar_.lookupExtInst(inst->ext_inst_type, word, &ext_inst))
480 return diagnostic() << "Invalid extended instruction number: " << word;
481 spvPrependOperandTypes(ext_inst->operandTypes, expected_operands);
David Neto445ce442015-10-15 15:22:06 -0400482 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400483
David Neto21196942015-11-11 02:45:45 -0500484 case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
485 assert(SpvOpSpecConstantOp == inst->opcode);
486 if (grammar_.lookupSpecConstantOpcode(SpvOp(word))) {
487 return diagnostic() << "Invalid " << spvOperandTypeStr(type) << ": "
488 << word;
489 }
490 spv_opcode_desc opcode_entry = nullptr;
491 if (grammar_.lookupOpcode(SpvOp(word), &opcode_entry)) {
492 return diagnostic(SPV_ERROR_INTERNAL)
493 << "OpSpecConstant opcode table out of sync";
494 }
495 // OpSpecConstant opcodes must have a type and result. We've already
496 // processed them, so skip them when preparing to parse the other
497 // operants for the opcode.
498 assert(opcode_entry->hasType);
499 assert(opcode_entry->hasResult);
500 assert(opcode_entry->numTypes >= 2);
501 spvPrependOperandTypes(opcode_entry->operandTypes + 2, expected_operands);
502 } break;
503
David Neto445ce442015-10-15 15:22:06 -0400504 case SPV_OPERAND_TYPE_LITERAL_INTEGER:
Lei Zhang6483bd72015-10-14 17:02:39 -0400505 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER:
David Neto201caf72015-11-04 17:38:17 -0500506 // These are regular single-word literal integer operands.
507 // Post-parsing validation should check the range of the parsed value.
David Neto0ca6b592015-10-30 16:06:15 -0400508 parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_INTEGER;
David Neto201caf72015-11-04 17:38:17 -0500509 // It turns out they are always unsigned integers!
510 parsed_operand.number_kind = SPV_NUMBER_UNSIGNED_INT;
511 parsed_operand.number_bit_width = 32;
512 break;
513
514 case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER:
515 case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
Lei Zhangaa3cd5a2015-11-10 14:29:35 -0500516 parsed_operand.type = SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER;
David Neto0ca6b592015-10-30 16:06:15 -0400517 if (inst->opcode == SpvOpSwitch) {
518 // The literal operands have the same type as the value
519 // referenced by the selector Id.
David Neto7bff3eb2015-11-20 14:21:10 -0500520 const uint32_t selector_id = peekAt(inst_offset + 1);
David Neto3664bd52015-12-23 13:21:43 -0500521 const auto type_id_iter = _.id_to_type_id.find(selector_id);
522 if (type_id_iter == _.id_to_type_id.end() ||
523 type_id_iter->second == 0) {
David Neto0ca6b592015-10-30 16:06:15 -0400524 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
525 << " has no type";
526 }
527 uint32_t type_id = type_id_iter->second;
528
529 if (selector_id == type_id) {
530 // Recall that by convention, a result ID that is a type definition
531 // maps to itself.
532 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
533 << " is a type, not a value";
534 }
535 if (auto error = setNumericTypeInfoForType(&parsed_operand, type_id))
536 return error;
537 if (parsed_operand.number_kind != SPV_NUMBER_UNSIGNED_INT &&
538 parsed_operand.number_kind != SPV_NUMBER_SIGNED_INT) {
539 return diagnostic() << "Invalid OpSwitch: selector id " << selector_id
540 << " is not a scalar integer";
541 }
Lei Zhangb41d1502015-09-14 15:22:23 -0400542 } else {
David Neto201caf72015-11-04 17:38:17 -0500543 assert(inst->opcode == SpvOpConstant ||
544 inst->opcode == SpvOpSpecConstant);
545 // The literal number type is determined by the type Id for the
546 // constant.
547 assert(inst->type_id);
548 if (auto error =
549 setNumericTypeInfoForType(&parsed_operand, inst->type_id))
550 return error;
Lei Zhangb41d1502015-09-14 15:22:23 -0400551 }
David Neto0ca6b592015-10-30 16:06:15 -0400552 break;
553
David Neto78c3b432015-08-27 13:03:52 -0400554 case SPV_OPERAND_TYPE_LITERAL_STRING:
555 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
David Neto7bff3eb2015-11-20 14:21:10 -0500556 convert_operand_endianness = false;
David Neto0ca6b592015-10-30 16:06:15 -0400557 const char* string =
558 reinterpret_cast<const char*>(_.words + _.word_index);
David Netod9ad0502015-11-24 18:37:24 -0500559 // Compute the length of the string, but make sure we don't run off the
560 // end of the input.
561 const size_t remaining_input_bytes =
562 sizeof(uint32_t) * (_.num_words - _.word_index);
563 const size_t string_num_content_bytes =
564 strnlen(string, remaining_input_bytes);
565 // If there was no terminating null byte, then that's an end-of-input
566 // error.
567 if (string_num_content_bytes == remaining_input_bytes)
568 return exhaustedInputDiagnostic(inst_offset, inst->opcode, type);
569 // Account for null in the word length, so add 1 for null, then add 3 to
570 // make sure we round up. The following is equivalent to:
571 // (string_num_content_bytes + 1 + 3) / 4
572 const size_t string_num_words = string_num_content_bytes / 4 + 1;
David Neto0ca6b592015-10-30 16:06:15 -0400573 // Make sure we can record the word count without overflow.
David Netod9ad0502015-11-24 18:37:24 -0500574 //
575 // This error can't currently be triggered because of validity
576 // checks elsewhere.
David Neto0ca6b592015-10-30 16:06:15 -0400577 if (string_num_words > std::numeric_limits<uint16_t>::max()) {
578 return diagnostic() << "Literal string is longer than "
579 << std::numeric_limits<uint16_t>::max()
580 << " words: " << string_num_words << " words long";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100581 }
Andrew Woloszyn3a4bc7e2015-11-19 09:22:53 -0500582 parsed_operand.num_words = uint16_t(string_num_words);
David Neto0ca6b592015-10-30 16:06:15 -0400583 parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_STRING;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100584
David Neto0ca6b592015-10-30 16:06:15 -0400585 if (SpvOpExtInstImport == inst->opcode) {
586 // Record the extended instruction type for the ID for this import.
587 // There is only one string literal argument to OpExtInstImport,
588 // so it's sufficient to guard this just on the opcode.
589 const spv_ext_inst_type_t ext_inst_type =
590 spvExtInstImportTypeGet(string);
591 if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
592 return diagnostic() << "Invalid extended instruction import '"
593 << string << "'";
Andrew Woloszyne59e6b72015-10-14 14:18:43 -0400594 }
David Neto0ca6b592015-10-30 16:06:15 -0400595 // We must have parsed a valid result ID. It's a condition
596 // of the grammar, and we only accept non-zero result Ids.
597 assert(inst->result_id);
598 _.import_id_to_ext_inst_type[inst->result_id] = ext_inst_type;
Andrew Woloszyne59e6b72015-10-14 14:18:43 -0400599 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100600 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400601
602 case SPV_OPERAND_TYPE_OPTIONAL_EXECUTION_MODE:
603 parsed_operand.type = SPV_OPERAND_TYPE_EXECUTION_MODE;
604 // Fall through
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100605 case SPV_OPERAND_TYPE_CAPABILITY:
606 case SPV_OPERAND_TYPE_SOURCE_LANGUAGE:
607 case SPV_OPERAND_TYPE_EXECUTION_MODEL:
608 case SPV_OPERAND_TYPE_ADDRESSING_MODEL:
609 case SPV_OPERAND_TYPE_MEMORY_MODEL:
610 case SPV_OPERAND_TYPE_EXECUTION_MODE:
611 case SPV_OPERAND_TYPE_STORAGE_CLASS:
612 case SPV_OPERAND_TYPE_DIMENSIONALITY:
613 case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE:
614 case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE:
David Netod9ad0502015-11-24 18:37:24 -0500615 case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100616 case SPV_OPERAND_TYPE_FP_ROUNDING_MODE:
617 case SPV_OPERAND_TYPE_LINKAGE_TYPE:
618 case SPV_OPERAND_TYPE_ACCESS_QUALIFIER:
David Neto2889a0c2016-02-15 13:50:00 -0500619 case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100620 case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE:
621 case SPV_OPERAND_TYPE_DECORATION:
622 case SPV_OPERAND_TYPE_BUILT_IN:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100623 case SPV_OPERAND_TYPE_GROUP_OPERATION:
624 case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS:
David Neto47994822015-08-27 13:11:01 -0400625 case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: {
David Neto0ca6b592015-10-30 16:06:15 -0400626 // A single word that is a plain enum value.
David Neto2889a0c2016-02-15 13:50:00 -0500627
628 // Map an optional operand type to its corresponding concrete type.
629 if (type == SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER)
630 parsed_operand.type = SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
631
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100632 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400633 if (grammar_.lookupOperand(type, word, &entry)) {
David Neto201caf72015-11-04 17:38:17 -0500634 return diagnostic() << "Invalid "
635 << spvOperandTypeStr(parsed_operand.type)
David Neto0ca6b592015-10-30 16:06:15 -0400636 << " operand: " << word;
Lei Zhang40056702015-09-11 14:31:27 -0400637 }
David Neto78c3b432015-08-27 13:03:52 -0400638 // Prepare to accept operands to this operand, if needed.
David Neto0ca6b592015-10-30 16:06:15 -0400639 spvPrependOperandTypes(entry->operandTypes, expected_operands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100640 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400641
David Neto619db262015-09-25 12:43:37 -0400642 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
643 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
644 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Neto2889a0c2016-02-15 13:50:00 -0500645 case SPV_OPERAND_TYPE_IMAGE:
David Neto619db262015-09-25 12:43:37 -0400646 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
647 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
648 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
649 // This operand is a mask.
David Neto201caf72015-11-04 17:38:17 -0500650
651 // Map an optional operand type to its corresponding concrete type.
652 if (type == SPV_OPERAND_TYPE_OPTIONAL_IMAGE)
653 parsed_operand.type = SPV_OPERAND_TYPE_IMAGE;
654 else if (type == SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS)
655 parsed_operand.type = SPV_OPERAND_TYPE_MEMORY_ACCESS;
656
David Neto0ca6b592015-10-30 16:06:15 -0400657 // Check validity of set mask bits. Also prepare for operands for those
658 // masks if they have any. To get operand order correct, scan from
659 // MSB to LSB since we can only prepend operands to a pattern.
660 // The only case in the grammar where you have more than one mask bit
661 // having an operand is for image operands. See SPIR-V 3.14 Image
662 // Operands.
663 uint32_t remaining_word = word;
664 for (uint32_t mask = (1u << 31); remaining_word; mask >>= 1) {
David Neto619db262015-09-25 12:43:37 -0400665 if (remaining_word & mask) {
David Neto619db262015-09-25 12:43:37 -0400666 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400667 if (grammar_.lookupOperand(type, mask, &entry)) {
David Neto201caf72015-11-04 17:38:17 -0500668 return diagnostic()
669 << "Invalid " << spvOperandTypeStr(parsed_operand.type)
670 << " operand: " << word << " has invalid mask component "
671 << mask;
David Neto619db262015-09-25 12:43:37 -0400672 }
David Neto0ca6b592015-10-30 16:06:15 -0400673 remaining_word ^= mask;
674 spvPrependOperandTypes(entry->operandTypes, expected_operands);
David Neto619db262015-09-25 12:43:37 -0400675 }
676 }
David Neto0ca6b592015-10-30 16:06:15 -0400677 if (word == 0) {
678 // An all-zeroes mask *might* also be valid.
David Neto619db262015-09-25 12:43:37 -0400679 spv_operand_desc entry;
David Neto0ca6b592015-10-30 16:06:15 -0400680 if (SPV_SUCCESS == grammar_.lookupOperand(type, 0, &entry)) {
David Neto619db262015-09-25 12:43:37 -0400681 // Prepare for its operands, if any.
David Neto0ca6b592015-10-30 16:06:15 -0400682 spvPrependOperandTypes(entry->operandTypes, expected_operands);
David Neto619db262015-09-25 12:43:37 -0400683 }
684 }
David Neto619db262015-09-25 12:43:37 -0400685 } break;
David Neto0ca6b592015-10-30 16:06:15 -0400686 default:
687 return diagnostic() << "Internal error: Unhandled operand type: " << type;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100688 }
689
David Neto0ca6b592015-10-30 16:06:15 -0400690 assert(int(SPV_OPERAND_TYPE_FIRST_CONCRETE_TYPE) <= int(parsed_operand.type));
691 assert(int(SPV_OPERAND_TYPE_LAST_CONCRETE_TYPE) >= int(parsed_operand.type));
692
693 operands->push_back(parsed_operand);
694
David Neto7bff3eb2015-11-20 14:21:10 -0500695 const size_t index_after_operand = _.word_index + parsed_operand.num_words;
David Netod9ad0502015-11-24 18:37:24 -0500696
697 // Avoid buffer overrun for the cases where the operand has more than one
698 // word, and where it isn't a string. (Those other cases have already been
699 // handled earlier.) For example, this error can occur for a multi-word
700 // argument to OpConstant, or a multi-word case literal operand for OpSwitch.
701 if (_.num_words < index_after_operand)
702 return exhaustedInputDiagnostic(inst_offset, inst->opcode, type);
703
David Neto7bff3eb2015-11-20 14:21:10 -0500704 if (_.requires_endian_conversion) {
705 // Copy instruction words. Translate to native endianness as needed.
706 if (convert_operand_endianness) {
707 const spv_endianness_t endianness = _.endian;
708 std::transform(_.words + _.word_index, _.words + index_after_operand,
Andrew Woloszyn3b69d052016-01-11 13:54:30 -0500709 std::back_inserter(*words),
710 [endianness](const uint32_t raw_word) {
David Neto677e0c72016-01-05 14:56:02 -0500711 return spvFixWord(raw_word, endianness);
David Neto7bff3eb2015-11-20 14:21:10 -0500712 });
713 } else {
714 words->insert(words->end(), _.words + _.word_index,
715 _.words + index_after_operand);
716 }
717 }
718
719 // Advance past the operand.
720 _.word_index = index_after_operand;
David Neto0ca6b592015-10-30 16:06:15 -0400721
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100722 return SPV_SUCCESS;
723}
724
David Neto0ca6b592015-10-30 16:06:15 -0400725spv_result_t Parser::setNumericTypeInfoForType(
726 spv_parsed_operand_t* parsed_operand, uint32_t type_id) {
David Neto201caf72015-11-04 17:38:17 -0500727 assert(type_id != 0);
David Neto0ca6b592015-10-30 16:06:15 -0400728 auto type_info_iter = _.type_id_to_number_type_info.find(type_id);
729 if (type_info_iter == _.type_id_to_number_type_info.end()) {
730 return diagnostic() << "Type Id " << type_id << " is not a type";
731 }
732 const NumberType& info = type_info_iter->second;
733 if (info.type == SPV_NUMBER_NONE) {
734 // This is a valid type, but for something other than a scalar number.
735 return diagnostic() << "Type Id " << type_id
736 << " is not a scalar numeric type";
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400737 }
738
David Neto0ca6b592015-10-30 16:06:15 -0400739 parsed_operand->number_kind = info.type;
740 parsed_operand->number_bit_width = info.bit_width;
David Neto066bd522016-01-05 14:57:58 -0500741 // Round up the word count.
742 parsed_operand->num_words = static_cast<uint16_t>((info.bit_width + 31) / 32);
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400743 return SPV_SUCCESS;
744}
745
David Neto7bff3eb2015-11-20 14:21:10 -0500746void Parser::recordNumberType(size_t inst_offset,
747 const spv_parsed_instruction_t* inst) {
David Neto0ca6b592015-10-30 16:06:15 -0400748 if (spvOpcodeGeneratesType(inst->opcode)) {
749 NumberType info = {SPV_NUMBER_NONE, 0};
750 if (SpvOpTypeInt == inst->opcode) {
David Neto7bff3eb2015-11-20 14:21:10 -0500751 const bool is_signed = peekAt(inst_offset + 3) != 0;
David Neto0ca6b592015-10-30 16:06:15 -0400752 info.type = is_signed ? SPV_NUMBER_SIGNED_INT : SPV_NUMBER_UNSIGNED_INT;
David Neto7bff3eb2015-11-20 14:21:10 -0500753 info.bit_width = peekAt(inst_offset + 2);
David Neto0ca6b592015-10-30 16:06:15 -0400754 } else if (SpvOpTypeFloat == inst->opcode) {
755 info.type = SPV_NUMBER_FLOATING;
David Neto7bff3eb2015-11-20 14:21:10 -0500756 info.bit_width = peekAt(inst_offset + 2);
Lei Zhang40056702015-09-11 14:31:27 -0400757 }
David Neto0ca6b592015-10-30 16:06:15 -0400758 // The *result* Id of a type generating instruction is the type Id.
759 _.type_id_to_number_type_info[inst->result_id] = info;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100760 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100761}
762
David Neto0ca6b592015-10-30 16:06:15 -0400763} // anonymous namespace
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400764
Lei Zhang972788b2015-11-12 13:48:30 -0500765spv_result_t spvBinaryParse(const spv_const_context context, void* user_data,
766 const uint32_t* code, const size_t num_words,
David Neto0ca6b592015-10-30 16:06:15 -0400767 spv_parsed_header_fn_t parsed_header,
768 spv_parsed_instruction_fn_t parsed_instruction,
769 spv_diagnostic* diagnostic) {
Lei Zhang972788b2015-11-12 13:48:30 -0500770 Parser parser(context, user_data, parsed_header, parsed_instruction);
David Neto0ca6b592015-10-30 16:06:15 -0400771 return parser.parse(code, num_words, diagnostic);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100772}
773
David Neto0ca6b592015-10-30 16:06:15 -0400774// TODO(dneto): This probably belongs in text.cpp since that's the only place
775// that a spv_binary_t value is created.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100776void spvBinaryDestroy(spv_binary binary) {
Lei Zhang40056702015-09-11 14:31:27 -0400777 if (!binary) return;
Eric Engestromeb6ae972016-02-18 23:41:16 +0000778 delete[] binary->code;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100779 delete binary;
780}