blob: 5c2e3629e1c689412b6bb6bbd473774f55d1345e [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
Kenneth Graunke3f4a0b82010-07-05 21:15:32 -0700320 case ir_binop_dot:
321 assert(op[0]->type->is_vector() && op[1]->type->is_vector());
322 data.f[0] = 0;
323 for (c = 0; c < op[0]->type->components(); c++) {
324 switch (ir->operands[0]->type->base_type) {
325 case GLSL_TYPE_UINT:
326 data.u[0] += op[0]->value.u[c] * op[1]->value.u[c];
327 break;
328 case GLSL_TYPE_INT:
329 data.i[0] += op[0]->value.i[c] * op[1]->value.i[c];
330 break;
331 case GLSL_TYPE_FLOAT:
332 data.f[0] += op[0]->value.f[c] * op[1]->value.f[c];
333 break;
334 default:
335 assert(0);
336 }
337 }
338
339 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000340 case ir_binop_add:
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700341 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
342 for (unsigned c = 0, c0 = 0, c1 = 0;
343 c < components;
344 c0 += c0_inc, c1 += c1_inc, c++) {
345
346 switch (ir->operands[0]->type->base_type) {
347 case GLSL_TYPE_UINT:
348 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
349 break;
350 case GLSL_TYPE_INT:
351 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
352 break;
353 case GLSL_TYPE_FLOAT:
354 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
355 break;
356 default:
357 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000358 }
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700359 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700360
Eric Anholtd251b922010-04-01 18:35:42 -1000361 break;
362 case ir_binop_sub:
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700363 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
364 for (unsigned c = 0, c0 = 0, c1 = 0;
365 c < components;
366 c0 += c0_inc, c1 += c1_inc, c++) {
367
368 switch (ir->operands[0]->type->base_type) {
369 case GLSL_TYPE_UINT:
370 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
371 break;
372 case GLSL_TYPE_INT:
373 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
374 break;
375 case GLSL_TYPE_FLOAT:
376 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
377 break;
378 default:
379 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000380 }
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700381 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700382
Eric Anholtd251b922010-04-01 18:35:42 -1000383 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000384 case ir_binop_mul:
Kenneth Graunkecf80a4d2010-07-05 23:19:56 -0700385 /* Check for equal types, or unequal types involving scalars */
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700386 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
387 || op0_scalar || op1_scalar) {
388 for (unsigned c = 0, c0 = 0, c1 = 0;
389 c < components;
390 c0 += c0_inc, c1 += c1_inc, c++) {
391
Eric Anholtd98da972010-04-01 18:25:11 -1000392 switch (ir->operands[0]->type->base_type) {
393 case GLSL_TYPE_UINT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700394 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000395 break;
396 case GLSL_TYPE_INT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700397 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000398 break;
399 case GLSL_TYPE_FLOAT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700400 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000401 break;
402 default:
403 assert(0);
404 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000405 }
Kenneth Graunkecf80a4d2010-07-05 23:19:56 -0700406 } else {
407 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
408
409 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
410 * matrix can be a GLSL vector, either N or P can be 1.
411 *
412 * For vec*mat, the vector is treated as a row vector. This
413 * means the vector is a 1-row x M-column matrix.
414 *
415 * For mat*vec, the vector is treated as a column vector. Since
416 * matrix_columns is 1 for vectors, this just works.
417 */
418 const unsigned n = op[0]->type->is_vector()
419 ? 1 : op[0]->type->vector_elements;
420 const unsigned m = op[1]->type->vector_elements;
421 const unsigned p = op[1]->type->matrix_columns;
422 for (unsigned j = 0; j < p; j++) {
423 for (unsigned i = 0; i < n; i++) {
424 for (unsigned k = 0; k < m; k++) {
425 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
426 }
427 }
428 }
429 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700430
Eric Anholta576f9d2010-03-31 16:25:12 -1000431 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000432 case ir_binop_div:
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700433 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
434 for (unsigned c = 0, c0 = 0, c1 = 0;
435 c < components;
436 c0 += c0_inc, c1 += c1_inc, c++) {
437
438 switch (ir->operands[0]->type->base_type) {
439 case GLSL_TYPE_UINT:
440 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
441 break;
442 case GLSL_TYPE_INT:
443 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
444 break;
445 case GLSL_TYPE_FLOAT:
446 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
447 break;
448 default:
449 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000450 }
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700451 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700452
Eric Anholtd251b922010-04-01 18:35:42 -1000453 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000454 case ir_binop_logic_and:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700455 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholtd98da972010-04-01 18:25:11 -1000456 for (c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700457 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000458 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000459 case ir_binop_logic_xor:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700460 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholtd251b922010-04-01 18:35:42 -1000461 for (c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700462 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000463 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000464 case ir_binop_logic_or:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700465 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Eric Anholtd98da972010-04-01 18:25:11 -1000466 for (c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700467 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000468 break;
Eric Anholt85171c22010-04-06 09:55:45 -0700469
470 case ir_binop_less:
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 case ir_binop_greater:
Eric Anholt85171c22010-04-06 09:55:45 -0700486 switch (ir->operands[0]->type->base_type) {
487 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700488 data.b[0] = op[0]->value.u[0] > op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700489 break;
490 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700491 data.b[0] = op[0]->value.i[0] > op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700492 break;
493 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700494 data.b[0] = op[0]->value.f[0] > op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700495 break;
496 default:
497 assert(0);
498 }
499 break;
500 case ir_binop_lequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700501 switch (ir->operands[0]->type->base_type) {
502 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700503 data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700504 break;
505 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700506 data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700507 break;
508 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700509 data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700510 break;
511 default:
512 assert(0);
513 }
514 break;
515 case ir_binop_gequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700516 switch (ir->operands[0]->type->base_type) {
517 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700518 data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700519 break;
520 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700521 data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700522 break;
523 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700524 data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700525 break;
526 default:
527 assert(0);
528 }
529 break;
530
Eric Anholtec1949e2010-04-06 10:02:27 -0700531 case ir_binop_equal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700532 data.b[0] = true;
533 for (c = 0; c < ir->operands[0]->type->components(); c++) {
534 switch (ir->operands[0]->type->base_type) {
535 case GLSL_TYPE_UINT:
536 data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c];
537 break;
538 case GLSL_TYPE_INT:
539 data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c];
540 break;
541 case GLSL_TYPE_FLOAT:
542 data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c];
543 break;
544 case GLSL_TYPE_BOOL:
545 data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c];
546 break;
547 default:
548 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700549 }
550 }
551 break;
552 case ir_binop_nequal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700553 data.b[0] = false;
554 for (c = 0; c < ir->operands[0]->type->components(); c++) {
555 switch (ir->operands[0]->type->base_type) {
556 case GLSL_TYPE_UINT:
557 data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c];
558 break;
559 case GLSL_TYPE_INT:
560 data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c];
561 break;
562 case GLSL_TYPE_FLOAT:
563 data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c];
564 break;
565 case GLSL_TYPE_BOOL:
566 data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c];
567 break;
568 default:
569 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700570 }
571 }
572 break;
573
Eric Anholta576f9d2010-03-31 16:25:12 -1000574 default:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700575 /* FINISHME: Should handle all expression types. */
576 return;
Eric Anholta576f9d2010-03-31 16:25:12 -1000577 }
Eric Anholtd98da972010-04-01 18:25:11 -1000578
Eric Anholt6b01b502010-06-24 15:18:39 -0700579 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700580 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanick1cf43a42010-03-30 16:56:50 -0700581}
582
583
584void
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700585ir_constant_visitor::visit(ir_texture *ir)
586{
587 // FINISHME: Do stuff with texture lookups
588 (void) ir;
589 value = NULL;
590}
591
592
593void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700594ir_constant_visitor::visit(ir_swizzle *ir)
595{
Ian Romanickc2ba6192010-06-11 12:30:28 -0700596 ir_constant *v = ir->val->constant_expression_value();
597
598 this->value = NULL;
599
600 if (v != NULL) {
Ian Romanick0bb70a32010-06-11 15:49:49 -0700601 ir_constant_data data;
Ian Romanickc2ba6192010-06-11 12:30:28 -0700602
603 const unsigned swiz_idx[4] = {
604 ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w
605 };
606
607 for (unsigned i = 0; i < ir->mask.num_components; i++) {
608 switch (v->type->base_type) {
609 case GLSL_TYPE_UINT:
610 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
611 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
612 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
613 default: assert(!"Should not get here."); break;
614 }
615 }
616
Eric Anholt6b01b502010-06-24 15:18:39 -0700617 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700618 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanickc2ba6192010-06-11 12:30:28 -0700619 }
Ian Romanick1cf43a42010-03-30 16:56:50 -0700620}
621
622
623void
Ian Romanickc7b10462010-05-19 13:20:12 +0200624ir_constant_visitor::visit(ir_dereference_variable *ir)
Ian Romanick1cf43a42010-03-30 16:56:50 -0700625{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700626 value = NULL;
Eric Anholt326c6762010-04-06 10:30:54 -0700627
Ian Romanickc7b10462010-05-19 13:20:12 +0200628 ir_variable *var = ir->variable_referenced();
629 if (var && var->constant_value)
Eric Anholt4b6fd392010-06-23 11:37:12 -0700630 value = (ir_constant *)var->constant_value->clone(NULL);
Ian Romanickc7b10462010-05-19 13:20:12 +0200631}
632
633
634void
635ir_constant_visitor::visit(ir_dereference_array *ir)
636{
Carl Worth1660a292010-06-23 18:11:51 -0700637 void *ctx = talloc_parent(ir);
Ian Romanick9b92af92010-06-11 12:20:12 -0700638 ir_constant *array = ir->array->constant_expression_value();
639 ir_constant *idx = ir->array_index->constant_expression_value();
640
641 this->value = NULL;
642
643 if ((array != NULL) && (idx != NULL)) {
644 if (array->type->is_matrix()) {
645 /* Array access of a matrix results in a vector.
646 */
647 const unsigned column = idx->value.u[0];
648
649 const glsl_type *const column_type = array->type->column_type();
650
651 /* Offset in the constant matrix to the first element of the column
652 * to be extracted.
653 */
654 const unsigned mat_idx = column * column_type->vector_elements;
655
Ian Romanick0bb70a32010-06-11 15:49:49 -0700656 ir_constant_data data;
Ian Romanick9b92af92010-06-11 12:20:12 -0700657
658 switch (column_type->base_type) {
659 case GLSL_TYPE_UINT:
660 case GLSL_TYPE_INT:
661 for (unsigned i = 0; i < column_type->vector_elements; i++)
662 data.u[i] = array->value.u[mat_idx + i];
663
664 break;
665
666 case GLSL_TYPE_FLOAT:
667 for (unsigned i = 0; i < column_type->vector_elements; i++)
668 data.f[i] = array->value.f[mat_idx + i];
669
670 break;
671
672 default:
673 assert(!"Should not get here.");
674 break;
675 }
676
Carl Worth1660a292010-06-23 18:11:51 -0700677 this->value = new(ctx) ir_constant(column_type, &data);
Ian Romanick9b92af92010-06-11 12:20:12 -0700678 } else if (array->type->is_vector()) {
679 const unsigned component = idx->value.u[0];
680
Carl Worth1660a292010-06-23 18:11:51 -0700681 this->value = new(ctx) ir_constant(array, component);
Ian Romanick9b92af92010-06-11 12:20:12 -0700682 } else {
683 /* FINISHME: Handle access of constant arrays. */
684 }
685 }
Ian Romanickc7b10462010-05-19 13:20:12 +0200686}
687
688
689void
690ir_constant_visitor::visit(ir_dereference_record *ir)
691{
Ian Romanick253dede2010-06-09 17:30:19 -0700692 ir_constant *v = ir->record->constant_expression_value();
693
694 this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL;
Ian Romanick1cf43a42010-03-30 16:56:50 -0700695}
696
697
698void
699ir_constant_visitor::visit(ir_assignment *ir)
700{
701 (void) ir;
702 value = NULL;
703}
704
705
706void
707ir_constant_visitor::visit(ir_constant *ir)
708{
709 value = ir;
710}
711
712
713void
714ir_constant_visitor::visit(ir_call *ir)
715{
716 (void) ir;
717 value = NULL;
718}
719
720
721void
722ir_constant_visitor::visit(ir_return *ir)
723{
724 (void) ir;
725 value = NULL;
726}
727
728
729void
Kenneth Graunke16efab12010-06-30 10:47:34 -0700730ir_constant_visitor::visit(ir_discard *ir)
731{
732 (void) ir;
733 value = NULL;
734}
735
736
737void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700738ir_constant_visitor::visit(ir_if *ir)
739{
740 (void) ir;
741 value = NULL;
742}
Ian Romanickfad607a2010-04-05 16:16:07 -0700743
744
745void
746ir_constant_visitor::visit(ir_loop *ir)
747{
748 (void) ir;
749 value = NULL;
750}
Ian Romanickf8e31e02010-04-05 16:28:15 -0700751
752
753void
754ir_constant_visitor::visit(ir_loop_jump *ir)
755{
756 (void) ir;
757 value = NULL;
758}