nir/opt_algebraic: Fix some expressions with ambiguous bit sizes
[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 "ir_print_visitor.h"
25 #include "compiler/glsl_types.h"
26 #include "glsl_parser_extras.h"
27 #include "main/macros.h"
28 #include "util/hash_table.h"
29
30 static void print_type(FILE *f, const glsl_type *t);
31
32 void
33 ir_instruction::print(void) const
34 {
35 this->fprint(stdout);
36 }
37
38 void
39 ir_instruction::fprint(FILE *f) const
40 {
41 ir_instruction *deconsted = const_cast<ir_instruction *>(this);
42
43 ir_print_visitor v(f);
44 deconsted->accept(&v);
45 }
46
47 extern "C" {
48 void
49 _mesa_print_ir(FILE *f, exec_list *instructions,
50 struct _mesa_glsl_parse_state *state)
51 {
52 if (state) {
53 for (unsigned i = 0; i < state->num_user_structures; i++) {
54 const glsl_type *const s = state->user_structures[i];
55
56 fprintf(f, "(structure (%s) (%s@%p) (%u) (\n",
57 s->name, s->name, (void *) s, s->length);
58
59 for (unsigned j = 0; j < s->length; j++) {
60 fprintf(f, "\t((");
61 print_type(f, s->fields.structure[j].type);
62 fprintf(f, ")(%s))\n", s->fields.structure[j].name);
63 }
64
65 fprintf(f, ")\n");
66 }
67 }
68
69 fprintf(f, "(\n");
70 foreach_in_list(ir_instruction, ir, instructions) {
71 ir->fprint(f);
72 if (ir->ir_type != ir_type_function)
73 fprintf(f, "\n");
74 }
75 fprintf(f, ")\n");
76 }
77
78 void
79 fprint_ir(FILE *f, const void *instruction)
80 {
81 const ir_instruction *ir = (const ir_instruction *)instruction;
82 ir->fprint(f);
83 }
84
85 } /* extern "C" */
86
87 ir_print_visitor::ir_print_visitor(FILE *f)
88 : f(f)
89 {
90 indentation = 0;
91 printable_names =
92 _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
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, -1, 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, -1, name, var);
141 return name;
142 }
143
144 static void
145 print_type(FILE *f, const glsl_type *t)
146 {
147 if (t->base_type == GLSL_TYPE_ARRAY) {
148 fprintf(f, "(array ");
149 print_type(f, t->fields.array);
150 fprintf(f, " %u)", t->length);
151 } else if ((t->base_type == GLSL_TYPE_STRUCT)
152 && !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 loc[256] = {0};
169 if (ir->data.location != -1)
170 snprintf(loc, sizeof(loc), "location=%i ", ir->data.location);
171
172 const char *const cent = (ir->data.centroid) ? "centroid " : "";
173 const char *const samp = (ir->data.sample) ? "sample " : "";
174 const char *const patc = (ir->data.patch) ? "patch " : "";
175 const char *const inv = (ir->data.invariant) ? "invariant " : "";
176 const char *const prec = (ir->data.precise) ? "precise " : "";
177 const char *const mode[] = { "", "uniform ", "shader_storage ",
178 "shader_shared ", "shader_in ", "shader_out ",
179 "in ", "out ", "inout ",
180 "const_in ", "sys ", "temporary " };
181 STATIC_ASSERT(ARRAY_SIZE(mode) == ir_var_mode_count);
182 const char *const stream [] = {"", "stream1 ", "stream2 ", "stream3 "};
183 const char *const interp[] = { "", "smooth", "flat", "noperspective" };
184 STATIC_ASSERT(ARRAY_SIZE(interp) == INTERP_QUALIFIER_COUNT);
185
186 fprintf(f, "(%s%s%s%s%s%s%s%s%s) ",
187 loc, cent, samp, patc, inv, prec, mode[ir->data.mode],
188 stream[ir->data.stream],
189 interp[ir->data.interpolation]);
190
191 print_type(f, ir->type);
192 fprintf(f, " %s)", unique_name(ir));
193 }
194
195
196 void ir_print_visitor::visit(ir_function_signature *ir)
197 {
198 _mesa_symbol_table_push_scope(symbols);
199 fprintf(f, "(signature ");
200 indentation++;
201
202 print_type(f, ir->return_type);
203 fprintf(f, "\n");
204 indent();
205
206 fprintf(f, "(parameters\n");
207 indentation++;
208
209 foreach_in_list(ir_variable, inst, &ir->parameters) {
210 indent();
211 inst->accept(this);
212 fprintf(f, "\n");
213 }
214 indentation--;
215
216 indent();
217 fprintf(f, ")\n");
218
219 indent();
220
221 fprintf(f, "(\n");
222 indentation++;
223
224 foreach_in_list(ir_instruction, inst, &ir->body) {
225 indent();
226 inst->accept(this);
227 fprintf(f, "\n");
228 }
229 indentation--;
230 indent();
231 fprintf(f, "))\n");
232 indentation--;
233 _mesa_symbol_table_pop_scope(symbols);
234 }
235
236
237 void ir_print_visitor::visit(ir_function *ir)
238 {
239 fprintf(f, "(%s function %s\n", ir->is_subroutine ? "subroutine" : "", ir->name);
240 indentation++;
241 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
242 indent();
243 sig->accept(this);
244 fprintf(f, "\n");
245 }
246 indentation--;
247 indent();
248 fprintf(f, ")\n\n");
249 }
250
251
252 void ir_print_visitor::visit(ir_expression *ir)
253 {
254 fprintf(f, "(expression ");
255
256 print_type(f, ir->type);
257
258 fprintf(f, " %s ", ir->operator_string());
259
260 for (unsigned i = 0; i < ir->get_num_operands(); i++) {
261 ir->operands[i]->accept(this);
262 }
263
264 fprintf(f, ") ");
265 }
266
267
268 void ir_print_visitor::visit(ir_texture *ir)
269 {
270 fprintf(f, "(%s ", ir->opcode_string());
271
272 if (ir->op == ir_samples_identical) {
273 ir->sampler->accept(this);
274 fprintf(f, " ");
275 ir->coordinate->accept(this);
276 fprintf(f, ")");
277 return;
278 }
279
280 print_type(f, ir->type);
281 fprintf(f, " ");
282
283 ir->sampler->accept(this);
284 fprintf(f, " ");
285
286 if (ir->op != ir_txs && ir->op != ir_query_levels &&
287 ir->op != ir_texture_samples) {
288 ir->coordinate->accept(this);
289
290 fprintf(f, " ");
291
292 if (ir->offset != NULL) {
293 ir->offset->accept(this);
294 } else {
295 fprintf(f, "0");
296 }
297
298 fprintf(f, " ");
299 }
300
301 if (ir->op != ir_txf && ir->op != ir_txf_ms &&
302 ir->op != ir_txs && ir->op != ir_tg4 &&
303 ir->op != ir_query_levels && ir->op != ir_texture_samples) {
304 if (ir->projector)
305 ir->projector->accept(this);
306 else
307 fprintf(f, "1");
308
309 if (ir->shadow_comparitor) {
310 fprintf(f, " ");
311 ir->shadow_comparitor->accept(this);
312 } else {
313 fprintf(f, " ()");
314 }
315 }
316
317 fprintf(f, " ");
318 switch (ir->op)
319 {
320 case ir_tex:
321 case ir_lod:
322 case ir_query_levels:
323 case ir_texture_samples:
324 break;
325 case ir_txb:
326 ir->lod_info.bias->accept(this);
327 break;
328 case ir_txl:
329 case ir_txf:
330 case ir_txs:
331 ir->lod_info.lod->accept(this);
332 break;
333 case ir_txf_ms:
334 ir->lod_info.sample_index->accept(this);
335 break;
336 case ir_txd:
337 fprintf(f, "(");
338 ir->lod_info.grad.dPdx->accept(this);
339 fprintf(f, " ");
340 ir->lod_info.grad.dPdy->accept(this);
341 fprintf(f, ")");
342 break;
343 case ir_tg4:
344 ir->lod_info.component->accept(this);
345 break;
346 case ir_samples_identical:
347 unreachable(!"ir_samples_identical was already handled");
348 };
349 fprintf(f, ")");
350 }
351
352
353 void ir_print_visitor::visit(ir_swizzle *ir)
354 {
355 const unsigned swiz[4] = {
356 ir->mask.x,
357 ir->mask.y,
358 ir->mask.z,
359 ir->mask.w,
360 };
361
362 fprintf(f, "(swiz ");
363 for (unsigned i = 0; i < ir->mask.num_components; i++) {
364 fprintf(f, "%c", "xyzw"[swiz[i]]);
365 }
366 fprintf(f, " ");
367 ir->val->accept(this);
368 fprintf(f, ")");
369 }
370
371
372 void ir_print_visitor::visit(ir_dereference_variable *ir)
373 {
374 ir_variable *var = ir->variable_referenced();
375 fprintf(f, "(var_ref %s) ", unique_name(var));
376 }
377
378
379 void ir_print_visitor::visit(ir_dereference_array *ir)
380 {
381 fprintf(f, "(array_ref ");
382 ir->array->accept(this);
383 ir->array_index->accept(this);
384 fprintf(f, ") ");
385 }
386
387
388 void ir_print_visitor::visit(ir_dereference_record *ir)
389 {
390 fprintf(f, "(record_ref ");
391 ir->record->accept(this);
392 fprintf(f, " %s) ", ir->field);
393 }
394
395
396 void ir_print_visitor::visit(ir_assignment *ir)
397 {
398 fprintf(f, "(assign ");
399
400 if (ir->condition)
401 ir->condition->accept(this);
402
403 char mask[5];
404 unsigned j = 0;
405
406 for (unsigned i = 0; i < 4; i++) {
407 if ((ir->write_mask & (1 << i)) != 0) {
408 mask[j] = "xyzw"[i];
409 j++;
410 }
411 }
412 mask[j] = '\0';
413
414 fprintf(f, " (%s) ", mask);
415
416 ir->lhs->accept(this);
417
418 fprintf(f, " ");
419
420 ir->rhs->accept(this);
421 fprintf(f, ") ");
422 }
423
424
425 void ir_print_visitor::visit(ir_constant *ir)
426 {
427 fprintf(f, "(constant ");
428 print_type(f, ir->type);
429 fprintf(f, " (");
430
431 if (ir->type->is_array()) {
432 for (unsigned i = 0; i < ir->type->length; i++)
433 ir->get_array_element(i)->accept(this);
434 } else if (ir->type->is_record()) {
435 ir_constant *value = (ir_constant *) ir->components.get_head();
436 for (unsigned i = 0; i < ir->type->length; i++) {
437 fprintf(f, "(%s ", ir->type->fields.structure[i].name);
438 value->accept(this);
439 fprintf(f, ")");
440
441 value = (ir_constant *) value->next;
442 }
443 } else {
444 for (unsigned i = 0; i < ir->type->components(); i++) {
445 if (i != 0)
446 fprintf(f, " ");
447 switch (ir->type->base_type) {
448 case GLSL_TYPE_UINT: fprintf(f, "%u", ir->value.u[i]); break;
449 case GLSL_TYPE_INT: fprintf(f, "%d", ir->value.i[i]); break;
450 case GLSL_TYPE_FLOAT:
451 if (ir->value.f[i] == 0.0f)
452 /* 0.0 == -0.0, so print with %f to get the proper sign. */
453 fprintf(f, "%f", ir->value.f[i]);
454 else if (fabs(ir->value.f[i]) < 0.000001f)
455 fprintf(f, "%a", ir->value.f[i]);
456 else if (fabs(ir->value.f[i]) > 1000000.0f)
457 fprintf(f, "%e", ir->value.f[i]);
458 else
459 fprintf(f, "%f", ir->value.f[i]);
460 break;
461 case GLSL_TYPE_BOOL: fprintf(f, "%d", ir->value.b[i]); break;
462 case GLSL_TYPE_DOUBLE:
463 if (ir->value.d[i] == 0.0)
464 /* 0.0 == -0.0, so print with %f to get the proper sign. */
465 fprintf(f, "%.1f", ir->value.d[i]);
466 else if (fabs(ir->value.d[i]) < 0.000001)
467 fprintf(f, "%a", ir->value.d[i]);
468 else if (fabs(ir->value.d[i]) > 1000000.0)
469 fprintf(f, "%e", ir->value.d[i]);
470 else
471 fprintf(f, "%f", ir->value.d[i]);
472 break;
473 default: assert(0);
474 }
475 }
476 }
477 fprintf(f, ")) ");
478 }
479
480
481 void
482 ir_print_visitor::visit(ir_call *ir)
483 {
484 fprintf(f, "(call %s ", ir->callee_name());
485 if (ir->return_deref)
486 ir->return_deref->accept(this);
487 fprintf(f, " (");
488 foreach_in_list(ir_rvalue, param, &ir->actual_parameters) {
489 param->accept(this);
490 }
491 fprintf(f, "))\n");
492 }
493
494
495 void
496 ir_print_visitor::visit(ir_return *ir)
497 {
498 fprintf(f, "(return");
499
500 ir_rvalue *const value = ir->get_value();
501 if (value) {
502 fprintf(f, " ");
503 value->accept(this);
504 }
505
506 fprintf(f, ")");
507 }
508
509
510 void
511 ir_print_visitor::visit(ir_discard *ir)
512 {
513 fprintf(f, "(discard ");
514
515 if (ir->condition != NULL) {
516 fprintf(f, " ");
517 ir->condition->accept(this);
518 }
519
520 fprintf(f, ")");
521 }
522
523
524 void
525 ir_print_visitor::visit(ir_if *ir)
526 {
527 fprintf(f, "(if ");
528 ir->condition->accept(this);
529
530 fprintf(f, "(\n");
531 indentation++;
532
533 foreach_in_list(ir_instruction, inst, &ir->then_instructions) {
534 indent();
535 inst->accept(this);
536 fprintf(f, "\n");
537 }
538
539 indentation--;
540 indent();
541 fprintf(f, ")\n");
542
543 indent();
544 if (!ir->else_instructions.is_empty()) {
545 fprintf(f, "(\n");
546 indentation++;
547
548 foreach_in_list(ir_instruction, inst, &ir->else_instructions) {
549 indent();
550 inst->accept(this);
551 fprintf(f, "\n");
552 }
553 indentation--;
554 indent();
555 fprintf(f, "))\n");
556 } else {
557 fprintf(f, "())\n");
558 }
559 }
560
561
562 void
563 ir_print_visitor::visit(ir_loop *ir)
564 {
565 fprintf(f, "(loop (\n");
566 indentation++;
567
568 foreach_in_list(ir_instruction, inst, &ir->body_instructions) {
569 indent();
570 inst->accept(this);
571 fprintf(f, "\n");
572 }
573 indentation--;
574 indent();
575 fprintf(f, "))\n");
576 }
577
578
579 void
580 ir_print_visitor::visit(ir_loop_jump *ir)
581 {
582 fprintf(f, "%s", ir->is_break() ? "break" : "continue");
583 }
584
585 void
586 ir_print_visitor::visit(ir_emit_vertex *ir)
587 {
588 fprintf(f, "(emit-vertex ");
589 ir->stream->accept(this);
590 fprintf(f, ")\n");
591 }
592
593 void
594 ir_print_visitor::visit(ir_end_primitive *ir)
595 {
596 fprintf(f, "(end-primitive ");
597 ir->stream->accept(this);
598 fprintf(f, ")\n");
599 }
600
601 void
602 ir_print_visitor::visit(ir_barrier *)
603 {
604 fprintf(f, "(barrier)\n");
605 }