blob: dc3519e77fda210587a7d038b670ca35be2454e8 [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01002//
David Neto9fc86582016-09-01 15:33:59 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01008//
David Neto9fc86582016-09-01 15:33:59 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010014
David Neto36b0c0f2015-09-16 18:32:54 -040015#include "text.h"
16
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040017#include <algorithm>
18#include <cassert>
Andrew Woloszyn13804e52015-09-22 15:50:33 -040019#include <cctype>
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040020#include <cstdio>
21#include <cstdlib>
David Neto36b0c0f2015-09-16 18:32:54 -040022#include <cstring>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040023#include <memory>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040024#include <sstream>
David Neto201caf72015-11-04 17:38:17 -050025#include <string>
David Neto36b0c0f2015-09-16 18:32:54 -040026#include <unordered_map>
27#include <vector>
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040028
David Netofcc7d582015-10-27 15:31:10 -040029#include "assembly_grammar.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010030#include "binary.h"
31#include "diagnostic.h"
32#include "ext_inst.h"
David Netob5dc8fc2015-10-06 16:22:00 -040033#include "instruction.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010034#include "opcode.h"
35#include "operand.h"
David Neto5a703352016-02-17 14:44:00 -050036#include "spirv-tools/libspirv.h"
Lei Zhangaa056cd2015-11-11 14:24:04 -050037#include "spirv_constant.h"
Lei Zhang7a222e42015-11-11 12:40:25 -050038#include "table.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040039#include "text_handler.h"
Andrew Woloszynf731cbf2015-10-23 09:53:58 -040040#include "util/bitutils.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010041
Andrew Woloszyn13804e52015-09-22 15:50:33 -040042bool spvIsValidIDCharacter(const char value) {
43 return value == '_' || 0 != ::isalnum(value);
44}
45
46// Returns true if the given string represents a valid ID name.
Dejan Mircevski14c4b102015-09-29 17:07:21 -040047bool spvIsValidID(const char* textValue) {
48 const char* c = textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -040049 for (; *c != '\0'; ++c) {
50 if (!spvIsValidIDCharacter(*c)) {
51 return false;
52 }
53 }
54 // If the string was empty, then the ID also is not valid.
55 return c != textValue;
56}
57
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040058// Text API
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010059
Dejan Mircevski903f9d62015-09-28 17:04:39 -040060spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010061 bool isSigned = false;
David Netoaffa6962015-08-24 15:33:14 -040062 int numPeriods = 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010063 bool isString = false;
64
David Netoaffa6962015-08-24 15:33:14 -040065 const size_t len = strlen(textValue);
David Neto98290a22015-08-24 16:27:02 -040066 if (len == 0) return SPV_FAILED_MATCH;
67
David Netoaffa6962015-08-24 15:33:14 -040068 for (uint64_t index = 0; index < len; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010069 switch (textValue[index]) {
70 case '0':
71 case '1':
72 case '2':
73 case '3':
74 case '4':
75 case '5':
76 case '6':
77 case '7':
78 case '8':
79 case '9':
80 break;
81 case '.':
David Netoaffa6962015-08-24 15:33:14 -040082 numPeriods++;
83 break;
84 case '-':
85 if (index == 0) {
86 isSigned = true;
87 } else {
88 isString = true;
89 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010090 break;
91 default:
92 isString = true;
Lei Zhange78a7c12015-09-10 17:07:21 -040093 index = len; // break out of the loop too.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010094 break;
95 }
96 }
97
David Netoaffa6962015-08-24 15:33:14 -040098 pLiteral->type = spv_literal_type_t(99);
99
Lei Zhange78a7c12015-09-10 17:07:21 -0400100 if (isString || numPeriods > 1 || (isSigned && len == 1)) {
David Neto98290a22015-08-24 16:27:02 -0400101 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
102 return SPV_FAILED_MATCH;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400103 bool escaping = false;
Lei Zhang6483bd72015-10-14 17:02:39 -0400104 for (const char* val = textValue + 1; val != textValue + len - 1; ++val) {
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400105 if ((*val == '\\') && (!escaping)) {
106 escaping = true;
107 } else {
108 // Have to save space for the null-terminator
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500109 if (pLiteral->str.size() >= SPV_LIMIT_LITERAL_STRING_BYTES_MAX)
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400110 return SPV_ERROR_OUT_OF_MEMORY;
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500111 pLiteral->str.push_back(*val);
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400112 escaping = false;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400113 }
114 }
115
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100116 pLiteral->type = SPV_LITERAL_TYPE_STRING;
David Netoaffa6962015-08-24 15:33:14 -0400117 } else if (numPeriods == 1) {
118 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100119 float f = (float)d;
120 if (d == (double)f) {
121 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
122 pLiteral->value.f = f;
123 } else {
124 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
125 pLiteral->value.d = d;
126 }
127 } else if (isSigned) {
128 int64_t i64 = strtoll(textValue, nullptr, 10);
129 int32_t i32 = (int32_t)i64;
130 if (i64 == (int64_t)i32) {
131 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
132 pLiteral->value.i32 = i32;
133 } else {
134 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
135 pLiteral->value.i64 = i64;
136 }
137 } else {
138 uint64_t u64 = strtoull(textValue, nullptr, 10);
139 uint32_t u32 = (uint32_t)u64;
140 if (u64 == (uint64_t)u32) {
141 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
142 pLiteral->value.u32 = u32;
143 } else {
144 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
145 pLiteral->value.u64 = u64;
146 }
147 }
148
149 return SPV_SUCCESS;
150}
151
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400152namespace {
153
154/// Parses an immediate integer from text, guarding against overflow. If
155/// successful, adds the parsed value to pInst, advances the context past it,
156/// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics,
157/// and returns SPV_ERROR_INVALID_TEXT.
158spv_result_t encodeImmediate(libspirv::AssemblyContext* context,
159 const char* text, spv_instruction_t* pInst) {
160 assert(*text == '!');
David Neto78e677b2015-10-05 13:28:46 -0400161 uint32_t parse_result;
162 if (auto error =
David Neto51013d12015-10-14 11:31:51 -0400163 context->parseNumber(text + 1, SPV_ERROR_INVALID_TEXT, &parse_result,
David Neto78e677b2015-10-05 13:28:46 -0400164 "Invalid immediate integer: !"))
165 return error;
166 context->binaryEncodeU32(parse_result, pInst);
Ben Vanik01c8d7a2015-11-22 08:32:53 -0800167 context->seekForward(static_cast<uint32_t>(strlen(text)));
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400168 return SPV_SUCCESS;
169}
170
171} // anonymous namespace
172
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400173/// @brief Translate an Opcode operand to binary form
174///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400175/// @param[in] grammar the grammar to use for compilation
176/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400177/// @param[in] type of the operand
178/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400179/// @param[out] pInst return binary Opcode
180/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400181///
182/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400183spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400184 libspirv::AssemblyContext* context,
185 const spv_operand_type_t type,
186 const char* textValue,
187 spv_instruction_t* pInst,
188 spv_operand_pattern_t* pExpectedOperands) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100189 // NOTE: Handle immediate int in the stream
190 if ('!' == textValue[0]) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400191 if (auto error = encodeImmediate(context, textValue, pInst)) {
192 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400193 }
Dejan Mircevski897bff92015-09-29 10:38:18 -0400194 *pExpectedOperands =
195 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100196 return SPV_SUCCESS;
197 }
198
David Neto51013d12015-10-14 11:31:51 -0400199 // Optional literal operands can fail to parse. In that case use
200 // SPV_FAILED_MATCH to avoid emitting a diagostic. Use the following
201 // for those situations.
202 spv_result_t error_code_for_literals =
203 spvOperandIsOptional(type) ? SPV_FAILED_MATCH : SPV_ERROR_INVALID_TEXT;
204
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100205 switch (type) {
David Netoe3f70b92015-08-27 13:50:05 -0400206 case SPV_OPERAND_TYPE_ID:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400207 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto201caf72015-11-04 17:38:17 -0500208 case SPV_OPERAND_TYPE_RESULT_ID:
209 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
210 case SPV_OPERAND_TYPE_SCOPE_ID:
211 case SPV_OPERAND_TYPE_OPTIONAL_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400212 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100213 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100214 } else {
David Netoac508b02015-10-09 15:48:09 -0400215 return context->diagnostic() << "Expected id to start with %.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100216 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400217 if (!spvIsValidID(textValue)) {
David Netoac508b02015-10-09 15:48:09 -0400218 return context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400219 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400220 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400221 if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
David Netob5dc8fc2015-10-06 16:22:00 -0400222 spvInstructionAddWord(pInst, id);
David Neto2ae4a682015-11-09 18:55:42 -0500223
224 // Set the extended instruction type.
225 // The import set id is the 3rd operand of OpExtInst.
226 if (pInst->opcode == SpvOpExtInst && pInst->words.size() == 4) {
227 auto ext_inst_type = context->getExtInstTypeForId(pInst->words[3]);
228 if (ext_inst_type == SPV_EXT_INST_TYPE_NONE) {
229 return context->diagnostic()
230 << "Invalid extended instruction import Id "
231 << pInst->words[2];
232 }
233 pInst->extInstType = ext_inst_type;
234 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100235 } break;
David Neto201caf72015-11-04 17:38:17 -0500236
David Neto445ce442015-10-15 15:22:06 -0400237 case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
238 // The assembler accepts the symbolic name for an extended instruction,
239 // and emits its corresponding number.
240 spv_ext_inst_desc extInst;
241 if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
242 return context->diagnostic() << "Invalid extended instruction name '"
243 << textValue << "'.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100244 }
David Neto445ce442015-10-15 15:22:06 -0400245 spvInstructionAddWord(pInst, extInst->ext_inst);
246
247 // Prepare to parse the operands for the extended instructions.
248 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
David Neto0f166be2015-11-11 01:56:49 -0500249 } break;
David Neto445ce442015-10-15 15:22:06 -0400250
David Neto0f166be2015-11-11 01:56:49 -0500251 case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
252 // The assembler accepts the symbolic name for the opcode, but without
253 // the "Op" prefix. For example, "IAdd" is accepted. The number
254 // of the opcode is emitted.
255 SpvOp opcode;
256 if (grammar.lookupSpecConstantOpcode(textValue, &opcode)) {
257 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
258 << " '" << textValue << "'.";
259 }
260 spv_opcode_desc opcodeEntry = nullptr;
261 if (grammar.lookupOpcode(opcode, &opcodeEntry)) {
262 return context->diagnostic(SPV_ERROR_INTERNAL)
263 << "OpSpecConstant opcode table out of sync";
264 }
265 spvInstructionAddWord(pInst, uint32_t(opcodeEntry->opcode));
266
267 // Prepare to parse the operands for the opcode. Except skip the
268 // type Id and result Id, since they've already been processed.
269 assert(opcodeEntry->hasType);
270 assert(opcodeEntry->hasResult);
271 assert(opcodeEntry->numTypes >= 2);
272 spvPrependOperandTypes(opcodeEntry->operandTypes + 2, pExpectedOperands);
David Neto445ce442015-10-15 15:22:06 -0400273 } break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400274
David Neto201caf72015-11-04 17:38:17 -0500275 case SPV_OPERAND_TYPE_LITERAL_INTEGER:
276 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: {
277 // The current operand is an *unsigned* 32-bit integer.
278 // That's just how the grammar works.
279 libspirv::IdType expected_type = {
280 32, false, libspirv::IdTypeClass::kScalarIntegerType};
David Neto78e677b2015-10-05 13:28:46 -0400281 if (auto error = context->binaryEncodeNumericLiteral(
David Neto51013d12015-10-14 11:31:51 -0400282 textValue, error_code_for_literals, expected_type, pInst)) {
David Neto78e677b2015-10-05 13:28:46 -0400283 return error;
David Neto51013d12015-10-14 11:31:51 -0400284 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100285 } break;
David Neto201caf72015-11-04 17:38:17 -0500286
287 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
288 // This is a context-independent literal number which can be a 32-bit
289 // number of floating point value.
290 if (auto error = context->binaryEncodeNumericLiteral(
291 textValue, error_code_for_literals, libspirv::kUnknownType,
292 pInst)) {
293 return error;
294 }
295 break;
296
297 case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
298 case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: {
299 libspirv::IdType expected_type = libspirv::kUnknownType;
300 // The encoding for OpConstant, OpSpecConstant and OpSwitch all
301 // depend on either their own result-id or the result-id of
302 // one of their parameters.
303 if (SpvOpConstant == pInst->opcode ||
304 SpvOpSpecConstant == pInst->opcode) {
305 // The type of the literal is determined by the type Id of the
306 // instruction.
307 expected_type =
308 context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
309 if (!libspirv::isScalarFloating(expected_type) &&
310 !libspirv::isScalarIntegral(expected_type)) {
311 spv_opcode_desc d;
312 const char* opcode_name = "opcode";
313 if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
314 opcode_name = d->name;
315 }
316 return context->diagnostic()
317 << "Type for " << opcode_name
318 << " must be a scalar floating point or integer type";
319 }
320 } else if (pInst->opcode == SpvOpSwitch) {
321 // The type of the literal is the same as the type of the selector.
322 expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
323 if (!libspirv::isScalarIntegral(expected_type)) {
324 return context->diagnostic()
325 << "The selector operand for OpSwitch must be the result"
326 " of an instruction that generates an integer scalar";
327 }
328 }
329 if (auto error = context->binaryEncodeNumericLiteral(
330 textValue, error_code_for_literals, expected_type, pInst)) {
331 return error;
332 }
333 } break;
334
David Neto78c3b432015-08-27 13:03:52 -0400335 case SPV_OPERAND_TYPE_LITERAL_STRING:
336 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400337 spv_literal_t literal = {};
338 spv_result_t error = spvTextToLiteral(textValue, &literal);
339 if (error != SPV_SUCCESS) {
340 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
David Neto51013d12015-10-14 11:31:51 -0400341 return context->diagnostic(error_code_for_literals)
342 << "Invalid literal string '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400343 }
Lei Zhang6d415812015-09-15 13:36:21 -0400344 if (literal.type != SPV_LITERAL_TYPE_STRING) {
David Neto51013d12015-10-14 11:31:51 -0400345 return context->diagnostic()
David Netoac508b02015-10-09 15:48:09 -0400346 << "Expected literal string, found literal number '" << textValue
347 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400348 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100349
350 // NOTE: Special case for extended instruction library import
Lei Zhangb36e7042015-10-28 13:40:52 -0400351 if (SpvOpExtInstImport == pInst->opcode) {
David Neto2ae4a682015-11-09 18:55:42 -0500352 const spv_ext_inst_type_t ext_inst_type =
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500353 spvExtInstImportTypeGet(literal.str.c_str());
David Neto2ae4a682015-11-09 18:55:42 -0500354 if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
355 return context->diagnostic()
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500356 << "Invalid extended instruction import '" << literal.str
David Neto2ae4a682015-11-09 18:55:42 -0500357 << "'";
358 }
David Neto677e0c72016-01-05 14:56:02 -0500359 if ((error = context->recordIdAsExtInstImport(pInst->words[1],
360 ext_inst_type)))
David Neto2ae4a682015-11-09 18:55:42 -0500361 return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100362 }
363
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500364 if (context->binaryEncodeString(literal.str.c_str(), pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400365 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100366 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400367 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
368 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
369 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Neto2889a0c2016-02-15 13:50:00 -0500370 case SPV_OPERAND_TYPE_IMAGE:
David Netoee1b3bb2015-09-18 11:19:18 -0400371 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400372 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400373 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
374 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400375 if (grammar.parseMaskOperand(type, textValue, &value)) {
David Netoac508b02015-10-09 15:48:09 -0400376 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
David Netod9ad0502015-11-24 18:37:24 -0500377 << " operand '" << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400378 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400379 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400380 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400381 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400382 } break;
383 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
384 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400385 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
386 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400387 if (error == SPV_FAILED_MATCH) {
388 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400389 error = spvTextEncodeOperand(grammar, context,
390 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
391 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400392 }
393 if (error == SPV_FAILED_MATCH) {
394 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400395 error =
396 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
397 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400398 }
399 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400400 return context->diagnostic(error)
401 << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400402 }
403 if (pExpectedOperands->empty()) {
404 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
405 }
David Neto36b0c0f2015-09-16 18:32:54 -0400406 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100407 default: {
408 // NOTE: All non literal operands are handled here using the operand
409 // table.
410 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400411 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
David Netoac508b02015-10-09 15:48:09 -0400412 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
413 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400414 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400415 if (context->binaryEncodeU32(entry->value, pInst)) {
David Netoac508b02015-10-09 15:48:09 -0400416 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
417 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400418 }
David Neto78c3b432015-08-27 13:03:52 -0400419
420 // Prepare to parse the operands for this logical operand.
421 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100422 } break;
423 }
424 return SPV_SUCCESS;
425}
426
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400427namespace {
428
429/// Encodes an instruction started by !<integer> at the given position in text.
430///
431/// Puts the encoded words into *pInst. If successful, moves position past the
432/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
433/// leaves position pointing to the error in text.
434spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400435 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400436 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400437 std::string firstWord;
438 spv_position_t nextPosition = {};
Lei Zhang6032b982016-03-15 16:52:40 -0400439 auto error = context->getWord(&firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400440 if (error) return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400441
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400442 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
443 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400444 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400445 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400446 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400447 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400448
449 // Otherwise, there must be an operand that's either a literal, an ID, or
450 // an immediate.
451 std::string operandValue;
Lei Zhang6032b982016-03-15 16:52:40 -0400452 if ((error = context->getWord(&operandValue, &nextPosition)))
David Netoac508b02015-10-09 15:48:09 -0400453 return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400454
David Netoac508b02015-10-09 15:48:09 -0400455 if (operandValue == "=")
456 return context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400457
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400458 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
459 // expanded.
460 spv_operand_pattern_t dummyExpectedOperands;
461 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400462 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
463 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400464 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400465 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400466 }
467 return SPV_SUCCESS;
468}
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400469
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400470/// @brief Translate single Opcode and operands to binary form
471///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400472/// @param[in] grammar the grammar to use for compilation
473/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400474/// @param[in] text stream to translate
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400475/// @param[out] pInst returned binary Opcode
476/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400477///
478/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400479spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400480 libspirv::AssemblyContext* context,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400481 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400482 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400483 if ('!' == context->peek()) {
484 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400485 }
486
Lei Zhang06efdc52015-09-10 14:00:00 -0400487 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100488 spv_position_t nextPosition = {};
Lei Zhang6032b982016-03-15 16:52:40 -0400489 spv_result_t error = context->getWord(&firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400490 if (error) return context->diagnostic() << "Internal Error";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100491
Lei Zhang06efdc52015-09-10 14:00:00 -0400492 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400493 std::string result_id;
494 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400495 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400496 opcodeName = firstWord;
497 } else {
Lei Zhang06efdc52015-09-10 14:00:00 -0400498 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400499 if ('%' != result_id.front()) {
David Netoac508b02015-10-09 15:48:09 -0400500 return context->diagnostic()
501 << "Expected <opcode> or <result-id> at the beginning "
502 "of an instruction, found '"
503 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400504 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400505 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400506
507 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400508 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400509 if (context->advance())
510 return context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400511 std::string equal_sign;
Lei Zhang6032b982016-03-15 16:52:40 -0400512 error = context->getWord(&equal_sign, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400513 if ("=" != equal_sign)
514 return context->diagnostic() << "'=' expected after result id.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400515
516 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400517 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400518 if (context->advance())
519 return context->diagnostic() << "Expected opcode, found end of stream.";
Lei Zhang6032b982016-03-15 16:52:40 -0400520 error = context->getWord(&opcodeName, &nextPosition);
Lei Zhang6483bd72015-10-14 17:02:39 -0400521 if (error) return context->diagnostic(error) << "Internal Error";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400522 if (!context->startsWithOp()) {
David Netoac508b02015-10-09 15:48:09 -0400523 return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName
524 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400525 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400526 }
527
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100528 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400529 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100530
531 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400532 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400533 if (error) {
Lei Zhang6032b982016-03-15 16:52:40 -0400534 return context->diagnostic(error) << "Invalid Opcode name '" << opcodeName
535 << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400536 }
Lei Zhang9042f402015-11-05 17:45:09 -0500537 if (opcodeEntry->hasResult && result_id.empty()) {
538 return context->diagnostic()
539 << "Expected <result-id> at the beginning of an instruction, found '"
540 << firstWord << "'.";
Lei Zhang06efdc52015-09-10 14:00:00 -0400541 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100542 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400543 context->setPosition(nextPosition);
David Netob5dc8fc2015-10-06 16:22:00 -0400544 // Reserve the first word for the instruction.
545 spvInstructionAddWord(pInst, 0);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100546
David Neto78c3b432015-08-27 13:03:52 -0400547 // Maintains the ordered list of expected operand types.
548 // For many instructions we only need the {numTypes, operandTypes}
549 // entries in opcodeEntry. However, sometimes we need to modify
550 // the list as we parse the operands. This occurs when an operand
551 // has its own logical operands (such as the LocalSize operand for
552 // ExecutionMode), or for extended instructions that may have their
553 // own operands depending on the selected extended instruction.
554 spv_operand_pattern_t expectedOperands(
555 opcodeEntry->operandTypes,
556 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400557
David Neto78c3b432015-08-27 13:03:52 -0400558 while (!expectedOperands.empty()) {
559 const spv_operand_type_t type = expectedOperands.front();
560 expectedOperands.pop_front();
561
562 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400563 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400564
565 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
566 // Handle the <result-id> for value generating instructions.
567 // We've already consumed it from the text stream. Here
568 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400569 spv_position_t temp_pos = context->position();
570 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
571 result_id.c_str(), pInst, nullptr);
572 result_id_position = context->position();
573 // Because we are injecting we have to reset the position afterwards.
574 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400575 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100576 } else {
David Neto78c3b432015-08-27 13:03:52 -0400577 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400578 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400579 if (error == SPV_END_OF_STREAM) {
580 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400581 // This would have been the last potential operand for the
582 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400583 // and we didn't find one. We're finished parsing this instruction.
584 break;
585 } else {
David Netoac508b02015-10-09 15:48:09 -0400586 return context->diagnostic()
587 << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400588 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100589 }
David Neto78c3b432015-08-27 13:03:52 -0400590 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
591
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400592 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400593 if (spvOperandIsOptional(type)) {
594 break;
595 } else {
David Netoac508b02015-10-09 15:48:09 -0400596 return context->diagnostic()
597 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400598 }
599 }
600
601 std::string operandValue;
Lei Zhang6032b982016-03-15 16:52:40 -0400602 error = context->getWord(&operandValue, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400603 if (error) return context->diagnostic(error) << "Internal Error";
David Neto78c3b432015-08-27 13:03:52 -0400604
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400605 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
606 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400607
608 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
609 return SPV_SUCCESS;
610
Lei Zhang40056702015-09-11 14:31:27 -0400611 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400612
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400613 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100614 }
615 }
616
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400617 if (spvOpcodeGeneratesType(pInst->opcode)) {
618 if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
619 return SPV_ERROR_INVALID_TEXT;
620 }
621 } else if (opcodeEntry->hasType) {
622 // SPIR-V dictates that if an instruction has both a return value and a
623 // type ID then the type id is first, and the return value is second.
624 assert(opcodeEntry->hasResult &&
625 "Unknown opcode: has a type but no result.");
626 context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
627 }
628
David Netob5dc8fc2015-10-06 16:22:00 -0400629 if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Netoac508b02015-10-09 15:48:09 -0400630 return context->diagnostic()
631 << "Instruction too long: " << pInst->words.size()
632 << " words, but the limit is "
633 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
David Netob5dc8fc2015-10-06 16:22:00 -0400634 }
635
David Netod9ad0502015-11-24 18:37:24 -0500636 pInst->words[0] =
637 spvOpcodeMake(uint16_t(pInst->words.size()), opcodeEntry->opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100638
639 return SPV_SUCCESS;
640}
641
David Netod9ad0502015-11-24 18:37:24 -0500642enum { kAssemblerVersion = 0 };
David Neto14b93e42015-11-12 18:33:47 -0500643
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400644// Populates a binary stream's |header|. The target environment is specified via
645// |env| and Id bound is via |bound|.
646spv_result_t SetHeader(spv_target_env env, const uint32_t bound,
647 uint32_t* header) {
648 if (!header) return SPV_ERROR_INVALID_BINARY;
David Neto0b981682015-10-27 15:51:34 -0400649
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400650 header[SPV_INDEX_MAGIC_NUMBER] = SpvMagicNumber;
651 header[SPV_INDEX_VERSION_NUMBER] = spvVersionForTargetEnv(env);
652 header[SPV_INDEX_GENERATOR_NUMBER] =
David Neto14b93e42015-11-12 18:33:47 -0500653 SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS_ASSEMBLER, kAssemblerVersion);
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400654 header[SPV_INDEX_BOUND] = bound;
655 header[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
David Neto0b981682015-10-27 15:51:34 -0400656
657 return SPV_SUCCESS;
658}
659
David Netoc9786432015-09-01 18:05:14 -0400660// Translates a given assembly language module into binary form.
661// If a diagnostic is generated, it is not yet marked as being
662// for a text-based input.
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400663spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
Lei Zhang9042f402015-11-05 17:45:09 -0500664 const spv_text text, spv_binary* pBinary,
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400665 spv_diagnostic* pDiagnostic) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400666 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
667 libspirv::AssemblyContext context(text, pDiagnostic);
David Neto201caf72015-11-04 17:38:17 -0500668 if (!text->str) return context.diagnostic() << "Missing assembly text.";
David Netoac508b02015-10-09 15:48:09 -0400669
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400670 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400671 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400672 }
Lei Zhang40056702015-09-11 14:31:27 -0400673 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100674
675 // NOTE: Ensure diagnostic is zero initialised
676 *pDiagnostic = {};
677
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100678 std::vector<spv_instruction_t> instructions;
679
David Netoea633a62015-11-02 11:40:59 -0500680 // Skip past whitespace and comments.
681 context.advance();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100682
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400683 while (context.hasText()) {
David Netob5dc8fc2015-10-06 16:22:00 -0400684 instructions.push_back({});
685 spv_instruction_t& inst = instructions.back();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100686
Lei Zhang9042f402015-11-05 17:45:09 -0500687 if (spvTextEncodeOpcode(grammar, &context, &inst)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400688 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400689 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400690
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400691 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100692 }
693
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100694 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400695 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400696 totalSize += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100697 }
698
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400699 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400700 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100701 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400702 for (auto& inst : instructions) {
Lei Zhang6483bd72015-10-14 17:02:39 -0400703 memcpy(data + currentIndex, inst.words.data(),
704 sizeof(uint32_t) * inst.words.size());
David Netob5dc8fc2015-10-06 16:22:00 -0400705 currentIndex += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100706 }
707
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400708 if (auto error = SetHeader(grammar.target_env(), context.getBound(), data))
709 return error;
David Netoe4945de2015-10-28 13:50:32 -0400710
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100711 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400712 if (!binary) {
713 delete[] data;
714 return SPV_ERROR_OUT_OF_MEMORY;
715 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100716 binary->code = data;
717 binary->wordCount = totalSize;
718
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100719 *pBinary = binary;
720
721 return SPV_SUCCESS;
722}
723
Lei Zhange78a7c12015-09-10 17:07:21 -0400724} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400725
Lei Zhang972788b2015-11-12 13:48:30 -0500726spv_result_t spvTextToBinary(const spv_const_context context,
727 const char* input_text,
Lei Zhangdf920ec2015-11-11 11:33:26 -0500728 const size_t input_text_size, spv_binary* pBinary,
729 spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400730 spv_text_t text = {input_text, input_text_size};
Lei Zhang972788b2015-11-12 13:48:30 -0500731 libspirv::AssemblyGrammar grammar(context);
Lei Zhang9042f402015-11-05 17:45:09 -0500732
Lei Zhang06efdc52015-09-10 14:00:00 -0400733 spv_result_t result =
Lei Zhang9042f402015-11-05 17:45:09 -0500734 spvTextToBinaryInternal(grammar, &text, 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;
Eric Engestromeb6ae972016-02-18 23:41:16 +0000742 delete[] text->str;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100743 delete text;
744}