blob: 6d6ee093d791a3fd683e471dabaea5d6758a489b [file] [log] [blame]
Ian Romanick1cf43a42010-03-30 16:56:50 -07001/*
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/**
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
27 *
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
30 *
31 * * Sizes of arrays
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
34 */
35
Eric Anholt43ad37a2010-05-12 14:42:21 -070036#include <math.h>
Ian Romanick1cf43a42010-03-30 16:56:50 -070037#include "ir.h"
38#include "ir_visitor.h"
Eric Anholta576f9d2010-03-31 16:25:12 -100039#include "glsl_types.h"
Ian Romanick1cf43a42010-03-30 16:56:50 -070040
41/**
42 * Visitor class for evaluating constant expressions
43 */
44class ir_constant_visitor : public ir_visitor {
45public:
46 ir_constant_visitor()
47 : value(NULL)
48 {
49 /* empty */
50 }
51
52 virtual ~ir_constant_visitor()
53 {
54 /* empty */
55 }
56
57 /**
58 * \name Visit methods
59 *
60 * As typical for the visitor pattern, there must be one \c visit method for
61 * each concrete subclass of \c ir_instruction. Virtual base classes within
62 * the hierarchy should not have \c visit methods.
63 */
64 /*@{*/
65 virtual void visit(ir_variable *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070066 virtual void visit(ir_function_signature *);
67 virtual void visit(ir_function *);
68 virtual void visit(ir_expression *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -070069 virtual void visit(ir_texture *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070070 virtual void visit(ir_swizzle *);
Ian Romanickc7b10462010-05-19 13:20:12 +020071 virtual void visit(ir_dereference_variable *);
72 virtual void visit(ir_dereference_array *);
73 virtual void visit(ir_dereference_record *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070074 virtual void visit(ir_assignment *);
75 virtual void visit(ir_constant *);
76 virtual void visit(ir_call *);
77 virtual void visit(ir_return *);
Kenneth Graunke16efab12010-06-30 10:47:34 -070078 virtual void visit(ir_discard *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070079 virtual void visit(ir_if *);
Ian Romanickfad607a2010-04-05 16:16:07 -070080 virtual void visit(ir_loop *);
Ian Romanickf8e31e02010-04-05 16:28:15 -070081 virtual void visit(ir_loop_jump *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070082 /*@}*/
83
84 /**
85 * Value of the constant expression.
86 *
87 * \note
88 * This field will be \c NULL if the expression is not constant valued.
89 */
90 /* FINIHSME: This cannot hold values for constant arrays or structures. */
91 ir_constant *value;
92};
93
94
95ir_constant *
96ir_instruction::constant_expression_value()
97{
98 ir_constant_visitor visitor;
99
100 this->accept(& visitor);
101 return visitor.value;
102}
103
104
105void
106ir_constant_visitor::visit(ir_variable *ir)
107{
108 (void) ir;
109 value = NULL;
110}
111
112
113void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700114ir_constant_visitor::visit(ir_function_signature *ir)
115{
116 (void) ir;
117 value = NULL;
118}
119
120
121void
122ir_constant_visitor::visit(ir_function *ir)
123{
124 (void) ir;
125 value = NULL;
126}
127
Ian Romanick1cf43a42010-03-30 16:56:50 -0700128void
129ir_constant_visitor::visit(ir_expression *ir)
130{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700131 value = NULL;
Kenneth Graunke6bc432e2010-07-02 17:12:23 -0700132 ir_constant *op[2] = { NULL, NULL };
Eric Anholtd98da972010-04-01 18:25:11 -1000133 unsigned int operand, c;
Ian Romanick4daaab62010-06-11 16:08:47 -0700134 ir_constant_data data;
Eric Anholt160d0922010-04-01 18:07:08 -1000135
Kenneth Graunkec63a1db2010-07-05 22:33:35 -0700136 memset(&data, 0, sizeof(data));
137
Eric Anholtd98da972010-04-01 18:25:11 -1000138 for (operand = 0; operand < ir->get_num_operands(); operand++) {
139 op[operand] = ir->operands[operand]->constant_expression_value();
140 if (!op[operand])
Eric Anholt160d0922010-04-01 18:07:08 -1000141 return;
142 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000143
144 switch (ir->operation) {
Eric Anholt528bb852010-03-31 21:09:02 -1000145 case ir_unop_logic_not:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700146 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholtd98da972010-04-01 18:25:11 -1000147 for (c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700148 data.b[c] = !op[0]->value.b[c];
Eric Anholt528bb852010-03-31 21:09:02 -1000149 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700150
151 case ir_unop_f2i:
152 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholtaf186412010-04-06 10:53:57 -0700153 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700154 data.i[c] = op[0]->value.f[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700155 }
156 break;
157 case ir_unop_i2f:
158 assert(op[0]->type->base_type == GLSL_TYPE_UINT ||
159 op[0]->type->base_type == GLSL_TYPE_INT);
Eric Anholtaf186412010-04-06 10:53:57 -0700160 for (c = 0; c < ir->operands[0]->type->components(); c++) {
161 if (op[0]->type->base_type == GLSL_TYPE_INT)
Ian Romanick4daaab62010-06-11 16:08:47 -0700162 data.f[c] = op[0]->value.i[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700163 else
Ian Romanick4daaab62010-06-11 16:08:47 -0700164 data.f[c] = op[0]->value.u[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700165 }
166 break;
Ian Romanick7dc2b712010-06-07 15:10:14 -0700167 case ir_unop_b2f:
168 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Ian Romanick7dc2b712010-06-07 15:10:14 -0700169 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700170 data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0;
Ian Romanick7dc2b712010-06-07 15:10:14 -0700171 }
172 break;
173 case ir_unop_f2b:
174 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Ian Romanick7dc2b712010-06-07 15:10:14 -0700175 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700176 data.b[c] = bool(op[0]->value.f[c]);
Ian Romanick7dc2b712010-06-07 15:10:14 -0700177 }
178 break;
Ian Romanick39d6dd32010-06-11 13:46:30 -0700179 case ir_unop_b2i:
180 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Ian Romanick39d6dd32010-06-11 13:46:30 -0700181 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700182 data.u[c] = op[0]->value.b[c] ? 1 : 0;
Ian Romanick39d6dd32010-06-11 13:46:30 -0700183 }
184 break;
185 case ir_unop_i2b:
186 assert(op[0]->type->is_integer());
Ian Romanick39d6dd32010-06-11 13:46:30 -0700187 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700188 data.b[c] = bool(op[0]->value.u[c]);
Ian Romanick39d6dd32010-06-11 13:46:30 -0700189 }
190 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700191
Eric Anholtd925c912010-07-01 10:37:11 -0700192 case ir_unop_fract:
193 for (c = 0; c < ir->operands[0]->type->components(); c++) {
194 switch (ir->type->base_type) {
195 case GLSL_TYPE_UINT:
196 data.u[c] = 0;
197 break;
198 case GLSL_TYPE_INT:
199 data.i[c] = 0;
200 break;
201 case GLSL_TYPE_FLOAT:
202 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
203 break;
204 default:
205 assert(0);
206 }
207 }
208 break;
209
Eric Anholt43ad37a2010-05-12 14:42:21 -0700210 case ir_unop_neg:
Eric Anholt43ad37a2010-05-12 14:42:21 -0700211 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700212 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700213 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700214 data.u[c] = -op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700215 break;
216 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700217 data.i[c] = -op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700218 break;
219 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700220 data.f[c] = -op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700221 break;
222 default:
223 assert(0);
224 }
225 }
226 break;
227
228 case ir_unop_abs:
229 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700230 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700231 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700232 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700233 data.u[c] = op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700234 break;
235 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700236 data.i[c] = op[0]->value.i[c];
237 if (data.i[c] < 0)
238 data.i[c] = -data.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700239 break;
240 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700241 data.f[c] = fabs(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700242 break;
243 default:
244 assert(0);
245 }
246 }
247 break;
248
249 case ir_unop_rcp:
250 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700251 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700252 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700253 case GLSL_TYPE_UINT:
254 if (op[0]->value.u[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700255 data.u[c] = 1 / op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700256 break;
257 case GLSL_TYPE_INT:
258 if (op[0]->value.i[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700259 data.i[c] = 1 / op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700260 break;
261 case GLSL_TYPE_FLOAT:
262 if (op[0]->value.f[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700263 data.f[c] = 1.0 / op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700264 break;
265 default:
266 assert(0);
267 }
268 }
269 break;
270
271 case ir_unop_rsq:
272 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700273 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700274 data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700275 }
276 break;
277
278 case ir_unop_sqrt:
279 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700280 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700281 data.f[c] = sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700282 }
283 break;
284
285 case ir_unop_exp:
286 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700287 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700288 data.f[c] = expf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700289 }
290 break;
291
292 case ir_unop_log:
293 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700294 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700295 data.f[c] = logf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700296 }
297 break;
298
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700299 case ir_unop_dFdx:
300 case ir_unop_dFdy:
301 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700302 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700303 data.f[c] = 0.0;
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700304 }
305 break;
306
Eric Anholtd251b922010-04-01 18:35:42 -1000307 case ir_binop_add:
308 if (ir->operands[0]->type == ir->operands[1]->type) {
Eric Anholtd251b922010-04-01 18:35:42 -1000309 for (c = 0; c < ir->operands[0]->type->components(); c++) {
310 switch (ir->operands[0]->type->base_type) {
311 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700312 data.u[c] = op[0]->value.u[c] + op[1]->value.u[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000313 break;
314 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700315 data.i[c] = op[0]->value.i[c] + op[1]->value.i[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000316 break;
317 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700318 data.f[c] = op[0]->value.f[c] + op[1]->value.f[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000319 break;
320 default:
321 assert(0);
322 }
323 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700324 } else
325 /* FINISHME: Support operations with non-equal types. */
326 return;
327
Eric Anholtd251b922010-04-01 18:35:42 -1000328 break;
329 case ir_binop_sub:
330 if (ir->operands[0]->type == ir->operands[1]->type) {
Eric Anholtd251b922010-04-01 18:35:42 -1000331 for (c = 0; c < ir->operands[0]->type->components(); c++) {
332 switch (ir->operands[0]->type->base_type) {
333 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700334 data.u[c] = op[0]->value.u[c] - op[1]->value.u[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000335 break;
336 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700337 data.i[c] = op[0]->value.i[c] - op[1]->value.i[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000338 break;
339 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700340 data.f[c] = op[0]->value.f[c] - op[1]->value.f[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000341 break;
342 default:
343 assert(0);
344 }
345 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700346 } else
347 /* FINISHME: Support operations with non-equal types. */
348 return;
349
Eric Anholtd251b922010-04-01 18:35:42 -1000350 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000351 case ir_binop_mul:
Eric Anholtd98da972010-04-01 18:25:11 -1000352 if (ir->operands[0]->type == ir->operands[1]->type &&
353 !ir->operands[0]->type->is_matrix()) {
Eric Anholtd98da972010-04-01 18:25:11 -1000354 for (c = 0; c < ir->operands[0]->type->components(); c++) {
355 switch (ir->operands[0]->type->base_type) {
356 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700357 data.u[c] = op[0]->value.u[c] * op[1]->value.u[c];
Eric Anholtd98da972010-04-01 18:25:11 -1000358 break;
359 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700360 data.i[c] = op[0]->value.i[c] * op[1]->value.i[c];
Eric Anholtd98da972010-04-01 18:25:11 -1000361 break;
362 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700363 data.f[c] = op[0]->value.f[c] * op[1]->value.f[c];
Eric Anholtd98da972010-04-01 18:25:11 -1000364 break;
365 default:
366 assert(0);
367 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000368 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700369 } else
370 /* FINISHME: Support operations with non-equal types. */
371 return;
372
Eric Anholta576f9d2010-03-31 16:25:12 -1000373 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000374 case ir_binop_div:
375 if (ir->operands[0]->type == ir->operands[1]->type) {
Eric Anholtd251b922010-04-01 18:35:42 -1000376 for (c = 0; c < ir->operands[0]->type->components(); c++) {
377 switch (ir->operands[0]->type->base_type) {
378 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700379 data.u[c] = op[0]->value.u[c] / op[1]->value.u[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000380 break;
381 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700382 data.i[c] = op[0]->value.i[c] / op[1]->value.i[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000383 break;
384 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700385 data.f[c] = op[0]->value.f[c] / op[1]->value.f[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000386 break;
387 default:
388 assert(0);
389 }
390 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700391 } else
392 /* FINISHME: Support operations with non-equal types. */
393 return;
394
Eric Anholtd251b922010-04-01 18:35:42 -1000395 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000396 case ir_binop_logic_and:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700397 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholtd98da972010-04-01 18:25:11 -1000398 for (c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700399 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000400 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000401 case ir_binop_logic_xor:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700402 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholtd251b922010-04-01 18:35:42 -1000403 for (c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700404 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000405 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000406 case ir_binop_logic_or:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700407 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholtd98da972010-04-01 18:25:11 -1000408 for (c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700409 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000410 break;
Eric Anholt85171c22010-04-06 09:55:45 -0700411
412 case ir_binop_less:
Eric Anholt85171c22010-04-06 09:55:45 -0700413 switch (ir->operands[0]->type->base_type) {
414 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700415 data.b[0] = op[0]->value.u[0] < op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700416 break;
417 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700418 data.b[0] = op[0]->value.i[0] < op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700419 break;
420 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700421 data.b[0] = op[0]->value.f[0] < op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700422 break;
423 default:
424 assert(0);
425 }
426 break;
427 case ir_binop_greater:
Eric Anholt85171c22010-04-06 09:55:45 -0700428 switch (ir->operands[0]->type->base_type) {
429 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700430 data.b[0] = op[0]->value.u[0] > op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700431 break;
432 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700433 data.b[0] = op[0]->value.i[0] > op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700434 break;
435 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700436 data.b[0] = op[0]->value.f[0] > op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700437 break;
438 default:
439 assert(0);
440 }
441 break;
442 case ir_binop_lequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700443 switch (ir->operands[0]->type->base_type) {
444 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700445 data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700446 break;
447 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700448 data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700449 break;
450 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700451 data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700452 break;
453 default:
454 assert(0);
455 }
456 break;
457 case ir_binop_gequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700458 switch (ir->operands[0]->type->base_type) {
459 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700460 data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700461 break;
462 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700463 data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700464 break;
465 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700466 data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700467 break;
468 default:
469 assert(0);
470 }
471 break;
472
Eric Anholtec1949e2010-04-06 10:02:27 -0700473 case ir_binop_equal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700474 data.b[0] = true;
475 for (c = 0; c < ir->operands[0]->type->components(); c++) {
476 switch (ir->operands[0]->type->base_type) {
477 case GLSL_TYPE_UINT:
478 data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c];
479 break;
480 case GLSL_TYPE_INT:
481 data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c];
482 break;
483 case GLSL_TYPE_FLOAT:
484 data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c];
485 break;
486 case GLSL_TYPE_BOOL:
487 data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c];
488 break;
489 default:
490 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700491 }
492 }
493 break;
494 case ir_binop_nequal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700495 data.b[0] = false;
496 for (c = 0; c < ir->operands[0]->type->components(); c++) {
497 switch (ir->operands[0]->type->base_type) {
498 case GLSL_TYPE_UINT:
499 data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c];
500 break;
501 case GLSL_TYPE_INT:
502 data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c];
503 break;
504 case GLSL_TYPE_FLOAT:
505 data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c];
506 break;
507 case GLSL_TYPE_BOOL:
508 data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c];
509 break;
510 default:
511 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700512 }
513 }
514 break;
515
Eric Anholta576f9d2010-03-31 16:25:12 -1000516 default:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700517 /* FINISHME: Should handle all expression types. */
518 return;
Eric Anholta576f9d2010-03-31 16:25:12 -1000519 }
Eric Anholtd98da972010-04-01 18:25:11 -1000520
Eric Anholt6b01b502010-06-24 15:18:39 -0700521 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700522 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanick1cf43a42010-03-30 16:56:50 -0700523}
524
525
526void
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700527ir_constant_visitor::visit(ir_texture *ir)
528{
529 // FINISHME: Do stuff with texture lookups
530 (void) ir;
531 value = NULL;
532}
533
534
535void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700536ir_constant_visitor::visit(ir_swizzle *ir)
537{
Ian Romanickc2ba6192010-06-11 12:30:28 -0700538 ir_constant *v = ir->val->constant_expression_value();
539
540 this->value = NULL;
541
542 if (v != NULL) {
Ian Romanick0bb70a32010-06-11 15:49:49 -0700543 ir_constant_data data;
Ian Romanickc2ba6192010-06-11 12:30:28 -0700544
545 const unsigned swiz_idx[4] = {
546 ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w
547 };
548
549 for (unsigned i = 0; i < ir->mask.num_components; i++) {
550 switch (v->type->base_type) {
551 case GLSL_TYPE_UINT:
552 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
553 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
554 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
555 default: assert(!"Should not get here."); break;
556 }
557 }
558
Eric Anholt6b01b502010-06-24 15:18:39 -0700559 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700560 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanickc2ba6192010-06-11 12:30:28 -0700561 }
Ian Romanick1cf43a42010-03-30 16:56:50 -0700562}
563
564
565void
Ian Romanickc7b10462010-05-19 13:20:12 +0200566ir_constant_visitor::visit(ir_dereference_variable *ir)
Ian Romanick1cf43a42010-03-30 16:56:50 -0700567{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700568 value = NULL;
Eric Anholt326c6762010-04-06 10:30:54 -0700569
Ian Romanickc7b10462010-05-19 13:20:12 +0200570 ir_variable *var = ir->variable_referenced();
571 if (var && var->constant_value)
Eric Anholt4b6fd392010-06-23 11:37:12 -0700572 value = (ir_constant *)var->constant_value->clone(NULL);
Ian Romanickc7b10462010-05-19 13:20:12 +0200573}
574
575
576void
577ir_constant_visitor::visit(ir_dereference_array *ir)
578{
Carl Worth1660a292010-06-23 18:11:51 -0700579 void *ctx = talloc_parent(ir);
Ian Romanick9b92af92010-06-11 12:20:12 -0700580 ir_constant *array = ir->array->constant_expression_value();
581 ir_constant *idx = ir->array_index->constant_expression_value();
582
583 this->value = NULL;
584
585 if ((array != NULL) && (idx != NULL)) {
586 if (array->type->is_matrix()) {
587 /* Array access of a matrix results in a vector.
588 */
589 const unsigned column = idx->value.u[0];
590
591 const glsl_type *const column_type = array->type->column_type();
592
593 /* Offset in the constant matrix to the first element of the column
594 * to be extracted.
595 */
596 const unsigned mat_idx = column * column_type->vector_elements;
597
Ian Romanick0bb70a32010-06-11 15:49:49 -0700598 ir_constant_data data;
Ian Romanick9b92af92010-06-11 12:20:12 -0700599
600 switch (column_type->base_type) {
601 case GLSL_TYPE_UINT:
602 case GLSL_TYPE_INT:
603 for (unsigned i = 0; i < column_type->vector_elements; i++)
604 data.u[i] = array->value.u[mat_idx + i];
605
606 break;
607
608 case GLSL_TYPE_FLOAT:
609 for (unsigned i = 0; i < column_type->vector_elements; i++)
610 data.f[i] = array->value.f[mat_idx + i];
611
612 break;
613
614 default:
615 assert(!"Should not get here.");
616 break;
617 }
618
Carl Worth1660a292010-06-23 18:11:51 -0700619 this->value = new(ctx) ir_constant(column_type, &data);
Ian Romanick9b92af92010-06-11 12:20:12 -0700620 } else if (array->type->is_vector()) {
621 const unsigned component = idx->value.u[0];
622
Carl Worth1660a292010-06-23 18:11:51 -0700623 this->value = new(ctx) ir_constant(array, component);
Ian Romanick9b92af92010-06-11 12:20:12 -0700624 } else {
625 /* FINISHME: Handle access of constant arrays. */
626 }
627 }
Ian Romanickc7b10462010-05-19 13:20:12 +0200628}
629
630
631void
632ir_constant_visitor::visit(ir_dereference_record *ir)
633{
Ian Romanick253dede2010-06-09 17:30:19 -0700634 ir_constant *v = ir->record->constant_expression_value();
635
636 this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL;
Ian Romanick1cf43a42010-03-30 16:56:50 -0700637}
638
639
640void
641ir_constant_visitor::visit(ir_assignment *ir)
642{
643 (void) ir;
644 value = NULL;
645}
646
647
648void
649ir_constant_visitor::visit(ir_constant *ir)
650{
651 value = ir;
652}
653
654
655void
656ir_constant_visitor::visit(ir_call *ir)
657{
658 (void) ir;
659 value = NULL;
660}
661
662
663void
664ir_constant_visitor::visit(ir_return *ir)
665{
666 (void) ir;
667 value = NULL;
668}
669
670
671void
Kenneth Graunke16efab12010-06-30 10:47:34 -0700672ir_constant_visitor::visit(ir_discard *ir)
673{
674 (void) ir;
675 value = NULL;
676}
677
678
679void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700680ir_constant_visitor::visit(ir_if *ir)
681{
682 (void) ir;
683 value = NULL;
684}
Ian Romanickfad607a2010-04-05 16:16:07 -0700685
686
687void
688ir_constant_visitor::visit(ir_loop *ir)
689{
690 (void) ir;
691 value = NULL;
692}
Ian Romanickf8e31e02010-04-05 16:28:15 -0700693
694
695void
696ir_constant_visitor::visit(ir_loop_jump *ir)
697{
698 (void) ir;
699 value = NULL;
700}