pan/midgard: Decode register/component in load/store argument
[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)
973 {
974 /* Try to interpret as a register */
975 midgard_ldst_register_select sel;
976 memcpy(&sel, &arg, sizeof(arg));
977
978 unsigned reg = REGISTER_LDST_BASE + sel.select;
979 char comp = components[sel.component];
980
981 printf("r%d.%c /* 0x%X */", reg, comp, arg);
982 }
983
984 static void
985 print_load_store_instr(uint64_t data,
986 unsigned tabs)
987 {
988 midgard_load_store_word *word = (midgard_load_store_word *) &data;
989
990 print_ld_st_opcode(word->op);
991
992 if (is_op_varying(word->op))
993 print_varying_parameters(word);
994
995 printf(" r%d", word->reg);
996 print_mask_4(word->mask);
997
998 int address = word->address;
999
1000 if (word->op == midgard_op_ld_uniform_32) {
1001 /* Uniforms use their own addressing scheme */
1002
1003 int lo = word->varying_parameters >> 7;
1004 int hi = word->address;
1005
1006 /* TODO: Combine fields logically */
1007 address = (hi << 3) | lo;
1008 }
1009
1010 printf(", %d", address);
1011
1012 print_swizzle_vec4(word->swizzle, false, false);
1013
1014 printf(", ");
1015 print_load_store_arg(word->arg_1);
1016 printf(", ");
1017 print_load_store_arg(word->arg_2);
1018 printf(" /* %X */\n", word->varying_parameters);
1019 }
1020
1021 static void
1022 print_load_store_word(uint32_t *word, unsigned tabs)
1023 {
1024 midgard_load_store *load_store = (midgard_load_store *) word;
1025
1026 if (load_store->word1 != 3) {
1027 print_load_store_instr(load_store->word1, tabs);
1028 }
1029
1030 if (load_store->word2 != 3) {
1031 print_load_store_instr(load_store->word2, tabs);
1032 }
1033 }
1034
1035 static void
1036 print_texture_reg(bool full, bool select, bool upper)
1037 {
1038 if (full)
1039 printf("r%d", REG_TEX_BASE + select);
1040 else
1041 printf("hr%d", (REG_TEX_BASE + select) * 2 + upper);
1042
1043 if (full && upper)
1044 printf("// error: out full / upper mutually exclusive\n");
1045
1046 }
1047
1048 static void
1049 print_texture_reg_triple(unsigned triple)
1050 {
1051 bool full = triple & 1;
1052 bool select = triple & 2;
1053 bool upper = triple & 4;
1054
1055 print_texture_reg(full, select, upper);
1056 }
1057
1058 static void
1059 print_texture_format(int format)
1060 {
1061 /* Act like a modifier */
1062 printf(".");
1063
1064 switch (format) {
1065 DEFINE_CASE(MALI_TEX_1D, "1d");
1066 DEFINE_CASE(MALI_TEX_2D, "2d");
1067 DEFINE_CASE(MALI_TEX_3D, "3d");
1068 DEFINE_CASE(MALI_TEX_CUBE, "cube");
1069
1070 default:
1071 unreachable("Bad format");
1072 }
1073 }
1074
1075 static void
1076 print_texture_op(unsigned op, bool gather)
1077 {
1078 /* Act like a bare name, like ESSL functions */
1079
1080 if (gather) {
1081 printf("textureGather");
1082
1083 unsigned component = op >> 4;
1084 unsigned bottom = op & 0xF;
1085
1086 if (bottom != 0x2)
1087 printf("_unk%d", bottom);
1088
1089 printf(".%c", components[component]);
1090 return;
1091 }
1092
1093 switch (op) {
1094 DEFINE_CASE(TEXTURE_OP_NORMAL, "texture");
1095 DEFINE_CASE(TEXTURE_OP_LOD, "textureLod");
1096 DEFINE_CASE(TEXTURE_OP_TEXEL_FETCH, "texelFetch");
1097 DEFINE_CASE(TEXTURE_OP_DFDX, "dFdx");
1098 DEFINE_CASE(TEXTURE_OP_DFDY, "dFdy");
1099
1100 default:
1101 printf("tex_%X", op);
1102 break;
1103 }
1104 }
1105
1106 static bool
1107 texture_op_takes_bias(unsigned op)
1108 {
1109 return op == TEXTURE_OP_NORMAL;
1110 }
1111
1112 static char
1113 sampler_type_name(enum mali_sampler_type t)
1114 {
1115 switch (t) {
1116 case MALI_SAMPLER_FLOAT:
1117 return 'f';
1118 case MALI_SAMPLER_UNSIGNED:
1119 return 'u';
1120 case MALI_SAMPLER_SIGNED:
1121 return 'i';
1122 default:
1123 return '?';
1124 }
1125
1126 }
1127
1128 #undef DEFINE_CASE
1129
1130 static void
1131 print_texture_word(uint32_t *word, unsigned tabs)
1132 {
1133 midgard_texture_word *texture = (midgard_texture_word *) word;
1134
1135 /* Broad category of texture operation in question */
1136 print_texture_op(texture->op, texture->is_gather);
1137
1138 /* Specific format in question */
1139 print_texture_format(texture->format);
1140
1141 assert(texture->zero == 0);
1142
1143 /* Instruction "modifiers" parallel the ALU instructions. */
1144
1145 if (texture->shadow)
1146 printf(".shadow");
1147
1148 if (texture->cont)
1149 printf(".cont");
1150
1151 if (texture->last)
1152 printf(".last");
1153
1154 printf(" ");
1155
1156 print_texture_reg(texture->out_full, texture->out_reg_select, texture->out_upper);
1157 print_mask_4(texture->mask);
1158 printf(", ");
1159
1160 printf("texture%d, ", texture->texture_handle);
1161
1162 /* Print the type, GL style */
1163 printf("%c", sampler_type_name(texture->sampler_type));
1164 printf("sampler%d", texture->sampler_handle);
1165 print_swizzle_vec4(texture->swizzle, false, false);
1166 printf(", ");
1167
1168 print_texture_reg(texture->in_reg_full, texture->in_reg_select, texture->in_reg_upper);
1169 print_swizzle_vec4(texture->in_reg_swizzle, false, false);
1170
1171 /* There is *always* an offset attached. Of
1172 * course, that offset is just immediate #0 for a
1173 * GLES call that doesn't take an offset. If there
1174 * is a non-negative non-zero offset, this is
1175 * specified in immediate offset mode, with the
1176 * values in the offset_* fields as immediates. If
1177 * this is a negative offset, we instead switch to
1178 * a register offset mode, where the offset_*
1179 * fields become register triplets */
1180
1181 if (texture->offset_register) {
1182 printf(" + ");
1183 print_texture_reg_triple(texture->offset_x);
1184
1185 /* The less questions you ask, the better. */
1186
1187 unsigned swizzle_lo, swizzle_hi;
1188 unsigned orig_y = texture->offset_y;
1189 unsigned orig_z = texture->offset_z;
1190
1191 memcpy(&swizzle_lo, &orig_y, sizeof(unsigned));
1192 memcpy(&swizzle_hi, &orig_z, sizeof(unsigned));
1193
1194 /* Duplicate hi swizzle over */
1195 assert(swizzle_hi < 4);
1196 swizzle_hi = (swizzle_hi << 2) | swizzle_hi;
1197
1198 unsigned swiz = (swizzle_lo << 4) | swizzle_hi;
1199 unsigned reversed = util_bitreverse(swiz) >> 24;
1200 print_swizzle_vec4(reversed, false, false);
1201
1202 printf(", ");
1203 } else if (texture->offset_x || texture->offset_y || texture->offset_z) {
1204 /* Only select ops allow negative immediate offsets, verify */
1205
1206 bool neg_x = texture->offset_x < 0;
1207 bool neg_y = texture->offset_y < 0;
1208 bool neg_z = texture->offset_z < 0;
1209 bool any_neg = neg_x || neg_y || neg_z;
1210
1211 if (any_neg && texture->op != TEXTURE_OP_TEXEL_FETCH)
1212 printf("/* invalid negative */ ");
1213
1214 /* Regardless, just print the immediate offset */
1215
1216 printf(" + <%d, %d, %d>, ",
1217 texture->offset_x,
1218 texture->offset_y,
1219 texture->offset_z);
1220 } else {
1221 printf(", ");
1222 }
1223
1224 char lod_operand = texture_op_takes_bias(texture->op) ? '+' : '=';
1225
1226 if (texture->lod_register) {
1227 midgard_tex_register_select sel;
1228 uint8_t raw = texture->bias;
1229 memcpy(&sel, &raw, sizeof(raw));
1230
1231 printf("lod %c ", lod_operand);
1232 print_texture_reg(sel.full, sel.select, sel.upper);
1233 printf(".%c, ", components[sel.component]);
1234
1235 if (texture->bias_int)
1236 printf(" /* bias_int = 0x%X */", texture->bias_int);
1237
1238 if (sel.zero)
1239 printf(" /* sel.zero = 0x%X */", sel.zero);
1240 } else if (texture->op == TEXTURE_OP_TEXEL_FETCH) {
1241 /* For texel fetch, the int LOD is in the fractional place and
1242 * there is no fraction / possibility of bias. We *always* have
1243 * an explicit LOD, even if it's zero. */
1244
1245 if (texture->bias_int)
1246 printf(" /* bias_int = 0x%X */ ", texture->bias_int);
1247
1248 printf("lod = %d, ", texture->bias);
1249 } else if (texture->bias || texture->bias_int) {
1250 signed bias_int = texture->bias_int;
1251 float bias_frac = texture->bias / 256.0f;
1252 float bias = bias_int + bias_frac;
1253
1254 bool is_bias = texture_op_takes_bias(texture->op);
1255 char sign = (bias >= 0.0) ? '+' : '-';
1256 char operand = is_bias ? sign : '=';
1257
1258 printf("lod %c %f, ", operand, fabsf(bias));
1259 }
1260
1261 printf("\n");
1262
1263 /* While not zero in general, for these simple instructions the
1264 * following unknowns are zero, so we don't include them */
1265
1266 if (texture->unknown2 ||
1267 texture->unknown4 ||
1268 texture->unknownA ||
1269 texture->unknown8) {
1270 printf("// unknown2 = 0x%x\n", texture->unknown2);
1271 printf("// unknown4 = 0x%x\n", texture->unknown4);
1272 printf("// unknownA = 0x%x\n", texture->unknownA);
1273 printf("// unknown8 = 0x%x\n", texture->unknown8);
1274 }
1275 }
1276
1277 void
1278 disassemble_midgard(uint8_t *code, size_t size)
1279 {
1280 uint32_t *words = (uint32_t *) code;
1281 unsigned num_words = size / 4;
1282 int tabs = 0;
1283
1284 bool prefetch_flag = false;
1285
1286 int last_next_tag = -1;
1287
1288 unsigned i = 0;
1289
1290 while (i < num_words) {
1291 unsigned tag = words[i] & 0xF;
1292 unsigned next_tag = (words[i] >> 4) & 0xF;
1293 unsigned num_quad_words = midgard_word_size[tag];
1294
1295 /* Check the tag */
1296 if (last_next_tag > 1) {
1297 if (last_next_tag != tag) {
1298 printf("/* TAG ERROR got ");
1299 print_tag_short(tag);
1300 printf(" expected ");
1301 print_tag_short(last_next_tag);
1302 printf(" */ ");
1303 }
1304 } else {
1305 /* TODO: Check ALU case */
1306 }
1307
1308 last_next_tag = next_tag;
1309
1310 switch (midgard_word_types[tag]) {
1311 case midgard_word_type_texture:
1312 print_texture_word(&words[i], tabs);
1313 break;
1314
1315 case midgard_word_type_load_store:
1316 print_load_store_word(&words[i], tabs);
1317 break;
1318
1319 case midgard_word_type_alu:
1320 print_alu_word(&words[i], num_quad_words, tabs);
1321
1322 if (prefetch_flag)
1323 return;
1324
1325 /* Reset word static analysis state */
1326 is_embedded_constant_half = false;
1327 is_embedded_constant_int = false;
1328
1329 break;
1330
1331 default:
1332 printf("Unknown word type %u:\n", words[i] & 0xF);
1333 num_quad_words = 1;
1334 print_quad_word(&words[i], tabs);
1335 printf("\n");
1336 break;
1337 }
1338
1339 printf("\n");
1340
1341 unsigned next = (words[i] & 0xF0) >> 4;
1342
1343 i += 4 * num_quad_words;
1344
1345 /* Break based on instruction prefetch flag */
1346
1347 if (i < num_words && next == 1) {
1348 prefetch_flag = true;
1349
1350 if (midgard_word_types[words[i] & 0xF] != midgard_word_type_alu)
1351 return;
1352 }
1353 }
1354
1355 return;
1356 }