• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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            "tls_ptr", "wls_ptr", "program_counter",
59    };
60
61    assert(fau_idx < ARRAY_SIZE(names));
62    return names[fau_idx];
63}
64
65static const char *
66bir_passthrough_name(unsigned idx)
67{
68    const char *names[] = {
69            "s0", "s1", "s2", "t", "fau.x", "fau.y", "t0", "t1"
70    };
71
72    assert(idx < ARRAY_SIZE(names));
73    return names[idx];
74}
75
76static void
77bi_print_index(FILE *fp, bi_index index)
78{
79    if (index.discard)
80        fputs("^", fp);
81
82    if (bi_is_null(index))
83        fprintf(fp, "_");
84    else if (index.type == BI_INDEX_CONSTANT)
85        fprintf(fp, "#0x%x", index.value);
86    else if (index.type == BI_INDEX_FAU && index.value >= BIR_FAU_UNIFORM)
87        fprintf(fp, "u%u", index.value & ~BIR_FAU_UNIFORM);
88    else if (index.type == BI_INDEX_FAU)
89        fprintf(fp, "%s", bir_fau_name(index.value));
90    else if (index.type == BI_INDEX_PASS)
91        fprintf(fp, "%s", bir_passthrough_name(index.value));
92    else if (index.type == BI_INDEX_REGISTER)
93        fprintf(fp, "br%u", index.value);
94    else if (index.type == BI_INDEX_NORMAL && index.reg)
95        fprintf(fp, "r%u", index.value);
96    else if (index.type == BI_INDEX_NORMAL)
97        fprintf(fp, "%u", index.value);
98    else
99        unreachable("Invalid index");
100
101    if (index.offset)
102        fprintf(fp, "[%u]", index.offset);
103
104    if (index.abs)
105        fputs(".abs", fp);
106
107    if (index.neg)
108        fputs(".neg", fp);
109
110    fputs(bi_swizzle_as_str(index.swizzle), fp);
111}
112
113% for mod in sorted(modifiers):
114% if len(modifiers[mod]) > 2: # otherwise just boolean
115
116UNUSED static inline const char *
117bi_${mod}_as_str(enum bi_${mod} ${mod})
118{
119    switch (${mod}) {
120% for i, state in enumerate(sorted(modifiers[mod])):
121% if state == "none":
122    case BI_${mod.upper()}_NONE: return "";
123% elif state != "reserved":
124    case BI_${mod.upper()}_${state.upper()}: return ".${state.lower()}";
125% endif
126% endfor
127    }
128
129    unreachable("Invalid ${mod}");
130};
131% endif
132% endfor
133
134<%def name="print_modifiers(mods, table)">
135    % for mod in mods:
136    % if mod not in ["lane_dest"]:
137    % if len(table[mod]) > 2:
138        fputs(bi_${mod}_as_str(I->${mod}), fp);
139    % else:
140        if (I->${mod}) fputs(".${mod}", fp);
141    % endif
142    % endif
143    % endfor
144</%def>
145
146<%def name="print_source_modifiers(mods, src, table)">
147    % for mod in mods:
148    % if mod[0:-1] not in ["lane", "lanes", "replicate", "swz", "widen", "swap", "abs", "neg", "sign", "not"]:
149    % if len(table[mod[0:-1]]) > 2:
150        fputs(bi_${mod[0:-1]}_as_str(I->${mod[0:-1]}[${src}]), fp);
151    % elif mod == "bytes2":
152        if (I->bytes2) fputs(".bytes", fp);
153    % else:
154        if (I->${mod[0:-1]}[${src}]) fputs(".${mod[0:-1]}", fp);
155    % endif
156    %endif
157    % endfor
158</%def>
159
160void
161bi_print_instr(const bi_instr *I, FILE *fp)
162{
163    if (I->op == BI_OPCODE_SPLIT_I32) {
164        for (unsigned d = 0; d < I->nr_dests; ++d) {
165            if (d > 0) fprintf(fp, ", ");
166
167            bi_print_index(fp, I->dest[d]);
168        }
169    } else {
170        bi_foreach_dest(I, d) {
171            if (bi_is_null(I->dest[d])) break;
172            if (d > 0) fprintf(fp, ", ");
173
174            bi_print_index(fp, I->dest[d]);
175        }
176    }
177
178    fprintf(fp, " = %s", bi_opcode_props[I->op].name);
179
180    if (I->table)
181        fprintf(fp, ".table%u", I->table);
182
183    if (I->flow)
184        fprintf(fp, ".flow%u", I->flow);
185
186    if (I->op == BI_OPCODE_COLLECT_I32) {
187        for (unsigned i = 0; i < I->nr_srcs; ++i) {
188            if (i > 0)
189                fputs(", ", fp);
190            else
191                fputs(" ", fp);
192
193            bi_print_index(fp, I->src[i]);
194        }
195    }
196
197    switch (I->op) {
198% for opcode in ops:
199<%
200    # Extract modifiers that are not per-source
201    root_modifiers = [x for x in ops[opcode]["modifiers"] if x[-1] not in "0123"]
202%>
203    case BI_OPCODE_${opcode.replace('.', '_').upper()}:
204        ${print_modifiers(root_modifiers, modifiers)}
205        fputs(" ", fp);
206    % for src in range(src_count(ops[opcode])):
207    % if src > 0:
208        fputs(", ", fp);
209    % endif
210        bi_print_index(fp, I->src[${src}]);
211        ${print_source_modifiers([m for m in ops[opcode]["modifiers"] if m[-1] == str(src)], src, modifiers)}
212    % endfor
213    % for imm in ops[opcode]["immediates"]:
214        fprintf(fp, ", ${imm}:%u", I->${imm});
215    % endfor
216        break;
217% endfor
218    default:
219        unreachable("Invalid opcode");
220    }
221
222    if (I->branch_target)
223            fprintf(fp, " -> block%u", I->branch_target->index);
224
225    fputs("\\n", fp);
226
227}"""
228
229import sys
230from bifrost_isa import *
231from mako.template import Template
232
233instructions = parse_instructions(sys.argv[1], include_pseudo = True)
234ir_instructions = partition_mnemonics(instructions)
235modifier_lists = order_modifiers(ir_instructions)
236
237print(Template(COPYRIGHT + TEMPLATE).render(ops = ir_instructions, modifiers = modifier_lists, src_count = src_count))
238