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> |
| 31 | #include <cstdio> |
| 32 | #include <cstdlib> |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 33 | #include <cstring> |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 34 | #include <string> |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 35 | #include <unordered_map> |
| 36 | #include <vector> |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 37 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 38 | #include "binary.h" |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 39 | #include "bitwisecast.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 40 | #include "diagnostic.h" |
| 41 | #include "ext_inst.h" |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 42 | #include <libspirv/libspirv.h> |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 43 | #include "opcode.h" |
| 44 | #include "operand.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 45 | |
Lei Zhang | 610c525 | 2015-09-09 10:36:48 -0400 | [diff] [blame] | 46 | using spvutils::BitwiseCast; |
| 47 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 48 | // Structures |
| 49 | |
| 50 | struct spv_named_id_table_t { |
| 51 | std::unordered_map<std::string, uint32_t> namedIds; |
| 52 | }; |
| 53 | |
| 54 | // Text API |
| 55 | |
David Neto | a48678a | 2015-09-11 12:04:03 -0400 | [diff] [blame] | 56 | std::string spvGetWord(const char *str) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 57 | size_t index = 0; |
| 58 | while (true) { |
| 59 | switch (str[index]) { |
| 60 | case '\0': |
| 61 | case '\t': |
David Neto | a48678a | 2015-09-11 12:04:03 -0400 | [diff] [blame] | 62 | case '\v': |
| 63 | case '\r': |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 64 | case '\n': |
| 65 | case ' ': |
David Neto | a48678a | 2015-09-11 12:04:03 -0400 | [diff] [blame] | 66 | return std::string(str, str + index); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 67 | default: |
| 68 | index++; |
| 69 | } |
| 70 | } |
David Neto | a48678a | 2015-09-11 12:04:03 -0400 | [diff] [blame] | 71 | assert(0 && "Unreachable"); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 72 | return ""; // Make certain compilers happy. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | spv_named_id_table spvNamedIdTableCreate() { |
| 76 | return new spv_named_id_table_t(); |
| 77 | } |
| 78 | |
| 79 | void spvNamedIdTableDestory(spv_named_id_table table) { delete table; } |
| 80 | |
| 81 | uint32_t spvNamedIdAssignOrGet(spv_named_id_table table, const char *textValue, |
| 82 | uint32_t *pBound) { |
| 83 | if (table->namedIds.end() == table->namedIds.find(textValue)) { |
| 84 | table->namedIds[textValue] = *pBound; |
| 85 | } |
| 86 | return table->namedIds[textValue]; |
| 87 | } |
| 88 | |
| 89 | int32_t spvTextIsNamedId(const char *textValue) { |
| 90 | // TODO: Strengthen the parsing of textValue to only include allow names that |
| 91 | // match: ([a-z]|[A-Z])(_|[a-z]|[A-Z]|[0-9])* |
| 92 | switch (textValue[0]) { |
| 93 | case '0': |
| 94 | case '1': |
| 95 | case '2': |
| 96 | case '3': |
| 97 | case '4': |
| 98 | case '5': |
| 99 | case '6': |
| 100 | case '7': |
| 101 | case '8': |
| 102 | case '9': |
| 103 | return false; |
| 104 | default: |
| 105 | break; |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | spv_result_t spvTextAdvanceLine(const spv_text text, spv_position position) { |
| 111 | while (true) { |
| 112 | switch (text->str[position->index]) { |
| 113 | case '\0': |
| 114 | return SPV_END_OF_STREAM; |
| 115 | case '\n': |
| 116 | position->column = 0; |
| 117 | position->line++; |
| 118 | position->index++; |
| 119 | return SPV_SUCCESS; |
| 120 | default: |
Lei Zhang | ee87cc2 | 2015-08-21 11:51:28 -0400 | [diff] [blame] | 121 | position->column++; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 122 | position->index++; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | spv_result_t spvTextAdvance(const spv_text text, spv_position position) { |
| 129 | // NOTE: Consume white space, otherwise don't advance. |
| 130 | switch (text->str[position->index]) { |
| 131 | case '\0': |
| 132 | return SPV_END_OF_STREAM; |
| 133 | case ';': |
Lei Zhang | ee87cc2 | 2015-08-21 11:51:28 -0400 | [diff] [blame] | 134 | if (spv_result_t error = spvTextAdvanceLine(text, position)) return error; |
| 135 | return spvTextAdvance(text, position); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 136 | case ' ': |
| 137 | case '\t': |
| 138 | position->column++; |
| 139 | position->index++; |
| 140 | return spvTextAdvance(text, position); |
| 141 | case '\n': |
| 142 | position->column = 0; |
| 143 | position->line++; |
| 144 | position->index++; |
| 145 | return spvTextAdvance(text, position); |
| 146 | default: |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | return SPV_SUCCESS; |
| 151 | } |
| 152 | |
| 153 | spv_result_t spvTextWordGet(const spv_text text, |
| 154 | const spv_position startPosition, std::string &word, |
| 155 | spv_position endPosition) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 156 | if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT; |
| 157 | if (!startPosition || !endPosition) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 158 | |
| 159 | *endPosition = *startPosition; |
| 160 | |
David Neto | e7ee4c4 | 2015-08-25 14:21:13 -0400 | [diff] [blame] | 161 | bool quoting = false; |
| 162 | bool escaping = false; |
| 163 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 164 | // NOTE: Assumes first character is not white space! |
| 165 | while (true) { |
David Neto | e7ee4c4 | 2015-08-25 14:21:13 -0400 | [diff] [blame] | 166 | const char ch = text->str[endPosition->index]; |
| 167 | if (ch == '\\') |
| 168 | escaping = !escaping; |
| 169 | else { |
| 170 | switch (ch) { |
| 171 | case '"': |
| 172 | if (!escaping) quoting = !quoting; |
| 173 | break; |
| 174 | case ' ': |
| 175 | case ';': |
| 176 | case '\t': |
| 177 | case '\n': |
| 178 | if (escaping || quoting) break; |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 179 | // Fall through. |
David Neto | e7ee4c4 | 2015-08-25 14:21:13 -0400 | [diff] [blame] | 180 | case '\0': { // NOTE: End of word found! |
| 181 | word.assign(text->str + startPosition->index, |
| 182 | (size_t)(endPosition->index - startPosition->index)); |
| 183 | return SPV_SUCCESS; |
| 184 | } |
| 185 | default: |
| 186 | break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 187 | } |
David Neto | e7ee4c4 | 2015-08-25 14:21:13 -0400 | [diff] [blame] | 188 | escaping = false; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | endPosition->column++; |
| 192 | endPosition->index++; |
| 193 | } |
| 194 | } |
| 195 | |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 196 | namespace { |
| 197 | |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 198 | // Returns true if the string at the given position in text starts with "Op". |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 199 | bool spvStartsWithOp(const spv_text text, const spv_position position) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 200 | if (text->length < position->index + 3) return false; |
| 201 | char ch0 = text->str[position->index]; |
| 202 | char ch1 = text->str[position->index + 1]; |
| 203 | char ch2 = text->str[position->index + 2]; |
| 204 | return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z')); |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 207 | } // anonymous namespace |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 208 | |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 209 | // Returns true if a new instruction begins at the given position in text. |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 210 | bool spvTextIsStartOfNewInst(const spv_text text, const spv_position position) { |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 211 | spv_position_t nextPosition = *position; |
| 212 | if (spvTextAdvance(text, &nextPosition)) return false; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 213 | if (spvStartsWithOp(text, &nextPosition)) return true; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 214 | |
| 215 | std::string word; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 216 | spv_position_t startPosition = *position; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 217 | if (spvTextWordGet(text, &startPosition, word, &nextPosition)) return false; |
| 218 | if ('%' != word.front()) return false; |
| 219 | |
| 220 | if (spvTextAdvance(text, &nextPosition)) return false; |
| 221 | startPosition = nextPosition; |
| 222 | if (spvTextWordGet(text, &startPosition, word, &nextPosition)) return false; |
| 223 | if ("=" != word) return false; |
| 224 | |
| 225 | if (spvTextAdvance(text, &nextPosition)) return false; |
| 226 | startPosition = nextPosition; |
| 227 | if (spvStartsWithOp(text, &startPosition)) return true; |
| 228 | return false; |
| 229 | } |
| 230 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 231 | spv_result_t spvTextStringGet(const spv_text text, |
| 232 | const spv_position startPosition, |
| 233 | std::string &string, spv_position endPosition) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 234 | if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT; |
| 235 | if (!startPosition || !endPosition) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 236 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 237 | if ('"' != text->str[startPosition->index]) return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 238 | |
| 239 | *endPosition = *startPosition; |
| 240 | |
| 241 | // NOTE: Assumes first character is not white space |
| 242 | while (true) { |
| 243 | endPosition->column++; |
| 244 | endPosition->index++; |
| 245 | |
| 246 | switch (text->str[endPosition->index]) { |
| 247 | case '"': { |
| 248 | endPosition->column++; |
| 249 | endPosition->index++; |
| 250 | |
| 251 | string.assign(text->str + startPosition->index, |
| 252 | (size_t)(endPosition->index - startPosition->index)); |
| 253 | |
| 254 | return SPV_SUCCESS; |
| 255 | } |
| 256 | case '\n': |
| 257 | case '\0': |
| 258 | return SPV_ERROR_INVALID_TEXT; |
| 259 | default: |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | spv_result_t spvTextToUInt32(const char *textValue, uint32_t *pValue) { |
| 266 | char *endPtr = nullptr; |
| 267 | *pValue = strtoul(textValue, &endPtr, 0); |
| 268 | if (0 == *pValue && textValue == endPtr) { |
| 269 | return SPV_ERROR_INVALID_TEXT; |
| 270 | } |
| 271 | return SPV_SUCCESS; |
| 272 | } |
| 273 | |
| 274 | spv_result_t spvTextToLiteral(const char *textValue, spv_literal_t *pLiteral) { |
| 275 | bool isSigned = false; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 276 | int numPeriods = 0; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 277 | bool isString = false; |
| 278 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 279 | const size_t len = strlen(textValue); |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 280 | if (len == 0) return SPV_FAILED_MATCH; |
| 281 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 282 | for (uint64_t index = 0; index < len; ++index) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 283 | switch (textValue[index]) { |
| 284 | case '0': |
| 285 | case '1': |
| 286 | case '2': |
| 287 | case '3': |
| 288 | case '4': |
| 289 | case '5': |
| 290 | case '6': |
| 291 | case '7': |
| 292 | case '8': |
| 293 | case '9': |
| 294 | break; |
| 295 | case '.': |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 296 | numPeriods++; |
| 297 | break; |
| 298 | case '-': |
| 299 | if (index == 0) { |
| 300 | isSigned = true; |
| 301 | } else { |
| 302 | isString = true; |
| 303 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 304 | break; |
| 305 | default: |
| 306 | isString = true; |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 307 | index = len; // break out of the loop too. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 308 | break; |
| 309 | } |
| 310 | } |
| 311 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 312 | pLiteral->type = spv_literal_type_t(99); |
| 313 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 314 | if (isString || numPeriods > 1 || (isSigned && len == 1)) { |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 315 | // TODO(dneto): Allow escaping. |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 316 | if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"') |
| 317 | return SPV_FAILED_MATCH; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 318 | pLiteral->type = SPV_LITERAL_TYPE_STRING; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 319 | // Need room for the null-terminator. |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 320 | if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY; |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 321 | strncpy(pLiteral->value.str, textValue + 1, len - 2); |
| 322 | pLiteral->value.str[len - 2] = 0; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 323 | } else if (numPeriods == 1) { |
| 324 | double d = std::strtod(textValue, nullptr); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 325 | float f = (float)d; |
| 326 | if (d == (double)f) { |
| 327 | pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32; |
| 328 | pLiteral->value.f = f; |
| 329 | } else { |
| 330 | pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64; |
| 331 | pLiteral->value.d = d; |
| 332 | } |
| 333 | } else if (isSigned) { |
| 334 | int64_t i64 = strtoll(textValue, nullptr, 10); |
| 335 | int32_t i32 = (int32_t)i64; |
| 336 | if (i64 == (int64_t)i32) { |
| 337 | pLiteral->type = SPV_LITERAL_TYPE_INT_32; |
| 338 | pLiteral->value.i32 = i32; |
| 339 | } else { |
| 340 | pLiteral->type = SPV_LITERAL_TYPE_INT_64; |
| 341 | pLiteral->value.i64 = i64; |
| 342 | } |
| 343 | } else { |
| 344 | uint64_t u64 = strtoull(textValue, nullptr, 10); |
| 345 | uint32_t u32 = (uint32_t)u64; |
| 346 | if (u64 == (uint64_t)u32) { |
| 347 | pLiteral->type = SPV_LITERAL_TYPE_UINT_32; |
| 348 | pLiteral->value.u32 = u32; |
| 349 | } else { |
| 350 | pLiteral->type = SPV_LITERAL_TYPE_UINT_64; |
| 351 | pLiteral->value.u64 = u64; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return SPV_SUCCESS; |
| 356 | } |
| 357 | |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 358 | namespace { |
| 359 | |
| 360 | // True if type is an ID type, false otherwise. |
| 361 | bool isIdType(spv_operand_type_t type) { |
| 362 | switch (type) { |
| 363 | case SPV_OPERAND_TYPE_EXECUTION_SCOPE: |
| 364 | case SPV_OPERAND_TYPE_ID: |
| 365 | case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE: |
| 366 | case SPV_OPERAND_TYPE_OPTIONAL_ID: |
| 367 | case SPV_OPERAND_TYPE_RESULT_ID: |
| 368 | return true; |
| 369 | default: |
| 370 | return false; |
| 371 | } |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | } // anonymous namespace |
| 376 | |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 377 | spv_result_t spvTextParseMaskOperand(const spv_operand_table operandTable, |
| 378 | const spv_operand_type_t type, |
| 379 | const char *textValue, uint32_t *pValue) { |
| 380 | if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT; |
| 381 | size_t text_length = strlen(textValue); |
| 382 | if (text_length == 0) return SPV_ERROR_INVALID_TEXT; |
| 383 | const char *text_end = textValue + text_length; |
| 384 | |
| 385 | // We only support mask expressions in ASCII, so the separator value is a |
| 386 | // char. |
| 387 | const char separator = '|'; |
| 388 | |
| 389 | // Accumulate the result by interpreting one word at a time, scanning |
| 390 | // from left to right. |
| 391 | uint32_t value = 0; |
| 392 | const char *begin = textValue; // The left end of the current word. |
| 393 | const char *end = nullptr; // One character past the end of the current word. |
| 394 | do { |
| 395 | end = std::find(begin, text_end, separator); |
| 396 | |
| 397 | spv_operand_desc entry = nullptr; |
| 398 | if (spvOperandTableNameLookup(operandTable, type, begin, end - begin, |
| 399 | &entry)) { |
| 400 | return SPV_ERROR_INVALID_TEXT; |
| 401 | } |
| 402 | value |= entry->value; |
| 403 | |
| 404 | // Advance to the next word by skipping over the separator. |
| 405 | begin = end + 1; |
| 406 | } while (end != text_end); |
| 407 | |
| 408 | *pValue = value; |
| 409 | return SPV_SUCCESS; |
| 410 | } |
| 411 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 412 | spv_result_t spvTextEncodeOperand( |
| 413 | const spv_operand_type_t type, const char *textValue, |
| 414 | const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, |
| 415 | spv_named_id_table namedIdTable, spv_instruction_t *pInst, |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 416 | spv_operand_pattern_t *pExpectedOperands, uint32_t *pBound, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 417 | const spv_position position, spv_diagnostic *pDiagnostic) { |
| 418 | // NOTE: Handle immediate int in the stream |
| 419 | if ('!' == textValue[0]) { |
| 420 | const char *begin = textValue + 1; |
| 421 | char *end = nullptr; |
| 422 | uint32_t immediateInt = strtoul(begin, &end, 0); |
| 423 | size_t size = strlen(textValue); |
| 424 | size_t length = (end - begin); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 425 | if (size - 1 != length) { |
| 426 | DIAGNOSTIC << "Invalid immediate integer '" << textValue << "'."; |
| 427 | return SPV_ERROR_INVALID_TEXT; |
| 428 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 429 | position->column += size; |
| 430 | position->index += size; |
| 431 | pInst->words[pInst->wordCount] = immediateInt; |
| 432 | pInst->wordCount += 1; |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 433 | if (isIdType(type)) *pBound = std::max(*pBound, immediateInt + 1); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 434 | return SPV_SUCCESS; |
| 435 | } |
| 436 | |
| 437 | switch (type) { |
David Neto | e3f70b9 | 2015-08-27 13:50:05 -0400 | [diff] [blame] | 438 | case SPV_OPERAND_TYPE_ID: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 439 | case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE: |
| 440 | case SPV_OPERAND_TYPE_OPTIONAL_ID: |
David Neto | fadbf62 | 2015-09-14 17:07:11 -0400 | [diff] [blame] | 441 | case SPV_OPERAND_TYPE_RESULT_ID: |
| 442 | case SPV_OPERAND_TYPE_EXECUTION_SCOPE: { |
Lei Zhang | abafd5e | 2015-08-21 11:52:29 -0400 | [diff] [blame] | 443 | if ('%' == textValue[0]) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 444 | textValue++; |
| 445 | } |
Lei Zhang | abafd5e | 2015-08-21 11:52:29 -0400 | [diff] [blame] | 446 | // TODO: Force all ID's to be prefixed with '%'. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 447 | uint32_t id = 0; |
| 448 | if (spvTextIsNamedId(textValue)) { |
| 449 | id = spvNamedIdAssignOrGet(namedIdTable, textValue, pBound); |
| 450 | } else { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 451 | if (spvTextToUInt32(textValue, &id) != SPV_SUCCESS) { |
| 452 | if (spvOperandIsOptional(type)) { |
| 453 | return SPV_FAILED_MATCH; |
| 454 | } else { |
David Neto | fadbf62 | 2015-09-14 17:07:11 -0400 | [diff] [blame] | 455 | DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" |
| 456 | << textValue << "'."; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 457 | return SPV_ERROR_INVALID_TEXT; |
| 458 | } |
| 459 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 460 | } |
| 461 | pInst->words[pInst->wordCount++] = id; |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 462 | *pBound = std::max(*pBound, id + 1); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 463 | } break; |
| 464 | case SPV_OPERAND_TYPE_LITERAL_NUMBER: { |
| 465 | // NOTE: Special case for extension instruction lookup |
| 466 | if (OpExtInst == pInst->opcode) { |
| 467 | spv_ext_inst_desc extInst; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 468 | if (spvExtInstTableNameLookup(extInstTable, pInst->extInstType, |
| 469 | textValue, &extInst)) { |
| 470 | DIAGNOSTIC << "Invalid extended instruction name '" << textValue |
| 471 | << "'."; |
| 472 | return SPV_ERROR_INVALID_TEXT; |
| 473 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 474 | pInst->words[pInst->wordCount++] = extInst->ext_inst; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 475 | |
| 476 | // Prepare to parse the operands for the extended instructions. |
| 477 | spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands); |
| 478 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 479 | return SPV_SUCCESS; |
| 480 | } |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 481 | } // Fall through for the general case. |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 482 | case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER: |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 483 | // TODO(antiagainst): SPV_OPERAND_TYPE_LITERAL should be removed. |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 484 | case SPV_OPERAND_TYPE_LITERAL: |
| 485 | case SPV_OPERAND_TYPE_LITERAL_IN_OPTIONAL_TUPLE: |
| 486 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL: { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 487 | spv_literal_t literal = {}; |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 488 | spv_result_t error = spvTextToLiteral(textValue, &literal); |
| 489 | if (error != SPV_SUCCESS) { |
| 490 | if (error == SPV_ERROR_OUT_OF_MEMORY) return error; |
| 491 | if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH; |
| 492 | DIAGNOSTIC << "Invalid literal number '" << textValue << "'."; |
| 493 | return SPV_ERROR_INVALID_TEXT; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 494 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 495 | switch (literal.type) { |
Andrew Woloszyn | 4b4acde | 2015-09-10 10:28:22 -0400 | [diff] [blame] | 496 | // We do not have to print diagnostics here because spvBinaryEncode* |
| 497 | // prints diagnostic messages on failure. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 498 | case SPV_LITERAL_TYPE_INT_32: |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 499 | if (spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32), |
| 500 | pInst, position, pDiagnostic)) |
| 501 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 502 | break; |
| 503 | case SPV_LITERAL_TYPE_INT_64: { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 504 | if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64), |
| 505 | pInst, position, pDiagnostic)) |
| 506 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 507 | } break; |
| 508 | case SPV_LITERAL_TYPE_UINT_32: { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 509 | if (spvBinaryEncodeU32(literal.value.u32, pInst, position, |
| 510 | pDiagnostic)) |
| 511 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 512 | } break; |
| 513 | case SPV_LITERAL_TYPE_UINT_64: { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 514 | if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64), |
| 515 | pInst, position, pDiagnostic)) |
| 516 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 517 | } break; |
| 518 | case SPV_LITERAL_TYPE_FLOAT_32: { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 519 | if (spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f), pInst, |
| 520 | position, pDiagnostic)) |
| 521 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 522 | } break; |
| 523 | case SPV_LITERAL_TYPE_FLOAT_64: { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 524 | if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d), pInst, |
| 525 | position, pDiagnostic)) |
| 526 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 527 | } break; |
| 528 | case SPV_LITERAL_TYPE_STRING: { |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 529 | DIAGNOSTIC << "Expected literal number, found literal string '" |
| 530 | << textValue << "'."; |
| 531 | return SPV_FAILED_MATCH; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 532 | } break; |
| 533 | default: |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 534 | DIAGNOSTIC << "Invalid literal number '" << textValue << "'"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 535 | return SPV_ERROR_INVALID_TEXT; |
| 536 | } |
| 537 | } break; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 538 | case SPV_OPERAND_TYPE_LITERAL_STRING: |
| 539 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: { |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 540 | spv_literal_t literal = {}; |
| 541 | spv_result_t error = spvTextToLiteral(textValue, &literal); |
| 542 | if (error != SPV_SUCCESS) { |
| 543 | if (error == SPV_ERROR_OUT_OF_MEMORY) return error; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 544 | if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH; |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 545 | DIAGNOSTIC << "Invalid literal string '" << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 546 | return SPV_ERROR_INVALID_TEXT; |
| 547 | } |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 548 | if (literal.type != SPV_LITERAL_TYPE_STRING) { |
| 549 | DIAGNOSTIC << "Expected literal string, found literal number '" |
| 550 | << textValue << "'."; |
| 551 | return SPV_FAILED_MATCH; |
| 552 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 553 | |
| 554 | // NOTE: Special case for extended instruction library import |
| 555 | if (OpExtInstImport == pInst->opcode) { |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 556 | pInst->extInstType = spvExtInstImportTypeGet(literal.value.str); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 557 | } |
| 558 | |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 559 | if (spvBinaryEncodeString(literal.value.str, pInst, position, |
| 560 | pDiagnostic)) |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 561 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 562 | } break; |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 563 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
| 564 | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
| 565 | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
David Neto | ee1b3bb | 2015-09-18 11:19:18 -0400 | [diff] [blame] | 566 | case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 567 | case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 568 | case SPV_OPERAND_TYPE_SELECTION_CONTROL: { |
| 569 | uint32_t value; |
| 570 | if (spvTextParseMaskOperand(operandTable, type, textValue, &value)) { |
| 571 | DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue |
| 572 | << "'."; |
| 573 | return SPV_ERROR_INVALID_TEXT; |
| 574 | } |
| 575 | if (auto error = spvBinaryEncodeU32(value, pInst, position, pDiagnostic)) |
| 576 | return error; |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 577 | // Prepare to parse the operands for this logical operand. |
| 578 | spvPrependOperandTypesForMask(operandTable, type, value, pExpectedOperands); |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 579 | } break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 580 | default: { |
| 581 | // NOTE: All non literal operands are handled here using the operand |
| 582 | // table. |
| 583 | spv_operand_desc entry; |
David Neto | 388c40d | 2015-09-16 16:42:56 -0400 | [diff] [blame] | 584 | if (spvOperandTableNameLookup(operandTable, type, textValue, |
| 585 | strlen(textValue), &entry)) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 586 | DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue |
| 587 | << "'."; |
| 588 | return SPV_ERROR_INVALID_TEXT; |
| 589 | } |
| 590 | if (spvBinaryEncodeU32(entry->value, pInst, position, pDiagnostic)) { |
| 591 | DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue |
| 592 | << "'."; |
| 593 | return SPV_ERROR_INVALID_TEXT; |
| 594 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 595 | |
| 596 | // Prepare to parse the operands for this logical operand. |
| 597 | spvPrependOperandTypes(entry->operandTypes, pExpectedOperands); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 598 | } break; |
| 599 | } |
| 600 | return SPV_SUCCESS; |
| 601 | } |
| 602 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 603 | namespace { |
| 604 | |
| 605 | /// Encodes an instruction started by !<integer> at the given position in text. |
| 606 | /// |
| 607 | /// Puts the encoded words into *pInst. If successful, moves position past the |
| 608 | /// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and |
| 609 | /// leaves position pointing to the error in text. |
| 610 | spv_result_t encodeInstructionStartingWithImmediate( |
| 611 | const spv_text text, const spv_operand_table operandTable, |
| 612 | const spv_ext_inst_table extInstTable, spv_named_id_table namedIdTable, |
| 613 | uint32_t *pBound, spv_instruction_t *pInst, spv_position position, |
| 614 | spv_diagnostic *pDiagnostic) { |
| 615 | std::string firstWord; |
| 616 | spv_position_t nextPosition = {}; |
| 617 | auto error = spvTextWordGet(text, position, firstWord, &nextPosition); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 618 | if (error) { |
| 619 | DIAGNOSTIC << "Internal Error"; |
| 620 | return error; |
| 621 | } |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 622 | |
| 623 | assert(firstWord[0] == '!'); |
| 624 | const char *begin = firstWord.data() + 1; |
| 625 | char *end = nullptr; |
| 626 | uint32_t immediateInt = strtoul(begin, &end, 0); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 627 | if ((begin + firstWord.size() - 1) != end) { |
| 628 | DIAGNOSTIC << "Invalid immediate integer '" << firstWord << "'."; |
| 629 | return SPV_ERROR_INVALID_TEXT; |
| 630 | } |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 631 | position->column += firstWord.size(); |
| 632 | position->index += firstWord.size(); |
| 633 | pInst->words[0] = immediateInt; |
| 634 | pInst->wordCount = 1; |
| 635 | while (spvTextAdvance(text, position) != SPV_END_OF_STREAM) { |
| 636 | // A beginning of a new instruction means we're done. |
| 637 | if (spvTextIsStartOfNewInst(text, position)) return SPV_SUCCESS; |
| 638 | |
| 639 | // Otherwise, there must be an operand that's either a literal, an ID, or |
| 640 | // an immediate. |
| 641 | std::string operandValue; |
| 642 | if ((error = spvTextWordGet(text, position, operandValue, &nextPosition))) { |
| 643 | DIAGNOSTIC << "Internal Error"; |
| 644 | return error; |
| 645 | } |
| 646 | |
Dejan Mircevski | e3a19c0 | 2015-09-11 15:03:54 -0400 | [diff] [blame] | 647 | if (operandValue == "=") { |
| 648 | DIAGNOSTIC << firstWord << " not allowed before =."; |
| 649 | return SPV_ERROR_INVALID_TEXT; |
| 650 | } |
| 651 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 652 | // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be |
| 653 | // expanded. |
| 654 | spv_operand_pattern_t dummyExpectedOperands; |
| 655 | error = spvTextEncodeOperand( |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 656 | // TODO(antiagainst): SPV_OPERAND_TYPE_OPTIONAL_LITERAL should be |
| 657 | // substituted by SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER. |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 658 | SPV_OPERAND_TYPE_OPTIONAL_LITERAL, operandValue.c_str(), operandTable, |
| 659 | extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound, |
| 660 | position, pDiagnostic); |
| 661 | if (error == SPV_FAILED_MATCH) { |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 662 | // It's not a literal number -- is it a literal string? |
| 663 | error = spvTextEncodeOperand( |
| 664 | SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING, operandValue.c_str(), |
| 665 | operandTable, extInstTable, namedIdTable, pInst, |
| 666 | &dummyExpectedOperands, pBound, position, pDiagnostic); |
| 667 | } |
| 668 | if (error == SPV_FAILED_MATCH) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 669 | // It's not a literal -- is it an ID? |
| 670 | error = spvTextEncodeOperand( |
| 671 | SPV_OPERAND_TYPE_OPTIONAL_ID, operandValue.c_str(), operandTable, |
| 672 | extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound, |
| 673 | position, pDiagnostic); |
| 674 | if (error) { |
| 675 | DIAGNOSTIC << "Invalid word following " << firstWord << ": " |
| 676 | << operandValue; |
| 677 | } |
| 678 | } |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 679 | if (error) return error; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 680 | *position = nextPosition; |
| 681 | } |
| 682 | return SPV_SUCCESS; |
| 683 | } |
| 684 | |
| 685 | } // anonymous namespace |
| 686 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 687 | spv_result_t spvTextEncodeOpcode( |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 688 | const spv_text text, spv_assembly_syntax_format_t format, |
| 689 | const spv_opcode_table opcodeTable, const spv_operand_table operandTable, |
| 690 | const spv_ext_inst_table extInstTable, spv_named_id_table namedIdTable, |
| 691 | uint32_t *pBound, spv_instruction_t *pInst, spv_position position, |
| 692 | spv_diagnostic *pDiagnostic) { |
Andrew Woloszyn | fabeeb8 | 2015-09-22 11:54:48 -0400 | [diff] [blame] | 693 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 694 | // Check for !<integer> first. |
| 695 | if ('!' == text->str[position->index]) { |
| 696 | return encodeInstructionStartingWithImmediate( |
| 697 | text, operandTable, extInstTable, namedIdTable, pBound, pInst, position, |
| 698 | pDiagnostic); |
| 699 | } |
| 700 | |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 701 | // An assembly instruction has two possible formats: |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 702 | // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void". |
| 703 | // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid". |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 704 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 705 | std::string firstWord; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 706 | spv_position_t nextPosition = {}; |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 707 | spv_result_t error = spvTextWordGet(text, position, firstWord, &nextPosition); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 708 | if (error) { |
| 709 | DIAGNOSTIC << "Internal Error"; |
| 710 | return error; |
| 711 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 712 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 713 | std::string opcodeName; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 714 | std::string result_id; |
| 715 | spv_position_t result_id_position = {}; |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 716 | if (spvStartsWithOp(text, position)) { |
| 717 | opcodeName = firstWord; |
| 718 | } else { |
| 719 | // If the first word of this instruction is not an opcode, we must be |
| 720 | // processing AAF now. |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 721 | if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) { |
| 722 | DIAGNOSTIC |
| 723 | << "Expected <opcode> at the beginning of an instruction, found '" |
| 724 | << firstWord << "'."; |
| 725 | return SPV_ERROR_INVALID_TEXT; |
| 726 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 727 | |
| 728 | result_id = firstWord; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 729 | if ('%' != result_id.front()) { |
| 730 | DIAGNOSTIC << "Expected <opcode> or <result-id> at the beginning " |
| 731 | "of an instruction, found '" |
| 732 | << result_id << "'."; |
| 733 | return SPV_ERROR_INVALID_TEXT; |
| 734 | } |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 735 | result_id_position = *position; |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 736 | |
| 737 | // The '=' sign. |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 738 | *position = nextPosition; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 739 | if (spvTextAdvance(text, position)) { |
| 740 | DIAGNOSTIC << "Expected '=', found end of stream."; |
| 741 | return SPV_ERROR_INVALID_TEXT; |
| 742 | } |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 743 | std::string equal_sign; |
| 744 | error = spvTextWordGet(text, position, equal_sign, &nextPosition); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 745 | if ("=" != equal_sign) { |
| 746 | DIAGNOSTIC << "'=' expected after result id."; |
| 747 | return SPV_ERROR_INVALID_TEXT; |
| 748 | } |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 749 | |
| 750 | // The <opcode> after the '=' sign. |
| 751 | *position = nextPosition; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 752 | if (spvTextAdvance(text, position)) { |
| 753 | DIAGNOSTIC << "Expected opcode, found end of stream."; |
| 754 | return SPV_ERROR_INVALID_TEXT; |
| 755 | } |
Lei Zhang | 977e9bc | 2015-08-21 11:52:03 -0400 | [diff] [blame] | 756 | error = spvTextWordGet(text, position, opcodeName, &nextPosition); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 757 | if (error) { |
| 758 | DIAGNOSTIC << "Internal Error"; |
| 759 | return error; |
| 760 | } |
| 761 | if (!spvStartsWithOp(text, position)) { |
| 762 | DIAGNOSTIC << "Invalid Opcode prefix '" << opcodeName << "'."; |
| 763 | return SPV_ERROR_INVALID_TEXT; |
| 764 | } |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 765 | } |
| 766 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 767 | // NOTE: The table contains Opcode names without the "Op" prefix. |
| 768 | const char *pInstName = opcodeName.data() + 2; |
| 769 | |
| 770 | spv_opcode_desc opcodeEntry; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 771 | error = spvOpcodeTableNameLookup(opcodeTable, pInstName, &opcodeEntry); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 772 | if (error) { |
| 773 | DIAGNOSTIC << "Invalid Opcode name '" |
| 774 | << spvGetWord(text->str + position->index) << "'"; |
| 775 | return error; |
| 776 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 777 | if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) { |
| 778 | // If this instruction has <result-id>, check it follows AAF. |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 779 | if (opcodeEntry->hasResult && result_id.empty()) { |
| 780 | DIAGNOSTIC << "Expected <result-id> at the beginning of an " |
| 781 | "instruction, found '" |
| 782 | << firstWord << "'."; |
| 783 | return SPV_ERROR_INVALID_TEXT; |
| 784 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 785 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 786 | pInst->opcode = opcodeEntry->opcode; |
| 787 | *position = nextPosition; |
| 788 | pInst->wordCount++; |
| 789 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 790 | // Maintains the ordered list of expected operand types. |
| 791 | // For many instructions we only need the {numTypes, operandTypes} |
| 792 | // entries in opcodeEntry. However, sometimes we need to modify |
| 793 | // the list as we parse the operands. This occurs when an operand |
| 794 | // has its own logical operands (such as the LocalSize operand for |
| 795 | // ExecutionMode), or for extended instructions that may have their |
| 796 | // own operands depending on the selected extended instruction. |
| 797 | spv_operand_pattern_t expectedOperands( |
| 798 | opcodeEntry->operandTypes, |
| 799 | opcodeEntry->operandTypes + opcodeEntry->numTypes); |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 800 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 801 | while (!expectedOperands.empty()) { |
| 802 | const spv_operand_type_t type = expectedOperands.front(); |
| 803 | expectedOperands.pop_front(); |
| 804 | |
| 805 | // Expand optional tuples lazily. |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 806 | if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 807 | |
| 808 | if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) { |
| 809 | // Handle the <result-id> for value generating instructions. |
| 810 | // We've already consumed it from the text stream. Here |
| 811 | // we inject its words into the instruction. |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 812 | error = spvTextEncodeOperand(SPV_OPERAND_TYPE_RESULT_ID, |
| 813 | result_id.c_str(), operandTable, |
| 814 | extInstTable, namedIdTable, pInst, nullptr, |
| 815 | pBound, &result_id_position, pDiagnostic); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 816 | if (error) return error; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 817 | } else { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 818 | // Find the next word. |
| 819 | error = spvTextAdvance(text, position); |
| 820 | if (error == SPV_END_OF_STREAM) { |
| 821 | if (spvOperandIsOptional(type)) { |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 822 | // This would have been the last potential operand for the |
| 823 | // instruction, |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 824 | // and we didn't find one. We're finished parsing this instruction. |
| 825 | break; |
| 826 | } else { |
| 827 | DIAGNOSTIC << "Expected operand, found end of stream."; |
| 828 | return SPV_ERROR_INVALID_TEXT; |
| 829 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 830 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 831 | assert(error == SPV_SUCCESS && "Somebody added another way to fail"); |
| 832 | |
| 833 | if (spvTextIsStartOfNewInst(text, position)) { |
| 834 | if (spvOperandIsOptional(type)) { |
| 835 | break; |
| 836 | } else { |
| 837 | DIAGNOSTIC << "Expected operand, found next instruction instead."; |
| 838 | return SPV_ERROR_INVALID_TEXT; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | std::string operandValue; |
| 843 | error = spvTextWordGet(text, position, operandValue, &nextPosition); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 844 | if (error) { |
| 845 | DIAGNOSTIC << "Internal Error"; |
| 846 | return error; |
| 847 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 848 | |
| 849 | error = spvTextEncodeOperand( |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 850 | type, operandValue.c_str(), operandTable, extInstTable, namedIdTable, |
| 851 | pInst, &expectedOperands, pBound, position, pDiagnostic); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 852 | |
| 853 | if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type)) |
| 854 | return SPV_SUCCESS; |
| 855 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 856 | if (error) return error; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 857 | |
| 858 | *position = nextPosition; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
| 862 | pInst->words[0] = spvOpcodeMake(pInst->wordCount, opcodeEntry->opcode); |
| 863 | |
| 864 | return SPV_SUCCESS; |
| 865 | } |
| 866 | |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 867 | namespace { |
| 868 | |
| 869 | // Translates a given assembly language module into binary form. |
| 870 | // If a diagnostic is generated, it is not yet marked as being |
| 871 | // for a text-based input. |
| 872 | spv_result_t spvTextToBinaryInternal(const spv_text text, |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 873 | spv_assembly_syntax_format_t format, |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 874 | const spv_opcode_table opcodeTable, |
| 875 | const spv_operand_table operandTable, |
| 876 | const spv_ext_inst_table extInstTable, |
| 877 | spv_binary *pBinary, |
| 878 | spv_diagnostic *pDiagnostic) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 879 | spv_position_t position = {}; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 880 | if (!text->str || !text->length) { |
| 881 | DIAGNOSTIC << "Text stream is empty."; |
| 882 | return SPV_ERROR_INVALID_TEXT; |
| 883 | } |
| 884 | if (!opcodeTable || !operandTable || !extInstTable) |
| 885 | return SPV_ERROR_INVALID_TABLE; |
| 886 | if (!pBinary) return SPV_ERROR_INVALID_POINTER; |
| 887 | if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 888 | |
| 889 | // NOTE: Ensure diagnostic is zero initialised |
| 890 | *pDiagnostic = {}; |
| 891 | |
| 892 | uint32_t bound = 1; |
| 893 | |
| 894 | std::vector<spv_instruction_t> instructions; |
| 895 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 896 | if (spvTextAdvance(text, &position)) { |
| 897 | DIAGNOSTIC << "Text stream is empty."; |
| 898 | return SPV_ERROR_INVALID_TEXT; |
| 899 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 900 | |
| 901 | spv_named_id_table namedIdTable = spvNamedIdTableCreate(); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 902 | if (!namedIdTable) return SPV_ERROR_OUT_OF_MEMORY; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 903 | |
| 904 | spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE; |
| 905 | while (text->length > position.index) { |
| 906 | spv_instruction_t inst = {}; |
| 907 | inst.extInstType = extInstType; |
| 908 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 909 | if (spvTextEncodeOpcode(text, format, opcodeTable, operandTable, |
| 910 | extInstTable, namedIdTable, &bound, &inst, |
| 911 | &position, pDiagnostic)) { |
| 912 | spvNamedIdTableDestory(namedIdTable); |
| 913 | return SPV_ERROR_INVALID_TEXT; |
| 914 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 915 | extInstType = inst.extInstType; |
| 916 | |
| 917 | instructions.push_back(inst); |
| 918 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 919 | if (spvTextAdvance(text, &position)) break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | spvNamedIdTableDestory(namedIdTable); |
| 923 | |
| 924 | size_t totalSize = SPV_INDEX_INSTRUCTION; |
| 925 | for (auto &inst : instructions) { |
| 926 | totalSize += inst.wordCount; |
| 927 | } |
| 928 | |
| 929 | uint32_t *data = new uint32_t[totalSize]; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 930 | if (!data) return SPV_ERROR_OUT_OF_MEMORY; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 931 | uint64_t currentIndex = SPV_INDEX_INSTRUCTION; |
| 932 | for (auto &inst : instructions) { |
| 933 | memcpy(data + currentIndex, inst.words, sizeof(uint32_t) * inst.wordCount); |
| 934 | currentIndex += inst.wordCount; |
| 935 | } |
| 936 | |
| 937 | spv_binary binary = new spv_binary_t(); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 938 | if (!binary) { |
| 939 | delete[] data; |
| 940 | return SPV_ERROR_OUT_OF_MEMORY; |
| 941 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 942 | binary->code = data; |
| 943 | binary->wordCount = totalSize; |
| 944 | |
| 945 | spv_result_t error = spvBinaryHeaderSet(binary, bound); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 946 | if (error) { |
| 947 | spvBinaryDestroy(binary); |
| 948 | return error; |
| 949 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 950 | |
| 951 | *pBinary = binary; |
| 952 | |
| 953 | return SPV_SUCCESS; |
| 954 | } |
| 955 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 956 | } // anonymous namespace |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 957 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 958 | spv_result_t spvTextToBinary(const char *input_text, |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 959 | const uint64_t input_text_size, |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 960 | const spv_opcode_table opcodeTable, |
| 961 | const spv_operand_table operandTable, |
| 962 | const spv_ext_inst_table extInstTable, |
| 963 | spv_binary *pBinary, spv_diagnostic *pDiagnostic) { |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 964 | return spvTextWithFormatToBinary( |
| 965 | input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT, |
| 966 | opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic); |
| 967 | } |
| 968 | |
| 969 | spv_result_t spvTextWithFormatToBinary( |
| 970 | const char *input_text, const uint64_t input_text_size, |
| 971 | spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable, |
| 972 | const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, |
| 973 | spv_binary *pBinary, spv_diagnostic *pDiagnostic) { |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 974 | spv_text_t text = {input_text, input_text_size}; |
| 975 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 976 | spv_result_t result = |
| 977 | spvTextToBinaryInternal(&text, format, opcodeTable, operandTable, |
| 978 | extInstTable, pBinary, pDiagnostic); |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 979 | if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true; |
| 980 | |
| 981 | return result; |
| 982 | } |
| 983 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 984 | void spvTextDestroy(spv_text text) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 985 | if (!text) return; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 986 | if (text->str) { |
| 987 | delete[] text->str; |
| 988 | } |
| 989 | delete text; |
| 990 | } |