Home | History | Annotate | Download | only in SPIRV
      1 //
      2 //Copyright (C) 2014-2015 LunarG, Inc.
      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 // Parameterize the SPIR-V enumerants.
     37 //
     38 
     39 #include "spirv.hpp"
     40 
     41 #include <vector>
     42 
     43 namespace spv {
     44 
     45 // Fill in all the parameters
     46 void Parameterize();
     47 
     48 // Return the English names of all the enums.
     49 const char* SourceString(int);
     50 const char* AddressingString(int);
     51 const char* MemoryString(int);
     52 const char* ExecutionModelString(int);
     53 const char* ExecutionModeString(int);
     54 const char* StorageClassString(int);
     55 const char* DecorationString(int);
     56 const char* BuiltInString(int);
     57 const char* DimensionString(int);
     58 const char* SelectControlString(int);
     59 const char* LoopControlString(int);
     60 const char* FunctionControlString(int);
     61 const char* SamplerAddressingModeString(int);
     62 const char* SamplerFilterModeString(int);
     63 const char* ImageFormatString(int);
     64 const char* ImageChannelOrderString(int);
     65 const char* ImageChannelTypeString(int);
     66 const char* ImageChannelDataTypeString(int type);
     67 const char* ImageOperandsString(int format);
     68 const char* ImageOperands(int);
     69 const char* FPFastMathString(int);
     70 const char* FPRoundingModeString(int);
     71 const char* LinkageTypeString(int);
     72 const char* FuncParamAttrString(int);
     73 const char* AccessQualifierString(int);
     74 const char* MemorySemanticsString(int);
     75 const char* MemoryAccessString(int);
     76 const char* ExecutionScopeString(int);
     77 const char* GroupOperationString(int);
     78 const char* KernelEnqueueFlagsString(int);
     79 const char* KernelProfilingInfoString(int);
     80 const char* CapabilityString(int);
     81 const char* OpcodeString(int);
     82 const char* ScopeString(int mem);
     83 
     84 // For grouping opcodes into subsections
     85 enum OpcodeClass {
     86     OpClassMisc,
     87     OpClassDebug,
     88     OpClassAnnotate,
     89     OpClassExtension,
     90     OpClassMode,
     91     OpClassType,
     92     OpClassConstant,
     93     OpClassMemory,
     94     OpClassFunction,
     95     OpClassImage,
     96     OpClassConvert,
     97     OpClassComposite,
     98     OpClassArithmetic,
     99     OpClassBit,
    100     OpClassRelationalLogical,
    101     OpClassDerivative,
    102     OpClassFlowControl,
    103     OpClassAtomic,
    104     OpClassPrimitive,
    105     OpClassBarrier,
    106     OpClassGroup,
    107     OpClassDeviceSideEnqueue,
    108     OpClassPipe,
    109 
    110     OpClassCount,
    111     OpClassMissing             // all instructions start out as missing
    112 };
    113 
    114 // For parameterizing operands.
    115 enum OperandClass {
    116     OperandNone,
    117     OperandId,
    118     OperandVariableIds,
    119     OperandOptionalLiteral,
    120     OperandOptionalLiteralString,
    121     OperandVariableLiterals,
    122     OperandVariableIdLiteral,
    123     OperandVariableLiteralId,
    124     OperandLiteralNumber,
    125     OperandLiteralString,
    126     OperandSource,
    127     OperandExecutionModel,
    128     OperandAddressing,
    129     OperandMemory,
    130     OperandExecutionMode,
    131     OperandStorage,
    132     OperandDimensionality,
    133     OperandSamplerAddressingMode,
    134     OperandSamplerFilterMode,
    135     OperandSamplerImageFormat,
    136     OperandImageChannelOrder,
    137     OperandImageChannelDataType,
    138     OperandImageOperands,
    139     OperandFPFastMath,
    140     OperandFPRoundingMode,
    141     OperandLinkageType,
    142     OperandAccessQualifier,
    143     OperandFuncParamAttr,
    144     OperandDecoration,
    145     OperandBuiltIn,
    146     OperandSelect,
    147     OperandLoop,
    148     OperandFunction,
    149     OperandMemorySemantics,
    150     OperandMemoryAccess,
    151     OperandScope,
    152 	OperandGroupOperation,
    153     OperandKernelEnqueueFlags,
    154     OperandKernelProfilingInfo,
    155     OperandCapability,
    156 
    157     OperandOpcode,
    158 
    159     OperandCount
    160 };
    161 
    162 // Any specific enum can have a set of capabilities that allow it:
    163 typedef std::vector<Capability> EnumCaps;
    164 
    165 // Parameterize a set of operands with their OperandClass(es) and descriptions.
    166 class OperandParameters {
    167 public:
    168     OperandParameters() { }
    169     void push(OperandClass oc, const char* d, bool opt = false)
    170     {
    171         opClass.push_back(oc);
    172         desc.push_back(d);
    173         optional.push_back(opt);
    174     }
    175     void setOptional();
    176     OperandClass getClass(int op) const { return opClass[op]; }
    177     const char* getDesc(int op) const { return desc[op]; }
    178     bool isOptional(int op) const { return optional[op]; }
    179     int getNum() const { return (int)opClass.size(); }
    180 
    181 protected:
    182     std::vector<OperandClass> opClass;
    183     std::vector<const char*> desc;
    184     std::vector<bool> optional;
    185 };
    186 
    187 // Parameterize an enumerant
    188 class EnumParameters {
    189 public:
    190     EnumParameters() : desc(0) { }
    191     EnumCaps caps;
    192     const char* desc;
    193 };
    194 
    195 // Parameterize a set of enumerants that form an enum
    196 class EnumDefinition : public EnumParameters {
    197 public:
    198     EnumDefinition() :
    199         ceiling(0), bitmask(false), getName(0), enumParams(0), operandParams(0) { }
    200     void set(int ceil, const char* (*name)(int), EnumParameters* ep, bool mask = false)
    201     {
    202         ceiling = ceil;
    203         getName = name;
    204         bitmask = mask;
    205         enumParams = ep;
    206     }
    207     void setOperands(OperandParameters* op) { operandParams = op; }
    208     int ceiling;   // ceiling of enumerants
    209     bool bitmask;  // true if these enumerants combine into a bitmask
    210     const char* (*getName)(int);      // a function that returns the name for each enumerant value (or shift)
    211     EnumParameters* enumParams;       // parameters for each individual enumerant
    212     OperandParameters* operandParams; // sets of operands
    213 };
    214 
    215 // Parameterize an instruction's logical format, including its known set of operands,
    216 // per OperandParameters above.
    217 class InstructionParameters {
    218 public:
    219     InstructionParameters() :
    220         opDesc("TBD"),
    221         opClass(OpClassMissing),
    222         typePresent(true),         // most normal, only exceptions have to be spelled out
    223         resultPresent(true)        // most normal, only exceptions have to be spelled out
    224     { }
    225 
    226     void setResultAndType(bool r, bool t)
    227     {
    228         resultPresent = r;
    229         typePresent = t;
    230     }
    231 
    232     bool hasResult() const { return resultPresent != 0; }
    233     bool hasType()   const { return typePresent != 0; }
    234 
    235     const char* opDesc;
    236     EnumCaps capabilities;
    237     OpcodeClass opClass;
    238     OperandParameters operands;
    239 
    240 protected:
    241     int typePresent   : 1;
    242     int resultPresent : 1;
    243 };
    244 
    245 const int OpcodeCeiling = 321;
    246 
    247 // The set of objects that hold all the instruction/operand
    248 // parameterization information.
    249 extern InstructionParameters InstructionDesc[];
    250 
    251 // These hold definitions of the enumerants used for operands
    252 extern EnumDefinition OperandClassParams[];
    253 
    254 const char* GetOperandDesc(OperandClass operand);
    255 void PrintImmediateRow(int imm, const char* name, const EnumParameters* enumParams, bool caps, bool hex = false);
    256 const char* AccessQualifierString(int attr);
    257 
    258 void PrintOperands(const OperandParameters& operands, int reservedOperands);
    259 
    260 };  // end namespace spv
    261