gallium/docs: remove reference to non-existent label
[mesa.git] / src / compiler / glsl / ir_print_visitor.cpp
1 /*
2 * Copyright © 2010 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <inttypes.h> /* for PRIx64 macro */
25 #include "ir_print_visitor.h"
26 #include "compiler/glsl_types.h"
27 #include "glsl_parser_extras.h"
28 #include "main/macros.h"
29 #include "util/hash_table.h"
30 #include "util/u_string.h"
31 #include "util/half_float.h"
32
33 static void print_type(FILE *f, const glsl_type *t);
34
35 void
36 ir_instruction::print(void) const
37 {
38 this->fprint(stdout);
39 }
40
41 void
42 ir_instruction::fprint(FILE *f) const
43 {
44 ir_instruction *deconsted = const_cast<ir_instruction *>(this);
45
46 ir_print_visitor v(f);
47 deconsted->accept(&v);
48 }
49
50 extern "C" {
51 void
52 _mesa_print_ir(FILE *f, exec_list *instructions,
53 struct _mesa_glsl_parse_state *state)
54 {
55 if (state) {
56 for (unsigned i = 0; i < state->num_user_structures; i++) {
57 const glsl_type *const s = state->user_structures[i];
58
59 fprintf(f, "(structure (%s) (%s@%p) (%u) (\n",
60 s->name, s->name, (void *) s, s->length);
61
62 for (unsigned j = 0; j < s->length; j++) {
63 fprintf(f, "\t((");
64 print_type(f, s->fields.structure[j].type);
65 fprintf(f, ")(%s))\n", s->fields.structure[j].name);
66 }
67
68 fprintf(f, ")\n");
69 }
70 }
71
72 fprintf(f, "(\n");
73 foreach_in_list(ir_instruction, ir, instructions) {
74 ir->fprint(f);
75 if (ir->ir_type != ir_type_function)
76 fprintf(f, "\n");
77 }
78 fprintf(f, ")\n");
79 }
80
81 void
82 fprint_ir(FILE *f, const void *instruction)
83 {
84 const ir_instruction *ir = (const ir_instruction *)instruction;
85 ir->fprint(f);
86 }
87
88 } /* extern "C" */
89
90 ir_print_visitor::ir_print_visitor(FILE *f)
91 : f(f)
92 {
93 indentation = 0;
94 printable_names = _mesa_pointer_hash_table_create(NULL);
95 symbols = _mesa_symbol_table_ctor();
96 mem_ctx = ralloc_context(NULL);
97 }
98
99 ir_print_visitor::~ir_print_visitor()
100 {
101 _mesa_hash_table_destroy(printable_names, NULL);
102 _mesa_symbol_table_dtor(symbols);
103 ralloc_free(mem_ctx);
104 }
105
106 void ir_print_visitor::indent(void)
107 {
108 for (int i = 0; i < indentation; i++)
109 fprintf(f, " ");
110 }
111
112 const char *
113 ir_print_visitor::unique_name(ir_variable *var)
114 {
115 /* var->name can be NULL in function prototypes when a type is given for a
116 * parameter but no name is given. In that case, just return an empty
117 * string. Don't worry about tracking the generated name in the printable
118 * names hash because this is the only scope where it can ever appear.
119 */
120 if (var->name == NULL) {
121 static unsigned arg = 1;
122 return ralloc_asprintf(this->mem_ctx, "parameter@%u", arg++);
123 }
124
125 /* Do we already have a name for this variable? */
126 struct hash_entry * entry =
127 _mesa_hash_table_search(this->printable_names, var);
128
129 if (entry != NULL) {
130 return (const char *) entry->data;
131 }
132
133 /* If there's no conflict, just use the original name */
134 const char* name = NULL;
135 if (_mesa_symbol_table_find_symbol(this->symbols, var->name) == NULL) {
136 name = var->name;
137 } else {
138 static unsigned i = 1;
139 name = ralloc_asprintf(this->mem_ctx, "%s@%u", var->name, ++i);
140 }
141 _mesa_hash_table_insert(this->printable_names, var, (void *) name);
142 _mesa_symbol_table_add_symbol(this->symbols, name, var);
143 return name;
144 }
145
146 static void
147 print_type(FILE *f, const glsl_type *t)
148 {
149 if (t->is_array()) {
150 fprintf(f, "(array ");
151 print_type(f, t->fields.array);
152 fprintf(f, " %u)", t->length);
153 } else if (t->is_struct() && !is_gl_identifier(t->name)) {
154 fprintf(f, "%s@%p", t->name, (void *) t);
155 } else {
156 fprintf(f, "%s", t->name);
157 }
158 }
159
160 void ir_print_visitor::visit(ir_rvalue *)
161 {
162 fprintf(f, "error");
163 }
164
165 void ir_print_visitor::visit(ir_variable *ir)
166 {
167 fprintf(f, "(declare ");
168
169 char binding[32] = {0};
170 if (ir->data.binding)
171 snprintf(binding, sizeof(binding), "binding=%i ", ir->data.binding);
172
173 char loc[32] = {0};
174 if (ir->data.location != -1)
175 snprintf(loc, sizeof(loc), "location=%i ", ir->data.location);
176
177 char component[32] = {0};
178 if (ir->data.explicit_component || ir->data.location_frac != 0)
179 snprintf(component, sizeof(component), "component=%i ",
180 ir->data.location_frac);
181
182 char stream[32] = {0};
183 if (ir->data.stream & (1u << 31)) {
184 if (ir->data.stream & ~(1u << 31)) {
185 snprintf(stream, sizeof(stream), "stream(%u,%u,%u,%u) ",
186 ir->data.stream & 3, (ir->data.stream >> 2) & 3,
187 (ir->data.stream >> 4) & 3, (ir->data.stream >> 6) & 3);
188 }
189 } else if (ir->data.stream) {
190 snprintf(stream, sizeof(stream), "stream%u ", ir->data.stream);
191 }
192
193 char image_format[32] = {0};
194 if (ir->data.image_format) {
195 snprintf(image_format, sizeof(image_format), "format=%x ",
196 ir->data.image_format);
197 }
198
199 const char *const cent = (ir->data.centroid) ? "centroid " : "";
200 const char *const samp = (ir->data.sample) ? "sample " : "";
201 const char *const patc = (ir->data.patch) ? "patch " : "";
202 const char *const inv = (ir->data.invariant) ? "invariant " : "";
203 const char *const explicit_inv = (ir->data.explicit_invariant) ? "explicit_invariant " : "";
204 const char *const prec = (ir->data.precise) ? "precise " : "";
205 const char *const bindless = (ir->data.bindless) ? "bindless " : "";
206 const char *const bound = (ir->data.bound) ? "bound " : "";
207 const char *const memory_read_only = (ir->data.memory_read_only) ? "readonly " : "";
208 const char *const memory_write_only = (ir->data.memory_write_only) ? "writeonly " : "";
209 const char *const memory_coherent = (ir->data.memory_coherent) ? "coherent " : "";
210 const char *const memory_volatile = (ir->data.memory_volatile) ? "volatile " : "";
211 const char *const memory_restrict = (ir->data.memory_restrict) ? "restrict " : "";
212 const char *const mode[] = { "", "uniform ", "shader_storage ",
213 "shader_shared ", "shader_in ", "shader_out ",
214 "in ", "out ", "inout ",
215 "const_in ", "sys ", "temporary " };
216 STATIC_ASSERT(ARRAY_SIZE(mode) == ir_var_mode_count);
217 const char *const interp[] = { "", "smooth", "flat", "noperspective", "explicit" };
218 STATIC_ASSERT(ARRAY_SIZE(interp) == INTERP_MODE_COUNT);
219
220 fprintf(f, "(%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s) ",
221 binding, loc, component, cent, bindless, bound,
222 image_format, memory_read_only, memory_write_only,
223 memory_coherent, memory_volatile, memory_restrict,
224 samp, patc, inv, explicit_inv, prec, mode[ir->data.mode],
225 stream,
226 interp[ir->data.interpolation]);
227
228 print_type(f, ir->type);
229 fprintf(f, " %s)", unique_name(ir));
230 }
231
232
233 void ir_print_visitor::visit(ir_function_signature *ir)
234 {
235 _mesa_symbol_table_push_scope(symbols);
236 fprintf(f, "(signature ");
237 indentation++;
238
239 print_type(f, ir->return_type);
240 fprintf(f, "\n");
241 indent();
242
243 fprintf(f, "(parameters\n");
244 indentation++;
245
246 foreach_in_list(ir_variable, inst, &ir->parameters) {
247 indent();
248 inst->accept(this);
249 fprintf(f, "\n");
250 }
251 indentation--;
252
253 indent();
254 fprintf(f, ")\n");
255
256 indent();
257
258 fprintf(f, "(\n");
259 indentation++;
260
261 foreach_in_list(ir_instruction, inst, &ir->body) {
262 indent();
263 inst->accept(this);
264 fprintf(f, "\n");
265 }
266 indentation--;
267 indent();
268 fprintf(f, "))\n");
269 indentation--;
270 _mesa_symbol_table_pop_scope(symbols);
271 }
272
273
274 void ir_print_visitor::visit(ir_function *ir)
275 {
276 fprintf(f, "(%s function %s\n", ir->is_subroutine ? "subroutine" : "", ir->name);
277 indentation++;
278 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
279 indent();
280 sig->accept(this);
281 fprintf(f, "\n");
282 }
283 indentation--;
284 indent();
285 fprintf(f, ")\n\n");
286 }
287
288
289 void ir_print_visitor::visit(ir_expression *ir)
290 {
291 fprintf(f, "(expression ");
292
293 print_type(f, ir->type);
294
295 fprintf(f, " %s ", ir_expression_operation_strings[ir->operation]);
296
297 for (unsigned i = 0; i < ir->num_operands; i++) {
298 ir->operands[i]->accept(this);
299 }
300
301 fprintf(f, ") ");
302 }
303
304
305 void ir_print_visitor::visit(ir_texture *ir)
306 {
307 fprintf(f, "(%s ", ir->opcode_string());
308
309 if (ir->op == ir_samples_identical) {
310 ir->sampler->accept(this);
311 fprintf(f, " ");
312 ir->coordinate->accept(this);
313 fprintf(f, ")");
314 return;
315 }
316
317 print_type(f, ir->type);
318 fprintf(f, " ");
319
320 ir->sampler->accept(this);
321 fprintf(f, " ");
322
323 if (ir->op != ir_txs && ir->op != ir_query_levels &&
324 ir->op != ir_texture_samples) {
325 ir->coordinate->accept(this);
326
327 fprintf(f, " ");
328
329 if (ir->offset != NULL) {
330 ir->offset->accept(this);
331 } else {
332 fprintf(f, "0");
333 }
334
335 fprintf(f, " ");
336 }
337
338 if (ir->op != ir_txf && ir->op != ir_txf_ms &&
339 ir->op != ir_txs && ir->op != ir_tg4 &&
340 ir->op != ir_query_levels && ir->op != ir_texture_samples) {
341 if (ir->projector)
342 ir->projector->accept(this);
343 else
344 fprintf(f, "1");
345
346 if (ir->shadow_comparator) {
347 fprintf(f, " ");
348 ir->shadow_comparator->accept(this);
349 } else {
350 fprintf(f, " ()");
351 }
352 }
353
354 fprintf(f, " ");
355 switch (ir->op)
356 {
357 case ir_tex:
358 case ir_lod:
359 case ir_query_levels:
360 case ir_texture_samples:
361 break;
362 case ir_txb:
363 ir->lod_info.bias->accept(this);
364 break;
365 case ir_txl:
366 case ir_txf:
367 case ir_txs:
368 ir->lod_info.lod->accept(this);
369 break;
370 case ir_txf_ms:
371 ir->lod_info.sample_index->accept(this);
372 break;
373 case ir_txd:
374 fprintf(f, "(");
375 ir->lod_info.grad.dPdx->accept(this);
376 fprintf(f, " ");
377 ir->lod_info.grad.dPdy->accept(this);
378 fprintf(f, ")");
379 break;
380 case ir_tg4:
381 ir->lod_info.component->accept(this);
382 break;
383 case ir_samples_identical:
384 unreachable("ir_samples_identical was already handled");
385 };
386 fprintf(f, ")");
387 }
388
389
390 void ir_print_visitor::visit(ir_swizzle *ir)
391 {
392 const unsigned swiz[4] = {
393 ir->mask.x,
394 ir->mask.y,
395 ir->mask.z,
396 ir->mask.w,
397 };
398
399 fprintf(f, "(swiz ");
400 for (unsigned i = 0; i < ir->mask.num_components; i++) {
401 fprintf(f, "%c", "xyzw"[swiz[i]]);
402 }
403 fprintf(f, " ");
404 ir->val->accept(this);
405 fprintf(f, ")");
406 }
407
408
409 void ir_print_visitor::visit(ir_dereference_variable *ir)
410 {
411 ir_variable *var = ir->variable_referenced();
412 fprintf(f, "(var_ref %s) ", unique_name(var));
413 }
414
415
416 void ir_print_visitor::visit(ir_dereference_array *ir)
417 {
418 fprintf(f, "(array_ref ");
419 ir->array->accept(this);
420 ir->array_index->accept(this);
421 fprintf(f, ") ");
422 }
423
424
425 void ir_print_visitor::visit(ir_dereference_record *ir)
426 {
427 fprintf(f, "(record_ref ");
428 ir->record->accept(this);
429
430 const char *field_name =
431 ir->record->type->fields.structure[ir->field_idx].name;
432 fprintf(f, " %s) ", field_name);
433 }
434
435
436 void ir_print_visitor::visit(ir_assignment *ir)
437 {
438 fprintf(f, "(assign ");
439
440 if (ir->condition)
441 ir->condition->accept(this);
442
443 char mask[5];
444 unsigned j = 0;
445
446 for (unsigned i = 0; i < 4; i++) {
447 if ((ir->write_mask & (1 << i)) != 0) {
448 mask[j] = "xyzw"[i];
449 j++;
450 }
451 }
452 mask[j] = '\0';
453
454 fprintf(f, " (%s) ", mask);
455
456 ir->lhs->accept(this);
457
458 fprintf(f, " ");
459
460 ir->rhs->accept(this);
461 fprintf(f, ") ");
462 }
463
464 static void
465 print_float_constant(FILE *f, float val)
466 {
467 if (val == 0.0f)
468 /* 0.0 == -0.0, so print with %f to get the proper sign. */
469 fprintf(f, "%f", val);
470 else if (fabs(val) < 0.000001f)
471 fprintf(f, "%a", val);
472 else if (fabs(val) > 1000000.0f)
473 fprintf(f, "%e", val);
474 else
475 fprintf(f, "%f", val);
476 }
477
478 void ir_print_visitor::visit(ir_constant *ir)
479 {
480 fprintf(f, "(constant ");
481 print_type(f, ir->type);
482 fprintf(f, " (");
483
484 if (ir->type->is_array()) {
485 for (unsigned i = 0; i < ir->type->length; i++)
486 ir->get_array_element(i)->accept(this);
487 } else if (ir->type->is_struct()) {
488 for (unsigned i = 0; i < ir->type->length; i++) {
489 fprintf(f, "(%s ", ir->type->fields.structure[i].name);
490 ir->get_record_field(i)->accept(this);
491 fprintf(f, ")");
492 }
493 } else {
494 for (unsigned i = 0; i < ir->type->components(); i++) {
495 if (i != 0)
496 fprintf(f, " ");
497 switch (ir->type->base_type) {
498 case GLSL_TYPE_UINT16:fprintf(f, "%u", ir->value.u16[i]); break;
499 case GLSL_TYPE_INT16: fprintf(f, "%d", ir->value.i16[i]); break;
500 case GLSL_TYPE_UINT: fprintf(f, "%u", ir->value.u[i]); break;
501 case GLSL_TYPE_INT: fprintf(f, "%d", ir->value.i[i]); break;
502 case GLSL_TYPE_FLOAT:
503 print_float_constant(f, ir->value.f[i]);
504 break;
505 case GLSL_TYPE_FLOAT16:
506 print_float_constant(f, _mesa_half_to_float(ir->value.f16[i]));
507 break;
508 case GLSL_TYPE_SAMPLER:
509 case GLSL_TYPE_IMAGE:
510 case GLSL_TYPE_UINT64:
511 fprintf(f, "%" PRIu64, ir->value.u64[i]);
512 break;
513 case GLSL_TYPE_INT64: fprintf(f, "%" PRIi64, ir->value.i64[i]); break;
514 case GLSL_TYPE_BOOL: fprintf(f, "%d", ir->value.b[i]); break;
515 case GLSL_TYPE_DOUBLE:
516 if (ir->value.d[i] == 0.0)
517 /* 0.0 == -0.0, so print with %f to get the proper sign. */
518 fprintf(f, "%.1f", ir->value.d[i]);
519 else if (fabs(ir->value.d[i]) < 0.000001)
520 fprintf(f, "%a", ir->value.d[i]);
521 else if (fabs(ir->value.d[i]) > 1000000.0)
522 fprintf(f, "%e", ir->value.d[i]);
523 else
524 fprintf(f, "%f", ir->value.d[i]);
525 break;
526 default:
527 unreachable("Invalid constant type");
528 }
529 }
530 }
531 fprintf(f, ")) ");
532 }
533
534
535 void
536 ir_print_visitor::visit(ir_call *ir)
537 {
538 fprintf(f, "(call %s ", ir->callee_name());
539 if (ir->return_deref)
540 ir->return_deref->accept(this);
541 fprintf(f, " (");
542 foreach_in_list(ir_rvalue, param, &ir->actual_parameters) {
543 param->accept(this);
544 }
545 fprintf(f, "))\n");
546 }
547
548
549 void
550 ir_print_visitor::visit(ir_return *ir)
551 {
552 fprintf(f, "(return");
553
554 ir_rvalue *const value = ir->get_value();
555 if (value) {
556 fprintf(f, " ");
557 value->accept(this);
558 }
559
560 fprintf(f, ")");
561 }
562
563
564 void
565 ir_print_visitor::visit(ir_discard *ir)
566 {
567 fprintf(f, "(discard ");
568
569 if (ir->condition != NULL) {
570 fprintf(f, " ");
571 ir->condition->accept(this);
572 }
573
574 fprintf(f, ")");
575 }
576
577
578 void
579 ir_print_visitor::visit(ir_demote *ir)
580 {
581 fprintf(f, "(demote)");
582 }
583
584
585 void
586 ir_print_visitor::visit(ir_if *ir)
587 {
588 fprintf(f, "(if ");
589 ir->condition->accept(this);
590
591 fprintf(f, "(\n");
592 indentation++;
593
594 foreach_in_list(ir_instruction, inst, &ir->then_instructions) {
595 indent();
596 inst->accept(this);
597 fprintf(f, "\n");
598 }
599
600 indentation--;
601 indent();
602 fprintf(f, ")\n");
603
604 indent();
605 if (!ir->else_instructions.is_empty()) {
606 fprintf(f, "(\n");
607 indentation++;
608
609 foreach_in_list(ir_instruction, inst, &ir->else_instructions) {
610 indent();
611 inst->accept(this);
612 fprintf(f, "\n");
613 }
614 indentation--;
615 indent();
616 fprintf(f, "))\n");
617 } else {
618 fprintf(f, "())\n");
619 }
620 }
621
622
623 void
624 ir_print_visitor::visit(ir_loop *ir)
625 {
626 fprintf(f, "(loop (\n");
627 indentation++;
628
629 foreach_in_list(ir_instruction, inst, &ir->body_instructions) {
630 indent();
631 inst->accept(this);
632 fprintf(f, "\n");
633 }
634 indentation--;
635 indent();
636 fprintf(f, "))\n");
637 }
638
639
640 void
641 ir_print_visitor::visit(ir_loop_jump *ir)
642 {
643 fprintf(f, "%s", ir->is_break() ? "break" : "continue");
644 }
645
646 void
647 ir_print_visitor::visit(ir_emit_vertex *ir)
648 {
649 fprintf(f, "(emit-vertex ");
650 ir->stream->accept(this);
651 fprintf(f, ")\n");
652 }
653
654 void
655 ir_print_visitor::visit(ir_end_primitive *ir)
656 {
657 fprintf(f, "(end-primitive ");
658 ir->stream->accept(this);
659 fprintf(f, ")\n");
660 }
661
662 void
663 ir_print_visitor::visit(ir_barrier *)
664 {
665 fprintf(f, "(barrier)\n");
666 }