Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / glsl / ir.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 #include <string.h>
24 #include "main/core.h" /* for MAX2 */
25 #include "ir.h"
26 #include "ir_visitor.h"
27 #include "glsl_types.h"
28
29 ir_rvalue::ir_rvalue()
30 {
31    this->type = glsl_type::error_type;
32 }
33
34 bool ir_rvalue::is_zero() const
35 {
36    return false;
37 }
38
39 bool ir_rvalue::is_one() const
40 {
41    return false;
42 }
43
44 bool ir_rvalue::is_negative_one() const
45 {
46    return false;
47 }
48
49 /**
50  * Modify the swizzle make to move one component to another
51  *
52  * \param m    IR swizzle to be modified
53  * \param from Component in the RHS that is to be swizzled
54  * \param to   Desired swizzle location of \c from
55  */
56 static void
57 update_rhs_swizzle(ir_swizzle_mask &m, unsigned from, unsigned to)
58 {
59    switch (to) {
60    case 0: m.x = from; break;
61    case 1: m.y = from; break;
62    case 2: m.z = from; break;
63    case 3: m.w = from; break;
64    default: assert(!"Should not get here.");
65    }
66
67    m.num_components = MAX2(m.num_components, (to + 1));
68 }
69
70 void
71 ir_assignment::set_lhs(ir_rvalue *lhs)
72 {
73    void *mem_ctx = this;
74    bool swizzled = false;
75
76    while (lhs != NULL) {
77       ir_swizzle *swiz = lhs->as_swizzle();
78
79       if (swiz == NULL)
80          break;
81
82       unsigned write_mask = 0;
83       ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
84
85       for (unsigned i = 0; i < swiz->mask.num_components; i++) {
86          unsigned c = 0;
87
88          switch (i) {
89          case 0: c = swiz->mask.x; break;
90          case 1: c = swiz->mask.y; break;
91          case 2: c = swiz->mask.z; break;
92          case 3: c = swiz->mask.w; break;
93          default: assert(!"Should not get here.");
94          }
95
96          write_mask |= (((this->write_mask >> i) & 1) << c);
97          update_rhs_swizzle(rhs_swiz, i, c);
98       }
99
100       this->write_mask = write_mask;
101       lhs = swiz->val;
102
103       this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
104       swizzled = true;
105    }
106
107    if (swizzled) {
108       /* Now, RHS channels line up with the LHS writemask.  Collapse it
109        * to just the channels that will be written.
110        */
111       ir_swizzle_mask rhs_swiz = { 0, 0, 0, 0, 0, 0 };
112       int rhs_chan = 0;
113       for (int i = 0; i < 4; i++) {
114          if (write_mask & (1 << i))
115             update_rhs_swizzle(rhs_swiz, i, rhs_chan++);
116       }
117       this->rhs = new(mem_ctx) ir_swizzle(this->rhs, rhs_swiz);
118    }
119
120    assert((lhs == NULL) || lhs->as_dereference());
121
122    this->lhs = (ir_dereference *) lhs;
123 }
124
125 ir_variable *
126 ir_assignment::whole_variable_written()
127 {
128    ir_variable *v = this->lhs->whole_variable_referenced();
129
130    if (v == NULL)
131       return NULL;
132
133    if (v->type->is_scalar())
134       return v;
135
136    if (v->type->is_vector()) {
137       const unsigned mask = (1U << v->type->vector_elements) - 1;
138
139       if (mask != this->write_mask)
140          return NULL;
141    }
142
143    /* Either all the vector components are assigned or the variable is some
144     * composite type (and the whole thing is assigned.
145     */
146    return v;
147 }
148
149 ir_assignment::ir_assignment(ir_dereference *lhs, ir_rvalue *rhs,
150                              ir_rvalue *condition, unsigned write_mask)
151 {
152    this->ir_type = ir_type_assignment;
153    this->condition = condition;
154    this->rhs = rhs;
155    this->lhs = lhs;
156    this->write_mask = write_mask;
157
158    if (lhs->type->is_scalar() || lhs->type->is_vector()) {
159       int lhs_components = 0;
160       for (int i = 0; i < 4; i++) {
161          if (write_mask & (1 << i))
162             lhs_components++;
163       }
164
165       assert(lhs_components == this->rhs->type->vector_elements);
166    }
167 }
168
169 ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs,
170                              ir_rvalue *condition)
171 {
172    this->ir_type = ir_type_assignment;
173    this->condition = condition;
174    this->rhs = rhs;
175
176    /* If the RHS is a vector type, assume that all components of the vector
177     * type are being written to the LHS.  The write mask comes from the RHS
178     * because we can have a case where the LHS is a vec4 and the RHS is a
179     * vec3.  In that case, the assignment is:
180     *
181     *     (assign (...) (xyz) (var_ref lhs) (var_ref rhs))
182     */
183    if (rhs->type->is_vector())
184       this->write_mask = (1U << rhs->type->vector_elements) - 1;
185    else if (rhs->type->is_scalar())
186       this->write_mask = 1;
187    else
188       this->write_mask = 0;
189
190    this->set_lhs(lhs);
191 }
192
193
194 ir_expression::ir_expression(int op, const struct glsl_type *type,
195                              ir_rvalue *op0)
196 {
197    assert(get_num_operands(ir_expression_operation(op)) == 1);
198    this->ir_type = ir_type_expression;
199    this->type = type;
200    this->operation = ir_expression_operation(op);
201    this->operands[0] = op0;
202    this->operands[1] = NULL;
203    this->operands[2] = NULL;
204    this->operands[3] = NULL;
205 }
206
207 ir_expression::ir_expression(int op, const struct glsl_type *type,
208                              ir_rvalue *op0, ir_rvalue *op1)
209 {
210    assert(((op1 == NULL) && (get_num_operands(ir_expression_operation(op)) == 1))
211           || (get_num_operands(ir_expression_operation(op)) == 2));
212    this->ir_type = ir_type_expression;
213    this->type = type;
214    this->operation = ir_expression_operation(op);
215    this->operands[0] = op0;
216    this->operands[1] = op1;
217    this->operands[2] = NULL;
218    this->operands[3] = NULL;
219 }
220
221 ir_expression::ir_expression(int op, const struct glsl_type *type,
222                              ir_rvalue *op0, ir_rvalue *op1,
223                              ir_rvalue *op2, ir_rvalue *op3)
224 {
225    this->ir_type = ir_type_expression;
226    this->type = type;
227    this->operation = ir_expression_operation(op);
228    this->operands[0] = op0;
229    this->operands[1] = op1;
230    this->operands[2] = op2;
231    this->operands[3] = op3;
232 }
233
234 ir_expression::ir_expression(int op, ir_rvalue *op0)
235 {
236    this->ir_type = ir_type_expression;
237
238    this->operation = ir_expression_operation(op);
239    this->operands[0] = op0;
240    this->operands[1] = NULL;
241    this->operands[2] = NULL;
242    this->operands[3] = NULL;
243
244    assert(op <= ir_last_unop);
245
246    switch (this->operation) {
247    case ir_unop_bit_not:
248    case ir_unop_logic_not:
249    case ir_unop_neg:
250    case ir_unop_abs:
251    case ir_unop_sign:
252    case ir_unop_rcp:
253    case ir_unop_rsq:
254    case ir_unop_sqrt:
255    case ir_unop_exp:
256    case ir_unop_log:
257    case ir_unop_exp2:
258    case ir_unop_log2:
259    case ir_unop_trunc:
260    case ir_unop_ceil:
261    case ir_unop_floor:
262    case ir_unop_fract:
263    case ir_unop_round_even:
264    case ir_unop_sin:
265    case ir_unop_cos:
266    case ir_unop_sin_reduced:
267    case ir_unop_cos_reduced:
268    case ir_unop_dFdx:
269    case ir_unop_dFdy:
270       this->type = op0->type;
271       break;
272
273    case ir_unop_f2i:
274    case ir_unop_b2i:
275       this->type = glsl_type::get_instance(GLSL_TYPE_INT,
276                                            op0->type->vector_elements, 1);
277       break;
278
279    case ir_unop_b2f:
280    case ir_unop_i2f:
281    case ir_unop_u2f:
282       this->type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
283                                            op0->type->vector_elements, 1);
284       break;
285
286    case ir_unop_f2b:
287    case ir_unop_i2b:
288       this->type = glsl_type::get_instance(GLSL_TYPE_BOOL,
289                                            op0->type->vector_elements, 1);
290       break;
291
292    case ir_unop_noise:
293       this->type = glsl_type::float_type;
294       break;
295
296    case ir_unop_any:
297       this->type = glsl_type::bool_type;
298       break;
299
300    default:
301       assert(!"not reached: missing automatic type setup for ir_expression");
302       this->type = op0->type;
303       break;
304    }
305 }
306
307 ir_expression::ir_expression(int op, ir_rvalue *op0, ir_rvalue *op1)
308 {
309    this->ir_type = ir_type_expression;
310
311    this->operation = ir_expression_operation(op);
312    this->operands[0] = op0;
313    this->operands[1] = op1;
314    this->operands[2] = NULL;
315    this->operands[3] = NULL;
316
317    assert(op > ir_last_unop);
318
319    switch (this->operation) {
320    case ir_binop_all_equal:
321    case ir_binop_any_nequal:
322       this->type = glsl_type::bool_type;
323       break;
324
325    case ir_binop_add:
326    case ir_binop_sub:
327    case ir_binop_min:
328    case ir_binop_max:
329    case ir_binop_pow:
330    case ir_binop_mul:
331    case ir_binop_div:
332    case ir_binop_mod:
333       if (op0->type->is_scalar()) {
334          this->type = op1->type;
335       } else if (op1->type->is_scalar()) {
336          this->type = op0->type;
337       } else {
338          /* FINISHME: matrix types */
339          assert(!op0->type->is_matrix() && !op1->type->is_matrix());
340          assert(op0->type == op1->type);
341          this->type = op0->type;
342       }
343       break;
344
345    case ir_binop_logic_and:
346    case ir_binop_logic_xor:
347    case ir_binop_logic_or:
348    case ir_binop_bit_and:
349    case ir_binop_bit_xor:
350    case ir_binop_bit_or:
351       if (op0->type->is_scalar()) {
352          this->type = op1->type;
353       } else if (op1->type->is_scalar()) {
354          this->type = op0->type;
355       }
356       break;
357
358    case ir_binop_equal:
359    case ir_binop_nequal:
360    case ir_binop_lequal:
361    case ir_binop_gequal:
362    case ir_binop_less:
363    case ir_binop_greater:
364       assert(op0->type == op1->type);
365       this->type = glsl_type::get_instance(GLSL_TYPE_BOOL,
366                                            op0->type->vector_elements, 1);
367       break;
368
369    case ir_binop_dot:
370       this->type = glsl_type::float_type;
371       break;
372
373    case ir_binop_lshift:
374    case ir_binop_rshift:
375       this->type = op0->type;
376       break;
377
378    default:
379       assert(!"not reached: missing automatic type setup for ir_expression");
380       this->type = glsl_type::float_type;
381    }
382 }
383
384 unsigned int
385 ir_expression::get_num_operands(ir_expression_operation op)
386 {
387    assert(op <= ir_last_opcode);
388
389    if (op <= ir_last_unop)
390       return 1;
391
392    if (op <= ir_last_binop)
393       return 2;
394
395    if (op == ir_quadop_vector)
396       return 4;
397
398    assert(false);
399    return 0;
400 }
401
402 static const char *const operator_strs[] = {
403    "~",
404    "!",
405    "neg",
406    "abs",
407    "sign",
408    "rcp",
409    "rsq",
410    "sqrt",
411    "exp",
412    "log",
413    "exp2",
414    "log2",
415    "f2i",
416    "i2f",
417    "f2b",
418    "b2f",
419    "i2b",
420    "b2i",
421    "u2f",
422    "any",
423    "trunc",
424    "ceil",
425    "floor",
426    "fract",
427    "round_even",
428    "sin",
429    "cos",
430    "sin_reduced",
431    "cos_reduced",
432    "dFdx",
433    "dFdy",
434    "noise",
435    "+",
436    "-",
437    "*",
438    "/",
439    "%",
440    "<",
441    ">",
442    "<=",
443    ">=",
444    "==",
445    "!=",
446    "all_equal",
447    "any_nequal",
448    "<<",
449    ">>",
450    "&",
451    "^",
452    "|",
453    "&&",
454    "^^",
455    "||",
456    "dot",
457    "min",
458    "max",
459    "pow",
460    "vector",
461 };
462
463 const char *ir_expression::operator_string(ir_expression_operation op)
464 {
465    assert((unsigned int) op < Elements(operator_strs));
466    assert(Elements(operator_strs) == (ir_quadop_vector + 1));
467    return operator_strs[op];
468 }
469
470 const char *ir_expression::operator_string()
471 {
472    return operator_string(this->operation);
473 }
474
475 const char*
476 depth_layout_string(ir_depth_layout layout)
477 {
478    switch(layout) {
479    case ir_depth_layout_none:      return "";
480    case ir_depth_layout_any:       return "depth_any";
481    case ir_depth_layout_greater:   return "depth_greater";
482    case ir_depth_layout_less:      return "depth_less";
483    case ir_depth_layout_unchanged: return "depth_unchanged";
484
485    default:
486       assert(0);
487       return "";
488    }
489 }
490
491 ir_expression_operation
492 ir_expression::get_operator(const char *str)
493 {
494    const int operator_count = sizeof(operator_strs) / sizeof(operator_strs[0]);
495    for (int op = 0; op < operator_count; op++) {
496       if (strcmp(str, operator_strs[op]) == 0)
497          return (ir_expression_operation) op;
498    }
499    return (ir_expression_operation) -1;
500 }
501
502 ir_constant::ir_constant()
503 {
504    this->ir_type = ir_type_constant;
505 }
506
507 ir_constant::ir_constant(const struct glsl_type *type,
508                          const ir_constant_data *data)
509 {
510    assert((type->base_type >= GLSL_TYPE_UINT)
511           && (type->base_type <= GLSL_TYPE_BOOL));
512
513    this->ir_type = ir_type_constant;
514    this->type = type;
515    memcpy(& this->value, data, sizeof(this->value));
516 }
517
518 ir_constant::ir_constant(float f)
519 {
520    this->ir_type = ir_type_constant;
521    this->type = glsl_type::float_type;
522    this->value.f[0] = f;
523    for (int i = 1; i < 16; i++)  {
524       this->value.f[i] = 0;
525    }
526 }
527
528 ir_constant::ir_constant(unsigned int u)
529 {
530    this->ir_type = ir_type_constant;
531    this->type = glsl_type::uint_type;
532    this->value.u[0] = u;
533    for (int i = 1; i < 16; i++) {
534       this->value.u[i] = 0;
535    }
536 }
537
538 ir_constant::ir_constant(int i)
539 {
540    this->ir_type = ir_type_constant;
541    this->type = glsl_type::int_type;
542    this->value.i[0] = i;
543    for (int i = 1; i < 16; i++) {
544       this->value.i[i] = 0;
545    }
546 }
547
548 ir_constant::ir_constant(bool b)
549 {
550    this->ir_type = ir_type_constant;
551    this->type = glsl_type::bool_type;
552    this->value.b[0] = b;
553    for (int i = 1; i < 16; i++) {
554       this->value.b[i] = false;
555    }
556 }
557
558 ir_constant::ir_constant(const ir_constant *c, unsigned i)
559 {
560    this->ir_type = ir_type_constant;
561    this->type = c->type->get_base_type();
562
563    switch (this->type->base_type) {
564    case GLSL_TYPE_UINT:  this->value.u[0] = c->value.u[i]; break;
565    case GLSL_TYPE_INT:   this->value.i[0] = c->value.i[i]; break;
566    case GLSL_TYPE_FLOAT: this->value.f[0] = c->value.f[i]; break;
567    case GLSL_TYPE_BOOL:  this->value.b[0] = c->value.b[i]; break;
568    default:              assert(!"Should not get here."); break;
569    }
570 }
571
572 ir_constant::ir_constant(const struct glsl_type *type, exec_list *value_list)
573 {
574    this->ir_type = ir_type_constant;
575    this->type = type;
576
577    assert(type->is_scalar() || type->is_vector() || type->is_matrix()
578           || type->is_record() || type->is_array());
579
580    if (type->is_array()) {
581       this->array_elements = ralloc_array(this, ir_constant *, type->length);
582       unsigned i = 0;
583       foreach_list(node, value_list) {
584          ir_constant *value = (ir_constant *) node;
585          assert(value->as_constant() != NULL);
586
587          this->array_elements[i++] = value;
588       }
589       return;
590    }
591
592    /* If the constant is a record, the types of each of the entries in
593     * value_list must be a 1-for-1 match with the structure components.  Each
594     * entry must also be a constant.  Just move the nodes from the value_list
595     * to the list in the ir_constant.
596     */
597    /* FINISHME: Should there be some type checking and / or assertions here? */
598    /* FINISHME: Should the new constant take ownership of the nodes from
599     * FINISHME: value_list, or should it make copies?
600     */
601    if (type->is_record()) {
602       value_list->move_nodes_to(& this->components);
603       return;
604    }
605
606    for (unsigned i = 0; i < 16; i++) {
607       this->value.u[i] = 0;
608    }
609
610    ir_constant *value = (ir_constant *) (value_list->head);
611
612    /* Constructors with exactly one scalar argument are special for vectors
613     * and matrices.  For vectors, the scalar value is replicated to fill all
614     * the components.  For matrices, the scalar fills the components of the
615     * diagonal while the rest is filled with 0.
616     */
617    if (value->type->is_scalar() && value->next->is_tail_sentinel()) {
618       if (type->is_matrix()) {
619          /* Matrix - fill diagonal (rest is already set to 0) */
620          assert(type->base_type == GLSL_TYPE_FLOAT);
621          for (unsigned i = 0; i < type->matrix_columns; i++)
622             this->value.f[i * type->vector_elements + i] = value->value.f[0];
623       } else {
624          /* Vector or scalar - fill all components */
625          switch (type->base_type) {
626          case GLSL_TYPE_UINT:
627          case GLSL_TYPE_INT:
628             for (unsigned i = 0; i < type->components(); i++)
629                this->value.u[i] = value->value.u[0];
630             break;
631          case GLSL_TYPE_FLOAT:
632             for (unsigned i = 0; i < type->components(); i++)
633                this->value.f[i] = value->value.f[0];
634             break;
635          case GLSL_TYPE_BOOL:
636             for (unsigned i = 0; i < type->components(); i++)
637                this->value.b[i] = value->value.b[0];
638             break;
639          default:
640             assert(!"Should not get here.");
641             break;
642          }
643       }
644       return;
645    }
646
647    if (type->is_matrix() && value->type->is_matrix()) {
648       assert(value->next->is_tail_sentinel());
649
650       /* From section 5.4.2 of the GLSL 1.20 spec:
651        * "If a matrix is constructed from a matrix, then each component
652        *  (column i, row j) in the result that has a corresponding component
653        *  (column i, row j) in the argument will be initialized from there."
654        */
655       unsigned cols = MIN2(type->matrix_columns, value->type->matrix_columns);
656       unsigned rows = MIN2(type->vector_elements, value->type->vector_elements);
657       for (unsigned i = 0; i < cols; i++) {
658          for (unsigned j = 0; j < rows; j++) {
659             const unsigned src = i * value->type->vector_elements + j;
660             const unsigned dst = i * type->vector_elements + j;
661             this->value.f[dst] = value->value.f[src];
662          }
663       }
664
665       /* "All other components will be initialized to the identity matrix." */
666       for (unsigned i = cols; i < type->matrix_columns; i++)
667          this->value.f[i * type->vector_elements + i] = 1.0;
668
669       return;
670    }
671
672    /* Use each component from each entry in the value_list to initialize one
673     * component of the constant being constructed.
674     */
675    for (unsigned i = 0; i < type->components(); /* empty */) {
676       assert(value->as_constant() != NULL);
677       assert(!value->is_tail_sentinel());
678
679       for (unsigned j = 0; j < value->type->components(); j++) {
680          switch (type->base_type) {
681          case GLSL_TYPE_UINT:
682             this->value.u[i] = value->get_uint_component(j);
683             break;
684          case GLSL_TYPE_INT:
685             this->value.i[i] = value->get_int_component(j);
686             break;
687          case GLSL_TYPE_FLOAT:
688             this->value.f[i] = value->get_float_component(j);
689             break;
690          case GLSL_TYPE_BOOL:
691             this->value.b[i] = value->get_bool_component(j);
692             break;
693          default:
694             /* FINISHME: What to do?  Exceptions are not the answer.
695              */
696             break;
697          }
698
699          i++;
700          if (i >= type->components())
701             break;
702       }
703
704       value = (ir_constant *) value->next;
705    }
706 }
707
708 ir_constant *
709 ir_constant::zero(void *mem_ctx, const glsl_type *type)
710 {
711    assert(type->is_numeric() || type->is_boolean());
712
713    ir_constant *c = new(mem_ctx) ir_constant;
714    c->type = type;
715    memset(&c->value, 0, sizeof(c->value));
716
717    return c;
718 }
719
720 bool
721 ir_constant::get_bool_component(unsigned i) const
722 {
723    switch (this->type->base_type) {
724    case GLSL_TYPE_UINT:  return this->value.u[i] != 0;
725    case GLSL_TYPE_INT:   return this->value.i[i] != 0;
726    case GLSL_TYPE_FLOAT: return ((int)this->value.f[i]) != 0;
727    case GLSL_TYPE_BOOL:  return this->value.b[i];
728    default:              assert(!"Should not get here."); break;
729    }
730
731    /* Must return something to make the compiler happy.  This is clearly an
732     * error case.
733     */
734    return false;
735 }
736
737 float
738 ir_constant::get_float_component(unsigned i) const
739 {
740    switch (this->type->base_type) {
741    case GLSL_TYPE_UINT:  return (float) this->value.u[i];
742    case GLSL_TYPE_INT:   return (float) this->value.i[i];
743    case GLSL_TYPE_FLOAT: return this->value.f[i];
744    case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1.0 : 0.0;
745    default:              assert(!"Should not get here."); break;
746    }
747
748    /* Must return something to make the compiler happy.  This is clearly an
749     * error case.
750     */
751    return 0.0;
752 }
753
754 int
755 ir_constant::get_int_component(unsigned i) const
756 {
757    switch (this->type->base_type) {
758    case GLSL_TYPE_UINT:  return this->value.u[i];
759    case GLSL_TYPE_INT:   return this->value.i[i];
760    case GLSL_TYPE_FLOAT: return (int) this->value.f[i];
761    case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1 : 0;
762    default:              assert(!"Should not get here."); break;
763    }
764
765    /* Must return something to make the compiler happy.  This is clearly an
766     * error case.
767     */
768    return 0;
769 }
770
771 unsigned
772 ir_constant::get_uint_component(unsigned i) const
773 {
774    switch (this->type->base_type) {
775    case GLSL_TYPE_UINT:  return this->value.u[i];
776    case GLSL_TYPE_INT:   return this->value.i[i];
777    case GLSL_TYPE_FLOAT: return (unsigned) this->value.f[i];
778    case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1 : 0;
779    default:              assert(!"Should not get here."); break;
780    }
781
782    /* Must return something to make the compiler happy.  This is clearly an
783     * error case.
784     */
785    return 0;
786 }
787
788 ir_constant *
789 ir_constant::get_array_element(unsigned i) const
790 {
791    assert(this->type->is_array());
792
793    /* From page 35 (page 41 of the PDF) of the GLSL 1.20 spec:
794     *
795     *     "Behavior is undefined if a shader subscripts an array with an index
796     *     less than 0 or greater than or equal to the size the array was
797     *     declared with."
798     *
799     * Most out-of-bounds accesses are removed before things could get this far.
800     * There are cases where non-constant array index values can get constant
801     * folded.
802     */
803    if (int(i) < 0)
804       i = 0;
805    else if (i >= this->type->length)
806       i = this->type->length - 1;
807
808    return array_elements[i];
809 }
810
811 ir_constant *
812 ir_constant::get_record_field(const char *name)
813 {
814    int idx = this->type->field_index(name);
815
816    if (idx < 0)
817       return NULL;
818
819    if (this->components.is_empty())
820       return NULL;
821
822    exec_node *node = this->components.head;
823    for (int i = 0; i < idx; i++) {
824       node = node->next;
825
826       /* If the end of the list is encountered before the element matching the
827        * requested field is found, return NULL.
828        */
829       if (node->is_tail_sentinel())
830          return NULL;
831    }
832
833    return (ir_constant *) node;
834 }
835
836
837 bool
838 ir_constant::has_value(const ir_constant *c) const
839 {
840    if (this->type != c->type)
841       return false;
842
843    if (this->type->is_array()) {
844       for (unsigned i = 0; i < this->type->length; i++) {
845          if (!this->array_elements[i]->has_value(c->array_elements[i]))
846             return false;
847       }
848       return true;
849    }
850
851    if (this->type->base_type == GLSL_TYPE_STRUCT) {
852       const exec_node *a_node = this->components.head;
853       const exec_node *b_node = c->components.head;
854
855       while (!a_node->is_tail_sentinel()) {
856          assert(!b_node->is_tail_sentinel());
857
858          const ir_constant *const a_field = (ir_constant *) a_node;
859          const ir_constant *const b_field = (ir_constant *) b_node;
860
861          if (!a_field->has_value(b_field))
862             return false;
863
864          a_node = a_node->next;
865          b_node = b_node->next;
866       }
867
868       return true;
869    }
870
871    for (unsigned i = 0; i < this->type->components(); i++) {
872       switch (this->type->base_type) {
873       case GLSL_TYPE_UINT:
874          if (this->value.u[i] != c->value.u[i])
875             return false;
876          break;
877       case GLSL_TYPE_INT:
878          if (this->value.i[i] != c->value.i[i])
879             return false;
880          break;
881       case GLSL_TYPE_FLOAT:
882          if (this->value.f[i] != c->value.f[i])
883             return false;
884          break;
885       case GLSL_TYPE_BOOL:
886          if (this->value.b[i] != c->value.b[i])
887             return false;
888          break;
889       default:
890          assert(!"Should not get here.");
891          return false;
892       }
893    }
894
895    return true;
896 }
897
898 bool
899 ir_constant::is_zero() const
900 {
901    if (!this->type->is_scalar() && !this->type->is_vector())
902       return false;
903
904    for (unsigned c = 0; c < this->type->vector_elements; c++) {
905       switch (this->type->base_type) {
906       case GLSL_TYPE_FLOAT:
907          if (this->value.f[c] != 0.0)
908             return false;
909          break;
910       case GLSL_TYPE_INT:
911          if (this->value.i[c] != 0)
912             return false;
913          break;
914       case GLSL_TYPE_UINT:
915          if (this->value.u[c] != 0)
916             return false;
917          break;
918       case GLSL_TYPE_BOOL:
919          if (this->value.b[c] != false)
920             return false;
921          break;
922       default:
923          /* The only other base types are structures, arrays, and samplers.
924           * Samplers cannot be constants, and the others should have been
925           * filtered out above.
926           */
927          assert(!"Should not get here.");
928          return false;
929       }
930    }
931
932    return true;
933 }
934
935 bool
936 ir_constant::is_one() const
937 {
938    if (!this->type->is_scalar() && !this->type->is_vector())
939       return false;
940
941    for (unsigned c = 0; c < this->type->vector_elements; c++) {
942       switch (this->type->base_type) {
943       case GLSL_TYPE_FLOAT:
944          if (this->value.f[c] != 1.0)
945             return false;
946          break;
947       case GLSL_TYPE_INT:
948          if (this->value.i[c] != 1)
949             return false;
950          break;
951       case GLSL_TYPE_UINT:
952          if (this->value.u[c] != 1)
953             return false;
954          break;
955       case GLSL_TYPE_BOOL:
956          if (this->value.b[c] != true)
957             return false;
958          break;
959       default:
960          /* The only other base types are structures, arrays, and samplers.
961           * Samplers cannot be constants, and the others should have been
962           * filtered out above.
963           */
964          assert(!"Should not get here.");
965          return false;
966       }
967    }
968
969    return true;
970 }
971
972 bool
973 ir_constant::is_negative_one() const
974 {
975    if (!this->type->is_scalar() && !this->type->is_vector())
976       return false;
977
978    if (this->type->is_boolean())
979       return false;
980
981    for (unsigned c = 0; c < this->type->vector_elements; c++) {
982       switch (this->type->base_type) {
983       case GLSL_TYPE_FLOAT:
984          if (this->value.f[c] != -1.0)
985             return false;
986          break;
987       case GLSL_TYPE_INT:
988          if (this->value.i[c] != -1)
989             return false;
990          break;
991       case GLSL_TYPE_UINT:
992          if (int(this->value.u[c]) != -1)
993             return false;
994          break;
995       default:
996          /* The only other base types are structures, arrays, samplers, and
997           * booleans.  Samplers cannot be constants, and the others should
998           * have been filtered out above.
999           */
1000          assert(!"Should not get here.");
1001          return false;
1002       }
1003    }
1004
1005    return true;
1006 }
1007
1008 ir_loop::ir_loop()
1009 {
1010    this->ir_type = ir_type_loop;
1011    this->cmp = ir_unop_neg;
1012    this->from = NULL;
1013    this->to = NULL;
1014    this->increment = NULL;
1015    this->counter = NULL;
1016 }
1017
1018
1019 ir_dereference_variable::ir_dereference_variable(ir_variable *var)
1020 {
1021    this->ir_type = ir_type_dereference_variable;
1022    this->var = var;
1023    this->type = (var != NULL) ? var->type : glsl_type::error_type;
1024 }
1025
1026
1027 ir_dereference_array::ir_dereference_array(ir_rvalue *value,
1028                                            ir_rvalue *array_index)
1029 {
1030    this->ir_type = ir_type_dereference_array;
1031    this->array_index = array_index;
1032    this->set_array(value);
1033 }
1034
1035
1036 ir_dereference_array::ir_dereference_array(ir_variable *var,
1037                                            ir_rvalue *array_index)
1038 {
1039    void *ctx = ralloc_parent(var);
1040
1041    this->ir_type = ir_type_dereference_array;
1042    this->array_index = array_index;
1043    this->set_array(new(ctx) ir_dereference_variable(var));
1044 }
1045
1046
1047 void
1048 ir_dereference_array::set_array(ir_rvalue *value)
1049 {
1050    this->array = value;
1051    this->type = glsl_type::error_type;
1052
1053    if (this->array != NULL) {
1054       const glsl_type *const vt = this->array->type;
1055
1056       if (vt->is_array()) {
1057          type = vt->element_type();
1058       } else if (vt->is_matrix()) {
1059          type = vt->column_type();
1060       } else if (vt->is_vector()) {
1061          type = vt->get_base_type();
1062       }
1063    }
1064 }
1065
1066
1067 ir_dereference_record::ir_dereference_record(ir_rvalue *value,
1068                                              const char *field)
1069 {
1070    this->ir_type = ir_type_dereference_record;
1071    this->record = value;
1072    this->field = ralloc_strdup(this, field);
1073    this->type = (this->record != NULL)
1074       ? this->record->type->field_type(field) : glsl_type::error_type;
1075 }
1076
1077
1078 ir_dereference_record::ir_dereference_record(ir_variable *var,
1079                                              const char *field)
1080 {
1081    void *ctx = ralloc_parent(var);
1082
1083    this->ir_type = ir_type_dereference_record;
1084    this->record = new(ctx) ir_dereference_variable(var);
1085    this->field = ralloc_strdup(this, field);
1086    this->type = (this->record != NULL)
1087       ? this->record->type->field_type(field) : glsl_type::error_type;
1088 }
1089
1090 bool
1091 ir_dereference::is_lvalue()
1092 {
1093    ir_variable *var = this->variable_referenced();
1094
1095    /* Every l-value derference chain eventually ends in a variable.
1096     */
1097    if ((var == NULL) || var->read_only)
1098       return false;
1099
1100    /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
1101     *
1102     *    "Samplers cannot be treated as l-values; hence cannot be used
1103     *     as out or inout function parameters, nor can they be
1104     *     assigned into."
1105     */
1106    if (this->type->contains_sampler())
1107       return false;
1108
1109    return true;
1110 }
1111
1112
1113 const char *tex_opcode_strs[] = { "tex", "txb", "txl", "txd", "txf" };
1114
1115 const char *ir_texture::opcode_string()
1116 {
1117    assert((unsigned int) op <=
1118           sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]));
1119    return tex_opcode_strs[op];
1120 }
1121
1122 ir_texture_opcode
1123 ir_texture::get_opcode(const char *str)
1124 {
1125    const int count = sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]);
1126    for (int op = 0; op < count; op++) {
1127       if (strcmp(str, tex_opcode_strs[op]) == 0)
1128          return (ir_texture_opcode) op;
1129    }
1130    return (ir_texture_opcode) -1;
1131 }
1132
1133
1134 void
1135 ir_texture::set_sampler(ir_dereference *sampler, const glsl_type *type)
1136 {
1137    assert(sampler != NULL);
1138    assert(type != NULL);
1139    this->sampler = sampler;
1140    this->type = type;
1141
1142    assert(sampler->type->sampler_type == (int) type->base_type);
1143    if (sampler->type->sampler_shadow)
1144       assert(type->vector_elements == 4 || type->vector_elements == 1);
1145    else
1146       assert(type->vector_elements == 4);
1147 }
1148
1149
1150 void
1151 ir_swizzle::init_mask(const unsigned *comp, unsigned count)
1152 {
1153    assert((count >= 1) && (count <= 4));
1154
1155    memset(&this->mask, 0, sizeof(this->mask));
1156    this->mask.num_components = count;
1157
1158    unsigned dup_mask = 0;
1159    switch (count) {
1160    case 4:
1161       assert(comp[3] <= 3);
1162       dup_mask |= (1U << comp[3])
1163          & ((1U << comp[0]) | (1U << comp[1]) | (1U << comp[2]));
1164       this->mask.w = comp[3];
1165
1166    case 3:
1167       assert(comp[2] <= 3);
1168       dup_mask |= (1U << comp[2])
1169          & ((1U << comp[0]) | (1U << comp[1]));
1170       this->mask.z = comp[2];
1171
1172    case 2:
1173       assert(comp[1] <= 3);
1174       dup_mask |= (1U << comp[1])
1175          & ((1U << comp[0]));
1176       this->mask.y = comp[1];
1177
1178    case 1:
1179       assert(comp[0] <= 3);
1180       this->mask.x = comp[0];
1181    }
1182
1183    this->mask.has_duplicates = dup_mask != 0;
1184
1185    /* Based on the number of elements in the swizzle and the base type
1186     * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
1187     * generate the type of the resulting value.
1188     */
1189    type = glsl_type::get_instance(val->type->base_type, mask.num_components, 1);
1190 }
1191
1192 ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
1193                        unsigned w, unsigned count)
1194    : val(val)
1195 {
1196    const unsigned components[4] = { x, y, z, w };
1197    this->ir_type = ir_type_swizzle;
1198    this->init_mask(components, count);
1199 }
1200
1201 ir_swizzle::ir_swizzle(ir_rvalue *val, const unsigned *comp,
1202                        unsigned count)
1203    : val(val)
1204 {
1205    this->ir_type = ir_type_swizzle;
1206    this->init_mask(comp, count);
1207 }
1208
1209 ir_swizzle::ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask)
1210 {
1211    this->ir_type = ir_type_swizzle;
1212    this->val = val;
1213    this->mask = mask;
1214    this->type = glsl_type::get_instance(val->type->base_type,
1215                                         mask.num_components, 1);
1216 }
1217
1218 #define X 1
1219 #define R 5
1220 #define S 9
1221 #define I 13
1222
1223 ir_swizzle *
1224 ir_swizzle::create(ir_rvalue *val, const char *str, unsigned vector_length)
1225 {
1226    void *ctx = ralloc_parent(val);
1227
1228    /* For each possible swizzle character, this table encodes the value in
1229     * \c idx_map that represents the 0th element of the vector.  For invalid
1230     * swizzle characters (e.g., 'k'), a special value is used that will allow
1231     * detection of errors.
1232     */
1233    static const unsigned char base_idx[26] = {
1234    /* a  b  c  d  e  f  g  h  i  j  k  l  m */
1235       R, R, I, I, I, I, R, I, I, I, I, I, I,
1236    /* n  o  p  q  r  s  t  u  v  w  x  y  z */
1237       I, I, S, S, R, S, S, I, I, X, X, X, X
1238    };
1239
1240    /* Each valid swizzle character has an entry in the previous table.  This
1241     * table encodes the base index encoded in the previous table plus the actual
1242     * index of the swizzle character.  When processing swizzles, the first
1243     * character in the string is indexed in the previous table.  Each character
1244     * in the string is indexed in this table, and the value found there has the
1245     * value form the first table subtracted.  The result must be on the range
1246     * [0,3].
1247     *
1248     * For example, the string "wzyx" will get X from the first table.  Each of
1249     * the charcaters will get X+3, X+2, X+1, and X+0 from this table.  After
1250     * subtraction, the swizzle values are { 3, 2, 1, 0 }.
1251     *
1252     * The string "wzrg" will get X from the first table.  Each of the characters
1253     * will get X+3, X+2, R+0, and R+1 from this table.  After subtraction, the
1254     * swizzle values are { 3, 2, 4, 5 }.  Since 4 and 5 are outside the range
1255     * [0,3], the error is detected.
1256     */
1257    static const unsigned char idx_map[26] = {
1258    /* a    b    c    d    e    f    g    h    i    j    k    l    m */
1259       R+3, R+2, 0,   0,   0,   0,   R+1, 0,   0,   0,   0,   0,   0,
1260    /* n    o    p    q    r    s    t    u    v    w    x    y    z */
1261       0,   0,   S+2, S+3, R+0, S+0, S+1, 0,   0,   X+3, X+0, X+1, X+2
1262    };
1263
1264    int swiz_idx[4] = { 0, 0, 0, 0 };
1265    unsigned i;
1266
1267
1268    /* Validate the first character in the swizzle string and look up the base
1269     * index value as described above.
1270     */
1271    if ((str[0] < 'a') || (str[0] > 'z'))
1272       return NULL;
1273
1274    const unsigned base = base_idx[str[0] - 'a'];
1275
1276
1277    for (i = 0; (i < 4) && (str[i] != '\0'); i++) {
1278       /* Validate the next character, and, as described above, convert it to a
1279        * swizzle index.
1280        */
1281       if ((str[i] < 'a') || (str[i] > 'z'))
1282          return NULL;
1283
1284       swiz_idx[i] = idx_map[str[i] - 'a'] - base;
1285       if ((swiz_idx[i] < 0) || (swiz_idx[i] >= (int) vector_length))
1286          return NULL;
1287    }
1288
1289    if (str[i] != '\0')
1290          return NULL;
1291
1292    return new(ctx) ir_swizzle(val, swiz_idx[0], swiz_idx[1], swiz_idx[2],
1293                               swiz_idx[3], i);
1294 }
1295
1296 #undef X
1297 #undef R
1298 #undef S
1299 #undef I
1300
1301 ir_variable *
1302 ir_swizzle::variable_referenced()
1303 {
1304    return this->val->variable_referenced();
1305 }
1306
1307
1308 ir_variable::ir_variable(const struct glsl_type *type, const char *name,
1309                          ir_variable_mode mode)
1310    : max_array_access(0), read_only(false), centroid(false), invariant(false),
1311      mode(mode), interpolation(ir_var_smooth)
1312 {
1313    this->ir_type = ir_type_variable;
1314    this->type = type;
1315    this->name = ralloc_strdup(this, name);
1316    this->explicit_location = false;
1317    this->location = -1;
1318    this->warn_extension = NULL;
1319    this->constant_value = NULL;
1320    this->origin_upper_left = false;
1321    this->pixel_center_integer = false;
1322    this->depth_layout = ir_depth_layout_none;
1323    this->used = false;
1324
1325    if (type && type->base_type == GLSL_TYPE_SAMPLER)
1326       this->read_only = true;
1327 }
1328
1329
1330 const char *
1331 ir_variable::interpolation_string() const
1332 {
1333    switch (this->interpolation) {
1334    case ir_var_smooth:        return "smooth";
1335    case ir_var_flat:          return "flat";
1336    case ir_var_noperspective: return "noperspective";
1337    }
1338
1339    assert(!"Should not get here.");
1340    return "";
1341 }
1342
1343
1344 unsigned
1345 ir_variable::component_slots() const
1346 {
1347    /* FINISHME: Sparsely accessed arrays require fewer slots. */
1348    return this->type->component_slots();
1349 }
1350
1351
1352 ir_function_signature::ir_function_signature(const glsl_type *return_type)
1353    : return_type(return_type), is_defined(false), _function(NULL)
1354 {
1355    this->ir_type = ir_type_function_signature;
1356    this->is_builtin = false;
1357 }
1358
1359
1360 static bool
1361 modes_match(unsigned a, unsigned b)
1362 {
1363    if (a == b)
1364       return true;
1365
1366    /* Accept "in" vs. "const in" */
1367    if ((a == ir_var_const_in && b == ir_var_in) ||
1368        (b == ir_var_const_in && a == ir_var_in))
1369       return true;
1370
1371    return false;
1372 }
1373
1374
1375 const char *
1376 ir_function_signature::qualifiers_match(exec_list *params)
1377 {
1378    exec_list_iterator iter_a = parameters.iterator();
1379    exec_list_iterator iter_b = params->iterator();
1380
1381    /* check that the qualifiers match. */
1382    while (iter_a.has_next()) {
1383       ir_variable *a = (ir_variable *)iter_a.get();
1384       ir_variable *b = (ir_variable *)iter_b.get();
1385
1386       if (a->read_only != b->read_only ||
1387           !modes_match(a->mode, b->mode) ||
1388           a->interpolation != b->interpolation ||
1389           a->centroid != b->centroid) {
1390
1391          /* parameter a's qualifiers don't match */
1392          return a->name;
1393       }
1394
1395       iter_a.next();
1396       iter_b.next();
1397    }
1398    return NULL;
1399 }
1400
1401
1402 void
1403 ir_function_signature::replace_parameters(exec_list *new_params)
1404 {
1405    /* Destroy all of the previous parameter information.  If the previous
1406     * parameter information comes from the function prototype, it may either
1407     * specify incorrect parameter names or not have names at all.
1408     */
1409    foreach_iter(exec_list_iterator, iter, parameters) {
1410       assert(((ir_instruction *) iter.get())->as_variable() != NULL);
1411
1412       iter.remove();
1413    }
1414
1415    new_params->move_nodes_to(&parameters);
1416 }
1417
1418
1419 ir_function::ir_function(const char *name)
1420 {
1421    this->ir_type = ir_type_function;
1422    this->name = ralloc_strdup(this, name);
1423 }
1424
1425
1426 bool
1427 ir_function::has_user_signature()
1428 {
1429    foreach_list(n, &this->signatures) {
1430       ir_function_signature *const sig = (ir_function_signature *) n;
1431       if (!sig->is_builtin)
1432          return true;
1433    }
1434    return false;
1435 }
1436
1437
1438 ir_call *
1439 ir_call::get_error_instruction(void *ctx)
1440 {
1441    ir_call *call = new(ctx) ir_call;
1442
1443    call->type = glsl_type::error_type;
1444    return call;
1445 }
1446
1447 void
1448 ir_call::set_callee(ir_function_signature *sig)
1449 {
1450    assert((this->type == NULL) || (this->type == sig->return_type));
1451
1452    this->callee = sig;
1453 }
1454
1455 void
1456 visit_exec_list(exec_list *list, ir_visitor *visitor)
1457 {
1458    foreach_iter(exec_list_iterator, iter, *list) {
1459       ((ir_instruction *)iter.get())->accept(visitor);
1460    }
1461 }
1462
1463
1464 static void
1465 steal_memory(ir_instruction *ir, void *new_ctx)
1466 {
1467    ir_variable *var = ir->as_variable();
1468    ir_constant *constant = ir->as_constant();
1469    if (var != NULL && var->constant_value != NULL)
1470       steal_memory(var->constant_value, ir);
1471
1472    /* The components of aggregate constants are not visited by the normal
1473     * visitor, so steal their values by hand.
1474     */
1475    if (constant != NULL) {
1476       if (constant->type->is_record()) {
1477          foreach_iter(exec_list_iterator, iter, constant->components) {
1478             ir_constant *field = (ir_constant *)iter.get();
1479             steal_memory(field, ir);
1480          }
1481       } else if (constant->type->is_array()) {
1482          for (unsigned int i = 0; i < constant->type->length; i++) {
1483             steal_memory(constant->array_elements[i], ir);
1484          }
1485       }
1486    }
1487
1488    ralloc_steal(new_ctx, ir);
1489 }
1490
1491
1492 void
1493 reparent_ir(exec_list *list, void *mem_ctx)
1494 {
1495    foreach_list(node, list) {
1496       visit_tree((ir_instruction *) node, steal_memory, mem_ctx);
1497    }
1498 }
1499
1500
1501 static ir_rvalue *
1502 try_min_one(ir_rvalue *ir)
1503 {
1504    ir_expression *expr = ir->as_expression();
1505
1506    if (!expr || expr->operation != ir_binop_min)
1507       return NULL;
1508
1509    if (expr->operands[0]->is_one())
1510       return expr->operands[1];
1511
1512    if (expr->operands[1]->is_one())
1513       return expr->operands[0];
1514
1515    return NULL;
1516 }
1517
1518 static ir_rvalue *
1519 try_max_zero(ir_rvalue *ir)
1520 {
1521    ir_expression *expr = ir->as_expression();
1522
1523    if (!expr || expr->operation != ir_binop_max)
1524       return NULL;
1525
1526    if (expr->operands[0]->is_zero())
1527       return expr->operands[1];
1528
1529    if (expr->operands[1]->is_zero())
1530       return expr->operands[0];
1531
1532    return NULL;
1533 }
1534
1535 ir_rvalue *
1536 ir_rvalue::as_rvalue_to_saturate()
1537 {
1538    ir_expression *expr = this->as_expression();
1539
1540    if (!expr)
1541       return NULL;
1542
1543    ir_rvalue *max_zero = try_max_zero(expr);
1544    if (max_zero) {
1545       return try_min_one(max_zero);
1546    } else {
1547       ir_rvalue *min_one = try_min_one(expr);
1548       if (min_one) {
1549          return try_max_zero(min_one);
1550       }
1551    }
1552
1553    return NULL;
1554 }