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 | |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 27 | #include "text.h" |
| 28 | |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <cassert> |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 31 | #include <cctype> |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 32 | #include <cstdio> |
| 33 | #include <cstdlib> |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 34 | #include <cstring> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 35 | #include <memory> |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 36 | #include <string> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 37 | #include <sstream> |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 38 | #include <unordered_map> |
| 39 | #include <vector> |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 40 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 41 | #include "binary.h" |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 42 | #include "bitwisecast.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 43 | #include "diagnostic.h" |
| 44 | #include "ext_inst.h" |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 45 | #include "instruction.h" |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 46 | #include <libspirv/libspirv.h> |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 47 | #include "opcode.h" |
| 48 | #include "operand.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 49 | #include "text_handler.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 50 | |
Lei Zhang | 610c525 | 2015-09-09 10:36:48 -0400 | [diff] [blame] | 51 | using spvutils::BitwiseCast; |
| 52 | |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 53 | bool spvIsValidIDCharacter(const char value) { |
| 54 | return value == '_' || 0 != ::isalnum(value); |
| 55 | } |
| 56 | |
| 57 | // Returns true if the given string represents a valid ID name. |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 58 | bool spvIsValidID(const char* textValue) { |
| 59 | const char* c = textValue; |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 60 | for (; *c != '\0'; ++c) { |
| 61 | if (!spvIsValidIDCharacter(*c)) { |
| 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | // If the string was empty, then the ID also is not valid. |
| 66 | return c != textValue; |
| 67 | } |
| 68 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 69 | // Text API |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 70 | |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 71 | spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 72 | bool isSigned = false; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 73 | int numPeriods = 0; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 74 | bool isString = false; |
| 75 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 76 | const size_t len = strlen(textValue); |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 77 | if (len == 0) return SPV_FAILED_MATCH; |
| 78 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 79 | for (uint64_t index = 0; index < len; ++index) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 80 | switch (textValue[index]) { |
| 81 | case '0': |
| 82 | case '1': |
| 83 | case '2': |
| 84 | case '3': |
| 85 | case '4': |
| 86 | case '5': |
| 87 | case '6': |
| 88 | case '7': |
| 89 | case '8': |
| 90 | case '9': |
| 91 | break; |
| 92 | case '.': |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 93 | numPeriods++; |
| 94 | break; |
| 95 | case '-': |
| 96 | if (index == 0) { |
| 97 | isSigned = true; |
| 98 | } else { |
| 99 | isString = true; |
| 100 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 101 | break; |
| 102 | default: |
| 103 | isString = true; |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 104 | index = len; // break out of the loop too. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 109 | pLiteral->type = spv_literal_type_t(99); |
| 110 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 111 | if (isString || numPeriods > 1 || (isSigned && len == 1)) { |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 112 | // TODO(dneto): Allow escaping. |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 113 | if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"') |
| 114 | return SPV_FAILED_MATCH; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 115 | pLiteral->type = SPV_LITERAL_TYPE_STRING; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 116 | // Need room for the null-terminator. |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 117 | if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY; |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 118 | strncpy(pLiteral->value.str, textValue + 1, len - 2); |
| 119 | pLiteral->value.str[len - 2] = 0; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 120 | } else if (numPeriods == 1) { |
| 121 | double d = std::strtod(textValue, nullptr); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 122 | float f = (float)d; |
| 123 | if (d == (double)f) { |
| 124 | pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32; |
| 125 | pLiteral->value.f = f; |
| 126 | } else { |
| 127 | pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64; |
| 128 | pLiteral->value.d = d; |
| 129 | } |
| 130 | } else if (isSigned) { |
| 131 | int64_t i64 = strtoll(textValue, nullptr, 10); |
| 132 | int32_t i32 = (int32_t)i64; |
| 133 | if (i64 == (int64_t)i32) { |
| 134 | pLiteral->type = SPV_LITERAL_TYPE_INT_32; |
| 135 | pLiteral->value.i32 = i32; |
| 136 | } else { |
| 137 | pLiteral->type = SPV_LITERAL_TYPE_INT_64; |
| 138 | pLiteral->value.i64 = i64; |
| 139 | } |
| 140 | } else { |
| 141 | uint64_t u64 = strtoull(textValue, nullptr, 10); |
| 142 | uint32_t u32 = (uint32_t)u64; |
| 143 | if (u64 == (uint64_t)u32) { |
| 144 | pLiteral->type = SPV_LITERAL_TYPE_UINT_32; |
| 145 | pLiteral->value.u32 = u32; |
| 146 | } else { |
| 147 | pLiteral->type = SPV_LITERAL_TYPE_UINT_64; |
| 148 | pLiteral->value.u64 = u64; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return SPV_SUCCESS; |
| 153 | } |
| 154 | |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 155 | namespace { |
| 156 | |
| 157 | /// Parses an immediate integer from text, guarding against overflow. If |
| 158 | /// successful, adds the parsed value to pInst, advances the context past it, |
| 159 | /// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics, |
| 160 | /// and returns SPV_ERROR_INVALID_TEXT. |
| 161 | spv_result_t encodeImmediate(libspirv::AssemblyContext* context, |
| 162 | const char* text, spv_instruction_t* pInst) { |
| 163 | assert(*text == '!'); |
| 164 | const char* begin = text + 1; |
| 165 | char* end = nullptr; |
Dejan Mircevski | 97e2c8f | 2015-09-29 17:58:37 -0400 | [diff] [blame] | 166 | const uint64_t parseResult = strtoull(begin, &end, 0); |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 167 | size_t length = end - begin; |
| 168 | if (length != strlen(begin)) { |
| 169 | context->diagnostic() << "Invalid immediate integer '" << text << "'."; |
| 170 | return SPV_ERROR_INVALID_TEXT; |
Dejan Mircevski | 97e2c8f | 2015-09-29 17:58:37 -0400 | [diff] [blame] | 171 | } else if (length > 10 || (parseResult >> 32) != 0) { |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 172 | context->diagnostic() << "Immediate integer '" << text |
Dejan Mircevski | 114206e | 2015-09-30 09:49:00 -0400 | [diff] [blame] | 173 | << "' is outside the unsigned 32-bit range."; |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 174 | return SPV_ERROR_INVALID_TEXT; |
| 175 | } |
| 176 | context->binaryEncodeU32(parseResult, pInst); |
| 177 | context->seekForward(strlen(text)); |
| 178 | return SPV_SUCCESS; |
| 179 | } |
| 180 | |
| 181 | } // anonymous namespace |
| 182 | |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 183 | /// @brief Translate an Opcode operand to binary form |
| 184 | /// |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 185 | /// @param[in] grammar the grammar to use for compilation |
| 186 | /// @param[in, out] context the dynamic compilation info |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 187 | /// @param[in] type of the operand |
| 188 | /// @param[in] textValue word of text to be parsed |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 189 | /// @param[out] pInst return binary Opcode |
| 190 | /// @param[in,out] pExpectedOperands the operand types expected |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 191 | /// |
| 192 | /// @return result code |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 193 | spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 194 | libspirv::AssemblyContext* context, |
| 195 | const spv_operand_type_t type, |
| 196 | const char* textValue, |
| 197 | spv_instruction_t* pInst, |
| 198 | spv_operand_pattern_t* pExpectedOperands) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 199 | // NOTE: Handle immediate int in the stream |
| 200 | if ('!' == textValue[0]) { |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 201 | if (auto error = encodeImmediate(context, textValue, pInst)) { |
| 202 | return error; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 203 | } |
Dejan Mircevski | 897bff9 | 2015-09-29 10:38:18 -0400 | [diff] [blame] | 204 | *pExpectedOperands = |
| 205 | spvAlternatePatternFollowingImmediate(*pExpectedOperands); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 206 | return SPV_SUCCESS; |
| 207 | } |
| 208 | |
| 209 | switch (type) { |
David Neto | b14a727 | 2015-09-25 13:56:09 -0400 | [diff] [blame] | 210 | case SPV_OPERAND_TYPE_EXECUTION_SCOPE: |
David Neto | e3f70b9 | 2015-08-27 13:50:05 -0400 | [diff] [blame] | 211 | case SPV_OPERAND_TYPE_ID: |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 212 | case SPV_OPERAND_TYPE_TYPE_ID: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 213 | case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE: |
| 214 | case SPV_OPERAND_TYPE_OPTIONAL_ID: |
David Neto | b14a727 | 2015-09-25 13:56:09 -0400 | [diff] [blame] | 215 | case SPV_OPERAND_TYPE_MEMORY_SEMANTICS: |
| 216 | case SPV_OPERAND_TYPE_RESULT_ID: { |
Lei Zhang | abafd5e | 2015-08-21 11:52:29 -0400 | [diff] [blame] | 217 | if ('%' == textValue[0]) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 218 | textValue++; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 219 | } else { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 220 | context->diagnostic() << "Expected id to start with %."; |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 221 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 222 | } |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 223 | if (!spvIsValidID(textValue)) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 224 | context->diagnostic() << "Invalid ID " << textValue; |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 225 | return SPV_ERROR_INVALID_TEXT; |
| 226 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 227 | const uint32_t id = context->spvNamedIdAssignOrGet(textValue); |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 228 | if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 229 | spvInstructionAddWord(pInst, id); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 230 | } break; |
| 231 | case SPV_OPERAND_TYPE_LITERAL_NUMBER: { |
| 232 | // NOTE: Special case for extension instruction lookup |
| 233 | if (OpExtInst == pInst->opcode) { |
| 234 | spv_ext_inst_desc extInst; |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 235 | if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 236 | context->diagnostic() << "Invalid extended instruction name '" |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 237 | << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 238 | return SPV_ERROR_INVALID_TEXT; |
| 239 | } |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 240 | spvInstructionAddWord(pInst, extInst->ext_inst); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 241 | |
| 242 | // Prepare to parse the operands for the extended instructions. |
| 243 | spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands); |
| 244 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 245 | return SPV_SUCCESS; |
| 246 | } |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 247 | } // Fall through for the general case. |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 248 | case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER: |
David Neto | 561dc4e | 2015-09-25 14:23:29 -0400 | [diff] [blame] | 249 | case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE: |
| 250 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 251 | spv_literal_t literal = {}; |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 252 | spv_result_t error = spvTextToLiteral(textValue, &literal); |
| 253 | if (error != SPV_SUCCESS) { |
| 254 | if (error == SPV_ERROR_OUT_OF_MEMORY) return error; |
| 255 | if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH; |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 256 | context->diagnostic() << "Invalid literal number '" << textValue |
| 257 | << "'."; |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 258 | return SPV_ERROR_INVALID_TEXT; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 259 | } |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 260 | |
| 261 | // The encoding for OpConstant, OpSpecConstant and OpSwitch all |
| 262 | // depend on either their own result-id or the result-id of |
| 263 | // one of their parameters. |
| 264 | if (OpConstant == pInst->opcode || OpSpecConstant == pInst->opcode) { |
| 265 | // Special cases for encoding possibly non-32-bit literals here. |
| 266 | libspirv::IdType type = |
| 267 | context->getTypeOfTypeGeneratingValue(pInst->resultTypeId); |
| 268 | if (!libspirv::isScalarFloating(type) && |
| 269 | !libspirv::isScalarIntegral(type)) { |
| 270 | spv_opcode_desc d; |
| 271 | const char* opcode_name = "opcode"; |
| 272 | if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) { |
| 273 | opcode_name = d->name; |
| 274 | } |
| 275 | context->diagnostic() |
| 276 | << "Type for " << opcode_name |
| 277 | << " must be a scalar floating point or integer type"; |
| 278 | return SPV_ERROR_INVALID_TEXT; |
| 279 | } |
| 280 | } else if (pInst->opcode == OpSwitch) { |
| 281 | // We need to know the value of the selector. |
| 282 | libspirv::IdType type = |
| 283 | context->getTypeOfValueInstruction(pInst->words[1]); |
| 284 | if (type.type_class != libspirv::IdTypeClass::kScalarIntegerType) { |
| 285 | context->diagnostic() |
| 286 | << "The selector operand for OpSwitch must be the result" |
| 287 | " of an instruction that generates an integer scalar"; |
| 288 | return SPV_ERROR_INVALID_TEXT; |
| 289 | } |
| 290 | } |
| 291 | // TODO(awoloszyn): Generate the correct assembly for arbitrary |
| 292 | // bitwidths here instead of falling though. |
| 293 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 294 | switch (literal.type) { |
Andrew Woloszyn | 4b4acde | 2015-09-10 10:28:22 -0400 | [diff] [blame] | 295 | // We do not have to print diagnostics here because spvBinaryEncode* |
| 296 | // prints diagnostic messages on failure. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 297 | case SPV_LITERAL_TYPE_INT_32: |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 298 | if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32), |
| 299 | pInst)) |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 300 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 301 | break; |
| 302 | case SPV_LITERAL_TYPE_INT_64: { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 303 | if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64), |
| 304 | pInst)) |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 305 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 306 | } break; |
| 307 | case SPV_LITERAL_TYPE_UINT_32: { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 308 | if (context->binaryEncodeU32(literal.value.u32, pInst)) |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 309 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 310 | } break; |
| 311 | case SPV_LITERAL_TYPE_UINT_64: { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 312 | if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64), |
| 313 | pInst)) |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 314 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 315 | } break; |
| 316 | case SPV_LITERAL_TYPE_FLOAT_32: { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 317 | if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f), |
| 318 | pInst)) |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 319 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 320 | } break; |
| 321 | case SPV_LITERAL_TYPE_FLOAT_64: { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 322 | if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d), |
| 323 | pInst)) |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 324 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 325 | } break; |
| 326 | case SPV_LITERAL_TYPE_STRING: { |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 327 | context->diagnostic() |
| 328 | << "Expected literal number, found literal string '" << textValue |
| 329 | << "'."; |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 330 | return SPV_FAILED_MATCH; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 331 | } break; |
| 332 | default: |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 333 | context->diagnostic() << "Invalid literal number '" << textValue |
| 334 | << "'"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 335 | return SPV_ERROR_INVALID_TEXT; |
| 336 | } |
| 337 | } break; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 338 | case SPV_OPERAND_TYPE_LITERAL_STRING: |
| 339 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: { |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 340 | spv_literal_t literal = {}; |
| 341 | spv_result_t error = spvTextToLiteral(textValue, &literal); |
| 342 | if (error != SPV_SUCCESS) { |
| 343 | if (error == SPV_ERROR_OUT_OF_MEMORY) return error; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 344 | if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH; |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 345 | context->diagnostic() << "Invalid literal string '" << textValue |
| 346 | << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 347 | return SPV_ERROR_INVALID_TEXT; |
| 348 | } |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 349 | if (literal.type != SPV_LITERAL_TYPE_STRING) { |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 350 | context->diagnostic() |
| 351 | << "Expected literal string, found literal number '" << textValue |
| 352 | << "'."; |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 353 | return SPV_FAILED_MATCH; |
| 354 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 355 | |
| 356 | // NOTE: Special case for extended instruction library import |
| 357 | if (OpExtInstImport == pInst->opcode) { |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 358 | pInst->extInstType = spvExtInstImportTypeGet(literal.value.str); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 361 | if (context->binaryEncodeString(literal.value.str, pInst)) |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 362 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 363 | } break; |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 364 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
| 365 | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
| 366 | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
David Neto | ee1b3bb | 2015-09-18 11:19:18 -0400 | [diff] [blame] | 367 | case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 368 | case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 369 | case SPV_OPERAND_TYPE_SELECTION_CONTROL: { |
| 370 | uint32_t value; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 371 | if (grammar.parseMaskOperand(type, textValue, &value)) { |
| 372 | context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '" |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 373 | << textValue << "'."; |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 374 | return SPV_ERROR_INVALID_TEXT; |
| 375 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 376 | if (auto error = context->binaryEncodeU32(value, pInst)) return error; |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 377 | // Prepare to parse the operands for this logical operand. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 378 | grammar.prependOperandTypesForMask(type, value, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 379 | } break; |
| 380 | case SPV_OPERAND_TYPE_OPTIONAL_CIV: { |
| 381 | auto error = spvTextEncodeOperand( |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 382 | grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue, |
| 383 | pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 384 | if (error == SPV_FAILED_MATCH) { |
| 385 | // It's not a literal number -- is it a literal string? |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 386 | error = spvTextEncodeOperand(grammar, context, |
| 387 | SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING, |
| 388 | textValue, pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 389 | } |
| 390 | if (error == SPV_FAILED_MATCH) { |
| 391 | // It's not a literal -- is it an ID? |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 392 | error = |
| 393 | spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID, |
| 394 | textValue, pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 395 | } |
| 396 | if (error) { |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 397 | context->diagnostic() << "Invalid word following !<integer>: " |
| 398 | << textValue; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 399 | return error; |
| 400 | } |
| 401 | if (pExpectedOperands->empty()) { |
| 402 | pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV); |
| 403 | } |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 404 | } break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 405 | default: { |
| 406 | // NOTE: All non literal operands are handled here using the operand |
| 407 | // table. |
| 408 | spv_operand_desc entry; |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 409 | if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 410 | context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '" |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 411 | << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 412 | return SPV_ERROR_INVALID_TEXT; |
| 413 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 414 | if (context->binaryEncodeU32(entry->value, pInst)) { |
| 415 | context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '" |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 416 | << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 417 | return SPV_ERROR_INVALID_TEXT; |
| 418 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 419 | |
| 420 | // Prepare to parse the operands for this logical operand. |
| 421 | spvPrependOperandTypes(entry->operandTypes, pExpectedOperands); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 422 | } break; |
| 423 | } |
| 424 | return SPV_SUCCESS; |
| 425 | } |
| 426 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 427 | namespace { |
| 428 | |
| 429 | /// Encodes an instruction started by !<integer> at the given position in text. |
| 430 | /// |
| 431 | /// Puts the encoded words into *pInst. If successful, moves position past the |
| 432 | /// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and |
| 433 | /// leaves position pointing to the error in text. |
| 434 | spv_result_t encodeInstructionStartingWithImmediate( |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 435 | const libspirv::AssemblyGrammar& grammar, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 436 | libspirv::AssemblyContext* context, spv_instruction_t* pInst) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 437 | std::string firstWord; |
| 438 | spv_position_t nextPosition = {}; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 439 | auto error = context->getWord(firstWord, &nextPosition); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 440 | if (error) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 441 | context->diagnostic() << "Internal Error"; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 442 | return error; |
| 443 | } |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 444 | |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 445 | if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) { |
| 446 | return error; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 447 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 448 | while (context->advance() != SPV_END_OF_STREAM) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 449 | // A beginning of a new instruction means we're done. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 450 | if (context->isStartOfNewInst()) return SPV_SUCCESS; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 451 | |
| 452 | // Otherwise, there must be an operand that's either a literal, an ID, or |
| 453 | // an immediate. |
| 454 | std::string operandValue; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 455 | if ((error = context->getWord(operandValue, &nextPosition))) { |
| 456 | context->diagnostic() << "Internal Error"; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 457 | return error; |
| 458 | } |
| 459 | |
Dejan Mircevski | e3a19c0 | 2015-09-11 15:03:54 -0400 | [diff] [blame] | 460 | if (operandValue == "=") { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 461 | context->diagnostic() << firstWord << " not allowed before =."; |
Dejan Mircevski | e3a19c0 | 2015-09-11 15:03:54 -0400 | [diff] [blame] | 462 | return SPV_ERROR_INVALID_TEXT; |
| 463 | } |
| 464 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 465 | // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be |
| 466 | // expanded. |
| 467 | spv_operand_pattern_t dummyExpectedOperands; |
| 468 | error = spvTextEncodeOperand( |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 469 | grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(), |
| 470 | pInst, &dummyExpectedOperands); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 471 | if (error) return error; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 472 | context->setPosition(nextPosition); |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 473 | } |
| 474 | return SPV_SUCCESS; |
| 475 | } |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 476 | } // anonymous namespace |
| 477 | |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 478 | /// @brief Translate single Opcode and operands to binary form |
| 479 | /// |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 480 | /// @param[in] grammar the grammar to use for compilation |
| 481 | /// @param[in, out] context the dynamic compilation info |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 482 | /// @param[in] text stream to translate |
| 483 | /// @param[in] format the assembly syntax format of text |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 484 | /// @param[out] pInst returned binary Opcode |
| 485 | /// @param[in,out] pPosition in the text stream |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 486 | /// |
| 487 | /// @return result code |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 488 | spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 489 | libspirv::AssemblyContext* context, |
| 490 | spv_assembly_syntax_format_t format, |
| 491 | spv_instruction_t* pInst) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 492 | // Check for !<integer> first. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 493 | if ('!' == context->peek()) { |
| 494 | return encodeInstructionStartingWithImmediate(grammar, context, pInst); |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 495 | } |
| 496 | |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 497 | // An assembly instruction has two possible formats: |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 498 | // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void". |
| 499 | // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid". |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 500 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 501 | std::string firstWord; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 502 | spv_position_t nextPosition = {}; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 503 | spv_result_t error = context->getWord(firstWord, &nextPosition); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 504 | if (error) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 505 | context->diagnostic() << "Internal Error"; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 506 | return error; |
| 507 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 508 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 509 | std::string opcodeName; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 510 | std::string result_id; |
| 511 | spv_position_t result_id_position = {}; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 512 | if (context->startsWithOp()) { |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 513 | opcodeName = firstWord; |
| 514 | } else { |
| 515 | // If the first word of this instruction is not an opcode, we must be |
| 516 | // processing AAF now. |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 517 | if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 518 | context->diagnostic() |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 519 | << "Expected <opcode> at the beginning of an instruction, found '" |
| 520 | << firstWord << "'."; |
| 521 | return SPV_ERROR_INVALID_TEXT; |
| 522 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 523 | |
| 524 | result_id = firstWord; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 525 | if ('%' != result_id.front()) { |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 526 | context->diagnostic() |
| 527 | << "Expected <opcode> or <result-id> at the beginning " |
| 528 | "of an instruction, found '" |
| 529 | << result_id << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 530 | return SPV_ERROR_INVALID_TEXT; |
| 531 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 532 | result_id_position = context->position(); |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 533 | |
| 534 | // The '=' sign. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 535 | context->setPosition(nextPosition); |
| 536 | if (context->advance()) { |
| 537 | context->diagnostic() << "Expected '=', found end of stream."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 538 | return SPV_ERROR_INVALID_TEXT; |
| 539 | } |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 540 | std::string equal_sign; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 541 | error = context->getWord(equal_sign, &nextPosition); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 542 | if ("=" != equal_sign) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 543 | context->diagnostic() << "'=' expected after result id."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 544 | return SPV_ERROR_INVALID_TEXT; |
| 545 | } |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 546 | |
| 547 | // The <opcode> after the '=' sign. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 548 | context->setPosition(nextPosition); |
| 549 | if (context->advance()) { |
| 550 | context->diagnostic() << "Expected opcode, found end of stream."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 551 | return SPV_ERROR_INVALID_TEXT; |
| 552 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 553 | error = context->getWord(opcodeName, &nextPosition); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 554 | if (error) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 555 | context->diagnostic() << "Internal Error"; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 556 | return error; |
| 557 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 558 | if (!context->startsWithOp()) { |
| 559 | context->diagnostic() << "Invalid Opcode prefix '" << opcodeName << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 560 | return SPV_ERROR_INVALID_TEXT; |
| 561 | } |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 562 | } |
| 563 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 564 | // NOTE: The table contains Opcode names without the "Op" prefix. |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 565 | const char* pInstName = opcodeName.data() + 2; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 566 | |
| 567 | spv_opcode_desc opcodeEntry; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 568 | error = grammar.lookupOpcode(pInstName, &opcodeEntry); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 569 | if (error) { |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 570 | context->diagnostic() << "Invalid Opcode name '" << context->getWord() |
| 571 | << "'"; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 572 | return error; |
| 573 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 574 | if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) { |
| 575 | // If this instruction has <result-id>, check it follows AAF. |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 576 | if (opcodeEntry->hasResult && result_id.empty()) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 577 | context->diagnostic() << "Expected <result-id> at the beginning of an " |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 578 | "instruction, found '" |
| 579 | << firstWord << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 580 | return SPV_ERROR_INVALID_TEXT; |
| 581 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 582 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 583 | pInst->opcode = opcodeEntry->opcode; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 584 | context->setPosition(nextPosition); |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 585 | // Reserve the first word for the instruction. |
| 586 | spvInstructionAddWord(pInst, 0); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 587 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 588 | // Maintains the ordered list of expected operand types. |
| 589 | // For many instructions we only need the {numTypes, operandTypes} |
| 590 | // entries in opcodeEntry. However, sometimes we need to modify |
| 591 | // the list as we parse the operands. This occurs when an operand |
| 592 | // has its own logical operands (such as the LocalSize operand for |
| 593 | // ExecutionMode), or for extended instructions that may have their |
| 594 | // own operands depending on the selected extended instruction. |
| 595 | spv_operand_pattern_t expectedOperands( |
| 596 | opcodeEntry->operandTypes, |
| 597 | opcodeEntry->operandTypes + opcodeEntry->numTypes); |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 598 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 599 | while (!expectedOperands.empty()) { |
| 600 | const spv_operand_type_t type = expectedOperands.front(); |
| 601 | expectedOperands.pop_front(); |
| 602 | |
| 603 | // Expand optional tuples lazily. |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 604 | if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 605 | |
| 606 | if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) { |
| 607 | // Handle the <result-id> for value generating instructions. |
| 608 | // We've already consumed it from the text stream. Here |
| 609 | // we inject its words into the instruction. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 610 | spv_position_t temp_pos = context->position(); |
| 611 | error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID, |
| 612 | result_id.c_str(), pInst, nullptr); |
| 613 | result_id_position = context->position(); |
| 614 | // Because we are injecting we have to reset the position afterwards. |
| 615 | context->setPosition(temp_pos); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 616 | if (error) return error; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 617 | } else { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 618 | // Find the next word. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 619 | error = context->advance(); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 620 | if (error == SPV_END_OF_STREAM) { |
| 621 | if (spvOperandIsOptional(type)) { |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 622 | // This would have been the last potential operand for the |
| 623 | // instruction, |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 624 | // and we didn't find one. We're finished parsing this instruction. |
| 625 | break; |
| 626 | } else { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 627 | context->diagnostic() << "Expected operand, found end of stream."; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 628 | return SPV_ERROR_INVALID_TEXT; |
| 629 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 630 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 631 | assert(error == SPV_SUCCESS && "Somebody added another way to fail"); |
| 632 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 633 | if (context->isStartOfNewInst()) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 634 | if (spvOperandIsOptional(type)) { |
| 635 | break; |
| 636 | } else { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 637 | context->diagnostic() |
| 638 | << "Expected operand, found next instruction instead."; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 639 | return SPV_ERROR_INVALID_TEXT; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | std::string operandValue; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 644 | error = context->getWord(operandValue, &nextPosition); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 645 | if (error) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 646 | context->diagnostic() << "Internal Error"; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 647 | return error; |
| 648 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 649 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 650 | error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(), |
| 651 | pInst, &expectedOperands); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 652 | |
| 653 | if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type)) |
| 654 | return SPV_SUCCESS; |
| 655 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 656 | if (error) return error; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 657 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 658 | context->setPosition(nextPosition); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 662 | if (spvOpcodeGeneratesType(pInst->opcode)) { |
| 663 | if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) { |
| 664 | return SPV_ERROR_INVALID_TEXT; |
| 665 | } |
| 666 | } else if (opcodeEntry->hasType) { |
| 667 | // SPIR-V dictates that if an instruction has both a return value and a |
| 668 | // type ID then the type id is first, and the return value is second. |
| 669 | assert(opcodeEntry->hasResult && |
| 670 | "Unknown opcode: has a type but no result."); |
| 671 | context->recordTypeIdForValue(pInst->words[2], pInst->words[1]); |
| 672 | } |
| 673 | |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 674 | if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
| 675 | context->diagnostic() << "Instruction too long: " << pInst->words.size() |
| 676 | << " words, but the limit is " |
| 677 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX; |
| 678 | return SPV_ERROR_INVALID_TEXT; |
| 679 | } |
| 680 | |
| 681 | pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 682 | |
| 683 | return SPV_SUCCESS; |
| 684 | } |
| 685 | |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 686 | namespace { |
| 687 | |
| 688 | // Translates a given assembly language module into binary form. |
| 689 | // If a diagnostic is generated, it is not yet marked as being |
| 690 | // for a text-based input. |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 691 | spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar, |
| 692 | const spv_text text, |
| 693 | spv_assembly_syntax_format_t format, |
| 694 | spv_binary* pBinary, |
| 695 | spv_diagnostic* pDiagnostic) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 696 | if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC; |
| 697 | libspirv::AssemblyContext context(text, pDiagnostic); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 698 | if (!text->str || !text->length) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 699 | context.diagnostic() << "Text stream is empty."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 700 | return SPV_ERROR_INVALID_TEXT; |
| 701 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 702 | if (!grammar.isValid()) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 703 | return SPV_ERROR_INVALID_TABLE; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 704 | } |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 705 | if (!pBinary) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 706 | |
| 707 | // NOTE: Ensure diagnostic is zero initialised |
| 708 | *pDiagnostic = {}; |
| 709 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 710 | std::vector<spv_instruction_t> instructions; |
| 711 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 712 | if (context.advance()) { |
| 713 | context.diagnostic() << "Text stream is empty."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 714 | return SPV_ERROR_INVALID_TEXT; |
| 715 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 716 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 717 | spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE; |
| 718 | while (context.hasText()) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 719 | instructions.push_back({}); |
| 720 | spv_instruction_t& inst = instructions.back(); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 721 | inst.extInstType = extInstType; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 722 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 723 | if (spvTextEncodeOpcode(grammar, &context, format, &inst)) { |
| 724 | return SPV_ERROR_INVALID_TEXT; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 725 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 726 | extInstType = inst.extInstType; |
| 727 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 728 | if (context.advance()) break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 729 | } |
| 730 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 731 | size_t totalSize = SPV_INDEX_INSTRUCTION; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 732 | for (auto& inst : instructions) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 733 | totalSize += inst.words.size(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 734 | } |
| 735 | |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 736 | uint32_t* data = new uint32_t[totalSize]; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 737 | if (!data) return SPV_ERROR_OUT_OF_MEMORY; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 738 | uint64_t currentIndex = SPV_INDEX_INSTRUCTION; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 739 | for (auto& inst : instructions) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 740 | memcpy(data + currentIndex, inst.words.data(), sizeof(uint32_t) * inst.words.size()); |
| 741 | currentIndex += inst.words.size(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | spv_binary binary = new spv_binary_t(); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 745 | if (!binary) { |
| 746 | delete[] data; |
| 747 | return SPV_ERROR_OUT_OF_MEMORY; |
| 748 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 749 | binary->code = data; |
| 750 | binary->wordCount = totalSize; |
| 751 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 752 | spv_result_t error = spvBinaryHeaderSet(binary, context.getBound()); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 753 | if (error) { |
| 754 | spvBinaryDestroy(binary); |
| 755 | return error; |
| 756 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 757 | |
| 758 | *pBinary = binary; |
| 759 | |
| 760 | return SPV_SUCCESS; |
| 761 | } |
| 762 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 763 | } // anonymous namespace |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 764 | |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 765 | spv_result_t spvTextToBinary(const char* input_text, |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 766 | const uint64_t input_text_size, |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 767 | const spv_opcode_table opcodeTable, |
| 768 | const spv_operand_table operandTable, |
| 769 | const spv_ext_inst_table extInstTable, |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 770 | spv_binary* pBinary, spv_diagnostic* pDiagnostic) { |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 771 | return spvTextWithFormatToBinary( |
| 772 | input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT, |
| 773 | opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic); |
| 774 | } |
| 775 | |
| 776 | spv_result_t spvTextWithFormatToBinary( |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 777 | const char* input_text, const uint64_t input_text_size, |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 778 | spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable, |
| 779 | const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 780 | spv_binary* pBinary, spv_diagnostic* pDiagnostic) { |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 781 | spv_text_t text = {input_text, input_text_size}; |
| 782 | |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 783 | libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable); |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 784 | spv_result_t result = |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 785 | spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic); |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 786 | if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true; |
| 787 | |
| 788 | return result; |
| 789 | } |
| 790 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 791 | void spvTextDestroy(spv_text text) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 792 | if (!text) return; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 793 | if (text->str) { |
| 794 | delete[] text->str; |
| 795 | } |
| 796 | delete text; |
| 797 | } |