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