John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1 | // |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2 | //Copyright (C) 2014-2015 LunarG, Inc. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3 | // |
| 4 | //All rights reserved. |
| 5 | // |
| 6 | //Redistribution and use in source and binary forms, with or without |
| 7 | //modification, are permitted provided that the following conditions |
| 8 | //are met: |
| 9 | // |
| 10 | // Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // |
| 13 | // Redistributions in binary form must reproduce the above |
| 14 | // copyright notice, this list of conditions and the following |
| 15 | // disclaimer in the documentation and/or other materials provided |
| 16 | // with the distribution. |
| 17 | // |
| 18 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 19 | // contributors may be used to endorse or promote products derived |
| 20 | // from this software without specific prior written permission. |
| 21 | // |
| 22 | //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 25 | //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 26 | //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 28 | //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 29 | //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 30 | //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 | //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 32 | //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 33 | //POSSIBILITY OF SUCH DAMAGE. |
| 34 | |
| 35 | // |
| 36 | // Author: John Kessenich, LunarG |
| 37 | // |
| 38 | |
| 39 | // |
| 40 | // Parameterize the SPIR-V enumerants. |
| 41 | // |
| 42 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 43 | #include "spirv.hpp" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 44 | |
| 45 | #include <vector> |
| 46 | |
| 47 | namespace spv { |
| 48 | |
| 49 | // Fill in all the parameters |
| 50 | void Parameterize(); |
| 51 | |
| 52 | // Return the English names of all the enums. |
| 53 | const char* SourceString(int); |
| 54 | const char* AddressingString(int); |
| 55 | const char* MemoryString(int); |
| 56 | const char* ExecutionModelString(int); |
| 57 | const char* ExecutionModeString(int); |
| 58 | const char* StorageClassString(int); |
| 59 | const char* DecorationString(int); |
| 60 | const char* BuiltInString(int); |
| 61 | const char* DimensionString(int); |
| 62 | const char* SelectControlString(int); |
| 63 | const char* LoopControlString(int); |
| 64 | const char* FunctionControlString(int); |
| 65 | const char* SamplerAddressingModeString(int); |
| 66 | const char* SamplerFilterModeString(int); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 67 | const char* ImageFormatString(int); |
| 68 | const char* ImageChannelOrderString(int); |
| 69 | const char* ImageChannelTypeString(int); |
Mark Adams | 18b637f | 2016-02-23 12:17:11 -0500 | [diff] [blame] | 70 | const char* ImageChannelDataTypeString(int type); |
| 71 | const char* ImageOperandsString(int format); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 72 | const char* ImageOperands(int); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 73 | const char* FPFastMathString(int); |
| 74 | const char* FPRoundingModeString(int); |
| 75 | const char* LinkageTypeString(int); |
| 76 | const char* FuncParamAttrString(int); |
| 77 | const char* AccessQualifierString(int); |
| 78 | const char* MemorySemanticsString(int); |
| 79 | const char* MemoryAccessString(int); |
| 80 | const char* ExecutionScopeString(int); |
| 81 | const char* GroupOperationString(int); |
| 82 | const char* KernelEnqueueFlagsString(int); |
| 83 | const char* KernelProfilingInfoString(int); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 84 | const char* CapabilityString(int); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 85 | const char* OpcodeString(int); |
Mark Adams | 18b637f | 2016-02-23 12:17:11 -0500 | [diff] [blame] | 86 | const char* ScopeString(int mem); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 87 | |
| 88 | // For grouping opcodes into subsections |
| 89 | enum OpcodeClass { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 90 | OpClassMisc, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 91 | OpClassDebug, |
| 92 | OpClassAnnotate, |
| 93 | OpClassExtension, |
| 94 | OpClassMode, |
| 95 | OpClassType, |
| 96 | OpClassConstant, |
| 97 | OpClassMemory, |
| 98 | OpClassFunction, |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 99 | OpClassImage, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 100 | OpClassConvert, |
| 101 | OpClassComposite, |
| 102 | OpClassArithmetic, |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 103 | OpClassBit, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 104 | OpClassRelationalLogical, |
| 105 | OpClassDerivative, |
| 106 | OpClassFlowControl, |
| 107 | OpClassAtomic, |
| 108 | OpClassPrimitive, |
| 109 | OpClassBarrier, |
| 110 | OpClassGroup, |
| 111 | OpClassDeviceSideEnqueue, |
| 112 | OpClassPipe, |
| 113 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 114 | OpClassCount, |
| 115 | OpClassMissing // all instructions start out as missing |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | // For parameterizing operands. |
| 119 | enum OperandClass { |
| 120 | OperandNone, |
| 121 | OperandId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 122 | OperandVariableIds, |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 123 | OperandOptionalLiteral, |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 124 | OperandOptionalLiteralString, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 125 | OperandVariableLiterals, |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 126 | OperandVariableIdLiteral, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 127 | OperandVariableLiteralId, |
| 128 | OperandLiteralNumber, |
| 129 | OperandLiteralString, |
| 130 | OperandSource, |
| 131 | OperandExecutionModel, |
| 132 | OperandAddressing, |
| 133 | OperandMemory, |
| 134 | OperandExecutionMode, |
| 135 | OperandStorage, |
| 136 | OperandDimensionality, |
| 137 | OperandSamplerAddressingMode, |
| 138 | OperandSamplerFilterMode, |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 139 | OperandSamplerImageFormat, |
| 140 | OperandImageChannelOrder, |
| 141 | OperandImageChannelDataType, |
| 142 | OperandImageOperands, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 143 | OperandFPFastMath, |
| 144 | OperandFPRoundingMode, |
| 145 | OperandLinkageType, |
| 146 | OperandAccessQualifier, |
| 147 | OperandFuncParamAttr, |
| 148 | OperandDecoration, |
| 149 | OperandBuiltIn, |
| 150 | OperandSelect, |
| 151 | OperandLoop, |
| 152 | OperandFunction, |
| 153 | OperandMemorySemantics, |
| 154 | OperandMemoryAccess, |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 155 | OperandScope, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 156 | OperandGroupOperation, |
| 157 | OperandKernelEnqueueFlags, |
| 158 | OperandKernelProfilingInfo, |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 159 | OperandCapability, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 160 | |
| 161 | OperandOpcode, |
| 162 | |
| 163 | OperandCount |
| 164 | }; |
| 165 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 166 | // Any specific enum can have a set of capabilities that allow it: |
| 167 | typedef std::vector<Capability> EnumCaps; |
| 168 | |
| 169 | // Parameterize a set of operands with their OperandClass(es) and descriptions. |
| 170 | class OperandParameters { |
| 171 | public: |
| 172 | OperandParameters() { } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 173 | void push(OperandClass oc, const char* d, bool opt = false) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 174 | { |
| 175 | opClass.push_back(oc); |
| 176 | desc.push_back(d); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 177 | optional.push_back(opt); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 178 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 179 | void setOptional(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 180 | OperandClass getClass(int op) const { return opClass[op]; } |
| 181 | const char* getDesc(int op) const { return desc[op]; } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 182 | bool isOptional(int op) const { return optional[op]; } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 183 | int getNum() const { return (int)opClass.size(); } |
| 184 | |
| 185 | protected: |
| 186 | std::vector<OperandClass> opClass; |
| 187 | std::vector<const char*> desc; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 188 | std::vector<bool> optional; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | // Parameterize an enumerant |
| 192 | class EnumParameters { |
| 193 | public: |
| 194 | EnumParameters() : desc(0) { } |
| 195 | EnumCaps caps; |
| 196 | const char* desc; |
| 197 | }; |
| 198 | |
| 199 | // Parameterize a set of enumerants that form an enum |
| 200 | class EnumDefinition : public EnumParameters { |
| 201 | public: |
| 202 | EnumDefinition() : |
| 203 | ceiling(0), bitmask(false), getName(0), enumParams(0), operandParams(0) { } |
| 204 | void set(int ceil, const char* (*name)(int), EnumParameters* ep, bool mask = false) |
| 205 | { |
| 206 | ceiling = ceil; |
| 207 | getName = name; |
| 208 | bitmask = mask; |
| 209 | enumParams = ep; |
| 210 | } |
| 211 | void setOperands(OperandParameters* op) { operandParams = op; } |
| 212 | int ceiling; // ceiling of enumerants |
| 213 | bool bitmask; // true if these enumerants combine into a bitmask |
| 214 | const char* (*getName)(int); // a function that returns the name for each enumerant value (or shift) |
| 215 | EnumParameters* enumParams; // parameters for each individual enumerant |
| 216 | OperandParameters* operandParams; // sets of operands |
| 217 | }; |
| 218 | |
| 219 | // Parameterize an instruction's logical format, including its known set of operands, |
| 220 | // per OperandParameters above. |
| 221 | class InstructionParameters { |
| 222 | public: |
| 223 | InstructionParameters() : |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 224 | opDesc("TBD"), |
| 225 | opClass(OpClassMissing), |
Andre Weissflog | a5ade51 | 2015-07-11 14:45:57 +0200 | [diff] [blame] | 226 | typePresent(true), // most normal, only exceptions have to be spelled out |
| 227 | resultPresent(true) // most normal, only exceptions have to be spelled out |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 228 | { } |
| 229 | |
| 230 | void setResultAndType(bool r, bool t) |
| 231 | { |
| 232 | resultPresent = r; |
| 233 | typePresent = t; |
| 234 | } |
| 235 | |
| 236 | bool hasResult() const { return resultPresent != 0; } |
| 237 | bool hasType() const { return typePresent != 0; } |
| 238 | |
| 239 | const char* opDesc; |
| 240 | EnumCaps capabilities; |
| 241 | OpcodeClass opClass; |
| 242 | OperandParameters operands; |
| 243 | |
| 244 | protected: |
| 245 | int typePresent : 1; |
| 246 | int resultPresent : 1; |
| 247 | }; |
| 248 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 249 | const int OpcodeCeiling = 321; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 250 | |
| 251 | // The set of objects that hold all the instruction/operand |
| 252 | // parameterization information. |
| 253 | extern InstructionParameters InstructionDesc[]; |
| 254 | |
| 255 | // These hold definitions of the enumerants used for operands |
| 256 | extern EnumDefinition OperandClassParams[]; |
| 257 | |
| 258 | const char* GetOperandDesc(OperandClass operand); |
| 259 | void PrintImmediateRow(int imm, const char* name, const EnumParameters* enumParams, bool caps, bool hex = false); |
| 260 | const char* AccessQualifierString(int attr); |
| 261 | |
| 262 | void PrintOperands(const OperandParameters& operands, int reservedOperands); |
| 263 | |
| 264 | }; // end namespace spv |