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