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