blob: aa870c7d59d6a6d3ecf34c5cc01c3ec70662e6b0 [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 Zhangaa056cd2015-11-11 14:24:04 -050049#include "spirv_constant.h"
Lei Zhang7a222e42015-11-11 12:40:25 -050050#include "table.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040051#include "text_handler.h"
Andrew Woloszynf731cbf2015-10-23 09:53:58 -040052#include "util/bitutils.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010053
Andrew Woloszyn13804e52015-09-22 15:50:33 -040054bool spvIsValidIDCharacter(const char value) {
55 return value == '_' || 0 != ::isalnum(value);
56}
57
58// Returns true if the given string represents a valid ID name.
Dejan Mircevski14c4b102015-09-29 17:07:21 -040059bool spvIsValidID(const char* textValue) {
60 const char* c = textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -040061 for (; *c != '\0'; ++c) {
62 if (!spvIsValidIDCharacter(*c)) {
63 return false;
64 }
65 }
66 // If the string was empty, then the ID also is not valid.
67 return c != textValue;
68}
69
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040070// Text API
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010071
Dejan Mircevski903f9d62015-09-28 17:04:39 -040072spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073 bool isSigned = false;
David Netoaffa6962015-08-24 15:33:14 -040074 int numPeriods = 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010075 bool isString = false;
76
David Netoaffa6962015-08-24 15:33:14 -040077 const size_t len = strlen(textValue);
David Neto98290a22015-08-24 16:27:02 -040078 if (len == 0) return SPV_FAILED_MATCH;
79
David Netoaffa6962015-08-24 15:33:14 -040080 for (uint64_t index = 0; index < len; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010081 switch (textValue[index]) {
82 case '0':
83 case '1':
84 case '2':
85 case '3':
86 case '4':
87 case '5':
88 case '6':
89 case '7':
90 case '8':
91 case '9':
92 break;
93 case '.':
David Netoaffa6962015-08-24 15:33:14 -040094 numPeriods++;
95 break;
96 case '-':
97 if (index == 0) {
98 isSigned = true;
99 } else {
100 isString = true;
101 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100102 break;
103 default:
104 isString = true;
Lei Zhange78a7c12015-09-10 17:07:21 -0400105 index = len; // break out of the loop too.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100106 break;
107 }
108 }
109
David Netoaffa6962015-08-24 15:33:14 -0400110 pLiteral->type = spv_literal_type_t(99);
111
Lei Zhange78a7c12015-09-10 17:07:21 -0400112 if (isString || numPeriods > 1 || (isSigned && len == 1)) {
David Neto98290a22015-08-24 16:27:02 -0400113 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
114 return SPV_FAILED_MATCH;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400115 bool escaping = false;
Lei Zhang6483bd72015-10-14 17:02:39 -0400116 for (const char* val = textValue + 1; val != textValue + len - 1; ++val) {
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400117 if ((*val == '\\') && (!escaping)) {
118 escaping = true;
119 } else {
120 // Have to save space for the null-terminator
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500121 if (pLiteral->str.size() >= SPV_LIMIT_LITERAL_STRING_BYTES_MAX)
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400122 return SPV_ERROR_OUT_OF_MEMORY;
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500123 pLiteral->str.push_back(*val);
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400124 escaping = false;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400125 }
126 }
127
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100128 pLiteral->type = SPV_LITERAL_TYPE_STRING;
David Netoaffa6962015-08-24 15:33:14 -0400129 } else if (numPeriods == 1) {
130 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100131 float f = (float)d;
132 if (d == (double)f) {
133 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
134 pLiteral->value.f = f;
135 } else {
136 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
137 pLiteral->value.d = d;
138 }
139 } else if (isSigned) {
140 int64_t i64 = strtoll(textValue, nullptr, 10);
141 int32_t i32 = (int32_t)i64;
142 if (i64 == (int64_t)i32) {
143 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
144 pLiteral->value.i32 = i32;
145 } else {
146 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
147 pLiteral->value.i64 = i64;
148 }
149 } else {
150 uint64_t u64 = strtoull(textValue, nullptr, 10);
151 uint32_t u32 = (uint32_t)u64;
152 if (u64 == (uint64_t)u32) {
153 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
154 pLiteral->value.u32 = u32;
155 } else {
156 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
157 pLiteral->value.u64 = u64;
158 }
159 }
160
161 return SPV_SUCCESS;
162}
163
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400164namespace {
165
166/// Parses an immediate integer from text, guarding against overflow. If
167/// successful, adds the parsed value to pInst, advances the context past it,
168/// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics,
169/// and returns SPV_ERROR_INVALID_TEXT.
170spv_result_t encodeImmediate(libspirv::AssemblyContext* context,
171 const char* text, spv_instruction_t* pInst) {
172 assert(*text == '!');
David Neto78e677b2015-10-05 13:28:46 -0400173 uint32_t parse_result;
174 if (auto error =
David Neto51013d12015-10-14 11:31:51 -0400175 context->parseNumber(text + 1, SPV_ERROR_INVALID_TEXT, &parse_result,
David Neto78e677b2015-10-05 13:28:46 -0400176 "Invalid immediate integer: !"))
177 return error;
178 context->binaryEncodeU32(parse_result, pInst);
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400179 context->seekForward(strlen(text));
180 return SPV_SUCCESS;
181}
182
183} // anonymous namespace
184
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400185/// @brief Translate an Opcode operand to binary form
186///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400187/// @param[in] grammar the grammar to use for compilation
188/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400189/// @param[in] type of the operand
190/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400191/// @param[out] pInst return binary Opcode
192/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400193///
194/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400195spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400196 libspirv::AssemblyContext* context,
197 const spv_operand_type_t type,
198 const char* textValue,
199 spv_instruction_t* pInst,
200 spv_operand_pattern_t* pExpectedOperands) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100201 // NOTE: Handle immediate int in the stream
202 if ('!' == textValue[0]) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400203 if (auto error = encodeImmediate(context, textValue, pInst)) {
204 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400205 }
Dejan Mircevski897bff92015-09-29 10:38:18 -0400206 *pExpectedOperands =
207 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100208 return SPV_SUCCESS;
209 }
210
David Neto51013d12015-10-14 11:31:51 -0400211 // Optional literal operands can fail to parse. In that case use
212 // SPV_FAILED_MATCH to avoid emitting a diagostic. Use the following
213 // for those situations.
214 spv_result_t error_code_for_literals =
215 spvOperandIsOptional(type) ? SPV_FAILED_MATCH : SPV_ERROR_INVALID_TEXT;
216
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100217 switch (type) {
David Netoe3f70b92015-08-27 13:50:05 -0400218 case SPV_OPERAND_TYPE_ID:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400219 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto201caf72015-11-04 17:38:17 -0500220 case SPV_OPERAND_TYPE_RESULT_ID:
221 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
222 case SPV_OPERAND_TYPE_SCOPE_ID:
223 case SPV_OPERAND_TYPE_OPTIONAL_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400224 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100225 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100226 } else {
David Netoac508b02015-10-09 15:48:09 -0400227 return context->diagnostic() << "Expected id to start with %.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100228 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400229 if (!spvIsValidID(textValue)) {
David Netoac508b02015-10-09 15:48:09 -0400230 return context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400231 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400232 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400233 if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
David Netob5dc8fc2015-10-06 16:22:00 -0400234 spvInstructionAddWord(pInst, id);
David Neto2ae4a682015-11-09 18:55:42 -0500235
236 // Set the extended instruction type.
237 // The import set id is the 3rd operand of OpExtInst.
238 if (pInst->opcode == SpvOpExtInst && pInst->words.size() == 4) {
239 auto ext_inst_type = context->getExtInstTypeForId(pInst->words[3]);
240 if (ext_inst_type == SPV_EXT_INST_TYPE_NONE) {
241 return context->diagnostic()
242 << "Invalid extended instruction import Id "
243 << pInst->words[2];
244 }
245 pInst->extInstType = ext_inst_type;
246 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100247 } break;
David Neto201caf72015-11-04 17:38:17 -0500248
David Neto445ce442015-10-15 15:22:06 -0400249 case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
250 // The assembler accepts the symbolic name for an extended instruction,
251 // and emits its corresponding number.
252 spv_ext_inst_desc extInst;
253 if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
254 return context->diagnostic() << "Invalid extended instruction name '"
255 << textValue << "'.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100256 }
David Neto445ce442015-10-15 15:22:06 -0400257 spvInstructionAddWord(pInst, extInst->ext_inst);
258
259 // Prepare to parse the operands for the extended instructions.
260 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
David Neto0f166be2015-11-11 01:56:49 -0500261 } break;
David Neto445ce442015-10-15 15:22:06 -0400262
David Neto0f166be2015-11-11 01:56:49 -0500263 case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
264 // The assembler accepts the symbolic name for the opcode, but without
265 // the "Op" prefix. For example, "IAdd" is accepted. The number
266 // of the opcode is emitted.
267 SpvOp opcode;
268 if (grammar.lookupSpecConstantOpcode(textValue, &opcode)) {
269 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
270 << " '" << textValue << "'.";
271 }
272 spv_opcode_desc opcodeEntry = nullptr;
273 if (grammar.lookupOpcode(opcode, &opcodeEntry)) {
274 return context->diagnostic(SPV_ERROR_INTERNAL)
275 << "OpSpecConstant opcode table out of sync";
276 }
277 spvInstructionAddWord(pInst, uint32_t(opcodeEntry->opcode));
278
279 // Prepare to parse the operands for the opcode. Except skip the
280 // type Id and result Id, since they've already been processed.
281 assert(opcodeEntry->hasType);
282 assert(opcodeEntry->hasResult);
283 assert(opcodeEntry->numTypes >= 2);
284 spvPrependOperandTypes(opcodeEntry->operandTypes + 2, pExpectedOperands);
David Neto445ce442015-10-15 15:22:06 -0400285 } break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400286
David Neto201caf72015-11-04 17:38:17 -0500287 case SPV_OPERAND_TYPE_LITERAL_INTEGER:
288 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: {
289 // The current operand is an *unsigned* 32-bit integer.
290 // That's just how the grammar works.
291 libspirv::IdType expected_type = {
292 32, false, libspirv::IdTypeClass::kScalarIntegerType};
David Neto78e677b2015-10-05 13:28:46 -0400293 if (auto error = context->binaryEncodeNumericLiteral(
David Neto51013d12015-10-14 11:31:51 -0400294 textValue, error_code_for_literals, expected_type, pInst)) {
David Neto78e677b2015-10-05 13:28:46 -0400295 return error;
David Neto51013d12015-10-14 11:31:51 -0400296 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100297 } break;
David Neto201caf72015-11-04 17:38:17 -0500298
299 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
300 // This is a context-independent literal number which can be a 32-bit
301 // number of floating point value.
302 if (auto error = context->binaryEncodeNumericLiteral(
303 textValue, error_code_for_literals, libspirv::kUnknownType,
304 pInst)) {
305 return error;
306 }
307 break;
308
309 case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
310 case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: {
311 libspirv::IdType expected_type = libspirv::kUnknownType;
312 // The encoding for OpConstant, OpSpecConstant and OpSwitch all
313 // depend on either their own result-id or the result-id of
314 // one of their parameters.
315 if (SpvOpConstant == pInst->opcode ||
316 SpvOpSpecConstant == pInst->opcode) {
317 // The type of the literal is determined by the type Id of the
318 // instruction.
319 expected_type =
320 context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
321 if (!libspirv::isScalarFloating(expected_type) &&
322 !libspirv::isScalarIntegral(expected_type)) {
323 spv_opcode_desc d;
324 const char* opcode_name = "opcode";
325 if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
326 opcode_name = d->name;
327 }
328 return context->diagnostic()
329 << "Type for " << opcode_name
330 << " must be a scalar floating point or integer type";
331 }
332 } else if (pInst->opcode == SpvOpSwitch) {
333 // The type of the literal is the same as the type of the selector.
334 expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
335 if (!libspirv::isScalarIntegral(expected_type)) {
336 return context->diagnostic()
337 << "The selector operand for OpSwitch must be the result"
338 " of an instruction that generates an integer scalar";
339 }
340 }
341 if (auto error = context->binaryEncodeNumericLiteral(
342 textValue, error_code_for_literals, expected_type, pInst)) {
343 return error;
344 }
345 } break;
346
David Neto78c3b432015-08-27 13:03:52 -0400347 case SPV_OPERAND_TYPE_LITERAL_STRING:
348 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400349 spv_literal_t literal = {};
350 spv_result_t error = spvTextToLiteral(textValue, &literal);
351 if (error != SPV_SUCCESS) {
352 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
David Neto51013d12015-10-14 11:31:51 -0400353 return context->diagnostic(error_code_for_literals)
354 << "Invalid literal string '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400355 }
Lei Zhang6d415812015-09-15 13:36:21 -0400356 if (literal.type != SPV_LITERAL_TYPE_STRING) {
David Neto51013d12015-10-14 11:31:51 -0400357 return context->diagnostic()
David Netoac508b02015-10-09 15:48:09 -0400358 << "Expected literal string, found literal number '" << textValue
359 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400360 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100361
362 // NOTE: Special case for extended instruction library import
Lei Zhangb36e7042015-10-28 13:40:52 -0400363 if (SpvOpExtInstImport == pInst->opcode) {
David Neto2ae4a682015-11-09 18:55:42 -0500364 const spv_ext_inst_type_t ext_inst_type =
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500365 spvExtInstImportTypeGet(literal.str.c_str());
David Neto2ae4a682015-11-09 18:55:42 -0500366 if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
367 return context->diagnostic()
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500368 << "Invalid extended instruction import '" << literal.str
David Neto2ae4a682015-11-09 18:55:42 -0500369 << "'";
370 }
David Neto0f166be2015-11-11 01:56:49 -0500371 if (auto error = context->recordIdAsExtInstImport(pInst->words[1],
372 ext_inst_type))
David Neto2ae4a682015-11-09 18:55:42 -0500373 return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100374 }
375
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500376 if (context->binaryEncodeString(literal.str.c_str(), pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400377 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100378 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400379 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
380 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
381 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400382 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400383 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400384 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
385 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400386 if (grammar.parseMaskOperand(type, textValue, &value)) {
David Netoac508b02015-10-09 15:48:09 -0400387 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
388 << " '" << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400389 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400390 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400391 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400392 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400393 } break;
394 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
395 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400396 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
397 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400398 if (error == SPV_FAILED_MATCH) {
399 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400400 error = spvTextEncodeOperand(grammar, context,
401 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
402 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400403 }
404 if (error == SPV_FAILED_MATCH) {
405 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400406 error =
407 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
408 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400409 }
410 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400411 return context->diagnostic(error)
412 << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400413 }
414 if (pExpectedOperands->empty()) {
415 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
416 }
David Neto36b0c0f2015-09-16 18:32:54 -0400417 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100418 default: {
419 // NOTE: All non literal operands are handled here using the operand
420 // table.
421 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400422 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
David Netoac508b02015-10-09 15:48:09 -0400423 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
424 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400425 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400426 if (context->binaryEncodeU32(entry->value, pInst)) {
David Netoac508b02015-10-09 15:48:09 -0400427 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
428 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400429 }
David Neto78c3b432015-08-27 13:03:52 -0400430
431 // Prepare to parse the operands for this logical operand.
432 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100433 } break;
434 }
435 return SPV_SUCCESS;
436}
437
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400438namespace {
439
440/// Encodes an instruction started by !<integer> at the given position in text.
441///
442/// Puts the encoded words into *pInst. If successful, moves position past the
443/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
444/// leaves position pointing to the error in text.
445spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400446 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400447 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400448 std::string firstWord;
449 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400450 auto error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400451 if (error) return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400452
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400453 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
454 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400455 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400456 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400457 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400458 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400459
460 // Otherwise, there must be an operand that's either a literal, an ID, or
461 // an immediate.
462 std::string operandValue;
David Netoac508b02015-10-09 15:48:09 -0400463 if ((error = context->getWord(operandValue, &nextPosition)))
464 return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400465
David Netoac508b02015-10-09 15:48:09 -0400466 if (operandValue == "=")
467 return context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400468
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400469 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
470 // expanded.
471 spv_operand_pattern_t dummyExpectedOperands;
472 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400473 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
474 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400475 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400476 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400477 }
478 return SPV_SUCCESS;
479}
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400480} // anonymous namespace
481
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400482/// @brief Translate single Opcode and operands to binary form
483///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400484/// @param[in] grammar the grammar to use for compilation
485/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400486/// @param[in] text stream to translate
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400487/// @param[out] pInst returned binary Opcode
488/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400489///
490/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400491spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400492 libspirv::AssemblyContext* context,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400493 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400494 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400495 if ('!' == context->peek()) {
496 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400497 }
498
Lei Zhang06efdc52015-09-10 14:00:00 -0400499 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100500 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400501 spv_result_t error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400502 if (error) return context->diagnostic() << "Internal Error";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100503
Lei Zhang06efdc52015-09-10 14:00:00 -0400504 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400505 std::string result_id;
506 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400507 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400508 opcodeName = firstWord;
509 } else {
Lei Zhang06efdc52015-09-10 14:00:00 -0400510 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400511 if ('%' != result_id.front()) {
David Netoac508b02015-10-09 15:48:09 -0400512 return context->diagnostic()
513 << "Expected <opcode> or <result-id> at the beginning "
514 "of an instruction, found '"
515 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400516 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400517 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400518
519 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400520 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400521 if (context->advance())
522 return context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400523 std::string equal_sign;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400524 error = context->getWord(equal_sign, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400525 if ("=" != equal_sign)
526 return context->diagnostic() << "'=' expected after result id.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400527
528 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400529 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400530 if (context->advance())
531 return context->diagnostic() << "Expected opcode, found end of stream.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400532 error = context->getWord(opcodeName, &nextPosition);
Lei Zhang6483bd72015-10-14 17:02:39 -0400533 if (error) return context->diagnostic(error) << "Internal Error";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400534 if (!context->startsWithOp()) {
David Netoac508b02015-10-09 15:48:09 -0400535 return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName
536 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400537 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400538 }
539
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100540 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400541 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100542
543 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400544 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400545 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400546 return context->diagnostic(error) << "Invalid Opcode name '"
547 << context->getWord() << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400548 }
Lei Zhang9042f402015-11-05 17:45:09 -0500549 if (opcodeEntry->hasResult && result_id.empty()) {
550 return context->diagnostic()
551 << "Expected <result-id> at the beginning of an instruction, found '"
552 << firstWord << "'.";
Lei Zhang06efdc52015-09-10 14:00:00 -0400553 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100554 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400555 context->setPosition(nextPosition);
David Netob5dc8fc2015-10-06 16:22:00 -0400556 // Reserve the first word for the instruction.
557 spvInstructionAddWord(pInst, 0);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100558
David Neto78c3b432015-08-27 13:03:52 -0400559 // Maintains the ordered list of expected operand types.
560 // For many instructions we only need the {numTypes, operandTypes}
561 // entries in opcodeEntry. However, sometimes we need to modify
562 // the list as we parse the operands. This occurs when an operand
563 // has its own logical operands (such as the LocalSize operand for
564 // ExecutionMode), or for extended instructions that may have their
565 // own operands depending on the selected extended instruction.
566 spv_operand_pattern_t expectedOperands(
567 opcodeEntry->operandTypes,
568 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400569
David Neto78c3b432015-08-27 13:03:52 -0400570 while (!expectedOperands.empty()) {
571 const spv_operand_type_t type = expectedOperands.front();
572 expectedOperands.pop_front();
573
574 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400575 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400576
577 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
578 // Handle the <result-id> for value generating instructions.
579 // We've already consumed it from the text stream. Here
580 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400581 spv_position_t temp_pos = context->position();
582 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
583 result_id.c_str(), pInst, nullptr);
584 result_id_position = context->position();
585 // Because we are injecting we have to reset the position afterwards.
586 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400587 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100588 } else {
David Neto78c3b432015-08-27 13:03:52 -0400589 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400590 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400591 if (error == SPV_END_OF_STREAM) {
592 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400593 // This would have been the last potential operand for the
594 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400595 // and we didn't find one. We're finished parsing this instruction.
596 break;
597 } else {
David Netoac508b02015-10-09 15:48:09 -0400598 return context->diagnostic()
599 << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400600 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100601 }
David Neto78c3b432015-08-27 13:03:52 -0400602 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
603
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400604 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400605 if (spvOperandIsOptional(type)) {
606 break;
607 } else {
David Netoac508b02015-10-09 15:48:09 -0400608 return context->diagnostic()
609 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400610 }
611 }
612
613 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400614 error = context->getWord(operandValue, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400615 if (error) return context->diagnostic(error) << "Internal Error";
David Neto78c3b432015-08-27 13:03:52 -0400616
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400617 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
618 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400619
620 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
621 return SPV_SUCCESS;
622
Lei Zhang40056702015-09-11 14:31:27 -0400623 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400624
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400625 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100626 }
627 }
628
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400629 if (spvOpcodeGeneratesType(pInst->opcode)) {
630 if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
631 return SPV_ERROR_INVALID_TEXT;
632 }
633 } else if (opcodeEntry->hasType) {
634 // SPIR-V dictates that if an instruction has both a return value and a
635 // type ID then the type id is first, and the return value is second.
636 assert(opcodeEntry->hasResult &&
637 "Unknown opcode: has a type but no result.");
638 context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
639 }
640
David Netob5dc8fc2015-10-06 16:22:00 -0400641 if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Netoac508b02015-10-09 15:48:09 -0400642 return context->diagnostic()
643 << "Instruction too long: " << pInst->words.size()
644 << " words, but the limit is "
645 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
David Netob5dc8fc2015-10-06 16:22:00 -0400646 }
647
648 pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100649
650 return SPV_SUCCESS;
651}
652
David Netoc9786432015-09-01 18:05:14 -0400653namespace {
654
David Netoe4945de2015-10-28 13:50:32 -0400655/// @brief Populate a binary stream's words with this generator's header.
David Neto0b981682015-10-27 15:51:34 -0400656///
David Netoe4945de2015-10-28 13:50:32 -0400657/// @param[in,out] words the array of words
David Neto0b981682015-10-27 15:51:34 -0400658/// @param[in] bound the upper ID bound
659///
660/// @return result code
David Netoe4945de2015-10-28 13:50:32 -0400661spv_result_t SetHeader(uint32_t* words, const uint32_t bound) {
662 if (!words) return SPV_ERROR_INVALID_BINARY;
David Neto0b981682015-10-27 15:51:34 -0400663
Lei Zhang9049bb42015-11-11 10:17:16 -0500664 words[SPV_INDEX_MAGIC_NUMBER] = SpvMagicNumber;
665 words[SPV_INDEX_VERSION_NUMBER] = SpvVersion;
David Netoe4945de2015-10-28 13:50:32 -0400666 words[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS;
667 words[SPV_INDEX_BOUND] = bound;
668 words[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
David Neto0b981682015-10-27 15:51:34 -0400669
670 return SPV_SUCCESS;
671}
672
David Netoc9786432015-09-01 18:05:14 -0400673// Translates a given assembly language module into binary form.
674// If a diagnostic is generated, it is not yet marked as being
675// for a text-based input.
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400676spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
Lei Zhang9042f402015-11-05 17:45:09 -0500677 const spv_text text, spv_binary* pBinary,
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400678 spv_diagnostic* pDiagnostic) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400679 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
680 libspirv::AssemblyContext context(text, pDiagnostic);
David Neto201caf72015-11-04 17:38:17 -0500681 if (!text->str) return context.diagnostic() << "Missing assembly text.";
David Netoac508b02015-10-09 15:48:09 -0400682
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400683 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400684 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400685 }
Lei Zhang40056702015-09-11 14:31:27 -0400686 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100687
688 // NOTE: Ensure diagnostic is zero initialised
689 *pDiagnostic = {};
690
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100691 std::vector<spv_instruction_t> instructions;
692
David Netoea633a62015-11-02 11:40:59 -0500693 // Skip past whitespace and comments.
694 context.advance();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100695
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400696 while (context.hasText()) {
David Netob5dc8fc2015-10-06 16:22:00 -0400697 instructions.push_back({});
698 spv_instruction_t& inst = instructions.back();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100699
Lei Zhang9042f402015-11-05 17:45:09 -0500700 if (spvTextEncodeOpcode(grammar, &context, &inst)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400701 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400702 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400703
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400704 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100705 }
706
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100707 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400708 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400709 totalSize += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100710 }
711
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400712 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400713 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100714 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400715 for (auto& inst : instructions) {
Lei Zhang6483bd72015-10-14 17:02:39 -0400716 memcpy(data + currentIndex, inst.words.data(),
717 sizeof(uint32_t) * inst.words.size());
David Netob5dc8fc2015-10-06 16:22:00 -0400718 currentIndex += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100719 }
720
David Neto201caf72015-11-04 17:38:17 -0500721 if (auto error = SetHeader(data, context.getBound())) return error;
David Netoe4945de2015-10-28 13:50:32 -0400722
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100723 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400724 if (!binary) {
725 delete[] data;
726 return SPV_ERROR_OUT_OF_MEMORY;
727 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100728 binary->code = data;
729 binary->wordCount = totalSize;
730
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100731 *pBinary = binary;
732
733 return SPV_SUCCESS;
734}
735
Lei Zhange78a7c12015-09-10 17:07:21 -0400736} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400737
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400738spv_result_t spvTextToBinary(const char* input_text,
Lei Zhangdf920ec2015-11-11 11:33:26 -0500739 const size_t input_text_size, spv_binary* pBinary,
740 spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400741 spv_text_t text = {input_text, input_text_size};
Lei Zhangdf920ec2015-11-11 11:33:26 -0500742 libspirv::AssemblyGrammar grammar;
Lei Zhang9042f402015-11-05 17:45:09 -0500743
Lei Zhang06efdc52015-09-10 14:00:00 -0400744 spv_result_t result =
Lei Zhang9042f402015-11-05 17:45:09 -0500745 spvTextToBinaryInternal(grammar, &text, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400746 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
747
748 return result;
749}
750
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100751void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400752 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100753 if (text->str) {
754 delete[] text->str;
755 }
756 delete text;
757}