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