17e102996Smayafrom functools import reduce
27e102996Smayaimport operator
301e04c3fSmrg
401e04c3fSmrgtemplate = """\
501e04c3fSmrg/* Copyright (C) 2018 Red Hat
601e04c3fSmrg *
701e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
801e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
901e04c3fSmrg * to deal in the Software without restriction, including without limitation
1001e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1101e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
1201e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1301e04c3fSmrg *
1401e04c3fSmrg * The above copyright notice and this permission notice (including the next
1501e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1601e04c3fSmrg * Software.
1701e04c3fSmrg *
1801e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1901e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2001e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2101e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2201e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2301e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2401e04c3fSmrg * IN THE SOFTWARE.
2501e04c3fSmrg */
2601e04c3fSmrg
2701e04c3fSmrg#include "nir.h"
2801e04c3fSmrg
2901e04c3fSmrgconst nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics] = {
3001e04c3fSmrg% for name, opcode in sorted(INTR_OPCODES.items()):
3101e04c3fSmrg{
3201e04c3fSmrg   .name = "${name}",
3301e04c3fSmrg   .num_srcs = ${opcode.num_srcs},
3401e04c3fSmrg% if opcode.src_components:
3501e04c3fSmrg   .src_components = {
3601e04c3fSmrg      ${", ".join(str(comp) for comp in opcode.src_components)}
3701e04c3fSmrg   },
3801e04c3fSmrg% endif
3901e04c3fSmrg   .has_dest = ${"true" if opcode.has_dest else "false"},
4001e04c3fSmrg   .dest_components = ${max(opcode.dest_components, 0)},
417e102996Smaya   .dest_bit_sizes = ${hex(reduce(operator.or_, opcode.bit_sizes, 0))},
427ec681f3Smrg   .bit_size_src = ${opcode.bit_size_src},
4301e04c3fSmrg   .num_indices = ${opcode.num_indices},
4401e04c3fSmrg% if opcode.indices:
457ec681f3Smrg   .indices = {
467ec681f3Smrg% for i in range(len(opcode.indices)):
477ec681f3Smrg      NIR_INTRINSIC_${opcode.indices[i].name.upper()},
487ec681f3Smrg% endfor
497ec681f3Smrg   },
5001e04c3fSmrg   .index_map = {
5101e04c3fSmrg% for i in range(len(opcode.indices)):
527ec681f3Smrg      [NIR_INTRINSIC_${opcode.indices[i].name.upper()}] = ${i + 1},
5301e04c3fSmrg% endfor
5401e04c3fSmrg    },
5501e04c3fSmrg% endif
5601e04c3fSmrg   .flags = ${"0" if len(opcode.flags) == 0 else " | ".join(opcode.flags)},
5701e04c3fSmrg},
5801e04c3fSmrg% endfor
5901e04c3fSmrg};
607ec681f3Smrg
617ec681f3Smrgconst char *nir_intrinsic_index_names[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
627ec681f3Smrg% for index in INTR_INDICES:
637ec681f3Smrg   "${index.name}",
647ec681f3Smrg% endfor
657ec681f3Smrg};
6601e04c3fSmrg"""
6701e04c3fSmrg
687ec681f3Smrgfrom nir_intrinsics import INTR_OPCODES, INTR_INDICES
6901e04c3fSmrgfrom mako.template import Template
7001e04c3fSmrgimport argparse
7101e04c3fSmrgimport os
7201e04c3fSmrg
7301e04c3fSmrgdef main():
7401e04c3fSmrg    parser = argparse.ArgumentParser()
7501e04c3fSmrg    parser.add_argument('--outdir', required=True,
7601e04c3fSmrg                        help='Directory to put the generated files in')
7701e04c3fSmrg
7801e04c3fSmrg    args = parser.parse_args()
7901e04c3fSmrg
8001e04c3fSmrg    path = os.path.join(args.outdir, 'nir_intrinsics.c')
817ec681f3Smrg    with open(path, 'w') as f:
827ec681f3Smrg        f.write(Template(template).render(
837ec681f3Smrg            INTR_OPCODES=INTR_OPCODES, INTR_INDICES=INTR_INDICES,
847ec681f3Smrg            reduce=reduce, operator=operator))
8501e04c3fSmrg
8601e04c3fSmrgif __name__ == '__main__':
8701e04c3fSmrg    main()
8801e04c3fSmrg
89