8b2080f7ec520c266fb04ab73afed8c606eb135e
[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
27 static void
28 print_type(const glsl_type *t)
29 {
30 if (t->base_type == GLSL_TYPE_ARRAY) {
31 printf("array (");
32 print_type(t->fields.array);
33 printf(") (%u))", t->length);
34 } else if (t->base_type == GLSL_TYPE_STRUCT) {
35 printf("struct (%s %u ", t->name ? t->name : "@", t->length);
36 printf("(FINISHME: structure fields go here) ");
37 printf(")");
38 } else {
39 printf("%s", t->name);
40 }
41 }
42
43
44 void ir_print_visitor::visit(ir_variable *ir)
45 {
46 if (deref_depth) {
47 printf("(%s)", ir->name);
48 } else {
49 printf("(declare ");
50
51 const char *const cent = (ir->centroid) ? "centroid " : "";
52 const char *const inv = (ir->invariant) ? "invariant " : "";
53 const char *const mode[] = { "", "uniform ", "in ", "out ", "inout " };
54 const char *const interp[] = { "", "flat", "noperspective" };
55
56 printf("(%s%s%s%s) ",
57 cent, inv, mode[ir->mode], interp[ir->interpolation]);
58
59 printf("(");
60 print_type(ir->type);
61 printf(") ");
62 printf("(%s)) ", ir->name);
63 }
64 }
65
66
67 void ir_print_visitor::visit(ir_label *ir)
68 {
69 printf("\n(label %s)", ir->label);
70 }
71
72
73 void ir_print_visitor::visit(ir_function_signature *ir)
74 {
75 printf("%s:%d:\n", __func__, __LINE__);
76 (void) ir;
77 }
78
79
80 void ir_print_visitor::visit(ir_function *ir)
81 {
82 printf("(function %s\n", ir->name);
83 printf(")\n");
84 }
85
86
87 void ir_print_visitor::visit(ir_expression *ir)
88 {
89 static const char *const operators[] = {
90 "~",
91 "!",
92 "-",
93 "abs",
94 "rcp",
95 "rsq",
96 "sqrt",
97 "exp",
98 "log",
99 "f2i",
100 "i2f",
101 "u2f",
102 "trunc",
103 "ceil",
104 "floor",
105 "+",
106 "-",
107 "*",
108 "/",
109 "%",
110 "<",
111 ">",
112 "<=",
113 ">=",
114 "==",
115 "!=",
116 "<<",
117 ">>",
118 "&",
119 "^",
120 "|",
121 "&&",
122 "^^",
123 "||",
124 "!",
125 "dot",
126 "min",
127 "max",
128 "pow",
129 };
130
131 printf("(expression ");
132
133 assert((unsigned int)ir->operation <
134 sizeof(operators) / sizeof(operators[0]));
135
136 printf("%s", operators[ir->operation]);
137 printf("(");
138 if (ir->operands[0])
139 ir->operands[0]->accept(this);
140 printf(") ");
141
142 printf("(");
143 if (ir->operands[1])
144 ir->operands[1]->accept(this);
145 printf(")) ");
146 }
147
148
149 void ir_print_visitor::visit(ir_swizzle *ir)
150 {
151 const unsigned swiz[4] = {
152 ir->mask.x,
153 ir->mask.y,
154 ir->mask.z,
155 ir->mask.w,
156 };
157
158 printf("(swiz ");
159 for (unsigned i = 0; i < ir->mask.num_components; i++) {
160 printf("%c", "xyzw"[swiz[i]]);
161 }
162 printf(" ");
163 ir->val->accept(this);
164 printf(")");
165 }
166
167
168 void ir_print_visitor::visit(ir_dereference *ir)
169 {
170 deref_depth++;
171
172 switch (ir->mode) {
173 case ir_dereference::ir_reference_variable: {
174 printf("(var_ref ");
175 ir->var->accept(this);
176 printf(") ");
177 break;
178 }
179 case ir_dereference::ir_reference_array:
180 printf("(array_ref ");
181 ir->var->accept(this);
182 ir->selector.array_index->accept(this);
183 printf(") ");
184 break;
185 case ir_dereference::ir_reference_record:
186 printf("(record_ref ");
187 ir->var->accept(this);
188 printf("(%s)) ", ir->selector.field);
189 break;
190 }
191
192 deref_depth--;
193 }
194
195
196 void ir_print_visitor::visit(ir_assignment *ir)
197 {
198 printf("(assign (");
199
200 if (ir->condition)
201 ir->condition->accept(this);
202 else
203 printf("true");
204
205 printf(") (");
206
207 ir->lhs->accept(this);
208
209 printf(") (");
210
211 ir->rhs->accept(this);
212 printf(") ");
213 }
214
215
216 void ir_print_visitor::visit(ir_constant *ir)
217 {
218 const glsl_type *const base_type = ir->type->get_base_type();
219
220 printf("(constant (");
221 print_type(base_type);
222 printf(") ");
223
224 printf("(%d) (", ir->type->components());
225 for (unsigned i = 0; i < ir->type->components(); i++) {
226 if (i != 0)
227 printf(", ");
228
229 switch (base_type->base_type) {
230 case GLSL_TYPE_UINT: printf("%u", ir->value.u[i]); break;
231 case GLSL_TYPE_INT: printf("%d", ir->value.i[i]); break;
232 case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
233 case GLSL_TYPE_BOOL: printf("%d", ir->value.b[i]); break;
234 default: assert(0);
235 }
236 }
237 printf(")) ");
238 }
239
240
241 void
242 ir_print_visitor::visit(ir_call *ir)
243 {
244 printf("(call (%s) ", ir->callee_name());
245 foreach_iter(exec_list_iterator, iter, *ir) {
246 ir_instruction *const inst = (ir_instruction *) iter.get();
247
248 inst->accept(this);
249 }
250 }
251
252
253 void
254 ir_print_visitor::visit(ir_return *ir)
255 {
256 printf("(return");
257
258 ir_rvalue *const value = ir->get_value();
259 if (value) {
260 printf(" ");
261 value->accept(this);
262 }
263
264 printf(")");
265 }