glsl: include streamId when reading/printing ir_variable IR.
[mesa.git] / src / 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 "glsl_types.h"
26 #include "glsl_parser_extras.h"
27 #include "main/macros.h"
28 #include "program/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_list(n, instructions) {
71 ir_instruction *ir = (ir_instruction *) n;
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 hash_table_ctor(32, hash_table_pointer_hash, hash_table_pointer_compare);
94 symbols = _mesa_symbol_table_ctor();
95 mem_ctx = ralloc_context(NULL);
96 }
97
98 ir_print_visitor::~ir_print_visitor()
99 {
100 hash_table_dtor(printable_names);
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 const char *name = (const char *) hash_table_find(this->printable_names, var);
126 if (name != NULL)
127 return name;
128
129 /* If there's no conflict, just use the original name */
130 if (_mesa_symbol_table_find_symbol(this->symbols, -1, var->name) == NULL) {
131 name = var->name;
132 } else {
133 static unsigned i = 1;
134 name = ralloc_asprintf(this->mem_ctx, "%s@%u", var->name, ++i);
135 }
136 hash_table_insert(this->printable_names, (void *) name, var);
137 _mesa_symbol_table_add_symbol(this->symbols, -1, name, var);
138 return name;
139 }
140
141 static void
142 print_type(FILE *f, const glsl_type *t)
143 {
144 if (t->base_type == GLSL_TYPE_ARRAY) {
145 fprintf(f, "(array ");
146 print_type(f, t->fields.array);
147 fprintf(f, " %u)", t->length);
148 } else if ((t->base_type == GLSL_TYPE_STRUCT)
149 && !is_gl_identifier(t->name)) {
150 fprintf(f, "%s@%p", t->name, (void *) t);
151 } else {
152 fprintf(f, "%s", t->name);
153 }
154 }
155
156 void ir_print_visitor::visit(ir_rvalue *)
157 {
158 fprintf(f, "error");
159 }
160
161 void ir_print_visitor::visit(ir_variable *ir)
162 {
163 fprintf(f, "(declare ");
164
165 const char *const cent = (ir->data.centroid) ? "centroid " : "";
166 const char *const samp = (ir->data.sample) ? "sample " : "";
167 const char *const inv = (ir->data.invariant) ? "invariant " : "";
168 const char *const mode[] = { "", "uniform ", "shader_in ", "shader_out ",
169 "in ", "out ", "inout ",
170 "const_in ", "sys ", "temporary " };
171 STATIC_ASSERT(ARRAY_SIZE(mode) == ir_var_mode_count);
172 const char *const stream [] = {"", "stream1 ", "stream2 ", "stream3 "};
173 const char *const interp[] = { "", "smooth", "flat", "noperspective" };
174 STATIC_ASSERT(ARRAY_SIZE(interp) == INTERP_QUALIFIER_COUNT);
175
176 fprintf(f, "(%s%s%s%s%s%s) ",
177 cent, samp, inv, mode[ir->data.mode],
178 stream[ir->data.stream],
179 interp[ir->data.interpolation]);
180
181 print_type(f, ir->type);
182 fprintf(f, " %s)", unique_name(ir));
183 }
184
185
186 void ir_print_visitor::visit(ir_function_signature *ir)
187 {
188 _mesa_symbol_table_push_scope(symbols);
189 fprintf(f, "(signature ");
190 indentation++;
191
192 print_type(f, ir->return_type);
193 fprintf(f, "\n");
194 indent();
195
196 fprintf(f, "(parameters\n");
197 indentation++;
198
199 foreach_list(n, &ir->parameters) {
200 ir_variable *const inst = (ir_variable *) n;
201
202 indent();
203 inst->accept(this);
204 fprintf(f, "\n");
205 }
206 indentation--;
207
208 indent();
209 fprintf(f, ")\n");
210
211 indent();
212
213 fprintf(f, "(\n");
214 indentation++;
215
216 foreach_list(n, &ir->body) {
217 ir_instruction *const inst = (ir_instruction *) n;
218
219 indent();
220 inst->accept(this);
221 fprintf(f, "\n");
222 }
223 indentation--;
224 indent();
225 fprintf(f, "))\n");
226 indentation--;
227 _mesa_symbol_table_pop_scope(symbols);
228 }
229
230
231 void ir_print_visitor::visit(ir_function *ir)
232 {
233 fprintf(f, "(function %s\n", ir->name);
234 indentation++;
235 foreach_list(n, &ir->signatures) {
236 ir_function_signature *const sig = (ir_function_signature *) n;
237 indent();
238 sig->accept(this);
239 fprintf(f, "\n");
240 }
241 indentation--;
242 indent();
243 fprintf(f, ")\n\n");
244 }
245
246
247 void ir_print_visitor::visit(ir_expression *ir)
248 {
249 fprintf(f, "(expression ");
250
251 print_type(f, ir->type);
252
253 fprintf(f, " %s ", ir->operator_string());
254
255 for (unsigned i = 0; i < ir->get_num_operands(); i++) {
256 ir->operands[i]->accept(this);
257 }
258
259 fprintf(f, ") ");
260 }
261
262
263 void ir_print_visitor::visit(ir_texture *ir)
264 {
265 fprintf(f, "(%s ", ir->opcode_string());
266
267 print_type(f, ir->type);
268 fprintf(f, " ");
269
270 ir->sampler->accept(this);
271 fprintf(f, " ");
272
273 if (ir->op != ir_txs && ir->op != ir_query_levels) {
274 ir->coordinate->accept(this);
275
276 fprintf(f, " ");
277
278 if (ir->offset != NULL) {
279 ir->offset->accept(this);
280 } else {
281 fprintf(f, "0");
282 }
283
284 fprintf(f, " ");
285 }
286
287 if (ir->op != ir_txf && ir->op != ir_txf_ms &&
288 ir->op != ir_txs && ir->op != ir_tg4 &&
289 ir->op != ir_query_levels) {
290 if (ir->projector)
291 ir->projector->accept(this);
292 else
293 fprintf(f, "1");
294
295 if (ir->shadow_comparitor) {
296 fprintf(f, " ");
297 ir->shadow_comparitor->accept(this);
298 } else {
299 fprintf(f, " ()");
300 }
301 }
302
303 fprintf(f, " ");
304 switch (ir->op)
305 {
306 case ir_tex:
307 case ir_lod:
308 case ir_query_levels:
309 break;
310 case ir_txb:
311 ir->lod_info.bias->accept(this);
312 break;
313 case ir_txl:
314 case ir_txf:
315 case ir_txs:
316 ir->lod_info.lod->accept(this);
317 break;
318 case ir_txf_ms:
319 ir->lod_info.sample_index->accept(this);
320 break;
321 case ir_txd:
322 fprintf(f, "(");
323 ir->lod_info.grad.dPdx->accept(this);
324 fprintf(f, " ");
325 ir->lod_info.grad.dPdy->accept(this);
326 fprintf(f, ")");
327 break;
328 case ir_tg4:
329 ir->lod_info.component->accept(this);
330 break;
331 };
332 fprintf(f, ")");
333 }
334
335
336 void ir_print_visitor::visit(ir_swizzle *ir)
337 {
338 const unsigned swiz[4] = {
339 ir->mask.x,
340 ir->mask.y,
341 ir->mask.z,
342 ir->mask.w,
343 };
344
345 fprintf(f, "(swiz ");
346 for (unsigned i = 0; i < ir->mask.num_components; i++) {
347 fprintf(f, "%c", "xyzw"[swiz[i]]);
348 }
349 fprintf(f, " ");
350 ir->val->accept(this);
351 fprintf(f, ")");
352 }
353
354
355 void ir_print_visitor::visit(ir_dereference_variable *ir)
356 {
357 ir_variable *var = ir->variable_referenced();
358 fprintf(f, "(var_ref %s) ", unique_name(var));
359 }
360
361
362 void ir_print_visitor::visit(ir_dereference_array *ir)
363 {
364 fprintf(f, "(array_ref ");
365 ir->array->accept(this);
366 ir->array_index->accept(this);
367 fprintf(f, ") ");
368 }
369
370
371 void ir_print_visitor::visit(ir_dereference_record *ir)
372 {
373 fprintf(f, "(record_ref ");
374 ir->record->accept(this);
375 fprintf(f, " %s) ", ir->field);
376 }
377
378
379 void ir_print_visitor::visit(ir_assignment *ir)
380 {
381 fprintf(f, "(assign ");
382
383 if (ir->condition)
384 ir->condition->accept(this);
385
386 char mask[5];
387 unsigned j = 0;
388
389 for (unsigned i = 0; i < 4; i++) {
390 if ((ir->write_mask & (1 << i)) != 0) {
391 mask[j] = "xyzw"[i];
392 j++;
393 }
394 }
395 mask[j] = '\0';
396
397 fprintf(f, " (%s) ", mask);
398
399 ir->lhs->accept(this);
400
401 fprintf(f, " ");
402
403 ir->rhs->accept(this);
404 fprintf(f, ") ");
405 }
406
407
408 void ir_print_visitor::visit(ir_constant *ir)
409 {
410 fprintf(f, "(constant ");
411 print_type(f, ir->type);
412 fprintf(f, " (");
413
414 if (ir->type->is_array()) {
415 for (unsigned i = 0; i < ir->type->length; i++)
416 ir->get_array_element(i)->accept(this);
417 } else if (ir->type->is_record()) {
418 ir_constant *value = (ir_constant *) ir->components.get_head();
419 for (unsigned i = 0; i < ir->type->length; i++) {
420 fprintf(f, "(%s ", ir->type->fields.structure[i].name);
421 value->accept(this);
422 fprintf(f, ")");
423
424 value = (ir_constant *) value->next;
425 }
426 } else {
427 for (unsigned i = 0; i < ir->type->components(); i++) {
428 if (i != 0)
429 fprintf(f, " ");
430 switch (ir->type->base_type) {
431 case GLSL_TYPE_UINT: fprintf(f, "%u", ir->value.u[i]); break;
432 case GLSL_TYPE_INT: fprintf(f, "%d", ir->value.i[i]); break;
433 case GLSL_TYPE_FLOAT:
434 if (ir->value.f[i] == 0.0f)
435 /* 0.0 == -0.0, so print with %f to get the proper sign. */
436 fprintf(f, "%f", ir->value.f[i]);
437 else if (fabs(ir->value.f[i]) < 0.000001f)
438 fprintf(f, "%a", ir->value.f[i]);
439 else if (fabs(ir->value.f[i]) > 1000000.0f)
440 fprintf(f, "%e", ir->value.f[i]);
441 else
442 fprintf(f, "%f", ir->value.f[i]);
443 break;
444 case GLSL_TYPE_BOOL: fprintf(f, "%d", ir->value.b[i]); break;
445 default: assert(0);
446 }
447 }
448 }
449 fprintf(f, ")) ");
450 }
451
452
453 void
454 ir_print_visitor::visit(ir_call *ir)
455 {
456 fprintf(f, "(call %s ", ir->callee_name());
457 if (ir->return_deref)
458 ir->return_deref->accept(this);
459 fprintf(f, " (");
460 foreach_list(n, &ir->actual_parameters) {
461 ir_rvalue *const param = (ir_rvalue *) n;
462
463 param->accept(this);
464 }
465 fprintf(f, "))\n");
466 }
467
468
469 void
470 ir_print_visitor::visit(ir_return *ir)
471 {
472 fprintf(f, "(return");
473
474 ir_rvalue *const value = ir->get_value();
475 if (value) {
476 fprintf(f, " ");
477 value->accept(this);
478 }
479
480 fprintf(f, ")");
481 }
482
483
484 void
485 ir_print_visitor::visit(ir_discard *ir)
486 {
487 fprintf(f, "(discard ");
488
489 if (ir->condition != NULL) {
490 fprintf(f, " ");
491 ir->condition->accept(this);
492 }
493
494 fprintf(f, ")");
495 }
496
497
498 void
499 ir_print_visitor::visit(ir_if *ir)
500 {
501 fprintf(f, "(if ");
502 ir->condition->accept(this);
503
504 fprintf(f, "(\n");
505 indentation++;
506
507 foreach_list(n, &ir->then_instructions) {
508 ir_instruction *const inst = (ir_instruction *) n;
509
510 indent();
511 inst->accept(this);
512 fprintf(f, "\n");
513 }
514
515 indentation--;
516 indent();
517 fprintf(f, ")\n");
518
519 indent();
520 if (!ir->else_instructions.is_empty()) {
521 fprintf(f, "(\n");
522 indentation++;
523
524 foreach_list(n, &ir->else_instructions) {
525 ir_instruction *const inst = (ir_instruction *) n;
526
527 indent();
528 inst->accept(this);
529 fprintf(f, "\n");
530 }
531 indentation--;
532 indent();
533 fprintf(f, "))\n");
534 } else {
535 fprintf(f, "())\n");
536 }
537 }
538
539
540 void
541 ir_print_visitor::visit(ir_loop *ir)
542 {
543 fprintf(f, "(loop (\n");
544 indentation++;
545
546 foreach_list(n, &ir->body_instructions) {
547 ir_instruction *const inst = (ir_instruction *) n;
548
549 indent();
550 inst->accept(this);
551 fprintf(f, "\n");
552 }
553 indentation--;
554 indent();
555 fprintf(f, "))\n");
556 }
557
558
559 void
560 ir_print_visitor::visit(ir_loop_jump *ir)
561 {
562 fprintf(f, "%s", ir->is_break() ? "break" : "continue");
563 }
564
565 void
566 ir_print_visitor::visit(ir_emit_vertex *ir)
567 {
568 fprintf(f, "(emit-vertex ");
569 ir->stream->accept(this);
570 fprintf(f, ")\n");
571 }
572
573 void
574 ir_print_visitor::visit(ir_end_primitive *ir)
575 {
576 fprintf(f, "(end-primitive ");
577 ir->stream->accept(this);
578 fprintf(f, ")\n");
579
580 }