blob: 32bbf85a1ff9fa260dea06a0d69dbbe42fca8924 [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 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"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010050
Lei Zhang610c5252015-09-09 10:36:48 -040051using spvutils::BitwiseCast;
52
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.
Dejan Mircevski14c4b102015-09-29 17:07:21 -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
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400155namespace {
156
157/// Parses an immediate integer from text, guarding against overflow. If
158/// successful, adds the parsed value to pInst, advances the context past it,
159/// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics,
160/// and returns SPV_ERROR_INVALID_TEXT.
161spv_result_t encodeImmediate(libspirv::AssemblyContext* context,
162 const char* text, spv_instruction_t* pInst) {
163 assert(*text == '!');
164 const char* begin = text + 1;
165 char* end = nullptr;
Dejan Mircevski97e2c8f2015-09-29 17:58:37 -0400166 const uint64_t parseResult = strtoull(begin, &end, 0);
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400167 size_t length = end - begin;
168 if (length != strlen(begin)) {
169 context->diagnostic() << "Invalid immediate integer '" << text << "'.";
170 return SPV_ERROR_INVALID_TEXT;
Dejan Mircevski97e2c8f2015-09-29 17:58:37 -0400171 } else if (length > 10 || (parseResult >> 32) != 0) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400172 context->diagnostic() << "Immediate integer '" << text
Dejan Mircevski114206e2015-09-30 09:49:00 -0400173 << "' is outside the unsigned 32-bit range.";
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400174 return SPV_ERROR_INVALID_TEXT;
175 }
176 context->binaryEncodeU32(parseResult, pInst);
177 context->seekForward(strlen(text));
178 return SPV_SUCCESS;
179}
180
181} // anonymous namespace
182
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400183/// @brief Translate an Opcode operand to binary form
184///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400185/// @param[in] grammar the grammar to use for compilation
186/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400187/// @param[in] type of the operand
188/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400189/// @param[out] pInst return binary Opcode
190/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400191///
192/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400193spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400194 libspirv::AssemblyContext* context,
195 const spv_operand_type_t type,
196 const char* textValue,
197 spv_instruction_t* pInst,
198 spv_operand_pattern_t* pExpectedOperands) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100199 // NOTE: Handle immediate int in the stream
200 if ('!' == textValue[0]) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400201 if (auto error = encodeImmediate(context, textValue, pInst)) {
202 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400203 }
Dejan Mircevski897bff92015-09-29 10:38:18 -0400204 *pExpectedOperands =
205 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100206 return SPV_SUCCESS;
207 }
208
209 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400210 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Netoe3f70b92015-08-27 13:50:05 -0400211 case SPV_OPERAND_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400212 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
213 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netob14a7272015-09-25 13:56:09 -0400214 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
215 case SPV_OPERAND_TYPE_RESULT_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400216 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100217 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100218 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400219 context->diagnostic() << "Expected id to start with %.";
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400220 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100221 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400222 if (!spvIsValidID(textValue)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400223 context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400224 return SPV_ERROR_INVALID_TEXT;
225 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400226 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
David Netob5dc8fc2015-10-06 16:22:00 -0400227 spvInstructionAddWord(pInst, id);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100228 } break;
229 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
230 // NOTE: Special case for extension instruction lookup
231 if (OpExtInst == pInst->opcode) {
232 spv_ext_inst_desc extInst;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400233 if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400234 context->diagnostic() << "Invalid extended instruction name '"
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400235 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400236 return SPV_ERROR_INVALID_TEXT;
237 }
David Netob5dc8fc2015-10-06 16:22:00 -0400238 spvInstructionAddWord(pInst, extInst->ext_inst);
David Neto78c3b432015-08-27 13:03:52 -0400239
240 // Prepare to parse the operands for the extended instructions.
241 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
242
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100243 return SPV_SUCCESS;
244 }
Lei Zhang6d415812015-09-15 13:36:21 -0400245 } // Fall through for the general case.
Lei Zhangb41d1502015-09-14 15:22:23 -0400246 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
David Neto561dc4e2015-09-25 14:23:29 -0400247 case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE:
248 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100249 spv_literal_t literal = {};
Lei Zhang6d415812015-09-15 13:36:21 -0400250 spv_result_t error = spvTextToLiteral(textValue, &literal);
251 if (error != SPV_SUCCESS) {
252 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
253 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400254 context->diagnostic() << "Invalid literal number '" << textValue
255 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400256 return SPV_ERROR_INVALID_TEXT;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400257 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100258 switch (literal.type) {
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400259 // We do not have to print diagnostics here because spvBinaryEncode*
260 // prints diagnostic messages on failure.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100261 case SPV_LITERAL_TYPE_INT_32:
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400262 if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32),
263 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400264 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100265 break;
266 case SPV_LITERAL_TYPE_INT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400267 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64),
268 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400269 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100270 } break;
271 case SPV_LITERAL_TYPE_UINT_32: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400272 if (context->binaryEncodeU32(literal.value.u32, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400273 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100274 } break;
275 case SPV_LITERAL_TYPE_UINT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400276 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64),
277 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400278 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100279 } break;
280 case SPV_LITERAL_TYPE_FLOAT_32: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400281 if (context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f),
282 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400283 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100284 } break;
285 case SPV_LITERAL_TYPE_FLOAT_64: {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400286 if (context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d),
287 pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400288 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100289 } break;
290 case SPV_LITERAL_TYPE_STRING: {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400291 context->diagnostic()
292 << "Expected literal number, found literal string '" << textValue
293 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400294 return SPV_FAILED_MATCH;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100295 } break;
296 default:
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400297 context->diagnostic() << "Invalid literal number '" << textValue
298 << "'";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100299 return SPV_ERROR_INVALID_TEXT;
300 }
301 } break;
David Neto78c3b432015-08-27 13:03:52 -0400302 case SPV_OPERAND_TYPE_LITERAL_STRING:
303 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400304 spv_literal_t literal = {};
305 spv_result_t error = spvTextToLiteral(textValue, &literal);
306 if (error != SPV_SUCCESS) {
307 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
Lei Zhang40056702015-09-11 14:31:27 -0400308 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400309 context->diagnostic() << "Invalid literal string '" << textValue
310 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400311 return SPV_ERROR_INVALID_TEXT;
312 }
Lei Zhang6d415812015-09-15 13:36:21 -0400313 if (literal.type != SPV_LITERAL_TYPE_STRING) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400314 context->diagnostic()
315 << "Expected literal string, found literal number '" << textValue
316 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400317 return SPV_FAILED_MATCH;
318 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100319
320 // NOTE: Special case for extended instruction library import
321 if (OpExtInstImport == pInst->opcode) {
Lei Zhang6d415812015-09-15 13:36:21 -0400322 pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100323 }
324
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400325 if (context->binaryEncodeString(literal.value.str, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400326 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100327 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400328 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
329 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
330 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400331 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400332 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400333 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
334 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400335 if (grammar.parseMaskOperand(type, textValue, &value)) {
336 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400337 << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400338 return SPV_ERROR_INVALID_TEXT;
339 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400340 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400341 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400342 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400343 } break;
344 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
345 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400346 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
347 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400348 if (error == SPV_FAILED_MATCH) {
349 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400350 error = spvTextEncodeOperand(grammar, context,
351 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
352 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400353 }
354 if (error == SPV_FAILED_MATCH) {
355 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400356 error =
357 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
358 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400359 }
360 if (error) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400361 context->diagnostic() << "Invalid word following !<integer>: "
362 << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400363 return error;
364 }
365 if (pExpectedOperands->empty()) {
366 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
367 }
David Neto36b0c0f2015-09-16 18:32:54 -0400368 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100369 default: {
370 // NOTE: All non literal operands are handled here using the operand
371 // table.
372 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400373 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400374 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400375 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400376 return SPV_ERROR_INVALID_TEXT;
377 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400378 if (context->binaryEncodeU32(entry->value, pInst)) {
379 context->diagnostic() << "Invalid " << spvOperandTypeStr(type) << " '"
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400380 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400381 return SPV_ERROR_INVALID_TEXT;
382 }
David Neto78c3b432015-08-27 13:03:52 -0400383
384 // Prepare to parse the operands for this logical operand.
385 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100386 } break;
387 }
388 return SPV_SUCCESS;
389}
390
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400391namespace {
392
393/// Encodes an instruction started by !<integer> at the given position in text.
394///
395/// Puts the encoded words into *pInst. If successful, moves position past the
396/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
397/// leaves position pointing to the error in text.
398spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400399 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400400 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400401 std::string firstWord;
402 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400403 auto error = context->getWord(firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400404 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400405 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400406 return error;
407 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400408
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400409 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
410 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400411 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400412 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400413 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400414 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400415
416 // Otherwise, there must be an operand that's either a literal, an ID, or
417 // an immediate.
418 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400419 if ((error = context->getWord(operandValue, &nextPosition))) {
420 context->diagnostic() << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400421 return error;
422 }
423
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400424 if (operandValue == "=") {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400425 context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400426 return SPV_ERROR_INVALID_TEXT;
427 }
428
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400429 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
430 // expanded.
431 spv_operand_pattern_t dummyExpectedOperands;
432 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400433 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
434 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400435 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400436 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400437 }
438 return SPV_SUCCESS;
439}
440
441} // anonymous namespace
442
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400443/// @brief Translate single Opcode and operands to binary form
444///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400445/// @param[in] grammar the grammar to use for compilation
446/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400447/// @param[in] text stream to translate
448/// @param[in] format the assembly syntax format of text
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400449/// @param[out] pInst returned binary Opcode
450/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400451///
452/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400453spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400454 libspirv::AssemblyContext* context,
455 spv_assembly_syntax_format_t format,
456 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400457 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400458 if ('!' == context->peek()) {
459 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400460 }
461
Lei Zhangdfc50082015-08-21 11:50:55 -0400462 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400463 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
464 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400465
Lei Zhang06efdc52015-09-10 14:00:00 -0400466 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100467 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400468 spv_result_t error = context->getWord(firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400469 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400470 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400471 return error;
472 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100473
Lei Zhang06efdc52015-09-10 14:00:00 -0400474 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400475 std::string result_id;
476 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400477 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400478 opcodeName = firstWord;
479 } else {
480 // If the first word of this instruction is not an opcode, we must be
481 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400482 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400483 context->diagnostic()
Lei Zhang40056702015-09-11 14:31:27 -0400484 << "Expected <opcode> at the beginning of an instruction, found '"
485 << firstWord << "'.";
486 return SPV_ERROR_INVALID_TEXT;
487 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400488
489 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400490 if ('%' != result_id.front()) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400491 context->diagnostic()
492 << "Expected <opcode> or <result-id> at the beginning "
493 "of an instruction, found '"
494 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400495 return SPV_ERROR_INVALID_TEXT;
496 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400497 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400498
499 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400500 context->setPosition(nextPosition);
501 if (context->advance()) {
502 context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhang40056702015-09-11 14:31:27 -0400503 return SPV_ERROR_INVALID_TEXT;
504 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400505 std::string equal_sign;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400506 error = context->getWord(equal_sign, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400507 if ("=" != equal_sign) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400508 context->diagnostic() << "'=' expected after result id.";
Lei Zhang40056702015-09-11 14:31:27 -0400509 return SPV_ERROR_INVALID_TEXT;
510 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400511
512 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400513 context->setPosition(nextPosition);
514 if (context->advance()) {
515 context->diagnostic() << "Expected opcode, found end of stream.";
Lei Zhang40056702015-09-11 14:31:27 -0400516 return SPV_ERROR_INVALID_TEXT;
517 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400518 error = context->getWord(opcodeName, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400519 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400520 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400521 return error;
522 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400523 if (!context->startsWithOp()) {
524 context->diagnostic() << "Invalid Opcode prefix '" << opcodeName << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400525 return SPV_ERROR_INVALID_TEXT;
526 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400527 }
528
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100529 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400530 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100531
532 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400533 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400534 if (error) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400535 context->diagnostic() << "Invalid Opcode name '" << context->getWord()
536 << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400537 return error;
538 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400539 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
540 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400541 if (opcodeEntry->hasResult && result_id.empty()) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400542 context->diagnostic() << "Expected <result-id> at the beginning of an "
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400543 "instruction, found '"
544 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400545 return SPV_ERROR_INVALID_TEXT;
546 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400547 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100548 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400549 context->setPosition(nextPosition);
David Netob5dc8fc2015-10-06 16:22:00 -0400550 // Reserve the first word for the instruction.
551 spvInstructionAddWord(pInst, 0);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100552
David Neto78c3b432015-08-27 13:03:52 -0400553 // Maintains the ordered list of expected operand types.
554 // For many instructions we only need the {numTypes, operandTypes}
555 // entries in opcodeEntry. However, sometimes we need to modify
556 // the list as we parse the operands. This occurs when an operand
557 // has its own logical operands (such as the LocalSize operand for
558 // ExecutionMode), or for extended instructions that may have their
559 // own operands depending on the selected extended instruction.
560 spv_operand_pattern_t expectedOperands(
561 opcodeEntry->operandTypes,
562 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400563
David Neto78c3b432015-08-27 13:03:52 -0400564 while (!expectedOperands.empty()) {
565 const spv_operand_type_t type = expectedOperands.front();
566 expectedOperands.pop_front();
567
568 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400569 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400570
571 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
572 // Handle the <result-id> for value generating instructions.
573 // We've already consumed it from the text stream. Here
574 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400575 spv_position_t temp_pos = context->position();
576 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
577 result_id.c_str(), pInst, nullptr);
578 result_id_position = context->position();
579 // Because we are injecting we have to reset the position afterwards.
580 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400581 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100582 } else {
David Neto78c3b432015-08-27 13:03:52 -0400583 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400584 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400585 if (error == SPV_END_OF_STREAM) {
586 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400587 // This would have been the last potential operand for the
588 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400589 // and we didn't find one. We're finished parsing this instruction.
590 break;
591 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400592 context->diagnostic() << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400593 return SPV_ERROR_INVALID_TEXT;
594 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100595 }
David Neto78c3b432015-08-27 13:03:52 -0400596 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
597
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400598 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400599 if (spvOperandIsOptional(type)) {
600 break;
601 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400602 context->diagnostic()
603 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400604 return SPV_ERROR_INVALID_TEXT;
605 }
606 }
607
608 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400609 error = context->getWord(operandValue, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400610 if (error) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400611 context->diagnostic() << "Internal Error";
Lei Zhang40056702015-09-11 14:31:27 -0400612 return error;
613 }
David Neto78c3b432015-08-27 13:03:52 -0400614
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400615 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
616 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400617
618 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
619 return SPV_SUCCESS;
620
Lei Zhang40056702015-09-11 14:31:27 -0400621 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400622
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400623 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100624 }
625 }
626
David Netob5dc8fc2015-10-06 16:22:00 -0400627 if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
628 context->diagnostic() << "Instruction too long: " << pInst->words.size()
629 << " words, but the limit is "
630 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
631 return SPV_ERROR_INVALID_TEXT;
632 }
633
634 pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100635
636 return SPV_SUCCESS;
637}
638
David Netoc9786432015-09-01 18:05:14 -0400639namespace {
640
641// Translates a given assembly language module into binary form.
642// If a diagnostic is generated, it is not yet marked as being
643// for a text-based input.
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400644spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
645 const spv_text text,
646 spv_assembly_syntax_format_t format,
647 spv_binary* pBinary,
648 spv_diagnostic* pDiagnostic) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400649 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
650 libspirv::AssemblyContext context(text, pDiagnostic);
Lei Zhang40056702015-09-11 14:31:27 -0400651 if (!text->str || !text->length) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400652 context.diagnostic() << "Text stream is empty.";
Lei Zhang40056702015-09-11 14:31:27 -0400653 return SPV_ERROR_INVALID_TEXT;
654 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400655 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400656 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400657 }
Lei Zhang40056702015-09-11 14:31:27 -0400658 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100659
660 // NOTE: Ensure diagnostic is zero initialised
661 *pDiagnostic = {};
662
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100663 std::vector<spv_instruction_t> instructions;
664
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400665 if (context.advance()) {
666 context.diagnostic() << "Text stream is empty.";
Lei Zhang40056702015-09-11 14:31:27 -0400667 return SPV_ERROR_INVALID_TEXT;
668 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100669
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400670 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
671 while (context.hasText()) {
David Netob5dc8fc2015-10-06 16:22:00 -0400672 instructions.push_back({});
673 spv_instruction_t& inst = instructions.back();
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400674 inst.extInstType = extInstType;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100675
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400676 if (spvTextEncodeOpcode(grammar, &context, format, &inst)) {
677 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400678 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400679 extInstType = inst.extInstType;
680
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400681 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100682 }
683
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100684 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400685 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400686 totalSize += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100687 }
688
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400689 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400690 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100691 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400692 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400693 memcpy(data + currentIndex, inst.words.data(), sizeof(uint32_t) * inst.words.size());
694 currentIndex += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100695 }
696
697 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400698 if (!binary) {
699 delete[] data;
700 return SPV_ERROR_OUT_OF_MEMORY;
701 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100702 binary->code = data;
703 binary->wordCount = totalSize;
704
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400705 spv_result_t error = spvBinaryHeaderSet(binary, context.getBound());
Lei Zhang40056702015-09-11 14:31:27 -0400706 if (error) {
707 spvBinaryDestroy(binary);
708 return error;
709 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100710
711 *pBinary = binary;
712
713 return SPV_SUCCESS;
714}
715
Lei Zhange78a7c12015-09-10 17:07:21 -0400716} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400717
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400718spv_result_t spvTextToBinary(const char* input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400719 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400720 const spv_opcode_table opcodeTable,
721 const spv_operand_table operandTable,
722 const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400723 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400724 return spvTextWithFormatToBinary(
725 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
726 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
727}
728
729spv_result_t spvTextWithFormatToBinary(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400730 const char* input_text, const uint64_t input_text_size,
Lei Zhang06efdc52015-09-10 14:00:00 -0400731 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
732 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400733 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400734 spv_text_t text = {input_text, input_text_size};
735
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400736 libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable);
Lei Zhang06efdc52015-09-10 14:00:00 -0400737 spv_result_t result =
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400738 spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400739 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
740
741 return result;
742}
743
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100744void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400745 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100746 if (text->str) {
747 delete[] text->str;
748 }
749 delete text;
750}