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