1b8e80941Smrg#
2b8e80941Smrg# Copyright (C) 2014 Connor Abbott
3b8e80941Smrg#
4b8e80941Smrg# Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg# copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg# to deal in the Software without restriction, including without limitation
7b8e80941Smrg# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg# and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg# Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg#
11b8e80941Smrg# The above copyright notice and this permission notice (including the next
12b8e80941Smrg# paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg# Software.
14b8e80941Smrg#
15b8e80941Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21b8e80941Smrg# IN THE SOFTWARE.
22b8e80941Smrg#
23b8e80941Smrg# Authors:
24b8e80941Smrg#    Connor Abbott (cwabbott0@gmail.com)
25b8e80941Smrg
26b8e80941Smrgfrom __future__ import print_function
27b8e80941Smrg
28b8e80941Smrgfrom nir_opcodes import opcodes, type_sizes
29b8e80941Smrgfrom mako.template import Template
30b8e80941Smrg
31b8e80941Smrgtemplate = Template("""
32b8e80941Smrg#include "nir.h"
33b8e80941Smrg
34b8e80941Smrgnir_op
35b8e80941Smrgnir_type_conversion_op(nir_alu_type src, nir_alu_type dst, nir_rounding_mode rnd)
36b8e80941Smrg{
37b8e80941Smrg   nir_alu_type src_base = (nir_alu_type) nir_alu_type_get_base_type(src);
38b8e80941Smrg   nir_alu_type dst_base = (nir_alu_type) nir_alu_type_get_base_type(dst);
39b8e80941Smrg   unsigned src_bit_size = nir_alu_type_get_type_size(src);
40b8e80941Smrg   unsigned dst_bit_size = nir_alu_type_get_type_size(dst);
41b8e80941Smrg
42b8e80941Smrg   if (src == dst && src_base == nir_type_float) {
43b8e80941Smrg      return nir_op_fmov;
44b8e80941Smrg   } else if (src == dst && src_base == nir_type_bool) {
45b8e80941Smrg      return nir_op_imov;
46b8e80941Smrg   } else if ((src_base == nir_type_int || src_base == nir_type_uint) &&
47b8e80941Smrg              (dst_base == nir_type_int || dst_base == nir_type_uint) &&
48b8e80941Smrg              src_bit_size == dst_bit_size) {
49b8e80941Smrg      /* Integer <-> integer conversions with the same bit-size on both
50b8e80941Smrg       * ends are just no-op moves.
51b8e80941Smrg       */
52b8e80941Smrg      return nir_op_imov;
53b8e80941Smrg   }
54b8e80941Smrg
55b8e80941Smrg   switch (src_base) {
56b8e80941Smrg%     for src_t in ['int', 'uint', 'float', 'bool']:
57b8e80941Smrg      case nir_type_${src_t}:
58b8e80941Smrg         switch (dst_base) {
59b8e80941Smrg%           for dst_t in ['int', 'uint', 'float', 'bool']:
60b8e80941Smrg            case nir_type_${dst_t}:
61b8e80941Smrg%              if src_t in ['int', 'uint'] and dst_t in ['int', 'uint']:
62b8e80941Smrg%                 if dst_t == 'int':
63b8e80941Smrg<%                   continue %>
64b8e80941Smrg%                 else:
65b8e80941Smrg<%                   dst_t = src_t %>
66b8e80941Smrg%                 endif
67b8e80941Smrg%              elif src_t == 'bool' and dst_t in ['int', 'uint', 'bool']:
68b8e80941Smrg%                 if dst_t == 'int':
69b8e80941Smrg<%                   continue %>
70b8e80941Smrg%                 else:
71b8e80941Smrg<%                   dst_t = 'int' %>
72b8e80941Smrg%                 endif
73b8e80941Smrg%              elif src_t == 'uint' and dst_t == 'bool':
74b8e80941Smrg<%                src_t = 'int' %>
75b8e80941Smrg%              endif
76b8e80941Smrg               switch (dst_bit_size) {
77b8e80941Smrg%                 for dst_bits in type_sizes(dst_t):
78b8e80941Smrg                  case ${dst_bits}:
79b8e80941Smrg%                    if src_t == 'float' and dst_t == 'float' and dst_bits == 16:
80b8e80941Smrg                     switch(rnd) {
81b8e80941Smrg%                       for rnd_t in [('rtne', '_rtne'), ('rtz', '_rtz'), ('undef', '')]:
82b8e80941Smrg                        case nir_rounding_mode_${rnd_t[0]}:
83b8e80941Smrg                           return ${'nir_op_{0}2{1}{2}{3}'.format(src_t[0], dst_t[0],
84b8e80941Smrg                                                                   dst_bits, rnd_t[1])};
85b8e80941Smrg%                       endfor
86b8e80941Smrg                        default:
87b8e80941Smrg                           unreachable("Invalid 16-bit nir rounding mode");
88b8e80941Smrg                     }
89b8e80941Smrg%                    else:
90b8e80941Smrg                     assert(rnd == nir_rounding_mode_undef);
91b8e80941Smrg                     return ${'nir_op_{0}2{1}{2}'.format(src_t[0], dst_t[0], dst_bits)};
92b8e80941Smrg%                    endif
93b8e80941Smrg%                 endfor
94b8e80941Smrg                  default:
95b8e80941Smrg                     unreachable("Invalid nir alu bit size");
96b8e80941Smrg               }
97b8e80941Smrg%           endfor
98b8e80941Smrg            default:
99b8e80941Smrg               unreachable("Invalid nir alu base type");
100b8e80941Smrg         }
101b8e80941Smrg%     endfor
102b8e80941Smrg      default:
103b8e80941Smrg         unreachable("Invalid nir alu base type");
104b8e80941Smrg   }
105b8e80941Smrg}
106b8e80941Smrg
107b8e80941Smrgconst nir_op_info nir_op_infos[nir_num_opcodes] = {
108b8e80941Smrg% for name, opcode in sorted(opcodes.items()):
109b8e80941Smrg{
110b8e80941Smrg   .name = "${name}",
111b8e80941Smrg   .num_inputs = ${opcode.num_inputs},
112b8e80941Smrg   .output_size = ${opcode.output_size},
113b8e80941Smrg   .output_type = ${"nir_type_" + opcode.output_type},
114b8e80941Smrg   .input_sizes = {
115b8e80941Smrg      ${ ", ".join(str(size) for size in opcode.input_sizes) }
116b8e80941Smrg   },
117b8e80941Smrg   .input_types = {
118b8e80941Smrg      ${ ", ".join("nir_type_" + type for type in opcode.input_types) }
119b8e80941Smrg   },
120b8e80941Smrg   .is_conversion = ${"true" if opcode.is_conversion else "false"},
121b8e80941Smrg   .algebraic_properties =
122b8e80941Smrg      ${ "0" if opcode.algebraic_properties == "" else " | ".join(
123b8e80941Smrg            "NIR_OP_IS_" + prop.upper() for prop in
124b8e80941Smrg               opcode.algebraic_properties.strip().split(" ")) }
125b8e80941Smrg},
126b8e80941Smrg% endfor
127b8e80941Smrg};
128b8e80941Smrg""")
129b8e80941Smrg
130b8e80941Smrgprint(template.render(opcodes=opcodes, type_sizes=type_sizes))
131