1template = """\ 2/* Copyright (C) 2015 Broadcom 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24#ifndef _NIR_BUILDER_OPCODES_ 25#define _NIR_BUILDER_OPCODES_ 26 27<% 28def src_decl_list(num_srcs): 29 return ', '.join('nir_ssa_def *src' + str(i) for i in range(num_srcs)) 30 31def src_list(num_srcs): 32 if num_srcs <= 4: 33 return ', '.join('src' + str(i) if i < num_srcs else 'NULL' for i in range(4)) 34 else: 35 return ', '.join('src' + str(i) for i in range(num_srcs)) 36%> 37 38% for name, opcode in sorted(opcodes.items()): 39static inline nir_ssa_def * 40nir_${name}(nir_builder *build, ${src_decl_list(opcode.num_inputs)}) 41{ 42% if opcode.num_inputs <= 4: 43 return nir_build_alu(build, nir_op_${name}, ${src_list(opcode.num_inputs)}); 44% else: 45 nir_ssa_def *srcs[${opcode.num_inputs}] = {${src_list(opcode.num_inputs)}}; 46 return nir_build_alu_src_arr(build, nir_op_${name}, srcs); 47% endif 48} 49% endfor 50 51% for name, opcode in sorted(INTR_OPCODES.items()): 52struct _nir_${name}_indices { 53 int _; /* exists to avoid empty initializers */ 54% for index in opcode.indices: 55 ${index.c_data_type} ${index.name}; 56% endfor 57}; 58% endfor 59 60<% 61def intrinsic_decl_list(opcode): 62 need_components = opcode.dest_components == 0 and \ 63 0 not in opcode.src_components 64 65 res = '' 66 if (opcode.has_dest or opcode.num_srcs) and need_components: 67 res += ', unsigned num_components' 68 if opcode.has_dest and len(opcode.bit_sizes) != 1 and opcode.bit_size_src == -1: 69 res += ', unsigned bit_size' 70 for i in range(opcode.num_srcs): 71 res += ', nir_ssa_def *src' + str(i) 72 if opcode.indices: 73 res += ', struct _nir_' + opcode.name + '_indices indices' 74 return res 75 76def intrinsic_macro_list(opcode): 77 need_components = opcode.dest_components == 0 and \ 78 0 not in opcode.src_components 79 80 res = '' 81 if (opcode.has_dest or opcode.num_srcs) and need_components: 82 res += ', num_components' 83 if opcode.has_dest and len(opcode.bit_sizes) != 1 and opcode.bit_size_src == -1: 84 res += ', bit_size' 85 for i in range(opcode.num_srcs): 86 res += ', src' + str(i) 87 return res 88 89def get_intrinsic_bitsize(opcode): 90 if len(opcode.bit_sizes) == 1: 91 return str(opcode.bit_sizes[0]) 92 elif opcode.bit_size_src != -1: 93 return 'src' + str(opcode.bit_size_src) + '->bit_size' 94 else: 95 return 'bit_size' 96%> 97 98% for name, opcode in sorted(INTR_OPCODES.items()): 99% if opcode.has_dest: 100static inline nir_ssa_def * 101% else: 102static inline nir_intrinsic_instr * 103% endif 104_nir_build_${name}(nir_builder *build${intrinsic_decl_list(opcode)}) 105{ 106 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create( 107 build->shader, nir_intrinsic_${name}); 108 109 % if 0 in opcode.src_components: 110 intrin->num_components = src${opcode.src_components.index(0)}->num_components; 111 % elif opcode.dest_components == 0: 112 intrin->num_components = num_components; 113 % endif 114 % if opcode.has_dest: 115 % if opcode.dest_components == 0: 116 nir_ssa_dest_init(&intrin->instr, &intrin->dest, intrin->num_components, ${get_intrinsic_bitsize(opcode)}, NULL); 117 % else: 118 nir_ssa_dest_init(&intrin->instr, &intrin->dest, ${opcode.dest_components}, ${get_intrinsic_bitsize(opcode)}, NULL); 119 % endif 120 % endif 121 % for i in range(opcode.num_srcs): 122 intrin->src[${i}] = nir_src_for_ssa(src${i}); 123 % endfor 124 % for index in opcode.indices: 125 nir_intrinsic_set_${index.name}(intrin, indices.${index.name}); 126 % endfor 127 128 nir_builder_instr_insert(build, &intrin->instr); 129 % if opcode.has_dest: 130 return &intrin->dest.ssa; 131 % else: 132 return intrin; 133 % endif 134} 135% endfor 136 137% for name, opcode in sorted(INTR_OPCODES.items()): 138% if opcode.indices: 139#ifdef __cplusplus 140#define nir_build_${name}(build${intrinsic_macro_list(opcode)}, ...) ${'\\\\'} 141_nir_build_${name}(build${intrinsic_macro_list(opcode)}, _nir_${name}_indices{0, __VA_ARGS__}) 142#else 143#define nir_build_${name}(build${intrinsic_macro_list(opcode)}, ...) ${'\\\\'} 144_nir_build_${name}(build${intrinsic_macro_list(opcode)}, (struct _nir_${name}_indices){0, __VA_ARGS__}) 145#endif 146% else: 147#define nir_build_${name} _nir_build_${name} 148% endif 149#define nir_${name} nir_build_${name} 150% endfor 151 152#endif /* _NIR_BUILDER_OPCODES_ */""" 153 154from nir_opcodes import opcodes 155from nir_intrinsics import INTR_OPCODES 156from mako.template import Template 157 158print(Template(template).render(opcodes=opcodes, INTR_OPCODES=INTR_OPCODES)) 159