2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
32 #define PTRID(x) ((unsigned long)(x))
34 /* ansi escape sequences: */
35 #define RESET "\x1b[0m"
36 #define RED "\x1b[0;31m"
37 #define GREEN "\x1b[0;32m"
38 #define BLUE "\x1b[0;34m"
39 #define MAGENTA "\x1b[0;35m"
41 /* syntax coloring, mostly to make it easier to see different sorts of
42 * srcs (immediate, constant, ssa, array, ...)
44 #define SYN_REG(x) RED x RESET
45 #define SYN_IMMED(x) GREEN x RESET
46 #define SYN_CONST(x) GREEN x RESET
47 #define SYN_SSA(x) BLUE x RESET
48 #define SYN_ARRAY(x) MAGENTA x RESET
51 type_name(type_t type
)
53 static const char *type_names
[] = {
63 return type_names
[type
];
66 static void print_instr_name(struct ir3_instruction
*instr
, bool flags
)
71 printf("%04u:", instr
->serialno
);
73 printf("%04u:", instr
->name
);
74 printf("%04u:", instr
->ip
);
75 if (instr
->flags
& IR3_INSTR_UNUSED
) {
78 printf("%03u: ", instr
->use_count
);
83 if (instr
->flags
& IR3_INSTR_SY
)
85 if (instr
->flags
& IR3_INSTR_SS
)
87 if (instr
->flags
& IR3_INSTR_JP
)
90 printf("(rpt%d)", instr
->repeat
);
92 printf("(nop%d)", instr
->nop
);
93 if (instr
->flags
& IR3_INSTR_UL
)
100 switch (instr
->opc
) {
101 case OPC_META_INPUT
: printf("_meta:in"); break;
102 case OPC_META_SPLIT
: printf("_meta:split"); break;
103 case OPC_META_COLLECT
: printf("_meta:collect"); break;
104 case OPC_META_TEX_PREFETCH
: printf("_meta:tex_prefetch"); break;
106 /* shouldn't hit here.. just for debugging: */
107 default: printf("_meta:%d", instr
->opc
); break;
109 } else if (instr
->opc
== OPC_MOV
) {
110 if (instr
->cat1
.src_type
== instr
->cat1
.dst_type
)
114 printf(".%s%s", type_name(instr
->cat1
.src_type
),
115 type_name(instr
->cat1
.dst_type
));
117 printf("%s", ir3_instr_name(instr
));
118 if (instr
->flags
& IR3_INSTR_3D
)
120 if (instr
->flags
& IR3_INSTR_A
)
122 if (instr
->flags
& IR3_INSTR_O
)
124 if (instr
->flags
& IR3_INSTR_P
)
126 if (instr
->flags
& IR3_INSTR_S
)
128 if (instr
->flags
& IR3_INSTR_A1EN
)
130 if (instr
->opc
== OPC_LDC
)
131 printf(".offset%d", instr
->cat6
.d
);
132 if (instr
->flags
& IR3_INSTR_B
) {
134 is_tex(instr
) ? instr
->cat5
.tex_base
: instr
->cat6
.base
);
136 if (instr
->flags
& IR3_INSTR_S2EN
)
139 static const char *cond
[0x7] = {
148 switch (instr
->opc
) {
155 printf(".%s", cond
[instr
->cat2
.condition
& 0x7]);
163 static void print_reg_name(struct ir3_register
*reg
)
165 if ((reg
->flags
& (IR3_REG_FABS
| IR3_REG_SABS
)) &&
166 (reg
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
)))
168 else if (reg
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
))
170 else if (reg
->flags
& (IR3_REG_FABS
| IR3_REG_SABS
))
173 if (reg
->flags
& IR3_REG_R
)
176 if (reg
->flags
& IR3_REG_HIGH
)
178 if (reg
->flags
& IR3_REG_HALF
)
181 if (reg
->flags
& IR3_REG_IMMED
) {
182 printf(SYN_IMMED("imm[%f,%d,0x%x]"), reg
->fim_val
, reg
->iim_val
, reg
->iim_val
);
183 } else if (reg
->flags
& IR3_REG_ARRAY
) {
184 printf(SYN_ARRAY("arr[id=%u, offset=%d, size=%u"), reg
->array
.id
,
185 reg
->array
.offset
, reg
->size
);
186 /* for ARRAY we could have null src, for example first write
190 printf(SYN_ARRAY(", "));
191 printf(SYN_SSA("_["));
192 print_instr_name(reg
->instr
, false);
193 printf(SYN_SSA("]"));
195 printf(SYN_ARRAY("]"));
196 } else if (reg
->flags
& IR3_REG_SSA
) {
197 printf(SYN_SSA("_["));
198 print_instr_name(reg
->instr
, false);
199 printf(SYN_SSA("]"));
200 } else if (reg
->flags
& IR3_REG_RELATIV
) {
201 if (reg
->flags
& IR3_REG_CONST
)
202 printf(SYN_CONST("c<a0.x + %d>"), reg
->array
.offset
);
204 printf(SYN_REG("r<a0.x + %d>")" (%u)", reg
->array
.offset
, reg
->size
);
206 if (reg
->flags
& IR3_REG_CONST
)
207 printf(SYN_CONST("c%u.%c"), reg_num(reg
), "xyzw"[reg_comp(reg
)]);
209 printf(SYN_REG("r%u.%c"), reg_num(reg
), "xyzw"[reg_comp(reg
)]);
212 if (reg
->wrmask
> 0x1)
213 printf(" (wrmask=0x%x)", reg
->wrmask
);
219 for (int i
= 0; i
< lvl
; i
++)
224 print_instr(struct ir3_instruction
*instr
, int lvl
)
230 print_instr_name(instr
, true);
233 printf(" (%s)(", type_name(instr
->cat5
.type
));
234 for (i
= 0; i
< 4; i
++)
235 if (instr
->regs
[0]->wrmask
& (1 << i
))
236 printf("%c", "xyzw"[i
]);
238 } else if (instr
->regs_count
> 0) {
242 for (i
= 0; i
< instr
->regs_count
; i
++) {
243 struct ir3_register
*reg
= instr
->regs
[i
];
245 printf(i
? ", " : "");
249 if (is_tex(instr
) && !(instr
->flags
& IR3_INSTR_S2EN
)) {
250 if (!!(instr
->flags
& IR3_INSTR_B
)) {
251 if (!!(instr
->flags
& IR3_INSTR_A1EN
)) {
252 printf(", s#%d", instr
->cat5
.samp
);
254 printf(", s#%d, t#%d", instr
->cat5
.samp
& 0xf,
255 instr
->cat5
.samp
>> 4);
258 printf(", s#%d, t#%d", instr
->cat5
.samp
, instr
->cat5
.tex
);
262 if (instr
->address
) {
263 printf(", address=_");
265 print_instr_name(instr
->address
, false);
269 if (instr
->cp
.left
) {
272 print_instr_name(instr
->cp
.left
, false);
276 if (instr
->cp
.right
) {
279 print_instr_name(instr
->cp
.right
, false);
283 if (instr
->opc
== OPC_META_SPLIT
) {
284 printf(", off=%d", instr
->split
.off
);
285 } else if (instr
->opc
== OPC_META_TEX_PREFETCH
) {
286 printf(", tex=%d, samp=%d, input_offset=%d", instr
->prefetch
.tex
,
287 instr
->prefetch
.samp
, instr
->prefetch
.input_offset
);
290 if (is_flow(instr
) && instr
->cat0
.target
) {
291 /* the predicate register src is implied: */
292 if (instr
->opc
== OPC_B
) {
293 printf("r %sp0.x", instr
->cat0
.inv
? "!" : "");
295 printf(", target=block%u", block_id(instr
->cat0
.target
));
298 if (instr
->deps_count
) {
299 printf(", false-deps:");
300 for (unsigned i
= 0; i
< instr
->deps_count
; i
++) {
304 print_instr_name(instr
->deps
[i
], false);
312 void ir3_print_instr(struct ir3_instruction
*instr
)
314 print_instr(instr
, 0);
318 print_block(struct ir3_block
*block
, int lvl
)
320 tab(lvl
); printf("block%u {\n", block_id(block
));
322 /* computerator (ir3 assembler) doesn't really use blocks for flow
323 * control, so block->predecessors will be null.
325 if (block
->predecessors
&& block
->predecessors
->entries
> 0) {
329 set_foreach (block
->predecessors
, entry
) {
330 struct ir3_block
*pred
= (struct ir3_block
*)entry
->key
;
333 printf("block%u", block_id(pred
));
338 foreach_instr (instr
, &block
->instr_list
) {
339 print_instr(instr
, lvl
+1);
342 tab(lvl
+1); printf("/* keeps:\n");
343 for (unsigned i
= 0; i
< block
->keeps_count
; i
++) {
344 print_instr(block
->keeps
[i
], lvl
+2);
346 tab(lvl
+1); printf(" */\n");
348 if (block
->successors
[1]) {
349 /* leading into if/else: */
351 printf("/* succs: if _[");
352 print_instr_name(block
->condition
, false);
353 printf("] block%u; else block%u; */\n",
354 block_id(block
->successors
[0]),
355 block_id(block
->successors
[1]));
356 } else if (block
->successors
[0]) {
358 printf("/* succs: block%u; */\n",
359 block_id(block
->successors
[0]));
361 tab(lvl
); printf("}\n");
365 ir3_print(struct ir3
*ir
)
367 foreach_block (block
, &ir
->block_list
)
368 print_block(block
, 0);
370 foreach_output_n (out
, i
, ir
) {
371 printf("out%d: ", i
);