Coverage Report

Created: 2025-07-12 07:43

/src/shaderc/third_party/spirv-tools/source/assembly_grammar.h
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
#ifndef SOURCE_ASSEMBLY_GRAMMAR_H_
16
#define SOURCE_ASSEMBLY_GRAMMAR_H_
17
18
#include "source/enum_set.h"
19
#include "source/latest_version_spirv_header.h"
20
#include "source/operand.h"
21
#include "source/table.h"
22
#include "source/util/span.h"
23
#include "spirv-tools/libspirv.h"
24
25
namespace spvtools {
26
27
// Encapsulates the grammar to use for SPIR-V assembly.
28
// Contains methods to query for valid instructions and operands.
29
class AssemblyGrammar {
30
 public:
31
  explicit AssemblyGrammar(const spv_const_context context)
32
15.2k
      : target_env_(context->target_env) {}
33
34
  // Returns the SPIR-V target environment.
35
0
  spv_target_env target_env() const { return target_env_; }
36
37
  // Removes capabilities not available in the current target environment and
38
  // returns the rest.
39
  // TODO(crbug.com/266223071) Remove this.
40
  CapabilitySet filterCapsAgainstTargetEnv(const spv::Capability* cap_array,
41
                                           uint32_t count) const;
42
  // Removes capabilities not available in the current target environment and
43
  // returns the rest.
44
  CapabilitySet filterCapsAgainstTargetEnv(
45
283k
      const spvtools::utils::Span<const spv::Capability>& caps) const {
46
283k
    return filterCapsAgainstTargetEnv(caps.begin(),
47
283k
                                      static_cast<uint32_t>(caps.size()));
48
283k
  }
49
50
  // Finds operand entry in the grammar table and returns its name.
51
  // Returns "Unknown" if not found.
52
  const char* lookupOperandName(spv_operand_type_t type,
53
                                uint32_t operand) const;
54
55
  // Finds the opcode for the given OpSpecConstantOp opcode name. The name
56
  // should not have the "Op" prefix.  For example, "IAdd" corresponds to
57
  // the integer add opcode for OpSpecConstantOp.  On success, returns
58
  // SPV_SUCCESS and sends the discovered operation code through the opcode
59
  // parameter.  On failure, returns SPV_ERROR_INVALID_LOOKUP.
60
  spv_result_t lookupSpecConstantOpcode(const char* name,
61
                                        spv::Op* opcode) const;
62
63
  // Returns SPV_SUCCESS if the given opcode is valid as the opcode operand
64
  // to OpSpecConstantOp.
65
  spv_result_t lookupSpecConstantOpcode(spv::Op opcode) const;
66
67
  // Parses a mask expression string for the given operand type.
68
  //
69
  // A mask expression is a sequence of one or more terms separated by '|',
70
  // where each term is a named enum value for a given type. No whitespace
71
  // is permitted.
72
  //
73
  // On success, the value is written to pValue, and SPV_SUCCESS is returned.
74
  // The operand type is defined by the type parameter, and the text to be
75
  // parsed is defined by the textValue parameter.
76
  spv_result_t parseMaskOperand(const spv_operand_type_t type,
77
                                const char* textValue, uint32_t* pValue) const;
78
79
  // Inserts the operands expected after the given typed mask onto the end
80
  // of the given pattern.
81
  //
82
  // Each set bit in the mask represents zero or more operand types that
83
  // should be appended onto the pattern. Operands for a less significant
84
  // bit must always match before operands for a more significant bit, so
85
  // the operands for a less significant bit must appear closer to the end
86
  // of the pattern stack.
87
  //
88
  // If a set bit is unknown, then we assume it has no operands.
89
  void pushOperandTypesForMask(const spv_operand_type_t type,
90
                               const uint32_t mask,
91
                               spv_operand_pattern_t* pattern) const;
92
93
 private:
94
  const spv_target_env target_env_;
95
};
96
97
}  // namespace spvtools
98
99
#endif  // SOURCE_ASSEMBLY_GRAMMAR_H_