compiler: add glsl_print_type
[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 glsl_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 void ir_print_visitor::visit(ir_rvalue *)
145 {
146 fprintf(f, "error");
147 }
148
149 void ir_print_visitor::visit(ir_variable *ir)
150 {
151 fprintf(f, "(declare ");
152
153 char binding[32] = {0};
154 if (ir->data.binding)
155 snprintf(binding, sizeof(binding), "binding=%i ", ir->data.binding);
156
157 char loc[32] = {0};
158 if (ir->data.location != -1)
159 snprintf(loc, sizeof(loc), "location=%i ", ir->data.location);
160
161 char component[32] = {0};
162 if (ir->data.explicit_component || ir->data.location_frac != 0)
163 snprintf(component, sizeof(component), "component=%i ",
164 ir->data.location_frac);
165
166 char stream[32] = {0};
167 if (ir->data.stream & (1u << 31)) {
168 if (ir->data.stream & ~(1u << 31)) {
169 snprintf(stream, sizeof(stream), "stream(%u,%u,%u,%u) ",
170 ir->data.stream & 3, (ir->data.stream >> 2) & 3,
171 (ir->data.stream >> 4) & 3, (ir->data.stream >> 6) & 3);
172 }
173 } else if (ir->data.stream) {
174 snprintf(stream, sizeof(stream), "stream%u ", ir->data.stream);
175 }
176
177 char image_format[32] = {0};
178 if (ir->data.image_format) {
179 snprintf(image_format, sizeof(image_format), "format=%x ",
180 ir->data.image_format);
181 }
182
183 const char *const cent = (ir->data.centroid) ? "centroid " : "";
184 const char *const samp = (ir->data.sample) ? "sample " : "";
185 const char *const patc = (ir->data.patch) ? "patch " : "";
186 const char *const inv = (ir->data.invariant) ? "invariant " : "";
187 const char *const explicit_inv = (ir->data.explicit_invariant) ? "explicit_invariant " : "";
188 const char *const prec = (ir->data.precise) ? "precise " : "";
189 const char *const bindless = (ir->data.bindless) ? "bindless " : "";
190 const char *const bound = (ir->data.bound) ? "bound " : "";
191 const char *const memory_read_only = (ir->data.memory_read_only) ? "readonly " : "";
192 const char *const memory_write_only = (ir->data.memory_write_only) ? "writeonly " : "";
193 const char *const memory_coherent = (ir->data.memory_coherent) ? "coherent " : "";
194 const char *const memory_volatile = (ir->data.memory_volatile) ? "volatile " : "";
195 const char *const memory_restrict = (ir->data.memory_restrict) ? "restrict " : "";
196 const char *const mode[] = { "", "uniform ", "shader_storage ",
197 "shader_shared ", "shader_in ", "shader_out ",
198 "in ", "out ", "inout ",
199 "const_in ", "sys ", "temporary " };
200 STATIC_ASSERT(ARRAY_SIZE(mode) == ir_var_mode_count);
201 const char *const interp[] = { "", "smooth", "flat", "noperspective", "explicit" };
202 STATIC_ASSERT(ARRAY_SIZE(interp) == INTERP_MODE_COUNT);
203 const char *const precision[] = { "", "highp ", "mediump ", "lowp "};
204
205 fprintf(f, "(%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s) ",
206 binding, loc, component, cent, bindless, bound,
207 image_format, memory_read_only, memory_write_only,
208 memory_coherent, memory_volatile, memory_restrict,
209 samp, patc, inv, explicit_inv, prec, mode[ir->data.mode],
210 stream,
211 interp[ir->data.interpolation], precision[ir->data.precision]);
212
213 glsl_print_type(f, ir->type);
214 fprintf(f, " %s)", unique_name(ir));
215
216 if (ir->constant_initializer) {
217 fprintf(f, " ");
218 visit(ir->constant_initializer);
219 }
220
221 if (ir->constant_value) {
222 fprintf(f, " ");
223 visit(ir->constant_value);
224 }
225 }
226
227
228 void ir_print_visitor::visit(ir_function_signature *ir)
229 {
230 _mesa_symbol_table_push_scope(symbols);
231 fprintf(f, "(signature ");
232 indentation++;
233
234 glsl_print_type(f, ir->return_type);
235 fprintf(f, "\n");
236 indent();
237
238 fprintf(f, "(parameters\n");
239 indentation++;
240
241 foreach_in_list(ir_variable, inst, &ir->parameters) {
242 indent();
243 inst->accept(this);
244 fprintf(f, "\n");
245 }
246 indentation--;
247
248 indent();
249 fprintf(f, ")\n");
250
251 indent();
252
253 fprintf(f, "(\n");
254 indentation++;
255
256 foreach_in_list(ir_instruction, inst, &ir->body) {
257 indent();
258 inst->accept(this);
259 fprintf(f, "\n");
260 }
261 indentation--;
262 indent();
263 fprintf(f, "))\n");
264 indentation--;
265 _mesa_symbol_table_pop_scope(symbols);
266 }
267
268
269 void ir_print_visitor::visit(ir_function *ir)
270 {
271 fprintf(f, "(%s function %s\n", ir->is_subroutine ? "subroutine" : "", ir->name);
272 indentation++;
273 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
274 indent();
275 sig->accept(this);
276 fprintf(f, "\n");
277 }
278 indentation--;
279 indent();
280 fprintf(f, ")\n\n");
281 }
282
283
284 void ir_print_visitor::visit(ir_expression *ir)
285 {
286 fprintf(f, "(expression ");
287
288 glsl_print_type(f, ir->type);
289
290 fprintf(f, " %s ", ir_expression_operation_strings[ir->operation]);
291
292 for (unsigned i = 0; i < ir->num_operands; i++) {
293 ir->operands[i]->accept(this);
294 }
295
296 fprintf(f, ") ");
297 }
298
299
300 void ir_print_visitor::visit(ir_texture *ir)
301 {
302 fprintf(f, "(%s ", ir->opcode_string());
303
304 if (ir->op == ir_samples_identical) {
305 ir->sampler->accept(this);
306 fprintf(f, " ");
307 ir->coordinate->accept(this);
308 fprintf(f, ")");
309 return;
310 }
311
312 glsl_print_type(f, ir->type);
313 fprintf(f, " ");
314
315 ir->sampler->accept(this);
316 fprintf(f, " ");
317
318 if (ir->op != ir_txs && ir->op != ir_query_levels &&
319 ir->op != ir_texture_samples) {
320 ir->coordinate->accept(this);
321
322 fprintf(f, " ");
323
324 if (ir->offset != NULL) {
325 ir->offset->accept(this);
326 } else {
327 fprintf(f, "0");
328 }
329
330 fprintf(f, " ");
331 }
332
333 if (ir->op != ir_txf && ir->op != ir_txf_ms &&
334 ir->op != ir_txs && ir->op != ir_tg4 &&
335 ir->op != ir_query_levels && ir->op != ir_texture_samples) {
336 if (ir->projector)
337 ir->projector->accept(this);
338 else
339 fprintf(f, "1");
340
341 if (ir->shadow_comparator) {
342 fprintf(f, " ");
343 ir->shadow_comparator->accept(this);
344 } else {
345 fprintf(f, " ()");
346 }
347 }
348
349 fprintf(f, " ");
350 switch (ir->op)
351 {
352 case ir_tex:
353 case ir_lod:
354 case ir_query_levels:
355 case ir_texture_samples:
356 break;
357 case ir_txb:
358 ir->lod_info.bias->accept(this);
359 break;
360 case ir_txl:
361 case ir_txf:
362 case ir_txs:
363 ir->lod_info.lod->accept(this);
364 break;
365 case ir_txf_ms:
366 ir->lod_info.sample_index->accept(this);
367 break;
368 case ir_txd:
369 fprintf(f, "(");
370 ir->lod_info.grad.dPdx->accept(this);
371 fprintf(f, " ");
372 ir->lod_info.grad.dPdy->accept(this);
373 fprintf(f, ")");
374 break;
375 case ir_tg4:
376 ir->lod_info.component->accept(this);
377 break;
378 case ir_samples_identical:
379 unreachable("ir_samples_identical was already handled");
380 };
381 fprintf(f, ")");
382 }
383
384
385 void ir_print_visitor::visit(ir_swizzle *ir)
386 {
387 const unsigned swiz[4] = {
388 ir->mask.x,
389 ir->mask.y,
390 ir->mask.z,
391 ir->mask.w,
392 };
393
394 fprintf(f, "(swiz ");
395 for (unsigned i = 0; i < ir->mask.num_components; i++) {
396 fprintf(f, "%c", "xyzw"[swiz[i]]);
397 }
398 fprintf(f, " ");
399 ir->val->accept(this);
400 fprintf(f, ")");
401 }
402
403
404 void ir_print_visitor::visit(ir_dereference_variable *ir)
405 {
406 ir_variable *var = ir->variable_referenced();
407 fprintf(f, "(var_ref %s) ", unique_name(var));
408 }
409
410
411 void ir_print_visitor::visit(ir_dereference_array *ir)
412 {
413 fprintf(f, "(array_ref ");
414 ir->array->accept(this);
415 ir->array_index->accept(this);
416 fprintf(f, ") ");
417 }
418
419
420 void ir_print_visitor::visit(ir_dereference_record *ir)
421 {
422 fprintf(f, "(record_ref ");
423 ir->record->accept(this);
424
425 const char *field_name =
426 ir->record->type->fields.structure[ir->field_idx].name;
427 fprintf(f, " %s) ", field_name);
428 }
429
430
431 void ir_print_visitor::visit(ir_assignment *ir)
432 {
433 fprintf(f, "(assign ");
434
435 if (ir->condition)
436 ir->condition->accept(this);
437
438 char mask[5];
439 unsigned j = 0;
440
441 for (unsigned i = 0; i < 4; i++) {
442 if ((ir->write_mask & (1 << i)) != 0) {
443 mask[j] = "xyzw"[i];
444 j++;
445 }
446 }
447 mask[j] = '\0';
448
449 fprintf(f, " (%s) ", mask);
450
451 ir->lhs->accept(this);
452
453 fprintf(f, " ");
454
455 ir->rhs->accept(this);
456 fprintf(f, ") ");
457 }
458
459 static void
460 print_float_constant(FILE *f, float val)
461 {
462 if (val == 0.0f)
463 /* 0.0 == -0.0, so print with %f to get the proper sign. */
464 fprintf(f, "%f", val);
465 else if (fabs(val) < 0.000001f)
466 fprintf(f, "%a", val);
467 else if (fabs(val) > 1000000.0f)
468 fprintf(f, "%e", val);
469 else
470 fprintf(f, "%f", val);
471 }
472
473 void ir_print_visitor::visit(ir_constant *ir)
474 {
475 fprintf(f, "(constant ");
476 glsl_print_type(f, ir->type);
477 fprintf(f, " (");
478
479 if (ir->type->is_array()) {
480 for (unsigned i = 0; i < ir->type->length; i++)
481 ir->get_array_element(i)->accept(this);
482 } else if (ir->type->is_struct()) {
483 for (unsigned i = 0; i < ir->type->length; i++) {
484 fprintf(f, "(%s ", ir->type->fields.structure[i].name);
485 ir->get_record_field(i)->accept(this);
486 fprintf(f, ")");
487 }
488 } else {
489 for (unsigned i = 0; i < ir->type->components(); i++) {
490 if (i != 0)
491 fprintf(f, " ");
492 switch (ir->type->base_type) {
493 case GLSL_TYPE_UINT16:fprintf(f, "%u", ir->value.u16[i]); break;
494 case GLSL_TYPE_INT16: fprintf(f, "%d", ir->value.i16[i]); break;
495 case GLSL_TYPE_UINT: fprintf(f, "%u", ir->value.u[i]); break;
496 case GLSL_TYPE_INT: fprintf(f, "%d", ir->value.i[i]); break;
497 case GLSL_TYPE_FLOAT:
498 print_float_constant(f, ir->value.f[i]);
499 break;
500 case GLSL_TYPE_FLOAT16:
501 print_float_constant(f, _mesa_half_to_float(ir->value.f16[i]));
502 break;
503 case GLSL_TYPE_SAMPLER:
504 case GLSL_TYPE_IMAGE:
505 case GLSL_TYPE_UINT64:
506 fprintf(f, "%" PRIu64, ir->value.u64[i]);
507 break;
508 case GLSL_TYPE_INT64: fprintf(f, "%" PRIi64, ir->value.i64[i]); break;
509 case GLSL_TYPE_BOOL: fprintf(f, "%d", ir->value.b[i]); break;
510 case GLSL_TYPE_DOUBLE:
511 if (ir->value.d[i] == 0.0)
512 /* 0.0 == -0.0, so print with %f to get the proper sign. */
513 fprintf(f, "%.1f", ir->value.d[i]);
514 else if (fabs(ir->value.d[i]) < 0.000001)
515 fprintf(f, "%a", ir->value.d[i]);
516 else if (fabs(ir->value.d[i]) > 1000000.0)
517 fprintf(f, "%e", ir->value.d[i]);
518 else
519 fprintf(f, "%f", ir->value.d[i]);
520 break;
521 default:
522 unreachable("Invalid constant type");
523 }
524 }
525 }
526 fprintf(f, ")) ");
527 }
528
529
530 void
531 ir_print_visitor::visit(ir_call *ir)
532 {
533 fprintf(f, "(call %s ", ir->callee_name());
534 if (ir->return_deref)
535 ir->return_deref->accept(this);
536 fprintf(f, " (");
537 foreach_in_list(ir_rvalue, param, &ir->actual_parameters) {
538 param->accept(this);
539 }
540 fprintf(f, "))\n");
541 }
542
543
544 void
545 ir_print_visitor::visit(ir_return *ir)
546 {
547 fprintf(f, "(return");
548
549 ir_rvalue *const value = ir->get_value();
550 if (value) {
551 fprintf(f, " ");
552 value->accept(this);
553 }
554
555 fprintf(f, ")");
556 }
557
558
559 void
560 ir_print_visitor::visit(ir_discard *ir)
561 {
562 fprintf(f, "(discard ");
563
564 if (ir->condition != NULL) {
565 fprintf(f, " ");
566 ir->condition->accept(this);
567 }
568
569 fprintf(f, ")");
570 }
571
572
573 void
574 ir_print_visitor::visit(ir_demote *ir)
575 {
576 fprintf(f, "(demote)");
577 }
578
579
580 void
581 ir_print_visitor::visit(ir_if *ir)
582 {
583 fprintf(f, "(if ");
584 ir->condition->accept(this);
585
586 fprintf(f, "(\n");
587 indentation++;
588
589 foreach_in_list(ir_instruction, inst, &ir->then_instructions) {
590 indent();
591 inst->accept(this);
592 fprintf(f, "\n");
593 }
594
595 indentation--;
596 indent();
597 fprintf(f, ")\n");
598
599 indent();
600 if (!ir->else_instructions.is_empty()) {
601 fprintf(f, "(\n");
602 indentation++;
603
604 foreach_in_list(ir_instruction, inst, &ir->else_instructions) {
605 indent();
606 inst->accept(this);
607 fprintf(f, "\n");
608 }
609 indentation--;
610 indent();
611 fprintf(f, "))\n");
612 } else {
613 fprintf(f, "())\n");
614 }
615 }
616
617
618 void
619 ir_print_visitor::visit(ir_loop *ir)
620 {
621 fprintf(f, "(loop (\n");
622 indentation++;
623
624 foreach_in_list(ir_instruction, inst, &ir->body_instructions) {
625 indent();
626 inst->accept(this);
627 fprintf(f, "\n");
628 }
629 indentation--;
630 indent();
631 fprintf(f, "))\n");
632 }
633
634
635 void
636 ir_print_visitor::visit(ir_loop_jump *ir)
637 {
638 fprintf(f, "%s", ir->is_break() ? "break" : "continue");
639 }
640
641 void
642 ir_print_visitor::visit(ir_emit_vertex *ir)
643 {
644 fprintf(f, "(emit-vertex ");
645 ir->stream->accept(this);
646 fprintf(f, ")\n");
647 }
648
649 void
650 ir_print_visitor::visit(ir_end_primitive *ir)
651 {
652 fprintf(f, "(end-primitive ");
653 ir->stream->accept(this);
654 fprintf(f, ")\n");
655 }
656
657 void
658 ir_print_visitor::visit(ir_barrier *)
659 {
660 fprintf(f, "(barrier)\n");
661 }