blob: 5776b1beb143e98648cfb16bfce2a51e1e00bfa1 [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
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040027#include <algorithm>
28#include <cassert>
29#include <cstdio>
30#include <cstdlib>
31#include <string>
32
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010033#include <libspirv/libspirv.h>
Lei Zhang610c5252015-09-09 10:36:48 -040034
Lei Zhang4e092d32015-09-11 13:45:18 -040035#include "bitwisecast.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010036#include "binary.h"
37#include "diagnostic.h"
38#include "ext_inst.h"
39#include "opcode.h"
40#include "operand.h"
41#include "text.h"
42
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010043#include <string>
44#include <vector>
45#include <unordered_map>
46
Lei Zhang610c5252015-09-09 10:36:48 -040047using spvutils::BitwiseCast;
48
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010049// Structures
50
51struct spv_named_id_table_t {
52 std::unordered_map<std::string, uint32_t> namedIds;
53};
54
55// Text API
56
David Netoa48678a2015-09-11 12:04:03 -040057std::string spvGetWord(const char *str) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010058 size_t index = 0;
59 while (true) {
60 switch (str[index]) {
61 case '\0':
62 case '\t':
David Netoa48678a2015-09-11 12:04:03 -040063 case '\v':
64 case '\r':
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010065 case '\n':
66 case ' ':
David Netoa48678a2015-09-11 12:04:03 -040067 return std::string(str, str + index);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010068 default:
69 index++;
70 }
71 }
David Netoa48678a2015-09-11 12:04:03 -040072 assert(0 && "Unreachable");
Lei Zhang40056702015-09-11 14:31:27 -040073 return ""; // Make certain compilers happy.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010074}
75
76spv_named_id_table spvNamedIdTableCreate() {
77 return new spv_named_id_table_t();
78}
79
80void spvNamedIdTableDestory(spv_named_id_table table) { delete table; }
81
82uint32_t spvNamedIdAssignOrGet(spv_named_id_table table, const char *textValue,
83 uint32_t *pBound) {
84 if (table->namedIds.end() == table->namedIds.find(textValue)) {
85 table->namedIds[textValue] = *pBound;
86 }
87 return table->namedIds[textValue];
88}
89
90int32_t spvTextIsNamedId(const char *textValue) {
91 // TODO: Strengthen the parsing of textValue to only include allow names that
92 // match: ([a-z]|[A-Z])(_|[a-z]|[A-Z]|[0-9])*
93 switch (textValue[0]) {
94 case '0':
95 case '1':
96 case '2':
97 case '3':
98 case '4':
99 case '5':
100 case '6':
101 case '7':
102 case '8':
103 case '9':
104 return false;
105 default:
106 break;
107 }
108 return true;
109}
110
111spv_result_t spvTextAdvanceLine(const spv_text text, spv_position position) {
112 while (true) {
113 switch (text->str[position->index]) {
114 case '\0':
115 return SPV_END_OF_STREAM;
116 case '\n':
117 position->column = 0;
118 position->line++;
119 position->index++;
120 return SPV_SUCCESS;
121 default:
Lei Zhangee87cc22015-08-21 11:51:28 -0400122 position->column++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100123 position->index++;
124 break;
125 }
126 }
127}
128
129spv_result_t spvTextAdvance(const spv_text text, spv_position position) {
130 // NOTE: Consume white space, otherwise don't advance.
131 switch (text->str[position->index]) {
132 case '\0':
133 return SPV_END_OF_STREAM;
134 case ';':
Lei Zhangee87cc22015-08-21 11:51:28 -0400135 if (spv_result_t error = spvTextAdvanceLine(text, position)) return error;
136 return spvTextAdvance(text, position);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100137 case ' ':
138 case '\t':
139 position->column++;
140 position->index++;
141 return spvTextAdvance(text, position);
142 case '\n':
143 position->column = 0;
144 position->line++;
145 position->index++;
146 return spvTextAdvance(text, position);
147 default:
148 break;
149 }
150
151 return SPV_SUCCESS;
152}
153
154spv_result_t spvTextWordGet(const spv_text text,
155 const spv_position startPosition, std::string &word,
156 spv_position endPosition) {
Lei Zhang40056702015-09-11 14:31:27 -0400157 if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
158 if (!startPosition || !endPosition) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100159
160 *endPosition = *startPosition;
161
David Netoe7ee4c42015-08-25 14:21:13 -0400162 bool quoting = false;
163 bool escaping = false;
164
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100165 // NOTE: Assumes first character is not white space!
166 while (true) {
David Netoe7ee4c42015-08-25 14:21:13 -0400167 const char ch = text->str[endPosition->index];
168 if (ch == '\\')
169 escaping = !escaping;
170 else {
171 switch (ch) {
172 case '"':
173 if (!escaping) quoting = !quoting;
174 break;
175 case ' ':
176 case ';':
177 case '\t':
178 case '\n':
179 if (escaping || quoting) break;
Lei Zhange78a7c12015-09-10 17:07:21 -0400180 // Fall through.
David Netoe7ee4c42015-08-25 14:21:13 -0400181 case '\0': { // NOTE: End of word found!
182 word.assign(text->str + startPosition->index,
183 (size_t)(endPosition->index - startPosition->index));
184 return SPV_SUCCESS;
185 }
186 default:
187 break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100188 }
David Netoe7ee4c42015-08-25 14:21:13 -0400189 escaping = false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100190 }
191
192 endPosition->column++;
193 endPosition->index++;
194 }
195}
196
David Netoc9786432015-09-01 18:05:14 -0400197namespace {
198
Lei Zhangdfc50082015-08-21 11:50:55 -0400199// Returns true if the string at the given position in text starts with "Op".
David Netoc9786432015-09-01 18:05:14 -0400200bool spvStartsWithOp(const spv_text text, const spv_position position) {
David Neto78c3b432015-08-27 13:03:52 -0400201 if (text->length < position->index + 3) return false;
202 char ch0 = text->str[position->index];
203 char ch1 = text->str[position->index + 1];
204 char ch2 = text->str[position->index + 2];
205 return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
Lei Zhangdfc50082015-08-21 11:50:55 -0400206}
207
Lei Zhange78a7c12015-09-10 17:07:21 -0400208} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400209
Lei Zhangdfc50082015-08-21 11:50:55 -0400210// Returns true if a new instruction begins at the given position in text.
Lei Zhange78a7c12015-09-10 17:07:21 -0400211bool spvTextIsStartOfNewInst(const spv_text text, const spv_position position) {
Lei Zhangdfc50082015-08-21 11:50:55 -0400212 spv_position_t nextPosition = *position;
213 if (spvTextAdvance(text, &nextPosition)) return false;
David Neto78c3b432015-08-27 13:03:52 -0400214 if (spvStartsWithOp(text, &nextPosition)) return true;
Lei Zhangdfc50082015-08-21 11:50:55 -0400215
216 std::string word;
David Neto78c3b432015-08-27 13:03:52 -0400217 spv_position_t startPosition = *position;
Lei Zhangdfc50082015-08-21 11:50:55 -0400218 if (spvTextWordGet(text, &startPosition, word, &nextPosition)) return false;
219 if ('%' != word.front()) return false;
220
221 if (spvTextAdvance(text, &nextPosition)) return false;
222 startPosition = nextPosition;
223 if (spvTextWordGet(text, &startPosition, word, &nextPosition)) return false;
224 if ("=" != word) return false;
225
226 if (spvTextAdvance(text, &nextPosition)) return false;
227 startPosition = nextPosition;
228 if (spvStartsWithOp(text, &startPosition)) return true;
229 return false;
230}
231
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100232spv_result_t spvTextStringGet(const spv_text text,
233 const spv_position startPosition,
234 std::string &string, spv_position endPosition) {
Lei Zhang40056702015-09-11 14:31:27 -0400235 if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
236 if (!startPosition || !endPosition) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100237
Lei Zhang40056702015-09-11 14:31:27 -0400238 if ('"' != text->str[startPosition->index]) return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100239
240 *endPosition = *startPosition;
241
242 // NOTE: Assumes first character is not white space
243 while (true) {
244 endPosition->column++;
245 endPosition->index++;
246
247 switch (text->str[endPosition->index]) {
248 case '"': {
249 endPosition->column++;
250 endPosition->index++;
251
252 string.assign(text->str + startPosition->index,
253 (size_t)(endPosition->index - startPosition->index));
254
255 return SPV_SUCCESS;
256 }
257 case '\n':
258 case '\0':
259 return SPV_ERROR_INVALID_TEXT;
260 default:
261 break;
262 }
263 }
264}
265
266spv_result_t spvTextToUInt32(const char *textValue, uint32_t *pValue) {
267 char *endPtr = nullptr;
268 *pValue = strtoul(textValue, &endPtr, 0);
269 if (0 == *pValue && textValue == endPtr) {
270 return SPV_ERROR_INVALID_TEXT;
271 }
272 return SPV_SUCCESS;
273}
274
275spv_result_t spvTextToLiteral(const char *textValue, spv_literal_t *pLiteral) {
276 bool isSigned = false;
David Netoaffa6962015-08-24 15:33:14 -0400277 int numPeriods = 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100278 bool isString = false;
279
David Netoaffa6962015-08-24 15:33:14 -0400280 const size_t len = strlen(textValue);
David Neto98290a22015-08-24 16:27:02 -0400281 if (len == 0) return SPV_FAILED_MATCH;
282
David Netoaffa6962015-08-24 15:33:14 -0400283 for (uint64_t index = 0; index < len; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100284 switch (textValue[index]) {
285 case '0':
286 case '1':
287 case '2':
288 case '3':
289 case '4':
290 case '5':
291 case '6':
292 case '7':
293 case '8':
294 case '9':
295 break;
296 case '.':
David Netoaffa6962015-08-24 15:33:14 -0400297 numPeriods++;
298 break;
299 case '-':
300 if (index == 0) {
301 isSigned = true;
302 } else {
303 isString = true;
304 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100305 break;
306 default:
307 isString = true;
Lei Zhange78a7c12015-09-10 17:07:21 -0400308 index = len; // break out of the loop too.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100309 break;
310 }
311 }
312
David Netoaffa6962015-08-24 15:33:14 -0400313 pLiteral->type = spv_literal_type_t(99);
314
Lei Zhange78a7c12015-09-10 17:07:21 -0400315 if (isString || numPeriods > 1 || (isSigned && len == 1)) {
David Netoaffa6962015-08-24 15:33:14 -0400316 // TODO(dneto): Allow escaping.
David Neto98290a22015-08-24 16:27:02 -0400317 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
318 return SPV_FAILED_MATCH;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100319 pLiteral->type = SPV_LITERAL_TYPE_STRING;
David Netoaffa6962015-08-24 15:33:14 -0400320 // Need room for the null-terminator.
David Neto98290a22015-08-24 16:27:02 -0400321 if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY;
Lei Zhange78a7c12015-09-10 17:07:21 -0400322 strncpy(pLiteral->value.str, textValue + 1, len - 2);
323 pLiteral->value.str[len - 2] = 0;
David Netoaffa6962015-08-24 15:33:14 -0400324 } else if (numPeriods == 1) {
325 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100326 float f = (float)d;
327 if (d == (double)f) {
328 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
329 pLiteral->value.f = f;
330 } else {
331 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
332 pLiteral->value.d = d;
333 }
334 } else if (isSigned) {
335 int64_t i64 = strtoll(textValue, nullptr, 10);
336 int32_t i32 = (int32_t)i64;
337 if (i64 == (int64_t)i32) {
338 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
339 pLiteral->value.i32 = i32;
340 } else {
341 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
342 pLiteral->value.i64 = i64;
343 }
344 } else {
345 uint64_t u64 = strtoull(textValue, nullptr, 10);
346 uint32_t u32 = (uint32_t)u64;
347 if (u64 == (uint64_t)u32) {
348 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
349 pLiteral->value.u32 = u32;
350 } else {
351 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
352 pLiteral->value.u64 = u64;
353 }
354 }
355
356 return SPV_SUCCESS;
357}
358
Dejan Mircevskiba569fb2015-09-11 16:34:49 -0400359namespace {
360
361// True if type is an ID type, false otherwise.
362bool isIdType(spv_operand_type_t type) {
363 switch (type) {
364 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
365 case SPV_OPERAND_TYPE_ID:
366 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
367 case SPV_OPERAND_TYPE_OPTIONAL_ID:
368 case SPV_OPERAND_TYPE_RESULT_ID:
369 return true;
370 default:
371 return false;
372 }
373 return false;
374}
375
376} // anonymous namespace
377
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100378spv_result_t spvTextEncodeOperand(
379 const spv_operand_type_t type, const char *textValue,
380 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
381 spv_named_id_table namedIdTable, spv_instruction_t *pInst,
Lei Zhange78a7c12015-09-10 17:07:21 -0400382 spv_operand_pattern_t *pExpectedOperands, uint32_t *pBound,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100383 const spv_position position, spv_diagnostic *pDiagnostic) {
384 // NOTE: Handle immediate int in the stream
385 if ('!' == textValue[0]) {
386 const char *begin = textValue + 1;
387 char *end = nullptr;
388 uint32_t immediateInt = strtoul(begin, &end, 0);
389 size_t size = strlen(textValue);
390 size_t length = (end - begin);
Lei Zhang40056702015-09-11 14:31:27 -0400391 if (size - 1 != length) {
392 DIAGNOSTIC << "Invalid immediate integer '" << textValue << "'.";
393 return SPV_ERROR_INVALID_TEXT;
394 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100395 position->column += size;
396 position->index += size;
397 pInst->words[pInst->wordCount] = immediateInt;
398 pInst->wordCount += 1;
Dejan Mircevskiba569fb2015-09-11 16:34:49 -0400399 if (isIdType(type)) *pBound = std::max(*pBound, immediateInt + 1);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100400 return SPV_SUCCESS;
401 }
402
403 switch (type) {
David Netoe3f70b92015-08-27 13:50:05 -0400404 case SPV_OPERAND_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400405 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
406 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netofadbf622015-09-14 17:07:11 -0400407 case SPV_OPERAND_TYPE_RESULT_ID:
408 case SPV_OPERAND_TYPE_EXECUTION_SCOPE: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400409 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100410 textValue++;
411 }
Lei Zhangabafd5e2015-08-21 11:52:29 -0400412 // TODO: Force all ID's to be prefixed with '%'.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100413 uint32_t id = 0;
414 if (spvTextIsNamedId(textValue)) {
415 id = spvNamedIdAssignOrGet(namedIdTable, textValue, pBound);
416 } else {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400417 if (spvTextToUInt32(textValue, &id) != SPV_SUCCESS) {
418 if (spvOperandIsOptional(type)) {
419 return SPV_FAILED_MATCH;
420 } else {
David Netofadbf622015-09-14 17:07:11 -0400421 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '"
422 << textValue << "'.";
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400423 return SPV_ERROR_INVALID_TEXT;
424 }
425 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100426 }
427 pInst->words[pInst->wordCount++] = id;
Dejan Mircevskiba569fb2015-09-11 16:34:49 -0400428 *pBound = std::max(*pBound, id + 1);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100429 } break;
430 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
431 // NOTE: Special case for extension instruction lookup
432 if (OpExtInst == pInst->opcode) {
433 spv_ext_inst_desc extInst;
Lei Zhang40056702015-09-11 14:31:27 -0400434 if (spvExtInstTableNameLookup(extInstTable, pInst->extInstType,
435 textValue, &extInst)) {
436 DIAGNOSTIC << "Invalid extended instruction name '" << textValue
437 << "'.";
438 return SPV_ERROR_INVALID_TEXT;
439 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100440 pInst->words[pInst->wordCount++] = extInst->ext_inst;
David Neto78c3b432015-08-27 13:03:52 -0400441
442 // Prepare to parse the operands for the extended instructions.
443 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
444
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100445 return SPV_SUCCESS;
446 }
447
448 // TODO: Literal numbers can be any number up to 64 bits wide. This
449 // includes integers and floating point numbers.
David Neto78c3b432015-08-27 13:03:52 -0400450 // TODO(dneto): Suggest using spvTextToLiteral and looking for an
451 // appropriate result type.
Lei Zhang40056702015-09-11 14:31:27 -0400452 if (spvTextToUInt32(textValue, &pInst->words[pInst->wordCount++])) {
453 DIAGNOSTIC << "Invalid literal number '" << textValue << "'.";
454 return SPV_ERROR_INVALID_TEXT;
455 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100456 } break;
Lei Zhangb41d1502015-09-14 15:22:23 -0400457 // TODO(antiagainst): the handling of literal numbers in this function need
458 // to be reorganized.
459 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
David Neto78c3b432015-08-27 13:03:52 -0400460 case SPV_OPERAND_TYPE_LITERAL:
461 case SPV_OPERAND_TYPE_LITERAL_IN_OPTIONAL_TUPLE:
462 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100463 spv_literal_t literal = {};
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400464 if (spvTextToLiteral(textValue, &literal) != SPV_SUCCESS) {
465 if (spvOperandIsOptional(type)) {
466 return SPV_FAILED_MATCH;
467 } else {
468 DIAGNOSTIC << "Invalid literal '" << textValue << "'.";
469 return SPV_ERROR_INVALID_TEXT;
470 }
471 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100472 switch (literal.type) {
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400473 // We do not have to print diagnostics here because spvBinaryEncode*
474 // prints diagnostic messages on failure.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100475 case SPV_LITERAL_TYPE_INT_32:
Lei Zhang40056702015-09-11 14:31:27 -0400476 if (spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32),
477 pInst, position, pDiagnostic))
478 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100479 break;
480 case SPV_LITERAL_TYPE_INT_64: {
Lei Zhang40056702015-09-11 14:31:27 -0400481 if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64),
482 pInst, position, pDiagnostic))
483 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100484 } break;
485 case SPV_LITERAL_TYPE_UINT_32: {
Lei Zhang40056702015-09-11 14:31:27 -0400486 if (spvBinaryEncodeU32(literal.value.u32, pInst, position,
487 pDiagnostic))
488 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100489 } break;
490 case SPV_LITERAL_TYPE_UINT_64: {
Lei Zhang40056702015-09-11 14:31:27 -0400491 if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64),
492 pInst, position, pDiagnostic))
493 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100494 } break;
495 case SPV_LITERAL_TYPE_FLOAT_32: {
Lei Zhang40056702015-09-11 14:31:27 -0400496 if (spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f), pInst,
497 position, pDiagnostic))
498 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100499 } break;
500 case SPV_LITERAL_TYPE_FLOAT_64: {
Lei Zhang40056702015-09-11 14:31:27 -0400501 if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d), pInst,
502 position, pDiagnostic))
503 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100504 } break;
505 case SPV_LITERAL_TYPE_STRING: {
Lei Zhang40056702015-09-11 14:31:27 -0400506 if (spvBinaryEncodeString(literal.value.str, pInst, position,
507 pDiagnostic))
508 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100509 } break;
510 default:
511 DIAGNOSTIC << "Invalid literal '" << textValue << "'";
512 return SPV_ERROR_INVALID_TEXT;
513 }
514 } break;
David Neto78c3b432015-08-27 13:03:52 -0400515 case SPV_OPERAND_TYPE_LITERAL_STRING:
516 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100517 size_t len = strlen(textValue);
Lei Zhang40056702015-09-11 14:31:27 -0400518 if ('"' != textValue[0] && '"' != textValue[len - 1]) {
519 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
520 DIAGNOSTIC << "Invalid literal string '" << textValue
521 << "', expected quotes.";
522 return SPV_ERROR_INVALID_TEXT;
523 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100524 // NOTE: Strip quotes
525 std::string text(textValue + 1, len - 2);
526
527 // NOTE: Special case for extended instruction library import
528 if (OpExtInstImport == pInst->opcode) {
529 pInst->extInstType = spvExtInstImportTypeGet(text.c_str());
530 }
531
Lei Zhang40056702015-09-11 14:31:27 -0400532 if (spvBinaryEncodeString(text.c_str(), pInst, position, pDiagnostic))
533 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100534 } break;
David Neto78c3b432015-08-27 13:03:52 -0400535 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
536 assert(0 && " Handle optional optional image operands");
537 break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100538 default: {
539 // NOTE: All non literal operands are handled here using the operand
540 // table.
541 spv_operand_desc entry;
David Neto388c40d2015-09-16 16:42:56 -0400542 if (spvOperandTableNameLookup(operandTable, type, textValue,
543 strlen(textValue), &entry)) {
Lei Zhang40056702015-09-11 14:31:27 -0400544 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
545 << "'.";
546 return SPV_ERROR_INVALID_TEXT;
547 }
548 if (spvBinaryEncodeU32(entry->value, pInst, position, pDiagnostic)) {
549 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
550 << "'.";
551 return SPV_ERROR_INVALID_TEXT;
552 }
David Neto78c3b432015-08-27 13:03:52 -0400553
554 // Prepare to parse the operands for this logical operand.
555 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100556 } break;
557 }
558 return SPV_SUCCESS;
559}
560
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400561namespace {
562
563/// Encodes an instruction started by !<integer> at the given position in text.
564///
565/// Puts the encoded words into *pInst. If successful, moves position past the
566/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
567/// leaves position pointing to the error in text.
568spv_result_t encodeInstructionStartingWithImmediate(
569 const spv_text text, const spv_operand_table operandTable,
570 const spv_ext_inst_table extInstTable, spv_named_id_table namedIdTable,
571 uint32_t *pBound, spv_instruction_t *pInst, spv_position position,
572 spv_diagnostic *pDiagnostic) {
573 std::string firstWord;
574 spv_position_t nextPosition = {};
575 auto error = spvTextWordGet(text, position, firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400576 if (error) {
577 DIAGNOSTIC << "Internal Error";
578 return error;
579 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400580
581 assert(firstWord[0] == '!');
582 const char *begin = firstWord.data() + 1;
583 char *end = nullptr;
584 uint32_t immediateInt = strtoul(begin, &end, 0);
Lei Zhang40056702015-09-11 14:31:27 -0400585 if ((begin + firstWord.size() - 1) != end) {
586 DIAGNOSTIC << "Invalid immediate integer '" << firstWord << "'.";
587 return SPV_ERROR_INVALID_TEXT;
588 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400589 position->column += firstWord.size();
590 position->index += firstWord.size();
591 pInst->words[0] = immediateInt;
592 pInst->wordCount = 1;
593 while (spvTextAdvance(text, position) != SPV_END_OF_STREAM) {
594 // A beginning of a new instruction means we're done.
595 if (spvTextIsStartOfNewInst(text, position)) return SPV_SUCCESS;
596
597 // Otherwise, there must be an operand that's either a literal, an ID, or
598 // an immediate.
599 std::string operandValue;
600 if ((error = spvTextWordGet(text, position, operandValue, &nextPosition))) {
601 DIAGNOSTIC << "Internal Error";
602 return error;
603 }
604
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400605 if (operandValue == "=") {
606 DIAGNOSTIC << firstWord << " not allowed before =.";
607 return SPV_ERROR_INVALID_TEXT;
608 }
609
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400610 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
611 // expanded.
612 spv_operand_pattern_t dummyExpectedOperands;
613 error = spvTextEncodeOperand(
614 SPV_OPERAND_TYPE_OPTIONAL_LITERAL, operandValue.c_str(), operandTable,
615 extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound,
616 position, pDiagnostic);
617 if (error == SPV_FAILED_MATCH) {
618 // It's not a literal -- is it an ID?
619 error = spvTextEncodeOperand(
620 SPV_OPERAND_TYPE_OPTIONAL_ID, operandValue.c_str(), operandTable,
621 extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound,
622 position, pDiagnostic);
623 if (error) {
624 DIAGNOSTIC << "Invalid word following " << firstWord << ": "
625 << operandValue;
626 }
627 }
Lei Zhang40056702015-09-11 14:31:27 -0400628 if (error) return error;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400629 *position = nextPosition;
630 }
631 return SPV_SUCCESS;
632}
633
634} // anonymous namespace
635
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100636spv_result_t spvTextEncodeOpcode(
Lei Zhang06efdc52015-09-10 14:00:00 -0400637 const spv_text text, spv_assembly_syntax_format_t format,
638 const spv_opcode_table opcodeTable, const spv_operand_table operandTable,
639 const spv_ext_inst_table extInstTable, spv_named_id_table namedIdTable,
640 uint32_t *pBound, spv_instruction_t *pInst, spv_position position,
641 spv_diagnostic *pDiagnostic) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400642 // Check for !<integer> first.
643 if ('!' == text->str[position->index]) {
644 return encodeInstructionStartingWithImmediate(
645 text, operandTable, extInstTable, namedIdTable, pBound, pInst, position,
646 pDiagnostic);
647 }
648
Lei Zhangdfc50082015-08-21 11:50:55 -0400649 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400650 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
651 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400652
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400653 if ('!' == text->str[position->index]) {
654 return encodeInstructionStartingWithImmediate(
655 text, operandTable, extInstTable, namedIdTable, pBound, pInst, position,
656 pDiagnostic);
657 }
658
Lei Zhang06efdc52015-09-10 14:00:00 -0400659 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100660 spv_position_t nextPosition = {};
Lei Zhang06efdc52015-09-10 14:00:00 -0400661 spv_result_t error = spvTextWordGet(text, position, firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400662 if (error) {
663 DIAGNOSTIC << "Internal Error";
664 return error;
665 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100666
Lei Zhang06efdc52015-09-10 14:00:00 -0400667 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400668 std::string result_id;
669 spv_position_t result_id_position = {};
Lei Zhang06efdc52015-09-10 14:00:00 -0400670 if (spvStartsWithOp(text, position)) {
671 opcodeName = firstWord;
672 } else {
673 // If the first word of this instruction is not an opcode, we must be
674 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400675 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
676 DIAGNOSTIC
677 << "Expected <opcode> at the beginning of an instruction, found '"
678 << firstWord << "'.";
679 return SPV_ERROR_INVALID_TEXT;
680 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400681
682 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400683 if ('%' != result_id.front()) {
684 DIAGNOSTIC << "Expected <opcode> or <result-id> at the beginning "
685 "of an instruction, found '"
686 << result_id << "'.";
687 return SPV_ERROR_INVALID_TEXT;
688 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400689 result_id_position = *position;
Lei Zhang06efdc52015-09-10 14:00:00 -0400690
691 // The '=' sign.
Lei Zhangdfc50082015-08-21 11:50:55 -0400692 *position = nextPosition;
Lei Zhang40056702015-09-11 14:31:27 -0400693 if (spvTextAdvance(text, position)) {
694 DIAGNOSTIC << "Expected '=', found end of stream.";
695 return SPV_ERROR_INVALID_TEXT;
696 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400697 std::string equal_sign;
698 error = spvTextWordGet(text, position, equal_sign, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400699 if ("=" != equal_sign) {
700 DIAGNOSTIC << "'=' expected after result id.";
701 return SPV_ERROR_INVALID_TEXT;
702 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400703
704 // The <opcode> after the '=' sign.
705 *position = nextPosition;
Lei Zhang40056702015-09-11 14:31:27 -0400706 if (spvTextAdvance(text, position)) {
707 DIAGNOSTIC << "Expected opcode, found end of stream.";
708 return SPV_ERROR_INVALID_TEXT;
709 }
Lei Zhang977e9bc2015-08-21 11:52:03 -0400710 error = spvTextWordGet(text, position, opcodeName, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400711 if (error) {
712 DIAGNOSTIC << "Internal Error";
713 return error;
714 }
715 if (!spvStartsWithOp(text, position)) {
716 DIAGNOSTIC << "Invalid Opcode prefix '" << opcodeName << "'.";
717 return SPV_ERROR_INVALID_TEXT;
718 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400719 }
720
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100721 // NOTE: The table contains Opcode names without the "Op" prefix.
722 const char *pInstName = opcodeName.data() + 2;
723
724 spv_opcode_desc opcodeEntry;
Lei Zhangdfc50082015-08-21 11:50:55 -0400725 error = spvOpcodeTableNameLookup(opcodeTable, pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400726 if (error) {
727 DIAGNOSTIC << "Invalid Opcode name '"
728 << spvGetWord(text->str + position->index) << "'";
729 return error;
730 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400731 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
732 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400733 if (opcodeEntry->hasResult && result_id.empty()) {
734 DIAGNOSTIC << "Expected <result-id> at the beginning of an "
735 "instruction, found '"
736 << firstWord << "'.";
737 return SPV_ERROR_INVALID_TEXT;
738 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400739 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100740 pInst->opcode = opcodeEntry->opcode;
741 *position = nextPosition;
742 pInst->wordCount++;
743
David Neto78c3b432015-08-27 13:03:52 -0400744 // Maintains the ordered list of expected operand types.
745 // For many instructions we only need the {numTypes, operandTypes}
746 // entries in opcodeEntry. However, sometimes we need to modify
747 // the list as we parse the operands. This occurs when an operand
748 // has its own logical operands (such as the LocalSize operand for
749 // ExecutionMode), or for extended instructions that may have their
750 // own operands depending on the selected extended instruction.
751 spv_operand_pattern_t expectedOperands(
752 opcodeEntry->operandTypes,
753 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400754
David Neto78c3b432015-08-27 13:03:52 -0400755 while (!expectedOperands.empty()) {
756 const spv_operand_type_t type = expectedOperands.front();
757 expectedOperands.pop_front();
758
759 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400760 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400761
762 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
763 // Handle the <result-id> for value generating instructions.
764 // We've already consumed it from the text stream. Here
765 // we inject its words into the instruction.
Lei Zhange78a7c12015-09-10 17:07:21 -0400766 error = spvTextEncodeOperand(SPV_OPERAND_TYPE_RESULT_ID,
767 result_id.c_str(), operandTable,
768 extInstTable, namedIdTable, pInst, nullptr,
769 pBound, &result_id_position, pDiagnostic);
Lei Zhang40056702015-09-11 14:31:27 -0400770 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100771 } else {
David Neto78c3b432015-08-27 13:03:52 -0400772 // Find the next word.
773 error = spvTextAdvance(text, position);
774 if (error == SPV_END_OF_STREAM) {
775 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400776 // This would have been the last potential operand for the
777 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400778 // and we didn't find one. We're finished parsing this instruction.
779 break;
780 } else {
781 DIAGNOSTIC << "Expected operand, found end of stream.";
782 return SPV_ERROR_INVALID_TEXT;
783 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100784 }
David Neto78c3b432015-08-27 13:03:52 -0400785 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
786
787 if (spvTextIsStartOfNewInst(text, position)) {
788 if (spvOperandIsOptional(type)) {
789 break;
790 } else {
791 DIAGNOSTIC << "Expected operand, found next instruction instead.";
792 return SPV_ERROR_INVALID_TEXT;
793 }
794 }
795
796 std::string operandValue;
797 error = spvTextWordGet(text, position, operandValue, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400798 if (error) {
799 DIAGNOSTIC << "Internal Error";
800 return error;
801 }
David Neto78c3b432015-08-27 13:03:52 -0400802
803 error = spvTextEncodeOperand(
Lei Zhange78a7c12015-09-10 17:07:21 -0400804 type, operandValue.c_str(), operandTable, extInstTable, namedIdTable,
805 pInst, &expectedOperands, pBound, position, pDiagnostic);
David Neto78c3b432015-08-27 13:03:52 -0400806
807 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
808 return SPV_SUCCESS;
809
Lei Zhang40056702015-09-11 14:31:27 -0400810 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400811
812 *position = nextPosition;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100813 }
814 }
815
816 pInst->words[0] = spvOpcodeMake(pInst->wordCount, opcodeEntry->opcode);
817
818 return SPV_SUCCESS;
819}
820
David Netoc9786432015-09-01 18:05:14 -0400821namespace {
822
823// Translates a given assembly language module into binary form.
824// If a diagnostic is generated, it is not yet marked as being
825// for a text-based input.
826spv_result_t spvTextToBinaryInternal(const spv_text text,
Lei Zhang06efdc52015-09-10 14:00:00 -0400827 spv_assembly_syntax_format_t format,
David Netoc9786432015-09-01 18:05:14 -0400828 const spv_opcode_table opcodeTable,
829 const spv_operand_table operandTable,
830 const spv_ext_inst_table extInstTable,
831 spv_binary *pBinary,
832 spv_diagnostic *pDiagnostic) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100833 spv_position_t position = {};
Lei Zhang40056702015-09-11 14:31:27 -0400834 if (!text->str || !text->length) {
835 DIAGNOSTIC << "Text stream is empty.";
836 return SPV_ERROR_INVALID_TEXT;
837 }
838 if (!opcodeTable || !operandTable || !extInstTable)
839 return SPV_ERROR_INVALID_TABLE;
840 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
841 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100842
843 // NOTE: Ensure diagnostic is zero initialised
844 *pDiagnostic = {};
845
846 uint32_t bound = 1;
847
848 std::vector<spv_instruction_t> instructions;
849
Lei Zhang40056702015-09-11 14:31:27 -0400850 if (spvTextAdvance(text, &position)) {
851 DIAGNOSTIC << "Text stream is empty.";
852 return SPV_ERROR_INVALID_TEXT;
853 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100854
855 spv_named_id_table namedIdTable = spvNamedIdTableCreate();
Lei Zhang40056702015-09-11 14:31:27 -0400856 if (!namedIdTable) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100857
858 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
859 while (text->length > position.index) {
860 spv_instruction_t inst = {};
861 inst.extInstType = extInstType;
862
Lei Zhang40056702015-09-11 14:31:27 -0400863 if (spvTextEncodeOpcode(text, format, opcodeTable, operandTable,
864 extInstTable, namedIdTable, &bound, &inst,
865 &position, pDiagnostic)) {
866 spvNamedIdTableDestory(namedIdTable);
867 return SPV_ERROR_INVALID_TEXT;
868 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100869 extInstType = inst.extInstType;
870
871 instructions.push_back(inst);
872
Lei Zhang40056702015-09-11 14:31:27 -0400873 if (spvTextAdvance(text, &position)) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100874 }
875
876 spvNamedIdTableDestory(namedIdTable);
877
878 size_t totalSize = SPV_INDEX_INSTRUCTION;
879 for (auto &inst : instructions) {
880 totalSize += inst.wordCount;
881 }
882
883 uint32_t *data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400884 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100885 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
886 for (auto &inst : instructions) {
887 memcpy(data + currentIndex, inst.words, sizeof(uint32_t) * inst.wordCount);
888 currentIndex += inst.wordCount;
889 }
890
891 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400892 if (!binary) {
893 delete[] data;
894 return SPV_ERROR_OUT_OF_MEMORY;
895 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100896 binary->code = data;
897 binary->wordCount = totalSize;
898
899 spv_result_t error = spvBinaryHeaderSet(binary, bound);
Lei Zhang40056702015-09-11 14:31:27 -0400900 if (error) {
901 spvBinaryDestroy(binary);
902 return error;
903 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100904
905 *pBinary = binary;
906
907 return SPV_SUCCESS;
908}
909
Lei Zhange78a7c12015-09-10 17:07:21 -0400910} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400911
Lei Zhang06efdc52015-09-10 14:00:00 -0400912spv_result_t spvTextToBinary(const char *input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400913 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400914 const spv_opcode_table opcodeTable,
915 const spv_operand_table operandTable,
916 const spv_ext_inst_table extInstTable,
917 spv_binary *pBinary, spv_diagnostic *pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400918 return spvTextWithFormatToBinary(
919 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
920 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
921}
922
923spv_result_t spvTextWithFormatToBinary(
924 const char *input_text, const uint64_t input_text_size,
925 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
926 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
927 spv_binary *pBinary, spv_diagnostic *pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400928 spv_text_t text = {input_text, input_text_size};
929
Lei Zhang06efdc52015-09-10 14:00:00 -0400930 spv_result_t result =
931 spvTextToBinaryInternal(&text, format, opcodeTable, operandTable,
932 extInstTable, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400933 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
934
935 return result;
936}
937
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100938void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400939 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100940 if (text->str) {
941 delete[] text->str;
942 }
943 delete text;
944}