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