OSDN Git Service

builtins: Add the mix(gentype, gentype, float) variant.
[android-x86/external-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 #include <cstdio>
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_swizzle *ir)
147 {
148    const unsigned swiz[4] = {
149       ir->mask.x,
150       ir->mask.y,
151       ir->mask.z,
152       ir->mask.w,
153    };
154
155    printf("(swiz ");
156    for (unsigned i = 0; i < ir->mask.num_components; i++) {
157       printf("%c", "xyzw"[swiz[i]]);
158    }
159    printf(" ");
160    ir->val->accept(this);
161    printf(")");
162 }
163
164
165 void ir_print_visitor::visit(ir_dereference_variable *ir)
166 {
167    printf("(var_ref %s) ", ir->variable_referenced()->name);
168 }
169
170
171 void ir_print_visitor::visit(ir_dereference_array *ir)
172 {
173    printf("(array_ref ");
174    ir->array->accept(this);
175    ir->array_index->accept(this);
176    printf(") ");
177 }
178
179
180 void ir_print_visitor::visit(ir_dereference_record *ir)
181 {
182    printf("(record_ref ");
183    ir->record->accept(this);
184    printf(" %s) ", ir->field);
185 }
186
187
188 void ir_print_visitor::visit(ir_assignment *ir)
189 {
190    printf("(assign ");
191
192    if (ir->condition)
193       ir->condition->accept(this);
194    else
195       printf("(constant bool (1))");
196
197    printf(" ");
198
199    ir->lhs->accept(this);
200
201    printf(" ");
202
203    ir->rhs->accept(this);
204    printf(") ");
205 }
206
207
208 void ir_print_visitor::visit(ir_constant *ir)
209 {
210    const glsl_type *const base_type = ir->type->get_base_type();
211
212    printf("(constant ");
213    print_type(ir->type);
214    printf(" (");
215
216    for (unsigned i = 0; i < ir->type->components(); i++) {
217       if (i != 0)
218          printf(", ");
219
220       switch (base_type->base_type) {
221       case GLSL_TYPE_UINT:  printf("%u", ir->value.u[i]); break;
222       case GLSL_TYPE_INT:   printf("%d", ir->value.i[i]); break;
223       case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
224       case GLSL_TYPE_BOOL:  printf("%d", ir->value.b[i]); break;
225       default: assert(0);
226       }
227    }
228    printf(")) ");
229 }
230
231
232 void
233 ir_print_visitor::visit(ir_call *ir)
234 {
235    printf("(call %s (", ir->callee_name());
236    foreach_iter(exec_list_iterator, iter, *ir) {
237       ir_instruction *const inst = (ir_instruction *) iter.get();
238
239       inst->accept(this);
240    }
241    printf("))\n");
242 }
243
244
245 void
246 ir_print_visitor::visit(ir_return *ir)
247 {
248    printf("(return");
249
250    ir_rvalue *const value = ir->get_value();
251    if (value) {
252       printf(" ");
253       value->accept(this);
254    }
255
256    printf(")");
257 }
258
259
260 void
261 ir_print_visitor::visit(ir_if *ir)
262 {
263    printf("(if ");
264    ir->condition->accept(this);
265
266    printf("(\n");
267    foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
268       ir_instruction *const inst = (ir_instruction *) iter.get();
269
270       inst->accept(this);
271       printf("\n");
272    }
273    printf(")\n");
274
275    printf("(\n");
276    foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
277       ir_instruction *const inst = (ir_instruction *) iter.get();
278
279       inst->accept(this);
280       printf("\n");
281    }
282    printf("))\n");
283 }
284
285
286 void
287 ir_print_visitor::visit(ir_loop *ir)
288 {
289    printf("(loop (");
290    if (ir->counter != NULL)
291       ir->counter->accept(this);
292    printf(") (");
293    if (ir->from != NULL)
294       ir->from->accept(this);
295    printf(") (");
296    if (ir->to != NULL)
297       ir->to->accept(this);
298    printf(") (");
299    if (ir->increment != NULL)
300       ir->increment->accept(this);
301    printf(") (\n");
302    foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
303       ir_instruction *const inst = (ir_instruction *) iter.get();
304
305       inst->accept(this);
306       printf("\n");
307    }
308    printf("))\n");
309 }
310
311
312 void
313 ir_print_visitor::visit(ir_loop_jump *ir)
314 {
315    printf("%s", ir->is_break() ? "break" : "continue");
316 }