1bf215546Sopenharmony_ci#
2bf215546Sopenharmony_ci# Copyright (C) 2014 Connor Abbott
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# Authors:
24bf215546Sopenharmony_ci#    Connor Abbott (cwabbott0@gmail.com)
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_cifrom nir_opcodes import opcodes, type_sizes
27bf215546Sopenharmony_cifrom mako.template import Template
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_citemplate = Template("""
30bf215546Sopenharmony_ci#include "nir.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_cinir_op
33bf215546Sopenharmony_cinir_type_conversion_op(nir_alu_type src, nir_alu_type dst, nir_rounding_mode rnd)
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci   nir_alu_type src_base = (nir_alu_type) nir_alu_type_get_base_type(src);
36bf215546Sopenharmony_ci   nir_alu_type dst_base = (nir_alu_type) nir_alu_type_get_base_type(dst);
37bf215546Sopenharmony_ci   unsigned src_bit_size = nir_alu_type_get_type_size(src);
38bf215546Sopenharmony_ci   unsigned dst_bit_size = nir_alu_type_get_type_size(dst);
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci   if (src == dst && src_base == nir_type_float) {
41bf215546Sopenharmony_ci      return nir_op_mov;
42bf215546Sopenharmony_ci   } else if (src == dst && src_base == nir_type_bool) {
43bf215546Sopenharmony_ci      return nir_op_mov;
44bf215546Sopenharmony_ci   } else if ((src_base == nir_type_int || src_base == nir_type_uint) &&
45bf215546Sopenharmony_ci              (dst_base == nir_type_int || dst_base == nir_type_uint) &&
46bf215546Sopenharmony_ci              src_bit_size == dst_bit_size) {
47bf215546Sopenharmony_ci      /* Integer <-> integer conversions with the same bit-size on both
48bf215546Sopenharmony_ci       * ends are just no-op moves.
49bf215546Sopenharmony_ci       */
50bf215546Sopenharmony_ci      return nir_op_mov;
51bf215546Sopenharmony_ci   }
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci   switch (src_base) {
54bf215546Sopenharmony_ci%     for src_t in ['int', 'uint', 'float', 'bool']:
55bf215546Sopenharmony_ci      case nir_type_${src_t}:
56bf215546Sopenharmony_ci         switch (dst_base) {
57bf215546Sopenharmony_ci%           for dst_t in ['int', 'uint', 'float', 'bool']:
58bf215546Sopenharmony_ci            case nir_type_${dst_t}:
59bf215546Sopenharmony_ci%              if src_t in ['int', 'uint'] and dst_t in ['int', 'uint']:
60bf215546Sopenharmony_ci%                 if dst_t == 'int':
61bf215546Sopenharmony_ci<%                   continue %>
62bf215546Sopenharmony_ci%                 else:
63bf215546Sopenharmony_ci<%                   dst_t = src_t %>
64bf215546Sopenharmony_ci%                 endif
65bf215546Sopenharmony_ci%              elif src_t == 'bool' and dst_t in ['int', 'uint', 'bool']:
66bf215546Sopenharmony_ci%                 if dst_t == 'int':
67bf215546Sopenharmony_ci<%                   continue %>
68bf215546Sopenharmony_ci%                 else:
69bf215546Sopenharmony_ci<%                   dst_t = 'int' %>
70bf215546Sopenharmony_ci%                 endif
71bf215546Sopenharmony_ci%              elif src_t == 'uint' and dst_t == 'bool':
72bf215546Sopenharmony_ci<%                src_t = 'int' %>
73bf215546Sopenharmony_ci%              endif
74bf215546Sopenharmony_ci               switch (dst_bit_size) {
75bf215546Sopenharmony_ci%                 for dst_bits in type_sizes(dst_t):
76bf215546Sopenharmony_ci                  case ${dst_bits}:
77bf215546Sopenharmony_ci%                    if src_t == 'float' and dst_t == 'float' and dst_bits == 16:
78bf215546Sopenharmony_ci                     switch(rnd) {
79bf215546Sopenharmony_ci%                       for rnd_t in [('rtne', '_rtne'), ('rtz', '_rtz'), ('undef', '')]:
80bf215546Sopenharmony_ci                        case nir_rounding_mode_${rnd_t[0]}:
81bf215546Sopenharmony_ci                           return ${'nir_op_{0}2{1}{2}{3}'.format(src_t[0], dst_t[0],
82bf215546Sopenharmony_ci                                                                   dst_bits, rnd_t[1])};
83bf215546Sopenharmony_ci%                       endfor
84bf215546Sopenharmony_ci                        default:
85bf215546Sopenharmony_ci                           unreachable("Invalid 16-bit nir rounding mode");
86bf215546Sopenharmony_ci                     }
87bf215546Sopenharmony_ci%                    else:
88bf215546Sopenharmony_ci                     assert(rnd == nir_rounding_mode_undef);
89bf215546Sopenharmony_ci                     return ${'nir_op_{0}2{1}{2}'.format(src_t[0], dst_t[0], dst_bits)};
90bf215546Sopenharmony_ci%                    endif
91bf215546Sopenharmony_ci%                 endfor
92bf215546Sopenharmony_ci                  default:
93bf215546Sopenharmony_ci                     unreachable("Invalid nir alu bit size");
94bf215546Sopenharmony_ci               }
95bf215546Sopenharmony_ci%           endfor
96bf215546Sopenharmony_ci            default:
97bf215546Sopenharmony_ci               unreachable("Invalid nir alu base type");
98bf215546Sopenharmony_ci         }
99bf215546Sopenharmony_ci%     endfor
100bf215546Sopenharmony_ci      default:
101bf215546Sopenharmony_ci         unreachable("Invalid nir alu base type");
102bf215546Sopenharmony_ci   }
103bf215546Sopenharmony_ci}
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ciconst nir_op_info nir_op_infos[nir_num_opcodes] = {
106bf215546Sopenharmony_ci% for name, opcode in sorted(opcodes.items()):
107bf215546Sopenharmony_ci{
108bf215546Sopenharmony_ci   .name = "${name}",
109bf215546Sopenharmony_ci   .num_inputs = ${opcode.num_inputs},
110bf215546Sopenharmony_ci   .output_size = ${opcode.output_size},
111bf215546Sopenharmony_ci   .output_type = ${"nir_type_" + opcode.output_type},
112bf215546Sopenharmony_ci   .input_sizes = {
113bf215546Sopenharmony_ci      ${ ", ".join(str(size) for size in opcode.input_sizes) }
114bf215546Sopenharmony_ci   },
115bf215546Sopenharmony_ci   .input_types = {
116bf215546Sopenharmony_ci      ${ ", ".join("nir_type_" + type for type in opcode.input_types) }
117bf215546Sopenharmony_ci   },
118bf215546Sopenharmony_ci   .is_conversion = ${"true" if opcode.is_conversion else "false"},
119bf215546Sopenharmony_ci   .algebraic_properties =
120bf215546Sopenharmony_ci      ${ "0" if opcode.algebraic_properties == "" else " | ".join(
121bf215546Sopenharmony_ci            "NIR_OP_IS_" + prop.upper() for prop in
122bf215546Sopenharmony_ci               opcode.algebraic_properties.strip().split(" ")) }
123bf215546Sopenharmony_ci},
124bf215546Sopenharmony_ci% endfor
125bf215546Sopenharmony_ci};
126bf215546Sopenharmony_ci""")
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ciprint(template.render(opcodes=opcodes, type_sizes=type_sizes))
129