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