Home | History | Annotate | Download | only in spirv-tool
      1 #!/usr/bin/bash
      2 # script to generate code for LLVM/SPIR-V translator based on khronos 
      3 # header file spirv.hpp.
      4 #
      5 
      6 
      7 ######################
      8 #
      9 # generate NameMap
     10 #
     11 ######################
     12 
     13 genNameMap() {
     14 prefix=$1
     15 echo "template<> inline void
     16 SPIRVMap<$prefix, std::string>::init() {"
     17 
     18 cat $spirvHeader | sed -n -e "/^ *${prefix}[^a-z]/s:^ *${prefix}\([^= ][^= ]*\)[= ][= ]*\([0x]*[0-9][0-9]*\).*:\1 \2:p"  | while read a b; do
     19   printf "  add(${prefix}%s, \"%s\");\n" $a $a
     20 done
     21 
     22 echo "}
     23 SPIRV_DEF_NAMEMAP($prefix, SPIRV${prefix}NameMap)
     24 "
     25 
     26 }
     27 
     28 ###########################
     29 #
     30 # generate isValid function
     31 #
     32 ###########################
     33 genIsValid() {
     34 prefix=$1
     35 echo "inline bool
     36 isValid(spv::$prefix V) {
     37   switch(V) {"
     38 
     39   cat $spirvHeader | sed -n -e "/^ *${prefix}[^a-z]/s:^ *${prefix}\([^= ][^= ]*\)[= ][= ]*\(.*\).*:\1 \2:p"  | while read a b; do
     40   if [[ $a == CapabilityNone ]]; then
     41     continue
     42   fi
     43   printf "    case ${prefix}%s:\n" $a
     44 done
     45 
     46 echo "      return true;
     47     default:
     48       return false;
     49   }
     50 }
     51 "
     52 }
     53 genMaskIsValid() {
     54 prefix=$1
     55 subprefix=`echo $prefix | sed -e "s:Mask::g"`
     56 echo "inline bool
     57 isValid$prefix(SPIRVWord Mask) {
     58   SPIRVWord ValidMask = 0u;"
     59 
     60   cat $spirvHeader | sed -n -e "/^ *${subprefix}[^a-z]/s:^ *${subprefix}\([^= ][^= ]*\)Mask[= ][= ]*\(.*\).*:\1 \2:p"  | while read a b; do
     61   if [[ $a == None ]]; then
     62     continue
     63   fi
     64   printf "  ValidMask |= ${subprefix}%sMask;\n" $a
     65 done
     66 
     67 echo "
     68   return (Mask & ~ValidMask) == 0;
     69 }
     70 "
     71 }
     72 
     73 ##############################
     74 #
     75 # generate entries for td file
     76 #
     77 ##############################
     78 genTd() {
     79 prefix=$1
     80 
     81 if [[ $prefix == "Capability" ]]; then
     82   echo "class SPIRV${prefix}_ {"
     83 else
     84   echo "def SPIRV${prefix} : Operand<i32> {
     85   let PrintMethod = \"printSPIRV${prefix}\";
     86 "
     87 fi
     88 
     89 cat $spirvHeader | sed -n -e "/^ *${prefix}[^a-z]/s:^ *${prefix}\([^= ][^= ]*\)[= ][= ]*\([0xX]*[0-9a-fA-F][0-9a-fA-F]*\).*:\1 \2:p"  | while read a b; do
     90   if [[ $a == CapabilityNone ]]; then
     91     continue
     92   fi
     93   printf "  int %s = %s;\n" $a $b
     94 done
     95 
     96 if [[ $prefix == "Capability" ]]; then
     97   echo "}
     98 def SPIRV${prefix} : SPIRV${prefix}_;
     99 "
    100 else 
    101   echo "}
    102 "
    103 fi
    104 }
    105 
    106 gen() {
    107 type=$1
    108 for prefix in SourceLanguage ExecutionModel AddressingModel MemoryModel ExecutionMode StorageClass Dim SamplerAddressingMode SamplerFilterMode ImageFormat \
    109   ImageChannelOrder ImageChannelDataType FPRoundingMode LinkageType AccessQualifier FunctionParameterAttribute Decoration BuiltIn Scope GroupOperation \
    110   KernelEnqueueFlags Capability Op; do
    111   if [[ "$type" == NameMap ]]; then
    112     genNameMap $prefix
    113   elif [[ "$type" == isValid ]]; then
    114     genIsValid $prefix
    115   elif [[ "$type" == td ]]; then
    116     genTd $prefix
    117   else
    118     echo "invalid type \"$type\"."
    119     exit
    120   fi
    121 done
    122 for prefix in ImageOperandsMask FPFastMathModeMask SelectionControlMask LoopControlMask FunctionControlMask MemorySemanticsMask MemoryAccessMask \
    123   KernelProfilingInfoMask; do
    124   if [[ "$type" == isValid ]]; then
    125     genMaskIsValid $prefix
    126   fi
    127 done
    128 }
    129 
    130 ####################
    131 #
    132 # main
    133 #
    134 ####################
    135 
    136 if [[ $# -ne 3 ]]; then
    137   echo "usage: gen_spirv path_to_spirv.hpp [NameMap|isValid|td] output_file"
    138   exit
    139 fi
    140 
    141 spirvHeader=$1
    142 type=$2
    143 outputFile=$3
    144 includeGuard="`echo ${outputFile} | tr '[:lower:]' '[:upper:]' | sed -e 's/\./_/g'`_"
    145 
    146 echo "//===- ${outputFile} - SPIR-V ${type} enums ----------------*- C++ -*-===//
    147 //
    148 //                     The LLVM/SPIRV Translator
    149 //
    150 // This file is distributed under the University of Illinois Open Source
    151 // License. See LICENSE.TXT for details.
    152 //
    153 // Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved.
    154 //
    155 // Permission is hereby granted, free of charge, to any person obtaining a
    156 // copy of this software and associated documentation files (the \"Software\"),
    157 // to deal with the Software without restriction, including without limitation
    158 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
    159 // and/or sell copies of the Software, and to permit persons to whom the
    160 // Software is furnished to do so, subject to the following conditions:
    161 //
    162 // Redistributions of source code must retain the above copyright notice,
    163 // this list of conditions and the following disclaimers.
    164 // Redistributions in binary form must reproduce the above copyright notice,
    165 // this list of conditions and the following disclaimers in the documentation
    166 // and/or other materials provided with the distribution.
    167 // Neither the names of Advanced Micro Devices, Inc., nor the names of its
    168 // contributors may be used to endorse or promote products derived from this
    169 // Software without specific prior written permission.
    170 // THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    171 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    172 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    173 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    174 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    175 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
    176 // THE SOFTWARE.
    177 //
    178 //===----------------------------------------------------------------------===//
    179 /// \\file
    180 ///
    181 /// This file defines SPIR-V ${type} enums.
    182 ///
    183 //===----------------------------------------------------------------------===//
    184 // WARNING:
    185 //
    186 // This file has been generated using \`tools/spirv-tool/gen_spirv.bash\` and
    187 // should not be modified manually. If the file needs to be updated, edit the
    188 // script and any other source file instead, before re-generating this file.
    189 //===----------------------------------------------------------------------===//
    190 
    191 #ifndef ${includeGuard}
    192 #define ${includeGuard}
    193 
    194 #include \"spirv.hpp\"
    195 #include \"SPIRVEnum.h\"
    196 
    197 using namespace spv;
    198 
    199 namespace SPIRV {
    200 " > ${outputFile}
    201 
    202 gen $type >> ${outputFile}
    203 
    204 echo "} /* namespace SPIRV */
    205 
    206 #endif /* ${includeGuard} */" >> ${outputFile}
    207