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
];
157 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
158 type
= ir
->operands
[0]->type
;
159 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
160 switch (ir
->operands
[0]->type
->base_type
) {
162 u
[c
] = op
[0]->value
.u
[c
] + op
[1]->value
.u
[c
];
165 i
[c
] = op
[0]->value
.i
[c
] + op
[1]->value
.i
[c
];
167 case GLSL_TYPE_FLOAT
:
168 f
[c
] = op
[0]->value
.f
[c
] + op
[1]->value
.f
[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 !ir
->operands
[0]->type
->is_matrix()) {
199 type
= ir
->operands
[0]->type
;
200 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
201 switch (ir
->operands
[0]->type
->base_type
) {
203 u
[c
] = op
[0]->value
.u
[c
] * op
[1]->value
.u
[c
];
206 i
[c
] = op
[0]->value
.i
[c
] * op
[1]->value
.i
[c
];
208 case GLSL_TYPE_FLOAT
:
209 f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
];
218 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
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
];
237 case ir_binop_logic_and
:
238 type
= ir
->operands
[0]->type
;
239 assert(type
->base_type
== GLSL_TYPE_BOOL
);
240 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
241 b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
243 case ir_binop_logic_xor
:
244 type
= ir
->operands
[0]->type
;
245 assert(type
->base_type
== GLSL_TYPE_BOOL
);
246 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
247 b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
249 case ir_binop_logic_or
:
250 type
= ir
->operands
[0]->type
;
251 assert(type
->base_type
== GLSL_TYPE_BOOL
);
252 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
253 b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
257 type
= glsl_type::bool_type
;
258 switch (ir
->operands
[0]->type
->base_type
) {
260 b
[0] = op
[0]->value
.u
[0] < op
[1]->value
.u
[0];
263 b
[0] = op
[0]->value
.i
[0] < op
[1]->value
.i
[0];
265 case GLSL_TYPE_FLOAT
:
266 b
[0] = op
[0]->value
.f
[0] < op
[1]->value
.f
[0];
272 case ir_binop_greater
:
273 type
= glsl_type::bool_type
;
274 switch (ir
->operands
[0]->type
->base_type
) {
276 b
[0] = op
[0]->value
.u
[0] > op
[1]->value
.u
[0];
279 b
[0] = op
[0]->value
.i
[0] > op
[1]->value
.i
[0];
281 case GLSL_TYPE_FLOAT
:
282 b
[0] = op
[0]->value
.f
[0] > op
[1]->value
.f
[0];
288 case ir_binop_lequal
:
289 type
= glsl_type::bool_type
;
290 switch (ir
->operands
[0]->type
->base_type
) {
292 b
[0] = op
[0]->value
.u
[0] <= op
[1]->value
.u
[0];
295 b
[0] = op
[0]->value
.i
[0] <= op
[1]->value
.i
[0];
297 case GLSL_TYPE_FLOAT
:
298 b
[0] = op
[0]->value
.f
[0] <= op
[1]->value
.f
[0];
304 case ir_binop_gequal
:
305 type
= glsl_type::bool_type
;
306 switch (ir
->operands
[0]->type
->base_type
) {
308 b
[0] = op
[0]->value
.u
[0] >= op
[1]->value
.u
[0];
311 b
[0] = op
[0]->value
.i
[0] >= op
[1]->value
.i
[0];
313 case GLSL_TYPE_FLOAT
:
314 b
[0] = op
[0]->value
.f
[0] >= op
[1]->value
.f
[0];
322 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
323 type
= glsl_type::bool_type
;
325 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
326 switch (ir
->operands
[0]->type
->base_type
) {
328 b
[0] = b
[0] && op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
331 b
[0] = b
[0] && op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
333 case GLSL_TYPE_FLOAT
:
334 b
[0] = b
[0] && op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
342 case ir_binop_nequal
:
343 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
344 type
= glsl_type::bool_type
;
346 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
347 switch (ir
->operands
[0]->type
->base_type
) {
349 b
[0] = b
[0] || op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
352 b
[0] = b
[0] || op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
354 case GLSL_TYPE_FLOAT
:
355 b
[0] = b
[0] || op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
369 switch (type
->base_type
) {
371 value
= new ir_constant(type
, u
);
374 value
= new ir_constant(type
, i
);
376 case GLSL_TYPE_FLOAT
:
377 value
= new ir_constant(type
, f
);
380 value
= new ir_constant(type
, b
);
388 ir_constant_visitor::visit(ir_swizzle
*ir
)
396 ir_constant_visitor::visit(ir_dereference
*ir
)
404 ir_constant_visitor::visit(ir_assignment
*ir
)
412 ir_constant_visitor::visit(ir_constant
*ir
)
419 ir_constant_visitor::visit(ir_call
*ir
)
427 ir_constant_visitor::visit(ir_return
*ir
)
435 ir_constant_visitor::visit(ir_if
*ir
)