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