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 | |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 27 | #include "text.h" |
| 28 | |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <cassert> |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 31 | #include <cctype> |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 32 | #include <cstdio> |
| 33 | #include <cstdlib> |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 34 | #include <cstring> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 35 | #include <memory> |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 36 | #include <string> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 37 | #include <sstream> |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 38 | #include <unordered_map> |
| 39 | #include <vector> |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 40 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 41 | #include "binary.h" |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 42 | #include "bitwisecast.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 43 | #include "diagnostic.h" |
| 44 | #include "ext_inst.h" |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 45 | #include "instruction.h" |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 46 | #include <libspirv/libspirv.h> |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 47 | #include "opcode.h" |
| 48 | #include "operand.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 49 | #include "text_handler.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 50 | |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 51 | bool spvIsValidIDCharacter(const char value) { |
| 52 | return value == '_' || 0 != ::isalnum(value); |
| 53 | } |
| 54 | |
| 55 | // Returns true if the given string represents a valid ID name. |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 56 | bool spvIsValidID(const char* textValue) { |
| 57 | const char* c = textValue; |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 58 | for (; *c != '\0'; ++c) { |
| 59 | if (!spvIsValidIDCharacter(*c)) { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | // If the string was empty, then the ID also is not valid. |
| 64 | return c != textValue; |
| 65 | } |
| 66 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 67 | // Text API |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 68 | |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 69 | spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 70 | bool isSigned = false; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 71 | int numPeriods = 0; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 72 | bool isString = false; |
| 73 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 74 | const size_t len = strlen(textValue); |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 75 | if (len == 0) return SPV_FAILED_MATCH; |
| 76 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 77 | for (uint64_t index = 0; index < len; ++index) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 78 | switch (textValue[index]) { |
| 79 | case '0': |
| 80 | case '1': |
| 81 | case '2': |
| 82 | case '3': |
| 83 | case '4': |
| 84 | case '5': |
| 85 | case '6': |
| 86 | case '7': |
| 87 | case '8': |
| 88 | case '9': |
| 89 | break; |
| 90 | case '.': |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 91 | numPeriods++; |
| 92 | break; |
| 93 | case '-': |
| 94 | if (index == 0) { |
| 95 | isSigned = true; |
| 96 | } else { |
| 97 | isString = true; |
| 98 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 99 | break; |
| 100 | default: |
| 101 | isString = true; |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 102 | index = len; // break out of the loop too. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 107 | pLiteral->type = spv_literal_type_t(99); |
| 108 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 109 | if (isString || numPeriods > 1 || (isSigned && len == 1)) { |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 110 | if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"') |
| 111 | return SPV_FAILED_MATCH; |
Andrew Woloszyn | 3e69cd1 | 2015-10-14 12:44:19 -0400 | [diff] [blame] | 112 | bool escaping = false; |
| 113 | size_t write_index = 0; |
| 114 | for(const char* val = textValue + 1; val != textValue + len - 1; ++val) { |
| 115 | if ((*val == '\\') && (!escaping)) { |
| 116 | escaping = true; |
| 117 | } else { |
| 118 | // Have to save space for the null-terminator |
| 119 | if (write_index >= sizeof(pLiteral->value.str) - 1) |
| 120 | return SPV_ERROR_OUT_OF_MEMORY; |
| 121 | pLiteral->value.str[write_index] = *val; |
| 122 | escaping = false; |
| 123 | ++write_index; |
| 124 | } |
| 125 | } |
| 126 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 127 | pLiteral->type = SPV_LITERAL_TYPE_STRING; |
Andrew Woloszyn | 3e69cd1 | 2015-10-14 12:44:19 -0400 | [diff] [blame] | 128 | pLiteral->value.str[write_index] = '\0'; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 129 | } else if (numPeriods == 1) { |
| 130 | double d = std::strtod(textValue, nullptr); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 131 | float f = (float)d; |
| 132 | if (d == (double)f) { |
| 133 | pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32; |
| 134 | pLiteral->value.f = f; |
| 135 | } else { |
| 136 | pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64; |
| 137 | pLiteral->value.d = d; |
| 138 | } |
| 139 | } else if (isSigned) { |
| 140 | int64_t i64 = strtoll(textValue, nullptr, 10); |
| 141 | int32_t i32 = (int32_t)i64; |
| 142 | if (i64 == (int64_t)i32) { |
| 143 | pLiteral->type = SPV_LITERAL_TYPE_INT_32; |
| 144 | pLiteral->value.i32 = i32; |
| 145 | } else { |
| 146 | pLiteral->type = SPV_LITERAL_TYPE_INT_64; |
| 147 | pLiteral->value.i64 = i64; |
| 148 | } |
| 149 | } else { |
| 150 | uint64_t u64 = strtoull(textValue, nullptr, 10); |
| 151 | uint32_t u32 = (uint32_t)u64; |
| 152 | if (u64 == (uint64_t)u32) { |
| 153 | pLiteral->type = SPV_LITERAL_TYPE_UINT_32; |
| 154 | pLiteral->value.u32 = u32; |
| 155 | } else { |
| 156 | pLiteral->type = SPV_LITERAL_TYPE_UINT_64; |
| 157 | pLiteral->value.u64 = u64; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return SPV_SUCCESS; |
| 162 | } |
| 163 | |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 164 | namespace { |
| 165 | |
| 166 | /// Parses an immediate integer from text, guarding against overflow. If |
| 167 | /// successful, adds the parsed value to pInst, advances the context past it, |
| 168 | /// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics, |
| 169 | /// and returns SPV_ERROR_INVALID_TEXT. |
| 170 | spv_result_t encodeImmediate(libspirv::AssemblyContext* context, |
| 171 | const char* text, spv_instruction_t* pInst) { |
| 172 | assert(*text == '!'); |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 173 | uint32_t parse_result; |
| 174 | if (auto error = |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 175 | context->parseNumber(text + 1, SPV_ERROR_INVALID_TEXT, &parse_result, |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 176 | "Invalid immediate integer: !")) |
| 177 | return error; |
| 178 | context->binaryEncodeU32(parse_result, pInst); |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 179 | context->seekForward(strlen(text)); |
| 180 | return SPV_SUCCESS; |
| 181 | } |
| 182 | |
| 183 | } // anonymous namespace |
| 184 | |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 185 | /// @brief Translate an Opcode operand to binary form |
| 186 | /// |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 187 | /// @param[in] grammar the grammar to use for compilation |
| 188 | /// @param[in, out] context the dynamic compilation info |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 189 | /// @param[in] type of the operand |
| 190 | /// @param[in] textValue word of text to be parsed |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 191 | /// @param[out] pInst return binary Opcode |
| 192 | /// @param[in,out] pExpectedOperands the operand types expected |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 193 | /// |
| 194 | /// @return result code |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 195 | spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 196 | libspirv::AssemblyContext* context, |
| 197 | const spv_operand_type_t type, |
| 198 | const char* textValue, |
| 199 | spv_instruction_t* pInst, |
| 200 | spv_operand_pattern_t* pExpectedOperands) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 201 | // NOTE: Handle immediate int in the stream |
| 202 | if ('!' == textValue[0]) { |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 203 | if (auto error = encodeImmediate(context, textValue, pInst)) { |
| 204 | return error; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 205 | } |
Dejan Mircevski | 897bff9 | 2015-09-29 10:38:18 -0400 | [diff] [blame] | 206 | *pExpectedOperands = |
| 207 | spvAlternatePatternFollowingImmediate(*pExpectedOperands); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 208 | return SPV_SUCCESS; |
| 209 | } |
| 210 | |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 211 | // Optional literal operands can fail to parse. In that case use |
| 212 | // SPV_FAILED_MATCH to avoid emitting a diagostic. Use the following |
| 213 | // for those situations. |
| 214 | spv_result_t error_code_for_literals = |
| 215 | spvOperandIsOptional(type) ? SPV_FAILED_MATCH : SPV_ERROR_INVALID_TEXT; |
| 216 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 217 | switch (type) { |
David Neto | b14a727 | 2015-09-25 13:56:09 -0400 | [diff] [blame] | 218 | case SPV_OPERAND_TYPE_EXECUTION_SCOPE: |
David Neto | e3f70b9 | 2015-08-27 13:50:05 -0400 | [diff] [blame] | 219 | case SPV_OPERAND_TYPE_ID: |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 220 | case SPV_OPERAND_TYPE_TYPE_ID: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 221 | case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE: |
| 222 | case SPV_OPERAND_TYPE_OPTIONAL_ID: |
David Neto | b14a727 | 2015-09-25 13:56:09 -0400 | [diff] [blame] | 223 | case SPV_OPERAND_TYPE_MEMORY_SEMANTICS: |
| 224 | case SPV_OPERAND_TYPE_RESULT_ID: { |
Lei Zhang | abafd5e | 2015-08-21 11:52:29 -0400 | [diff] [blame] | 225 | if ('%' == textValue[0]) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 226 | textValue++; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 227 | } else { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 228 | return context->diagnostic() << "Expected id to start with %."; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 229 | } |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 230 | if (!spvIsValidID(textValue)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 231 | return context->diagnostic() << "Invalid ID " << textValue; |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 232 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 233 | const uint32_t id = context->spvNamedIdAssignOrGet(textValue); |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 234 | if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 235 | spvInstructionAddWord(pInst, id); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 236 | } break; |
| 237 | case SPV_OPERAND_TYPE_LITERAL_NUMBER: { |
| 238 | // NOTE: Special case for extension instruction lookup |
| 239 | if (OpExtInst == pInst->opcode) { |
| 240 | spv_ext_inst_desc extInst; |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 241 | if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 242 | return context->diagnostic() << "Invalid extended instruction name '" |
| 243 | << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 244 | } |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 245 | spvInstructionAddWord(pInst, extInst->ext_inst); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 246 | |
| 247 | // Prepare to parse the operands for the extended instructions. |
| 248 | spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands); |
| 249 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 250 | return SPV_SUCCESS; |
| 251 | } |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 252 | } // Fall through for the general case. |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 253 | case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER: |
David Neto | 561dc4e | 2015-09-25 14:23:29 -0400 | [diff] [blame] | 254 | case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE: |
| 255 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 256 | libspirv::IdType expected_type = libspirv::kUnknownType; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 257 | // The encoding for OpConstant, OpSpecConstant and OpSwitch all |
| 258 | // depend on either their own result-id or the result-id of |
| 259 | // one of their parameters. |
| 260 | if (OpConstant == pInst->opcode || OpSpecConstant == pInst->opcode) { |
| 261 | // Special cases for encoding possibly non-32-bit literals here. |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 262 | expected_type = |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 263 | context->getTypeOfTypeGeneratingValue(pInst->resultTypeId); |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 264 | if (!libspirv::isScalarFloating(expected_type) && |
| 265 | !libspirv::isScalarIntegral(expected_type)) { |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 266 | spv_opcode_desc d; |
| 267 | const char* opcode_name = "opcode"; |
| 268 | if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) { |
| 269 | opcode_name = d->name; |
| 270 | } |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 271 | return context->diagnostic() |
| 272 | << "Type for " << opcode_name |
| 273 | << " must be a scalar floating point or integer type"; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 274 | } |
| 275 | } else if (pInst->opcode == OpSwitch) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 276 | // We need to know the type of the selector. |
| 277 | expected_type = context->getTypeOfValueInstruction(pInst->words[1]); |
| 278 | if (!libspirv::isScalarIntegral(expected_type)) { |
| 279 | context->diagnostic() |
| 280 | << "The selector operand for OpSwitch must be the result" |
| 281 | " of an instruction that generates an integer scalar"; |
| 282 | return SPV_ERROR_INVALID_TEXT; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 283 | } |
| 284 | } |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 285 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 286 | if (auto error = context->binaryEncodeNumericLiteral( |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 287 | textValue, error_code_for_literals, expected_type, pInst)) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 288 | return error; |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 289 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 290 | } break; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 291 | case SPV_OPERAND_TYPE_LITERAL_STRING: |
| 292 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: { |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 293 | spv_literal_t literal = {}; |
| 294 | spv_result_t error = spvTextToLiteral(textValue, &literal); |
| 295 | if (error != SPV_SUCCESS) { |
| 296 | if (error == SPV_ERROR_OUT_OF_MEMORY) return error; |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 297 | return context->diagnostic(error_code_for_literals) |
| 298 | << "Invalid literal string '" << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 299 | } |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 300 | if (literal.type != SPV_LITERAL_TYPE_STRING) { |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 301 | return context->diagnostic() |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 302 | << "Expected literal string, found literal number '" << textValue |
| 303 | << "'."; |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 304 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 305 | |
| 306 | // NOTE: Special case for extended instruction library import |
| 307 | if (OpExtInstImport == pInst->opcode) { |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 308 | pInst->extInstType = spvExtInstImportTypeGet(literal.value.str); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 311 | if (context->binaryEncodeString(literal.value.str, pInst)) |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 312 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 313 | } break; |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 314 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
| 315 | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
| 316 | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
David Neto | ee1b3bb | 2015-09-18 11:19:18 -0400 | [diff] [blame] | 317 | case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 318 | case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 319 | case SPV_OPERAND_TYPE_SELECTION_CONTROL: { |
| 320 | uint32_t value; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 321 | if (grammar.parseMaskOperand(type, textValue, &value)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 322 | return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) |
| 323 | << " '" << textValue << "'."; |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 324 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 325 | if (auto error = context->binaryEncodeU32(value, pInst)) return error; |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 326 | // Prepare to parse the operands for this logical operand. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 327 | grammar.prependOperandTypesForMask(type, value, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 328 | } break; |
| 329 | case SPV_OPERAND_TYPE_OPTIONAL_CIV: { |
| 330 | auto error = spvTextEncodeOperand( |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 331 | grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue, |
| 332 | pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 333 | if (error == SPV_FAILED_MATCH) { |
| 334 | // It's not a literal number -- is it a literal string? |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 335 | error = spvTextEncodeOperand(grammar, context, |
| 336 | SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING, |
| 337 | textValue, pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 338 | } |
| 339 | if (error == SPV_FAILED_MATCH) { |
| 340 | // It's not a literal -- is it an ID? |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 341 | error = |
| 342 | spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID, |
| 343 | textValue, pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 344 | } |
| 345 | if (error) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 346 | return context->diagnostic(error) |
| 347 | << "Invalid word following !<integer>: " << textValue; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 348 | } |
| 349 | if (pExpectedOperands->empty()) { |
| 350 | pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV); |
| 351 | } |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 352 | } break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 353 | default: { |
| 354 | // NOTE: All non literal operands are handled here using the operand |
| 355 | // table. |
| 356 | spv_operand_desc entry; |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 357 | if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 358 | return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) |
| 359 | << " '" << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 360 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 361 | if (context->binaryEncodeU32(entry->value, pInst)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 362 | return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) |
| 363 | << " '" << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 364 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 365 | |
| 366 | // Prepare to parse the operands for this logical operand. |
| 367 | spvPrependOperandTypes(entry->operandTypes, pExpectedOperands); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 368 | } break; |
| 369 | } |
| 370 | return SPV_SUCCESS; |
| 371 | } |
| 372 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 373 | namespace { |
| 374 | |
| 375 | /// Encodes an instruction started by !<integer> at the given position in text. |
| 376 | /// |
| 377 | /// Puts the encoded words into *pInst. If successful, moves position past the |
| 378 | /// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and |
| 379 | /// leaves position pointing to the error in text. |
| 380 | spv_result_t encodeInstructionStartingWithImmediate( |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 381 | const libspirv::AssemblyGrammar& grammar, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 382 | libspirv::AssemblyContext* context, spv_instruction_t* pInst) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 383 | std::string firstWord; |
| 384 | spv_position_t nextPosition = {}; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 385 | auto error = context->getWord(firstWord, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 386 | if (error) return context->diagnostic(error) << "Internal Error"; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 387 | |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 388 | if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) { |
| 389 | return error; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 390 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 391 | while (context->advance() != SPV_END_OF_STREAM) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 392 | // A beginning of a new instruction means we're done. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 393 | if (context->isStartOfNewInst()) return SPV_SUCCESS; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 394 | |
| 395 | // Otherwise, there must be an operand that's either a literal, an ID, or |
| 396 | // an immediate. |
| 397 | std::string operandValue; |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 398 | if ((error = context->getWord(operandValue, &nextPosition))) |
| 399 | return context->diagnostic(error) << "Internal Error"; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 400 | |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 401 | if (operandValue == "=") |
| 402 | return context->diagnostic() << firstWord << " not allowed before =."; |
Dejan Mircevski | e3a19c0 | 2015-09-11 15:03:54 -0400 | [diff] [blame] | 403 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 404 | // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be |
| 405 | // expanded. |
| 406 | spv_operand_pattern_t dummyExpectedOperands; |
| 407 | error = spvTextEncodeOperand( |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 408 | grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(), |
| 409 | pInst, &dummyExpectedOperands); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 410 | if (error) return error; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 411 | context->setPosition(nextPosition); |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 412 | } |
| 413 | return SPV_SUCCESS; |
| 414 | } |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 415 | } // anonymous namespace |
| 416 | |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 417 | /// @brief Translate single Opcode and operands to binary form |
| 418 | /// |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 419 | /// @param[in] grammar the grammar to use for compilation |
| 420 | /// @param[in, out] context the dynamic compilation info |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 421 | /// @param[in] text stream to translate |
| 422 | /// @param[in] format the assembly syntax format of text |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 423 | /// @param[out] pInst returned binary Opcode |
| 424 | /// @param[in,out] pPosition in the text stream |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 425 | /// |
| 426 | /// @return result code |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 427 | spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 428 | libspirv::AssemblyContext* context, |
| 429 | spv_assembly_syntax_format_t format, |
| 430 | spv_instruction_t* pInst) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 431 | // Check for !<integer> first. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 432 | if ('!' == context->peek()) { |
| 433 | return encodeInstructionStartingWithImmediate(grammar, context, pInst); |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 436 | // An assembly instruction has two possible formats: |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 437 | // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void". |
| 438 | // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid". |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 439 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 440 | std::string firstWord; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 441 | spv_position_t nextPosition = {}; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 442 | spv_result_t error = context->getWord(firstWord, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 443 | if (error) return context->diagnostic() << "Internal Error"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 444 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 445 | std::string opcodeName; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 446 | std::string result_id; |
| 447 | spv_position_t result_id_position = {}; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 448 | if (context->startsWithOp()) { |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 449 | opcodeName = firstWord; |
| 450 | } else { |
| 451 | // If the first word of this instruction is not an opcode, we must be |
| 452 | // processing AAF now. |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 453 | if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 454 | return context->diagnostic() |
| 455 | << "Expected <opcode> at the beginning of an instruction, found '" |
| 456 | << firstWord << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 457 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 458 | |
| 459 | result_id = firstWord; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 460 | if ('%' != result_id.front()) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 461 | return context->diagnostic() |
| 462 | << "Expected <opcode> or <result-id> at the beginning " |
| 463 | "of an instruction, found '" |
| 464 | << result_id << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 465 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 466 | result_id_position = context->position(); |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 467 | |
| 468 | // The '=' sign. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 469 | context->setPosition(nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 470 | if (context->advance()) |
| 471 | return context->diagnostic() << "Expected '=', found end of stream."; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 472 | std::string equal_sign; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 473 | error = context->getWord(equal_sign, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 474 | if ("=" != equal_sign) |
| 475 | return context->diagnostic() << "'=' expected after result id."; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 476 | |
| 477 | // The <opcode> after the '=' sign. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 478 | context->setPosition(nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 479 | if (context->advance()) |
| 480 | return context->diagnostic() << "Expected opcode, found end of stream."; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 481 | error = context->getWord(opcodeName, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 482 | if (error) |
| 483 | return context->diagnostic(error) << "Internal Error"; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 484 | if (!context->startsWithOp()) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 485 | return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName |
| 486 | << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 487 | } |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 488 | } |
| 489 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 490 | // NOTE: The table contains Opcode names without the "Op" prefix. |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 491 | const char* pInstName = opcodeName.data() + 2; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 492 | |
| 493 | spv_opcode_desc opcodeEntry; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 494 | error = grammar.lookupOpcode(pInstName, &opcodeEntry); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 495 | if (error) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 496 | return context->diagnostic(error) << "Invalid Opcode name '" |
| 497 | << context->getWord() << "'"; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 498 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 499 | if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) { |
| 500 | // If this instruction has <result-id>, check it follows AAF. |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 501 | if (opcodeEntry->hasResult && result_id.empty()) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 502 | return context->diagnostic() |
| 503 | << "Expected <result-id> at the beginning of an " |
| 504 | "instruction, found '" |
| 505 | << firstWord << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 506 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 507 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 508 | pInst->opcode = opcodeEntry->opcode; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 509 | context->setPosition(nextPosition); |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 510 | // Reserve the first word for the instruction. |
| 511 | spvInstructionAddWord(pInst, 0); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 512 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 513 | // Maintains the ordered list of expected operand types. |
| 514 | // For many instructions we only need the {numTypes, operandTypes} |
| 515 | // entries in opcodeEntry. However, sometimes we need to modify |
| 516 | // the list as we parse the operands. This occurs when an operand |
| 517 | // has its own logical operands (such as the LocalSize operand for |
| 518 | // ExecutionMode), or for extended instructions that may have their |
| 519 | // own operands depending on the selected extended instruction. |
| 520 | spv_operand_pattern_t expectedOperands( |
| 521 | opcodeEntry->operandTypes, |
| 522 | opcodeEntry->operandTypes + opcodeEntry->numTypes); |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 523 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 524 | while (!expectedOperands.empty()) { |
| 525 | const spv_operand_type_t type = expectedOperands.front(); |
| 526 | expectedOperands.pop_front(); |
| 527 | |
| 528 | // Expand optional tuples lazily. |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 529 | if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 530 | |
| 531 | if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) { |
| 532 | // Handle the <result-id> for value generating instructions. |
| 533 | // We've already consumed it from the text stream. Here |
| 534 | // we inject its words into the instruction. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 535 | spv_position_t temp_pos = context->position(); |
| 536 | error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID, |
| 537 | result_id.c_str(), pInst, nullptr); |
| 538 | result_id_position = context->position(); |
| 539 | // Because we are injecting we have to reset the position afterwards. |
| 540 | context->setPosition(temp_pos); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 541 | if (error) return error; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 542 | } else { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 543 | // Find the next word. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 544 | error = context->advance(); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 545 | if (error == SPV_END_OF_STREAM) { |
| 546 | if (spvOperandIsOptional(type)) { |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 547 | // This would have been the last potential operand for the |
| 548 | // instruction, |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 549 | // and we didn't find one. We're finished parsing this instruction. |
| 550 | break; |
| 551 | } else { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 552 | return context->diagnostic() |
| 553 | << "Expected operand, found end of stream."; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 554 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 555 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 556 | assert(error == SPV_SUCCESS && "Somebody added another way to fail"); |
| 557 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 558 | if (context->isStartOfNewInst()) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 559 | if (spvOperandIsOptional(type)) { |
| 560 | break; |
| 561 | } else { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 562 | return context->diagnostic() |
| 563 | << "Expected operand, found next instruction instead."; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | std::string operandValue; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 568 | error = context->getWord(operandValue, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 569 | if (error) return context->diagnostic(error) << "Internal Error"; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 570 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 571 | error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(), |
| 572 | pInst, &expectedOperands); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 573 | |
| 574 | if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type)) |
| 575 | return SPV_SUCCESS; |
| 576 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 577 | if (error) return error; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 578 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 579 | context->setPosition(nextPosition); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 583 | if (spvOpcodeGeneratesType(pInst->opcode)) { |
| 584 | if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) { |
| 585 | return SPV_ERROR_INVALID_TEXT; |
| 586 | } |
| 587 | } else if (opcodeEntry->hasType) { |
| 588 | // SPIR-V dictates that if an instruction has both a return value and a |
| 589 | // type ID then the type id is first, and the return value is second. |
| 590 | assert(opcodeEntry->hasResult && |
| 591 | "Unknown opcode: has a type but no result."); |
| 592 | context->recordTypeIdForValue(pInst->words[2], pInst->words[1]); |
| 593 | } |
| 594 | |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 595 | if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 596 | return context->diagnostic() |
| 597 | << "Instruction too long: " << pInst->words.size() |
| 598 | << " words, but the limit is " |
| 599 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 603 | |
| 604 | return SPV_SUCCESS; |
| 605 | } |
| 606 | |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 607 | namespace { |
| 608 | |
| 609 | // Translates a given assembly language module into binary form. |
| 610 | // If a diagnostic is generated, it is not yet marked as being |
| 611 | // for a text-based input. |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 612 | spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar, |
| 613 | const spv_text text, |
| 614 | spv_assembly_syntax_format_t format, |
| 615 | spv_binary* pBinary, |
| 616 | spv_diagnostic* pDiagnostic) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 617 | if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC; |
| 618 | libspirv::AssemblyContext context(text, pDiagnostic); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 619 | if (!text->str || !text->length) |
| 620 | return context.diagnostic() << "Text stream is empty."; |
| 621 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 622 | if (!grammar.isValid()) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 623 | return SPV_ERROR_INVALID_TABLE; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 624 | } |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 625 | if (!pBinary) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 626 | |
| 627 | // NOTE: Ensure diagnostic is zero initialised |
| 628 | *pDiagnostic = {}; |
| 629 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 630 | std::vector<spv_instruction_t> instructions; |
| 631 | |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 632 | if (context.advance()) return context.diagnostic() << "Text stream is empty."; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 633 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 634 | spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE; |
| 635 | while (context.hasText()) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 636 | instructions.push_back({}); |
| 637 | spv_instruction_t& inst = instructions.back(); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 638 | inst.extInstType = extInstType; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 639 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 640 | if (spvTextEncodeOpcode(grammar, &context, format, &inst)) { |
| 641 | return SPV_ERROR_INVALID_TEXT; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 642 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 643 | extInstType = inst.extInstType; |
| 644 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 645 | if (context.advance()) break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 646 | } |
| 647 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 648 | size_t totalSize = SPV_INDEX_INSTRUCTION; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 649 | for (auto& inst : instructions) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 650 | totalSize += inst.words.size(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 651 | } |
| 652 | |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 653 | uint32_t* data = new uint32_t[totalSize]; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 654 | if (!data) return SPV_ERROR_OUT_OF_MEMORY; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 655 | uint64_t currentIndex = SPV_INDEX_INSTRUCTION; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 656 | for (auto& inst : instructions) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 657 | memcpy(data + currentIndex, inst.words.data(), sizeof(uint32_t) * inst.words.size()); |
| 658 | currentIndex += inst.words.size(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | spv_binary binary = new spv_binary_t(); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 662 | if (!binary) { |
| 663 | delete[] data; |
| 664 | return SPV_ERROR_OUT_OF_MEMORY; |
| 665 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 666 | binary->code = data; |
| 667 | binary->wordCount = totalSize; |
| 668 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 669 | spv_result_t error = spvBinaryHeaderSet(binary, context.getBound()); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 670 | if (error) { |
| 671 | spvBinaryDestroy(binary); |
| 672 | return error; |
| 673 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 674 | |
| 675 | *pBinary = binary; |
| 676 | |
| 677 | return SPV_SUCCESS; |
| 678 | } |
| 679 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 680 | } // anonymous namespace |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 681 | |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 682 | spv_result_t spvTextToBinary(const char* input_text, |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 683 | const uint64_t input_text_size, |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 684 | const spv_opcode_table opcodeTable, |
| 685 | const spv_operand_table operandTable, |
| 686 | const spv_ext_inst_table extInstTable, |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 687 | spv_binary* pBinary, spv_diagnostic* pDiagnostic) { |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 688 | return spvTextWithFormatToBinary( |
| 689 | input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT, |
| 690 | opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic); |
| 691 | } |
| 692 | |
| 693 | spv_result_t spvTextWithFormatToBinary( |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 694 | const char* input_text, const uint64_t input_text_size, |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 695 | spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable, |
| 696 | const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 697 | spv_binary* pBinary, spv_diagnostic* pDiagnostic) { |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 698 | spv_text_t text = {input_text, input_text_size}; |
| 699 | |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 700 | libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable); |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 701 | spv_result_t result = |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 702 | spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic); |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 703 | if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true; |
| 704 | |
| 705 | return result; |
| 706 | } |
| 707 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 708 | void spvTextDestroy(spv_text text) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 709 | if (!text) return; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 710 | if (text->str) { |
| 711 | delete[] text->str; |
| 712 | } |
| 713 | delete text; |
| 714 | } |