Merge branch 'glsl2-head' into glsl2
[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 for (unsigned i = 0; i < state->num_user_structures; i++) {
44 const glsl_type *const s = state->user_structures[i];
45
46 printf("(structure (%s) (%s@%p) (%u) (\n",
47 s->name, s->name, s, s->length);
48
49 for (unsigned j = 0; j < s->length; j++) {
50 printf("\t((");
51 print_type(s->fields.structure[j].type);
52 printf(")(%s))\n", s->fields.structure[j].name);
53 }
54
55 printf(")\n");
56 }
57
58 printf("(\n");
59 foreach_iter(exec_list_iterator, iter, *instructions) {
60 ((ir_instruction *)iter.get())->print();
61 printf("\n");
62 }
63 printf("\n)");
64 }
65
66 static void
67 print_type(const glsl_type *t)
68 {
69 if (t->base_type == GLSL_TYPE_ARRAY) {
70 printf("(array ");
71 print_type(t->fields.array);
72 printf(" %u)", t->length);
73 } else if ((t->base_type == GLSL_TYPE_STRUCT)
74 && (strncmp("gl_", t->name, 3) != 0)) {
75 printf("%s@%p", t->name, t);
76 } else {
77 printf("%s", t->name);
78 }
79 }
80
81
82 void ir_print_visitor::visit(ir_variable *ir)
83 {
84 printf("(declare ");
85
86 const char *const cent = (ir->centroid) ? "centroid " : "";
87 const char *const inv = (ir->invariant) ? "invariant " : "";
88 const char *const mode[] = { "", "uniform ", "in ", "out ", "inout " };
89 const char *const interp[] = { "", "flat", "noperspective" };
90
91 printf("(%s%s%s%s) ",
92 cent, inv, mode[ir->mode], interp[ir->interpolation]);
93
94 print_type(ir->type);
95 printf(" %s@%p)", ir->name, ir);
96 }
97
98
99 void ir_print_visitor::visit(ir_function_signature *ir)
100 {
101 printf("(signature ");
102 print_type(ir->return_type);
103 printf("\n (parameters\n");
104 foreach_iter(exec_list_iterator, iter, ir->parameters) {
105 ir_variable *const inst = (ir_variable *) iter.get();
106
107 inst->accept(this);
108 printf("\n");
109 }
110 printf(" )\n(");
111
112 foreach_iter(exec_list_iterator, iter, ir->body) {
113 ir_instruction *const inst = (ir_instruction *) iter.get();
114
115 inst->accept(this);
116 printf("\n");
117 }
118 printf("))\n");
119 }
120
121
122 void ir_print_visitor::visit(ir_function *ir)
123 {
124 printf("(function %s\n", ir->name);
125 foreach_iter(exec_list_iterator, iter, *ir) {
126 ir_function_signature *const sig = (ir_function_signature *) iter.get();
127
128 sig->accept(this);
129 printf("\n");
130 }
131
132 printf(")\n");
133 }
134
135
136 void ir_print_visitor::visit(ir_expression *ir)
137 {
138 printf("(expression ");
139
140 print_type(ir->type);
141
142 printf(" %s ", ir->operator_string());
143
144 if (ir->operands[0])
145 ir->operands[0]->accept(this);
146
147 if (ir->operands[1])
148 ir->operands[1]->accept(this);
149 printf(") ");
150 }
151
152
153 void ir_print_visitor::visit(ir_texture *ir)
154 {
155 printf("(%s ", ir->opcode_string());
156
157 ir->sampler->accept(this);
158 printf(" ");
159
160 ir->coordinate->accept(this);
161
162 printf(" (%d %d %d) ", ir->offsets[0], ir->offsets[1], ir->offsets[2]);
163
164 if (ir->op != ir_txf) {
165 if (ir->projector)
166 ir->projector->accept(this);
167 else
168 printf("1");
169
170 if (ir->shadow_comparitor) {
171 printf(" ");
172 ir->shadow_comparitor->accept(this);
173 } else {
174 printf(" ()");
175 }
176 }
177
178 printf(" ");
179 switch (ir->op)
180 {
181 case ir_tex:
182 break;
183 case ir_txb:
184 ir->lod_info.bias->accept(this);
185 break;
186 case ir_txl:
187 case ir_txf:
188 ir->lod_info.lod->accept(this);
189 break;
190 case ir_txd:
191 printf("(");
192 ir->lod_info.grad.dPdx->accept(this);
193 printf(" ");
194 ir->lod_info.grad.dPdy->accept(this);
195 printf(")");
196 break;
197 };
198 printf(")");
199 }
200
201
202 void ir_print_visitor::visit(ir_swizzle *ir)
203 {
204 const unsigned swiz[4] = {
205 ir->mask.x,
206 ir->mask.y,
207 ir->mask.z,
208 ir->mask.w,
209 };
210
211 printf("(swiz ");
212 for (unsigned i = 0; i < ir->mask.num_components; i++) {
213 printf("%c", "xyzw"[swiz[i]]);
214 }
215 printf(" ");
216 ir->val->accept(this);
217 printf(")");
218 }
219
220
221 void ir_print_visitor::visit(ir_dereference_variable *ir)
222 {
223 ir_variable *var = ir->variable_referenced();
224 printf("(var_ref %s@%p) ", var->name, var);
225 }
226
227
228 void ir_print_visitor::visit(ir_dereference_array *ir)
229 {
230 printf("(array_ref ");
231 ir->array->accept(this);
232 ir->array_index->accept(this);
233 printf(") ");
234 }
235
236
237 void ir_print_visitor::visit(ir_dereference_record *ir)
238 {
239 printf("(record_ref ");
240 ir->record->accept(this);
241 printf(" %s) ", ir->field);
242 }
243
244
245 void ir_print_visitor::visit(ir_assignment *ir)
246 {
247 printf("(assign ");
248
249 if (ir->condition)
250 ir->condition->accept(this);
251 else
252 printf("(constant bool (1))");
253
254 printf(" ");
255
256 ir->lhs->accept(this);
257
258 printf(" ");
259
260 ir->rhs->accept(this);
261 printf(") ");
262 }
263
264
265 void ir_print_visitor::visit(ir_constant *ir)
266 {
267 const glsl_type *const base_type = ir->type->get_base_type();
268
269 printf("(constant ");
270 print_type(ir->type);
271 printf(" (");
272
273 for (unsigned i = 0; i < ir->type->components(); i++) {
274 if (i != 0)
275 printf(", ");
276
277 switch (base_type->base_type) {
278 case GLSL_TYPE_UINT: printf("%u", ir->value.u[i]); break;
279 case GLSL_TYPE_INT: printf("%d", ir->value.i[i]); break;
280 case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
281 case GLSL_TYPE_BOOL: printf("%d", ir->value.b[i]); break;
282 default: assert(0);
283 }
284 }
285 printf(")) ");
286 }
287
288
289 void
290 ir_print_visitor::visit(ir_call *ir)
291 {
292 printf("(call %s (", ir->callee_name());
293 foreach_iter(exec_list_iterator, iter, *ir) {
294 ir_instruction *const inst = (ir_instruction *) iter.get();
295
296 inst->accept(this);
297 }
298 printf("))\n");
299 }
300
301
302 void
303 ir_print_visitor::visit(ir_return *ir)
304 {
305 printf("(return");
306
307 ir_rvalue *const value = ir->get_value();
308 if (value) {
309 printf(" ");
310 value->accept(this);
311 }
312
313 printf(")");
314 }
315
316
317 void
318 ir_print_visitor::visit(ir_if *ir)
319 {
320 printf("(if ");
321 ir->condition->accept(this);
322
323 printf("(\n");
324 foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
325 ir_instruction *const inst = (ir_instruction *) iter.get();
326
327 inst->accept(this);
328 printf("\n");
329 }
330 printf(")\n");
331
332 printf("(\n");
333 foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
334 ir_instruction *const inst = (ir_instruction *) iter.get();
335
336 inst->accept(this);
337 printf("\n");
338 }
339 printf("))\n");
340 }
341
342
343 void
344 ir_print_visitor::visit(ir_loop *ir)
345 {
346 printf("(loop (");
347 if (ir->counter != NULL)
348 ir->counter->accept(this);
349 printf(") (");
350 if (ir->from != NULL)
351 ir->from->accept(this);
352 printf(") (");
353 if (ir->to != NULL)
354 ir->to->accept(this);
355 printf(") (");
356 if (ir->increment != NULL)
357 ir->increment->accept(this);
358 printf(") (\n");
359 foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
360 ir_instruction *const inst = (ir_instruction *) iter.get();
361
362 inst->accept(this);
363 printf("\n");
364 }
365 printf("))\n");
366 }
367
368
369 void
370 ir_print_visitor::visit(ir_loop_jump *ir)
371 {
372 printf("%s", ir->is_break() ? "break" : "continue");
373 }