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