blob: 2fab03bb60efd37708cf3f3a08f8ea5a24f63cf4 [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
Kenneth Graunke6fc983b2010-07-06 02:39:57 -0700144 if (op[1] != NULL)
145 assert(op[0]->type->base_type == op[1]->type->base_type);
146
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700147 bool op0_scalar = op[0]->type->is_scalar();
148 bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar();
149
150 /* When iterating over a vector or matrix's components, we want to increase
151 * the loop counter. However, for scalars, we want to stay at 0.
152 */
153 unsigned c0_inc = op0_scalar ? 1 : 0;
154 unsigned c1_inc = op1_scalar ? 1 : 0;
155 unsigned components = op[op1_scalar ? 0 : 1]->type->components();
156
Eric Anholta576f9d2010-03-31 16:25:12 -1000157 switch (ir->operation) {
Eric Anholt528bb852010-03-31 21:09:02 -1000158 case ir_unop_logic_not:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700159 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholtd98da972010-04-01 18:25:11 -1000160 for (c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700161 data.b[c] = !op[0]->value.b[c];
Eric Anholt528bb852010-03-31 21:09:02 -1000162 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700163
164 case ir_unop_f2i:
165 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholtaf186412010-04-06 10:53:57 -0700166 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700167 data.i[c] = op[0]->value.f[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700168 }
169 break;
170 case ir_unop_i2f:
171 assert(op[0]->type->base_type == GLSL_TYPE_UINT ||
172 op[0]->type->base_type == GLSL_TYPE_INT);
Eric Anholtaf186412010-04-06 10:53:57 -0700173 for (c = 0; c < ir->operands[0]->type->components(); c++) {
174 if (op[0]->type->base_type == GLSL_TYPE_INT)
Ian Romanick4daaab62010-06-11 16:08:47 -0700175 data.f[c] = op[0]->value.i[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700176 else
Ian Romanick4daaab62010-06-11 16:08:47 -0700177 data.f[c] = op[0]->value.u[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700178 }
179 break;
Ian Romanick7dc2b712010-06-07 15:10:14 -0700180 case ir_unop_b2f:
181 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Ian Romanick7dc2b712010-06-07 15:10:14 -0700182 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700183 data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0;
Ian Romanick7dc2b712010-06-07 15:10:14 -0700184 }
185 break;
186 case ir_unop_f2b:
187 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Ian Romanick7dc2b712010-06-07 15:10:14 -0700188 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700189 data.b[c] = bool(op[0]->value.f[c]);
Ian Romanick7dc2b712010-06-07 15:10:14 -0700190 }
191 break;
Ian Romanick39d6dd32010-06-11 13:46:30 -0700192 case ir_unop_b2i:
193 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Ian Romanick39d6dd32010-06-11 13:46:30 -0700194 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700195 data.u[c] = op[0]->value.b[c] ? 1 : 0;
Ian Romanick39d6dd32010-06-11 13:46:30 -0700196 }
197 break;
198 case ir_unop_i2b:
199 assert(op[0]->type->is_integer());
Ian Romanick39d6dd32010-06-11 13:46:30 -0700200 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700201 data.b[c] = bool(op[0]->value.u[c]);
Ian Romanick39d6dd32010-06-11 13:46:30 -0700202 }
203 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700204
Eric Anholtd925c912010-07-01 10:37:11 -0700205 case ir_unop_fract:
206 for (c = 0; c < ir->operands[0]->type->components(); c++) {
207 switch (ir->type->base_type) {
208 case GLSL_TYPE_UINT:
209 data.u[c] = 0;
210 break;
211 case GLSL_TYPE_INT:
212 data.i[c] = 0;
213 break;
214 case GLSL_TYPE_FLOAT:
215 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
216 break;
217 default:
218 assert(0);
219 }
220 }
221 break;
222
Eric Anholt43ad37a2010-05-12 14:42:21 -0700223 case ir_unop_neg:
Eric Anholt43ad37a2010-05-12 14:42:21 -0700224 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700225 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700226 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700227 data.u[c] = -op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700228 break;
229 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700230 data.i[c] = -op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700231 break;
232 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700233 data.f[c] = -op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700234 break;
235 default:
236 assert(0);
237 }
238 }
239 break;
240
241 case ir_unop_abs:
242 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700243 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700244 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700245 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700246 data.u[c] = op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700247 break;
248 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700249 data.i[c] = op[0]->value.i[c];
250 if (data.i[c] < 0)
251 data.i[c] = -data.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700252 break;
253 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700254 data.f[c] = fabs(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700255 break;
256 default:
257 assert(0);
258 }
259 }
260 break;
261
262 case ir_unop_rcp:
263 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700264 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700265 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700266 case GLSL_TYPE_UINT:
267 if (op[0]->value.u[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700268 data.u[c] = 1 / op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700269 break;
270 case GLSL_TYPE_INT:
271 if (op[0]->value.i[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700272 data.i[c] = 1 / op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700273 break;
274 case GLSL_TYPE_FLOAT:
275 if (op[0]->value.f[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700276 data.f[c] = 1.0 / op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700277 break;
278 default:
279 assert(0);
280 }
281 }
282 break;
283
284 case ir_unop_rsq:
285 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700286 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700287 data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700288 }
289 break;
290
291 case ir_unop_sqrt:
292 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700293 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700294 data.f[c] = sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700295 }
296 break;
297
298 case ir_unop_exp:
299 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700300 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700301 data.f[c] = expf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700302 }
303 break;
304
305 case ir_unop_log:
306 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700307 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700308 data.f[c] = logf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700309 }
310 break;
311
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700312 case ir_unop_dFdx:
313 case ir_unop_dFdy:
314 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700315 for (c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700316 data.f[c] = 0.0;
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700317 }
318 break;
319
Eric Anholtd251b922010-04-01 18:35:42 -1000320 case ir_binop_add:
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700321 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
322 for (unsigned c = 0, c0 = 0, c1 = 0;
323 c < components;
324 c0 += c0_inc, c1 += c1_inc, c++) {
325
326 switch (ir->operands[0]->type->base_type) {
327 case GLSL_TYPE_UINT:
328 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
329 break;
330 case GLSL_TYPE_INT:
331 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
332 break;
333 case GLSL_TYPE_FLOAT:
334 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
335 break;
336 default:
337 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000338 }
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700339 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700340
Eric Anholtd251b922010-04-01 18:35:42 -1000341 break;
342 case ir_binop_sub:
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700343 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
344 for (unsigned c = 0, c0 = 0, c1 = 0;
345 c < components;
346 c0 += c0_inc, c1 += c1_inc, c++) {
347
348 switch (ir->operands[0]->type->base_type) {
349 case GLSL_TYPE_UINT:
350 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
351 break;
352 case GLSL_TYPE_INT:
353 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
354 break;
355 case GLSL_TYPE_FLOAT:
356 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
357 break;
358 default:
359 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000360 }
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700361 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700362
Eric Anholtd251b922010-04-01 18:35:42 -1000363 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000364 case ir_binop_mul:
Eric Anholtd98da972010-04-01 18:25:11 -1000365 if (ir->operands[0]->type == ir->operands[1]->type &&
366 !ir->operands[0]->type->is_matrix()) {
Eric Anholtd98da972010-04-01 18:25:11 -1000367 for (c = 0; c < ir->operands[0]->type->components(); c++) {
368 switch (ir->operands[0]->type->base_type) {
369 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700370 data.u[c] = op[0]->value.u[c] * op[1]->value.u[c];
Eric Anholtd98da972010-04-01 18:25:11 -1000371 break;
372 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700373 data.i[c] = op[0]->value.i[c] * op[1]->value.i[c];
Eric Anholtd98da972010-04-01 18:25:11 -1000374 break;
375 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700376 data.f[c] = op[0]->value.f[c] * op[1]->value.f[c];
Eric Anholtd98da972010-04-01 18:25:11 -1000377 break;
378 default:
379 assert(0);
380 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000381 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700382 } else
383 /* FINISHME: Support operations with non-equal types. */
384 return;
385
Eric Anholta576f9d2010-03-31 16:25:12 -1000386 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000387 case ir_binop_div:
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700388 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
389 for (unsigned c = 0, c0 = 0, c1 = 0;
390 c < components;
391 c0 += c0_inc, c1 += c1_inc, c++) {
392
393 switch (ir->operands[0]->type->base_type) {
394 case GLSL_TYPE_UINT:
395 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
396 break;
397 case GLSL_TYPE_INT:
398 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
399 break;
400 case GLSL_TYPE_FLOAT:
401 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
402 break;
403 default:
404 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000405 }
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700406 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700407
Eric Anholtd251b922010-04-01 18:35:42 -1000408 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000409 case ir_binop_logic_and:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700410 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholtd98da972010-04-01 18:25:11 -1000411 for (c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700412 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000413 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000414 case ir_binop_logic_xor:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700415 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholtd251b922010-04-01 18:35:42 -1000416 for (c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700417 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000418 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000419 case ir_binop_logic_or:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700420 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholtd98da972010-04-01 18:25:11 -1000421 for (c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700422 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000423 break;
Eric Anholt85171c22010-04-06 09:55:45 -0700424
425 case ir_binop_less:
Eric Anholt85171c22010-04-06 09:55:45 -0700426 switch (ir->operands[0]->type->base_type) {
427 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700428 data.b[0] = op[0]->value.u[0] < op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700429 break;
430 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700431 data.b[0] = op[0]->value.i[0] < op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700432 break;
433 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700434 data.b[0] = op[0]->value.f[0] < op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700435 break;
436 default:
437 assert(0);
438 }
439 break;
440 case ir_binop_greater:
Eric Anholt85171c22010-04-06 09:55:45 -0700441 switch (ir->operands[0]->type->base_type) {
442 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700443 data.b[0] = op[0]->value.u[0] > op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700444 break;
445 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700446 data.b[0] = op[0]->value.i[0] > op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700447 break;
448 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700449 data.b[0] = op[0]->value.f[0] > op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700450 break;
451 default:
452 assert(0);
453 }
454 break;
455 case ir_binop_lequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700456 switch (ir->operands[0]->type->base_type) {
457 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700458 data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700459 break;
460 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700461 data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700462 break;
463 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700464 data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700465 break;
466 default:
467 assert(0);
468 }
469 break;
470 case ir_binop_gequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700471 switch (ir->operands[0]->type->base_type) {
472 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700473 data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700474 break;
475 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700476 data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700477 break;
478 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700479 data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700480 break;
481 default:
482 assert(0);
483 }
484 break;
485
Eric Anholtec1949e2010-04-06 10:02:27 -0700486 case ir_binop_equal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700487 data.b[0] = true;
488 for (c = 0; c < ir->operands[0]->type->components(); c++) {
489 switch (ir->operands[0]->type->base_type) {
490 case GLSL_TYPE_UINT:
491 data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c];
492 break;
493 case GLSL_TYPE_INT:
494 data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c];
495 break;
496 case GLSL_TYPE_FLOAT:
497 data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c];
498 break;
499 case GLSL_TYPE_BOOL:
500 data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c];
501 break;
502 default:
503 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700504 }
505 }
506 break;
507 case ir_binop_nequal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700508 data.b[0] = false;
509 for (c = 0; c < ir->operands[0]->type->components(); c++) {
510 switch (ir->operands[0]->type->base_type) {
511 case GLSL_TYPE_UINT:
512 data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c];
513 break;
514 case GLSL_TYPE_INT:
515 data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c];
516 break;
517 case GLSL_TYPE_FLOAT:
518 data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c];
519 break;
520 case GLSL_TYPE_BOOL:
521 data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c];
522 break;
523 default:
524 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700525 }
526 }
527 break;
528
Eric Anholta576f9d2010-03-31 16:25:12 -1000529 default:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700530 /* FINISHME: Should handle all expression types. */
531 return;
Eric Anholta576f9d2010-03-31 16:25:12 -1000532 }
Eric Anholtd98da972010-04-01 18:25:11 -1000533
Eric Anholt6b01b502010-06-24 15:18:39 -0700534 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700535 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanick1cf43a42010-03-30 16:56:50 -0700536}
537
538
539void
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700540ir_constant_visitor::visit(ir_texture *ir)
541{
542 // FINISHME: Do stuff with texture lookups
543 (void) ir;
544 value = NULL;
545}
546
547
548void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700549ir_constant_visitor::visit(ir_swizzle *ir)
550{
Ian Romanickc2ba6192010-06-11 12:30:28 -0700551 ir_constant *v = ir->val->constant_expression_value();
552
553 this->value = NULL;
554
555 if (v != NULL) {
Ian Romanick0bb70a32010-06-11 15:49:49 -0700556 ir_constant_data data;
Ian Romanickc2ba6192010-06-11 12:30:28 -0700557
558 const unsigned swiz_idx[4] = {
559 ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w
560 };
561
562 for (unsigned i = 0; i < ir->mask.num_components; i++) {
563 switch (v->type->base_type) {
564 case GLSL_TYPE_UINT:
565 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
566 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
567 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
568 default: assert(!"Should not get here."); break;
569 }
570 }
571
Eric Anholt6b01b502010-06-24 15:18:39 -0700572 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700573 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanickc2ba6192010-06-11 12:30:28 -0700574 }
Ian Romanick1cf43a42010-03-30 16:56:50 -0700575}
576
577
578void
Ian Romanickc7b10462010-05-19 13:20:12 +0200579ir_constant_visitor::visit(ir_dereference_variable *ir)
Ian Romanick1cf43a42010-03-30 16:56:50 -0700580{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700581 value = NULL;
Eric Anholt326c6762010-04-06 10:30:54 -0700582
Ian Romanickc7b10462010-05-19 13:20:12 +0200583 ir_variable *var = ir->variable_referenced();
584 if (var && var->constant_value)
Eric Anholt4b6fd392010-06-23 11:37:12 -0700585 value = (ir_constant *)var->constant_value->clone(NULL);
Ian Romanickc7b10462010-05-19 13:20:12 +0200586}
587
588
589void
590ir_constant_visitor::visit(ir_dereference_array *ir)
591{
Carl Worth1660a292010-06-23 18:11:51 -0700592 void *ctx = talloc_parent(ir);
Ian Romanick9b92af92010-06-11 12:20:12 -0700593 ir_constant *array = ir->array->constant_expression_value();
594 ir_constant *idx = ir->array_index->constant_expression_value();
595
596 this->value = NULL;
597
598 if ((array != NULL) && (idx != NULL)) {
599 if (array->type->is_matrix()) {
600 /* Array access of a matrix results in a vector.
601 */
602 const unsigned column = idx->value.u[0];
603
604 const glsl_type *const column_type = array->type->column_type();
605
606 /* Offset in the constant matrix to the first element of the column
607 * to be extracted.
608 */
609 const unsigned mat_idx = column * column_type->vector_elements;
610
Ian Romanick0bb70a32010-06-11 15:49:49 -0700611 ir_constant_data data;
Ian Romanick9b92af92010-06-11 12:20:12 -0700612
613 switch (column_type->base_type) {
614 case GLSL_TYPE_UINT:
615 case GLSL_TYPE_INT:
616 for (unsigned i = 0; i < column_type->vector_elements; i++)
617 data.u[i] = array->value.u[mat_idx + i];
618
619 break;
620
621 case GLSL_TYPE_FLOAT:
622 for (unsigned i = 0; i < column_type->vector_elements; i++)
623 data.f[i] = array->value.f[mat_idx + i];
624
625 break;
626
627 default:
628 assert(!"Should not get here.");
629 break;
630 }
631
Carl Worth1660a292010-06-23 18:11:51 -0700632 this->value = new(ctx) ir_constant(column_type, &data);
Ian Romanick9b92af92010-06-11 12:20:12 -0700633 } else if (array->type->is_vector()) {
634 const unsigned component = idx->value.u[0];
635
Carl Worth1660a292010-06-23 18:11:51 -0700636 this->value = new(ctx) ir_constant(array, component);
Ian Romanick9b92af92010-06-11 12:20:12 -0700637 } else {
638 /* FINISHME: Handle access of constant arrays. */
639 }
640 }
Ian Romanickc7b10462010-05-19 13:20:12 +0200641}
642
643
644void
645ir_constant_visitor::visit(ir_dereference_record *ir)
646{
Ian Romanick253dede2010-06-09 17:30:19 -0700647 ir_constant *v = ir->record->constant_expression_value();
648
649 this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL;
Ian Romanick1cf43a42010-03-30 16:56:50 -0700650}
651
652
653void
654ir_constant_visitor::visit(ir_assignment *ir)
655{
656 (void) ir;
657 value = NULL;
658}
659
660
661void
662ir_constant_visitor::visit(ir_constant *ir)
663{
664 value = ir;
665}
666
667
668void
669ir_constant_visitor::visit(ir_call *ir)
670{
671 (void) ir;
672 value = NULL;
673}
674
675
676void
677ir_constant_visitor::visit(ir_return *ir)
678{
679 (void) ir;
680 value = NULL;
681}
682
683
684void
Kenneth Graunke16efab12010-06-30 10:47:34 -0700685ir_constant_visitor::visit(ir_discard *ir)
686{
687 (void) ir;
688 value = NULL;
689}
690
691
692void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700693ir_constant_visitor::visit(ir_if *ir)
694{
695 (void) ir;
696 value = NULL;
697}
Ian Romanickfad607a2010-04-05 16:16:07 -0700698
699
700void
701ir_constant_visitor::visit(ir_loop *ir)
702{
703 (void) ir;
704 value = NULL;
705}
Ian Romanickf8e31e02010-04-05 16:28:15 -0700706
707
708void
709ir_constant_visitor::visit(ir_loop_jump *ir)
710{
711 (void) ir;
712 value = NULL;
713}