2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
38 #include "ir_visitor.h"
39 #include "glsl_types.h"
42 * Visitor class for evaluating constant expressions
44 class ir_constant_visitor
: public ir_visitor
{
52 virtual ~ir_constant_visitor()
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.
65 virtual void visit(ir_variable
*);
66 virtual void visit(ir_label
*);
67 virtual void visit(ir_function_signature
*);
68 virtual void visit(ir_function
*);
69 virtual void visit(ir_expression
*);
70 virtual void visit(ir_swizzle
*);
71 virtual void visit(ir_dereference
*);
72 virtual void visit(ir_assignment
*);
73 virtual void visit(ir_constant
*);
74 virtual void visit(ir_call
*);
75 virtual void visit(ir_return
*);
76 virtual void visit(ir_if
*);
80 * Value of the constant expression.
83 * This field will be \c NULL if the expression is not constant valued.
85 /* FINIHSME: This cannot hold values for constant arrays or structures. */
91 ir_instruction::constant_expression_value()
93 ir_constant_visitor visitor
;
95 this->accept(& visitor
);
101 ir_constant_visitor::visit(ir_variable
*ir
)
109 ir_constant_visitor::visit(ir_label
*ir
)
117 ir_constant_visitor::visit(ir_function_signature
*ir
)
125 ir_constant_visitor::visit(ir_function
*ir
)
132 ir_constant_visitor::visit(ir_expression
*ir
)
136 unsigned int operand
, c
;
141 const glsl_type
*type
= NULL
;
143 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
144 op
[operand
] = ir
->operands
[operand
]->constant_expression_value();
149 switch (ir
->operation
) {
150 case ir_unop_logic_not
:
151 type
= ir
->operands
[0]->type
;
152 assert(type
->base_type
== GLSL_TYPE_BOOL
);
153 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
154 b
[c
] = !op
[0]->value
.b
[c
];
158 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
160 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
161 i
[c
] = op
[0]->value
.f
[c
];
165 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
||
166 op
[0]->type
->base_type
== GLSL_TYPE_INT
);
168 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
169 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
)
170 f
[c
] = op
[0]->value
.i
[c
];
172 f
[c
] = op
[0]->value
.u
[c
];
177 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
178 type
= ir
->operands
[0]->type
;
179 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
180 switch (ir
->operands
[0]->type
->base_type
) {
182 u
[c
] = op
[0]->value
.u
[c
] + op
[1]->value
.u
[c
];
185 i
[c
] = op
[0]->value
.i
[c
] + op
[1]->value
.i
[c
];
187 case GLSL_TYPE_FLOAT
:
188 f
[c
] = op
[0]->value
.f
[c
] + op
[1]->value
.f
[c
];
197 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
198 type
= ir
->operands
[0]->type
;
199 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
200 switch (ir
->operands
[0]->type
->base_type
) {
202 u
[c
] = op
[0]->value
.u
[c
] - op
[1]->value
.u
[c
];
205 i
[c
] = op
[0]->value
.i
[c
] - op
[1]->value
.i
[c
];
207 case GLSL_TYPE_FLOAT
:
208 f
[c
] = op
[0]->value
.f
[c
] - op
[1]->value
.f
[c
];
217 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
&&
218 !ir
->operands
[0]->type
->is_matrix()) {
219 type
= ir
->operands
[0]->type
;
220 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
221 switch (ir
->operands
[0]->type
->base_type
) {
223 u
[c
] = op
[0]->value
.u
[c
] * op
[1]->value
.u
[c
];
226 i
[c
] = op
[0]->value
.i
[c
] * op
[1]->value
.i
[c
];
228 case GLSL_TYPE_FLOAT
:
229 f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
];
238 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
239 type
= ir
->operands
[0]->type
;
240 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
241 switch (ir
->operands
[0]->type
->base_type
) {
243 u
[c
] = op
[0]->value
.u
[c
] / op
[1]->value
.u
[c
];
246 i
[c
] = op
[0]->value
.i
[c
] / op
[1]->value
.i
[c
];
248 case GLSL_TYPE_FLOAT
:
249 f
[c
] = op
[0]->value
.f
[c
] / op
[1]->value
.f
[c
];
257 case ir_binop_logic_and
:
258 type
= ir
->operands
[0]->type
;
259 assert(type
->base_type
== GLSL_TYPE_BOOL
);
260 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
261 b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
263 case ir_binop_logic_xor
:
264 type
= ir
->operands
[0]->type
;
265 assert(type
->base_type
== GLSL_TYPE_BOOL
);
266 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
267 b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
269 case ir_binop_logic_or
:
270 type
= ir
->operands
[0]->type
;
271 assert(type
->base_type
== GLSL_TYPE_BOOL
);
272 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
273 b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
277 type
= glsl_type::bool_type
;
278 switch (ir
->operands
[0]->type
->base_type
) {
280 b
[0] = op
[0]->value
.u
[0] < op
[1]->value
.u
[0];
283 b
[0] = op
[0]->value
.i
[0] < op
[1]->value
.i
[0];
285 case GLSL_TYPE_FLOAT
:
286 b
[0] = op
[0]->value
.f
[0] < op
[1]->value
.f
[0];
292 case ir_binop_greater
:
293 type
= glsl_type::bool_type
;
294 switch (ir
->operands
[0]->type
->base_type
) {
296 b
[0] = op
[0]->value
.u
[0] > op
[1]->value
.u
[0];
299 b
[0] = op
[0]->value
.i
[0] > op
[1]->value
.i
[0];
301 case GLSL_TYPE_FLOAT
:
302 b
[0] = op
[0]->value
.f
[0] > op
[1]->value
.f
[0];
308 case ir_binop_lequal
:
309 type
= glsl_type::bool_type
;
310 switch (ir
->operands
[0]->type
->base_type
) {
312 b
[0] = op
[0]->value
.u
[0] <= op
[1]->value
.u
[0];
315 b
[0] = op
[0]->value
.i
[0] <= op
[1]->value
.i
[0];
317 case GLSL_TYPE_FLOAT
:
318 b
[0] = op
[0]->value
.f
[0] <= op
[1]->value
.f
[0];
324 case ir_binop_gequal
:
325 type
= glsl_type::bool_type
;
326 switch (ir
->operands
[0]->type
->base_type
) {
328 b
[0] = op
[0]->value
.u
[0] >= op
[1]->value
.u
[0];
331 b
[0] = op
[0]->value
.i
[0] >= op
[1]->value
.i
[0];
333 case GLSL_TYPE_FLOAT
:
334 b
[0] = op
[0]->value
.f
[0] >= op
[1]->value
.f
[0];
342 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
343 type
= glsl_type::bool_type
;
345 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
346 switch (ir
->operands
[0]->type
->base_type
) {
348 b
[0] = b
[0] && op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
351 b
[0] = b
[0] && op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
353 case GLSL_TYPE_FLOAT
:
354 b
[0] = b
[0] && op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
362 case ir_binop_nequal
:
363 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
364 type
= glsl_type::bool_type
;
366 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
367 switch (ir
->operands
[0]->type
->base_type
) {
369 b
[0] = b
[0] || op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
372 b
[0] = b
[0] || op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
374 case GLSL_TYPE_FLOAT
:
375 b
[0] = b
[0] || op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
389 switch (type
->base_type
) {
391 value
= new ir_constant(type
, u
);
394 value
= new ir_constant(type
, i
);
396 case GLSL_TYPE_FLOAT
:
397 value
= new ir_constant(type
, f
);
400 value
= new ir_constant(type
, b
);
408 ir_constant_visitor::visit(ir_swizzle
*ir
)
416 ir_constant_visitor::visit(ir_dereference
*ir
)
420 if (ir
->mode
== ir_dereference::ir_reference_variable
) {
421 ir_variable
*var
= ir
->var
->as_variable();
422 if (var
&& var
->constant_value
) {
423 value
= new ir_constant(ir
->type
, &var
->constant_value
->value
);
426 /* FINISHME: Other dereference modes. */
431 ir_constant_visitor::visit(ir_assignment
*ir
)
439 ir_constant_visitor::visit(ir_constant
*ir
)
446 ir_constant_visitor::visit(ir_call
*ir
)
454 ir_constant_visitor::visit(ir_return
*ir
)
462 ir_constant_visitor::visit(ir_if
*ir
)