blob: c8a0f8af00a671dabe34c0cb1ef671c5c4b37f0b [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
27#include <libspirv/libspirv.h>
Lei Zhang610c5252015-09-09 10:36:48 -040028
Lei Zhang4e092d32015-09-11 13:45:18 -040029#include "bitwisecast.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010030#include "binary.h"
31#include "diagnostic.h"
32#include "ext_inst.h"
33#include "opcode.h"
34#include "operand.h"
35#include "text.h"
36
37#include <assert.h>
38#include <stdio.h>
David Netoaffa6962015-08-24 15:33:14 -040039#include <cstdlib>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010040#include <string.h>
41
42#include <string>
43#include <vector>
44#include <unordered_map>
45
Lei Zhang610c5252015-09-09 10:36:48 -040046using spvutils::BitwiseCast;
47
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010048// Structures
49
50struct spv_named_id_table_t {
51 std::unordered_map<std::string, uint32_t> namedIds;
52};
53
54// Text API
55
David Netoa48678a2015-09-11 12:04:03 -040056std::string spvGetWord(const char *str) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010057 size_t index = 0;
58 while (true) {
59 switch (str[index]) {
60 case '\0':
61 case '\t':
David Netoa48678a2015-09-11 12:04:03 -040062 case '\v':
63 case '\r':
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010064 case '\n':
65 case ' ':
David Netoa48678a2015-09-11 12:04:03 -040066 return std::string(str, str + index);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010067 default:
68 index++;
69 }
70 }
David Netoa48678a2015-09-11 12:04:03 -040071 assert(0 && "Unreachable");
Lei Zhang40056702015-09-11 14:31:27 -040072 return ""; // Make certain compilers happy.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073}
74
75spv_named_id_table spvNamedIdTableCreate() {
76 return new spv_named_id_table_t();
77}
78
79void spvNamedIdTableDestory(spv_named_id_table table) { delete table; }
80
81uint32_t spvNamedIdAssignOrGet(spv_named_id_table table, const char *textValue,
82 uint32_t *pBound) {
83 if (table->namedIds.end() == table->namedIds.find(textValue)) {
84 table->namedIds[textValue] = *pBound;
85 }
86 return table->namedIds[textValue];
87}
88
89int32_t spvTextIsNamedId(const char *textValue) {
90 // TODO: Strengthen the parsing of textValue to only include allow names that
91 // match: ([a-z]|[A-Z])(_|[a-z]|[A-Z]|[0-9])*
92 switch (textValue[0]) {
93 case '0':
94 case '1':
95 case '2':
96 case '3':
97 case '4':
98 case '5':
99 case '6':
100 case '7':
101 case '8':
102 case '9':
103 return false;
104 default:
105 break;
106 }
107 return true;
108}
109
110spv_result_t spvTextAdvanceLine(const spv_text text, spv_position position) {
111 while (true) {
112 switch (text->str[position->index]) {
113 case '\0':
114 return SPV_END_OF_STREAM;
115 case '\n':
116 position->column = 0;
117 position->line++;
118 position->index++;
119 return SPV_SUCCESS;
120 default:
Lei Zhangee87cc22015-08-21 11:51:28 -0400121 position->column++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100122 position->index++;
123 break;
124 }
125 }
126}
127
128spv_result_t spvTextAdvance(const spv_text text, spv_position position) {
129 // NOTE: Consume white space, otherwise don't advance.
130 switch (text->str[position->index]) {
131 case '\0':
132 return SPV_END_OF_STREAM;
133 case ';':
Lei Zhangee87cc22015-08-21 11:51:28 -0400134 if (spv_result_t error = spvTextAdvanceLine(text, position)) return error;
135 return spvTextAdvance(text, position);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100136 case ' ':
137 case '\t':
138 position->column++;
139 position->index++;
140 return spvTextAdvance(text, position);
141 case '\n':
142 position->column = 0;
143 position->line++;
144 position->index++;
145 return spvTextAdvance(text, position);
146 default:
147 break;
148 }
149
150 return SPV_SUCCESS;
151}
152
153spv_result_t spvTextWordGet(const spv_text text,
154 const spv_position startPosition, std::string &word,
155 spv_position endPosition) {
Lei Zhang40056702015-09-11 14:31:27 -0400156 if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
157 if (!startPosition || !endPosition) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100158
159 *endPosition = *startPosition;
160
David Netoe7ee4c42015-08-25 14:21:13 -0400161 bool quoting = false;
162 bool escaping = false;
163
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100164 // NOTE: Assumes first character is not white space!
165 while (true) {
David Netoe7ee4c42015-08-25 14:21:13 -0400166 const char ch = text->str[endPosition->index];
167 if (ch == '\\')
168 escaping = !escaping;
169 else {
170 switch (ch) {
171 case '"':
172 if (!escaping) quoting = !quoting;
173 break;
174 case ' ':
175 case ';':
176 case '\t':
177 case '\n':
178 if (escaping || quoting) break;
Lei Zhange78a7c12015-09-10 17:07:21 -0400179 // Fall through.
David Netoe7ee4c42015-08-25 14:21:13 -0400180 case '\0': { // NOTE: End of word found!
181 word.assign(text->str + startPosition->index,
182 (size_t)(endPosition->index - startPosition->index));
183 return SPV_SUCCESS;
184 }
185 default:
186 break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100187 }
David Netoe7ee4c42015-08-25 14:21:13 -0400188 escaping = false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100189 }
190
191 endPosition->column++;
192 endPosition->index++;
193 }
194}
195
David Netoc9786432015-09-01 18:05:14 -0400196namespace {
197
Lei Zhangdfc50082015-08-21 11:50:55 -0400198// Returns true if the string at the given position in text starts with "Op".
David Netoc9786432015-09-01 18:05:14 -0400199bool spvStartsWithOp(const spv_text text, const spv_position position) {
David Neto78c3b432015-08-27 13:03:52 -0400200 if (text->length < position->index + 3) return false;
201 char ch0 = text->str[position->index];
202 char ch1 = text->str[position->index + 1];
203 char ch2 = text->str[position->index + 2];
204 return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
Lei Zhangdfc50082015-08-21 11:50:55 -0400205}
206
Lei Zhange78a7c12015-09-10 17:07:21 -0400207} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400208
Lei Zhangdfc50082015-08-21 11:50:55 -0400209// Returns true if a new instruction begins at the given position in text.
Lei Zhange78a7c12015-09-10 17:07:21 -0400210bool spvTextIsStartOfNewInst(const spv_text text, const spv_position position) {
Lei Zhangdfc50082015-08-21 11:50:55 -0400211 spv_position_t nextPosition = *position;
212 if (spvTextAdvance(text, &nextPosition)) return false;
David Neto78c3b432015-08-27 13:03:52 -0400213 if (spvStartsWithOp(text, &nextPosition)) return true;
Lei Zhangdfc50082015-08-21 11:50:55 -0400214
215 std::string word;
David Neto78c3b432015-08-27 13:03:52 -0400216 spv_position_t startPosition = *position;
Lei Zhangdfc50082015-08-21 11:50:55 -0400217 if (spvTextWordGet(text, &startPosition, word, &nextPosition)) return false;
218 if ('%' != word.front()) return false;
219
220 if (spvTextAdvance(text, &nextPosition)) return false;
221 startPosition = nextPosition;
222 if (spvTextWordGet(text, &startPosition, word, &nextPosition)) return false;
223 if ("=" != word) return false;
224
225 if (spvTextAdvance(text, &nextPosition)) return false;
226 startPosition = nextPosition;
227 if (spvStartsWithOp(text, &startPosition)) return true;
228 return false;
229}
230
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100231spv_result_t spvTextStringGet(const spv_text text,
232 const spv_position startPosition,
233 std::string &string, spv_position endPosition) {
Lei Zhang40056702015-09-11 14:31:27 -0400234 if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
235 if (!startPosition || !endPosition) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100236
Lei Zhang40056702015-09-11 14:31:27 -0400237 if ('"' != text->str[startPosition->index]) return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100238
239 *endPosition = *startPosition;
240
241 // NOTE: Assumes first character is not white space
242 while (true) {
243 endPosition->column++;
244 endPosition->index++;
245
246 switch (text->str[endPosition->index]) {
247 case '"': {
248 endPosition->column++;
249 endPosition->index++;
250
251 string.assign(text->str + startPosition->index,
252 (size_t)(endPosition->index - startPosition->index));
253
254 return SPV_SUCCESS;
255 }
256 case '\n':
257 case '\0':
258 return SPV_ERROR_INVALID_TEXT;
259 default:
260 break;
261 }
262 }
263}
264
265spv_result_t spvTextToUInt32(const char *textValue, uint32_t *pValue) {
266 char *endPtr = nullptr;
267 *pValue = strtoul(textValue, &endPtr, 0);
268 if (0 == *pValue && textValue == endPtr) {
269 return SPV_ERROR_INVALID_TEXT;
270 }
271 return SPV_SUCCESS;
272}
273
274spv_result_t spvTextToLiteral(const char *textValue, spv_literal_t *pLiteral) {
275 bool isSigned = false;
David Netoaffa6962015-08-24 15:33:14 -0400276 int numPeriods = 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100277 bool isString = false;
278
David Netoaffa6962015-08-24 15:33:14 -0400279 const size_t len = strlen(textValue);
David Neto98290a22015-08-24 16:27:02 -0400280 if (len == 0) return SPV_FAILED_MATCH;
281
David Netoaffa6962015-08-24 15:33:14 -0400282 for (uint64_t index = 0; index < len; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100283 switch (textValue[index]) {
284 case '0':
285 case '1':
286 case '2':
287 case '3':
288 case '4':
289 case '5':
290 case '6':
291 case '7':
292 case '8':
293 case '9':
294 break;
295 case '.':
David Netoaffa6962015-08-24 15:33:14 -0400296 numPeriods++;
297 break;
298 case '-':
299 if (index == 0) {
300 isSigned = true;
301 } else {
302 isString = true;
303 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100304 break;
305 default:
306 isString = true;
Lei Zhange78a7c12015-09-10 17:07:21 -0400307 index = len; // break out of the loop too.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100308 break;
309 }
310 }
311
David Netoaffa6962015-08-24 15:33:14 -0400312 pLiteral->type = spv_literal_type_t(99);
313
Lei Zhange78a7c12015-09-10 17:07:21 -0400314 if (isString || numPeriods > 1 || (isSigned && len == 1)) {
David Netoaffa6962015-08-24 15:33:14 -0400315 // TODO(dneto): Allow escaping.
David Neto98290a22015-08-24 16:27:02 -0400316 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
317 return SPV_FAILED_MATCH;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100318 pLiteral->type = SPV_LITERAL_TYPE_STRING;
David Netoaffa6962015-08-24 15:33:14 -0400319 // Need room for the null-terminator.
David Neto98290a22015-08-24 16:27:02 -0400320 if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY;
Lei Zhange78a7c12015-09-10 17:07:21 -0400321 strncpy(pLiteral->value.str, textValue + 1, len - 2);
322 pLiteral->value.str[len - 2] = 0;
David Netoaffa6962015-08-24 15:33:14 -0400323 } else if (numPeriods == 1) {
324 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100325 float f = (float)d;
326 if (d == (double)f) {
327 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
328 pLiteral->value.f = f;
329 } else {
330 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
331 pLiteral->value.d = d;
332 }
333 } else if (isSigned) {
334 int64_t i64 = strtoll(textValue, nullptr, 10);
335 int32_t i32 = (int32_t)i64;
336 if (i64 == (int64_t)i32) {
337 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
338 pLiteral->value.i32 = i32;
339 } else {
340 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
341 pLiteral->value.i64 = i64;
342 }
343 } else {
344 uint64_t u64 = strtoull(textValue, nullptr, 10);
345 uint32_t u32 = (uint32_t)u64;
346 if (u64 == (uint64_t)u32) {
347 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
348 pLiteral->value.u32 = u32;
349 } else {
350 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
351 pLiteral->value.u64 = u64;
352 }
353 }
354
355 return SPV_SUCCESS;
356}
357
358spv_result_t spvTextEncodeOperand(
359 const spv_operand_type_t type, const char *textValue,
360 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
361 spv_named_id_table namedIdTable, spv_instruction_t *pInst,
Lei Zhange78a7c12015-09-10 17:07:21 -0400362 spv_operand_pattern_t *pExpectedOperands, uint32_t *pBound,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100363 const spv_position position, spv_diagnostic *pDiagnostic) {
364 // NOTE: Handle immediate int in the stream
365 if ('!' == textValue[0]) {
366 const char *begin = textValue + 1;
367 char *end = nullptr;
368 uint32_t immediateInt = strtoul(begin, &end, 0);
369 size_t size = strlen(textValue);
370 size_t length = (end - begin);
Lei Zhang40056702015-09-11 14:31:27 -0400371 if (size - 1 != length) {
372 DIAGNOSTIC << "Invalid immediate integer '" << textValue << "'.";
373 return SPV_ERROR_INVALID_TEXT;
374 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100375 position->column += size;
376 position->index += size;
377 pInst->words[pInst->wordCount] = immediateInt;
378 pInst->wordCount += 1;
379 return SPV_SUCCESS;
380 }
381
382 switch (type) {
David Netoe3f70b92015-08-27 13:50:05 -0400383 case SPV_OPERAND_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400384 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
385 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netofadbf622015-09-14 17:07:11 -0400386 case SPV_OPERAND_TYPE_RESULT_ID:
387 case SPV_OPERAND_TYPE_EXECUTION_SCOPE: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400388 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100389 textValue++;
390 }
Lei Zhangabafd5e2015-08-21 11:52:29 -0400391 // TODO: Force all ID's to be prefixed with '%'.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100392 uint32_t id = 0;
393 if (spvTextIsNamedId(textValue)) {
394 id = spvNamedIdAssignOrGet(namedIdTable, textValue, pBound);
395 } else {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400396 if (spvTextToUInt32(textValue, &id) != SPV_SUCCESS) {
397 if (spvOperandIsOptional(type)) {
398 return SPV_FAILED_MATCH;
399 } else {
David Netofadbf622015-09-14 17:07:11 -0400400 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '"
401 << textValue << "'.";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400402 return SPV_ERROR_INVALID_TEXT;
403 }
404 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100405 }
406 pInst->words[pInst->wordCount++] = id;
407 if (*pBound <= id) {
408 *pBound = id + 1;
409 }
410 } break;
411 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
412 // NOTE: Special case for extension instruction lookup
413 if (OpExtInst == pInst->opcode) {
414 spv_ext_inst_desc extInst;
Lei Zhang40056702015-09-11 14:31:27 -0400415 if (spvExtInstTableNameLookup(extInstTable, pInst->extInstType,
416 textValue, &extInst)) {
417 DIAGNOSTIC << "Invalid extended instruction name '" << textValue
418 << "'.";
419 return SPV_ERROR_INVALID_TEXT;
420 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100421 pInst->words[pInst->wordCount++] = extInst->ext_inst;
David Neto78c3b432015-08-27 13:03:52 -0400422
423 // Prepare to parse the operands for the extended instructions.
424 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
425
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100426 return SPV_SUCCESS;
427 }
428
429 // TODO: Literal numbers can be any number up to 64 bits wide. This
430 // includes integers and floating point numbers.
David Neto78c3b432015-08-27 13:03:52 -0400431 // TODO(dneto): Suggest using spvTextToLiteral and looking for an
432 // appropriate result type.
Lei Zhang40056702015-09-11 14:31:27 -0400433 if (spvTextToUInt32(textValue, &pInst->words[pInst->wordCount++])) {
434 DIAGNOSTIC << "Invalid literal number '" << textValue << "'.";
435 return SPV_ERROR_INVALID_TEXT;
436 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100437 } break;
David Neto78c3b432015-08-27 13:03:52 -0400438 case SPV_OPERAND_TYPE_LITERAL:
439 case SPV_OPERAND_TYPE_LITERAL_IN_OPTIONAL_TUPLE:
440 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100441 spv_literal_t literal = {};
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400442 if (spvTextToLiteral(textValue, &literal) != SPV_SUCCESS) {
443 if (spvOperandIsOptional(type)) {
444 return SPV_FAILED_MATCH;
445 } else {
446 DIAGNOSTIC << "Invalid literal '" << textValue << "'.";
447 return SPV_ERROR_INVALID_TEXT;
448 }
449 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100450 switch (literal.type) {
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400451 // We do not have to print diagnostics here because spvBinaryEncode*
452 // prints diagnostic messages on failure.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100453 case SPV_LITERAL_TYPE_INT_32:
Lei Zhang40056702015-09-11 14:31:27 -0400454 if (spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32),
455 pInst, position, pDiagnostic))
456 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100457 break;
458 case SPV_LITERAL_TYPE_INT_64: {
Lei Zhang40056702015-09-11 14:31:27 -0400459 if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64),
460 pInst, position, pDiagnostic))
461 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100462 } break;
463 case SPV_LITERAL_TYPE_UINT_32: {
Lei Zhang40056702015-09-11 14:31:27 -0400464 if (spvBinaryEncodeU32(literal.value.u32, pInst, position,
465 pDiagnostic))
466 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100467 } break;
468 case SPV_LITERAL_TYPE_UINT_64: {
Lei Zhang40056702015-09-11 14:31:27 -0400469 if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64),
470 pInst, position, pDiagnostic))
471 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100472 } break;
473 case SPV_LITERAL_TYPE_FLOAT_32: {
Lei Zhang40056702015-09-11 14:31:27 -0400474 if (spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f), pInst,
475 position, pDiagnostic))
476 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100477 } break;
478 case SPV_LITERAL_TYPE_FLOAT_64: {
Lei Zhang40056702015-09-11 14:31:27 -0400479 if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d), pInst,
480 position, pDiagnostic))
481 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100482 } break;
483 case SPV_LITERAL_TYPE_STRING: {
Lei Zhang40056702015-09-11 14:31:27 -0400484 if (spvBinaryEncodeString(literal.value.str, pInst, position,
485 pDiagnostic))
486 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100487 } break;
488 default:
489 DIAGNOSTIC << "Invalid literal '" << textValue << "'";
490 return SPV_ERROR_INVALID_TEXT;
491 }
492 } break;
David Neto78c3b432015-08-27 13:03:52 -0400493 case SPV_OPERAND_TYPE_LITERAL_STRING:
494 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100495 size_t len = strlen(textValue);
Lei Zhang40056702015-09-11 14:31:27 -0400496 if ('"' != textValue[0] && '"' != textValue[len - 1]) {
497 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
498 DIAGNOSTIC << "Invalid literal string '" << textValue
499 << "', expected quotes.";
500 return SPV_ERROR_INVALID_TEXT;
501 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100502 // NOTE: Strip quotes
503 std::string text(textValue + 1, len - 2);
504
505 // NOTE: Special case for extended instruction library import
506 if (OpExtInstImport == pInst->opcode) {
507 pInst->extInstType = spvExtInstImportTypeGet(text.c_str());
508 }
509
Lei Zhang40056702015-09-11 14:31:27 -0400510 if (spvBinaryEncodeString(text.c_str(), pInst, position, pDiagnostic))
511 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100512 } break;
David Neto78c3b432015-08-27 13:03:52 -0400513 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
514 assert(0 && " Handle optional optional image operands");
515 break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100516 default: {
517 // NOTE: All non literal operands are handled here using the operand
518 // table.
519 spv_operand_desc entry;
Lei Zhang40056702015-09-11 14:31:27 -0400520 if (spvOperandTableNameLookup(operandTable, type, textValue, &entry)) {
521 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
522 << "'.";
523 return SPV_ERROR_INVALID_TEXT;
524 }
525 if (spvBinaryEncodeU32(entry->value, pInst, position, pDiagnostic)) {
526 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
527 << "'.";
528 return SPV_ERROR_INVALID_TEXT;
529 }
David Neto78c3b432015-08-27 13:03:52 -0400530
531 // Prepare to parse the operands for this logical operand.
532 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100533 } break;
534 }
535 return SPV_SUCCESS;
536}
537
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400538namespace {
539
540/// Encodes an instruction started by !<integer> at the given position in text.
541///
542/// Puts the encoded words into *pInst. If successful, moves position past the
543/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
544/// leaves position pointing to the error in text.
545spv_result_t encodeInstructionStartingWithImmediate(
546 const spv_text text, const spv_operand_table operandTable,
547 const spv_ext_inst_table extInstTable, spv_named_id_table namedIdTable,
548 uint32_t *pBound, spv_instruction_t *pInst, spv_position position,
549 spv_diagnostic *pDiagnostic) {
550 std::string firstWord;
551 spv_position_t nextPosition = {};
552 auto error = spvTextWordGet(text, position, firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400553 if (error) {
554 DIAGNOSTIC << "Internal Error";
555 return error;
556 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400557
558 assert(firstWord[0] == '!');
559 const char *begin = firstWord.data() + 1;
560 char *end = nullptr;
561 uint32_t immediateInt = strtoul(begin, &end, 0);
Lei Zhang40056702015-09-11 14:31:27 -0400562 if ((begin + firstWord.size() - 1) != end) {
563 DIAGNOSTIC << "Invalid immediate integer '" << firstWord << "'.";
564 return SPV_ERROR_INVALID_TEXT;
565 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400566 position->column += firstWord.size();
567 position->index += firstWord.size();
568 pInst->words[0] = immediateInt;
569 pInst->wordCount = 1;
570 while (spvTextAdvance(text, position) != SPV_END_OF_STREAM) {
571 // A beginning of a new instruction means we're done.
572 if (spvTextIsStartOfNewInst(text, position)) return SPV_SUCCESS;
573
574 // Otherwise, there must be an operand that's either a literal, an ID, or
575 // an immediate.
576 std::string operandValue;
577 if ((error = spvTextWordGet(text, position, operandValue, &nextPosition))) {
578 DIAGNOSTIC << "Internal Error";
579 return error;
580 }
581
582 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
583 // expanded.
584 spv_operand_pattern_t dummyExpectedOperands;
585 error = spvTextEncodeOperand(
586 SPV_OPERAND_TYPE_OPTIONAL_LITERAL, operandValue.c_str(), operandTable,
587 extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound,
588 position, pDiagnostic);
589 if (error == SPV_FAILED_MATCH) {
590 // It's not a literal -- is it an ID?
591 error = spvTextEncodeOperand(
592 SPV_OPERAND_TYPE_OPTIONAL_ID, operandValue.c_str(), operandTable,
593 extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound,
594 position, pDiagnostic);
595 if (error) {
596 DIAGNOSTIC << "Invalid word following " << firstWord << ": "
597 << operandValue;
598 }
599 }
Lei Zhang40056702015-09-11 14:31:27 -0400600 if (error) return error;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400601 *position = nextPosition;
602 }
603 return SPV_SUCCESS;
604}
605
606} // anonymous namespace
607
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100608spv_result_t spvTextEncodeOpcode(
Lei Zhang06efdc52015-09-10 14:00:00 -0400609 const spv_text text, spv_assembly_syntax_format_t format,
610 const spv_opcode_table opcodeTable, const spv_operand_table operandTable,
611 const spv_ext_inst_table extInstTable, spv_named_id_table namedIdTable,
612 uint32_t *pBound, spv_instruction_t *pInst, spv_position position,
613 spv_diagnostic *pDiagnostic) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400614 // Check for !<integer> first.
615 if ('!' == text->str[position->index]) {
616 return encodeInstructionStartingWithImmediate(
617 text, operandTable, extInstTable, namedIdTable, pBound, pInst, position,
618 pDiagnostic);
619 }
620
Lei Zhangdfc50082015-08-21 11:50:55 -0400621 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400622 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
623 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400624
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400625 if ('!' == text->str[position->index]) {
626 return encodeInstructionStartingWithImmediate(
627 text, operandTable, extInstTable, namedIdTable, pBound, pInst, position,
628 pDiagnostic);
629 }
630
Lei Zhang06efdc52015-09-10 14:00:00 -0400631 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100632 spv_position_t nextPosition = {};
Lei Zhang06efdc52015-09-10 14:00:00 -0400633 spv_result_t error = spvTextWordGet(text, position, firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400634 if (error) {
635 DIAGNOSTIC << "Internal Error";
636 return error;
637 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100638
Lei Zhang06efdc52015-09-10 14:00:00 -0400639 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400640 std::string result_id;
641 spv_position_t result_id_position = {};
Lei Zhang06efdc52015-09-10 14:00:00 -0400642 if (spvStartsWithOp(text, position)) {
643 opcodeName = firstWord;
644 } else {
645 // If the first word of this instruction is not an opcode, we must be
646 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400647 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
648 DIAGNOSTIC
649 << "Expected <opcode> at the beginning of an instruction, found '"
650 << firstWord << "'.";
651 return SPV_ERROR_INVALID_TEXT;
652 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400653
654 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400655 if ('%' != result_id.front()) {
656 DIAGNOSTIC << "Expected <opcode> or <result-id> at the beginning "
657 "of an instruction, found '"
658 << result_id << "'.";
659 return SPV_ERROR_INVALID_TEXT;
660 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400661 result_id_position = *position;
Lei Zhang06efdc52015-09-10 14:00:00 -0400662
663 // The '=' sign.
Lei Zhangdfc50082015-08-21 11:50:55 -0400664 *position = nextPosition;
Lei Zhang40056702015-09-11 14:31:27 -0400665 if (spvTextAdvance(text, position)) {
666 DIAGNOSTIC << "Expected '=', found end of stream.";
667 return SPV_ERROR_INVALID_TEXT;
668 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400669 std::string equal_sign;
670 error = spvTextWordGet(text, position, equal_sign, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400671 if ("=" != equal_sign) {
672 DIAGNOSTIC << "'=' expected after result id.";
673 return SPV_ERROR_INVALID_TEXT;
674 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400675
676 // The <opcode> after the '=' sign.
677 *position = nextPosition;
Lei Zhang40056702015-09-11 14:31:27 -0400678 if (spvTextAdvance(text, position)) {
679 DIAGNOSTIC << "Expected opcode, found end of stream.";
680 return SPV_ERROR_INVALID_TEXT;
681 }
Lei Zhang977e9bc2015-08-21 11:52:03 -0400682 error = spvTextWordGet(text, position, opcodeName, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400683 if (error) {
684 DIAGNOSTIC << "Internal Error";
685 return error;
686 }
687 if (!spvStartsWithOp(text, position)) {
688 DIAGNOSTIC << "Invalid Opcode prefix '" << opcodeName << "'.";
689 return SPV_ERROR_INVALID_TEXT;
690 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400691 }
692
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100693 // NOTE: The table contains Opcode names without the "Op" prefix.
694 const char *pInstName = opcodeName.data() + 2;
695
696 spv_opcode_desc opcodeEntry;
Lei Zhangdfc50082015-08-21 11:50:55 -0400697 error = spvOpcodeTableNameLookup(opcodeTable, pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400698 if (error) {
699 DIAGNOSTIC << "Invalid Opcode name '"
700 << spvGetWord(text->str + position->index) << "'";
701 return error;
702 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400703 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
704 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400705 if (opcodeEntry->hasResult && result_id.empty()) {
706 DIAGNOSTIC << "Expected <result-id> at the beginning of an "
707 "instruction, found '"
708 << firstWord << "'.";
709 return SPV_ERROR_INVALID_TEXT;
710 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400711 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100712 pInst->opcode = opcodeEntry->opcode;
713 *position = nextPosition;
714 pInst->wordCount++;
715
David Neto78c3b432015-08-27 13:03:52 -0400716 // Maintains the ordered list of expected operand types.
717 // For many instructions we only need the {numTypes, operandTypes}
718 // entries in opcodeEntry. However, sometimes we need to modify
719 // the list as we parse the operands. This occurs when an operand
720 // has its own logical operands (such as the LocalSize operand for
721 // ExecutionMode), or for extended instructions that may have their
722 // own operands depending on the selected extended instruction.
723 spv_operand_pattern_t expectedOperands(
724 opcodeEntry->operandTypes,
725 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400726
David Neto78c3b432015-08-27 13:03:52 -0400727 while (!expectedOperands.empty()) {
728 const spv_operand_type_t type = expectedOperands.front();
729 expectedOperands.pop_front();
730
731 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400732 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400733
734 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
735 // Handle the <result-id> for value generating instructions.
736 // We've already consumed it from the text stream. Here
737 // we inject its words into the instruction.
Lei Zhange78a7c12015-09-10 17:07:21 -0400738 error = spvTextEncodeOperand(SPV_OPERAND_TYPE_RESULT_ID,
739 result_id.c_str(), operandTable,
740 extInstTable, namedIdTable, pInst, nullptr,
741 pBound, &result_id_position, pDiagnostic);
Lei Zhang40056702015-09-11 14:31:27 -0400742 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100743 } else {
David Neto78c3b432015-08-27 13:03:52 -0400744 // Find the next word.
745 error = spvTextAdvance(text, position);
746 if (error == SPV_END_OF_STREAM) {
747 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400748 // This would have been the last potential operand for the
749 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400750 // and we didn't find one. We're finished parsing this instruction.
751 break;
752 } else {
753 DIAGNOSTIC << "Expected operand, found end of stream.";
754 return SPV_ERROR_INVALID_TEXT;
755 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100756 }
David Neto78c3b432015-08-27 13:03:52 -0400757 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
758
759 if (spvTextIsStartOfNewInst(text, position)) {
760 if (spvOperandIsOptional(type)) {
761 break;
762 } else {
763 DIAGNOSTIC << "Expected operand, found next instruction instead.";
764 return SPV_ERROR_INVALID_TEXT;
765 }
766 }
767
768 std::string operandValue;
769 error = spvTextWordGet(text, position, operandValue, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400770 if (error) {
771 DIAGNOSTIC << "Internal Error";
772 return error;
773 }
David Neto78c3b432015-08-27 13:03:52 -0400774
775 error = spvTextEncodeOperand(
Lei Zhange78a7c12015-09-10 17:07:21 -0400776 type, operandValue.c_str(), operandTable, extInstTable, namedIdTable,
777 pInst, &expectedOperands, pBound, position, pDiagnostic);
David Neto78c3b432015-08-27 13:03:52 -0400778
779 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
780 return SPV_SUCCESS;
781
Lei Zhang40056702015-09-11 14:31:27 -0400782 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400783
784 *position = nextPosition;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100785 }
786 }
787
788 pInst->words[0] = spvOpcodeMake(pInst->wordCount, opcodeEntry->opcode);
789
790 return SPV_SUCCESS;
791}
792
David Netoc9786432015-09-01 18:05:14 -0400793namespace {
794
795// Translates a given assembly language module into binary form.
796// If a diagnostic is generated, it is not yet marked as being
797// for a text-based input.
798spv_result_t spvTextToBinaryInternal(const spv_text text,
Lei Zhang06efdc52015-09-10 14:00:00 -0400799 spv_assembly_syntax_format_t format,
David Netoc9786432015-09-01 18:05:14 -0400800 const spv_opcode_table opcodeTable,
801 const spv_operand_table operandTable,
802 const spv_ext_inst_table extInstTable,
803 spv_binary *pBinary,
804 spv_diagnostic *pDiagnostic) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100805 spv_position_t position = {};
Lei Zhang40056702015-09-11 14:31:27 -0400806 if (!text->str || !text->length) {
807 DIAGNOSTIC << "Text stream is empty.";
808 return SPV_ERROR_INVALID_TEXT;
809 }
810 if (!opcodeTable || !operandTable || !extInstTable)
811 return SPV_ERROR_INVALID_TABLE;
812 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
813 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100814
815 // NOTE: Ensure diagnostic is zero initialised
816 *pDiagnostic = {};
817
818 uint32_t bound = 1;
819
820 std::vector<spv_instruction_t> instructions;
821
Lei Zhang40056702015-09-11 14:31:27 -0400822 if (spvTextAdvance(text, &position)) {
823 DIAGNOSTIC << "Text stream is empty.";
824 return SPV_ERROR_INVALID_TEXT;
825 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100826
827 spv_named_id_table namedIdTable = spvNamedIdTableCreate();
Lei Zhang40056702015-09-11 14:31:27 -0400828 if (!namedIdTable) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100829
830 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
831 while (text->length > position.index) {
832 spv_instruction_t inst = {};
833 inst.extInstType = extInstType;
834
Lei Zhang40056702015-09-11 14:31:27 -0400835 if (spvTextEncodeOpcode(text, format, opcodeTable, operandTable,
836 extInstTable, namedIdTable, &bound, &inst,
837 &position, pDiagnostic)) {
838 spvNamedIdTableDestory(namedIdTable);
839 return SPV_ERROR_INVALID_TEXT;
840 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100841 extInstType = inst.extInstType;
842
843 instructions.push_back(inst);
844
Lei Zhang40056702015-09-11 14:31:27 -0400845 if (spvTextAdvance(text, &position)) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100846 }
847
848 spvNamedIdTableDestory(namedIdTable);
849
850 size_t totalSize = SPV_INDEX_INSTRUCTION;
851 for (auto &inst : instructions) {
852 totalSize += inst.wordCount;
853 }
854
855 uint32_t *data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400856 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100857 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
858 for (auto &inst : instructions) {
859 memcpy(data + currentIndex, inst.words, sizeof(uint32_t) * inst.wordCount);
860 currentIndex += inst.wordCount;
861 }
862
863 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400864 if (!binary) {
865 delete[] data;
866 return SPV_ERROR_OUT_OF_MEMORY;
867 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100868 binary->code = data;
869 binary->wordCount = totalSize;
870
871 spv_result_t error = spvBinaryHeaderSet(binary, bound);
Lei Zhang40056702015-09-11 14:31:27 -0400872 if (error) {
873 spvBinaryDestroy(binary);
874 return error;
875 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100876
877 *pBinary = binary;
878
879 return SPV_SUCCESS;
880}
881
Lei Zhange78a7c12015-09-10 17:07:21 -0400882} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400883
Lei Zhang06efdc52015-09-10 14:00:00 -0400884spv_result_t spvTextToBinary(const char *input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400885 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400886 const spv_opcode_table opcodeTable,
887 const spv_operand_table operandTable,
888 const spv_ext_inst_table extInstTable,
889 spv_binary *pBinary, spv_diagnostic *pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400890 return spvTextWithFormatToBinary(
891 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
892 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
893}
894
895spv_result_t spvTextWithFormatToBinary(
896 const char *input_text, const uint64_t input_text_size,
897 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
898 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
899 spv_binary *pBinary, spv_diagnostic *pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400900 spv_text_t text = {input_text, input_text_size};
901
Lei Zhang06efdc52015-09-10 14:00:00 -0400902 spv_result_t result =
903 spvTextToBinaryInternal(&text, format, opcodeTable, operandTable,
904 extInstTable, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400905 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
906
907 return result;
908}
909
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100910void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400911 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100912 if (text->str) {
913 delete[] text->str;
914 }
915 delete text;
916}