freedreno/ir3: sync instr/disasm and add ldib encoding
[mesa.git] / src / freedreno / ir3 / disasm-a3xx.c
1 /*
2 * Copyright (c) 2013 Rob Clark <robdclark@gmail.com>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include <string.h>
29 #include <assert.h>
30
31 #include <util/u_debug.h>
32
33 #include "instr-a3xx.h"
34
35 /* bitmask of debug flags */
36 enum debug_t {
37 PRINT_RAW = 0x1, /* dump raw hexdump */
38 PRINT_VERBOSE = 0x2,
39 };
40
41 static enum debug_t debug;
42
43 #define printf debug_printf
44
45 static const char *levels[] = {
46 "",
47 "\t",
48 "\t\t",
49 "\t\t\t",
50 "\t\t\t\t",
51 "\t\t\t\t\t",
52 "\t\t\t\t\t\t",
53 "\t\t\t\t\t\t\t",
54 "\t\t\t\t\t\t\t\t",
55 "\t\t\t\t\t\t\t\t\t",
56 "x",
57 "x",
58 "x",
59 "x",
60 "x",
61 "x",
62 };
63
64 static const char *component = "xyzw";
65
66 static const char *type[] = {
67 [TYPE_F16] = "f16",
68 [TYPE_F32] = "f32",
69 [TYPE_U16] = "u16",
70 [TYPE_U32] = "u32",
71 [TYPE_S16] = "s16",
72 [TYPE_S32] = "s32",
73 [TYPE_U8] = "u8",
74 [TYPE_S8] = "s8",
75 };
76
77 struct disasm_ctx {
78 FILE *out;
79 int level;
80 unsigned gpu_id;
81
82 /* current instruction repeat flag: */
83 unsigned repeat;
84 };
85
86 static void print_reg(struct disasm_ctx *ctx, reg_t reg, bool full, bool r,
87 bool c, bool im, bool neg, bool abs, bool addr_rel)
88 {
89 const char type = c ? 'c' : 'r';
90
91 // XXX I prefer - and || for neg/abs, but preserving format used
92 // by libllvm-a3xx for easy diffing..
93
94 if (abs && neg)
95 fprintf(ctx->out, "(absneg)");
96 else if (neg)
97 fprintf(ctx->out, "(neg)");
98 else if (abs)
99 fprintf(ctx->out, "(abs)");
100
101 if (r)
102 fprintf(ctx->out, "(r)");
103
104 if (im) {
105 fprintf(ctx->out, "%d", reg.iim_val);
106 } else if (addr_rel) {
107 /* I would just use %+d but trying to make it diff'able with
108 * libllvm-a3xx...
109 */
110 if (reg.iim_val < 0)
111 fprintf(ctx->out, "%s%c<a0.x - %d>", full ? "" : "h", type, -reg.iim_val);
112 else if (reg.iim_val > 0)
113 fprintf(ctx->out, "%s%c<a0.x + %d>", full ? "" : "h", type, reg.iim_val);
114 else
115 fprintf(ctx->out, "%s%c<a0.x>", full ? "" : "h", type);
116 } else if ((reg.num == REG_A0) && !c) {
117 fprintf(ctx->out, "a0.%c", component[reg.comp]);
118 } else if ((reg.num == REG_P0) && !c) {
119 fprintf(ctx->out, "p0.%c", component[reg.comp]);
120 } else {
121 fprintf(ctx->out, "%s%c%d.%c", full ? "" : "h", type, reg.num, component[reg.comp]);
122 }
123 }
124
125
126 static void print_reg_dst(struct disasm_ctx *ctx, reg_t reg, bool full, bool addr_rel)
127 {
128 print_reg(ctx, reg, full, false, false, false, false, false, addr_rel);
129 }
130
131 static void print_reg_src(struct disasm_ctx *ctx, reg_t reg, bool full, bool r,
132 bool c, bool im, bool neg, bool abs, bool addr_rel)
133 {
134 print_reg(ctx, reg, full, r, c, im, neg, abs, addr_rel);
135 }
136
137 /* TODO switch to using reginfo struct everywhere, since more readable
138 * than passing a bunch of bools to print_reg_src
139 */
140
141 struct reginfo {
142 reg_t reg;
143 bool full;
144 bool r;
145 bool c;
146 bool im;
147 bool neg;
148 bool abs;
149 bool addr_rel;
150 };
151
152 static void print_src(struct disasm_ctx *ctx, struct reginfo *info)
153 {
154 print_reg_src(ctx, info->reg, info->full, info->r, info->c, info->im,
155 info->neg, info->abs, info->addr_rel);
156 }
157
158 //static void print_dst(struct disasm_ctx *ctx, struct reginfo *info)
159 //{
160 // print_reg_dst(ctx, info->reg, info->full, info->addr_rel);
161 //}
162
163 static void print_instr_cat0(struct disasm_ctx *ctx, instr_t *instr)
164 {
165 instr_cat0_t *cat0 = &instr->cat0;
166
167 switch (cat0->opc) {
168 case OPC_KILL:
169 fprintf(ctx->out, " %sp0.%c", cat0->inv ? "!" : "",
170 component[cat0->comp]);
171 break;
172 case OPC_BR:
173 fprintf(ctx->out, " %sp0.%c, #%d", cat0->inv ? "!" : "",
174 component[cat0->comp], cat0->a3xx.immed);
175 break;
176 case OPC_JUMP:
177 case OPC_CALL:
178 fprintf(ctx->out, " #%d", cat0->a3xx.immed);
179 break;
180 }
181
182 if ((debug & PRINT_VERBOSE) && (cat0->dummy2|cat0->dummy3|cat0->dummy4))
183 fprintf(ctx->out, "\t{0: %x,%x,%x}", cat0->dummy2, cat0->dummy3, cat0->dummy4);
184 }
185
186 static void print_instr_cat1(struct disasm_ctx *ctx, instr_t *instr)
187 {
188 instr_cat1_t *cat1 = &instr->cat1;
189
190 if (cat1->ul)
191 fprintf(ctx->out, "(ul)");
192
193 if (cat1->src_type == cat1->dst_type) {
194 if ((cat1->src_type == TYPE_S16) && (((reg_t)cat1->dst).num == REG_A0)) {
195 /* special case (nmemonic?): */
196 fprintf(ctx->out, "mova");
197 } else {
198 fprintf(ctx->out, "mov.%s%s", type[cat1->src_type], type[cat1->dst_type]);
199 }
200 } else {
201 fprintf(ctx->out, "cov.%s%s", type[cat1->src_type], type[cat1->dst_type]);
202 }
203
204 fprintf(ctx->out, " ");
205
206 if (cat1->even)
207 fprintf(ctx->out, "(even)");
208
209 if (cat1->pos_inf)
210 fprintf(ctx->out, "(pos_infinity)");
211
212 print_reg_dst(ctx, (reg_t)(cat1->dst), type_size(cat1->dst_type) == 32,
213 cat1->dst_rel);
214
215 fprintf(ctx->out, ", ");
216
217 /* ugg, have to special case this.. vs print_reg().. */
218 if (cat1->src_im) {
219 if (type_float(cat1->src_type))
220 fprintf(ctx->out, "(%f)", cat1->fim_val);
221 else if (type_uint(cat1->src_type))
222 fprintf(ctx->out, "0x%08x", cat1->uim_val);
223 else
224 fprintf(ctx->out, "%d", cat1->iim_val);
225 } else if (cat1->src_rel && !cat1->src_c) {
226 /* I would just use %+d but trying to make it diff'able with
227 * libllvm-a3xx...
228 */
229 char type = cat1->src_rel_c ? 'c' : 'r';
230 if (cat1->off < 0)
231 fprintf(ctx->out, "%c<a0.x - %d>", type, -cat1->off);
232 else if (cat1->off > 0)
233 fprintf(ctx->out, "%c<a0.x + %d>", type, cat1->off);
234 else
235 fprintf(ctx->out, "%c<a0.x>", type);
236 } else {
237 print_reg_src(ctx, (reg_t)(cat1->src), type_size(cat1->src_type) == 32,
238 cat1->src_r, cat1->src_c, cat1->src_im, false, false, false);
239 }
240
241 if ((debug & PRINT_VERBOSE) && (cat1->must_be_0))
242 fprintf(ctx->out, "\t{1: %x}", cat1->must_be_0);
243 }
244
245 static void print_instr_cat2(struct disasm_ctx *ctx, instr_t *instr)
246 {
247 instr_cat2_t *cat2 = &instr->cat2;
248 static const char *cond[] = {
249 "lt",
250 "le",
251 "gt",
252 "ge",
253 "eq",
254 "ne",
255 "?6?",
256 };
257
258 switch (_OPC(2, cat2->opc)) {
259 case OPC_CMPS_F:
260 case OPC_CMPS_U:
261 case OPC_CMPS_S:
262 case OPC_CMPV_F:
263 case OPC_CMPV_U:
264 case OPC_CMPV_S:
265 fprintf(ctx->out, ".%s", cond[cat2->cond]);
266 break;
267 }
268
269 fprintf(ctx->out, " ");
270 if (cat2->ei)
271 fprintf(ctx->out, "(ei)");
272 print_reg_dst(ctx, (reg_t)(cat2->dst), cat2->full ^ cat2->dst_half, false);
273 fprintf(ctx->out, ", ");
274
275 unsigned src1_r = cat2->repeat ? cat2->src1_r : 0;
276 if (cat2->c1.src1_c) {
277 print_reg_src(ctx, (reg_t)(cat2->c1.src1), cat2->full, src1_r,
278 cat2->c1.src1_c, cat2->src1_im, cat2->src1_neg,
279 cat2->src1_abs, false);
280 } else if (cat2->rel1.src1_rel) {
281 print_reg_src(ctx, (reg_t)(cat2->rel1.src1), cat2->full, src1_r,
282 cat2->rel1.src1_c, cat2->src1_im, cat2->src1_neg,
283 cat2->src1_abs, cat2->rel1.src1_rel);
284 } else {
285 print_reg_src(ctx, (reg_t)(cat2->src1), cat2->full, src1_r,
286 false, cat2->src1_im, cat2->src1_neg,
287 cat2->src1_abs, false);
288 }
289
290 unsigned src2_r = cat2->repeat ? cat2->src2_r : 0;
291 switch (_OPC(2, cat2->opc)) {
292 case OPC_ABSNEG_F:
293 case OPC_ABSNEG_S:
294 case OPC_CLZ_B:
295 case OPC_CLZ_S:
296 case OPC_SIGN_F:
297 case OPC_FLOOR_F:
298 case OPC_CEIL_F:
299 case OPC_RNDNE_F:
300 case OPC_RNDAZ_F:
301 case OPC_TRUNC_F:
302 case OPC_NOT_B:
303 case OPC_BFREV_B:
304 case OPC_SETRM:
305 case OPC_CBITS_B:
306 /* these only have one src reg */
307 break;
308 default:
309 fprintf(ctx->out, ", ");
310 if (cat2->c2.src2_c) {
311 print_reg_src(ctx, (reg_t)(cat2->c2.src2), cat2->full, src2_r,
312 cat2->c2.src2_c, cat2->src2_im, cat2->src2_neg,
313 cat2->src2_abs, false);
314 } else if (cat2->rel2.src2_rel) {
315 print_reg_src(ctx, (reg_t)(cat2->rel2.src2), cat2->full, src2_r,
316 cat2->rel2.src2_c, cat2->src2_im, cat2->src2_neg,
317 cat2->src2_abs, cat2->rel2.src2_rel);
318 } else {
319 print_reg_src(ctx, (reg_t)(cat2->src2), cat2->full, src2_r,
320 false, cat2->src2_im, cat2->src2_neg,
321 cat2->src2_abs, false);
322 }
323 break;
324 }
325 }
326
327 static void print_instr_cat3(struct disasm_ctx *ctx, instr_t *instr)
328 {
329 instr_cat3_t *cat3 = &instr->cat3;
330 bool full = instr_cat3_full(cat3);
331
332 fprintf(ctx->out, " ");
333 print_reg_dst(ctx, (reg_t)(cat3->dst), full ^ cat3->dst_half, false);
334 fprintf(ctx->out, ", ");
335 unsigned src1_r = cat3->repeat ? cat3->src1_r : 0;
336 if (cat3->c1.src1_c) {
337 print_reg_src(ctx, (reg_t)(cat3->c1.src1), full,
338 src1_r, cat3->c1.src1_c, false, cat3->src1_neg,
339 false, false);
340 } else if (cat3->rel1.src1_rel) {
341 print_reg_src(ctx, (reg_t)(cat3->rel1.src1), full,
342 src1_r, cat3->rel1.src1_c, false, cat3->src1_neg,
343 false, cat3->rel1.src1_rel);
344 } else {
345 print_reg_src(ctx, (reg_t)(cat3->src1), full,
346 src1_r, false, false, cat3->src1_neg,
347 false, false);
348 }
349 fprintf(ctx->out, ", ");
350 unsigned src2_r = cat3->repeat ? cat3->src2_r : 0;
351 print_reg_src(ctx, (reg_t)cat3->src2, full,
352 src2_r, cat3->src2_c, false, cat3->src2_neg,
353 false, false);
354 fprintf(ctx->out, ", ");
355 if (cat3->c2.src3_c) {
356 print_reg_src(ctx, (reg_t)(cat3->c2.src3), full,
357 cat3->src3_r, cat3->c2.src3_c, false, cat3->src3_neg,
358 false, false);
359 } else if (cat3->rel2.src3_rel) {
360 print_reg_src(ctx, (reg_t)(cat3->rel2.src3), full,
361 cat3->src3_r, cat3->rel2.src3_c, false, cat3->src3_neg,
362 false, cat3->rel2.src3_rel);
363 } else {
364 print_reg_src(ctx, (reg_t)(cat3->src3), full,
365 cat3->src3_r, false, false, cat3->src3_neg,
366 false, false);
367 }
368 }
369
370 static void print_instr_cat4(struct disasm_ctx *ctx, instr_t *instr)
371 {
372 instr_cat4_t *cat4 = &instr->cat4;
373
374 fprintf(ctx->out, " ");
375 print_reg_dst(ctx, (reg_t)(cat4->dst), cat4->full ^ cat4->dst_half, false);
376 fprintf(ctx->out, ", ");
377
378 if (cat4->c.src_c) {
379 print_reg_src(ctx, (reg_t)(cat4->c.src), cat4->full,
380 cat4->src_r, cat4->c.src_c, cat4->src_im,
381 cat4->src_neg, cat4->src_abs, false);
382 } else if (cat4->rel.src_rel) {
383 print_reg_src(ctx, (reg_t)(cat4->rel.src), cat4->full,
384 cat4->src_r, cat4->rel.src_c, cat4->src_im,
385 cat4->src_neg, cat4->src_abs, cat4->rel.src_rel);
386 } else {
387 print_reg_src(ctx, (reg_t)(cat4->src), cat4->full,
388 cat4->src_r, false, cat4->src_im,
389 cat4->src_neg, cat4->src_abs, false);
390 }
391
392 if ((debug & PRINT_VERBOSE) && (cat4->dummy1|cat4->dummy2))
393 fprintf(ctx->out, "\t{4: %x,%x}", cat4->dummy1, cat4->dummy2);
394 }
395
396 static void print_instr_cat5(struct disasm_ctx *ctx, instr_t *instr)
397 {
398 static const struct {
399 bool src1, src2, samp, tex;
400 } info[0x1f] = {
401 [opc_op(OPC_ISAM)] = { true, false, true, true, },
402 [opc_op(OPC_ISAML)] = { true, true, true, true, },
403 [opc_op(OPC_ISAMM)] = { true, false, true, true, },
404 [opc_op(OPC_SAM)] = { true, false, true, true, },
405 [opc_op(OPC_SAMB)] = { true, true, true, true, },
406 [opc_op(OPC_SAML)] = { true, true, true, true, },
407 [opc_op(OPC_SAMGQ)] = { true, false, true, true, },
408 [opc_op(OPC_GETLOD)] = { true, false, true, true, },
409 [opc_op(OPC_CONV)] = { true, true, true, true, },
410 [opc_op(OPC_CONVM)] = { true, true, true, true, },
411 [opc_op(OPC_GETSIZE)] = { true, false, false, true, },
412 [opc_op(OPC_GETBUF)] = { false, false, false, true, },
413 [opc_op(OPC_GETPOS)] = { true, false, false, true, },
414 [opc_op(OPC_GETINFO)] = { false, false, false, true, },
415 [opc_op(OPC_DSX)] = { true, false, false, false, },
416 [opc_op(OPC_DSY)] = { true, false, false, false, },
417 [opc_op(OPC_GATHER4R)] = { true, false, true, true, },
418 [opc_op(OPC_GATHER4G)] = { true, false, true, true, },
419 [opc_op(OPC_GATHER4B)] = { true, false, true, true, },
420 [opc_op(OPC_GATHER4A)] = { true, false, true, true, },
421 [opc_op(OPC_SAMGP0)] = { true, false, true, true, },
422 [opc_op(OPC_SAMGP1)] = { true, false, true, true, },
423 [opc_op(OPC_SAMGP2)] = { true, false, true, true, },
424 [opc_op(OPC_SAMGP3)] = { true, false, true, true, },
425 [opc_op(OPC_DSXPP_1)] = { true, false, false, false, },
426 [opc_op(OPC_DSYPP_1)] = { true, false, false, false, },
427 [opc_op(OPC_RGETPOS)] = { false, false, false, false, },
428 [opc_op(OPC_RGETINFO)] = { false, false, false, false, },
429 };
430 instr_cat5_t *cat5 = &instr->cat5;
431 int i;
432
433 if (cat5->is_3d) fprintf(ctx->out, ".3d");
434 if (cat5->is_a) fprintf(ctx->out, ".a");
435 if (cat5->is_o) fprintf(ctx->out, ".o");
436 if (cat5->is_p) fprintf(ctx->out, ".p");
437 if (cat5->is_s) fprintf(ctx->out, ".s");
438 if (cat5->is_s2en) fprintf(ctx->out, ".s2en");
439
440 fprintf(ctx->out, " ");
441
442 switch (_OPC(5, cat5->opc)) {
443 case OPC_DSXPP_1:
444 case OPC_DSYPP_1:
445 break;
446 default:
447 fprintf(ctx->out, "(%s)", type[cat5->type]);
448 break;
449 }
450
451 fprintf(ctx->out, "(");
452 for (i = 0; i < 4; i++)
453 if (cat5->wrmask & (1 << i))
454 fprintf(ctx->out, "%c", "xyzw"[i]);
455 fprintf(ctx->out, ")");
456
457 print_reg_dst(ctx, (reg_t)(cat5->dst), type_size(cat5->type) == 32, false);
458
459 if (info[cat5->opc].src1) {
460 fprintf(ctx->out, ", ");
461 print_reg_src(ctx, (reg_t)(cat5->src1), cat5->full, false, false, false,
462 false, false, false);
463 }
464
465 if (cat5->is_s2en) {
466 fprintf(ctx->out, ", ");
467 print_reg_src(ctx, (reg_t)(cat5->s2en.src2), cat5->full, false, false, false,
468 false, false, false);
469 fprintf(ctx->out, ", ");
470 print_reg_src(ctx, (reg_t)(cat5->s2en.src3), false, false, false, false,
471 false, false, false);
472 } else {
473 if (cat5->is_o || info[cat5->opc].src2) {
474 fprintf(ctx->out, ", ");
475 print_reg_src(ctx, (reg_t)(cat5->norm.src2), cat5->full,
476 false, false, false, false, false, false);
477 }
478 if (info[cat5->opc].samp)
479 fprintf(ctx->out, ", s#%d", cat5->norm.samp);
480 if (info[cat5->opc].tex)
481 fprintf(ctx->out, ", t#%d", cat5->norm.tex);
482 }
483
484 if (debug & PRINT_VERBOSE) {
485 if (cat5->is_s2en) {
486 if ((debug & PRINT_VERBOSE) && (cat5->s2en.dummy1|cat5->s2en.dummy2|cat5->dummy2))
487 fprintf(ctx->out, "\t{5: %x,%x,%x}", cat5->s2en.dummy1, cat5->s2en.dummy2, cat5->dummy2);
488 } else {
489 if ((debug & PRINT_VERBOSE) && (cat5->norm.dummy1|cat5->dummy2))
490 fprintf(ctx->out, "\t{5: %x,%x}", cat5->norm.dummy1, cat5->dummy2);
491 }
492 }
493 }
494
495 static void print_instr_cat6_a3xx(struct disasm_ctx *ctx, instr_t *instr)
496 {
497 instr_cat6_t *cat6 = &instr->cat6;
498 char sd = 0, ss = 0; /* dst/src address space */
499 bool nodst = false;
500 struct reginfo dst, src1, src2;
501 int src1off = 0, dstoff = 0;
502
503 memset(&dst, 0, sizeof(dst));
504 memset(&src1, 0, sizeof(src1));
505 memset(&src2, 0, sizeof(src2));
506
507 switch (_OPC(6, cat6->opc)) {
508 case OPC_RESINFO:
509 case OPC_RESFMT:
510 dst.full = type_size(cat6->type) == 32;
511 src1.full = type_size(cat6->type) == 32;
512 src2.full = type_size(cat6->type) == 32;
513 break;
514 case OPC_L2G:
515 case OPC_G2L:
516 dst.full = true;
517 src1.full = true;
518 src2.full = true;
519 break;
520 case OPC_STG:
521 case OPC_STL:
522 case OPC_STP:
523 case OPC_STLW:
524 case OPC_STIB:
525 dst.full = true;
526 src1.full = type_size(cat6->type) == 32;
527 src2.full = type_size(cat6->type) == 32;
528 break;
529 default:
530 dst.full = type_size(cat6->type) == 32;
531 src1.full = true;
532 src2.full = true;
533 break;
534 }
535
536 switch (_OPC(6, cat6->opc)) {
537 case OPC_PREFETCH:
538 break;
539 case OPC_RESINFO:
540 fprintf(ctx->out, ".%dd", cat6->ldgb.d + 1);
541 break;
542 case OPC_LDGB:
543 fprintf(ctx->out, ".%s", cat6->ldgb.typed ? "typed" : "untyped");
544 fprintf(ctx->out, ".%dd", cat6->ldgb.d + 1);
545 fprintf(ctx->out, ".%s", type[cat6->type]);
546 fprintf(ctx->out, ".%d", cat6->ldgb.type_size + 1);
547 break;
548 case OPC_STGB:
549 case OPC_STIB:
550 fprintf(ctx->out, ".%s", cat6->stgb.typed ? "typed" : "untyped");
551 fprintf(ctx->out, ".%dd", cat6->stgb.d + 1);
552 fprintf(ctx->out, ".%s", type[cat6->type]);
553 fprintf(ctx->out, ".%d", cat6->stgb.type_size + 1);
554 break;
555 case OPC_ATOMIC_ADD:
556 case OPC_ATOMIC_SUB:
557 case OPC_ATOMIC_XCHG:
558 case OPC_ATOMIC_INC:
559 case OPC_ATOMIC_DEC:
560 case OPC_ATOMIC_CMPXCHG:
561 case OPC_ATOMIC_MIN:
562 case OPC_ATOMIC_MAX:
563 case OPC_ATOMIC_AND:
564 case OPC_ATOMIC_OR:
565 case OPC_ATOMIC_XOR:
566 ss = cat6->g ? 'g' : 'l';
567 fprintf(ctx->out, ".%s", cat6->ldgb.typed ? "typed" : "untyped");
568 fprintf(ctx->out, ".%dd", cat6->ldgb.d + 1);
569 fprintf(ctx->out, ".%s", type[cat6->type]);
570 fprintf(ctx->out, ".%d", cat6->ldgb.type_size + 1);
571 fprintf(ctx->out, ".%c", ss);
572 break;
573 default:
574 dst.im = cat6->g && !cat6->dst_off;
575 fprintf(ctx->out, ".%s", type[cat6->type]);
576 break;
577 }
578 fprintf(ctx->out, " ");
579
580 switch (_OPC(6, cat6->opc)) {
581 case OPC_STG:
582 sd = 'g';
583 break;
584 case OPC_STP:
585 sd = 'p';
586 break;
587 case OPC_STL:
588 case OPC_STLW:
589 sd = 'l';
590 break;
591
592 case OPC_LDG:
593 case OPC_LDC:
594 ss = 'g';
595 break;
596 case OPC_LDP:
597 ss = 'p';
598 break;
599 case OPC_LDL:
600 case OPC_LDLW:
601 case OPC_LDLV:
602 ss = 'l';
603 break;
604
605 case OPC_L2G:
606 ss = 'l';
607 sd = 'g';
608 break;
609
610 case OPC_G2L:
611 ss = 'g';
612 sd = 'l';
613 break;
614
615 case OPC_PREFETCH:
616 ss = 'g';
617 nodst = true;
618 break;
619 }
620
621 if ((_OPC(6, cat6->opc) == OPC_STGB) || (_OPC(6, cat6->opc) == OPC_STIB)) {
622 struct reginfo src3;
623
624 memset(&src3, 0, sizeof(src3));
625
626 src1.reg = (reg_t)(cat6->stgb.src1);
627 src2.reg = (reg_t)(cat6->stgb.src2);
628 src2.im = cat6->stgb.src2_im;
629 src3.reg = (reg_t)(cat6->stgb.src3);
630 src3.im = cat6->stgb.src3_im;
631 src3.full = true;
632
633 fprintf(ctx->out, "g[%u], ", cat6->stgb.dst_ssbo);
634 print_src(ctx, &src1);
635 fprintf(ctx->out, ", ");
636 print_src(ctx, &src2);
637 fprintf(ctx->out, ", ");
638 print_src(ctx, &src3);
639
640 if (debug & PRINT_VERBOSE)
641 fprintf(ctx->out, " (pad0=%x, pad3=%x)", cat6->stgb.pad0, cat6->stgb.pad3);
642
643 return;
644 }
645
646 if (is_atomic(_OPC(6, cat6->opc))) {
647
648 src1.reg = (reg_t)(cat6->ldgb.src1);
649 src1.im = cat6->ldgb.src1_im;
650 src2.reg = (reg_t)(cat6->ldgb.src2);
651 src2.im = cat6->ldgb.src2_im;
652 dst.reg = (reg_t)(cat6->ldgb.dst);
653
654 print_src(ctx, &dst);
655 fprintf(ctx->out, ", ");
656 if (ss == 'g') {
657 struct reginfo src3;
658 memset(&src3, 0, sizeof(src3));
659
660 src3.reg = (reg_t)(cat6->ldgb.src3);
661 src3.full = true;
662
663 /* For images, the ".typed" variant is used and src2 is
664 * the ivecN coordinates, ie ivec2 for 2d.
665 *
666 * For SSBOs, the ".untyped" variant is used and src2 is
667 * a simple dword offset.. src3 appears to be
668 * uvec2(offset * 4, 0). Not sure the point of that.
669 */
670
671 fprintf(ctx->out, "g[%u], ", cat6->ldgb.src_ssbo);
672 print_src(ctx, &src1); /* value */
673 fprintf(ctx->out, ", ");
674 print_src(ctx, &src2); /* offset/coords */
675 fprintf(ctx->out, ", ");
676 print_src(ctx, &src3); /* 64b byte offset.. */
677
678 if (debug & PRINT_VERBOSE) {
679 fprintf(ctx->out, " (pad0=%x, pad3=%x, mustbe0=%x)", cat6->ldgb.pad0,
680 cat6->ldgb.pad3, cat6->ldgb.mustbe0);
681 }
682 } else { /* ss == 'l' */
683 fprintf(ctx->out, "l[");
684 print_src(ctx, &src1); /* simple byte offset */
685 fprintf(ctx->out, "], ");
686 print_src(ctx, &src2); /* value */
687
688 if (debug & PRINT_VERBOSE) {
689 fprintf(ctx->out, " (src3=%x, pad0=%x, pad3=%x, mustbe0=%x)",
690 cat6->ldgb.src3, cat6->ldgb.pad0,
691 cat6->ldgb.pad3, cat6->ldgb.mustbe0);
692 }
693 }
694
695 return;
696 } else if (_OPC(6, cat6->opc) == OPC_RESINFO) {
697 dst.reg = (reg_t)(cat6->ldgb.dst);
698
699 print_src(ctx, &dst);
700 fprintf(ctx->out, ", ");
701 fprintf(ctx->out, "g[%u]", cat6->ldgb.src_ssbo);
702
703 return;
704 } else if (_OPC(6, cat6->opc) == OPC_LDGB) {
705
706 src1.reg = (reg_t)(cat6->ldgb.src1);
707 src1.im = cat6->ldgb.src1_im;
708 src2.reg = (reg_t)(cat6->ldgb.src2);
709 src2.im = cat6->ldgb.src2_im;
710 dst.reg = (reg_t)(cat6->ldgb.dst);
711
712 print_src(ctx, &dst);
713 fprintf(ctx->out, ", ");
714 fprintf(ctx->out, "g[%u], ", cat6->ldgb.src_ssbo);
715 print_src(ctx, &src1);
716 fprintf(ctx->out, ", ");
717 print_src(ctx, &src2);
718
719 if (debug & PRINT_VERBOSE)
720 fprintf(ctx->out, " (pad0=%x, pad3=%x, mustbe0=%x)", cat6->ldgb.pad0, cat6->ldgb.pad3, cat6->ldgb.mustbe0);
721
722 return;
723 }
724 if (cat6->dst_off) {
725 dst.reg = (reg_t)(cat6->c.dst);
726 dstoff = cat6->c.off;
727 } else {
728 dst.reg = (reg_t)(cat6->d.dst);
729 }
730
731 if (cat6->src_off) {
732 src1.reg = (reg_t)(cat6->a.src1);
733 src1.im = cat6->a.src1_im;
734 src2.reg = (reg_t)(cat6->a.src2);
735 src2.im = cat6->a.src2_im;
736 src1off = cat6->a.off;
737 } else {
738 src1.reg = (reg_t)(cat6->b.src1);
739 src1.im = cat6->b.src1_im;
740 src2.reg = (reg_t)(cat6->b.src2);
741 src2.im = cat6->b.src2_im;
742 }
743
744 if (!nodst) {
745 if (sd)
746 fprintf(ctx->out, "%c[", sd);
747 /* note: dst might actually be a src (ie. address to store to) */
748 print_src(ctx, &dst);
749 if (dstoff)
750 fprintf(ctx->out, "%+d", dstoff);
751 if (sd)
752 fprintf(ctx->out, "]");
753 fprintf(ctx->out, ", ");
754 }
755
756 if (ss)
757 fprintf(ctx->out, "%c[", ss);
758
759 /* can have a larger than normal immed, so hack: */
760 if (src1.im) {
761 fprintf(ctx->out, "%u", src1.reg.dummy13);
762 } else {
763 print_src(ctx, &src1);
764 }
765
766 if (src1off)
767 fprintf(ctx->out, "%+d", src1off);
768 if (ss)
769 fprintf(ctx->out, "]");
770
771 switch (_OPC(6, cat6->opc)) {
772 case OPC_RESINFO:
773 case OPC_RESFMT:
774 break;
775 default:
776 fprintf(ctx->out, ", ");
777 print_src(ctx, &src2);
778 break;
779 }
780 }
781
782 static void print_instr_cat6_a6xx(struct disasm_ctx *ctx, instr_t *instr)
783 {
784 instr_cat6_a6xx_t *cat6 = &instr->cat6_a6xx;
785 struct reginfo src1, src2;
786 bool has_dest = _OPC(6, cat6->opc) == OPC_LDIB;
787 char ss = 0;
788
789 memset(&src1, 0, sizeof(src1));
790 memset(&src2, 0, sizeof(src2));
791
792 fprintf(ctx->out, ".%s", cat6->typed ? "typed" : "untyped");
793 fprintf(ctx->out, ".%dd", cat6->d + 1);
794 fprintf(ctx->out, ".%s", type[cat6->type]);
795 fprintf(ctx->out, ".%u ", cat6->type_size + 1);
796
797 if (has_dest) {
798 src2.reg = (reg_t)(cat6->src2);
799 src2.full = true; // XXX
800 print_src(ctx, &src2);
801
802 fprintf(ctx->out, ", ");
803 }
804
805 /* NOTE: blob seems to use old encoding for ldl/stl (local memory) */
806 ss = 'g';
807
808 fprintf(ctx->out, "%c[%u", ss, cat6->ssbo);
809 fprintf(ctx->out, "] + ");
810 src1.reg = (reg_t)(cat6->src1);
811 src1.full = true; // XXX
812 print_src(ctx, &src1);
813
814 if (!has_dest) {
815 fprintf(ctx->out, ", ");
816
817 src2.reg = (reg_t)(cat6->src2);
818 src2.full = true; // XXX
819 print_src(ctx, &src2);
820 }
821
822 if (debug & PRINT_VERBOSE) {
823 fprintf(ctx->out, " (pad1=%x, pad2=%x, pad3=%x, pad4=%x)", cat6->pad1,
824 cat6->pad2, cat6->pad3, cat6->pad4);
825 }
826 }
827
828 static void print_instr_cat6(struct disasm_ctx *ctx, instr_t *instr)
829 {
830 // TODO not sure if this is the best way to figure
831 // out if new vs old encoding, but it kinda seems
832 // to work:
833 if ((ctx->gpu_id >= 600) && (instr->cat6.opc == 0)) {
834 print_instr_cat6_a6xx(ctx, instr);
835 if (debug & PRINT_VERBOSE)
836 fprintf(ctx->out, " NEW");
837 } else {
838 print_instr_cat6_a3xx(ctx, instr);
839 if (debug & PRINT_VERBOSE)
840 fprintf(ctx->out, " LEGACY");
841 }
842 }
843 static void print_instr_cat7(struct disasm_ctx *ctx, instr_t *instr)
844 {
845 instr_cat7_t *cat7 = &instr->cat7;
846
847 if (cat7->g)
848 fprintf(ctx->out, ".g");
849 if (cat7->l)
850 fprintf(ctx->out, ".l");
851
852 if (_OPC(7, cat7->opc) == OPC_FENCE) {
853 if (cat7->r)
854 fprintf(ctx->out, ".r");
855 if (cat7->w)
856 fprintf(ctx->out, ".w");
857 }
858 }
859
860 /* size of largest OPC field of all the instruction categories: */
861 #define NOPC_BITS 6
862
863 static const struct opc_info {
864 uint16_t cat;
865 uint16_t opc;
866 const char *name;
867 void (*print)(struct disasm_ctx *ctx, instr_t *instr);
868 } opcs[1 << (3+NOPC_BITS)] = {
869 #define OPC(cat, opc, name) [(opc)] = { (cat), (opc), #name, print_instr_cat##cat }
870 /* category 0: */
871 OPC(0, OPC_NOP, nop),
872 OPC(0, OPC_BR, br),
873 OPC(0, OPC_JUMP, jump),
874 OPC(0, OPC_CALL, call),
875 OPC(0, OPC_RET, ret),
876 OPC(0, OPC_KILL, kill),
877 OPC(0, OPC_END, end),
878 OPC(0, OPC_EMIT, emit),
879 OPC(0, OPC_CUT, cut),
880 OPC(0, OPC_CHMASK, chmask),
881 OPC(0, OPC_CHSH, chsh),
882 OPC(0, OPC_FLOW_REV, flow_rev),
883
884 /* category 1: */
885 OPC(1, OPC_MOV, ),
886
887 /* category 2: */
888 OPC(2, OPC_ADD_F, add.f),
889 OPC(2, OPC_MIN_F, min.f),
890 OPC(2, OPC_MAX_F, max.f),
891 OPC(2, OPC_MUL_F, mul.f),
892 OPC(2, OPC_SIGN_F, sign.f),
893 OPC(2, OPC_CMPS_F, cmps.f),
894 OPC(2, OPC_ABSNEG_F, absneg.f),
895 OPC(2, OPC_CMPV_F, cmpv.f),
896 OPC(2, OPC_FLOOR_F, floor.f),
897 OPC(2, OPC_CEIL_F, ceil.f),
898 OPC(2, OPC_RNDNE_F, rndne.f),
899 OPC(2, OPC_RNDAZ_F, rndaz.f),
900 OPC(2, OPC_TRUNC_F, trunc.f),
901 OPC(2, OPC_ADD_U, add.u),
902 OPC(2, OPC_ADD_S, add.s),
903 OPC(2, OPC_SUB_U, sub.u),
904 OPC(2, OPC_SUB_S, sub.s),
905 OPC(2, OPC_CMPS_U, cmps.u),
906 OPC(2, OPC_CMPS_S, cmps.s),
907 OPC(2, OPC_MIN_U, min.u),
908 OPC(2, OPC_MIN_S, min.s),
909 OPC(2, OPC_MAX_U, max.u),
910 OPC(2, OPC_MAX_S, max.s),
911 OPC(2, OPC_ABSNEG_S, absneg.s),
912 OPC(2, OPC_AND_B, and.b),
913 OPC(2, OPC_OR_B, or.b),
914 OPC(2, OPC_NOT_B, not.b),
915 OPC(2, OPC_XOR_B, xor.b),
916 OPC(2, OPC_CMPV_U, cmpv.u),
917 OPC(2, OPC_CMPV_S, cmpv.s),
918 OPC(2, OPC_MUL_U, mul.u),
919 OPC(2, OPC_MUL_S, mul.s),
920 OPC(2, OPC_MULL_U, mull.u),
921 OPC(2, OPC_BFREV_B, bfrev.b),
922 OPC(2, OPC_CLZ_S, clz.s),
923 OPC(2, OPC_CLZ_B, clz.b),
924 OPC(2, OPC_SHL_B, shl.b),
925 OPC(2, OPC_SHR_B, shr.b),
926 OPC(2, OPC_ASHR_B, ashr.b),
927 OPC(2, OPC_BARY_F, bary.f),
928 OPC(2, OPC_MGEN_B, mgen.b),
929 OPC(2, OPC_GETBIT_B, getbit.b),
930 OPC(2, OPC_SETRM, setrm),
931 OPC(2, OPC_CBITS_B, cbits.b),
932 OPC(2, OPC_SHB, shb),
933 OPC(2, OPC_MSAD, msad),
934
935 /* category 3: */
936 OPC(3, OPC_MAD_U16, mad.u16),
937 OPC(3, OPC_MADSH_U16, madsh.u16),
938 OPC(3, OPC_MAD_S16, mad.s16),
939 OPC(3, OPC_MADSH_M16, madsh.m16),
940 OPC(3, OPC_MAD_U24, mad.u24),
941 OPC(3, OPC_MAD_S24, mad.s24),
942 OPC(3, OPC_MAD_F16, mad.f16),
943 OPC(3, OPC_MAD_F32, mad.f32),
944 OPC(3, OPC_SEL_B16, sel.b16),
945 OPC(3, OPC_SEL_B32, sel.b32),
946 OPC(3, OPC_SEL_S16, sel.s16),
947 OPC(3, OPC_SEL_S32, sel.s32),
948 OPC(3, OPC_SEL_F16, sel.f16),
949 OPC(3, OPC_SEL_F32, sel.f32),
950 OPC(3, OPC_SAD_S16, sad.s16),
951 OPC(3, OPC_SAD_S32, sad.s32),
952
953 /* category 4: */
954 OPC(4, OPC_RCP, rcp),
955 OPC(4, OPC_RSQ, rsq),
956 OPC(4, OPC_LOG2, log2),
957 OPC(4, OPC_EXP2, exp2),
958 OPC(4, OPC_SIN, sin),
959 OPC(4, OPC_COS, cos),
960 OPC(4, OPC_SQRT, sqrt),
961
962 /* category 5: */
963 OPC(5, OPC_ISAM, isam),
964 OPC(5, OPC_ISAML, isaml),
965 OPC(5, OPC_ISAMM, isamm),
966 OPC(5, OPC_SAM, sam),
967 OPC(5, OPC_SAMB, samb),
968 OPC(5, OPC_SAML, saml),
969 OPC(5, OPC_SAMGQ, samgq),
970 OPC(5, OPC_GETLOD, getlod),
971 OPC(5, OPC_CONV, conv),
972 OPC(5, OPC_CONVM, convm),
973 OPC(5, OPC_GETSIZE, getsize),
974 OPC(5, OPC_GETBUF, getbuf),
975 OPC(5, OPC_GETPOS, getpos),
976 OPC(5, OPC_GETINFO, getinfo),
977 OPC(5, OPC_DSX, dsx),
978 OPC(5, OPC_DSY, dsy),
979 OPC(5, OPC_GATHER4R, gather4r),
980 OPC(5, OPC_GATHER4G, gather4g),
981 OPC(5, OPC_GATHER4B, gather4b),
982 OPC(5, OPC_GATHER4A, gather4a),
983 OPC(5, OPC_SAMGP0, samgp0),
984 OPC(5, OPC_SAMGP1, samgp1),
985 OPC(5, OPC_SAMGP2, samgp2),
986 OPC(5, OPC_SAMGP3, samgp3),
987 OPC(5, OPC_DSXPP_1, dsxpp.1),
988 OPC(5, OPC_DSYPP_1, dsypp.1),
989 OPC(5, OPC_RGETPOS, rgetpos),
990 OPC(5, OPC_RGETINFO, rgetinfo),
991
992
993 /* category 6: */
994 OPC(6, OPC_LDG, ldg),
995 OPC(6, OPC_LDL, ldl),
996 OPC(6, OPC_LDP, ldp),
997 OPC(6, OPC_STG, stg),
998 OPC(6, OPC_STL, stl),
999 OPC(6, OPC_STP, stp),
1000 OPC(6, OPC_LDIB, ldib),
1001 OPC(6, OPC_G2L, g2l),
1002 OPC(6, OPC_L2G, l2g),
1003 OPC(6, OPC_PREFETCH, prefetch),
1004 OPC(6, OPC_LDLW, ldlw),
1005 OPC(6, OPC_STLW, stlw),
1006 OPC(6, OPC_RESFMT, resfmt),
1007 OPC(6, OPC_RESINFO, resinfo),
1008 OPC(6, OPC_ATOMIC_ADD, atomic.add),
1009 OPC(6, OPC_ATOMIC_SUB, atomic.sub),
1010 OPC(6, OPC_ATOMIC_XCHG, atomic.xchg),
1011 OPC(6, OPC_ATOMIC_INC, atomic.inc),
1012 OPC(6, OPC_ATOMIC_DEC, atomic.dec),
1013 OPC(6, OPC_ATOMIC_CMPXCHG, atomic.cmpxchg),
1014 OPC(6, OPC_ATOMIC_MIN, atomic.min),
1015 OPC(6, OPC_ATOMIC_MAX, atomic.max),
1016 OPC(6, OPC_ATOMIC_AND, atomic.and),
1017 OPC(6, OPC_ATOMIC_OR, atomic.or),
1018 OPC(6, OPC_ATOMIC_XOR, atomic.xor),
1019 OPC(6, OPC_LDGB, ldgb),
1020 OPC(6, OPC_STGB, stgb),
1021 OPC(6, OPC_STIB, stib),
1022 OPC(6, OPC_LDC, ldc),
1023 OPC(6, OPC_LDLV, ldlv),
1024
1025 OPC(7, OPC_BAR, bar),
1026 OPC(7, OPC_FENCE, fence),
1027
1028 #undef OPC
1029 };
1030
1031 #define GETINFO(instr) (&(opcs[((instr)->opc_cat << NOPC_BITS) | instr_opc(instr, ctx->gpu_id)]))
1032
1033 // XXX hack.. probably should move this table somewhere common:
1034 #include "ir3.h"
1035 const char *ir3_instr_name(struct ir3_instruction *instr)
1036 {
1037 if (opc_cat(instr->opc) == -1) return "??meta??";
1038 return opcs[instr->opc].name;
1039 }
1040
1041 static bool print_instr(struct disasm_ctx *ctx, uint32_t *dwords, int n)
1042 {
1043 instr_t *instr = (instr_t *)dwords;
1044 uint32_t opc = instr_opc(instr, ctx->gpu_id);
1045 const char *name;
1046
1047 if (debug & PRINT_VERBOSE)
1048 fprintf(ctx->out, "%s%04d[%08xx_%08xx] ", levels[ctx->level], n, dwords[1], dwords[0]);
1049
1050 /* NOTE: order flags are printed is a bit fugly.. but for now I
1051 * try to match the order in llvm-a3xx disassembler for easy
1052 * diff'ing..
1053 */
1054
1055 ctx->repeat = instr_repeat(instr);
1056
1057 if (instr->sync)
1058 fprintf(ctx->out, "(sy)");
1059 if (instr->ss && ((instr->opc_cat <= 4) || (instr->opc_cat == 7)))
1060 fprintf(ctx->out, "(ss)");
1061 if (instr->jmp_tgt)
1062 fprintf(ctx->out, "(jp)");
1063 if (instr_sat(instr))
1064 fprintf(ctx->out, "(sat)");
1065 if (ctx->repeat) {
1066 fprintf(ctx->out, "(rpt%d)", ctx->repeat);
1067 } else if ((instr->opc_cat == 2) && (instr->cat2.src1_r || instr->cat2.src2_r)) {
1068 unsigned nop = (instr->cat2.src2_r * 2) + instr->cat2.src1_r;
1069 fprintf(ctx->out, "(nop%d)", nop);
1070 } else if ((instr->opc_cat == 3) && (instr->cat3.src1_r || instr->cat3.src2_r)) {
1071 unsigned nop = (instr->cat3.src2_r * 2) + instr->cat3.src1_r;
1072 fprintf(ctx->out, "(nop%d)", nop);
1073 }
1074 if (instr->ul && ((2 <= instr->opc_cat) && (instr->opc_cat <= 4)))
1075 fprintf(ctx->out, "(ul)");
1076
1077 name = GETINFO(instr)->name;
1078
1079 if (name) {
1080 fprintf(ctx->out, "%s", name);
1081 GETINFO(instr)->print(ctx, instr);
1082 } else {
1083 fprintf(ctx->out, "unknown(%d,%d)", instr->opc_cat, opc);
1084 }
1085
1086 fprintf(ctx->out, "\n");
1087
1088 return (instr->opc_cat == 0) && (opc == OPC_END);
1089 }
1090
1091 int disasm_a3xx(uint32_t *dwords, int sizedwords, int level, FILE *out, unsigned gpu_id)
1092 {
1093 struct disasm_ctx ctx;
1094 int i;
1095
1096 assert((sizedwords % 2) == 0);
1097
1098 memset(&ctx, 0, sizeof(ctx));
1099 ctx.out = out;
1100 ctx.level = level;
1101 ctx.gpu_id = gpu_id;
1102
1103 for (i = 0; i < sizedwords; i += 2)
1104 print_instr(&ctx, &dwords[i], i/2);
1105
1106 return 0;
1107 }