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;
45 /* Prints a short form of the tag for branching, the minimum needed to be
46 * legible and unambiguous */
49 print_tag_short(unsigned tag
)
51 switch (midgard_word_types
[tag
]) {
52 case midgard_word_type_texture
:
53 printf("tex/%X", tag
);
56 case midgard_word_type_load_store
:
60 case midgard_word_type_alu
:
61 printf("alu%d/%X", midgard_word_size
[tag
], tag
);
65 printf("%s%X", (tag
> 0) ? "" : "unk", tag
);
71 print_alu_opcode(midgard_alu_op op
)
75 if (alu_opcode_props
[op
].name
) {
76 printf("%s", alu_opcode_props
[op
].name
);
78 int_op
= midgard_is_integer_op(op
);
80 printf("alu_op_%02X", op
);
82 /* For constant analysis */
83 is_instruction_int
= int_op
;
87 print_ld_st_opcode(midgard_load_store_op op
)
89 if (load_store_opcode_names
[op
])
90 printf("%s", load_store_opcode_names
[op
]);
92 printf("ldst_op_%02X", op
);
95 static bool is_embedded_constant_half
= false;
96 static bool is_embedded_constant_int
= false;
99 prefix_for_bits(unsigned bits
)
114 print_reg(unsigned reg
, unsigned bits
)
116 /* Perform basic static analysis for expanding constants correctly */
119 is_embedded_constant_int
= is_instruction_int
;
120 is_embedded_constant_half
= (bits
< 32);
123 char prefix
= prefix_for_bits(bits
);
131 static char *outmod_names_float
[4] = {
138 static char *outmod_names_int
[4] = {
145 static char *srcmod_names_int
[4] = {
153 print_outmod(unsigned outmod
, bool is_int
)
155 printf("%s", is_int
? outmod_names_int
[outmod
] :
156 outmod_names_float
[outmod
]);
160 print_quad_word(uint32_t *words
, unsigned tabs
)
164 for (i
= 0; i
< 4; i
++)
165 printf("0x%08X%s ", words
[i
], i
== 3 ? "" : ",");
170 static const char components
[16] = "xyzwefghijklmnop";
172 /* Helper to print 4 chars of a swizzle */
174 print_swizzle_helper(unsigned swizzle
, bool upper
)
176 for (unsigned i
= 0; i
< 4; ++i
) {
177 unsigned c
= (swizzle
>> (i
* 2)) & 3;
179 printf("%c", components
[c
]);
183 /* Helper to print 8 chars of a swizzle, duplicating over */
185 print_swizzle_helper_8(unsigned swizzle
, bool upper
)
187 for (unsigned i
= 0; i
< 4; ++i
) {
188 unsigned c
= (swizzle
>> (i
* 2)) & 3;
191 printf("%c%c", components
[c
], components
[c
+1]);
196 print_swizzle_vec16(unsigned swizzle
, bool rep_high
, bool rep_low
,
197 midgard_dest_override override
)
201 if (override
== midgard_dest_override_upper
) {
203 printf(" /* rep_high */ ");
205 printf(" /* rep_low */ ");
207 if (!rep_high
&& rep_low
)
208 print_swizzle_helper_8(swizzle
, true);
210 print_swizzle_helper_8(swizzle
, false);
212 print_swizzle_helper_8(swizzle
, rep_high
& 1);
213 print_swizzle_helper_8(swizzle
, !rep_low
& 1);
218 print_swizzle_vec8(unsigned swizzle
, bool rep_high
, bool rep_low
)
222 print_swizzle_helper(swizzle
, rep_high
& 1);
223 print_swizzle_helper(swizzle
, !rep_low
& 1);
227 print_swizzle_vec4(unsigned swizzle
, bool rep_high
, bool rep_low
)
230 printf(" /* rep_high */ ");
232 printf(" /* rep_low */ ");
234 if (swizzle
== 0xE4) return; /* xyzw */
237 print_swizzle_helper(swizzle
, 0);
240 print_swizzle_vec2(unsigned swizzle
, bool rep_high
, bool rep_low
)
243 printf(" /* rep_high */ ");
245 printf(" /* rep_low */ ");
247 if (swizzle
== 0xE4) return; /* XY */
251 for (unsigned i
= 0; i
< 4; i
+= 2) {
252 unsigned a
= (swizzle
>> (i
* 2)) & 3;
253 unsigned b
= (swizzle
>> ((i
+1) * 2)) & 3;
255 /* Normally we're adjacent, but if there's an issue, don't make
259 printf("[%c%c]", components
[a
], components
[b
]);
261 printf("%c", components
[a
>> 1]);
262 else if (b
== (a
+ 1))
263 printf("%c", "XY"[a
>> 1]);
265 printf("[%c%c]", components
[a
], components
[b
]);
270 bits_for_mode(midgard_reg_mode mode
)
273 case midgard_reg_mode_8
:
275 case midgard_reg_mode_16
:
277 case midgard_reg_mode_32
:
279 case midgard_reg_mode_64
:
287 bits_for_mode_halved(midgard_reg_mode mode
, bool half
)
289 unsigned bits
= bits_for_mode(mode
);
298 print_vector_src(unsigned src_binary
,
299 midgard_reg_mode mode
, unsigned reg
,
300 midgard_dest_override override
, bool is_int
)
302 midgard_vector_alu_src
*src
= (midgard_vector_alu_src
*)&src_binary
;
304 /* Modifiers change meaning depending on the op's context */
306 midgard_int_mod int_mod
= src
->mod
;
309 printf("%s", srcmod_names_int
[int_mod
]);
311 if (src
->mod
& MIDGARD_FLOAT_MOD_NEG
)
314 if (src
->mod
& MIDGARD_FLOAT_MOD_ABS
)
319 unsigned bits
= bits_for_mode_halved(mode
, src
->half
);
320 print_reg(reg
, bits
);
324 print_swizzle_vec8(src
->swizzle
, src
->rep_high
, src
->rep_low
);
326 print_swizzle_vec16(src
->swizzle
, src
->rep_high
, src
->rep_low
, override
);
328 print_swizzle_vec4(src
->swizzle
, src
->rep_high
, src
->rep_low
);
330 print_swizzle_vec2(src
->swizzle
, src
->rep_high
, src
->rep_low
);
332 /* Since we wrapped with a function-looking thing */
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
))
342 decode_vector_imm(unsigned src2_reg
, unsigned imm
)
345 ret
= src2_reg
<< 11;
346 ret
|= (imm
& 0x7) << 8;
347 ret
|= (imm
>> 3) & 0xFF;
352 print_immediate(uint16_t imm
)
354 if (is_instruction_int
)
357 printf("#%g", _mesa_half_to_float(imm
));
361 print_dest(unsigned reg
, midgard_reg_mode mode
, midgard_dest_override override
)
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 */
367 unsigned bits
= bits_for_mode(mode
);
369 if (override
!= midgard_dest_override_none
)
372 print_reg(reg
, bits
);
378 print_mask_vec16(uint8_t mask
, midgard_dest_override override
)
382 if (override
== midgard_dest_override_none
) {
383 for (unsigned i
= 0; i
< 8; i
++) {
387 components
[i
*2 + 1]);
390 bool upper
= (override
== midgard_dest_override_upper
);
392 for (unsigned i
= 0; i
< 8; i
++) {
394 printf("%c", components
[i
+ (upper
? 8 : 0)]);
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 */
407 print_mask(uint8_t mask
, unsigned bits
, midgard_dest_override override
)
410 print_mask_vec16(mask
, override
);
414 /* Skip 'complete' masks */
416 if (bits
>= 32 && mask
== 0xFF) return;
421 else if (mask
== 0xF0) {
429 unsigned skip
= (bits
/ 16);
430 bool uppercase
= bits
> 32;
431 bool tripped
= false;
433 for (unsigned i
= 0; i
< 8; i
+= skip
) {
434 bool a
= (mask
& (1 << i
)) != 0;
436 for (unsigned j
= 1; j
< skip
; ++j
) {
437 bool dupe
= (mask
& (1 << (i
+ j
))) != 0;
438 tripped
|= (dupe
!= a
);
442 char c
= components
[i
/ skip
];
452 printf(" /* %X */", mask
);
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 */
459 print_mask_4(unsigned mask
)
461 if (mask
== 0xF) return;
465 for (unsigned i
= 0; i
< 4; ++i
) {
466 bool a
= (mask
& (1 << i
)) != 0;
468 printf("%c", components
[i
]);
473 print_vector_field(const char *name
, uint16_t *words
, uint16_t reg_word
,
476 midgard_reg_info
*reg_info
= (midgard_reg_info
*)®_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
;
481 /* For now, prefix instruction names with their unit, until we
482 * understand how this works on a deeper level */
485 print_alu_opcode(alu_field
->op
);
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
;
492 printf("%c", postfix
? postfix
: 'r');
494 /* Print the outmod, if there is one */
495 print_outmod(alu_field
->outmod
,
496 midgard_is_integer_out_op(alu_field
->op
));
500 /* Mask denoting status of 8-lanes */
501 uint8_t mask
= alu_field
->mask
;
503 /* First, print the destination */
505 print_dest(reg_info
->out_reg
, mode
, alu_field
->dest_override
);
507 /* Apply the destination override to the mask */
509 if (mode
== midgard_reg_mode_32
|| mode
== midgard_reg_mode_64
) {
510 if (override
== midgard_dest_override_lower
)
512 else if (override
== midgard_dest_override_upper
)
514 } else if (mode
== midgard_reg_mode_16
515 && override
== midgard_dest_override_lower
) {
519 if (override
!= midgard_dest_override_none
) {
520 bool modeable
= (mode
!= midgard_reg_mode_8
);
521 bool known
= override
!= 0x3; /* Unused value */
523 if (!(modeable
&& known
))
524 printf("/* do%d */ ", override
);
527 print_mask(mask
, dest_size
, override
);
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
);
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
);
540 print_vector_src(alu_field
->src2
, mode
,
541 reg_info
->src2_reg
, override
, is_int
);
548 print_scalar_src(unsigned src_binary
, unsigned reg
)
550 midgard_scalar_alu_src
*src
= (midgard_scalar_alu_src
*)&src_binary
;
558 print_reg(reg
, src
->full
? 32 : 16);
560 unsigned c
= src
->component
;
563 assert((c
& 1) == 0);
567 printf(".%c", components
[c
]);
575 decode_scalar_imm(unsigned src2_reg
, unsigned imm
)
578 ret
= src2_reg
<< 11;
579 ret
|= (imm
& 3) << 9;
580 ret
|= (imm
& 4) << 6;
581 ret
|= (imm
& 0x38) << 2;
587 print_scalar_field(const char *name
, uint16_t *words
, uint16_t reg_word
,
590 midgard_reg_info
*reg_info
= (midgard_reg_info
*)®_word
;
591 midgard_scalar_alu
*alu_field
= (midgard_scalar_alu
*) words
;
593 if (alu_field
->unknown
)
594 printf("scalar ALU unknown bit set\n");
597 print_alu_opcode(alu_field
->op
);
598 print_outmod(alu_field
->outmod
,
599 midgard_is_integer_out_op(alu_field
->op
));
602 bool full
= alu_field
->output_full
;
603 print_reg(reg_info
->out_reg
, full
? 32 : 16);
604 unsigned c
= alu_field
->output_component
;
607 assert((c
& 1) == 0);
611 printf(".%c, ", components
[c
]);
613 print_scalar_src(alu_field
->src1
, reg_info
->src1_reg
);
617 if (reg_info
->src2_imm
) {
618 uint16_t imm
= decode_scalar_imm(reg_info
->src2_reg
,
620 print_immediate(imm
);
622 print_scalar_src(alu_field
->src2
, reg_info
->src2_reg
);
628 print_branch_op(int op
)
631 case midgard_jmp_writeout_op_branch_uncond
:
635 case midgard_jmp_writeout_op_branch_cond
:
639 case midgard_jmp_writeout_op_writeout
:
643 case midgard_jmp_writeout_op_tilebuffer_pending
:
644 printf("tilebuffer.");
647 case midgard_jmp_writeout_op_discard
:
652 printf("unk%d.", op
);
658 print_branch_cond(int cond
)
661 case midgard_condition_write0
:
665 case midgard_condition_false
:
669 case midgard_condition_true
:
673 case midgard_condition_always
:
678 printf("unk%X", cond
);
684 print_compact_branch_writeout_field(uint16_t word
)
686 midgard_jmp_writeout_op op
= word
& 0x7;
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 ");
694 if (br_uncond
.unknown
!= 1)
695 printf("unknown:%d, ", br_uncond
.unknown
);
697 if (br_uncond
.offset
>= 0)
700 printf("%d -> ", br_uncond
.offset
);
701 print_tag_short(br_uncond
.dest_tag
);
707 case midgard_jmp_writeout_op_branch_cond
:
708 case midgard_jmp_writeout_op_writeout
:
709 case midgard_jmp_writeout_op_discard
:
711 midgard_branch_cond br_cond
;
712 memcpy((char *) &br_cond
, (char *) &word
, sizeof(br_cond
));
716 print_branch_op(br_cond
.op
);
717 print_branch_cond(br_cond
.cond
);
721 if (br_cond
.offset
>= 0)
724 printf("%d -> ", br_cond
.offset
);
725 print_tag_short(br_cond
.dest_tag
);
734 print_extended_branch_writeout_field(uint8_t *words
)
736 midgard_branch_extended br
;
737 memcpy((char *) &br
, (char *) words
, sizeof(br
));
741 print_branch_op(br
.op
);
743 /* Condition repeated 8 times in all known cases. Check this. */
745 unsigned cond
= br
.cond
& 0x3;
747 for (unsigned i
= 0; i
< 16; i
+= 2) {
748 assert(((br
.cond
>> i
) & 0x3) == cond
);
751 print_branch_cond(cond
);
754 printf(".unknown%d", br
.unknown
);
761 printf("%d -> ", br
.offset
);
762 print_tag_short(br
.dest_tag
);
767 num_alu_fields_enabled(uint32_t control_word
)
771 if ((control_word
>> 17) & 1)
774 if ((control_word
>> 19) & 1)
777 if ((control_word
>> 21) & 1)
780 if ((control_word
>> 23) & 1)
783 if ((control_word
>> 25) & 1)
790 float_bitcast(uint32_t integer
)
802 print_alu_word(uint32_t *words
, unsigned num_quad_words
,
805 uint32_t control_word
= words
[0];
806 uint16_t *beginning_ptr
= (uint16_t *)(words
+ 1);
807 unsigned num_fields
= num_alu_fields_enabled(control_word
);
808 uint16_t *word_ptr
= beginning_ptr
+ num_fields
;
809 unsigned num_words
= 2 + num_fields
;
811 if ((control_word
>> 16) & 1)
812 printf("unknown bit 16 enabled\n");
814 if ((control_word
>> 17) & 1) {
815 print_vector_field("vmul", word_ptr
, *beginning_ptr
, tabs
);
821 if ((control_word
>> 18) & 1)
822 printf("unknown bit 18 enabled\n");
824 if ((control_word
>> 19) & 1) {
825 print_scalar_field("sadd", word_ptr
, *beginning_ptr
, tabs
);
831 if ((control_word
>> 20) & 1)
832 printf("unknown bit 20 enabled\n");
834 if ((control_word
>> 21) & 1) {
835 print_vector_field("vadd", word_ptr
, *beginning_ptr
, tabs
);
841 if ((control_word
>> 22) & 1)
842 printf("unknown bit 22 enabled\n");
844 if ((control_word
>> 23) & 1) {
845 print_scalar_field("smul", word_ptr
, *beginning_ptr
, tabs
);
851 if ((control_word
>> 24) & 1)
852 printf("unknown bit 24 enabled\n");
854 if ((control_word
>> 25) & 1) {
855 print_vector_field("lut", word_ptr
, *beginning_ptr
, tabs
);
861 if ((control_word
>> 26) & 1) {
862 print_compact_branch_writeout_field(*word_ptr
);
867 if ((control_word
>> 27) & 1) {
868 print_extended_branch_writeout_field((uint8_t *) word_ptr
);
873 if (num_quad_words
> (num_words
+ 7) / 8) {
874 assert(num_quad_words
== (num_words
+ 15) / 8);
875 //Assume that the extra quadword is constants
876 void *consts
= words
+ (4 * num_quad_words
- 4);
878 if (is_embedded_constant_int
) {
879 if (is_embedded_constant_half
) {
880 int16_t *sconsts
= (int16_t *) consts
;
881 printf("sconstants %d, %d, %d, %d\n",
887 int32_t *iconsts
= (int32_t *) consts
;
888 printf("iconstants %d, %d, %d, %d\n",
895 if (is_embedded_constant_half
) {
896 uint16_t *hconsts
= (uint16_t *) consts
;
897 printf("hconstants %g, %g, %g, %g\n",
898 _mesa_half_to_float(hconsts
[0]),
899 _mesa_half_to_float(hconsts
[1]),
900 _mesa_half_to_float(hconsts
[2]),
901 _mesa_half_to_float(hconsts
[3]));
903 uint32_t *fconsts
= (uint32_t *) consts
;
904 printf("fconstants %g, %g, %g, %g\n",
905 float_bitcast(fconsts
[0]),
906 float_bitcast(fconsts
[1]),
907 float_bitcast(fconsts
[2]),
908 float_bitcast(fconsts
[3]));
916 print_varying_parameters(midgard_load_store_word
*word
)
918 midgard_varying_parameter param
;
919 unsigned v
= word
->varying_parameters
;
920 memcpy(¶m
, &v
, sizeof(param
));
922 if (param
.is_varying
) {
923 /* If a varying, there are qualifiers */
927 if (param
.interpolation
!= midgard_interp_default
) {
928 if (param
.interpolation
== midgard_interp_centroid
)
931 printf(".interp%d", param
.interpolation
);
934 if (param
.modifier
!= midgard_varying_mod_none
) {
935 if (param
.modifier
== midgard_varying_mod_perspective_w
)
936 printf(".perspectivew");
937 else if (param
.modifier
== midgard_varying_mod_perspective_z
)
938 printf(".perspectivez");
940 printf(".mod%d", param
.modifier
);
942 } else if (param
.flat
|| param
.interpolation
|| param
.modifier
) {
943 printf(" /* is_varying not set but varying metadata attached */");
946 if (param
.zero0
|| param
.zero1
|| param
.zero2
)
947 printf(" /* zero tripped, %d %d %d */ ", param
.zero0
, param
.zero1
, param
.zero2
);
951 is_op_varying(unsigned op
)
954 case midgard_op_st_vary_16
:
955 case midgard_op_st_vary_32
:
956 case midgard_op_st_vary_32i
:
957 case midgard_op_st_vary_32u
:
958 case midgard_op_ld_vary_16
:
959 case midgard_op_ld_vary_32
:
960 case midgard_op_ld_vary_32i
:
961 case midgard_op_ld_vary_32u
:
969 print_load_store_instr(uint64_t data
,
972 midgard_load_store_word
*word
= (midgard_load_store_word
*) &data
;
974 print_ld_st_opcode(word
->op
);
976 if (is_op_varying(word
->op
))
977 print_varying_parameters(word
);
979 printf(" r%d", word
->reg
);
980 print_mask_4(word
->mask
);
982 int address
= word
->address
;
984 if (word
->op
== midgard_op_ld_uniform_32
) {
985 /* Uniforms use their own addressing scheme */
987 int lo
= word
->varying_parameters
>> 7;
988 int hi
= word
->address
;
990 /* TODO: Combine fields logically */
991 address
= (hi
<< 3) | lo
;
994 printf(", %d", address
);
996 print_swizzle_vec4(word
->swizzle
, false, false);
998 printf(", 0x%X /* %X */\n", word
->unknown
, word
->varying_parameters
);
1002 print_load_store_word(uint32_t *word
, unsigned tabs
)
1004 midgard_load_store
*load_store
= (midgard_load_store
*) word
;
1006 if (load_store
->word1
!= 3) {
1007 print_load_store_instr(load_store
->word1
, tabs
);
1010 if (load_store
->word2
!= 3) {
1011 print_load_store_instr(load_store
->word2
, tabs
);
1016 print_texture_reg(bool full
, bool select
, bool upper
)
1019 printf("r%d", REG_TEX_BASE
+ select
);
1021 printf("hr%d", (REG_TEX_BASE
+ select
) * 2 + upper
);
1024 printf("// error: out full / upper mutually exclusive\n");
1029 print_texture_reg_triple(unsigned triple
)
1031 bool full
= triple
& 1;
1032 bool select
= triple
& 2;
1033 bool upper
= triple
& 4;
1035 print_texture_reg(full
, select
, upper
);
1039 print_texture_format(int format
)
1041 /* Act like a modifier */
1045 DEFINE_CASE(MALI_TEX_1D
, "1d");
1046 DEFINE_CASE(MALI_TEX_2D
, "2d");
1047 DEFINE_CASE(MALI_TEX_3D
, "3d");
1048 DEFINE_CASE(MALI_TEX_CUBE
, "cube");
1051 unreachable("Bad format");
1056 print_texture_op(unsigned op
, bool gather
)
1058 /* Act like a bare name, like ESSL functions */
1061 printf("textureGather");
1063 unsigned component
= op
>> 4;
1064 unsigned bottom
= op
& 0xF;
1067 printf("_unk%d", bottom
);
1069 printf(".%c", components
[component
]);
1074 DEFINE_CASE(TEXTURE_OP_NORMAL
, "texture");
1075 DEFINE_CASE(TEXTURE_OP_LOD
, "textureLod");
1076 DEFINE_CASE(TEXTURE_OP_TEXEL_FETCH
, "texelFetch");
1079 printf("tex_%d", op
);
1085 texture_op_takes_bias(unsigned op
)
1087 return op
== TEXTURE_OP_NORMAL
;
1091 sampler_type_name(enum mali_sampler_type t
)
1094 case MALI_SAMPLER_FLOAT
:
1096 case MALI_SAMPLER_UNSIGNED
:
1098 case MALI_SAMPLER_SIGNED
:
1109 print_texture_word(uint32_t *word
, unsigned tabs
)
1111 midgard_texture_word
*texture
= (midgard_texture_word
*) word
;
1113 /* Broad category of texture operation in question */
1114 print_texture_op(texture
->op
, texture
->is_gather
);
1116 /* Specific format in question */
1117 print_texture_format(texture
->format
);
1119 assert(texture
->zero
== 0);
1121 /* Instruction "modifiers" parallel the ALU instructions. */
1123 if (texture
->shadow
)
1134 print_texture_reg(texture
->out_full
, texture
->out_reg_select
, texture
->out_upper
);
1135 print_mask_4(texture
->mask
);
1138 printf("texture%d, ", texture
->texture_handle
);
1140 /* Print the type, GL style */
1141 printf("%c", sampler_type_name(texture
->sampler_type
));
1142 printf("sampler%d", texture
->sampler_handle
);
1143 print_swizzle_vec4(texture
->swizzle
, false, false);
1146 print_texture_reg(texture
->in_reg_full
, texture
->in_reg_select
, texture
->in_reg_upper
);
1147 print_swizzle_vec4(texture
->in_reg_swizzle
, false, false);
1149 /* There is *always* an offset attached. Of
1150 * course, that offset is just immediate #0 for a
1151 * GLES call that doesn't take an offset. If there
1152 * is a non-negative non-zero offset, this is
1153 * specified in immediate offset mode, with the
1154 * values in the offset_* fields as immediates. If
1155 * this is a negative offset, we instead switch to
1156 * a register offset mode, where the offset_*
1157 * fields become register triplets */
1159 if (texture
->offset_register
) {
1161 print_texture_reg_triple(texture
->offset_x
);
1163 /* The less questions you ask, the better. */
1165 unsigned swizzle_lo
, swizzle_hi
;
1166 unsigned orig_y
= texture
->offset_y
;
1167 unsigned orig_z
= texture
->offset_z
;
1169 memcpy(&swizzle_lo
, &orig_y
, sizeof(unsigned));
1170 memcpy(&swizzle_hi
, &orig_z
, sizeof(unsigned));
1172 /* Duplicate hi swizzle over */
1173 assert(swizzle_hi
< 4);
1174 swizzle_hi
= (swizzle_hi
<< 2) | swizzle_hi
;
1176 unsigned swiz
= (swizzle_lo
<< 4) | swizzle_hi
;
1177 unsigned reversed
= util_bitreverse(swiz
) >> 24;
1178 print_swizzle_vec4(reversed
, false, false);
1181 } else if (texture
->offset_x
|| texture
->offset_y
|| texture
->offset_z
) {
1182 /* Only select ops allow negative immediate offsets, verify */
1184 bool neg_x
= texture
->offset_x
< 0;
1185 bool neg_y
= texture
->offset_y
< 0;
1186 bool neg_z
= texture
->offset_z
< 0;
1187 bool any_neg
= neg_x
|| neg_y
|| neg_z
;
1189 if (any_neg
&& texture
->op
!= TEXTURE_OP_TEXEL_FETCH
)
1190 printf("/* invalid negative */ ");
1192 /* Regardless, just print the immediate offset */
1194 printf(" + <%d, %d, %d>, ",
1202 char lod_operand
= texture_op_takes_bias(texture
->op
) ? '+' : '=';
1204 if (texture
->lod_register
) {
1205 midgard_tex_register_select sel
;
1206 uint8_t raw
= texture
->bias
;
1207 memcpy(&sel
, &raw
, sizeof(raw
));
1209 unsigned c
= (sel
.component_hi
<< 1) | sel
.component_lo
;
1211 printf("lod %c ", lod_operand
);
1212 print_texture_reg(sel
.full
, sel
.select
, sel
.upper
);
1213 printf(".%c, ", components
[c
]);
1215 if (!sel
.component_hi
)
1216 printf(" /* gradient? */");
1218 if (texture
->bias_int
)
1219 printf(" /* bias_int = 0x%X */", texture
->bias_int
);
1222 printf(" /* sel.zero = 0x%X */", sel
.zero
);
1223 } else if (texture
->op
== TEXTURE_OP_TEXEL_FETCH
) {
1224 /* For texel fetch, the int LOD is in the fractional place and
1225 * there is no fraction / possibility of bias. We *always* have
1226 * an explicit LOD, even if it's zero. */
1228 if (texture
->bias_int
)
1229 printf(" /* bias_int = 0x%X */ ", texture
->bias_int
);
1231 printf("lod = %d, ", texture
->bias
);
1232 } else if (texture
->bias
|| texture
->bias_int
) {
1233 signed bias_int
= texture
->bias_int
;
1234 float bias_frac
= texture
->bias
/ 256.0f
;
1235 float bias
= bias_int
+ bias_frac
;
1237 bool is_bias
= texture_op_takes_bias(texture
->op
);
1238 char sign
= (bias
>= 0.0) ? '+' : '-';
1239 char operand
= is_bias
? sign
: '=';
1241 printf("lod %c %f, ", operand
, fabsf(bias
));
1246 /* While not zero in general, for these simple instructions the
1247 * following unknowns are zero, so we don't include them */
1249 if (texture
->unknown2
||
1250 texture
->unknown4
||
1251 texture
->unknownA
||
1252 texture
->unknown8
) {
1253 printf("// unknown2 = 0x%x\n", texture
->unknown2
);
1254 printf("// unknown4 = 0x%x\n", texture
->unknown4
);
1255 printf("// unknownA = 0x%x\n", texture
->unknownA
);
1256 printf("// unknown8 = 0x%x\n", texture
->unknown8
);
1261 disassemble_midgard(uint8_t *code
, size_t size
)
1263 uint32_t *words
= (uint32_t *) code
;
1264 unsigned num_words
= size
/ 4;
1267 bool prefetch_flag
= false;
1269 int last_next_tag
= -1;
1273 while (i
< num_words
) {
1274 unsigned tag
= words
[i
] & 0xF;
1275 unsigned next_tag
= (words
[i
] >> 4) & 0xF;
1276 unsigned num_quad_words
= midgard_word_size
[tag
];
1279 if (last_next_tag
> 1) {
1280 if (last_next_tag
!= tag
) {
1281 printf("/* TAG ERROR got ");
1282 print_tag_short(tag
);
1283 printf(" expected ");
1284 print_tag_short(last_next_tag
);
1288 /* TODO: Check ALU case */
1291 last_next_tag
= next_tag
;
1293 switch (midgard_word_types
[tag
]) {
1294 case midgard_word_type_texture
:
1295 print_texture_word(&words
[i
], tabs
);
1298 case midgard_word_type_load_store
:
1299 print_load_store_word(&words
[i
], tabs
);
1302 case midgard_word_type_alu
:
1303 print_alu_word(&words
[i
], num_quad_words
, tabs
);
1308 /* Reset word static analysis state */
1309 is_embedded_constant_half
= false;
1310 is_embedded_constant_int
= false;
1315 printf("Unknown word type %u:\n", words
[i
] & 0xF);
1317 print_quad_word(&words
[i
], tabs
);
1324 unsigned next
= (words
[i
] & 0xF0) >> 4;
1326 i
+= 4 * num_quad_words
;
1328 /* Break based on instruction prefetch flag */
1330 if (i
< num_words
&& next
== 1) {
1331 prefetch_flag
= true;
1333 if (midgard_word_types
[words
[i
] & 0xF] != midgard_word_type_alu
)