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