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