Coverage Report

Created: 2025-06-13 06:48

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