blob: aa4931787fcc211ce3190602598b73c36584eb8f [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
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 Netoaffa6962015-08-24 15:33:14 -0400111 // TODO(dneto): Allow escaping.
David Neto98290a22015-08-24 16:27:02 -0400112 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
113 return SPV_FAILED_MATCH;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100114 pLiteral->type = SPV_LITERAL_TYPE_STRING;
David Netoaffa6962015-08-24 15:33:14 -0400115 // Need room for the null-terminator.
David Neto98290a22015-08-24 16:27:02 -0400116 if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY;
Lei Zhange78a7c12015-09-10 17:07:21 -0400117 strncpy(pLiteral->value.str, textValue + 1, len - 2);
118 pLiteral->value.str[len - 2] = 0;
David Netoaffa6962015-08-24 15:33:14 -0400119 } else if (numPeriods == 1) {
120 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100121 float f = (float)d;
122 if (d == (double)f) {
123 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
124 pLiteral->value.f = f;
125 } else {
126 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
127 pLiteral->value.d = d;
128 }
129 } else if (isSigned) {
130 int64_t i64 = strtoll(textValue, nullptr, 10);
131 int32_t i32 = (int32_t)i64;
132 if (i64 == (int64_t)i32) {
133 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
134 pLiteral->value.i32 = i32;
135 } else {
136 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
137 pLiteral->value.i64 = i64;
138 }
139 } else {
140 uint64_t u64 = strtoull(textValue, nullptr, 10);
141 uint32_t u32 = (uint32_t)u64;
142 if (u64 == (uint64_t)u32) {
143 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
144 pLiteral->value.u32 = u32;
145 } else {
146 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
147 pLiteral->value.u64 = u64;
148 }
149 }
150
151 return SPV_SUCCESS;
152}
153
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400154namespace {
155
156/// Parses an immediate integer from text, guarding against overflow. If
157/// successful, adds the parsed value to pInst, advances the context past it,
158/// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics,
159/// and returns SPV_ERROR_INVALID_TEXT.
160spv_result_t encodeImmediate(libspirv::AssemblyContext* context,
161 const char* text, spv_instruction_t* pInst) {
162 assert(*text == '!');
163 const char* begin = text + 1;
164 char* end = nullptr;
Dejan Mircevski97e2c8f2015-09-29 17:58:37 -0400165 const uint64_t parseResult = strtoull(begin, &end, 0);
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400166 size_t length = end - begin;
167 if (length != strlen(begin)) {
168 context->diagnostic() << "Invalid immediate integer '" << text << "'.";
169 return SPV_ERROR_INVALID_TEXT;
Dejan Mircevski97e2c8f2015-09-29 17:58:37 -0400170 } else if (length > 10 || (parseResult >> 32) != 0) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400171 context->diagnostic() << "Immediate integer '" << text
Dejan Mircevski114206e2015-09-30 09:49:00 -0400172 << "' is outside the unsigned 32-bit range.";
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400173 return SPV_ERROR_INVALID_TEXT;
174 }
175 context->binaryEncodeU32(parseResult, pInst);
176 context->seekForward(strlen(text));
177 return SPV_SUCCESS;
178}
179
180} // anonymous namespace
181
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400182/// @brief Translate an Opcode operand to binary form
183///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400184/// @param[in] grammar the grammar to use for compilation
185/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400186/// @param[in] type of the operand
187/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400188/// @param[out] pInst return binary Opcode
189/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400190///
191/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400192spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400193 libspirv::AssemblyContext* context,
194 const spv_operand_type_t type,
195 const char* textValue,
196 spv_instruction_t* pInst,
197 spv_operand_pattern_t* pExpectedOperands) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100198 // NOTE: Handle immediate int in the stream
199 if ('!' == textValue[0]) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400200 if (auto error = encodeImmediate(context, textValue, pInst)) {
201 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400202 }
Dejan Mircevski897bff92015-09-29 10:38:18 -0400203 *pExpectedOperands =
204 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100205 return SPV_SUCCESS;
206 }
207
208 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400209 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Netoe3f70b92015-08-27 13:50:05 -0400210 case SPV_OPERAND_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400211 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
212 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netob14a7272015-09-25 13:56:09 -0400213 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
214 case SPV_OPERAND_TYPE_RESULT_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400215 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100216 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100217 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400218 context->diagnostic() << "Expected id to start with %.";
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400219 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100220 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400221 if (!spvIsValidID(textValue)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400222 context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400223 return SPV_ERROR_INVALID_TEXT;
224 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400225 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100226 pInst->words[pInst->wordCount++] = id;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100227 } break;
228 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
229 // NOTE: Special case for extension instruction lookup
230 if (OpExtInst == pInst->opcode) {
231 spv_ext_inst_desc extInst;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400232 if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400233 context->diagnostic() << "Invalid extended instruction name '"
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400234 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400235 return SPV_ERROR_INVALID_TEXT;
236 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100237 pInst->words[pInst->wordCount++] = extInst->ext_inst;
David Neto78c3b432015-08-27 13:03:52 -0400238
239 // Prepare to parse the operands for the extended instructions.
240 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
241
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100242 return SPV_SUCCESS;
243 }
Lei Zhang6d415812015-09-15 13:36:21 -0400244 } // Fall through for the general case.
Lei Zhangb41d1502015-09-14 15:22:23 -0400245 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
David Neto561dc4e2015-09-25 14:23:29 -0400246 case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE:
247 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100248 spv_literal_t literal = {};
Lei Zhang6d415812015-09-15 13:36:21 -0400249 spv_result_t error = spvTextToLiteral(textValue, &literal);
250 if (error != SPV_SUCCESS) {
251 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
252 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400253 context->diagnostic() << "Invalid literal number '" << textValue
254 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400255 return SPV_ERROR_INVALID_TEXT;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400256 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100257 switch (literal.type) {
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400258 // We do not have to print diagnostics here because spvBinaryEncode*
259 // prints diagnostic messages on failure.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100260 case SPV_LITERAL_TYPE_INT_32:
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400261 if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32),
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_INT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400266 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64),
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_UINT_32: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400271 if (context->binaryEncodeU32(literal.value.u32, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400272 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100273 } break;
274 case SPV_LITERAL_TYPE_UINT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400275 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64),
276 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400277 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100278 } break;
279 case SPV_LITERAL_TYPE_FLOAT_32: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400280 if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f),
281 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400282 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100283 } break;
284 case SPV_LITERAL_TYPE_FLOAT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400285 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d),
286 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400287 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100288 } break;
289 case SPV_LITERAL_TYPE_STRING: {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400290 context->diagnostic()
291 << "Expected literal number, found literal string '" << textValue
292 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400293 return SPV_FAILED_MATCH;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100294 } break;
295 default:
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400296 context->diagnostic() << "Invalid literal number '" << textValue
297 << "'";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100298 return SPV_ERROR_INVALID_TEXT;
299 }
300 } break;
David Neto78c3b432015-08-27 13:03:52 -0400301 case SPV_OPERAND_TYPE_LITERAL_STRING:
302 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400303 spv_literal_t literal = {};
304 spv_result_t error = spvTextToLiteral(textValue, &literal);
305 if (error != SPV_SUCCESS) {
306 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
Lei Zhang40056702015-09-11 14:31:27 -0400307 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400308 context->diagnostic() << "Invalid literal string '" << textValue
309 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400310 return SPV_ERROR_INVALID_TEXT;
311 }
Lei Zhang6d415812015-09-15 13:36:21 -0400312 if (literal.type != SPV_LITERAL_TYPE_STRING) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400313 context->diagnostic()
314 << "Expected literal string, found literal number '" << textValue
315 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400316 return SPV_FAILED_MATCH;
317 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100318
319 // NOTE: Special case for extended instruction library import
320 if (OpExtInstImport == pInst->opcode) {
Lei Zhang6d415812015-09-15 13:36:21 -0400321 pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100322 }
323
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400324 if (context->binaryEncodeString(literal.value.str, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400325 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100326 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400327 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
328 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
329 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400330 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400331 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400332 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
333 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400334 if (grammar.parseMaskOperand(type, textValue, &value)) {
335 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400336 << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400337 return SPV_ERROR_INVALID_TEXT;
338 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400339 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400340 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400341 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400342 } break;
343 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
344 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400345 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
346 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400347 if (error == SPV_FAILED_MATCH) {
348 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400349 error = spvTextEncodeOperand(grammar, context,
350 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
351 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400352 }
353 if (error == SPV_FAILED_MATCH) {
354 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400355 error =
356 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
357 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400358 }
359 if (error) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400360 context->diagnostic() << "Invalid word following !<integer>: "
361 << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400362 return error;
363 }
364 if (pExpectedOperands->empty()) {
365 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
366 }
David Neto36b0c0f2015-09-16 18:32:54 -0400367 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100368 default: {
369 // NOTE: All non literal operands are handled here using the operand
370 // table.
371 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400372 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400373 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400374 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400375 return SPV_ERROR_INVALID_TEXT;
376 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400377 if (context->binaryEncodeU32(entry->value, pInst)) {
378 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400379 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400380 return SPV_ERROR_INVALID_TEXT;
381 }
David Neto78c3b432015-08-27 13:03:52 -0400382
383 // Prepare to parse the operands for this logical operand.
384 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100385 } break;
386 }
387 return SPV_SUCCESS;
388}
389
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400390namespace {
391
392/// Encodes an instruction started by !<integer> at the given position in text.
393///
394/// Puts the encoded words into *pInst. If successful, moves position past the
395/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
396/// leaves position pointing to the error in text.
397spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400398 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400399 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400400 std::string firstWord;
401 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400402 auto error = context->getWord(firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400403 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400404 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400405 return error;
406 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400407
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400408 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
409 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400410 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400411 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400412 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400413 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400414
415 // Otherwise, there must be an operand that's either a literal, an ID, or
416 // an immediate.
417 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400418 if ((error = context->getWord(operandValue, &nextPosition))) {
419 context->diagnostic() << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400420 return error;
421 }
422
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400423 if (operandValue == "=") {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400424 context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400425 return SPV_ERROR_INVALID_TEXT;
426 }
427
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400428 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
429 // expanded.
430 spv_operand_pattern_t dummyExpectedOperands;
431 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400432 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
433 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400434 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400435 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400436 }
437 return SPV_SUCCESS;
438}
439
440} // anonymous namespace
441
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400442/// @brief Translate single Opcode and operands to binary form
443///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400444/// @param[in] grammar the grammar to use for compilation
445/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400446/// @param[in] text stream to translate
447/// @param[in] format the assembly syntax format of text
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400448/// @param[out] pInst returned binary Opcode
449/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400450///
451/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400452spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400453 libspirv::AssemblyContext* context,
454 spv_assembly_syntax_format_t format,
455 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400456 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400457 if ('!' == context->peek()) {
458 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400459 }
460
Lei Zhangdfc50082015-08-21 11:50:55 -0400461 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400462 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
463 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400464
Lei Zhang06efdc52015-09-10 14:00:00 -0400465 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100466 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400467 spv_result_t error = context->getWord(firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400468 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400469 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400470 return error;
471 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100472
Lei Zhang06efdc52015-09-10 14:00:00 -0400473 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400474 std::string result_id;
475 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400476 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400477 opcodeName = firstWord;
478 } else {
479 // If the first word of this instruction is not an opcode, we must be
480 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400481 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400482 context->diagnostic()
Lei Zhang40056702015-09-11 14:31:27 -0400483 << "Expected <opcode> at the beginning of an instruction, found '"
484 << firstWord << "'.";
485 return SPV_ERROR_INVALID_TEXT;
486 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400487
488 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400489 if ('%' != result_id.front()) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400490 context->diagnostic()
491 << "Expected <opcode> or <result-id> at the beginning "
492 "of an instruction, found '"
493 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400494 return SPV_ERROR_INVALID_TEXT;
495 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400496 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400497
498 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400499 context->setPosition(nextPosition);
500 if (context->advance()) {
501 context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhang40056702015-09-11 14:31:27 -0400502 return SPV_ERROR_INVALID_TEXT;
503 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400504 std::string equal_sign;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400505 error = context->getWord(equal_sign, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400506 if ("=" != equal_sign) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400507 context->diagnostic() << "'=' expected after result id.";
Lei Zhang40056702015-09-11 14:31:27 -0400508 return SPV_ERROR_INVALID_TEXT;
509 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400510
511 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400512 context->setPosition(nextPosition);
513 if (context->advance()) {
514 context->diagnostic() << "Expected opcode, found end of stream.";
Lei Zhang40056702015-09-11 14:31:27 -0400515 return SPV_ERROR_INVALID_TEXT;
516 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400517 error = context->getWord(opcodeName, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400518 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400519 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400520 return error;
521 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400522 if (!context->startsWithOp()) {
523 context->diagnostic() << "Invalid Opcode prefix '" << opcodeName << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400524 return SPV_ERROR_INVALID_TEXT;
525 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400526 }
527
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100528 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400529 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100530
531 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400532 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400533 if (error) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400534 context->diagnostic() << "Invalid Opcode name '" << context->getWord()
535 << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400536 return error;
537 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400538 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
539 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400540 if (opcodeEntry->hasResult && result_id.empty()) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400541 context->diagnostic() << "Expected <result-id> at the beginning of an "
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400542 "instruction, found '"
543 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400544 return SPV_ERROR_INVALID_TEXT;
545 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400546 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100547 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400548 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100549 pInst->wordCount++;
550
David Neto78c3b432015-08-27 13:03:52 -0400551 // Maintains the ordered list of expected operand types.
552 // For many instructions we only need the {numTypes, operandTypes}
553 // entries in opcodeEntry. However, sometimes we need to modify
554 // the list as we parse the operands. This occurs when an operand
555 // has its own logical operands (such as the LocalSize operand for
556 // ExecutionMode), or for extended instructions that may have their
557 // own operands depending on the selected extended instruction.
558 spv_operand_pattern_t expectedOperands(
559 opcodeEntry->operandTypes,
560 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400561
David Neto78c3b432015-08-27 13:03:52 -0400562 while (!expectedOperands.empty()) {
563 const spv_operand_type_t type = expectedOperands.front();
564 expectedOperands.pop_front();
565
566 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400567 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400568
569 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
570 // Handle the <result-id> for value generating instructions.
571 // We've already consumed it from the text stream. Here
572 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400573 spv_position_t temp_pos = context->position();
574 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
575 result_id.c_str(), pInst, nullptr);
576 result_id_position = context->position();
577 // Because we are injecting we have to reset the position afterwards.
578 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400579 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100580 } else {
David Neto78c3b432015-08-27 13:03:52 -0400581 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400582 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400583 if (error == SPV_END_OF_STREAM) {
584 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400585 // This would have been the last potential operand for the
586 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400587 // and we didn't find one. We're finished parsing this instruction.
588 break;
589 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400590 context->diagnostic() << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400591 return SPV_ERROR_INVALID_TEXT;
592 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100593 }
David Neto78c3b432015-08-27 13:03:52 -0400594 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
595
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400596 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400597 if (spvOperandIsOptional(type)) {
598 break;
599 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400600 context->diagnostic()
601 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400602 return SPV_ERROR_INVALID_TEXT;
603 }
604 }
605
606 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400607 error = context->getWord(operandValue, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400608 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400609 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400610 return error;
611 }
David Neto78c3b432015-08-27 13:03:52 -0400612
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400613 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
614 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400615
616 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
617 return SPV_SUCCESS;
618
Lei Zhang40056702015-09-11 14:31:27 -0400619 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400620
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400621 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100622 }
623 }
624
625 pInst->words[0] = spvOpcodeMake(pInst->wordCount, opcodeEntry->opcode);
626
627 return SPV_SUCCESS;
628}
629
David Netoc9786432015-09-01 18:05:14 -0400630namespace {
631
632// Translates a given assembly language module into binary form.
633// If a diagnostic is generated, it is not yet marked as being
634// for a text-based input.
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400635spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
636 const spv_text text,
637 spv_assembly_syntax_format_t format,
638 spv_binary* pBinary,
639 spv_diagnostic* pDiagnostic) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400640 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
641 libspirv::AssemblyContext context(text, pDiagnostic);
Lei Zhang40056702015-09-11 14:31:27 -0400642 if (!text->str || !text->length) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400643 context.diagnostic() << "Text stream is empty.";
Lei Zhang40056702015-09-11 14:31:27 -0400644 return SPV_ERROR_INVALID_TEXT;
645 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400646 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400647 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400648 }
Lei Zhang40056702015-09-11 14:31:27 -0400649 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100650
651 // NOTE: Ensure diagnostic is zero initialised
652 *pDiagnostic = {};
653
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100654 std::vector<spv_instruction_t> instructions;
655
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400656 if (context.advance()) {
657 context.diagnostic() << "Text stream is empty.";
Lei Zhang40056702015-09-11 14:31:27 -0400658 return SPV_ERROR_INVALID_TEXT;
659 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100660
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400661 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
662 while (context.hasText()) {
663 spv_instruction_t inst = {};
664 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
671 instructions.push_back(inst);
672
673 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100674 }
675
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100676 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400677 for (auto& inst : instructions) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100678 totalSize += inst.wordCount;
679 }
680
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400681 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400682 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100683 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400684 for (auto& inst : instructions) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100685 memcpy(data + currentIndex, inst.words, sizeof(uint32_t) * inst.wordCount);
686 currentIndex += inst.wordCount;
687 }
688
689 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400690 if (!binary) {
691 delete[] data;
692 return SPV_ERROR_OUT_OF_MEMORY;
693 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100694 binary->code = data;
695 binary->wordCount = totalSize;
696
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400697 spv_result_t error = spvBinaryHeaderSet(binary, context.getBound());
Lei Zhang40056702015-09-11 14:31:27 -0400698 if (error) {
699 spvBinaryDestroy(binary);
700 return error;
701 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100702
703 *pBinary = binary;
704
705 return SPV_SUCCESS;
706}
707
Lei Zhange78a7c12015-09-10 17:07:21 -0400708} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400709
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400710spv_result_t spvTextToBinary(const char* input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400711 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400712 const spv_opcode_table opcodeTable,
713 const spv_operand_table operandTable,
714 const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400715 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400716 return spvTextWithFormatToBinary(
717 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
718 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
719}
720
721spv_result_t spvTextWithFormatToBinary(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400722 const char* input_text, const uint64_t input_text_size,
Lei Zhang06efdc52015-09-10 14:00:00 -0400723 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
724 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400725 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400726 spv_text_t text = {input_text, input_text_size};
727
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400728 libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable);
Lei Zhang06efdc52015-09-10 14:00:00 -0400729 spv_result_t result =
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400730 spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400731 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
732
733 return result;
734}
735
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100736void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400737 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100738 if (text->str) {
739 delete[] text->str;
740 }
741 delete text;
742}