blob: 0c190b383ab9b7ee86ff7b26443e05cb1d852efc [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>
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040035#include <string>
David Neto36b0c0f2015-09-16 18:32:54 -040036#include <unordered_map>
37#include <vector>
Dejan Mircevskiba569fb2015-09-11 16:34:49 -040038
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010039#include "binary.h"
David Neto36b0c0f2015-09-16 18:32:54 -040040#include "bitwisecast.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010041#include "diagnostic.h"
42#include "ext_inst.h"
David Neto36b0c0f2015-09-16 18:32:54 -040043#include <libspirv/libspirv.h>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010044#include "opcode.h"
45#include "operand.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010046
Lei Zhang610c5252015-09-09 10:36:48 -040047using spvutils::BitwiseCast;
48
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010049// Structures
50
Andrew Woloszyn13804e52015-09-22 15:50:33 -040051using spv_named_id_table = std::unordered_map<std::string, uint32_t>;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010052
53// Text API
54
Dejan Mircevski903f9d62015-09-28 17:04:39 -040055std::string spvGetWord(const char* str) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010056 size_t index = 0;
57 while (true) {
58 switch (str[index]) {
59 case '\0':
60 case '\t':
David Netoa48678a2015-09-11 12:04:03 -040061 case '\v':
62 case '\r':
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010063 case '\n':
64 case ' ':
David Netoa48678a2015-09-11 12:04:03 -040065 return std::string(str, str + index);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010066 default:
67 index++;
68 }
69 }
David Netoa48678a2015-09-11 12:04:03 -040070 assert(0 && "Unreachable");
Lei Zhang40056702015-09-11 14:31:27 -040071 return ""; // Make certain compilers happy.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010072}
73
Dejan Mircevski903f9d62015-09-28 17:04:39 -040074uint32_t spvNamedIdAssignOrGet(spv_named_id_table* table, const char* textValue,
75 uint32_t* pBound) {
Andrew Woloszyn13804e52015-09-22 15:50:33 -040076 if (table->end() == table->find(textValue)) {
77 (*table)[std::string(textValue)] = *pBound;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010078 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -040079 return (*table)[textValue];
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010080}
81
82spv_result_t spvTextAdvanceLine(const spv_text text, spv_position position) {
83 while (true) {
84 switch (text->str[position->index]) {
85 case '\0':
86 return SPV_END_OF_STREAM;
87 case '\n':
88 position->column = 0;
89 position->line++;
90 position->index++;
91 return SPV_SUCCESS;
92 default:
Lei Zhangee87cc22015-08-21 11:51:28 -040093 position->column++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010094 position->index++;
95 break;
96 }
97 }
98}
99
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400100bool spvIsValidIDCharacter(const char value) {
101 return value == '_' || 0 != ::isalnum(value);
102}
103
104// Returns true if the given string represents a valid ID name.
105bool spvIsValidID(const char* textValue) {
106 const char* c = textValue;
107 for (; *c != '\0'; ++c) {
108 if (!spvIsValidIDCharacter(*c)) {
109 return false;
110 }
111 }
112 // If the string was empty, then the ID also is not valid.
113 return c != textValue;
114}
115
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100116spv_result_t spvTextAdvance(const spv_text text, spv_position position) {
117 // NOTE: Consume white space, otherwise don't advance.
118 switch (text->str[position->index]) {
119 case '\0':
120 return SPV_END_OF_STREAM;
121 case ';':
Lei Zhangee87cc22015-08-21 11:51:28 -0400122 if (spv_result_t error = spvTextAdvanceLine(text, position)) return error;
123 return spvTextAdvance(text, position);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100124 case ' ':
125 case '\t':
126 position->column++;
127 position->index++;
128 return spvTextAdvance(text, position);
129 case '\n':
130 position->column = 0;
131 position->line++;
132 position->index++;
133 return spvTextAdvance(text, position);
134 default:
135 break;
136 }
137
138 return SPV_SUCCESS;
139}
140
141spv_result_t spvTextWordGet(const spv_text text,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400142 const spv_position startPosition, std::string& word,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100143 spv_position endPosition) {
Lei Zhang40056702015-09-11 14:31:27 -0400144 if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
145 if (!startPosition || !endPosition) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100146
147 *endPosition = *startPosition;
148
David Netoe7ee4c42015-08-25 14:21:13 -0400149 bool quoting = false;
150 bool escaping = false;
151
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100152 // NOTE: Assumes first character is not white space!
153 while (true) {
David Netoe7ee4c42015-08-25 14:21:13 -0400154 const char ch = text->str[endPosition->index];
155 if (ch == '\\')
156 escaping = !escaping;
157 else {
158 switch (ch) {
159 case '"':
160 if (!escaping) quoting = !quoting;
161 break;
162 case ' ':
163 case ';':
164 case '\t':
165 case '\n':
166 if (escaping || quoting) break;
Lei Zhange78a7c12015-09-10 17:07:21 -0400167 // Fall through.
David Netoe7ee4c42015-08-25 14:21:13 -0400168 case '\0': { // NOTE: End of word found!
169 word.assign(text->str + startPosition->index,
170 (size_t)(endPosition->index - startPosition->index));
171 return SPV_SUCCESS;
172 }
173 default:
174 break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100175 }
David Netoe7ee4c42015-08-25 14:21:13 -0400176 escaping = false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100177 }
178
179 endPosition->column++;
180 endPosition->index++;
181 }
182}
183
David Netoc9786432015-09-01 18:05:14 -0400184namespace {
185
Lei Zhangdfc50082015-08-21 11:50:55 -0400186// Returns true if the string at the given position in text starts with "Op".
David Netoc9786432015-09-01 18:05:14 -0400187bool spvStartsWithOp(const spv_text text, const spv_position position) {
David Neto78c3b432015-08-27 13:03:52 -0400188 if (text->length < position->index + 3) return false;
189 char ch0 = text->str[position->index];
190 char ch1 = text->str[position->index + 1];
191 char ch2 = text->str[position->index + 2];
192 return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
Lei Zhangdfc50082015-08-21 11:50:55 -0400193}
194
Lei Zhange78a7c12015-09-10 17:07:21 -0400195} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400196
Lei Zhangdfc50082015-08-21 11:50:55 -0400197// Returns true if a new instruction begins at the given position in text.
Lei Zhange78a7c12015-09-10 17:07:21 -0400198bool spvTextIsStartOfNewInst(const spv_text text, const spv_position position) {
Lei Zhangdfc50082015-08-21 11:50:55 -0400199 spv_position_t nextPosition = *position;
200 if (spvTextAdvance(text, &nextPosition)) return false;
David Neto78c3b432015-08-27 13:03:52 -0400201 if (spvStartsWithOp(text, &nextPosition)) return true;
Lei Zhangdfc50082015-08-21 11:50:55 -0400202
203 std::string word;
David Neto78c3b432015-08-27 13:03:52 -0400204 spv_position_t startPosition = *position;
Lei Zhangdfc50082015-08-21 11:50:55 -0400205 if (spvTextWordGet(text, &startPosition, word, &nextPosition)) return false;
206 if ('%' != word.front()) return false;
207
208 if (spvTextAdvance(text, &nextPosition)) return false;
209 startPosition = nextPosition;
210 if (spvTextWordGet(text, &startPosition, word, &nextPosition)) return false;
211 if ("=" != word) return false;
212
213 if (spvTextAdvance(text, &nextPosition)) return false;
214 startPosition = nextPosition;
215 if (spvStartsWithOp(text, &startPosition)) return true;
216 return false;
217}
218
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100219spv_result_t spvTextStringGet(const spv_text text,
220 const spv_position startPosition,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400221 std::string& string, spv_position endPosition) {
Lei Zhang40056702015-09-11 14:31:27 -0400222 if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
223 if (!startPosition || !endPosition) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100224
Lei Zhang40056702015-09-11 14:31:27 -0400225 if ('"' != text->str[startPosition->index]) return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100226
227 *endPosition = *startPosition;
228
229 // NOTE: Assumes first character is not white space
230 while (true) {
231 endPosition->column++;
232 endPosition->index++;
233
234 switch (text->str[endPosition->index]) {
235 case '"': {
236 endPosition->column++;
237 endPosition->index++;
238
239 string.assign(text->str + startPosition->index,
240 (size_t)(endPosition->index - startPosition->index));
241
242 return SPV_SUCCESS;
243 }
244 case '\n':
245 case '\0':
246 return SPV_ERROR_INVALID_TEXT;
247 default:
248 break;
249 }
250 }
251}
252
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400253spv_result_t spvTextToUInt32(const char* textValue, uint32_t* pValue) {
254 char* endPtr = nullptr;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100255 *pValue = strtoul(textValue, &endPtr, 0);
256 if (0 == *pValue && textValue == endPtr) {
257 return SPV_ERROR_INVALID_TEXT;
258 }
259 return SPV_SUCCESS;
260}
261
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400262spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100263 bool isSigned = false;
David Netoaffa6962015-08-24 15:33:14 -0400264 int numPeriods = 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100265 bool isString = false;
266
David Netoaffa6962015-08-24 15:33:14 -0400267 const size_t len = strlen(textValue);
David Neto98290a22015-08-24 16:27:02 -0400268 if (len == 0) return SPV_FAILED_MATCH;
269
David Netoaffa6962015-08-24 15:33:14 -0400270 for (uint64_t index = 0; index < len; ++index) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100271 switch (textValue[index]) {
272 case '0':
273 case '1':
274 case '2':
275 case '3':
276 case '4':
277 case '5':
278 case '6':
279 case '7':
280 case '8':
281 case '9':
282 break;
283 case '.':
David Netoaffa6962015-08-24 15:33:14 -0400284 numPeriods++;
285 break;
286 case '-':
287 if (index == 0) {
288 isSigned = true;
289 } else {
290 isString = true;
291 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100292 break;
293 default:
294 isString = true;
Lei Zhange78a7c12015-09-10 17:07:21 -0400295 index = len; // break out of the loop too.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100296 break;
297 }
298 }
299
David Netoaffa6962015-08-24 15:33:14 -0400300 pLiteral->type = spv_literal_type_t(99);
301
Lei Zhange78a7c12015-09-10 17:07:21 -0400302 if (isString || numPeriods > 1 || (isSigned && len == 1)) {
David Netoaffa6962015-08-24 15:33:14 -0400303 // TODO(dneto): Allow escaping.
David Neto98290a22015-08-24 16:27:02 -0400304 if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
305 return SPV_FAILED_MATCH;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100306 pLiteral->type = SPV_LITERAL_TYPE_STRING;
David Netoaffa6962015-08-24 15:33:14 -0400307 // Need room for the null-terminator.
David Neto98290a22015-08-24 16:27:02 -0400308 if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY;
Lei Zhange78a7c12015-09-10 17:07:21 -0400309 strncpy(pLiteral->value.str, textValue + 1, len - 2);
310 pLiteral->value.str[len - 2] = 0;
David Netoaffa6962015-08-24 15:33:14 -0400311 } else if (numPeriods == 1) {
312 double d = std::strtod(textValue, nullptr);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100313 float f = (float)d;
314 if (d == (double)f) {
315 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
316 pLiteral->value.f = f;
317 } else {
318 pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
319 pLiteral->value.d = d;
320 }
321 } else if (isSigned) {
322 int64_t i64 = strtoll(textValue, nullptr, 10);
323 int32_t i32 = (int32_t)i64;
324 if (i64 == (int64_t)i32) {
325 pLiteral->type = SPV_LITERAL_TYPE_INT_32;
326 pLiteral->value.i32 = i32;
327 } else {
328 pLiteral->type = SPV_LITERAL_TYPE_INT_64;
329 pLiteral->value.i64 = i64;
330 }
331 } else {
332 uint64_t u64 = strtoull(textValue, nullptr, 10);
333 uint32_t u32 = (uint32_t)u64;
334 if (u64 == (uint64_t)u32) {
335 pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
336 pLiteral->value.u32 = u32;
337 } else {
338 pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
339 pLiteral->value.u64 = u64;
340 }
341 }
342
343 return SPV_SUCCESS;
344}
345
David Neto36b0c0f2015-09-16 18:32:54 -0400346spv_result_t spvTextParseMaskOperand(const spv_operand_table operandTable,
347 const spv_operand_type_t type,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400348 const char* textValue, uint32_t* pValue) {
David Neto36b0c0f2015-09-16 18:32:54 -0400349 if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT;
350 size_t text_length = strlen(textValue);
351 if (text_length == 0) return SPV_ERROR_INVALID_TEXT;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400352 const char* text_end = textValue + text_length;
David Neto36b0c0f2015-09-16 18:32:54 -0400353
354 // We only support mask expressions in ASCII, so the separator value is a
355 // char.
356 const char separator = '|';
357
358 // Accumulate the result by interpreting one word at a time, scanning
359 // from left to right.
360 uint32_t value = 0;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400361 const char* begin = textValue; // The left end of the current word.
362 const char* end = nullptr; // One character past the end of the current word.
David Neto36b0c0f2015-09-16 18:32:54 -0400363 do {
364 end = std::find(begin, text_end, separator);
365
366 spv_operand_desc entry = nullptr;
367 if (spvOperandTableNameLookup(operandTable, type, begin, end - begin,
368 &entry)) {
369 return SPV_ERROR_INVALID_TEXT;
370 }
371 value |= entry->value;
372
373 // Advance to the next word by skipping over the separator.
374 begin = end + 1;
375 } while (end != text_end);
376
377 *pValue = value;
378 return SPV_SUCCESS;
379}
380
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400381/// @brief Translate an Opcode operand to binary form
382///
383/// @param[in] type of the operand
384/// @param[in] textValue word of text to be parsed
385/// @param[in] operandTable operand lookup table
386/// @param[in,out] namedIdTable table of named ID's
387/// @param[out] pInst return binary Opcode
388/// @param[in,out] pExpectedOperands the operand types expected
389/// @param[in,out] pBound current highest defined ID value
390/// @param[in] pPosition used in diagnostic on error
391/// @param[out] pDiagnostic populated on error
392///
393/// @return result code
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100394spv_result_t spvTextEncodeOperand(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400395 const spv_operand_type_t type, const char* textValue,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100396 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400397 spv_named_id_table* namedIdTable, spv_instruction_t* pInst,
398 spv_operand_pattern_t* pExpectedOperands, uint32_t* pBound,
399 const spv_position position, spv_diagnostic* pDiagnostic) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100400 // NOTE: Handle immediate int in the stream
401 if ('!' == textValue[0]) {
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400402 const char* begin = textValue + 1;
403 char* end = nullptr;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100404 uint32_t immediateInt = strtoul(begin, &end, 0);
405 size_t size = strlen(textValue);
406 size_t length = (end - begin);
Lei Zhang40056702015-09-11 14:31:27 -0400407 if (size - 1 != length) {
408 DIAGNOSTIC << "Invalid immediate integer '" << textValue << "'.";
409 return SPV_ERROR_INVALID_TEXT;
410 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100411 position->column += size;
412 position->index += size;
413 pInst->words[pInst->wordCount] = immediateInt;
414 pInst->wordCount += 1;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400415 spvSwitchToAlternateParsingAfterImmediate(pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100416 return SPV_SUCCESS;
417 }
418
419 switch (type) {
David Netob14a7272015-09-25 13:56:09 -0400420 case SPV_OPERAND_TYPE_EXECUTION_SCOPE:
David Netoe3f70b92015-08-27 13:50:05 -0400421 case SPV_OPERAND_TYPE_ID:
David Neto78c3b432015-08-27 13:03:52 -0400422 case SPV_OPERAND_TYPE_ID_IN_OPTIONAL_TUPLE:
423 case SPV_OPERAND_TYPE_OPTIONAL_ID:
David Netob14a7272015-09-25 13:56:09 -0400424 case SPV_OPERAND_TYPE_MEMORY_SEMANTICS:
425 case SPV_OPERAND_TYPE_RESULT_ID: {
Lei Zhangabafd5e2015-08-21 11:52:29 -0400426 if ('%' == textValue[0]) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100427 textValue++;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100428 } else {
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400429 DIAGNOSTIC << "Expected id to start with %.";
430 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100431 }
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400432 if (!spvIsValidID(textValue)) {
433 DIAGNOSTIC << "Invalid ID " << textValue;
434 return SPV_ERROR_INVALID_TEXT;
435 }
436 const uint32_t id =
437 spvNamedIdAssignOrGet(namedIdTable, textValue, pBound);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100438 pInst->words[pInst->wordCount++] = id;
Dejan Mircevskiba569fb2015-09-11 16:34:49 -0400439 *pBound = std::max(*pBound, id + 1);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100440 } break;
441 case SPV_OPERAND_TYPE_LITERAL_NUMBER: {
442 // NOTE: Special case for extension instruction lookup
443 if (OpExtInst == pInst->opcode) {
444 spv_ext_inst_desc extInst;
Lei Zhang40056702015-09-11 14:31:27 -0400445 if (spvExtInstTableNameLookup(extInstTable, pInst->extInstType,
446 textValue, &extInst)) {
447 DIAGNOSTIC << "Invalid extended instruction name '" << textValue
448 << "'.";
449 return SPV_ERROR_INVALID_TEXT;
450 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100451 pInst->words[pInst->wordCount++] = extInst->ext_inst;
David Neto78c3b432015-08-27 13:03:52 -0400452
453 // Prepare to parse the operands for the extended instructions.
454 spvPrependOperandTypes(extInst->operandTypes, pExpectedOperands);
455
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100456 return SPV_SUCCESS;
457 }
Lei Zhang6d415812015-09-15 13:36:21 -0400458 } // Fall through for the general case.
Lei Zhangb41d1502015-09-14 15:22:23 -0400459 case SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER:
David Neto561dc4e2015-09-25 14:23:29 -0400460 case SPV_OPERAND_TYPE_LITERAL_NUMBER_IN_OPTIONAL_TUPLE:
461 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100462 spv_literal_t literal = {};
Lei Zhang6d415812015-09-15 13:36:21 -0400463 spv_result_t error = spvTextToLiteral(textValue, &literal);
464 if (error != SPV_SUCCESS) {
465 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
466 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
467 DIAGNOSTIC << "Invalid literal number '" << textValue << "'.";
468 return SPV_ERROR_INVALID_TEXT;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400469 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100470 switch (literal.type) {
Andrew Woloszyn4b4acde2015-09-10 10:28:22 -0400471 // We do not have to print diagnostics here because spvBinaryEncode*
472 // prints diagnostic messages on failure.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100473 case SPV_LITERAL_TYPE_INT_32:
Lei Zhang40056702015-09-11 14:31:27 -0400474 if (spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.i32),
475 pInst, position, pDiagnostic))
476 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100477 break;
478 case SPV_LITERAL_TYPE_INT_64: {
Lei Zhang40056702015-09-11 14:31:27 -0400479 if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.i64),
480 pInst, position, pDiagnostic))
481 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100482 } break;
483 case SPV_LITERAL_TYPE_UINT_32: {
Lei Zhang40056702015-09-11 14:31:27 -0400484 if (spvBinaryEncodeU32(literal.value.u32, pInst, position,
485 pDiagnostic))
486 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100487 } break;
488 case SPV_LITERAL_TYPE_UINT_64: {
Lei Zhang40056702015-09-11 14:31:27 -0400489 if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.u64),
490 pInst, position, pDiagnostic))
491 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100492 } break;
493 case SPV_LITERAL_TYPE_FLOAT_32: {
Lei Zhang40056702015-09-11 14:31:27 -0400494 if (spvBinaryEncodeU32(BitwiseCast<uint32_t>(literal.value.f), pInst,
495 position, pDiagnostic))
496 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100497 } break;
498 case SPV_LITERAL_TYPE_FLOAT_64: {
Lei Zhang40056702015-09-11 14:31:27 -0400499 if (spvBinaryEncodeU64(BitwiseCast<uint64_t>(literal.value.d), pInst,
500 position, pDiagnostic))
501 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100502 } break;
503 case SPV_LITERAL_TYPE_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400504 DIAGNOSTIC << "Expected literal number, found literal string '"
505 << textValue << "'.";
506 return SPV_FAILED_MATCH;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100507 } break;
508 default:
Lei Zhang6d415812015-09-15 13:36:21 -0400509 DIAGNOSTIC << "Invalid literal number '" << textValue << "'";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100510 return SPV_ERROR_INVALID_TEXT;
511 }
512 } break;
David Neto78c3b432015-08-27 13:03:52 -0400513 case SPV_OPERAND_TYPE_LITERAL_STRING:
514 case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
Lei Zhang6d415812015-09-15 13:36:21 -0400515 spv_literal_t literal = {};
516 spv_result_t error = spvTextToLiteral(textValue, &literal);
517 if (error != SPV_SUCCESS) {
518 if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
Lei Zhang40056702015-09-11 14:31:27 -0400519 if (spvOperandIsOptional(type)) return SPV_FAILED_MATCH;
Lei Zhang6d415812015-09-15 13:36:21 -0400520 DIAGNOSTIC << "Invalid literal string '" << textValue << "'.";
Lei Zhang40056702015-09-11 14:31:27 -0400521 return SPV_ERROR_INVALID_TEXT;
522 }
Lei Zhang6d415812015-09-15 13:36:21 -0400523 if (literal.type != SPV_LITERAL_TYPE_STRING) {
524 DIAGNOSTIC << "Expected literal string, found literal number '"
525 << textValue << "'.";
526 return SPV_FAILED_MATCH;
527 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100528
529 // NOTE: Special case for extended instruction library import
530 if (OpExtInstImport == pInst->opcode) {
Lei Zhang6d415812015-09-15 13:36:21 -0400531 pInst->extInstType = spvExtInstImportTypeGet(literal.value.str);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100532 }
533
Lei Zhang6d415812015-09-15 13:36:21 -0400534 if (spvBinaryEncodeString(literal.value.str, pInst, position,
535 pDiagnostic))
Lei Zhang40056702015-09-11 14:31:27 -0400536 return SPV_ERROR_INVALID_TEXT;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100537 } break;
David Neto36b0c0f2015-09-16 18:32:54 -0400538 case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
539 case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
540 case SPV_OPERAND_TYPE_LOOP_CONTROL:
David Netoee1b3bb2015-09-18 11:19:18 -0400541 case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
David Neto5bf88fc2015-09-17 17:06:10 -0400542 case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
David Neto36b0c0f2015-09-16 18:32:54 -0400543 case SPV_OPERAND_TYPE_SELECTION_CONTROL: {
544 uint32_t value;
545 if (spvTextParseMaskOperand(operandTable, type, textValue, &value)) {
546 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
547 << "'.";
548 return SPV_ERROR_INVALID_TEXT;
549 }
550 if (auto error = spvBinaryEncodeU32(value, pInst, position, pDiagnostic))
551 return error;
David Neto5bf88fc2015-09-17 17:06:10 -0400552 // Prepare to parse the operands for this logical operand.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400553 spvPrependOperandTypesForMask(operandTable, type, value,
554 pExpectedOperands);
555 } break;
556 case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
557 auto error = spvTextEncodeOperand(
558 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue, operandTable,
559 extInstTable, namedIdTable, pInst, pExpectedOperands, pBound,
560 position, pDiagnostic);
561 if (error == SPV_FAILED_MATCH) {
562 // It's not a literal number -- is it a literal string?
563 error = spvTextEncodeOperand(SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
564 textValue, operandTable, extInstTable,
565 namedIdTable, pInst, pExpectedOperands,
566 pBound, position, pDiagnostic);
567 }
568 if (error == SPV_FAILED_MATCH) {
569 // It's not a literal -- is it an ID?
570 error = spvTextEncodeOperand(SPV_OPERAND_TYPE_OPTIONAL_ID, textValue,
571 operandTable, extInstTable, namedIdTable,
572 pInst, pExpectedOperands, pBound, position,
573 pDiagnostic);
574 }
575 if (error) {
576 DIAGNOSTIC << "Invalid word following !<integer>: " << textValue;
577 return error;
578 }
579 if (pExpectedOperands->empty()) {
580 pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
581 }
David Neto36b0c0f2015-09-16 18:32:54 -0400582 } break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100583 default: {
584 // NOTE: All non literal operands are handled here using the operand
585 // table.
586 spv_operand_desc entry;
David Neto388c40d2015-09-16 16:42:56 -0400587 if (spvOperandTableNameLookup(operandTable, type, textValue,
588 strlen(textValue), &entry)) {
Lei Zhang40056702015-09-11 14:31:27 -0400589 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
590 << "'.";
591 return SPV_ERROR_INVALID_TEXT;
592 }
593 if (spvBinaryEncodeU32(entry->value, pInst, position, pDiagnostic)) {
594 DIAGNOSTIC << "Invalid " << spvOperandTypeStr(type) << " '" << textValue
595 << "'.";
596 return SPV_ERROR_INVALID_TEXT;
597 }
David Neto78c3b432015-08-27 13:03:52 -0400598
599 // Prepare to parse the operands for this logical operand.
600 spvPrependOperandTypes(entry->operandTypes, pExpectedOperands);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100601 } break;
602 }
603 return SPV_SUCCESS;
604}
605
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400606namespace {
607
608/// Encodes an instruction started by !<integer> at the given position in text.
609///
610/// Puts the encoded words into *pInst. If successful, moves position past the
611/// instruction and returns SPV_SUCCESS. Otherwise, returns an error code and
612/// leaves position pointing to the error in text.
613spv_result_t encodeInstructionStartingWithImmediate(
614 const spv_text text, const spv_operand_table operandTable,
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400615 const spv_ext_inst_table extInstTable, spv_named_id_table* namedIdTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400616 uint32_t* pBound, spv_instruction_t* pInst, spv_position position,
617 spv_diagnostic* pDiagnostic) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400618 std::string firstWord;
619 spv_position_t nextPosition = {};
620 auto error = spvTextWordGet(text, position, firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400621 if (error) {
622 DIAGNOSTIC << "Internal Error";
623 return error;
624 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400625
626 assert(firstWord[0] == '!');
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400627 const char* begin = firstWord.data() + 1;
628 char* end = nullptr;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400629 uint32_t immediateInt = strtoul(begin, &end, 0);
Lei Zhang40056702015-09-11 14:31:27 -0400630 if ((begin + firstWord.size() - 1) != end) {
631 DIAGNOSTIC << "Invalid immediate integer '" << firstWord << "'.";
632 return SPV_ERROR_INVALID_TEXT;
633 }
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400634 position->column += firstWord.size();
635 position->index += firstWord.size();
636 pInst->words[0] = immediateInt;
637 pInst->wordCount = 1;
638 while (spvTextAdvance(text, position) != SPV_END_OF_STREAM) {
639 // A beginning of a new instruction means we're done.
640 if (spvTextIsStartOfNewInst(text, position)) return SPV_SUCCESS;
641
642 // Otherwise, there must be an operand that's either a literal, an ID, or
643 // an immediate.
644 std::string operandValue;
645 if ((error = spvTextWordGet(text, position, operandValue, &nextPosition))) {
646 DIAGNOSTIC << "Internal Error";
647 return error;
648 }
649
Dejan Mircevskie3a19c02015-09-11 15:03:54 -0400650 if (operandValue == "=") {
651 DIAGNOSTIC << firstWord << " not allowed before =.";
652 return SPV_ERROR_INVALID_TEXT;
653 }
654
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400655 // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
656 // expanded.
657 spv_operand_pattern_t dummyExpectedOperands;
658 error = spvTextEncodeOperand(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400659 SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(), operandTable,
660 extInstTable, namedIdTable, pInst, &dummyExpectedOperands, pBound,
661 position, pDiagnostic);
Lei Zhang40056702015-09-11 14:31:27 -0400662 if (error) return error;
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400663 *position = nextPosition;
664 }
665 return SPV_SUCCESS;
666}
667
668} // anonymous namespace
669
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400670/// @brief Translate single Opcode and operands to binary form
671///
672/// @param[in] text stream to translate
673/// @param[in] format the assembly syntax format of text
674/// @param[in] opcodeTable Opcode lookup table
675/// @param[in] operandTable operand lookup table
676/// @param[in,out] namedIdTable table of named ID's
677/// @param[in,out] pBound current highest defined ID value
678/// @param[out] pInst returned binary Opcode
679/// @param[in,out] pPosition in the text stream
680/// @param[out] pDiagnostic populated on failure
681///
682/// @return result code
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100683spv_result_t spvTextEncodeOpcode(
Lei Zhang06efdc52015-09-10 14:00:00 -0400684 const spv_text text, spv_assembly_syntax_format_t format,
685 const spv_opcode_table opcodeTable, const spv_operand_table operandTable,
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400686 const spv_ext_inst_table extInstTable, spv_named_id_table* namedIdTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400687 uint32_t* pBound, spv_instruction_t* pInst, spv_position position,
688 spv_diagnostic* pDiagnostic) {
Dejan Mircevskif79519c2015-09-11 00:43:11 -0400689 // Check for !<integer> first.
690 if ('!' == text->str[position->index]) {
691 return encodeInstructionStartingWithImmediate(
692 text, operandTable, extInstTable, namedIdTable, pBound, pInst, position,
693 pDiagnostic);
694 }
695
Lei Zhangdfc50082015-08-21 11:50:55 -0400696 // An assembly instruction has two possible formats:
Lei Zhang06efdc52015-09-10 14:00:00 -0400697 // 1(CAF): <opcode> <operand>..., e.g., "OpTypeVoid %void".
698 // 2(AAF): <result-id> = <opcode> <operand>..., e.g., "%void = OpTypeVoid".
Lei Zhangdfc50082015-08-21 11:50:55 -0400699
Lei Zhang06efdc52015-09-10 14:00:00 -0400700 std::string firstWord;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100701 spv_position_t nextPosition = {};
Lei Zhang06efdc52015-09-10 14:00:00 -0400702 spv_result_t error = spvTextWordGet(text, position, firstWord, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400703 if (error) {
704 DIAGNOSTIC << "Internal Error";
705 return error;
706 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100707
Lei Zhang06efdc52015-09-10 14:00:00 -0400708 std::string opcodeName;
Lei Zhangdfc50082015-08-21 11:50:55 -0400709 std::string result_id;
710 spv_position_t result_id_position = {};
Lei Zhang06efdc52015-09-10 14:00:00 -0400711 if (spvStartsWithOp(text, position)) {
712 opcodeName = firstWord;
713 } else {
714 // If the first word of this instruction is not an opcode, we must be
715 // processing AAF now.
Lei Zhang40056702015-09-11 14:31:27 -0400716 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT != format) {
717 DIAGNOSTIC
718 << "Expected <opcode> at the beginning of an instruction, found '"
719 << firstWord << "'.";
720 return SPV_ERROR_INVALID_TEXT;
721 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400722
723 result_id = firstWord;
Lei Zhang40056702015-09-11 14:31:27 -0400724 if ('%' != result_id.front()) {
725 DIAGNOSTIC << "Expected <opcode> or <result-id> at the beginning "
726 "of an instruction, found '"
727 << result_id << "'.";
728 return SPV_ERROR_INVALID_TEXT;
729 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400730 result_id_position = *position;
Lei Zhang06efdc52015-09-10 14:00:00 -0400731
732 // The '=' sign.
Lei Zhangdfc50082015-08-21 11:50:55 -0400733 *position = nextPosition;
Lei Zhang40056702015-09-11 14:31:27 -0400734 if (spvTextAdvance(text, position)) {
735 DIAGNOSTIC << "Expected '=', found end of stream.";
736 return SPV_ERROR_INVALID_TEXT;
737 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400738 std::string equal_sign;
739 error = spvTextWordGet(text, position, equal_sign, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400740 if ("=" != equal_sign) {
741 DIAGNOSTIC << "'=' expected after result id.";
742 return SPV_ERROR_INVALID_TEXT;
743 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400744
745 // The <opcode> after the '=' sign.
746 *position = nextPosition;
Lei Zhang40056702015-09-11 14:31:27 -0400747 if (spvTextAdvance(text, position)) {
748 DIAGNOSTIC << "Expected opcode, found end of stream.";
749 return SPV_ERROR_INVALID_TEXT;
750 }
Lei Zhang977e9bc2015-08-21 11:52:03 -0400751 error = spvTextWordGet(text, position, opcodeName, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400752 if (error) {
753 DIAGNOSTIC << "Internal Error";
754 return error;
755 }
756 if (!spvStartsWithOp(text, position)) {
757 DIAGNOSTIC << "Invalid Opcode prefix '" << opcodeName << "'.";
758 return SPV_ERROR_INVALID_TEXT;
759 }
Lei Zhangdfc50082015-08-21 11:50:55 -0400760 }
761
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100762 // NOTE: The table contains Opcode names without the "Op" prefix.
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400763 const char* pInstName = opcodeName.data() + 2;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100764
765 spv_opcode_desc opcodeEntry;
Lei Zhangdfc50082015-08-21 11:50:55 -0400766 error = spvOpcodeTableNameLookup(opcodeTable, pInstName, &opcodeEntry);
Lei Zhang40056702015-09-11 14:31:27 -0400767 if (error) {
768 DIAGNOSTIC << "Invalid Opcode name '"
769 << spvGetWord(text->str + position->index) << "'";
770 return error;
771 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400772 if (SPV_ASSEMBLY_SYNTAX_FORMAT_ASSIGNMENT == format) {
773 // If this instruction has <result-id>, check it follows AAF.
Lei Zhang40056702015-09-11 14:31:27 -0400774 if (opcodeEntry->hasResult && result_id.empty()) {
775 DIAGNOSTIC << "Expected <result-id> at the beginning of an "
776 "instruction, found '"
777 << firstWord << "'.";
778 return SPV_ERROR_INVALID_TEXT;
779 }
Lei Zhang06efdc52015-09-10 14:00:00 -0400780 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100781 pInst->opcode = opcodeEntry->opcode;
782 *position = nextPosition;
783 pInst->wordCount++;
784
David Neto78c3b432015-08-27 13:03:52 -0400785 // Maintains the ordered list of expected operand types.
786 // For many instructions we only need the {numTypes, operandTypes}
787 // entries in opcodeEntry. However, sometimes we need to modify
788 // the list as we parse the operands. This occurs when an operand
789 // has its own logical operands (such as the LocalSize operand for
790 // ExecutionMode), or for extended instructions that may have their
791 // own operands depending on the selected extended instruction.
792 spv_operand_pattern_t expectedOperands(
793 opcodeEntry->operandTypes,
794 opcodeEntry->operandTypes + opcodeEntry->numTypes);
Lei Zhangdfc50082015-08-21 11:50:55 -0400795
David Neto78c3b432015-08-27 13:03:52 -0400796 while (!expectedOperands.empty()) {
797 const spv_operand_type_t type = expectedOperands.front();
798 expectedOperands.pop_front();
799
800 // Expand optional tuples lazily.
Lei Zhange78a7c12015-09-10 17:07:21 -0400801 if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
David Neto78c3b432015-08-27 13:03:52 -0400802
803 if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
804 // Handle the <result-id> for value generating instructions.
805 // We've already consumed it from the text stream. Here
806 // we inject its words into the instruction.
Lei Zhange78a7c12015-09-10 17:07:21 -0400807 error = spvTextEncodeOperand(SPV_OPERAND_TYPE_RESULT_ID,
808 result_id.c_str(), operandTable,
809 extInstTable, namedIdTable, pInst, nullptr,
810 pBound, &result_id_position, pDiagnostic);
Lei Zhang40056702015-09-11 14:31:27 -0400811 if (error) return error;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100812 } else {
David Neto78c3b432015-08-27 13:03:52 -0400813 // Find the next word.
814 error = spvTextAdvance(text, position);
815 if (error == SPV_END_OF_STREAM) {
816 if (spvOperandIsOptional(type)) {
Lei Zhange78a7c12015-09-10 17:07:21 -0400817 // This would have been the last potential operand for the
818 // instruction,
David Neto78c3b432015-08-27 13:03:52 -0400819 // and we didn't find one. We're finished parsing this instruction.
820 break;
821 } else {
822 DIAGNOSTIC << "Expected operand, found end of stream.";
823 return SPV_ERROR_INVALID_TEXT;
824 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100825 }
David Neto78c3b432015-08-27 13:03:52 -0400826 assert(error == SPV_SUCCESS && "Somebody added another way to fail");
827
828 if (spvTextIsStartOfNewInst(text, position)) {
829 if (spvOperandIsOptional(type)) {
830 break;
831 } else {
832 DIAGNOSTIC << "Expected operand, found next instruction instead.";
833 return SPV_ERROR_INVALID_TEXT;
834 }
835 }
836
837 std::string operandValue;
838 error = spvTextWordGet(text, position, operandValue, &nextPosition);
Lei Zhang40056702015-09-11 14:31:27 -0400839 if (error) {
840 DIAGNOSTIC << "Internal Error";
841 return error;
842 }
David Neto78c3b432015-08-27 13:03:52 -0400843
844 error = spvTextEncodeOperand(
Lei Zhange78a7c12015-09-10 17:07:21 -0400845 type, operandValue.c_str(), operandTable, extInstTable, namedIdTable,
846 pInst, &expectedOperands, pBound, position, pDiagnostic);
David Neto78c3b432015-08-27 13:03:52 -0400847
848 if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
849 return SPV_SUCCESS;
850
Lei Zhang40056702015-09-11 14:31:27 -0400851 if (error) return error;
David Neto78c3b432015-08-27 13:03:52 -0400852
853 *position = nextPosition;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100854 }
855 }
856
857 pInst->words[0] = spvOpcodeMake(pInst->wordCount, opcodeEntry->opcode);
858
859 return SPV_SUCCESS;
860}
861
David Netoc9786432015-09-01 18:05:14 -0400862namespace {
863
864// Translates a given assembly language module into binary form.
865// If a diagnostic is generated, it is not yet marked as being
866// for a text-based input.
867spv_result_t spvTextToBinaryInternal(const spv_text text,
Lei Zhang06efdc52015-09-10 14:00:00 -0400868 spv_assembly_syntax_format_t format,
David Netoc9786432015-09-01 18:05:14 -0400869 const spv_opcode_table opcodeTable,
870 const spv_operand_table operandTable,
871 const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400872 spv_binary* pBinary,
873 spv_diagnostic* pDiagnostic) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100874 spv_position_t position = {};
Lei Zhang40056702015-09-11 14:31:27 -0400875 if (!text->str || !text->length) {
876 DIAGNOSTIC << "Text stream is empty.";
877 return SPV_ERROR_INVALID_TEXT;
878 }
879 if (!opcodeTable || !operandTable || !extInstTable)
880 return SPV_ERROR_INVALID_TABLE;
881 if (!pBinary) return SPV_ERROR_INVALID_POINTER;
882 if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100883
884 // NOTE: Ensure diagnostic is zero initialised
885 *pDiagnostic = {};
886
887 uint32_t bound = 1;
888
889 std::vector<spv_instruction_t> instructions;
890
Lei Zhang40056702015-09-11 14:31:27 -0400891 if (spvTextAdvance(text, &position)) {
892 DIAGNOSTIC << "Text stream is empty.";
893 return SPV_ERROR_INVALID_TEXT;
894 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100895
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400896 // This causes namedIdTable to get cleaned up as soon as it is no
897 // longer necessary.
898 {
899 spv_named_id_table namedIdTable;
900 spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
901 while (text->length > position.index) {
902 spv_instruction_t inst = {};
903 inst.extInstType = extInstType;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100904
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400905 if (spvTextEncodeOpcode(text, format, opcodeTable, operandTable,
906 extInstTable, &namedIdTable, &bound, &inst,
907 &position, pDiagnostic)) {
908 return SPV_ERROR_INVALID_TEXT;
909 }
910 extInstType = inst.extInstType;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100911
Andrew Woloszyn13804e52015-09-22 15:50:33 -0400912 instructions.push_back(inst);
913
914 if (spvTextAdvance(text, &position)) break;
Lei Zhang40056702015-09-11 14:31:27 -0400915 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100916 }
917
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100918 size_t totalSize = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400919 for (auto& inst : instructions) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100920 totalSize += inst.wordCount;
921 }
922
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400923 uint32_t* data = new uint32_t[totalSize];
Lei Zhang40056702015-09-11 14:31:27 -0400924 if (!data) return SPV_ERROR_OUT_OF_MEMORY;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100925 uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400926 for (auto& inst : instructions) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100927 memcpy(data + currentIndex, inst.words, sizeof(uint32_t) * inst.wordCount);
928 currentIndex += inst.wordCount;
929 }
930
931 spv_binary binary = new spv_binary_t();
Lei Zhang40056702015-09-11 14:31:27 -0400932 if (!binary) {
933 delete[] data;
934 return SPV_ERROR_OUT_OF_MEMORY;
935 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100936 binary->code = data;
937 binary->wordCount = totalSize;
938
939 spv_result_t error = spvBinaryHeaderSet(binary, bound);
Lei Zhang40056702015-09-11 14:31:27 -0400940 if (error) {
941 spvBinaryDestroy(binary);
942 return error;
943 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100944
945 *pBinary = binary;
946
947 return SPV_SUCCESS;
948}
949
Lei Zhange78a7c12015-09-10 17:07:21 -0400950} // anonymous namespace
David Netoc9786432015-09-01 18:05:14 -0400951
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400952spv_result_t spvTextToBinary(const char* input_text,
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400953 const uint64_t input_text_size,
David Netoc9786432015-09-01 18:05:14 -0400954 const spv_opcode_table opcodeTable,
955 const spv_operand_table operandTable,
956 const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400957 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Lei Zhang06efdc52015-09-10 14:00:00 -0400958 return spvTextWithFormatToBinary(
959 input_text, input_text_size, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT,
960 opcodeTable, operandTable, extInstTable, pBinary, pDiagnostic);
961}
962
963spv_result_t spvTextWithFormatToBinary(
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400964 const char* input_text, const uint64_t input_text_size,
Lei Zhang06efdc52015-09-10 14:00:00 -0400965 spv_assembly_syntax_format_t format, const spv_opcode_table opcodeTable,
966 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Dejan Mircevski903f9d62015-09-28 17:04:39 -0400967 spv_binary* pBinary, spv_diagnostic* pDiagnostic) {
Andrew Woloszyncfeac482015-09-09 13:04:32 -0400968 spv_text_t text = {input_text, input_text_size};
969
Lei Zhang06efdc52015-09-10 14:00:00 -0400970 spv_result_t result =
971 spvTextToBinaryInternal(&text, format, opcodeTable, operandTable,
972 extInstTable, pBinary, pDiagnostic);
David Netoc9786432015-09-01 18:05:14 -0400973 if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
974
975 return result;
976}
977
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100978void spvTextDestroy(spv_text text) {
Lei Zhang40056702015-09-11 14:31:27 -0400979 if (!text) return;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100980 if (text->str) {
981 delete[] text->str;
982 }
983 delete text;
984}