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