blob: e8820a0f8760e423a3d7254cc39b33b0188bae40 [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
36#define NULL 0
37#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 *);
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 *);
77 /*@}*/
78
79 /**
80 * Value of the constant expression.
81 *
82 * \note
83 * This field will be \c NULL if the expression is not constant valued.
84 */
85 /* FINIHSME: This cannot hold values for constant arrays or structures. */
86 ir_constant *value;
87};
88
89
90ir_constant *
91ir_instruction::constant_expression_value()
92{
93 ir_constant_visitor visitor;
94
95 this->accept(& visitor);
96 return visitor.value;
97}
98
99
100void
101ir_constant_visitor::visit(ir_variable *ir)
102{
103 (void) ir;
104 value = NULL;
105}
106
107
108void
109ir_constant_visitor::visit(ir_label *ir)
110{
111 (void) ir;
112 value = NULL;
113}
114
115
116void
117ir_constant_visitor::visit(ir_function_signature *ir)
118{
119 (void) ir;
120 value = NULL;
121}
122
123
124void
125ir_constant_visitor::visit(ir_function *ir)
126{
127 (void) ir;
128 value = NULL;
129}
130
Ian Romanick1cf43a42010-03-30 16:56:50 -0700131void
132ir_constant_visitor::visit(ir_expression *ir)
133{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700134 value = NULL;
Eric Anholta576f9d2010-03-31 16:25:12 -1000135 ir_constant *op[2];
Eric Anholtd98da972010-04-01 18:25:11 -1000136 unsigned int operand, c;
137 unsigned u[16];
138 int i[16];
139 float f[16];
140 bool b[16];
141 const glsl_type *type = NULL;
Eric Anholt160d0922010-04-01 18:07:08 -1000142
Eric Anholtd98da972010-04-01 18:25:11 -1000143 for (operand = 0; operand < ir->get_num_operands(); operand++) {
144 op[operand] = ir->operands[operand]->constant_expression_value();
145 if (!op[operand])
Eric Anholt160d0922010-04-01 18:07:08 -1000146 return;
147 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000148
149 switch (ir->operation) {
Eric Anholt528bb852010-03-31 21:09:02 -1000150 case ir_unop_logic_not:
Eric Anholtd98da972010-04-01 18:25:11 -1000151 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];
Eric Anholt528bb852010-03-31 21:09:02 -1000155 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000156 case ir_binop_mul:
Eric Anholtd98da972010-04-01 18:25:11 -1000157 if (ir->operands[0]->type == ir->operands[1]->type &&
158 !ir->operands[0]->type->is_matrix()) {
159 type = ir->operands[0]->type;
160 for (c = 0; c < ir->operands[0]->type->components(); c++) {
161 switch (ir->operands[0]->type->base_type) {
162 case GLSL_TYPE_UINT:
163 u[c] = op[0]->value.u[c] * op[1]->value.u[c];
164 break;
165 case GLSL_TYPE_INT:
166 i[c] = op[0]->value.i[c] * op[1]->value.i[c];
167 break;
168 case GLSL_TYPE_FLOAT:
169 f[c] = op[0]->value.f[c] * op[1]->value.f[c];
170 break;
171 default:
172 assert(0);
173 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000174 }
175 }
176 break;
177 case ir_binop_logic_and:
Eric Anholtd98da972010-04-01 18:25:11 -1000178 type = ir->operands[0]->type;
179 assert(type->base_type == GLSL_TYPE_BOOL);
180 for (c = 0; c < ir->operands[0]->type->components(); c++)
181 b[c] = op[0]->value.b[c] && op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000182 break;
183 case ir_binop_logic_or:
Eric Anholtd98da972010-04-01 18:25:11 -1000184 type = ir->operands[0]->type;
185 assert(type->base_type == GLSL_TYPE_BOOL);
186 for (c = 0; c < ir->operands[0]->type->components(); c++)
187 b[c] = op[0]->value.b[c] || op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000188 break;
189 default:
190 break;
191 }
Eric Anholtd98da972010-04-01 18:25:11 -1000192
193 if (type) {
194 switch (type->base_type) {
195 case GLSL_TYPE_UINT:
196 value = new ir_constant(type, u);
197 break;
198 case GLSL_TYPE_INT:
199 value = new ir_constant(type, i);
200 break;
201 case GLSL_TYPE_FLOAT:
202 value = new ir_constant(type, f);
203 break;
204 case GLSL_TYPE_BOOL:
205 value = new ir_constant(type, b);
206 break;
207 }
208 }
Ian Romanick1cf43a42010-03-30 16:56:50 -0700209}
210
211
212void
213ir_constant_visitor::visit(ir_swizzle *ir)
214{
215 (void) ir;
216 value = NULL;
217}
218
219
220void
221ir_constant_visitor::visit(ir_dereference *ir)
222{
223 (void) ir;
224 value = NULL;
225}
226
227
228void
229ir_constant_visitor::visit(ir_assignment *ir)
230{
231 (void) ir;
232 value = NULL;
233}
234
235
236void
237ir_constant_visitor::visit(ir_constant *ir)
238{
239 value = ir;
240}
241
242
243void
244ir_constant_visitor::visit(ir_call *ir)
245{
246 (void) ir;
247 value = NULL;
248}
249
250
251void
252ir_constant_visitor::visit(ir_return *ir)
253{
254 (void) ir;
255 value = NULL;
256}
257
258
259void
260ir_constant_visitor::visit(ir_if *ir)
261{
262 (void) ir;
263 value = NULL;
264}