blob: d317264b29a74845c5c4c1bbf20c12473243420e [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"
qining1773b952016-09-01 14:27:04 -040041#include "util/parse_number.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010042
Andrew Woloszyn13804e52015-09-22 15:50:33 -040043bool spvIsValidIDCharacter(const char value) {
44 return value == '_' || 0 != ::isalnum(value);
45}
46
47// Returns true if the given string represents a valid ID name.
Dejan Mircevski14c4b102015-09-29 17:07:21 -040048bool spvIsValidID(const char* textValue) {
49 const char* c = textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -040050 for (; *c != '\0'; ++c) {
51 if (!spvIsValidIDCharacter(*c)) {
52 return false;
53 }
54 }
55 // If the string was empty, then the ID also is not valid.
56 return c != textValue;
57}
58
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040059// Text API
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010060
Dejan Mircevski903f9d62015-09-28 17:04:39 -040061spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010062 bool isSigned = false;
David Netoaffa6962015-08-24 15:33:14 -040063 int numPeriods = 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010064 bool isString = false;
65
David Netoaffa6962015-08-24 15:33:14 -040066 const size_t len = strlen(textValue);
David Neto98290a22015-08-24 16:27:02 -040067 if (len == 0) return SPV_FAILED_MATCH;
68
David Netoaffa6962015-08-24 15:33:14 -040069 for (uint64_t index = 0; index < len; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010070 switch (textValue[index]) {
71 case '0':
72 case '1':
73 case '2':
74 case '3':
75 case '4':
76 case '5':
77 case '6':
78 case '7':
79 case '8':
80 case '9':
81 break;
82 case '.':
David Netoaffa6962015-08-24 15:33:14 -040083 numPeriods++;
84 break;
85 case '-':
86 if (index == 0) {
87 isSigned = true;
88 } else {
89 isString = true;
90 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010091 break;
92 default:
93 isString = true;
Lei Zhange78a7c12015-09-10 17:07:21 -040094 index = len; // break out of the loop too.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010095 break;
96 }
97 }
98
David Netoaffa6962015-08-24 15:33:14 -040099 pLiteral->type = spv_literal_type_t(99);
100
Lei Zhange78a7c12015-09-10 17:07:21 -0400101 if (isString || numPeriods > 1 || (isSigned && len == 1)) {
David Neto98290a22015-08-24 16:27:02 -0400102 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
103 return SPV_FAILED_MATCH;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400104 bool escaping = false;
Lei Zhang6483bd72015-10-14 17:02:39 -0400105 for (const char* val = textValue + 1; val != textValue + len - 1; ++val) {
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400106 if ((*val == '\\') && (!escaping)) {
107 escaping = true;
108 } else {
109 // Have to save space for the null-terminator
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500110 if (pLiteral->str.size() >= SPV_LIMIT_LITERAL_STRING_BYTES_MAX)
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400111 return SPV_ERROR_OUT_OF_MEMORY;
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500112 pLiteral->str.push_back(*val);
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400113 escaping = false;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400114 }
115 }
116
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100117 pLiteral->type = SPV_LITERAL_TYPE_STRING;
David Netoaffa6962015-08-24 15:33:14 -0400118 } else if (numPeriods == 1) {
119 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100120 float f = (float)d;
121 if (d == (double)f) {
122 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
123 pLiteral->value.f = f;
124 } else {
125 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
126 pLiteral->value.d = d;
127 }
128 } else if (isSigned) {
129 int64_t i64 = strtoll(textValue, nullptr, 10);
130 int32_t i32 = (int32_t)i64;
131 if (i64 == (int64_t)i32) {
132 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
133 pLiteral->value.i32 = i32;
134 } else {
135 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
136 pLiteral->value.i64 = i64;
137 }
138 } else {
139 uint64_t u64 = strtoull(textValue, nullptr, 10);
140 uint32_t u32 = (uint32_t)u64;
141 if (u64 == (uint64_t)u32) {
142 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
143 pLiteral->value.u32 = u32;
144 } else {
145 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
146 pLiteral->value.u64 = u64;
147 }
148 }
149
150 return SPV_SUCCESS;
151}
152
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400153namespace {
154
155/// Parses an immediate integer from text, guarding against overflow. If
156/// successful, adds the parsed value to pInst, advances the context past it,
157/// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics,
158/// and returns SPV_ERROR_INVALID_TEXT.
159spv_result_t encodeImmediate(libspirv::AssemblyContext* context,
160 const char* text, spv_instruction_t* pInst) {
161 assert(*text == '!');
David Neto78e677b2015-10-05 13:28:46 -0400162 uint32_t parse_result;
qining1773b952016-09-01 14:27:04 -0400163 if (!spvutils::ParseNumber(text + 1, &parse_result)) {
164 return context->diagnostic(SPV_ERROR_INVALID_TEXT)
165 << "Invalid immediate integer: !" << text + 1;
166 }
David Neto78e677b2015-10-05 13:28:46 -0400167 context->binaryEncodeU32(parse_result, pInst);
Ben Vanik01c8d7a2015-11-22 08:32:53 -0800168 context->seekForward(static_cast<uint32_t>(strlen(text)));
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400169 return SPV_SUCCESS;
170}
171
172} // anonymous namespace
173
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400174/// @brief Translate an Opcode operand to binary form
175///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400176/// @param[in] grammar the grammar to use for compilation
177/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400178/// @param[in] type of the operand
179/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400180/// @param[out] pInst return binary Opcode
181/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400182///
183/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400184spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400185 libspirv::AssemblyContext* context,
186 const spv_operand_type_t type,
187 const char* textValue,
188 spv_instruction_t* pInst,
189 spv_operand_pattern_t* pExpectedOperands) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100190 // NOTE: Handle immediate int in the stream
191 if ('!' == textValue[0]) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400192 if (auto error = encodeImmediate(context, textValue, pInst)) {
193 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400194 }
Dejan Mircevski897bff92015-09-29 10:38:18 -0400195 *pExpectedOperands =
196 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100197 return SPV_SUCCESS;
198 }
199
David Neto51013d12015-10-14 11:31:51 -0400200 // Optional literal operands can fail to parse. In that case use
201 // SPV_FAILED_MATCH to avoid emitting a diagostic. Use the following
202 // for those situations.
203 spv_result_t error_code_for_literals =
204 spvOperandIsOptional(type) ? SPV_FAILED_MATCH : SPV_ERROR_INVALID_TEXT;
205
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100206 switch (type) {
David Netoe3f70b92015-08-27 13:50:05 -0400207 case SPV_OPERAND_TYPE_ID:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400208 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto201caf72015-11-04 17:38:17 -0500209 case SPV_OPERAND_TYPE_RESULT_ID:
210 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
211 case SPV_OPERAND_TYPE_SCOPE_ID:
212 case SPV_OPERAND_TYPE_OPTIONAL_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400213 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100214 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100215 } else {
David Netoac508b02015-10-09 15:48:09 -0400216 return context->diagnostic() << "Expected id to start with %.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100217 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400218 if (!spvIsValidID(textValue)) {
David Netoac508b02015-10-09 15:48:09 -0400219 return context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400220 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400221 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400222 if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
David Netob5dc8fc2015-10-06 16:22:00 -0400223 spvInstructionAddWord(pInst, id);
David Neto2ae4a682015-11-09 18:55:42 -0500224
225 // Set the extended instruction type.
226 // The import set id is the 3rd operand of OpExtInst.
227 if (pInst->opcode == SpvOpExtInst && pInst->words.size() == 4) {
228 auto ext_inst_type = context->getExtInstTypeForId(pInst->words[3]);
229 if (ext_inst_type == SPV_EXT_INST_TYPE_NONE) {
230 return context->diagnostic()
231 << "Invalid extended instruction import Id "
232 << pInst->words[2];
233 }
234 pInst->extInstType = ext_inst_type;
235 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100236 } break;
David Neto201caf72015-11-04 17:38:17 -0500237
David Neto445ce442015-10-15 15:22:06 -0400238 case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
239 // The assembler accepts the symbolic name for an extended instruction,
240 // and emits its corresponding number.
241 spv_ext_inst_desc extInst;
242 if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
243 return context->diagnostic() << "Invalid extended instruction name '"
244 << textValue << "'.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100245 }
David Neto445ce442015-10-15 15:22:06 -0400246 spvInstructionAddWord(pInst, extInst->ext_inst);
247
248 // Prepare to parse the operands for the extended instructions.
249 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
David Neto0f166be2015-11-11 01:56:49 -0500250 } break;
David Neto445ce442015-10-15 15:22:06 -0400251
David Neto0f166be2015-11-11 01:56:49 -0500252 case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
253 // The assembler accepts the symbolic name for the opcode, but without
254 // the "Op" prefix. For example, "IAdd" is accepted. The number
255 // of the opcode is emitted.
256 SpvOp opcode;
257 if (grammar.lookupSpecConstantOpcode(textValue, &opcode)) {
258 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
259 << " '" << textValue << "'.";
260 }
261 spv_opcode_desc opcodeEntry = nullptr;
262 if (grammar.lookupOpcode(opcode, &opcodeEntry)) {
263 return context->diagnostic(SPV_ERROR_INTERNAL)
264 << "OpSpecConstant opcode table out of sync";
265 }
266 spvInstructionAddWord(pInst, uint32_t(opcodeEntry->opcode));
267
268 // Prepare to parse the operands for the opcode. Except skip the
269 // type Id and result Id, since they've already been processed.
270 assert(opcodeEntry->hasType);
271 assert(opcodeEntry->hasResult);
272 assert(opcodeEntry->numTypes >= 2);
273 spvPrependOperandTypes(opcodeEntry->operandTypes + 2, pExpectedOperands);
David Neto445ce442015-10-15 15:22:06 -0400274 } break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400275
David Neto201caf72015-11-04 17:38:17 -0500276 case SPV_OPERAND_TYPE_LITERAL_INTEGER:
277 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: {
278 // The current operand is an *unsigned* 32-bit integer.
279 // That's just how the grammar works.
280 libspirv::IdType expected_type = {
281 32, false, libspirv::IdTypeClass::kScalarIntegerType};
David Neto78e677b2015-10-05 13:28:46 -0400282 if (auto error = context->binaryEncodeNumericLiteral(
David Neto51013d12015-10-14 11:31:51 -0400283 textValue, error_code_for_literals, expected_type, pInst)) {
David Neto78e677b2015-10-05 13:28:46 -0400284 return error;
David Neto51013d12015-10-14 11:31:51 -0400285 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100286 } break;
David Neto201caf72015-11-04 17:38:17 -0500287
288 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
289 // This is a context-independent literal number which can be a 32-bit
290 // number of floating point value.
291 if (auto error = context->binaryEncodeNumericLiteral(
292 textValue, error_code_for_literals, libspirv::kUnknownType,
293 pInst)) {
294 return error;
295 }
296 break;
297
298 case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
299 case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: {
300 libspirv::IdType expected_type = libspirv::kUnknownType;
301 // The encoding for OpConstant, OpSpecConstant and OpSwitch all
302 // depend on either their own result-id or the result-id of
303 // one of their parameters.
304 if (SpvOpConstant == pInst->opcode ||
305 SpvOpSpecConstant == pInst->opcode) {
306 // The type of the literal is determined by the type Id of the
307 // instruction.
308 expected_type =
309 context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
310 if (!libspirv::isScalarFloating(expected_type) &&
311 !libspirv::isScalarIntegral(expected_type)) {
312 spv_opcode_desc d;
313 const char* opcode_name = "opcode";
314 if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
315 opcode_name = d->name;
316 }
317 return context->diagnostic()
318 << "Type for " << opcode_name
319 << " must be a scalar floating point or integer type";
320 }
321 } else if (pInst->opcode == SpvOpSwitch) {
322 // The type of the literal is the same as the type of the selector.
323 expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
324 if (!libspirv::isScalarIntegral(expected_type)) {
325 return context->diagnostic()
326 << "The selector operand for OpSwitch must be the result"
327 " of an instruction that generates an integer scalar";
328 }
329 }
330 if (auto error = context->binaryEncodeNumericLiteral(
331 textValue, error_code_for_literals, expected_type, pInst)) {
332 return error;
333 }
334 } break;
335
David Neto78c3b432015-08-27 13:03:52 -0400336 case SPV_OPERAND_TYPE_LITERAL_STRING:
337 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400338 spv_literal_t literal = {};
339 spv_result_t error = spvTextToLiteral(textValue, &literal);
340 if (error != SPV_SUCCESS) {
341 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
David Neto51013d12015-10-14 11:31:51 -0400342 return context->diagnostic(error_code_for_literals)
343 << "Invalid literal string '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400344 }
Lei Zhang6d415812015-09-15 13:36:21 -0400345 if (literal.type != SPV_LITERAL_TYPE_STRING) {
David Neto51013d12015-10-14 11:31:51 -0400346 return context->diagnostic()
David Netoac508b02015-10-09 15:48:09 -0400347 << "Expected literal string, found literal number '" << textValue
348 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400349 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100350
351 // NOTE: Special case for extended instruction library import
Lei Zhangb36e7042015-10-28 13:40:52 -0400352 if (SpvOpExtInstImport == pInst->opcode) {
David Neto2ae4a682015-11-09 18:55:42 -0500353 const spv_ext_inst_type_t ext_inst_type =
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500354 spvExtInstImportTypeGet(literal.str.c_str());
David Neto2ae4a682015-11-09 18:55:42 -0500355 if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
356 return context->diagnostic()
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500357 << "Invalid extended instruction import '" << literal.str
David Neto2ae4a682015-11-09 18:55:42 -0500358 << "'";
359 }
David Neto677e0c72016-01-05 14:56:02 -0500360 if ((error = context->recordIdAsExtInstImport(pInst->words[1],
361 ext_inst_type)))
David Neto2ae4a682015-11-09 18:55:42 -0500362 return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100363 }
364
Lei Zhang16f3ddf2015-11-11 15:37:01 -0500365 if (context->binaryEncodeString(literal.str.c_str(), pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400366 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100367 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400368 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
369 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
370 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Neto2889a0c2016-02-15 13:50:00 -0500371 case SPV_OPERAND_TYPE_IMAGE:
David Netoee1b3bb2015-09-18 11:19:18 -0400372 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400373 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400374 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
375 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400376 if (grammar.parseMaskOperand(type, textValue, &value)) {
David Netoac508b02015-10-09 15:48:09 -0400377 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
David Netod9ad0502015-11-24 18:37:24 -0500378 << " operand '" << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400379 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400380 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400381 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400382 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400383 } break;
384 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
385 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400386 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
387 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400388 if (error == SPV_FAILED_MATCH) {
389 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400390 error = spvTextEncodeOperand(grammar, context,
391 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
392 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400393 }
394 if (error == SPV_FAILED_MATCH) {
395 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400396 error =
397 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
398 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400399 }
400 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400401 return context->diagnostic(error)
402 << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400403 }
404 if (pExpectedOperands->empty()) {
405 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
406 }
David Neto36b0c0f2015-09-16 18:32:54 -0400407 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100408 default: {
409 // NOTE: All non literal operands are handled here using the operand
410 // table.
411 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400412 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
David Netoac508b02015-10-09 15:48:09 -0400413 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
414 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400415 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400416 if (context->binaryEncodeU32(entry->value, pInst)) {
David Netoac508b02015-10-09 15:48:09 -0400417 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
418 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400419 }
David Neto78c3b432015-08-27 13:03:52 -0400420
421 // Prepare to parse the operands for this logical operand.
422 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100423 } break;
424 }
425 return SPV_SUCCESS;
426}
427
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400428namespace {
429
430/// Encodes an instruction started by !<integer> at the given position in text.
431///
432/// Puts the encoded words into *pInst. If successful, moves position past the
433/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
434/// leaves position pointing to the error in text.
435spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400436 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400437 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400438 std::string firstWord;
439 spv_position_t nextPosition = {};
Lei Zhang6032b982016-03-15 16:52:40 -0400440 auto error = context->getWord(&firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400441 if (error) return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400442
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400443 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
444 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400445 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400446 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400447 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400448 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400449
450 // Otherwise, there must be an operand that's either a literal, an ID, or
451 // an immediate.
452 std::string operandValue;
Lei Zhang6032b982016-03-15 16:52:40 -0400453 if ((error = context->getWord(&operandValue, &nextPosition)))
David Netoac508b02015-10-09 15:48:09 -0400454 return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400455
David Netoac508b02015-10-09 15:48:09 -0400456 if (operandValue == "=")
457 return context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400458
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400459 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
460 // expanded.
461 spv_operand_pattern_t dummyExpectedOperands;
462 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400463 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
464 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400465 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400466 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400467 }
468 return SPV_SUCCESS;
469}
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400470
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400471/// @brief Translate single Opcode and operands to binary form
472///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400473/// @param[in] grammar the grammar to use for compilation
474/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400475/// @param[in] text stream to translate
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400476/// @param[out] pInst returned binary Opcode
477/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400478///
479/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400480spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400481 libspirv::AssemblyContext* context,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400482 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400483 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400484 if ('!' == context->peek()) {
485 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400486 }
487
Lei Zhang06efdc52015-09-10 14:00:00 -0400488 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100489 spv_position_t nextPosition = {};
Lei Zhang6032b982016-03-15 16:52:40 -0400490 spv_result_t error = context->getWord(&firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400491 if (error) return context->diagnostic() << "Internal Error";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100492
Lei Zhang06efdc52015-09-10 14:00:00 -0400493 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400494 std::string result_id;
495 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400496 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400497 opcodeName = firstWord;
498 } else {
Lei Zhang06efdc52015-09-10 14:00:00 -0400499 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400500 if ('%' != result_id.front()) {
David Netoac508b02015-10-09 15:48:09 -0400501 return context->diagnostic()
502 << "Expected <opcode> or <result-id> at the beginning "
503 "of an instruction, found '"
504 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400505 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400506 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400507
508 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400509 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400510 if (context->advance())
511 return context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400512 std::string equal_sign;
Lei Zhang6032b982016-03-15 16:52:40 -0400513 error = context->getWord(&equal_sign, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400514 if ("=" != equal_sign)
515 return context->diagnostic() << "'=' expected after result id.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400516
517 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400518 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400519 if (context->advance())
520 return context->diagnostic() << "Expected opcode, found end of stream.";
Lei Zhang6032b982016-03-15 16:52:40 -0400521 error = context->getWord(&opcodeName, &nextPosition);
Lei Zhang6483bd72015-10-14 17:02:39 -0400522 if (error) return context->diagnostic(error) << "Internal Error";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400523 if (!context->startsWithOp()) {
David Netoac508b02015-10-09 15:48:09 -0400524 return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName
525 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400526 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400527 }
528
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100529 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400530 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100531
532 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400533 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400534 if (error) {
Lei Zhang6032b982016-03-15 16:52:40 -0400535 return context->diagnostic(error) << "Invalid Opcode name '" << opcodeName
536 << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400537 }
Lei Zhang9042f402015-11-05 17:45:09 -0500538 if (opcodeEntry->hasResult && result_id.empty()) {
539 return context->diagnostic()
540 << "Expected <result-id> at the beginning of an instruction, found '"
541 << firstWord << "'.";
Lei Zhang06efdc52015-09-10 14:00:00 -0400542 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100543 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400544 context->setPosition(nextPosition);
David Netob5dc8fc2015-10-06 16:22:00 -0400545 // Reserve the first word for the instruction.
546 spvInstructionAddWord(pInst, 0);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100547
David Neto78c3b432015-08-27 13:03:52 -0400548 // Maintains the ordered list of expected operand types.
549 // For many instructions we only need the {numTypes, operandTypes}
550 // entries in opcodeEntry. However, sometimes we need to modify
551 // the list as we parse the operands. This occurs when an operand
552 // has its own logical operands (such as the LocalSize operand for
553 // ExecutionMode), or for extended instructions that may have their
554 // own operands depending on the selected extended instruction.
555 spv_operand_pattern_t expectedOperands(
556 opcodeEntry->operandTypes,
557 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400558
David Neto78c3b432015-08-27 13:03:52 -0400559 while (!expectedOperands.empty()) {
560 const spv_operand_type_t type = expectedOperands.front();
561 expectedOperands.pop_front();
562
563 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400564 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400565
566 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
567 // Handle the <result-id> for value generating instructions.
568 // We've already consumed it from the text stream. Here
569 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400570 spv_position_t temp_pos = context->position();
571 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
572 result_id.c_str(), pInst, nullptr);
573 result_id_position = context->position();
574 // Because we are injecting we have to reset the position afterwards.
575 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400576 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100577 } else {
David Neto78c3b432015-08-27 13:03:52 -0400578 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400579 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400580 if (error == SPV_END_OF_STREAM) {
581 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400582 // This would have been the last potential operand for the
583 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400584 // and we didn't find one. We're finished parsing this instruction.
585 break;
586 } else {
David Netoac508b02015-10-09 15:48:09 -0400587 return context->diagnostic()
588 << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400589 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100590 }
David Neto78c3b432015-08-27 13:03:52 -0400591 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
592
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400593 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400594 if (spvOperandIsOptional(type)) {
595 break;
596 } else {
David Netoac508b02015-10-09 15:48:09 -0400597 return context->diagnostic()
598 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400599 }
600 }
601
602 std::string operandValue;
Lei Zhang6032b982016-03-15 16:52:40 -0400603 error = context->getWord(&operandValue, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400604 if (error) return context->diagnostic(error) << "Internal Error";
David Neto78c3b432015-08-27 13:03:52 -0400605
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400606 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
607 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400608
609 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
610 return SPV_SUCCESS;
611
Lei Zhang40056702015-09-11 14:31:27 -0400612 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400613
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400614 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100615 }
616 }
617
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400618 if (spvOpcodeGeneratesType(pInst->opcode)) {
619 if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
620 return SPV_ERROR_INVALID_TEXT;
621 }
622 } else if (opcodeEntry->hasType) {
623 // SPIR-V dictates that if an instruction has both a return value and a
624 // type ID then the type id is first, and the return value is second.
625 assert(opcodeEntry->hasResult &&
626 "Unknown opcode: has a type but no result.");
627 context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
628 }
629
David Netob5dc8fc2015-10-06 16:22:00 -0400630 if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Netoac508b02015-10-09 15:48:09 -0400631 return context->diagnostic()
632 << "Instruction too long: " << pInst->words.size()
633 << " words, but the limit is "
634 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
David Netob5dc8fc2015-10-06 16:22:00 -0400635 }
636
David Netod9ad0502015-11-24 18:37:24 -0500637 pInst->words[0] =
638 spvOpcodeMake(uint16_t(pInst->words.size()), opcodeEntry->opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100639
640 return SPV_SUCCESS;
641}
642
David Netod9ad0502015-11-24 18:37:24 -0500643enum { kAssemblerVersion = 0 };
David Neto14b93e42015-11-12 18:33:47 -0500644
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400645// Populates a binary stream's |header|. The target environment is specified via
646// |env| and Id bound is via |bound|.
647spv_result_t SetHeader(spv_target_env env, const uint32_t bound,
648 uint32_t* header) {
649 if (!header) return SPV_ERROR_INVALID_BINARY;
David Neto0b981682015-10-27 15:51:34 -0400650
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400651 header[SPV_INDEX_MAGIC_NUMBER] = SpvMagicNumber;
652 header[SPV_INDEX_VERSION_NUMBER] = spvVersionForTargetEnv(env);
653 header[SPV_INDEX_GENERATOR_NUMBER] =
David Neto14b93e42015-11-12 18:33:47 -0500654 SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS_ASSEMBLER, kAssemblerVersion);
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400655 header[SPV_INDEX_BOUND] = bound;
656 header[SPV_INDEX_SCHEMA] = 0; // NOTE: Reserved
David Neto0b981682015-10-27 15:51:34 -0400657
658 return SPV_SUCCESS;
659}
660
David Netoc9786432015-09-01 18:05:14 -0400661// Translates a given assembly language module into binary form.
662// If a diagnostic is generated, it is not yet marked as being
663// for a text-based input.
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400664spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
Lei Zhang9042f402015-11-05 17:45:09 -0500665 const spv_text text, spv_binary* pBinary,
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400666 spv_diagnostic* pDiagnostic) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400667 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
668 libspirv::AssemblyContext context(text, pDiagnostic);
David Neto201caf72015-11-04 17:38:17 -0500669 if (!text->str) return context.diagnostic() << "Missing assembly text.";
David Netoac508b02015-10-09 15:48:09 -0400670
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400671 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400672 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400673 }
Lei Zhang40056702015-09-11 14:31:27 -0400674 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100675
676 // NOTE: Ensure diagnostic is zero initialised
677 *pDiagnostic = {};
678
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100679 std::vector<spv_instruction_t> instructions;
680
David Netoea633a62015-11-02 11:40:59 -0500681 // Skip past whitespace and comments.
682 context.advance();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100683
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400684 while (context.hasText()) {
David Netob5dc8fc2015-10-06 16:22:00 -0400685 instructions.push_back({});
686 spv_instruction_t& inst = instructions.back();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100687
Lei Zhang9042f402015-11-05 17:45:09 -0500688 if (spvTextEncodeOpcode(grammar, &context, &inst)) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400689 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400690 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400691
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400692 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100693 }
694
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100695 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400696 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400697 totalSize += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100698 }
699
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400700 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400701 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100702 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400703 for (auto& inst : instructions) {
Lei Zhang6483bd72015-10-14 17:02:39 -0400704 memcpy(data + currentIndex, inst.words.data(),
705 sizeof(uint32_t) * inst.words.size());
David Netob5dc8fc2015-10-06 16:22:00 -0400706 currentIndex += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100707 }
708
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400709 if (auto error = SetHeader(grammar.target_env(), context.getBound(), data))
710 return error;
David Netoe4945de2015-10-28 13:50:32 -0400711
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100712 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400713 if (!binary) {
714 delete[] data;
715 return SPV_ERROR_OUT_OF_MEMORY;
716 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100717 binary->code = data;
718 binary->wordCount = totalSize;
719
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100720 *pBinary = binary;
721
722 return SPV_SUCCESS;
723}
724
Lei Zhange78a7c12015-09-10 17:07:21 -0400725} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400726
Lei Zhang972788b2015-11-12 13:48:30 -0500727spv_result_t spvTextToBinary(const spv_const_context context,
728 const char* input_text,
Lei Zhangdf920ec2015-11-11 11:33:26 -0500729 const size_t input_text_size, spv_binary* pBinary,
730 spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400731 spv_text_t text = {input_text, input_text_size};
Lei Zhang972788b2015-11-12 13:48:30 -0500732 libspirv::AssemblyGrammar grammar(context);
Lei Zhang9042f402015-11-05 17:45:09 -0500733
Lei Zhang06efdc52015-09-10 14:00:00 -0400734 spv_result_t result =
Lei Zhang9042f402015-11-05 17:45:09 -0500735 spvTextToBinaryInternal(grammar, &text, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400736 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
737
738 return result;
739}
740
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100741void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400742 if (!text) return;
Eric Engestromeb6ae972016-02-18 23:41:16 +0000743 delete[] text->str;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100744 delete text;
745}