Home | History | Annotate | Download | only in source
      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 LIBSPIRV_ASSEMBLY_GRAMMAR_H_
     16 #define LIBSPIRV_ASSEMBLY_GRAMMAR_H_
     17 
     18 #include "operand.h"
     19 #include "spirv-tools/libspirv.h"
     20 #include "spirv/1.2/spirv.h"
     21 #include "table.h"
     22 
     23 namespace libspirv {
     24 
     25 // Encapsulates the grammar to use for SPIR-V assembly.
     26 // Contains methods to query for valid instructions and operands.
     27 class AssemblyGrammar {
     28  public:
     29   explicit AssemblyGrammar(const spv_const_context context)
     30       : target_env_(context->target_env),
     31         operandTable_(context->operand_table),
     32         opcodeTable_(context->opcode_table),
     33         extInstTable_(context->ext_inst_table) {}
     34 
     35   // Returns true if the internal tables have been initialized with valid data.
     36   bool isValid() const;
     37 
     38   // Returns the SPIR-V target environment.
     39   spv_target_env target_env() const { return target_env_; }
     40 
     41   // Fills in the desc parameter with the information about the opcode
     42   // of the given name. Returns SPV_SUCCESS if the opcode was found, and
     43   // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
     44   spv_result_t lookupOpcode(const char* name, spv_opcode_desc* desc) const;
     45 
     46   // Fills in the desc parameter with the information about the opcode
     47   // of the valid. Returns SPV_SUCCESS if the opcode was found, and
     48   // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
     49   spv_result_t lookupOpcode(SpvOp opcode, spv_opcode_desc* desc) const;
     50 
     51   // Fills in the desc parameter with the information about the given
     52   // operand. Returns SPV_SUCCESS if the operand was found, and
     53   // SPV_ERROR_INVALID_LOOKUP otherwise.
     54   spv_result_t lookupOperand(spv_operand_type_t type, const char* name,
     55                              size_t name_len, spv_operand_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, uint32_t operand,
     61                              spv_operand_desc* desc) const;
     62 
     63   // Finds the opcode for the given OpSpecConstantOp opcode name. The name
     64   // should not have the "Op" prefix.  For example, "IAdd" corresponds to
     65   // the integer add opcode for OpSpecConstantOp.  On success, returns
     66   // SPV_SUCCESS and sends the discovered operation code through the opcode
     67   // parameter.  On failure, returns SPV_ERROR_INVALID_LOOKUP.
     68   spv_result_t lookupSpecConstantOpcode(const char* name, SpvOp* opcode) const;
     69 
     70   // Returns SPV_SUCCESS if the given opcode is valid as the opcode operand
     71   // to OpSpecConstantOp.
     72   spv_result_t lookupSpecConstantOpcode(SpvOp opcode) const;
     73 
     74   // Parses a mask expression string for the given operand type.
     75   //
     76   // A mask expression is a sequence of one or more terms separated by '|',
     77   // where each term is a named enum value for a given type. No whitespace
     78   // is permitted.
     79   //
     80   // On success, the value is written to pValue, and SPV_SUCCESS is returned.
     81   // The operand type is defined by the type parameter, and the text to be
     82   // parsed is defined by the textValue parameter.
     83   spv_result_t parseMaskOperand(const spv_operand_type_t type,
     84                                 const char* textValue, uint32_t* pValue) const;
     85 
     86   // Writes the extended operand with the given type and text to the *extInst
     87   // parameter.
     88   // Returns SPV_SUCCESS if the value could be found.
     89   spv_result_t lookupExtInst(spv_ext_inst_type_t type, const char* textValue,
     90                              spv_ext_inst_desc* extInst) const;
     91 
     92   // Writes the extended operand with the given type and first encoded word
     93   // to the *extInst parameter.
     94   // Returns SPV_SUCCESS if the value could be found.
     95   spv_result_t lookupExtInst(spv_ext_inst_type_t type, uint32_t firstWord,
     96                              spv_ext_inst_desc* extInst) const;
     97 
     98   // Inserts the operands expected after the given typed mask onto the end
     99   // of the given pattern.
    100   //
    101   // Each set bit in the mask represents zero or more operand types that
    102   // should be appended onto the pattern. Operands for a less significant
    103   // bit must always match before operands for a more significant bit, so
    104   // the operands for a less significant bit must appear closer to the end
    105   // of the pattern stack.
    106   //
    107   // If a set bit is unknown, then we assume it has no operands.
    108   void pushOperandTypesForMask(const spv_operand_type_t type,
    109                                const uint32_t mask,
    110                                spv_operand_pattern_t* pattern) const;
    111 
    112  private:
    113   const spv_target_env target_env_;
    114   const spv_operand_table operandTable_;
    115   const spv_opcode_table opcodeTable_;
    116   const spv_ext_inst_table extInstTable_;
    117 };
    118 }
    119 
    120 #endif  // LIBSPIRV_ASSEMBLY_GRAMMAR_H_
    121