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