Update path-to-regexp to address CVE-2024-45296 (#5895)
[KhronosGroup/SPIRV-Tools.git] / source / assembly_grammar.h
blob36fdd08a6d6afba27d86969c96b6f502e758bb15
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.
15 #ifndef SOURCE_ASSEMBLY_GRAMMAR_H_
16 #define SOURCE_ASSEMBLY_GRAMMAR_H_
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 "spirv-tools/libspirv.h"
24 namespace spvtools {
26 // Encapsulates the grammar to use for SPIR-V assembly.
27 // Contains methods to query for valid instructions and operands.
28 class AssemblyGrammar {
29 public:
30 explicit AssemblyGrammar(const spv_const_context context)
31 : target_env_(context->target_env),
32 operandTable_(context->operand_table),
33 opcodeTable_(context->opcode_table),
34 extInstTable_(context->ext_inst_table) {}
36 // Returns true if the internal tables have been initialized with valid data.
37 bool isValid() const;
39 // Returns the SPIR-V target environment.
40 spv_target_env target_env() const { return target_env_; }
42 // Removes capabilities not available in the current target environment and
43 // returns the rest.
44 CapabilitySet filterCapsAgainstTargetEnv(const spv::Capability* cap_array,
45 uint32_t count) const;
47 // Fills in the desc parameter with the information about the opcode
48 // of the given name. Returns SPV_SUCCESS if the opcode was found, and
49 // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
50 spv_result_t lookupOpcode(const char* name, spv_opcode_desc* desc) const;
52 // Fills in the desc parameter with the information about the opcode
53 // of the valid. Returns SPV_SUCCESS if the opcode was found, and
54 // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
55 spv_result_t lookupOpcode(spv::Op opcode, spv_opcode_desc* desc) const;
57 // Fills in the desc parameter with the information about the given
58 // operand. Returns SPV_SUCCESS if the operand was found, and
59 // SPV_ERROR_INVALID_LOOKUP otherwise.
60 spv_result_t lookupOperand(spv_operand_type_t type, const char* name,
61 size_t name_len, spv_operand_desc* desc) const;
63 // Fills in the desc parameter with the information about the given
64 // operand. Returns SPV_SUCCESS if the operand was found, and
65 // SPV_ERROR_INVALID_LOOKUP otherwise.
66 spv_result_t lookupOperand(spv_operand_type_t type, uint32_t operand,
67 spv_operand_desc* desc) const;
69 // Finds operand entry in the grammar table and returns its name.
70 // Returns "Unknown" if not found.
71 const char* lookupOperandName(spv_operand_type_t type,
72 uint32_t operand) const {
73 spv_operand_desc desc = nullptr;
74 if (lookupOperand(type, operand, &desc) != SPV_SUCCESS || !desc) {
75 return "Unknown";
77 return desc->name;
80 // Finds the opcode for the given OpSpecConstantOp opcode name. The name
81 // should not have the "Op" prefix. For example, "IAdd" corresponds to
82 // the integer add opcode for OpSpecConstantOp. On success, returns
83 // SPV_SUCCESS and sends the discovered operation code through the opcode
84 // parameter. On failure, returns SPV_ERROR_INVALID_LOOKUP.
85 spv_result_t lookupSpecConstantOpcode(const char* name,
86 spv::Op* opcode) const;
88 // Returns SPV_SUCCESS if the given opcode is valid as the opcode operand
89 // to OpSpecConstantOp.
90 spv_result_t lookupSpecConstantOpcode(spv::Op opcode) const;
92 // Parses a mask expression string for the given operand type.
94 // A mask expression is a sequence of one or more terms separated by '|',
95 // where each term is a named enum value for a given type. No whitespace
96 // is permitted.
98 // On success, the value is written to pValue, and SPV_SUCCESS is returned.
99 // The operand type is defined by the type parameter, and the text to be
100 // parsed is defined by the textValue parameter.
101 spv_result_t parseMaskOperand(const spv_operand_type_t type,
102 const char* textValue, uint32_t* pValue) const;
104 // Writes the extended operand with the given type and text to the *extInst
105 // parameter.
106 // Returns SPV_SUCCESS if the value could be found.
107 spv_result_t lookupExtInst(spv_ext_inst_type_t type, const char* textValue,
108 spv_ext_inst_desc* extInst) const;
110 // Writes the extended operand with the given type and first encoded word
111 // to the *extInst parameter.
112 // Returns SPV_SUCCESS if the value could be found.
113 spv_result_t lookupExtInst(spv_ext_inst_type_t type, uint32_t firstWord,
114 spv_ext_inst_desc* extInst) const;
116 // Inserts the operands expected after the given typed mask onto the end
117 // of the given pattern.
119 // Each set bit in the mask represents zero or more operand types that
120 // should be appended onto the pattern. Operands for a less significant
121 // bit must always match before operands for a more significant bit, so
122 // the operands for a less significant bit must appear closer to the end
123 // of the pattern stack.
125 // If a set bit is unknown, then we assume it has no operands.
126 void pushOperandTypesForMask(const spv_operand_type_t type,
127 const uint32_t mask,
128 spv_operand_pattern_t* pattern) const;
130 private:
131 const spv_target_env target_env_;
132 const spv_operand_table operandTable_;
133 const spv_opcode_table opcodeTable_;
134 const spv_ext_inst_table extInstTable_;
137 } // namespace spvtools
139 #endif // SOURCE_ASSEMBLY_GRAMMAR_H_