blob: 1810dc2e33aff2e70476b7ae8e3e150fc5368542 [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;
Lei Zhang40056702015-09-11 14:31:27 -0400542 if (spvOperandTableNameLookup(operandTable, type, textValue, &entry)) {
543 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
544 << "'.";
545 return SPV_ERROR_INVALID_TEXT;
546 }
547 if (spvBinaryEncodeU32(entry->value, pInst, position, pDiagnostic)) {
548 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
549 << "'.";
550 return SPV_ERROR_INVALID_TEXT;
551 }
David Neto78c3b432015-08-27 13:03:52 -0400552
553 // Prepare to parse the operands for this logical operand.
554 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100555 } break;
556 }
557 return SPV_SUCCESS;
558}
559
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400560namespace {
561
562/// Encodes an instruction started by !<integer> at the given position in text.
563///
564/// Puts the encoded words into *pInst. If successful, moves position past the
565/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
566/// leaves position pointing to the error in text.
567spv_result_t encodeInstructionStartingWithImmediate(
568 const spv_text text, const spv_operand_table operandTable,
569 const spv_ext_inst_table extInstTable, spv_named_id_table namedIdTable,
570 uint32_t *pBound, spv_instruction_t *pInst, spv_position position,
571 spv_diagnostic *pDiagnostic) {
572 std::string firstWord;
573 spv_position_t nextPosition = {};
574 auto error = spvTextWordGet(text, position, firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400575 if (error) {
576 DIAGNOSTIC << "Internal Error";
577 return error;
578 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400579
580 assert(firstWord[0] == '!');
581 const char *begin = firstWord.data() + 1;
582 char *end = nullptr;
583 uint32_t immediateInt = strtoul(begin, &end, 0);
Lei Zhang40056702015-09-11 14:31:27 -0400584 if ((begin + firstWord.size() - 1) != end) {
585 DIAGNOSTIC << "Invalid immediate integer '" << firstWord << "'.";
586 return SPV_ERROR_INVALID_TEXT;
587 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400588 position->column += firstWord.size();
589 position->index += firstWord.size();
590 pInst->words[0] = immediateInt;
591 pInst->wordCount = 1;
592 while (spvTextAdvance(text, position) != SPV_END_OF_STREAM) {
593 // A beginning of a new instruction means we're done.
594 if (spvTextIsStartOfNewInst(text, position)) return SPV_SUCCESS;
595
596 // Otherwise, there must be an operand that's either a literal, an ID, or
597 // an immediate.
598 std::string operandValue;
599 if ((error = spvTextWordGet(text, position, operandValue, &nextPosition))) {
600 DIAGNOSTIC << "Internal Error";
601 return error;
602 }
603
604 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
605 // expanded.
606 spv_operand_pattern_t dummyExpectedOperands;
607 error = spvTextEncodeOperand(
608 SPV_OPERAND_TYPE_OPTIONAL_LITERAL, operandValue.c_str(), operandTable,
609 extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound,
610 position, pDiagnostic);
611 if (error == SPV_FAILED_MATCH) {
612 // It's not a literal -- is it an ID?
613 error = spvTextEncodeOperand(
614 SPV_OPERAND_TYPE_OPTIONAL_ID, operandValue.c_str(), operandTable,
615 extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound,
616 position, pDiagnostic);
617 if (error) {
618 DIAGNOSTIC << "Invalid word following " << firstWord << ": "
619 << operandValue;
620 }
621 }
Lei Zhang40056702015-09-11 14:31:27 -0400622 if (error) return error;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400623 *position = nextPosition;
624 }
625 return SPV_SUCCESS;
626}
627
628} // anonymous namespace
629
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100630spv_result_t spvTextEncodeOpcode(
Lei Zhang06efdc52015-09-10 14:00:00 -0400631 const spv_text text, spv_assembly_syntax_format_t format,
632 const spv_opcode_table opcodeTable, const spv_operand_table operandTable,
633 const spv_ext_inst_table extInstTable, spv_named_id_table namedIdTable,
634 uint32_t *pBound, spv_instruction_t *pInst, spv_position position,
635 spv_diagnostic *pDiagnostic) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400636 // Check for !<integer> first.
637 if ('!' == text->str[position->index]) {
638 return encodeInstructionStartingWithImmediate(
639 text, operandTable, extInstTable, namedIdTable, pBound, pInst, position,
640 pDiagnostic);
641 }
642
Lei Zhangdfc50082015-08-21 11:50:55 -0400643 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400644 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
645 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400646
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400647 if ('!' == text->str[position->index]) {
648 return encodeInstructionStartingWithImmediate(
649 text, operandTable, extInstTable, namedIdTable, pBound, pInst, position,
650 pDiagnostic);
651 }
652
Lei Zhang06efdc52015-09-10 14:00:00 -0400653 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100654 spv_position_t nextPosition = {};
Lei Zhang06efdc52015-09-10 14:00:00 -0400655 spv_result_t error = spvTextWordGet(text, position, firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400656 if (error) {
657 DIAGNOSTIC << "Internal Error";
658 return error;
659 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100660
Lei Zhang06efdc52015-09-10 14:00:00 -0400661 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400662 std::string result_id;
663 spv_position_t result_id_position = {};
Lei Zhang06efdc52015-09-10 14:00:00 -0400664 if (spvStartsWithOp(text, position)) {
665 opcodeName = firstWord;
666 } else {
667 // If the first word of this instruction is not an opcode, we must be
668 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400669 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
670 DIAGNOSTIC
671 << "Expected <opcode> at the beginning of an instruction, found '"
672 << firstWord << "'.";
673 return SPV_ERROR_INVALID_TEXT;
674 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400675
676 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400677 if ('%' != result_id.front()) {
678 DIAGNOSTIC << "Expected <opcode> or <result-id> at the beginning "
679 "of an instruction, found '"
680 << result_id << "'.";
681 return SPV_ERROR_INVALID_TEXT;
682 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400683 result_id_position = *position;
Lei Zhang06efdc52015-09-10 14:00:00 -0400684
685 // The '=' sign.
Lei Zhangdfc50082015-08-21 11:50:55 -0400686 *position = nextPosition;
Lei Zhang40056702015-09-11 14:31:27 -0400687 if (spvTextAdvance(text, position)) {
688 DIAGNOSTIC << "Expected '=', found end of stream.";
689 return SPV_ERROR_INVALID_TEXT;
690 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400691 std::string equal_sign;
692 error = spvTextWordGet(text, position, equal_sign, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400693 if ("=" != equal_sign) {
694 DIAGNOSTIC << "'=' expected after result id.";
695 return SPV_ERROR_INVALID_TEXT;
696 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400697
698 // The <opcode> after the '=' sign.
699 *position = nextPosition;
Lei Zhang40056702015-09-11 14:31:27 -0400700 if (spvTextAdvance(text, position)) {
701 DIAGNOSTIC << "Expected opcode, found end of stream.";
702 return SPV_ERROR_INVALID_TEXT;
703 }
Lei Zhang977e9bc2015-08-21 11:52:03 -0400704 error = spvTextWordGet(text, position, opcodeName, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400705 if (error) {
706 DIAGNOSTIC << "Internal Error";
707 return error;
708 }
709 if (!spvStartsWithOp(text, position)) {
710 DIAGNOSTIC << "Invalid Opcode prefix '" << opcodeName << "'.";
711 return SPV_ERROR_INVALID_TEXT;
712 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400713 }
714
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100715 // NOTE: The table contains Opcode names without the "Op" prefix.
716 const char *pInstName = opcodeName.data() + 2;
717
718 spv_opcode_desc opcodeEntry;
Lei Zhangdfc50082015-08-21 11:50:55 -0400719 error = spvOpcodeTableNameLookup(opcodeTable, pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400720 if (error) {
721 DIAGNOSTIC << "Invalid Opcode name '"
722 << spvGetWord(text->str + position->index) << "'";
723 return error;
724 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400725 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
726 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400727 if (opcodeEntry->hasResult && result_id.empty()) {
728 DIAGNOSTIC << "Expected <result-id> at the beginning of an "
729 "instruction, found '"
730 << firstWord << "'.";
731 return SPV_ERROR_INVALID_TEXT;
732 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400733 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100734 pInst->opcode = opcodeEntry->opcode;
735 *position = nextPosition;
736 pInst->wordCount++;
737
David Neto78c3b432015-08-27 13:03:52 -0400738 // Maintains the ordered list of expected operand types.
739 // For many instructions we only need the {numTypes, operandTypes}
740 // entries in opcodeEntry. However, sometimes we need to modify
741 // the list as we parse the operands. This occurs when an operand
742 // has its own logical operands (such as the LocalSize operand for
743 // ExecutionMode), or for extended instructions that may have their
744 // own operands depending on the selected extended instruction.
745 spv_operand_pattern_t expectedOperands(
746 opcodeEntry->operandTypes,
747 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400748
David Neto78c3b432015-08-27 13:03:52 -0400749 while (!expectedOperands.empty()) {
750 const spv_operand_type_t type = expectedOperands.front();
751 expectedOperands.pop_front();
752
753 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400754 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400755
756 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
757 // Handle the <result-id> for value generating instructions.
758 // We've already consumed it from the text stream. Here
759 // we inject its words into the instruction.
Lei Zhange78a7c12015-09-10 17:07:21 -0400760 error = spvTextEncodeOperand(SPV_OPERAND_TYPE_RESULT_ID,
761 result_id.c_str(), operandTable,
762 extInstTable, namedIdTable, pInst, nullptr,
763 pBound, &result_id_position, pDiagnostic);
Lei Zhang40056702015-09-11 14:31:27 -0400764 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100765 } else {
David Neto78c3b432015-08-27 13:03:52 -0400766 // Find the next word.
767 error = spvTextAdvance(text, position);
768 if (error == SPV_END_OF_STREAM) {
769 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400770 // This would have been the last potential operand for the
771 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400772 // and we didn't find one. We're finished parsing this instruction.
773 break;
774 } else {
775 DIAGNOSTIC << "Expected operand, found end of stream.";
776 return SPV_ERROR_INVALID_TEXT;
777 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100778 }
David Neto78c3b432015-08-27 13:03:52 -0400779 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
780
781 if (spvTextIsStartOfNewInst(text, position)) {
782 if (spvOperandIsOptional(type)) {
783 break;
784 } else {
785 DIAGNOSTIC << "Expected operand, found next instruction instead.";
786 return SPV_ERROR_INVALID_TEXT;
787 }
788 }
789
790 std::string operandValue;
791 error = spvTextWordGet(text, position, operandValue, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400792 if (error) {
793 DIAGNOSTIC << "Internal Error";
794 return error;
795 }
David Neto78c3b432015-08-27 13:03:52 -0400796
797 error = spvTextEncodeOperand(
Lei Zhange78a7c12015-09-10 17:07:21 -0400798 type, operandValue.c_str(), operandTable, extInstTable, namedIdTable,
799 pInst, &expectedOperands, pBound, position, pDiagnostic);
David Neto78c3b432015-08-27 13:03:52 -0400800
801 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
802 return SPV_SUCCESS;
803
Lei Zhang40056702015-09-11 14:31:27 -0400804 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400805
806 *position = nextPosition;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100807 }
808 }
809
810 pInst->words[0] = spvOpcodeMake(pInst->wordCount, opcodeEntry->opcode);
811
812 return SPV_SUCCESS;
813}
814
David Netoc9786432015-09-01 18:05:14 -0400815namespace {
816
817// Translates a given assembly language module into binary form.
818// If a diagnostic is generated, it is not yet marked as being
819// for a text-based input.
820spv_result_t spvTextToBinaryInternal(const spv_text text,
Lei Zhang06efdc52015-09-10 14:00:00 -0400821 spv_assembly_syntax_format_t format,
David Netoc9786432015-09-01 18:05:14 -0400822 const spv_opcode_table opcodeTable,
823 const spv_operand_table operandTable,
824 const spv_ext_inst_table extInstTable,
825 spv_binary *pBinary,
826 spv_diagnostic *pDiagnostic) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100827 spv_position_t position = {};
Lei Zhang40056702015-09-11 14:31:27 -0400828 if (!text->str || !text->length) {
829 DIAGNOSTIC << "Text stream is empty.";
830 return SPV_ERROR_INVALID_TEXT;
831 }
832 if (!opcodeTable || !operandTable || !extInstTable)
833 return SPV_ERROR_INVALID_TABLE;
834 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
835 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100836
837 // NOTE: Ensure diagnostic is zero initialised
838 *pDiagnostic = {};
839
840 uint32_t bound = 1;
841
842 std::vector<spv_instruction_t> instructions;
843
Lei Zhang40056702015-09-11 14:31:27 -0400844 if (spvTextAdvance(text, &position)) {
845 DIAGNOSTIC << "Text stream is empty.";
846 return SPV_ERROR_INVALID_TEXT;
847 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100848
849 spv_named_id_table namedIdTable = spvNamedIdTableCreate();
Lei Zhang40056702015-09-11 14:31:27 -0400850 if (!namedIdTable) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100851
852 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
853 while (text->length > position.index) {
854 spv_instruction_t inst = {};
855 inst.extInstType = extInstType;
856
Lei Zhang40056702015-09-11 14:31:27 -0400857 if (spvTextEncodeOpcode(text, format, opcodeTable, operandTable,
858 extInstTable, namedIdTable, &bound, &inst,
859 &position, pDiagnostic)) {
860 spvNamedIdTableDestory(namedIdTable);
861 return SPV_ERROR_INVALID_TEXT;
862 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100863 extInstType = inst.extInstType;
864
865 instructions.push_back(inst);
866
Lei Zhang40056702015-09-11 14:31:27 -0400867 if (spvTextAdvance(text, &position)) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100868 }
869
870 spvNamedIdTableDestory(namedIdTable);
871
872 size_t totalSize = SPV_INDEX_INSTRUCTION;
873 for (auto &inst : instructions) {
874 totalSize += inst.wordCount;
875 }
876
877 uint32_t *data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400878 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100879 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
880 for (auto &inst : instructions) {
881 memcpy(data + currentIndex, inst.words, sizeof(uint32_t) * inst.wordCount);
882 currentIndex += inst.wordCount;
883 }
884
885 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400886 if (!binary) {
887 delete[] data;
888 return SPV_ERROR_OUT_OF_MEMORY;
889 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100890 binary->code = data;
891 binary->wordCount = totalSize;
892
893 spv_result_t error = spvBinaryHeaderSet(binary, bound);
Lei Zhang40056702015-09-11 14:31:27 -0400894 if (error) {
895 spvBinaryDestroy(binary);
896 return error;
897 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100898
899 *pBinary = binary;
900
901 return SPV_SUCCESS;
902}
903
Lei Zhange78a7c12015-09-10 17:07:21 -0400904} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400905
Lei Zhang06efdc52015-09-10 14:00:00 -0400906spv_result_t spvTextToBinary(const char *input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400907 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400908 const spv_opcode_table opcodeTable,
909 const spv_operand_table operandTable,
910 const spv_ext_inst_table extInstTable,
911 spv_binary *pBinary, spv_diagnostic *pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400912 return spvTextWithFormatToBinary(
913 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
914 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
915}
916
917spv_result_t spvTextWithFormatToBinary(
918 const char *input_text, const uint64_t input_text_size,
919 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
920 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
921 spv_binary *pBinary, spv_diagnostic *pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400922 spv_text_t text = {input_text, input_text_size};
923
Lei Zhang06efdc52015-09-10 14:00:00 -0400924 spv_result_t result =
925 spvTextToBinaryInternal(&text, format, opcodeTable, operandTable,
926 extInstTable, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400927 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
928
929 return result;
930}
931
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100932void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400933 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100934 if (text->str) {
935 delete[] text->str;
936 }
937 delete text;
938}