pan/midgard: Prefix blobber-db output for grepping
[mesa.git] / src / panfrost / midgard / disassemble.c
1 /* Author(s):
2 * Connor Abbott
3 * Alyssa Rosenzweig
4 *
5 * Copyright (c) 2013 Connor Abbott (connor@abbott.cx)
6 * Copyright (c) 2018 Alyssa Rosenzweig (alyssa@rosenzweig.io)
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <assert.h>
30 #include <inttypes.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include "midgard.h"
34 #include "midgard-parse.h"
35 #include "midgard_ops.h"
36 #include "disassemble.h"
37 #include "helpers.h"
38 #include "util/half_float.h"
39 #include "util/u_math.h"
40
41 #define DEFINE_CASE(define, str) case define: { printf(str); break; }
42
43 static bool is_instruction_int = false;
44
45 /* Stats */
46
47 static unsigned nr_ins = 0;
48
49 /* Prints a short form of the tag for branching, the minimum needed to be
50 * legible and unambiguous */
51
52 static void
53 print_tag_short(unsigned tag)
54 {
55 switch (midgard_word_types[tag]) {
56 case midgard_word_type_texture:
57 printf("tex/%X", tag);
58 break;
59
60 case midgard_word_type_load_store:
61 printf("ldst");
62 break;
63
64 case midgard_word_type_alu:
65 printf("alu%d/%X", midgard_word_size[tag], tag);
66 break;
67
68 default:
69 printf("%s%X", (tag > 0) ? "" : "unk", tag);
70 break;
71 }
72 }
73
74 static void
75 print_alu_opcode(midgard_alu_op op)
76 {
77 bool int_op = false;
78
79 if (alu_opcode_props[op].name) {
80 printf("%s", alu_opcode_props[op].name);
81
82 int_op = midgard_is_integer_op(op);
83 } else
84 printf("alu_op_%02X", op);
85
86 /* For constant analysis */
87 is_instruction_int = int_op;
88 }
89
90 static void
91 print_ld_st_opcode(midgard_load_store_op op)
92 {
93 if (load_store_opcode_names[op])
94 printf("%s", load_store_opcode_names[op]);
95 else
96 printf("ldst_op_%02X", op);
97 }
98
99 static bool is_embedded_constant_half = false;
100 static bool is_embedded_constant_int = false;
101
102 static char
103 prefix_for_bits(unsigned bits)
104 {
105 switch (bits) {
106 case 8:
107 return 'q';
108 case 16:
109 return 'h';
110 case 64:
111 return 'd';
112 default:
113 return 0;
114 }
115 }
116
117 static void
118 print_reg(unsigned reg, unsigned bits)
119 {
120 /* Perform basic static analysis for expanding constants correctly */
121
122 if (reg == 26) {
123 is_embedded_constant_int = is_instruction_int;
124 is_embedded_constant_half = (bits < 32);
125 }
126
127 char prefix = prefix_for_bits(bits);
128
129 if (prefix)
130 putchar(prefix);
131
132 printf("r%u", reg);
133 }
134
135 static char *outmod_names_float[4] = {
136 "",
137 ".pos",
138 ".unk2",
139 ".sat"
140 };
141
142 static char *outmod_names_int[4] = {
143 ".isat",
144 ".usat",
145 "",
146 ".hi"
147 };
148
149 static char *srcmod_names_int[4] = {
150 "sext(",
151 "zext(",
152 "",
153 "("
154 };
155
156 static void
157 print_outmod(unsigned outmod, bool is_int)
158 {
159 printf("%s", is_int ? outmod_names_int[outmod] :
160 outmod_names_float[outmod]);
161 }
162
163 static void
164 print_quad_word(uint32_t *words, unsigned tabs)
165 {
166 unsigned i;
167
168 for (i = 0; i < 4; i++)
169 printf("0x%08X%s ", words[i], i == 3 ? "" : ",");
170
171 printf("\n");
172 }
173
174 static const char components[16] = "xyzwefghijklmnop";
175
176 /* Helper to print 4 chars of a swizzle */
177 static void
178 print_swizzle_helper(unsigned swizzle, bool upper)
179 {
180 for (unsigned i = 0; i < 4; ++i) {
181 unsigned c = (swizzle >> (i * 2)) & 3;
182 c += upper*4;
183 printf("%c", components[c]);
184 }
185 }
186
187 /* Helper to print 8 chars of a swizzle, duplicating over */
188 static void
189 print_swizzle_helper_8(unsigned swizzle, bool upper)
190 {
191 for (unsigned i = 0; i < 4; ++i) {
192 unsigned c = (swizzle >> (i * 2)) & 3;
193 c *= 2;
194 c += upper*8;
195 printf("%c%c", components[c], components[c+1]);
196 }
197 }
198
199 static void
200 print_swizzle_vec16(unsigned swizzle, bool rep_high, bool rep_low,
201 midgard_dest_override override)
202 {
203 printf(".");
204
205 if (override == midgard_dest_override_upper) {
206 if (rep_high)
207 printf(" /* rep_high */ ");
208 if (rep_low)
209 printf(" /* rep_low */ ");
210
211 if (!rep_high && rep_low)
212 print_swizzle_helper_8(swizzle, true);
213 else
214 print_swizzle_helper_8(swizzle, false);
215 } else {
216 print_swizzle_helper_8(swizzle, rep_high & 1);
217 print_swizzle_helper_8(swizzle, !rep_low & 1);
218 }
219 }
220
221 static void
222 print_swizzle_vec8(unsigned swizzle, bool rep_high, bool rep_low)
223 {
224 printf(".");
225
226 print_swizzle_helper(swizzle, rep_high & 1);
227 print_swizzle_helper(swizzle, !rep_low & 1);
228 }
229
230 static void
231 print_swizzle_vec4(unsigned swizzle, bool rep_high, bool rep_low)
232 {
233 if (rep_high)
234 printf(" /* rep_high */ ");
235 if (rep_low)
236 printf(" /* rep_low */ ");
237
238 if (swizzle == 0xE4) return; /* xyzw */
239
240 printf(".");
241 print_swizzle_helper(swizzle, 0);
242 }
243 static void
244 print_swizzle_vec2(unsigned swizzle, bool rep_high, bool rep_low)
245 {
246 if (rep_high)
247 printf(" /* rep_high */ ");
248 if (rep_low)
249 printf(" /* rep_low */ ");
250
251 if (swizzle == 0xE4) return; /* XY */
252
253 printf(".");
254
255 for (unsigned i = 0; i < 4; i += 2) {
256 unsigned a = (swizzle >> (i * 2)) & 3;
257 unsigned b = (swizzle >> ((i+1) * 2)) & 3;
258
259 /* Normally we're adjacent, but if there's an issue, don't make
260 * it ambiguous */
261
262 if (a & 0x1)
263 printf("[%c%c]", components[a], components[b]);
264 else if (a == b)
265 printf("%c", components[a >> 1]);
266 else if (b == (a + 1))
267 printf("%c", "XY"[a >> 1]);
268 else
269 printf("[%c%c]", components[a], components[b]);
270 }
271 }
272
273 static int
274 bits_for_mode(midgard_reg_mode mode)
275 {
276 switch (mode) {
277 case midgard_reg_mode_8:
278 return 8;
279 case midgard_reg_mode_16:
280 return 16;
281 case midgard_reg_mode_32:
282 return 32;
283 case midgard_reg_mode_64:
284 return 64;
285 default:
286 return 0;
287 }
288 }
289
290 static int
291 bits_for_mode_halved(midgard_reg_mode mode, bool half)
292 {
293 unsigned bits = bits_for_mode(mode);
294
295 if (half)
296 bits >>= 1;
297
298 return bits;
299 }
300
301 static void
302 print_vector_src(unsigned src_binary,
303 midgard_reg_mode mode, unsigned reg,
304 midgard_dest_override override, bool is_int)
305 {
306 midgard_vector_alu_src *src = (midgard_vector_alu_src *)&src_binary;
307
308 /* Modifiers change meaning depending on the op's context */
309
310 midgard_int_mod int_mod = src->mod;
311
312 if (is_int) {
313 printf("%s", srcmod_names_int[int_mod]);
314 } else {
315 if (src->mod & MIDGARD_FLOAT_MOD_NEG)
316 printf("-");
317
318 if (src->mod & MIDGARD_FLOAT_MOD_ABS)
319 printf("abs(");
320 }
321
322 //register
323 unsigned bits = bits_for_mode_halved(mode, src->half);
324 print_reg(reg, bits);
325
326 //swizzle
327 if (bits == 16)
328 print_swizzle_vec8(src->swizzle, src->rep_high, src->rep_low);
329 else if (bits == 8)
330 print_swizzle_vec16(src->swizzle, src->rep_high, src->rep_low, override);
331 else if (bits == 32)
332 print_swizzle_vec4(src->swizzle, src->rep_high, src->rep_low);
333 else if (bits == 64)
334 print_swizzle_vec2(src->swizzle, src->rep_high, src->rep_low);
335
336 /* Since we wrapped with a function-looking thing */
337
338 if (is_int && int_mod == midgard_int_shift)
339 printf(") << %d", bits);
340 else if ((is_int && (int_mod != midgard_int_normal))
341 || (!is_int && src->mod & MIDGARD_FLOAT_MOD_ABS))
342 printf(")");
343 }
344
345 static uint16_t
346 decode_vector_imm(unsigned src2_reg, unsigned imm)
347 {
348 uint16_t ret;
349 ret = src2_reg << 11;
350 ret |= (imm & 0x7) << 8;
351 ret |= (imm >> 3) & 0xFF;
352 return ret;
353 }
354
355 static void
356 print_immediate(uint16_t imm)
357 {
358 if (is_instruction_int)
359 printf("#%d", imm);
360 else
361 printf("#%g", _mesa_half_to_float(imm));
362 }
363
364 static unsigned
365 print_dest(unsigned reg, midgard_reg_mode mode, midgard_dest_override override)
366 {
367 /* Depending on the mode and override, we determine the type of
368 * destination addressed. Absent an override, we address just the
369 * type of the operation itself */
370
371 unsigned bits = bits_for_mode(mode);
372
373 if (override != midgard_dest_override_none)
374 bits /= 2;
375
376 print_reg(reg, bits);
377
378 return bits;
379 }
380
381 static void
382 print_mask_vec16(uint8_t mask, midgard_dest_override override)
383 {
384 printf(".");
385
386 if (override == midgard_dest_override_none) {
387 for (unsigned i = 0; i < 8; i++) {
388 if (mask & (1 << i))
389 printf("%c%c",
390 components[i*2 + 0],
391 components[i*2 + 1]);
392 }
393 } else {
394 bool upper = (override == midgard_dest_override_upper);
395
396 for (unsigned i = 0; i < 8; i++) {
397 if (mask & (1 << i))
398 printf("%c", components[i + (upper ? 8 : 0)]);
399 }
400 }
401 }
402
403 /* For 16-bit+ masks, we read off from the 8-bit mask field. For 16-bit (vec8),
404 * it's just one bit per channel, easy peasy. For 32-bit (vec4), it's one bit
405 * per channel with one duplicate bit in the middle. For 64-bit (vec2), it's
406 * one-bit per channel with _3_ duplicate bits in the middle. Basically, just
407 * subdividing the 128-bit word in 16-bit increments. For 64-bit, we uppercase
408 * the mask to make it obvious what happened */
409
410 static void
411 print_mask(uint8_t mask, unsigned bits, midgard_dest_override override)
412 {
413 if (bits == 8) {
414 print_mask_vec16(mask, override);
415 return;
416 }
417
418 /* Skip 'complete' masks */
419
420 if (bits >= 32 && mask == 0xFF) return;
421
422 if (bits == 16) {
423 if (mask == 0x0F)
424 return;
425 else if (mask == 0xF0) {
426 printf("'");
427 return;
428 }
429 }
430
431 printf(".");
432
433 unsigned skip = (bits / 16);
434 bool uppercase = bits > 32;
435 bool tripped = false;
436
437 for (unsigned i = 0; i < 8; i += skip) {
438 bool a = (mask & (1 << i)) != 0;
439
440 for (unsigned j = 1; j < skip; ++j) {
441 bool dupe = (mask & (1 << (i + j))) != 0;
442 tripped |= (dupe != a);
443 }
444
445 if (a) {
446 char c = components[i / skip];
447
448 if (uppercase)
449 c = toupper(c);
450
451 printf("%c", c);
452 }
453 }
454
455 if (tripped)
456 printf(" /* %X */", mask);
457 }
458
459 /* Prints the 4-bit masks found in texture and load/store ops, as opposed to
460 * the 8-bit masks found in (vector) ALU ops */
461
462 static void
463 print_mask_4(unsigned mask)
464 {
465 if (mask == 0xF) return;
466
467 printf(".");
468
469 for (unsigned i = 0; i < 4; ++i) {
470 bool a = (mask & (1 << i)) != 0;
471 if (a)
472 printf("%c", components[i]);
473 }
474 }
475
476 static void
477 print_vector_field(const char *name, uint16_t *words, uint16_t reg_word,
478 unsigned tabs)
479 {
480 midgard_reg_info *reg_info = (midgard_reg_info *)&reg_word;
481 midgard_vector_alu *alu_field = (midgard_vector_alu *) words;
482 midgard_reg_mode mode = alu_field->reg_mode;
483 unsigned override = alu_field->dest_override;
484
485 /* For now, prefix instruction names with their unit, until we
486 * understand how this works on a deeper level */
487 printf("%s.", name);
488
489 print_alu_opcode(alu_field->op);
490
491 /* Postfix with the size to disambiguate if necessary */
492 char postfix = prefix_for_bits(bits_for_mode(mode));
493 bool size_ambiguous = override != midgard_dest_override_none;
494
495 if (size_ambiguous)
496 printf("%c", postfix ? postfix : 'r');
497
498 /* Print the outmod, if there is one */
499 print_outmod(alu_field->outmod,
500 midgard_is_integer_out_op(alu_field->op));
501
502 printf(" ");
503
504 /* Mask denoting status of 8-lanes */
505 uint8_t mask = alu_field->mask;
506
507 /* First, print the destination */
508 unsigned dest_size =
509 print_dest(reg_info->out_reg, mode, alu_field->dest_override);
510
511 /* Apply the destination override to the mask */
512
513 if (mode == midgard_reg_mode_32 || mode == midgard_reg_mode_64) {
514 if (override == midgard_dest_override_lower)
515 mask &= 0x0F;
516 else if (override == midgard_dest_override_upper)
517 mask &= 0xF0;
518 } else if (mode == midgard_reg_mode_16
519 && override == midgard_dest_override_lower) {
520 /* stub */
521 }
522
523 if (override != midgard_dest_override_none) {
524 bool modeable = (mode != midgard_reg_mode_8);
525 bool known = override != 0x3; /* Unused value */
526
527 if (!(modeable && known))
528 printf("/* do%d */ ", override);
529 }
530
531 print_mask(mask, dest_size, override);
532
533 printf(", ");
534
535 bool is_int = midgard_is_integer_op(alu_field->op);
536 print_vector_src(alu_field->src1, mode, reg_info->src1_reg, override, is_int);
537
538 printf(", ");
539
540 if (reg_info->src2_imm) {
541 uint16_t imm = decode_vector_imm(reg_info->src2_reg, alu_field->src2 >> 2);
542 print_immediate(imm);
543 } else {
544 print_vector_src(alu_field->src2, mode,
545 reg_info->src2_reg, override, is_int);
546 }
547
548 nr_ins++;
549 printf("\n");
550 }
551
552 static void
553 print_scalar_src(unsigned src_binary, unsigned reg)
554 {
555 midgard_scalar_alu_src *src = (midgard_scalar_alu_src *)&src_binary;
556
557 if (src->negate)
558 printf("-");
559
560 if (src->abs)
561 printf("abs(");
562
563 print_reg(reg, src->full ? 32 : 16);
564
565 unsigned c = src->component;
566
567 if (src->full) {
568 assert((c & 1) == 0);
569 c >>= 1;
570 }
571
572 printf(".%c", components[c]);
573
574 if (src->abs)
575 printf(")");
576
577 }
578
579 static uint16_t
580 decode_scalar_imm(unsigned src2_reg, unsigned imm)
581 {
582 uint16_t ret;
583 ret = src2_reg << 11;
584 ret |= (imm & 3) << 9;
585 ret |= (imm & 4) << 6;
586 ret |= (imm & 0x38) << 2;
587 ret |= imm >> 6;
588 return ret;
589 }
590
591 static void
592 print_scalar_field(const char *name, uint16_t *words, uint16_t reg_word,
593 unsigned tabs)
594 {
595 midgard_reg_info *reg_info = (midgard_reg_info *)&reg_word;
596 midgard_scalar_alu *alu_field = (midgard_scalar_alu *) words;
597
598 if (alu_field->unknown)
599 printf("scalar ALU unknown bit set\n");
600
601 printf("%s.", name);
602 print_alu_opcode(alu_field->op);
603 print_outmod(alu_field->outmod,
604 midgard_is_integer_out_op(alu_field->op));
605 printf(" ");
606
607 bool full = alu_field->output_full;
608 print_reg(reg_info->out_reg, full ? 32 : 16);
609 unsigned c = alu_field->output_component;
610
611 if (full) {
612 assert((c & 1) == 0);
613 c >>= 1;
614 }
615
616 printf(".%c, ", components[c]);
617
618 print_scalar_src(alu_field->src1, reg_info->src1_reg);
619
620 printf(", ");
621
622 if (reg_info->src2_imm) {
623 uint16_t imm = decode_scalar_imm(reg_info->src2_reg,
624 alu_field->src2);
625 print_immediate(imm);
626 } else
627 print_scalar_src(alu_field->src2, reg_info->src2_reg);
628
629 nr_ins++;
630 printf("\n");
631 }
632
633 static void
634 print_branch_op(int op)
635 {
636 switch (op) {
637 case midgard_jmp_writeout_op_branch_uncond:
638 printf("uncond.");
639 break;
640
641 case midgard_jmp_writeout_op_branch_cond:
642 printf("cond.");
643 break;
644
645 case midgard_jmp_writeout_op_writeout:
646 printf("write.");
647 break;
648
649 case midgard_jmp_writeout_op_tilebuffer_pending:
650 printf("tilebuffer.");
651 break;
652
653 case midgard_jmp_writeout_op_discard:
654 printf("discard.");
655 break;
656
657 default:
658 printf("unk%d.", op);
659 break;
660 }
661 }
662
663 static void
664 print_branch_cond(int cond)
665 {
666 switch (cond) {
667 case midgard_condition_write0:
668 printf("write0");
669 break;
670
671 case midgard_condition_false:
672 printf("false");
673 break;
674
675 case midgard_condition_true:
676 printf("true");
677 break;
678
679 case midgard_condition_always:
680 printf("always");
681 break;
682
683 default:
684 printf("unk%X", cond);
685 break;
686 }
687 }
688
689 static void
690 print_compact_branch_writeout_field(uint16_t word)
691 {
692 midgard_jmp_writeout_op op = word & 0x7;
693
694 switch (op) {
695 case midgard_jmp_writeout_op_branch_uncond: {
696 midgard_branch_uncond br_uncond;
697 memcpy((char *) &br_uncond, (char *) &word, sizeof(br_uncond));
698 printf("br.uncond ");
699
700 if (br_uncond.unknown != 1)
701 printf("unknown:%d, ", br_uncond.unknown);
702
703 if (br_uncond.offset >= 0)
704 printf("+");
705
706 printf("%d -> ", br_uncond.offset);
707 print_tag_short(br_uncond.dest_tag);
708 printf("\n");
709
710 break;
711 }
712
713 case midgard_jmp_writeout_op_branch_cond:
714 case midgard_jmp_writeout_op_writeout:
715 case midgard_jmp_writeout_op_discard:
716 default: {
717 midgard_branch_cond br_cond;
718 memcpy((char *) &br_cond, (char *) &word, sizeof(br_cond));
719
720 printf("br.");
721
722 print_branch_op(br_cond.op);
723 print_branch_cond(br_cond.cond);
724
725 printf(" ");
726
727 if (br_cond.offset >= 0)
728 printf("+");
729
730 printf("%d -> ", br_cond.offset);
731 print_tag_short(br_cond.dest_tag);
732 printf("\n");
733
734 break;
735 }
736 }
737
738 nr_ins++;
739 }
740
741 static void
742 print_extended_branch_writeout_field(uint8_t *words)
743 {
744 midgard_branch_extended br;
745 memcpy((char *) &br, (char *) words, sizeof(br));
746
747 printf("brx.");
748
749 print_branch_op(br.op);
750
751 /* Condition codes are a LUT in the general case, but simply repeated 8 times for single-channel conditions.. Check this. */
752
753 bool single_channel = true;
754
755 for (unsigned i = 0; i < 16; i += 2) {
756 single_channel &= (((br.cond >> i) & 0x3) == (br.cond & 0x3));
757 }
758
759 if (single_channel)
760 print_branch_cond(br.cond & 0x3);
761 else
762 printf("lut%X", br.cond);
763
764 if (br.unknown)
765 printf(".unknown%d", br.unknown);
766
767 printf(" ");
768
769 if (br.offset >= 0)
770 printf("+");
771
772 printf("%d -> ", br.offset);
773 print_tag_short(br.dest_tag);
774 printf("\n");
775
776 nr_ins++;
777 }
778
779 static unsigned
780 num_alu_fields_enabled(uint32_t control_word)
781 {
782 unsigned ret = 0;
783
784 if ((control_word >> 17) & 1)
785 ret++;
786
787 if ((control_word >> 19) & 1)
788 ret++;
789
790 if ((control_word >> 21) & 1)
791 ret++;
792
793 if ((control_word >> 23) & 1)
794 ret++;
795
796 if ((control_word >> 25) & 1)
797 ret++;
798
799 return ret;
800 }
801
802 static float
803 float_bitcast(uint32_t integer)
804 {
805 union {
806 uint32_t i;
807 float f;
808 } v;
809
810 v.i = integer;
811 return v.f;
812 }
813
814 static void
815 print_alu_word(uint32_t *words, unsigned num_quad_words,
816 unsigned tabs)
817 {
818 uint32_t control_word = words[0];
819 uint16_t *beginning_ptr = (uint16_t *)(words + 1);
820 unsigned num_fields = num_alu_fields_enabled(control_word);
821 uint16_t *word_ptr = beginning_ptr + num_fields;
822 unsigned num_words = 2 + num_fields;
823
824 if ((control_word >> 16) & 1)
825 printf("unknown bit 16 enabled\n");
826
827 if ((control_word >> 17) & 1) {
828 print_vector_field("vmul", word_ptr, *beginning_ptr, tabs);
829 beginning_ptr += 1;
830 word_ptr += 3;
831 num_words += 3;
832 }
833
834 if ((control_word >> 18) & 1)
835 printf("unknown bit 18 enabled\n");
836
837 if ((control_word >> 19) & 1) {
838 print_scalar_field("sadd", word_ptr, *beginning_ptr, tabs);
839 beginning_ptr += 1;
840 word_ptr += 2;
841 num_words += 2;
842 }
843
844 if ((control_word >> 20) & 1)
845 printf("unknown bit 20 enabled\n");
846
847 if ((control_word >> 21) & 1) {
848 print_vector_field("vadd", word_ptr, *beginning_ptr, tabs);
849 beginning_ptr += 1;
850 word_ptr += 3;
851 num_words += 3;
852 }
853
854 if ((control_word >> 22) & 1)
855 printf("unknown bit 22 enabled\n");
856
857 if ((control_word >> 23) & 1) {
858 print_scalar_field("smul", word_ptr, *beginning_ptr, tabs);
859 beginning_ptr += 1;
860 word_ptr += 2;
861 num_words += 2;
862 }
863
864 if ((control_word >> 24) & 1)
865 printf("unknown bit 24 enabled\n");
866
867 if ((control_word >> 25) & 1) {
868 print_vector_field("lut", word_ptr, *beginning_ptr, tabs);
869 beginning_ptr += 1;
870 word_ptr += 3;
871 num_words += 3;
872 }
873
874 if ((control_word >> 26) & 1) {
875 print_compact_branch_writeout_field(*word_ptr);
876 word_ptr += 1;
877 num_words += 1;
878 }
879
880 if ((control_word >> 27) & 1) {
881 print_extended_branch_writeout_field((uint8_t *) word_ptr);
882 word_ptr += 3;
883 num_words += 3;
884 }
885
886 if (num_quad_words > (num_words + 7) / 8) {
887 assert(num_quad_words == (num_words + 15) / 8);
888 //Assume that the extra quadword is constants
889 void *consts = words + (4 * num_quad_words - 4);
890
891 if (is_embedded_constant_int) {
892 if (is_embedded_constant_half) {
893 int16_t *sconsts = (int16_t *) consts;
894 printf("sconstants %d, %d, %d, %d\n",
895 sconsts[0],
896 sconsts[1],
897 sconsts[2],
898 sconsts[3]);
899 } else {
900 int32_t *iconsts = (int32_t *) consts;
901 printf("iconstants %d, %d, %d, %d\n",
902 iconsts[0],
903 iconsts[1],
904 iconsts[2],
905 iconsts[3]);
906 }
907 } else {
908 if (is_embedded_constant_half) {
909 uint16_t *hconsts = (uint16_t *) consts;
910 printf("hconstants %g, %g, %g, %g\n",
911 _mesa_half_to_float(hconsts[0]),
912 _mesa_half_to_float(hconsts[1]),
913 _mesa_half_to_float(hconsts[2]),
914 _mesa_half_to_float(hconsts[3]));
915 } else {
916 uint32_t *fconsts = (uint32_t *) consts;
917 printf("fconstants %g, %g, %g, %g\n",
918 float_bitcast(fconsts[0]),
919 float_bitcast(fconsts[1]),
920 float_bitcast(fconsts[2]),
921 float_bitcast(fconsts[3]));
922 }
923
924 }
925 }
926 }
927
928 static void
929 print_varying_parameters(midgard_load_store_word *word)
930 {
931 midgard_varying_parameter param;
932 unsigned v = word->varying_parameters;
933 memcpy(&param, &v, sizeof(param));
934
935 if (param.is_varying) {
936 /* If a varying, there are qualifiers */
937 if (param.flat)
938 printf(".flat");
939
940 if (param.interpolation != midgard_interp_default) {
941 if (param.interpolation == midgard_interp_centroid)
942 printf(".centroid");
943 else
944 printf(".interp%d", param.interpolation);
945 }
946
947 if (param.modifier != midgard_varying_mod_none) {
948 if (param.modifier == midgard_varying_mod_perspective_w)
949 printf(".perspectivew");
950 else if (param.modifier == midgard_varying_mod_perspective_z)
951 printf(".perspectivez");
952 else
953 printf(".mod%d", param.modifier);
954 }
955 } else if (param.flat || param.interpolation || param.modifier) {
956 printf(" /* is_varying not set but varying metadata attached */");
957 }
958
959 if (param.zero0 || param.zero1 || param.zero2)
960 printf(" /* zero tripped, %d %d %d */ ", param.zero0, param.zero1, param.zero2);
961 }
962
963 static bool
964 is_op_varying(unsigned op)
965 {
966 switch (op) {
967 case midgard_op_st_vary_16:
968 case midgard_op_st_vary_32:
969 case midgard_op_st_vary_32i:
970 case midgard_op_st_vary_32u:
971 case midgard_op_ld_vary_16:
972 case midgard_op_ld_vary_32:
973 case midgard_op_ld_vary_32i:
974 case midgard_op_ld_vary_32u:
975 return true;
976 }
977
978 return false;
979 }
980
981 static void
982 print_load_store_arg(uint8_t arg, unsigned index)
983 {
984 /* Try to interpret as a register */
985 midgard_ldst_register_select sel;
986 memcpy(&sel, &arg, sizeof(arg));
987
988 /* If unknown is set, we're not sure what this is or how to
989 * interpret it. But if it's zero, we get it. */
990
991 if (sel.unknown) {
992 printf("0x%02X", arg);
993 return;
994 }
995
996 unsigned reg = REGISTER_LDST_BASE + sel.select;
997 char comp = components[sel.component];
998
999 printf("r%d.%c", reg, comp);
1000
1001 /* Only print a shift if it's non-zero. Shifts only make sense for the
1002 * second index. For the first, we're not sure what it means yet */
1003
1004 if (index == 1) {
1005 if (sel.shift)
1006 printf(" << %d", sel.shift);
1007 } else {
1008 printf(" /* %X */", sel.shift);
1009 }
1010 }
1011
1012 static void
1013 print_load_store_instr(uint64_t data,
1014 unsigned tabs)
1015 {
1016 midgard_load_store_word *word = (midgard_load_store_word *) &data;
1017
1018 print_ld_st_opcode(word->op);
1019
1020 if (is_op_varying(word->op))
1021 print_varying_parameters(word);
1022
1023 printf(" r%d", word->reg);
1024 print_mask_4(word->mask);
1025
1026 int address = word->address;
1027
1028 if (word->op == midgard_op_ld_uniform_32) {
1029 /* Uniforms use their own addressing scheme */
1030
1031 int lo = word->varying_parameters >> 7;
1032 int hi = word->address;
1033
1034 /* TODO: Combine fields logically */
1035 address = (hi << 3) | lo;
1036 }
1037
1038 printf(", %d", address);
1039
1040 print_swizzle_vec4(word->swizzle, false, false);
1041
1042 printf(", ");
1043 print_load_store_arg(word->arg_1, 0);
1044 printf(", ");
1045 print_load_store_arg(word->arg_2, 1);
1046 printf(" /* %X */\n", word->varying_parameters);
1047
1048 nr_ins++;
1049 }
1050
1051 static void
1052 print_load_store_word(uint32_t *word, unsigned tabs)
1053 {
1054 midgard_load_store *load_store = (midgard_load_store *) word;
1055
1056 if (load_store->word1 != 3) {
1057 print_load_store_instr(load_store->word1, tabs);
1058 }
1059
1060 if (load_store->word2 != 3) {
1061 print_load_store_instr(load_store->word2, tabs);
1062 }
1063 }
1064
1065 static void
1066 print_texture_reg(bool full, bool select, bool upper)
1067 {
1068 if (full)
1069 printf("r%d", REG_TEX_BASE + select);
1070 else
1071 printf("hr%d", (REG_TEX_BASE + select) * 2 + upper);
1072
1073 if (full && upper)
1074 printf("// error: out full / upper mutually exclusive\n");
1075
1076 }
1077
1078 static void
1079 print_texture_reg_triple(unsigned triple)
1080 {
1081 bool full = triple & 1;
1082 bool select = triple & 2;
1083 bool upper = triple & 4;
1084
1085 print_texture_reg(full, select, upper);
1086 }
1087
1088 static void
1089 print_texture_format(int format)
1090 {
1091 /* Act like a modifier */
1092 printf(".");
1093
1094 switch (format) {
1095 DEFINE_CASE(MALI_TEX_1D, "1d");
1096 DEFINE_CASE(MALI_TEX_2D, "2d");
1097 DEFINE_CASE(MALI_TEX_3D, "3d");
1098 DEFINE_CASE(MALI_TEX_CUBE, "cube");
1099
1100 default:
1101 unreachable("Bad format");
1102 }
1103 }
1104
1105 static void
1106 print_texture_op(unsigned op, bool gather)
1107 {
1108 /* Act like a bare name, like ESSL functions */
1109
1110 if (gather) {
1111 printf("textureGather");
1112
1113 unsigned component = op >> 4;
1114 unsigned bottom = op & 0xF;
1115
1116 if (bottom != 0x2)
1117 printf("_unk%d", bottom);
1118
1119 printf(".%c", components[component]);
1120 return;
1121 }
1122
1123 switch (op) {
1124 DEFINE_CASE(TEXTURE_OP_NORMAL, "texture");
1125 DEFINE_CASE(TEXTURE_OP_LOD, "textureLod");
1126 DEFINE_CASE(TEXTURE_OP_TEXEL_FETCH, "texelFetch");
1127 DEFINE_CASE(TEXTURE_OP_DFDX, "dFdx");
1128 DEFINE_CASE(TEXTURE_OP_DFDY, "dFdy");
1129
1130 default:
1131 printf("tex_%X", op);
1132 break;
1133 }
1134 }
1135
1136 static bool
1137 texture_op_takes_bias(unsigned op)
1138 {
1139 return op == TEXTURE_OP_NORMAL;
1140 }
1141
1142 static char
1143 sampler_type_name(enum mali_sampler_type t)
1144 {
1145 switch (t) {
1146 case MALI_SAMPLER_FLOAT:
1147 return 'f';
1148 case MALI_SAMPLER_UNSIGNED:
1149 return 'u';
1150 case MALI_SAMPLER_SIGNED:
1151 return 'i';
1152 default:
1153 return '?';
1154 }
1155
1156 }
1157
1158 #undef DEFINE_CASE
1159
1160 static void
1161 print_texture_word(uint32_t *word, unsigned tabs)
1162 {
1163 midgard_texture_word *texture = (midgard_texture_word *) word;
1164
1165 /* Broad category of texture operation in question */
1166 print_texture_op(texture->op, texture->is_gather);
1167
1168 /* Specific format in question */
1169 print_texture_format(texture->format);
1170
1171 assert(texture->zero == 0);
1172
1173 /* Instruction "modifiers" parallel the ALU instructions. */
1174
1175 if (texture->shadow)
1176 printf(".shadow");
1177
1178 if (texture->cont)
1179 printf(".cont");
1180
1181 if (texture->last)
1182 printf(".last");
1183
1184 /* Output modifiers are always interpreted floatly */
1185 print_outmod(texture->outmod, false);
1186
1187 printf(" ");
1188
1189 print_texture_reg(texture->out_full, texture->out_reg_select, texture->out_upper);
1190 print_mask_4(texture->mask);
1191 printf(", ");
1192
1193 printf("texture%d, ", texture->texture_handle);
1194
1195 /* Print the type, GL style */
1196 printf("%c", sampler_type_name(texture->sampler_type));
1197 printf("sampler%d", texture->sampler_handle);
1198 print_swizzle_vec4(texture->swizzle, false, false);
1199 printf(", ");
1200
1201 print_texture_reg(texture->in_reg_full, texture->in_reg_select, texture->in_reg_upper);
1202 print_swizzle_vec4(texture->in_reg_swizzle, false, false);
1203
1204 /* There is *always* an offset attached. Of
1205 * course, that offset is just immediate #0 for a
1206 * GLES call that doesn't take an offset. If there
1207 * is a non-negative non-zero offset, this is
1208 * specified in immediate offset mode, with the
1209 * values in the offset_* fields as immediates. If
1210 * this is a negative offset, we instead switch to
1211 * a register offset mode, where the offset_*
1212 * fields become register triplets */
1213
1214 if (texture->offset_register) {
1215 printf(" + ");
1216 print_texture_reg_triple(texture->offset_x);
1217
1218 /* The less questions you ask, the better. */
1219
1220 unsigned swizzle_lo, swizzle_hi;
1221 unsigned orig_y = texture->offset_y;
1222 unsigned orig_z = texture->offset_z;
1223
1224 memcpy(&swizzle_lo, &orig_y, sizeof(unsigned));
1225 memcpy(&swizzle_hi, &orig_z, sizeof(unsigned));
1226
1227 /* Duplicate hi swizzle over */
1228 assert(swizzle_hi < 4);
1229 swizzle_hi = (swizzle_hi << 2) | swizzle_hi;
1230
1231 unsigned swiz = (swizzle_lo << 4) | swizzle_hi;
1232 unsigned reversed = util_bitreverse(swiz) >> 24;
1233 print_swizzle_vec4(reversed, false, false);
1234
1235 printf(", ");
1236 } else if (texture->offset_x || texture->offset_y || texture->offset_z) {
1237 /* Only select ops allow negative immediate offsets, verify */
1238
1239 bool neg_x = texture->offset_x < 0;
1240 bool neg_y = texture->offset_y < 0;
1241 bool neg_z = texture->offset_z < 0;
1242 bool any_neg = neg_x || neg_y || neg_z;
1243
1244 if (any_neg && texture->op != TEXTURE_OP_TEXEL_FETCH)
1245 printf("/* invalid negative */ ");
1246
1247 /* Regardless, just print the immediate offset */
1248
1249 printf(" + <%d, %d, %d>, ",
1250 texture->offset_x,
1251 texture->offset_y,
1252 texture->offset_z);
1253 } else {
1254 printf(", ");
1255 }
1256
1257 char lod_operand = texture_op_takes_bias(texture->op) ? '+' : '=';
1258
1259 if (texture->lod_register) {
1260 midgard_tex_register_select sel;
1261 uint8_t raw = texture->bias;
1262 memcpy(&sel, &raw, sizeof(raw));
1263
1264 printf("lod %c ", lod_operand);
1265 print_texture_reg(sel.full, sel.select, sel.upper);
1266 printf(".%c, ", components[sel.component]);
1267
1268 if (texture->bias_int)
1269 printf(" /* bias_int = 0x%X */", texture->bias_int);
1270
1271 if (sel.zero)
1272 printf(" /* sel.zero = 0x%X */", sel.zero);
1273 } else if (texture->op == TEXTURE_OP_TEXEL_FETCH) {
1274 /* For texel fetch, the int LOD is in the fractional place and
1275 * there is no fraction / possibility of bias. We *always* have
1276 * an explicit LOD, even if it's zero. */
1277
1278 if (texture->bias_int)
1279 printf(" /* bias_int = 0x%X */ ", texture->bias_int);
1280
1281 printf("lod = %d, ", texture->bias);
1282 } else if (texture->bias || texture->bias_int) {
1283 signed bias_int = texture->bias_int;
1284 float bias_frac = texture->bias / 256.0f;
1285 float bias = bias_int + bias_frac;
1286
1287 bool is_bias = texture_op_takes_bias(texture->op);
1288 char sign = (bias >= 0.0) ? '+' : '-';
1289 char operand = is_bias ? sign : '=';
1290
1291 printf("lod %c %f, ", operand, fabsf(bias));
1292 }
1293
1294 printf("\n");
1295
1296 /* While not zero in general, for these simple instructions the
1297 * following unknowns are zero, so we don't include them */
1298
1299 if (texture->unknown4 ||
1300 texture->unknownA ||
1301 texture->unknown8) {
1302 printf("// unknown4 = 0x%x\n", texture->unknown4);
1303 printf("// unknownA = 0x%x\n", texture->unknownA);
1304 printf("// unknown8 = 0x%x\n", texture->unknown8);
1305 }
1306
1307 nr_ins++;
1308 }
1309
1310 void
1311 disassemble_midgard(uint8_t *code, size_t size, bool stats, unsigned nr_registers, const char *prefix)
1312 {
1313 uint32_t *words = (uint32_t *) code;
1314 unsigned num_words = size / 4;
1315 int tabs = 0;
1316
1317 bool prefetch_flag = false;
1318
1319 int last_next_tag = -1;
1320
1321 unsigned i = 0;
1322
1323 /* Stats for shader-db */
1324 unsigned nr_bundles = 0;
1325 unsigned nr_quadwords = 0;
1326 nr_ins = 0;
1327
1328 while (i < num_words) {
1329 unsigned tag = words[i] & 0xF;
1330 unsigned next_tag = (words[i] >> 4) & 0xF;
1331 unsigned num_quad_words = midgard_word_size[tag];
1332
1333 /* Check the tag */
1334 if (last_next_tag > 1) {
1335 if (last_next_tag != tag) {
1336 printf("/* TAG ERROR got ");
1337 print_tag_short(tag);
1338 printf(" expected ");
1339 print_tag_short(last_next_tag);
1340 printf(" */ ");
1341 }
1342 } else {
1343 /* TODO: Check ALU case */
1344 }
1345
1346 last_next_tag = next_tag;
1347
1348 switch (midgard_word_types[tag]) {
1349 case midgard_word_type_texture:
1350 print_texture_word(&words[i], tabs);
1351 break;
1352
1353 case midgard_word_type_load_store:
1354 print_load_store_word(&words[i], tabs);
1355 break;
1356
1357 case midgard_word_type_alu:
1358 print_alu_word(&words[i], num_quad_words, tabs);
1359
1360 /* Reset word static analysis state */
1361 is_embedded_constant_half = false;
1362 is_embedded_constant_int = false;
1363
1364 break;
1365
1366 default:
1367 printf("Unknown word type %u:\n", words[i] & 0xF);
1368 num_quad_words = 1;
1369 print_quad_word(&words[i], tabs);
1370 printf("\n");
1371 break;
1372 }
1373
1374 if (prefetch_flag && midgard_word_types[tag] == midgard_word_type_alu)
1375 break;
1376
1377 printf("\n");
1378
1379 unsigned next = (words[i] & 0xF0) >> 4;
1380
1381 i += 4 * num_quad_words;
1382
1383 /* We are parsing per bundle anyway */
1384 nr_bundles++;
1385 nr_quadwords += num_quad_words;
1386
1387 /* Break based on instruction prefetch flag */
1388
1389 if (i < num_words && next == 1) {
1390 prefetch_flag = true;
1391
1392 if (midgard_word_types[words[i] & 0xF] != midgard_word_type_alu)
1393 break;
1394 }
1395 }
1396
1397 if (stats) {
1398 unsigned nr_threads =
1399 (nr_registers <= 4) ? 4 :
1400 (nr_registers <= 8) ? 2 :
1401 1;
1402
1403 printf("%s"
1404 "%u inst, %u bundles, %u quadwords, "
1405 "%u registers, %u threads, 0 loops\n",
1406 prefix,
1407 nr_ins, nr_bundles, nr_quadwords,
1408 nr_registers, nr_threads);
1409
1410 }
1411 }