Dejan Mircevski | b6fe02f | 2016-01-07 13:44:22 -0500 | [diff] [blame] | 1 | // Copyright (c) 2015-2016 The Khronos Group Inc. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 2 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 6 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 8 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 14 | |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 15 | #include "text.h" |
| 16 | |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 17 | #include <algorithm> |
| 18 | #include <cassert> |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 19 | #include <cctype> |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 20 | #include <cstdio> |
| 21 | #include <cstdlib> |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 22 | #include <cstring> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 23 | #include <memory> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 24 | #include <sstream> |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 25 | #include <string> |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 26 | #include <unordered_map> |
| 27 | #include <vector> |
Dejan Mircevski | ba569fb | 2015-09-11 16:34:49 -0400 | [diff] [blame] | 28 | |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 29 | #include "assembly_grammar.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 30 | #include "binary.h" |
| 31 | #include "diagnostic.h" |
| 32 | #include "ext_inst.h" |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 33 | #include "instruction.h" |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 34 | #include "message.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 35 | #include "opcode.h" |
| 36 | #include "operand.h" |
David Neto | 5a70335 | 2016-02-17 14:44:00 -0500 | [diff] [blame] | 37 | #include "spirv-tools/libspirv.h" |
Lei Zhang | aa056cd | 2015-11-11 14:24:04 -0500 | [diff] [blame] | 38 | #include "spirv_constant.h" |
Alan Baker | 42840d1 | 2018-04-06 14:15:27 -0400 | [diff] [blame] | 39 | #include "spirv_target_env.h" |
Lei Zhang | 7a222e4 | 2015-11-11 12:40:25 -0500 | [diff] [blame] | 40 | #include "table.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 41 | #include "text_handler.h" |
Andrew Woloszyn | f731cbf | 2015-10-23 09:53:58 -0400 | [diff] [blame] | 42 | #include "util/bitutils.h" |
qining | 1773b95 | 2016-09-01 14:27:04 -0400 | [diff] [blame] | 43 | #include "util/parse_number.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 44 | |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 45 | bool spvIsValidIDCharacter(const char value) { |
| 46 | return value == '_' || 0 != ::isalnum(value); |
| 47 | } |
| 48 | |
| 49 | // Returns true if the given string represents a valid ID name. |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 50 | bool spvIsValidID(const char* textValue) { |
| 51 | const char* c = textValue; |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 52 | for (; *c != '\0'; ++c) { |
| 53 | if (!spvIsValidIDCharacter(*c)) { |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | // If the string was empty, then the ID also is not valid. |
| 58 | return c != textValue; |
| 59 | } |
| 60 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 61 | // Text API |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 62 | |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 63 | spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 64 | bool isSigned = false; |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 65 | int numPeriods = 0; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 66 | bool isString = false; |
| 67 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 68 | const size_t len = strlen(textValue); |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 69 | if (len == 0) return SPV_FAILED_MATCH; |
| 70 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 71 | for (uint64_t index = 0; index < len; ++index) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 72 | switch (textValue[index]) { |
| 73 | case '0': |
| 74 | case '1': |
| 75 | case '2': |
| 76 | case '3': |
| 77 | case '4': |
| 78 | case '5': |
| 79 | case '6': |
| 80 | case '7': |
| 81 | case '8': |
| 82 | case '9': |
| 83 | break; |
| 84 | case '.': |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 85 | numPeriods++; |
| 86 | break; |
| 87 | case '-': |
| 88 | if (index == 0) { |
| 89 | isSigned = true; |
| 90 | } else { |
| 91 | isString = true; |
| 92 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 93 | break; |
| 94 | default: |
| 95 | isString = true; |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 96 | index = len; // break out of the loop too. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 97 | break; |
| 98 | } |
| 99 | } |
| 100 | |
David Neto | affa696 | 2015-08-24 15:33:14 -0400 | [diff] [blame] | 101 | pLiteral->type = spv_literal_type_t(99); |
| 102 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 103 | if (isString || numPeriods > 1 || (isSigned && len == 1)) { |
David Neto | 98290a2 | 2015-08-24 16:27:02 -0400 | [diff] [blame] | 104 | if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"') |
| 105 | return SPV_FAILED_MATCH; |
Andrew Woloszyn | 3e69cd1 | 2015-10-14 12:44:19 -0400 | [diff] [blame] | 106 | bool escaping = false; |
Lei Zhang | 6483bd7 | 2015-10-14 17:02:39 -0400 | [diff] [blame] | 107 | for (const char* val = textValue + 1; val != textValue + len - 1; ++val) { |
Andrew Woloszyn | 3e69cd1 | 2015-10-14 12:44:19 -0400 | [diff] [blame] | 108 | if ((*val == '\\') && (!escaping)) { |
| 109 | escaping = true; |
| 110 | } else { |
| 111 | // Have to save space for the null-terminator |
Lei Zhang | 16f3ddf | 2015-11-11 15:37:01 -0500 | [diff] [blame] | 112 | if (pLiteral->str.size() >= SPV_LIMIT_LITERAL_STRING_BYTES_MAX) |
Andrew Woloszyn | 3e69cd1 | 2015-10-14 12:44:19 -0400 | [diff] [blame] | 113 | return SPV_ERROR_OUT_OF_MEMORY; |
Lei Zhang | 16f3ddf | 2015-11-11 15:37:01 -0500 | [diff] [blame] | 114 | pLiteral->str.push_back(*val); |
Andrew Woloszyn | 3e69cd1 | 2015-10-14 12:44:19 -0400 | [diff] [blame] | 115 | escaping = false; |
Andrew Woloszyn | 3e69cd1 | 2015-10-14 12:44:19 -0400 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 119 | pLiteral->type = SPV_LITERAL_TYPE_STRING; |
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 == '!'); |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 164 | uint32_t parse_result; |
qining | 1773b95 | 2016-09-01 14:27:04 -0400 | [diff] [blame] | 165 | if (!spvutils::ParseNumber(text + 1, &parse_result)) { |
| 166 | return context->diagnostic(SPV_ERROR_INVALID_TEXT) |
| 167 | << "Invalid immediate integer: !" << text + 1; |
| 168 | } |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 169 | context->binaryEncodeU32(parse_result, pInst); |
Ben Vanik | 01c8d7a | 2015-11-22 08:32:53 -0800 | [diff] [blame] | 170 | context->seekForward(static_cast<uint32_t>(strlen(text))); |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 171 | return SPV_SUCCESS; |
| 172 | } |
| 173 | |
| 174 | } // anonymous namespace |
| 175 | |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 176 | /// @brief Translate an Opcode operand to binary form |
| 177 | /// |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 178 | /// @param[in] grammar the grammar to use for compilation |
| 179 | /// @param[in, out] context the dynamic compilation info |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 180 | /// @param[in] type of the operand |
| 181 | /// @param[in] textValue word of text to be parsed |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 182 | /// @param[out] pInst return binary Opcode |
| 183 | /// @param[in,out] pExpectedOperands the operand types expected |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 184 | /// |
| 185 | /// @return result code |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 186 | spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 187 | libspirv::AssemblyContext* context, |
| 188 | const spv_operand_type_t type, |
| 189 | const char* textValue, |
| 190 | spv_instruction_t* pInst, |
| 191 | spv_operand_pattern_t* pExpectedOperands) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 192 | // NOTE: Handle immediate int in the stream |
| 193 | if ('!' == textValue[0]) { |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 194 | if (auto error = encodeImmediate(context, textValue, pInst)) { |
| 195 | return error; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 196 | } |
Dejan Mircevski | 897bff9 | 2015-09-29 10:38:18 -0400 | [diff] [blame] | 197 | *pExpectedOperands = |
| 198 | spvAlternatePatternFollowingImmediate(*pExpectedOperands); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 199 | return SPV_SUCCESS; |
| 200 | } |
| 201 | |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 202 | // Optional literal operands can fail to parse. In that case use |
| 203 | // SPV_FAILED_MATCH to avoid emitting a diagostic. Use the following |
| 204 | // for those situations. |
| 205 | spv_result_t error_code_for_literals = |
| 206 | spvOperandIsOptional(type) ? SPV_FAILED_MATCH : SPV_ERROR_INVALID_TEXT; |
| 207 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 208 | switch (type) { |
David Neto | e3f70b9 | 2015-08-27 13:50:05 -0400 | [diff] [blame] | 209 | case SPV_OPERAND_TYPE_ID: |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 210 | case SPV_OPERAND_TYPE_TYPE_ID: |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 211 | case SPV_OPERAND_TYPE_RESULT_ID: |
| 212 | case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID: |
| 213 | case SPV_OPERAND_TYPE_SCOPE_ID: |
| 214 | case SPV_OPERAND_TYPE_OPTIONAL_ID: { |
Lei Zhang | abafd5e | 2015-08-21 11:52:29 -0400 | [diff] [blame] | 215 | if ('%' == textValue[0]) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 216 | textValue++; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 217 | } else { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 218 | return context->diagnostic() << "Expected id to start with %."; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 219 | } |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 220 | if (!spvIsValidID(textValue)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 221 | return context->diagnostic() << "Invalid ID " << textValue; |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 222 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 223 | const uint32_t id = context->spvNamedIdAssignOrGet(textValue); |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 224 | if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 225 | spvInstructionAddWord(pInst, id); |
David Neto | 2ae4a68 | 2015-11-09 18:55:42 -0500 | [diff] [blame] | 226 | |
| 227 | // Set the extended instruction type. |
| 228 | // The import set id is the 3rd operand of OpExtInst. |
| 229 | if (pInst->opcode == SpvOpExtInst && pInst->words.size() == 4) { |
| 230 | auto ext_inst_type = context->getExtInstTypeForId(pInst->words[3]); |
| 231 | if (ext_inst_type == SPV_EXT_INST_TYPE_NONE) { |
| 232 | return context->diagnostic() |
| 233 | << "Invalid extended instruction import Id " |
| 234 | << pInst->words[2]; |
| 235 | } |
| 236 | pInst->extInstType = ext_inst_type; |
| 237 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 238 | } break; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 239 | |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 240 | case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: { |
| 241 | // The assembler accepts the symbolic name for an extended instruction, |
| 242 | // and emits its corresponding number. |
| 243 | spv_ext_inst_desc extInst; |
| 244 | if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) { |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 245 | return context->diagnostic() |
| 246 | << "Invalid extended instruction name '" << textValue << "'."; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 247 | } |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 248 | spvInstructionAddWord(pInst, extInst->ext_inst); |
| 249 | |
| 250 | // Prepare to parse the operands for the extended instructions. |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 251 | spvPushOperandTypes(extInst->operandTypes, pExpectedOperands); |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 252 | } break; |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 253 | |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 254 | case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: { |
| 255 | // The assembler accepts the symbolic name for the opcode, but without |
| 256 | // the "Op" prefix. For example, "IAdd" is accepted. The number |
| 257 | // of the opcode is emitted. |
| 258 | SpvOp opcode; |
| 259 | if (grammar.lookupSpecConstantOpcode(textValue, &opcode)) { |
| 260 | return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) |
| 261 | << " '" << textValue << "'."; |
| 262 | } |
| 263 | spv_opcode_desc opcodeEntry = nullptr; |
| 264 | if (grammar.lookupOpcode(opcode, &opcodeEntry)) { |
| 265 | return context->diagnostic(SPV_ERROR_INTERNAL) |
| 266 | << "OpSpecConstant opcode table out of sync"; |
| 267 | } |
| 268 | spvInstructionAddWord(pInst, uint32_t(opcodeEntry->opcode)); |
| 269 | |
| 270 | // Prepare to parse the operands for the opcode. Except skip the |
| 271 | // type Id and result Id, since they've already been processed. |
| 272 | assert(opcodeEntry->hasType); |
| 273 | assert(opcodeEntry->hasResult); |
| 274 | assert(opcodeEntry->numTypes >= 2); |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 275 | spvPushOperandTypes(opcodeEntry->operandTypes + 2, pExpectedOperands); |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 276 | } break; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 277 | |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 278 | case SPV_OPERAND_TYPE_LITERAL_INTEGER: |
| 279 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: { |
| 280 | // The current operand is an *unsigned* 32-bit integer. |
| 281 | // That's just how the grammar works. |
| 282 | libspirv::IdType expected_type = { |
| 283 | 32, false, libspirv::IdTypeClass::kScalarIntegerType}; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 284 | if (auto error = context->binaryEncodeNumericLiteral( |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 285 | textValue, error_code_for_literals, expected_type, pInst)) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 286 | return error; |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 287 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 288 | } break; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 289 | |
| 290 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: |
| 291 | // This is a context-independent literal number which can be a 32-bit |
| 292 | // number of floating point value. |
| 293 | if (auto error = context->binaryEncodeNumericLiteral( |
| 294 | textValue, error_code_for_literals, libspirv::kUnknownType, |
| 295 | pInst)) { |
| 296 | return error; |
| 297 | } |
| 298 | break; |
| 299 | |
| 300 | case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER: |
| 301 | case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: { |
| 302 | libspirv::IdType expected_type = libspirv::kUnknownType; |
| 303 | // The encoding for OpConstant, OpSpecConstant and OpSwitch all |
| 304 | // depend on either their own result-id or the result-id of |
| 305 | // one of their parameters. |
| 306 | if (SpvOpConstant == pInst->opcode || |
| 307 | SpvOpSpecConstant == pInst->opcode) { |
| 308 | // The type of the literal is determined by the type Id of the |
| 309 | // instruction. |
| 310 | expected_type = |
| 311 | context->getTypeOfTypeGeneratingValue(pInst->resultTypeId); |
| 312 | if (!libspirv::isScalarFloating(expected_type) && |
| 313 | !libspirv::isScalarIntegral(expected_type)) { |
| 314 | spv_opcode_desc d; |
| 315 | const char* opcode_name = "opcode"; |
| 316 | if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) { |
| 317 | opcode_name = d->name; |
| 318 | } |
| 319 | return context->diagnostic() |
| 320 | << "Type for " << opcode_name |
| 321 | << " must be a scalar floating point or integer type"; |
| 322 | } |
| 323 | } else if (pInst->opcode == SpvOpSwitch) { |
| 324 | // The type of the literal is the same as the type of the selector. |
| 325 | expected_type = context->getTypeOfValueInstruction(pInst->words[1]); |
| 326 | if (!libspirv::isScalarIntegral(expected_type)) { |
| 327 | return context->diagnostic() |
| 328 | << "The selector operand for OpSwitch must be the result" |
| 329 | " of an instruction that generates an integer scalar"; |
| 330 | } |
| 331 | } |
| 332 | if (auto error = context->binaryEncodeNumericLiteral( |
| 333 | textValue, error_code_for_literals, expected_type, pInst)) { |
| 334 | return error; |
| 335 | } |
| 336 | } break; |
| 337 | |
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; |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 344 | return context->diagnostic(error_code_for_literals) |
| 345 | << "Invalid literal string '" << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 346 | } |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 347 | if (literal.type != SPV_LITERAL_TYPE_STRING) { |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 348 | return context->diagnostic() |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 349 | << "Expected literal string, found literal number '" << textValue |
| 350 | << "'."; |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 351 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 352 | |
| 353 | // NOTE: Special case for extended instruction library import |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 354 | if (SpvOpExtInstImport == pInst->opcode) { |
David Neto | 2ae4a68 | 2015-11-09 18:55:42 -0500 | [diff] [blame] | 355 | const spv_ext_inst_type_t ext_inst_type = |
Lei Zhang | 16f3ddf | 2015-11-11 15:37:01 -0500 | [diff] [blame] | 356 | spvExtInstImportTypeGet(literal.str.c_str()); |
David Neto | 2ae4a68 | 2015-11-09 18:55:42 -0500 | [diff] [blame] | 357 | if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) { |
| 358 | return context->diagnostic() |
Lei Zhang | 16f3ddf | 2015-11-11 15:37:01 -0500 | [diff] [blame] | 359 | << "Invalid extended instruction import '" << literal.str |
David Neto | 2ae4a68 | 2015-11-09 18:55:42 -0500 | [diff] [blame] | 360 | << "'"; |
| 361 | } |
David Neto | 677e0c7 | 2016-01-05 14:56:02 -0500 | [diff] [blame] | 362 | if ((error = context->recordIdAsExtInstImport(pInst->words[1], |
| 363 | ext_inst_type))) |
David Neto | 2ae4a68 | 2015-11-09 18:55:42 -0500 | [diff] [blame] | 364 | return error; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Lei Zhang | 16f3ddf | 2015-11-11 15:37:01 -0500 | [diff] [blame] | 367 | if (context->binaryEncodeString(literal.str.c_str(), pInst)) |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 368 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 369 | } break; |
David Neto | 59de610 | 2017-12-03 12:30:08 -0500 | [diff] [blame] | 370 | |
| 371 | // Masks. |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 372 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
| 373 | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
| 374 | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
David Neto | 2889a0c | 2016-02-15 13:50:00 -0500 | [diff] [blame] | 375 | case SPV_OPERAND_TYPE_IMAGE: |
David Neto | ee1b3bb | 2015-09-18 11:19:18 -0400 | [diff] [blame] | 376 | case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 377 | case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
David Neto | 59de610 | 2017-12-03 12:30:08 -0500 | [diff] [blame] | 378 | case SPV_OPERAND_TYPE_SELECTION_CONTROL: |
| 379 | case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS: { |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 380 | uint32_t value; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 381 | if (grammar.parseMaskOperand(type, textValue, &value)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 382 | return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 383 | << " operand '" << textValue << "'."; |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 384 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 385 | if (auto error = context->binaryEncodeU32(value, pInst)) return error; |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 386 | // Prepare to parse the operands for this logical operand. |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 387 | grammar.pushOperandTypesForMask(type, value, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 388 | } break; |
| 389 | case SPV_OPERAND_TYPE_OPTIONAL_CIV: { |
| 390 | auto error = spvTextEncodeOperand( |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 391 | grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue, |
| 392 | pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 393 | if (error == SPV_FAILED_MATCH) { |
| 394 | // It's not a literal number -- is it a literal string? |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 395 | error = spvTextEncodeOperand(grammar, context, |
| 396 | SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING, |
| 397 | textValue, pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 398 | } |
| 399 | if (error == SPV_FAILED_MATCH) { |
| 400 | // It's not a literal -- is it an ID? |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 401 | error = |
| 402 | spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID, |
| 403 | textValue, pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 404 | } |
| 405 | if (error) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 406 | return context->diagnostic(error) |
| 407 | << "Invalid word following !<integer>: " << textValue; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 408 | } |
| 409 | if (pExpectedOperands->empty()) { |
| 410 | pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV); |
| 411 | } |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 412 | } break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 413 | default: { |
| 414 | // NOTE: All non literal operands are handled here using the operand |
| 415 | // table. |
| 416 | spv_operand_desc entry; |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 417 | if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 418 | return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) |
| 419 | << " '" << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 420 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 421 | if (context->binaryEncodeU32(entry->value, pInst)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 422 | return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) |
| 423 | << " '" << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 424 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 425 | |
| 426 | // Prepare to parse the operands for this logical operand. |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 427 | spvPushOperandTypes(entry->operandTypes, pExpectedOperands); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 428 | } break; |
| 429 | } |
| 430 | return SPV_SUCCESS; |
| 431 | } |
| 432 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 433 | namespace { |
| 434 | |
| 435 | /// Encodes an instruction started by !<integer> at the given position in text. |
| 436 | /// |
| 437 | /// Puts the encoded words into *pInst. If successful, moves position past the |
| 438 | /// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and |
| 439 | /// leaves position pointing to the error in text. |
| 440 | spv_result_t encodeInstructionStartingWithImmediate( |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 441 | const libspirv::AssemblyGrammar& grammar, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 442 | libspirv::AssemblyContext* context, spv_instruction_t* pInst) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 443 | std::string firstWord; |
| 444 | spv_position_t nextPosition = {}; |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 445 | auto error = context->getWord(&firstWord, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 446 | if (error) return context->diagnostic(error) << "Internal Error"; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 447 | |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 448 | if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) { |
| 449 | return error; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 450 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 451 | while (context->advance() != SPV_END_OF_STREAM) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 452 | // A beginning of a new instruction means we're done. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 453 | if (context->isStartOfNewInst()) return SPV_SUCCESS; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 454 | |
| 455 | // Otherwise, there must be an operand that's either a literal, an ID, or |
| 456 | // an immediate. |
| 457 | std::string operandValue; |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 458 | if ((error = context->getWord(&operandValue, &nextPosition))) |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 459 | return context->diagnostic(error) << "Internal Error"; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 460 | |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 461 | if (operandValue == "=") |
| 462 | return context->diagnostic() << firstWord << " not allowed before =."; |
Dejan Mircevski | e3a19c0 | 2015-09-11 15:03:54 -0400 | [diff] [blame] | 463 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 464 | // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be |
| 465 | // expanded. |
| 466 | spv_operand_pattern_t dummyExpectedOperands; |
| 467 | error = spvTextEncodeOperand( |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 468 | grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(), |
| 469 | pInst, &dummyExpectedOperands); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 470 | if (error) return error; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 471 | context->setPosition(nextPosition); |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 472 | } |
| 473 | return SPV_SUCCESS; |
| 474 | } |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 475 | |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 476 | /// @brief Translate single Opcode and operands to binary form |
| 477 | /// |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 478 | /// @param[in] grammar the grammar to use for compilation |
| 479 | /// @param[in, out] context the dynamic compilation info |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 480 | /// @param[in] text stream to translate |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 481 | /// @param[out] pInst returned binary Opcode |
| 482 | /// @param[in,out] pPosition in the text stream |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 483 | /// |
| 484 | /// @return result code |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 485 | spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 486 | libspirv::AssemblyContext* context, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 487 | spv_instruction_t* pInst) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 488 | // Check for !<integer> first. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 489 | if ('!' == context->peek()) { |
| 490 | return encodeInstructionStartingWithImmediate(grammar, context, pInst); |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 491 | } |
| 492 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 493 | std::string firstWord; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 494 | spv_position_t nextPosition = {}; |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 495 | spv_result_t error = context->getWord(&firstWord, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 496 | if (error) return context->diagnostic() << "Internal Error"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 497 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 498 | std::string opcodeName; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 499 | std::string result_id; |
| 500 | spv_position_t result_id_position = {}; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 501 | if (context->startsWithOp()) { |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 502 | opcodeName = firstWord; |
| 503 | } else { |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 504 | result_id = firstWord; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 505 | if ('%' != result_id.front()) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 506 | return context->diagnostic() |
| 507 | << "Expected <opcode> or <result-id> at the beginning " |
| 508 | "of an instruction, found '" |
| 509 | << result_id << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 510 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 511 | result_id_position = context->position(); |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 512 | |
| 513 | // The '=' sign. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 514 | context->setPosition(nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 515 | if (context->advance()) |
| 516 | return context->diagnostic() << "Expected '=', found end of stream."; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 517 | std::string equal_sign; |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 518 | error = context->getWord(&equal_sign, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 519 | if ("=" != equal_sign) |
| 520 | return context->diagnostic() << "'=' expected after result id."; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 521 | |
| 522 | // The <opcode> after the '=' sign. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 523 | context->setPosition(nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 524 | if (context->advance()) |
| 525 | return context->diagnostic() << "Expected opcode, found end of stream."; |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 526 | error = context->getWord(&opcodeName, &nextPosition); |
Lei Zhang | 6483bd7 | 2015-10-14 17:02:39 -0400 | [diff] [blame] | 527 | if (error) return context->diagnostic(error) << "Internal Error"; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 528 | if (!context->startsWithOp()) { |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 529 | return context->diagnostic() |
| 530 | << "Invalid Opcode prefix '" << opcodeName << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 531 | } |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 532 | } |
| 533 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 534 | // NOTE: The table contains Opcode names without the "Op" prefix. |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 535 | const char* pInstName = opcodeName.data() + 2; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 536 | |
| 537 | spv_opcode_desc opcodeEntry; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 538 | error = grammar.lookupOpcode(pInstName, &opcodeEntry); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 539 | if (error) { |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 540 | return context->diagnostic(error) |
| 541 | << "Invalid Opcode name '" << opcodeName << "'"; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 542 | } |
Lei Zhang | 9042f40 | 2015-11-05 17:45:09 -0500 | [diff] [blame] | 543 | if (opcodeEntry->hasResult && result_id.empty()) { |
| 544 | return context->diagnostic() |
| 545 | << "Expected <result-id> at the beginning of an instruction, found '" |
| 546 | << firstWord << "'."; |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 547 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 548 | pInst->opcode = opcodeEntry->opcode; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 549 | context->setPosition(nextPosition); |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 550 | // Reserve the first word for the instruction. |
| 551 | spvInstructionAddWord(pInst, 0); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 552 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 553 | // Maintains the ordered list of expected operand types. |
| 554 | // For many instructions we only need the {numTypes, operandTypes} |
| 555 | // entries in opcodeEntry. However, sometimes we need to modify |
| 556 | // the list as we parse the operands. This occurs when an operand |
| 557 | // has its own logical operands (such as the LocalSize operand for |
| 558 | // ExecutionMode), or for extended instructions that may have their |
| 559 | // own operands depending on the selected extended instruction. |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 560 | spv_operand_pattern_t expectedOperands; |
| 561 | expectedOperands.reserve(opcodeEntry->numTypes); |
| 562 | for (auto i = 0; i < opcodeEntry->numTypes; i++) |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 563 | expectedOperands.push_back( |
| 564 | opcodeEntry->operandTypes[opcodeEntry->numTypes - i - 1]); |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 565 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 566 | while (!expectedOperands.empty()) { |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 567 | const spv_operand_type_t type = expectedOperands.back(); |
| 568 | expectedOperands.pop_back(); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 569 | |
| 570 | // Expand optional tuples lazily. |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 571 | if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 572 | |
| 573 | if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) { |
| 574 | // Handle the <result-id> for value generating instructions. |
| 575 | // We've already consumed it from the text stream. Here |
| 576 | // we inject its words into the instruction. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 577 | spv_position_t temp_pos = context->position(); |
| 578 | error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID, |
| 579 | result_id.c_str(), pInst, nullptr); |
| 580 | result_id_position = context->position(); |
| 581 | // Because we are injecting we have to reset the position afterwards. |
| 582 | context->setPosition(temp_pos); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 583 | if (error) return error; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 584 | } else { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 585 | // Find the next word. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 586 | error = context->advance(); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 587 | if (error == SPV_END_OF_STREAM) { |
| 588 | if (spvOperandIsOptional(type)) { |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 589 | // This would have been the last potential operand for the |
| 590 | // instruction, |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 591 | // and we didn't find one. We're finished parsing this instruction. |
| 592 | break; |
| 593 | } else { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 594 | return context->diagnostic() |
| 595 | << "Expected operand, found end of stream."; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 596 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 597 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 598 | assert(error == SPV_SUCCESS && "Somebody added another way to fail"); |
| 599 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 600 | if (context->isStartOfNewInst()) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 601 | if (spvOperandIsOptional(type)) { |
| 602 | break; |
| 603 | } else { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 604 | return context->diagnostic() |
| 605 | << "Expected operand, found next instruction instead."; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | std::string operandValue; |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 610 | error = context->getWord(&operandValue, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 611 | if (error) return context->diagnostic(error) << "Internal Error"; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 612 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 613 | error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(), |
| 614 | pInst, &expectedOperands); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 615 | |
| 616 | if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type)) |
| 617 | return SPV_SUCCESS; |
| 618 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 619 | if (error) return error; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 620 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 621 | context->setPosition(nextPosition); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 625 | if (spvOpcodeGeneratesType(pInst->opcode)) { |
| 626 | if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) { |
| 627 | return SPV_ERROR_INVALID_TEXT; |
| 628 | } |
| 629 | } else if (opcodeEntry->hasType) { |
| 630 | // SPIR-V dictates that if an instruction has both a return value and a |
| 631 | // type ID then the type id is first, and the return value is second. |
| 632 | assert(opcodeEntry->hasResult && |
| 633 | "Unknown opcode: has a type but no result."); |
| 634 | context->recordTypeIdForValue(pInst->words[2], pInst->words[1]); |
| 635 | } |
| 636 | |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 637 | if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 638 | return context->diagnostic() |
| 639 | << "Instruction too long: " << pInst->words.size() |
| 640 | << " words, but the limit is " |
| 641 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 642 | } |
| 643 | |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 644 | pInst->words[0] = |
| 645 | spvOpcodeMake(uint16_t(pInst->words.size()), opcodeEntry->opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 646 | |
| 647 | return SPV_SUCCESS; |
| 648 | } |
| 649 | |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 650 | enum { kAssemblerVersion = 0 }; |
David Neto | 14b93e4 | 2015-11-12 18:33:47 -0500 | [diff] [blame] | 651 | |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 652 | // Populates a binary stream's |header|. The target environment is specified via |
| 653 | // |env| and Id bound is via |bound|. |
| 654 | spv_result_t SetHeader(spv_target_env env, const uint32_t bound, |
| 655 | uint32_t* header) { |
| 656 | if (!header) return SPV_ERROR_INVALID_BINARY; |
David Neto | 0b98168 | 2015-10-27 15:51:34 -0400 | [diff] [blame] | 657 | |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 658 | header[SPV_INDEX_MAGIC_NUMBER] = SpvMagicNumber; |
| 659 | header[SPV_INDEX_VERSION_NUMBER] = spvVersionForTargetEnv(env); |
| 660 | header[SPV_INDEX_GENERATOR_NUMBER] = |
David Neto | 14b93e4 | 2015-11-12 18:33:47 -0500 | [diff] [blame] | 661 | SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS_ASSEMBLER, kAssemblerVersion); |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 662 | header[SPV_INDEX_BOUND] = bound; |
| 663 | header[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved |
David Neto | 0b98168 | 2015-10-27 15:51:34 -0400 | [diff] [blame] | 664 | |
| 665 | return SPV_SUCCESS; |
| 666 | } |
| 667 | |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 668 | // Collects all numeric ids in the module source into |numeric_ids|. |
| 669 | // This function is essentially a dry-run of spvTextToBinary. |
| 670 | spv_result_t GetNumericIds(const libspirv::AssemblyGrammar& grammar, |
| 671 | const spvtools::MessageConsumer& consumer, |
| 672 | const spv_text text, |
| 673 | std::set<uint32_t>* numeric_ids) { |
| 674 | libspirv::AssemblyContext context(text, consumer); |
| 675 | |
| 676 | if (!text->str) return context.diagnostic() << "Missing assembly text."; |
| 677 | |
| 678 | if (!grammar.isValid()) { |
| 679 | return SPV_ERROR_INVALID_TABLE; |
| 680 | } |
| 681 | |
| 682 | // Skip past whitespace and comments. |
| 683 | context.advance(); |
| 684 | |
| 685 | while (context.hasText()) { |
| 686 | spv_instruction_t inst; |
| 687 | |
| 688 | if (spvTextEncodeOpcode(grammar, &context, &inst)) { |
| 689 | return SPV_ERROR_INVALID_TEXT; |
| 690 | } |
| 691 | |
| 692 | if (context.advance()) break; |
| 693 | } |
| 694 | |
| 695 | *numeric_ids = context.GetNumericIds(); |
| 696 | return SPV_SUCCESS; |
| 697 | } |
| 698 | |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 699 | // Translates a given assembly language module into binary form. |
| 700 | // If a diagnostic is generated, it is not yet marked as being |
| 701 | // for a text-based input. |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 702 | spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar, |
| 703 | const spvtools::MessageConsumer& consumer, |
| 704 | const spv_text text, |
| 705 | const uint32_t options, |
| 706 | spv_binary* pBinary) { |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 707 | // The ids in this set will have the same values both in source and binary. |
| 708 | // All other ids will be generated by filling in the gaps. |
| 709 | std::set<uint32_t> ids_to_preserve; |
| 710 | |
| 711 | if (options & SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS) { |
| 712 | // Collect all numeric ids from the source into ids_to_preserve. |
| 713 | const spv_result_t result = |
| 714 | GetNumericIds(grammar, consumer, text, &ids_to_preserve); |
| 715 | if (result != SPV_SUCCESS) return result; |
| 716 | } |
| 717 | |
| 718 | libspirv::AssemblyContext context(text, consumer, std::move(ids_to_preserve)); |
| 719 | |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 720 | if (!text->str) return context.diagnostic() << "Missing assembly text."; |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 721 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 722 | if (!grammar.isValid()) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 723 | return SPV_ERROR_INVALID_TABLE; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 724 | } |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 725 | if (!pBinary) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 726 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 727 | std::vector<spv_instruction_t> instructions; |
| 728 | |
David Neto | ea633a6 | 2015-11-02 11:40:59 -0500 | [diff] [blame] | 729 | // Skip past whitespace and comments. |
| 730 | context.advance(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 731 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 732 | while (context.hasText()) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 733 | instructions.push_back({}); |
| 734 | spv_instruction_t& inst = instructions.back(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 735 | |
Lei Zhang | 9042f40 | 2015-11-05 17:45:09 -0500 | [diff] [blame] | 736 | if (spvTextEncodeOpcode(grammar, &context, &inst)) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 737 | return SPV_ERROR_INVALID_TEXT; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 738 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 739 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 740 | if (context.advance()) break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 743 | size_t totalSize = SPV_INDEX_INSTRUCTION; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 744 | for (auto& inst : instructions) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 745 | totalSize += inst.words.size(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 748 | uint32_t* data = new uint32_t[totalSize]; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 749 | if (!data) return SPV_ERROR_OUT_OF_MEMORY; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 750 | uint64_t currentIndex = SPV_INDEX_INSTRUCTION; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 751 | for (auto& inst : instructions) { |
Lei Zhang | 6483bd7 | 2015-10-14 17:02:39 -0400 | [diff] [blame] | 752 | memcpy(data + currentIndex, inst.words.data(), |
| 753 | sizeof(uint32_t) * inst.words.size()); |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 754 | currentIndex += inst.words.size(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 755 | } |
| 756 | |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 757 | if (auto error = SetHeader(grammar.target_env(), context.getBound(), data)) |
| 758 | return error; |
David Neto | e4945de | 2015-10-28 13:50:32 -0400 | [diff] [blame] | 759 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 760 | spv_binary binary = new spv_binary_t(); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 761 | if (!binary) { |
| 762 | delete[] data; |
| 763 | return SPV_ERROR_OUT_OF_MEMORY; |
| 764 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 765 | binary->code = data; |
| 766 | binary->wordCount = totalSize; |
| 767 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 768 | *pBinary = binary; |
| 769 | |
| 770 | return SPV_SUCCESS; |
| 771 | } |
| 772 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 773 | } // anonymous namespace |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 774 | |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 775 | spv_result_t spvTextToBinary(const spv_const_context context, |
| 776 | const char* input_text, |
Lei Zhang | df920ec | 2015-11-11 11:33:26 -0500 | [diff] [blame] | 777 | const size_t input_text_size, spv_binary* pBinary, |
| 778 | spv_diagnostic* pDiagnostic) { |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 779 | return spvTextToBinaryWithOptions(context, input_text, input_text_size, |
| 780 | SPV_BINARY_TO_TEXT_OPTION_NONE, pBinary, |
| 781 | pDiagnostic); |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 782 | } |
| 783 | |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 784 | spv_result_t spvTextToBinaryWithOptions(const spv_const_context context, |
| 785 | const char* input_text, |
| 786 | const size_t input_text_size, |
| 787 | const uint32_t options, |
| 788 | spv_binary* pBinary, |
| 789 | spv_diagnostic* pDiagnostic) { |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 790 | spv_context_t hijack_context = *context; |
| 791 | if (pDiagnostic) { |
| 792 | *pDiagnostic = nullptr; |
| 793 | libspirv::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic); |
| 794 | } |
| 795 | |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 796 | spv_text_t text = {input_text, input_text_size}; |
Lei Zhang | 755f97f | 2016-09-02 18:06:18 -0400 | [diff] [blame] | 797 | libspirv::AssemblyGrammar grammar(&hijack_context); |
Lei Zhang | 9042f40 | 2015-11-05 17:45:09 -0500 | [diff] [blame] | 798 | |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 799 | spv_result_t result = spvTextToBinaryInternal( |
| 800 | grammar, hijack_context.consumer, &text, options, pBinary); |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 801 | if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true; |
| 802 | |
| 803 | return result; |
| 804 | } |
| 805 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 806 | void spvTextDestroy(spv_text text) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 807 | if (!text) return; |
Eric Engestrom | eb6ae97 | 2016-02-18 23:41:16 +0000 | [diff] [blame] | 808 | delete[] text->str; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 809 | delete text; |
| 810 | } |