101e04c3fSmrg#
201e04c3fSmrg# Copyright (C) 2014 Connor Abbott
301e04c3fSmrg#
401e04c3fSmrg# Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg# copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg# to deal in the Software without restriction, including without limitation
701e04c3fSmrg# the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg# and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg# Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg#
1101e04c3fSmrg# The above copyright notice and this permission notice (including the next
1201e04c3fSmrg# paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg# Software.
1401e04c3fSmrg#
1501e04c3fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg# IN THE SOFTWARE.
2201e04c3fSmrg#
2301e04c3fSmrg# Authors:
2401e04c3fSmrg#    Connor Abbott (cwabbott0@gmail.com)
2501e04c3fSmrg
267e102996Smayafrom nir_opcodes import opcodes, type_sizes
2701e04c3fSmrgfrom mako.template import Template
2801e04c3fSmrg
2901e04c3fSmrgtemplate = Template("""
3001e04c3fSmrg#include "nir.h"
3101e04c3fSmrg
3201e04c3fSmrgnir_op
3301e04c3fSmrgnir_type_conversion_op(nir_alu_type src, nir_alu_type dst, nir_rounding_mode rnd)
3401e04c3fSmrg{
3501e04c3fSmrg   nir_alu_type src_base = (nir_alu_type) nir_alu_type_get_base_type(src);
3601e04c3fSmrg   nir_alu_type dst_base = (nir_alu_type) nir_alu_type_get_base_type(dst);
3701e04c3fSmrg   unsigned src_bit_size = nir_alu_type_get_type_size(src);
3801e04c3fSmrg   unsigned dst_bit_size = nir_alu_type_get_type_size(dst);
3901e04c3fSmrg
4001e04c3fSmrg   if (src == dst && src_base == nir_type_float) {
417ec681f3Smrg      return nir_op_mov;
427e102996Smaya   } else if (src == dst && src_base == nir_type_bool) {
437ec681f3Smrg      return nir_op_mov;
4401e04c3fSmrg   } else if ((src_base == nir_type_int || src_base == nir_type_uint) &&
4501e04c3fSmrg              (dst_base == nir_type_int || dst_base == nir_type_uint) &&
4601e04c3fSmrg              src_bit_size == dst_bit_size) {
4701e04c3fSmrg      /* Integer <-> integer conversions with the same bit-size on both
4801e04c3fSmrg       * ends are just no-op moves.
4901e04c3fSmrg       */
507ec681f3Smrg      return nir_op_mov;
5101e04c3fSmrg   }
5201e04c3fSmrg
5301e04c3fSmrg   switch (src_base) {
547e102996Smaya%     for src_t in ['int', 'uint', 'float', 'bool']:
5501e04c3fSmrg      case nir_type_${src_t}:
5601e04c3fSmrg         switch (dst_base) {
577e102996Smaya%           for dst_t in ['int', 'uint', 'float', 'bool']:
5801e04c3fSmrg            case nir_type_${dst_t}:
5901e04c3fSmrg%              if src_t in ['int', 'uint'] and dst_t in ['int', 'uint']:
6001e04c3fSmrg%                 if dst_t == 'int':
6101e04c3fSmrg<%                   continue %>
6201e04c3fSmrg%                 else:
6301e04c3fSmrg<%                   dst_t = src_t %>
6401e04c3fSmrg%                 endif
657e102996Smaya%              elif src_t == 'bool' and dst_t in ['int', 'uint', 'bool']:
667e102996Smaya%                 if dst_t == 'int':
677e102996Smaya<%                   continue %>
6801e04c3fSmrg%                 else:
697e102996Smaya<%                   dst_t = 'int' %>
7001e04c3fSmrg%                 endif
717e102996Smaya%              elif src_t == 'uint' and dst_t == 'bool':
727e102996Smaya<%                src_t = 'int' %>
737e102996Smaya%              endif
747e102996Smaya               switch (dst_bit_size) {
757e102996Smaya%                 for dst_bits in type_sizes(dst_t):
7601e04c3fSmrg                  case ${dst_bits}:
7701e04c3fSmrg%                    if src_t == 'float' and dst_t == 'float' and dst_bits == 16:
7801e04c3fSmrg                     switch(rnd) {
7901e04c3fSmrg%                       for rnd_t in [('rtne', '_rtne'), ('rtz', '_rtz'), ('undef', '')]:
8001e04c3fSmrg                        case nir_rounding_mode_${rnd_t[0]}:
8101e04c3fSmrg                           return ${'nir_op_{0}2{1}{2}{3}'.format(src_t[0], dst_t[0],
8201e04c3fSmrg                                                                   dst_bits, rnd_t[1])};
8301e04c3fSmrg%                       endfor
8401e04c3fSmrg                        default:
8501e04c3fSmrg                           unreachable("Invalid 16-bit nir rounding mode");
8601e04c3fSmrg                     }
8701e04c3fSmrg%                    else:
8801e04c3fSmrg                     assert(rnd == nir_rounding_mode_undef);
8901e04c3fSmrg                     return ${'nir_op_{0}2{1}{2}'.format(src_t[0], dst_t[0], dst_bits)};
9001e04c3fSmrg%                    endif
9101e04c3fSmrg%                 endfor
9201e04c3fSmrg                  default:
9301e04c3fSmrg                     unreachable("Invalid nir alu bit size");
9401e04c3fSmrg               }
9501e04c3fSmrg%           endfor
9601e04c3fSmrg            default:
9701e04c3fSmrg               unreachable("Invalid nir alu base type");
9801e04c3fSmrg         }
9901e04c3fSmrg%     endfor
10001e04c3fSmrg      default:
10101e04c3fSmrg         unreachable("Invalid nir alu base type");
10201e04c3fSmrg   }
10301e04c3fSmrg}
10401e04c3fSmrg
10501e04c3fSmrgconst nir_op_info nir_op_infos[nir_num_opcodes] = {
10601e04c3fSmrg% for name, opcode in sorted(opcodes.items()):
10701e04c3fSmrg{
10801e04c3fSmrg   .name = "${name}",
10901e04c3fSmrg   .num_inputs = ${opcode.num_inputs},
11001e04c3fSmrg   .output_size = ${opcode.output_size},
11101e04c3fSmrg   .output_type = ${"nir_type_" + opcode.output_type},
11201e04c3fSmrg   .input_sizes = {
11301e04c3fSmrg      ${ ", ".join(str(size) for size in opcode.input_sizes) }
11401e04c3fSmrg   },
11501e04c3fSmrg   .input_types = {
11601e04c3fSmrg      ${ ", ".join("nir_type_" + type for type in opcode.input_types) }
11701e04c3fSmrg   },
1187e102996Smaya   .is_conversion = ${"true" if opcode.is_conversion else "false"},
11901e04c3fSmrg   .algebraic_properties =
12001e04c3fSmrg      ${ "0" if opcode.algebraic_properties == "" else " | ".join(
12101e04c3fSmrg            "NIR_OP_IS_" + prop.upper() for prop in
12201e04c3fSmrg               opcode.algebraic_properties.strip().split(" ")) }
12301e04c3fSmrg},
12401e04c3fSmrg% endfor
12501e04c3fSmrg};
12601e04c3fSmrg""")
12701e04c3fSmrg
1287e102996Smayaprint(template.render(opcodes=opcodes, type_sizes=type_sizes))
129