blob: 96086eea06f29719b5dc9bbc11540efc266ec1c4 [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;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100113 pLiteral->type = SPV_LITERAL_TYPE_STRING;
David Netoaffa6962015-08-24 15:33:14 -0400114 // Need room for the null-terminator.
David Neto98290a22015-08-24 16:27:02 -0400115 if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY;
Lei Zhange78a7c12015-09-10 17:07:21 -0400116 strncpy(pLiteral->value.str, textValue + 1, len - 2);
117 pLiteral->value.str[len - 2] = 0;
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;
163 if (auto error =
164 context->parseNumber(text + 1, false, &parse_result,
165 "Invalid immediate integer: !"))
166 return error;
167 context->binaryEncodeU32(parse_result, pInst);
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400168 context->seekForward(strlen(text));
169 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
200 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400201 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Netoe3f70b92015-08-27 13:50:05 -0400202 case SPV_OPERAND_TYPE_ID:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400203 case SPV_OPERAND_TYPE_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400204 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
205 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netob14a7272015-09-25 13:56:09 -0400206 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
207 case SPV_OPERAND_TYPE_RESULT_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400208 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100209 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100210 } else {
David Netoac508b02015-10-09 15:48:09 -0400211 return context->diagnostic() << "Expected id to start with %.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100212 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400213 if (!spvIsValidID(textValue)) {
David Netoac508b02015-10-09 15:48:09 -0400214 return context->diagnostic() << "Invalid ID " << textValue;
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400215 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400216 const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400217 if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
David Netob5dc8fc2015-10-06 16:22:00 -0400218 spvInstructionAddWord(pInst, id);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100219 } break;
220 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
221 // NOTE: Special case for extension instruction lookup
222 if (OpExtInst == pInst->opcode) {
223 spv_ext_inst_desc extInst;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400224 if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst)) {
David Netoac508b02015-10-09 15:48:09 -0400225 return context->diagnostic() << "Invalid extended instruction name '"
226 << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400227 }
David Netob5dc8fc2015-10-06 16:22:00 -0400228 spvInstructionAddWord(pInst, extInst->ext_inst);
David Neto78c3b432015-08-27 13:03:52 -0400229
230 // Prepare to parse the operands for the extended instructions.
231 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
232
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100233 return SPV_SUCCESS;
234 }
Lei Zhang6d415812015-09-15 13:36:21 -0400235 } // Fall through for the general case.
Lei Zhangb41d1502015-09-14 15:22:23 -0400236 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
David Neto561dc4e2015-09-25 14:23:29 -0400237 case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE:
238 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: {
David Neto78e677b2015-10-05 13:28:46 -0400239 libspirv::IdType expected_type = libspirv::kUnknownType;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400240 // The encoding for OpConstant, OpSpecConstant and OpSwitch all
241 // depend on either their own result-id or the result-id of
242 // one of their parameters.
243 if (OpConstant == pInst->opcode || OpSpecConstant == pInst->opcode) {
244 // Special cases for encoding possibly non-32-bit literals here.
David Neto78e677b2015-10-05 13:28:46 -0400245 expected_type =
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400246 context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
David Neto78e677b2015-10-05 13:28:46 -0400247 if (!libspirv::isScalarFloating(expected_type) &&
248 !libspirv::isScalarIntegral(expected_type)) {
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400249 spv_opcode_desc d;
250 const char* opcode_name = "opcode";
251 if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
252 opcode_name = d->name;
253 }
David Netoac508b02015-10-09 15:48:09 -0400254 return context->diagnostic()
255 << "Type for " << opcode_name
256 << " must be a scalar floating point or integer type";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400257 }
258 } else if (pInst->opcode == OpSwitch) {
David Neto78e677b2015-10-05 13:28:46 -0400259 // We need to know the type of the selector.
260 expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
261 if (!libspirv::isScalarIntegral(expected_type)) {
262 context->diagnostic()
263 << "The selector operand for OpSwitch must be the result"
264 " of an instruction that generates an integer scalar";
265 return SPV_ERROR_INVALID_TEXT;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400266 }
267 }
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400268
David Neto78e677b2015-10-05 13:28:46 -0400269 if (auto error = context->binaryEncodeNumericLiteral(
270 textValue, spvOperandIsOptional(type), expected_type, pInst)) {
271 return error;
272 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100273 } break;
David Neto78c3b432015-08-27 13:03:52 -0400274 case SPV_OPERAND_TYPE_LITERAL_STRING:
275 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400276 spv_literal_t literal = {};
277 spv_result_t error = spvTextToLiteral(textValue, &literal);
278 if (error != SPV_SUCCESS) {
279 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
Lei Zhang40056702015-09-11 14:31:27 -0400280 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
David Netoac508b02015-10-09 15:48:09 -0400281 return context->diagnostic() << "Invalid literal string '" << textValue
282 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400283 }
Lei Zhang6d415812015-09-15 13:36:21 -0400284 if (literal.type != SPV_LITERAL_TYPE_STRING) {
David Netoac508b02015-10-09 15:48:09 -0400285 return context->diagnostic(SPV_FAILED_MATCH)
286 << "Expected literal string, found literal number '" << textValue
287 << "'.";
Lei Zhang6d415812015-09-15 13:36:21 -0400288 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100289
290 // NOTE: Special case for extended instruction library import
291 if (OpExtInstImport == pInst->opcode) {
Lei Zhang6d415812015-09-15 13:36:21 -0400292 pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100293 }
294
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400295 if (context->binaryEncodeString(literal.value.str, pInst))
Lei Zhang40056702015-09-11 14:31:27 -0400296 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100297 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400298 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
299 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
300 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400301 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400302 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400303 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
304 uint32_t value;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400305 if (grammar.parseMaskOperand(type, textValue, &value)) {
David Netoac508b02015-10-09 15:48:09 -0400306 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
307 << " '" << textValue << "'.";
David Neto36b0c0f2015-09-16 18:32:54 -0400308 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400309 if (auto error = context->binaryEncodeU32(value, pInst)) return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400310 // Prepare to parse the operands for this logical operand.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400311 grammar.prependOperandTypesForMask(type, value, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400312 } break;
313 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
314 auto error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400315 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
316 pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400317 if (error == SPV_FAILED_MATCH) {
318 // It's not a literal number -- is it a literal string?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400319 error = spvTextEncodeOperand(grammar, context,
320 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
321 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400322 }
323 if (error == SPV_FAILED_MATCH) {
324 // It's not a literal -- is it an ID?
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400325 error =
326 spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
327 textValue, pInst, pExpectedOperands);
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400328 }
329 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400330 return context->diagnostic(error)
331 << "Invalid word following !<integer>: " << textValue;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400332 }
333 if (pExpectedOperands->empty()) {
334 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
335 }
David Neto36b0c0f2015-09-16 18:32:54 -0400336 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100337 default: {
338 // NOTE: All non literal operands are handled here using the operand
339 // table.
340 spv_operand_desc entry;
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400341 if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
David Netoac508b02015-10-09 15:48:09 -0400342 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
343 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400344 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400345 if (context->binaryEncodeU32(entry->value, pInst)) {
David Netoac508b02015-10-09 15:48:09 -0400346 return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
347 << " '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400348 }
David Neto78c3b432015-08-27 13:03:52 -0400349
350 // Prepare to parse the operands for this logical operand.
351 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100352 } break;
353 }
354 return SPV_SUCCESS;
355}
356
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400357namespace {
358
359/// Encodes an instruction started by !<integer> at the given position in text.
360///
361/// Puts the encoded words into *pInst. If successful, moves position past the
362/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
363/// leaves position pointing to the error in text.
364spv_result_t encodeInstructionStartingWithImmediate(
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400365 const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400366 libspirv::AssemblyContext* context, spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400367 std::string firstWord;
368 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400369 auto error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400370 if (error) return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400371
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400372 if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
373 return error;
Lei Zhang40056702015-09-11 14:31:27 -0400374 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400375 while (context->advance() != SPV_END_OF_STREAM) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400376 // A beginning of a new instruction means we're done.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400377 if (context->isStartOfNewInst()) return SPV_SUCCESS;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400378
379 // Otherwise, there must be an operand that's either a literal, an ID, or
380 // an immediate.
381 std::string operandValue;
David Netoac508b02015-10-09 15:48:09 -0400382 if ((error = context->getWord(operandValue, &nextPosition)))
383 return context->diagnostic(error) << "Internal Error";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400384
David Netoac508b02015-10-09 15:48:09 -0400385 if (operandValue == "=")
386 return context->diagnostic() << firstWord << " not allowed before =.";
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400387
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400388 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
389 // expanded.
390 spv_operand_pattern_t dummyExpectedOperands;
391 error = spvTextEncodeOperand(
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400392 grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
393 pInst, &dummyExpectedOperands);
Lei Zhang40056702015-09-11 14:31:27 -0400394 if (error) return error;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400395 context->setPosition(nextPosition);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400396 }
397 return SPV_SUCCESS;
398}
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400399} // anonymous namespace
400
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400401/// @brief Translate single Opcode and operands to binary form
402///
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400403/// @param[in] grammar the grammar to use for compilation
404/// @param[in, out] context the dynamic compilation info
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400405/// @param[in] text stream to translate
406/// @param[in] format the assembly syntax format of text
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400407/// @param[out] pInst returned binary Opcode
408/// @param[in,out] pPosition in the text stream
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400409///
410/// @return result code
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400411spv_result_t spvTextEncodeOpcode(const libspirv::AssemblyGrammar& grammar,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400412 libspirv::AssemblyContext* context,
413 spv_assembly_syntax_format_t format,
414 spv_instruction_t* pInst) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400415 // Check for !<integer> first.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400416 if ('!' == context->peek()) {
417 return encodeInstructionStartingWithImmediate(grammar, context, pInst);
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400418 }
419
Lei Zhangdfc50082015-08-21 11:50:55 -0400420 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400421 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
422 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400423
Lei Zhang06efdc52015-09-10 14:00:00 -0400424 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100425 spv_position_t nextPosition = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400426 spv_result_t error = context->getWord(firstWord, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400427 if (error) return context->diagnostic() << "Internal Error";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100428
Lei Zhang06efdc52015-09-10 14:00:00 -0400429 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400430 std::string result_id;
431 spv_position_t result_id_position = {};
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400432 if (context->startsWithOp()) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400433 opcodeName = firstWord;
434 } else {
435 // If the first word of this instruction is not an opcode, we must be
436 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400437 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
David Netoac508b02015-10-09 15:48:09 -0400438 return context->diagnostic()
439 << "Expected <opcode> at the beginning of an instruction, found '"
440 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400441 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400442
443 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400444 if ('%' != result_id.front()) {
David Netoac508b02015-10-09 15:48:09 -0400445 return context->diagnostic()
446 << "Expected <opcode> or <result-id> at the beginning "
447 "of an instruction, found '"
448 << result_id << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400449 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400450 result_id_position = context->position();
Lei Zhang06efdc52015-09-10 14:00:00 -0400451
452 // The '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400453 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400454 if (context->advance())
455 return context->diagnostic() << "Expected '=', found end of stream.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400456 std::string equal_sign;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400457 error = context->getWord(equal_sign, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400458 if ("=" != equal_sign)
459 return context->diagnostic() << "'=' expected after result id.";
Lei Zhangdfc50082015-08-21 11:50:55 -0400460
461 // The <opcode> after the '=' sign.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400462 context->setPosition(nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400463 if (context->advance())
464 return context->diagnostic() << "Expected opcode, found end of stream.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400465 error = context->getWord(opcodeName, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400466 if (error)
467 return context->diagnostic(error) << "Internal Error";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400468 if (!context->startsWithOp()) {
David Netoac508b02015-10-09 15:48:09 -0400469 return context->diagnostic() << "Invalid Opcode prefix '" << opcodeName
470 << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400471 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400472 }
473
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100474 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400475 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100476
477 spv_opcode_desc opcodeEntry;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400478 error = grammar.lookupOpcode(pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400479 if (error) {
David Netoac508b02015-10-09 15:48:09 -0400480 return context->diagnostic(error) << "Invalid Opcode name '"
481 << context->getWord() << "'";
Lei Zhang40056702015-09-11 14:31:27 -0400482 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400483 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
484 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400485 if (opcodeEntry->hasResult && result_id.empty()) {
David Netoac508b02015-10-09 15:48:09 -0400486 return context->diagnostic()
487 << "Expected <result-id> at the beginning of an "
488 "instruction, found '"
489 << firstWord << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400490 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400491 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100492 pInst->opcode = opcodeEntry->opcode;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400493 context->setPosition(nextPosition);
David Netob5dc8fc2015-10-06 16:22:00 -0400494 // Reserve the first word for the instruction.
495 spvInstructionAddWord(pInst, 0);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100496
David Neto78c3b432015-08-27 13:03:52 -0400497 // Maintains the ordered list of expected operand types.
498 // For many instructions we only need the {numTypes, operandTypes}
499 // entries in opcodeEntry. However, sometimes we need to modify
500 // the list as we parse the operands. This occurs when an operand
501 // has its own logical operands (such as the LocalSize operand for
502 // ExecutionMode), or for extended instructions that may have their
503 // own operands depending on the selected extended instruction.
504 spv_operand_pattern_t expectedOperands(
505 opcodeEntry->operandTypes,
506 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400507
David Neto78c3b432015-08-27 13:03:52 -0400508 while (!expectedOperands.empty()) {
509 const spv_operand_type_t type = expectedOperands.front();
510 expectedOperands.pop_front();
511
512 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400513 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400514
515 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
516 // Handle the <result-id> for value generating instructions.
517 // We've already consumed it from the text stream. Here
518 // we inject its words into the instruction.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400519 spv_position_t temp_pos = context->position();
520 error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
521 result_id.c_str(), pInst, nullptr);
522 result_id_position = context->position();
523 // Because we are injecting we have to reset the position afterwards.
524 context->setPosition(temp_pos);
Lei Zhang40056702015-09-11 14:31:27 -0400525 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100526 } else {
David Neto78c3b432015-08-27 13:03:52 -0400527 // Find the next word.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400528 error = context->advance();
David Neto78c3b432015-08-27 13:03:52 -0400529 if (error == SPV_END_OF_STREAM) {
530 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400531 // This would have been the last potential operand for the
532 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400533 // and we didn't find one. We're finished parsing this instruction.
534 break;
535 } else {
David Netoac508b02015-10-09 15:48:09 -0400536 return context->diagnostic()
537 << "Expected operand, found end of stream.";
David Neto78c3b432015-08-27 13:03:52 -0400538 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100539 }
David Neto78c3b432015-08-27 13:03:52 -0400540 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
541
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400542 if (context->isStartOfNewInst()) {
David Neto78c3b432015-08-27 13:03:52 -0400543 if (spvOperandIsOptional(type)) {
544 break;
545 } else {
David Netoac508b02015-10-09 15:48:09 -0400546 return context->diagnostic()
547 << "Expected operand, found next instruction instead.";
David Neto78c3b432015-08-27 13:03:52 -0400548 }
549 }
550
551 std::string operandValue;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400552 error = context->getWord(operandValue, &nextPosition);
David Netoac508b02015-10-09 15:48:09 -0400553 if (error) return context->diagnostic(error) << "Internal Error";
David Neto78c3b432015-08-27 13:03:52 -0400554
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400555 error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
556 pInst, &expectedOperands);
David Neto78c3b432015-08-27 13:03:52 -0400557
558 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
559 return SPV_SUCCESS;
560
Lei Zhang40056702015-09-11 14:31:27 -0400561 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400562
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400563 context->setPosition(nextPosition);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100564 }
565 }
566
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400567 if (spvOpcodeGeneratesType(pInst->opcode)) {
568 if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
569 return SPV_ERROR_INVALID_TEXT;
570 }
571 } else if (opcodeEntry->hasType) {
572 // SPIR-V dictates that if an instruction has both a return value and a
573 // type ID then the type id is first, and the return value is second.
574 assert(opcodeEntry->hasResult &&
575 "Unknown opcode: has a type but no result.");
576 context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
577 }
578
David Netob5dc8fc2015-10-06 16:22:00 -0400579 if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Netoac508b02015-10-09 15:48:09 -0400580 return context->diagnostic()
581 << "Instruction too long: " << pInst->words.size()
582 << " words, but the limit is "
583 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
David Netob5dc8fc2015-10-06 16:22:00 -0400584 }
585
586 pInst->words[0] = spvOpcodeMake(pInst->words.size(), opcodeEntry->opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100587
588 return SPV_SUCCESS;
589}
590
David Netoc9786432015-09-01 18:05:14 -0400591namespace {
592
593// Translates a given assembly language module into binary form.
594// If a diagnostic is generated, it is not yet marked as being
595// for a text-based input.
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400596spv_result_t spvTextToBinaryInternal(const libspirv::AssemblyGrammar& grammar,
597 const spv_text text,
598 spv_assembly_syntax_format_t format,
599 spv_binary* pBinary,
600 spv_diagnostic* pDiagnostic) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400601 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
602 libspirv::AssemblyContext context(text, pDiagnostic);
David Netoac508b02015-10-09 15:48:09 -0400603 if (!text->str || !text->length)
604 return context.diagnostic() << "Text stream is empty.";
605
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400606 if (!grammar.isValid()) {
Lei Zhang40056702015-09-11 14:31:27 -0400607 return SPV_ERROR_INVALID_TABLE;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400608 }
Lei Zhang40056702015-09-11 14:31:27 -0400609 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100610
611 // NOTE: Ensure diagnostic is zero initialised
612 *pDiagnostic = {};
613
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100614 std::vector<spv_instruction_t> instructions;
615
David Netoac508b02015-10-09 15:48:09 -0400616 if (context.advance()) return context.diagnostic() << "Text stream is empty.";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100617
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400618 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
619 while (context.hasText()) {
David Netob5dc8fc2015-10-06 16:22:00 -0400620 instructions.push_back({});
621 spv_instruction_t& inst = instructions.back();
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400622 inst.extInstType = extInstType;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100623
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400624 if (spvTextEncodeOpcode(grammar, &context, format, &inst)) {
625 return SPV_ERROR_INVALID_TEXT;
Lei Zhang40056702015-09-11 14:31:27 -0400626 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400627 extInstType = inst.extInstType;
628
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400629 if (context.advance()) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100630 }
631
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100632 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400633 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400634 totalSize += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100635 }
636
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400637 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400638 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100639 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400640 for (auto& inst : instructions) {
David Netob5dc8fc2015-10-06 16:22:00 -0400641 memcpy(data + currentIndex, inst.words.data(), sizeof(uint32_t) * inst.words.size());
642 currentIndex += inst.words.size();
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100643 }
644
645 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400646 if (!binary) {
647 delete[] data;
648 return SPV_ERROR_OUT_OF_MEMORY;
649 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100650 binary->code = data;
651 binary->wordCount = totalSize;
652
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400653 spv_result_t error = spvBinaryHeaderSet(binary, context.getBound());
Lei Zhang40056702015-09-11 14:31:27 -0400654 if (error) {
655 spvBinaryDestroy(binary);
656 return error;
657 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100658
659 *pBinary = binary;
660
661 return SPV_SUCCESS;
662}
663
Lei Zhange78a7c12015-09-10 17:07:21 -0400664} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400665
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400666spv_result_t spvTextToBinary(const char* input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400667 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400668 const spv_opcode_table opcodeTable,
669 const spv_operand_table operandTable,
670 const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400671 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400672 return spvTextWithFormatToBinary(
673 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
674 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
675}
676
677spv_result_t spvTextWithFormatToBinary(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400678 const char* input_text, const uint64_t input_text_size,
Lei Zhang06efdc52015-09-10 14:00:00 -0400679 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
680 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400681 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400682 spv_text_t text = {input_text, input_text_size};
683
Dejan Mircevski14c4b102015-09-29 17:07:21 -0400684 libspirv::AssemblyGrammar grammar(operandTable, opcodeTable, extInstTable);
Lei Zhang06efdc52015-09-10 14:00:00 -0400685 spv_result_t result =
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400686 spvTextToBinaryInternal(grammar, &text, format, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400687 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
688
689 return result;
690}
691
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100692void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400693 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100694 if (text->str) {
695 delete[] text->str;
696 }
697 delete text;
698}