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