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