Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/panfrost/bifrost/bi_printer.c.py
4564 views
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
23
TEMPLATE = """#include <stdio.h>
24
#include "compiler.h"
25
26
static const char *
27
bi_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
48
static const char *
49
bir_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
64
static const char *
65
bir_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
75
static void
76
bi_print_index(FILE *fp, bi_index index)
77
{
78
if (bi_is_null(index))
79
fprintf(fp, "_");
80
else if (index.type == BI_INDEX_CONSTANT)
81
fprintf(fp, "#0x%x", index.value);
82
else if (index.type == BI_INDEX_FAU && index.value >= BIR_FAU_UNIFORM)
83
fprintf(fp, "u%u", index.value & ~BIR_FAU_UNIFORM);
84
else if (index.type == BI_INDEX_FAU)
85
fprintf(fp, "%s", bir_fau_name(index.value));
86
else if (index.type == BI_INDEX_PASS)
87
fprintf(fp, "%s", bir_passthrough_name(index.value));
88
else if (index.type == BI_INDEX_REGISTER)
89
fprintf(fp, "br%u", index.value);
90
else if (index.type == BI_INDEX_NORMAL && index.reg)
91
fprintf(fp, "r%u", index.value);
92
else if (index.type == BI_INDEX_NORMAL)
93
fprintf(fp, "%u", index.value);
94
else
95
unreachable("Invalid index");
96
97
if (index.offset)
98
fprintf(fp, "[%u]", index.offset);
99
100
if (index.abs)
101
fputs(".abs", fp);
102
103
if (index.neg)
104
fputs(".neg", fp);
105
106
fputs(bi_swizzle_as_str(index.swizzle), fp);
107
}
108
109
% for mod in sorted(modifiers):
110
% if len(modifiers[mod]) > 2: # otherwise just boolean
111
112
static inline const char *
113
bi_${mod}_as_str(enum bi_${mod} ${mod})
114
{
115
switch (${mod}) {
116
% for i, state in enumerate(sorted(modifiers[mod])):
117
% if state == "none":
118
case BI_${mod.upper()}_NONE: return "";
119
% elif state != "reserved":
120
case BI_${mod.upper()}_${state.upper()}: return ".${state.lower()}";
121
% endif
122
% endfor
123
}
124
125
unreachable("Invalid ${mod}");
126
};
127
% endif
128
% endfor
129
130
<%def name="print_modifiers(mods, table)">
131
% for mod in mods:
132
% if mod not in ["lane_dest"]:
133
% if len(table[mod]) > 2:
134
fputs(bi_${mod}_as_str(I->${mod}), fp);
135
% else:
136
if (I->${mod}) fputs(".${mod}", fp);
137
% endif
138
% endif
139
% endfor
140
</%def>
141
142
<%def name="print_source_modifiers(mods, src, table)">
143
% for mod in mods:
144
% if mod[0:-1] not in ["lane", "lanes", "replicate", "swz", "widen", "swap", "abs", "neg", "sign", "not"]:
145
% if len(table[mod[0:-1]]) > 2:
146
fputs(bi_${mod[0:-1]}_as_str(I->${mod[0:-1]}[${src}]), fp);
147
% elif mod == "bytes2":
148
if (I->bytes2) fputs(".bytes", fp);
149
% else:
150
if (I->${mod[0:-1]}[${src}]) fputs(".${mod[0:-1]}", fp);
151
% endif
152
%endif
153
% endfor
154
</%def>
155
156
void
157
bi_print_instr(bi_instr *I, FILE *fp)
158
{
159
bi_foreach_dest(I, d) {
160
if (bi_is_null(I->dest[d])) break;
161
if (d > 0) fprintf(fp, ", ");
162
163
bi_print_index(fp, I->dest[d]);
164
}
165
166
fprintf(fp, " = %s", bi_opcode_props[I->op].name);
167
168
if (I->table)
169
fprintf(fp, ".%s", bi_table_as_str(I->table));
170
171
switch (I->op) {
172
% for opcode in ops:
173
<%
174
# Extract modifiers that are not per-source
175
root_modifiers = [x for x in ops[opcode]["modifiers"] if x[-1] not in "0123"]
176
%>
177
case BI_OPCODE_${opcode.replace('.', '_').upper()}:
178
${print_modifiers(root_modifiers, modifiers)}
179
fputs(" ", fp);
180
% for src in range(src_count(ops[opcode])):
181
% if src > 0:
182
fputs(", ", fp);
183
% endif
184
bi_print_index(fp, I->src[${src}]);
185
${print_source_modifiers([m for m in ops[opcode]["modifiers"] if m[-1] == str(src)], src, modifiers)}
186
% endfor
187
% for imm in ops[opcode]["immediates"]:
188
fprintf(fp, ", ${imm}:%u", I->${imm});
189
% endfor
190
break;
191
% endfor
192
default:
193
unreachable("Invalid opcode");
194
}
195
196
if (I->branch_target)
197
fprintf(fp, " -> block%u", I->branch_target->base.name);
198
199
fputs("\\n", fp);
200
201
}"""
202
203
import sys
204
from bifrost_isa import *
205
from mako.template import Template
206
207
instructions = parse_instructions(sys.argv[1], include_pseudo = True)
208
ir_instructions = partition_mnemonics(instructions)
209
modifier_lists = order_modifiers(ir_instructions)
210
211
print(Template(COPYRIGHT + TEMPLATE).render(ops = ir_instructions, modifiers = modifier_lists, src_count = src_count))
212
213