panfrost/midgard: Identify the in_reg_full field
[mesa.git] / src / gallium / drivers / 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
40 #define DEFINE_CASE(define, str) case define: { printf(str); break; }
41
42 static bool is_instruction_int = false;
43
44 /* Prints a short form of the tag for branching, the minimum needed to be
45 * legible and unambiguous */
46
47 static void
48 print_tag_short(unsigned tag)
49 {
50 switch (midgard_word_types[tag]) {
51 case midgard_word_type_texture:
52 printf("tex/%X", tag);
53 break;
54
55 case midgard_word_type_load_store:
56 printf("ldst");
57 break;
58
59 case midgard_word_type_alu:
60 printf("alu%d/%X", midgard_word_size[tag], tag);
61 break;
62
63 default:
64 printf("%s%X", (tag > 0) ? "" : "unk", tag);
65 break;
66 }
67 }
68
69 static void
70 print_alu_opcode(midgard_alu_op op)
71 {
72 bool int_op = false;
73
74 if (alu_opcode_props[op].name) {
75 printf("%s", alu_opcode_props[op].name);
76
77 int_op = midgard_is_integer_op(op);
78 } else
79 printf("alu_op_%02X", op);
80
81 /* For constant analysis */
82 is_instruction_int = int_op;
83 }
84
85 static void
86 print_ld_st_opcode(midgard_load_store_op op)
87 {
88 if (load_store_opcode_names[op])
89 printf("%s", load_store_opcode_names[op]);
90 else
91 printf("ldst_op_%02X", op);
92 }
93
94 static bool is_embedded_constant_half = false;
95 static bool is_embedded_constant_int = false;
96
97 static char
98 prefix_for_bits(unsigned bits)
99 {
100 switch (bits) {
101 case 8:
102 return 'q';
103 case 16:
104 return 'h';
105 case 64:
106 return 'd';
107 default:
108 return 0;
109 }
110 }
111
112 static void
113 print_reg(unsigned reg, unsigned bits)
114 {
115 /* Perform basic static analysis for expanding constants correctly */
116
117 if (reg == 26) {
118 is_embedded_constant_int = is_instruction_int;
119 is_embedded_constant_half = (bits < 32);
120 }
121
122 char prefix = prefix_for_bits(bits);
123
124 if (prefix)
125 putchar(prefix);
126
127 printf("r%u", reg);
128 }
129
130 static char *outmod_names_float[4] = {
131 "",
132 ".pos",
133 ".unk2",
134 ".sat"
135 };
136
137 static char *outmod_names_int[4] = {
138 ".isat",
139 ".usat",
140 "",
141 ".hi"
142 };
143
144 static char *srcmod_names_int[4] = {
145 "sext(",
146 "zext(",
147 "",
148 "("
149 };
150
151 static void
152 print_outmod(unsigned outmod, bool is_int)
153 {
154 printf("%s", is_int ? outmod_names_int[outmod] :
155 outmod_names_float[outmod]);
156 }
157
158 static void
159 print_quad_word(uint32_t *words, unsigned tabs)
160 {
161 unsigned i;
162
163 for (i = 0; i < 4; i++)
164 printf("0x%08X%s ", words[i], i == 3 ? "" : ",");
165
166 printf("\n");
167 }
168
169 static const char components[16] = "xyzwefghijklmnop";
170
171 /* Helper to print 4 chars of a swizzle */
172 static void
173 print_swizzle_helper(unsigned swizzle, bool upper)
174 {
175 for (unsigned i = 0; i < 4; ++i) {
176 unsigned c = (swizzle >> (i * 2)) & 3;
177 c += upper*4;
178 printf("%c", components[c]);
179 }
180 }
181
182 /* Helper to print 8 chars of a swizzle, duplicating over */
183 static void
184 print_swizzle_helper_8(unsigned swizzle, bool upper)
185 {
186 for (unsigned i = 0; i < 4; ++i) {
187 unsigned c = (swizzle >> (i * 2)) & 3;
188 c *= 2;
189 c += upper*8;
190 printf("%c%c", components[c], components[c+1]);
191 }
192 }
193
194 static void
195 print_swizzle_vec16(unsigned swizzle, bool rep_high, bool rep_low,
196 midgard_dest_override override)
197 {
198 printf(".");
199
200 if (override == midgard_dest_override_upper) {
201 if (rep_high)
202 printf(" /* rep_high */ ");
203 if (rep_low)
204 printf(" /* rep_low */ ");
205
206 if (!rep_high && rep_low)
207 print_swizzle_helper_8(swizzle, true);
208 else
209 print_swizzle_helper_8(swizzle, false);
210 } else {
211 print_swizzle_helper_8(swizzle, rep_high & 1);
212 print_swizzle_helper_8(swizzle, !rep_low & 1);
213 }
214 }
215
216 static void
217 print_swizzle_vec8(unsigned swizzle, bool rep_high, bool rep_low)
218 {
219 printf(".");
220
221 print_swizzle_helper(swizzle, rep_high & 1);
222 print_swizzle_helper(swizzle, !rep_low & 1);
223 }
224
225 static void
226 print_swizzle_vec4(unsigned swizzle, bool rep_high, bool rep_low)
227 {
228 if (rep_high)
229 printf(" /* rep_high */ ");
230 if (rep_low)
231 printf(" /* rep_low */ ");
232
233 if (swizzle == 0xE4) return; /* xyzw */
234
235 printf(".");
236 print_swizzle_helper(swizzle, 0);
237 }
238 static void
239 print_swizzle_vec2(unsigned swizzle, bool rep_high, bool rep_low)
240 {
241 if (rep_high)
242 printf(" /* rep_high */ ");
243 if (rep_low)
244 printf(" /* rep_low */ ");
245
246 if (swizzle == 0xE4) return; /* XY */
247
248 printf(".");
249
250 for (unsigned i = 0; i < 4; i += 2) {
251 unsigned a = (swizzle >> (i * 2)) & 3;
252 unsigned b = (swizzle >> ((i+1) * 2)) & 3;
253
254 /* Normally we're adjacent, but if there's an issue, don't make
255 * it ambiguous */
256
257 if (a & 0x1)
258 printf("[%c%c]", components[a], components[b]);
259 else if (a == b)
260 printf("%c", components[a >> 1]);
261 else if (b == (a + 1))
262 printf("%c", "XY"[a >> 1]);
263 else
264 printf("[%c%c]", components[a], components[b]);
265 }
266 }
267
268 static int
269 bits_for_mode(midgard_reg_mode mode)
270 {
271 switch (mode) {
272 case midgard_reg_mode_8:
273 return 8;
274 case midgard_reg_mode_16:
275 return 16;
276 case midgard_reg_mode_32:
277 return 32;
278 case midgard_reg_mode_64:
279 return 64;
280 default:
281 return 0;
282 }
283 }
284
285 static int
286 bits_for_mode_halved(midgard_reg_mode mode, bool half)
287 {
288 unsigned bits = bits_for_mode(mode);
289
290 if (half)
291 bits >>= 1;
292
293 return bits;
294 }
295
296 static void
297 print_vector_src(unsigned src_binary,
298 midgard_reg_mode mode, unsigned reg,
299 midgard_dest_override override, bool is_int)
300 {
301 midgard_vector_alu_src *src = (midgard_vector_alu_src *)&src_binary;
302
303 /* Modifiers change meaning depending on the op's context */
304
305 midgard_int_mod int_mod = src->mod;
306
307 if (is_int) {
308 printf("%s", srcmod_names_int[int_mod]);
309 } else {
310 if (src->mod & MIDGARD_FLOAT_MOD_NEG)
311 printf("-");
312
313 if (src->mod & MIDGARD_FLOAT_MOD_ABS)
314 printf("abs(");
315 }
316
317 //register
318 unsigned bits = bits_for_mode_halved(mode, src->half);
319 print_reg(reg, bits);
320
321 //swizzle
322 if (bits == 16)
323 print_swizzle_vec8(src->swizzle, src->rep_high, src->rep_low);
324 else if (bits == 8)
325 print_swizzle_vec16(src->swizzle, src->rep_high, src->rep_low, override);
326 else if (bits == 32)
327 print_swizzle_vec4(src->swizzle, src->rep_high, src->rep_low);
328 else if (bits == 64)
329 print_swizzle_vec2(src->swizzle, src->rep_high, src->rep_low);
330
331 /* Since we wrapped with a function-looking thing */
332
333 if (is_int && int_mod == midgard_int_shift)
334 printf(") << %d", bits);
335 else if ((is_int && (int_mod != midgard_int_normal))
336 || (!is_int && src->mod & MIDGARD_FLOAT_MOD_ABS))
337 printf(")");
338 }
339
340 static uint16_t
341 decode_vector_imm(unsigned src2_reg, unsigned imm)
342 {
343 uint16_t ret;
344 ret = src2_reg << 11;
345 ret |= (imm & 0x7) << 8;
346 ret |= (imm >> 3) & 0xFF;
347 return ret;
348 }
349
350 static void
351 print_immediate(uint16_t imm)
352 {
353 if (is_instruction_int)
354 printf("#%d", imm);
355 else
356 printf("#%g", _mesa_half_to_float(imm));
357 }
358
359 static unsigned
360 print_dest(unsigned reg, midgard_reg_mode mode, midgard_dest_override override)
361 {
362 /* Depending on the mode and override, we determine the type of
363 * destination addressed. Absent an override, we address just the
364 * type of the operation itself */
365
366 unsigned bits = bits_for_mode(mode);
367
368 if (override != midgard_dest_override_none)
369 bits /= 2;
370
371 print_reg(reg, bits);
372
373 return bits;
374 }
375
376 static void
377 print_mask_vec16(uint8_t mask, midgard_dest_override override)
378 {
379 printf(".");
380
381 if (override == midgard_dest_override_none) {
382 for (unsigned i = 0; i < 8; i++) {
383 if (mask & (1 << i))
384 printf("%c%c",
385 components[i*2 + 0],
386 components[i*2 + 1]);
387 }
388 } else {
389 bool upper = (override == midgard_dest_override_upper);
390
391 for (unsigned i = 0; i < 8; i++) {
392 if (mask & (1 << i))
393 printf("%c", components[i + (upper ? 8 : 0)]);
394 }
395 }
396 }
397
398 /* For 16-bit+ masks, we read off from the 8-bit mask field. For 16-bit (vec8),
399 * it's just one bit per channel, easy peasy. For 32-bit (vec4), it's one bit
400 * per channel with one duplicate bit in the middle. For 64-bit (vec2), it's
401 * one-bit per channel with _3_ duplicate bits in the middle. Basically, just
402 * subdividing the 128-bit word in 16-bit increments. For 64-bit, we uppercase
403 * the mask to make it obvious what happened */
404
405 static void
406 print_mask(uint8_t mask, unsigned bits, midgard_dest_override override)
407 {
408 if (bits == 8) {
409 print_mask_vec16(mask, override);
410 return;
411 }
412
413 /* Skip 'complete' masks */
414
415 if (bits >= 32 && mask == 0xFF) return;
416
417 if (bits == 16) {
418 if (mask == 0x0F)
419 return;
420 else if (mask == 0xF0) {
421 printf("'");
422 return;
423 }
424 }
425
426 printf(".");
427
428 unsigned skip = (bits / 16);
429 bool uppercase = bits > 32;
430 bool tripped = false;
431
432 for (unsigned i = 0; i < 8; i += skip) {
433 bool a = (mask & (1 << i)) != 0;
434
435 for (unsigned j = 1; j < skip; ++j) {
436 bool dupe = (mask & (1 << (i + j))) != 0;
437 tripped |= (dupe != a);
438 }
439
440 if (a) {
441 char c = components[i / skip];
442
443 if (uppercase)
444 c = toupper(c);
445
446 printf("%c", c);
447 }
448 }
449
450 if (tripped)
451 printf(" /* %X */", mask);
452 }
453
454 static void
455 print_mask_4(unsigned mask)
456 {
457 printf(".");
458
459 for (unsigned i = 0; i < 4; ++i) {
460 bool a = (mask & (1 << i)) != 0;
461 if (a)
462 printf("%c", components[i]);
463 }
464 }
465
466 static void
467 print_vector_field(const char *name, uint16_t *words, uint16_t reg_word,
468 unsigned tabs)
469 {
470 midgard_reg_info *reg_info = (midgard_reg_info *)&reg_word;
471 midgard_vector_alu *alu_field = (midgard_vector_alu *) words;
472 midgard_reg_mode mode = alu_field->reg_mode;
473 unsigned override = alu_field->dest_override;
474
475 /* For now, prefix instruction names with their unit, until we
476 * understand how this works on a deeper level */
477 printf("%s.", name);
478
479 print_alu_opcode(alu_field->op);
480
481 /* Postfix with the size to disambiguate if necessary */
482 char postfix = prefix_for_bits(bits_for_mode(mode));
483 bool size_ambiguous = override != midgard_dest_override_none;
484
485 if (size_ambiguous)
486 printf("%c", postfix ? postfix : 'r');
487
488 /* Print the outmod, if there is one */
489 print_outmod(alu_field->outmod,
490 midgard_is_integer_out_op(alu_field->op));
491
492 printf(" ");
493
494 /* Mask denoting status of 8-lanes */
495 uint8_t mask = alu_field->mask;
496
497 /* First, print the destination */
498 unsigned dest_size =
499 print_dest(reg_info->out_reg, mode, alu_field->dest_override);
500
501 /* Apply the destination override to the mask */
502
503 if (mode == midgard_reg_mode_32 || mode == midgard_reg_mode_64) {
504 if (override == midgard_dest_override_lower)
505 mask &= 0x0F;
506 else if (override == midgard_dest_override_upper)
507 mask &= 0xF0;
508 } else if (mode == midgard_reg_mode_16
509 && override == midgard_dest_override_lower) {
510 /* stub */
511 }
512
513 if (override != midgard_dest_override_none) {
514 bool modeable = (mode != midgard_reg_mode_8);
515 bool known = override != 0x3; /* Unused value */
516
517 if (!(modeable && known))
518 printf("/* do%d */ ", override);
519 }
520
521 print_mask(mask, dest_size, override);
522
523 printf(", ");
524
525 bool is_int = midgard_is_integer_op(alu_field->op);
526 print_vector_src(alu_field->src1, mode, reg_info->src1_reg, override, is_int);
527
528 printf(", ");
529
530 if (reg_info->src2_imm) {
531 uint16_t imm = decode_vector_imm(reg_info->src2_reg, alu_field->src2 >> 2);
532 print_immediate(imm);
533 } else {
534 print_vector_src(alu_field->src2, mode,
535 reg_info->src2_reg, override, is_int);
536 }
537
538 printf("\n");
539 }
540
541 static void
542 print_scalar_src(unsigned src_binary, unsigned reg)
543 {
544 midgard_scalar_alu_src *src = (midgard_scalar_alu_src *)&src_binary;
545
546 if (src->negate)
547 printf("-");
548
549 if (src->abs)
550 printf("abs(");
551
552 print_reg(reg, src->full ? 32 : 16);
553
554 unsigned c = src->component;
555
556 if (src->full) {
557 assert((c & 1) == 0);
558 c >>= 1;
559 }
560
561 printf(".%c", components[c]);
562
563 if (src->abs)
564 printf(")");
565
566 }
567
568 static uint16_t
569 decode_scalar_imm(unsigned src2_reg, unsigned imm)
570 {
571 uint16_t ret;
572 ret = src2_reg << 11;
573 ret |= (imm & 3) << 9;
574 ret |= (imm & 4) << 6;
575 ret |= (imm & 0x38) << 2;
576 ret |= imm >> 6;
577 return ret;
578 }
579
580 static void
581 print_scalar_field(const char *name, uint16_t *words, uint16_t reg_word,
582 unsigned tabs)
583 {
584 midgard_reg_info *reg_info = (midgard_reg_info *)&reg_word;
585 midgard_scalar_alu *alu_field = (midgard_scalar_alu *) words;
586
587 if (alu_field->unknown)
588 printf("scalar ALU unknown bit set\n");
589
590 printf("%s.", name);
591 print_alu_opcode(alu_field->op);
592 print_outmod(alu_field->outmod,
593 midgard_is_integer_out_op(alu_field->op));
594 printf(" ");
595
596 bool full = alu_field->output_full;
597 print_reg(reg_info->out_reg, full ? 32 : 16);
598 unsigned c = alu_field->output_component;
599
600 if (full) {
601 assert((c & 1) == 0);
602 c >>= 1;
603 }
604
605 printf(".%c, ", components[c]);
606
607 print_scalar_src(alu_field->src1, reg_info->src1_reg);
608
609 printf(", ");
610
611 if (reg_info->src2_imm) {
612 uint16_t imm = decode_scalar_imm(reg_info->src2_reg,
613 alu_field->src2);
614 print_immediate(imm);
615 } else
616 print_scalar_src(alu_field->src2, reg_info->src2_reg);
617
618 printf("\n");
619 }
620
621 static void
622 print_branch_op(int op)
623 {
624 switch (op) {
625 case midgard_jmp_writeout_op_branch_uncond:
626 printf("uncond.");
627 break;
628
629 case midgard_jmp_writeout_op_branch_cond:
630 printf("cond.");
631 break;
632
633 case midgard_jmp_writeout_op_writeout:
634 printf("write.");
635 break;
636
637 case midgard_jmp_writeout_op_tilebuffer_pending:
638 printf("tilebuffer.");
639 break;
640
641 case midgard_jmp_writeout_op_discard:
642 printf("discard.");
643 break;
644
645 default:
646 printf("unk%d.", op);
647 break;
648 }
649 }
650
651 static void
652 print_branch_cond(int cond)
653 {
654 switch (cond) {
655 case midgard_condition_write0:
656 printf("write0");
657 break;
658
659 case midgard_condition_false:
660 printf("false");
661 break;
662
663 case midgard_condition_true:
664 printf("true");
665 break;
666
667 case midgard_condition_always:
668 printf("always");
669 break;
670
671 default:
672 printf("unk%X", cond);
673 break;
674 }
675 }
676
677 static void
678 print_compact_branch_writeout_field(uint16_t word)
679 {
680 midgard_jmp_writeout_op op = word & 0x7;
681
682 switch (op) {
683 case midgard_jmp_writeout_op_branch_uncond: {
684 midgard_branch_uncond br_uncond;
685 memcpy((char *) &br_uncond, (char *) &word, sizeof(br_uncond));
686 printf("br.uncond ");
687
688 if (br_uncond.unknown != 1)
689 printf("unknown:%d, ", br_uncond.unknown);
690
691 if (br_uncond.offset >= 0)
692 printf("+");
693
694 printf("%d -> ", br_uncond.offset);
695 print_tag_short(br_uncond.dest_tag);
696 printf("\n");
697
698 break;
699 }
700
701 case midgard_jmp_writeout_op_branch_cond:
702 case midgard_jmp_writeout_op_writeout:
703 case midgard_jmp_writeout_op_discard:
704 default: {
705 midgard_branch_cond br_cond;
706 memcpy((char *) &br_cond, (char *) &word, sizeof(br_cond));
707
708 printf("br.");
709
710 print_branch_op(br_cond.op);
711 print_branch_cond(br_cond.cond);
712
713 printf(" ");
714
715 if (br_cond.offset >= 0)
716 printf("+");
717
718 printf("%d -> ", br_cond.offset);
719 print_tag_short(br_cond.dest_tag);
720 printf("\n");
721
722 break;
723 }
724 }
725 }
726
727 static void
728 print_extended_branch_writeout_field(uint8_t *words)
729 {
730 midgard_branch_extended br;
731 memcpy((char *) &br, (char *) words, sizeof(br));
732
733 printf("brx.");
734
735 print_branch_op(br.op);
736
737 /* Condition repeated 8 times in all known cases. Check this. */
738
739 unsigned cond = br.cond & 0x3;
740
741 for (unsigned i = 0; i < 16; i += 2) {
742 assert(((br.cond >> i) & 0x3) == cond);
743 }
744
745 print_branch_cond(cond);
746
747 if (br.unknown)
748 printf(".unknown%d", br.unknown);
749
750 printf(" ");
751
752 if (br.offset >= 0)
753 printf("+");
754
755 printf("%d -> ", br.offset);
756 print_tag_short(br.dest_tag);
757 printf("\n");
758 }
759
760 static unsigned
761 num_alu_fields_enabled(uint32_t control_word)
762 {
763 unsigned ret = 0;
764
765 if ((control_word >> 17) & 1)
766 ret++;
767
768 if ((control_word >> 19) & 1)
769 ret++;
770
771 if ((control_word >> 21) & 1)
772 ret++;
773
774 if ((control_word >> 23) & 1)
775 ret++;
776
777 if ((control_word >> 25) & 1)
778 ret++;
779
780 return ret;
781 }
782
783 static float
784 float_bitcast(uint32_t integer)
785 {
786 union {
787 uint32_t i;
788 float f;
789 } v;
790
791 v.i = integer;
792 return v.f;
793 }
794
795 static void
796 print_alu_word(uint32_t *words, unsigned num_quad_words,
797 unsigned tabs)
798 {
799 uint32_t control_word = words[0];
800 uint16_t *beginning_ptr = (uint16_t *)(words + 1);
801 unsigned num_fields = num_alu_fields_enabled(control_word);
802 uint16_t *word_ptr = beginning_ptr + num_fields;
803 unsigned num_words = 2 + num_fields;
804
805 if ((control_word >> 16) & 1)
806 printf("unknown bit 16 enabled\n");
807
808 if ((control_word >> 17) & 1) {
809 print_vector_field("vmul", word_ptr, *beginning_ptr, tabs);
810 beginning_ptr += 1;
811 word_ptr += 3;
812 num_words += 3;
813 }
814
815 if ((control_word >> 18) & 1)
816 printf("unknown bit 18 enabled\n");
817
818 if ((control_word >> 19) & 1) {
819 print_scalar_field("sadd", word_ptr, *beginning_ptr, tabs);
820 beginning_ptr += 1;
821 word_ptr += 2;
822 num_words += 2;
823 }
824
825 if ((control_word >> 20) & 1)
826 printf("unknown bit 20 enabled\n");
827
828 if ((control_word >> 21) & 1) {
829 print_vector_field("vadd", word_ptr, *beginning_ptr, tabs);
830 beginning_ptr += 1;
831 word_ptr += 3;
832 num_words += 3;
833 }
834
835 if ((control_word >> 22) & 1)
836 printf("unknown bit 22 enabled\n");
837
838 if ((control_word >> 23) & 1) {
839 print_scalar_field("smul", word_ptr, *beginning_ptr, tabs);
840 beginning_ptr += 1;
841 word_ptr += 2;
842 num_words += 2;
843 }
844
845 if ((control_word >> 24) & 1)
846 printf("unknown bit 24 enabled\n");
847
848 if ((control_word >> 25) & 1) {
849 print_vector_field("lut", word_ptr, *beginning_ptr, tabs);
850 beginning_ptr += 1;
851 word_ptr += 3;
852 num_words += 3;
853 }
854
855 if ((control_word >> 26) & 1) {
856 print_compact_branch_writeout_field(*word_ptr);
857 word_ptr += 1;
858 num_words += 1;
859 }
860
861 if ((control_word >> 27) & 1) {
862 print_extended_branch_writeout_field((uint8_t *) word_ptr);
863 word_ptr += 3;
864 num_words += 3;
865 }
866
867 if (num_quad_words > (num_words + 7) / 8) {
868 assert(num_quad_words == (num_words + 15) / 8);
869 //Assume that the extra quadword is constants
870 void *consts = words + (4 * num_quad_words - 4);
871
872 if (is_embedded_constant_int) {
873 if (is_embedded_constant_half) {
874 int16_t *sconsts = (int16_t *) consts;
875 printf("sconstants %d, %d, %d, %d\n",
876 sconsts[0],
877 sconsts[1],
878 sconsts[2],
879 sconsts[3]);
880 } else {
881 int32_t *iconsts = (int32_t *) consts;
882 printf("iconstants %d, %d, %d, %d\n",
883 iconsts[0],
884 iconsts[1],
885 iconsts[2],
886 iconsts[3]);
887 }
888 } else {
889 if (is_embedded_constant_half) {
890 uint16_t *hconsts = (uint16_t *) consts;
891 printf("hconstants %g, %g, %g, %g\n",
892 _mesa_half_to_float(hconsts[0]),
893 _mesa_half_to_float(hconsts[1]),
894 _mesa_half_to_float(hconsts[2]),
895 _mesa_half_to_float(hconsts[3]));
896 } else {
897 uint32_t *fconsts = (uint32_t *) consts;
898 printf("fconstants %g, %g, %g, %g\n",
899 float_bitcast(fconsts[0]),
900 float_bitcast(fconsts[1]),
901 float_bitcast(fconsts[2]),
902 float_bitcast(fconsts[3]));
903 }
904
905 }
906 }
907 }
908
909 static void
910 print_varying_parameters(midgard_load_store_word *word)
911 {
912 midgard_varying_parameter param;
913 unsigned v = word->varying_parameters;
914 memcpy(&param, &v, sizeof(param));
915
916 if (param.is_varying) {
917 /* If a varying, there are qualifiers */
918 if (param.flat)
919 printf(".flat");
920
921 if (param.interpolation != midgard_interp_default) {
922 if (param.interpolation == midgard_interp_centroid)
923 printf(".centroid");
924 else
925 printf(".interp%d", param.interpolation);
926 }
927
928 if (param.modifier != midgard_varying_mod_none) {
929 if (param.modifier == midgard_varying_mod_perspective_w)
930 printf(".perspectivew");
931 else if (param.modifier == midgard_varying_mod_perspective_z)
932 printf(".perspectivez");
933 else
934 printf(".mod%d", param.modifier);
935 }
936 } else if (param.flat || param.interpolation || param.modifier) {
937 printf(" /* is_varying not set but varying metadata attached */");
938 }
939
940 if (param.zero0 || param.zero1 || param.zero2)
941 printf(" /* zero tripped, %d %d %d */ ", param.zero0, param.zero1, param.zero2);
942 }
943
944 static bool
945 is_op_varying(unsigned op)
946 {
947 switch (op) {
948 case midgard_op_st_vary_16:
949 case midgard_op_st_vary_32:
950 case midgard_op_ld_vary_16:
951 case midgard_op_ld_vary_32:
952 return true;
953 }
954
955 return false;
956 }
957
958 static void
959 print_load_store_instr(uint64_t data,
960 unsigned tabs)
961 {
962 midgard_load_store_word *word = (midgard_load_store_word *) &data;
963
964 print_ld_st_opcode(word->op);
965
966 if (is_op_varying(word->op))
967 print_varying_parameters(word);
968
969 printf(" r%d", word->reg);
970 print_mask_4(word->mask);
971
972 int address = word->address;
973
974 if (word->op == midgard_op_ld_uniform_32) {
975 /* Uniforms use their own addressing scheme */
976
977 int lo = word->varying_parameters >> 7;
978 int hi = word->address;
979
980 /* TODO: Combine fields logically */
981 address = (hi << 3) | lo;
982 }
983
984 printf(", %d", address);
985
986 print_swizzle_vec4(word->swizzle, false, false);
987
988 printf(", 0x%X /* %X */\n", word->unknown, word->varying_parameters);
989 }
990
991 static void
992 print_load_store_word(uint32_t *word, unsigned tabs)
993 {
994 midgard_load_store *load_store = (midgard_load_store *) word;
995
996 if (load_store->word1 != 3) {
997 print_load_store_instr(load_store->word1, tabs);
998 }
999
1000 if (load_store->word2 != 3) {
1001 print_load_store_instr(load_store->word2, tabs);
1002 }
1003 }
1004
1005 static void
1006 print_texture_reg(bool full, bool select, bool upper)
1007 {
1008 if (full)
1009 printf("r%d", REG_TEX_BASE + select);
1010 else
1011 printf("hr%d", (REG_TEX_BASE + select) * 2 + upper);
1012
1013 if (full && upper)
1014 printf("// error: out full / upper mutually exclusive\n");
1015
1016 }
1017
1018 static void
1019 print_texture_reg_triple(unsigned triple)
1020 {
1021 bool full = triple & 1;
1022 bool select = triple & 2;
1023 bool upper = triple & 4;
1024
1025 print_texture_reg(full, select, upper);
1026 }
1027
1028 static void
1029 print_texture_format(int format)
1030 {
1031 /* Act like a modifier */
1032 printf(".");
1033
1034 switch (format) {
1035 DEFINE_CASE(TEXTURE_2D, "2d");
1036 DEFINE_CASE(TEXTURE_3D, "3d");
1037 DEFINE_CASE(TEXTURE_CUBE, "cube");
1038
1039 default:
1040 printf("fmt_%d", format);
1041 break;
1042 }
1043 }
1044
1045 static void
1046 print_texture_op(unsigned op)
1047 {
1048 /* Act like a bare name, like ESSL functions */
1049
1050 switch (op) {
1051 DEFINE_CASE(TEXTURE_OP_NORMAL, "texture");
1052 DEFINE_CASE(TEXTURE_OP_LOD, "textureLod");
1053 DEFINE_CASE(TEXTURE_OP_TEXEL_FETCH, "texelFetch");
1054
1055 default:
1056 printf("tex_%d", op);
1057 break;
1058 }
1059 }
1060
1061 static bool
1062 texture_op_takes_bias(unsigned op)
1063 {
1064 return op == TEXTURE_OP_NORMAL;
1065 }
1066
1067 #undef DEFINE_CASE
1068
1069 static void
1070 print_texture_word(uint32_t *word, unsigned tabs)
1071 {
1072 midgard_texture_word *texture = (midgard_texture_word *) word;
1073
1074 /* Broad category of texture operation in question */
1075 print_texture_op(texture->op);
1076
1077 /* Specific format in question */
1078 print_texture_format(texture->format);
1079
1080 /* Instruction "modifiers" parallel the ALU instructions. */
1081
1082 if (texture->shadow)
1083 printf(".shadow");
1084
1085 if (texture->cont)
1086 printf(".cont");
1087
1088 if (texture->last)
1089 printf(".last");
1090
1091 printf(" ");
1092
1093 print_texture_reg(texture->out_full, texture->out_reg_select, texture->out_upper);
1094 print_mask_4(texture->mask);
1095 printf(", ");
1096
1097 printf("texture%d, ", texture->texture_handle);
1098
1099 printf("sampler%d", texture->sampler_handle);
1100 print_swizzle_vec4(texture->swizzle, false, false);
1101 printf(", ");
1102
1103 print_texture_reg(texture->in_reg_full, texture->in_reg_select, texture->in_reg_upper);
1104 print_swizzle_vec4(texture->in_reg_swizzle, false, false);
1105
1106 /* There is *always* an offset attached. Of
1107 * course, that offset is just immediate #0 for a
1108 * GLES call that doesn't take an offset. If there
1109 * is a non-negative non-zero offset, this is
1110 * specified in immediate offset mode, with the
1111 * values in the offset_* fields as immediates. If
1112 * this is a negative offset, we instead switch to
1113 * a register offset mode, where the offset_*
1114 * fields become register triplets */
1115
1116 if (texture->offset_register) {
1117 printf(" + ");
1118 print_texture_reg_triple(texture->offset_x);
1119
1120 /* I've never seen them different than this */
1121 if (texture->offset_y != 2)
1122 printf(" /* y = %d */", texture->offset_y);
1123
1124 if (texture->offset_z != 1)
1125 printf(" /* z = %d */", texture->offset_z);
1126
1127 printf(", ");
1128 } else if (texture->offset_x || texture->offset_y || texture->offset_z) {
1129 printf(" + <%d, %d, %d>, ",
1130 texture->offset_x,
1131 texture->offset_y,
1132 texture->offset_z);
1133 } else {
1134 printf(", ");
1135 }
1136
1137 if (texture->lod_register) {
1138 /* TODO: Decode */
1139 printf("lod/bias/grad reg 0x%X (%X), ", texture->bias, texture->bias_int);
1140 } else if (texture->bias || texture->bias_int) {
1141 int bias_int = texture->bias_int;
1142 float bias_frac = texture->bias / 256.0f;
1143 float bias = bias_int + bias_frac;
1144
1145 bool is_bias = texture_op_takes_bias(texture->op);
1146 char operand = is_bias ? '+' : '=';
1147
1148 printf("lod %c %f, ", operand, bias);
1149 }
1150
1151 printf("\n");
1152
1153 /* While not zero in general, for these simple instructions the
1154 * following unknowns are zero, so we don't include them */
1155
1156 if (texture->unknown2 ||
1157 texture->unknown3 ||
1158 texture->unknown4 ||
1159 texture->unknownA ||
1160 texture->unknownB ||
1161 texture->unknown8) {
1162 printf("// unknown2 = 0x%x\n", texture->unknown2);
1163 printf("// unknown3 = 0x%x\n", texture->unknown3);
1164 printf("// unknown4 = 0x%x\n", texture->unknown4);
1165 printf("// unknownA = 0x%x\n", texture->unknownA);
1166 printf("// unknownB = 0x%x\n", texture->unknownB);
1167 printf("// unknown8 = 0x%x\n", texture->unknown8);
1168 }
1169
1170 if (texture->offset_unknown4 ||
1171 texture->offset_unknown8) {
1172 printf("// offset_unknown4 = 0x%x\n", texture->offset_unknown4);
1173 printf("// offset_unknown8 = 0x%x\n", texture->offset_unknown8);
1174 }
1175
1176 /* Don't blow up */
1177 if (texture->unknown7 != 0x1)
1178 printf("// (!) unknown7 = %d\n", texture->unknown7);
1179 }
1180
1181 void
1182 disassemble_midgard(uint8_t *code, size_t size)
1183 {
1184 uint32_t *words = (uint32_t *) code;
1185 unsigned num_words = size / 4;
1186 int tabs = 0;
1187
1188 bool prefetch_flag = false;
1189
1190 unsigned i = 0;
1191
1192 while (i < num_words) {
1193 unsigned tag = words[i] & 0xF;
1194 unsigned num_quad_words = midgard_word_size[tag];
1195
1196 switch (midgard_word_types[tag]) {
1197 case midgard_word_type_texture:
1198 print_texture_word(&words[i], tabs);
1199 break;
1200
1201 case midgard_word_type_load_store:
1202 print_load_store_word(&words[i], tabs);
1203 break;
1204
1205 case midgard_word_type_alu:
1206 print_alu_word(&words[i], num_quad_words, tabs);
1207
1208 if (prefetch_flag)
1209 return;
1210
1211 /* Reset word static analysis state */
1212 is_embedded_constant_half = false;
1213 is_embedded_constant_int = false;
1214
1215 break;
1216
1217 default:
1218 printf("Unknown word type %u:\n", words[i] & 0xF);
1219 num_quad_words = 1;
1220 print_quad_word(&words[i], tabs);
1221 printf("\n");
1222 break;
1223 }
1224
1225 printf("\n");
1226
1227 unsigned next = (words[i] & 0xF0) >> 4;
1228
1229 i += 4 * num_quad_words;
1230
1231 /* Break based on instruction prefetch flag */
1232
1233 if (i < num_words && next == 1) {
1234 prefetch_flag = true;
1235
1236 if (midgard_word_types[words[i] & 0xF] != midgard_word_type_alu)
1237 return;
1238 }
1239 }
1240
1241 return;
1242 }