Dejan Mircevski | b6fe02f | 2016-01-07 13:44:22 -0500 | [diff] [blame] | 1 | // Copyright (c) 2015-2016 The Khronos Group Inc. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 2 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 3 | // 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) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 6 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 8 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 9 | // 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) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 14 | |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 15 | #include "source/binary.h" |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 16 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 17 | #include <algorithm> |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 18 | #include <cassert> |
| 19 | #include <cstring> |
Andrew Woloszyn | 7ffd8ff | 2016-01-11 16:22:34 -0500 | [diff] [blame] | 20 | #include <iterator> |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 21 | #include <limits> |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 22 | #include <string> |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 23 | #include <unordered_map> |
Lei Zhang | dc6e483 | 2016-09-21 17:16:31 -0400 | [diff] [blame] | 24 | #include <vector> |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 25 | |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 26 | #include "source/assembly_grammar.h" |
| 27 | #include "source/diagnostic.h" |
| 28 | #include "source/ext_inst.h" |
| 29 | #include "source/latest_version_spirv_header.h" |
| 30 | #include "source/opcode.h" |
| 31 | #include "source/operand.h" |
| 32 | #include "source/spirv_constant.h" |
| 33 | #include "source/spirv_endian.h" |
Andrew Woloszyn | 157e41b | 2015-10-16 15:11:00 -0400 | [diff] [blame] | 34 | |
Andrew Woloszyn | 55ecc2e | 2015-11-11 11:05:07 -0500 | [diff] [blame] | 35 | spv_result_t spvBinaryHeaderGet(const spv_const_binary binary, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 36 | const spv_endianness_t endian, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 37 | spv_header_t* pHeader) { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 38 | if (!binary->code) return SPV_ERROR_INVALID_BINARY; |
| 39 | if (binary->wordCount < SPV_INDEX_INSTRUCTION) |
| 40 | return SPV_ERROR_INVALID_BINARY; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 41 | if (!pHeader) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 42 | |
| 43 | // TODO: Validation checking? |
| 44 | pHeader->magic = spvFixWord(binary->code[SPV_INDEX_MAGIC_NUMBER], endian); |
| 45 | pHeader->version = spvFixWord(binary->code[SPV_INDEX_VERSION_NUMBER], endian); |
| 46 | pHeader->generator = |
| 47 | spvFixWord(binary->code[SPV_INDEX_GENERATOR_NUMBER], endian); |
| 48 | pHeader->bound = spvFixWord(binary->code[SPV_INDEX_BOUND], endian); |
| 49 | pHeader->schema = spvFixWord(binary->code[SPV_INDEX_SCHEMA], endian); |
| 50 | pHeader->instructions = &binary->code[SPV_INDEX_INSTRUCTION]; |
| 51 | |
| 52 | return SPV_SUCCESS; |
| 53 | } |
| 54 | |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 55 | namespace { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 56 | |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 57 | // A SPIR-V binary parser. A parser instance communicates detailed parse |
| 58 | // results via callbacks. |
| 59 | class Parser { |
| 60 | public: |
| 61 | // The user_data value is provided to the callbacks as context. |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 62 | Parser(const spv_const_context context, void* user_data, |
| 63 | spv_parsed_header_fn_t parsed_header_fn, |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 64 | spv_parsed_instruction_fn_t parsed_instruction_fn) |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 65 | : grammar_(context), |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 66 | consumer_(context->consumer), |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 67 | user_data_(user_data), |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 68 | parsed_header_fn_(parsed_header_fn), |
| 69 | parsed_instruction_fn_(parsed_instruction_fn) {} |
| 70 | |
| 71 | // Parses the specified binary SPIR-V module, issuing callbacks on a parsed |
| 72 | // header and for each parsed instruction. Returns SPV_SUCCESS on success. |
| 73 | // Otherwise returns an error code and issues a diagnostic. |
| 74 | spv_result_t parse(const uint32_t* words, size_t num_words, |
| 75 | spv_diagnostic* diagnostic); |
| 76 | |
| 77 | private: |
| 78 | // All remaining methods work on the current module parse state. |
| 79 | |
| 80 | // Like the parse method, but works on the current module parse state. |
| 81 | spv_result_t parseModule(); |
| 82 | |
| 83 | // Parses an instruction at the current position of the binary. Assumes |
| 84 | // the header has been parsed, the endian has been set, and the word index is |
| 85 | // still in range. Advances the parsing position past the instruction, and |
| 86 | // updates other parsing state for the current module. |
| 87 | // On success, returns SPV_SUCCESS and issues the parsed-instruction callback. |
| 88 | // On failure, returns an error code and issues a diagnostic. |
| 89 | spv_result_t parseInstruction(); |
| 90 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 91 | // Parses an instruction operand with the given type, for an instruction |
| 92 | // starting at inst_offset words into the SPIR-V binary. |
| 93 | // If the SPIR-V binary is the same endianness as the host, then the |
| 94 | // endian_converted_inst_words parameter is ignored. Otherwise, this method |
| 95 | // appends the words for this operand, converted to host native endianness, |
| 96 | // to the end of endian_converted_inst_words. This method also updates the |
| 97 | // expected_operands parameter, and the scalar members of the inst parameter. |
| 98 | // On success, returns SPV_SUCCESS, advances past the operand, and pushes a |
| 99 | // new entry on to the operands vector. Otherwise returns an error code and |
| 100 | // issues a diagnostic. |
| 101 | spv_result_t parseOperand(size_t inst_offset, spv_parsed_instruction_t* inst, |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 102 | const spv_operand_type_t type, |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 103 | std::vector<uint32_t>* endian_converted_inst_words, |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 104 | std::vector<spv_parsed_operand_t>* operands, |
| 105 | spv_operand_pattern_t* expected_operands); |
| 106 | |
| 107 | // Records the numeric type for an operand according to the type information |
| 108 | // associated with the given non-zero type Id. This can fail if the type Id |
| 109 | // is not a type Id, or if the type Id does not reference a scalar numeric |
| 110 | // type. On success, return SPV_SUCCESS and populates the num_words, |
| 111 | // number_kind, and number_bit_width fields of parsed_operand. |
| 112 | spv_result_t setNumericTypeInfoForType(spv_parsed_operand_t* parsed_operand, |
| 113 | uint32_t type_id); |
| 114 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 115 | // Records the number type for an instruction at the given offset, if that |
| 116 | // instruction generates a type. For types that aren't scalar numbers, |
| 117 | // record something with number kind SPV_NUMBER_NONE. |
| 118 | void recordNumberType(size_t inst_offset, |
| 119 | const spv_parsed_instruction_t* inst); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 120 | |
| 121 | // Returns a diagnostic stream object initialized with current position in |
| 122 | // the input stream, and for the given error code. Any data written to the |
| 123 | // returned object will be propagated to the current parse's diagnostic |
| 124 | // object. |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 125 | spvtools::DiagnosticStream diagnostic(spv_result_t error) { |
dan sinclair | 167f127 | 2018-12-21 16:24:15 -0500 | [diff] [blame^] | 126 | return spvtools::DiagnosticStream({0, 0, _.instruction_count}, consumer_, |
| 127 | "", error); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Returns a diagnostic stream object with the default parse error code. |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 131 | spvtools::DiagnosticStream diagnostic() { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 132 | // The default failure for parsing is invalid binary. |
| 133 | return diagnostic(SPV_ERROR_INVALID_BINARY); |
| 134 | } |
| 135 | |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 136 | // Issues a diagnostic describing an exhaustion of input condition when |
| 137 | // trying to decode an instruction operand, and returns |
| 138 | // SPV_ERROR_INVALID_BINARY. |
| 139 | spv_result_t exhaustedInputDiagnostic(size_t inst_offset, SpvOp opcode, |
| 140 | spv_operand_type_t type) { |
| 141 | return diagnostic() << "End of input reached while decoding Op" |
| 142 | << spvOpcodeString(opcode) << " starting at word " |
| 143 | << inst_offset |
| 144 | << ((_.word_index < _.num_words) ? ": truncated " |
| 145 | : ": missing ") |
| 146 | << spvOperandTypeStr(type) << " operand at word offset " |
| 147 | << _.word_index - inst_offset << "."; |
| 148 | } |
| 149 | |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 150 | // Returns the endian-corrected word at the current position. |
| 151 | uint32_t peek() const { return peekAt(_.word_index); } |
| 152 | |
| 153 | // Returns the endian-corrected word at the given position. |
| 154 | uint32_t peekAt(size_t index) const { |
| 155 | assert(index < _.num_words); |
| 156 | return spvFixWord(_.words[index], _.endian); |
| 157 | } |
| 158 | |
| 159 | // Data members |
| 160 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 161 | const spvtools::AssemblyGrammar grammar_; // SPIR-V syntax utility. |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 162 | const spvtools::MessageConsumer& consumer_; // Message consumer callback. |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 163 | void* const user_data_; // Context for the callbacks |
| 164 | const spv_parsed_header_fn_t parsed_header_fn_; // Parsed header callback |
| 165 | const spv_parsed_instruction_fn_t |
| 166 | parsed_instruction_fn_; // Parsed instruction callback |
| 167 | |
| 168 | // Describes the format of a typed literal number. |
| 169 | struct NumberType { |
| 170 | spv_number_kind_t type; |
| 171 | uint32_t bit_width; |
| 172 | }; |
| 173 | |
| 174 | // The state used to parse a single SPIR-V binary module. |
| 175 | struct State { |
| 176 | State(const uint32_t* words_arg, size_t num_words_arg, |
| 177 | spv_diagnostic* diagnostic_arg) |
| 178 | : words(words_arg), |
| 179 | num_words(num_words_arg), |
| 180 | diagnostic(diagnostic_arg), |
| 181 | word_index(0), |
dan sinclair | 167f127 | 2018-12-21 16:24:15 -0500 | [diff] [blame^] | 182 | instruction_count(0), |
Lei Zhang | 712bed0 | 2016-02-25 16:11:16 -0500 | [diff] [blame] | 183 | endian(), |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 184 | requires_endian_conversion(false) { |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 185 | // Temporary storage for parser state within a single instruction. |
| 186 | // Most instructions require fewer than 25 words or operands. |
| 187 | operands.reserve(25); |
| 188 | endian_converted_words.reserve(25); |
| 189 | expected_operands.reserve(25); |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 190 | } |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 191 | State() : State(0, 0, nullptr) {} |
| 192 | const uint32_t* words; // Words in the binary SPIR-V module. |
| 193 | size_t num_words; // Number of words in the module. |
| 194 | spv_diagnostic* diagnostic; // Where diagnostics go. |
| 195 | size_t word_index; // The current position in words. |
dan sinclair | 167f127 | 2018-12-21 16:24:15 -0500 | [diff] [blame^] | 196 | size_t instruction_count; // The count of processed instructions |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 197 | spv_endianness_t endian; // The endianness of the binary. |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 198 | // Is the SPIR-V binary in a different endiannes from the host native |
| 199 | // endianness? |
| 200 | bool requires_endian_conversion; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 201 | |
| 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; |
Chris Forbes | fcd991f | 2017-06-27 11:00:06 -0700 | [diff] [blame] | 211 | |
| 212 | // Used by parseOperand |
| 213 | std::vector<spv_parsed_operand_t> operands; |
| 214 | std::vector<uint32_t> endian_converted_words; |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 215 | spv_operand_pattern_t expected_operands; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 216 | } _; |
| 217 | }; |
| 218 | |
| 219 | spv_result_t Parser::parse(const uint32_t* words, size_t num_words, |
| 220 | spv_diagnostic* diagnostic_arg) { |
| 221 | _ = State(words, num_words, diagnostic_arg); |
| 222 | |
| 223 | const spv_result_t result = parseModule(); |
| 224 | |
| 225 | // Clear the module state. The tables might be big. |
| 226 | _ = State(); |
| 227 | |
| 228 | return result; |
| 229 | } |
| 230 | |
| 231 | spv_result_t Parser::parseModule() { |
| 232 | if (!_.words) return diagnostic() << "Missing module."; |
| 233 | |
| 234 | if (_.num_words < SPV_INDEX_INSTRUCTION) |
| 235 | return diagnostic() << "Module has incomplete header: only " << _.num_words |
| 236 | << " words instead of " << SPV_INDEX_INSTRUCTION; |
| 237 | |
| 238 | // Check the magic number and detect the module's endianness. |
Andrew Woloszyn | 55ecc2e | 2015-11-11 11:05:07 -0500 | [diff] [blame] | 239 | spv_const_binary_t binary{_.words, _.num_words}; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 240 | if (spvBinaryEndianness(&binary, &_.endian)) { |
| 241 | return diagnostic() << "Invalid SPIR-V magic number '" << std::hex |
| 242 | << _.words[0] << "'."; |
| 243 | } |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 244 | _.requires_endian_conversion = !spvIsHostEndian(_.endian); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 245 | |
| 246 | // Process the header. |
| 247 | spv_header_t header; |
| 248 | if (spvBinaryHeaderGet(&binary, _.endian, &header)) { |
| 249 | // It turns out there is no way to trigger this error since the only |
| 250 | // failure cases are already handled above, with better messages. |
| 251 | return diagnostic(SPV_ERROR_INTERNAL) |
| 252 | << "Internal error: unhandled header parse failure"; |
| 253 | } |
| 254 | if (parsed_header_fn_) { |
| 255 | if (auto error = parsed_header_fn_(user_data_, _.endian, header.magic, |
| 256 | header.version, header.generator, |
| 257 | header.bound, header.schema)) { |
| 258 | return error; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // Process the instructions. |
| 263 | _.word_index = SPV_INDEX_INSTRUCTION; |
| 264 | while (_.word_index < _.num_words) |
| 265 | if (auto error = parseInstruction()) return error; |
| 266 | |
| 267 | // Running off the end should already have been reported earlier. |
| 268 | assert(_.word_index == _.num_words); |
| 269 | |
| 270 | return SPV_SUCCESS; |
| 271 | } |
| 272 | |
| 273 | spv_result_t Parser::parseInstruction() { |
dan sinclair | 167f127 | 2018-12-21 16:24:15 -0500 | [diff] [blame^] | 274 | _.instruction_count++; |
| 275 | |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 276 | // The zero values for all members except for opcode are the |
| 277 | // correct initial values. |
| 278 | spv_parsed_instruction_t inst = {}; |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 279 | |
| 280 | const uint32_t first_word = peek(); |
| 281 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 282 | // If the module's endianness is different from the host native endianness, |
| 283 | // then converted_words contains the the endian-translated words in the |
| 284 | // instruction. |
Chris Forbes | fcd991f | 2017-06-27 11:00:06 -0700 | [diff] [blame] | 285 | _.endian_converted_words.clear(); |
| 286 | _.endian_converted_words.push_back(first_word); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 287 | |
| 288 | // After a successful parse of the instruction, the inst.operands member |
| 289 | // will point to this vector's storage. |
Chris Forbes | fcd991f | 2017-06-27 11:00:06 -0700 | [diff] [blame] | 290 | _.operands.clear(); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 291 | |
| 292 | assert(_.word_index < _.num_words); |
| 293 | // Decompose and check the first word. |
| 294 | uint16_t inst_word_count = 0; |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 295 | spvOpcodeSplit(first_word, &inst_word_count, &inst.opcode); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 296 | if (inst_word_count < 1) { |
| 297 | return diagnostic() << "Invalid instruction word count: " |
| 298 | << inst_word_count; |
| 299 | } |
| 300 | spv_opcode_desc opcode_desc; |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 301 | if (grammar_.lookupOpcode(static_cast<SpvOp>(inst.opcode), &opcode_desc)) |
| 302 | return diagnostic() << "Invalid opcode: " << inst.opcode; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 303 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 304 | // Advance past the opcode word. But remember the of the start |
| 305 | // of the instruction. |
| 306 | const size_t inst_offset = _.word_index; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 307 | _.word_index++; |
| 308 | |
| 309 | // Maintains the ordered list of expected operand types. |
| 310 | // For many instructions we only need the {numTypes, operandTypes} |
| 311 | // entries in opcode_desc. However, sometimes we need to modify |
| 312 | // the list as we parse the operands. This occurs when an operand |
| 313 | // has its own logical operands (such as the LocalSize operand for |
| 314 | // ExecutionMode), or for extended instructions that may have their |
| 315 | // own operands depending on the selected extended instruction. |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 316 | _.expected_operands.clear(); |
| 317 | for (auto i = 0; i < opcode_desc->numTypes; i++) |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 318 | _.expected_operands.push_back( |
| 319 | opcode_desc->operandTypes[opcode_desc->numTypes - i - 1]); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 320 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 321 | while (_.word_index < inst_offset + inst_word_count) { |
| 322 | const uint16_t inst_word_index = uint16_t(_.word_index - inst_offset); |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 323 | if (_.expected_operands.empty()) { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 324 | return diagnostic() << "Invalid instruction Op" << opcode_desc->name |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 325 | << " starting at word " << inst_offset |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 326 | << ": expected no more operands after " |
| 327 | << inst_word_index |
| 328 | << " words, but stated word count is " |
| 329 | << inst_word_count << "."; |
| 330 | } |
| 331 | |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 332 | spv_operand_type_t type = |
| 333 | spvTakeFirstMatchableOperand(&_.expected_operands); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 334 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 335 | if (auto error = |
Chris Forbes | fcd991f | 2017-06-27 11:00:06 -0700 | [diff] [blame] | 336 | parseOperand(inst_offset, &inst, type, &_.endian_converted_words, |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 337 | &_.operands, &_.expected_operands)) { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 338 | return error; |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 339 | } |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 340 | } |
| 341 | |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 342 | if (!_.expected_operands.empty() && |
| 343 | !spvOperandIsOptional(_.expected_operands.back())) { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 344 | return diagnostic() << "End of input reached while decoding Op" |
| 345 | << opcode_desc->name << " starting at word " |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 346 | << inst_offset << ": expected more operands after " |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 347 | << inst_word_count << " words."; |
| 348 | } |
| 349 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 350 | if ((inst_offset + inst_word_count) != _.word_index) { |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 351 | return diagnostic() << "Invalid word count: Op" << opcode_desc->name |
| 352 | << " starting at word " << inst_offset |
| 353 | << " says it has " << inst_word_count |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 354 | << " words, but found " << _.word_index - inst_offset |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 355 | << " words instead."; |
| 356 | } |
David Neto | 15afbf9 | 2015-11-23 14:17:35 -0500 | [diff] [blame] | 357 | |
| 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 || |
Chris Forbes | fcd991f | 2017-06-27 11:00:06 -0700 | [diff] [blame] | 364 | (inst_word_count == _.endian_converted_words.size())); |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 365 | assert(_.requires_endian_conversion || |
| 366 | (_.endian_converted_words.size() == 1)); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 367 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 368 | recordNumberType(inst_offset, &inst); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 369 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 370 | if (_.requires_endian_conversion) { |
| 371 | // We must wait until here to set this pointer, because the vector might |
| 372 | // have been be resized while we accumulated its elements. |
Chris Forbes | fcd991f | 2017-06-27 11:00:06 -0700 | [diff] [blame] | 373 | inst.words = _.endian_converted_words.data(); |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 374 | } else { |
| 375 | // If no conversion is required, then just point to the underlying binary. |
| 376 | // This saves time and space. |
| 377 | inst.words = _.words + inst_offset; |
| 378 | } |
| 379 | inst.num_words = inst_word_count; |
| 380 | |
| 381 | // We must wait until here to set this pointer, because the vector might |
| 382 | // have been be resized while we accumulated its elements. |
Chris Forbes | fcd991f | 2017-06-27 11:00:06 -0700 | [diff] [blame] | 383 | inst.operands = _.operands.data(); |
| 384 | inst.num_operands = uint16_t(_.operands.size()); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 385 | |
| 386 | // Issue the callback. The callee should know that all the storage in inst |
| 387 | // is transient, and will disappear immediately afterward. |
| 388 | if (parsed_instruction_fn_) { |
| 389 | if (auto error = parsed_instruction_fn_(user_data_, &inst)) return error; |
| 390 | } |
| 391 | |
| 392 | return SPV_SUCCESS; |
| 393 | } |
| 394 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 395 | spv_result_t Parser::parseOperand(size_t inst_offset, |
| 396 | spv_parsed_instruction_t* inst, |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 397 | const spv_operand_type_t type, |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 398 | std::vector<uint32_t>* words, |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 399 | std::vector<spv_parsed_operand_t>* operands, |
| 400 | spv_operand_pattern_t* expected_operands) { |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 401 | const SpvOp opcode = static_cast<SpvOp>(inst->opcode); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 402 | // We'll fill in this result as we go along. |
| 403 | spv_parsed_operand_t parsed_operand; |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 404 | parsed_operand.offset = uint16_t(_.word_index - inst_offset); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 405 | // Most operands occupy one word. This might be be adjusted later. |
| 406 | parsed_operand.num_words = 1; |
| 407 | // The type argument is the one used by the grammar to parse the instruction. |
| 408 | // But it can exposes internal parser details such as whether an operand is |
| 409 | // optional or actually represents a variable-length sequence of operands. |
| 410 | // The resulting type should be adjusted to avoid those internal details. |
| 411 | // In most cases, the resulting operand type is the same as the grammar type. |
| 412 | parsed_operand.type = type; |
| 413 | |
| 414 | // Assume non-numeric values. This will be updated for literal numbers. |
| 415 | parsed_operand.number_kind = SPV_NUMBER_NONE; |
| 416 | parsed_operand.number_bit_width = 0; |
| 417 | |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 418 | if (_.word_index >= _.num_words) |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 419 | return exhaustedInputDiagnostic(inst_offset, opcode, type); |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 420 | |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 421 | const uint32_t word = peek(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 422 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 423 | // Do the words in this operand have to be converted to native endianness? |
| 424 | // True for all but literal strings. |
| 425 | bool convert_operand_endianness = true; |
| 426 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 427 | switch (type) { |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 428 | case SPV_OPERAND_TYPE_TYPE_ID: |
David Neto | 677e0c7 | 2016-01-05 14:56:02 -0500 | [diff] [blame] | 429 | if (!word) |
| 430 | return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Type Id is 0"; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 431 | inst->type_id = word; |
| 432 | break; |
| 433 | |
| 434 | case SPV_OPERAND_TYPE_RESULT_ID: |
David Neto | 677e0c7 | 2016-01-05 14:56:02 -0500 | [diff] [blame] | 435 | if (!word) |
| 436 | return diagnostic(SPV_ERROR_INVALID_ID) << "Error: Result Id is 0"; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 437 | inst->result_id = word; |
| 438 | // Save the result ID to type ID mapping. |
| 439 | // In the grammar, type ID always appears before result ID. |
| 440 | if (_.id_to_type_id.find(inst->result_id) != _.id_to_type_id.end()) |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 441 | return diagnostic(SPV_ERROR_INVALID_ID) |
| 442 | << "Id " << inst->result_id << " is defined more than once"; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 443 | // Record it. |
| 444 | // A regular value maps to its type. Some instructions (e.g. OpLabel) |
| 445 | // have no type Id, and will map to 0. The result Id for a |
| 446 | // type-generating instruction (e.g. OpTypeInt) maps to itself. |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 447 | _.id_to_type_id[inst->result_id] = |
| 448 | spvOpcodeGeneratesType(opcode) ? inst->result_id : inst->type_id; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 449 | break; |
| 450 | |
| 451 | case SPV_OPERAND_TYPE_ID: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 452 | case SPV_OPERAND_TYPE_OPTIONAL_ID: |
Umar Arshad | f76e0f5 | 2015-11-18 15:43:43 -0500 | [diff] [blame] | 453 | if (!word) return diagnostic(SPV_ERROR_INVALID_ID) << "Id is 0"; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 454 | parsed_operand.type = SPV_OPERAND_TYPE_ID; |
| 455 | |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 456 | if (opcode == SpvOpExtInst && parsed_operand.offset == 3) { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 457 | // The current word is the extended instruction set Id. |
| 458 | // Set the extended instruction set type for the current instruction. |
| 459 | auto ext_inst_type_iter = _.import_id_to_ext_inst_type.find(word); |
| 460 | if (ext_inst_type_iter == _.import_id_to_ext_inst_type.end()) { |
Umar Arshad | f76e0f5 | 2015-11-18 15:43:43 -0500 | [diff] [blame] | 461 | return diagnostic(SPV_ERROR_INVALID_ID) |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 462 | << "OpExtInst set Id " << word |
| 463 | << " does not reference an OpExtInstImport result Id"; |
| 464 | } |
| 465 | inst->ext_inst_type = ext_inst_type_iter->second; |
| 466 | } |
| 467 | break; |
| 468 | |
David Neto | 64a9be9 | 2015-11-18 15:48:32 -0500 | [diff] [blame] | 469 | case SPV_OPERAND_TYPE_SCOPE_ID: |
| 470 | case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID: |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 471 | // Check for trivially invalid values. The operand descriptions already |
| 472 | // have the word "ID" in them. |
| 473 | if (!word) return diagnostic() << spvOperandTypeStr(type) << " is 0"; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 474 | break; |
| 475 | |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 476 | case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: { |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 477 | assert(SpvOpExtInst == opcode); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 478 | assert(inst->ext_inst_type != SPV_EXT_INST_TYPE_NONE); |
| 479 | spv_ext_inst_desc ext_inst; |
| 480 | if (grammar_.lookupExtInst(inst->ext_inst_type, word, &ext_inst)) |
| 481 | return diagnostic() << "Invalid extended instruction number: " << word; |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 482 | spvPushOperandTypes(ext_inst->operandTypes, expected_operands); |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 483 | } break; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 484 | |
David Neto | 2119694 | 2015-11-11 02:45:45 -0500 | [diff] [blame] | 485 | case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: { |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 486 | assert(SpvOpSpecConstantOp == opcode); |
David Neto | 2119694 | 2015-11-11 02:45:45 -0500 | [diff] [blame] | 487 | if (grammar_.lookupSpecConstantOpcode(SpvOp(word))) { |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 488 | return diagnostic() |
| 489 | << "Invalid " << spvOperandTypeStr(type) << ": " << word; |
David Neto | 2119694 | 2015-11-11 02:45:45 -0500 | [diff] [blame] | 490 | } |
| 491 | spv_opcode_desc opcode_entry = nullptr; |
| 492 | if (grammar_.lookupOpcode(SpvOp(word), &opcode_entry)) { |
| 493 | return diagnostic(SPV_ERROR_INTERNAL) |
| 494 | << "OpSpecConstant opcode table out of sync"; |
| 495 | } |
| 496 | // OpSpecConstant opcodes must have a type and result. We've already |
| 497 | // processed them, so skip them when preparing to parse the other |
| 498 | // operants for the opcode. |
| 499 | assert(opcode_entry->hasType); |
| 500 | assert(opcode_entry->hasResult); |
| 501 | assert(opcode_entry->numTypes >= 2); |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 502 | spvPushOperandTypes(opcode_entry->operandTypes + 2, expected_operands); |
David Neto | 2119694 | 2015-11-11 02:45:45 -0500 | [diff] [blame] | 503 | } break; |
| 504 | |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 505 | case SPV_OPERAND_TYPE_LITERAL_INTEGER: |
Lei Zhang | 6483bd7 | 2015-10-14 17:02:39 -0400 | [diff] [blame] | 506 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 507 | // These are regular single-word literal integer operands. |
| 508 | // Post-parsing validation should check the range of the parsed value. |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 509 | parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_INTEGER; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 510 | // It turns out they are always unsigned integers! |
| 511 | parsed_operand.number_kind = SPV_NUMBER_UNSIGNED_INT; |
| 512 | parsed_operand.number_bit_width = 32; |
| 513 | break; |
| 514 | |
| 515 | case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: |
| 516 | case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER: |
Lei Zhang | aa3cd5a | 2015-11-10 14:29:35 -0500 | [diff] [blame] | 517 | parsed_operand.type = SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER; |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 518 | if (opcode == SpvOpSwitch) { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 519 | // The literal operands have the same type as the value |
| 520 | // referenced by the selector Id. |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 521 | const uint32_t selector_id = peekAt(inst_offset + 1); |
David Neto | 3664bd5 | 2015-12-23 13:21:43 -0500 | [diff] [blame] | 522 | const auto type_id_iter = _.id_to_type_id.find(selector_id); |
| 523 | if (type_id_iter == _.id_to_type_id.end() || |
| 524 | type_id_iter->second == 0) { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 525 | return diagnostic() << "Invalid OpSwitch: selector id " << selector_id |
| 526 | << " has no type"; |
| 527 | } |
| 528 | uint32_t type_id = type_id_iter->second; |
| 529 | |
| 530 | if (selector_id == type_id) { |
| 531 | // Recall that by convention, a result ID that is a type definition |
| 532 | // maps to itself. |
| 533 | return diagnostic() << "Invalid OpSwitch: selector id " << selector_id |
| 534 | << " is a type, not a value"; |
| 535 | } |
| 536 | if (auto error = setNumericTypeInfoForType(&parsed_operand, type_id)) |
| 537 | return error; |
| 538 | if (parsed_operand.number_kind != SPV_NUMBER_UNSIGNED_INT && |
| 539 | parsed_operand.number_kind != SPV_NUMBER_SIGNED_INT) { |
| 540 | return diagnostic() << "Invalid OpSwitch: selector id " << selector_id |
| 541 | << " is not a scalar integer"; |
| 542 | } |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 543 | } else { |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 544 | assert(opcode == SpvOpConstant || opcode == SpvOpSpecConstant); |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 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 Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 551 | } |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 552 | break; |
| 553 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 554 | case SPV_OPERAND_TYPE_LITERAL_STRING: |
| 555 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: { |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 556 | convert_operand_endianness = false; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 557 | const char* string = |
| 558 | reinterpret_cast<const char*>(_.words + _.word_index); |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 559 | // 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 = |
David Neto | 37422e9 | 2016-12-19 13:26:42 -0500 | [diff] [blame] | 564 | spv_strnlen_s(string, remaining_input_bytes); |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 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) |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 568 | return exhaustedInputDiagnostic(inst_offset, opcode, type); |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 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 Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 573 | // Make sure we can record the word count without overflow. |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 574 | // |
| 575 | // This error can't currently be triggered because of validity |
| 576 | // checks elsewhere. |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 577 | 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) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 581 | } |
Andrew Woloszyn | 3a4bc7e | 2015-11-19 09:22:53 -0500 | [diff] [blame] | 582 | parsed_operand.num_words = uint16_t(string_num_words); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 583 | parsed_operand.type = SPV_OPERAND_TYPE_LITERAL_STRING; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 584 | |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 585 | if (SpvOpExtInstImport == opcode) { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 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) { |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 592 | return diagnostic() |
| 593 | << "Invalid extended instruction import '" << string << "'"; |
Andrew Woloszyn | e59e6b7 | 2015-10-14 14:18:43 -0400 | [diff] [blame] | 594 | } |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 595 | // 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 Woloszyn | e59e6b7 | 2015-10-14 14:18:43 -0400 | [diff] [blame] | 599 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 600 | } break; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 601 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 602 | case SPV_OPERAND_TYPE_CAPABILITY: |
| 603 | case SPV_OPERAND_TYPE_SOURCE_LANGUAGE: |
| 604 | case SPV_OPERAND_TYPE_EXECUTION_MODEL: |
| 605 | case SPV_OPERAND_TYPE_ADDRESSING_MODEL: |
| 606 | case SPV_OPERAND_TYPE_MEMORY_MODEL: |
| 607 | case SPV_OPERAND_TYPE_EXECUTION_MODE: |
| 608 | case SPV_OPERAND_TYPE_STORAGE_CLASS: |
| 609 | case SPV_OPERAND_TYPE_DIMENSIONALITY: |
| 610 | case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE: |
| 611 | case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE: |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 612 | case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 613 | case SPV_OPERAND_TYPE_FP_ROUNDING_MODE: |
| 614 | case SPV_OPERAND_TYPE_LINKAGE_TYPE: |
| 615 | case SPV_OPERAND_TYPE_ACCESS_QUALIFIER: |
David Neto | 2889a0c | 2016-02-15 13:50:00 -0500 | [diff] [blame] | 616 | case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 617 | case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE: |
| 618 | case SPV_OPERAND_TYPE_DECORATION: |
| 619 | case SPV_OPERAND_TYPE_BUILT_IN: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 620 | case SPV_OPERAND_TYPE_GROUP_OPERATION: |
| 621 | case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS: |
David Neto | 59de610 | 2017-12-03 12:30:08 -0500 | [diff] [blame] | 622 | case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: |
| 623 | case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
| 624 | case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE: |
| 625 | case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER: |
| 626 | case SPV_OPERAND_TYPE_DEBUG_OPERATION: { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 627 | // A single word that is a plain enum value. |
David Neto | 2889a0c | 2016-02-15 13:50:00 -0500 | [diff] [blame] | 628 | |
| 629 | // Map an optional operand type to its corresponding concrete type. |
| 630 | if (type == SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER) |
| 631 | parsed_operand.type = SPV_OPERAND_TYPE_ACCESS_QUALIFIER; |
| 632 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 633 | spv_operand_desc entry; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 634 | if (grammar_.lookupOperand(type, word, &entry)) { |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 635 | return diagnostic() |
| 636 | << "Invalid " << spvOperandTypeStr(parsed_operand.type) |
| 637 | << " operand: " << word; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 638 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 639 | // Prepare to accept operands to this operand, if needed. |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 640 | spvPushOperandTypes(entry->operandTypes, expected_operands); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 641 | } break; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 642 | |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 643 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
| 644 | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
| 645 | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
David Neto | 2889a0c | 2016-02-15 13:50:00 -0500 | [diff] [blame] | 646 | case SPV_OPERAND_TYPE_IMAGE: |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 647 | case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
| 648 | case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
David Neto | 59de610 | 2017-12-03 12:30:08 -0500 | [diff] [blame] | 649 | case SPV_OPERAND_TYPE_SELECTION_CONTROL: |
| 650 | case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS: { |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 651 | // This operand is a mask. |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 652 | |
| 653 | // Map an optional operand type to its corresponding concrete type. |
| 654 | if (type == SPV_OPERAND_TYPE_OPTIONAL_IMAGE) |
| 655 | parsed_operand.type = SPV_OPERAND_TYPE_IMAGE; |
| 656 | else if (type == SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS) |
| 657 | parsed_operand.type = SPV_OPERAND_TYPE_MEMORY_ACCESS; |
| 658 | |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 659 | // Check validity of set mask bits. Also prepare for operands for those |
| 660 | // masks if they have any. To get operand order correct, scan from |
| 661 | // MSB to LSB since we can only prepend operands to a pattern. |
| 662 | // The only case in the grammar where you have more than one mask bit |
| 663 | // having an operand is for image operands. See SPIR-V 3.14 Image |
| 664 | // Operands. |
| 665 | uint32_t remaining_word = word; |
| 666 | for (uint32_t mask = (1u << 31); remaining_word; mask >>= 1) { |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 667 | if (remaining_word & mask) { |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 668 | spv_operand_desc entry; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 669 | if (grammar_.lookupOperand(type, mask, &entry)) { |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 670 | return diagnostic() |
| 671 | << "Invalid " << spvOperandTypeStr(parsed_operand.type) |
| 672 | << " operand: " << word << " has invalid mask component " |
| 673 | << mask; |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 674 | } |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 675 | remaining_word ^= mask; |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 676 | spvPushOperandTypes(entry->operandTypes, expected_operands); |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 677 | } |
| 678 | } |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 679 | if (word == 0) { |
| 680 | // An all-zeroes mask *might* also be valid. |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 681 | spv_operand_desc entry; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 682 | if (SPV_SUCCESS == grammar_.lookupOperand(type, 0, &entry)) { |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 683 | // Prepare for its operands, if any. |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 684 | spvPushOperandTypes(entry->operandTypes, expected_operands); |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 685 | } |
| 686 | } |
David Neto | 619db26 | 2015-09-25 12:43:37 -0400 | [diff] [blame] | 687 | } break; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 688 | default: |
| 689 | return diagnostic() << "Internal error: Unhandled operand type: " << type; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 690 | } |
| 691 | |
David Neto | 0dbe184 | 2017-12-03 14:26:16 -0500 | [diff] [blame] | 692 | assert(spvOperandIsConcrete(parsed_operand.type)); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 693 | |
| 694 | operands->push_back(parsed_operand); |
| 695 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 696 | const size_t index_after_operand = _.word_index + parsed_operand.num_words; |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 697 | |
| 698 | // Avoid buffer overrun for the cases where the operand has more than one |
| 699 | // word, and where it isn't a string. (Those other cases have already been |
| 700 | // handled earlier.) For example, this error can occur for a multi-word |
| 701 | // argument to OpConstant, or a multi-word case literal operand for OpSwitch. |
| 702 | if (_.num_words < index_after_operand) |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 703 | return exhaustedInputDiagnostic(inst_offset, opcode, type); |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 704 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 705 | if (_.requires_endian_conversion) { |
| 706 | // Copy instruction words. Translate to native endianness as needed. |
| 707 | if (convert_operand_endianness) { |
| 708 | const spv_endianness_t endianness = _.endian; |
| 709 | std::transform(_.words + _.word_index, _.words + index_after_operand, |
Andrew Woloszyn | 3b69d05 | 2016-01-11 13:54:30 -0500 | [diff] [blame] | 710 | std::back_inserter(*words), |
| 711 | [endianness](const uint32_t raw_word) { |
David Neto | 677e0c7 | 2016-01-05 14:56:02 -0500 | [diff] [blame] | 712 | return spvFixWord(raw_word, endianness); |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 713 | }); |
| 714 | } else { |
| 715 | words->insert(words->end(), _.words + _.word_index, |
| 716 | _.words + index_after_operand); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | // Advance past the operand. |
| 721 | _.word_index = index_after_operand; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 722 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 723 | return SPV_SUCCESS; |
| 724 | } |
| 725 | |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 726 | spv_result_t Parser::setNumericTypeInfoForType( |
| 727 | spv_parsed_operand_t* parsed_operand, uint32_t type_id) { |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 728 | assert(type_id != 0); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 729 | auto type_info_iter = _.type_id_to_number_type_info.find(type_id); |
| 730 | if (type_info_iter == _.type_id_to_number_type_info.end()) { |
| 731 | return diagnostic() << "Type Id " << type_id << " is not a type"; |
| 732 | } |
| 733 | const NumberType& info = type_info_iter->second; |
| 734 | if (info.type == SPV_NUMBER_NONE) { |
| 735 | // This is a valid type, but for something other than a scalar number. |
| 736 | return diagnostic() << "Type Id " << type_id |
| 737 | << " is not a scalar numeric type"; |
Andrew Woloszyn | 157e41b | 2015-10-16 15:11:00 -0400 | [diff] [blame] | 738 | } |
| 739 | |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 740 | parsed_operand->number_kind = info.type; |
| 741 | parsed_operand->number_bit_width = info.bit_width; |
David Neto | 066bd52 | 2016-01-05 14:57:58 -0500 | [diff] [blame] | 742 | // Round up the word count. |
| 743 | parsed_operand->num_words = static_cast<uint16_t>((info.bit_width + 31) / 32); |
Andrew Woloszyn | 157e41b | 2015-10-16 15:11:00 -0400 | [diff] [blame] | 744 | return SPV_SUCCESS; |
| 745 | } |
| 746 | |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 747 | void Parser::recordNumberType(size_t inst_offset, |
| 748 | const spv_parsed_instruction_t* inst) { |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 749 | const SpvOp opcode = static_cast<SpvOp>(inst->opcode); |
| 750 | if (spvOpcodeGeneratesType(opcode)) { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 751 | NumberType info = {SPV_NUMBER_NONE, 0}; |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 752 | if (SpvOpTypeInt == opcode) { |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 753 | const bool is_signed = peekAt(inst_offset + 3) != 0; |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 754 | info.type = is_signed ? SPV_NUMBER_SIGNED_INT : SPV_NUMBER_UNSIGNED_INT; |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 755 | info.bit_width = peekAt(inst_offset + 2); |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 756 | } else if (SpvOpTypeFloat == opcode) { |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 757 | info.type = SPV_NUMBER_FLOATING; |
David Neto | 7bff3eb | 2015-11-20 14:21:10 -0500 | [diff] [blame] | 758 | info.bit_width = peekAt(inst_offset + 2); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 759 | } |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 760 | // The *result* Id of a type generating instruction is the type Id. |
| 761 | _.type_id_to_number_type_info[inst->result_id] = info; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 762 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 763 | } |
| 764 | |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 765 | } // anonymous namespace |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 766 | |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 767 | spv_result_t spvBinaryParse(const spv_const_context context, void* user_data, |
| 768 | const uint32_t* code, const size_t num_words, |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 769 | spv_parsed_header_fn_t parsed_header, |
| 770 | spv_parsed_instruction_fn_t parsed_instruction, |
| 771 | spv_diagnostic* diagnostic) { |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 772 | spv_context_t hijack_context = *context; |
| 773 | if (diagnostic) { |
| 774 | *diagnostic = nullptr; |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 775 | spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, diagnostic); |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 776 | } |
| 777 | Parser parser(&hijack_context, user_data, parsed_header, parsed_instruction); |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 778 | return parser.parse(code, num_words, diagnostic); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 779 | } |
| 780 | |
David Neto | 0ca6b59 | 2015-10-30 16:06:15 -0400 | [diff] [blame] | 781 | // TODO(dneto): This probably belongs in text.cpp since that's the only place |
| 782 | // that a spv_binary_t value is created. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 783 | void spvBinaryDestroy(spv_binary binary) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 784 | if (!binary) return; |
Eric Engestrom | eb6ae97 | 2016-02-18 23:41:16 +0000 | [diff] [blame] | 785 | delete[] binary->code; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 786 | delete binary; |
| 787 | } |
David Neto | 37422e9 | 2016-12-19 13:26:42 -0500 | [diff] [blame] | 788 | |
| 789 | size_t spv_strnlen_s(const char* str, size_t strsz) { |
| 790 | if (!str) return 0; |
| 791 | for (size_t i = 0; i < strsz; i++) { |
| 792 | if (!str[i]) return i; |
| 793 | } |
| 794 | return strsz; |
| 795 | } |