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 */
82 2, /* ir_binop_less */
83 2, /* ir_binop_greater */
84 2, /* ir_binop_lequal */
85 2, /* ir_binop_gequal */
86 2, /* ir_binop_equal */
87 2, /* ir_binop_nequal */
89 2, /* ir_binop_lshift */
90 2, /* ir_binop_rshift */
91 2, /* ir_binop_bit_and */
92 2, /* ir_binop_bit_xor */
93 2, /* ir_binop_bit_or */
95 2, /* ir_binop_logic_and */
96 2, /* ir_binop_logic_xor */
97 2, /* ir_binop_logic_or */
100 2, /* ir_binop_min */
101 2, /* ir_binop_max */
103 2, /* ir_binop_pow */
106 assert(sizeof(num_operands
) / sizeof(num_operands
[0]) == ir_binop_pow
+ 1);
108 return num_operands
[op
];
111 static const char *const operator_strs
[] = {
159 const char *ir_expression::operator_string()
161 assert((unsigned int) operation
<=
162 sizeof(operator_strs
) / sizeof(operator_strs
[0]));
163 return operator_strs
[operation
];
166 ir_expression_operation
167 ir_expression::get_operator(const char *str
)
169 const int operator_count
= sizeof(operator_strs
) / sizeof(operator_strs
[0]);
170 for (int op
= 0; op
< operator_count
; op
++) {
171 if (strcmp(str
, operator_strs
[op
]) == 0)
172 return (ir_expression_operation
) op
;
174 return (ir_expression_operation
) -1;
177 ir_constant::ir_constant(const struct glsl_type
*type
, const void *data
)
182 switch (type
->base_type
) {
183 case GLSL_TYPE_UINT
: size
= sizeof(this->value
.u
[0]); break;
184 case GLSL_TYPE_INT
: size
= sizeof(this->value
.i
[0]); break;
185 case GLSL_TYPE_FLOAT
: size
= sizeof(this->value
.f
[0]); break;
186 case GLSL_TYPE_BOOL
: size
= sizeof(this->value
.b
[0]); break;
188 /* FINISHME: What to do? Exceptions are not the answer.
193 memcpy(& this->value
, data
, size
* type
->components());
196 ir_constant::ir_constant(float f
)
198 this->type
= glsl_type::float_type
;
199 this->value
.f
[0] = f
;
202 ir_constant::ir_constant(unsigned int u
)
204 this->type
= glsl_type::uint_type
;
205 this->value
.u
[0] = u
;
208 ir_constant::ir_constant(int i
)
210 this->type
= glsl_type::int_type
;
211 this->value
.i
[0] = i
;
214 ir_constant::ir_constant(bool b
)
216 this->type
= glsl_type::bool_type
;
217 this->value
.b
[0] = b
;
221 ir_dereference::ir_dereference(ir_instruction
*var
)
223 this->mode
= ir_reference_variable
;
225 this->type
= (var
!= NULL
) ? var
->type
: glsl_type::error_type
;
229 ir_dereference::ir_dereference(ir_instruction
*var
,
230 ir_rvalue
*array_index
)
231 : mode(ir_reference_array
), var(var
)
233 type
= glsl_type::error_type
;
236 const glsl_type
*const vt
= var
->type
;
238 if (vt
->is_array()) {
239 type
= vt
->element_type();
240 } else if (vt
->is_matrix()) {
241 type
= vt
->column_type();
242 } else if (vt
->is_vector()) {
243 type
= vt
->get_base_type();
247 this->selector
.array_index
= array_index
;
250 ir_dereference::ir_dereference(ir_instruction
*variable
, const char *field
)
251 : mode(ir_reference_record
), var(variable
)
253 this->selector
.field
= field
;
254 this->type
= (var
!= NULL
)
255 ? var
->type
->field_type(field
) : glsl_type::error_type
;
259 ir_dereference::is_lvalue()
264 ir_variable
*const as_var
= var
->as_variable();
265 if (mode
== ir_reference_variable
) {
269 if (as_var
->type
->is_array() && !as_var
->array_lvalue
)
274 return !as_var
->read_only
;
276 /* Walk up the dereference chain and figure out if the variable is read-only.
278 return this->var
->as_rvalue()->is_lvalue();
281 ir_swizzle::ir_swizzle(ir_rvalue
*val
, unsigned x
, unsigned y
, unsigned z
,
282 unsigned w
, unsigned count
)
285 assert((count
>= 1) && (count
<= 4));
287 const unsigned dup_mask
= 0
288 | ((count
> 1) ? ((1U << y
) & ((1U << x
) )) : 0)
289 | ((count
> 2) ? ((1U << z
) & ((1U << x
) | (1U << y
) )) : 0)
290 | ((count
> 3) ? ((1U << w
) & ((1U << x
) | (1U << y
) | (1U << z
))) : 0);
301 mask
.num_components
= count
;
302 mask
.has_duplicates
= dup_mask
!= 0;
304 /* Based on the number of elements in the swizzle and the base type
305 * (i.e., float, int, unsigned, or bool) of the vector being swizzled,
306 * generate the type of the resulting value.
308 type
= glsl_type::get_instance(val
->type
->base_type
, mask
.num_components
, 1);
311 ir_swizzle::ir_swizzle(ir_rvalue
*val
, ir_swizzle_mask mask
)
315 this->type
= glsl_type::get_instance(val
->type
->base_type
,
316 mask
.num_components
, 1);
325 ir_swizzle::create(ir_rvalue
*val
, const char *str
, unsigned vector_length
)
327 /* For each possible swizzle character, this table encodes the value in
328 * \c idx_map that represents the 0th element of the vector. For invalid
329 * swizzle characters (e.g., 'k'), a special value is used that will allow
330 * detection of errors.
332 static const unsigned char base_idx
[26] = {
333 /* a b c d e f g h i j k l m */
334 R
, R
, I
, I
, I
, I
, R
, I
, I
, I
, I
, I
, I
,
335 /* n o p q r s t u v w x y z */
336 I
, I
, S
, S
, R
, S
, S
, I
, I
, X
, X
, X
, X
339 /* Each valid swizzle character has an entry in the previous table. This
340 * table encodes the base index encoded in the previous table plus the actual
341 * index of the swizzle character. When processing swizzles, the first
342 * character in the string is indexed in the previous table. Each character
343 * in the string is indexed in this table, and the value found there has the
344 * value form the first table subtracted. The result must be on the range
347 * For example, the string "wzyx" will get X from the first table. Each of
348 * the charcaters will get X+3, X+2, X+1, and X+0 from this table. After
349 * subtraction, the swizzle values are { 3, 2, 1, 0 }.
351 * The string "wzrg" will get X from the first table. Each of the characters
352 * will get X+3, X+2, R+0, and R+1 from this table. After subtraction, the
353 * swizzle values are { 3, 2, 4, 5 }. Since 4 and 5 are outside the range
354 * [0,3], the error is detected.
356 static const unsigned char idx_map
[26] = {
357 /* a b c d e f g h i j k l m */
358 R
+3, R
+2, 0, 0, 0, 0, R
+1, 0, 0, 0, 0, 0, 0,
359 /* n o p q r s t u v w x y z */
360 0, 0, S
+2, S
+3, R
+0, S
+0, S
+1, 0, 0, X
+3, X
+0, X
+1, X
+2
363 int swiz_idx
[4] = { 0, 0, 0, 0 };
367 /* Validate the first character in the swizzle string and look up the base
368 * index value as described above.
370 if ((str
[0] < 'a') || (str
[0] > 'z'))
373 const unsigned base
= base_idx
[str
[0] - 'a'];
376 for (i
= 0; (i
< 4) && (str
[i
] != '\0'); i
++) {
377 /* Validate the next character, and, as described above, convert it to a
380 if ((str
[i
] < 'a') || (str
[i
] > 'z'))
383 swiz_idx
[i
] = idx_map
[str
[i
] - 'a'] - base
;
384 if ((swiz_idx
[i
] < 0) || (swiz_idx
[i
] >= (int) vector_length
))
391 return new ir_swizzle(val
, swiz_idx
[0], swiz_idx
[1], swiz_idx
[2],
401 ir_variable::ir_variable(const struct glsl_type
*type
, const char *name
)
402 : max_array_access(0), read_only(false), centroid(false), invariant(false),
403 mode(ir_var_auto
), interpolation(ir_var_smooth
)
407 this->constant_value
= NULL
;
409 if (type
&& type
->base_type
== GLSL_TYPE_SAMPLER
)
410 this->read_only
= true;
414 ir_function_signature::ir_function_signature(const glsl_type
*return_type
)
415 : return_type(return_type
), is_defined(false)
422 ir_function_signature::qualifiers_match(exec_list
*params
)
424 exec_list_iterator iter_a
= parameters
.iterator();
425 exec_list_iterator iter_b
= params
->iterator();
427 /* check that the qualifiers match. */
428 while (iter_a
.has_next()) {
429 ir_variable
*a
= (ir_variable
*)iter_a
.get();
430 ir_variable
*b
= (ir_variable
*)iter_b
.get();
432 if (a
->read_only
!= b
->read_only
||
433 a
->interpolation
!= b
->interpolation
||
434 a
->centroid
!= b
->centroid
) {
436 /* parameter a's qualifiers don't match */
448 ir_function_signature::replace_parameters(exec_list
*new_params
)
450 /* Destroy all of the previous parameter information. If the previous
451 * parameter information comes from the function prototype, it may either
452 * specify incorrect parameter names or not have names at all.
454 foreach_iter(exec_list_iterator
, iter
, parameters
) {
455 assert(((ir_instruction
*) iter
.get())->as_variable() != NULL
);
458 delete (ir_instruction
*) iter
.get();
461 new_params
->move_nodes_to(¶meters
);
465 ir_function::ir_function(const char *name
)
473 ir_call::get_error_instruction()
475 ir_call
*call
= new ir_call
;
477 call
->type
= glsl_type::error_type
;
482 visit_exec_list(exec_list
*list
, ir_visitor
*visitor
)
484 foreach_iter(exec_list_iterator
, iter
, *list
) {
485 ((ir_instruction
*)iter
.get())->accept(visitor
);