Debian Codegraph @

src/panfrost/compiler/bifrost/valhall/disasm.py

raw
1 #encoding=utf-8
2
3 # Copyright (C) 2021 Collabora, Ltd.
4 # Copyright (C) 2026 Arm Ltd.
5 # SPDX-License-Identifier: MIT
6
7 import argparse
8 import sys
9 from valhall import valhall_parse_isa
10 from mako.template import Template
11 from mako import exceptions
12
13 parser = argparse.ArgumentParser()
14 parser.add_argument('--xml', required=True, help='Input ISA XML file')
15
16 args = parser.parse_args()
17
18 (instructions, immediates, enums, typesize, safe_name) = valhall_parse_isa(args.xml)
19
20 template = """
21 #include "disassemble.h"
22
23 #define BIT(b) (1ull << (b))
24 #define MASK(count) ((1ull << (count)) - 1)
25 #define SEXT(b, count) ((b ^ BIT(count - 1)) - BIT(count - 1))
26 #define UNUSED __attribute__((unused))
27
28 #define VA_SRC_UNIFORM_TYPE 0x2
29 #define VA_SRC_IMM_TYPE 0x3
30
31 % for name, en in ENUMS.items():
32 UNUSED static const char *valhall_${name}[] = {
33 % for v in en.values:
34 "${"" if v.default else "." + v.value}",
35 % endfor
36 };
37
38 % endfor
39 static const uint32_t va_immediates[32] = {
40 % for imm in IMMEDIATES:
41 ${hex(imm)},
42 % endfor
43 };
44
45 static inline void
46 va_print_src(FILE *fp, unsigned type, unsigned value, unsigned fau_page)
47 {
48 if (type == VA_SRC_IMM_TYPE) {
49 if (value >= 32) {
50 if (fau_page == 0)
51 fputs(valhall_fau_special_page_0[(value - 0x20) >> 1] + 1, fp);
52 else if (fau_page == 1)
53 fputs(valhall_fau_special_page_1[(value - 0x20) >> 1] + 1, fp);
54 else if (fau_page == 3)
55 fputs(valhall_fau_special_page_3[(value - 0x20) >> 1] + 1, fp);
56 else
57 fprintf(fp, "reserved_page2");
58
59 fprintf(fp, ".w%u", value & 1);
60 } else {
61 fprintf(fp, "0x%X", va_immediates[value]);
62 }
63 } else if (type == VA_SRC_UNIFORM_TYPE) {
64 fprintf(fp, "u%u", value | (fau_page << 6));
65 } else {
66 bool discard = (type & 1);
67 fprintf(fp, "r%u%s", value, discard ? "^" : "");
68 }
69 }
70
71 static inline void
72 va_print_float_src(FILE *fp, unsigned type, unsigned value, unsigned fau_page, bool neg, bool abs)
73 {
74 if (type == VA_SRC_IMM_TYPE) {
75 assert(value < 32 && "overflow in LUT");
76 fprintf(fp, "0x%X", va_immediates[value]);
77 } else {
78 va_print_src(fp, type, value, fau_page);
79 }
80
81 if (neg)
82 fprintf(fp, ".neg");
83
84 if (abs)
85 fprintf(fp, ".abs");
86 }
87
88 static inline void
89 va_print_dest(FILE *fp, unsigned mask, unsigned value, bool can_mask)
90 {
91 fprintf(fp, "r%u", value);
92
93 /* Should write at least one component */
94 // assert(mask != 0);
95 // assert(mask == 0x3 || can_mask);
96
97 if (mask != 0x3)
98 fprintf(fp, ".h%u", (mask == 1) ? 0 : 1);
99 }
100
101 <%def name="print_instr(op)">
102 <% no_comma = True %>
103 fputs("${op.name}", fp);
104 % for mod in op.modifiers:
105 % if mod.name not in ["staging_register_count", "staging_register_write_count"]:
106 % if mod.is_enum:
107 fputs(valhall_${safe_name(mod.enum)}[(instr >> ${mod.start}) & ${hex((1 << mod.size) - 1)}], fp);
108 % else:
109 if (instr & BIT(${mod.start})) fputs(".${mod.name}", fp);
110 % endif
111 % endif
112 % endfor
113 fprintf(fp, "%s ", valhall_flow[(instr >> ${op.offset['flow']}) & ${hex(op.mask['flow'])}]);
114 % for i, dest in enumerate(op.dests):
115 <% no_comma = False %>
116 va_print_dest(fp, (instr >> ${dest.offset['mode']}) & ${hex(dest.mask['mode'])}, (instr >> ${dest.offset['value']}) & ${hex(dest.mask['value'])}, true);
117 % endfor
118 % for index, sr in enumerate(op.staging):
119 % if not no_comma:
120 fputs(", ", fp);
121 % endif
122 <%
123 no_comma = False
124
125 if sr.count != 0:
126 sr_count = sr.count;
127 else:
128 for mod in op.modifiers:
129 if mod.name == "staging_register_write_count" and sr.write:
130 sr_count = f"(((instr >> {mod.start}) & {hex((1 << mod.size) - 1)}) + 1)";
131 elif mod.name == "staging_register_count":
132 sr_count = f"((instr >> {mod.start}) & {hex((1 << mod.size) - 1)})";
133 %>
134 // assert(((instr >> ${sr.start}) & 0xC0) == ${sr.encoded_flags});
135 fprintf(fp, "@");
136 for (unsigned i = 0; i < ${sr_count}; ++i) {
137 fprintf(fp, "%sr%u", (i == 0) ? "" : ":",
138 (uint32_t) (((instr >> ${sr.offset['value']}) & ${hex(sr.mask['value'])}) + i));
139 }
140 % endfor
141 % for i, src in enumerate(op.srcs):
142 % if not no_comma:
143 fputs(", ", fp);
144 % endif
145 <% no_comma = False %>
146 % if src.absneg:
147 va_print_float_src(fp, (instr >> ${src.offset['mode']}) & ${hex(src.mask['mode'])}, (instr >> ${src.offset['value']}) & ${hex(src.mask['value'])},
148 (instr >> ${op.offset['fau_page']}) & ${hex(op.mask['fau_page'])},
149 instr & BIT(${src.offset['neg']}),
150 instr & BIT(${src.offset['abs']}));
151 % elif src.is_float:
152 va_print_float_src(fp, (instr >> ${src.offset['mode']}) & ${src.mask['mode']}, (instr >> ${src.offset['value']}) & ${hex(src.mask['value'])},
153 (instr >> ${op.offset['fau_page']}) & ${hex(op.mask['fau_page'])}, false, false);
154 % else:
155 va_print_src(fp, (instr >> ${src.offset['mode']}) & ${src.mask['mode']}, (instr >> ${src.offset['value']}) & ${hex(src.mask['value'])},
156 (instr >> ${op.offset['fau_page']}) & ${hex(op.mask['fau_page'])});
157 % endif
158 % if src.swizzle:
159 % if src.size == 32:
160 fputs(valhall_widen[(instr >> ${src.offset['swizzle']}) & ${hex(src.mask['swizzle'])}], fp);
161 % else:
162 fputs(valhall_swizzles_16_bit[(instr >> ${src.offset['swizzle']}) & ${hex(src.mask['swizzle'])}], fp);
163 % endif
164 % endif
165 % if src.lanes:
166 fputs(valhall_lanes_8_bit[(instr >> ${src.offset['widen']}) & ${hex(src.mask['widen'])}], fp);
167 % elif src.halfswizzle:
168 fputs(valhall_half_swizzles_8_bit[(instr >> ${src.offset['widen']}) & ${hex(src.mask['widen'])}], fp);
169 % elif src.widen:
170 fputs(valhall_swizzles_${src.size}_bit[(instr >> ${src.offset['widen']}) & ${hex(src.mask['widen'])}], fp);
171 % elif src.combine:
172 fputs(valhall_combine[(instr >> ${src.offset['combine']}) & ${hex(src.mask['combine'])}], fp);
173 % endif
174 % if src.lane:
175 fputs(valhall_lane_${src.size}_bit[(instr >> ${src.offset['lane']}) & ${hex(src.mask['lane'])}], fp);
176 % endif
177 % if 'not' in src.offset:
178 if (instr & BIT(${src.offset['not']})) fputs(".not", fp);
179 % endif
180 % endfor
181 % for imm in op.immediates:
182 <%
183 prefix = "#" if imm.name == "constant" else imm.name + ":"
184 fmt = "%d" if imm.signed else "0x%X"
185 %>
186 fprintf(fp, ", ${prefix}${fmt}", (uint32_t) ${"SEXT(" if imm.signed else ""}
187 ((instr >> ${imm.start}) & MASK(${imm.size})) ${f", {imm.size})" if imm.signed else ""});
188 % endfor
189 </%def>
190
191 <%def name="recurse_subcodes(op_bucket)">
192 %if op_bucket.instr:
193 ${print_instr(op_bucket.instr)}
194 %else:
195 opcode = (instr >> ${op_bucket.start}) & ${hex(op_bucket.mask)};
196 switch (opcode) {
197 %for op in op_bucket.children:
198 case ${hex(op)}:
199 {
200 ${recurse_subcodes(op_bucket.children[op])}
201 break;
202 }
203 %endfor
204 }
205 %endif
206 </%def>
207
208
209 void
210 va_disasm_instr(FILE *fp, uint64_t instr)
211 {
212 unsigned opcode;
213
214 ${recurse_subcodes(OPCODES)}
215 }
216
217 static bool is_branch(uint64_t instr)
218 {
219 <% (exact, mask) = OPCODES.get_exact_mask("BRANCHZ") %>
220 if ((instr & ${hex(mask)}) == ${hex(exact)})
221 return true;
222 <% (exact, mask) = OPCODES.get_exact_mask("BRANCHZI") %>
223 if ((instr & ${hex(mask)}) == ${hex(exact)})
224 return true;
225 return false;
226 }
227
228 void
229 disassemble_valhall(FILE *fp, const void *code, size_t size, bool verbose)
230 {
231 assert((size & 7) == 0);
232
233 const uint64_t *words = (const uint64_t *)code;
234
235 /* Segment into 8-byte instructions */
236 for (unsigned i = 0; i < (size / 8); ++i) {
237 uint64_t instr = words[i];
238
239 if (instr == 0) {
240 fprintf(fp, "\\n");
241 return;
242 }
243
244 if (verbose) {
245 /* Print byte pattern */
246 for (unsigned j = 0; j < 8; ++j)
247 fprintf(fp, "%02x ", (uint8_t)(instr >> (j * 8)));
248
249 fprintf(fp, " ");
250 } else {
251 /* Print whitespace */
252 fprintf(fp, " ");
253 }
254
255 va_disasm_instr(fp, instr);
256 fprintf(fp, "\\n");
257
258 /* Separate blocks visually by inserting whitespace after branches */
259 if (is_branch(instr))
260 fprintf(fp, "\\n");
261 }
262
263 fprintf(fp, "\\n");
264 }
265 """
266
267 class OpBucket:
268 def __init__(self):
269 self.start = None
270 self.mask = None
271 self.instr = None
272 self.children = {}
273
274 def insert(self, subcodes, ins):
275 if len(subcodes) == 0:
276 self.instr = ins
277 else:
278 sc = subcodes[0]
279 assert(self.start is None or self.start == sc.start)
280 assert(self.mask is None or self.mask == sc.mask)
281 self.start = sc.start
282 self.mask = sc.mask
283 if sc.value not in self.children:
284 self.children[sc.value] = OpBucket()
285 self.children[sc.value].insert(subcodes[1:], ins)
286
287 def get_exact_mask(self, op_name, exact = 0, mask = 0):
288 if self.instr:
289 if self.instr.name == op_name:
290 return (exact, mask)
291 else:
292 return ()
293 else:
294 for op in self.children:
295 exact_mask = self.children[op].get_exact_mask(op_name,
296 exact | (op << self.start),
297 mask | (self.mask << self.start))
298 if exact_mask:
299 return exact_mask
300 return ()
301
302 # Build opcode hierarchy:
303 OPCODES = OpBucket()
304 for ins in instructions:
305 OPCODES.insert(ins.opcode, ins)
306
307 try:
308 print(Template(template).render(OPCODES = OPCODES, IMMEDIATES = immediates, ENUMS = enums, typesize = typesize, safe_name = safe_name))
309 except:
310 print(exceptions.text_error_template().render())