fb0dd08f810b74352a2bcc1c52a45747da72573c
[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(const glsl_type *t);
31
32 void
33 ir_instruction::print(void) const
34 {
35 ir_instruction *deconsted = const_cast<ir_instruction *>(this);
36
37 ir_print_visitor v;
38 deconsted->accept(&v);
39 }
40
41 void
42 _mesa_print_ir(exec_list *instructions,
43 struct _mesa_glsl_parse_state *state)
44 {
45 if (state) {
46 for (unsigned i = 0; i < state->num_user_structures; i++) {
47 const glsl_type *const s = state->user_structures[i];
48
49 printf("(structure (%s) (%s@%p) (%u) (\n",
50 s->name, s->name, (void *) s, s->length);
51
52 for (unsigned j = 0; j < s->length; j++) {
53 printf("\t((");
54 print_type(s->fields.structure[j].type);
55 printf(")(%s))\n", s->fields.structure[j].name);
56 }
57
58 printf(")\n");
59 }
60 }
61
62 printf("(\n");
63 foreach_iter(exec_list_iterator, iter, *instructions) {
64 ir_instruction *ir = (ir_instruction *)iter.get();
65 ir->print();
66 if (ir->ir_type != ir_type_function)
67 printf("\n");
68 }
69 printf("\n)");
70 }
71
72 ir_print_visitor::ir_print_visitor()
73 {
74 indentation = 0;
75 printable_names =
76 hash_table_ctor(32, hash_table_pointer_hash, hash_table_pointer_compare);
77 symbols = _mesa_symbol_table_ctor();
78 mem_ctx = ralloc_context(NULL);
79 }
80
81 ir_print_visitor::~ir_print_visitor()
82 {
83 hash_table_dtor(printable_names);
84 _mesa_symbol_table_dtor(symbols);
85 ralloc_free(mem_ctx);
86 }
87
88 void ir_print_visitor::indent(void)
89 {
90 for (int i = 0; i < indentation; i++)
91 printf(" ");
92 }
93
94 const char *
95 ir_print_visitor::unique_name(ir_variable *var)
96 {
97 /* var->name can be NULL in function prototypes when a type is given for a
98 * parameter but no name is given. In that case, just return an empty
99 * string. Don't worry about tracking the generated name in the printable
100 * names hash because this is the only scope where it can ever appear.
101 */
102 if (var->name == NULL) {
103 static unsigned arg = 1;
104 return ralloc_asprintf(this->mem_ctx, "parameter@%u", arg++);
105 }
106
107 /* Do we already have a name for this variable? */
108 const char *name = (const char *) hash_table_find(this->printable_names, var);
109 if (name != NULL)
110 return name;
111
112 /* If there's no conflict, just use the original name */
113 if (_mesa_symbol_table_find_symbol(this->symbols, -1, var->name) == NULL) {
114 name = var->name;
115 } else {
116 static unsigned i = 1;
117 name = ralloc_asprintf(this->mem_ctx, "%s@%u", var->name, ++i);
118 }
119 hash_table_insert(this->printable_names, (void *) name, var);
120 _mesa_symbol_table_add_symbol(this->symbols, -1, name, var);
121 return name;
122 }
123
124 static void
125 print_type(const glsl_type *t)
126 {
127 if (t->base_type == GLSL_TYPE_ARRAY) {
128 printf("(array ");
129 print_type(t->fields.array);
130 printf(" %u)", t->length);
131 } else if ((t->base_type == GLSL_TYPE_STRUCT)
132 && (strncmp("gl_", t->name, 3) != 0)) {
133 printf("%s@%p", t->name, (void *) t);
134 } else {
135 printf("%s", t->name);
136 }
137 }
138
139 void ir_print_visitor::visit(ir_rvalue *ir)
140 {
141 printf("error");
142 }
143
144 void ir_print_visitor::visit(ir_variable *ir)
145 {
146 printf("(declare ");
147
148 const char *const cent = (ir->centroid) ? "centroid " : "";
149 const char *const inv = (ir->invariant) ? "invariant " : "";
150 const char *const mode[] = { "", "uniform ", "shader_in ", "shader_out ",
151 "in ", "out ", "inout ",
152 "const_in ", "sys ", "temporary " };
153 const char *const interp[] = { "", "smooth", "flat", "noperspective" };
154 STATIC_ASSERT(ARRAY_SIZE(interp) == INTERP_QUALIFIER_COUNT);
155
156 printf("(%s%s%s%s) ",
157 cent, inv, mode[ir->mode], interp[ir->interpolation]);
158
159 print_type(ir->type);
160 printf(" %s)", unique_name(ir));
161 }
162
163
164 void ir_print_visitor::visit(ir_function_signature *ir)
165 {
166 _mesa_symbol_table_push_scope(symbols);
167 printf("(signature ");
168 indentation++;
169
170 print_type(ir->return_type);
171 printf("\n");
172 indent();
173
174 printf("(parameters\n");
175 indentation++;
176
177 foreach_iter(exec_list_iterator, iter, ir->parameters) {
178 ir_variable *const inst = (ir_variable *) iter.get();
179
180 indent();
181 inst->accept(this);
182 printf("\n");
183 }
184 indentation--;
185
186 indent();
187 printf(")\n");
188
189 indent();
190
191 printf("(\n");
192 indentation++;
193
194 foreach_iter(exec_list_iterator, iter, ir->body) {
195 ir_instruction *const inst = (ir_instruction *) iter.get();
196
197 indent();
198 inst->accept(this);
199 printf("\n");
200 }
201 indentation--;
202 indent();
203 printf("))\n");
204 indentation--;
205 _mesa_symbol_table_pop_scope(symbols);
206 }
207
208
209 void ir_print_visitor::visit(ir_function *ir)
210 {
211 printf("(function %s\n", ir->name);
212 indentation++;
213 foreach_iter(exec_list_iterator, iter, *ir) {
214 ir_function_signature *const sig = (ir_function_signature *) iter.get();
215 indent();
216 sig->accept(this);
217 printf("\n");
218 }
219 indentation--;
220 indent();
221 printf(")\n\n");
222 }
223
224
225 void ir_print_visitor::visit(ir_expression *ir)
226 {
227 printf("(expression ");
228
229 print_type(ir->type);
230
231 printf(" %s ", ir->operator_string());
232
233 for (unsigned i = 0; i < ir->get_num_operands(); i++) {
234 ir->operands[i]->accept(this);
235 }
236
237 printf(") ");
238 }
239
240
241 void ir_print_visitor::visit(ir_texture *ir)
242 {
243 printf("(%s ", ir->opcode_string());
244
245 print_type(ir->type);
246 printf(" ");
247
248 ir->sampler->accept(this);
249 printf(" ");
250
251 if (ir->op != ir_txs) {
252 ir->coordinate->accept(this);
253
254 printf(" ");
255
256 if (ir->offset != NULL) {
257 ir->offset->accept(this);
258 } else {
259 printf("0");
260 }
261
262 printf(" ");
263 }
264
265 if (ir->op != ir_txf && ir->op != ir_txf_ms && ir->op != ir_txs) {
266 if (ir->projector)
267 ir->projector->accept(this);
268 else
269 printf("1");
270
271 if (ir->shadow_comparitor) {
272 printf(" ");
273 ir->shadow_comparitor->accept(this);
274 } else {
275 printf(" ()");
276 }
277 }
278
279 printf(" ");
280 switch (ir->op)
281 {
282 case ir_tex:
283 case ir_lod:
284 break;
285 case ir_txb:
286 ir->lod_info.bias->accept(this);
287 break;
288 case ir_txl:
289 case ir_txf:
290 case ir_txs:
291 ir->lod_info.lod->accept(this);
292 break;
293 case ir_txf_ms:
294 ir->lod_info.sample_index->accept(this);
295 break;
296 case ir_txd:
297 printf("(");
298 ir->lod_info.grad.dPdx->accept(this);
299 printf(" ");
300 ir->lod_info.grad.dPdy->accept(this);
301 printf(")");
302 break;
303 };
304 printf(")");
305 }
306
307
308 void ir_print_visitor::visit(ir_swizzle *ir)
309 {
310 const unsigned swiz[4] = {
311 ir->mask.x,
312 ir->mask.y,
313 ir->mask.z,
314 ir->mask.w,
315 };
316
317 printf("(swiz ");
318 for (unsigned i = 0; i < ir->mask.num_components; i++) {
319 printf("%c", "xyzw"[swiz[i]]);
320 }
321 printf(" ");
322 ir->val->accept(this);
323 printf(")");
324 }
325
326
327 void ir_print_visitor::visit(ir_dereference_variable *ir)
328 {
329 ir_variable *var = ir->variable_referenced();
330 printf("(var_ref %s) ", unique_name(var));
331 }
332
333
334 void ir_print_visitor::visit(ir_dereference_array *ir)
335 {
336 printf("(array_ref ");
337 ir->array->accept(this);
338 ir->array_index->accept(this);
339 printf(") ");
340 }
341
342
343 void ir_print_visitor::visit(ir_dereference_record *ir)
344 {
345 printf("(record_ref ");
346 ir->record->accept(this);
347 printf(" %s) ", ir->field);
348 }
349
350
351 void ir_print_visitor::visit(ir_assignment *ir)
352 {
353 printf("(assign ");
354
355 if (ir->condition)
356 ir->condition->accept(this);
357
358 char mask[5];
359 unsigned j = 0;
360
361 for (unsigned i = 0; i < 4; i++) {
362 if ((ir->write_mask & (1 << i)) != 0) {
363 mask[j] = "xyzw"[i];
364 j++;
365 }
366 }
367 mask[j] = '\0';
368
369 printf(" (%s) ", mask);
370
371 ir->lhs->accept(this);
372
373 printf(" ");
374
375 ir->rhs->accept(this);
376 printf(") ");
377 }
378
379
380 void ir_print_visitor::visit(ir_constant *ir)
381 {
382 printf("(constant ");
383 print_type(ir->type);
384 printf(" (");
385
386 if (ir->type->is_array()) {
387 for (unsigned i = 0; i < ir->type->length; i++)
388 ir->get_array_element(i)->accept(this);
389 } else if (ir->type->is_record()) {
390 ir_constant *value = (ir_constant *) ir->components.get_head();
391 for (unsigned i = 0; i < ir->type->length; i++) {
392 printf("(%s ", ir->type->fields.structure[i].name);
393 value->accept(this);
394 printf(")");
395
396 value = (ir_constant *) value->next;
397 }
398 } else {
399 for (unsigned i = 0; i < ir->type->components(); i++) {
400 if (i != 0)
401 printf(" ");
402 switch (ir->type->base_type) {
403 case GLSL_TYPE_UINT: printf("%u", ir->value.u[i]); break;
404 case GLSL_TYPE_INT: printf("%d", ir->value.i[i]); break;
405 case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
406 case GLSL_TYPE_BOOL: printf("%d", ir->value.b[i]); break;
407 default: assert(0);
408 }
409 }
410 }
411 printf(")) ");
412 }
413
414
415 void
416 ir_print_visitor::visit(ir_call *ir)
417 {
418 printf("(call %s ", ir->callee_name());
419 if (ir->return_deref)
420 ir->return_deref->accept(this);
421 printf(" (");
422 foreach_iter(exec_list_iterator, iter, *ir) {
423 ir_instruction *const inst = (ir_instruction *) iter.get();
424
425 inst->accept(this);
426 }
427 printf("))\n");
428 }
429
430
431 void
432 ir_print_visitor::visit(ir_return *ir)
433 {
434 printf("(return");
435
436 ir_rvalue *const value = ir->get_value();
437 if (value) {
438 printf(" ");
439 value->accept(this);
440 }
441
442 printf(")");
443 }
444
445
446 void
447 ir_print_visitor::visit(ir_discard *ir)
448 {
449 printf("(discard ");
450
451 if (ir->condition != NULL) {
452 printf(" ");
453 ir->condition->accept(this);
454 }
455
456 printf(")");
457 }
458
459
460 void
461 ir_print_visitor::visit(ir_if *ir)
462 {
463 printf("(if ");
464 ir->condition->accept(this);
465
466 printf("(\n");
467 indentation++;
468
469 foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
470 ir_instruction *const inst = (ir_instruction *) iter.get();
471
472 indent();
473 inst->accept(this);
474 printf("\n");
475 }
476
477 indentation--;
478 indent();
479 printf(")\n");
480
481 indent();
482 if (!ir->else_instructions.is_empty()) {
483 printf("(\n");
484 indentation++;
485
486 foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
487 ir_instruction *const inst = (ir_instruction *) iter.get();
488
489 indent();
490 inst->accept(this);
491 printf("\n");
492 }
493 indentation--;
494 indent();
495 printf("))\n");
496 } else {
497 printf("())\n");
498 }
499 }
500
501
502 void
503 ir_print_visitor::visit(ir_loop *ir)
504 {
505 printf("(loop (");
506 if (ir->counter != NULL)
507 ir->counter->accept(this);
508 printf(") (");
509 if (ir->from != NULL)
510 ir->from->accept(this);
511 printf(") (");
512 if (ir->to != NULL)
513 ir->to->accept(this);
514 printf(") (");
515 if (ir->increment != NULL)
516 ir->increment->accept(this);
517 printf(") (\n");
518 indentation++;
519
520 foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
521 ir_instruction *const inst = (ir_instruction *) iter.get();
522
523 indent();
524 inst->accept(this);
525 printf("\n");
526 }
527 indentation--;
528 indent();
529 printf("))\n");
530 }
531
532
533 void
534 ir_print_visitor::visit(ir_loop_jump *ir)
535 {
536 printf("%s", ir->is_break() ? "break" : "continue");
537 }