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