blob: b440552325e73330c48f160a6cb50607813849a1 [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>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040036#include <sstream>
David Neto201caf72015-11-04 17:38:17 -050037#include <string>
David Neto36b0c0f2015-09-16 18:32:54 -040038#include <unordered_map>
39#include <vector>
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040040
David Netofcc7d582015-10-27 15:31:10 -040041#include "assembly_grammar.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010042#include "binary.h"
43#include "diagnostic.h"
44#include "ext_inst.h"
David Netob5dc8fc2015-10-06 16:22:00 -040045#include "instruction.h"
Lei Zhang923f6c12015-11-11 12:45:23 -050046#include "libspirv/libspirv.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010047#include "opcode.h"
48#include "operand.h"
Lei Zhang7a222e42015-11-11 12:40:25 -050049#include "table.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040050#include "text_handler.h"
Andrew Woloszynf731cbf2015-10-23 09:53:58 -040051#include "util/bitutils.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010052
Andrew Woloszyn13804e52015-09-22 15:50:33 -040053bool spvIsValidIDCharacter(const char value) {
54 return value == '_' || 0 != ::isalnum(value);
55}
56
57// Returns true if the given string represents a valid ID name.
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 Neto98290a22015-08-24 16:27:02 -0400112 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
113 return SPV_FAILED_MATCH;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400114 bool escaping = false;
Lei Zhang6483bd72015-10-14 17:02:39 -0400115 for (const char* val = textValue + 1; val != textValue + len - 1; ++val) {
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400116 if ((*val == '\\') && (!escaping)) {
117 escaping = true;
118 } else {
119 // Have to save space for the null-terminator
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500120 if (pLiteral->str.size() >= SPV_LIMIT_LITERAL_STRING_BYTES_MAX)
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400121 return SPV_ERROR_OUT_OF_MEMORY;
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500122 pLiteral->str.push_back(*val);
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400123 escaping = false;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400124 }
125 }
126
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100127 pLiteral->type = SPV_LITERAL_TYPE_STRING;
David Netoaffa6962015-08-24 15:33:14 -0400128 } else if (numPeriods == 1) {
129 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100130 float f = (float)d;
131 if (d == (double)f) {
132 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
133 pLiteral->value.f = f;
134 } else {
135 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
136 pLiteral->value.d = d;
137 }
138 } else if (isSigned) {
139 int64_t i64 = strtoll(textValue, nullptr, 10);
140 int32_t i32 = (int32_t)i64;
141 if (i64 == (int64_t)i32) {
142 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
143 pLiteral->value.i32 = i32;
144 } else {
145 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
146 pLiteral->value.i64 = i64;
147 }
148 } else {
149 uint64_t u64 = strtoull(textValue, nullptr, 10);
150 uint32_t u32 = (uint32_t)u64;
151 if (u64 == (uint64_t)u32) {
152 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
153 pLiteral->value.u32 = u32;
154 } else {
155 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
156 pLiteral->value.u64 = u64;
157 }
158 }
159
160 return SPV_SUCCESS;
161}
162
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400163namespace {
164
165/// Parses an immediate integer from text, guarding against overflow. If
166/// successful, adds the parsed value to pInst, advances the context past it,
167/// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics,
168/// and returns SPV_ERROR_INVALID_TEXT.
169spv_result_t encodeImmediate(libspirv::AssemblyContext* context,
170 const char* text, spv_instruction_t* pInst) {
171 assert(*text == '!');
David Neto78e677b2015-10-05 13:28:46 -0400172 uint32_t parse_result;
173 if (auto error =
David Neto51013d12015-10-14 11:31:51 -0400174 context->parseNumber(text + 1, SPV_ERROR_INVALID_TEXT, &parse_result,
David Neto78e677b2015-10-05 13:28:46 -0400175 "Invalid immediate integer: !"))
176 return error;
177 context->binaryEncodeU32(parse_result, pInst);
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400178 context->seekForward(strlen(text));
179 return SPV_SUCCESS;
180}
181
182} // anonymous namespace
183
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400184/// @brief Translate an Opcode operand to binary form
185///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400186/// @param[in] grammar the grammar to use for compilation
187/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400188/// @param[in] type of the operand
189/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400190/// @param[out] pInst return binary Opcode
191/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400192///
193/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400194spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400195 libspirv::AssemblyContext* context,
196 const spv_operand_type_t type,
197 const char* textValue,
198 spv_instruction_t* pInst,
199 spv_operand_pattern_t* pExpectedOperands) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100200 // NOTE: Handle immediate int in the stream
201 if ('!' == textValue[0]) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400202 if (auto error = encodeImmediate(context, textValue, pInst)) {
203 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400204 }
Dejan Mircevski897bff92015-09-29 10:38:18 -0400205 *pExpectedOperands =
206 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100207 return SPV_SUCCESS;
208 }
209
David Neto51013d12015-10-14 11:31:51 -0400210 // Optional literal operands can fail to parse. In that case use
211 // SPV_FAILED_MATCH to avoid emitting a diagostic. Use the following
212 // for those situations.
213 spv_result_t error_code_for_literals =
214 spvOperandIsOptional(type) ? SPV_FAILED_MATCH : SPV_ERROR_INVALID_TEXT;
215
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100216 switch (type) {
David Netoe3f70b92015-08-27 13:50:05 -0400217 case SPV_OPERAND_TYPE_ID:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400218 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto201caf72015-11-04 17:38:17 -0500219 case SPV_OPERAND_TYPE_RESULT_ID:
220 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
221 case SPV_OPERAND_TYPE_SCOPE_ID:
222 case SPV_OPERAND_TYPE_OPTIONAL_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400223 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100224 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100225 } else {
David Netoac508b02015-10-09 15:48:09 -0400226 return context->diagnostic() << "Expected id to start with %.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100227 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400228 if (!spvIsValidID(textValue)) {
David Netoac508b02015-10-09 15:48:09 -0400229 return context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400230 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400231 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400232 if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
David Netob5dc8fc2015-10-06 16:22:00 -0400233 spvInstructionAddWord(pInst, id);
David Neto2ae4a682015-11-09 18:55:42 -0500234
235 // Set the extended instruction type.
236 // The import set id is the 3rd operand of OpExtInst.
237 if (pInst->opcode == SpvOpExtInst && pInst->words.size() == 4) {
238 auto ext_inst_type = context->getExtInstTypeForId(pInst->words[3]);
239 if (ext_inst_type == SPV_EXT_INST_TYPE_NONE) {
240 return context->diagnostic()
241 << "Invalid extended instruction import Id "
242 << pInst->words[2];
243 }
244 pInst->extInstType = ext_inst_type;
245 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100246 } break;
David Neto201caf72015-11-04 17:38:17 -0500247
David Neto445ce442015-10-15 15:22:06 -0400248 case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
249 // The assembler accepts the symbolic name for an extended instruction,
250 // and emits its corresponding number.
251 spv_ext_inst_desc extInst;
252 if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
253 return context->diagnostic() << "Invalid extended instruction name '"
254 << textValue << "'.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100255 }
David Neto445ce442015-10-15 15:22:06 -0400256 spvInstructionAddWord(pInst, extInst->ext_inst);
257
258 // Prepare to parse the operands for the extended instructions.
259 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
David Neto0f166be2015-11-11 01:56:49 -0500260 } break;
David Neto445ce442015-10-15 15:22:06 -0400261
David Neto0f166be2015-11-11 01:56:49 -0500262 case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
263 // The assembler accepts the symbolic name for the opcode, but without
264 // the "Op" prefix. For example, "IAdd" is accepted. The number
265 // of the opcode is emitted.
266 SpvOp opcode;
267 if (grammar.lookupSpecConstantOpcode(textValue, &opcode)) {
268 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
269 << " '" << textValue << "'.";
270 }
271 spv_opcode_desc opcodeEntry = nullptr;
272 if (grammar.lookupOpcode(opcode, &opcodeEntry)) {
273 return context->diagnostic(SPV_ERROR_INTERNAL)
274 << "OpSpecConstant opcode table out of sync";
275 }
276 spvInstructionAddWord(pInst, uint32_t(opcodeEntry->opcode));
277
278 // Prepare to parse the operands for the opcode. Except skip the
279 // type Id and result Id, since they've already been processed.
280 assert(opcodeEntry->hasType);
281 assert(opcodeEntry->hasResult);
282 assert(opcodeEntry->numTypes >= 2);
283 spvPrependOperandTypes(opcodeEntry->operandTypes + 2, pExpectedOperands);
David Neto445ce442015-10-15 15:22:06 -0400284 } break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400285
David Neto201caf72015-11-04 17:38:17 -0500286 case SPV_OPERAND_TYPE_LITERAL_INTEGER:
287 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: {
288 // The current operand is an *unsigned* 32-bit integer.
289 // That's just how the grammar works.
290 libspirv::IdType expected_type = {
291 32, false, libspirv::IdTypeClass::kScalarIntegerType};
David Neto78e677b2015-10-05 13:28:46 -0400292 if (auto error = context->binaryEncodeNumericLiteral(
David Neto51013d12015-10-14 11:31:51 -0400293 textValue, error_code_for_literals, expected_type, pInst)) {
David Neto78e677b2015-10-05 13:28:46 -0400294 return error;
David Neto51013d12015-10-14 11:31:51 -0400295 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100296 } break;
David Neto201caf72015-11-04 17:38:17 -0500297
298 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
299 // This is a context-independent literal number which can be a 32-bit
300 // number of floating point value.
301 if (auto error = context->binaryEncodeNumericLiteral(
302 textValue, error_code_for_literals, libspirv::kUnknownType,
303 pInst)) {
304 return error;
305 }
306 break;
307
308 case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
309 case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: {
310 libspirv::IdType expected_type = libspirv::kUnknownType;
311 // The encoding for OpConstant, OpSpecConstant and OpSwitch all
312 // depend on either their own result-id or the result-id of
313 // one of their parameters.
314 if (SpvOpConstant == pInst->opcode ||
315 SpvOpSpecConstant == pInst->opcode) {
316 // The type of the literal is determined by the type Id of the
317 // instruction.
318 expected_type =
319 context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
320 if (!libspirv::isScalarFloating(expected_type) &&
321 !libspirv::isScalarIntegral(expected_type)) {
322 spv_opcode_desc d;
323 const char* opcode_name = "opcode";
324 if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
325 opcode_name = d->name;
326 }
327 return context->diagnostic()
328 << "Type for " << opcode_name
329 << " must be a scalar floating point or integer type";
330 }
331 } else if (pInst->opcode == SpvOpSwitch) {
332 // The type of the literal is the same as the type of the selector.
333 expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
334 if (!libspirv::isScalarIntegral(expected_type)) {
335 return context->diagnostic()
336 << "The selector operand for OpSwitch must be the result"
337 " of an instruction that generates an integer scalar";
338 }
339 }
340 if (auto error = context->binaryEncodeNumericLiteral(
341 textValue, error_code_for_literals, expected_type, pInst)) {
342 return error;
343 }
344 } break;
345
David Neto78c3b432015-08-27 13:03:52 -0400346 case SPV_OPERAND_TYPE_LITERAL_STRING:
347 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400348 spv_literal_t literal = {};
349 spv_result_t error = spvTextToLiteral(textValue, &literal);
350 if (error != SPV_SUCCESS) {
351 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
David Neto51013d12015-10-14 11:31:51 -0400352 return context->diagnostic(error_code_for_literals)
353 << "Invalid literal string '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400354 }
Lei Zhang6d415812015-09-15 13:36:21 -0400355 if (literal.type != SPV_LITERAL_TYPE_STRING) {
David Neto51013d12015-10-14 11:31:51 -0400356 return context->diagnostic()
David Netoac508b02015-10-09 15:48:09 -0400357 << "Expected literal string, found literal number '" << textValue
358 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400359 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100360
361 // NOTE: Special case for extended instruction library import
Lei Zhangb36e7042015-10-28 13:40:52 -0400362 if (SpvOpExtInstImport == pInst->opcode) {
David Neto2ae4a682015-11-09 18:55:42 -0500363 const spv_ext_inst_type_t ext_inst_type =
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500364 spvExtInstImportTypeGet(literal.str.c_str());
David Neto2ae4a682015-11-09 18:55:42 -0500365 if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
366 return context->diagnostic()
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500367 << "Invalid extended instruction import '" << literal.str
David Neto2ae4a682015-11-09 18:55:42 -0500368 << "'";
369 }
David Neto0f166be2015-11-11 01:56:49 -0500370 if (auto error = context->recordIdAsExtInstImport(pInst->words[1],
371 ext_inst_type))
David Neto2ae4a682015-11-09 18:55:42 -0500372 return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100373 }
374
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500375 if (context->binaryEncodeString(literal.str.c_str(), pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400376 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100377 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400378 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
379 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
380 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400381 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400382 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400383 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
384 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400385 if (grammar.parseMaskOperand(type, textValue, &value)) {
David Netoac508b02015-10-09 15:48:09 -0400386 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
387 << " '" << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400388 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400389 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400390 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400391 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400392 } break;
393 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
394 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400395 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
396 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400397 if (error == SPV_FAILED_MATCH) {
398 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400399 error = spvTextEncodeOperand(grammar, context,
400 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
401 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400402 }
403 if (error == SPV_FAILED_MATCH) {
404 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400405 error =
406 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
407 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400408 }
409 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400410 return context->diagnostic(error)
411 << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400412 }
413 if (pExpectedOperands->empty()) {
414 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
415 }
David Neto36b0c0f2015-09-16 18:32:54 -0400416 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100417 default: {
418 // NOTE: All non literal operands are handled here using the operand
419 // table.
420 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400421 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
David Netoac508b02015-10-09 15:48:09 -0400422 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
423 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400424 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400425 if (context->binaryEncodeU32(entry->value, pInst)) {
David Netoac508b02015-10-09 15:48:09 -0400426 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
427 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400428 }
David Neto78c3b432015-08-27 13:03:52 -0400429
430 // Prepare to parse the operands for this logical operand.
431 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100432 } break;
433 }
434 return SPV_SUCCESS;
435}
436
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400437namespace {
438
439/// Encodes an instruction started by !<integer> at the given position in text.
440///
441/// Puts the encoded words into *pInst. If successful, moves position past the
442/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
443/// leaves position pointing to the error in text.
444spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400445 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400446 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400447 std::string firstWord;
448 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400449 auto error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400450 if (error) return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400451
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400452 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
453 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400454 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400455 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400456 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400457 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400458
459 // Otherwise, there must be an operand that's either a literal, an ID, or
460 // an immediate.
461 std::string operandValue;
David Netoac508b02015-10-09 15:48:09 -0400462 if ((error = context->getWord(operandValue, &nextPosition)))
463 return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400464
David Netoac508b02015-10-09 15:48:09 -0400465 if (operandValue == "=")
466 return context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400467
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400468 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
469 // expanded.
470 spv_operand_pattern_t dummyExpectedOperands;
471 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400472 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
473 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400474 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400475 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400476 }
477 return SPV_SUCCESS;
478}
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400479} // anonymous namespace
480
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400481/// @brief Translate single Opcode and operands to binary form
482///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400483/// @param[in] grammar the grammar to use for compilation
484/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400485/// @param[in] text stream to translate
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400486/// @param[out] pInst returned binary Opcode
487/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400488///
489/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400490spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400491 libspirv::AssemblyContext* context,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400492 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400493 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400494 if ('!' == context->peek()) {
495 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400496 }
497
Lei Zhang06efdc52015-09-10 14:00:00 -0400498 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100499 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400500 spv_result_t error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400501 if (error) return context->diagnostic() << "Internal Error";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100502
Lei Zhang06efdc52015-09-10 14:00:00 -0400503 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400504 std::string result_id;
505 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400506 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400507 opcodeName = firstWord;
508 } else {
Lei Zhang06efdc52015-09-10 14:00:00 -0400509 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400510 if ('%' != result_id.front()) {
David Netoac508b02015-10-09 15:48:09 -0400511 return context->diagnostic()
512 << "Expected <opcode> or <result-id> at the beginning "
513 "of an instruction, found '"
514 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400515 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400516 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400517
518 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400519 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400520 if (context->advance())
521 return context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400522 std::string equal_sign;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400523 error = context->getWord(equal_sign, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400524 if ("=" != equal_sign)
525 return context->diagnostic() << "'=' expected after result id.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400526
527 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400528 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400529 if (context->advance())
530 return context->diagnostic() << "Expected opcode, found end of stream.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400531 error = context->getWord(opcodeName, &nextPosition);
Lei Zhang6483bd72015-10-14 17:02:39 -0400532 if (error) return context->diagnostic(error) << "Internal Error";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400533 if (!context->startsWithOp()) {
David Netoac508b02015-10-09 15:48:09 -0400534 return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName
535 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400536 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400537 }
538
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100539 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400540 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100541
542 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400543 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400544 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400545 return context->diagnostic(error) << "Invalid Opcode name '"
546 << context->getWord() << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400547 }
Lei Zhang9042f402015-11-05 17:45:09 -0500548 if (opcodeEntry->hasResult && result_id.empty()) {
549 return context->diagnostic()
550 << "Expected <result-id> at the beginning of an instruction, found '"
551 << firstWord << "'.";
Lei Zhang06efdc52015-09-10 14:00:00 -0400552 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100553 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400554 context->setPosition(nextPosition);
David Netob5dc8fc2015-10-06 16:22:00 -0400555 // Reserve the first word for the instruction.
556 spvInstructionAddWord(pInst, 0);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100557
David Neto78c3b432015-08-27 13:03:52 -0400558 // Maintains the ordered list of expected operand types.
559 // For many instructions we only need the {numTypes, operandTypes}
560 // entries in opcodeEntry. However, sometimes we need to modify
561 // the list as we parse the operands. This occurs when an operand
562 // has its own logical operands (such as the LocalSize operand for
563 // ExecutionMode), or for extended instructions that may have their
564 // own operands depending on the selected extended instruction.
565 spv_operand_pattern_t expectedOperands(
566 opcodeEntry->operandTypes,
567 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400568
David Neto78c3b432015-08-27 13:03:52 -0400569 while (!expectedOperands.empty()) {
570 const spv_operand_type_t type = expectedOperands.front();
571 expectedOperands.pop_front();
572
573 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400574 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400575
576 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
577 // Handle the <result-id> for value generating instructions.
578 // We've already consumed it from the text stream. Here
579 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400580 spv_position_t temp_pos = context->position();
581 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
582 result_id.c_str(), pInst, nullptr);
583 result_id_position = context->position();
584 // Because we are injecting we have to reset the position afterwards.
585 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400586 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100587 } else {
David Neto78c3b432015-08-27 13:03:52 -0400588 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400589 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400590 if (error == SPV_END_OF_STREAM) {
591 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400592 // This would have been the last potential operand for the
593 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400594 // and we didn't find one. We're finished parsing this instruction.
595 break;
596 } else {
David Netoac508b02015-10-09 15:48:09 -0400597 return context->diagnostic()
598 << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400599 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100600 }
David Neto78c3b432015-08-27 13:03:52 -0400601 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
602
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400603 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400604 if (spvOperandIsOptional(type)) {
605 break;
606 } else {
David Netoac508b02015-10-09 15:48:09 -0400607 return context->diagnostic()
608 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400609 }
610 }
611
612 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400613 error = context->getWord(operandValue, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400614 if (error) return context->diagnostic(error) << "Internal Error";
David Neto78c3b432015-08-27 13:03:52 -0400615
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400616 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
617 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400618
619 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
620 return SPV_SUCCESS;
621
Lei Zhang40056702015-09-11 14:31:27 -0400622 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400623
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400624 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100625 }
626 }
627
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400628 if (spvOpcodeGeneratesType(pInst->opcode)) {
629 if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
630 return SPV_ERROR_INVALID_TEXT;
631 }
632 } else if (opcodeEntry->hasType) {
633 // SPIR-V dictates that if an instruction has both a return value and a
634 // type ID then the type id is first, and the return value is second.
635 assert(opcodeEntry->hasResult &&
636 "Unknown opcode: has a type but no result.");
637 context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
638 }
639
David Netob5dc8fc2015-10-06 16:22:00 -0400640 if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Netoac508b02015-10-09 15:48:09 -0400641 return context->diagnostic()
642 << "Instruction too long: " << pInst->words.size()
643 << " words, but the limit is "
644 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
David Netob5dc8fc2015-10-06 16:22:00 -0400645 }
646
647 pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100648
649 return SPV_SUCCESS;
650}
651
David Netoc9786432015-09-01 18:05:14 -0400652namespace {
653
David Netoe4945de2015-10-28 13:50:32 -0400654/// @brief Populate a binary stream's words with this generator's header.
David Neto0b981682015-10-27 15:51:34 -0400655///
David Netoe4945de2015-10-28 13:50:32 -0400656/// @param[in,out] words the array of words
David Neto0b981682015-10-27 15:51:34 -0400657/// @param[in] bound the upper ID bound
658///
659/// @return result code
David Netoe4945de2015-10-28 13:50:32 -0400660spv_result_t SetHeader(uint32_t* words, const uint32_t bound) {
661 if (!words) return SPV_ERROR_INVALID_BINARY;
David Neto0b981682015-10-27 15:51:34 -0400662
Lei Zhang9049bb42015-11-11 10:17:16 -0500663 words[SPV_INDEX_MAGIC_NUMBER] = SpvMagicNumber;
664 words[SPV_INDEX_VERSION_NUMBER] = SpvVersion;
David Netoe4945de2015-10-28 13:50:32 -0400665 words[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS;
666 words[SPV_INDEX_BOUND] = bound;
667 words[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
David Neto0b981682015-10-27 15:51:34 -0400668
669 return SPV_SUCCESS;
670}
671
David Netoc9786432015-09-01 18:05:14 -0400672// Translates a given assembly language module into binary form.
673// If a diagnostic is generated, it is not yet marked as being
674// for a text-based input.
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400675spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
Lei Zhang9042f402015-11-05 17:45:09 -0500676 const spv_text text, spv_binary* pBinary,
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400677 spv_diagnostic* pDiagnostic) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400678 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
679 libspirv::AssemblyContext context(text, pDiagnostic);
David Neto201caf72015-11-04 17:38:17 -0500680 if (!text->str) return context.diagnostic() << "Missing assembly text.";
David Netoac508b02015-10-09 15:48:09 -0400681
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400682 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400683 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400684 }
Lei Zhang40056702015-09-11 14:31:27 -0400685 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100686
687 // NOTE: Ensure diagnostic is zero initialised
688 *pDiagnostic = {};
689
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100690 std::vector<spv_instruction_t> instructions;
691
David Netoea633a62015-11-02 11:40:59 -0500692 // Skip past whitespace and comments.
693 context.advance();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100694
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400695 while (context.hasText()) {
David Netob5dc8fc2015-10-06 16:22:00 -0400696 instructions.push_back({});
697 spv_instruction_t& inst = instructions.back();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100698
Lei Zhang9042f402015-11-05 17:45:09 -0500699 if (spvTextEncodeOpcode(grammar, &context, &inst)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400700 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400701 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400702
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400703 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100704 }
705
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100706 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400707 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400708 totalSize += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100709 }
710
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400711 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400712 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100713 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400714 for (auto& inst : instructions) {
Lei Zhang6483bd72015-10-14 17:02:39 -0400715 memcpy(data + currentIndex, inst.words.data(),
716 sizeof(uint32_t) * inst.words.size());
David Netob5dc8fc2015-10-06 16:22:00 -0400717 currentIndex += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100718 }
719
David Neto201caf72015-11-04 17:38:17 -0500720 if (auto error = SetHeader(data, context.getBound())) return error;
David Netoe4945de2015-10-28 13:50:32 -0400721
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100722 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400723 if (!binary) {
724 delete[] data;
725 return SPV_ERROR_OUT_OF_MEMORY;
726 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100727 binary->code = data;
728 binary->wordCount = totalSize;
729
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100730 *pBinary = binary;
731
732 return SPV_SUCCESS;
733}
734
Lei Zhange78a7c12015-09-10 17:07:21 -0400735} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400736
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400737spv_result_t spvTextToBinary(const char* input_text,
Lei Zhangdf920ec2015-11-11 11:33:26 -0500738 const size_t input_text_size, spv_binary* pBinary,
739 spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400740 spv_text_t text = {input_text, input_text_size};
Lei Zhangdf920ec2015-11-11 11:33:26 -0500741 libspirv::AssemblyGrammar grammar;
Lei Zhang9042f402015-11-05 17:45:09 -0500742
Lei Zhang06efdc52015-09-10 14:00:00 -0400743 spv_result_t result =
Lei Zhang9042f402015-11-05 17:45:09 -0500744 spvTextToBinaryInternal(grammar, &text, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400745 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
746
747 return result;
748}
749
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100750void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400751 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100752 if (text->str) {
753 delete[] text->str;
754 }
755 delete text;
756}