glsl: Remove pointless uses of glsl_type::get_base_type().
[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
139 void ir_print_visitor::visit(ir_variable *ir)
140 {
141 printf("(declare ");
142
143 const char *const cent = (ir->centroid) ? "centroid " : "";
144 const char *const inv = (ir->invariant) ? "invariant " : "";
145 const char *const mode[] = { "", "uniform ", "in ", "out ", "inout ",
146 "const_in ", "sys ", "temporary " };
147 const char *const interp[] = { "", "flat", "noperspective" };
148
149 printf("(%s%s%s%s) ",
150 cent, inv, mode[ir->mode], interp[ir->interpolation]);
151
152 print_type(ir->type);
153 printf(" %s)", unique_name(ir));
154 }
155
156
157 void ir_print_visitor::visit(ir_function_signature *ir)
158 {
159 _mesa_symbol_table_push_scope(symbols);
160 printf("(signature ");
161 indentation++;
162
163 print_type(ir->return_type);
164 printf("\n");
165 indent();
166
167 printf("(parameters\n");
168 indentation++;
169
170 foreach_iter(exec_list_iterator, iter, ir->parameters) {
171 ir_variable *const inst = (ir_variable *) iter.get();
172
173 indent();
174 inst->accept(this);
175 printf("\n");
176 }
177 indentation--;
178
179 indent();
180 printf(")\n");
181
182 indent();
183
184 printf("(\n");
185 indentation++;
186
187 foreach_iter(exec_list_iterator, iter, ir->body) {
188 ir_instruction *const inst = (ir_instruction *) iter.get();
189
190 indent();
191 inst->accept(this);
192 printf("\n");
193 }
194 indentation--;
195 indent();
196 printf("))\n");
197 indentation--;
198 _mesa_symbol_table_pop_scope(symbols);
199 }
200
201
202 void ir_print_visitor::visit(ir_function *ir)
203 {
204 printf("(function %s\n", ir->name);
205 indentation++;
206 foreach_iter(exec_list_iterator, iter, *ir) {
207 ir_function_signature *const sig = (ir_function_signature *) iter.get();
208 indent();
209 sig->accept(this);
210 printf("\n");
211 }
212 indentation--;
213 indent();
214 printf(")\n\n");
215 }
216
217
218 void ir_print_visitor::visit(ir_expression *ir)
219 {
220 printf("(expression ");
221
222 print_type(ir->type);
223
224 printf(" %s ", ir->operator_string());
225
226 for (unsigned i = 0; i < ir->get_num_operands(); i++) {
227 ir->operands[i]->accept(this);
228 }
229
230 printf(") ");
231 }
232
233
234 void ir_print_visitor::visit(ir_texture *ir)
235 {
236 printf("(%s ", ir->opcode_string());
237
238 print_type(ir->type);
239 printf(" ");
240
241 ir->sampler->accept(this);
242 printf(" ");
243
244 if (ir->op != ir_txs) {
245 ir->coordinate->accept(this);
246
247 printf(" ");
248
249 if (ir->offset != NULL) {
250 ir->offset->accept(this);
251 } else {
252 printf("0");
253 }
254
255 printf(" ");
256 }
257
258 if (ir->op != ir_txf && ir->op != ir_txs) {
259 if (ir->projector)
260 ir->projector->accept(this);
261 else
262 printf("1");
263
264 if (ir->shadow_comparitor) {
265 printf(" ");
266 ir->shadow_comparitor->accept(this);
267 } else {
268 printf(" ()");
269 }
270 }
271
272 printf(" ");
273 switch (ir->op)
274 {
275 case ir_tex:
276 break;
277 case ir_txb:
278 ir->lod_info.bias->accept(this);
279 break;
280 case ir_txl:
281 case ir_txf:
282 case ir_txs:
283 ir->lod_info.lod->accept(this);
284 break;
285 case ir_txd:
286 printf("(");
287 ir->lod_info.grad.dPdx->accept(this);
288 printf(" ");
289 ir->lod_info.grad.dPdy->accept(this);
290 printf(")");
291 break;
292 };
293 printf(")");
294 }
295
296
297 void ir_print_visitor::visit(ir_swizzle *ir)
298 {
299 const unsigned swiz[4] = {
300 ir->mask.x,
301 ir->mask.y,
302 ir->mask.z,
303 ir->mask.w,
304 };
305
306 printf("(swiz ");
307 for (unsigned i = 0; i < ir->mask.num_components; i++) {
308 printf("%c", "xyzw"[swiz[i]]);
309 }
310 printf(" ");
311 ir->val->accept(this);
312 printf(")");
313 }
314
315
316 void ir_print_visitor::visit(ir_dereference_variable *ir)
317 {
318 ir_variable *var = ir->variable_referenced();
319 printf("(var_ref %s) ", unique_name(var));
320 }
321
322
323 void ir_print_visitor::visit(ir_dereference_array *ir)
324 {
325 printf("(array_ref ");
326 ir->array->accept(this);
327 ir->array_index->accept(this);
328 printf(") ");
329 }
330
331
332 void ir_print_visitor::visit(ir_dereference_record *ir)
333 {
334 printf("(record_ref ");
335 ir->record->accept(this);
336 printf(" %s) ", ir->field);
337 }
338
339
340 void ir_print_visitor::visit(ir_assignment *ir)
341 {
342 printf("(assign ");
343
344 if (ir->condition)
345 ir->condition->accept(this);
346
347 char mask[5];
348 unsigned j = 0;
349
350 for (unsigned i = 0; i < 4; i++) {
351 if ((ir->write_mask & (1 << i)) != 0) {
352 mask[j] = "xyzw"[i];
353 j++;
354 }
355 }
356 mask[j] = '\0';
357
358 printf(" (%s) ", mask);
359
360 ir->lhs->accept(this);
361
362 printf(" ");
363
364 ir->rhs->accept(this);
365 printf(") ");
366 }
367
368
369 void ir_print_visitor::visit(ir_constant *ir)
370 {
371 printf("(constant ");
372 print_type(ir->type);
373 printf(" (");
374
375 if (ir->type->is_array()) {
376 for (unsigned i = 0; i < ir->type->length; i++)
377 ir->get_array_element(i)->accept(this);
378 } else if (ir->type->is_record()) {
379 ir_constant *value = (ir_constant *) ir->components.get_head();
380 for (unsigned i = 0; i < ir->type->length; i++) {
381 printf("(%s ", ir->type->fields.structure[i].name);
382 value->accept(this);
383 printf(")");
384
385 value = (ir_constant *) value->next;
386 }
387 } else {
388 for (unsigned i = 0; i < ir->type->components(); i++) {
389 if (i != 0)
390 printf(" ");
391 switch (ir->type->base_type) {
392 case GLSL_TYPE_UINT: printf("%u", ir->value.u[i]); break;
393 case GLSL_TYPE_INT: printf("%d", ir->value.i[i]); break;
394 case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
395 case GLSL_TYPE_BOOL: printf("%d", ir->value.b[i]); break;
396 default: assert(0);
397 }
398 }
399 }
400 printf(")) ");
401 }
402
403
404 void
405 ir_print_visitor::visit(ir_call *ir)
406 {
407 printf("(call %s (", ir->callee_name());
408 foreach_iter(exec_list_iterator, iter, *ir) {
409 ir_instruction *const inst = (ir_instruction *) iter.get();
410
411 inst->accept(this);
412 }
413 printf("))\n");
414 }
415
416
417 void
418 ir_print_visitor::visit(ir_return *ir)
419 {
420 printf("(return");
421
422 ir_rvalue *const value = ir->get_value();
423 if (value) {
424 printf(" ");
425 value->accept(this);
426 }
427
428 printf(")");
429 }
430
431
432 void
433 ir_print_visitor::visit(ir_discard *ir)
434 {
435 printf("(discard ");
436
437 if (ir->condition != NULL) {
438 printf(" ");
439 ir->condition->accept(this);
440 }
441
442 printf(")");
443 }
444
445
446 void
447 ir_print_visitor::visit(ir_if *ir)
448 {
449 printf("(if ");
450 ir->condition->accept(this);
451
452 printf("(\n");
453 indentation++;
454
455 foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
456 ir_instruction *const inst = (ir_instruction *) iter.get();
457
458 indent();
459 inst->accept(this);
460 printf("\n");
461 }
462
463 indentation--;
464 indent();
465 printf(")\n");
466
467 indent();
468 if (!ir->else_instructions.is_empty()) {
469 printf("(\n");
470 indentation++;
471
472 foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
473 ir_instruction *const inst = (ir_instruction *) iter.get();
474
475 indent();
476 inst->accept(this);
477 printf("\n");
478 }
479 indentation--;
480 indent();
481 printf("))\n");
482 } else {
483 printf("())\n");
484 }
485 }
486
487
488 void
489 ir_print_visitor::visit(ir_loop *ir)
490 {
491 printf("(loop (");
492 if (ir->counter != NULL)
493 ir->counter->accept(this);
494 printf(") (");
495 if (ir->from != NULL)
496 ir->from->accept(this);
497 printf(") (");
498 if (ir->to != NULL)
499 ir->to->accept(this);
500 printf(") (");
501 if (ir->increment != NULL)
502 ir->increment->accept(this);
503 printf(") (\n");
504 indentation++;
505
506 foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
507 ir_instruction *const inst = (ir_instruction *) iter.get();
508
509 indent();
510 inst->accept(this);
511 printf("\n");
512 }
513 indentation--;
514 indent();
515 printf("))\n");
516 }
517
518
519 void
520 ir_print_visitor::visit(ir_loop_jump *ir)
521 {
522 printf("%s", ir->is_break() ? "break" : "continue");
523 }