5 * Copyright (c) 2013 Connor Abbott (connor@abbott.cx)
6 * Copyright (c) 2018 Alyssa Rosenzweig (alyssa@rosenzweig.io)
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
34 #include "midgard-parse.h"
35 #include "midgard_ops.h"
36 #include "disassemble.h"
38 #include "util/half_float.h"
39 #include "util/u_math.h"
41 #define DEFINE_CASE(define, str) case define: { printf(str); break; }
43 static bool is_instruction_int
= false;
47 static unsigned nr_ins
= 0;
49 /* Prints a short form of the tag for branching, the minimum needed to be
50 * legible and unambiguous */
53 print_tag_short(unsigned tag
)
55 switch (midgard_word_types
[tag
]) {
56 case midgard_word_type_texture
:
57 printf("tex/%X", tag
);
60 case midgard_word_type_load_store
:
64 case midgard_word_type_alu
:
65 printf("alu%d/%X", midgard_word_size
[tag
], tag
);
69 printf("%s%X", (tag
> 0) ? "" : "unk", tag
);
75 print_alu_opcode(midgard_alu_op op
)
79 if (alu_opcode_props
[op
].name
) {
80 printf("%s", alu_opcode_props
[op
].name
);
82 int_op
= midgard_is_integer_op(op
);
84 printf("alu_op_%02X", op
);
86 /* For constant analysis */
87 is_instruction_int
= int_op
;
91 print_ld_st_opcode(midgard_load_store_op op
)
93 if (load_store_opcode_names
[op
])
94 printf("%s", load_store_opcode_names
[op
]);
96 printf("ldst_op_%02X", op
);
99 static bool is_embedded_constant_half
= false;
100 static bool is_embedded_constant_int
= false;
103 prefix_for_bits(unsigned bits
)
118 print_reg(unsigned reg
, unsigned bits
)
120 /* Perform basic static analysis for expanding constants correctly */
123 is_embedded_constant_int
= is_instruction_int
;
124 is_embedded_constant_half
= (bits
< 32);
127 char prefix
= prefix_for_bits(bits
);
135 static char *outmod_names_float
[4] = {
142 static char *outmod_names_int
[4] = {
149 static char *srcmod_names_int
[4] = {
157 print_outmod(unsigned outmod
, bool is_int
)
159 printf("%s", is_int
? outmod_names_int
[outmod
] :
160 outmod_names_float
[outmod
]);
164 print_quad_word(uint32_t *words
, unsigned tabs
)
168 for (i
= 0; i
< 4; i
++)
169 printf("0x%08X%s ", words
[i
], i
== 3 ? "" : ",");
174 static const char components
[16] = "xyzwefghijklmnop";
176 /* Helper to print 4 chars of a swizzle */
178 print_swizzle_helper(unsigned swizzle
, bool upper
)
180 for (unsigned i
= 0; i
< 4; ++i
) {
181 unsigned c
= (swizzle
>> (i
* 2)) & 3;
183 printf("%c", components
[c
]);
187 /* Helper to print 8 chars of a swizzle, duplicating over */
189 print_swizzle_helper_8(unsigned swizzle
, bool upper
)
191 for (unsigned i
= 0; i
< 4; ++i
) {
192 unsigned c
= (swizzle
>> (i
* 2)) & 3;
195 printf("%c%c", components
[c
], components
[c
+1]);
200 print_swizzle_vec16(unsigned swizzle
, bool rep_high
, bool rep_low
,
201 midgard_dest_override override
)
205 if (override
== midgard_dest_override_upper
) {
207 printf(" /* rep_high */ ");
209 printf(" /* rep_low */ ");
211 if (!rep_high
&& rep_low
)
212 print_swizzle_helper_8(swizzle
, true);
214 print_swizzle_helper_8(swizzle
, false);
216 print_swizzle_helper_8(swizzle
, rep_high
& 1);
217 print_swizzle_helper_8(swizzle
, !rep_low
& 1);
222 print_swizzle_vec8(unsigned swizzle
, bool rep_high
, bool rep_low
)
226 print_swizzle_helper(swizzle
, rep_high
& 1);
227 print_swizzle_helper(swizzle
, !rep_low
& 1);
231 print_swizzle_vec4(unsigned swizzle
, bool rep_high
, bool rep_low
)
234 printf(" /* rep_high */ ");
236 printf(" /* rep_low */ ");
238 if (swizzle
== 0xE4) return; /* xyzw */
241 print_swizzle_helper(swizzle
, 0);
244 print_swizzle_vec2(unsigned swizzle
, bool rep_high
, bool rep_low
)
247 printf(" /* rep_high */ ");
249 printf(" /* rep_low */ ");
251 if (swizzle
== 0xE4) return; /* XY */
255 for (unsigned i
= 0; i
< 4; i
+= 2) {
256 unsigned a
= (swizzle
>> (i
* 2)) & 3;
257 unsigned b
= (swizzle
>> ((i
+1) * 2)) & 3;
259 /* Normally we're adjacent, but if there's an issue, don't make
263 printf("[%c%c]", components
[a
], components
[b
]);
265 printf("%c", components
[a
>> 1]);
266 else if (b
== (a
+ 1))
267 printf("%c", "XY"[a
>> 1]);
269 printf("[%c%c]", components
[a
], components
[b
]);
274 bits_for_mode(midgard_reg_mode mode
)
277 case midgard_reg_mode_8
:
279 case midgard_reg_mode_16
:
281 case midgard_reg_mode_32
:
283 case midgard_reg_mode_64
:
286 unreachable("Invalid reg mode");
292 bits_for_mode_halved(midgard_reg_mode mode
, bool half
)
294 unsigned bits
= bits_for_mode(mode
);
303 print_vector_src(unsigned src_binary
,
304 midgard_reg_mode mode
, unsigned reg
,
305 midgard_dest_override override
, bool is_int
)
307 midgard_vector_alu_src
*src
= (midgard_vector_alu_src
*)&src_binary
;
309 /* Modifiers change meaning depending on the op's context */
311 midgard_int_mod int_mod
= src
->mod
;
314 printf("%s", srcmod_names_int
[int_mod
]);
316 if (src
->mod
& MIDGARD_FLOAT_MOD_NEG
)
319 if (src
->mod
& MIDGARD_FLOAT_MOD_ABS
)
324 unsigned bits
= bits_for_mode_halved(mode
, src
->half
);
325 print_reg(reg
, bits
);
329 print_swizzle_vec8(src
->swizzle
, src
->rep_high
, src
->rep_low
);
331 print_swizzle_vec16(src
->swizzle
, src
->rep_high
, src
->rep_low
, override
);
333 print_swizzle_vec4(src
->swizzle
, src
->rep_high
, src
->rep_low
);
335 print_swizzle_vec2(src
->swizzle
, src
->rep_high
, src
->rep_low
);
337 /* Since we wrapped with a function-looking thing */
339 if (is_int
&& int_mod
== midgard_int_shift
)
340 printf(") << %d", bits
);
341 else if ((is_int
&& (int_mod
!= midgard_int_normal
))
342 || (!is_int
&& src
->mod
& MIDGARD_FLOAT_MOD_ABS
))
347 decode_vector_imm(unsigned src2_reg
, unsigned imm
)
350 ret
= src2_reg
<< 11;
351 ret
|= (imm
& 0x7) << 8;
352 ret
|= (imm
>> 3) & 0xFF;
357 print_immediate(uint16_t imm
)
359 if (is_instruction_int
)
362 printf("#%g", _mesa_half_to_float(imm
));
366 print_dest(unsigned reg
, midgard_reg_mode mode
, midgard_dest_override override
)
368 /* Depending on the mode and override, we determine the type of
369 * destination addressed. Absent an override, we address just the
370 * type of the operation itself */
372 unsigned bits
= bits_for_mode(mode
);
374 if (override
!= midgard_dest_override_none
)
377 print_reg(reg
, bits
);
383 print_mask_vec16(uint8_t mask
, midgard_dest_override override
)
387 if (override
== midgard_dest_override_none
) {
388 for (unsigned i
= 0; i
< 8; i
++) {
392 components
[i
*2 + 1]);
395 bool upper
= (override
== midgard_dest_override_upper
);
397 for (unsigned i
= 0; i
< 8; i
++) {
399 printf("%c", components
[i
+ (upper
? 8 : 0)]);
404 /* For 16-bit+ masks, we read off from the 8-bit mask field. For 16-bit (vec8),
405 * it's just one bit per channel, easy peasy. For 32-bit (vec4), it's one bit
406 * per channel with one duplicate bit in the middle. For 64-bit (vec2), it's
407 * one-bit per channel with _3_ duplicate bits in the middle. Basically, just
408 * subdividing the 128-bit word in 16-bit increments. For 64-bit, we uppercase
409 * the mask to make it obvious what happened */
412 print_mask(uint8_t mask
, unsigned bits
, midgard_dest_override override
)
415 /* Shouldn't happen but with junk / out-of-spec shaders it
416 * would cause an infinite loop */
418 printf("/* XXX: bits = %d */", bits
);
423 print_mask_vec16(mask
, override
);
427 /* Skip 'complete' masks */
429 if (bits
>= 32 && mask
== 0xFF) return;
434 else if (mask
== 0xF0) {
442 unsigned skip
= (bits
/ 16);
443 bool uppercase
= bits
> 32;
444 bool tripped
= false;
446 for (unsigned i
= 0; i
< 8; i
+= skip
) {
447 bool a
= (mask
& (1 << i
)) != 0;
449 for (unsigned j
= 1; j
< skip
; ++j
) {
450 bool dupe
= (mask
& (1 << (i
+ j
))) != 0;
451 tripped
|= (dupe
!= a
);
455 char c
= components
[i
/ skip
];
465 printf(" /* %X */", mask
);
468 /* Prints the 4-bit masks found in texture and load/store ops, as opposed to
469 * the 8-bit masks found in (vector) ALU ops */
472 print_mask_4(unsigned mask
)
474 if (mask
== 0xF) return;
478 for (unsigned i
= 0; i
< 4; ++i
) {
479 bool a
= (mask
& (1 << i
)) != 0;
481 printf("%c", components
[i
]);
486 print_vector_field(const char *name
, uint16_t *words
, uint16_t reg_word
,
489 midgard_reg_info
*reg_info
= (midgard_reg_info
*)®_word
;
490 midgard_vector_alu
*alu_field
= (midgard_vector_alu
*) words
;
491 midgard_reg_mode mode
= alu_field
->reg_mode
;
492 unsigned override
= alu_field
->dest_override
;
494 /* For now, prefix instruction names with their unit, until we
495 * understand how this works on a deeper level */
498 print_alu_opcode(alu_field
->op
);
500 /* Postfix with the size to disambiguate if necessary */
501 char postfix
= prefix_for_bits(bits_for_mode(mode
));
502 bool size_ambiguous
= override
!= midgard_dest_override_none
;
505 printf("%c", postfix
? postfix
: 'r');
507 /* Print the outmod, if there is one */
508 print_outmod(alu_field
->outmod
,
509 midgard_is_integer_out_op(alu_field
->op
));
513 /* Mask denoting status of 8-lanes */
514 uint8_t mask
= alu_field
->mask
;
516 /* First, print the destination */
518 print_dest(reg_info
->out_reg
, mode
, alu_field
->dest_override
);
520 /* Apply the destination override to the mask */
522 if (mode
== midgard_reg_mode_32
|| mode
== midgard_reg_mode_64
) {
523 if (override
== midgard_dest_override_lower
)
525 else if (override
== midgard_dest_override_upper
)
527 } else if (mode
== midgard_reg_mode_16
528 && override
== midgard_dest_override_lower
) {
532 if (override
!= midgard_dest_override_none
) {
533 bool modeable
= (mode
!= midgard_reg_mode_8
);
534 bool known
= override
!= 0x3; /* Unused value */
536 if (!(modeable
&& known
))
537 printf("/* do%d */ ", override
);
540 print_mask(mask
, dest_size
, override
);
544 bool is_int
= midgard_is_integer_op(alu_field
->op
);
545 print_vector_src(alu_field
->src1
, mode
, reg_info
->src1_reg
, override
, is_int
);
549 if (reg_info
->src2_imm
) {
550 uint16_t imm
= decode_vector_imm(reg_info
->src2_reg
, alu_field
->src2
>> 2);
551 print_immediate(imm
);
553 print_vector_src(alu_field
->src2
, mode
,
554 reg_info
->src2_reg
, override
, is_int
);
562 print_scalar_src(unsigned src_binary
, unsigned reg
)
564 midgard_scalar_alu_src
*src
= (midgard_scalar_alu_src
*)&src_binary
;
572 print_reg(reg
, src
->full
? 32 : 16);
574 unsigned c
= src
->component
;
577 assert((c
& 1) == 0);
581 printf(".%c", components
[c
]);
589 decode_scalar_imm(unsigned src2_reg
, unsigned imm
)
592 ret
= src2_reg
<< 11;
593 ret
|= (imm
& 3) << 9;
594 ret
|= (imm
& 4) << 6;
595 ret
|= (imm
& 0x38) << 2;
601 print_scalar_field(const char *name
, uint16_t *words
, uint16_t reg_word
,
604 midgard_reg_info
*reg_info
= (midgard_reg_info
*)®_word
;
605 midgard_scalar_alu
*alu_field
= (midgard_scalar_alu
*) words
;
607 if (alu_field
->unknown
)
608 printf("scalar ALU unknown bit set\n");
611 print_alu_opcode(alu_field
->op
);
612 print_outmod(alu_field
->outmod
,
613 midgard_is_integer_out_op(alu_field
->op
));
616 bool full
= alu_field
->output_full
;
617 print_reg(reg_info
->out_reg
, full
? 32 : 16);
618 unsigned c
= alu_field
->output_component
;
621 assert((c
& 1) == 0);
625 printf(".%c, ", components
[c
]);
627 print_scalar_src(alu_field
->src1
, reg_info
->src1_reg
);
631 if (reg_info
->src2_imm
) {
632 uint16_t imm
= decode_scalar_imm(reg_info
->src2_reg
,
634 print_immediate(imm
);
636 print_scalar_src(alu_field
->src2
, reg_info
->src2_reg
);
643 print_branch_op(int op
)
646 case midgard_jmp_writeout_op_branch_uncond
:
650 case midgard_jmp_writeout_op_branch_cond
:
654 case midgard_jmp_writeout_op_writeout
:
658 case midgard_jmp_writeout_op_tilebuffer_pending
:
659 printf("tilebuffer.");
662 case midgard_jmp_writeout_op_discard
:
667 printf("unk%d.", op
);
673 print_branch_cond(int cond
)
676 case midgard_condition_write0
:
680 case midgard_condition_false
:
684 case midgard_condition_true
:
688 case midgard_condition_always
:
693 printf("unk%X", cond
);
699 print_compact_branch_writeout_field(uint16_t word
)
701 midgard_jmp_writeout_op op
= word
& 0x7;
704 case midgard_jmp_writeout_op_branch_uncond
: {
705 midgard_branch_uncond br_uncond
;
706 memcpy((char *) &br_uncond
, (char *) &word
, sizeof(br_uncond
));
707 printf("br.uncond ");
709 if (br_uncond
.unknown
!= 1)
710 printf("unknown:%d, ", br_uncond
.unknown
);
712 if (br_uncond
.offset
>= 0)
715 printf("%d -> ", br_uncond
.offset
);
716 print_tag_short(br_uncond
.dest_tag
);
722 case midgard_jmp_writeout_op_branch_cond
:
723 case midgard_jmp_writeout_op_writeout
:
724 case midgard_jmp_writeout_op_discard
:
726 midgard_branch_cond br_cond
;
727 memcpy((char *) &br_cond
, (char *) &word
, sizeof(br_cond
));
731 print_branch_op(br_cond
.op
);
732 print_branch_cond(br_cond
.cond
);
736 if (br_cond
.offset
>= 0)
739 printf("%d -> ", br_cond
.offset
);
740 print_tag_short(br_cond
.dest_tag
);
751 print_extended_branch_writeout_field(uint8_t *words
)
753 midgard_branch_extended br
;
754 memcpy((char *) &br
, (char *) words
, sizeof(br
));
758 print_branch_op(br
.op
);
760 /* Condition codes are a LUT in the general case, but simply repeated 8 times for single-channel conditions.. Check this. */
762 bool single_channel
= true;
764 for (unsigned i
= 0; i
< 16; i
+= 2) {
765 single_channel
&= (((br
.cond
>> i
) & 0x3) == (br
.cond
& 0x3));
769 print_branch_cond(br
.cond
& 0x3);
771 printf("lut%X", br
.cond
);
774 printf(".unknown%d", br
.unknown
);
781 printf("%d -> ", br
.offset
);
782 print_tag_short(br
.dest_tag
);
789 num_alu_fields_enabled(uint32_t control_word
)
793 if ((control_word
>> 17) & 1)
796 if ((control_word
>> 19) & 1)
799 if ((control_word
>> 21) & 1)
802 if ((control_word
>> 23) & 1)
805 if ((control_word
>> 25) & 1)
812 float_bitcast(uint32_t integer
)
824 print_alu_word(uint32_t *words
, unsigned num_quad_words
,
827 uint32_t control_word
= words
[0];
828 uint16_t *beginning_ptr
= (uint16_t *)(words
+ 1);
829 unsigned num_fields
= num_alu_fields_enabled(control_word
);
830 uint16_t *word_ptr
= beginning_ptr
+ num_fields
;
831 unsigned num_words
= 2 + num_fields
;
833 if ((control_word
>> 16) & 1)
834 printf("unknown bit 16 enabled\n");
836 if ((control_word
>> 17) & 1) {
837 print_vector_field("vmul", word_ptr
, *beginning_ptr
, tabs
);
843 if ((control_word
>> 18) & 1)
844 printf("unknown bit 18 enabled\n");
846 if ((control_word
>> 19) & 1) {
847 print_scalar_field("sadd", word_ptr
, *beginning_ptr
, tabs
);
853 if ((control_word
>> 20) & 1)
854 printf("unknown bit 20 enabled\n");
856 if ((control_word
>> 21) & 1) {
857 print_vector_field("vadd", word_ptr
, *beginning_ptr
, tabs
);
863 if ((control_word
>> 22) & 1)
864 printf("unknown bit 22 enabled\n");
866 if ((control_word
>> 23) & 1) {
867 print_scalar_field("smul", word_ptr
, *beginning_ptr
, tabs
);
873 if ((control_word
>> 24) & 1)
874 printf("unknown bit 24 enabled\n");
876 if ((control_word
>> 25) & 1) {
877 print_vector_field("lut", word_ptr
, *beginning_ptr
, tabs
);
883 if ((control_word
>> 26) & 1) {
884 print_compact_branch_writeout_field(*word_ptr
);
889 if ((control_word
>> 27) & 1) {
890 print_extended_branch_writeout_field((uint8_t *) word_ptr
);
895 if (num_quad_words
> (num_words
+ 7) / 8) {
896 assert(num_quad_words
== (num_words
+ 15) / 8);
897 //Assume that the extra quadword is constants
898 void *consts
= words
+ (4 * num_quad_words
- 4);
900 if (is_embedded_constant_int
) {
901 if (is_embedded_constant_half
) {
902 int16_t *sconsts
= (int16_t *) consts
;
903 printf("sconstants %d, %d, %d, %d\n",
909 int32_t *iconsts
= (int32_t *) consts
;
910 printf("iconstants %d, %d, %d, %d\n",
917 if (is_embedded_constant_half
) {
918 uint16_t *hconsts
= (uint16_t *) consts
;
919 printf("hconstants %g, %g, %g, %g\n",
920 _mesa_half_to_float(hconsts
[0]),
921 _mesa_half_to_float(hconsts
[1]),
922 _mesa_half_to_float(hconsts
[2]),
923 _mesa_half_to_float(hconsts
[3]));
925 uint32_t *fconsts
= (uint32_t *) consts
;
926 printf("fconstants %g, %g, %g, %g\n",
927 float_bitcast(fconsts
[0]),
928 float_bitcast(fconsts
[1]),
929 float_bitcast(fconsts
[2]),
930 float_bitcast(fconsts
[3]));
938 print_varying_parameters(midgard_load_store_word
*word
)
940 midgard_varying_parameter param
;
941 unsigned v
= word
->varying_parameters
;
942 memcpy(¶m
, &v
, sizeof(param
));
944 if (param
.is_varying
) {
945 /* If a varying, there are qualifiers */
949 if (param
.interpolation
!= midgard_interp_default
) {
950 if (param
.interpolation
== midgard_interp_centroid
)
953 printf(".interp%d", param
.interpolation
);
956 if (param
.modifier
!= midgard_varying_mod_none
) {
957 if (param
.modifier
== midgard_varying_mod_perspective_w
)
958 printf(".perspectivew");
959 else if (param
.modifier
== midgard_varying_mod_perspective_z
)
960 printf(".perspectivez");
962 printf(".mod%d", param
.modifier
);
964 } else if (param
.flat
|| param
.interpolation
|| param
.modifier
) {
965 printf(" /* is_varying not set but varying metadata attached */");
968 if (param
.zero0
|| param
.zero1
|| param
.zero2
)
969 printf(" /* zero tripped, %d %d %d */ ", param
.zero0
, param
.zero1
, param
.zero2
);
973 is_op_varying(unsigned op
)
976 case midgard_op_st_vary_16
:
977 case midgard_op_st_vary_32
:
978 case midgard_op_st_vary_32i
:
979 case midgard_op_st_vary_32u
:
980 case midgard_op_ld_vary_16
:
981 case midgard_op_ld_vary_32
:
982 case midgard_op_ld_vary_32i
:
983 case midgard_op_ld_vary_32u
:
991 print_load_store_arg(uint8_t arg
, unsigned index
)
993 /* Try to interpret as a register */
994 midgard_ldst_register_select sel
;
995 memcpy(&sel
, &arg
, sizeof(arg
));
997 /* If unknown is set, we're not sure what this is or how to
998 * interpret it. But if it's zero, we get it. */
1001 printf("0x%02X", arg
);
1005 unsigned reg
= REGISTER_LDST_BASE
+ sel
.select
;
1006 char comp
= components
[sel
.component
];
1008 printf("r%d.%c", reg
, comp
);
1010 /* Only print a shift if it's non-zero. Shifts only make sense for the
1011 * second index. For the first, we're not sure what it means yet */
1015 printf(" << %d", sel
.shift
);
1017 printf(" /* %X */", sel
.shift
);
1022 print_load_store_instr(uint64_t data
,
1025 midgard_load_store_word
*word
= (midgard_load_store_word
*) &data
;
1027 print_ld_st_opcode(word
->op
);
1029 if (is_op_varying(word
->op
))
1030 print_varying_parameters(word
);
1032 printf(" r%d", word
->reg
);
1033 print_mask_4(word
->mask
);
1035 int address
= word
->address
;
1037 if (OP_IS_UBO_READ(word
->op
)) {
1038 /* UBOs use their own addressing scheme */
1040 int lo
= word
->varying_parameters
>> 7;
1041 int hi
= word
->address
;
1043 /* TODO: Combine fields logically */
1044 address
= (hi
<< 3) | lo
;
1047 printf(", %d", address
);
1049 print_swizzle_vec4(word
->swizzle
, false, false);
1052 print_load_store_arg(word
->arg_1
, 0);
1054 print_load_store_arg(word
->arg_2
, 1);
1055 printf(" /* %X */\n", word
->varying_parameters
);
1061 print_load_store_word(uint32_t *word
, unsigned tabs
)
1063 midgard_load_store
*load_store
= (midgard_load_store
*) word
;
1065 if (load_store
->word1
!= 3) {
1066 print_load_store_instr(load_store
->word1
, tabs
);
1069 if (load_store
->word2
!= 3) {
1070 print_load_store_instr(load_store
->word2
, tabs
);
1075 print_texture_reg(bool full
, bool select
, bool upper
)
1078 printf("r%d", REG_TEX_BASE
+ select
);
1080 printf("hr%d", (REG_TEX_BASE
+ select
) * 2 + upper
);
1083 printf("// error: out full / upper mutually exclusive\n");
1088 print_texture_reg_triple(unsigned triple
)
1090 bool full
= triple
& 1;
1091 bool select
= triple
& 2;
1092 bool upper
= triple
& 4;
1094 print_texture_reg(full
, select
, upper
);
1098 print_texture_format(int format
)
1100 /* Act like a modifier */
1104 DEFINE_CASE(MALI_TEX_1D
, "1d");
1105 DEFINE_CASE(MALI_TEX_2D
, "2d");
1106 DEFINE_CASE(MALI_TEX_3D
, "3d");
1107 DEFINE_CASE(MALI_TEX_CUBE
, "cube");
1110 unreachable("Bad format");
1115 print_texture_op(unsigned op
, bool gather
)
1117 /* Act like a bare name, like ESSL functions */
1120 printf("textureGather");
1122 unsigned component
= op
>> 4;
1123 unsigned bottom
= op
& 0xF;
1126 printf("_unk%d", bottom
);
1128 printf(".%c", components
[component
]);
1133 DEFINE_CASE(TEXTURE_OP_NORMAL
, "texture");
1134 DEFINE_CASE(TEXTURE_OP_LOD
, "textureLod");
1135 DEFINE_CASE(TEXTURE_OP_TEXEL_FETCH
, "texelFetch");
1136 DEFINE_CASE(TEXTURE_OP_DFDX
, "dFdx");
1137 DEFINE_CASE(TEXTURE_OP_DFDY
, "dFdy");
1140 printf("tex_%X", op
);
1146 texture_op_takes_bias(unsigned op
)
1148 return op
== TEXTURE_OP_NORMAL
;
1152 sampler_type_name(enum mali_sampler_type t
)
1155 case MALI_SAMPLER_FLOAT
:
1157 case MALI_SAMPLER_UNSIGNED
:
1159 case MALI_SAMPLER_SIGNED
:
1170 print_texture_word(uint32_t *word
, unsigned tabs
)
1172 midgard_texture_word
*texture
= (midgard_texture_word
*) word
;
1174 /* Broad category of texture operation in question */
1175 print_texture_op(texture
->op
, texture
->is_gather
);
1177 /* Specific format in question */
1178 print_texture_format(texture
->format
);
1180 assert(texture
->zero
== 0);
1182 /* Instruction "modifiers" parallel the ALU instructions. */
1184 if (texture
->shadow
)
1193 /* Output modifiers are always interpreted floatly */
1194 print_outmod(texture
->outmod
, false);
1198 print_texture_reg(texture
->out_full
, texture
->out_reg_select
, texture
->out_upper
);
1199 print_mask_4(texture
->mask
);
1202 printf("texture%d, ", texture
->texture_handle
);
1204 /* Print the type, GL style */
1205 printf("%c", sampler_type_name(texture
->sampler_type
));
1206 printf("sampler%d", texture
->sampler_handle
);
1207 print_swizzle_vec4(texture
->swizzle
, false, false);
1210 print_texture_reg(texture
->in_reg_full
, texture
->in_reg_select
, texture
->in_reg_upper
);
1211 print_swizzle_vec4(texture
->in_reg_swizzle
, false, false);
1213 /* There is *always* an offset attached. Of
1214 * course, that offset is just immediate #0 for a
1215 * GLES call that doesn't take an offset. If there
1216 * is a non-negative non-zero offset, this is
1217 * specified in immediate offset mode, with the
1218 * values in the offset_* fields as immediates. If
1219 * this is a negative offset, we instead switch to
1220 * a register offset mode, where the offset_*
1221 * fields become register triplets */
1223 if (texture
->offset_register
) {
1225 print_texture_reg_triple(texture
->offset_x
);
1227 /* The less questions you ask, the better. */
1229 unsigned swizzle_lo
, swizzle_hi
;
1230 unsigned orig_y
= texture
->offset_y
;
1231 unsigned orig_z
= texture
->offset_z
;
1233 memcpy(&swizzle_lo
, &orig_y
, sizeof(unsigned));
1234 memcpy(&swizzle_hi
, &orig_z
, sizeof(unsigned));
1236 /* Duplicate hi swizzle over */
1237 assert(swizzle_hi
< 4);
1238 swizzle_hi
= (swizzle_hi
<< 2) | swizzle_hi
;
1240 unsigned swiz
= (swizzle_lo
<< 4) | swizzle_hi
;
1241 unsigned reversed
= util_bitreverse(swiz
) >> 24;
1242 print_swizzle_vec4(reversed
, false, false);
1245 } else if (texture
->offset_x
|| texture
->offset_y
|| texture
->offset_z
) {
1246 /* Only select ops allow negative immediate offsets, verify */
1248 bool neg_x
= texture
->offset_x
< 0;
1249 bool neg_y
= texture
->offset_y
< 0;
1250 bool neg_z
= texture
->offset_z
< 0;
1251 bool any_neg
= neg_x
|| neg_y
|| neg_z
;
1253 if (any_neg
&& texture
->op
!= TEXTURE_OP_TEXEL_FETCH
)
1254 printf("/* invalid negative */ ");
1256 /* Regardless, just print the immediate offset */
1258 printf(" + <%d, %d, %d>, ",
1266 char lod_operand
= texture_op_takes_bias(texture
->op
) ? '+' : '=';
1268 if (texture
->lod_register
) {
1269 midgard_tex_register_select sel
;
1270 uint8_t raw
= texture
->bias
;
1271 memcpy(&sel
, &raw
, sizeof(raw
));
1273 printf("lod %c ", lod_operand
);
1274 print_texture_reg(sel
.full
, sel
.select
, sel
.upper
);
1275 printf(".%c, ", components
[sel
.component
]);
1277 if (texture
->bias_int
)
1278 printf(" /* bias_int = 0x%X */", texture
->bias_int
);
1281 printf(" /* sel.zero = 0x%X */", sel
.zero
);
1282 } else if (texture
->op
== TEXTURE_OP_TEXEL_FETCH
) {
1283 /* For texel fetch, the int LOD is in the fractional place and
1284 * there is no fraction / possibility of bias. We *always* have
1285 * an explicit LOD, even if it's zero. */
1287 if (texture
->bias_int
)
1288 printf(" /* bias_int = 0x%X */ ", texture
->bias_int
);
1290 printf("lod = %d, ", texture
->bias
);
1291 } else if (texture
->bias
|| texture
->bias_int
) {
1292 signed bias_int
= texture
->bias_int
;
1293 float bias_frac
= texture
->bias
/ 256.0f
;
1294 float bias
= bias_int
+ bias_frac
;
1296 bool is_bias
= texture_op_takes_bias(texture
->op
);
1297 char sign
= (bias
>= 0.0) ? '+' : '-';
1298 char operand
= is_bias
? sign
: '=';
1300 printf("lod %c %f, ", operand
, fabsf(bias
));
1305 /* While not zero in general, for these simple instructions the
1306 * following unknowns are zero, so we don't include them */
1308 if (texture
->unknown4
||
1309 texture
->unknownA
||
1310 texture
->unknown8
) {
1311 printf("// unknown4 = 0x%x\n", texture
->unknown4
);
1312 printf("// unknownA = 0x%x\n", texture
->unknownA
);
1313 printf("// unknown8 = 0x%x\n", texture
->unknown8
);
1320 disassemble_midgard(uint8_t *code
, size_t size
, bool stats
, unsigned nr_registers
, const char *prefix
)
1322 uint32_t *words
= (uint32_t *) code
;
1323 unsigned num_words
= size
/ 4;
1326 bool prefetch_flag
= false;
1328 int last_next_tag
= -1;
1332 /* Stats for shader-db */
1333 unsigned nr_bundles
= 0;
1334 unsigned nr_quadwords
= 0;
1337 while (i
< num_words
) {
1338 unsigned tag
= words
[i
] & 0xF;
1339 unsigned next_tag
= (words
[i
] >> 4) & 0xF;
1340 unsigned num_quad_words
= midgard_word_size
[tag
];
1343 if (last_next_tag
> 1) {
1344 if (last_next_tag
!= tag
) {
1345 printf("/* TAG ERROR got ");
1346 print_tag_short(tag
);
1347 printf(" expected ");
1348 print_tag_short(last_next_tag
);
1352 /* TODO: Check ALU case */
1355 last_next_tag
= next_tag
;
1357 switch (midgard_word_types
[tag
]) {
1358 case midgard_word_type_texture
:
1359 print_texture_word(&words
[i
], tabs
);
1362 case midgard_word_type_load_store
:
1363 print_load_store_word(&words
[i
], tabs
);
1366 case midgard_word_type_alu
:
1367 print_alu_word(&words
[i
], num_quad_words
, tabs
);
1369 /* Reset word static analysis state */
1370 is_embedded_constant_half
= false;
1371 is_embedded_constant_int
= false;
1376 printf("Unknown word type %u:\n", words
[i
] & 0xF);
1378 print_quad_word(&words
[i
], tabs
);
1383 if (prefetch_flag
&& midgard_word_types
[tag
] == midgard_word_type_alu
)
1388 unsigned next
= (words
[i
] & 0xF0) >> 4;
1390 /* We are parsing per bundle anyway */
1392 nr_quadwords
+= num_quad_words
;
1394 /* Break based on instruction prefetch flag */
1396 if (i
< num_words
&& next
== 1) {
1397 prefetch_flag
= true;
1399 if (midgard_word_types
[words
[i
] & 0xF] != midgard_word_type_alu
)
1403 i
+= 4 * num_quad_words
;
1407 unsigned nr_threads
=
1408 (nr_registers
<= 4) ? 4 :
1409 (nr_registers
<= 8) ? 2 :
1413 "%u inst, %u bundles, %u quadwords, "
1414 "%u registers, %u threads, 0 loops\n",
1416 nr_ins
, nr_bundles
, nr_quadwords
,
1417 nr_registers
, nr_threads
);