ir_constant: Support constant structures in clone
[mesa.git] / ir_constant_expression.cpp
1 /*
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 <math.h>
38 #include "ir.h"
39 #include "ir_visitor.h"
40 #include "glsl_types.h"
41
42 /**
43 * Visitor class for evaluating constant expressions
44 */
45 class ir_constant_visitor : public ir_visitor {
46 public:
47 ir_constant_visitor()
48 : value(NULL)
49 {
50 /* empty */
51 }
52
53 virtual ~ir_constant_visitor()
54 {
55 /* empty */
56 }
57
58 /**
59 * \name Visit methods
60 *
61 * As typical for the visitor pattern, there must be one \c visit method for
62 * each concrete subclass of \c ir_instruction. Virtual base classes within
63 * the hierarchy should not have \c visit methods.
64 */
65 /*@{*/
66 virtual void visit(ir_variable *);
67 virtual void visit(ir_function_signature *);
68 virtual void visit(ir_function *);
69 virtual void visit(ir_expression *);
70 virtual void visit(ir_texture *);
71 virtual void visit(ir_swizzle *);
72 virtual void visit(ir_dereference_variable *);
73 virtual void visit(ir_dereference_array *);
74 virtual void visit(ir_dereference_record *);
75 virtual void visit(ir_assignment *);
76 virtual void visit(ir_constant *);
77 virtual void visit(ir_call *);
78 virtual void visit(ir_return *);
79 virtual void visit(ir_if *);
80 virtual void visit(ir_loop *);
81 virtual void visit(ir_loop_jump *);
82 /*@}*/
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
95 ir_constant *
96 ir_instruction::constant_expression_value()
97 {
98 ir_constant_visitor visitor;
99
100 this->accept(& visitor);
101 return visitor.value;
102 }
103
104
105 void
106 ir_constant_visitor::visit(ir_variable *ir)
107 {
108 (void) ir;
109 value = NULL;
110 }
111
112
113 void
114 ir_constant_visitor::visit(ir_function_signature *ir)
115 {
116 (void) ir;
117 value = NULL;
118 }
119
120
121 void
122 ir_constant_visitor::visit(ir_function *ir)
123 {
124 (void) ir;
125 value = NULL;
126 }
127
128 void
129 ir_constant_visitor::visit(ir_expression *ir)
130 {
131 value = NULL;
132 ir_constant *op[2];
133 unsigned int operand, c;
134 unsigned u[16];
135 int i[16];
136 float f[16];
137 bool b[16];
138 const glsl_type *type = NULL;
139
140 for (operand = 0; operand < ir->get_num_operands(); operand++) {
141 op[operand] = ir->operands[operand]->constant_expression_value();
142 if (!op[operand])
143 return;
144 }
145
146 switch (ir->operation) {
147 case ir_unop_logic_not:
148 type = ir->operands[0]->type;
149 assert(type->base_type == GLSL_TYPE_BOOL);
150 for (c = 0; c < ir->operands[0]->type->components(); c++)
151 b[c] = !op[0]->value.b[c];
152 break;
153
154 case ir_unop_f2i:
155 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
156 type = ir->type;
157 for (c = 0; c < ir->operands[0]->type->components(); c++) {
158 i[c] = op[0]->value.f[c];
159 }
160 break;
161 case ir_unop_i2f:
162 assert(op[0]->type->base_type == GLSL_TYPE_UINT ||
163 op[0]->type->base_type == GLSL_TYPE_INT);
164 type = ir->type;
165 for (c = 0; c < ir->operands[0]->type->components(); c++) {
166 if (op[0]->type->base_type == GLSL_TYPE_INT)
167 f[c] = op[0]->value.i[c];
168 else
169 f[c] = op[0]->value.u[c];
170 }
171 break;
172 case ir_unop_b2f:
173 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
174 type = ir->type;
175 for (c = 0; c < ir->operands[0]->type->components(); c++) {
176 f[c] = op[0]->value.b[c] ? 1.0 : 0.0;
177 }
178 break;
179 case ir_unop_f2b:
180 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
181 type = ir->type;
182 for (c = 0; c < ir->operands[0]->type->components(); c++) {
183 b[c] = bool(op[0]->value.f[c]);
184 }
185 break;
186
187 case ir_unop_neg:
188 type = ir->type;
189 for (c = 0; c < ir->operands[0]->type->components(); c++) {
190 switch (type->base_type) {
191 case GLSL_TYPE_UINT:
192 u[c] = -op[0]->value.u[c];
193 break;
194 case GLSL_TYPE_INT:
195 i[c] = -op[0]->value.i[c];
196 break;
197 case GLSL_TYPE_FLOAT:
198 f[c] = -op[0]->value.f[c];
199 break;
200 default:
201 assert(0);
202 }
203 }
204 break;
205
206 case ir_unop_abs:
207 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
208 type = ir->type;
209 for (c = 0; c < ir->operands[0]->type->components(); c++) {
210 switch (type->base_type) {
211 case GLSL_TYPE_UINT:
212 u[c] = op[0]->value.u[c];
213 break;
214 case GLSL_TYPE_INT:
215 i[c] = op[0]->value.i[c];
216 if (i[c] < 0)
217 i[c] = -i[c];
218 break;
219 case GLSL_TYPE_FLOAT:
220 f[c] = fabs(op[0]->value.f[c]);
221 break;
222 default:
223 assert(0);
224 }
225 }
226 break;
227
228 case ir_unop_rcp:
229 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
230 type = ir->type;
231 for (c = 0; c < ir->operands[0]->type->components(); c++) {
232 switch (type->base_type) {
233 case GLSL_TYPE_UINT:
234 if (op[0]->value.u[c] != 0.0)
235 u[c] = 1 / op[0]->value.u[c];
236 break;
237 case GLSL_TYPE_INT:
238 if (op[0]->value.i[c] != 0.0)
239 i[c] = 1 / op[0]->value.i[c];
240 break;
241 case GLSL_TYPE_FLOAT:
242 if (op[0]->value.f[c] != 0.0)
243 f[c] = 1.0 / op[0]->value.f[c];
244 break;
245 default:
246 assert(0);
247 }
248 }
249 break;
250
251 case ir_unop_rsq:
252 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
253 type = ir->type;
254 for (c = 0; c < ir->operands[0]->type->components(); c++) {
255 f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
256 }
257 break;
258
259 case ir_unop_sqrt:
260 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
261 type = ir->type;
262 for (c = 0; c < ir->operands[0]->type->components(); c++) {
263 f[c] = sqrtf(op[0]->value.f[c]);
264 }
265 break;
266
267 case ir_unop_exp:
268 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
269 type = ir->type;
270 for (c = 0; c < ir->operands[0]->type->components(); c++) {
271 f[c] = expf(op[0]->value.f[c]);
272 }
273 break;
274
275 case ir_unop_log:
276 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
277 type = ir->type;
278 for (c = 0; c < ir->operands[0]->type->components(); c++) {
279 f[c] = logf(op[0]->value.f[c]);
280 }
281 break;
282
283 case ir_unop_dFdx:
284 case ir_unop_dFdy:
285 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
286 type = ir->type;
287 for (c = 0; c < ir->operands[0]->type->components(); c++) {
288 f[c] = 0.0;
289 }
290 break;
291
292 case ir_binop_add:
293 if (ir->operands[0]->type == ir->operands[1]->type) {
294 type = ir->operands[0]->type;
295 for (c = 0; c < ir->operands[0]->type->components(); c++) {
296 switch (ir->operands[0]->type->base_type) {
297 case GLSL_TYPE_UINT:
298 u[c] = op[0]->value.u[c] + op[1]->value.u[c];
299 break;
300 case GLSL_TYPE_INT:
301 i[c] = op[0]->value.i[c] + op[1]->value.i[c];
302 break;
303 case GLSL_TYPE_FLOAT:
304 f[c] = op[0]->value.f[c] + op[1]->value.f[c];
305 break;
306 default:
307 assert(0);
308 }
309 }
310 }
311 break;
312 case ir_binop_sub:
313 if (ir->operands[0]->type == ir->operands[1]->type) {
314 type = ir->operands[0]->type;
315 for (c = 0; c < ir->operands[0]->type->components(); c++) {
316 switch (ir->operands[0]->type->base_type) {
317 case GLSL_TYPE_UINT:
318 u[c] = op[0]->value.u[c] - op[1]->value.u[c];
319 break;
320 case GLSL_TYPE_INT:
321 i[c] = op[0]->value.i[c] - op[1]->value.i[c];
322 break;
323 case GLSL_TYPE_FLOAT:
324 f[c] = op[0]->value.f[c] - op[1]->value.f[c];
325 break;
326 default:
327 assert(0);
328 }
329 }
330 }
331 break;
332 case ir_binop_mul:
333 if (ir->operands[0]->type == ir->operands[1]->type &&
334 !ir->operands[0]->type->is_matrix()) {
335 type = ir->operands[0]->type;
336 for (c = 0; c < ir->operands[0]->type->components(); c++) {
337 switch (ir->operands[0]->type->base_type) {
338 case GLSL_TYPE_UINT:
339 u[c] = op[0]->value.u[c] * op[1]->value.u[c];
340 break;
341 case GLSL_TYPE_INT:
342 i[c] = op[0]->value.i[c] * op[1]->value.i[c];
343 break;
344 case GLSL_TYPE_FLOAT:
345 f[c] = op[0]->value.f[c] * op[1]->value.f[c];
346 break;
347 default:
348 assert(0);
349 }
350 }
351 }
352 break;
353 case ir_binop_div:
354 if (ir->operands[0]->type == ir->operands[1]->type) {
355 type = ir->operands[0]->type;
356 for (c = 0; c < ir->operands[0]->type->components(); c++) {
357 switch (ir->operands[0]->type->base_type) {
358 case GLSL_TYPE_UINT:
359 u[c] = op[0]->value.u[c] / op[1]->value.u[c];
360 break;
361 case GLSL_TYPE_INT:
362 i[c] = op[0]->value.i[c] / op[1]->value.i[c];
363 break;
364 case GLSL_TYPE_FLOAT:
365 f[c] = op[0]->value.f[c] / op[1]->value.f[c];
366 break;
367 default:
368 assert(0);
369 }
370 }
371 }
372 break;
373 case ir_binop_logic_and:
374 type = ir->operands[0]->type;
375 assert(type->base_type == GLSL_TYPE_BOOL);
376 for (c = 0; c < ir->operands[0]->type->components(); c++)
377 b[c] = op[0]->value.b[c] && op[1]->value.b[c];
378 break;
379 case ir_binop_logic_xor:
380 type = ir->operands[0]->type;
381 assert(type->base_type == GLSL_TYPE_BOOL);
382 for (c = 0; c < ir->operands[0]->type->components(); c++)
383 b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
384 break;
385 case ir_binop_logic_or:
386 type = ir->operands[0]->type;
387 assert(type->base_type == GLSL_TYPE_BOOL);
388 for (c = 0; c < ir->operands[0]->type->components(); c++)
389 b[c] = op[0]->value.b[c] || op[1]->value.b[c];
390 break;
391
392 case ir_binop_less:
393 type = glsl_type::bool_type;
394 switch (ir->operands[0]->type->base_type) {
395 case GLSL_TYPE_UINT:
396 b[0] = op[0]->value.u[0] < op[1]->value.u[0];
397 break;
398 case GLSL_TYPE_INT:
399 b[0] = op[0]->value.i[0] < op[1]->value.i[0];
400 break;
401 case GLSL_TYPE_FLOAT:
402 b[0] = op[0]->value.f[0] < op[1]->value.f[0];
403 break;
404 default:
405 assert(0);
406 }
407 break;
408 case ir_binop_greater:
409 type = glsl_type::bool_type;
410 switch (ir->operands[0]->type->base_type) {
411 case GLSL_TYPE_UINT:
412 b[0] = op[0]->value.u[0] > op[1]->value.u[0];
413 break;
414 case GLSL_TYPE_INT:
415 b[0] = op[0]->value.i[0] > op[1]->value.i[0];
416 break;
417 case GLSL_TYPE_FLOAT:
418 b[0] = op[0]->value.f[0] > op[1]->value.f[0];
419 break;
420 default:
421 assert(0);
422 }
423 break;
424 case ir_binop_lequal:
425 type = glsl_type::bool_type;
426 switch (ir->operands[0]->type->base_type) {
427 case GLSL_TYPE_UINT:
428 b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
429 break;
430 case GLSL_TYPE_INT:
431 b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
432 break;
433 case GLSL_TYPE_FLOAT:
434 b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
435 break;
436 default:
437 assert(0);
438 }
439 break;
440 case ir_binop_gequal:
441 type = glsl_type::bool_type;
442 switch (ir->operands[0]->type->base_type) {
443 case GLSL_TYPE_UINT:
444 b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
445 break;
446 case GLSL_TYPE_INT:
447 b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
448 break;
449 case GLSL_TYPE_FLOAT:
450 b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
451 break;
452 default:
453 assert(0);
454 }
455 break;
456
457 case ir_binop_equal:
458 if (ir->operands[0]->type == ir->operands[1]->type) {
459 type = glsl_type::bool_type;
460 b[0] = true;
461 for (c = 0; c < ir->operands[0]->type->components(); c++) {
462 switch (ir->operands[0]->type->base_type) {
463 case GLSL_TYPE_UINT:
464 b[0] = b[0] && op[0]->value.u[c] == op[1]->value.u[c];
465 break;
466 case GLSL_TYPE_INT:
467 b[0] = b[0] && op[0]->value.i[c] == op[1]->value.i[c];
468 break;
469 case GLSL_TYPE_FLOAT:
470 b[0] = b[0] && op[0]->value.f[c] == op[1]->value.f[c];
471 break;
472 case GLSL_TYPE_BOOL:
473 b[0] = b[0] && op[0]->value.b[c] == op[1]->value.b[c];
474 break;
475 default:
476 assert(0);
477 }
478 }
479 }
480 break;
481 case ir_binop_nequal:
482 if (ir->operands[0]->type == ir->operands[1]->type) {
483 type = glsl_type::bool_type;
484 b[0] = false;
485 for (c = 0; c < ir->operands[0]->type->components(); c++) {
486 switch (ir->operands[0]->type->base_type) {
487 case GLSL_TYPE_UINT:
488 b[0] = b[0] || op[0]->value.u[c] != op[1]->value.u[c];
489 break;
490 case GLSL_TYPE_INT:
491 b[0] = b[0] || op[0]->value.i[c] != op[1]->value.i[c];
492 break;
493 case GLSL_TYPE_FLOAT:
494 b[0] = b[0] || op[0]->value.f[c] != op[1]->value.f[c];
495 break;
496 case GLSL_TYPE_BOOL:
497 b[0] = b[0] || op[0]->value.b[c] != op[1]->value.b[c];
498 break;
499 default:
500 assert(0);
501 }
502 }
503 }
504 break;
505
506 default:
507 break;
508 }
509
510 if (type) {
511 switch (type->base_type) {
512 case GLSL_TYPE_UINT:
513 value = new ir_constant(type, u);
514 break;
515 case GLSL_TYPE_INT:
516 value = new ir_constant(type, i);
517 break;
518 case GLSL_TYPE_FLOAT:
519 value = new ir_constant(type, f);
520 break;
521 case GLSL_TYPE_BOOL:
522 value = new ir_constant(type, b);
523 break;
524 }
525 }
526 }
527
528
529 void
530 ir_constant_visitor::visit(ir_texture *ir)
531 {
532 // FINISHME: Do stuff with texture lookups
533 (void) ir;
534 value = NULL;
535 }
536
537
538 void
539 ir_constant_visitor::visit(ir_swizzle *ir)
540 {
541 (void) ir;
542 value = NULL;
543 }
544
545
546 void
547 ir_constant_visitor::visit(ir_dereference_variable *ir)
548 {
549 value = NULL;
550
551 ir_variable *var = ir->variable_referenced();
552 if (var && var->constant_value)
553 value = new ir_constant(ir->type, &var->constant_value->value);
554 }
555
556
557 void
558 ir_constant_visitor::visit(ir_dereference_array *ir)
559 {
560 (void) ir;
561 value = NULL;
562 /* FINISHME: Other dereference modes. */
563 }
564
565
566 void
567 ir_constant_visitor::visit(ir_dereference_record *ir)
568 {
569 (void) ir;
570 value = NULL;
571 /* FINISHME: Other dereference modes. */
572 }
573
574
575 void
576 ir_constant_visitor::visit(ir_assignment *ir)
577 {
578 (void) ir;
579 value = NULL;
580 }
581
582
583 void
584 ir_constant_visitor::visit(ir_constant *ir)
585 {
586 value = ir;
587 }
588
589
590 void
591 ir_constant_visitor::visit(ir_call *ir)
592 {
593 (void) ir;
594 value = NULL;
595 }
596
597
598 void
599 ir_constant_visitor::visit(ir_return *ir)
600 {
601 (void) ir;
602 value = NULL;
603 }
604
605
606 void
607 ir_constant_visitor::visit(ir_if *ir)
608 {
609 (void) ir;
610 value = NULL;
611 }
612
613
614 void
615 ir_constant_visitor::visit(ir_loop *ir)
616 {
617 (void) ir;
618 value = NULL;
619 }
620
621
622 void
623 ir_constant_visitor::visit(ir_loop_jump *ir)
624 {
625 (void) ir;
626 value = NULL;
627 }