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