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