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