glsl: Make the symbol table's add_variable just use the variable's name.
[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 "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 if (!ir->has_user_signature())
157 return;
158
159 printf("(function %s\n", ir->name);
160 indentation++;
161 foreach_iter(exec_list_iterator, iter, *ir) {
162 ir_function_signature *const sig = (ir_function_signature *) iter.get();
163
164 if (sig->is_builtin)
165 continue;
166
167 indent();
168 sig->accept(this);
169 printf("\n");
170 }
171 indentation--;
172 indent();
173 printf(")\n\n");
174 }
175
176
177 void ir_print_visitor::visit(ir_expression *ir)
178 {
179 printf("(expression ");
180
181 print_type(ir->type);
182
183 printf(" %s ", ir->operator_string());
184
185 for (unsigned i = 0; i < ir->get_num_operands(); i++) {
186 ir->operands[i]->accept(this);
187 }
188
189 printf(") ");
190 }
191
192
193 void ir_print_visitor::visit(ir_texture *ir)
194 {
195 printf("(%s ", ir->opcode_string());
196
197 ir->sampler->accept(this);
198 printf(" ");
199
200 ir->coordinate->accept(this);
201
202 printf(" (%d %d %d) ", ir->offsets[0], ir->offsets[1], ir->offsets[2]);
203
204 if (ir->op != ir_txf) {
205 if (ir->projector)
206 ir->projector->accept(this);
207 else
208 printf("1");
209
210 if (ir->shadow_comparitor) {
211 printf(" ");
212 ir->shadow_comparitor->accept(this);
213 } else {
214 printf(" ()");
215 }
216 }
217
218 printf(" ");
219 switch (ir->op)
220 {
221 case ir_tex:
222 break;
223 case ir_txb:
224 ir->lod_info.bias->accept(this);
225 break;
226 case ir_txl:
227 case ir_txf:
228 ir->lod_info.lod->accept(this);
229 break;
230 case ir_txd:
231 printf("(");
232 ir->lod_info.grad.dPdx->accept(this);
233 printf(" ");
234 ir->lod_info.grad.dPdy->accept(this);
235 printf(")");
236 break;
237 };
238 printf(")");
239 }
240
241
242 void ir_print_visitor::visit(ir_swizzle *ir)
243 {
244 const unsigned swiz[4] = {
245 ir->mask.x,
246 ir->mask.y,
247 ir->mask.z,
248 ir->mask.w,
249 };
250
251 printf("(swiz ");
252 for (unsigned i = 0; i < ir->mask.num_components; i++) {
253 printf("%c", "xyzw"[swiz[i]]);
254 }
255 printf(" ");
256 ir->val->accept(this);
257 printf(")");
258 }
259
260
261 void ir_print_visitor::visit(ir_dereference_variable *ir)
262 {
263 ir_variable *var = ir->variable_referenced();
264 printf("(var_ref %s@%p) ", var->name, (void *) var);
265 }
266
267
268 void ir_print_visitor::visit(ir_dereference_array *ir)
269 {
270 printf("(array_ref ");
271 ir->array->accept(this);
272 ir->array_index->accept(this);
273 printf(") ");
274 }
275
276
277 void ir_print_visitor::visit(ir_dereference_record *ir)
278 {
279 printf("(record_ref ");
280 ir->record->accept(this);
281 printf(" %s) ", ir->field);
282 }
283
284
285 void ir_print_visitor::visit(ir_assignment *ir)
286 {
287 printf("(assign ");
288
289 if (ir->condition)
290 ir->condition->accept(this);
291 else
292 printf("(constant bool (1))");
293
294
295 char mask[5];
296 unsigned j = 0;
297
298 for (unsigned i = 0; i < 4; i++) {
299 if ((ir->write_mask & (1 << i)) != 0) {
300 mask[j] = "xyzw"[i];
301 j++;
302 }
303 }
304 mask[j] = '\0';
305
306 printf(" (%s) ", mask);
307
308 ir->lhs->accept(this);
309
310 printf(" ");
311
312 ir->rhs->accept(this);
313 printf(") ");
314 }
315
316
317 void ir_print_visitor::visit(ir_constant *ir)
318 {
319 const glsl_type *const base_type = ir->type->get_base_type();
320
321 printf("(constant ");
322 print_type(ir->type);
323 printf(" (");
324
325 if (ir->type->is_array()) {
326 for (unsigned i = 0; i < ir->type->length; i++)
327 ir->get_array_element(i)->accept(this);
328 } else {
329 for (unsigned i = 0; i < ir->type->components(); i++) {
330 if (i != 0)
331 printf(" ");
332 switch (base_type->base_type) {
333 case GLSL_TYPE_UINT: printf("%u", ir->value.u[i]); break;
334 case GLSL_TYPE_INT: printf("%d", ir->value.i[i]); break;
335 case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
336 case GLSL_TYPE_BOOL: printf("%d", ir->value.b[i]); break;
337 default: assert(0);
338 }
339 }
340 }
341 printf(")) ");
342 }
343
344
345 void
346 ir_print_visitor::visit(ir_call *ir)
347 {
348 printf("(call %s (", ir->callee_name());
349 foreach_iter(exec_list_iterator, iter, *ir) {
350 ir_instruction *const inst = (ir_instruction *) iter.get();
351
352 inst->accept(this);
353 }
354 printf("))\n");
355 }
356
357
358 void
359 ir_print_visitor::visit(ir_return *ir)
360 {
361 printf("(return");
362
363 ir_rvalue *const value = ir->get_value();
364 if (value) {
365 printf(" ");
366 value->accept(this);
367 }
368
369 printf(")");
370 }
371
372
373 void
374 ir_print_visitor::visit(ir_discard *ir)
375 {
376 printf("(discard ");
377
378 if (ir->condition != NULL) {
379 printf(" ");
380 ir->condition->accept(this);
381 }
382
383 printf(")");
384 }
385
386
387 void
388 ir_print_visitor::visit(ir_if *ir)
389 {
390 printf("(if ");
391 ir->condition->accept(this);
392
393 printf("(\n");
394 indentation++;
395
396 foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
397 ir_instruction *const inst = (ir_instruction *) iter.get();
398
399 indent();
400 inst->accept(this);
401 printf("\n");
402 }
403
404 indentation--;
405 indent();
406 printf(")\n");
407
408 indent();
409 if (!ir->else_instructions.is_empty()) {
410 printf("(\n");
411 indentation++;
412
413 foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
414 ir_instruction *const inst = (ir_instruction *) iter.get();
415
416 indent();
417 inst->accept(this);
418 printf("\n");
419 }
420 indentation--;
421 indent();
422 printf("))\n");
423 } else {
424 printf("())\n");
425 }
426 }
427
428
429 void
430 ir_print_visitor::visit(ir_loop *ir)
431 {
432 printf("(loop (");
433 if (ir->counter != NULL)
434 ir->counter->accept(this);
435 printf(") (");
436 if (ir->from != NULL)
437 ir->from->accept(this);
438 printf(") (");
439 if (ir->to != NULL)
440 ir->to->accept(this);
441 printf(") (");
442 if (ir->increment != NULL)
443 ir->increment->accept(this);
444 printf(") (\n");
445 indentation++;
446
447 foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
448 ir_instruction *const inst = (ir_instruction *) iter.get();
449
450 indent();
451 inst->accept(this);
452 printf("\n");
453 }
454 indentation--;
455 indent();
456 printf("))\n");
457 }
458
459
460 void
461 ir_print_visitor::visit(ir_loop_jump *ir)
462 {
463 printf("%s", ir->is_break() ? "break" : "continue");
464 }