blob: fa6e6eb4faeb418d833303dde74dec6401a55c00 [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 Neto0f166be2015-11-11 01:56:49 -050041#include <libspirv/libspirv.h>
David Netofcc7d582015-10-27 15:31:10 -040042#include "assembly_grammar.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010043#include "binary.h"
44#include "diagnostic.h"
45#include "ext_inst.h"
David Netob5dc8fc2015-10-06 16:22:00 -040046#include "instruction.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;
115 size_t write_index = 0;
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
121 if (write_index >= sizeof(pLiteral->value.str) - 1)
122 return SPV_ERROR_OUT_OF_MEMORY;
123 pLiteral->value.str[write_index] = *val;
124 escaping = false;
125 ++write_index;
126 }
127 }
128
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100129 pLiteral->type = SPV_LITERAL_TYPE_STRING;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400130 pLiteral->value.str[write_index] = '\0';
David Netoaffa6962015-08-24 15:33:14 -0400131 } else if (numPeriods == 1) {
132 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100133 float f = (float)d;
134 if (d == (double)f) {
135 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
136 pLiteral->value.f = f;
137 } else {
138 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
139 pLiteral->value.d = d;
140 }
141 } else if (isSigned) {
142 int64_t i64 = strtoll(textValue, nullptr, 10);
143 int32_t i32 = (int32_t)i64;
144 if (i64 == (int64_t)i32) {
145 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
146 pLiteral->value.i32 = i32;
147 } else {
148 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
149 pLiteral->value.i64 = i64;
150 }
151 } else {
152 uint64_t u64 = strtoull(textValue, nullptr, 10);
153 uint32_t u32 = (uint32_t)u64;
154 if (u64 == (uint64_t)u32) {
155 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
156 pLiteral->value.u32 = u32;
157 } else {
158 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
159 pLiteral->value.u64 = u64;
160 }
161 }
162
163 return SPV_SUCCESS;
164}
165
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400166namespace {
167
168/// Parses an immediate integer from text, guarding against overflow. If
169/// successful, adds the parsed value to pInst, advances the context past it,
170/// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics,
171/// and returns SPV_ERROR_INVALID_TEXT.
172spv_result_t encodeImmediate(libspirv::AssemblyContext* context,
173 const char* text, spv_instruction_t* pInst) {
174 assert(*text == '!');
David Neto78e677b2015-10-05 13:28:46 -0400175 uint32_t parse_result;
176 if (auto error =
David Neto51013d12015-10-14 11:31:51 -0400177 context->parseNumber(text + 1, SPV_ERROR_INVALID_TEXT, &parse_result,
David Neto78e677b2015-10-05 13:28:46 -0400178 "Invalid immediate integer: !"))
179 return error;
180 context->binaryEncodeU32(parse_result, pInst);
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400181 context->seekForward(strlen(text));
182 return SPV_SUCCESS;
183}
184
185} // anonymous namespace
186
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400187/// @brief Translate an Opcode operand to binary form
188///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400189/// @param[in] grammar the grammar to use for compilation
190/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400191/// @param[in] type of the operand
192/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400193/// @param[out] pInst return binary Opcode
194/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400195///
196/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400197spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400198 libspirv::AssemblyContext* context,
199 const spv_operand_type_t type,
200 const char* textValue,
201 spv_instruction_t* pInst,
202 spv_operand_pattern_t* pExpectedOperands) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100203 // NOTE: Handle immediate int in the stream
204 if ('!' == textValue[0]) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400205 if (auto error = encodeImmediate(context, textValue, pInst)) {
206 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400207 }
Dejan Mircevski897bff92015-09-29 10:38:18 -0400208 *pExpectedOperands =
209 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100210 return SPV_SUCCESS;
211 }
212
David Neto51013d12015-10-14 11:31:51 -0400213 // Optional literal operands can fail to parse. In that case use
214 // SPV_FAILED_MATCH to avoid emitting a diagostic. Use the following
215 // for those situations.
216 spv_result_t error_code_for_literals =
217 spvOperandIsOptional(type) ? SPV_FAILED_MATCH : SPV_ERROR_INVALID_TEXT;
218
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100219 switch (type) {
David Netoe3f70b92015-08-27 13:50:05 -0400220 case SPV_OPERAND_TYPE_ID:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400221 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto201caf72015-11-04 17:38:17 -0500222 case SPV_OPERAND_TYPE_RESULT_ID:
223 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
224 case SPV_OPERAND_TYPE_SCOPE_ID:
225 case SPV_OPERAND_TYPE_OPTIONAL_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400226 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100227 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100228 } else {
David Netoac508b02015-10-09 15:48:09 -0400229 return context->diagnostic() << "Expected id to start with %.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100230 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400231 if (!spvIsValidID(textValue)) {
David Netoac508b02015-10-09 15:48:09 -0400232 return context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400233 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400234 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400235 if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
David Netob5dc8fc2015-10-06 16:22:00 -0400236 spvInstructionAddWord(pInst, id);
David Neto2ae4a682015-11-09 18:55:42 -0500237
238 // Set the extended instruction type.
239 // The import set id is the 3rd operand of OpExtInst.
240 if (pInst->opcode == SpvOpExtInst && pInst->words.size() == 4) {
241 auto ext_inst_type = context->getExtInstTypeForId(pInst->words[3]);
242 if (ext_inst_type == SPV_EXT_INST_TYPE_NONE) {
243 return context->diagnostic()
244 << "Invalid extended instruction import Id "
245 << pInst->words[2];
246 }
247 pInst->extInstType = ext_inst_type;
248 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100249 } break;
David Neto201caf72015-11-04 17:38:17 -0500250
David Neto445ce442015-10-15 15:22:06 -0400251 case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
252 // The assembler accepts the symbolic name for an extended instruction,
253 // and emits its corresponding number.
254 spv_ext_inst_desc extInst;
255 if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
256 return context->diagnostic() << "Invalid extended instruction name '"
257 << textValue << "'.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100258 }
David Neto445ce442015-10-15 15:22:06 -0400259 spvInstructionAddWord(pInst, extInst->ext_inst);
260
261 // Prepare to parse the operands for the extended instructions.
262 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
David Neto0f166be2015-11-11 01:56:49 -0500263 } break;
David Neto445ce442015-10-15 15:22:06 -0400264
David Neto0f166be2015-11-11 01:56:49 -0500265 case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
266 // The assembler accepts the symbolic name for the opcode, but without
267 // the "Op" prefix. For example, "IAdd" is accepted. The number
268 // of the opcode is emitted.
269 SpvOp opcode;
270 if (grammar.lookupSpecConstantOpcode(textValue, &opcode)) {
271 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
272 << " '" << textValue << "'.";
273 }
274 spv_opcode_desc opcodeEntry = nullptr;
275 if (grammar.lookupOpcode(opcode, &opcodeEntry)) {
276 return context->diagnostic(SPV_ERROR_INTERNAL)
277 << "OpSpecConstant opcode table out of sync";
278 }
279 spvInstructionAddWord(pInst, uint32_t(opcodeEntry->opcode));
280
281 // Prepare to parse the operands for the opcode. Except skip the
282 // type Id and result Id, since they've already been processed.
283 assert(opcodeEntry->hasType);
284 assert(opcodeEntry->hasResult);
285 assert(opcodeEntry->numTypes >= 2);
286 spvPrependOperandTypes(opcodeEntry->operandTypes + 2, pExpectedOperands);
David Neto445ce442015-10-15 15:22:06 -0400287 } break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400288
David Neto201caf72015-11-04 17:38:17 -0500289 case SPV_OPERAND_TYPE_LITERAL_INTEGER:
290 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: {
291 // The current operand is an *unsigned* 32-bit integer.
292 // That's just how the grammar works.
293 libspirv::IdType expected_type = {
294 32, false, libspirv::IdTypeClass::kScalarIntegerType};
David Neto78e677b2015-10-05 13:28:46 -0400295 if (auto error = context->binaryEncodeNumericLiteral(
David Neto51013d12015-10-14 11:31:51 -0400296 textValue, error_code_for_literals, expected_type, pInst)) {
David Neto78e677b2015-10-05 13:28:46 -0400297 return error;
David Neto51013d12015-10-14 11:31:51 -0400298 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100299 } break;
David Neto201caf72015-11-04 17:38:17 -0500300
301 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
302 // This is a context-independent literal number which can be a 32-bit
303 // number of floating point value.
304 if (auto error = context->binaryEncodeNumericLiteral(
305 textValue, error_code_for_literals, libspirv::kUnknownType,
306 pInst)) {
307 return error;
308 }
309 break;
310
311 case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
312 case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: {
313 libspirv::IdType expected_type = libspirv::kUnknownType;
314 // The encoding for OpConstant, OpSpecConstant and OpSwitch all
315 // depend on either their own result-id or the result-id of
316 // one of their parameters.
317 if (SpvOpConstant == pInst->opcode ||
318 SpvOpSpecConstant == pInst->opcode) {
319 // The type of the literal is determined by the type Id of the
320 // instruction.
321 expected_type =
322 context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
323 if (!libspirv::isScalarFloating(expected_type) &&
324 !libspirv::isScalarIntegral(expected_type)) {
325 spv_opcode_desc d;
326 const char* opcode_name = "opcode";
327 if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
328 opcode_name = d->name;
329 }
330 return context->diagnostic()
331 << "Type for " << opcode_name
332 << " must be a scalar floating point or integer type";
333 }
334 } else if (pInst->opcode == SpvOpSwitch) {
335 // The type of the literal is the same as the type of the selector.
336 expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
337 if (!libspirv::isScalarIntegral(expected_type)) {
338 return context->diagnostic()
339 << "The selector operand for OpSwitch must be the result"
340 " of an instruction that generates an integer scalar";
341 }
342 }
343 if (auto error = context->binaryEncodeNumericLiteral(
344 textValue, error_code_for_literals, expected_type, pInst)) {
345 return error;
346 }
347 } break;
348
David Neto78c3b432015-08-27 13:03:52 -0400349 case SPV_OPERAND_TYPE_LITERAL_STRING:
350 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400351 spv_literal_t literal = {};
352 spv_result_t error = spvTextToLiteral(textValue, &literal);
353 if (error != SPV_SUCCESS) {
354 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
David Neto51013d12015-10-14 11:31:51 -0400355 return context->diagnostic(error_code_for_literals)
356 << "Invalid literal string '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400357 }
Lei Zhang6d415812015-09-15 13:36:21 -0400358 if (literal.type != SPV_LITERAL_TYPE_STRING) {
David Neto51013d12015-10-14 11:31:51 -0400359 return context->diagnostic()
David Netoac508b02015-10-09 15:48:09 -0400360 << "Expected literal string, found literal number '" << textValue
361 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400362 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100363
364 // NOTE: Special case for extended instruction library import
Lei Zhangb36e7042015-10-28 13:40:52 -0400365 if (SpvOpExtInstImport == pInst->opcode) {
David Neto2ae4a682015-11-09 18:55:42 -0500366 const spv_ext_inst_type_t ext_inst_type =
367 spvExtInstImportTypeGet(literal.value.str);
368 if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
369 return context->diagnostic()
370 << "Invalid extended instruction import '" << literal.value.str
371 << "'";
372 }
David Neto0f166be2015-11-11 01:56:49 -0500373 if (auto error = context->recordIdAsExtInstImport(pInst->words[1],
374 ext_inst_type))
David Neto2ae4a682015-11-09 18:55:42 -0500375 return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100376 }
377
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400378 if (context->binaryEncodeString(literal.value.str, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400379 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100380 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400381 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
382 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
383 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400384 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400385 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400386 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
387 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400388 if (grammar.parseMaskOperand(type, textValue, &value)) {
David Netoac508b02015-10-09 15:48:09 -0400389 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
390 << " '" << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400391 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400392 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400393 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400394 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400395 } break;
396 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
397 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400398 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
399 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400400 if (error == SPV_FAILED_MATCH) {
401 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400402 error = spvTextEncodeOperand(grammar, context,
403 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
404 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400405 }
406 if (error == SPV_FAILED_MATCH) {
407 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400408 error =
409 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
410 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400411 }
412 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400413 return context->diagnostic(error)
414 << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400415 }
416 if (pExpectedOperands->empty()) {
417 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
418 }
David Neto36b0c0f2015-09-16 18:32:54 -0400419 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100420 default: {
421 // NOTE: All non literal operands are handled here using the operand
422 // table.
423 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400424 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
David Netoac508b02015-10-09 15:48:09 -0400425 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
426 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400427 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400428 if (context->binaryEncodeU32(entry->value, pInst)) {
David Netoac508b02015-10-09 15:48:09 -0400429 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
430 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400431 }
David Neto78c3b432015-08-27 13:03:52 -0400432
433 // Prepare to parse the operands for this logical operand.
434 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100435 } break;
436 }
437 return SPV_SUCCESS;
438}
439
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400440namespace {
441
442/// Encodes an instruction started by !<integer> at the given position in text.
443///
444/// Puts the encoded words into *pInst. If successful, moves position past the
445/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
446/// leaves position pointing to the error in text.
447spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400448 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400449 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400450 std::string firstWord;
451 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400452 auto error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400453 if (error) return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400454
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400455 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
456 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400457 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400458 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400459 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400460 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400461
462 // Otherwise, there must be an operand that's either a literal, an ID, or
463 // an immediate.
464 std::string operandValue;
David Netoac508b02015-10-09 15:48:09 -0400465 if ((error = context->getWord(operandValue, &nextPosition)))
466 return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400467
David Netoac508b02015-10-09 15:48:09 -0400468 if (operandValue == "=")
469 return context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400470
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400471 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
472 // expanded.
473 spv_operand_pattern_t dummyExpectedOperands;
474 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400475 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
476 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400477 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400478 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400479 }
480 return SPV_SUCCESS;
481}
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400482} // anonymous namespace
483
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400484/// @brief Translate single Opcode and operands to binary form
485///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400486/// @param[in] grammar the grammar to use for compilation
487/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400488/// @param[in] text stream to translate
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400489/// @param[out] pInst returned binary Opcode
490/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400491///
492/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400493spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400494 libspirv::AssemblyContext* context,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400495 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400496 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400497 if ('!' == context->peek()) {
498 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400499 }
500
Lei Zhang06efdc52015-09-10 14:00:00 -0400501 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100502 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400503 spv_result_t error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400504 if (error) return context->diagnostic() << "Internal Error";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100505
Lei Zhang06efdc52015-09-10 14:00:00 -0400506 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400507 std::string result_id;
508 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400509 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400510 opcodeName = firstWord;
511 } else {
Lei Zhang06efdc52015-09-10 14:00:00 -0400512 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400513 if ('%' != result_id.front()) {
David Netoac508b02015-10-09 15:48:09 -0400514 return context->diagnostic()
515 << "Expected <opcode> or <result-id> at the beginning "
516 "of an instruction, found '"
517 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400518 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400519 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400520
521 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400522 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400523 if (context->advance())
524 return context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400525 std::string equal_sign;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400526 error = context->getWord(equal_sign, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400527 if ("=" != equal_sign)
528 return context->diagnostic() << "'=' expected after result id.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400529
530 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400531 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400532 if (context->advance())
533 return context->diagnostic() << "Expected opcode, found end of stream.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400534 error = context->getWord(opcodeName, &nextPosition);
Lei Zhang6483bd72015-10-14 17:02:39 -0400535 if (error) return context->diagnostic(error) << "Internal Error";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400536 if (!context->startsWithOp()) {
David Netoac508b02015-10-09 15:48:09 -0400537 return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName
538 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400539 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400540 }
541
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100542 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400543 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100544
545 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400546 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400547 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400548 return context->diagnostic(error) << "Invalid Opcode name '"
549 << context->getWord() << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400550 }
Lei Zhang9042f402015-11-05 17:45:09 -0500551 if (opcodeEntry->hasResult && result_id.empty()) {
552 return context->diagnostic()
553 << "Expected <result-id> at the beginning of an instruction, found '"
554 << firstWord << "'.";
Lei Zhang06efdc52015-09-10 14:00:00 -0400555 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100556 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400557 context->setPosition(nextPosition);
David Netob5dc8fc2015-10-06 16:22:00 -0400558 // Reserve the first word for the instruction.
559 spvInstructionAddWord(pInst, 0);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100560
David Neto78c3b432015-08-27 13:03:52 -0400561 // Maintains the ordered list of expected operand types.
562 // For many instructions we only need the {numTypes, operandTypes}
563 // entries in opcodeEntry. However, sometimes we need to modify
564 // the list as we parse the operands. This occurs when an operand
565 // has its own logical operands (such as the LocalSize operand for
566 // ExecutionMode), or for extended instructions that may have their
567 // own operands depending on the selected extended instruction.
568 spv_operand_pattern_t expectedOperands(
569 opcodeEntry->operandTypes,
570 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400571
David Neto78c3b432015-08-27 13:03:52 -0400572 while (!expectedOperands.empty()) {
573 const spv_operand_type_t type = expectedOperands.front();
574 expectedOperands.pop_front();
575
576 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400577 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400578
579 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
580 // Handle the <result-id> for value generating instructions.
581 // We've already consumed it from the text stream. Here
582 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400583 spv_position_t temp_pos = context->position();
584 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
585 result_id.c_str(), pInst, nullptr);
586 result_id_position = context->position();
587 // Because we are injecting we have to reset the position afterwards.
588 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400589 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100590 } else {
David Neto78c3b432015-08-27 13:03:52 -0400591 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400592 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400593 if (error == SPV_END_OF_STREAM) {
594 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400595 // This would have been the last potential operand for the
596 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400597 // and we didn't find one. We're finished parsing this instruction.
598 break;
599 } else {
David Netoac508b02015-10-09 15:48:09 -0400600 return context->diagnostic()
601 << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400602 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100603 }
David Neto78c3b432015-08-27 13:03:52 -0400604 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
605
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400606 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400607 if (spvOperandIsOptional(type)) {
608 break;
609 } else {
David Netoac508b02015-10-09 15:48:09 -0400610 return context->diagnostic()
611 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400612 }
613 }
614
615 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400616 error = context->getWord(operandValue, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400617 if (error) return context->diagnostic(error) << "Internal Error";
David Neto78c3b432015-08-27 13:03:52 -0400618
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400619 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
620 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400621
622 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
623 return SPV_SUCCESS;
624
Lei Zhang40056702015-09-11 14:31:27 -0400625 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400626
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400627 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100628 }
629 }
630
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400631 if (spvOpcodeGeneratesType(pInst->opcode)) {
632 if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
633 return SPV_ERROR_INVALID_TEXT;
634 }
635 } else if (opcodeEntry->hasType) {
636 // SPIR-V dictates that if an instruction has both a return value and a
637 // type ID then the type id is first, and the return value is second.
638 assert(opcodeEntry->hasResult &&
639 "Unknown opcode: has a type but no result.");
640 context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
641 }
642
David Netob5dc8fc2015-10-06 16:22:00 -0400643 if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Netoac508b02015-10-09 15:48:09 -0400644 return context->diagnostic()
645 << "Instruction too long: " << pInst->words.size()
646 << " words, but the limit is "
647 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
David Netob5dc8fc2015-10-06 16:22:00 -0400648 }
649
650 pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100651
652 return SPV_SUCCESS;
653}
654
David Netoc9786432015-09-01 18:05:14 -0400655namespace {
656
David Netoe4945de2015-10-28 13:50:32 -0400657/// @brief Populate a binary stream's words with this generator's header.
David Neto0b981682015-10-27 15:51:34 -0400658///
David Netoe4945de2015-10-28 13:50:32 -0400659/// @param[in,out] words the array of words
David Neto0b981682015-10-27 15:51:34 -0400660/// @param[in] bound the upper ID bound
661///
662/// @return result code
David Netoe4945de2015-10-28 13:50:32 -0400663spv_result_t SetHeader(uint32_t* words, const uint32_t bound) {
664 if (!words) return SPV_ERROR_INVALID_BINARY;
David Neto0b981682015-10-27 15:51:34 -0400665
Lei Zhang9049bb42015-11-11 10:17:16 -0500666 words[SPV_INDEX_MAGIC_NUMBER] = SpvMagicNumber;
667 words[SPV_INDEX_VERSION_NUMBER] = SpvVersion;
David Netoe4945de2015-10-28 13:50:32 -0400668 words[SPV_INDEX_GENERATOR_NUMBER] = SPV_GENERATOR_KHRONOS;
669 words[SPV_INDEX_BOUND] = bound;
670 words[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
David Neto0b981682015-10-27 15:51:34 -0400671
672 return SPV_SUCCESS;
673}
674
David Netoc9786432015-09-01 18:05:14 -0400675// Translates a given assembly language module into binary form.
676// If a diagnostic is generated, it is not yet marked as being
677// for a text-based input.
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400678spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
Lei Zhang9042f402015-11-05 17:45:09 -0500679 const spv_text text, spv_binary* pBinary,
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400680 spv_diagnostic* pDiagnostic) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400681 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
682 libspirv::AssemblyContext context(text, pDiagnostic);
David Neto201caf72015-11-04 17:38:17 -0500683 if (!text->str) return context.diagnostic() << "Missing assembly text.";
David Netoac508b02015-10-09 15:48:09 -0400684
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400685 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400686 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400687 }
Lei Zhang40056702015-09-11 14:31:27 -0400688 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100689
690 // NOTE: Ensure diagnostic is zero initialised
691 *pDiagnostic = {};
692
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100693 std::vector<spv_instruction_t> instructions;
694
David Netoea633a62015-11-02 11:40:59 -0500695 // Skip past whitespace and comments.
696 context.advance();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100697
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400698 while (context.hasText()) {
David Netob5dc8fc2015-10-06 16:22:00 -0400699 instructions.push_back({});
700 spv_instruction_t& inst = instructions.back();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100701
Lei Zhang9042f402015-11-05 17:45:09 -0500702 if (spvTextEncodeOpcode(grammar, &context, &inst)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400703 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400704 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400705
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400706 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100707 }
708
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100709 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400710 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400711 totalSize += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100712 }
713
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400714 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400715 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100716 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400717 for (auto& inst : instructions) {
Lei Zhang6483bd72015-10-14 17:02:39 -0400718 memcpy(data + currentIndex, inst.words.data(),
719 sizeof(uint32_t) * inst.words.size());
David Netob5dc8fc2015-10-06 16:22:00 -0400720 currentIndex += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100721 }
722
David Neto201caf72015-11-04 17:38:17 -0500723 if (auto error = SetHeader(data, context.getBound())) return error;
David Netoe4945de2015-10-28 13:50:32 -0400724
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100725 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400726 if (!binary) {
727 delete[] data;
728 return SPV_ERROR_OUT_OF_MEMORY;
729 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100730 binary->code = data;
731 binary->wordCount = totalSize;
732
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100733 *pBinary = binary;
734
735 return SPV_SUCCESS;
736}
737
Lei Zhange78a7c12015-09-10 17:07:21 -0400738} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400739
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400740spv_result_t spvTextToBinary(const char* input_text,
Lei Zhangdf920ec2015-11-11 11:33:26 -0500741 const size_t input_text_size, spv_binary* pBinary,
742 spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400743 spv_text_t text = {input_text, input_text_size};
Lei Zhangdf920ec2015-11-11 11:33:26 -0500744 libspirv::AssemblyGrammar grammar;
Lei Zhang9042f402015-11-05 17:45:09 -0500745
Lei Zhang06efdc52015-09-10 14:00:00 -0400746 spv_result_t result =
Lei Zhang9042f402015-11-05 17:45:09 -0500747 spvTextToBinaryInternal(grammar, &text, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400748 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
749
750 return result;
751}
752
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100753void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400754 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100755 if (text->str) {
756 delete[] text->str;
757 }
758 delete text;
759}