• 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 #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 "spirv-tools/libspirv.h"
23 
24 namespace spvtools {
25 
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:
AssemblyGrammar(const spv_const_context context)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) {}
35 
36   // Returns true if the internal tables have been initialized with valid data.
37   bool isValid() const;
38 
39   // Returns the SPIR-V target environment.
target_env()40   spv_target_env target_env() const { return target_env_; }
41 
42   // Removes capabilities not available in the current target environment and
43   // returns the rest.
44   CapabilitySet filterCapsAgainstTargetEnv(const SpvCapability* cap_array,
45                                            uint32_t count) const;
46 
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;
51 
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(SpvOp opcode, spv_opcode_desc* desc) const;
56 
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;
62 
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;
68 
69   // Finds operand entry in the grammar table and returns its name.
70   // Returns "Unknown" if not found.
lookupOperandName(spv_operand_type_t type,uint32_t operand)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";
76     }
77     return desc->name;
78   }
79 
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, SpvOp* opcode) const;
86 
87   // Returns SPV_SUCCESS if the given opcode is valid as the opcode operand
88   // to OpSpecConstantOp.
89   spv_result_t lookupSpecConstantOpcode(SpvOp opcode) const;
90 
91   // Parses a mask expression string for the given operand type.
92   //
93   // A mask expression is a sequence of one or more terms separated by '|',
94   // where each term is a named enum value for a given type. No whitespace
95   // is permitted.
96   //
97   // On success, the value is written to pValue, and SPV_SUCCESS is returned.
98   // The operand type is defined by the type parameter, and the text to be
99   // parsed is defined by the textValue parameter.
100   spv_result_t parseMaskOperand(const spv_operand_type_t type,
101                                 const char* textValue, uint32_t* pValue) const;
102 
103   // Writes the extended operand with the given type and text to the *extInst
104   // parameter.
105   // Returns SPV_SUCCESS if the value could be found.
106   spv_result_t lookupExtInst(spv_ext_inst_type_t type, const char* textValue,
107                              spv_ext_inst_desc* extInst) const;
108 
109   // Writes the extended operand with the given type and first encoded word
110   // to the *extInst parameter.
111   // Returns SPV_SUCCESS if the value could be found.
112   spv_result_t lookupExtInst(spv_ext_inst_type_t type, uint32_t firstWord,
113                              spv_ext_inst_desc* extInst) const;
114 
115   // Inserts the operands expected after the given typed mask onto the end
116   // of the given pattern.
117   //
118   // Each set bit in the mask represents zero or more operand types that
119   // should be appended onto the pattern. Operands for a less significant
120   // bit must always match before operands for a more significant bit, so
121   // the operands for a less significant bit must appear closer to the end
122   // of the pattern stack.
123   //
124   // If a set bit is unknown, then we assume it has no operands.
125   void pushOperandTypesForMask(const spv_operand_type_t type,
126                                const uint32_t mask,
127                                spv_operand_pattern_t* pattern) const;
128 
129  private:
130   const spv_target_env target_env_;
131   const spv_operand_table operandTable_;
132   const spv_opcode_table opcodeTable_;
133   const spv_ext_inst_table extInstTable_;
134 };
135 
136 }  // namespace spvtools
137 
138 #endif  // SOURCE_ASSEMBLY_GRAMMAR_H_
139