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