blob: 388ebdbf015330e9559335e173eb5d8d14ca1a4f [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"
42#include "diagnostic.h"
43#include "ext_inst.h"
David Netob5dc8fc2015-10-06 16:22:00 -040044#include "instruction.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"
Andrew Woloszynf731cbf2015-10-23 09:53:58 -040049#include "util/bitutils.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010050
Andrew Woloszyn13804e52015-09-22 15:50:33 -040051bool spvIsValidIDCharacter(const char value) {
52 return value == '_' || 0 != ::isalnum(value);
53}
54
55// Returns true if the given string represents a valid ID name.
Dejan Mircevski14c4b102015-09-29 17:07:21 -040056bool spvIsValidID(const char* textValue) {
57 const char* c = textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -040058 for (; *c != '\0'; ++c) {
59 if (!spvIsValidIDCharacter(*c)) {
60 return false;
61 }
62 }
63 // If the string was empty, then the ID also is not valid.
64 return c != textValue;
65}
66
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040067// Text API
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010068
Dejan Mircevski903f9d62015-09-28 17:04:39 -040069spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010070 bool isSigned = false;
David Netoaffa6962015-08-24 15:33:14 -040071 int numPeriods = 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010072 bool isString = false;
73
David Netoaffa6962015-08-24 15:33:14 -040074 const size_t len = strlen(textValue);
David Neto98290a22015-08-24 16:27:02 -040075 if (len == 0) return SPV_FAILED_MATCH;
76
David Netoaffa6962015-08-24 15:33:14 -040077 for (uint64_t index = 0; index < len; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010078 switch (textValue[index]) {
79 case '0':
80 case '1':
81 case '2':
82 case '3':
83 case '4':
84 case '5':
85 case '6':
86 case '7':
87 case '8':
88 case '9':
89 break;
90 case '.':
David Netoaffa6962015-08-24 15:33:14 -040091 numPeriods++;
92 break;
93 case '-':
94 if (index == 0) {
95 isSigned = true;
96 } else {
97 isString = true;
98 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010099 break;
100 default:
101 isString = true;
Lei Zhange78a7c12015-09-10 17:07:21 -0400102 index = len; // break out of the loop too.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100103 break;
104 }
105 }
106
David Netoaffa6962015-08-24 15:33:14 -0400107 pLiteral->type = spv_literal_type_t(99);
108
Lei Zhange78a7c12015-09-10 17:07:21 -0400109 if (isString || numPeriods > 1 || (isSigned && len == 1)) {
David Neto98290a22015-08-24 16:27:02 -0400110 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
111 return SPV_FAILED_MATCH;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400112 bool escaping = false;
113 size_t write_index = 0;
Lei Zhang6483bd72015-10-14 17:02:39 -0400114 for (const char* val = textValue + 1; val != textValue + len - 1; ++val) {
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400115 if ((*val == '\\') && (!escaping)) {
116 escaping = true;
117 } else {
118 // Have to save space for the null-terminator
119 if (write_index >= sizeof(pLiteral->value.str) - 1)
120 return SPV_ERROR_OUT_OF_MEMORY;
121 pLiteral->value.str[write_index] = *val;
122 escaping = false;
123 ++write_index;
124 }
125 }
126
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100127 pLiteral->type = SPV_LITERAL_TYPE_STRING;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400128 pLiteral->value.str[write_index] = '\0';
David Netoaffa6962015-08-24 15:33:14 -0400129 } else if (numPeriods == 1) {
130 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100131 float f = (float)d;
132 if (d == (double)f) {
133 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
134 pLiteral->value.f = f;
135 } else {
136 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
137 pLiteral->value.d = d;
138 }
139 } else if (isSigned) {
140 int64_t i64 = strtoll(textValue, nullptr, 10);
141 int32_t i32 = (int32_t)i64;
142 if (i64 == (int64_t)i32) {
143 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
144 pLiteral->value.i32 = i32;
145 } else {
146 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
147 pLiteral->value.i64 = i64;
148 }
149 } else {
150 uint64_t u64 = strtoull(textValue, nullptr, 10);
151 uint32_t u32 = (uint32_t)u64;
152 if (u64 == (uint64_t)u32) {
153 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
154 pLiteral->value.u32 = u32;
155 } else {
156 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
157 pLiteral->value.u64 = u64;
158 }
159 }
160
161 return SPV_SUCCESS;
162}
163
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400164namespace {
165
166/// Parses an immediate integer from text, guarding against overflow. If
167/// successful, adds the parsed value to pInst, advances the context past it,
168/// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics,
169/// and returns SPV_ERROR_INVALID_TEXT.
170spv_result_t encodeImmediate(libspirv::AssemblyContext* context,
171 const char* text, spv_instruction_t* pInst) {
172 assert(*text == '!');
David Neto78e677b2015-10-05 13:28:46 -0400173 uint32_t parse_result;
174 if (auto error =
David Neto51013d12015-10-14 11:31:51 -0400175 context->parseNumber(text + 1, SPV_ERROR_INVALID_TEXT, &parse_result,
David Neto78e677b2015-10-05 13:28:46 -0400176 "Invalid immediate integer: !"))
177 return error;
178 context->binaryEncodeU32(parse_result, pInst);
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400179 context->seekForward(strlen(text));
180 return SPV_SUCCESS;
181}
182
183} // anonymous namespace
184
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400185/// @brief Translate an Opcode operand to binary form
186///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400187/// @param[in] grammar the grammar to use for compilation
188/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400189/// @param[in] type of the operand
190/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400191/// @param[out] pInst return binary Opcode
192/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400193///
194/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400195spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400196 libspirv::AssemblyContext* context,
197 const spv_operand_type_t type,
198 const char* textValue,
199 spv_instruction_t* pInst,
200 spv_operand_pattern_t* pExpectedOperands) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100201 // NOTE: Handle immediate int in the stream
202 if ('!' == textValue[0]) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400203 if (auto error = encodeImmediate(context, textValue, pInst)) {
204 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400205 }
Dejan Mircevski897bff92015-09-29 10:38:18 -0400206 *pExpectedOperands =
207 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100208 return SPV_SUCCESS;
209 }
210
David Neto51013d12015-10-14 11:31:51 -0400211 // Optional literal operands can fail to parse. In that case use
212 // SPV_FAILED_MATCH to avoid emitting a diagostic. Use the following
213 // for those situations.
214 spv_result_t error_code_for_literals =
215 spvOperandIsOptional(type) ? SPV_FAILED_MATCH : SPV_ERROR_INVALID_TEXT;
216
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100217 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400218 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Netoe3f70b92015-08-27 13:50:05 -0400219 case SPV_OPERAND_TYPE_ID:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400220 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400221 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
222 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netob14a7272015-09-25 13:56:09 -0400223 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
224 case SPV_OPERAND_TYPE_RESULT_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400225 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100226 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100227 } else {
David Netoac508b02015-10-09 15:48:09 -0400228 return context->diagnostic() << "Expected id to start with %.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100229 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400230 if (!spvIsValidID(textValue)) {
David Netoac508b02015-10-09 15:48:09 -0400231 return context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400232 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400233 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400234 if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
David Netob5dc8fc2015-10-06 16:22:00 -0400235 spvInstructionAddWord(pInst, id);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100236 } break;
David Neto445ce442015-10-15 15:22:06 -0400237 case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
238 // The assembler accepts the symbolic name for an extended instruction,
239 // and emits its corresponding number.
240 spv_ext_inst_desc extInst;
241 if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
242 return context->diagnostic() << "Invalid extended instruction name '"
243 << textValue << "'.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100244 }
David Neto445ce442015-10-15 15:22:06 -0400245 spvInstructionAddWord(pInst, extInst->ext_inst);
246
247 // Prepare to parse the operands for the extended instructions.
248 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
249
250 return SPV_SUCCESS;
251 } break;
252 case SPV_OPERAND_TYPE_LITERAL_INTEGER:
Lei Zhangb41d1502015-09-14 15:22:23 -0400253 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
Lei Zhang6483bd72015-10-14 17:02:39 -0400254 case SPV_OPERAND_TYPE_LITERAL_INTEGER_IN_OPTIONAL_TUPLE:
255 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
256 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: {
David Neto78e677b2015-10-05 13:28:46 -0400257 libspirv::IdType expected_type = libspirv::kUnknownType;
Lei Zhang6483bd72015-10-14 17:02:39 -0400258 if (type != SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER) {
259 // From now, it's safe to assume that the current operand is an unsigned
260 // integer, apart from the special cases handled below.
261 expected_type = {32, false, libspirv::IdTypeClass::kScalarIntegerType};
262 // The encoding for OpConstant, OpSpecConstant and OpSwitch all
263 // depend on either their own result-id or the result-id of
264 // one of their parameters.
265 if (OpConstant == pInst->opcode || OpSpecConstant == pInst->opcode) {
266 // Special cases for encoding possibly non-32-bit literals here.
267 expected_type =
268 context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
269 if (!libspirv::isScalarFloating(expected_type) &&
270 !libspirv::isScalarIntegral(expected_type)) {
271 spv_opcode_desc d;
272 const char* opcode_name = "opcode";
273 if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
274 opcode_name = d->name;
275 }
276 return context->diagnostic()
277 << "Type for " << opcode_name
278 << " must be a scalar floating point or integer type";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400279 }
Lei Zhang6483bd72015-10-14 17:02:39 -0400280 } else if (pInst->opcode == OpSwitch) {
281 // We need to know the type of the selector.
282 expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
283 if (!libspirv::isScalarIntegral(expected_type)) {
284 context->diagnostic()
285 << "The selector operand for OpSwitch must be the result"
286 " of an instruction that generates an integer scalar";
287 return SPV_ERROR_INVALID_TEXT;
288 }
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400289 }
290 }
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400291
David Neto78e677b2015-10-05 13:28:46 -0400292 if (auto error = context->binaryEncodeNumericLiteral(
David Neto51013d12015-10-14 11:31:51 -0400293 textValue, error_code_for_literals, expected_type, pInst)) {
David Neto78e677b2015-10-05 13:28:46 -0400294 return error;
David Neto51013d12015-10-14 11:31:51 -0400295 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100296 } break;
David Neto78c3b432015-08-27 13:03:52 -0400297 case SPV_OPERAND_TYPE_LITERAL_STRING:
298 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400299 spv_literal_t literal = {};
300 spv_result_t error = spvTextToLiteral(textValue, &literal);
301 if (error != SPV_SUCCESS) {
302 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
David Neto51013d12015-10-14 11:31:51 -0400303 return context->diagnostic(error_code_for_literals)
304 << "Invalid literal string '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400305 }
Lei Zhang6d415812015-09-15 13:36:21 -0400306 if (literal.type != SPV_LITERAL_TYPE_STRING) {
David Neto51013d12015-10-14 11:31:51 -0400307 return context->diagnostic()
David Netoac508b02015-10-09 15:48:09 -0400308 << "Expected literal string, found literal number '" << textValue
309 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400310 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100311
312 // NOTE: Special case for extended instruction library import
313 if (OpExtInstImport == pInst->opcode) {
Lei Zhang6d415812015-09-15 13:36:21 -0400314 pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100315 }
316
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400317 if (context->binaryEncodeString(literal.value.str, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400318 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100319 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400320 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
321 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
322 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400323 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400324 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400325 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
326 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400327 if (grammar.parseMaskOperand(type, textValue, &value)) {
David Netoac508b02015-10-09 15:48:09 -0400328 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
329 << " '" << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400330 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400331 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400332 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400333 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400334 } break;
335 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
336 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400337 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
338 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400339 if (error == SPV_FAILED_MATCH) {
340 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400341 error = spvTextEncodeOperand(grammar, context,
342 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
343 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400344 }
345 if (error == SPV_FAILED_MATCH) {
346 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400347 error =
348 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
349 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400350 }
351 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400352 return context->diagnostic(error)
353 << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400354 }
355 if (pExpectedOperands->empty()) {
356 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
357 }
David Neto36b0c0f2015-09-16 18:32:54 -0400358 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100359 default: {
360 // NOTE: All non literal operands are handled here using the operand
361 // table.
362 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400363 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
David Netoac508b02015-10-09 15:48:09 -0400364 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
365 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400366 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400367 if (context->binaryEncodeU32(entry->value, pInst)) {
David Netoac508b02015-10-09 15:48:09 -0400368 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
369 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400370 }
David Neto78c3b432015-08-27 13:03:52 -0400371
372 // Prepare to parse the operands for this logical operand.
373 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100374 } break;
375 }
376 return SPV_SUCCESS;
377}
378
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400379namespace {
380
381/// Encodes an instruction started by !<integer> at the given position in text.
382///
383/// Puts the encoded words into *pInst. If successful, moves position past the
384/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
385/// leaves position pointing to the error in text.
386spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400387 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400388 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400389 std::string firstWord;
390 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400391 auto error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400392 if (error) return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400393
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400394 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
395 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400396 }
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;
David Netoac508b02015-10-09 15:48:09 -0400404 if ((error = context->getWord(operandValue, &nextPosition)))
405 return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400406
David Netoac508b02015-10-09 15:48:09 -0400407 if (operandValue == "=")
408 return context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400409
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400410 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
411 // expanded.
412 spv_operand_pattern_t dummyExpectedOperands;
413 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400414 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
415 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400416 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400417 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400418 }
419 return SPV_SUCCESS;
420}
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400421} // anonymous namespace
422
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400423/// @brief Translate single Opcode and operands to binary form
424///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400425/// @param[in] grammar the grammar to use for compilation
426/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400427/// @param[in] text stream to translate
428/// @param[in] format the assembly syntax format of text
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400429/// @param[out] pInst returned binary Opcode
430/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400431///
432/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400433spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400434 libspirv::AssemblyContext* context,
435 spv_assembly_syntax_format_t format,
436 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400437 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400438 if ('!' == context->peek()) {
439 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400440 }
441
Lei Zhangdfc50082015-08-21 11:50:55 -0400442 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400443 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
444 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400445
Lei Zhang06efdc52015-09-10 14:00:00 -0400446 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100447 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400448 spv_result_t error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400449 if (error) return context->diagnostic() << "Internal Error";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100450
Lei Zhang06efdc52015-09-10 14:00:00 -0400451 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400452 std::string result_id;
453 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400454 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400455 opcodeName = firstWord;
456 } else {
457 // If the first word of this instruction is not an opcode, we must be
458 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400459 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
David Netoac508b02015-10-09 15:48:09 -0400460 return context->diagnostic()
461 << "Expected <opcode> at the beginning of an instruction, found '"
462 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400463 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400464
465 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400466 if ('%' != result_id.front()) {
David Netoac508b02015-10-09 15:48:09 -0400467 return context->diagnostic()
468 << "Expected <opcode> or <result-id> at the beginning "
469 "of an instruction, found '"
470 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400471 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400472 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400473
474 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400475 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400476 if (context->advance())
477 return context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400478 std::string equal_sign;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400479 error = context->getWord(equal_sign, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400480 if ("=" != equal_sign)
481 return context->diagnostic() << "'=' expected after result id.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400482
483 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400484 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400485 if (context->advance())
486 return context->diagnostic() << "Expected opcode, found end of stream.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400487 error = context->getWord(opcodeName, &nextPosition);
Lei Zhang6483bd72015-10-14 17:02:39 -0400488 if (error) return context->diagnostic(error) << "Internal Error";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400489 if (!context->startsWithOp()) {
David Netoac508b02015-10-09 15:48:09 -0400490 return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName
491 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400492 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400493 }
494
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100495 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400496 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100497
498 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400499 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400500 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400501 return context->diagnostic(error) << "Invalid Opcode name '"
502 << context->getWord() << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400503 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400504 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
505 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400506 if (opcodeEntry->hasResult && result_id.empty()) {
David Netoac508b02015-10-09 15:48:09 -0400507 return context->diagnostic()
508 << "Expected <result-id> at the beginning of an "
509 "instruction, found '"
510 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400511 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400512 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100513 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400514 context->setPosition(nextPosition);
David Netob5dc8fc2015-10-06 16:22:00 -0400515 // Reserve the first word for the instruction.
516 spvInstructionAddWord(pInst, 0);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100517
David Neto78c3b432015-08-27 13:03:52 -0400518 // Maintains the ordered list of expected operand types.
519 // For many instructions we only need the {numTypes, operandTypes}
520 // entries in opcodeEntry. However, sometimes we need to modify
521 // the list as we parse the operands. This occurs when an operand
522 // has its own logical operands (such as the LocalSize operand for
523 // ExecutionMode), or for extended instructions that may have their
524 // own operands depending on the selected extended instruction.
525 spv_operand_pattern_t expectedOperands(
526 opcodeEntry->operandTypes,
527 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400528
David Neto78c3b432015-08-27 13:03:52 -0400529 while (!expectedOperands.empty()) {
530 const spv_operand_type_t type = expectedOperands.front();
531 expectedOperands.pop_front();
532
533 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400534 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400535
536 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
537 // Handle the <result-id> for value generating instructions.
538 // We've already consumed it from the text stream. Here
539 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400540 spv_position_t temp_pos = context->position();
541 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
542 result_id.c_str(), pInst, nullptr);
543 result_id_position = context->position();
544 // Because we are injecting we have to reset the position afterwards.
545 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400546 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100547 } else {
David Neto78c3b432015-08-27 13:03:52 -0400548 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400549 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400550 if (error == SPV_END_OF_STREAM) {
551 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400552 // This would have been the last potential operand for the
553 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400554 // and we didn't find one. We're finished parsing this instruction.
555 break;
556 } else {
David Netoac508b02015-10-09 15:48:09 -0400557 return context->diagnostic()
558 << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400559 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100560 }
David Neto78c3b432015-08-27 13:03:52 -0400561 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
562
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400563 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400564 if (spvOperandIsOptional(type)) {
565 break;
566 } else {
David Netoac508b02015-10-09 15:48:09 -0400567 return context->diagnostic()
568 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400569 }
570 }
571
572 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400573 error = context->getWord(operandValue, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400574 if (error) return context->diagnostic(error) << "Internal Error";
David Neto78c3b432015-08-27 13:03:52 -0400575
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400576 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
577 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400578
579 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
580 return SPV_SUCCESS;
581
Lei Zhang40056702015-09-11 14:31:27 -0400582 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400583
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400584 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100585 }
586 }
587
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400588 if (spvOpcodeGeneratesType(pInst->opcode)) {
589 if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
590 return SPV_ERROR_INVALID_TEXT;
591 }
592 } else if (opcodeEntry->hasType) {
593 // SPIR-V dictates that if an instruction has both a return value and a
594 // type ID then the type id is first, and the return value is second.
595 assert(opcodeEntry->hasResult &&
596 "Unknown opcode: has a type but no result.");
597 context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
598 }
599
David Netob5dc8fc2015-10-06 16:22:00 -0400600 if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Netoac508b02015-10-09 15:48:09 -0400601 return context->diagnostic()
602 << "Instruction too long: " << pInst->words.size()
603 << " words, but the limit is "
604 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
David Netob5dc8fc2015-10-06 16:22:00 -0400605 }
606
607 pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100608
609 return SPV_SUCCESS;
610}
611
David Netoc9786432015-09-01 18:05:14 -0400612namespace {
613
614// Translates a given assembly language module into binary form.
615// If a diagnostic is generated, it is not yet marked as being
616// for a text-based input.
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400617spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
618 const spv_text text,
619 spv_assembly_syntax_format_t format,
620 spv_binary* pBinary,
621 spv_diagnostic* pDiagnostic) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400622 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
623 libspirv::AssemblyContext context(text, pDiagnostic);
David Netoac508b02015-10-09 15:48:09 -0400624 if (!text->str || !text->length)
625 return context.diagnostic() << "Text stream is empty.";
626
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400627 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400628 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400629 }
Lei Zhang40056702015-09-11 14:31:27 -0400630 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100631
632 // NOTE: Ensure diagnostic is zero initialised
633 *pDiagnostic = {};
634
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100635 std::vector<spv_instruction_t> instructions;
636
David Netoac508b02015-10-09 15:48:09 -0400637 if (context.advance()) return context.diagnostic() << "Text stream is empty.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100638
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400639 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
640 while (context.hasText()) {
David Netob5dc8fc2015-10-06 16:22:00 -0400641 instructions.push_back({});
642 spv_instruction_t& inst = instructions.back();
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400643 inst.extInstType = extInstType;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100644
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400645 if (spvTextEncodeOpcode(grammar, &context, format, &inst)) {
646 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400647 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400648 extInstType = inst.extInstType;
649
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400650 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100651 }
652
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100653 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400654 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400655 totalSize += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100656 }
657
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400658 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400659 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100660 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400661 for (auto& inst : instructions) {
Lei Zhang6483bd72015-10-14 17:02:39 -0400662 memcpy(data + currentIndex, inst.words.data(),
663 sizeof(uint32_t) * inst.words.size());
David Netob5dc8fc2015-10-06 16:22:00 -0400664 currentIndex += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100665 }
666
667 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400668 if (!binary) {
669 delete[] data;
670 return SPV_ERROR_OUT_OF_MEMORY;
671 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100672 binary->code = data;
673 binary->wordCount = totalSize;
674
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400675 spv_result_t error = spvBinaryHeaderSet(binary, context.getBound());
Lei Zhang40056702015-09-11 14:31:27 -0400676 if (error) {
677 spvBinaryDestroy(binary);
678 return error;
679 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100680
681 *pBinary = binary;
682
683 return SPV_SUCCESS;
684}
685
Lei Zhange78a7c12015-09-10 17:07:21 -0400686} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400687
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400688spv_result_t spvTextToBinary(const char* input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400689 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400690 const spv_opcode_table opcodeTable,
691 const spv_operand_table operandTable,
692 const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400693 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400694 return spvTextWithFormatToBinary(
695 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
696 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
697}
698
699spv_result_t spvTextWithFormatToBinary(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400700 const char* input_text, const uint64_t input_text_size,
Lei Zhang06efdc52015-09-10 14:00:00 -0400701 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
702 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400703 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400704 spv_text_t text = {input_text, input_text_size};
705
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400706 libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable);
Lei Zhang06efdc52015-09-10 14:00:00 -0400707 spv_result_t result =
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400708 spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400709 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
710
711 return result;
712}
713
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100714void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400715 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100716 if (text->str) {
717 delete[] text->str;
718 }
719 delete text;
720}