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