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