nir/print: fix coverity error
[mesa.git] / src / glsl / nir / nir_print.c
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include "shader_enums.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 static void
34 print_tabs(unsigned num_tabs, FILE *fp)
35 {
36 for (unsigned i = 0; i < num_tabs; i++)
37 fprintf(fp, "\t");
38 }
39
40 typedef struct {
41 FILE *fp;
42 nir_shader *shader;
43 /** map from nir_variable -> printable name */
44 struct hash_table *ht;
45
46 /** set of names used so far for nir_variables */
47 struct set *syms;
48
49 /* an index used to make new non-conflicting names */
50 unsigned index;
51 } print_state;
52
53 static void
54 print_register(nir_register *reg, print_state *state)
55 {
56 FILE *fp = state->fp;
57 if (reg->name != NULL)
58 fprintf(fp, "/* %s */ ", reg->name);
59 if (reg->is_global)
60 fprintf(fp, "gr%u", reg->index);
61 else
62 fprintf(fp, "r%u", reg->index);
63 }
64
65 static const char *sizes[] = { "error", "vec1", "vec2", "vec3", "vec4" };
66
67 static void
68 print_register_decl(nir_register *reg, print_state *state)
69 {
70 FILE *fp = state->fp;
71 fprintf(fp, "decl_reg %s ", sizes[reg->num_components]);
72 if (reg->is_packed)
73 fprintf(fp, "(packed) ");
74 print_register(reg, state);
75 if (reg->num_array_elems != 0)
76 fprintf(fp, "[%u]", reg->num_array_elems);
77 fprintf(fp, "\n");
78 }
79
80 static void
81 print_ssa_def(nir_ssa_def *def, print_state *state)
82 {
83 FILE *fp = state->fp;
84 if (def->name != NULL)
85 fprintf(fp, "/* %s */ ", def->name);
86 fprintf(fp, "%s ssa_%u", sizes[def->num_components], def->index);
87 }
88
89 static void
90 print_ssa_use(nir_ssa_def *def, print_state *state)
91 {
92 FILE *fp = state->fp;
93 if (def->name != NULL)
94 fprintf(fp, "/* %s */ ", def->name);
95 fprintf(fp, "ssa_%u", def->index);
96 }
97
98 static void print_src(nir_src *src, print_state *state);
99
100 static void
101 print_reg_src(nir_reg_src *src, print_state *state)
102 {
103 FILE *fp = state->fp;
104 print_register(src->reg, state);
105 if (src->reg->num_array_elems != 0) {
106 fprintf(fp, "[%u", src->base_offset);
107 if (src->indirect != NULL) {
108 fprintf(fp, " + ");
109 print_src(src->indirect, state);
110 }
111 fprintf(fp, "]");
112 }
113 }
114
115 static void
116 print_reg_dest(nir_reg_dest *dest, print_state *state)
117 {
118 FILE *fp = state->fp;
119 print_register(dest->reg, state);
120 if (dest->reg->num_array_elems != 0) {
121 fprintf(fp, "[%u", dest->base_offset);
122 if (dest->indirect != NULL) {
123 fprintf(fp, " + ");
124 print_src(dest->indirect, state);
125 }
126 fprintf(fp, "]");
127 }
128 }
129
130 static void
131 print_src(nir_src *src, print_state *state)
132 {
133 if (src->is_ssa)
134 print_ssa_use(src->ssa, state);
135 else
136 print_reg_src(&src->reg, state);
137 }
138
139 static void
140 print_dest(nir_dest *dest, print_state *state)
141 {
142 if (dest->is_ssa)
143 print_ssa_def(&dest->ssa, state);
144 else
145 print_reg_dest(&dest->reg, state);
146 }
147
148 static void
149 print_alu_src(nir_alu_instr *instr, unsigned src, print_state *state)
150 {
151 FILE *fp = state->fp;
152
153 if (instr->src[src].negate)
154 fprintf(fp, "-");
155 if (instr->src[src].abs)
156 fprintf(fp, "abs(");
157
158 print_src(&instr->src[src].src, state);
159
160 bool print_swizzle = false;
161 for (unsigned i = 0; i < 4; i++) {
162 if (!nir_alu_instr_channel_used(instr, src, i))
163 continue;
164
165 if (instr->src[src].swizzle[i] != i) {
166 print_swizzle = true;
167 break;
168 }
169 }
170
171 if (print_swizzle) {
172 fprintf(fp, ".");
173 for (unsigned i = 0; i < 4; i++) {
174 if (!nir_alu_instr_channel_used(instr, src, i))
175 continue;
176
177 fprintf(fp, "%c", "xyzw"[instr->src[src].swizzle[i]]);
178 }
179 }
180
181 if (instr->src[src].abs)
182 fprintf(fp, ")");
183 }
184
185 static void
186 print_alu_dest(nir_alu_dest *dest, print_state *state)
187 {
188 FILE *fp = state->fp;
189 /* we're going to print the saturate modifier later, after the opcode */
190
191 print_dest(&dest->dest, state);
192
193 if (!dest->dest.is_ssa &&
194 dest->write_mask != (1 << dest->dest.reg.reg->num_components) - 1) {
195 fprintf(fp, ".");
196 for (unsigned i = 0; i < 4; i++)
197 if ((dest->write_mask >> i) & 1)
198 fprintf(fp, "%c", "xyzw"[i]);
199 }
200 }
201
202 static void
203 print_alu_instr(nir_alu_instr *instr, print_state *state)
204 {
205 FILE *fp = state->fp;
206
207 print_alu_dest(&instr->dest, state);
208
209 fprintf(fp, " = %s", nir_op_infos[instr->op].name);
210 if (instr->dest.saturate)
211 fprintf(fp, ".sat");
212 fprintf(fp, " ");
213
214 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
215 if (i != 0)
216 fprintf(fp, ", ");
217
218 print_alu_src(instr, i, state);
219 }
220 }
221
222 static void
223 print_var_decl(nir_variable *var, print_state *state)
224 {
225 FILE *fp = state->fp;
226
227 fprintf(fp, "decl_var ");
228
229 const char *const cent = (var->data.centroid) ? "centroid " : "";
230 const char *const samp = (var->data.sample) ? "sample " : "";
231 const char *const inv = (var->data.invariant) ? "invariant " : "";
232 const char *const mode[] = { "shader_in ", "shader_out ", "", "",
233 "uniform ", "shader_storage", "system " };
234
235 fprintf(fp, "%s%s%s%s%s ",
236 cent, samp, inv, mode[var->data.mode],
237 glsl_interp_qualifier_name(var->data.interpolation));
238
239 glsl_print_type(var->type, fp);
240
241 struct set_entry *entry = NULL;
242 if (state->syms)
243 entry = _mesa_set_search(state->syms, var->name);
244
245 char *name;
246
247 if (entry != NULL) {
248 /* we have a collision with another name, append an @ + a unique index */
249 name = ralloc_asprintf(state->syms, "%s@%u", var->name, state->index++);
250 } else {
251 name = var->name;
252 }
253
254 fprintf(fp, " %s", name);
255
256 if (var->data.mode == nir_var_shader_in ||
257 var->data.mode == nir_var_shader_out ||
258 var->data.mode == nir_var_uniform ||
259 var->data.mode == nir_var_shader_storage) {
260 const char *loc = NULL;
261 char buf[4];
262
263 switch (state->shader->stage) {
264 case MESA_SHADER_VERTEX:
265 if (var->data.mode == nir_var_shader_in)
266 loc = gl_vert_attrib_name(var->data.location);
267 else if (var->data.mode == nir_var_shader_out)
268 loc = gl_varying_slot_name(var->data.location);
269 break;
270 case MESA_SHADER_GEOMETRY:
271 if ((var->data.mode == nir_var_shader_in) ||
272 (var->data.mode == nir_var_shader_out))
273 loc = gl_varying_slot_name(var->data.location);
274 break;
275 case MESA_SHADER_FRAGMENT:
276 if (var->data.mode == nir_var_shader_in)
277 loc = gl_varying_slot_name(var->data.location);
278 else if (var->data.mode == nir_var_shader_out)
279 loc = gl_frag_result_name(var->data.location);
280 break;
281 case MESA_SHADER_TESS_CTRL:
282 case MESA_SHADER_TESS_EVAL:
283 case MESA_SHADER_COMPUTE:
284 default:
285 /* TODO */
286 break;
287 }
288
289 if (!loc) {
290 snprintf(buf, sizeof(buf), "%u", var->data.location);
291 loc = buf;
292 }
293
294 fprintf(fp, " (%s, %u)", loc, var->data.driver_location);
295 }
296
297 fprintf(fp, "\n");
298
299 if (state->syms) {
300 _mesa_set_add(state->syms, name);
301 _mesa_hash_table_insert(state->ht, var, name);
302 }
303 }
304
305 static void
306 print_var(nir_variable *var, print_state *state)
307 {
308 FILE *fp = state->fp;
309 const char *name;
310 if (state->ht) {
311 struct hash_entry *entry = _mesa_hash_table_search(state->ht, var);
312
313 assert(entry != NULL);
314 name = entry->data;
315 } else {
316 name = var->name;
317 }
318
319 fprintf(fp, "%s", name);
320 }
321
322 static void
323 print_deref_var(nir_deref_var *deref, print_state *state)
324 {
325 print_var(deref->var, state);
326 }
327
328 static void
329 print_deref_array(nir_deref_array *deref, print_state *state)
330 {
331 FILE *fp = state->fp;
332 fprintf(fp, "[");
333 switch (deref->deref_array_type) {
334 case nir_deref_array_type_direct:
335 fprintf(fp, "%u", deref->base_offset);
336 break;
337 case nir_deref_array_type_indirect:
338 if (deref->base_offset != 0)
339 fprintf(fp, "%u + ", deref->base_offset);
340 print_src(&deref->indirect, state);
341 break;
342 case nir_deref_array_type_wildcard:
343 fprintf(fp, "*");
344 break;
345 }
346 fprintf(fp, "]");
347 }
348
349 static void
350 print_deref_struct(nir_deref_struct *deref, const struct glsl_type *parent_type,
351 print_state *state)
352 {
353 FILE *fp = state->fp;
354 fprintf(fp, ".%s", glsl_get_struct_elem_name(parent_type, deref->index));
355 }
356
357 static void
358 print_deref(nir_deref_var *deref, print_state *state)
359 {
360 nir_deref *tail = &deref->deref;
361 nir_deref *pretail = NULL;
362 while (tail != NULL) {
363 switch (tail->deref_type) {
364 case nir_deref_type_var:
365 assert(pretail == NULL);
366 assert(tail == &deref->deref);
367 print_deref_var(deref, state);
368 break;
369
370 case nir_deref_type_array:
371 assert(pretail != NULL);
372 print_deref_array(nir_deref_as_array(tail), state);
373 break;
374
375 case nir_deref_type_struct:
376 assert(pretail != NULL);
377 print_deref_struct(nir_deref_as_struct(tail),
378 pretail->type, state);
379 break;
380
381 default:
382 unreachable("Invalid deref type");
383 }
384
385 pretail = tail;
386 tail = pretail->child;
387 }
388 }
389
390 static void
391 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
392 {
393 unsigned num_srcs = nir_intrinsic_infos[instr->intrinsic].num_srcs;
394 FILE *fp = state->fp;
395
396 if (nir_intrinsic_infos[instr->intrinsic].has_dest) {
397 print_dest(&instr->dest, state);
398 fprintf(fp, " = ");
399 }
400
401 fprintf(fp, "intrinsic %s (", nir_intrinsic_infos[instr->intrinsic].name);
402
403 for (unsigned i = 0; i < num_srcs; i++) {
404 if (i != 0)
405 fprintf(fp, ", ");
406
407 print_src(&instr->src[i], state);
408 }
409
410 fprintf(fp, ") (");
411
412 unsigned num_vars = nir_intrinsic_infos[instr->intrinsic].num_variables;
413
414 for (unsigned i = 0; i < num_vars; i++) {
415 if (i != 0)
416 fprintf(fp, ", ");
417
418 print_deref(instr->variables[i], state);
419 }
420
421 fprintf(fp, ") (");
422
423 unsigned num_indices = nir_intrinsic_infos[instr->intrinsic].num_indices;
424
425 for (unsigned i = 0; i < num_indices; i++) {
426 if (i != 0)
427 fprintf(fp, ", ");
428
429 fprintf(fp, "%u", instr->const_index[i]);
430 }
431
432 fprintf(fp, ")");
433
434 if (!state->shader)
435 return;
436
437 struct exec_list *var_list = NULL;
438
439 switch (instr->intrinsic) {
440 case nir_intrinsic_load_uniform:
441 case nir_intrinsic_load_uniform_indirect:
442 var_list = &state->shader->uniforms;
443 break;
444 case nir_intrinsic_load_input:
445 case nir_intrinsic_load_input_indirect:
446 var_list = &state->shader->inputs;
447 break;
448 case nir_intrinsic_store_output:
449 case nir_intrinsic_store_output_indirect:
450 var_list = &state->shader->outputs;
451 break;
452 default:
453 return;
454 }
455
456 foreach_list_typed(nir_variable, var, node, var_list) {
457 if ((var->data.driver_location == instr->const_index[0]) &&
458 var->name) {
459 fprintf(fp, "\t/* %s */", var->name);
460 break;
461 }
462 }
463 }
464
465 static void
466 print_tex_instr(nir_tex_instr *instr, print_state *state)
467 {
468 FILE *fp = state->fp;
469
470 print_dest(&instr->dest, state);
471
472 fprintf(fp, " = ");
473
474 switch (instr->op) {
475 case nir_texop_tex:
476 fprintf(fp, "tex ");
477 break;
478 case nir_texop_txb:
479 fprintf(fp, "txb ");
480 break;
481 case nir_texop_txl:
482 fprintf(fp, "txl ");
483 break;
484 case nir_texop_txd:
485 fprintf(fp, "txd ");
486 break;
487 case nir_texop_txf:
488 fprintf(fp, "txf ");
489 break;
490 case nir_texop_txf_ms:
491 fprintf(fp, "txf_ms ");
492 break;
493 case nir_texop_txs:
494 fprintf(fp, "txs ");
495 break;
496 case nir_texop_lod:
497 fprintf(fp, "lod ");
498 break;
499 case nir_texop_tg4:
500 fprintf(fp, "tg4 ");
501 break;
502 case nir_texop_query_levels:
503 fprintf(fp, "query_levels ");
504 break;
505 case nir_texop_texture_samples:
506 fprintf(fp, "texture_samples ");
507 break;
508
509 default:
510 unreachable("Invalid texture operation");
511 break;
512 }
513
514 for (unsigned i = 0; i < instr->num_srcs; i++) {
515 print_src(&instr->src[i].src, state);
516
517 fprintf(fp, " ");
518
519 switch(instr->src[i].src_type) {
520 case nir_tex_src_coord:
521 fprintf(fp, "(coord)");
522 break;
523 case nir_tex_src_projector:
524 fprintf(fp, "(projector)");
525 break;
526 case nir_tex_src_comparitor:
527 fprintf(fp, "(comparitor)");
528 break;
529 case nir_tex_src_offset:
530 fprintf(fp, "(offset)");
531 break;
532 case nir_tex_src_bias:
533 fprintf(fp, "(bias)");
534 break;
535 case nir_tex_src_lod:
536 fprintf(fp, "(lod)");
537 break;
538 case nir_tex_src_ms_index:
539 fprintf(fp, "(ms_index)");
540 break;
541 case nir_tex_src_ddx:
542 fprintf(fp, "(ddx)");
543 break;
544 case nir_tex_src_ddy:
545 fprintf(fp, "(ddy)");
546 break;
547 case nir_tex_src_sampler_offset:
548 fprintf(fp, "(sampler_offset)");
549 break;
550
551 default:
552 unreachable("Invalid texture source type");
553 break;
554 }
555
556 fprintf(fp, ", ");
557 }
558
559 bool has_nonzero_offset = false;
560 for (unsigned i = 0; i < 4; i++) {
561 if (instr->const_offset[i] != 0) {
562 has_nonzero_offset = true;
563 break;
564 }
565 }
566
567 if (has_nonzero_offset) {
568 fprintf(fp, "[%i %i %i %i] (offset), ",
569 instr->const_offset[0], instr->const_offset[1],
570 instr->const_offset[2], instr->const_offset[3]);
571 }
572
573 if (instr->op == nir_texop_tg4) {
574 fprintf(fp, "%u (gather_component), ", instr->component);
575 }
576
577 if (instr->sampler) {
578 print_deref(instr->sampler, state);
579 } else {
580 fprintf(fp, "%u", instr->sampler_index);
581 }
582
583 fprintf(fp, " (sampler)");
584 }
585
586 static void
587 print_call_instr(nir_call_instr *instr, print_state *state)
588 {
589 FILE *fp = state->fp;
590
591 fprintf(fp, "call %s ", instr->callee->function->name);
592
593 for (unsigned i = 0; i < instr->num_params; i++) {
594 if (i != 0)
595 fprintf(fp, ", ");
596
597 print_deref(instr->params[i], state);
598 }
599
600 if (instr->return_deref != NULL) {
601 if (instr->num_params != 0)
602 fprintf(fp, ", ");
603 fprintf(fp, "returning ");
604 print_deref(instr->return_deref, state);
605 }
606 }
607
608 static void
609 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
610 {
611 FILE *fp = state->fp;
612
613 print_ssa_def(&instr->def, state);
614
615 fprintf(fp, " = load_const (");
616
617 for (unsigned i = 0; i < instr->def.num_components; i++) {
618 if (i != 0)
619 fprintf(fp, ", ");
620
621 /*
622 * we don't really know the type of the constant (if it will be used as a
623 * float or an int), so just print the raw constant in hex for fidelity
624 * and then print the float in a comment for readability.
625 */
626
627 fprintf(fp, "0x%08x /* %f */", instr->value.u[i], instr->value.f[i]);
628 }
629
630 fprintf(fp, ")");
631 }
632
633 static void
634 print_jump_instr(nir_jump_instr *instr, print_state *state)
635 {
636 FILE *fp = state->fp;
637
638 switch (instr->type) {
639 case nir_jump_break:
640 fprintf(fp, "break");
641 break;
642
643 case nir_jump_continue:
644 fprintf(fp, "continue");
645 break;
646
647 case nir_jump_return:
648 fprintf(fp, "return");
649 break;
650 }
651 }
652
653 static void
654 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
655 {
656 FILE *fp = state->fp;
657 print_ssa_def(&instr->def, state);
658 fprintf(fp, " = undefined");
659 }
660
661 static void
662 print_phi_instr(nir_phi_instr *instr, print_state *state)
663 {
664 FILE *fp = state->fp;
665 print_dest(&instr->dest, state);
666 fprintf(fp, " = phi ");
667 nir_foreach_phi_src(instr, src) {
668 if (&src->node != exec_list_get_head(&instr->srcs))
669 fprintf(fp, ", ");
670
671 fprintf(fp, "block_%u: ", src->pred->index);
672 print_src(&src->src, state);
673 }
674 }
675
676 static void
677 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
678 {
679 FILE *fp = state->fp;
680 nir_foreach_parallel_copy_entry(instr, entry) {
681 if (&entry->node != exec_list_get_head(&instr->entries))
682 fprintf(fp, "; ");
683
684 print_dest(&entry->dest, state);
685 fprintf(fp, " = ");
686 print_src(&entry->src, state);
687 }
688 }
689
690 static void
691 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
692 {
693 FILE *fp = state->fp;
694 print_tabs(tabs, fp);
695
696 switch (instr->type) {
697 case nir_instr_type_alu:
698 print_alu_instr(nir_instr_as_alu(instr), state);
699 break;
700
701 case nir_instr_type_call:
702 print_call_instr(nir_instr_as_call(instr), state);
703 break;
704
705 case nir_instr_type_intrinsic:
706 print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
707 break;
708
709 case nir_instr_type_tex:
710 print_tex_instr(nir_instr_as_tex(instr), state);
711 break;
712
713 case nir_instr_type_load_const:
714 print_load_const_instr(nir_instr_as_load_const(instr), state);
715 break;
716
717 case nir_instr_type_jump:
718 print_jump_instr(nir_instr_as_jump(instr), state);
719 break;
720
721 case nir_instr_type_ssa_undef:
722 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
723 break;
724
725 case nir_instr_type_phi:
726 print_phi_instr(nir_instr_as_phi(instr), state);
727 break;
728
729 case nir_instr_type_parallel_copy:
730 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
731 break;
732
733 default:
734 unreachable("Invalid instruction type");
735 break;
736 }
737 }
738
739 static int
740 compare_block_index(const void *p1, const void *p2)
741 {
742 const nir_block *block1 = *((const nir_block **) p1);
743 const nir_block *block2 = *((const nir_block **) p2);
744
745 return (int) block1->index - (int) block2->index;
746 }
747
748 static void print_cf_node(nir_cf_node *node, print_state *state,
749 unsigned tabs);
750
751 static void
752 print_block(nir_block *block, print_state *state, unsigned tabs)
753 {
754 FILE *fp = state->fp;
755
756 print_tabs(tabs, fp);
757 fprintf(fp, "block block_%u:\n", block->index);
758
759 /* sort the predecessors by index so we consistently print the same thing */
760
761 nir_block **preds =
762 malloc(block->predecessors->entries * sizeof(nir_block *));
763
764 struct set_entry *entry;
765 unsigned i = 0;
766 set_foreach(block->predecessors, entry) {
767 preds[i++] = (nir_block *) entry->key;
768 }
769
770 qsort(preds, block->predecessors->entries, sizeof(nir_block *),
771 compare_block_index);
772
773 print_tabs(tabs, fp);
774 fprintf(fp, "/* preds: ");
775 for (unsigned i = 0; i < block->predecessors->entries; i++) {
776 fprintf(fp, "block_%u ", preds[i]->index);
777 }
778 fprintf(fp, "*/\n");
779
780 free(preds);
781
782 nir_foreach_instr(block, instr) {
783 print_instr(instr, state, tabs);
784 fprintf(fp, "\n");
785 }
786
787 print_tabs(tabs, fp);
788 fprintf(fp, "/* succs: ");
789 for (unsigned i = 0; i < 2; i++)
790 if (block->successors[i]) {
791 fprintf(fp, "block_%u ", block->successors[i]->index);
792 }
793 fprintf(fp, "*/\n");
794 }
795
796 static void
797 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
798 {
799 FILE *fp = state->fp;
800
801 print_tabs(tabs, fp);
802 fprintf(fp, "if ");
803 print_src(&if_stmt->condition, state);
804 fprintf(fp, " {\n");
805 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
806 print_cf_node(node, state, tabs + 1);
807 }
808 print_tabs(tabs, fp);
809 fprintf(fp, "} else {\n");
810 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
811 print_cf_node(node, state, tabs + 1);
812 }
813 print_tabs(tabs, fp);
814 fprintf(fp, "}\n");
815 }
816
817 static void
818 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
819 {
820 FILE *fp = state->fp;
821
822 print_tabs(tabs, fp);
823 fprintf(fp, "loop {\n");
824 foreach_list_typed(nir_cf_node, node, node, &loop->body) {
825 print_cf_node(node, state, tabs + 1);
826 }
827 print_tabs(tabs, fp);
828 fprintf(fp, "}\n");
829 }
830
831 static void
832 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
833 {
834 switch (node->type) {
835 case nir_cf_node_block:
836 print_block(nir_cf_node_as_block(node), state, tabs);
837 break;
838
839 case nir_cf_node_if:
840 print_if(nir_cf_node_as_if(node), state, tabs);
841 break;
842
843 case nir_cf_node_loop:
844 print_loop(nir_cf_node_as_loop(node), state, tabs);
845 break;
846
847 default:
848 unreachable("Invalid CFG node type");
849 }
850 }
851
852 static void
853 print_function_impl(nir_function_impl *impl, print_state *state)
854 {
855 FILE *fp = state->fp;
856
857 fprintf(fp, "\nimpl %s ", impl->overload->function->name);
858
859 for (unsigned i = 0; i < impl->num_params; i++) {
860 if (i != 0)
861 fprintf(fp, ", ");
862
863 print_var(impl->params[i], state);
864 }
865
866 if (impl->return_var != NULL) {
867 if (impl->num_params != 0)
868 fprintf(fp, ", ");
869 fprintf(fp, "returning ");
870 print_var(impl->return_var, state);
871 }
872
873 fprintf(fp, "{\n");
874
875 foreach_list_typed(nir_variable, var, node, &impl->locals) {
876 fprintf(fp, "\t");
877 print_var_decl(var, state);
878 }
879
880 foreach_list_typed(nir_register, reg, node, &impl->registers) {
881 fprintf(fp, "\t");
882 print_register_decl(reg, state);
883 }
884
885 nir_index_blocks(impl);
886
887 foreach_list_typed(nir_cf_node, node, node, &impl->body) {
888 print_cf_node(node, state, 1);
889 }
890
891 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
892 }
893
894 static void
895 print_function_overload(nir_function_overload *overload,
896 print_state *state)
897 {
898 FILE *fp = state->fp;
899
900 fprintf(fp, "decl_overload %s ", overload->function->name);
901
902 for (unsigned i = 0; i < overload->num_params; i++) {
903 if (i != 0)
904 fprintf(fp, ", ");
905
906 switch (overload->params[i].param_type) {
907 case nir_parameter_in:
908 fprintf(fp, "in ");
909 break;
910 case nir_parameter_out:
911 fprintf(fp, "out ");
912 break;
913 case nir_parameter_inout:
914 fprintf(fp, "inout ");
915 break;
916 default:
917 unreachable("Invalid parameter type");
918 }
919
920 glsl_print_type(overload->params[i].type, fp);
921 }
922
923 if (overload->return_type != NULL) {
924 if (overload->num_params != 0)
925 fprintf(fp, ", ");
926 fprintf(fp, "returning ");
927 glsl_print_type(overload->return_type, fp);
928 }
929
930 fprintf(fp, "\n");
931
932 if (overload->impl != NULL) {
933 print_function_impl(overload->impl, state);
934 return;
935 }
936 }
937
938 static void
939 print_function(nir_function *func, print_state *state)
940 {
941 foreach_list_typed(nir_function_overload, overload, node, &func->overload_list) {
942 print_function_overload(overload, state);
943 }
944 }
945
946 static void
947 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
948 {
949 state->fp = fp;
950 state->shader = shader;
951 state->ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
952 _mesa_key_pointer_equal);
953 state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
954 _mesa_key_string_equal);
955 state->index = 0;
956 }
957
958 static void
959 destroy_print_state(print_state *state)
960 {
961 _mesa_hash_table_destroy(state->ht, NULL);
962 _mesa_set_destroy(state->syms, NULL);
963 }
964
965 void
966 nir_print_shader(nir_shader *shader, FILE *fp)
967 {
968 print_state state;
969 init_print_state(&state, shader, fp);
970
971 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->stage));
972
973 foreach_list_typed(nir_variable, var, node, &shader->uniforms) {
974 print_var_decl(var, &state);
975 }
976
977 foreach_list_typed(nir_variable, var, node, &shader->inputs) {
978 print_var_decl(var, &state);
979 }
980
981 foreach_list_typed(nir_variable, var, node, &shader->outputs) {
982 print_var_decl(var, &state);
983 }
984
985 foreach_list_typed(nir_variable, var, node, &shader->globals) {
986 print_var_decl(var, &state);
987 }
988
989 foreach_list_typed(nir_variable, var, node, &shader->system_values) {
990 print_var_decl(var, &state);
991 }
992
993 foreach_list_typed(nir_register, reg, node, &shader->registers) {
994 print_register_decl(reg, &state);
995 }
996
997 foreach_list_typed(nir_function, func, node, &shader->functions) {
998 print_function(func, &state);
999 }
1000
1001 destroy_print_state(&state);
1002 }
1003
1004 void
1005 nir_print_instr(const nir_instr *instr, FILE *fp)
1006 {
1007 print_state state = {
1008 .fp = fp,
1009 };
1010 print_instr(instr, &state, 0);
1011
1012 }