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