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