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 codes are a LUT in the general case, but simply repeated 8 times for single-channel conditions.. Check this. */
745 bool single_channel
= true;
747 for (unsigned i
= 0; i
< 16; i
+= 2) {
748 single_channel
&= (((br
.cond
>> i
) & 0x3) == (br
.cond
& 0x3));
752 print_branch_cond(br
.cond
& 0x3);
754 printf("lut%X", br
.cond
);
757 printf(".unknown%d", br
.unknown
);
764 printf("%d -> ", br
.offset
);
765 print_tag_short(br
.dest_tag
);
770 num_alu_fields_enabled(uint32_t control_word
)
774 if ((control_word
>> 17) & 1)
777 if ((control_word
>> 19) & 1)
780 if ((control_word
>> 21) & 1)
783 if ((control_word
>> 23) & 1)
786 if ((control_word
>> 25) & 1)
793 float_bitcast(uint32_t integer
)
805 print_alu_word(uint32_t *words
, unsigned num_quad_words
,
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
;
814 if ((control_word
>> 16) & 1)
815 printf("unknown bit 16 enabled\n");
817 if ((control_word
>> 17) & 1) {
818 print_vector_field("vmul", word_ptr
, *beginning_ptr
, tabs
);
824 if ((control_word
>> 18) & 1)
825 printf("unknown bit 18 enabled\n");
827 if ((control_word
>> 19) & 1) {
828 print_scalar_field("sadd", word_ptr
, *beginning_ptr
, tabs
);
834 if ((control_word
>> 20) & 1)
835 printf("unknown bit 20 enabled\n");
837 if ((control_word
>> 21) & 1) {
838 print_vector_field("vadd", word_ptr
, *beginning_ptr
, tabs
);
844 if ((control_word
>> 22) & 1)
845 printf("unknown bit 22 enabled\n");
847 if ((control_word
>> 23) & 1) {
848 print_scalar_field("smul", word_ptr
, *beginning_ptr
, tabs
);
854 if ((control_word
>> 24) & 1)
855 printf("unknown bit 24 enabled\n");
857 if ((control_word
>> 25) & 1) {
858 print_vector_field("lut", word_ptr
, *beginning_ptr
, tabs
);
864 if ((control_word
>> 26) & 1) {
865 print_compact_branch_writeout_field(*word_ptr
);
870 if ((control_word
>> 27) & 1) {
871 print_extended_branch_writeout_field((uint8_t *) word_ptr
);
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);
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",
890 int32_t *iconsts
= (int32_t *) consts
;
891 printf("iconstants %d, %d, %d, %d\n",
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]));
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]));
919 print_varying_parameters(midgard_load_store_word
*word
)
921 midgard_varying_parameter param
;
922 unsigned v
= word
->varying_parameters
;
923 memcpy(¶m
, &v
, sizeof(param
));
925 if (param
.is_varying
) {
926 /* If a varying, there are qualifiers */
930 if (param
.interpolation
!= midgard_interp_default
) {
931 if (param
.interpolation
== midgard_interp_centroid
)
934 printf(".interp%d", param
.interpolation
);
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");
943 printf(".mod%d", param
.modifier
);
945 } else if (param
.flat
|| param
.interpolation
|| param
.modifier
) {
946 printf(" /* is_varying not set but varying metadata attached */");
949 if (param
.zero0
|| param
.zero1
|| param
.zero2
)
950 printf(" /* zero tripped, %d %d %d */ ", param
.zero0
, param
.zero1
, param
.zero2
);
954 is_op_varying(unsigned 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
:
972 print_load_store_arg(uint8_t arg
)
974 /* Try to interpret as a register */
975 midgard_ldst_register_select sel
;
976 memcpy(&sel
, &arg
, sizeof(arg
));
978 unsigned reg
= REGISTER_LDST_BASE
+ sel
.select
;
979 char comp
= components
[sel
.component
];
981 printf("r%d.%c /* 0x%X */", reg
, comp
, arg
);
985 print_load_store_instr(uint64_t data
,
988 midgard_load_store_word
*word
= (midgard_load_store_word
*) &data
;
990 print_ld_st_opcode(word
->op
);
992 if (is_op_varying(word
->op
))
993 print_varying_parameters(word
);
995 printf(" r%d", word
->reg
);
996 print_mask_4(word
->mask
);
998 int address
= word
->address
;
1000 if (word
->op
== midgard_op_ld_uniform_32
) {
1001 /* Uniforms use their own addressing scheme */
1003 int lo
= word
->varying_parameters
>> 7;
1004 int hi
= word
->address
;
1006 /* TODO: Combine fields logically */
1007 address
= (hi
<< 3) | lo
;
1010 printf(", %d", address
);
1012 print_swizzle_vec4(word
->swizzle
, false, false);
1015 print_load_store_arg(word
->arg_1
);
1017 print_load_store_arg(word
->arg_2
);
1018 printf(" /* %X */\n", word
->varying_parameters
);
1022 print_load_store_word(uint32_t *word
, unsigned tabs
)
1024 midgard_load_store
*load_store
= (midgard_load_store
*) word
;
1026 if (load_store
->word1
!= 3) {
1027 print_load_store_instr(load_store
->word1
, tabs
);
1030 if (load_store
->word2
!= 3) {
1031 print_load_store_instr(load_store
->word2
, tabs
);
1036 print_texture_reg(bool full
, bool select
, bool upper
)
1039 printf("r%d", REG_TEX_BASE
+ select
);
1041 printf("hr%d", (REG_TEX_BASE
+ select
) * 2 + upper
);
1044 printf("// error: out full / upper mutually exclusive\n");
1049 print_texture_reg_triple(unsigned triple
)
1051 bool full
= triple
& 1;
1052 bool select
= triple
& 2;
1053 bool upper
= triple
& 4;
1055 print_texture_reg(full
, select
, upper
);
1059 print_texture_format(int format
)
1061 /* Act like a modifier */
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");
1071 unreachable("Bad format");
1076 print_texture_op(unsigned op
, bool gather
)
1078 /* Act like a bare name, like ESSL functions */
1081 printf("textureGather");
1083 unsigned component
= op
>> 4;
1084 unsigned bottom
= op
& 0xF;
1087 printf("_unk%d", bottom
);
1089 printf(".%c", components
[component
]);
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");
1101 printf("tex_%X", op
);
1107 texture_op_takes_bias(unsigned op
)
1109 return op
== TEXTURE_OP_NORMAL
;
1113 sampler_type_name(enum mali_sampler_type t
)
1116 case MALI_SAMPLER_FLOAT
:
1118 case MALI_SAMPLER_UNSIGNED
:
1120 case MALI_SAMPLER_SIGNED
:
1131 print_texture_word(uint32_t *word
, unsigned tabs
)
1133 midgard_texture_word
*texture
= (midgard_texture_word
*) word
;
1135 /* Broad category of texture operation in question */
1136 print_texture_op(texture
->op
, texture
->is_gather
);
1138 /* Specific format in question */
1139 print_texture_format(texture
->format
);
1141 assert(texture
->zero
== 0);
1143 /* Instruction "modifiers" parallel the ALU instructions. */
1145 if (texture
->shadow
)
1156 print_texture_reg(texture
->out_full
, texture
->out_reg_select
, texture
->out_upper
);
1157 print_mask_4(texture
->mask
);
1160 printf("texture%d, ", texture
->texture_handle
);
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);
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);
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 */
1181 if (texture
->offset_register
) {
1183 print_texture_reg_triple(texture
->offset_x
);
1185 /* The less questions you ask, the better. */
1187 unsigned swizzle_lo
, swizzle_hi
;
1188 unsigned orig_y
= texture
->offset_y
;
1189 unsigned orig_z
= texture
->offset_z
;
1191 memcpy(&swizzle_lo
, &orig_y
, sizeof(unsigned));
1192 memcpy(&swizzle_hi
, &orig_z
, sizeof(unsigned));
1194 /* Duplicate hi swizzle over */
1195 assert(swizzle_hi
< 4);
1196 swizzle_hi
= (swizzle_hi
<< 2) | swizzle_hi
;
1198 unsigned swiz
= (swizzle_lo
<< 4) | swizzle_hi
;
1199 unsigned reversed
= util_bitreverse(swiz
) >> 24;
1200 print_swizzle_vec4(reversed
, false, false);
1203 } else if (texture
->offset_x
|| texture
->offset_y
|| texture
->offset_z
) {
1204 /* Only select ops allow negative immediate offsets, verify */
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
;
1211 if (any_neg
&& texture
->op
!= TEXTURE_OP_TEXEL_FETCH
)
1212 printf("/* invalid negative */ ");
1214 /* Regardless, just print the immediate offset */
1216 printf(" + <%d, %d, %d>, ",
1224 char lod_operand
= texture_op_takes_bias(texture
->op
) ? '+' : '=';
1226 if (texture
->lod_register
) {
1227 midgard_tex_register_select sel
;
1228 uint8_t raw
= texture
->bias
;
1229 memcpy(&sel
, &raw
, sizeof(raw
));
1231 printf("lod %c ", lod_operand
);
1232 print_texture_reg(sel
.full
, sel
.select
, sel
.upper
);
1233 printf(".%c, ", components
[sel
.component
]);
1235 if (texture
->bias_int
)
1236 printf(" /* bias_int = 0x%X */", texture
->bias_int
);
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. */
1245 if (texture
->bias_int
)
1246 printf(" /* bias_int = 0x%X */ ", texture
->bias_int
);
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
;
1254 bool is_bias
= texture_op_takes_bias(texture
->op
);
1255 char sign
= (bias
>= 0.0) ? '+' : '-';
1256 char operand
= is_bias
? sign
: '=';
1258 printf("lod %c %f, ", operand
, fabsf(bias
));
1263 /* While not zero in general, for these simple instructions the
1264 * following unknowns are zero, so we don't include them */
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
);
1278 disassemble_midgard(uint8_t *code
, size_t size
)
1280 uint32_t *words
= (uint32_t *) code
;
1281 unsigned num_words
= size
/ 4;
1284 bool prefetch_flag
= false;
1286 int last_next_tag
= -1;
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
];
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
);
1305 /* TODO: Check ALU case */
1308 last_next_tag
= next_tag
;
1310 switch (midgard_word_types
[tag
]) {
1311 case midgard_word_type_texture
:
1312 print_texture_word(&words
[i
], tabs
);
1315 case midgard_word_type_load_store
:
1316 print_load_store_word(&words
[i
], tabs
);
1319 case midgard_word_type_alu
:
1320 print_alu_word(&words
[i
], num_quad_words
, tabs
);
1325 /* Reset word static analysis state */
1326 is_embedded_constant_half
= false;
1327 is_embedded_constant_int
= false;
1332 printf("Unknown word type %u:\n", words
[i
] & 0xF);
1334 print_quad_word(&words
[i
], tabs
);
1341 unsigned next
= (words
[i
] & 0xF0) >> 4;
1343 i
+= 4 * num_quad_words
;
1345 /* Break based on instruction prefetch flag */
1347 if (i
< num_words
&& next
== 1) {
1348 prefetch_flag
= true;
1350 if (midgard_word_types
[words
[i
] & 0xF] != midgard_word_type_alu
)