blob: 5bc0b5dfbe545d1462e6e3595aca4dd867b5797a [file] [log] [blame]
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01001// 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 Neto36b0c0f2015-09-16 18:32:54 -040027#include "text.h"
28
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040029#include <algorithm>
30#include <cassert>
Andrew Woloszyn13804e52015-09-22 15:50:33 -040031#include <cctype>
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040032#include <cstdio>
33#include <cstdlib>
David Neto36b0c0f2015-09-16 18:32:54 -040034#include <cstring>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040035#include <memory>
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040036#include <string>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040037#include <sstream>
David Neto36b0c0f2015-09-16 18:32:54 -040038#include <unordered_map>
39#include <vector>
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040040
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010041#include "binary.h"
David Neto36b0c0f2015-09-16 18:32:54 -040042#include "bitwisecast.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010043#include "diagnostic.h"
44#include "ext_inst.h"
David Neto36b0c0f2015-09-16 18:32:54 -040045#include <libspirv/libspirv.h>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010046#include "opcode.h"
47#include "operand.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040048#include "text_handler.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010049
Lei Zhang610c5252015-09-09 10:36:48 -040050using spvutils::BitwiseCast;
51
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010052
Andrew Woloszyn13804e52015-09-22 15:50:33 -040053bool spvIsValidIDCharacter(const char value) {
54 return value == '_' || 0 != ::isalnum(value);
55}
56
57// Returns true if the given string represents a valid ID name.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040058bool spvIsValidID(const char *textValue) {
59 const char *c = textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -040060 for (; *c != '\0'; ++c) {
61 if (!spvIsValidIDCharacter(*c)) {
62 return false;
63 }
64 }
65 // If the string was empty, then the ID also is not valid.
66 return c != textValue;
67}
68
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040069// Text API
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010070
Dejan Mircevski903f9d62015-09-28 17:04:39 -040071spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010072 bool isSigned = false;
David Netoaffa6962015-08-24 15:33:14 -040073 int numPeriods = 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010074 bool isString = false;
75
David Netoaffa6962015-08-24 15:33:14 -040076 const size_t len = strlen(textValue);
David Neto98290a22015-08-24 16:27:02 -040077 if (len == 0) return SPV_FAILED_MATCH;
78
David Netoaffa6962015-08-24 15:33:14 -040079 for (uint64_t index = 0; index < len; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010080 switch (textValue[index]) {
81 case '0':
82 case '1':
83 case '2':
84 case '3':
85 case '4':
86 case '5':
87 case '6':
88 case '7':
89 case '8':
90 case '9':
91 break;
92 case '.':
David Netoaffa6962015-08-24 15:33:14 -040093 numPeriods++;
94 break;
95 case '-':
96 if (index == 0) {
97 isSigned = true;
98 } else {
99 isString = true;
100 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100101 break;
102 default:
103 isString = true;
Lei Zhange78a7c12015-09-10 17:07:21 -0400104 index = len; // break out of the loop too.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100105 break;
106 }
107 }
108
David Netoaffa6962015-08-24 15:33:14 -0400109 pLiteral->type = spv_literal_type_t(99);
110
Lei Zhange78a7c12015-09-10 17:07:21 -0400111 if (isString || numPeriods > 1 || (isSigned && len == 1)) {
David Netoaffa6962015-08-24 15:33:14 -0400112 // TODO(dneto): Allow escaping.
David Neto98290a22015-08-24 16:27:02 -0400113 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
114 return SPV_FAILED_MATCH;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100115 pLiteral->type = SPV_LITERAL_TYPE_STRING;
David Netoaffa6962015-08-24 15:33:14 -0400116 // Need room for the null-terminator.
David Neto98290a22015-08-24 16:27:02 -0400117 if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY;
Lei Zhange78a7c12015-09-10 17:07:21 -0400118 strncpy(pLiteral->value.str, textValue + 1, len - 2);
119 pLiteral->value.str[len - 2] = 0;
David Netoaffa6962015-08-24 15:33:14 -0400120 } else if (numPeriods == 1) {
121 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100122 float f = (float)d;
123 if (d == (double)f) {
124 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
125 pLiteral->value.f = f;
126 } else {
127 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
128 pLiteral->value.d = d;
129 }
130 } else if (isSigned) {
131 int64_t i64 = strtoll(textValue, nullptr, 10);
132 int32_t i32 = (int32_t)i64;
133 if (i64 == (int64_t)i32) {
134 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
135 pLiteral->value.i32 = i32;
136 } else {
137 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
138 pLiteral->value.i64 = i64;
139 }
140 } else {
141 uint64_t u64 = strtoull(textValue, nullptr, 10);
142 uint32_t u32 = (uint32_t)u64;
143 if (u64 == (uint64_t)u32) {
144 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
145 pLiteral->value.u32 = u32;
146 } else {
147 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
148 pLiteral->value.u64 = u64;
149 }
150 }
151
152 return SPV_SUCCESS;
153}
154
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400155/// @brief Translate an Opcode operand to binary form
156///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400157/// @param[in] grammar the grammar to use for compilation
158/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400159/// @param[in] type of the operand
160/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400161/// @param[out] pInst return binary Opcode
162/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400163///
164/// @return result code
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400165spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar &grammar,
166 libspirv::AssemblyContext* context,
167 const spv_operand_type_t type,
168 const char* textValue,
169 spv_instruction_t* pInst,
170 spv_operand_pattern_t* pExpectedOperands) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100171 // NOTE: Handle immediate int in the stream
172 if ('!' == textValue[0]) {
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400173 const char* begin = textValue + 1;
174 char* end = nullptr;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100175 uint32_t immediateInt = strtoul(begin, &end, 0);
176 size_t size = strlen(textValue);
177 size_t length = (end - begin);
Lei Zhang40056702015-09-11 14:31:27 -0400178 if (size - 1 != length) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400179 context->diagnostic() << "Invalid immediate integer '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400180 return SPV_ERROR_INVALID_TEXT;
181 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400182 context->seekForward(size);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100183 pInst->words[pInst->wordCount] = immediateInt;
184 pInst->wordCount += 1;
Dejan Mircevski897bff92015-09-29 10:38:18 -0400185 *pExpectedOperands =
186 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100187 return SPV_SUCCESS;
188 }
189
190 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400191 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Netoe3f70b92015-08-27 13:50:05 -0400192 case SPV_OPERAND_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400193 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
194 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netob14a7272015-09-25 13:56:09 -0400195 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
196 case SPV_OPERAND_TYPE_RESULT_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400197 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100198 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100199 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400200 context->diagnostic() << "Expected id to start with %.";
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400201 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100202 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400203 if (!spvIsValidID(textValue)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400204 context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400205 return SPV_ERROR_INVALID_TEXT;
206 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400207 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100208 pInst->words[pInst->wordCount++] = id;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100209 } break;
210 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
211 // NOTE: Special case for extension instruction lookup
212 if (OpExtInst == pInst->opcode) {
213 spv_ext_inst_desc extInst;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400214 if (grammar.lookupExtInst(pInst->extInstType, textValue,
215 &extInst)) {
216 context->diagnostic() << "Invalid extended instruction name '"
217 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400218 return SPV_ERROR_INVALID_TEXT;
219 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100220 pInst->words[pInst->wordCount++] = extInst->ext_inst;
David Neto78c3b432015-08-27 13:03:52 -0400221
222 // Prepare to parse the operands for the extended instructions.
223 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
224
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100225 return SPV_SUCCESS;
226 }
Lei Zhang6d415812015-09-15 13:36:21 -0400227 } // Fall through for the general case.
Lei Zhangb41d1502015-09-14 15:22:23 -0400228 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
David Neto561dc4e2015-09-25 14:23:29 -0400229 case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE:
230 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100231 spv_literal_t literal = {};
Lei Zhang6d415812015-09-15 13:36:21 -0400232 spv_result_t error = spvTextToLiteral(textValue, &literal);
233 if (error != SPV_SUCCESS) {
234 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
235 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400236 context->diagnostic() << "Invalid literal number '" << textValue << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400237 return SPV_ERROR_INVALID_TEXT;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400238 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100239 switch (literal.type) {
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400240 // We do not have to print diagnostics here because spvBinaryEncode*
241 // prints diagnostic messages on failure.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100242 case SPV_LITERAL_TYPE_INT_32:
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400243 if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32),
244 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400245 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100246 break;
247 case SPV_LITERAL_TYPE_INT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400248 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64),
249 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400250 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100251 } break;
252 case SPV_LITERAL_TYPE_UINT_32: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400253 if (context->binaryEncodeU32(literal.value.u32, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400254 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100255 } break;
256 case SPV_LITERAL_TYPE_UINT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400257 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64),
258 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400259 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100260 } break;
261 case SPV_LITERAL_TYPE_FLOAT_32: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400262 if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f),
263 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400264 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100265 } break;
266 case SPV_LITERAL_TYPE_FLOAT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400267 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d),
268 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400269 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100270 } break;
271 case SPV_LITERAL_TYPE_STRING: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400272 context->diagnostic() << "Expected literal number, found literal string '"
273 << textValue << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400274 return SPV_FAILED_MATCH;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100275 } break;
276 default:
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400277 context->diagnostic() << "Invalid literal number '" << textValue << "'";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100278 return SPV_ERROR_INVALID_TEXT;
279 }
280 } break;
David Neto78c3b432015-08-27 13:03:52 -0400281 case SPV_OPERAND_TYPE_LITERAL_STRING:
282 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400283 spv_literal_t literal = {};
284 spv_result_t error = spvTextToLiteral(textValue, &literal);
285 if (error != SPV_SUCCESS) {
286 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
Lei Zhang40056702015-09-11 14:31:27 -0400287 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400288 context->diagnostic() << "Invalid literal string '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400289 return SPV_ERROR_INVALID_TEXT;
290 }
Lei Zhang6d415812015-09-15 13:36:21 -0400291 if (literal.type != SPV_LITERAL_TYPE_STRING) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400292 context->diagnostic() << "Expected literal string, found literal number '"
293 << textValue << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400294 return SPV_FAILED_MATCH;
295 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100296
297 // NOTE: Special case for extended instruction library import
298 if (OpExtInstImport == pInst->opcode) {
Lei Zhang6d415812015-09-15 13:36:21 -0400299 pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100300 }
301
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400302 if (context->binaryEncodeString(literal.value.str, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400303 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100304 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400305 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
306 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
307 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400308 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400309 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400310 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
311 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400312 if (grammar.parseMaskOperand(type, textValue, &value)) {
313 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
314 << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400315 return SPV_ERROR_INVALID_TEXT;
316 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400317 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400318 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400319 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400320 } break;
321 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
322 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400323 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
324 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400325 if (error == SPV_FAILED_MATCH) {
326 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400327 error = spvTextEncodeOperand(grammar, context,
328 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
329 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400330 }
331 if (error == SPV_FAILED_MATCH) {
332 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400333 error =
334 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
335 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400336 }
337 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400338 context->diagnostic() << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400339 return error;
340 }
341 if (pExpectedOperands->empty()) {
342 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
343 }
David Neto36b0c0f2015-09-16 18:32:54 -0400344 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100345 default: {
346 // NOTE: All non literal operands are handled here using the operand
347 // table.
348 spv_operand_desc entry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400349 if (grammar.lookupOperand(type, textValue, strlen(textValue),
350 &entry)) {
351 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
352 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400353 return SPV_ERROR_INVALID_TEXT;
354 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400355 if (context->binaryEncodeU32(entry->value, pInst)) {
356 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
357 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400358 return SPV_ERROR_INVALID_TEXT;
359 }
David Neto78c3b432015-08-27 13:03:52 -0400360
361 // Prepare to parse the operands for this logical operand.
362 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100363 } break;
364 }
365 return SPV_SUCCESS;
366}
367
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400368namespace {
369
370/// Encodes an instruction started by !<integer> at the given position in text.
371///
372/// Puts the encoded words into *pInst. If successful, moves position past the
373/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
374/// leaves position pointing to the error in text.
375spv_result_t encodeInstructionStartingWithImmediate(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400376 const libspirv::AssemblyGrammar &grammar,
377 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400378 std::string firstWord;
379 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400380 auto error = context->getWord(firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400381 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400382 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400383 return error;
384 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400385
386 assert(firstWord[0] == '!');
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400387 const char* begin = firstWord.data() + 1;
388 char* end = nullptr;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400389 uint32_t immediateInt = strtoul(begin, &end, 0);
Lei Zhang40056702015-09-11 14:31:27 -0400390 if ((begin + firstWord.size() - 1) != end) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400391 context->diagnostic() << "Invalid immediate integer '" << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400392 return SPV_ERROR_INVALID_TEXT;
393 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400394 context->seekForward(firstWord.size());
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400395 pInst->words[0] = immediateInt;
396 pInst->wordCount = 1;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400397 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400398 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400399 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400400
401 // Otherwise, there must be an operand that's either a literal, an ID, or
402 // an immediate.
403 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400404 if ((error = context->getWord(operandValue, &nextPosition))) {
405 context->diagnostic() << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400406 return error;
407 }
408
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400409 if (operandValue == "=") {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400410 context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400411 return SPV_ERROR_INVALID_TEXT;
412 }
413
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400414 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
415 // expanded.
416 spv_operand_pattern_t dummyExpectedOperands;
417 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400418 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
419 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400420 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400421 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400422 }
423 return SPV_SUCCESS;
424}
425
426} // anonymous namespace
427
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400428/// @brief Translate single Opcode and operands to binary form
429///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400430/// @param[in] grammar the grammar to use for compilation
431/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400432/// @param[in] text stream to translate
433/// @param[in] format the assembly syntax format of text
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400434/// @param[out] pInst returned binary Opcode
435/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400436///
437/// @return result code
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400438spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar &grammar,
439 libspirv::AssemblyContext* context,
440 spv_assembly_syntax_format_t format,
441 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400442 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400443 if ('!' == context->peek()) {
444 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400445 }
446
Lei Zhangdfc50082015-08-21 11:50:55 -0400447 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400448 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
449 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400450
Lei Zhang06efdc52015-09-10 14:00:00 -0400451 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100452 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400453 spv_result_t error = context->getWord(firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400454 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400455 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400456 return error;
457 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100458
Lei Zhang06efdc52015-09-10 14:00:00 -0400459 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400460 std::string result_id;
461 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400462 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400463 opcodeName = firstWord;
464 } else {
465 // If the first word of this instruction is not an opcode, we must be
466 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400467 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400468 context->diagnostic()
Lei Zhang40056702015-09-11 14:31:27 -0400469 << "Expected <opcode> at the beginning of an instruction, found '"
470 << firstWord << "'.";
471 return SPV_ERROR_INVALID_TEXT;
472 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400473
474 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400475 if ('%' != result_id.front()) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400476 context->diagnostic() << "Expected <opcode> or <result-id> at the beginning "
477 "of an instruction, found '"
478 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400479 return SPV_ERROR_INVALID_TEXT;
480 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400481 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400482
483 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400484 context->setPosition(nextPosition);
485 if (context->advance()) {
486 context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhang40056702015-09-11 14:31:27 -0400487 return SPV_ERROR_INVALID_TEXT;
488 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400489 std::string equal_sign;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400490 error = context->getWord(equal_sign, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400491 if ("=" != equal_sign) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400492 context->diagnostic() << "'=' expected after result id.";
Lei Zhang40056702015-09-11 14:31:27 -0400493 return SPV_ERROR_INVALID_TEXT;
494 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400495
496 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400497 context->setPosition(nextPosition);
498 if (context->advance()) {
499 context->diagnostic() << "Expected opcode, found end of stream.";
Lei Zhang40056702015-09-11 14:31:27 -0400500 return SPV_ERROR_INVALID_TEXT;
501 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400502 error = context->getWord(opcodeName, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400503 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400504 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400505 return error;
506 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400507 if (!context->startsWithOp()) {
508 context->diagnostic() << "Invalid Opcode prefix '" << opcodeName << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400509 return SPV_ERROR_INVALID_TEXT;
510 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400511 }
512
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100513 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400514 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100515
516 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400517 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400518 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400519 context->diagnostic() << "Invalid Opcode name '" << context->getWord() << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400520 return error;
521 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400522 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
523 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400524 if (opcodeEntry->hasResult && result_id.empty()) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400525 context->diagnostic() << "Expected <result-id> at the beginning of an "
526 "instruction, found '"
527 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400528 return SPV_ERROR_INVALID_TEXT;
529 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400530 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100531 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400532 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100533 pInst->wordCount++;
534
David Neto78c3b432015-08-27 13:03:52 -0400535 // 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 Zhangdfc50082015-08-21 11:50:55 -0400545
David Neto78c3b432015-08-27 13:03:52 -0400546 while (!expectedOperands.empty()) {
547 const spv_operand_type_t type = expectedOperands.front();
548 expectedOperands.pop_front();
549
550 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400551 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400552
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 Woloszyn71fc0552015-09-24 10:26:51 -0400557 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 Zhang40056702015-09-11 14:31:27 -0400563 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100564 } else {
David Neto78c3b432015-08-27 13:03:52 -0400565 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400566 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400567 if (error == SPV_END_OF_STREAM) {
568 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400569 // This would have been the last potential operand for the
570 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400571 // and we didn't find one. We're finished parsing this instruction.
572 break;
573 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400574 context->diagnostic() << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400575 return SPV_ERROR_INVALID_TEXT;
576 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100577 }
David Neto78c3b432015-08-27 13:03:52 -0400578 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
579
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400580 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400581 if (spvOperandIsOptional(type)) {
582 break;
583 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400584 context->diagnostic()
585 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400586 return SPV_ERROR_INVALID_TEXT;
587 }
588 }
589
590 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400591 error = context->getWord(operandValue, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400592 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400593 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400594 return error;
595 }
David Neto78c3b432015-08-27 13:03:52 -0400596
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400597 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
598 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400599
600 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
601 return SPV_SUCCESS;
602
Lei Zhang40056702015-09-11 14:31:27 -0400603 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400604
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400605 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100606 }
607 }
608
609 pInst->words[0] = spvOpcodeMake(pInst->wordCount, opcodeEntry->opcode);
610
611 return SPV_SUCCESS;
612}
613
David Netoc9786432015-09-01 18:05:14 -0400614namespace {
615
616// Translates a given assembly language module into binary form.
617// If a diagnostic is generated, it is not yet marked as being
618// for a text-based input.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400619spv_result_t spvTextToBinaryInternal(
620 const libspirv::AssemblyGrammar &grammar, const spv_text text,
621 spv_assembly_syntax_format_t format, spv_binary* pBinary,
622 spv_diagnostic* pDiagnostic) {
623 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
624 libspirv::AssemblyContext context(text, pDiagnostic);
Lei Zhang40056702015-09-11 14:31:27 -0400625 if (!text->str || !text->length) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400626 context.diagnostic() << "Text stream is empty.";
Lei Zhang40056702015-09-11 14:31:27 -0400627 return SPV_ERROR_INVALID_TEXT;
628 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400629 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400630 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400631 }
Lei Zhang40056702015-09-11 14:31:27 -0400632 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100633
634 // NOTE: Ensure diagnostic is zero initialised
635 *pDiagnostic = {};
636
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100637 std::vector<spv_instruction_t> instructions;
638
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400639 if (context.advance()) {
640 context.diagnostic() << "Text stream is empty.";
Lei Zhang40056702015-09-11 14:31:27 -0400641 return SPV_ERROR_INVALID_TEXT;
642 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100643
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400644 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
645 while (context.hasText()) {
646 spv_instruction_t inst = {};
647 inst.extInstType = extInstType;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100648
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400649 if (spvTextEncodeOpcode(grammar, &context, format, &inst)) {
650 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400651 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400652 extInstType = inst.extInstType;
653
654 instructions.push_back(inst);
655
656 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100657 }
658
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100659 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400660 for (auto& inst : instructions) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100661 totalSize += inst.wordCount;
662 }
663
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400664 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400665 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100666 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400667 for (auto& inst : instructions) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100668 memcpy(data + currentIndex, inst.words, sizeof(uint32_t) * inst.wordCount);
669 currentIndex += inst.wordCount;
670 }
671
672 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400673 if (!binary) {
674 delete[] data;
675 return SPV_ERROR_OUT_OF_MEMORY;
676 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100677 binary->code = data;
678 binary->wordCount = totalSize;
679
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400680 spv_result_t error = spvBinaryHeaderSet(binary, context.getBound());
Lei Zhang40056702015-09-11 14:31:27 -0400681 if (error) {
682 spvBinaryDestroy(binary);
683 return error;
684 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100685
686 *pBinary = binary;
687
688 return SPV_SUCCESS;
689}
690
Lei Zhange78a7c12015-09-10 17:07:21 -0400691} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400692
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400693spv_result_t spvTextToBinary(const char* input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400694 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400695 const spv_opcode_table opcodeTable,
696 const spv_operand_table operandTable,
697 const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400698 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400699 return spvTextWithFormatToBinary(
700 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
701 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
702}
703
704spv_result_t spvTextWithFormatToBinary(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400705 const char* input_text, const uint64_t input_text_size,
Lei Zhang06efdc52015-09-10 14:00:00 -0400706 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
707 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400708 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400709 spv_text_t text = {input_text, input_text_size};
710
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400711 libspirv::AssemblyGrammar grammar(operandTable, opcodeTable,
712 extInstTable);
Lei Zhang06efdc52015-09-10 14:00:00 -0400713 spv_result_t result =
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400714 spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400715 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
716
717 return result;
718}
719
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100720void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400721 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100722 if (text->str) {
723 delete[] text->str;
724 }
725 delete text;
726}