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 | |
| 27 | #include <libspirv/libspirv.h> |
Lei Zhang | 610c525 | 2015-09-09 10:36:48 -0400 | [diff] [blame] | 28 | |
Lei Zhang | 4e092d3 | 2015-09-11 13:45:18 -0400 | [diff] [blame^] | 29 | #include "bitwisecast.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 30 | #include "binary.h" |
| 31 | #include "diagnostic.h" |
| 32 | #include "ext_inst.h" |
| 33 | #include "opcode.h" |
| 34 | #include "operand.h" |
| 35 | #include "text.h" |
| 36 | |
| 37 | #include <assert.h> |
| 38 | #include <stdio.h> |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 39 | #include <cstdlib> |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 40 | #include <string.h> |
| 41 | |
| 42 | #include <string> |
| 43 | #include <vector> |
| 44 | #include <unordered_map> |
| 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"); |
| 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) { |
| 156 | spvCheck(!text->str || !text->length, return SPV_ERROR_INVALID_TEXT); |
| 157 | spvCheck(!startPosition || !endPosition, return SPV_ERROR_INVALID_POINTER); |
| 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) { |
| 234 | spvCheck(!text->str || !text->length, return SPV_ERROR_INVALID_TEXT); |
| 235 | spvCheck(!startPosition || !endPosition, return SPV_ERROR_INVALID_POINTER); |
| 236 | |
| 237 | spvCheck('"' != text->str[startPosition->index], |
| 238 | return SPV_ERROR_INVALID_TEXT); |
| 239 | |
| 240 | *endPosition = *startPosition; |
| 241 | |
| 242 | // NOTE: Assumes first character is not white space |
| 243 | while (true) { |
| 244 | endPosition->column++; |
| 245 | endPosition->index++; |
| 246 | |
| 247 | switch (text->str[endPosition->index]) { |
| 248 | case '"': { |
| 249 | endPosition->column++; |
| 250 | endPosition->index++; |
| 251 | |
| 252 | string.assign(text->str + startPosition->index, |
| 253 | (size_t)(endPosition->index - startPosition->index)); |
| 254 | |
| 255 | return SPV_SUCCESS; |
| 256 | } |
| 257 | case '\n': |
| 258 | case '\0': |
| 259 | return SPV_ERROR_INVALID_TEXT; |
| 260 | default: |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | spv_result_t spvTextToUInt32(const char *textValue, uint32_t *pValue) { |
| 267 | char *endPtr = nullptr; |
| 268 | *pValue = strtoul(textValue, &endPtr, 0); |
| 269 | if (0 == *pValue && textValue == endPtr) { |
| 270 | return SPV_ERROR_INVALID_TEXT; |
| 271 | } |
| 272 | return SPV_SUCCESS; |
| 273 | } |
| 274 | |
| 275 | spv_result_t spvTextToLiteral(const char *textValue, spv_literal_t *pLiteral) { |
| 276 | bool isSigned = false; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 277 | int numPeriods = 0; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 278 | bool isString = false; |
| 279 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 280 | const size_t len = strlen(textValue); |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 281 | if (len == 0) return SPV_FAILED_MATCH; |
| 282 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 283 | for (uint64_t index = 0; index < len; ++index) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 284 | switch (textValue[index]) { |
| 285 | case '0': |
| 286 | case '1': |
| 287 | case '2': |
| 288 | case '3': |
| 289 | case '4': |
| 290 | case '5': |
| 291 | case '6': |
| 292 | case '7': |
| 293 | case '8': |
| 294 | case '9': |
| 295 | break; |
| 296 | case '.': |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 297 | numPeriods++; |
| 298 | break; |
| 299 | case '-': |
| 300 | if (index == 0) { |
| 301 | isSigned = true; |
| 302 | } else { |
| 303 | isString = true; |
| 304 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 305 | break; |
| 306 | default: |
| 307 | isString = true; |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 308 | index = len; // break out of the loop too. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 309 | break; |
| 310 | } |
| 311 | } |
| 312 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 313 | pLiteral->type = spv_literal_type_t(99); |
| 314 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 315 | if (isString || numPeriods > 1 || (isSigned && len == 1)) { |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 316 | // TODO(dneto): Allow escaping. |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 317 | if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"') |
| 318 | return SPV_FAILED_MATCH; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 319 | pLiteral->type = SPV_LITERAL_TYPE_STRING; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 320 | // Need room for the null-terminator. |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 321 | if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY; |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 322 | strncpy(pLiteral->value.str, textValue + 1, len - 2); |
| 323 | pLiteral->value.str[len - 2] = 0; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 324 | } else if (numPeriods == 1) { |
| 325 | double d = std::strtod(textValue, nullptr); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 326 | float f = (float)d; |
| 327 | if (d == (double)f) { |
| 328 | pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32; |
| 329 | pLiteral->value.f = f; |
| 330 | } else { |
| 331 | pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64; |
| 332 | pLiteral->value.d = d; |
| 333 | } |
| 334 | } else if (isSigned) { |
| 335 | int64_t i64 = strtoll(textValue, nullptr, 10); |
| 336 | int32_t i32 = (int32_t)i64; |
| 337 | if (i64 == (int64_t)i32) { |
| 338 | pLiteral->type = SPV_LITERAL_TYPE_INT_32; |
| 339 | pLiteral->value.i32 = i32; |
| 340 | } else { |
| 341 | pLiteral->type = SPV_LITERAL_TYPE_INT_64; |
| 342 | pLiteral->value.i64 = i64; |
| 343 | } |
| 344 | } else { |
| 345 | uint64_t u64 = strtoull(textValue, nullptr, 10); |
| 346 | uint32_t u32 = (uint32_t)u64; |
| 347 | if (u64 == (uint64_t)u32) { |
| 348 | pLiteral->type = SPV_LITERAL_TYPE_UINT_32; |
| 349 | pLiteral->value.u32 = u32; |
| 350 | } else { |
| 351 | pLiteral->type = SPV_LITERAL_TYPE_UINT_64; |
| 352 | pLiteral->value.u64 = u64; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return SPV_SUCCESS; |
| 357 | } |
| 358 | |
| 359 | spv_result_t spvTextEncodeOperand( |
| 360 | const spv_operand_type_t type, const char *textValue, |
| 361 | const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, |
| 362 | spv_named_id_table namedIdTable, spv_instruction_t *pInst, |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 363 | spv_operand_pattern_t *pExpectedOperands, uint32_t *pBound, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 364 | const spv_position position, spv_diagnostic *pDiagnostic) { |
| 365 | // NOTE: Handle immediate int in the stream |
| 366 | if ('!' == textValue[0]) { |
| 367 | const char *begin = textValue + 1; |
| 368 | char *end = nullptr; |
| 369 | uint32_t immediateInt = strtoul(begin, &end, 0); |
| 370 | size_t size = strlen(textValue); |
| 371 | size_t length = (end - begin); |
| 372 | spvCheck(size - 1 != length, DIAGNOSTIC << "Invalid immediate integer '" |
| 373 | << textValue << "'."; |
| 374 | return SPV_ERROR_INVALID_TEXT); |
| 375 | position->column += size; |
| 376 | position->index += size; |
| 377 | pInst->words[pInst->wordCount] = immediateInt; |
| 378 | pInst->wordCount += 1; |
| 379 | return SPV_SUCCESS; |
| 380 | } |
| 381 | |
| 382 | switch (type) { |
David Neto | e3f70b9 | 2015-08-27 13:50:05 -0400 | [diff] [blame] | 383 | case SPV_OPERAND_TYPE_ID: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 384 | case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE: |
| 385 | case SPV_OPERAND_TYPE_OPTIONAL_ID: |
David Neto | e3f70b9 | 2015-08-27 13:50:05 -0400 | [diff] [blame] | 386 | case SPV_OPERAND_TYPE_RESULT_ID: { |
Lei Zhang | abafd5e | 2015-08-21 11:52:29 -0400 | [diff] [blame] | 387 | if ('%' == textValue[0]) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 388 | textValue++; |
| 389 | } |
Lei Zhang | abafd5e | 2015-08-21 11:52:29 -0400 | [diff] [blame] | 390 | // TODO: Force all ID's to be prefixed with '%'. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 391 | uint32_t id = 0; |
| 392 | if (spvTextIsNamedId(textValue)) { |
| 393 | id = spvNamedIdAssignOrGet(namedIdTable, textValue, pBound); |
| 394 | } else { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 395 | if (spvTextToUInt32(textValue, &id) != SPV_SUCCESS) { |
| 396 | if (spvOperandIsOptional(type)) { |
| 397 | return SPV_FAILED_MATCH; |
| 398 | } else { |
| 399 | DIAGNOSTIC << "Invalid " |
| 400 | << ((type == SPV_OPERAND_TYPE_RESULT_ID) ? "result " |
| 401 | : "") |
| 402 | << "ID '" << textValue << "'."; |
| 403 | return SPV_ERROR_INVALID_TEXT; |
| 404 | } |
| 405 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 406 | } |
| 407 | pInst->words[pInst->wordCount++] = id; |
| 408 | if (*pBound <= id) { |
| 409 | *pBound = id + 1; |
| 410 | } |
| 411 | } break; |
| 412 | case SPV_OPERAND_TYPE_LITERAL_NUMBER: { |
| 413 | // NOTE: Special case for extension instruction lookup |
| 414 | if (OpExtInst == pInst->opcode) { |
| 415 | spv_ext_inst_desc extInst; |
| 416 | spvCheck(spvExtInstTableNameLookup(extInstTable, pInst->extInstType, |
| 417 | textValue, &extInst), |
| 418 | DIAGNOSTIC << "Invalid extended instruction name '" |
| 419 | << textValue << "'."; |
| 420 | return SPV_ERROR_INVALID_TEXT); |
| 421 | pInst->words[pInst->wordCount++] = extInst->ext_inst; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 422 | |
| 423 | // Prepare to parse the operands for the extended instructions. |
| 424 | spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands); |
| 425 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 426 | return SPV_SUCCESS; |
| 427 | } |
| 428 | |
| 429 | // TODO: Literal numbers can be any number up to 64 bits wide. This |
| 430 | // includes integers and floating point numbers. |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 431 | // TODO(dneto): Suggest using spvTextToLiteral and looking for an |
| 432 | // appropriate result type. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 433 | spvCheck(spvTextToUInt32(textValue, &pInst->words[pInst->wordCount++]), |
| 434 | DIAGNOSTIC << "Invalid literal number '" << textValue << "'."; |
| 435 | return SPV_ERROR_INVALID_TEXT); |
| 436 | } break; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 437 | case SPV_OPERAND_TYPE_LITERAL: |
| 438 | case SPV_OPERAND_TYPE_LITERAL_IN_OPTIONAL_TUPLE: |
| 439 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL: { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 440 | spv_literal_t literal = {}; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 441 | if (spvTextToLiteral(textValue, &literal) != SPV_SUCCESS) { |
| 442 | if (spvOperandIsOptional(type)) { |
| 443 | return SPV_FAILED_MATCH; |
| 444 | } else { |
| 445 | DIAGNOSTIC << "Invalid literal '" << textValue << "'."; |
| 446 | return SPV_ERROR_INVALID_TEXT; |
| 447 | } |
| 448 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 449 | switch (literal.type) { |
Andrew Woloszyn | 4b4acde | 2015-09-10 10:28:22 -0400 | [diff] [blame] | 450 | // We do not have to print diagnostics here because spvBinaryEncode* |
| 451 | // prints diagnostic messages on failure. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 452 | case SPV_LITERAL_TYPE_INT_32: |
Lei Zhang | 610c525 | 2015-09-09 10:36:48 -0400 | [diff] [blame] | 453 | spvCheck(spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32), |
| 454 | pInst, position, pDiagnostic), |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 455 | return SPV_ERROR_INVALID_TEXT); |
| 456 | break; |
| 457 | case SPV_LITERAL_TYPE_INT_64: { |
Lei Zhang | 610c525 | 2015-09-09 10:36:48 -0400 | [diff] [blame] | 458 | spvCheck(spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64), |
| 459 | pInst, position, pDiagnostic), |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 460 | return SPV_ERROR_INVALID_TEXT); |
| 461 | } break; |
| 462 | case SPV_LITERAL_TYPE_UINT_32: { |
| 463 | spvCheck(spvBinaryEncodeU32(literal.value.u32, pInst, position, |
| 464 | pDiagnostic), |
| 465 | return SPV_ERROR_INVALID_TEXT); |
| 466 | } break; |
| 467 | case SPV_LITERAL_TYPE_UINT_64: { |
Lei Zhang | 610c525 | 2015-09-09 10:36:48 -0400 | [diff] [blame] | 468 | spvCheck(spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64), |
| 469 | pInst, position, pDiagnostic), |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 470 | return SPV_ERROR_INVALID_TEXT); |
| 471 | } break; |
| 472 | case SPV_LITERAL_TYPE_FLOAT_32: { |
Lei Zhang | 610c525 | 2015-09-09 10:36:48 -0400 | [diff] [blame] | 473 | spvCheck(spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f), |
| 474 | pInst, position, pDiagnostic), |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 475 | return SPV_ERROR_INVALID_TEXT); |
| 476 | } break; |
| 477 | case SPV_LITERAL_TYPE_FLOAT_64: { |
Lei Zhang | 610c525 | 2015-09-09 10:36:48 -0400 | [diff] [blame] | 478 | spvCheck(spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d), |
| 479 | pInst, position, pDiagnostic), |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 480 | return SPV_ERROR_INVALID_TEXT); |
| 481 | } break; |
| 482 | case SPV_LITERAL_TYPE_STRING: { |
| 483 | spvCheck(spvBinaryEncodeString(literal.value.str, pInst, position, |
| 484 | pDiagnostic), |
| 485 | return SPV_ERROR_INVALID_TEXT); |
| 486 | } break; |
| 487 | default: |
| 488 | DIAGNOSTIC << "Invalid literal '" << textValue << "'"; |
| 489 | return SPV_ERROR_INVALID_TEXT; |
| 490 | } |
| 491 | } break; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 492 | case SPV_OPERAND_TYPE_LITERAL_STRING: |
| 493 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 494 | size_t len = strlen(textValue); |
| 495 | spvCheck('"' != textValue[0] && '"' != textValue[len - 1], |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 496 | if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 497 | DIAGNOSTIC << "Invalid literal string '" << textValue |
| 498 | << "', expected quotes."; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 499 | return SPV_ERROR_INVALID_TEXT;); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 500 | // NOTE: Strip quotes |
| 501 | std::string text(textValue + 1, len - 2); |
| 502 | |
| 503 | // NOTE: Special case for extended instruction library import |
| 504 | if (OpExtInstImport == pInst->opcode) { |
| 505 | pInst->extInstType = spvExtInstImportTypeGet(text.c_str()); |
| 506 | } |
| 507 | |
| 508 | spvCheck( |
| 509 | spvBinaryEncodeString(text.c_str(), pInst, position, pDiagnostic), |
| 510 | return SPV_ERROR_INVALID_TEXT); |
| 511 | } break; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 512 | case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
| 513 | assert(0 && " Handle optional optional image operands"); |
| 514 | break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 515 | default: { |
| 516 | // NOTE: All non literal operands are handled here using the operand |
| 517 | // table. |
| 518 | spv_operand_desc entry; |
| 519 | spvCheck(spvOperandTableNameLookup(operandTable, type, textValue, &entry), |
| 520 | DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" |
| 521 | << textValue << "'."; |
| 522 | return SPV_ERROR_INVALID_TEXT;); |
| 523 | spvCheck(spvBinaryEncodeU32(entry->value, pInst, position, pDiagnostic), |
| 524 | DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" |
| 525 | << textValue << "'."; |
| 526 | return SPV_ERROR_INVALID_TEXT;); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 527 | |
| 528 | // Prepare to parse the operands for this logical operand. |
| 529 | spvPrependOperandTypes(entry->operandTypes, pExpectedOperands); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 530 | } break; |
| 531 | } |
| 532 | return SPV_SUCCESS; |
| 533 | } |
| 534 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 535 | namespace { |
| 536 | |
| 537 | /// Encodes an instruction started by !<integer> at the given position in text. |
| 538 | /// |
| 539 | /// Puts the encoded words into *pInst. If successful, moves position past the |
| 540 | /// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and |
| 541 | /// leaves position pointing to the error in text. |
| 542 | spv_result_t encodeInstructionStartingWithImmediate( |
| 543 | const spv_text text, const spv_operand_table operandTable, |
| 544 | const spv_ext_inst_table extInstTable, spv_named_id_table namedIdTable, |
| 545 | uint32_t *pBound, spv_instruction_t *pInst, spv_position position, |
| 546 | spv_diagnostic *pDiagnostic) { |
| 547 | std::string firstWord; |
| 548 | spv_position_t nextPosition = {}; |
| 549 | auto error = spvTextWordGet(text, position, firstWord, &nextPosition); |
| 550 | spvCheck(error, DIAGNOSTIC << "Internal Error"; return error); |
| 551 | |
| 552 | assert(firstWord[0] == '!'); |
| 553 | const char *begin = firstWord.data() + 1; |
| 554 | char *end = nullptr; |
| 555 | uint32_t immediateInt = strtoul(begin, &end, 0); |
| 556 | spvCheck((begin + firstWord.size() - 1) != end, |
| 557 | DIAGNOSTIC << "Invalid immediate integer '" << firstWord << "'."; |
| 558 | return SPV_ERROR_INVALID_TEXT); |
| 559 | position->column += firstWord.size(); |
| 560 | position->index += firstWord.size(); |
| 561 | pInst->words[0] = immediateInt; |
| 562 | pInst->wordCount = 1; |
| 563 | while (spvTextAdvance(text, position) != SPV_END_OF_STREAM) { |
| 564 | // A beginning of a new instruction means we're done. |
| 565 | if (spvTextIsStartOfNewInst(text, position)) return SPV_SUCCESS; |
| 566 | |
| 567 | // Otherwise, there must be an operand that's either a literal, an ID, or |
| 568 | // an immediate. |
| 569 | std::string operandValue; |
| 570 | if ((error = spvTextWordGet(text, position, operandValue, &nextPosition))) { |
| 571 | DIAGNOSTIC << "Internal Error"; |
| 572 | return error; |
| 573 | } |
| 574 | |
| 575 | // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be |
| 576 | // expanded. |
| 577 | spv_operand_pattern_t dummyExpectedOperands; |
| 578 | error = spvTextEncodeOperand( |
| 579 | SPV_OPERAND_TYPE_OPTIONAL_LITERAL, operandValue.c_str(), operandTable, |
| 580 | extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound, |
| 581 | position, pDiagnostic); |
| 582 | if (error == SPV_FAILED_MATCH) { |
| 583 | // It's not a literal -- is it an ID? |
| 584 | error = spvTextEncodeOperand( |
| 585 | SPV_OPERAND_TYPE_OPTIONAL_ID, operandValue.c_str(), operandTable, |
| 586 | extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound, |
| 587 | position, pDiagnostic); |
| 588 | if (error) { |
| 589 | DIAGNOSTIC << "Invalid word following " << firstWord << ": " |
| 590 | << operandValue; |
| 591 | } |
| 592 | } |
| 593 | spvCheck(error, return error); |
| 594 | *position = nextPosition; |
| 595 | } |
| 596 | return SPV_SUCCESS; |
| 597 | } |
| 598 | |
| 599 | } // anonymous namespace |
| 600 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 601 | spv_result_t spvTextEncodeOpcode( |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 602 | const spv_text text, spv_assembly_syntax_format_t format, |
| 603 | const spv_opcode_table opcodeTable, const spv_operand_table operandTable, |
| 604 | const spv_ext_inst_table extInstTable, spv_named_id_table namedIdTable, |
| 605 | uint32_t *pBound, spv_instruction_t *pInst, spv_position position, |
| 606 | spv_diagnostic *pDiagnostic) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 607 | // Check for !<integer> first. |
| 608 | if ('!' == text->str[position->index]) { |
| 609 | return encodeInstructionStartingWithImmediate( |
| 610 | text, operandTable, extInstTable, namedIdTable, pBound, pInst, position, |
| 611 | pDiagnostic); |
| 612 | } |
| 613 | |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 614 | // An assembly instruction has two possible formats: |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 615 | // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void". |
| 616 | // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid". |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 617 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 618 | if ('!' == text->str[position->index]) { |
| 619 | return encodeInstructionStartingWithImmediate( |
| 620 | text, operandTable, extInstTable, namedIdTable, pBound, pInst, position, |
| 621 | pDiagnostic); |
| 622 | } |
| 623 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 624 | std::string firstWord; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 625 | spv_position_t nextPosition = {}; |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 626 | spv_result_t error = spvTextWordGet(text, position, firstWord, &nextPosition); |
Andrew Woloszyn | 4b4acde | 2015-09-10 10:28:22 -0400 | [diff] [blame] | 627 | spvCheck(error, DIAGNOSTIC << "Internal Error"; return error); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 628 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 629 | std::string opcodeName; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 630 | std::string result_id; |
| 631 | spv_position_t result_id_position = {}; |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 632 | if (spvStartsWithOp(text, position)) { |
| 633 | opcodeName = firstWord; |
| 634 | } else { |
| 635 | // If the first word of this instruction is not an opcode, we must be |
| 636 | // processing AAF now. |
| 637 | spvCheck( |
| 638 | SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format, |
| 639 | DIAGNOSTIC |
| 640 | << "Expected <opcode> at the beginning of an instruction, found '" |
| 641 | << firstWord << "'."; |
| 642 | return SPV_ERROR_INVALID_TEXT); |
| 643 | |
| 644 | result_id = firstWord; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 645 | spvCheck('%' != result_id.front(), |
| 646 | DIAGNOSTIC << "Expected <opcode> or <result-id> at the beginning " |
| 647 | "of an instruction, found '" |
| 648 | << result_id << "'."; |
| 649 | return SPV_ERROR_INVALID_TEXT); |
| 650 | result_id_position = *position; |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 651 | |
| 652 | // The '=' sign. |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 653 | *position = nextPosition; |
| 654 | spvCheck(spvTextAdvance(text, position), |
| 655 | DIAGNOSTIC << "Expected '=', found end of stream."; |
| 656 | return SPV_ERROR_INVALID_TEXT); |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 657 | std::string equal_sign; |
| 658 | error = spvTextWordGet(text, position, equal_sign, &nextPosition); |
| 659 | spvCheck("=" != equal_sign, DIAGNOSTIC << "'=' expected after result id."; |
| 660 | return SPV_ERROR_INVALID_TEXT); |
| 661 | |
| 662 | // The <opcode> after the '=' sign. |
| 663 | *position = nextPosition; |
| 664 | spvCheck(spvTextAdvance(text, position), |
| 665 | DIAGNOSTIC << "Expected opcode, found end of stream."; |
| 666 | return SPV_ERROR_INVALID_TEXT); |
Lei Zhang | 977e9bc | 2015-08-21 11:52:03 -0400 | [diff] [blame] | 667 | error = spvTextWordGet(text, position, opcodeName, &nextPosition); |
Andrew Woloszyn | 4b4acde | 2015-09-10 10:28:22 -0400 | [diff] [blame] | 668 | spvCheck(error, DIAGNOSTIC << "Internal Error"; return error); |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 669 | spvCheck(!spvStartsWithOp(text, position), |
| 670 | DIAGNOSTIC << "Invalid Opcode prefix '" << opcodeName << "'."; |
| 671 | return SPV_ERROR_INVALID_TEXT); |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 672 | } |
| 673 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 674 | // NOTE: The table contains Opcode names without the "Op" prefix. |
| 675 | const char *pInstName = opcodeName.data() + 2; |
| 676 | |
| 677 | spv_opcode_desc opcodeEntry; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 678 | error = spvOpcodeTableNameLookup(opcodeTable, pInstName, &opcodeEntry); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 679 | spvCheck(error, DIAGNOSTIC << "Invalid Opcode name '" |
David Neto | a48678a | 2015-09-11 12:04:03 -0400 | [diff] [blame] | 680 | << spvGetWord(text->str + position->index) << "'"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 681 | return error); |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 682 | if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) { |
| 683 | // If this instruction has <result-id>, check it follows AAF. |
| 684 | spvCheck(opcodeEntry->hasResult && result_id.empty(), |
| 685 | DIAGNOSTIC << "Expected <result-id> at the beginning of an " |
| 686 | "instruction, found '" |
| 687 | << firstWord << "'."; |
| 688 | return SPV_ERROR_INVALID_TEXT); |
| 689 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 690 | pInst->opcode = opcodeEntry->opcode; |
| 691 | *position = nextPosition; |
| 692 | pInst->wordCount++; |
| 693 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 694 | // Maintains the ordered list of expected operand types. |
| 695 | // For many instructions we only need the {numTypes, operandTypes} |
| 696 | // entries in opcodeEntry. However, sometimes we need to modify |
| 697 | // the list as we parse the operands. This occurs when an operand |
| 698 | // has its own logical operands (such as the LocalSize operand for |
| 699 | // ExecutionMode), or for extended instructions that may have their |
| 700 | // own operands depending on the selected extended instruction. |
| 701 | spv_operand_pattern_t expectedOperands( |
| 702 | opcodeEntry->operandTypes, |
| 703 | opcodeEntry->operandTypes + opcodeEntry->numTypes); |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 704 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 705 | while (!expectedOperands.empty()) { |
| 706 | const spv_operand_type_t type = expectedOperands.front(); |
| 707 | expectedOperands.pop_front(); |
| 708 | |
| 709 | // Expand optional tuples lazily. |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 710 | if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 711 | |
| 712 | if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) { |
| 713 | // Handle the <result-id> for value generating instructions. |
| 714 | // We've already consumed it from the text stream. Here |
| 715 | // we inject its words into the instruction. |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 716 | error = spvTextEncodeOperand(SPV_OPERAND_TYPE_RESULT_ID, |
| 717 | result_id.c_str(), operandTable, |
| 718 | extInstTable, namedIdTable, pInst, nullptr, |
| 719 | pBound, &result_id_position, pDiagnostic); |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 720 | spvCheck(error, return error); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 721 | } else { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 722 | // Find the next word. |
| 723 | error = spvTextAdvance(text, position); |
| 724 | if (error == SPV_END_OF_STREAM) { |
| 725 | if (spvOperandIsOptional(type)) { |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 726 | // This would have been the last potential operand for the |
| 727 | // instruction, |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 728 | // and we didn't find one. We're finished parsing this instruction. |
| 729 | break; |
| 730 | } else { |
| 731 | DIAGNOSTIC << "Expected operand, found end of stream."; |
| 732 | return SPV_ERROR_INVALID_TEXT; |
| 733 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 734 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 735 | assert(error == SPV_SUCCESS && "Somebody added another way to fail"); |
| 736 | |
| 737 | if (spvTextIsStartOfNewInst(text, position)) { |
| 738 | if (spvOperandIsOptional(type)) { |
| 739 | break; |
| 740 | } else { |
| 741 | DIAGNOSTIC << "Expected operand, found next instruction instead."; |
| 742 | return SPV_ERROR_INVALID_TEXT; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | std::string operandValue; |
| 747 | error = spvTextWordGet(text, position, operandValue, &nextPosition); |
Andrew Woloszyn | 4b4acde | 2015-09-10 10:28:22 -0400 | [diff] [blame] | 748 | spvCheck(error, DIAGNOSTIC << "Internal Error"; return error); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 749 | |
| 750 | error = spvTextEncodeOperand( |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 751 | type, operandValue.c_str(), operandTable, extInstTable, namedIdTable, |
| 752 | pInst, &expectedOperands, pBound, position, pDiagnostic); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 753 | |
| 754 | if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type)) |
| 755 | return SPV_SUCCESS; |
| 756 | |
| 757 | spvCheck(error, return error); |
| 758 | |
| 759 | *position = nextPosition; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | pInst->words[0] = spvOpcodeMake(pInst->wordCount, opcodeEntry->opcode); |
| 764 | |
| 765 | return SPV_SUCCESS; |
| 766 | } |
| 767 | |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 768 | namespace { |
| 769 | |
| 770 | // Translates a given assembly language module into binary form. |
| 771 | // If a diagnostic is generated, it is not yet marked as being |
| 772 | // for a text-based input. |
| 773 | spv_result_t spvTextToBinaryInternal(const spv_text text, |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 774 | spv_assembly_syntax_format_t format, |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 775 | const spv_opcode_table opcodeTable, |
| 776 | const spv_operand_table operandTable, |
| 777 | const spv_ext_inst_table extInstTable, |
| 778 | spv_binary *pBinary, |
| 779 | spv_diagnostic *pDiagnostic) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 780 | spv_position_t position = {}; |
| 781 | spvCheck(!text->str || !text->length, DIAGNOSTIC << "Text stream is empty."; |
| 782 | return SPV_ERROR_INVALID_TEXT); |
| 783 | spvCheck(!opcodeTable || !operandTable || !extInstTable, |
| 784 | return SPV_ERROR_INVALID_TABLE); |
| 785 | spvCheck(!pBinary, return SPV_ERROR_INVALID_POINTER); |
| 786 | spvCheck(!pDiagnostic, return SPV_ERROR_INVALID_DIAGNOSTIC); |
| 787 | |
| 788 | // NOTE: Ensure diagnostic is zero initialised |
| 789 | *pDiagnostic = {}; |
| 790 | |
| 791 | uint32_t bound = 1; |
| 792 | |
| 793 | std::vector<spv_instruction_t> instructions; |
| 794 | |
| 795 | spvCheck(spvTextAdvance(text, &position), DIAGNOSTIC |
| 796 | << "Text stream is empty."; |
| 797 | return SPV_ERROR_INVALID_TEXT); |
| 798 | |
| 799 | spv_named_id_table namedIdTable = spvNamedIdTableCreate(); |
| 800 | spvCheck(!namedIdTable, return SPV_ERROR_OUT_OF_MEMORY); |
| 801 | |
| 802 | spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE; |
| 803 | while (text->length > position.index) { |
| 804 | spv_instruction_t inst = {}; |
| 805 | inst.extInstType = extInstType; |
| 806 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 807 | spvCheck(spvTextEncodeOpcode(text, format, opcodeTable, operandTable, |
| 808 | extInstTable, namedIdTable, &bound, &inst, |
| 809 | &position, pDiagnostic), |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 810 | spvNamedIdTableDestory(namedIdTable); |
| 811 | return SPV_ERROR_INVALID_TEXT); |
| 812 | extInstType = inst.extInstType; |
| 813 | |
| 814 | instructions.push_back(inst); |
| 815 | |
| 816 | spvCheck(spvTextAdvance(text, &position), break); |
| 817 | } |
| 818 | |
| 819 | spvNamedIdTableDestory(namedIdTable); |
| 820 | |
| 821 | size_t totalSize = SPV_INDEX_INSTRUCTION; |
| 822 | for (auto &inst : instructions) { |
| 823 | totalSize += inst.wordCount; |
| 824 | } |
| 825 | |
| 826 | uint32_t *data = new uint32_t[totalSize]; |
| 827 | spvCheck(!data, return SPV_ERROR_OUT_OF_MEMORY); |
| 828 | uint64_t currentIndex = SPV_INDEX_INSTRUCTION; |
| 829 | for (auto &inst : instructions) { |
| 830 | memcpy(data + currentIndex, inst.words, sizeof(uint32_t) * inst.wordCount); |
| 831 | currentIndex += inst.wordCount; |
| 832 | } |
| 833 | |
| 834 | spv_binary binary = new spv_binary_t(); |
| 835 | spvCheck(!binary, delete[] data; return SPV_ERROR_OUT_OF_MEMORY); |
| 836 | binary->code = data; |
| 837 | binary->wordCount = totalSize; |
| 838 | |
| 839 | spv_result_t error = spvBinaryHeaderSet(binary, bound); |
| 840 | spvCheck(error, spvBinaryDestroy(binary); return error); |
| 841 | |
| 842 | *pBinary = binary; |
| 843 | |
| 844 | return SPV_SUCCESS; |
| 845 | } |
| 846 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 847 | } // anonymous namespace |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 848 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 849 | spv_result_t spvTextToBinary(const char *input_text, |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 850 | const uint64_t input_text_size, |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 851 | const spv_opcode_table opcodeTable, |
| 852 | const spv_operand_table operandTable, |
| 853 | const spv_ext_inst_table extInstTable, |
| 854 | spv_binary *pBinary, spv_diagnostic *pDiagnostic) { |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 855 | return spvTextWithFormatToBinary( |
| 856 | input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT, |
| 857 | opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic); |
| 858 | } |
| 859 | |
| 860 | spv_result_t spvTextWithFormatToBinary( |
| 861 | const char *input_text, const uint64_t input_text_size, |
| 862 | spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable, |
| 863 | const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, |
| 864 | spv_binary *pBinary, spv_diagnostic *pDiagnostic) { |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 865 | spv_text_t text = {input_text, input_text_size}; |
| 866 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 867 | spv_result_t result = |
| 868 | spvTextToBinaryInternal(&text, format, opcodeTable, operandTable, |
| 869 | extInstTable, pBinary, pDiagnostic); |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 870 | if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true; |
| 871 | |
| 872 | return result; |
| 873 | } |
| 874 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 875 | void spvTextDestroy(spv_text text) { |
| 876 | spvCheck(!text, return ); |
| 877 | if (text->str) { |
| 878 | delete[] text->str; |
| 879 | } |
| 880 | delete text; |
| 881 | } |