blob: 87b479adeb6be37ceaf99b56e55d933737cc7ad5 [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 Mircevski903f9d62015-09-28 17:04:39 -0400185 spvSwitchToAlternateParsingAfterImmediate(pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100186 return SPV_SUCCESS;
187 }
188
189 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400190 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Netoe3f70b92015-08-27 13:50:05 -0400191 case SPV_OPERAND_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400192 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
193 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netob14a7272015-09-25 13:56:09 -0400194 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
195 case SPV_OPERAND_TYPE_RESULT_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400196 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100197 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100198 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400199 context->diagnostic() << "Expected id to start with %.";
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400200 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100201 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400202 if (!spvIsValidID(textValue)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400203 context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400204 return SPV_ERROR_INVALID_TEXT;
205 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400206 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100207 pInst->words[pInst->wordCount++] = id;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100208 } break;
209 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
210 // NOTE: Special case for extension instruction lookup
211 if (OpExtInst == pInst->opcode) {
212 spv_ext_inst_desc extInst;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400213 if (grammar.lookupExtInst(pInst->extInstType, textValue,
214 &extInst)) {
215 context->diagnostic() << "Invalid extended instruction name '"
216 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400217 return SPV_ERROR_INVALID_TEXT;
218 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100219 pInst->words[pInst->wordCount++] = extInst->ext_inst;
David Neto78c3b432015-08-27 13:03:52 -0400220
221 // Prepare to parse the operands for the extended instructions.
222 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
223
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100224 return SPV_SUCCESS;
225 }
Lei Zhang6d415812015-09-15 13:36:21 -0400226 } // Fall through for the general case.
Lei Zhangb41d1502015-09-14 15:22:23 -0400227 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
David Neto561dc4e2015-09-25 14:23:29 -0400228 case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE:
229 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100230 spv_literal_t literal = {};
Lei Zhang6d415812015-09-15 13:36:21 -0400231 spv_result_t error = spvTextToLiteral(textValue, &literal);
232 if (error != SPV_SUCCESS) {
233 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
234 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400235 context->diagnostic() << "Invalid literal number '" << textValue << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400236 return SPV_ERROR_INVALID_TEXT;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400237 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100238 switch (literal.type) {
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400239 // We do not have to print diagnostics here because spvBinaryEncode*
240 // prints diagnostic messages on failure.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100241 case SPV_LITERAL_TYPE_INT_32:
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400242 if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32),
243 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400244 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100245 break;
246 case SPV_LITERAL_TYPE_INT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400247 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64),
248 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400249 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100250 } break;
251 case SPV_LITERAL_TYPE_UINT_32: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400252 if (context->binaryEncodeU32(literal.value.u32, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400253 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100254 } break;
255 case SPV_LITERAL_TYPE_UINT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400256 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64),
257 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400258 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100259 } break;
260 case SPV_LITERAL_TYPE_FLOAT_32: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400261 if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f),
262 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400263 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100264 } break;
265 case SPV_LITERAL_TYPE_FLOAT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400266 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d),
267 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400268 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100269 } break;
270 case SPV_LITERAL_TYPE_STRING: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400271 context->diagnostic() << "Expected literal number, found literal string '"
272 << textValue << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400273 return SPV_FAILED_MATCH;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100274 } break;
275 default:
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400276 context->diagnostic() << "Invalid literal number '" << textValue << "'";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100277 return SPV_ERROR_INVALID_TEXT;
278 }
279 } break;
David Neto78c3b432015-08-27 13:03:52 -0400280 case SPV_OPERAND_TYPE_LITERAL_STRING:
281 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400282 spv_literal_t literal = {};
283 spv_result_t error = spvTextToLiteral(textValue, &literal);
284 if (error != SPV_SUCCESS) {
285 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
Lei Zhang40056702015-09-11 14:31:27 -0400286 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400287 context->diagnostic() << "Invalid literal string '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400288 return SPV_ERROR_INVALID_TEXT;
289 }
Lei Zhang6d415812015-09-15 13:36:21 -0400290 if (literal.type != SPV_LITERAL_TYPE_STRING) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400291 context->diagnostic() << "Expected literal string, found literal number '"
292 << textValue << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400293 return SPV_FAILED_MATCH;
294 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100295
296 // NOTE: Special case for extended instruction library import
297 if (OpExtInstImport == pInst->opcode) {
Lei Zhang6d415812015-09-15 13:36:21 -0400298 pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100299 }
300
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400301 if (context->binaryEncodeString(literal.value.str, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400302 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100303 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400304 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
305 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
306 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400307 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400308 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400309 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
310 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400311 if (grammar.parseMaskOperand(type, textValue, &value)) {
312 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
313 << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400314 return SPV_ERROR_INVALID_TEXT;
315 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400316 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400317 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400318 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400319 } break;
320 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
321 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400322 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
323 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400324 if (error == SPV_FAILED_MATCH) {
325 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400326 error = spvTextEncodeOperand(grammar, context,
327 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
328 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400329 }
330 if (error == SPV_FAILED_MATCH) {
331 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400332 error =
333 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
334 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400335 }
336 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400337 context->diagnostic() << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400338 return error;
339 }
340 if (pExpectedOperands->empty()) {
341 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
342 }
David Neto36b0c0f2015-09-16 18:32:54 -0400343 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100344 default: {
345 // NOTE: All non literal operands are handled here using the operand
346 // table.
347 spv_operand_desc entry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400348 if (grammar.lookupOperand(type, textValue, strlen(textValue),
349 &entry)) {
350 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
351 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400352 return SPV_ERROR_INVALID_TEXT;
353 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400354 if (context->binaryEncodeU32(entry->value, pInst)) {
355 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
356 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400357 return SPV_ERROR_INVALID_TEXT;
358 }
David Neto78c3b432015-08-27 13:03:52 -0400359
360 // Prepare to parse the operands for this logical operand.
361 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100362 } break;
363 }
364 return SPV_SUCCESS;
365}
366
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400367namespace {
368
369/// Encodes an instruction started by !<integer> at the given position in text.
370///
371/// Puts the encoded words into *pInst. If successful, moves position past the
372/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
373/// leaves position pointing to the error in text.
374spv_result_t encodeInstructionStartingWithImmediate(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400375 const libspirv::AssemblyGrammar &grammar,
376 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400377 std::string firstWord;
378 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400379 auto error = context->getWord(firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400380 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400381 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400382 return error;
383 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400384
385 assert(firstWord[0] == '!');
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400386 const char* begin = firstWord.data() + 1;
387 char* end = nullptr;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400388 uint32_t immediateInt = strtoul(begin, &end, 0);
Lei Zhang40056702015-09-11 14:31:27 -0400389 if ((begin + firstWord.size() - 1) != end) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400390 context->diagnostic() << "Invalid immediate integer '" << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400391 return SPV_ERROR_INVALID_TEXT;
392 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400393 context->seekForward(firstWord.size());
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400394 pInst->words[0] = immediateInt;
395 pInst->wordCount = 1;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400396 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400397 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400398 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400399
400 // Otherwise, there must be an operand that's either a literal, an ID, or
401 // an immediate.
402 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400403 if ((error = context->getWord(operandValue, &nextPosition))) {
404 context->diagnostic() << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400405 return error;
406 }
407
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400408 if (operandValue == "=") {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400409 context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400410 return SPV_ERROR_INVALID_TEXT;
411 }
412
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400413 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
414 // expanded.
415 spv_operand_pattern_t dummyExpectedOperands;
416 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400417 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
418 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400419 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400420 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400421 }
422 return SPV_SUCCESS;
423}
424
425} // anonymous namespace
426
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400427/// @brief Translate single Opcode and operands to binary form
428///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400429/// @param[in] grammar the grammar to use for compilation
430/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400431/// @param[in] text stream to translate
432/// @param[in] format the assembly syntax format of text
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400433/// @param[out] pInst returned binary Opcode
434/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400435///
436/// @return result code
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400437spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar &grammar,
438 libspirv::AssemblyContext* context,
439 spv_assembly_syntax_format_t format,
440 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400441 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400442 if ('!' == context->peek()) {
443 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400444 }
445
Lei Zhangdfc50082015-08-21 11:50:55 -0400446 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400447 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
448 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400449
Lei Zhang06efdc52015-09-10 14:00:00 -0400450 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100451 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400452 spv_result_t error = context->getWord(firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400453 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400454 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400455 return error;
456 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100457
Lei Zhang06efdc52015-09-10 14:00:00 -0400458 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400459 std::string result_id;
460 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400461 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400462 opcodeName = firstWord;
463 } else {
464 // If the first word of this instruction is not an opcode, we must be
465 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400466 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400467 context->diagnostic()
Lei Zhang40056702015-09-11 14:31:27 -0400468 << "Expected <opcode> at the beginning of an instruction, found '"
469 << firstWord << "'.";
470 return SPV_ERROR_INVALID_TEXT;
471 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400472
473 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400474 if ('%' != result_id.front()) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400475 context->diagnostic() << "Expected <opcode> or <result-id> at the beginning "
476 "of an instruction, found '"
477 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400478 return SPV_ERROR_INVALID_TEXT;
479 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400480 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400481
482 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400483 context->setPosition(nextPosition);
484 if (context->advance()) {
485 context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhang40056702015-09-11 14:31:27 -0400486 return SPV_ERROR_INVALID_TEXT;
487 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400488 std::string equal_sign;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400489 error = context->getWord(equal_sign, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400490 if ("=" != equal_sign) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400491 context->diagnostic() << "'=' expected after result id.";
Lei Zhang40056702015-09-11 14:31:27 -0400492 return SPV_ERROR_INVALID_TEXT;
493 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400494
495 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400496 context->setPosition(nextPosition);
497 if (context->advance()) {
498 context->diagnostic() << "Expected opcode, found end of stream.";
Lei Zhang40056702015-09-11 14:31:27 -0400499 return SPV_ERROR_INVALID_TEXT;
500 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400501 error = context->getWord(opcodeName, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400502 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400503 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400504 return error;
505 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400506 if (!context->startsWithOp()) {
507 context->diagnostic() << "Invalid Opcode prefix '" << opcodeName << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400508 return SPV_ERROR_INVALID_TEXT;
509 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400510 }
511
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100512 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400513 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100514
515 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400516 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400517 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400518 context->diagnostic() << "Invalid Opcode name '" << context->getWord() << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400519 return error;
520 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400521 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
522 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400523 if (opcodeEntry->hasResult && result_id.empty()) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400524 context->diagnostic() << "Expected <result-id> at the beginning of an "
525 "instruction, found '"
526 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400527 return SPV_ERROR_INVALID_TEXT;
528 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400529 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100530 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400531 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100532 pInst->wordCount++;
533
David Neto78c3b432015-08-27 13:03:52 -0400534 // Maintains the ordered list of expected operand types.
535 // For many instructions we only need the {numTypes, operandTypes}
536 // entries in opcodeEntry. However, sometimes we need to modify
537 // the list as we parse the operands. This occurs when an operand
538 // has its own logical operands (such as the LocalSize operand for
539 // ExecutionMode), or for extended instructions that may have their
540 // own operands depending on the selected extended instruction.
541 spv_operand_pattern_t expectedOperands(
542 opcodeEntry->operandTypes,
543 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400544
David Neto78c3b432015-08-27 13:03:52 -0400545 while (!expectedOperands.empty()) {
546 const spv_operand_type_t type = expectedOperands.front();
547 expectedOperands.pop_front();
548
549 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400550 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400551
552 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
553 // Handle the <result-id> for value generating instructions.
554 // We've already consumed it from the text stream. Here
555 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400556 spv_position_t temp_pos = context->position();
557 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
558 result_id.c_str(), pInst, nullptr);
559 result_id_position = context->position();
560 // Because we are injecting we have to reset the position afterwards.
561 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400562 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100563 } else {
David Neto78c3b432015-08-27 13:03:52 -0400564 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400565 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400566 if (error == SPV_END_OF_STREAM) {
567 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400568 // This would have been the last potential operand for the
569 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400570 // and we didn't find one. We're finished parsing this instruction.
571 break;
572 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400573 context->diagnostic() << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400574 return SPV_ERROR_INVALID_TEXT;
575 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100576 }
David Neto78c3b432015-08-27 13:03:52 -0400577 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
578
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400579 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400580 if (spvOperandIsOptional(type)) {
581 break;
582 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400583 context->diagnostic()
584 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400585 return SPV_ERROR_INVALID_TEXT;
586 }
587 }
588
589 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400590 error = context->getWord(operandValue, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400591 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400592 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400593 return error;
594 }
David Neto78c3b432015-08-27 13:03:52 -0400595
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400596 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
597 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400598
599 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
600 return SPV_SUCCESS;
601
Lei Zhang40056702015-09-11 14:31:27 -0400602 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400603
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400604 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100605 }
606 }
607
608 pInst->words[0] = spvOpcodeMake(pInst->wordCount, opcodeEntry->opcode);
609
610 return SPV_SUCCESS;
611}
612
David Netoc9786432015-09-01 18:05:14 -0400613namespace {
614
615// Translates a given assembly language module into binary form.
616// If a diagnostic is generated, it is not yet marked as being
617// for a text-based input.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400618spv_result_t spvTextToBinaryInternal(
619 const libspirv::AssemblyGrammar &grammar, const spv_text text,
620 spv_assembly_syntax_format_t format, spv_binary* pBinary,
621 spv_diagnostic* pDiagnostic) {
622 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
623 libspirv::AssemblyContext context(text, pDiagnostic);
Lei Zhang40056702015-09-11 14:31:27 -0400624 if (!text->str || !text->length) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400625 context.diagnostic() << "Text stream is empty.";
Lei Zhang40056702015-09-11 14:31:27 -0400626 return SPV_ERROR_INVALID_TEXT;
627 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400628 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400629 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400630 }
Lei Zhang40056702015-09-11 14:31:27 -0400631 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100632
633 // NOTE: Ensure diagnostic is zero initialised
634 *pDiagnostic = {};
635
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100636 std::vector<spv_instruction_t> instructions;
637
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400638 if (context.advance()) {
639 context.diagnostic() << "Text stream is empty.";
Lei Zhang40056702015-09-11 14:31:27 -0400640 return SPV_ERROR_INVALID_TEXT;
641 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100642
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400643 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
644 while (context.hasText()) {
645 spv_instruction_t inst = {};
646 inst.extInstType = extInstType;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100647
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400648 if (spvTextEncodeOpcode(grammar, &context, format, &inst)) {
649 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400650 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400651 extInstType = inst.extInstType;
652
653 instructions.push_back(inst);
654
655 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100656 }
657
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100658 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400659 for (auto& inst : instructions) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100660 totalSize += inst.wordCount;
661 }
662
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400663 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400664 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100665 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400666 for (auto& inst : instructions) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100667 memcpy(data + currentIndex, inst.words, sizeof(uint32_t) * inst.wordCount);
668 currentIndex += inst.wordCount;
669 }
670
671 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400672 if (!binary) {
673 delete[] data;
674 return SPV_ERROR_OUT_OF_MEMORY;
675 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100676 binary->code = data;
677 binary->wordCount = totalSize;
678
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400679 spv_result_t error = spvBinaryHeaderSet(binary, context.getBound());
Lei Zhang40056702015-09-11 14:31:27 -0400680 if (error) {
681 spvBinaryDestroy(binary);
682 return error;
683 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100684
685 *pBinary = binary;
686
687 return SPV_SUCCESS;
688}
689
Lei Zhange78a7c12015-09-10 17:07:21 -0400690} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400691
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400692spv_result_t spvTextToBinary(const char* input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400693 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400694 const spv_opcode_table opcodeTable,
695 const spv_operand_table operandTable,
696 const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400697 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400698 return spvTextWithFormatToBinary(
699 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
700 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
701}
702
703spv_result_t spvTextWithFormatToBinary(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400704 const char* input_text, const uint64_t input_text_size,
Lei Zhang06efdc52015-09-10 14:00:00 -0400705 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
706 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400707 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400708 spv_text_t text = {input_text, input_text_size};
709
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400710 libspirv::AssemblyGrammar grammar(operandTable, opcodeTable,
711 extInstTable);
Lei Zhang06efdc52015-09-10 14:00:00 -0400712 spv_result_t result =
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400713 spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400714 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
715
716 return result;
717}
718
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100719void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400720 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100721 if (text->str) {
722 delete[] text->str;
723 }
724 delete text;
725}