• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2015-2016 The Khronos Group Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "source/text.h"
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <cctype>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstring>
23 #include <memory>
24 #include <set>
25 #include <sstream>
26 #include <string>
27 #include <unordered_map>
28 #include <utility>
29 #include <vector>
30 
31 #include "source/assembly_grammar.h"
32 #include "source/binary.h"
33 #include "source/diagnostic.h"
34 #include "source/ext_inst.h"
35 #include "source/instruction.h"
36 #include "source/opcode.h"
37 #include "source/operand.h"
38 #include "source/spirv_constant.h"
39 #include "source/spirv_target_env.h"
40 #include "source/table.h"
41 #include "source/text_handler.h"
42 #include "source/util/bitutils.h"
43 #include "source/util/parse_number.h"
44 #include "spirv-tools/libspirv.h"
45 
spvIsValidIDCharacter(const char value)46 bool spvIsValidIDCharacter(const char value) {
47   return value == '_' || 0 != ::isalnum(value);
48 }
49 
50 // Returns true if the given string represents a valid ID name.
spvIsValidID(const char * textValue)51 bool spvIsValidID(const char* textValue) {
52   const char* c = textValue;
53   for (; *c != '\0'; ++c) {
54     if (!spvIsValidIDCharacter(*c)) {
55       return false;
56     }
57   }
58   // If the string was empty, then the ID also is not valid.
59   return c != textValue;
60 }
61 
62 // Text API
63 
spvTextToLiteral(const char * textValue,spv_literal_t * pLiteral)64 spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) {
65   bool isSigned = false;
66   int numPeriods = 0;
67   bool isString = false;
68 
69   const size_t len = strlen(textValue);
70   if (len == 0) return SPV_FAILED_MATCH;
71 
72   for (uint64_t index = 0; index < len; ++index) {
73     switch (textValue[index]) {
74       case '0':
75       case '1':
76       case '2':
77       case '3':
78       case '4':
79       case '5':
80       case '6':
81       case '7':
82       case '8':
83       case '9':
84         break;
85       case '.':
86         numPeriods++;
87         break;
88       case '-':
89         if (index == 0) {
90           isSigned = true;
91         } else {
92           isString = true;
93         }
94         break;
95       default:
96         isString = true;
97         index = len;  // break out of the loop too.
98         break;
99     }
100   }
101 
102   pLiteral->type = spv_literal_type_t(99);
103 
104   if (isString || numPeriods > 1 || (isSigned && len == 1)) {
105     if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
106       return SPV_FAILED_MATCH;
107     bool escaping = false;
108     for (const char* val = textValue + 1; val != textValue + len - 1; ++val) {
109       if ((*val == '\\') && (!escaping)) {
110         escaping = true;
111       } else {
112         // Have to save space for the null-terminator
113         if (pLiteral->str.size() >= SPV_LIMIT_LITERAL_STRING_BYTES_MAX)
114           return SPV_ERROR_OUT_OF_MEMORY;
115         pLiteral->str.push_back(*val);
116         escaping = false;
117       }
118     }
119 
120     pLiteral->type = SPV_LITERAL_TYPE_STRING;
121   } else if (numPeriods == 1) {
122     double d = std::strtod(textValue, nullptr);
123     float f = (float)d;
124     if (d == (double)f) {
125       pLiteral->type = SPV_LITERAL_TYPE_FLOAT_32;
126       pLiteral->value.f = f;
127     } else {
128       pLiteral->type = SPV_LITERAL_TYPE_FLOAT_64;
129       pLiteral->value.d = d;
130     }
131   } else if (isSigned) {
132     int64_t i64 = strtoll(textValue, nullptr, 10);
133     int32_t i32 = (int32_t)i64;
134     if (i64 == (int64_t)i32) {
135       pLiteral->type = SPV_LITERAL_TYPE_INT_32;
136       pLiteral->value.i32 = i32;
137     } else {
138       pLiteral->type = SPV_LITERAL_TYPE_INT_64;
139       pLiteral->value.i64 = i64;
140     }
141   } else {
142     uint64_t u64 = strtoull(textValue, nullptr, 10);
143     uint32_t u32 = (uint32_t)u64;
144     if (u64 == (uint64_t)u32) {
145       pLiteral->type = SPV_LITERAL_TYPE_UINT_32;
146       pLiteral->value.u32 = u32;
147     } else {
148       pLiteral->type = SPV_LITERAL_TYPE_UINT_64;
149       pLiteral->value.u64 = u64;
150     }
151   }
152 
153   return SPV_SUCCESS;
154 }
155 
156 namespace {
157 
158 /// Parses an immediate integer from text, guarding against overflow.  If
159 /// successful, adds the parsed value to pInst, advances the context past it,
160 /// and returns SPV_SUCCESS.  Otherwise, leaves pInst alone, emits diagnostics,
161 /// and returns SPV_ERROR_INVALID_TEXT.
encodeImmediate(spvtools::AssemblyContext * context,const char * text,spv_instruction_t * pInst)162 spv_result_t encodeImmediate(spvtools::AssemblyContext* context,
163                              const char* text, spv_instruction_t* pInst) {
164   assert(*text == '!');
165   uint32_t parse_result;
166   if (!spvtools::utils::ParseNumber(text + 1, &parse_result)) {
167     return context->diagnostic(SPV_ERROR_INVALID_TEXT)
168            << "Invalid immediate integer: !" << text + 1;
169   }
170   context->binaryEncodeU32(parse_result, pInst);
171   context->seekForward(static_cast<uint32_t>(strlen(text)));
172   return SPV_SUCCESS;
173 }
174 
175 }  // anonymous namespace
176 
177 /// @brief Translate an Opcode operand to binary form
178 ///
179 /// @param[in] grammar the grammar to use for compilation
180 /// @param[in, out] context the dynamic compilation info
181 /// @param[in] type of the operand
182 /// @param[in] textValue word of text to be parsed
183 /// @param[out] pInst return binary Opcode
184 /// @param[in,out] pExpectedOperands the operand types expected
185 ///
186 /// @return result code
spvTextEncodeOperand(const spvtools::AssemblyGrammar & grammar,spvtools::AssemblyContext * context,const spv_operand_type_t type,const char * textValue,spv_instruction_t * pInst,spv_operand_pattern_t * pExpectedOperands)187 spv_result_t spvTextEncodeOperand(const spvtools::AssemblyGrammar& grammar,
188                                   spvtools::AssemblyContext* context,
189                                   const spv_operand_type_t type,
190                                   const char* textValue,
191                                   spv_instruction_t* pInst,
192                                   spv_operand_pattern_t* pExpectedOperands) {
193   // NOTE: Handle immediate int in the stream
194   if ('!' == textValue[0]) {
195     if (auto error = encodeImmediate(context, textValue, pInst)) {
196       return error;
197     }
198     *pExpectedOperands =
199         spvAlternatePatternFollowingImmediate(*pExpectedOperands);
200     return SPV_SUCCESS;
201   }
202 
203   // Optional literal operands can fail to parse. In that case use
204   // SPV_FAILED_MATCH to avoid emitting a diagostic.  Use the following
205   // for those situations.
206   spv_result_t error_code_for_literals =
207       spvOperandIsOptional(type) ? SPV_FAILED_MATCH : SPV_ERROR_INVALID_TEXT;
208 
209   switch (type) {
210     case SPV_OPERAND_TYPE_ID:
211     case SPV_OPERAND_TYPE_TYPE_ID:
212     case SPV_OPERAND_TYPE_RESULT_ID:
213     case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
214     case SPV_OPERAND_TYPE_SCOPE_ID:
215     case SPV_OPERAND_TYPE_OPTIONAL_ID: {
216       if ('%' == textValue[0]) {
217         textValue++;
218       } else {
219         return context->diagnostic() << "Expected id to start with %.";
220       }
221       if (!spvIsValidID(textValue)) {
222         return context->diagnostic() << "Invalid ID " << textValue;
223       }
224       const uint32_t id = context->spvNamedIdAssignOrGet(textValue);
225       if (type == SPV_OPERAND_TYPE_TYPE_ID) pInst->resultTypeId = id;
226       spvInstructionAddWord(pInst, id);
227 
228       // Set the extended instruction type.
229       // The import set id is the 3rd operand of OpExtInst.
230       if (spv::Op(pInst->opcode) == spv::Op::OpExtInst &&
231           pInst->words.size() == 4) {
232         auto ext_inst_type = context->getExtInstTypeForId(pInst->words[3]);
233         if (ext_inst_type == SPV_EXT_INST_TYPE_NONE) {
234           return context->diagnostic()
235                  << "Invalid extended instruction import Id "
236                  << pInst->words[2];
237         }
238         pInst->extInstType = ext_inst_type;
239       }
240     } break;
241 
242     case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: {
243       // The assembler accepts the symbolic name for an extended instruction,
244       // and emits its corresponding number.
245       spv_ext_inst_desc extInst;
246       if (grammar.lookupExtInst(pInst->extInstType, textValue, &extInst) ==
247           SPV_SUCCESS) {
248         // if we know about this extended instruction, push the numeric value
249         spvInstructionAddWord(pInst, extInst->ext_inst);
250 
251         // Prepare to parse the operands for the extended instructions.
252         spvPushOperandTypes(extInst->operandTypes, pExpectedOperands);
253       } else {
254         // if we don't know this extended instruction and the set isn't
255         // non-semantic, we cannot process further
256         if (!spvExtInstIsNonSemantic(pInst->extInstType)) {
257           return context->diagnostic()
258                  << "Invalid extended instruction name '" << textValue << "'.";
259         } else {
260           // for non-semantic instruction sets, as long as the text name is an
261           // integer value we can encode it since we know the form of all such
262           // extended instructions
263           spv_literal_t extInstValue;
264           if (spvTextToLiteral(textValue, &extInstValue) ||
265               extInstValue.type != SPV_LITERAL_TYPE_UINT_32) {
266             return context->diagnostic()
267                    << "Couldn't translate unknown extended instruction name '"
268                    << textValue << "' to unsigned integer.";
269           }
270 
271           spvInstructionAddWord(pInst, extInstValue.value.u32);
272 
273           // opcode contains an unknown number of IDs.
274           pExpectedOperands->push_back(SPV_OPERAND_TYPE_VARIABLE_ID);
275         }
276       }
277     } break;
278 
279     case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
280       // The assembler accepts the symbolic name for the opcode, but without
281       // the "Op" prefix.  For example, "IAdd" is accepted.  The number
282       // of the opcode is emitted.
283       spv::Op opcode;
284       if (grammar.lookupSpecConstantOpcode(textValue, &opcode)) {
285         return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
286                                      << " '" << textValue << "'.";
287       }
288       spv_opcode_desc opcodeEntry = nullptr;
289       if (grammar.lookupOpcode(opcode, &opcodeEntry)) {
290         return context->diagnostic(SPV_ERROR_INTERNAL)
291                << "OpSpecConstant opcode table out of sync";
292       }
293       spvInstructionAddWord(pInst, uint32_t(opcodeEntry->opcode));
294 
295       // Prepare to parse the operands for the opcode.  Except skip the
296       // type Id and result Id, since they've already been processed.
297       assert(opcodeEntry->hasType);
298       assert(opcodeEntry->hasResult);
299       assert(opcodeEntry->numTypes >= 2);
300       spvPushOperandTypes(opcodeEntry->operandTypes + 2, pExpectedOperands);
301     } break;
302 
303     case SPV_OPERAND_TYPE_LITERAL_INTEGER:
304     case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: {
305       // The current operand is an *unsigned* 32-bit integer.
306       // That's just how the grammar works.
307       spvtools::IdType expected_type = {
308           32, false, spvtools::IdTypeClass::kScalarIntegerType};
309       if (auto error = context->binaryEncodeNumericLiteral(
310               textValue, error_code_for_literals, expected_type, pInst)) {
311         return error;
312       }
313     } break;
314 
315     case SPV_OPERAND_TYPE_LITERAL_FLOAT: {
316       // The current operand is a 32-bit float.
317       // That's just how the grammar works.
318       spvtools::IdType expected_type = {
319           32, false, spvtools::IdTypeClass::kScalarFloatType};
320       if (auto error = context->binaryEncodeNumericLiteral(
321               textValue, error_code_for_literals, expected_type, pInst)) {
322         return error;
323       }
324     } break;
325 
326     case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER:
327       // This is a context-independent literal number which can be a 32-bit
328       // number of floating point value.
329       if (auto error = context->binaryEncodeNumericLiteral(
330               textValue, error_code_for_literals, spvtools::kUnknownType,
331               pInst)) {
332         return error;
333       }
334       break;
335 
336     case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER:
337     case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: {
338       spvtools::IdType expected_type = spvtools::kUnknownType;
339       // The encoding for OpConstant, OpSpecConstant and OpSwitch all
340       // depend on either their own result-id or the result-id of
341       // one of their parameters.
342       if (spv::Op::OpConstant == pInst->opcode ||
343           spv::Op::OpSpecConstant == pInst->opcode) {
344         // The type of the literal is determined by the type Id of the
345         // instruction.
346         expected_type =
347             context->getTypeOfTypeGeneratingValue(pInst->resultTypeId);
348         if (!spvtools::isScalarFloating(expected_type) &&
349             !spvtools::isScalarIntegral(expected_type)) {
350           spv_opcode_desc d;
351           const char* opcode_name = "opcode";
352           if (SPV_SUCCESS == grammar.lookupOpcode(pInst->opcode, &d)) {
353             opcode_name = d->name;
354           }
355           return context->diagnostic()
356                  << "Type for " << opcode_name
357                  << " must be a scalar floating point or integer type";
358         }
359       } else if (pInst->opcode == spv::Op::OpSwitch) {
360         // The type of the literal is the same as the type of the selector.
361         expected_type = context->getTypeOfValueInstruction(pInst->words[1]);
362         if (!spvtools::isScalarIntegral(expected_type)) {
363           return context->diagnostic()
364                  << "The selector operand for OpSwitch must be the result"
365                     " of an instruction that generates an integer scalar";
366         }
367       }
368       if (auto error = context->binaryEncodeNumericLiteral(
369               textValue, error_code_for_literals, expected_type, pInst)) {
370         return error;
371       }
372     } break;
373 
374     case SPV_OPERAND_TYPE_LITERAL_STRING:
375     case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: {
376       spv_literal_t literal = {};
377       spv_result_t error = spvTextToLiteral(textValue, &literal);
378       if (error != SPV_SUCCESS) {
379         if (error == SPV_ERROR_OUT_OF_MEMORY) return error;
380         return context->diagnostic(error_code_for_literals)
381                << "Invalid literal string '" << textValue << "'.";
382       }
383       if (literal.type != SPV_LITERAL_TYPE_STRING) {
384         return context->diagnostic()
385                << "Expected literal string, found literal number '" << textValue
386                << "'.";
387       }
388 
389       // NOTE: Special case for extended instruction library import
390       if (spv::Op::OpExtInstImport == pInst->opcode) {
391         const spv_ext_inst_type_t ext_inst_type =
392             spvExtInstImportTypeGet(literal.str.c_str());
393         if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) {
394           return context->diagnostic()
395                  << "Invalid extended instruction import '" << literal.str
396                  << "'";
397         }
398         if ((error = context->recordIdAsExtInstImport(pInst->words[1],
399                                                       ext_inst_type)))
400           return error;
401       }
402 
403       if (context->binaryEncodeString(literal.str.c_str(), pInst))
404         return SPV_ERROR_INVALID_TEXT;
405     } break;
406 
407     // Masks.
408     case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE:
409     case SPV_OPERAND_TYPE_FUNCTION_CONTROL:
410     case SPV_OPERAND_TYPE_LOOP_CONTROL:
411     case SPV_OPERAND_TYPE_IMAGE:
412     case SPV_OPERAND_TYPE_OPTIONAL_IMAGE:
413     case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS:
414     case SPV_OPERAND_TYPE_OPTIONAL_RAW_ACCESS_CHAIN_OPERANDS:
415     case SPV_OPERAND_TYPE_SELECTION_CONTROL:
416     case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS:
417     case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS:
418     case SPV_OPERAND_TYPE_OPTIONAL_COOPERATIVE_MATRIX_OPERANDS: {
419       uint32_t value;
420       if (auto error = grammar.parseMaskOperand(type, textValue, &value)) {
421         return context->diagnostic(error)
422                << "Invalid " << spvOperandTypeStr(type) << " operand '"
423                << textValue << "'.";
424       }
425       if (auto error = context->binaryEncodeU32(value, pInst)) return error;
426       // Prepare to parse the operands for this logical operand.
427       grammar.pushOperandTypesForMask(type, value, pExpectedOperands);
428     } break;
429     case SPV_OPERAND_TYPE_OPTIONAL_CIV: {
430       auto error = spvTextEncodeOperand(
431           grammar, context, SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, textValue,
432           pInst, pExpectedOperands);
433       if (error == SPV_FAILED_MATCH) {
434         // It's not a literal number -- is it a literal string?
435         error = spvTextEncodeOperand(grammar, context,
436                                      SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
437                                      textValue, pInst, pExpectedOperands);
438       }
439       if (error == SPV_FAILED_MATCH) {
440         // It's not a literal -- is it an ID?
441         error =
442             spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_OPTIONAL_ID,
443                                  textValue, pInst, pExpectedOperands);
444       }
445       if (error) {
446         return context->diagnostic(error)
447                << "Invalid word following !<integer>: " << textValue;
448       }
449       if (pExpectedOperands->empty()) {
450         pExpectedOperands->push_back(SPV_OPERAND_TYPE_OPTIONAL_CIV);
451       }
452     } break;
453     default: {
454       // NOTE: All non literal operands are handled here using the operand
455       // table.
456       spv_operand_desc entry;
457       if (grammar.lookupOperand(type, textValue, strlen(textValue), &entry)) {
458         return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
459                                      << " '" << textValue << "'.";
460       }
461       if (context->binaryEncodeU32(entry->value, pInst)) {
462         return context->diagnostic() << "Invalid " << spvOperandTypeStr(type)
463                                      << " '" << textValue << "'.";
464       }
465 
466       // Prepare to parse the operands for this logical operand.
467       spvPushOperandTypes(entry->operandTypes, pExpectedOperands);
468     } break;
469   }
470   return SPV_SUCCESS;
471 }
472 
473 namespace {
474 
475 /// Encodes an instruction started by !<integer> at the given position in text.
476 ///
477 /// Puts the encoded words into *pInst.  If successful, moves position past the
478 /// instruction and returns SPV_SUCCESS.  Otherwise, returns an error code and
479 /// leaves position pointing to the error in text.
encodeInstructionStartingWithImmediate(const spvtools::AssemblyGrammar & grammar,spvtools::AssemblyContext * context,spv_instruction_t * pInst)480 spv_result_t encodeInstructionStartingWithImmediate(
481     const spvtools::AssemblyGrammar& grammar,
482     spvtools::AssemblyContext* context, spv_instruction_t* pInst) {
483   std::string firstWord;
484   spv_position_t nextPosition = {};
485   auto error = context->getWord(&firstWord, &nextPosition);
486   if (error) return context->diagnostic(error) << "Internal Error";
487 
488   if ((error = encodeImmediate(context, firstWord.c_str(), pInst))) {
489     return error;
490   }
491   while (context->advance() != SPV_END_OF_STREAM) {
492     // A beginning of a new instruction means we're done.
493     if (context->isStartOfNewInst()) return SPV_SUCCESS;
494 
495     // Otherwise, there must be an operand that's either a literal, an ID, or
496     // an immediate.
497     std::string operandValue;
498     if ((error = context->getWord(&operandValue, &nextPosition)))
499       return context->diagnostic(error) << "Internal Error";
500 
501     if (operandValue == "=")
502       return context->diagnostic() << firstWord << " not allowed before =.";
503 
504     // Needed to pass to spvTextEncodeOpcode(), but it shouldn't ever be
505     // expanded.
506     spv_operand_pattern_t dummyExpectedOperands;
507     error = spvTextEncodeOperand(
508         grammar, context, SPV_OPERAND_TYPE_OPTIONAL_CIV, operandValue.c_str(),
509         pInst, &dummyExpectedOperands);
510     if (error) return error;
511     context->setPosition(nextPosition);
512   }
513   return SPV_SUCCESS;
514 }
515 
516 /// @brief Translate single Opcode and operands to binary form
517 ///
518 /// @param[in] grammar the grammar to use for compilation
519 /// @param[in, out] context the dynamic compilation info
520 /// @param[in] text stream to translate
521 /// @param[out] pInst returned binary Opcode
522 /// @param[in,out] pPosition in the text stream
523 ///
524 /// @return result code
spvTextEncodeOpcode(const spvtools::AssemblyGrammar & grammar,spvtools::AssemblyContext * context,spv_instruction_t * pInst)525 spv_result_t spvTextEncodeOpcode(const spvtools::AssemblyGrammar& grammar,
526                                  spvtools::AssemblyContext* context,
527                                  spv_instruction_t* pInst) {
528   // Check for !<integer> first.
529   if ('!' == context->peek()) {
530     return encodeInstructionStartingWithImmediate(grammar, context, pInst);
531   }
532 
533   std::string firstWord;
534   spv_position_t nextPosition = {};
535   spv_result_t error = context->getWord(&firstWord, &nextPosition);
536   if (error) return context->diagnostic() << "Internal Error";
537 
538   std::string opcodeName;
539   std::string result_id;
540   spv_position_t result_id_position = {};
541   if (context->startsWithOp()) {
542     opcodeName = firstWord;
543   } else {
544     result_id = firstWord;
545     if ('%' != result_id.front()) {
546       return context->diagnostic()
547              << "Expected <opcode> or <result-id> at the beginning "
548                 "of an instruction, found '"
549              << result_id << "'.";
550     }
551     result_id_position = context->position();
552 
553     // The '=' sign.
554     context->setPosition(nextPosition);
555     if (context->advance())
556       return context->diagnostic() << "Expected '=', found end of stream.";
557     std::string equal_sign;
558     error = context->getWord(&equal_sign, &nextPosition);
559     if ("=" != equal_sign)
560       return context->diagnostic() << "'=' expected after result id but found '"
561                                    << equal_sign << "'.";
562 
563     // The <opcode> after the '=' sign.
564     context->setPosition(nextPosition);
565     if (context->advance())
566       return context->diagnostic() << "Expected opcode, found end of stream.";
567     error = context->getWord(&opcodeName, &nextPosition);
568     if (error) return context->diagnostic(error) << "Internal Error";
569     if (!context->startsWithOp()) {
570       return context->diagnostic()
571              << "Invalid Opcode prefix '" << opcodeName << "'.";
572     }
573   }
574 
575   // NOTE: The table contains Opcode names without the "Op" prefix.
576   const char* pInstName = opcodeName.data() + 2;
577 
578   spv_opcode_desc opcodeEntry;
579   error = grammar.lookupOpcode(pInstName, &opcodeEntry);
580   if (error) {
581     return context->diagnostic(error)
582            << "Invalid Opcode name '" << opcodeName << "'";
583   }
584   if (opcodeEntry->hasResult && result_id.empty()) {
585     return context->diagnostic()
586            << "Expected <result-id> at the beginning of an instruction, found '"
587            << firstWord << "'.";
588   }
589   if (!opcodeEntry->hasResult && !result_id.empty()) {
590     return context->diagnostic()
591            << "Cannot set ID " << result_id << " because " << opcodeName
592            << " does not produce a result ID.";
593   }
594   pInst->opcode = opcodeEntry->opcode;
595   context->setPosition(nextPosition);
596   // Reserve the first word for the instruction.
597   spvInstructionAddWord(pInst, 0);
598 
599   // Maintains the ordered list of expected operand types.
600   // For many instructions we only need the {numTypes, operandTypes}
601   // entries in opcodeEntry.  However, sometimes we need to modify
602   // the list as we parse the operands. This occurs when an operand
603   // has its own logical operands (such as the LocalSize operand for
604   // ExecutionMode), or for extended instructions that may have their
605   // own operands depending on the selected extended instruction.
606   spv_operand_pattern_t expectedOperands;
607   expectedOperands.reserve(opcodeEntry->numTypes);
608   for (auto i = 0; i < opcodeEntry->numTypes; i++)
609     expectedOperands.push_back(
610         opcodeEntry->operandTypes[opcodeEntry->numTypes - i - 1]);
611 
612   while (!expectedOperands.empty()) {
613     const spv_operand_type_t type = expectedOperands.back();
614     expectedOperands.pop_back();
615 
616     // Expand optional tuples lazily.
617     if (spvExpandOperandSequenceOnce(type, &expectedOperands)) continue;
618 
619     if (type == SPV_OPERAND_TYPE_RESULT_ID && !result_id.empty()) {
620       // Handle the <result-id> for value generating instructions.
621       // We've already consumed it from the text stream.  Here
622       // we inject its words into the instruction.
623       spv_position_t temp_pos = context->position();
624       error = spvTextEncodeOperand(grammar, context, SPV_OPERAND_TYPE_RESULT_ID,
625                                    result_id.c_str(), pInst, nullptr);
626       result_id_position = context->position();
627       // Because we are injecting we have to reset the position afterwards.
628       context->setPosition(temp_pos);
629       if (error) return error;
630     } else {
631       // Find the next word.
632       error = context->advance();
633       if (error == SPV_END_OF_STREAM) {
634         if (spvOperandIsOptional(type)) {
635           // This would have been the last potential operand for the
636           // instruction,
637           // and we didn't find one.  We're finished parsing this instruction.
638           break;
639         } else {
640           return context->diagnostic()
641                  << "Expected operand for " << opcodeName
642                  << " instruction, but found the end of the stream.";
643         }
644       }
645       assert(error == SPV_SUCCESS && "Somebody added another way to fail");
646 
647       if (context->isStartOfNewInst()) {
648         if (spvOperandIsOptional(type)) {
649           break;
650         } else {
651           return context->diagnostic()
652                  << "Expected operand for " << opcodeName
653                  << " instruction, but found the next instruction instead.";
654         }
655       }
656 
657       std::string operandValue;
658       error = context->getWord(&operandValue, &nextPosition);
659       if (error) return context->diagnostic(error) << "Internal Error";
660 
661       error = spvTextEncodeOperand(grammar, context, type, operandValue.c_str(),
662                                    pInst, &expectedOperands);
663 
664       if (error == SPV_FAILED_MATCH && spvOperandIsOptional(type))
665         return SPV_SUCCESS;
666 
667       if (error) return error;
668 
669       context->setPosition(nextPosition);
670     }
671   }
672 
673   if (spvOpcodeGeneratesType(pInst->opcode)) {
674     if (context->recordTypeDefinition(pInst) != SPV_SUCCESS) {
675       return SPV_ERROR_INVALID_TEXT;
676     }
677   } else if (opcodeEntry->hasType) {
678     // SPIR-V dictates that if an instruction has both a return value and a
679     // type ID then the type id is first, and the return value is second.
680     assert(opcodeEntry->hasResult &&
681            "Unknown opcode: has a type but no result.");
682     context->recordTypeIdForValue(pInst->words[2], pInst->words[1]);
683   }
684 
685   if (pInst->words.size() > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
686     return context->diagnostic()
687            << opcodeName << " Instruction too long: " << pInst->words.size()
688            << " words, but the limit is "
689            << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX;
690   }
691 
692   pInst->words[0] =
693       spvOpcodeMake(uint16_t(pInst->words.size()), opcodeEntry->opcode);
694 
695   return SPV_SUCCESS;
696 }
697 
698 enum { kAssemblerVersion = 0 };
699 
700 // Populates a binary stream's |header|. The target environment is specified via
701 // |env| and Id bound is via |bound|.
SetHeader(spv_target_env env,const uint32_t bound,uint32_t * header)702 spv_result_t SetHeader(spv_target_env env, const uint32_t bound,
703                        uint32_t* header) {
704   if (!header) return SPV_ERROR_INVALID_BINARY;
705 
706   header[SPV_INDEX_MAGIC_NUMBER] = spv::MagicNumber;
707   header[SPV_INDEX_VERSION_NUMBER] = spvVersionForTargetEnv(env);
708   header[SPV_INDEX_GENERATOR_NUMBER] =
709       SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS_ASSEMBLER, kAssemblerVersion);
710   header[SPV_INDEX_BOUND] = bound;
711   header[SPV_INDEX_SCHEMA] = 0;  // NOTE: Reserved
712 
713   return SPV_SUCCESS;
714 }
715 
716 // Collects all numeric ids in the module source into |numeric_ids|.
717 // This function is essentially a dry-run of spvTextToBinary.
GetNumericIds(const spvtools::AssemblyGrammar & grammar,const spvtools::MessageConsumer & consumer,const spv_text text,std::set<uint32_t> * numeric_ids)718 spv_result_t GetNumericIds(const spvtools::AssemblyGrammar& grammar,
719                            const spvtools::MessageConsumer& consumer,
720                            const spv_text text,
721                            std::set<uint32_t>* numeric_ids) {
722   spvtools::AssemblyContext context(text, consumer);
723 
724   if (!text->str) return context.diagnostic() << "Missing assembly text.";
725 
726   if (!grammar.isValid()) {
727     return SPV_ERROR_INVALID_TABLE;
728   }
729 
730   // Skip past whitespace and comments.
731   context.advance();
732 
733   while (context.hasText()) {
734     spv_instruction_t inst;
735 
736     // Operand parsing sometimes involves knowing the opcode of the instruction
737     // being parsed. A malformed input might feature such an operand *before*
738     // the opcode is known. To guard against accessing an uninitialized opcode,
739     // the instruction's opcode is initialized to a default value.
740     inst.opcode = spv::Op::Max;
741 
742     if (spvTextEncodeOpcode(grammar, &context, &inst)) {
743       return SPV_ERROR_INVALID_TEXT;
744     }
745 
746     if (context.advance()) break;
747   }
748 
749   *numeric_ids = context.GetNumericIds();
750   return SPV_SUCCESS;
751 }
752 
753 // Translates a given assembly language module into binary form.
754 // If a diagnostic is generated, it is not yet marked as being
755 // for a text-based input.
spvTextToBinaryInternal(const spvtools::AssemblyGrammar & grammar,const spvtools::MessageConsumer & consumer,const spv_text text,const uint32_t options,spv_binary * pBinary)756 spv_result_t spvTextToBinaryInternal(const spvtools::AssemblyGrammar& grammar,
757                                      const spvtools::MessageConsumer& consumer,
758                                      const spv_text text,
759                                      const uint32_t options,
760                                      spv_binary* pBinary) {
761   // The ids in this set will have the same values both in source and binary.
762   // All other ids will be generated by filling in the gaps.
763   std::set<uint32_t> ids_to_preserve;
764 
765   if (options & SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS) {
766     // Collect all numeric ids from the source into ids_to_preserve.
767     const spv_result_t result =
768         GetNumericIds(grammar, consumer, text, &ids_to_preserve);
769     if (result != SPV_SUCCESS) return result;
770   }
771 
772   spvtools::AssemblyContext context(text, consumer, std::move(ids_to_preserve));
773 
774   if (!text->str) return context.diagnostic() << "Missing assembly text.";
775 
776   if (!grammar.isValid()) {
777     return SPV_ERROR_INVALID_TABLE;
778   }
779   if (!pBinary) return SPV_ERROR_INVALID_POINTER;
780 
781   std::vector<spv_instruction_t> instructions;
782 
783   // Skip past whitespace and comments.
784   context.advance();
785 
786   while (context.hasText()) {
787     instructions.push_back({});
788     spv_instruction_t& inst = instructions.back();
789 
790     if (auto error = spvTextEncodeOpcode(grammar, &context, &inst)) {
791       return error;
792     }
793 
794     if (context.advance()) break;
795   }
796 
797   size_t totalSize = SPV_INDEX_INSTRUCTION;
798   for (auto& inst : instructions) {
799     totalSize += inst.words.size();
800   }
801 
802   uint32_t* data = new uint32_t[totalSize];
803   if (!data) return SPV_ERROR_OUT_OF_MEMORY;
804   uint64_t currentIndex = SPV_INDEX_INSTRUCTION;
805   for (auto& inst : instructions) {
806     memcpy(data + currentIndex, inst.words.data(),
807            sizeof(uint32_t) * inst.words.size());
808     currentIndex += inst.words.size();
809   }
810 
811   if (auto error = SetHeader(grammar.target_env(), context.getBound(), data))
812     return error;
813 
814   spv_binary binary = new spv_binary_t();
815   if (!binary) {
816     delete[] data;
817     return SPV_ERROR_OUT_OF_MEMORY;
818   }
819   binary->code = data;
820   binary->wordCount = totalSize;
821 
822   *pBinary = binary;
823 
824   return SPV_SUCCESS;
825 }
826 
827 }  // anonymous namespace
828 
spvTextToBinary(const spv_const_context context,const char * input_text,const size_t input_text_size,spv_binary * pBinary,spv_diagnostic * pDiagnostic)829 spv_result_t spvTextToBinary(const spv_const_context context,
830                              const char* input_text,
831                              const size_t input_text_size, spv_binary* pBinary,
832                              spv_diagnostic* pDiagnostic) {
833   return spvTextToBinaryWithOptions(context, input_text, input_text_size,
834                                     SPV_TEXT_TO_BINARY_OPTION_NONE, pBinary,
835                                     pDiagnostic);
836 }
837 
spvTextToBinaryWithOptions(const spv_const_context context,const char * input_text,const size_t input_text_size,const uint32_t options,spv_binary * pBinary,spv_diagnostic * pDiagnostic)838 spv_result_t spvTextToBinaryWithOptions(const spv_const_context context,
839                                         const char* input_text,
840                                         const size_t input_text_size,
841                                         const uint32_t options,
842                                         spv_binary* pBinary,
843                                         spv_diagnostic* pDiagnostic) {
844   spv_context_t hijack_context = *context;
845   if (pDiagnostic) {
846     *pDiagnostic = nullptr;
847     spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic);
848   }
849 
850   spv_text_t text = {input_text, input_text_size};
851   spvtools::AssemblyGrammar grammar(&hijack_context);
852 
853   spv_result_t result = spvTextToBinaryInternal(
854       grammar, hijack_context.consumer, &text, options, pBinary);
855   if (pDiagnostic && *pDiagnostic) (*pDiagnostic)->isTextSource = true;
856 
857   return result;
858 }
859 
spvTextDestroy(spv_text text)860 void spvTextDestroy(spv_text text) {
861   if (text) {
862     if (text->str) delete[] text->str;
863     delete text;
864   }
865 }
866