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
,
34 this->condition
= condition
;
38 ir_expression::ir_expression(int op
, const struct glsl_type
*type
,
39 ir_rvalue
*op0
, ir_rvalue
*op1
)
42 this->operation
= ir_expression_operation(op
);
43 this->operands
[0] = op0
;
44 this->operands
[1] = op1
;
48 ir_expression::get_num_operands(ir_expression_operation op
)
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 */
72 1, /* ir_unop_trunc */
74 1, /* ir_unop_floor */
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 */
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 */
101 2, /* ir_binop_logic_and */
102 2, /* ir_binop_logic_xor */
103 2, /* ir_binop_logic_or */
105 2, /* ir_binop_dot */
106 2, /* ir_binop_min */
107 2, /* ir_binop_max */
109 2, /* ir_binop_pow */
112 assert(sizeof(num_operands
) / sizeof(num_operands
[0]) == ir_binop_pow
+ 1);
114 return num_operands
[op
];
117 static const char *const operator_strs
[] = {
169 const char *ir_expression::operator_string()
171 assert((unsigned int) operation
<=
172 sizeof(operator_strs
) / sizeof(operator_strs
[0]));
173 return operator_strs
[operation
];
176 ir_expression_operation
177 ir_expression::get_operator(const char *str
)
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
;
184 return (ir_expression_operation
) -1;
187 ir_constant::ir_constant()
192 ir_constant::ir_constant(const struct glsl_type
*type
,
193 const ir_constant_data
*data
)
195 assert((type
->base_type
>= GLSL_TYPE_UINT
)
196 && (type
->base_type
<= GLSL_TYPE_BOOL
));
199 memcpy(& this->value
, data
, sizeof(this->value
));
202 ir_constant::ir_constant(float f
)
204 this->type
= glsl_type::float_type
;
205 this->value
.f
[0] = f
;
208 ir_constant::ir_constant(unsigned int u
)
210 this->type
= glsl_type::uint_type
;
211 this->value
.u
[0] = u
;
214 ir_constant::ir_constant(int i
)
216 this->type
= glsl_type::int_type
;
217 this->value
.i
[0] = i
;
220 ir_constant::ir_constant(bool b
)
222 this->type
= glsl_type::bool_type
;
223 this->value
.b
[0] = b
;
226 ir_constant::ir_constant(const ir_constant
*c
, unsigned i
)
228 this->type
= c
->type
->get_base_type();
230 switch (this->type
->base_type
) {
231 case GLSL_TYPE_UINT
: this->value
.u
[0] = c
->value
.u
[i
]; break;
232 case GLSL_TYPE_INT
: this->value
.i
[0] = c
->value
.i
[i
]; break;
233 case GLSL_TYPE_FLOAT
: this->value
.f
[0] = c
->value
.f
[i
]; break;
234 case GLSL_TYPE_BOOL
: this->value
.b
[0] = c
->value
.b
[i
]; break;
235 default: assert(!"Should not get here."); break;
239 ir_constant::ir_constant(const struct glsl_type
*type
, exec_list
*value_list
)
243 /* FINISHME: Support array types. */
244 assert(type
->is_scalar() || type
->is_vector() || type
->is_matrix()
245 || type
->is_record());
247 /* If the constant is a record, the types of each of the entries in
248 * value_list must be a 1-for-1 match with the structure components. Each
249 * entry must also be a constant. Just move the nodes from the value_list
250 * to the list in the ir_constant.
252 /* FINISHME: Should there be some type checking and / or assertions here? */
253 /* FINISHME: Should the new constant take ownership of the nodes from
254 * FINISHME: value_list, or should it make copies?
256 if (type
->is_record()) {
257 value_list
->move_nodes_to(& this->components
);
262 ir_constant
*value
= (ir_constant
*) (value_list
->head
);
264 /* Use each component from each entry in the value_list to initialize one
265 * component of the constant being constructed.
267 for (unsigned i
= 0; i
< type
->components(); /* empty */) {
268 assert(value
->as_constant() != NULL
);
269 assert(!value
->is_tail_sentinal());
271 for (unsigned j
= 0; j
< value
->type
->components(); j
++) {
272 switch (type
->base_type
) {
274 this->value
.u
[i
] = value
->get_uint_component(j
);
277 this->value
.i
[i
] = value
->get_int_component(j
);
279 case GLSL_TYPE_FLOAT
:
280 this->value
.f
[i
] = value
->get_float_component(j
);
283 this->value
.b
[i
] = value
->get_bool_component(j
);
286 /* FINISHME: What to do? Exceptions are not the answer.
292 if (i
>= type
->components())
296 value
= (ir_constant
*) value
->next
;
303 switch (this->type
->base_type
) {
306 case GLSL_TYPE_FLOAT
:
308 return new ir_constant(this->type
, &this->value
);
310 case GLSL_TYPE_STRUCT
: {
311 ir_constant
*c
= new ir_constant
;
313 c
->type
= this->type
;
314 for (exec_node
*node
= this->components
.head
315 ; !node
->is_tail_sentinal()
316 ; node
= node
->next
) {
317 ir_constant
*const orig
= (ir_constant
*) node
;
319 c
->components
.push_tail(orig
->clone());
326 assert(!"Should not get here."); break;
332 ir_constant::get_bool_component(unsigned i
) const
334 switch (this->type
->base_type
) {
335 case GLSL_TYPE_UINT
: return this->value
.u
[i
] != 0;
336 case GLSL_TYPE_INT
: return this->value
.i
[i
] != 0;
337 case GLSL_TYPE_FLOAT
: return ((int)this->value
.f
[i
]) != 0;
338 case GLSL_TYPE_BOOL
: return this->value
.b
[i
];
339 default: assert(!"Should not get here."); break;
342 /* Must return something to make the compiler happy. This is clearly an
349 ir_constant::get_float_component(unsigned i
) const
351 switch (this->type
->base_type
) {
352 case GLSL_TYPE_UINT
: return (float) this->value
.u
[i
];
353 case GLSL_TYPE_INT
: return (float) this->value
.i
[i
];
354 case GLSL_TYPE_FLOAT
: return this->value
.f
[i
];
355 case GLSL_TYPE_BOOL
: return this->value
.b
[i
] ? 1.0 : 0.0;
356 default: assert(!"Should not get here."); break;
359 /* Must return something to make the compiler happy. This is clearly an
366 ir_constant::get_int_component(unsigned i
) const
368 switch (this->type
->base_type
) {
369 case GLSL_TYPE_UINT
: return this->value
.u
[i
];
370 case GLSL_TYPE_INT
: return this->value
.i
[i
];
371 case GLSL_TYPE_FLOAT
: return (int) this->value
.f
[i
];
372 case GLSL_TYPE_BOOL
: return this->value
.b
[i
] ? 1 : 0;
373 default: assert(!"Should not get here."); break;
376 /* Must return something to make the compiler happy. This is clearly an
383 ir_constant::get_uint_component(unsigned i
) const
385 switch (this->type
->base_type
) {
386 case GLSL_TYPE_UINT
: return this->value
.u
[i
];
387 case GLSL_TYPE_INT
: return this->value
.i
[i
];
388 case GLSL_TYPE_FLOAT
: return (unsigned) this->value
.f
[i
];
389 case GLSL_TYPE_BOOL
: return this->value
.b
[i
] ? 1 : 0;
390 default: assert(!"Should not get here."); break;
393 /* Must return something to make the compiler happy. This is clearly an
401 ir_constant::get_record_field(const char *name
)
403 int idx
= this->type
->field_index(name
);
408 if (this->components
.is_empty())
411 exec_node
*node
= this->components
.head
;
412 for (int i
= 0; i
< idx
; i
++) {
415 /* If the end of the list is encountered before the element matching the
416 * requested field is found, return NULL.
418 if (node
->is_tail_sentinal())
422 return (ir_constant
*) node
;
426 ir_dereference_variable::ir_dereference_variable(ir_variable
*var
)
429 this->type
= (var
!= NULL
) ? var
->type
: glsl_type::error_type
;
433 ir_dereference_array::ir_dereference_array(ir_rvalue
*value
,
434 ir_rvalue
*array_index
)
436 this->array_index
= array_index
;
437 this->set_array(value
);
441 ir_dereference_array::ir_dereference_array(ir_variable
*var
,
442 ir_rvalue
*array_index
)
444 this->array_index
= array_index
;
445 this->set_array(new ir_dereference_variable(var
));
450 ir_dereference_array::set_array(ir_rvalue
*value
)
453 this->type
= glsl_type::error_type
;
455 if (this->array
!= NULL
) {
456 const glsl_type
*const vt
= this->array
->type
;
458 if (vt
->is_array()) {
459 type
= vt
->element_type();
460 } else if (vt
->is_matrix()) {
461 type
= vt
->column_type();
462 } else if (vt
->is_vector()) {
463 type
= vt
->get_base_type();
469 ir_dereference_record::ir_dereference_record(ir_rvalue
*value
,
472 this->record
= value
;
474 this->type
= (this->record
!= NULL
)
475 ? this->record
->type
->field_type(field
) : glsl_type::error_type
;
479 ir_dereference_record::ir_dereference_record(ir_variable
*var
,
482 this->record
= new ir_dereference_variable(var
);
484 this->type
= (this->record
!= NULL
)
485 ? this->record
->type
->field_type(field
) : glsl_type::error_type
;
490 ir_dereference::is_lvalue()
492 ir_variable
*var
= this->variable_referenced();
494 /* Every l-value derference chain eventually ends in a variable.
496 if ((var
== NULL
) || var
->read_only
)
499 if (this->type
->is_array() && !var
->array_lvalue
)
506 const char *tex_opcode_strs
[] = { "tex", "txb", "txl", "txd", "txf" };
508 const char *ir_texture::opcode_string()
510 assert((unsigned int) op
<=
511 sizeof(tex_opcode_strs
) / sizeof(tex_opcode_strs
[0]));
512 return tex_opcode_strs
[op
];
516 ir_texture::get_opcode(const char *str
)
518 const int count
= sizeof(tex_opcode_strs
) / sizeof(tex_opcode_strs
[0]);
519 for (int op
= 0; op
< count
; op
++) {
520 if (strcmp(str
, tex_opcode_strs
[op
]) == 0)
521 return (ir_texture_opcode
) op
;
523 return (ir_texture_opcode
) -1;
528 ir_texture::set_sampler(ir_dereference
*sampler
)
530 assert(sampler
!= NULL
);
531 this->sampler
= sampler
;
533 switch (sampler
->type
->sampler_type
) {
534 case GLSL_TYPE_FLOAT
:
535 this->type
= glsl_type::vec4_type
;
538 this->type
= glsl_type::ivec4_type
;
541 this->type
= glsl_type::uvec4_type
;
547 ir_swizzle::ir_swizzle(ir_rvalue
*val
, unsigned x
, unsigned y
, unsigned z
,
548 unsigned w
, unsigned count
)
551 assert((count
>= 1) && (count
<= 4));
553 const unsigned dup_mask
= 0
554 | ((count
> 1) ? ((1U << y
) & ((1U << x
) )) : 0)
555 | ((count
> 2) ? ((1U << z
) & ((1U << x
) | (1U << y
) )) : 0)
556 | ((count
> 3) ? ((1U << w
) & ((1U << x
) | (1U << y
) | (1U << z
))) : 0);
567 mask
.num_components
= count
;
568 mask
.has_duplicates
= dup_mask
!= 0;
570 /* Based on the number of elements in the swizzle and the base type
571 * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
572 * generate the type of the resulting value.
574 type
= glsl_type::get_instance(val
->type
->base_type
, mask
.num_components
, 1);
577 ir_swizzle::ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
)
581 this->type
= glsl_type::get_instance(val
->type
->base_type
,
582 mask
.num_components
, 1);
591 ir_swizzle::create(ir_rvalue
*val
, const char *str
, unsigned vector_length
)
593 /* For each possible swizzle character, this table encodes the value in
594 * \c idx_map that represents the 0th element of the vector. For invalid
595 * swizzle characters (e.g., 'k'), a special value is used that will allow
596 * detection of errors.
598 static const unsigned char base_idx
[26] = {
599 /* a b c d e f g h i j k l m */
600 R
, R
, I
, I
, I
, I
, R
, I
, I
, I
, I
, I
, I
,
601 /* n o p q r s t u v w x y z */
602 I
, I
, S
, S
, R
, S
, S
, I
, I
, X
, X
, X
, X
605 /* Each valid swizzle character has an entry in the previous table. This
606 * table encodes the base index encoded in the previous table plus the actual
607 * index of the swizzle character. When processing swizzles, the first
608 * character in the string is indexed in the previous table. Each character
609 * in the string is indexed in this table, and the value found there has the
610 * value form the first table subtracted. The result must be on the range
613 * For example, the string "wzyx" will get X from the first table. Each of
614 * the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
615 * subtraction, the swizzle values are { 3, 2, 1, 0 }.
617 * The string "wzrg" will get X from the first table. Each of the characters
618 * will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
619 * swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
620 * [0,3], the error is detected.
622 static const unsigned char idx_map
[26] = {
623 /* a b c d e f g h i j k l m */
624 R
+3, R
+2, 0, 0, 0, 0, R
+1, 0, 0, 0, 0, 0, 0,
625 /* n o p q r s t u v w x y z */
626 0, 0, S
+2, S
+3, R
+0, S
+0, S
+1, 0, 0, X
+3, X
+0, X
+1, X
+2
629 int swiz_idx
[4] = { 0, 0, 0, 0 };
633 /* Validate the first character in the swizzle string and look up the base
634 * index value as described above.
636 if ((str
[0] < 'a') || (str
[0] > 'z'))
639 const unsigned base
= base_idx
[str
[0] - 'a'];
642 for (i
= 0; (i
< 4) && (str
[i
] != '\0'); i
++) {
643 /* Validate the next character, and, as described above, convert it to a
646 if ((str
[i
] < 'a') || (str
[i
] > 'z'))
649 swiz_idx
[i
] = idx_map
[str
[i
] - 'a'] - base
;
650 if ((swiz_idx
[i
] < 0) || (swiz_idx
[i
] >= (int) vector_length
))
657 return new ir_swizzle(val
, swiz_idx
[0], swiz_idx
[1], swiz_idx
[2],
667 ir_swizzle::variable_referenced()
669 return this->val
->variable_referenced();
672 ir_variable::ir_variable(const struct glsl_type
*type
, const char *name
)
673 : max_array_access(0), read_only(false), centroid(false), invariant(false),
674 mode(ir_var_auto
), interpolation(ir_var_smooth
)
678 this->constant_value
= NULL
;
680 if (type
&& type
->base_type
== GLSL_TYPE_SAMPLER
)
681 this->read_only
= true;
685 ir_function_signature::ir_function_signature(const glsl_type
*return_type
)
686 : return_type(return_type
), is_defined(false)
693 ir_function_signature::qualifiers_match(exec_list
*params
)
695 exec_list_iterator iter_a
= parameters
.iterator();
696 exec_list_iterator iter_b
= params
->iterator();
698 /* check that the qualifiers match. */
699 while (iter_a
.has_next()) {
700 ir_variable
*a
= (ir_variable
*)iter_a
.get();
701 ir_variable
*b
= (ir_variable
*)iter_b
.get();
703 if (a
->read_only
!= b
->read_only
||
704 a
->mode
!= b
->mode
||
705 a
->interpolation
!= b
->interpolation
||
706 a
->centroid
!= b
->centroid
) {
708 /* parameter a's qualifiers don't match */
720 ir_function_signature::replace_parameters(exec_list
*new_params
)
722 /* Destroy all of the previous parameter information. If the previous
723 * parameter information comes from the function prototype, it may either
724 * specify incorrect parameter names or not have names at all.
726 foreach_iter(exec_list_iterator
, iter
, parameters
) {
727 assert(((ir_instruction
*) iter
.get())->as_variable() != NULL
);
730 delete (ir_instruction
*) iter
.get();
733 new_params
->move_nodes_to(¶meters
);
737 ir_function::ir_function(const char *name
)
745 ir_call::get_error_instruction()
747 ir_call
*call
= new ir_call
;
749 call
->type
= glsl_type::error_type
;
754 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
)
756 foreach_iter(exec_list_iterator
, iter
, *list
) {
757 ((ir_instruction
*)iter
.get())->accept(visitor
);