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