2 * Copyright © 2010 Intel Corporation
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:
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
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.
24 #include "main/imports.h"
26 #include "ir_visitor.h"
27 #include "glsl_types.h"
29 ir_assignment::ir_assignment(ir_rvalue
*lhs
, ir_rvalue
*rhs
,
32 this->ir_type
= ir_type_assignment
;
35 this->condition
= condition
;
39 ir_expression::ir_expression(int op
, const struct glsl_type
*type
,
40 ir_rvalue
*op0
, ir_rvalue
*op1
)
42 this->ir_type
= ir_type_expression
;
44 this->operation
= ir_expression_operation(op
);
45 this->operands
[0] = op0
;
46 this->operands
[1] = op1
;
50 ir_expression::get_num_operands(ir_expression_operation op
)
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 */
74 1, /* ir_unop_trunc */
76 1, /* ir_unop_floor */
77 1, /* ir_unop_fract */
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 */
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 */
104 2, /* ir_binop_logic_and */
105 2, /* ir_binop_logic_xor */
106 2, /* ir_binop_logic_or */
108 2, /* ir_binop_dot */
109 2, /* ir_binop_cross */
110 2, /* ir_binop_min */
111 2, /* ir_binop_max */
113 2, /* ir_binop_pow */
116 assert(sizeof(num_operands
) / sizeof(num_operands
[0]) == ir_binop_pow
+ 1);
118 return num_operands
[op
];
121 static const char *const operator_strs
[] = {
175 const char *ir_expression::operator_string()
177 assert((unsigned int) operation
<=
178 sizeof(operator_strs
) / sizeof(operator_strs
[0]));
179 return operator_strs
[operation
];
182 ir_expression_operation
183 ir_expression::get_operator(const char *str
)
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
;
190 return (ir_expression_operation
) -1;
193 ir_constant::ir_constant()
195 this->ir_type
= ir_type_constant
;
198 ir_constant::ir_constant(const struct glsl_type
*type
,
199 const ir_constant_data
*data
)
201 assert((type
->base_type
>= GLSL_TYPE_UINT
)
202 && (type
->base_type
<= GLSL_TYPE_BOOL
));
204 this->ir_type
= ir_type_constant
;
206 memcpy(& this->value
, data
, sizeof(this->value
));
209 ir_constant::ir_constant(float f
)
211 this->ir_type
= ir_type_constant
;
212 this->type
= glsl_type::float_type
;
213 this->value
.f
[0] = f
;
216 ir_constant::ir_constant(unsigned int u
)
218 this->ir_type
= ir_type_constant
;
219 this->type
= glsl_type::uint_type
;
220 this->value
.u
[0] = u
;
223 ir_constant::ir_constant(int i
)
225 this->ir_type
= ir_type_constant
;
226 this->type
= glsl_type::int_type
;
227 this->value
.i
[0] = i
;
230 ir_constant::ir_constant(bool b
)
232 this->ir_type
= ir_type_constant
;
233 this->type
= glsl_type::bool_type
;
234 this->value
.b
[0] = b
;
237 ir_constant::ir_constant(const ir_constant
*c
, unsigned i
)
239 this->ir_type
= ir_type_constant
;
240 this->type
= c
->type
->get_base_type();
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;
251 ir_constant::ir_constant(const struct glsl_type
*type
, exec_list
*value_list
)
253 this->ir_type
= ir_type_constant
;
256 assert(type
->is_scalar() || type
->is_vector() || type
->is_matrix()
257 || type
->is_record() || type
->is_array());
259 if (type
->is_array()) {
260 this->array_elements
= talloc_array(this, ir_constant
*, type
->length
);
262 foreach_list(node
, value_list
) {
263 ir_constant
*value
= (ir_constant
*) node
;
264 assert(value
->as_constant() != NULL
);
266 this->array_elements
[i
++] = value
;
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.
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?
280 if (type
->is_record()) {
281 value_list
->move_nodes_to(& this->components
);
286 ir_constant
*value
= (ir_constant
*) (value_list
->head
);
288 /* Use each component from each entry in the value_list to initialize one
289 * component of the constant being constructed.
291 for (unsigned i
= 0; i
< type
->components(); /* empty */) {
292 assert(value
->as_constant() != NULL
);
293 assert(!value
->is_tail_sentinal());
295 for (unsigned j
= 0; j
< value
->type
->components(); j
++) {
296 switch (type
->base_type
) {
298 this->value
.u
[i
] = value
->get_uint_component(j
);
301 this->value
.i
[i
] = value
->get_int_component(j
);
303 case GLSL_TYPE_FLOAT
:
304 this->value
.f
[i
] = value
->get_float_component(j
);
307 this->value
.b
[i
] = value
->get_bool_component(j
);
310 /* FINISHME: What to do? Exceptions are not the answer.
316 if (i
>= type
->components())
320 value
= (ir_constant
*) value
->next
;
325 ir_constant::get_bool_component(unsigned i
) const
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;
335 /* Must return something to make the compiler happy. This is clearly an
342 ir_constant::get_float_component(unsigned i
) const
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;
352 /* Must return something to make the compiler happy. This is clearly an
359 ir_constant::get_int_component(unsigned i
) const
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;
369 /* Must return something to make the compiler happy. This is clearly an
376 ir_constant::get_uint_component(unsigned i
) const
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;
386 /* Must return something to make the compiler happy. This is clearly an
393 ir_constant::get_array_element(unsigned i
) const
395 assert(this->type
->is_array());
396 assert(i
< this->type
->length
);
398 return array_elements
[i
];
402 ir_constant::get_record_field(const char *name
)
404 int idx
= this->type
->field_index(name
);
409 if (this->components
.is_empty())
412 exec_node
*node
= this->components
.head
;
413 for (int i
= 0; i
< idx
; i
++) {
416 /* If the end of the list is encountered before the element matching the
417 * requested field is found, return NULL.
419 if (node
->is_tail_sentinal())
423 return (ir_constant
*) node
;
428 ir_constant::has_value(const ir_constant
*c
) const
430 if (this->type
!= c
->type
)
433 if (this->type
->is_array()) {
434 for (unsigned i
= 0; i
< this->type
->length
; i
++) {
435 if (this->array_elements
[i
]->has_value(c
->array_elements
[i
]))
441 if (this->type
->base_type
== GLSL_TYPE_STRUCT
) {
442 const exec_node
*a_node
= this->components
.head
;
443 const exec_node
*b_node
= c
->components
.head
;
445 while (!a_node
->is_tail_sentinal()) {
446 assert(!b_node
->is_tail_sentinal());
448 const ir_constant
*const a_field
= (ir_constant
*) a_node
;
449 const ir_constant
*const b_field
= (ir_constant
*) b_node
;
451 if (!a_field
->has_value(b_field
))
454 a_node
= a_node
->next
;
455 b_node
= b_node
->next
;
461 for (unsigned i
= 0; i
< this->type
->components(); i
++) {
462 switch (this->type
->base_type
) {
464 if (this->value
.u
[i
] != c
->value
.u
[i
])
468 if (this->value
.i
[i
] != c
->value
.i
[i
])
471 case GLSL_TYPE_FLOAT
:
472 if (this->value
.f
[i
] != c
->value
.f
[i
])
476 if (this->value
.b
[i
] != c
->value
.b
[i
])
480 assert(!"Should not get here.");
488 ir_dereference_variable::ir_dereference_variable(ir_variable
*var
)
490 this->ir_type
= ir_type_dereference_variable
;
492 this->type
= (var
!= NULL
) ? var
->type
: glsl_type::error_type
;
496 ir_dereference_array::ir_dereference_array(ir_rvalue
*value
,
497 ir_rvalue
*array_index
)
499 this->ir_type
= ir_type_dereference_array
;
500 this->array_index
= array_index
;
501 this->set_array(value
);
505 ir_dereference_array::ir_dereference_array(ir_variable
*var
,
506 ir_rvalue
*array_index
)
508 void *ctx
= talloc_parent(var
);
510 this->ir_type
= ir_type_dereference_array
;
511 this->array_index
= array_index
;
512 this->set_array(new(ctx
) ir_dereference_variable(var
));
517 ir_dereference_array::set_array(ir_rvalue
*value
)
520 this->type
= glsl_type::error_type
;
522 if (this->array
!= NULL
) {
523 const glsl_type
*const vt
= this->array
->type
;
525 if (vt
->is_array()) {
526 type
= vt
->element_type();
527 } else if (vt
->is_matrix()) {
528 type
= vt
->column_type();
529 } else if (vt
->is_vector()) {
530 type
= vt
->get_base_type();
536 ir_dereference_record::ir_dereference_record(ir_rvalue
*value
,
539 this->ir_type
= ir_type_dereference_record
;
540 this->record
= value
;
541 this->field
= talloc_strdup(this, field
);
542 this->type
= (this->record
!= NULL
)
543 ? this->record
->type
->field_type(field
) : glsl_type::error_type
;
547 ir_dereference_record::ir_dereference_record(ir_variable
*var
,
550 void *ctx
= talloc_parent(var
);
552 this->ir_type
= ir_type_dereference_record
;
553 this->record
= new(ctx
) ir_dereference_variable(var
);
554 this->field
= talloc_strdup(this, field
);
555 this->type
= (this->record
!= NULL
)
556 ? this->record
->type
->field_type(field
) : glsl_type::error_type
;
561 ir_dereference::is_lvalue()
563 ir_variable
*var
= this->variable_referenced();
565 /* Every l-value derference chain eventually ends in a variable.
567 if ((var
== NULL
) || var
->read_only
)
570 if (this->type
->is_array() && !var
->array_lvalue
)
577 const char *tex_opcode_strs
[] = { "tex", "txb", "txl", "txd", "txf" };
579 const char *ir_texture::opcode_string()
581 assert((unsigned int) op
<=
582 sizeof(tex_opcode_strs
) / sizeof(tex_opcode_strs
[0]));
583 return tex_opcode_strs
[op
];
587 ir_texture::get_opcode(const char *str
)
589 const int count
= sizeof(tex_opcode_strs
) / sizeof(tex_opcode_strs
[0]);
590 for (int op
= 0; op
< count
; op
++) {
591 if (strcmp(str
, tex_opcode_strs
[op
]) == 0)
592 return (ir_texture_opcode
) op
;
594 return (ir_texture_opcode
) -1;
599 ir_texture::set_sampler(ir_dereference
*sampler
)
601 assert(sampler
!= NULL
);
602 this->sampler
= sampler
;
604 switch (sampler
->type
->sampler_type
) {
605 case GLSL_TYPE_FLOAT
:
606 this->type
= glsl_type::vec4_type
;
609 this->type
= glsl_type::ivec4_type
;
612 this->type
= glsl_type::uvec4_type
;
619 ir_swizzle::init_mask(const unsigned *comp
, unsigned count
)
621 assert((count
>= 1) && (count
<= 4));
623 memset(&this->mask
, 0, sizeof(this->mask
));
624 this->mask
.num_components
= count
;
626 unsigned dup_mask
= 0;
629 assert(comp
[3] <= 3);
630 dup_mask
|= (1U << comp
[3])
631 & ((1U << comp
[0]) | (1U << comp
[1]) | (1U << comp
[2]));
632 this->mask
.w
= comp
[3];
635 assert(comp
[2] <= 3);
636 dup_mask
|= (1U << comp
[2])
637 & ((1U << comp
[0]) | (1U << comp
[1]));
638 this->mask
.z
= comp
[2];
641 assert(comp
[1] <= 3);
642 dup_mask
|= (1U << comp
[1])
644 this->mask
.y
= comp
[1];
647 assert(comp
[0] <= 3);
648 this->mask
.x
= comp
[0];
651 this->mask
.has_duplicates
= dup_mask
!= 0;
653 /* Based on the number of elements in the swizzle and the base type
654 * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
655 * generate the type of the resulting value.
657 type
= glsl_type::get_instance(val
->type
->base_type
, mask
.num_components
, 1);
660 ir_swizzle::ir_swizzle(ir_rvalue
*val
, unsigned x
, unsigned y
, unsigned z
,
661 unsigned w
, unsigned count
)
664 const unsigned components
[4] = { x
, y
, z
, w
};
665 this->ir_type
= ir_type_swizzle
;
666 this->init_mask(components
, count
);
669 ir_swizzle::ir_swizzle(ir_rvalue
*val
, const unsigned *comp
,
673 this->ir_type
= ir_type_swizzle
;
674 this->init_mask(comp
, count
);
677 ir_swizzle::ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
)
679 this->ir_type
= ir_type_swizzle
;
682 this->type
= glsl_type::get_instance(val
->type
->base_type
,
683 mask
.num_components
, 1);
692 ir_swizzle::create(ir_rvalue
*val
, const char *str
, unsigned vector_length
)
694 void *ctx
= talloc_parent(val
);
696 /* For each possible swizzle character, this table encodes the value in
697 * \c idx_map that represents the 0th element of the vector. For invalid
698 * swizzle characters (e.g., 'k'), a special value is used that will allow
699 * detection of errors.
701 static const unsigned char base_idx
[26] = {
702 /* a b c d e f g h i j k l m */
703 R
, R
, I
, I
, I
, I
, R
, I
, I
, I
, I
, I
, I
,
704 /* n o p q r s t u v w x y z */
705 I
, I
, S
, S
, R
, S
, S
, I
, I
, X
, X
, X
, X
708 /* Each valid swizzle character has an entry in the previous table. This
709 * table encodes the base index encoded in the previous table plus the actual
710 * index of the swizzle character. When processing swizzles, the first
711 * character in the string is indexed in the previous table. Each character
712 * in the string is indexed in this table, and the value found there has the
713 * value form the first table subtracted. The result must be on the range
716 * For example, the string "wzyx" will get X from the first table. Each of
717 * the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
718 * subtraction, the swizzle values are { 3, 2, 1, 0 }.
720 * The string "wzrg" will get X from the first table. Each of the characters
721 * will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
722 * swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
723 * [0,3], the error is detected.
725 static const unsigned char idx_map
[26] = {
726 /* a b c d e f g h i j k l m */
727 R
+3, R
+2, 0, 0, 0, 0, R
+1, 0, 0, 0, 0, 0, 0,
728 /* n o p q r s t u v w x y z */
729 0, 0, S
+2, S
+3, R
+0, S
+0, S
+1, 0, 0, X
+3, X
+0, X
+1, X
+2
732 int swiz_idx
[4] = { 0, 0, 0, 0 };
736 /* Validate the first character in the swizzle string and look up the base
737 * index value as described above.
739 if ((str
[0] < 'a') || (str
[0] > 'z'))
742 const unsigned base
= base_idx
[str
[0] - 'a'];
745 for (i
= 0; (i
< 4) && (str
[i
] != '\0'); i
++) {
746 /* Validate the next character, and, as described above, convert it to a
749 if ((str
[i
] < 'a') || (str
[i
] > 'z'))
752 swiz_idx
[i
] = idx_map
[str
[i
] - 'a'] - base
;
753 if ((swiz_idx
[i
] < 0) || (swiz_idx
[i
] >= (int) vector_length
))
760 return new(ctx
) ir_swizzle(val
, swiz_idx
[0], swiz_idx
[1], swiz_idx
[2],
770 ir_swizzle::variable_referenced()
772 return this->val
->variable_referenced();
776 ir_variable::ir_variable(const struct glsl_type
*type
, const char *name
,
777 ir_variable_mode mode
)
778 : max_array_access(0), read_only(false), centroid(false), invariant(false),
779 shader_in(false), shader_out(false),
780 mode(mode
), interpolation(ir_var_smooth
), array_lvalue(false)
782 this->ir_type
= ir_type_variable
;
784 this->name
= talloc_strdup(this, name
);
786 this->warn_extension
= NULL
;
787 this->constant_value
= NULL
;
789 if (type
&& type
->base_type
== GLSL_TYPE_SAMPLER
)
790 this->read_only
= true;
795 ir_variable::interpolation_string() const
797 if (!this->shader_in
&& !this->shader_out
)
800 switch (this->interpolation
) {
801 case ir_var_smooth
: return "smooth";
802 case ir_var_flat
: return "flat";
803 case ir_var_noperspective
: return "noperspective";
806 assert(!"Should not get here.");
812 ir_variable::component_slots() const
814 /* FINISHME: Sparsely accessed arrays require fewer slots. */
815 return this->type
->component_slots();
819 ir_function_signature::ir_function_signature(const glsl_type
*return_type
)
820 : return_type(return_type
), is_defined(false), _function(NULL
)
822 this->ir_type
= ir_type_function_signature
;
827 ir_function_signature::qualifiers_match(exec_list
*params
)
829 exec_list_iterator iter_a
= parameters
.iterator();
830 exec_list_iterator iter_b
= params
->iterator();
832 /* check that the qualifiers match. */
833 while (iter_a
.has_next()) {
834 ir_variable
*a
= (ir_variable
*)iter_a
.get();
835 ir_variable
*b
= (ir_variable
*)iter_b
.get();
837 if (a
->read_only
!= b
->read_only
||
838 a
->mode
!= b
->mode
||
839 a
->interpolation
!= b
->interpolation
||
840 a
->centroid
!= b
->centroid
) {
842 /* parameter a's qualifiers don't match */
854 ir_function_signature::replace_parameters(exec_list
*new_params
)
856 /* Destroy all of the previous parameter information. If the previous
857 * parameter information comes from the function prototype, it may either
858 * specify incorrect parameter names or not have names at all.
860 foreach_iter(exec_list_iterator
, iter
, parameters
) {
861 assert(((ir_instruction
*) iter
.get())->as_variable() != NULL
);
866 new_params
->move_nodes_to(¶meters
);
870 ir_function::ir_function(const char *name
)
872 this->ir_type
= ir_type_function
;
873 this->name
= talloc_strdup(this, name
);
878 ir_call::get_error_instruction(void *ctx
)
880 ir_call
*call
= new(ctx
) ir_call
;
882 call
->type
= glsl_type::error_type
;
887 ir_call::set_callee(ir_function_signature
*sig
)
889 assert((this->type
== NULL
) || (this->type
== sig
->return_type
));
895 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
)
897 foreach_iter(exec_list_iterator
, iter
, *list
) {
898 ((ir_instruction
*)iter
.get())->accept(visitor
);
904 steal_memory(ir_instruction
*ir
, void *new_ctx
)
906 talloc_steal(new_ctx
, ir
);
911 reparent_ir(exec_list
*list
, void *mem_ctx
)
913 foreach_list(node
, list
) {
914 visit_tree((ir_instruction
*) node
, steal_memory
, mem_ctx
);