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