blob: 9e9104bdec99f9646d33c2ec435fd33363ff9c15 [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
David Netofcc7d582015-10-27 15:31:10 -040041#include "assembly_grammar.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010042#include "binary.h"
43#include "diagnostic.h"
44#include "ext_inst.h"
David Netob5dc8fc2015-10-06 16:22:00 -040045#include "instruction.h"
David Neto36b0c0f2015-09-16 18:32:54 -040046#include <libspirv/libspirv.h>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010047#include "opcode.h"
48#include "operand.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040049#include "text_handler.h"
Andrew Woloszynf731cbf2015-10-23 09:53:58 -040050#include "util/bitutils.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010051
Andrew Woloszyn13804e52015-09-22 15:50:33 -040052bool 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 Mircevski14c4b102015-09-29 17:07:21 -040057bool spvIsValidID(const char* textValue) {
58 const char* c = textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -040059 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 Woloszyn71fc0552015-09-24 10:26:51 -040068// Text API
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010069
Dejan Mircevski903f9d62015-09-28 17:04:39 -040070spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010071 bool isSigned = false;
David Netoaffa6962015-08-24 15:33:14 -040072 int numPeriods = 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073 bool isString = false;
74
David Netoaffa6962015-08-24 15:33:14 -040075 const size_t len = strlen(textValue);
David Neto98290a22015-08-24 16:27:02 -040076 if (len == 0) return SPV_FAILED_MATCH;
77
David Netoaffa6962015-08-24 15:33:14 -040078 for (uint64_t index = 0; index < len; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010079 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 Netoaffa6962015-08-24 15:33:14 -040092 numPeriods++;
93 break;
94 case '-':
95 if (index == 0) {
96 isSigned = true;
97 } else {
98 isString = true;
99 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100100 break;
101 default:
102 isString = true;
Lei Zhange78a7c12015-09-10 17:07:21 -0400103 index = len; // break out of the loop too.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100104 break;
105 }
106 }
107
David Netoaffa6962015-08-24 15:33:14 -0400108 pLiteral->type = spv_literal_type_t(99);
109
Lei Zhange78a7c12015-09-10 17:07:21 -0400110 if (isString || numPeriods > 1 || (isSigned && len == 1)) {
David Neto98290a22015-08-24 16:27:02 -0400111 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
112 return SPV_FAILED_MATCH;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400113 bool escaping = false;
114 size_t write_index = 0;
Lei Zhang6483bd72015-10-14 17:02:39 -0400115 for (const char* val = textValue + 1; val != textValue + len - 1; ++val) {
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400116 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)83e5a292015-05-22 18:26:19 +0100128 pLiteral->type = SPV_LITERAL_TYPE_STRING;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400129 pLiteral->value.str[write_index] = '\0';
David Netoaffa6962015-08-24 15:33:14 -0400130 } else if (numPeriods == 1) {
131 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100132 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 Mircevski14c4b102015-09-29 17:07:21 -0400165namespace {
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.
171spv_result_t encodeImmediate(libspirv::AssemblyContext* context,
172 const char* text, spv_instruction_t* pInst) {
173 assert(*text == '!');
David Neto78e677b2015-10-05 13:28:46 -0400174 uint32_t parse_result;
175 if (auto error =
David Neto51013d12015-10-14 11:31:51 -0400176 context->parseNumber(text + 1, SPV_ERROR_INVALID_TEXT, &parse_result,
David Neto78e677b2015-10-05 13:28:46 -0400177 "Invalid immediate integer: !"))
178 return error;
179 context->binaryEncodeU32(parse_result, pInst);
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400180 context->seekForward(strlen(text));
181 return SPV_SUCCESS;
182}
183
184} // anonymous namespace
185
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400186/// @brief Translate an Opcode operand to binary form
187///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400188/// @param[in] grammar the grammar to use for compilation
189/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400190/// @param[in] type of the operand
191/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400192/// @param[out] pInst return binary Opcode
193/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400194///
195/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400196spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400197 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)83e5a292015-05-22 18:26:19 +0100202 // NOTE: Handle immediate int in the stream
203 if ('!' == textValue[0]) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400204 if (auto error = encodeImmediate(context, textValue, pInst)) {
205 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400206 }
Dejan Mircevski897bff92015-09-29 10:38:18 -0400207 *pExpectedOperands =
208 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100209 return SPV_SUCCESS;
210 }
211
David Neto51013d12015-10-14 11:31:51 -0400212 // 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)83e5a292015-05-22 18:26:19 +0100218 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400219 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Netoe3f70b92015-08-27 13:50:05 -0400220 case SPV_OPERAND_TYPE_ID:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400221 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400222 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
223 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netob14a7272015-09-25 13:56:09 -0400224 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
225 case SPV_OPERAND_TYPE_RESULT_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400226 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100227 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100228 } else {
David Netoac508b02015-10-09 15:48:09 -0400229 return context->diagnostic() << "Expected id to start with %.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100230 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400231 if (!spvIsValidID(textValue)) {
David Netoac508b02015-10-09 15:48:09 -0400232 return context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400233 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400234 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400235 if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
David Netob5dc8fc2015-10-06 16:22:00 -0400236 spvInstructionAddWord(pInst, id);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100237 } break;
David Neto445ce442015-10-15 15:22:06 -0400238 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)83e5a292015-05-22 18:26:19 +0100245 }
David Neto445ce442015-10-15 15:22:06 -0400246 spvInstructionAddWord(pInst, extInst->ext_inst);
247
248 // Prepare to parse the operands for the extended instructions.
249 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
250
251 return SPV_SUCCESS;
252 } break;
253 case SPV_OPERAND_TYPE_LITERAL_INTEGER:
Lei Zhangb41d1502015-09-14 15:22:23 -0400254 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
Lei Zhang6483bd72015-10-14 17:02:39 -0400255 case SPV_OPERAND_TYPE_LITERAL_INTEGER_IN_OPTIONAL_TUPLE:
256 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
257 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: {
David Neto78e677b2015-10-05 13:28:46 -0400258 libspirv::IdType expected_type = libspirv::kUnknownType;
Lei Zhang6483bd72015-10-14 17:02:39 -0400259 if (type != SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER) {
260 // From now, it's safe to assume that the current operand is an unsigned
261 // integer, apart from the special cases handled below.
262 expected_type = {32, false, libspirv::IdTypeClass::kScalarIntegerType};
263 // The encoding for OpConstant, OpSpecConstant and OpSwitch all
264 // depend on either their own result-id or the result-id of
265 // one of their parameters.
Lei Zhangb36e7042015-10-28 13:40:52 -0400266 if (SpvOpConstant == pInst->opcode ||
267 SpvOpSpecConstant == pInst->opcode) {
Lei Zhang6483bd72015-10-14 17:02:39 -0400268 // Special cases for encoding possibly non-32-bit literals here.
269 expected_type =
270 context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
271 if (!libspirv::isScalarFloating(expected_type) &&
272 !libspirv::isScalarIntegral(expected_type)) {
273 spv_opcode_desc d;
274 const char* opcode_name = "opcode";
275 if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
276 opcode_name = d->name;
277 }
278 return context->diagnostic()
279 << "Type for " << opcode_name
280 << " must be a scalar floating point or integer type";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400281 }
Lei Zhangb36e7042015-10-28 13:40:52 -0400282 } else if (pInst->opcode == SpvOpSwitch) {
Lei Zhang6483bd72015-10-14 17:02:39 -0400283 // We need to know the type of the selector.
284 expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
285 if (!libspirv::isScalarIntegral(expected_type)) {
286 context->diagnostic()
287 << "The selector operand for OpSwitch must be the result"
288 " of an instruction that generates an integer scalar";
289 return SPV_ERROR_INVALID_TEXT;
290 }
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400291 }
292 }
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400293
David Neto78e677b2015-10-05 13:28:46 -0400294 if (auto error = context->binaryEncodeNumericLiteral(
David Neto51013d12015-10-14 11:31:51 -0400295 textValue, error_code_for_literals, expected_type, pInst)) {
David Neto78e677b2015-10-05 13:28:46 -0400296 return error;
David Neto51013d12015-10-14 11:31:51 -0400297 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100298 } break;
David Neto78c3b432015-08-27 13:03:52 -0400299 case SPV_OPERAND_TYPE_LITERAL_STRING:
300 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400301 spv_literal_t literal = {};
302 spv_result_t error = spvTextToLiteral(textValue, &literal);
303 if (error != SPV_SUCCESS) {
304 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
David Neto51013d12015-10-14 11:31:51 -0400305 return context->diagnostic(error_code_for_literals)
306 << "Invalid literal string '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400307 }
Lei Zhang6d415812015-09-15 13:36:21 -0400308 if (literal.type != SPV_LITERAL_TYPE_STRING) {
David Neto51013d12015-10-14 11:31:51 -0400309 return context->diagnostic()
David Netoac508b02015-10-09 15:48:09 -0400310 << "Expected literal string, found literal number '" << textValue
311 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400312 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100313
314 // NOTE: Special case for extended instruction library import
Lei Zhangb36e7042015-10-28 13:40:52 -0400315 if (SpvOpExtInstImport == pInst->opcode) {
Lei Zhang6d415812015-09-15 13:36:21 -0400316 pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100317 }
318
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400319 if (context->binaryEncodeString(literal.value.str, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400320 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100321 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400322 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
323 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
324 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400325 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400326 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400327 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
328 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400329 if (grammar.parseMaskOperand(type, textValue, &value)) {
David Netoac508b02015-10-09 15:48:09 -0400330 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
331 << " '" << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400332 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400333 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400334 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400335 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400336 } break;
337 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
338 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400339 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
340 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400341 if (error == SPV_FAILED_MATCH) {
342 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400343 error = spvTextEncodeOperand(grammar, context,
344 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
345 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400346 }
347 if (error == SPV_FAILED_MATCH) {
348 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400349 error =
350 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
351 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400352 }
353 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400354 return context->diagnostic(error)
355 << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400356 }
357 if (pExpectedOperands->empty()) {
358 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
359 }
David Neto36b0c0f2015-09-16 18:32:54 -0400360 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100361 default: {
362 // NOTE: All non literal operands are handled here using the operand
363 // table.
364 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400365 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
David Netoac508b02015-10-09 15:48:09 -0400366 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
367 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400368 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400369 if (context->binaryEncodeU32(entry->value, pInst)) {
David Netoac508b02015-10-09 15:48:09 -0400370 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
371 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400372 }
David Neto78c3b432015-08-27 13:03:52 -0400373
374 // Prepare to parse the operands for this logical operand.
375 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100376 } break;
377 }
378 return SPV_SUCCESS;
379}
380
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400381namespace {
382
383/// Encodes an instruction started by !<integer> at the given position in text.
384///
385/// Puts the encoded words into *pInst. If successful, moves position past the
386/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
387/// leaves position pointing to the error in text.
388spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400389 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400390 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400391 std::string firstWord;
392 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400393 auto error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400394 if (error) return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400395
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400396 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
397 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400398 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400399 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400400 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400401 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400402
403 // Otherwise, there must be an operand that's either a literal, an ID, or
404 // an immediate.
405 std::string operandValue;
David Netoac508b02015-10-09 15:48:09 -0400406 if ((error = context->getWord(operandValue, &nextPosition)))
407 return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400408
David Netoac508b02015-10-09 15:48:09 -0400409 if (operandValue == "=")
410 return context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400411
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400412 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
413 // expanded.
414 spv_operand_pattern_t dummyExpectedOperands;
415 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400416 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
417 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400418 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400419 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400420 }
421 return SPV_SUCCESS;
422}
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400423} // anonymous namespace
424
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400425/// @brief Translate single Opcode and operands to binary form
426///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400427/// @param[in] grammar the grammar to use for compilation
428/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400429/// @param[in] text stream to translate
430/// @param[in] format the assembly syntax format of text
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400431/// @param[out] pInst returned binary Opcode
432/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400433///
434/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400435spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400436 libspirv::AssemblyContext* context,
437 spv_assembly_syntax_format_t format,
438 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400439 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400440 if ('!' == context->peek()) {
441 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400442 }
443
Lei Zhangdfc50082015-08-21 11:50:55 -0400444 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400445 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
446 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400447
Lei Zhang06efdc52015-09-10 14:00:00 -0400448 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100449 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400450 spv_result_t error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400451 if (error) return context->diagnostic() << "Internal Error";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100452
Lei Zhang06efdc52015-09-10 14:00:00 -0400453 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400454 std::string result_id;
455 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400456 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400457 opcodeName = firstWord;
458 } else {
459 // If the first word of this instruction is not an opcode, we must be
460 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400461 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
David Netoac508b02015-10-09 15:48:09 -0400462 return context->diagnostic()
463 << "Expected <opcode> at the beginning of an instruction, found '"
464 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400465 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400466
467 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400468 if ('%' != result_id.front()) {
David Netoac508b02015-10-09 15:48:09 -0400469 return context->diagnostic()
470 << "Expected <opcode> or <result-id> at the beginning "
471 "of an instruction, found '"
472 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400473 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400474 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400475
476 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400477 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400478 if (context->advance())
479 return context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400480 std::string equal_sign;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400481 error = context->getWord(equal_sign, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400482 if ("=" != equal_sign)
483 return context->diagnostic() << "'=' expected after result id.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400484
485 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400486 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400487 if (context->advance())
488 return context->diagnostic() << "Expected opcode, found end of stream.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400489 error = context->getWord(opcodeName, &nextPosition);
Lei Zhang6483bd72015-10-14 17:02:39 -0400490 if (error) return context->diagnostic(error) << "Internal Error";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400491 if (!context->startsWithOp()) {
David Netoac508b02015-10-09 15:48:09 -0400492 return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName
493 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400494 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400495 }
496
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100497 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400498 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100499
500 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400501 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400502 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400503 return context->diagnostic(error) << "Invalid Opcode name '"
504 << context->getWord() << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400505 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400506 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
507 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400508 if (opcodeEntry->hasResult && result_id.empty()) {
David Netoac508b02015-10-09 15:48:09 -0400509 return context->diagnostic()
510 << "Expected <result-id> at the beginning of an "
511 "instruction, found '"
512 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400513 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400514 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100515 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400516 context->setPosition(nextPosition);
David Netob5dc8fc2015-10-06 16:22:00 -0400517 // Reserve the first word for the instruction.
518 spvInstructionAddWord(pInst, 0);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100519
David Neto78c3b432015-08-27 13:03:52 -0400520 // Maintains the ordered list of expected operand types.
521 // For many instructions we only need the {numTypes, operandTypes}
522 // entries in opcodeEntry. However, sometimes we need to modify
523 // the list as we parse the operands. This occurs when an operand
524 // has its own logical operands (such as the LocalSize operand for
525 // ExecutionMode), or for extended instructions that may have their
526 // own operands depending on the selected extended instruction.
527 spv_operand_pattern_t expectedOperands(
528 opcodeEntry->operandTypes,
529 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400530
David Neto78c3b432015-08-27 13:03:52 -0400531 while (!expectedOperands.empty()) {
532 const spv_operand_type_t type = expectedOperands.front();
533 expectedOperands.pop_front();
534
535 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400536 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400537
538 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
539 // Handle the <result-id> for value generating instructions.
540 // We've already consumed it from the text stream. Here
541 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400542 spv_position_t temp_pos = context->position();
543 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
544 result_id.c_str(), pInst, nullptr);
545 result_id_position = context->position();
546 // Because we are injecting we have to reset the position afterwards.
547 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400548 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100549 } else {
David Neto78c3b432015-08-27 13:03:52 -0400550 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400551 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400552 if (error == SPV_END_OF_STREAM) {
553 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400554 // This would have been the last potential operand for the
555 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400556 // and we didn't find one. We're finished parsing this instruction.
557 break;
558 } else {
David Netoac508b02015-10-09 15:48:09 -0400559 return context->diagnostic()
560 << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400561 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100562 }
David Neto78c3b432015-08-27 13:03:52 -0400563 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
564
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400565 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400566 if (spvOperandIsOptional(type)) {
567 break;
568 } else {
David Netoac508b02015-10-09 15:48:09 -0400569 return context->diagnostic()
570 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400571 }
572 }
573
574 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400575 error = context->getWord(operandValue, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400576 if (error) return context->diagnostic(error) << "Internal Error";
David Neto78c3b432015-08-27 13:03:52 -0400577
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400578 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
579 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400580
581 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
582 return SPV_SUCCESS;
583
Lei Zhang40056702015-09-11 14:31:27 -0400584 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400585
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400586 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100587 }
588 }
589
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400590 if (spvOpcodeGeneratesType(pInst->opcode)) {
591 if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
592 return SPV_ERROR_INVALID_TEXT;
593 }
594 } else if (opcodeEntry->hasType) {
595 // SPIR-V dictates that if an instruction has both a return value and a
596 // type ID then the type id is first, and the return value is second.
597 assert(opcodeEntry->hasResult &&
598 "Unknown opcode: has a type but no result.");
599 context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
600 }
601
David Netob5dc8fc2015-10-06 16:22:00 -0400602 if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Netoac508b02015-10-09 15:48:09 -0400603 return context->diagnostic()
604 << "Instruction too long: " << pInst->words.size()
605 << " words, but the limit is "
606 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
David Netob5dc8fc2015-10-06 16:22:00 -0400607 }
608
609 pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100610
611 return SPV_SUCCESS;
612}
613
David Netoc9786432015-09-01 18:05:14 -0400614namespace {
615
David Netoe4945de2015-10-28 13:50:32 -0400616/// @brief Populate a binary stream's words with this generator's header.
David Neto0b981682015-10-27 15:51:34 -0400617///
David Netoe4945de2015-10-28 13:50:32 -0400618/// @param[in,out] words the array of words
David Neto0b981682015-10-27 15:51:34 -0400619/// @param[in] bound the upper ID bound
620///
621/// @return result code
David Netoe4945de2015-10-28 13:50:32 -0400622spv_result_t SetHeader(uint32_t* words, const uint32_t bound) {
623 if (!words) return SPV_ERROR_INVALID_BINARY;
David Neto0b981682015-10-27 15:51:34 -0400624
David Netoe4945de2015-10-28 13:50:32 -0400625 words[SPV_INDEX_MAGIC_NUMBER] = SPV_MAGIC_NUMBER;
626 words[SPV_INDEX_VERSION_NUMBER] = SPV_VERSION_NUMBER;
627 words[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS;
628 words[SPV_INDEX_BOUND] = bound;
629 words[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
David Neto0b981682015-10-27 15:51:34 -0400630
631 return SPV_SUCCESS;
632}
633
David Netoc9786432015-09-01 18:05:14 -0400634// Translates a given assembly language module into binary form.
635// If a diagnostic is generated, it is not yet marked as being
636// for a text-based input.
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400637spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
638 const spv_text text,
639 spv_assembly_syntax_format_t format,
640 spv_binary* pBinary,
641 spv_diagnostic* pDiagnostic) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400642 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
643 libspirv::AssemblyContext context(text, pDiagnostic);
David Netoea633a62015-11-02 11:40:59 -0500644 if (!text->str)
645 return context.diagnostic() << "Missing assembly text.";
David Netoac508b02015-10-09 15:48:09 -0400646
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400647 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400648 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400649 }
Lei Zhang40056702015-09-11 14:31:27 -0400650 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100651
652 // NOTE: Ensure diagnostic is zero initialised
653 *pDiagnostic = {};
654
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100655 std::vector<spv_instruction_t> instructions;
656
David Netoea633a62015-11-02 11:40:59 -0500657 // Skip past whitespace and comments.
658 context.advance();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100659
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400660 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
661 while (context.hasText()) {
David Netob5dc8fc2015-10-06 16:22:00 -0400662 instructions.push_back({});
663 spv_instruction_t& inst = instructions.back();
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400664 inst.extInstType = extInstType;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100665
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400666 if (spvTextEncodeOpcode(grammar, &context, format, &inst)) {
667 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400668 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400669 extInstType = inst.extInstType;
670
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400671 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100672 }
673
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100674 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400675 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400676 totalSize += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100677 }
678
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400679 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400680 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100681 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400682 for (auto& inst : instructions) {
Lei Zhang6483bd72015-10-14 17:02:39 -0400683 memcpy(data + currentIndex, inst.words.data(),
684 sizeof(uint32_t) * inst.words.size());
David Netob5dc8fc2015-10-06 16:22:00 -0400685 currentIndex += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100686 }
687
David Netoe4945de2015-10-28 13:50:32 -0400688 if (auto error = SetHeader(data, context.getBound()))
689 return error;
690
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100691 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400692 if (!binary) {
693 delete[] data;
694 return SPV_ERROR_OUT_OF_MEMORY;
695 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100696 binary->code = data;
697 binary->wordCount = totalSize;
698
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100699 *pBinary = binary;
700
701 return SPV_SUCCESS;
702}
703
Lei Zhange78a7c12015-09-10 17:07:21 -0400704} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400705
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400706spv_result_t spvTextToBinary(const char* input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400707 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400708 const spv_opcode_table opcodeTable,
709 const spv_operand_table operandTable,
710 const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400711 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400712 return spvTextWithFormatToBinary(
713 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
714 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
715}
716
717spv_result_t spvTextWithFormatToBinary(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400718 const char* input_text, const uint64_t input_text_size,
Lei Zhang06efdc52015-09-10 14:00:00 -0400719 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
720 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400721 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400722 spv_text_t text = {input_text, input_text_size};
723
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400724 libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable);
Lei Zhang06efdc52015-09-10 14:00:00 -0400725 spv_result_t result =
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400726 spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400727 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
728
729 return result;
730}
731
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100732void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400733 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100734 if (text->str) {
735 delete[] text->str;
736 }
737 delete text;
738}