Debian Codegraph @

src/asahi/isa/disasm-internal.h

raw
1 /*
2 * Copyright 2025 Valve Corporation
3 * SPDX-License-Identifier: MIT
4 */
5 #include <assert.h>
6 #include <inttypes.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include "asahi/isa/agx_minifloat.h"
11 #include "util/bitset.h"
12 #include "util/u_math.h"
13 #include "disasm.h"
14
15 struct disasm_ctx {
16 FILE *fp;
17 bool any_operands;
18 bool error;
19 };
20
21 static inline uint64_t
22 bits(BITSET_WORD *word, unsigned start, unsigned size, unsigned shift)
23 {
24 return ((uint64_t)__bitset_extract(word, start, size)) << shift;
25 }
26
27 typedef void (*disasm_instr)(struct disasm_ctx *ctx, BITSET_WORD *code);
28
29 struct disasm_spec {
30 const char *display;
31 disasm_instr disassemble;
32 unsigned length_bit;
33 unsigned length_short;
34 unsigned length_long;
35 BITSET_WORD mask[4];
36 BITSET_WORD exact[4];
37 BITSET_WORD known[4];
38 };
39
40 static void
41 introduce_operand(struct disasm_ctx *ctx)
42 {
43 if (ctx->any_operands) {
44 fprintf(ctx->fp, ", ");
45 } else {
46 fprintf(ctx->fp, " ");
47 }
48
49 ctx->any_operands = true;
50 }
51
52 enum operand_kind {
53 KIND_NONE = 0,
54 KIND_REG,
55 KIND_UNIFORM,
56 KIND_CF,
57 KIND_TS,
58 KIND_SS,
59 KIND_IMM,
60 KIND_FIMM,
61 };
62
63 struct operand_desc {
64 enum operand_kind kind;
65 int32_t value;
66 unsigned hint, count;
67 bool optional, size16, size32, size64, abs, neg, sx, cache, lu;
68 };
69
70 static void
71 _print_operand(struct disasm_ctx *ctx, struct operand_desc d)
72 {
73 if (d.kind == KIND_NONE) {
74 if (!d.optional) {
75 introduce_operand(ctx);
76 fprintf(ctx->fp, "_");
77 }
78
79 return;
80 }
81
82 unsigned size = d.size64 ? 64 : d.size32 ? 32 : 16;
83 bool cache = d.cache || d.hint == 2;
84 bool lu = d.lu || d.hint == 3;
85 unsigned count = MAX2(d.count, 1);
86
87 introduce_operand(ctx);
88
89 if (lu && cache) {
90 fprintf(ctx->fp, "XXX invalid cache+lu set\n");
91 ctx->error = true;
92 }
93
94 if (lu)
95 fprintf(ctx->fp, "^");
96 if (cache)
97 fprintf(ctx->fp, "$");
98
99 const char *prefixes[][2] = {
100 [KIND_REG] = {"r", "dr"}, [KIND_UNIFORM] = {"u", "du"},
101 [KIND_CF] = {"cf"}, [KIND_TS] = {"ts"},
102 [KIND_SS] = {"ss"},
103 };
104
105 if (d.kind == KIND_IMM) {
106 fprintf(ctx->fp, "%d", d.value);
107 } else if (d.kind == KIND_FIMM) {
108 float f = agx_minifloat_decode(d.value);
109
110 /* Match python's float printing */
111 if (f == (int)f)
112 fprintf(ctx->fp, "%g.0", agx_minifloat_decode(d.value));
113 else
114 fprintf(ctx->fp, "%g", agx_minifloat_decode(d.value));
115 } else if (d.kind == KIND_CF || d.kind == KIND_TS || d.kind == KIND_SS) {
116 fprintf(ctx->fp, "%s%u", prefixes[d.kind][0], d.value);
117 } else {
118 for (unsigned i = 0; i < count; ++i) {
119 if (i)
120 fprintf(ctx->fp, "_");
121
122 unsigned reg = d.value + (i * (size / 16));
123 unsigned whole = reg >> 1;
124 unsigned part = reg & 1;
125 const char *prefix = prefixes[d.kind][size == 64];
126
127 if (size == 16) {
128 fprintf(ctx->fp, "%s%u%c", prefix, whole, "lh"[part]);
129 } else {
130 if (part != 0) {
131 fprintf(ctx->fp, "# 32-bit must be expected, but got raw %u\n",
132 reg);
133 ctx->error = true;
134 }
135
136 fprintf(ctx->fp, "%s%u", prefix, whole);
137 }
138 }
139 }
140
141 if (d.abs)
142 fprintf(ctx->fp, ".abs");
143 if (d.neg)
144 fprintf(ctx->fp, ".neg");
145 if (d.sx)
146 fprintf(ctx->fp, ".sx");
147 }
148
149 static void
150 print_immediate(struct disasm_ctx *ctx, bool is_signed, uint32_t value)
151 {
152 introduce_operand(ctx);
153 fprintf(ctx->fp, is_signed ? "%d" : "%u", value);
154 }
155
156 static void
157 _print_enum(struct disasm_ctx *ctx, const char **arr, unsigned n,
158 unsigned value)
159 {
160 if (value >= n || arr[value] == NULL) {
161 introduce_operand(ctx);
162 fprintf(ctx->fp, "XXX: Unknown enum value %u", value);
163 ctx->error = true;
164 } else if (arr[value][0] != 0) {
165 introduce_operand(ctx);
166 fprintf(ctx->fp, "%s", arr[value]);
167 }
168 }
169
170 #define print_enum(ctx, arr, value) \
171 _print_enum(ctx, arr, ARRAY_SIZE(arr), value)
172
173 static void
174 print_modifier(struct disasm_ctx *ctx, const char *display, unsigned value)
175 {
176 if (value) {
177 introduce_operand(ctx);
178 fprintf(ctx->fp, "%s", display);
179 }
180 }
181
182 static signed
183 _disassemble_instr(BITSET_WORD *code, FILE *fp, struct disasm_spec *specs,
184 unsigned nr_specs, unsigned offset, bool verbose)
185 {
186 BITSET_WORD tmp[4];
187 memcpy(tmp, code, sizeof(tmp));
188 struct disasm_spec *spec = NULL;
189 unsigned n = 0;
190 bool match = false;
191 BITSET_WORD masked[4] = {0};
192
193 for (unsigned i = 0; i < nr_specs; ++i) {
194 spec = &specs[i];
195
196 n = spec->length_short;
197 if (BITSET_TEST(tmp, spec->length_bit)) {
198 n = spec->length_long;
199 }
200
201 match = true;
202 for (unsigned i = 0; i < ARRAY_SIZE(tmp); ++i) {
203 masked[i] = tmp[i];
204
205 uint32_t bytes_left = (n - (i * 4));
206 if (bytes_left < 4) {
207 masked[i] &= ((1 << (bytes_left * 8)) - 1);
208 }
209
210 if ((masked[i] & spec->mask[i]) != spec->exact[i]) {
211 match = false;
212 break;
213 }
214 }
215
216 if (match)
217 break;
218 }
219
220 struct disasm_ctx ctx = {.fp = fp};
221
222 if (match) {
223 BITSET_WORD unknown[4];
224 __bitset_andnot(unknown, masked, spec->known, ARRAY_SIZE(masked));
225 int i;
226 BITSET_FOREACH_SET(i, unknown, n * 8) {
227 fprintf(fp, "# XXX: Unknown bit set %u\n", i);
228 ctx.error = true;
229 }
230 } else {
231 n = 2;
232 }
233
234 if (verbose) {
235 fprintf(fp, "%4x: ", offset);
236 for (unsigned i = 0; i < n; ++i) {
237 fprintf(fp, "%02x", __bitset_extract(masked, i * 8, 8));
238 }
239 for (unsigned i = n; i < 11; ++i) {
240 fprintf(fp, " ");
241 }
242 fprintf(fp, " ");
243 }
244
245 if (!match) {
246 fprintf(fp, "<unknown instr>\n");
247 return -n;
248 }
249
250 fprintf(fp, "%s", spec->display);
251
252 if (spec->disassemble)
253 spec->disassemble(&ctx, masked);
254
255 fprintf(fp, "\n");
256
257 return ctx.error ? -n : n;
258 }