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