blob: ddea72489515e17bf2c1ed69b5c2ea13e8fefefa [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)) {
David Netoac508b02015-10-09 15:48:09 -0400169 return context->diagnostic() << "Invalid immediate integer '" << text << "'.";
Dejan Mircevski97e2c8f2015-09-29 17:58:37 -0400170 } else if (length > 10 || (parseResult >> 32) != 0) {
David Netoac508b02015-10-09 15:48:09 -0400171 return 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 }
174 context->binaryEncodeU32(parseResult, pInst);
175 context->seekForward(strlen(text));
176 return SPV_SUCCESS;
177}
178
179} // anonymous namespace
180
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400181/// @brief Translate an Opcode operand to binary form
182///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400183/// @param[in] grammar the grammar to use for compilation
184/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400185/// @param[in] type of the operand
186/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400187/// @param[out] pInst return binary Opcode
188/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400189///
190/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400191spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400192 libspirv::AssemblyContext* context,
193 const spv_operand_type_t type,
194 const char* textValue,
195 spv_instruction_t* pInst,
196 spv_operand_pattern_t* pExpectedOperands) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100197 // NOTE: Handle immediate int in the stream
198 if ('!' == textValue[0]) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400199 if (auto error = encodeImmediate(context, textValue, pInst)) {
200 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400201 }
Dejan Mircevski897bff92015-09-29 10:38:18 -0400202 *pExpectedOperands =
203 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100204 return SPV_SUCCESS;
205 }
206
207 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400208 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Netoe3f70b92015-08-27 13:50:05 -0400209 case SPV_OPERAND_TYPE_ID:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400210 case SPV_OPERAND_TYPE_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 {
David Netoac508b02015-10-09 15:48:09 -0400218 return context->diagnostic() << "Expected id to start with %.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100219 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400220 if (!spvIsValidID(textValue)) {
David Netoac508b02015-10-09 15:48:09 -0400221 return context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400222 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400223 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400224 if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
David Netob5dc8fc2015-10-06 16:22:00 -0400225 spvInstructionAddWord(pInst, id);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100226 } break;
227 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
228 // NOTE: Special case for extension instruction lookup
229 if (OpExtInst == pInst->opcode) {
230 spv_ext_inst_desc extInst;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400231 if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
David Netoac508b02015-10-09 15:48:09 -0400232 return context->diagnostic() << "Invalid extended instruction name '"
233 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400234 }
David Netob5dc8fc2015-10-06 16:22:00 -0400235 spvInstructionAddWord(pInst, extInst->ext_inst);
David Neto78c3b432015-08-27 13:03:52 -0400236
237 // Prepare to parse the operands for the extended instructions.
238 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
239
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100240 return SPV_SUCCESS;
241 }
Lei Zhang6d415812015-09-15 13:36:21 -0400242 } // Fall through for the general case.
Lei Zhangb41d1502015-09-14 15:22:23 -0400243 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
David Neto561dc4e2015-09-25 14:23:29 -0400244 case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE:
245 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100246 spv_literal_t literal = {};
Lei Zhang6d415812015-09-15 13:36:21 -0400247 spv_result_t error = spvTextToLiteral(textValue, &literal);
248 if (error != SPV_SUCCESS) {
249 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
250 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
David Netoac508b02015-10-09 15:48:09 -0400251 return context->diagnostic() << "Invalid literal number '" << textValue
252 << "'.";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400253 }
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400254
255 // The encoding for OpConstant, OpSpecConstant and OpSwitch all
256 // depend on either their own result-id or the result-id of
257 // one of their parameters.
258 if (OpConstant == pInst->opcode || OpSpecConstant == pInst->opcode) {
259 // Special cases for encoding possibly non-32-bit literals here.
260 libspirv::IdType type =
261 context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
262 if (!libspirv::isScalarFloating(type) &&
263 !libspirv::isScalarIntegral(type)) {
264 spv_opcode_desc d;
265 const char* opcode_name = "opcode";
266 if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
267 opcode_name = d->name;
268 }
David Netoac508b02015-10-09 15:48:09 -0400269 return context->diagnostic()
270 << "Type for " << opcode_name
271 << " must be a scalar floating point or integer type";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400272 }
273 } else if (pInst->opcode == OpSwitch) {
274 // We need to know the value of the selector.
275 libspirv::IdType type =
276 context->getTypeOfValueInstruction(pInst->words[1]);
277 if (type.type_class != libspirv::IdTypeClass::kScalarIntegerType) {
David Netoac508b02015-10-09 15:48:09 -0400278 return context->diagnostic()
279 << "The selector operand for OpSwitch must be the result"
280 " of an instruction that generates an integer scalar";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400281 }
282 }
283 // TODO(awoloszyn): Generate the correct assembly for arbitrary
284 // bitwidths here instead of falling though.
285
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100286 switch (literal.type) {
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400287 // We do not have to print diagnostics here because spvBinaryEncode*
288 // prints diagnostic messages on failure.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100289 case SPV_LITERAL_TYPE_INT_32:
David Netoac508b02015-10-09 15:48:09 -0400290 context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32),
291 pInst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100292 break;
293 case SPV_LITERAL_TYPE_INT_64: {
David Netoac508b02015-10-09 15:48:09 -0400294 context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64),
295 pInst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100296 } break;
297 case SPV_LITERAL_TYPE_UINT_32: {
David Netoac508b02015-10-09 15:48:09 -0400298 context->binaryEncodeU32(literal.value.u32, pInst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100299 } break;
300 case SPV_LITERAL_TYPE_UINT_64: {
David Netoac508b02015-10-09 15:48:09 -0400301 context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64),
302 pInst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100303 } break;
304 case SPV_LITERAL_TYPE_FLOAT_32: {
David Netoac508b02015-10-09 15:48:09 -0400305 context->binaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f),
306 pInst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100307 } break;
308 case SPV_LITERAL_TYPE_FLOAT_64: {
David Netoac508b02015-10-09 15:48:09 -0400309 context->binaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d),
310 pInst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100311 } break;
312 case SPV_LITERAL_TYPE_STRING: {
David Netoac508b02015-10-09 15:48:09 -0400313 return context->diagnostic(SPV_FAILED_MATCH)
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400314 << "Expected literal number, found literal string '" << textValue
315 << "'.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100316 } break;
317 default:
David Netoac508b02015-10-09 15:48:09 -0400318 return context->diagnostic() << "Invalid literal number '"
319 << textValue << "'";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100320 }
321 } break;
David Neto78c3b432015-08-27 13:03:52 -0400322 case SPV_OPERAND_TYPE_LITERAL_STRING:
323 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400324 spv_literal_t literal = {};
325 spv_result_t error = spvTextToLiteral(textValue, &literal);
326 if (error != SPV_SUCCESS) {
327 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
Lei Zhang40056702015-09-11 14:31:27 -0400328 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
David Netoac508b02015-10-09 15:48:09 -0400329 return context->diagnostic() << "Invalid literal string '" << textValue
330 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400331 }
Lei Zhang6d415812015-09-15 13:36:21 -0400332 if (literal.type != SPV_LITERAL_TYPE_STRING) {
David Netoac508b02015-10-09 15:48:09 -0400333 return context->diagnostic(SPV_FAILED_MATCH)
334 << "Expected literal string, found literal number '" << textValue
335 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400336 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100337
338 // NOTE: Special case for extended instruction library import
339 if (OpExtInstImport == pInst->opcode) {
Lei Zhang6d415812015-09-15 13:36:21 -0400340 pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100341 }
342
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400343 if (context->binaryEncodeString(literal.value.str, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400344 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100345 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400346 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
347 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
348 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400349 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400350 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400351 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
352 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400353 if (grammar.parseMaskOperand(type, textValue, &value)) {
David Netoac508b02015-10-09 15:48:09 -0400354 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
355 << " '" << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400356 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400357 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400358 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400359 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400360 } break;
361 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
362 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400363 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
364 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400365 if (error == SPV_FAILED_MATCH) {
366 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400367 error = spvTextEncodeOperand(grammar, context,
368 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
369 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400370 }
371 if (error == SPV_FAILED_MATCH) {
372 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400373 error =
374 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
375 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400376 }
377 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400378 return context->diagnostic(error)
379 << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400380 }
381 if (pExpectedOperands->empty()) {
382 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
383 }
David Neto36b0c0f2015-09-16 18:32:54 -0400384 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100385 default: {
386 // NOTE: All non literal operands are handled here using the operand
387 // table.
388 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400389 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
David Netoac508b02015-10-09 15:48:09 -0400390 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
391 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400392 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400393 if (context->binaryEncodeU32(entry->value, pInst)) {
David Netoac508b02015-10-09 15:48:09 -0400394 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
395 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400396 }
David Neto78c3b432015-08-27 13:03:52 -0400397
398 // Prepare to parse the operands for this logical operand.
399 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100400 } break;
401 }
402 return SPV_SUCCESS;
403}
404
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400405namespace {
406
407/// Encodes an instruction started by !<integer> at the given position in text.
408///
409/// Puts the encoded words into *pInst. If successful, moves position past the
410/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
411/// leaves position pointing to the error in text.
412spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400413 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400414 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400415 std::string firstWord;
416 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400417 auto error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400418 if (error) return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400419
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400420 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
421 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400422 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400423 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400424 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400425 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400426
427 // Otherwise, there must be an operand that's either a literal, an ID, or
428 // an immediate.
429 std::string operandValue;
David Netoac508b02015-10-09 15:48:09 -0400430 if ((error = context->getWord(operandValue, &nextPosition)))
431 return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400432
David Netoac508b02015-10-09 15:48:09 -0400433 if (operandValue == "=")
434 return context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400435
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400436 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
437 // expanded.
438 spv_operand_pattern_t dummyExpectedOperands;
439 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400440 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
441 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400442 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400443 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400444 }
445 return SPV_SUCCESS;
446}
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400447} // anonymous namespace
448
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400449/// @brief Translate single Opcode and operands to binary form
450///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400451/// @param[in] grammar the grammar to use for compilation
452/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400453/// @param[in] text stream to translate
454/// @param[in] format the assembly syntax format of text
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400455/// @param[out] pInst returned binary Opcode
456/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400457///
458/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400459spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400460 libspirv::AssemblyContext* context,
461 spv_assembly_syntax_format_t format,
462 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400463 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400464 if ('!' == context->peek()) {
465 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400466 }
467
Lei Zhangdfc50082015-08-21 11:50:55 -0400468 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400469 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
470 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400471
Lei Zhang06efdc52015-09-10 14:00:00 -0400472 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100473 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400474 spv_result_t error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400475 if (error) return context->diagnostic() << "Internal Error";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100476
Lei Zhang06efdc52015-09-10 14:00:00 -0400477 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400478 std::string result_id;
479 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400480 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400481 opcodeName = firstWord;
482 } else {
483 // If the first word of this instruction is not an opcode, we must be
484 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400485 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
David Netoac508b02015-10-09 15:48:09 -0400486 return context->diagnostic()
487 << "Expected <opcode> at the beginning of an instruction, found '"
488 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400489 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400490
491 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400492 if ('%' != result_id.front()) {
David Netoac508b02015-10-09 15:48:09 -0400493 return context->diagnostic()
494 << "Expected <opcode> or <result-id> at the beginning "
495 "of an instruction, found '"
496 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400497 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400498 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400499
500 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400501 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400502 if (context->advance())
503 return context->diagnostic() << "Expected '=', found end of stream.";
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);
David Netoac508b02015-10-09 15:48:09 -0400506 if ("=" != equal_sign)
507 return context->diagnostic() << "'=' expected after result id.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400508
509 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400510 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400511 if (context->advance())
512 return context->diagnostic() << "Expected opcode, found end of stream.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400513 error = context->getWord(opcodeName, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400514 if (error)
515 return context->diagnostic(error) << "Internal Error";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400516 if (!context->startsWithOp()) {
David Netoac508b02015-10-09 15:48:09 -0400517 return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName
518 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400519 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400520 }
521
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100522 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400523 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100524
525 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400526 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400527 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400528 return context->diagnostic(error) << "Invalid Opcode name '"
529 << context->getWord() << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400530 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400531 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
532 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400533 if (opcodeEntry->hasResult && result_id.empty()) {
David Netoac508b02015-10-09 15:48:09 -0400534 return context->diagnostic()
535 << "Expected <result-id> at the beginning of an "
536 "instruction, found '"
537 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400538 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400539 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100540 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400541 context->setPosition(nextPosition);
David Netob5dc8fc2015-10-06 16:22:00 -0400542 // Reserve the first word for the instruction.
543 spvInstructionAddWord(pInst, 0);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100544
David Neto78c3b432015-08-27 13:03:52 -0400545 // Maintains the ordered list of expected operand types.
546 // For many instructions we only need the {numTypes, operandTypes}
547 // entries in opcodeEntry. However, sometimes we need to modify
548 // the list as we parse the operands. This occurs when an operand
549 // has its own logical operands (such as the LocalSize operand for
550 // ExecutionMode), or for extended instructions that may have their
551 // own operands depending on the selected extended instruction.
552 spv_operand_pattern_t expectedOperands(
553 opcodeEntry->operandTypes,
554 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400555
David Neto78c3b432015-08-27 13:03:52 -0400556 while (!expectedOperands.empty()) {
557 const spv_operand_type_t type = expectedOperands.front();
558 expectedOperands.pop_front();
559
560 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400561 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400562
563 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
564 // Handle the <result-id> for value generating instructions.
565 // We've already consumed it from the text stream. Here
566 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400567 spv_position_t temp_pos = context->position();
568 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
569 result_id.c_str(), pInst, nullptr);
570 result_id_position = context->position();
571 // Because we are injecting we have to reset the position afterwards.
572 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400573 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100574 } else {
David Neto78c3b432015-08-27 13:03:52 -0400575 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400576 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400577 if (error == SPV_END_OF_STREAM) {
578 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400579 // This would have been the last potential operand for the
580 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400581 // and we didn't find one. We're finished parsing this instruction.
582 break;
583 } else {
David Netoac508b02015-10-09 15:48:09 -0400584 return context->diagnostic()
585 << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400586 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100587 }
David Neto78c3b432015-08-27 13:03:52 -0400588 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
589
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400590 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400591 if (spvOperandIsOptional(type)) {
592 break;
593 } else {
David Netoac508b02015-10-09 15:48:09 -0400594 return context->diagnostic()
595 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400596 }
597 }
598
599 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400600 error = context->getWord(operandValue, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400601 if (error) return context->diagnostic(error) << "Internal Error";
David Neto78c3b432015-08-27 13:03:52 -0400602
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400603 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
604 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400605
606 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
607 return SPV_SUCCESS;
608
Lei Zhang40056702015-09-11 14:31:27 -0400609 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400610
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400611 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100612 }
613 }
614
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400615 if (spvOpcodeGeneratesType(pInst->opcode)) {
616 if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
617 return SPV_ERROR_INVALID_TEXT;
618 }
619 } else if (opcodeEntry->hasType) {
620 // SPIR-V dictates that if an instruction has both a return value and a
621 // type ID then the type id is first, and the return value is second.
622 assert(opcodeEntry->hasResult &&
623 "Unknown opcode: has a type but no result.");
624 context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
625 }
626
David Netob5dc8fc2015-10-06 16:22:00 -0400627 if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Netoac508b02015-10-09 15:48:09 -0400628 return context->diagnostic()
629 << "Instruction too long: " << pInst->words.size()
630 << " words, but the limit is "
631 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
David Netob5dc8fc2015-10-06 16:22:00 -0400632 }
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);
David Netoac508b02015-10-09 15:48:09 -0400651 if (!text->str || !text->length)
652 return context.diagnostic() << "Text stream is empty.";
653
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400654 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400655 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400656 }
Lei Zhang40056702015-09-11 14:31:27 -0400657 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100658
659 // NOTE: Ensure diagnostic is zero initialised
660 *pDiagnostic = {};
661
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100662 std::vector<spv_instruction_t> instructions;
663
David Netoac508b02015-10-09 15:48:09 -0400664 if (context.advance()) return context.diagnostic() << "Text stream is empty.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100665
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400666 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
667 while (context.hasText()) {
David Netob5dc8fc2015-10-06 16:22:00 -0400668 instructions.push_back({});
669 spv_instruction_t& inst = instructions.back();
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400670 inst.extInstType = extInstType;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100671
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400672 if (spvTextEncodeOpcode(grammar, &context, format, &inst)) {
673 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400674 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400675 extInstType = inst.extInstType;
676
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400677 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100678 }
679
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100680 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400681 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400682 totalSize += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100683 }
684
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400685 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400686 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100687 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400688 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400689 memcpy(data + currentIndex, inst.words.data(), sizeof(uint32_t) * inst.words.size());
690 currentIndex += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100691 }
692
693 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400694 if (!binary) {
695 delete[] data;
696 return SPV_ERROR_OUT_OF_MEMORY;
697 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100698 binary->code = data;
699 binary->wordCount = totalSize;
700
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400701 spv_result_t error = spvBinaryHeaderSet(binary, context.getBound());
Lei Zhang40056702015-09-11 14:31:27 -0400702 if (error) {
703 spvBinaryDestroy(binary);
704 return error;
705 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100706
707 *pBinary = binary;
708
709 return SPV_SUCCESS;
710}
711
Lei Zhange78a7c12015-09-10 17:07:21 -0400712} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400713
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400714spv_result_t spvTextToBinary(const char* input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400715 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400716 const spv_opcode_table opcodeTable,
717 const spv_operand_table operandTable,
718 const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400719 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400720 return spvTextWithFormatToBinary(
721 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
722 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
723}
724
725spv_result_t spvTextWithFormatToBinary(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400726 const char* input_text, const uint64_t input_text_size,
Lei Zhang06efdc52015-09-10 14:00:00 -0400727 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
728 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400729 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400730 spv_text_t text = {input_text, input_text_size};
731
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400732 libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable);
Lei Zhang06efdc52015-09-10 14:00:00 -0400733 spv_result_t result =
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400734 spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400735 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
736
737 return result;
738}
739
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100740void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400741 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100742 if (text->str) {
743 delete[] text->str;
744 }
745 delete text;
746}