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