ir.cpp revision 74e1802f5dd8921750851abc6128e4073602d405
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/imports.h"
25#include "ir.h"
26#include "ir_visitor.h"
27#include "glsl_types.h"
28
29ir_assignment::ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs,
30			     ir_rvalue *condition)
31{
32   this->ir_type = ir_type_assignment;
33   this->lhs = lhs;
34   this->rhs = rhs;
35   this->condition = condition;
36}
37
38
39ir_expression::ir_expression(int op, const struct glsl_type *type,
40			     ir_rvalue *op0, ir_rvalue *op1)
41{
42   this->ir_type = ir_type_expression;
43   this->type = type;
44   this->operation = ir_expression_operation(op);
45   this->operands[0] = op0;
46   this->operands[1] = op1;
47}
48
49unsigned int
50ir_expression::get_num_operands(ir_expression_operation op)
51{
52/* Update ir_print_visitor.cpp when updating this list. */
53   const int num_operands[] = {
54      1, /* ir_unop_bit_not */
55      1, /* ir_unop_logic_not */
56      1, /* ir_unop_neg */
57      1, /* ir_unop_abs */
58      1, /* ir_unop_sign */
59      1, /* ir_unop_rcp */
60      1, /* ir_unop_rsq */
61      1, /* ir_unop_sqrt */
62      1, /* ir_unop_exp */
63      1, /* ir_unop_log */
64      1, /* ir_unop_exp2 */
65      1, /* ir_unop_log2 */
66      1, /* ir_unop_f2i */
67      1, /* ir_unop_i2f */
68      1, /* ir_unop_f2b */
69      1, /* ir_unop_b2f */
70      1, /* ir_unop_i2b */
71      1, /* ir_unop_b2i */
72      1, /* ir_unop_u2f */
73
74      1, /* ir_unop_trunc */
75      1, /* ir_unop_ceil */
76      1, /* ir_unop_floor */
77      1, /* ir_unop_fract */
78
79      1, /* ir_unop_sin */
80      1, /* ir_unop_cos */
81
82      1, /* ir_unop_dFdx */
83      1, /* ir_unop_dFdy */
84
85      2, /* ir_binop_add */
86      2, /* ir_binop_sub */
87      2, /* ir_binop_mul */
88      2, /* ir_binop_div */
89      2, /* ir_binop_mod */
90
91      2, /* ir_binop_less */
92      2, /* ir_binop_greater */
93      2, /* ir_binop_lequal */
94      2, /* ir_binop_gequal */
95      2, /* ir_binop_equal */
96      2, /* ir_binop_nequal */
97
98      2, /* ir_binop_lshift */
99      2, /* ir_binop_rshift */
100      2, /* ir_binop_bit_and */
101      2, /* ir_binop_bit_xor */
102      2, /* ir_binop_bit_or */
103
104      2, /* ir_binop_logic_and */
105      2, /* ir_binop_logic_xor */
106      2, /* ir_binop_logic_or */
107
108      2, /* ir_binop_dot */
109      2, /* ir_binop_cross */
110      2, /* ir_binop_min */
111      2, /* ir_binop_max */
112
113      2, /* ir_binop_pow */
114   };
115
116   assert(sizeof(num_operands) / sizeof(num_operands[0]) == ir_binop_pow + 1);
117
118   return num_operands[op];
119}
120
121static const char *const operator_strs[] = {
122   "~",
123   "!",
124   "neg",
125   "abs",
126   "sign",
127   "rcp",
128   "rsq",
129   "sqrt",
130   "exp",
131   "log",
132   "exp2",
133   "log2",
134   "f2i",
135   "i2f",
136   "f2b",
137   "b2f",
138   "i2b",
139   "b2i",
140   "u2f",
141   "trunc",
142   "ceil",
143   "floor",
144   "fract",
145   "sin",
146   "cos",
147   "dFdx",
148   "dFdy",
149   "+",
150   "-",
151   "*",
152   "/",
153   "%",
154   "<",
155   ">",
156   "<=",
157   ">=",
158   "==",
159   "!=",
160   "<<",
161   ">>",
162   "&",
163   "^",
164   "|",
165   "&&",
166   "^^",
167   "||",
168   "dot",
169   "cross",
170   "min",
171   "max",
172   "pow",
173};
174
175const char *ir_expression::operator_string()
176{
177   assert((unsigned int) operation <=
178	  sizeof(operator_strs) / sizeof(operator_strs[0]));
179   return operator_strs[operation];
180}
181
182ir_expression_operation
183ir_expression::get_operator(const char *str)
184{
185   const int operator_count = sizeof(operator_strs) / sizeof(operator_strs[0]);
186   for (int op = 0; op < operator_count; op++) {
187      if (strcmp(str, operator_strs[op]) == 0)
188	 return (ir_expression_operation) op;
189   }
190   return (ir_expression_operation) -1;
191}
192
193ir_constant::ir_constant()
194{
195   this->ir_type = ir_type_constant;
196}
197
198ir_constant::ir_constant(const struct glsl_type *type,
199			 const ir_constant_data *data)
200{
201   assert((type->base_type >= GLSL_TYPE_UINT)
202	  && (type->base_type <= GLSL_TYPE_BOOL));
203
204   this->ir_type = ir_type_constant;
205   this->type = type;
206   memcpy(& this->value, data, sizeof(this->value));
207}
208
209ir_constant::ir_constant(float f)
210{
211   this->ir_type = ir_type_constant;
212   this->type = glsl_type::float_type;
213   this->value.f[0] = f;
214}
215
216ir_constant::ir_constant(unsigned int u)
217{
218   this->ir_type = ir_type_constant;
219   this->type = glsl_type::uint_type;
220   this->value.u[0] = u;
221}
222
223ir_constant::ir_constant(int i)
224{
225   this->ir_type = ir_type_constant;
226   this->type = glsl_type::int_type;
227   this->value.i[0] = i;
228}
229
230ir_constant::ir_constant(bool b)
231{
232   this->ir_type = ir_type_constant;
233   this->type = glsl_type::bool_type;
234   this->value.b[0] = b;
235}
236
237ir_constant::ir_constant(const ir_constant *c, unsigned i)
238{
239   this->ir_type = ir_type_constant;
240   this->type = c->type->get_base_type();
241
242   switch (this->type->base_type) {
243   case GLSL_TYPE_UINT:  this->value.u[0] = c->value.u[i]; break;
244   case GLSL_TYPE_INT:   this->value.i[0] = c->value.i[i]; break;
245   case GLSL_TYPE_FLOAT: this->value.f[0] = c->value.f[i]; break;
246   case GLSL_TYPE_BOOL:  this->value.b[0] = c->value.b[i]; break;
247   default:              assert(!"Should not get here."); break;
248   }
249}
250
251ir_constant::ir_constant(const struct glsl_type *type, exec_list *value_list)
252{
253   this->ir_type = ir_type_constant;
254   this->type = type;
255
256   assert(type->is_scalar() || type->is_vector() || type->is_matrix()
257	  || type->is_record() || type->is_array());
258
259   if (type->is_array()) {
260      this->array_elements = talloc_array(this, ir_constant *, type->length);
261      unsigned i = 0;
262      foreach_list(node, value_list) {
263	 ir_constant *value = (ir_constant *) node;
264	 assert(value->as_constant() != NULL);
265
266	 this->array_elements[i++] = value;
267      }
268      return;
269   }
270
271   /* If the constant is a record, the types of each of the entries in
272    * value_list must be a 1-for-1 match with the structure components.  Each
273    * entry must also be a constant.  Just move the nodes from the value_list
274    * to the list in the ir_constant.
275    */
276   /* FINISHME: Should there be some type checking and / or assertions here? */
277   /* FINISHME: Should the new constant take ownership of the nodes from
278    * FINISHME: value_list, or should it make copies?
279    */
280   if (type->is_record()) {
281      value_list->move_nodes_to(& this->components);
282      return;
283   }
284
285
286   ir_constant *value = (ir_constant *) (value_list->head);
287
288   /* Use each component from each entry in the value_list to initialize one
289    * component of the constant being constructed.
290    */
291   for (unsigned i = 0; i < type->components(); /* empty */) {
292      assert(value->as_constant() != NULL);
293      assert(!value->is_tail_sentinal());
294
295      for (unsigned j = 0; j < value->type->components(); j++) {
296	 switch (type->base_type) {
297	 case GLSL_TYPE_UINT:
298	    this->value.u[i] = value->get_uint_component(j);
299	    break;
300	 case GLSL_TYPE_INT:
301	    this->value.i[i] = value->get_int_component(j);
302	    break;
303	 case GLSL_TYPE_FLOAT:
304	    this->value.f[i] = value->get_float_component(j);
305	    break;
306	 case GLSL_TYPE_BOOL:
307	    this->value.b[i] = value->get_bool_component(j);
308	    break;
309	 default:
310	    /* FINISHME: What to do?  Exceptions are not the answer.
311	     */
312	    break;
313	 }
314
315	 i++;
316	 if (i >= type->components())
317	    break;
318      }
319
320      value = (ir_constant *) value->next;
321   }
322}
323
324bool
325ir_constant::get_bool_component(unsigned i) const
326{
327   switch (this->type->base_type) {
328   case GLSL_TYPE_UINT:  return this->value.u[i] != 0;
329   case GLSL_TYPE_INT:   return this->value.i[i] != 0;
330   case GLSL_TYPE_FLOAT: return ((int)this->value.f[i]) != 0;
331   case GLSL_TYPE_BOOL:  return this->value.b[i];
332   default:              assert(!"Should not get here."); break;
333   }
334
335   /* Must return something to make the compiler happy.  This is clearly an
336    * error case.
337    */
338   return false;
339}
340
341float
342ir_constant::get_float_component(unsigned i) const
343{
344   switch (this->type->base_type) {
345   case GLSL_TYPE_UINT:  return (float) this->value.u[i];
346   case GLSL_TYPE_INT:   return (float) this->value.i[i];
347   case GLSL_TYPE_FLOAT: return this->value.f[i];
348   case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1.0 : 0.0;
349   default:              assert(!"Should not get here."); break;
350   }
351
352   /* Must return something to make the compiler happy.  This is clearly an
353    * error case.
354    */
355   return 0.0;
356}
357
358int
359ir_constant::get_int_component(unsigned i) const
360{
361   switch (this->type->base_type) {
362   case GLSL_TYPE_UINT:  return this->value.u[i];
363   case GLSL_TYPE_INT:   return this->value.i[i];
364   case GLSL_TYPE_FLOAT: return (int) this->value.f[i];
365   case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1 : 0;
366   default:              assert(!"Should not get here."); break;
367   }
368
369   /* Must return something to make the compiler happy.  This is clearly an
370    * error case.
371    */
372   return 0;
373}
374
375unsigned
376ir_constant::get_uint_component(unsigned i) const
377{
378   switch (this->type->base_type) {
379   case GLSL_TYPE_UINT:  return this->value.u[i];
380   case GLSL_TYPE_INT:   return this->value.i[i];
381   case GLSL_TYPE_FLOAT: return (unsigned) this->value.f[i];
382   case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1 : 0;
383   default:              assert(!"Should not get here."); break;
384   }
385
386   /* Must return something to make the compiler happy.  This is clearly an
387    * error case.
388    */
389   return 0;
390}
391
392ir_constant *
393ir_constant::get_array_element(unsigned i) const
394{
395   assert(this->type->is_array());
396   assert(i < this->type->length);
397
398   return array_elements[i];
399}
400
401ir_constant *
402ir_constant::get_record_field(const char *name)
403{
404   int idx = this->type->field_index(name);
405
406   if (idx < 0)
407      return NULL;
408
409   if (this->components.is_empty())
410      return NULL;
411
412   exec_node *node = this->components.head;
413   for (int i = 0; i < idx; i++) {
414      node = node->next;
415
416      /* If the end of the list is encountered before the element matching the
417       * requested field is found, return NULL.
418       */
419      if (node->is_tail_sentinal())
420	 return NULL;
421   }
422
423   return (ir_constant *) node;
424}
425
426
427bool
428ir_constant::has_value(const ir_constant *c) const
429{
430   if (this->type != c->type)
431      return false;
432
433   /* FINISHME: This will probably also handle constant arrays as soon as those
434    * FINISHME: are supported.
435    */
436   if (this->type->base_type == GLSL_TYPE_STRUCT) {
437      const exec_node *a_node = this->components.head;
438      const exec_node *b_node = c->components.head;
439
440      while (!a_node->is_tail_sentinal()) {
441	 assert(!b_node->is_tail_sentinal());
442
443	 const ir_constant *const a_field = (ir_constant *) a_node;
444	 const ir_constant *const b_field = (ir_constant *) b_node;
445
446	 if (!a_field->has_value(b_field))
447	    return false;
448
449	 a_node = a_node->next;
450	 b_node = b_node->next;
451      }
452
453      return true;
454   }
455
456   for (unsigned i = 0; i < this->type->components(); i++) {
457      switch (this->type->base_type) {
458      case GLSL_TYPE_UINT:
459	 if (this->value.u[i] != c->value.u[i])
460	    return false;
461	 break;
462      case GLSL_TYPE_INT:
463	 if (this->value.i[i] != c->value.i[i])
464	    return false;
465	 break;
466      case GLSL_TYPE_FLOAT:
467	 if (this->value.f[i] != c->value.f[i])
468	    return false;
469	 break;
470      case GLSL_TYPE_BOOL:
471	 if (this->value.b[i] != c->value.b[i])
472	    return false;
473	 break;
474      default:
475	 assert(!"Should not get here.");
476	 return false;
477      }
478   }
479
480   return true;
481}
482
483ir_dereference_variable::ir_dereference_variable(ir_variable *var)
484{
485   this->ir_type = ir_type_dereference_variable;
486   this->var = var;
487   this->type = (var != NULL) ? var->type : glsl_type::error_type;
488}
489
490
491ir_dereference_array::ir_dereference_array(ir_rvalue *value,
492					   ir_rvalue *array_index)
493{
494   this->ir_type = ir_type_dereference_array;
495   this->array_index = array_index;
496   this->set_array(value);
497}
498
499
500ir_dereference_array::ir_dereference_array(ir_variable *var,
501					   ir_rvalue *array_index)
502{
503   void *ctx = talloc_parent(var);
504
505   this->ir_type = ir_type_dereference_array;
506   this->array_index = array_index;
507   this->set_array(new(ctx) ir_dereference_variable(var));
508}
509
510
511void
512ir_dereference_array::set_array(ir_rvalue *value)
513{
514   this->array = value;
515   this->type = glsl_type::error_type;
516
517   if (this->array != NULL) {
518      const glsl_type *const vt = this->array->type;
519
520      if (vt->is_array()) {
521	 type = vt->element_type();
522      } else if (vt->is_matrix()) {
523	 type = vt->column_type();
524      } else if (vt->is_vector()) {
525	 type = vt->get_base_type();
526      }
527   }
528}
529
530
531ir_dereference_record::ir_dereference_record(ir_rvalue *value,
532					     const char *field)
533{
534   this->ir_type = ir_type_dereference_record;
535   this->record = value;
536   this->field = talloc_strdup(this, field);
537   this->type = (this->record != NULL)
538      ? this->record->type->field_type(field) : glsl_type::error_type;
539}
540
541
542ir_dereference_record::ir_dereference_record(ir_variable *var,
543					     const char *field)
544{
545   void *ctx = talloc_parent(var);
546
547   this->ir_type = ir_type_dereference_record;
548   this->record = new(ctx) ir_dereference_variable(var);
549   this->field = talloc_strdup(this, field);
550   this->type = (this->record != NULL)
551      ? this->record->type->field_type(field) : glsl_type::error_type;
552}
553
554
555bool
556ir_dereference::is_lvalue()
557{
558   ir_variable *var = this->variable_referenced();
559
560   /* Every l-value derference chain eventually ends in a variable.
561    */
562   if ((var == NULL) || var->read_only)
563      return false;
564
565   if (this->type->is_array() && !var->array_lvalue)
566      return false;
567
568   return true;
569}
570
571
572const char *tex_opcode_strs[] = { "tex", "txb", "txl", "txd", "txf" };
573
574const char *ir_texture::opcode_string()
575{
576   assert((unsigned int) op <=
577	  sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]));
578   return tex_opcode_strs[op];
579}
580
581ir_texture_opcode
582ir_texture::get_opcode(const char *str)
583{
584   const int count = sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]);
585   for (int op = 0; op < count; op++) {
586      if (strcmp(str, tex_opcode_strs[op]) == 0)
587	 return (ir_texture_opcode) op;
588   }
589   return (ir_texture_opcode) -1;
590}
591
592
593void
594ir_texture::set_sampler(ir_dereference *sampler)
595{
596   assert(sampler != NULL);
597   this->sampler = sampler;
598
599   switch (sampler->type->sampler_type) {
600   case GLSL_TYPE_FLOAT:
601      this->type = glsl_type::vec4_type;
602      break;
603   case GLSL_TYPE_INT:
604      this->type = glsl_type::ivec4_type;
605      break;
606   case GLSL_TYPE_UINT:
607      this->type = glsl_type::uvec4_type;
608      break;
609   }
610}
611
612
613void
614ir_swizzle::init_mask(const unsigned *comp, unsigned count)
615{
616   assert((count >= 1) && (count <= 4));
617
618   memset(&this->mask, 0, sizeof(this->mask));
619   this->mask.num_components = count;
620
621   unsigned dup_mask = 0;
622   switch (count) {
623   case 4:
624      assert(comp[3] <= 3);
625      dup_mask |= (1U << comp[3])
626	 & ((1U << comp[0]) | (1U << comp[1]) | (1U << comp[2]));
627      this->mask.w = comp[3];
628
629   case 3:
630      assert(comp[2] <= 3);
631      dup_mask |= (1U << comp[2])
632	 & ((1U << comp[0]) | (1U << comp[1]));
633      this->mask.z = comp[2];
634
635   case 2:
636      assert(comp[1] <= 3);
637      dup_mask |= (1U << comp[1])
638	 & ((1U << comp[0]));
639      this->mask.y = comp[1];
640
641   case 1:
642      assert(comp[0] <= 3);
643      this->mask.x = comp[0];
644   }
645
646   this->mask.has_duplicates = dup_mask != 0;
647
648   /* Based on the number of elements in the swizzle and the base type
649    * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
650    * generate the type of the resulting value.
651    */
652   type = glsl_type::get_instance(val->type->base_type, mask.num_components, 1);
653}
654
655ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
656		       unsigned w, unsigned count)
657   : val(val)
658{
659   const unsigned components[4] = { x, y, z, w };
660   this->ir_type = ir_type_swizzle;
661   this->init_mask(components, count);
662}
663
664ir_swizzle::ir_swizzle(ir_rvalue *val, const unsigned *comp,
665		       unsigned count)
666   : val(val)
667{
668   this->ir_type = ir_type_swizzle;
669   this->init_mask(comp, count);
670}
671
672ir_swizzle::ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask)
673{
674   this->ir_type = ir_type_swizzle;
675   this->val = val;
676   this->mask = mask;
677   this->type = glsl_type::get_instance(val->type->base_type,
678					mask.num_components, 1);
679}
680
681#define X 1
682#define R 5
683#define S 9
684#define I 13
685
686ir_swizzle *
687ir_swizzle::create(ir_rvalue *val, const char *str, unsigned vector_length)
688{
689   void *ctx = talloc_parent(val);
690
691   /* For each possible swizzle character, this table encodes the value in
692    * \c idx_map that represents the 0th element of the vector.  For invalid
693    * swizzle characters (e.g., 'k'), a special value is used that will allow
694    * detection of errors.
695    */
696   static const unsigned char base_idx[26] = {
697   /* a  b  c  d  e  f  g  h  i  j  k  l  m */
698      R, R, I, I, I, I, R, I, I, I, I, I, I,
699   /* n  o  p  q  r  s  t  u  v  w  x  y  z */
700      I, I, S, S, R, S, S, I, I, X, X, X, X
701   };
702
703   /* Each valid swizzle character has an entry in the previous table.  This
704    * table encodes the base index encoded in the previous table plus the actual
705    * index of the swizzle character.  When processing swizzles, the first
706    * character in the string is indexed in the previous table.  Each character
707    * in the string is indexed in this table, and the value found there has the
708    * value form the first table subtracted.  The result must be on the range
709    * [0,3].
710    *
711    * For example, the string "wzyx" will get X from the first table.  Each of
712    * the charcaters will get X+3, X+2, X+1, and X+0 from this table.  After
713    * subtraction, the swizzle values are { 3, 2, 1, 0 }.
714    *
715    * The string "wzrg" will get X from the first table.  Each of the characters
716    * will get X+3, X+2, R+0, and R+1 from this table.  After subtraction, the
717    * swizzle values are { 3, 2, 4, 5 }.  Since 4 and 5 are outside the range
718    * [0,3], the error is detected.
719    */
720   static const unsigned char idx_map[26] = {
721   /* a    b    c    d    e    f    g    h    i    j    k    l    m */
722      R+3, R+2, 0,   0,   0,   0,   R+1, 0,   0,   0,   0,   0,   0,
723   /* n    o    p    q    r    s    t    u    v    w    x    y    z */
724      0,   0,   S+2, S+3, R+0, S+0, S+1, 0,   0,   X+3, X+0, X+1, X+2
725   };
726
727   int swiz_idx[4] = { 0, 0, 0, 0 };
728   unsigned i;
729
730
731   /* Validate the first character in the swizzle string and look up the base
732    * index value as described above.
733    */
734   if ((str[0] < 'a') || (str[0] > 'z'))
735      return NULL;
736
737   const unsigned base = base_idx[str[0] - 'a'];
738
739
740   for (i = 0; (i < 4) && (str[i] != '\0'); i++) {
741      /* Validate the next character, and, as described above, convert it to a
742       * swizzle index.
743       */
744      if ((str[i] < 'a') || (str[i] > 'z'))
745	 return NULL;
746
747      swiz_idx[i] = idx_map[str[i] - 'a'] - base;
748      if ((swiz_idx[i] < 0) || (swiz_idx[i] >= (int) vector_length))
749	 return NULL;
750   }
751
752   if (str[i] != '\0')
753	 return NULL;
754
755   return new(ctx) ir_swizzle(val, swiz_idx[0], swiz_idx[1], swiz_idx[2],
756			      swiz_idx[3], i);
757}
758
759#undef X
760#undef R
761#undef S
762#undef I
763
764ir_variable *
765ir_swizzle::variable_referenced()
766{
767   return this->val->variable_referenced();
768}
769
770
771ir_variable::ir_variable(const struct glsl_type *type, const char *name,
772			 ir_variable_mode mode)
773   : max_array_access(0), read_only(false), centroid(false), invariant(false),
774     shader_in(false), shader_out(false),
775     mode(mode), interpolation(ir_var_smooth), array_lvalue(false)
776{
777   this->ir_type = ir_type_variable;
778   this->type = type;
779   this->name = talloc_strdup(this, name);
780   this->location = -1;
781   this->warn_extension = NULL;
782   this->constant_value = NULL;
783
784   if (type && type->base_type == GLSL_TYPE_SAMPLER)
785      this->read_only = true;
786}
787
788
789const char *
790ir_variable::interpolation_string() const
791{
792   if (!this->shader_in && !this->shader_out)
793      return "";
794
795   switch (this->interpolation) {
796   case ir_var_smooth:        return "smooth";
797   case ir_var_flat:          return "flat";
798   case ir_var_noperspective: return "noperspective";
799   }
800
801   assert(!"Should not get here.");
802   return "";
803}
804
805
806unsigned
807ir_variable::component_slots() const
808{
809   /* FINISHME: Sparsely accessed arrays require fewer slots. */
810   return this->type->component_slots();
811}
812
813
814ir_function_signature::ir_function_signature(const glsl_type *return_type)
815   : return_type(return_type), is_defined(false), _function(NULL)
816{
817   this->ir_type = ir_type_function_signature;
818}
819
820
821const char *
822ir_function_signature::qualifiers_match(exec_list *params)
823{
824   exec_list_iterator iter_a = parameters.iterator();
825   exec_list_iterator iter_b = params->iterator();
826
827   /* check that the qualifiers match. */
828   while (iter_a.has_next()) {
829      ir_variable *a = (ir_variable *)iter_a.get();
830      ir_variable *b = (ir_variable *)iter_b.get();
831
832      if (a->read_only != b->read_only ||
833	  a->mode != b->mode ||
834	  a->interpolation != b->interpolation ||
835	  a->centroid != b->centroid) {
836
837	 /* parameter a's qualifiers don't match */
838	 return a->name;
839      }
840
841      iter_a.next();
842      iter_b.next();
843   }
844   return NULL;
845}
846
847
848void
849ir_function_signature::replace_parameters(exec_list *new_params)
850{
851   /* Destroy all of the previous parameter information.  If the previous
852    * parameter information comes from the function prototype, it may either
853    * specify incorrect parameter names or not have names at all.
854    */
855   foreach_iter(exec_list_iterator, iter, parameters) {
856      assert(((ir_instruction *) iter.get())->as_variable() != NULL);
857
858      iter.remove();
859   }
860
861   new_params->move_nodes_to(&parameters);
862}
863
864
865ir_function::ir_function(const char *name)
866{
867   this->ir_type = ir_type_function;
868   this->name = talloc_strdup(this, name);
869}
870
871
872ir_call *
873ir_call::get_error_instruction(void *ctx)
874{
875   ir_call *call = new(ctx) ir_call;
876
877   call->type = glsl_type::error_type;
878   return call;
879}
880
881void
882ir_call::set_callee(ir_function_signature *sig)
883{
884   assert((this->type == NULL) || (this->type == sig->return_type));
885
886   this->callee = sig;
887}
888
889void
890visit_exec_list(exec_list *list, ir_visitor *visitor)
891{
892   foreach_iter(exec_list_iterator, iter, *list) {
893      ((ir_instruction *)iter.get())->accept(visitor);
894   }
895}
896
897
898static void
899steal_memory(ir_instruction *ir, void *new_ctx)
900{
901   talloc_steal(new_ctx, ir);
902}
903
904
905void
906reparent_ir(exec_list *list, void *mem_ctx)
907{
908   foreach_list(node, list) {
909      visit_tree((ir_instruction *) node, steal_memory, mem_ctx);
910   }
911}
912