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> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 36 | #include <sstream> |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame^] | 37 | #include <string> |
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 | e3f70b9 | 2015-08-27 13:50:05 -0400 | [diff] [blame] | 219 | case SPV_OPERAND_TYPE_ID: |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 220 | case SPV_OPERAND_TYPE_TYPE_ID: |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame^] | 221 | case SPV_OPERAND_TYPE_RESULT_ID: |
| 222 | case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID: |
| 223 | case SPV_OPERAND_TYPE_SCOPE_ID: |
| 224 | case SPV_OPERAND_TYPE_OPTIONAL_ID: { |
Lei Zhang | abafd5e | 2015-08-21 11:52:29 -0400 | [diff] [blame] | 225 | if ('%' == textValue[0]) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 226 | textValue++; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 227 | } else { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 228 | return context->diagnostic() << "Expected id to start with %."; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 229 | } |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 230 | if (!spvIsValidID(textValue)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 231 | return context->diagnostic() << "Invalid ID " << textValue; |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 232 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 233 | const uint32_t id = context->spvNamedIdAssignOrGet(textValue); |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 234 | if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 235 | spvInstructionAddWord(pInst, id); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 236 | } break; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame^] | 237 | |
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; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 253 | |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame^] | 254 | case SPV_OPERAND_TYPE_LITERAL_INTEGER: |
| 255 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: { |
| 256 | // The current operand is an *unsigned* 32-bit integer. |
| 257 | // That's just how the grammar works. |
| 258 | libspirv::IdType expected_type = { |
| 259 | 32, false, libspirv::IdTypeClass::kScalarIntegerType}; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 260 | if (auto error = context->binaryEncodeNumericLiteral( |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 261 | textValue, error_code_for_literals, expected_type, pInst)) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 262 | return error; |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 263 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 264 | } break; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame^] | 265 | |
| 266 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: |
| 267 | // This is a context-independent literal number which can be a 32-bit |
| 268 | // number of floating point value. |
| 269 | if (auto error = context->binaryEncodeNumericLiteral( |
| 270 | textValue, error_code_for_literals, libspirv::kUnknownType, |
| 271 | pInst)) { |
| 272 | return error; |
| 273 | } |
| 274 | break; |
| 275 | |
| 276 | case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER: |
| 277 | case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: { |
| 278 | libspirv::IdType expected_type = libspirv::kUnknownType; |
| 279 | // The encoding for OpConstant, OpSpecConstant and OpSwitch all |
| 280 | // depend on either their own result-id or the result-id of |
| 281 | // one of their parameters. |
| 282 | if (SpvOpConstant == pInst->opcode || |
| 283 | SpvOpSpecConstant == pInst->opcode) { |
| 284 | // The type of the literal is determined by the type Id of the |
| 285 | // instruction. |
| 286 | expected_type = |
| 287 | context->getTypeOfTypeGeneratingValue(pInst->resultTypeId); |
| 288 | if (!libspirv::isScalarFloating(expected_type) && |
| 289 | !libspirv::isScalarIntegral(expected_type)) { |
| 290 | spv_opcode_desc d; |
| 291 | const char* opcode_name = "opcode"; |
| 292 | if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) { |
| 293 | opcode_name = d->name; |
| 294 | } |
| 295 | return context->diagnostic() |
| 296 | << "Type for " << opcode_name |
| 297 | << " must be a scalar floating point or integer type"; |
| 298 | } |
| 299 | } else if (pInst->opcode == SpvOpSwitch) { |
| 300 | // The type of the literal is the same as the type of the selector. |
| 301 | expected_type = context->getTypeOfValueInstruction(pInst->words[1]); |
| 302 | if (!libspirv::isScalarIntegral(expected_type)) { |
| 303 | return context->diagnostic() |
| 304 | << "The selector operand for OpSwitch must be the result" |
| 305 | " of an instruction that generates an integer scalar"; |
| 306 | } |
| 307 | } |
| 308 | if (auto error = context->binaryEncodeNumericLiteral( |
| 309 | textValue, error_code_for_literals, expected_type, pInst)) { |
| 310 | return error; |
| 311 | } |
| 312 | } break; |
| 313 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 314 | case SPV_OPERAND_TYPE_LITERAL_STRING: |
| 315 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: { |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 316 | spv_literal_t literal = {}; |
| 317 | spv_result_t error = spvTextToLiteral(textValue, &literal); |
| 318 | if (error != SPV_SUCCESS) { |
| 319 | if (error == SPV_ERROR_OUT_OF_MEMORY) return error; |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 320 | return context->diagnostic(error_code_for_literals) |
| 321 | << "Invalid literal string '" << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 322 | } |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 323 | if (literal.type != SPV_LITERAL_TYPE_STRING) { |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 324 | return context->diagnostic() |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 325 | << "Expected literal string, found literal number '" << textValue |
| 326 | << "'."; |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 327 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 328 | |
| 329 | // NOTE: Special case for extended instruction library import |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 330 | if (SpvOpExtInstImport == pInst->opcode) { |
Lei Zhang | 6d41581 | 2015-09-15 13:36:21 -0400 | [diff] [blame] | 331 | pInst->extInstType = spvExtInstImportTypeGet(literal.value.str); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 334 | if (context->binaryEncodeString(literal.value.str, pInst)) |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 335 | return SPV_ERROR_INVALID_TEXT; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 336 | } break; |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 337 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
| 338 | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
| 339 | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
David Neto | ee1b3bb | 2015-09-18 11:19:18 -0400 | [diff] [blame] | 340 | case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 341 | case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 342 | case SPV_OPERAND_TYPE_SELECTION_CONTROL: { |
| 343 | uint32_t value; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 344 | if (grammar.parseMaskOperand(type, textValue, &value)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 345 | return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) |
| 346 | << " '" << textValue << "'."; |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 347 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 348 | if (auto error = context->binaryEncodeU32(value, pInst)) return error; |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 349 | // Prepare to parse the operands for this logical operand. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 350 | grammar.prependOperandTypesForMask(type, value, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 351 | } break; |
| 352 | case SPV_OPERAND_TYPE_OPTIONAL_CIV: { |
| 353 | auto error = spvTextEncodeOperand( |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 354 | grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue, |
| 355 | pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 356 | if (error == SPV_FAILED_MATCH) { |
| 357 | // It's not a literal number -- is it a literal string? |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 358 | error = spvTextEncodeOperand(grammar, context, |
| 359 | SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING, |
| 360 | textValue, pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 361 | } |
| 362 | if (error == SPV_FAILED_MATCH) { |
| 363 | // It's not a literal -- is it an ID? |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 364 | error = |
| 365 | spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID, |
| 366 | textValue, pInst, pExpectedOperands); |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 367 | } |
| 368 | if (error) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 369 | return context->diagnostic(error) |
| 370 | << "Invalid word following !<integer>: " << textValue; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 371 | } |
| 372 | if (pExpectedOperands->empty()) { |
| 373 | pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV); |
| 374 | } |
David Neto | 36b0c0f | 2015-09-16 18:32:54 -0400 | [diff] [blame] | 375 | } break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 376 | default: { |
| 377 | // NOTE: All non literal operands are handled here using the operand |
| 378 | // table. |
| 379 | spv_operand_desc entry; |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 380 | if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 381 | return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) |
| 382 | << " '" << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 383 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 384 | if (context->binaryEncodeU32(entry->value, pInst)) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 385 | return context->diagnostic() << "Invalid " << spvOperandTypeStr(type) |
| 386 | << " '" << textValue << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 387 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 388 | |
| 389 | // Prepare to parse the operands for this logical operand. |
| 390 | spvPrependOperandTypes(entry->operandTypes, pExpectedOperands); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 391 | } break; |
| 392 | } |
| 393 | return SPV_SUCCESS; |
| 394 | } |
| 395 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 396 | namespace { |
| 397 | |
| 398 | /// Encodes an instruction started by !<integer> at the given position in text. |
| 399 | /// |
| 400 | /// Puts the encoded words into *pInst. If successful, moves position past the |
| 401 | /// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and |
| 402 | /// leaves position pointing to the error in text. |
| 403 | spv_result_t encodeInstructionStartingWithImmediate( |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 404 | const libspirv::AssemblyGrammar& grammar, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 405 | libspirv::AssemblyContext* context, spv_instruction_t* pInst) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 406 | std::string firstWord; |
| 407 | spv_position_t nextPosition = {}; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 408 | auto error = context->getWord(firstWord, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 409 | if (error) return context->diagnostic(error) << "Internal Error"; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 410 | |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 411 | if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) { |
| 412 | return error; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 413 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 414 | while (context->advance() != SPV_END_OF_STREAM) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 415 | // A beginning of a new instruction means we're done. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 416 | if (context->isStartOfNewInst()) return SPV_SUCCESS; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 417 | |
| 418 | // Otherwise, there must be an operand that's either a literal, an ID, or |
| 419 | // an immediate. |
| 420 | std::string operandValue; |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 421 | if ((error = context->getWord(operandValue, &nextPosition))) |
| 422 | return context->diagnostic(error) << "Internal Error"; |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 423 | |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 424 | if (operandValue == "=") |
| 425 | return context->diagnostic() << firstWord << " not allowed before =."; |
Dejan Mircevski | e3a19c0 | 2015-09-11 15:03:54 -0400 | [diff] [blame] | 426 | |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 427 | // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be |
| 428 | // expanded. |
| 429 | spv_operand_pattern_t dummyExpectedOperands; |
| 430 | error = spvTextEncodeOperand( |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 431 | grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(), |
| 432 | pInst, &dummyExpectedOperands); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 433 | if (error) return error; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 434 | context->setPosition(nextPosition); |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 435 | } |
| 436 | return SPV_SUCCESS; |
| 437 | } |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 438 | } // anonymous namespace |
| 439 | |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 440 | /// @brief Translate single Opcode and operands to binary form |
| 441 | /// |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 442 | /// @param[in] grammar the grammar to use for compilation |
| 443 | /// @param[in, out] context the dynamic compilation info |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 444 | /// @param[in] text stream to translate |
| 445 | /// @param[in] format the assembly syntax format of text |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 446 | /// @param[out] pInst returned binary Opcode |
| 447 | /// @param[in,out] pPosition in the text stream |
Andrew Woloszyn | 13804e5 | 2015-09-22 15:50:33 -0400 | [diff] [blame] | 448 | /// |
| 449 | /// @return result code |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 450 | spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 451 | libspirv::AssemblyContext* context, |
| 452 | spv_assembly_syntax_format_t format, |
| 453 | spv_instruction_t* pInst) { |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 454 | // Check for !<integer> first. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 455 | if ('!' == context->peek()) { |
| 456 | return encodeInstructionStartingWithImmediate(grammar, context, pInst); |
Dejan Mircevski | f79519c | 2015-09-11 00:43:11 -0400 | [diff] [blame] | 457 | } |
| 458 | |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 459 | // An assembly instruction has two possible formats: |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 460 | // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void". |
| 461 | // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid". |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 462 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 463 | std::string firstWord; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 464 | spv_position_t nextPosition = {}; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 465 | spv_result_t error = context->getWord(firstWord, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 466 | if (error) return context->diagnostic() << "Internal Error"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 467 | |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 468 | std::string opcodeName; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 469 | std::string result_id; |
| 470 | spv_position_t result_id_position = {}; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 471 | if (context->startsWithOp()) { |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 472 | opcodeName = firstWord; |
| 473 | } else { |
| 474 | // If the first word of this instruction is not an opcode, we must be |
| 475 | // processing AAF now. |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 476 | if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 477 | return context->diagnostic() |
| 478 | << "Expected <opcode> at the beginning of an instruction, found '" |
| 479 | << firstWord << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 480 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 481 | |
| 482 | result_id = firstWord; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 483 | if ('%' != result_id.front()) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 484 | return context->diagnostic() |
| 485 | << "Expected <opcode> or <result-id> at the beginning " |
| 486 | "of an instruction, found '" |
| 487 | << result_id << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 488 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 489 | result_id_position = context->position(); |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 490 | |
| 491 | // The '=' sign. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 492 | context->setPosition(nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 493 | if (context->advance()) |
| 494 | return context->diagnostic() << "Expected '=', found end of stream."; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 495 | std::string equal_sign; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 496 | error = context->getWord(equal_sign, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 497 | if ("=" != equal_sign) |
| 498 | return context->diagnostic() << "'=' expected after result id."; |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 499 | |
| 500 | // The <opcode> after the '=' sign. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 501 | context->setPosition(nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 502 | if (context->advance()) |
| 503 | return context->diagnostic() << "Expected opcode, found end of stream."; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 504 | error = context->getWord(opcodeName, &nextPosition); |
Lei Zhang | 6483bd7 | 2015-10-14 17:02:39 -0400 | [diff] [blame] | 505 | if (error) return context->diagnostic(error) << "Internal Error"; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 506 | if (!context->startsWithOp()) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 507 | return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName |
| 508 | << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 509 | } |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 510 | } |
| 511 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 512 | // NOTE: The table contains Opcode names without the "Op" prefix. |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 513 | const char* pInstName = opcodeName.data() + 2; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 514 | |
| 515 | spv_opcode_desc opcodeEntry; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 516 | error = grammar.lookupOpcode(pInstName, &opcodeEntry); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 517 | if (error) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 518 | return context->diagnostic(error) << "Invalid Opcode name '" |
| 519 | << context->getWord() << "'"; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 520 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 521 | if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) { |
| 522 | // If this instruction has <result-id>, check it follows AAF. |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 523 | if (opcodeEntry->hasResult && result_id.empty()) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 524 | return context->diagnostic() |
| 525 | << "Expected <result-id> at the beginning of an " |
| 526 | "instruction, found '" |
| 527 | << firstWord << "'."; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 528 | } |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 529 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 530 | pInst->opcode = opcodeEntry->opcode; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 531 | context->setPosition(nextPosition); |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 532 | // Reserve the first word for the instruction. |
| 533 | spvInstructionAddWord(pInst, 0); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 534 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 535 | // Maintains the ordered list of expected operand types. |
| 536 | // For many instructions we only need the {numTypes, operandTypes} |
| 537 | // entries in opcodeEntry. However, sometimes we need to modify |
| 538 | // the list as we parse the operands. This occurs when an operand |
| 539 | // has its own logical operands (such as the LocalSize operand for |
| 540 | // ExecutionMode), or for extended instructions that may have their |
| 541 | // own operands depending on the selected extended instruction. |
| 542 | spv_operand_pattern_t expectedOperands( |
| 543 | opcodeEntry->operandTypes, |
| 544 | opcodeEntry->operandTypes + opcodeEntry->numTypes); |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 545 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 546 | while (!expectedOperands.empty()) { |
| 547 | const spv_operand_type_t type = expectedOperands.front(); |
| 548 | expectedOperands.pop_front(); |
| 549 | |
| 550 | // Expand optional tuples lazily. |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 551 | if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 552 | |
| 553 | if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) { |
| 554 | // Handle the <result-id> for value generating instructions. |
| 555 | // We've already consumed it from the text stream. Here |
| 556 | // we inject its words into the instruction. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 557 | spv_position_t temp_pos = context->position(); |
| 558 | error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID, |
| 559 | result_id.c_str(), pInst, nullptr); |
| 560 | result_id_position = context->position(); |
| 561 | // Because we are injecting we have to reset the position afterwards. |
| 562 | context->setPosition(temp_pos); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 563 | if (error) return error; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 564 | } else { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 565 | // Find the next word. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 566 | error = context->advance(); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 567 | if (error == SPV_END_OF_STREAM) { |
| 568 | if (spvOperandIsOptional(type)) { |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 569 | // This would have been the last potential operand for the |
| 570 | // instruction, |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 571 | // and we didn't find one. We're finished parsing this instruction. |
| 572 | break; |
| 573 | } else { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 574 | return context->diagnostic() |
| 575 | << "Expected operand, found end of stream."; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 576 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 577 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 578 | assert(error == SPV_SUCCESS && "Somebody added another way to fail"); |
| 579 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 580 | if (context->isStartOfNewInst()) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 581 | if (spvOperandIsOptional(type)) { |
| 582 | break; |
| 583 | } else { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 584 | return context->diagnostic() |
| 585 | << "Expected operand, found next instruction instead."; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
| 589 | std::string operandValue; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 590 | error = context->getWord(operandValue, &nextPosition); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 591 | if (error) return context->diagnostic(error) << "Internal Error"; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 592 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 593 | error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(), |
| 594 | pInst, &expectedOperands); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 595 | |
| 596 | if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type)) |
| 597 | return SPV_SUCCESS; |
| 598 | |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 599 | if (error) return error; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 600 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 601 | context->setPosition(nextPosition); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 605 | if (spvOpcodeGeneratesType(pInst->opcode)) { |
| 606 | if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) { |
| 607 | return SPV_ERROR_INVALID_TEXT; |
| 608 | } |
| 609 | } else if (opcodeEntry->hasType) { |
| 610 | // SPIR-V dictates that if an instruction has both a return value and a |
| 611 | // type ID then the type id is first, and the return value is second. |
| 612 | assert(opcodeEntry->hasResult && |
| 613 | "Unknown opcode: has a type but no result."); |
| 614 | context->recordTypeIdForValue(pInst->words[2], pInst->words[1]); |
| 615 | } |
| 616 | |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 617 | if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 618 | return context->diagnostic() |
| 619 | << "Instruction too long: " << pInst->words.size() |
| 620 | << " words, but the limit is " |
| 621 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 625 | |
| 626 | return SPV_SUCCESS; |
| 627 | } |
| 628 | |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 629 | namespace { |
| 630 | |
David Neto | e4945de | 2015-10-28 13:50:32 -0400 | [diff] [blame] | 631 | /// @brief Populate a binary stream's words with this generator's header. |
David Neto | 0b98168 | 2015-10-27 15:51:34 -0400 | [diff] [blame] | 632 | /// |
David Neto | e4945de | 2015-10-28 13:50:32 -0400 | [diff] [blame] | 633 | /// @param[in,out] words the array of words |
David Neto | 0b98168 | 2015-10-27 15:51:34 -0400 | [diff] [blame] | 634 | /// @param[in] bound the upper ID bound |
| 635 | /// |
| 636 | /// @return result code |
David Neto | e4945de | 2015-10-28 13:50:32 -0400 | [diff] [blame] | 637 | spv_result_t SetHeader(uint32_t* words, const uint32_t bound) { |
| 638 | if (!words) return SPV_ERROR_INVALID_BINARY; |
David Neto | 0b98168 | 2015-10-27 15:51:34 -0400 | [diff] [blame] | 639 | |
David Neto | e4945de | 2015-10-28 13:50:32 -0400 | [diff] [blame] | 640 | words[SPV_INDEX_MAGIC_NUMBER] = SPV_MAGIC_NUMBER; |
| 641 | words[SPV_INDEX_VERSION_NUMBER] = SPV_VERSION_NUMBER; |
| 642 | words[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS; |
| 643 | words[SPV_INDEX_BOUND] = bound; |
| 644 | words[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved |
David Neto | 0b98168 | 2015-10-27 15:51:34 -0400 | [diff] [blame] | 645 | |
| 646 | return SPV_SUCCESS; |
| 647 | } |
| 648 | |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 649 | // Translates a given assembly language module into binary form. |
| 650 | // If a diagnostic is generated, it is not yet marked as being |
| 651 | // for a text-based input. |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 652 | spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar, |
| 653 | const spv_text text, |
| 654 | spv_assembly_syntax_format_t format, |
| 655 | spv_binary* pBinary, |
| 656 | spv_diagnostic* pDiagnostic) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 657 | if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC; |
| 658 | libspirv::AssemblyContext context(text, pDiagnostic); |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame^] | 659 | if (!text->str) return context.diagnostic() << "Missing assembly text."; |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 660 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 661 | if (!grammar.isValid()) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 662 | return SPV_ERROR_INVALID_TABLE; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 663 | } |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 664 | if (!pBinary) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 665 | |
| 666 | // NOTE: Ensure diagnostic is zero initialised |
| 667 | *pDiagnostic = {}; |
| 668 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 669 | std::vector<spv_instruction_t> instructions; |
| 670 | |
David Neto | ea633a6 | 2015-11-02 11:40:59 -0500 | [diff] [blame] | 671 | // Skip past whitespace and comments. |
| 672 | context.advance(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 673 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 674 | spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE; |
| 675 | while (context.hasText()) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 676 | instructions.push_back({}); |
| 677 | spv_instruction_t& inst = instructions.back(); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 678 | inst.extInstType = extInstType; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 679 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 680 | if (spvTextEncodeOpcode(grammar, &context, format, &inst)) { |
| 681 | return SPV_ERROR_INVALID_TEXT; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 682 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 683 | extInstType = inst.extInstType; |
| 684 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 685 | if (context.advance()) break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 688 | size_t totalSize = SPV_INDEX_INSTRUCTION; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 689 | for (auto& inst : instructions) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 690 | totalSize += inst.words.size(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 693 | uint32_t* data = new uint32_t[totalSize]; |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 694 | if (!data) return SPV_ERROR_OUT_OF_MEMORY; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 695 | uint64_t currentIndex = SPV_INDEX_INSTRUCTION; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 696 | for (auto& inst : instructions) { |
Lei Zhang | 6483bd7 | 2015-10-14 17:02:39 -0400 | [diff] [blame] | 697 | memcpy(data + currentIndex, inst.words.data(), |
| 698 | sizeof(uint32_t) * inst.words.size()); |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 699 | currentIndex += inst.words.size(); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 700 | } |
| 701 | |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame^] | 702 | if (auto error = SetHeader(data, context.getBound())) return error; |
David Neto | e4945de | 2015-10-28 13:50:32 -0400 | [diff] [blame] | 703 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 704 | spv_binary binary = new spv_binary_t(); |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 705 | if (!binary) { |
| 706 | delete[] data; |
| 707 | return SPV_ERROR_OUT_OF_MEMORY; |
| 708 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 709 | binary->code = data; |
| 710 | binary->wordCount = totalSize; |
| 711 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 712 | *pBinary = binary; |
| 713 | |
| 714 | return SPV_SUCCESS; |
| 715 | } |
| 716 | |
Lei Zhang | e78a7c1 | 2015-09-10 17:07:21 -0400 | [diff] [blame] | 717 | } // anonymous namespace |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 718 | |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 719 | spv_result_t spvTextToBinary(const char* input_text, |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 720 | const uint64_t input_text_size, |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 721 | const spv_opcode_table opcodeTable, |
| 722 | const spv_operand_table operandTable, |
| 723 | const spv_ext_inst_table extInstTable, |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 724 | spv_binary* pBinary, spv_diagnostic* pDiagnostic) { |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 725 | return spvTextWithFormatToBinary( |
| 726 | input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT, |
| 727 | opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic); |
| 728 | } |
| 729 | |
| 730 | spv_result_t spvTextWithFormatToBinary( |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 731 | const char* input_text, const uint64_t input_text_size, |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 732 | spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable, |
| 733 | const spv_operand_table operandTable, const spv_ext_inst_table extInstTable, |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 734 | spv_binary* pBinary, spv_diagnostic* pDiagnostic) { |
Andrew Woloszyn | cfeac48 | 2015-09-09 13:04:32 -0400 | [diff] [blame] | 735 | spv_text_t text = {input_text, input_text_size}; |
| 736 | |
Dejan Mircevski | 14c4b10 | 2015-09-29 17:07:21 -0400 | [diff] [blame] | 737 | libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable); |
Lei Zhang | 06efdc5 | 2015-09-10 14:00:00 -0400 | [diff] [blame] | 738 | spv_result_t result = |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 739 | spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic); |
David Neto | c978643 | 2015-09-01 18:05:14 -0400 | [diff] [blame] | 740 | if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true; |
| 741 | |
| 742 | return result; |
| 743 | } |
| 744 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 745 | void spvTextDestroy(spv_text text) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 746 | if (!text) return; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 747 | if (text->str) { |
| 748 | delete[] text->str; |
| 749 | } |
| 750 | delete text; |
| 751 | } |