blob: bbd16e0121c5d49f2dd74769360e067ba50feb2a [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>
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040036#include <string>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040037#include <sstream>
David Neto36b0c0f2015-09-16 18:32:54 -040038#include <unordered_map>
39#include <vector>
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040040
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010041#include "binary.h"
David Neto36b0c0f2015-09-16 18:32:54 -040042#include "bitwisecast.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010043#include "diagnostic.h"
44#include "ext_inst.h"
David Netob5dc8fc2015-10-06 16:22:00 -040045#include "instruction.h"
David Neto36b0c0f2015-09-16 18:32:54 -040046#include <libspirv/libspirv.h>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010047#include "opcode.h"
48#include "operand.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040049#include "text_handler.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010050
Andrew Woloszyn13804e52015-09-22 15:50:33 -040051bool spvIsValidIDCharacter(const char value) {
52 return value == '_' || 0 != ::isalnum(value);
53}
54
55// Returns true if the given string represents a valid ID name.
Dejan Mircevski14c4b102015-09-29 17:07:21 -040056bool spvIsValidID(const char* textValue) {
57 const char* c = textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -040058 for (; *c != '\0'; ++c) {
59 if (!spvIsValidIDCharacter(*c)) {
60 return false;
61 }
62 }
63 // If the string was empty, then the ID also is not valid.
64 return c != textValue;
65}
66
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040067// Text API
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010068
Dejan Mircevski903f9d62015-09-28 17:04:39 -040069spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010070 bool isSigned = false;
David Netoaffa6962015-08-24 15:33:14 -040071 int numPeriods = 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010072 bool isString = false;
73
David Netoaffa6962015-08-24 15:33:14 -040074 const size_t len = strlen(textValue);
David Neto98290a22015-08-24 16:27:02 -040075 if (len == 0) return SPV_FAILED_MATCH;
76
David Netoaffa6962015-08-24 15:33:14 -040077 for (uint64_t index = 0; index < len; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010078 switch (textValue[index]) {
79 case '0':
80 case '1':
81 case '2':
82 case '3':
83 case '4':
84 case '5':
85 case '6':
86 case '7':
87 case '8':
88 case '9':
89 break;
90 case '.':
David Netoaffa6962015-08-24 15:33:14 -040091 numPeriods++;
92 break;
93 case '-':
94 if (index == 0) {
95 isSigned = true;
96 } else {
97 isString = true;
98 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010099 break;
100 default:
101 isString = true;
Lei Zhange78a7c12015-09-10 17:07:21 -0400102 index = len; // break out of the loop too.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100103 break;
104 }
105 }
106
David Netoaffa6962015-08-24 15:33:14 -0400107 pLiteral->type = spv_literal_type_t(99);
108
Lei Zhange78a7c12015-09-10 17:07:21 -0400109 if (isString || numPeriods > 1 || (isSigned && len == 1)) {
David Netoaffa6962015-08-24 15:33:14 -0400110 // TODO(dneto): Allow escaping.
David Neto98290a22015-08-24 16:27:02 -0400111 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
112 return SPV_FAILED_MATCH;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400113 bool escaping = false;
114 size_t write_index = 0;
115 for(const char* val = textValue + 1; val != textValue + len - 1; ++val) {
116 if ((*val == '\\') && (!escaping)) {
117 escaping = true;
118 } else {
119 // Have to save space for the null-terminator
120 if (write_index >= sizeof(pLiteral->value.str) - 1)
121 return SPV_ERROR_OUT_OF_MEMORY;
122 pLiteral->value.str[write_index] = *val;
123 escaping = false;
124 ++write_index;
125 }
126 }
127
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100128 pLiteral->type = SPV_LITERAL_TYPE_STRING;
Andrew Woloszyn3e69cd12015-10-14 12:44:19 -0400129 pLiteral->value.str[write_index] = '\0';
David Netoaffa6962015-08-24 15:33:14 -0400130 } else if (numPeriods == 1) {
131 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100132 float f = (float)d;
133 if (d == (double)f) {
134 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
135 pLiteral->value.f = f;
136 } else {
137 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
138 pLiteral->value.d = d;
139 }
140 } else if (isSigned) {
141 int64_t i64 = strtoll(textValue, nullptr, 10);
142 int32_t i32 = (int32_t)i64;
143 if (i64 == (int64_t)i32) {
144 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
145 pLiteral->value.i32 = i32;
146 } else {
147 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
148 pLiteral->value.i64 = i64;
149 }
150 } else {
151 uint64_t u64 = strtoull(textValue, nullptr, 10);
152 uint32_t u32 = (uint32_t)u64;
153 if (u64 == (uint64_t)u32) {
154 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
155 pLiteral->value.u32 = u32;
156 } else {
157 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
158 pLiteral->value.u64 = u64;
159 }
160 }
161
162 return SPV_SUCCESS;
163}
164
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400165namespace {
166
167/// Parses an immediate integer from text, guarding against overflow. If
168/// successful, adds the parsed value to pInst, advances the context past it,
169/// and returns SPV_SUCCESS. Otherwise, leaves pInst alone, emits diagnostics,
170/// and returns SPV_ERROR_INVALID_TEXT.
171spv_result_t encodeImmediate(libspirv::AssemblyContext* context,
172 const char* text, spv_instruction_t* pInst) {
173 assert(*text == '!');
David Neto78e677b2015-10-05 13:28:46 -0400174 uint32_t parse_result;
175 if (auto error =
176 context->parseNumber(text + 1, false, &parse_result,
177 "Invalid immediate integer: !"))
178 return error;
179 context->binaryEncodeU32(parse_result, pInst);
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400180 context->seekForward(strlen(text));
181 return SPV_SUCCESS;
182}
183
184} // anonymous namespace
185
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400186/// @brief Translate an Opcode operand to binary form
187///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400188/// @param[in] grammar the grammar to use for compilation
189/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400190/// @param[in] type of the operand
191/// @param[in] textValue word of text to be parsed
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400192/// @param[out] pInst return binary Opcode
193/// @param[in,out] pExpectedOperands the operand types expected
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400194///
195/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400196spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400197 libspirv::AssemblyContext* context,
198 const spv_operand_type_t type,
199 const char* textValue,
200 spv_instruction_t* pInst,
201 spv_operand_pattern_t* pExpectedOperands) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100202 // NOTE: Handle immediate int in the stream
203 if ('!' == textValue[0]) {
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400204 if (auto error = encodeImmediate(context, textValue, pInst)) {
205 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400206 }
Dejan Mircevski897bff92015-09-29 10:38:18 -0400207 *pExpectedOperands =
208 spvAlternatePatternFollowingImmediate(*pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100209 return SPV_SUCCESS;
210 }
211
212 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400213 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Netoe3f70b92015-08-27 13:50:05 -0400214 case SPV_OPERAND_TYPE_ID:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400215 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400216 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
217 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netob14a7272015-09-25 13:56:09 -0400218 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
219 case SPV_OPERAND_TYPE_RESULT_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400220 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100221 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100222 } else {
David Netoac508b02015-10-09 15:48:09 -0400223 return context->diagnostic() << "Expected id to start with %.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100224 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400225 if (!spvIsValidID(textValue)) {
David Netoac508b02015-10-09 15:48:09 -0400226 return context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400227 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400228 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400229 if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
David Netob5dc8fc2015-10-06 16:22:00 -0400230 spvInstructionAddWord(pInst, id);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100231 } break;
232 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
233 // NOTE: Special case for extension instruction lookup
234 if (OpExtInst == pInst->opcode) {
235 spv_ext_inst_desc extInst;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400236 if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
David Netoac508b02015-10-09 15:48:09 -0400237 return context->diagnostic() << "Invalid extended instruction name '"
238 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400239 }
David Netob5dc8fc2015-10-06 16:22:00 -0400240 spvInstructionAddWord(pInst, extInst->ext_inst);
David Neto78c3b432015-08-27 13:03:52 -0400241
242 // Prepare to parse the operands for the extended instructions.
243 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
244
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100245 return SPV_SUCCESS;
246 }
Lei Zhang6d415812015-09-15 13:36:21 -0400247 } // Fall through for the general case.
Lei Zhangb41d1502015-09-14 15:22:23 -0400248 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
David Neto561dc4e2015-09-25 14:23:29 -0400249 case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE:
250 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: {
David Neto78e677b2015-10-05 13:28:46 -0400251 libspirv::IdType expected_type = libspirv::kUnknownType;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400252 // The encoding for OpConstant, OpSpecConstant and OpSwitch all
253 // depend on either their own result-id or the result-id of
254 // one of their parameters.
255 if (OpConstant == pInst->opcode || OpSpecConstant == pInst->opcode) {
256 // Special cases for encoding possibly non-32-bit literals here.
David Neto78e677b2015-10-05 13:28:46 -0400257 expected_type =
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400258 context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
David Neto78e677b2015-10-05 13:28:46 -0400259 if (!libspirv::isScalarFloating(expected_type) &&
260 !libspirv::isScalarIntegral(expected_type)) {
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400261 spv_opcode_desc d;
262 const char* opcode_name = "opcode";
263 if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
264 opcode_name = d->name;
265 }
David Netoac508b02015-10-09 15:48:09 -0400266 return context->diagnostic()
267 << "Type for " << opcode_name
268 << " must be a scalar floating point or integer type";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400269 }
270 } else if (pInst->opcode == OpSwitch) {
David Neto78e677b2015-10-05 13:28:46 -0400271 // We need to know the type of the selector.
272 expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
273 if (!libspirv::isScalarIntegral(expected_type)) {
274 context->diagnostic()
275 << "The selector operand for OpSwitch must be the result"
276 " of an instruction that generates an integer scalar";
277 return SPV_ERROR_INVALID_TEXT;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400278 }
279 }
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400280
David Neto78e677b2015-10-05 13:28:46 -0400281 if (auto error = context->binaryEncodeNumericLiteral(
282 textValue, spvOperandIsOptional(type), expected_type, pInst)) {
283 return error;
284 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100285 } break;
David Neto78c3b432015-08-27 13:03:52 -0400286 case SPV_OPERAND_TYPE_LITERAL_STRING:
287 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400288 spv_literal_t literal = {};
289 spv_result_t error = spvTextToLiteral(textValue, &literal);
290 if (error != SPV_SUCCESS) {
291 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
Lei Zhang40056702015-09-11 14:31:27 -0400292 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
David Netoac508b02015-10-09 15:48:09 -0400293 return context->diagnostic() << "Invalid literal string '" << textValue
294 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400295 }
Lei Zhang6d415812015-09-15 13:36:21 -0400296 if (literal.type != SPV_LITERAL_TYPE_STRING) {
David Netoac508b02015-10-09 15:48:09 -0400297 return context->diagnostic(SPV_FAILED_MATCH)
298 << "Expected literal string, found literal number '" << textValue
299 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400300 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100301
302 // NOTE: Special case for extended instruction library import
303 if (OpExtInstImport == pInst->opcode) {
Lei Zhang6d415812015-09-15 13:36:21 -0400304 pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100305 }
306
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400307 if (context->binaryEncodeString(literal.value.str, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400308 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100309 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400310 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
311 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
312 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400313 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400314 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400315 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
316 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400317 if (grammar.parseMaskOperand(type, textValue, &value)) {
David Netoac508b02015-10-09 15:48:09 -0400318 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
319 << " '" << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400320 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400321 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400322 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400323 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400324 } break;
325 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
326 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400327 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
328 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400329 if (error == SPV_FAILED_MATCH) {
330 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400331 error = spvTextEncodeOperand(grammar, context,
332 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
333 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400334 }
335 if (error == SPV_FAILED_MATCH) {
336 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400337 error =
338 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
339 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400340 }
341 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400342 return context->diagnostic(error)
343 << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400344 }
345 if (pExpectedOperands->empty()) {
346 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
347 }
David Neto36b0c0f2015-09-16 18:32:54 -0400348 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100349 default: {
350 // NOTE: All non literal operands are handled here using the operand
351 // table.
352 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400353 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
David Netoac508b02015-10-09 15:48:09 -0400354 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
355 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400356 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400357 if (context->binaryEncodeU32(entry->value, pInst)) {
David Netoac508b02015-10-09 15:48:09 -0400358 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
359 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400360 }
David Neto78c3b432015-08-27 13:03:52 -0400361
362 // Prepare to parse the operands for this logical operand.
363 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100364 } break;
365 }
366 return SPV_SUCCESS;
367}
368
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400369namespace {
370
371/// Encodes an instruction started by !<integer> at the given position in text.
372///
373/// Puts the encoded words into *pInst. If successful, moves position past the
374/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
375/// leaves position pointing to the error in text.
376spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400377 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400378 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400379 std::string firstWord;
380 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400381 auto error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400382 if (error) return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400383
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400384 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
385 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400386 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400387 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400388 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400389 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400390
391 // Otherwise, there must be an operand that's either a literal, an ID, or
392 // an immediate.
393 std::string operandValue;
David Netoac508b02015-10-09 15:48:09 -0400394 if ((error = context->getWord(operandValue, &nextPosition)))
395 return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400396
David Netoac508b02015-10-09 15:48:09 -0400397 if (operandValue == "=")
398 return context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400399
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400400 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
401 // expanded.
402 spv_operand_pattern_t dummyExpectedOperands;
403 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400404 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
405 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400406 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400407 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400408 }
409 return SPV_SUCCESS;
410}
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400411} // anonymous namespace
412
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400413/// @brief Translate single Opcode and operands to binary form
414///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400415/// @param[in] grammar the grammar to use for compilation
416/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400417/// @param[in] text stream to translate
418/// @param[in] format the assembly syntax format of text
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400419/// @param[out] pInst returned binary Opcode
420/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400421///
422/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400423spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400424 libspirv::AssemblyContext* context,
425 spv_assembly_syntax_format_t format,
426 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400427 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400428 if ('!' == context->peek()) {
429 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400430 }
431
Lei Zhangdfc50082015-08-21 11:50:55 -0400432 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400433 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
434 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400435
Lei Zhang06efdc52015-09-10 14:00:00 -0400436 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100437 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400438 spv_result_t error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400439 if (error) return context->diagnostic() << "Internal Error";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100440
Lei Zhang06efdc52015-09-10 14:00:00 -0400441 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400442 std::string result_id;
443 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400444 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400445 opcodeName = firstWord;
446 } else {
447 // If the first word of this instruction is not an opcode, we must be
448 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400449 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
David Netoac508b02015-10-09 15:48:09 -0400450 return context->diagnostic()
451 << "Expected <opcode> at the beginning of an instruction, found '"
452 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400453 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400454
455 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400456 if ('%' != result_id.front()) {
David Netoac508b02015-10-09 15:48:09 -0400457 return context->diagnostic()
458 << "Expected <opcode> or <result-id> at the beginning "
459 "of an instruction, found '"
460 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400461 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400462 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400463
464 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400465 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400466 if (context->advance())
467 return context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400468 std::string equal_sign;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400469 error = context->getWord(equal_sign, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400470 if ("=" != equal_sign)
471 return context->diagnostic() << "'=' expected after result id.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400472
473 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400474 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400475 if (context->advance())
476 return context->diagnostic() << "Expected opcode, found end of stream.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400477 error = context->getWord(opcodeName, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400478 if (error)
479 return context->diagnostic(error) << "Internal Error";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400480 if (!context->startsWithOp()) {
David Netoac508b02015-10-09 15:48:09 -0400481 return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName
482 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400483 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400484 }
485
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100486 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400487 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100488
489 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400490 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400491 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400492 return context->diagnostic(error) << "Invalid Opcode name '"
493 << context->getWord() << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400494 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400495 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
496 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400497 if (opcodeEntry->hasResult && result_id.empty()) {
David Netoac508b02015-10-09 15:48:09 -0400498 return context->diagnostic()
499 << "Expected <result-id> at the beginning of an "
500 "instruction, found '"
501 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400502 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400503 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100504 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400505 context->setPosition(nextPosition);
David Netob5dc8fc2015-10-06 16:22:00 -0400506 // Reserve the first word for the instruction.
507 spvInstructionAddWord(pInst, 0);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100508
David Neto78c3b432015-08-27 13:03:52 -0400509 // Maintains the ordered list of expected operand types.
510 // For many instructions we only need the {numTypes, operandTypes}
511 // entries in opcodeEntry. However, sometimes we need to modify
512 // the list as we parse the operands. This occurs when an operand
513 // has its own logical operands (such as the LocalSize operand for
514 // ExecutionMode), or for extended instructions that may have their
515 // own operands depending on the selected extended instruction.
516 spv_operand_pattern_t expectedOperands(
517 opcodeEntry->operandTypes,
518 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400519
David Neto78c3b432015-08-27 13:03:52 -0400520 while (!expectedOperands.empty()) {
521 const spv_operand_type_t type = expectedOperands.front();
522 expectedOperands.pop_front();
523
524 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400525 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400526
527 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
528 // Handle the <result-id> for value generating instructions.
529 // We've already consumed it from the text stream. Here
530 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400531 spv_position_t temp_pos = context->position();
532 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
533 result_id.c_str(), pInst, nullptr);
534 result_id_position = context->position();
535 // Because we are injecting we have to reset the position afterwards.
536 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400537 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100538 } else {
David Neto78c3b432015-08-27 13:03:52 -0400539 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400540 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400541 if (error == SPV_END_OF_STREAM) {
542 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400543 // This would have been the last potential operand for the
544 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400545 // and we didn't find one. We're finished parsing this instruction.
546 break;
547 } else {
David Netoac508b02015-10-09 15:48:09 -0400548 return context->diagnostic()
549 << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400550 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100551 }
David Neto78c3b432015-08-27 13:03:52 -0400552 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
553
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400554 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400555 if (spvOperandIsOptional(type)) {
556 break;
557 } else {
David Netoac508b02015-10-09 15:48:09 -0400558 return context->diagnostic()
559 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400560 }
561 }
562
563 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400564 error = context->getWord(operandValue, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400565 if (error) return context->diagnostic(error) << "Internal Error";
David Neto78c3b432015-08-27 13:03:52 -0400566
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400567 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
568 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400569
570 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
571 return SPV_SUCCESS;
572
Lei Zhang40056702015-09-11 14:31:27 -0400573 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400574
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400575 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100576 }
577 }
578
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400579 if (spvOpcodeGeneratesType(pInst->opcode)) {
580 if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
581 return SPV_ERROR_INVALID_TEXT;
582 }
583 } else if (opcodeEntry->hasType) {
584 // SPIR-V dictates that if an instruction has both a return value and a
585 // type ID then the type id is first, and the return value is second.
586 assert(opcodeEntry->hasResult &&
587 "Unknown opcode: has a type but no result.");
588 context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
589 }
590
David Netob5dc8fc2015-10-06 16:22:00 -0400591 if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Netoac508b02015-10-09 15:48:09 -0400592 return context->diagnostic()
593 << "Instruction too long: " << pInst->words.size()
594 << " words, but the limit is "
595 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
David Netob5dc8fc2015-10-06 16:22:00 -0400596 }
597
598 pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100599
600 return SPV_SUCCESS;
601}
602
David Netoc9786432015-09-01 18:05:14 -0400603namespace {
604
605// Translates a given assembly language module into binary form.
606// If a diagnostic is generated, it is not yet marked as being
607// for a text-based input.
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400608spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
609 const spv_text text,
610 spv_assembly_syntax_format_t format,
611 spv_binary* pBinary,
612 spv_diagnostic* pDiagnostic) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400613 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
614 libspirv::AssemblyContext context(text, pDiagnostic);
David Netoac508b02015-10-09 15:48:09 -0400615 if (!text->str || !text->length)
616 return context.diagnostic() << "Text stream is empty.";
617
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400618 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400619 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400620 }
Lei Zhang40056702015-09-11 14:31:27 -0400621 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100622
623 // NOTE: Ensure diagnostic is zero initialised
624 *pDiagnostic = {};
625
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100626 std::vector<spv_instruction_t> instructions;
627
David Netoac508b02015-10-09 15:48:09 -0400628 if (context.advance()) return context.diagnostic() << "Text stream is empty.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100629
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400630 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
631 while (context.hasText()) {
David Netob5dc8fc2015-10-06 16:22:00 -0400632 instructions.push_back({});
633 spv_instruction_t& inst = instructions.back();
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400634 inst.extInstType = extInstType;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100635
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400636 if (spvTextEncodeOpcode(grammar, &context, format, &inst)) {
637 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400638 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400639 extInstType = inst.extInstType;
640
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400641 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100642 }
643
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100644 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400645 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400646 totalSize += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100647 }
648
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400649 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400650 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100651 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400652 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400653 memcpy(data + currentIndex, inst.words.data(), sizeof(uint32_t) * inst.words.size());
654 currentIndex += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100655 }
656
657 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400658 if (!binary) {
659 delete[] data;
660 return SPV_ERROR_OUT_OF_MEMORY;
661 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100662 binary->code = data;
663 binary->wordCount = totalSize;
664
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400665 spv_result_t error = spvBinaryHeaderSet(binary, context.getBound());
Lei Zhang40056702015-09-11 14:31:27 -0400666 if (error) {
667 spvBinaryDestroy(binary);
668 return error;
669 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100670
671 *pBinary = binary;
672
673 return SPV_SUCCESS;
674}
675
Lei Zhange78a7c12015-09-10 17:07:21 -0400676} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400677
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400678spv_result_t spvTextToBinary(const char* input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400679 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400680 const spv_opcode_table opcodeTable,
681 const spv_operand_table operandTable,
682 const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400683 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400684 return spvTextWithFormatToBinary(
685 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
686 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
687}
688
689spv_result_t spvTextWithFormatToBinary(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400690 const char* input_text, const uint64_t input_text_size,
Lei Zhang06efdc52015-09-10 14:00:00 -0400691 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
692 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400693 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400694 spv_text_t text = {input_text, input_text_size};
695
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400696 libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable);
Lei Zhang06efdc52015-09-10 14:00:00 -0400697 spv_result_t result =
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400698 spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400699 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
700
701 return result;
702}
703
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100704void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400705 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100706 if (text->str) {
707 delete[] text->str;
708 }
709 delete text;
710}