1#
2# Copyright (C) 2020 Collabora, Ltd.
3#
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the "Software"),
6# to deal in the Software without restriction, including without limitation
7# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8# and/or sell copies of the Software, and to permit persons to whom the
9# Software is furnished to do so, subject to the following conditions:
10#
11# The above copyright notice and this permission notice (including the next
12# paragraph) shall be included in all copies or substantial portions of the
13# Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21# IN THE SOFTWARE.
22
23TEMPLATE = """#include <stdio.h>
24#include "compiler.h"
25
26static const char *
27bi_swizzle_as_str(enum bi_swizzle swz)
28{
29        switch (swz) {
30        case BI_SWIZZLE_H00: return ".h00";
31        case BI_SWIZZLE_H01: return "";
32        case BI_SWIZZLE_H10: return ".h10";
33        case BI_SWIZZLE_H11: return ".h11";
34        case BI_SWIZZLE_B0000: return ".b0";
35        case BI_SWIZZLE_B1111: return ".b1";
36        case BI_SWIZZLE_B2222: return ".b2";
37        case BI_SWIZZLE_B3333: return ".b3";
38        case BI_SWIZZLE_B0011: return ".b0011";
39        case BI_SWIZZLE_B2233: return ".b2233";
40        case BI_SWIZZLE_B1032: return ".b1032";
41        case BI_SWIZZLE_B3210: return ".b3210";
42        case BI_SWIZZLE_B0022: return ".b0022";
43        }
44
45        unreachable("Invalid swizzle");
46}
47
48static const char *
49bir_fau_name(unsigned fau_idx)
50{
51    const char *names[] = {
52            "zero", "lane-id", "wrap-id", "core-id", "fb-extent",
53            "atest-param", "sample-pos", "reserved",
54            "blend_descriptor_0", "blend_descriptor_1",
55            "blend_descriptor_2", "blend_descriptor_3",
56            "blend_descriptor_4", "blend_descriptor_5",
57            "blend_descriptor_6", "blend_descriptor_7",
58    };
59
60    assert(fau_idx < ARRAY_SIZE(names));
61    return names[fau_idx];
62}
63
64static const char *
65bir_passthrough_name(unsigned idx)
66{
67    const char *names[] = {
68            "s0", "s1", "s2", "t", "fau.x", "fau.y", "t0", "t1"
69    };
70
71    assert(idx < ARRAY_SIZE(names));
72    return names[idx];
73}
74
75static void
76bi_print_index(FILE *fp, bi_index index)
77{
78    if (index.discard)
79        fputs("`", fp);
80
81    if (bi_is_null(index))
82        fprintf(fp, "_");
83    else if (index.type == BI_INDEX_CONSTANT)
84        fprintf(fp, "#0x%x", index.value);
85    else if (index.type == BI_INDEX_FAU && index.value >= BIR_FAU_UNIFORM)
86        fprintf(fp, "u%u", index.value & ~BIR_FAU_UNIFORM);
87    else if (index.type == BI_INDEX_FAU)
88        fprintf(fp, "%s", bir_fau_name(index.value));
89    else if (index.type == BI_INDEX_PASS)
90        fprintf(fp, "%s", bir_passthrough_name(index.value));
91    else if (index.type == BI_INDEX_REGISTER)
92        fprintf(fp, "br%u", index.value);
93    else if (index.type == BI_INDEX_NORMAL && index.reg)
94        fprintf(fp, "r%u", index.value);
95    else if (index.type == BI_INDEX_NORMAL)
96        fprintf(fp, "%u", index.value);
97    else
98        unreachable("Invalid index");
99
100    if (index.offset)
101        fprintf(fp, "[%u]", index.offset);
102
103    if (index.abs)
104        fputs(".abs", fp);
105
106    if (index.neg)
107        fputs(".neg", fp);
108
109    fputs(bi_swizzle_as_str(index.swizzle), fp);
110}
111
112% for mod in sorted(modifiers):
113% if len(modifiers[mod]) > 2: # otherwise just boolean
114
115UNUSED static inline const char *
116bi_${mod}_as_str(enum bi_${mod} ${mod})
117{
118    switch (${mod}) {
119% for i, state in enumerate(sorted(modifiers[mod])):
120% if state == "none":
121    case BI_${mod.upper()}_NONE: return "";
122% elif state != "reserved":
123    case BI_${mod.upper()}_${state.upper()}: return ".${state.lower()}";
124% endif
125% endfor
126    }
127
128    unreachable("Invalid ${mod}");
129};
130% endif
131% endfor
132
133<%def name="print_modifiers(mods, table)">
134    % for mod in mods:
135    % if mod not in ["lane_dest"]:
136    % if len(table[mod]) > 2:
137        fputs(bi_${mod}_as_str(I->${mod}), fp);
138    % else:
139        if (I->${mod}) fputs(".${mod}", fp);
140    % endif
141    % endif
142    % endfor
143</%def>
144
145<%def name="print_source_modifiers(mods, src, table)">
146    % for mod in mods:
147    % if mod[0:-1] not in ["lane", "lanes", "replicate", "swz", "widen", "swap", "abs", "neg", "sign", "not"]:
148    % if len(table[mod[0:-1]]) > 2:
149        fputs(bi_${mod[0:-1]}_as_str(I->${mod[0:-1]}[${src}]), fp);
150    % elif mod == "bytes2":
151        if (I->bytes2) fputs(".bytes", fp);
152    % else:
153        if (I->${mod[0:-1]}[${src}]) fputs(".${mod[0:-1]}", fp);
154    % endif
155    %endif
156    % endfor
157</%def>
158
159void
160bi_print_instr(const bi_instr *I, FILE *fp)
161{
162    bi_foreach_dest(I, d) {
163        if (bi_is_null(I->dest[d])) break;
164        if (d > 0) fprintf(fp, ", ");
165
166        bi_print_index(fp, I->dest[d]);
167    }
168
169    fprintf(fp, " = %s", bi_opcode_props[I->op].name);
170
171    if (I->table)
172        fprintf(fp, ".%s", bi_table_as_str(I->table));
173
174    switch (I->op) {
175% for opcode in ops:
176<%
177    # Extract modifiers that are not per-source
178    root_modifiers = [x for x in ops[opcode]["modifiers"] if x[-1] not in "0123"]
179%>
180    case BI_OPCODE_${opcode.replace('.', '_').upper()}:
181        ${print_modifiers(root_modifiers, modifiers)}
182        fputs(" ", fp);
183    % for src in range(src_count(ops[opcode])):
184    % if src > 0:
185        fputs(", ", fp);
186    % endif
187        bi_print_index(fp, I->src[${src}]);
188        ${print_source_modifiers([m for m in ops[opcode]["modifiers"] if m[-1] == str(src)], src, modifiers)}
189    % endfor
190    % for imm in ops[opcode]["immediates"]:
191        fprintf(fp, ", ${imm}:%u", I->${imm});
192    % endfor
193        break;
194% endfor
195    default:
196        unreachable("Invalid opcode");
197    }
198
199    if (I->branch_target)
200            fprintf(fp, " -> block%u", I->branch_target->name);
201
202    fputs("\\n", fp);
203
204}"""
205
206import sys
207from bifrost_isa import *
208from mako.template import Template
209
210instructions = parse_instructions(sys.argv[1], include_pseudo = True)
211ir_instructions = partition_mnemonics(instructions)
212modifier_lists = order_modifiers(ir_instructions)
213
214print(Template(COPYRIGHT + TEMPLATE).render(ops = ir_instructions, modifiers = modifier_lists, src_count = src_count))
215