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