Debian Codegraph @

src/panfrost/compiler/midgard/disassemble.c

raw
1 /*
2 * Copyright (c) 2013 Connor Abbott ([email protected])
3 * Copyright (c) 2018 Alyssa Rosenzweig
4 * Copyright (C) 2019-2020 Collabora, Ltd.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "disassemble.h"
9 #include <assert.h>
10 #include <ctype.h>
11 #include <inttypes.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include "util/bitscan.h"
17 #include "util/half_float.h"
18 #include "util/u_math.h"
19 #include "helpers.h"
20 #include "midgard.h"
21 #include "midgard_ops.h"
22 #include "midgard_quirks.h"
23
24 #define DEFINE_CASE(define, str) \
25 case define: { \
26 fprintf(fp, str); \
27 break; \
28 }
29
30 /* These are not mapped to hardware values, they just represent the possible
31 * implicit arg modifiers that some midgard opcodes have, which can be decoded
32 * from the opcodes via midgard_{alu,ldst,tex}_special_arg_mod() */
33 typedef enum {
34 midgard_arg_mod_none = 0,
35 midgard_arg_mod_inv,
36 midgard_arg_mod_x2,
37 } midgard_special_arg_mod;
38
39 typedef struct {
40 unsigned *midg_tags;
41
42 /* For static analysis to ensure all registers are written at least once
43 * before use along the source code path (TODO: does this break done for
44 * complex CF?)
45 */
46
47 uint16_t midg_ever_written;
48 } disassemble_context;
49
50 /* Transform an expanded writemask (duplicated 8-bit format) into its condensed
51 * form (one bit per component) */
52
53 static inline unsigned
54 condense_writemask(unsigned expanded_mask, unsigned bits_per_component)
55 {
56 if (bits_per_component == 8) {
57 /* Duplicate every bit to go from 8 to 16-channel wrmask */
58 unsigned omask = 0;
59
60 for (unsigned i = 0; i < 8; ++i) {
61 if (expanded_mask & (1 << i))
62 omask |= (3 << (2 * i));
63 }
64
65 return omask;
66 }
67
68 unsigned slots_per_component = bits_per_component / 16;
69 unsigned max_comp = (16 * 8) / bits_per_component;
70 unsigned condensed_mask = 0;
71
72 for (unsigned i = 0; i < max_comp; i++) {
73 if (expanded_mask & (1 << (i * slots_per_component)))
74 condensed_mask |= (1 << i);
75 }
76
77 return condensed_mask;
78 }
79
80 static bool
81 print_alu_opcode(FILE *fp, midgard_alu_op op)
82 {
83 if (alu_opcode_props[op].name)
84 fprintf(fp, "%s", alu_opcode_props[op].name);
85 else
86 fprintf(fp, "alu_op_%02X", op);
87
88 /* For constant analysis */
89 return midgard_is_integer_op(op);
90 }
91
92 static void
93 print_ld_st_opcode(FILE *fp, midgard_load_store_op op)
94 {
95 if (load_store_opcode_props[op].name)
96 fprintf(fp, "%s", load_store_opcode_props[op].name);
97 else
98 fprintf(fp, "ldst_op_%02X", op);
99 }
100
101 static void
102 validate_sampler_type(enum mali_texture_op op,
103 enum mali_sampler_type sampler_type)
104 {
105 if (op == midgard_tex_op_mov || op == midgard_tex_op_barrier)
106 assert(sampler_type == 0);
107 else
108 assert(sampler_type > 0);
109 }
110
111 static void
112 validate_expand_mode(midgard_src_expand_mode expand_mode,
113 midgard_reg_mode reg_mode)
114 {
115 switch (expand_mode) {
116 case midgard_src_passthrough:
117 break;
118
119 case midgard_src_rep_low:
120 assert(reg_mode == midgard_reg_mode_8 || reg_mode == midgard_reg_mode_16);
121 break;
122
123 case midgard_src_rep_high:
124 assert(reg_mode == midgard_reg_mode_8 || reg_mode == midgard_reg_mode_16);
125 break;
126
127 case midgard_src_swap:
128 assert(reg_mode == midgard_reg_mode_8 || reg_mode == midgard_reg_mode_16);
129 break;
130
131 case midgard_src_expand_low:
132 assert(reg_mode != midgard_reg_mode_8);
133 break;
134
135 case midgard_src_expand_high:
136 assert(reg_mode != midgard_reg_mode_8);
137 break;
138
139 case midgard_src_expand_low_swap:
140 assert(reg_mode == midgard_reg_mode_16);
141 break;
142
143 case midgard_src_expand_high_swap:
144 assert(reg_mode == midgard_reg_mode_16);
145 break;
146
147 default:
148 UNREACHABLE("Invalid expand mode");
149 break;
150 }
151 }
152
153 static void
154 print_alu_reg(disassemble_context *ctx, FILE *fp, unsigned reg, bool is_write)
155 {
156 unsigned uniform_reg = 23 - reg;
157 bool is_uniform = false;
158
159 /* For r8-r15, it could be a work or uniform. We distinguish based on
160 * the fact work registers are ALWAYS written before use, but uniform
161 * registers are NEVER written before use. */
162
163 if ((reg >= 8 && reg < 16) && !(ctx->midg_ever_written & (1 << reg)))
164 is_uniform = true;
165
166 /* r16-r23 are always uniform */
167
168 if (reg >= 16 && reg <= 23)
169 is_uniform = true;
170
171 if (reg == REGISTER_UNUSED || reg == REGISTER_UNUSED + 1)
172 fprintf(fp, "TMP%u", reg - REGISTER_UNUSED);
173 else if (reg == REGISTER_TEXTURE_BASE || reg == REGISTER_TEXTURE_BASE + 1)
174 fprintf(fp, "%s%u", is_write ? "AT" : "TA", reg - REGISTER_TEXTURE_BASE);
175 else if (reg == REGISTER_LDST_BASE || reg == REGISTER_LDST_BASE + 1)
176 fprintf(fp, "AL%u", reg - REGISTER_LDST_BASE);
177 else if (is_uniform)
178 fprintf(fp, "U%u", uniform_reg);
179 else if (reg == 31 && !is_write)
180 fprintf(fp, "PC_SP");
181 else
182 fprintf(fp, "R%u", reg);
183 }
184
185 static void
186 print_ldst_write_reg(FILE *fp, unsigned reg)
187 {
188 switch (reg) {
189 case 26:
190 case 27:
191 fprintf(fp, "AL%u", reg - REGISTER_LDST_BASE);
192 break;
193 case 28:
194 case 29:
195 fprintf(fp, "AT%u", reg - REGISTER_TEXTURE_BASE);
196 break;
197 case 31:
198 fprintf(fp, "PC_SP");
199 break;
200 default:
201 fprintf(fp, "R%d", reg);
202 break;
203 }
204 }
205
206 static void
207 print_ldst_read_reg(FILE *fp, unsigned reg)
208 {
209 switch (reg) {
210 case 0:
211 case 1:
212 fprintf(fp, "AL%u", reg);
213 break;
214 case 2:
215 fprintf(fp, "PC_SP");
216 break;
217 case 3:
218 fprintf(fp, "LOCAL_STORAGE_PTR");
219 break;
220 case 4:
221 fprintf(fp, "LOCAL_THREAD_ID");
222 break;
223 case 5:
224 fprintf(fp, "GROUP_ID");
225 break;
226 case 6:
227 fprintf(fp, "GLOBAL_THREAD_ID");
228 break;
229 case 7:
230 fprintf(fp, "0");
231 break;
232 default:
233 UNREACHABLE("Invalid load/store register read");
234 }
235 }
236
237 static void
238 print_tex_reg(FILE *fp, unsigned reg, bool is_write)
239 {
240 char *str = is_write ? "TA" : "AT";
241 int select = reg & 1;
242
243 switch (reg) {
244 case 0:
245 case 1:
246 fprintf(fp, "R%d", select);
247 break;
248 case 26:
249 case 27:
250 fprintf(fp, "AL%d", select);
251 break;
252 case 28:
253 case 29:
254 fprintf(fp, "%s%d", str, select);
255 break;
256 default:
257 UNREACHABLE("Invalid texture register");
258 }
259 }
260
261 static char *srcmod_names_int[4] = {
262 ".sext",
263 ".zext",
264 ".replicate",
265 ".lshift",
266 };
267
268 static char *argmod_names[3] = {
269 "",
270 ".inv",
271 ".x2",
272 };
273
274 static char *index_format_names[4] = {"", ".u64", ".u32", ".s32"};
275
276 static void
277 print_alu_outmod(FILE *fp, unsigned outmod, bool is_int, bool half)
278 {
279 if (is_int && !half) {
280 assert(outmod == midgard_outmod_keeplo);
281 return;
282 }
283
284 if (!is_int && half)
285 fprintf(fp, ".shrink");
286
287 mir_print_outmod(fp, outmod, is_int);
288 }
289
290 /* arg == 0 (dest), arg == 1 (src1), arg == 2 (src2) */
291 static midgard_special_arg_mod
292 midgard_alu_special_arg_mod(midgard_alu_op op, unsigned arg)
293 {
294 midgard_special_arg_mod mod = midgard_arg_mod_none;
295
296 switch (op) {
297 case midgard_alu_op_ishladd:
298 case midgard_alu_op_ishlsub:
299 if (arg == 1)
300 mod = midgard_arg_mod_x2;
301 break;
302
303 default:
304 break;
305 }
306
307 return mod;
308 }
309
310 static void
311 print_quad_word(FILE *fp, const uint32_t *words, unsigned tabs)
312 {
313 unsigned i;
314
315 for (i = 0; i < 4; i++)
316 fprintf(fp, "0x%08X%s ", words[i], i == 3 ? "" : ",");
317
318 fprintf(fp, "\n");
319 }
320
321 static const char components[16] = "xyzwefghijklmnop";
322
323 static int
324 bits_for_mode(midgard_reg_mode mode)
325 {
326 switch (mode) {
327 case midgard_reg_mode_8:
328 return 8;
329 case midgard_reg_mode_16:
330 return 16;
331 case midgard_reg_mode_32:
332 return 32;
333 case midgard_reg_mode_64:
334 return 64;
335 default:
336 UNREACHABLE("Invalid reg mode");
337 return 0;
338 }
339 }
340
341 static int
342 bits_for_mode_halved(midgard_reg_mode mode, bool half)
343 {
344 unsigned bits = bits_for_mode(mode);
345
346 if (half)
347 bits >>= 1;
348
349 return bits;
350 }
351
352 static void
353 print_vec_selectors_64(FILE *fp, unsigned swizzle, midgard_reg_mode reg_mode,
354 midgard_src_expand_mode expand_mode,
355 unsigned selector_offset, uint8_t mask)
356 {
357 bool expands = INPUT_EXPANDS(expand_mode);
358
359 unsigned comp_skip = expands ? 1 : 2;
360 unsigned mask_bit = 0;
361 for (unsigned i = selector_offset; i < 4; i += comp_skip, mask_bit += 4) {
362 if (!(mask & (1 << mask_bit)))
363 continue;
364
365 unsigned a = (swizzle >> (i * 2)) & 3;
366
367 if (INPUT_EXPANDS(expand_mode)) {
368 if (expand_mode == midgard_src_expand_high)
369 a += 2;
370
371 fprintf(fp, "%c", components[a / 2]);
372 continue;
373 }
374
375 unsigned b = (swizzle >> ((i + 1) * 2)) & 3;
376
377 /* Normally we're adjacent, but if there's an issue,
378 * don't make it ambiguous */
379
380 if (b == a + 1)
381 fprintf(fp, "%c", a >> 1 ? 'Y' : 'X');
382 else
383 fprintf(fp, "[%c%c]", components[a], components[b]);
384 }
385 }
386
387 static void
388 print_vec_selectors(FILE *fp, unsigned swizzle, midgard_reg_mode reg_mode,
389 unsigned selector_offset, uint8_t mask,
390 unsigned *mask_offset)
391 {
392 assert(reg_mode != midgard_reg_mode_64);
393
394 unsigned mask_skip = MAX2(bits_for_mode(reg_mode) / 16, 1);
395
396 bool is_vec16 = reg_mode == midgard_reg_mode_8;
397
398 for (unsigned i = 0; i < 4; i++, *mask_offset += mask_skip) {
399 if (!(mask & (1 << *mask_offset)))
400 continue;
401
402 unsigned c = (swizzle >> (i * 2)) & 3;
403
404 /* Vec16 has two components per swizzle selector. */
405 if (is_vec16)
406 c *= 2;
407
408 c += selector_offset;
409
410 fprintf(fp, "%c", components[c]);
411 if (is_vec16)
412 fprintf(fp, "%c", components[c + 1]);
413 }
414 }
415
416 static void
417 print_vec_swizzle(FILE *fp, unsigned swizzle, midgard_src_expand_mode expand,
418 midgard_reg_mode mode, uint8_t mask)
419 {
420 unsigned bits = bits_for_mode_halved(mode, INPUT_EXPANDS(expand));
421
422 /* Swizzle selectors are divided in two halves that are always
423 * mirrored, the only difference is the starting component offset.
424 * The number represents an offset into the components[] array. */
425 unsigned first_half = 0;
426 unsigned second_half = (128 / bits) / 2; /* only used for 8 and 16-bit */
427
428 switch (expand) {
429 case midgard_src_passthrough:
430 if (swizzle == 0xE4)
431 return; /* identity swizzle */
432 break;
433
434 case midgard_src_expand_low:
435 second_half /= 2;
436 break;
437
438 case midgard_src_expand_high:
439 first_half = second_half;
440 second_half += second_half / 2;
441 break;
442
443 /* The rest of the cases are only used for 8 and 16-bit */
444
445 case midgard_src_rep_low:
446 second_half = 0;
447 break;
448
449 case midgard_src_rep_high:
450 first_half = second_half;
451 break;
452
453 case midgard_src_swap:
454 first_half = second_half;
455 second_half = 0;
456 break;
457
458 case midgard_src_expand_low_swap:
459 first_half = second_half / 2;
460 second_half = 0;
461 break;
462
463 case midgard_src_expand_high_swap:
464 first_half = second_half + second_half / 2;
465 break;
466
467 default:
468 UNREACHABLE("Invalid expand mode");
469 break;
470 }
471
472 fprintf(fp, ".");
473
474 /* Vec2 are weird so we use a separate function to simplify things. */
475 if (mode == midgard_reg_mode_64) {
476 print_vec_selectors_64(fp, swizzle, mode, expand, first_half, mask);
477 return;
478 }
479
480 unsigned mask_offs = 0;
481 print_vec_selectors(fp, swizzle, mode, first_half, mask, &mask_offs);
482 if (mode == midgard_reg_mode_8 || mode == midgard_reg_mode_16)
483 print_vec_selectors(fp, swizzle, mode, second_half, mask, &mask_offs);
484 }
485
486 static void
487 print_scalar_constant(FILE *fp, unsigned src_binary,
488 const midgard_constants *consts, midgard_scalar_alu *alu)
489 {
490 midgard_scalar_alu_src *src = (midgard_scalar_alu_src *)&src_binary;
491 assert(consts != NULL);
492
493 fprintf(fp, "#");
494 mir_print_constant_component(
495 fp, consts, src->component,
496 src->full ? midgard_reg_mode_32 : midgard_reg_mode_16, false, src->mod,
497 alu->op);
498 }
499
500 static void
501 print_vector_constants(FILE *fp, unsigned src_binary,
502 const midgard_constants *consts, midgard_vector_alu *alu)
503 {
504 midgard_vector_alu_src *src = (midgard_vector_alu_src *)&src_binary;
505 bool expands = INPUT_EXPANDS(src->expand_mode);
506 unsigned bits = bits_for_mode_halved(alu->reg_mode, expands);
507 unsigned max_comp = (sizeof(*consts) * 8) / bits;
508 unsigned comp_mask, num_comp = 0;
509
510 assert(consts);
511 assert(max_comp <= 16);
512
513 comp_mask =
514 effective_writemask(alu->op, condense_writemask(alu->mask, bits));
515 num_comp = util_bitcount(comp_mask);
516
517 if (num_comp > 1)
518 fprintf(fp, "<");
519 else
520 fprintf(fp, "#");
521
522 bool first = true;
523
524 for (unsigned i = 0; i < max_comp; ++i) {
525 if (!(comp_mask & (1 << i)))
526 continue;
527
528 unsigned c = (src->swizzle >> (i * 2)) & 3;
529
530 if (bits == 16 && !expands) {
531 bool upper = i >= 4;
532
533 switch (src->expand_mode) {
534 case midgard_src_passthrough:
535 c += upper * 4;
536 break;
537 case midgard_src_rep_low:
538 break;
539 case midgard_src_rep_high:
540 c += 4;
541 break;
542 case midgard_src_swap:
543 c += !upper * 4;
544 break;
545 default:
546 UNREACHABLE("invalid expand mode");
547 break;
548 }
549 } else if (bits == 32 && !expands) {
550 /* Implicitly ok */
551 } else if (bits == 64 && !expands) {
552 /* Implicitly ok */
553 } else if (bits == 8 && !expands) {
554 bool upper = i >= 8;
555
556 unsigned index = (i >> 1) & 3;
557 unsigned base = (src->swizzle >> (index * 2)) & 3;
558 c = base * 2;
559
560 switch (src->expand_mode) {
561 case midgard_src_passthrough:
562 c += upper * 8;
563 break;
564 case midgard_src_rep_low:
565 break;
566 case midgard_src_rep_high:
567 c += 8;
568 break;
569 case midgard_src_swap:
570 c += !upper * 8;
571 break;
572 default:
573 UNREACHABLE("invalid expand mode");
574 break;
575 }
576
577 /* We work on twos, actually */
578 if (i & 1)
579 c++;
580 }
581
582 if (first)
583 first = false;
584 else
585 fprintf(fp, ", ");
586
587 mir_print_constant_component(fp, consts, c, alu->reg_mode, expands,
588 src->mod, alu->op);
589 }
590
591 if (num_comp > 1)
592 fprintf(fp, ">");
593 }
594
595 static void
596 print_srcmod(FILE *fp, bool is_int, bool expands, unsigned mod, bool scalar)
597 {
598 /* Modifiers change meaning depending on the op's context */
599
600 if (is_int) {
601 if (expands)
602 fprintf(fp, "%s", srcmod_names_int[mod]);
603 } else {
604 if (mod & MIDGARD_FLOAT_MOD_ABS)
605 fprintf(fp, ".abs");
606 if (mod & MIDGARD_FLOAT_MOD_NEG)
607 fprintf(fp, ".neg");
608 if (expands)
609 fprintf(fp, ".widen");
610 }
611 }
612
613 static void
614 print_vector_src(disassemble_context *ctx, FILE *fp, unsigned src_binary,
615 midgard_reg_mode mode, unsigned reg,
616 midgard_shrink_mode shrink_mode, uint8_t src_mask, bool is_int,
617 midgard_special_arg_mod arg_mod)
618 {
619 midgard_vector_alu_src *src = (midgard_vector_alu_src *)&src_binary;
620
621 validate_expand_mode(src->expand_mode, mode);
622
623 print_alu_reg(ctx, fp, reg, false);
624
625 print_vec_swizzle(fp, src->swizzle, src->expand_mode, mode, src_mask);
626
627 fprintf(fp, "%s", argmod_names[arg_mod]);
628
629 print_srcmod(fp, is_int, INPUT_EXPANDS(src->expand_mode), src->mod, false);
630 }
631
632 static uint16_t
633 decode_vector_imm(unsigned src2_reg, unsigned imm)
634 {
635 uint16_t ret;
636 ret = src2_reg << 11;
637 ret |= (imm & 0x7) << 8;
638 ret |= (imm >> 3) & 0xFF;
639 return ret;
640 }
641
642 static void
643 print_immediate(FILE *fp, uint16_t imm, bool is_instruction_int)
644 {
645 if (is_instruction_int)
646 fprintf(fp, "#%u", imm);
647 else
648 fprintf(fp, "#%g", _mesa_half_to_float(imm));
649 }
650
651 static void
652 update_dest(disassemble_context *ctx, unsigned reg)
653 {
654 /* We should record writes as marking this as a work register. Store
655 * the max register in work_count; we'll add one at the end */
656
657 if (reg < 16)
658 ctx->midg_ever_written |= (1 << reg);
659 }
660
661 static void
662 print_dest(disassemble_context *ctx, FILE *fp, unsigned reg)
663 {
664 update_dest(ctx, reg);
665 print_alu_reg(ctx, fp, reg, true);
666 }
667
668 /* For 16-bit+ masks, we read off from the 8-bit mask field. For 16-bit (vec8),
669 * it's just one bit per channel, easy peasy. For 32-bit (vec4), it's one bit
670 * per channel with one duplicate bit in the middle. For 64-bit (vec2), it's
671 * one-bit per channel with _3_ duplicate bits in the middle. Basically, just
672 * subdividing the 128-bit word in 16-bit increments. For 64-bit, we uppercase
673 * the mask to make it obvious what happened */
674
675 static void
676 print_alu_mask(FILE *fp, uint8_t mask, unsigned bits,
677 midgard_shrink_mode shrink_mode)
678 {
679 /* Skip 'complete' masks */
680
681 if (shrink_mode == midgard_shrink_mode_none && mask == 0xFF)
682 return;
683
684 fprintf(fp, ".");
685
686 unsigned skip = MAX2(bits / 16, 1);
687 bool tripped = false;
688
689 /* To apply an upper destination shrink_mode, we "shift" the alphabet.
690 * E.g. with an upper shrink_mode on 32-bit, instead of xyzw, print efgh.
691 * For upper 16-bit, instead of xyzwefgh, print ijklmnop */
692
693 const char *alphabet = components;
694
695 if (shrink_mode == midgard_shrink_mode_upper) {
696 assert(bits != 8);
697 alphabet += (128 / bits);
698 }
699
700 for (unsigned i = 0; i < 8; i += skip) {
701 bool a = (mask & (1 << i)) != 0;
702
703 for (unsigned j = 1; j < skip; ++j) {
704 bool dupe = (mask & (1 << (i + j))) != 0;
705 tripped |= (dupe != a);
706 }
707
708 if (a) {
709 /* TODO: handle shrinking from 16-bit */
710 unsigned comp_idx = bits == 8 ? i * 2 : i;
711 char c = alphabet[comp_idx / skip];
712
713 fprintf(fp, "%c", c);
714 if (bits == 8)
715 fprintf(fp, "%c", alphabet[comp_idx + 1]);
716 }
717 }
718
719 if (tripped)
720 fprintf(fp, " /* %X */", mask);
721 }
722
723 /* TODO: 16-bit mode */
724 static void
725 print_ldst_mask(FILE *fp, unsigned mask, unsigned swizzle)
726 {
727 fprintf(fp, ".");
728
729 for (unsigned i = 0; i < 4; ++i) {
730 bool write = (mask & (1 << i)) != 0;
731 unsigned c = (swizzle >> (i * 2)) & 3;
732 /* We can't omit the swizzle here since many ldst ops have a
733 * combined swizzle/writemask, and it would be ambiguous to not
734 * print the masked-out components. */
735 fprintf(fp, "%c", write ? components[c] : '~');
736 }
737 }
738
739 static void
740 print_tex_mask(FILE *fp, unsigned mask, bool upper)
741 {
742 if (mask == 0xF) {
743 if (upper)
744 fprintf(fp, "'");
745
746 return;
747 }
748
749 fprintf(fp, ".");
750
751 for (unsigned i = 0; i < 4; ++i) {
752 bool a = (mask & (1 << i)) != 0;
753 if (a)
754 fprintf(fp, "%c", components[i + (upper ? 4 : 0)]);
755 }
756 }
757
758 static void
759 print_vector_field(disassemble_context *ctx, FILE *fp, const char *name,
760 uint16_t *words, uint16_t reg_word,
761 const midgard_constants *consts, unsigned tabs, bool verbose)
762 {
763 midgard_reg_info *reg_info = (midgard_reg_info *)&reg_word;
764 midgard_vector_alu *alu_field = (midgard_vector_alu *)words;
765 midgard_reg_mode mode = alu_field->reg_mode;
766 midgard_alu_op op = alu_field->op;
767 unsigned shrink_mode = alu_field->shrink_mode;
768 bool is_int = midgard_is_integer_op(op);
769 bool is_int_out = midgard_is_integer_out_op(op);
770
771 if (verbose)
772 fprintf(fp, "%s.", name);
773
774 bool is_instruction_int = print_alu_opcode(fp, alu_field->op);
775
776 /* Print lane width */
777 fprintf(fp, ".%c%d", is_int_out ? 'i' : 'f', bits_for_mode(mode));
778
779 fprintf(fp, " ");
780
781 /* Mask denoting status of 8-lanes */
782 uint8_t mask = alu_field->mask;
783
784 /* First, print the destination */
785 print_dest(ctx, fp, reg_info->out_reg);
786
787 if (shrink_mode != midgard_shrink_mode_none) {
788 bool shrinkable = (mode != midgard_reg_mode_8);
789 bool known = shrink_mode != 0x3; /* Unused value */
790
791 if (!(shrinkable && known))
792 fprintf(fp, "/* do%u */ ", shrink_mode);
793 }
794
795 /* Instructions like fdot4 do *not* replicate, ensure the
796 * mask is of only a single component */
797
798 unsigned rep = GET_CHANNEL_COUNT(alu_opcode_props[op].props);
799
800 if (rep) {
801 unsigned comp_mask = condense_writemask(mask, bits_for_mode(mode));
802 unsigned num_comp = util_bitcount(comp_mask);
803 if (num_comp != 1)
804 fprintf(fp, "/* err too many components */");
805 }
806 print_alu_mask(fp, mask, bits_for_mode(mode), shrink_mode);
807
808 /* Print output modifiers */
809
810 print_alu_outmod(fp, alu_field->outmod, is_int_out,
811 shrink_mode != midgard_shrink_mode_none);
812
813 /* Mask out unused components based on the writemask, but don't mask out
814 * components that are used for interlane instructions like fdot3. */
815 uint8_t src_mask =
816 rep ? expand_writemask(mask_of(rep),
817 util_logbase2(128 / bits_for_mode(mode)))
818 : mask;
819
820 fprintf(fp, ", ");
821
822 if (reg_info->src1_reg == REGISTER_CONSTANT)
823 print_vector_constants(fp, alu_field->src1, consts, alu_field);
824 else {
825 midgard_special_arg_mod argmod = midgard_alu_special_arg_mod(op, 1);
826 print_vector_src(ctx, fp, alu_field->src1, mode, reg_info->src1_reg,
827 shrink_mode, src_mask, is_int, argmod);
828 }
829
830 fprintf(fp, ", ");
831
832 if (reg_info->src2_imm) {
833 uint16_t imm =
834 decode_vector_imm(reg_info->src2_reg, alu_field->src2 >> 2);
835 print_immediate(fp, imm, is_instruction_int);
836 } else if (reg_info->src2_reg == REGISTER_CONSTANT) {
837 print_vector_constants(fp, alu_field->src2, consts, alu_field);
838 } else {
839 midgard_special_arg_mod argmod = midgard_alu_special_arg_mod(op, 2);
840 print_vector_src(ctx, fp, alu_field->src2, mode, reg_info->src2_reg,
841 shrink_mode, src_mask, is_int, argmod);
842 }
843
844 fprintf(fp, "\n");
845 }
846
847 static void
848 print_scalar_src(disassemble_context *ctx, FILE *fp, bool is_int,
849 unsigned src_binary, unsigned reg)
850 {
851 midgard_scalar_alu_src *src = (midgard_scalar_alu_src *)&src_binary;
852
853 print_alu_reg(ctx, fp, reg, false);
854
855 unsigned c = src->component;
856
857 if (src->full) {
858 assert((c & 1) == 0);
859 c >>= 1;
860 }
861
862 fprintf(fp, ".%c", components[c]);
863
864 print_srcmod(fp, is_int, !src->full, src->mod, true);
865 }
866
867 static uint16_t
868 decode_scalar_imm(unsigned src2_reg, unsigned imm)
869 {
870 uint16_t ret;
871 ret = src2_reg << 11;
872 ret |= (imm & 3) << 9;
873 ret |= (imm & 4) << 6;
874 ret |= (imm & 0x38) << 2;
875 ret |= imm >> 6;
876 return ret;
877 }
878
879 static void
880 print_scalar_field(disassemble_context *ctx, FILE *fp, const char *name,
881 uint16_t *words, uint16_t reg_word,
882 const midgard_constants *consts, unsigned tabs, bool verbose)
883 {
884 midgard_reg_info *reg_info = (midgard_reg_info *)&reg_word;
885 midgard_scalar_alu *alu_field = (midgard_scalar_alu *)words;
886 bool is_int = midgard_is_integer_op(alu_field->op);
887 bool is_int_out = midgard_is_integer_out_op(alu_field->op);
888 bool full = alu_field->output_full;
889
890 if (alu_field->reserved)
891 fprintf(fp, "scalar ALU reserved bit set\n");
892
893 if (verbose)
894 fprintf(fp, "%s.", name);
895
896 bool is_instruction_int = print_alu_opcode(fp, alu_field->op);
897
898 /* Print lane width, in this case the lane width is always 32-bit, but
899 * we print it anyway to make it consistent with the other instructions. */
900 fprintf(fp, ".%c32", is_int_out ? 'i' : 'f');
901
902 fprintf(fp, " ");
903
904 print_dest(ctx, fp, reg_info->out_reg);
905 unsigned c = alu_field->output_component;
906
907 if (full) {
908 assert((c & 1) == 0);
909 c >>= 1;
910 }
911
912 fprintf(fp, ".%c", components[c]);
913
914 print_alu_outmod(fp, alu_field->outmod, is_int_out, !full);
915
916 fprintf(fp, ", ");
917
918 if (reg_info->src1_reg == REGISTER_CONSTANT)
919 print_scalar_constant(fp, alu_field->src1, consts, alu_field);
920 else
921 print_scalar_src(ctx, fp, is_int, alu_field->src1, reg_info->src1_reg);
922
923 fprintf(fp, ", ");
924
925 if (reg_info->src2_imm) {
926 uint16_t imm = decode_scalar_imm(reg_info->src2_reg, alu_field->src2);
927 print_immediate(fp, imm, is_instruction_int);
928 } else if (reg_info->src2_reg == REGISTER_CONSTANT) {
929 print_scalar_constant(fp, alu_field->src2, consts, alu_field);
930 } else
931 print_scalar_src(ctx, fp, is_int, alu_field->src2, reg_info->src2_reg);
932
933 fprintf(fp, "\n");
934 }
935
936 static void
937 print_branch_op(FILE *fp, unsigned op)
938 {
939 switch (op) {
940 case midgard_jmp_writeout_op_branch_uncond:
941 fprintf(fp, "uncond.");
942 break;
943
944 case midgard_jmp_writeout_op_branch_cond:
945 fprintf(fp, "cond.");
946 break;
947
948 case midgard_jmp_writeout_op_writeout:
949 fprintf(fp, "write.");
950 break;
951
952 case midgard_jmp_writeout_op_tilebuffer_pending:
953 fprintf(fp, "tilebuffer.");
954 break;
955
956 case midgard_jmp_writeout_op_discard:
957 fprintf(fp, "discard.");
958 break;
959
960 default:
961 fprintf(fp, "unk%u.", op);
962 break;
963 }
964 }
965
966 static void
967 print_branch_cond(FILE *fp, int cond)
968 {
969 switch (cond) {
970 case midgard_condition_write0:
971 fprintf(fp, "write0");
972 break;
973
974 case midgard_condition_false:
975 fprintf(fp, "false");
976 break;
977
978 case midgard_condition_true:
979 fprintf(fp, "true");
980 break;
981
982 case midgard_condition_always:
983 fprintf(fp, "always");
984 break;
985
986 default:
987 fprintf(fp, "unk%X", cond);
988 break;
989 }
990 }
991
992 static const char *
993 function_call_mode(enum midgard_call_mode mode)
994 {
995 switch (mode) {
996 case midgard_call_mode_default:
997 return "";
998 case midgard_call_mode_call:
999 return ".call";
1000 case midgard_call_mode_return:
1001 return ".return";
1002 default:
1003 return ".reserved";
1004 }
1005 }
1006
1007 static bool
1008 print_compact_branch_writeout_field(disassemble_context *ctx, FILE *fp,
1009 uint16_t word)
1010 {
1011 midgard_jmp_writeout_op op = word & 0x7;
1012
1013 switch (op) {
1014 case midgard_jmp_writeout_op_branch_uncond: {
1015 midgard_branch_uncond br_uncond;
1016 memcpy((char *)&br_uncond, (char *)&word, sizeof(br_uncond));
1017 fprintf(fp, "br.uncond%s ", function_call_mode(br_uncond.call_mode));
1018
1019 if (br_uncond.offset >= 0)
1020 fprintf(fp, "+");
1021
1022 fprintf(fp, "%d -> %s", br_uncond.offset,
1023 midgard_tag_props[br_uncond.dest_tag].name);
1024 fprintf(fp, "\n");
1025
1026 return br_uncond.offset >= 0;
1027 }
1028
1029 case midgard_jmp_writeout_op_branch_cond:
1030 case midgard_jmp_writeout_op_writeout:
1031 case midgard_jmp_writeout_op_discard:
1032 default: {
1033 midgard_branch_cond br_cond;
1034 memcpy((char *)&br_cond, (char *)&word, sizeof(br_cond));
1035
1036 fprintf(fp, "br.");
1037
1038 print_branch_op(fp, br_cond.op);
1039 print_branch_cond(fp, br_cond.cond);
1040
1041 fprintf(fp, " ");
1042
1043 if (br_cond.offset >= 0)
1044 fprintf(fp, "+");
1045
1046 fprintf(fp, "%d -> %s", br_cond.offset,
1047 midgard_tag_props[br_cond.dest_tag].name);
1048 fprintf(fp, "\n");
1049
1050 return br_cond.offset >= 0;
1051 }
1052 }
1053
1054 return false;
1055 }
1056
1057 static bool
1058 print_extended_branch_writeout_field(disassemble_context *ctx, FILE *fp,
1059 uint8_t *words, unsigned next)
1060 {
1061 midgard_branch_extended br;
1062 memcpy((char *)&br, (char *)words, sizeof(br));
1063
1064 fprintf(fp, "brx%s.", function_call_mode(br.call_mode));
1065
1066 print_branch_op(fp, br.op);
1067
1068 /* Condition codes are a LUT in the general case, but simply repeated 8 times
1069 * for single-channel conditions.. Check this. */
1070
1071 bool single_channel = true;
1072
1073 for (unsigned i = 0; i < 16; i += 2) {
1074 single_channel &= (((br.cond >> i) & 0x3) == (br.cond & 0x3));
1075 }
1076
1077 if (single_channel)
1078 print_branch_cond(fp, br.cond & 0x3);
1079 else
1080 fprintf(fp, "lut%X", br.cond);
1081
1082 fprintf(fp, " ");
1083
1084 if (br.offset >= 0)
1085 fprintf(fp, "+");
1086
1087 fprintf(fp, "%d -> %s\n", br.offset, midgard_tag_props[br.dest_tag].name);
1088
1089 unsigned I = next + br.offset * 4;
1090
1091 if (ctx->midg_tags[I] && ctx->midg_tags[I] != br.dest_tag) {
1092 fprintf(fp, "\t/* XXX TAG ERROR: jumping to %s but tagged %s \n",
1093 midgard_tag_props[br.dest_tag].name,
1094 midgard_tag_props[ctx->midg_tags[I]].name);
1095 }
1096
1097 ctx->midg_tags[I] = br.dest_tag;
1098
1099 return br.offset >= 0;
1100 }
1101
1102 static unsigned
1103 num_alu_fields_enabled(uint32_t control_word)
1104 {
1105 unsigned ret = 0;
1106
1107 if ((control_word >> 17) & 1)
1108 ret++;
1109
1110 if ((control_word >> 19) & 1)
1111 ret++;
1112
1113 if ((control_word >> 21) & 1)
1114 ret++;
1115
1116 if ((control_word >> 23) & 1)
1117 ret++;
1118
1119 if ((control_word >> 25) & 1)
1120 ret++;
1121
1122 return ret;
1123 }
1124
1125 static bool
1126 print_alu_word(disassemble_context *ctx, FILE *fp, const uint32_t *words,
1127 unsigned num_quad_words, unsigned tabs, unsigned next,
1128 bool verbose)
1129 {
1130 uint32_t control_word = words[0];
1131 uint16_t *beginning_ptr = (uint16_t *)(words + 1);
1132 unsigned num_fields = num_alu_fields_enabled(control_word);
1133 uint16_t *word_ptr = beginning_ptr + num_fields;
1134 unsigned num_words = 2 + num_fields;
1135 const midgard_constants *consts = NULL;
1136 bool branch_forward = false;
1137
1138 if ((control_word >> 17) & 1)
1139 num_words += 3;
1140
1141 if ((control_word >> 19) & 1)
1142 num_words += 2;
1143
1144 if ((control_word >> 21) & 1)
1145 num_words += 3;
1146
1147 if ((control_word >> 23) & 1)
1148 num_words += 2;
1149
1150 if ((control_word >> 25) & 1)
1151 num_words += 3;
1152
1153 if ((control_word >> 26) & 1)
1154 num_words += 1;
1155
1156 if ((control_word >> 27) & 1)
1157 num_words += 3;
1158
1159 if (num_quad_words > (num_words + 7) / 8) {
1160 assert(num_quad_words == (num_words + 15) / 8);
1161 // Assume that the extra quadword is constants
1162 consts = (midgard_constants *)(words + (4 * num_quad_words - 4));
1163 }
1164
1165 if ((control_word >> 16) & 1)
1166 fprintf(fp, "unknown bit 16 enabled\n");
1167
1168 if ((control_word >> 17) & 1) {
1169 print_vector_field(ctx, fp, "vmul", word_ptr, *beginning_ptr, consts,
1170 tabs, verbose);
1171 beginning_ptr += 1;
1172 word_ptr += 3;
1173 }
1174
1175 if ((control_word >> 18) & 1)
1176 fprintf(fp, "unknown bit 18 enabled\n");
1177
1178 if ((control_word >> 19) & 1) {
1179 print_scalar_field(ctx, fp, "sadd", word_ptr, *beginning_ptr, consts,
1180 tabs, verbose);
1181 beginning_ptr += 1;
1182 word_ptr += 2;
1183 }
1184
1185 if ((control_word >> 20) & 1)
1186 fprintf(fp, "unknown bit 20 enabled\n");
1187
1188 if ((control_word >> 21) & 1) {
1189 print_vector_field(ctx, fp, "vadd", word_ptr, *beginning_ptr, consts,
1190 tabs, verbose);
1191 beginning_ptr += 1;
1192 word_ptr += 3;
1193 }
1194
1195 if ((control_word >> 22) & 1)
1196 fprintf(fp, "unknown bit 22 enabled\n");
1197
1198 if ((control_word >> 23) & 1) {
1199 print_scalar_field(ctx, fp, "smul", word_ptr, *beginning_ptr, consts,
1200 tabs, verbose);
1201 beginning_ptr += 1;
1202 word_ptr += 2;
1203 }
1204
1205 if ((control_word >> 24) & 1)
1206 fprintf(fp, "unknown bit 24 enabled\n");
1207
1208 if ((control_word >> 25) & 1) {
1209 print_vector_field(ctx, fp, "lut", word_ptr, *beginning_ptr, consts, tabs,
1210 verbose);
1211 word_ptr += 3;
1212 }
1213
1214 if ((control_word >> 26) & 1) {
1215 branch_forward |= print_compact_branch_writeout_field(ctx, fp, *word_ptr);
1216 word_ptr += 1;
1217 }
1218
1219 if ((control_word >> 27) & 1) {
1220 branch_forward |= print_extended_branch_writeout_field(
1221 ctx, fp, (uint8_t *)word_ptr, next);
1222 word_ptr += 3;
1223 }
1224
1225 if (consts)
1226 fprintf(fp, "uconstants 0x%X, 0x%X, 0x%X, 0x%X\n", consts->u32[0],
1227 consts->u32[1], consts->u32[2], consts->u32[3]);
1228
1229 return branch_forward;
1230 }
1231
1232 /* TODO: how can we use this now that we know that these params can't be known
1233 * before run time in every single case? Maybe just use it in the cases we can? */
1234 UNUSED static void
1235 print_varying_parameters(FILE *fp, midgard_load_store_word *word)
1236 {
1237 midgard_varying_params p = midgard_unpack_varying_params(*word);
1238
1239 /* If a varying, there are qualifiers */
1240 if (p.flat_shading)
1241 fprintf(fp, ".flat");
1242
1243 if (p.perspective_correction)
1244 fprintf(fp, ".correction");
1245
1246 if (p.centroid_mapping)
1247 fprintf(fp, ".centroid");
1248
1249 if (p.interpolate_sample)
1250 fprintf(fp, ".sample");
1251
1252 switch (p.modifier) {
1253 case midgard_varying_mod_perspective_y:
1254 fprintf(fp, ".perspectivey");
1255 break;
1256 case midgard_varying_mod_perspective_z:
1257 fprintf(fp, ".perspectivez");
1258 break;
1259 case midgard_varying_mod_perspective_w:
1260 fprintf(fp, ".perspectivew");
1261 break;
1262 default:
1263 UNREACHABLE("invalid varying modifier");
1264 break;
1265 }
1266 }
1267
1268 /* Helper to print integer well-formatted, but only when non-zero. */
1269 static void
1270 midgard_print_sint(FILE *fp, int n)
1271 {
1272 if (n > 0)
1273 fprintf(fp, " + 0x%X", n);
1274 else if (n < 0)
1275 fprintf(fp, " - 0x%X", -n);
1276 }
1277
1278 static void
1279 print_load_store_instr(disassemble_context *ctx, FILE *fp, uint64_t data,
1280 bool verbose)
1281 {
1282 midgard_load_store_word *word = (midgard_load_store_word *)&data;
1283
1284 print_ld_st_opcode(fp, word->op);
1285
1286 if (word->op == midgard_op_trap) {
1287 fprintf(fp, " 0x%X\n", word->signed_offset);
1288 return;
1289 }
1290
1291 /* Print opcode modifiers */
1292
1293 if (OP_USES_ATTRIB(word->op)) {
1294 /* Print non-default attribute tables */
1295 bool default_secondary = (word->op == midgard_op_st_vary_32) ||
1296 (word->op == midgard_op_st_vary_16) ||
1297 (word->op == midgard_op_st_vary_32u) ||
1298 (word->op == midgard_op_st_vary_32i) ||
1299 (word->op == midgard_op_ld_vary_32) ||
1300 (word->op == midgard_op_ld_vary_16) ||
1301 (word->op == midgard_op_ld_vary_32u) ||
1302 (word->op == midgard_op_ld_vary_32i);
1303
1304 bool default_primary = (word->op == midgard_op_ld_attr_32) ||
1305 (word->op == midgard_op_ld_attr_16) ||
1306 (word->op == midgard_op_ld_attr_32u) ||
1307 (word->op == midgard_op_ld_attr_32i);
1308
1309 bool has_default = (default_secondary || default_primary);
1310 bool auto32 = (word->index_format >> 0) & 1;
1311 bool is_secondary = (word->index_format >> 1) & 1;
1312
1313 if (auto32)
1314 fprintf(fp, ".a32");
1315
1316 if (has_default && (is_secondary != default_secondary))
1317 fprintf(fp, ".%s", is_secondary ? "secondary" : "primary");
1318 } else if (word->op == midgard_op_ld_cubemap_coords ||
1319 OP_IS_PROJECTION(word->op))
1320 fprintf(fp, ".%s", word->bitsize_toggle ? "f32" : "f16");
1321
1322 fprintf(fp, " ");
1323
1324 /* src/dest register */
1325
1326 if (!OP_IS_STORE(word->op)) {
1327 print_ldst_write_reg(fp, word->reg);
1328
1329 /* Some opcodes don't have a swizzable src register, and
1330 * instead the swizzle is applied before the result is written
1331 * to the dest reg. For these ops, we combine the writemask
1332 * with the swizzle to display them in the disasm compactly. */
1333 unsigned swizzle = word->swizzle;
1334 if ((OP_IS_REG2REG_LDST(word->op) && word->op != midgard_op_lea &&
1335 word->op != midgard_op_lea_image) ||
1336 OP_IS_ATOMIC(word->op))
1337 swizzle = 0xE4;
1338 print_ldst_mask(fp, word->mask, swizzle);
1339 } else {
1340 uint8_t mask = (word->mask & 0x1) | ((word->mask & 0x2) << 1) |
1341 ((word->mask & 0x4) << 2) | ((word->mask & 0x8) << 3);
1342 mask |= mask << 1;
1343 print_ldst_read_reg(fp, word->reg);
1344 print_vec_swizzle(fp, word->swizzle, midgard_src_passthrough,
1345 midgard_reg_mode_32, mask);
1346 }
1347
1348 /* ld_ubo args */
1349 if (OP_IS_UBO_READ(word->op)) {
1350 if (word->signed_offset & 1) { /* buffer index imm */
1351 unsigned imm = midgard_unpack_ubo_index_imm(*word);
1352 fprintf(fp, ", %u", imm);
1353 } else { /* buffer index from reg */
1354 fprintf(fp, ", ");
1355 print_ldst_read_reg(fp, word->arg_reg);
1356 fprintf(fp, ".%c", components[word->arg_comp]);
1357 }
1358
1359 fprintf(fp, ", ");
1360 print_ldst_read_reg(fp, word->index_reg);
1361 fprintf(fp, ".%c", components[word->index_comp]);
1362 if (word->index_shift)
1363 fprintf(fp, " << %u", word->index_shift);
1364 midgard_print_sint(fp, UNPACK_LDST_UBO_OFS(word->signed_offset));
1365 }
1366
1367 /* mem addr expression */
1368 if (OP_HAS_ADDRESS(word->op)) {
1369 fprintf(fp, ", ");
1370 bool first = true;
1371
1372 /* Skip printing zero */
1373 if (word->arg_reg != 7 || verbose) {
1374 print_ldst_read_reg(fp, word->arg_reg);
1375 fprintf(fp, ".u%d.%c", word->bitsize_toggle ? 64 : 32,
1376 components[word->arg_comp]);
1377 first = false;
1378 }
1379
1380 if ((word->op < midgard_op_atomic_cmpxchg ||
1381 word->op > midgard_op_atomic_cmpxchg64_be) &&
1382 word->index_reg != 0x7) {
1383 if (!first)
1384 fprintf(fp, " + ");
1385
1386 print_ldst_read_reg(fp, word->index_reg);
1387 fprintf(fp, "%s.%c", index_format_names[word->index_format],
1388 components[word->index_comp]);
1389 if (word->index_shift)
1390 fprintf(fp, " << %u", word->index_shift);
1391 }
1392
1393 midgard_print_sint(fp, word->signed_offset);
1394 }
1395
1396 /* src reg for reg2reg ldst opcodes */
1397 if (OP_IS_REG2REG_LDST(word->op)) {
1398 fprintf(fp, ", ");
1399 print_ldst_read_reg(fp, word->arg_reg);
1400 print_vec_swizzle(fp, word->swizzle, midgard_src_passthrough,
1401 midgard_reg_mode_32, 0xFF);
1402 }
1403
1404 /* atomic ops encode the source arg where the ldst swizzle would be. */
1405 if (OP_IS_ATOMIC(word->op)) {
1406 unsigned src = (word->swizzle >> 2) & 0x7;
1407 unsigned src_comp = word->swizzle & 0x3;
1408 fprintf(fp, ", ");
1409 print_ldst_read_reg(fp, src);
1410 fprintf(fp, ".%c", components[src_comp]);
1411 }
1412
1413 /* CMPXCHG encodes the extra comparison arg where the index reg would be. */
1414 if (word->op >= midgard_op_atomic_cmpxchg &&
1415 word->op <= midgard_op_atomic_cmpxchg64_be) {
1416 fprintf(fp, ", ");
1417 print_ldst_read_reg(fp, word->index_reg);
1418 fprintf(fp, ".%c", components[word->index_comp]);
1419 }
1420
1421 /* index reg for attr/vary/images, selector for ld/st_special */
1422 if (OP_IS_SPECIAL(word->op) || OP_USES_ATTRIB(word->op)) {
1423 fprintf(fp, ", ");
1424 print_ldst_read_reg(fp, word->index_reg);
1425 fprintf(fp, ".%c", components[word->index_comp]);
1426 if (word->index_shift)
1427 fprintf(fp, " << %u", word->index_shift);
1428 midgard_print_sint(fp, UNPACK_LDST_ATTRIB_OFS(word->signed_offset));
1429 }
1430
1431 /* vertex reg for attrib/varying ops, coord reg for image ops */
1432 if (OP_USES_ATTRIB(word->op)) {
1433 fprintf(fp, ", ");
1434 print_ldst_read_reg(fp, word->arg_reg);
1435
1436 if (OP_IS_IMAGE(word->op))
1437 fprintf(fp, ".u%d", word->bitsize_toggle ? 64 : 32);
1438
1439 fprintf(fp, ".%c", components[word->arg_comp]);
1440
1441 if (word->bitsize_toggle && !OP_IS_IMAGE(word->op))
1442 midgard_print_sint(fp, UNPACK_LDST_VERTEX_OFS(word->signed_offset));
1443 }
1444
1445 /* TODO: properly decode format specifier for PACK/UNPACK ops */
1446 if (OP_IS_PACK_COLOUR(word->op) || OP_IS_UNPACK_COLOUR(word->op)) {
1447 fprintf(fp, ", ");
1448 unsigned format_specifier =
1449 (word->signed_offset << 4) | word->index_shift;
1450 fprintf(fp, "0x%X", format_specifier);
1451 }
1452
1453 fprintf(fp, "\n");
1454
1455 /* Debugging stuff */
1456
1457 if (!OP_IS_STORE(word->op))
1458 update_dest(ctx, word->reg);
1459 }
1460
1461 static void
1462 print_load_store_word(disassemble_context *ctx, FILE *fp, const uint32_t *word,
1463 bool verbose)
1464 {
1465 midgard_load_store *load_store = (midgard_load_store *)word;
1466
1467 if (load_store->word1 != 3) {
1468 print_load_store_instr(ctx, fp, load_store->word1, verbose);
1469 }
1470
1471 if (load_store->word2 != 3) {
1472 print_load_store_instr(ctx, fp, load_store->word2, verbose);
1473 }
1474 }
1475
1476 static void
1477 print_texture_reg_select(FILE *fp, uint8_t u, unsigned base)
1478 {
1479 midgard_tex_register_select sel;
1480 memcpy(&sel, &u, sizeof(u));
1481
1482 print_tex_reg(fp, base + sel.select, false);
1483
1484 unsigned component = sel.component;
1485
1486 /* Use the upper half in half-reg mode */
1487 if (sel.upper) {
1488 assert(!sel.full);
1489 component += 4;
1490 }
1491
1492 fprintf(fp, ".%c.%d", components[component], sel.full ? 32 : 16);
1493
1494 assert(sel.zero == 0);
1495 }
1496
1497 static void
1498 print_texture_format(FILE *fp, int format)
1499 {
1500 /* Act like a modifier */
1501 fprintf(fp, ".");
1502
1503 switch (format) {
1504 DEFINE_CASE(1, "1d");
1505 DEFINE_CASE(2, "2d");
1506 DEFINE_CASE(3, "3d");
1507 DEFINE_CASE(0, "cube");
1508
1509 default:
1510 UNREACHABLE("Bad format");
1511 }
1512 }
1513
1514 static void
1515 print_texture_op(FILE *fp, unsigned op)
1516 {
1517 if (tex_opcode_props[op].name)
1518 fprintf(fp, "%s", tex_opcode_props[op].name);
1519 else
1520 fprintf(fp, "tex_op_%02X", op);
1521 }
1522
1523 static bool
1524 texture_op_takes_bias(unsigned op)
1525 {
1526 return op == midgard_tex_op_normal;
1527 }
1528
1529 static char
1530 sampler_type_name(enum mali_sampler_type t)
1531 {
1532 switch (t) {
1533 case MALI_SAMPLER_FLOAT:
1534 return 'f';
1535 case MALI_SAMPLER_UNSIGNED:
1536 return 'u';
1537 case MALI_SAMPLER_SIGNED:
1538 return 'i';
1539 default:
1540 return '?';
1541 }
1542 }
1543
1544 static void
1545 print_texture_barrier(FILE *fp, const uint32_t *word)
1546 {
1547 midgard_texture_barrier_word *barrier = (midgard_texture_barrier_word *)word;
1548
1549 if (barrier->type != TAG_TEXTURE_4_BARRIER)
1550 fprintf(fp, "/* barrier tag %X != tex/bar */ ", barrier->type);
1551
1552 if (!barrier->cont)
1553 fprintf(fp, "/* cont missing? */");
1554
1555 if (!barrier->last)
1556 fprintf(fp, "/* last missing? */");
1557
1558 if (barrier->zero1)
1559 fprintf(fp, "/* zero1 = 0x%X */ ", barrier->zero1);
1560
1561 if (barrier->zero2)
1562 fprintf(fp, "/* zero2 = 0x%X */ ", barrier->zero2);
1563
1564 if (barrier->zero3)
1565 fprintf(fp, "/* zero3 = 0x%X */ ", barrier->zero3);
1566
1567 if (barrier->zero4)
1568 fprintf(fp, "/* zero4 = 0x%X */ ", barrier->zero4);
1569
1570 if (barrier->zero5)
1571 fprintf(fp, "/* zero4 = 0x%" PRIx64 " */ ", barrier->zero5);
1572
1573 if (barrier->out_of_order)
1574 fprintf(fp, ".ooo%u", barrier->out_of_order);
1575
1576 fprintf(fp, "\n");
1577 }
1578
1579 #undef DEFINE_CASE
1580
1581 static const char *
1582 texture_mode(enum mali_texture_mode mode)
1583 {
1584 switch (mode) {
1585 case TEXTURE_NORMAL:
1586 return "";
1587 case TEXTURE_SHADOW:
1588 return ".shadow";
1589 case TEXTURE_GATHER_SHADOW:
1590 return ".gather.shadow";
1591 case TEXTURE_GATHER_X:
1592 return ".gatherX";
1593 case TEXTURE_GATHER_Y:
1594 return ".gatherY";
1595 case TEXTURE_GATHER_Z:
1596 return ".gatherZ";
1597 case TEXTURE_GATHER_W:
1598 return ".gatherW";
1599 default:
1600 return "unk";
1601 }
1602 }
1603
1604 static const char *
1605 derivative_mode(enum mali_derivative_mode mode)
1606 {
1607 switch (mode) {
1608 case TEXTURE_DFDX:
1609 return ".x";
1610 case TEXTURE_DFDY:
1611 return ".y";
1612 default:
1613 return "unk";
1614 }
1615 }
1616
1617 static const char *
1618 partial_exection_mode(enum midgard_partial_execution mode)
1619 {
1620 switch (mode) {
1621 case MIDGARD_PARTIAL_EXECUTION_NONE:
1622 return "";
1623 case MIDGARD_PARTIAL_EXECUTION_SKIP:
1624 return ".skip";
1625 case MIDGARD_PARTIAL_EXECUTION_KILL:
1626 return ".kill";
1627 default:
1628 return ".reserved";
1629 }
1630 }
1631
1632 static int
1633 s4_to_int(int val) {
1634 int sign_bit = val & 8;
1635 return (val ^ sign_bit) - sign_bit;
1636 }
1637
1638 static void
1639 print_texture_word(disassemble_context *ctx, FILE *fp, const uint32_t *word,
1640 unsigned tabs, unsigned in_reg_base, unsigned out_reg_base)
1641 {
1642 midgard_texture_word *texture = (midgard_texture_word *)word;
1643 validate_sampler_type(texture->op, texture->sampler_type);
1644
1645 /* Broad category of texture operation in question */
1646 print_texture_op(fp, texture->op);
1647
1648 /* Barriers use a dramatically different code path */
1649 if (texture->op == midgard_tex_op_barrier) {
1650 print_texture_barrier(fp, word);
1651 return;
1652 } else if (texture->type == TAG_TEXTURE_4_BARRIER)
1653 fprintf(fp, "/* nonbarrier had tex/bar tag */ ");
1654 else if (texture->type == TAG_TEXTURE_4_VTX)
1655 fprintf(fp, ".vtx");
1656
1657 if (texture->op == midgard_tex_op_derivative)
1658 fprintf(fp, "%s", derivative_mode(texture->mode));
1659 else
1660 fprintf(fp, "%s", texture_mode(texture->mode));
1661
1662 /* Specific format in question */
1663 print_texture_format(fp, texture->format);
1664
1665 /* Instruction "modifiers" parallel the ALU instructions. */
1666 fputs(partial_exection_mode(texture->exec), fp);
1667
1668 if (texture->out_of_order)
1669 fprintf(fp, ".ooo%u", texture->out_of_order);
1670
1671 fprintf(fp, " ");
1672 print_tex_reg(fp, out_reg_base + texture->out_reg_select, true);
1673 print_tex_mask(fp, texture->mask, texture->out_upper);
1674 fprintf(fp, ".%c%d", texture->sampler_type == MALI_SAMPLER_FLOAT ? 'f' : 'i',
1675 texture->out_full ? 32 : 16);
1676 assert(!(texture->out_full && texture->out_upper));
1677
1678 /* Output modifiers are only valid for float texture operations */
1679 if (texture->sampler_type == MALI_SAMPLER_FLOAT)
1680 mir_print_outmod(fp, texture->outmod, false);
1681
1682 fprintf(fp, ", ");
1683
1684 /* Depending on whether we read from textures directly or indirectly,
1685 * we may be able to update our analysis */
1686
1687 if (texture->texture_register) {
1688 fprintf(fp, "texture[");
1689 print_texture_reg_select(fp, texture->texture_handle, in_reg_base);
1690 fprintf(fp, "], ");
1691 } else {
1692 fprintf(fp, "texture%u, ", texture->texture_handle);
1693 }
1694
1695 /* Print the type, GL style */
1696 fprintf(fp, "%csampler", sampler_type_name(texture->sampler_type));
1697
1698 if (texture->sampler_register) {
1699 fprintf(fp, "[");
1700 print_texture_reg_select(fp, texture->sampler_handle, in_reg_base);
1701 fprintf(fp, "]");
1702 } else {
1703 fprintf(fp, "%u", texture->sampler_handle);
1704 }
1705
1706 print_vec_swizzle(fp, texture->swizzle, midgard_src_passthrough,
1707 midgard_reg_mode_32, 0xFF);
1708
1709 fprintf(fp, ", ");
1710
1711 midgard_src_expand_mode exp =
1712 texture->in_reg_upper ? midgard_src_expand_high : midgard_src_passthrough;
1713 print_tex_reg(fp, in_reg_base + texture->in_reg_select, false);
1714 print_vec_swizzle(fp, texture->in_reg_swizzle, exp, midgard_reg_mode_32,
1715 0xFF);
1716 fprintf(fp, ".%d", texture->in_reg_full ? 32 : 16);
1717 assert(!(texture->in_reg_full && texture->in_reg_upper));
1718
1719 /* There is *always* an offset attached. Of
1720 * course, that offset is just immediate #0 for a
1721 * GLES call that doesn't take an offset. If there
1722 * is a non-negative non-zero offset, this is
1723 * specified in immediate offset mode, with the
1724 * values in the offset_* fields as immediates. If
1725 * this is a negative offset, we instead switch to
1726 * a register offset mode, where the offset_*
1727 * fields become register triplets */
1728
1729 if (texture->offset_register) {
1730 fprintf(fp, " + ");
1731
1732 bool full = texture->offset & 1;
1733 bool select = texture->offset & 2;
1734 bool upper = texture->offset & 4;
1735 unsigned swizzle = texture->offset >> 3;
1736 midgard_src_expand_mode exp =
1737 upper ? midgard_src_expand_high : midgard_src_passthrough;
1738
1739 print_tex_reg(fp, in_reg_base + select, false);
1740 print_vec_swizzle(fp, swizzle, exp, midgard_reg_mode_32, 0xFF);
1741 fprintf(fp, ".%d", full ? 32 : 16);
1742 assert(!(texture->out_full && texture->out_upper));
1743
1744 fprintf(fp, ", ");
1745 } else if (texture->offset) {
1746 /* Only select ops allow negative immediate offsets, verify */
1747
1748 int offset_x = s4_to_int(texture->offset & 0xF);
1749 int offset_y = s4_to_int((texture->offset >> 4) & 0xF);
1750 int offset_z = s4_to_int((texture->offset >> 8) & 0xF);
1751
1752 bool neg_x = offset_x < 0;
1753 bool neg_y = offset_y < 0;
1754 bool neg_z = offset_z < 0;
1755 bool any_neg = neg_x || neg_y || neg_z;
1756
1757 if (any_neg && texture->op != midgard_tex_op_fetch)
1758 fprintf(fp, "/* invalid negative */ ");
1759
1760 /* Regardless, just print the immediate offset */
1761
1762 fprintf(fp, " + <%d, %d, %d>, ", offset_x, offset_y, offset_z);
1763 } else {
1764 fprintf(fp, ", ");
1765 }
1766
1767 char lod_operand = texture_op_takes_bias(texture->op) ? '+' : '=';
1768
1769 if (texture->lod_register) {
1770 fprintf(fp, "lod %c ", lod_operand);
1771 print_texture_reg_select(fp, texture->bias, in_reg_base);
1772 fprintf(fp, ", ");
1773
1774 if (texture->bias_int)
1775 fprintf(fp, " /* bias_int = 0x%X */", texture->bias_int);
1776 } else if (texture->op == midgard_tex_op_fetch) {
1777 /* For texel fetch, the int LOD is in the fractional place and
1778 * there is no fraction. We *always* have an explicit LOD, even
1779 * if it's zero. */
1780
1781 if (texture->bias_int)
1782 fprintf(fp, " /* bias_int = 0x%X */ ", texture->bias_int);
1783
1784 fprintf(fp, "lod = %u, ", texture->bias);
1785 } else if (texture->bias || texture->bias_int) {
1786 signed bias_int = texture->bias_int;
1787 float bias_frac = texture->bias / 256.0f;
1788 float bias = bias_int + bias_frac;
1789
1790 bool is_bias = texture_op_takes_bias(texture->op);
1791 char sign = (bias >= 0.0) ? '+' : '-';
1792 char operand = is_bias ? sign : '=';
1793
1794 fprintf(fp, "lod %c %f, ", operand, fabsf(bias));
1795 }
1796
1797 fprintf(fp, "\n");
1798
1799 /* While not zero in general, for these simple instructions the
1800 * following unknowns are zero, so we don't include them */
1801
1802 if (texture->unknown4 || texture->unknown8) {
1803 fprintf(fp, "// unknown4 = 0x%x\n", texture->unknown4);
1804 fprintf(fp, "// unknown8 = 0x%x\n", texture->unknown8);
1805 }
1806 }
1807
1808 void
1809 disassemble_midgard(FILE *fp, const void *code, size_t size, uint64_t gpu_id,
1810 bool verbose)
1811 {
1812 const uint32_t *words = (const uint32_t *)code;
1813 unsigned num_words = size / 4;
1814 int tabs = 0;
1815
1816 bool branch_forward = false;
1817
1818 unsigned last_next_tag = TAG_BREAK;
1819
1820 unsigned i = 0;
1821
1822 disassemble_context ctx = {
1823 .midg_tags = calloc(sizeof(ctx.midg_tags[0]), num_words),
1824 .midg_ever_written = 0,
1825 };
1826
1827 while (i < num_words) {
1828 unsigned tag = words[i] & 0xF;
1829 unsigned next_tag = (words[i] >> 4) & 0xF;
1830 unsigned num_quad_words = midgard_tag_props[tag].size;
1831
1832 if (ctx.midg_tags[i] && ctx.midg_tags[i] != tag) {
1833 fprintf(fp, "\t/* XXX: TAG ERROR branch, got %s expected %s */\n",
1834 midgard_tag_props[tag].name,
1835 midgard_tag_props[ctx.midg_tags[i]].name);
1836 }
1837
1838 ctx.midg_tags[i] = tag;
1839
1840 /* Check the tag. The idea is to ensure that next_tag is
1841 * *always* recoverable from the disassembly, such that we may
1842 * safely omit printing next_tag. To show this, we first
1843 * consider that next tags are semantically off-byone -- we end
1844 * up parsing tag n during step n+1. So, we ensure after we're
1845 * done disassembling the next tag of the final bundle is BREAK
1846 * and warn otherwise. We also ensure that the next tag is
1847 * never INVALID. Beyond that, since the last tag is checked
1848 * outside the loop, we can check one tag prior. If equal to
1849 * the current tag (which is unique), we're done. Otherwise, we
1850 * print if that tag was > TAG_BREAK, which implies the tag was
1851 * not TAG_BREAK or TAG_INVALID. But we already checked for
1852 * TAG_INVALID, so it's just if the last tag was TAG_BREAK that
1853 * we're silent. So we throw in a print for break-next on at
1854 * the end of the bundle (if it's not the final bundle, which
1855 * we already check for above), disambiguating this case as
1856 * well. Hence in all cases we are unambiguous, QED. */
1857
1858 if (next_tag == TAG_INVALID)
1859 fprintf(fp, "\t/* XXX: invalid next tag */\n");
1860
1861 if (last_next_tag > TAG_BREAK && last_next_tag != tag) {
1862 fprintf(fp, "\t/* XXX: TAG ERROR sequence, got %s expexted %s */\n",
1863 midgard_tag_props[tag].name,
1864 midgard_tag_props[last_next_tag].name);
1865 }
1866
1867 last_next_tag = next_tag;
1868
1869 /* Tags are unique in the following way:
1870 *
1871 * INVALID, BREAK, UNKNOWN_*: verbosely printed
1872 * TEXTURE_4_BARRIER: verified by barrier/!barrier op
1873 * TEXTURE_4_VTX: .vtx tag printed
1874 * TEXTURE_4: tetxure lack of barriers or .vtx
1875 * TAG_LOAD_STORE_4: only load/store
1876 * TAG_ALU_4/8/12/16: by number of instructions/constants
1877 * TAG_ALU_4_8/12/16_WRITEOUT: ^^ with .writeout tag
1878 */
1879
1880 switch (tag) {
1881 case TAG_TEXTURE_4_VTX ... TAG_TEXTURE_4_BARRIER: {
1882 bool interpipe_aliasing =
1883 midgard_get_quirks(gpu_id) & MIDGARD_INTERPIPE_REG_ALIASING;
1884
1885 print_texture_word(
1886 &ctx, fp, &words[i], tabs, interpipe_aliasing ? 0 : REG_TEX_BASE,
1887 interpipe_aliasing ? REGISTER_LDST_BASE : REG_TEX_BASE);
1888 break;
1889 }
1890
1891 case TAG_LOAD_STORE_4:
1892 print_load_store_word(&ctx, fp, &words[i], verbose);
1893 break;
1894
1895 case TAG_ALU_4 ... TAG_ALU_16_WRITEOUT:
1896 branch_forward = print_alu_word(&ctx, fp, &words[i], num_quad_words,
1897 tabs, i + 4 * num_quad_words, verbose);
1898
1899 /* TODO: infer/verify me */
1900 if (tag >= TAG_ALU_4_WRITEOUT)
1901 fprintf(fp, "writeout\n");
1902
1903 break;
1904
1905 default:
1906 fprintf(fp, "Unknown word type %u:\n", words[i] & 0xF);
1907 num_quad_words = 1;
1908 print_quad_word(fp, &words[i], tabs);
1909 fprintf(fp, "\n");
1910 break;
1911 }
1912
1913 /* Include a synthetic "break" instruction at the end of the
1914 * bundle to signify that if, absent a branch, the shader
1915 * execution will stop here. Stop disassembly at such a break
1916 * based on a heuristic */
1917
1918 if (next_tag == TAG_BREAK) {
1919 if (branch_forward) {
1920 fprintf(fp, "break\n");
1921 } else {
1922 fprintf(fp, "\n");
1923 break;
1924 }
1925 }
1926
1927 fprintf(fp, "\n");
1928
1929 i += 4 * num_quad_words;
1930 }
1931
1932 if (last_next_tag != TAG_BREAK) {
1933 fprintf(fp, "/* XXX: shader ended with tag %s */\n",
1934 midgard_tag_props[last_next_tag].name);
1935 }
1936
1937 free(ctx.midg_tags);
1938 }