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