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