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 13genNameMap() { 14prefix=$1 15echo "template<> inline void 16SPIRVMap<$prefix, std::string>::init() {" 17 18cat $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 20done 21 22echo "} 23SPIRV_DEF_NAMEMAP($prefix, SPIRV${prefix}NameMap) 24" 25 26} 27 28########################### 29# 30# generate isValid function 31# 32########################### 33genIsValid() { 34prefix=$1 35echo "inline bool 36isValid(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 44done 45 46echo " return true; 47 default: 48 return false; 49 } 50} 51" 52} 53genMaskIsValid() { 54prefix=$1 55subprefix=`echo $prefix | sed -e "s:Mask::g"` 56echo "inline bool 57isValid$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 65done 66 67echo " 68 return (Mask & ~ValidMask) == 0; 69} 70" 71} 72 73############################## 74# 75# generate entries for td file 76# 77############################## 78genTd() { 79prefix=$1 80 81if [[ $prefix == "Capability" ]]; then 82 echo "class SPIRV${prefix}_ {" 83else 84 echo "def SPIRV${prefix} : Operand<i32> { 85 let PrintMethod = \"printSPIRV${prefix}\"; 86" 87fi 88 89cat $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 94done 95 96if [[ $prefix == "Capability" ]]; then 97 echo "} 98def SPIRV${prefix} : SPIRV${prefix}_; 99" 100else 101 echo "} 102" 103fi 104} 105 106gen() { 107type=$1 108for 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 121done 122for prefix in ImageOperandsMask FPFastMathModeMask SelectionControlMask LoopControlMask FunctionControlMask MemorySemanticsMask MemoryAccessMask \ 123 KernelProfilingInfoMask; do 124 if [[ "$type" == isValid ]]; then 125 genMaskIsValid $prefix 126 fi 127done 128} 129 130#################### 131# 132# main 133# 134#################### 135 136if [[ $# -ne 3 ]]; then 137 echo "usage: gen_spirv path_to_spirv.hpp [NameMap|isValid|td] output_file" 138 exit 139fi 140 141spirvHeader=$1 142type=$2 143outputFile=$3 144includeGuard="`echo ${outputFile} | tr '[:lower:]' '[:upper:]' | sed -e 's/\./_/g'`_" 145 146echo "//===- ${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 197using namespace spv; 198 199namespace SPIRV { 200" > ${outputFile} 201 202gen $type >> ${outputFile} 203 204echo "} /* namespace SPIRV */ 205 206#endif /* ${includeGuard} */" >> ${outputFile} 207