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