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 "glsl_symbol_table.h"
26 #include "glsl_types.h"
28 #include "main/core.h" /* for MIN2 */
31 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
);
34 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
35 struct _mesa_glsl_parse_state
*state
);
38 process_parameters(exec_list
*instructions
, exec_list
*actual_parameters
,
39 exec_list
*parameters
,
40 struct _mesa_glsl_parse_state
*state
)
44 foreach_list_typed(ast_node
, ast
, link
, parameters
) {
45 ir_rvalue
*result
= ast
->hir(instructions
, state
);
47 ir_constant
*const constant
= result
->constant_expression_value();
51 actual_parameters
->push_tail(result
);
60 * Generate a source prototype for a function signature
62 * \param return_type Return type of the function. May be \c NULL.
63 * \param name Name of the function.
64 * \param parameters List of \c ir_instruction nodes representing the
65 * parameter list for the function. This may be either a
66 * formal (\c ir_variable) or actual (\c ir_rvalue)
67 * parameter list. Only the type is used.
70 * A ralloced string representing the prototype of the function.
73 prototype_string(const glsl_type
*return_type
, const char *name
,
74 exec_list
*parameters
)
78 if (return_type
!= NULL
)
79 str
= ralloc_asprintf(NULL
, "%s ", return_type
->name
);
81 ralloc_asprintf_append(&str
, "%s(", name
);
83 const char *comma
= "";
84 foreach_in_list(const ir_variable
, param
, parameters
) {
85 ralloc_asprintf_append(&str
, "%s%s", comma
, param
->type
->name
);
89 ralloc_strcat(&str
, ")");
94 verify_image_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
95 const ir_variable
*formal
, const ir_variable
*actual
)
98 * From the ARB_shader_image_load_store specification:
100 * "The values of image variables qualified with coherent,
101 * volatile, restrict, readonly, or writeonly may not be passed
102 * to functions whose formal parameters lack such
103 * qualifiers. [...] It is legal to have additional qualifiers
104 * on a formal parameter, but not to have fewer."
106 if (actual
->data
.image
.coherent
&& !formal
->data
.image
.coherent
) {
107 _mesa_glsl_error(loc
, state
,
108 "function call parameter `%s' drops "
109 "`coherent' qualifier", formal
->name
);
113 if (actual
->data
.image
._volatile
&& !formal
->data
.image
._volatile
) {
114 _mesa_glsl_error(loc
, state
,
115 "function call parameter `%s' drops "
116 "`volatile' qualifier", formal
->name
);
120 if (actual
->data
.image
.restrict_flag
&& !formal
->data
.image
.restrict_flag
) {
121 _mesa_glsl_error(loc
, state
,
122 "function call parameter `%s' drops "
123 "`restrict' qualifier", formal
->name
);
127 if (actual
->data
.image
.read_only
&& !formal
->data
.image
.read_only
) {
128 _mesa_glsl_error(loc
, state
,
129 "function call parameter `%s' drops "
130 "`readonly' qualifier", formal
->name
);
134 if (actual
->data
.image
.write_only
&& !formal
->data
.image
.write_only
) {
135 _mesa_glsl_error(loc
, state
,
136 "function call parameter `%s' drops "
137 "`writeonly' qualifier", formal
->name
);
145 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
146 * that 'const_in' formal parameters (an extension in our IR) correspond to
147 * ir_constant actual parameters.
150 verify_parameter_modes(_mesa_glsl_parse_state
*state
,
151 ir_function_signature
*sig
,
152 exec_list
&actual_ir_parameters
,
153 exec_list
&actual_ast_parameters
)
155 exec_node
*actual_ir_node
= actual_ir_parameters
.head
;
156 exec_node
*actual_ast_node
= actual_ast_parameters
.head
;
158 foreach_in_list(const ir_variable
, formal
, &sig
->parameters
) {
159 /* The lists must be the same length. */
160 assert(!actual_ir_node
->is_tail_sentinel());
161 assert(!actual_ast_node
->is_tail_sentinel());
163 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_node
;
164 const ast_expression
*const actual_ast
=
165 exec_node_data(ast_expression
, actual_ast_node
, link
);
167 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
170 YYLTYPE loc
= actual_ast
->get_location();
172 /* Verify that 'const_in' parameters are ir_constants. */
173 if (formal
->data
.mode
== ir_var_const_in
&&
174 actual
->ir_type
!= ir_type_constant
) {
175 _mesa_glsl_error(&loc
, state
,
176 "parameter `in %s' must be a constant expression",
181 /* Verify that shader_in parameters are shader inputs */
182 if (formal
->data
.must_be_shader_input
) {
183 ir_variable
*var
= actual
->variable_referenced();
184 if (var
&& var
->data
.mode
!= ir_var_shader_in
) {
185 _mesa_glsl_error(&loc
, state
,
186 "parameter `%s` must be a shader input",
191 if (actual
->ir_type
== ir_type_swizzle
) {
192 _mesa_glsl_error(&loc
, state
,
193 "parameter `%s` must not be swizzled",
199 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
200 if (formal
->data
.mode
== ir_var_function_out
201 || formal
->data
.mode
== ir_var_function_inout
) {
202 const char *mode
= NULL
;
203 switch (formal
->data
.mode
) {
204 case ir_var_function_out
: mode
= "out"; break;
205 case ir_var_function_inout
: mode
= "inout"; break;
206 default: assert(false); break;
209 /* This AST-based check catches errors like f(i++). The IR-based
210 * is_lvalue() is insufficient because the actual parameter at the
211 * IR-level is just a temporary value, which is an l-value.
213 if (actual_ast
->non_lvalue_description
!= NULL
) {
214 _mesa_glsl_error(&loc
, state
,
215 "function parameter '%s %s' references a %s",
217 actual_ast
->non_lvalue_description
);
221 ir_variable
*var
= actual
->variable_referenced();
223 var
->data
.assigned
= true;
225 if (var
&& var
->data
.read_only
) {
226 _mesa_glsl_error(&loc
, state
,
227 "function parameter '%s %s' references the "
228 "read-only variable '%s'",
230 actual
->variable_referenced()->name
);
232 } else if (!actual
->is_lvalue()) {
233 /* Even though ir_binop_vector_extract is not an l-value, let it
234 * slop through. generate_call will handle it correctly.
236 ir_expression
*const expr
= ((ir_rvalue
*) actual
)->as_expression();
238 || expr
->operation
!= ir_binop_vector_extract
239 || !expr
->operands
[0]->is_lvalue()) {
240 _mesa_glsl_error(&loc
, state
,
241 "function parameter '%s %s' is not an lvalue",
248 if (formal
->type
->is_image() &&
249 actual
->variable_referenced()) {
250 if (!verify_image_parameter(&loc
, state
, formal
,
251 actual
->variable_referenced()))
255 actual_ir_node
= actual_ir_node
->next
;
256 actual_ast_node
= actual_ast_node
->next
;
262 fix_parameter(void *mem_ctx
, ir_rvalue
*actual
, const glsl_type
*formal_type
,
263 exec_list
*before_instructions
, exec_list
*after_instructions
,
264 bool parameter_is_inout
)
266 ir_expression
*const expr
= actual
->as_expression();
268 /* If the types match exactly and the parameter is not a vector-extract,
269 * nothing needs to be done to fix the parameter.
271 if (formal_type
== actual
->type
272 && (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
))
275 /* To convert an out parameter, we need to create a temporary variable to
276 * hold the value before conversion, and then perform the conversion after
277 * the function call returns.
279 * This has the effect of transforming code like this:
285 * Into IR that's equivalent to this:
289 * int out_parameter_conversion;
290 * f(out_parameter_conversion);
291 * value = float(out_parameter_conversion);
293 * If the parameter is an ir_expression of ir_binop_vector_extract,
294 * additional conversion is needed in the post-call re-write.
297 new(mem_ctx
) ir_variable(formal_type
, "inout_tmp", ir_var_temporary
);
299 before_instructions
->push_tail(tmp
);
301 /* If the parameter is an inout parameter, copy the value of the actual
302 * parameter to the new temporary. Note that no type conversion is allowed
303 * here because inout parameters must match types exactly.
305 if (parameter_is_inout
) {
306 /* Inout parameters should never require conversion, since that would
307 * require an implicit conversion to exist both to and from the formal
308 * parameter type, and there are no bidirectional implicit conversions.
310 assert (actual
->type
== formal_type
);
312 ir_dereference_variable
*const deref_tmp_1
=
313 new(mem_ctx
) ir_dereference_variable(tmp
);
314 ir_assignment
*const assignment
=
315 new(mem_ctx
) ir_assignment(deref_tmp_1
, actual
);
316 before_instructions
->push_tail(assignment
);
319 /* Replace the parameter in the call with a dereference of the new
322 ir_dereference_variable
*const deref_tmp_2
=
323 new(mem_ctx
) ir_dereference_variable(tmp
);
324 actual
->replace_with(deref_tmp_2
);
327 /* Copy the temporary variable to the actual parameter with optional
328 * type conversion applied.
330 ir_rvalue
*rhs
= new(mem_ctx
) ir_dereference_variable(tmp
);
331 if (actual
->type
!= formal_type
)
332 rhs
= convert_component(rhs
, actual
->type
);
334 ir_rvalue
*lhs
= actual
;
335 if (expr
!= NULL
&& expr
->operation
== ir_binop_vector_extract
) {
336 rhs
= new(mem_ctx
) ir_expression(ir_triop_vector_insert
,
337 expr
->operands
[0]->type
,
338 expr
->operands
[0]->clone(mem_ctx
, NULL
),
340 expr
->operands
[1]->clone(mem_ctx
, NULL
));
341 lhs
= expr
->operands
[0]->clone(mem_ctx
, NULL
);
344 ir_assignment
*const assignment_2
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
345 after_instructions
->push_tail(assignment_2
);
349 * Generate a function call.
351 * For non-void functions, this returns a dereference of the temporary variable
352 * which stores the return value for the call. For void functions, this returns
356 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
357 exec_list
*actual_parameters
,
358 struct _mesa_glsl_parse_state
*state
)
361 exec_list post_call_conversions
;
363 /* Perform implicit conversion of arguments. For out parameters, we need
364 * to place them in a temporary variable and do the conversion after the
365 * call takes place. Since we haven't emitted the call yet, we'll place
366 * the post-call conversions in a temporary exec_list, and emit them later.
368 foreach_two_lists(formal_node
, &sig
->parameters
,
369 actual_node
, actual_parameters
) {
370 ir_rvalue
*actual
= (ir_rvalue
*) actual_node
;
371 ir_variable
*formal
= (ir_variable
*) formal_node
;
373 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
374 switch (formal
->data
.mode
) {
375 case ir_var_const_in
:
376 case ir_var_function_in
: {
378 = convert_component(actual
, formal
->type
);
379 actual
->replace_with(converted
);
382 case ir_var_function_out
:
383 case ir_var_function_inout
:
384 fix_parameter(ctx
, actual
, formal
->type
,
385 instructions
, &post_call_conversions
,
386 formal
->data
.mode
== ir_var_function_inout
);
389 assert (!"Illegal formal parameter mode");
395 /* If the function call is a constant expression, don't generate any
396 * instructions; just generate an ir_constant.
398 * Function calls were first allowed to be constant expressions in GLSL
399 * 1.20 and GLSL ES 3.00.
401 if (state
->is_version(120, 300)) {
402 ir_constant
*value
= sig
->constant_expression_value(actual_parameters
, NULL
);
408 ir_dereference_variable
*deref
= NULL
;
409 if (!sig
->return_type
->is_void()) {
410 /* Create a new temporary to hold the return value. */
413 var
= new(ctx
) ir_variable(sig
->return_type
,
414 ralloc_asprintf(ctx
, "%s_retval",
415 sig
->function_name()),
417 instructions
->push_tail(var
);
419 deref
= new(ctx
) ir_dereference_variable(var
);
421 ir_call
*call
= new(ctx
) ir_call(sig
, deref
, actual_parameters
);
422 instructions
->push_tail(call
);
424 /* Also emit any necessary out-parameter conversions. */
425 instructions
->append_list(&post_call_conversions
);
427 return deref
? deref
->clone(ctx
, NULL
) : NULL
;
431 * Given a function name and parameter list, find the matching signature.
433 static ir_function_signature
*
434 match_function_by_name(const char *name
,
435 exec_list
*actual_parameters
,
436 struct _mesa_glsl_parse_state
*state
)
439 ir_function
*f
= state
->symbols
->get_function(name
);
440 ir_function_signature
*local_sig
= NULL
;
441 ir_function_signature
*sig
= NULL
;
443 /* Is the function hidden by a record type constructor? */
444 if (state
->symbols
->get_type(name
))
445 goto done
; /* no match */
447 /* Is the function hidden by a variable (impossible in 1.10)? */
448 if (!state
->symbols
->separate_function_namespace
449 && state
->symbols
->get_variable(name
))
450 goto done
; /* no match */
453 /* In desktop GL, the presence of a user-defined signature hides any
454 * built-in signatures, so we must ignore them. In contrast, in ES2
455 * user-defined signatures add new overloads, so we must consider them.
457 bool allow_builtins
= state
->es_shader
|| !f
->has_user_signature();
459 /* Look for a match in the local shader. If exact, we're done. */
460 bool is_exact
= false;
461 sig
= local_sig
= f
->matching_signature(state
, actual_parameters
,
462 allow_builtins
, &is_exact
);
470 /* Local shader has no exact candidates; check the built-ins. */
471 _mesa_glsl_initialize_builtin_functions();
472 sig
= _mesa_glsl_find_builtin_function(state
, name
, actual_parameters
);
476 /* If the match is from a linked built-in shader, import the prototype. */
477 if (sig
!= local_sig
) {
479 f
= new(ctx
) ir_function(name
);
480 state
->symbols
->add_global_function(f
);
481 emit_function(state
, f
);
483 f
->add_signature(sig
->clone_prototype(f
, NULL
));
490 print_function_prototypes(_mesa_glsl_parse_state
*state
, YYLTYPE
*loc
,
496 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
497 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
500 char *str
= prototype_string(sig
->return_type
, f
->name
, &sig
->parameters
);
501 _mesa_glsl_error(loc
, state
, " %s", str
);
507 * Raise a "no matching function" error, listing all possible overloads the
508 * compiler considered so developers can figure out what went wrong.
511 no_matching_function_error(const char *name
,
513 exec_list
*actual_parameters
,
514 _mesa_glsl_parse_state
*state
)
516 gl_shader
*sh
= _mesa_glsl_get_builtin_function_shader();
518 if (state
->symbols
->get_function(name
) == NULL
519 && (!state
->uses_builtin_functions
520 || sh
->symbols
->get_function(name
) == NULL
)) {
521 _mesa_glsl_error(loc
, state
, "no function with name '%s'", name
);
523 char *str
= prototype_string(NULL
, name
, actual_parameters
);
524 _mesa_glsl_error(loc
, state
,
525 "no matching function for call to `%s'; candidates are:",
529 print_function_prototypes(state
, loc
, state
->symbols
->get_function(name
));
531 if (state
->uses_builtin_functions
) {
532 print_function_prototypes(state
, loc
, sh
->symbols
->get_function(name
));
538 * Perform automatic type conversion of constructor parameters
540 * This implements the rules in the "Conversion and Scalar Constructors"
541 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
544 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
546 void *ctx
= ralloc_parent(src
);
547 const unsigned a
= desired_type
->base_type
;
548 const unsigned b
= src
->type
->base_type
;
549 ir_expression
*result
= NULL
;
551 if (src
->type
->is_error())
554 assert(a
<= GLSL_TYPE_BOOL
);
555 assert(b
<= GLSL_TYPE_BOOL
);
564 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
566 case GLSL_TYPE_FLOAT
:
567 result
= new(ctx
) ir_expression(ir_unop_f2u
, src
);
570 result
= new(ctx
) ir_expression(ir_unop_i2u
,
571 new(ctx
) ir_expression(ir_unop_b2i
, src
));
578 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
580 case GLSL_TYPE_FLOAT
:
581 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
584 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
588 case GLSL_TYPE_FLOAT
:
591 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
594 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
597 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
604 result
= new(ctx
) ir_expression(ir_unop_i2b
,
605 new(ctx
) ir_expression(ir_unop_u2i
, src
));
608 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
610 case GLSL_TYPE_FLOAT
:
611 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
617 assert(result
!= NULL
);
618 assert(result
->type
== desired_type
);
620 /* Try constant folding; it may fold in the conversion we just added. */
621 ir_constant
*const constant
= result
->constant_expression_value();
622 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
626 * Dereference a specific component from a scalar, vector, or matrix
629 dereference_component(ir_rvalue
*src
, unsigned component
)
631 void *ctx
= ralloc_parent(src
);
632 assert(component
< src
->type
->components());
634 /* If the source is a constant, just create a new constant instead of a
635 * dereference of the existing constant.
637 ir_constant
*constant
= src
->as_constant();
639 return new(ctx
) ir_constant(constant
, component
);
641 if (src
->type
->is_scalar()) {
643 } else if (src
->type
->is_vector()) {
644 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
646 assert(src
->type
->is_matrix());
648 /* Dereference a row of the matrix, then call this function again to get
649 * a specific element from that row.
651 const int c
= component
/ src
->type
->column_type()->vector_elements
;
652 const int r
= component
% src
->type
->column_type()->vector_elements
;
653 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
654 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
656 col
->type
= src
->type
->column_type();
658 return dereference_component(col
, r
);
661 assert(!"Should not get here.");
667 process_vec_mat_constructor(exec_list
*instructions
,
668 const glsl_type
*constructor_type
,
669 YYLTYPE
*loc
, exec_list
*parameters
,
670 struct _mesa_glsl_parse_state
*state
)
674 /* The ARB_shading_language_420pack spec says:
676 * "If an initializer is a list of initializers enclosed in curly braces,
677 * the variable being declared must be a vector, a matrix, an array, or a
680 * int i = { 1 }; // illegal, i is not an aggregate"
682 if (constructor_type
->vector_elements
<= 1) {
683 _mesa_glsl_error(loc
, state
, "aggregates can only initialize vectors, "
684 "matrices, arrays, and structs");
685 return ir_rvalue::error_value(ctx
);
688 exec_list actual_parameters
;
689 const unsigned parameter_count
=
690 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
692 if (parameter_count
== 0
693 || (constructor_type
->is_vector() &&
694 constructor_type
->vector_elements
!= parameter_count
)
695 || (constructor_type
->is_matrix() &&
696 constructor_type
->matrix_columns
!= parameter_count
)) {
697 _mesa_glsl_error(loc
, state
, "%s constructor must have %u parameters",
698 constructor_type
->is_vector() ? "vector" : "matrix",
699 constructor_type
->vector_elements
);
700 return ir_rvalue::error_value(ctx
);
703 bool all_parameters_are_constant
= true;
705 /* Type cast each parameter and, if possible, fold constants. */
706 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
707 ir_rvalue
*result
= ir
;
709 /* Apply implicit conversions (not the scalar constructor rules!). See
710 * the spec quote above. */
711 if (constructor_type
->is_float()) {
712 const glsl_type
*desired_type
=
713 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
714 ir
->type
->vector_elements
,
715 ir
->type
->matrix_columns
);
716 if (result
->type
->can_implicitly_convert_to(desired_type
, state
)) {
717 /* Even though convert_component() implements the constructor
718 * conversion rules (not the implicit conversion rules), its safe
719 * to use it here because we already checked that the implicit
720 * conversion is legal.
722 result
= convert_component(ir
, desired_type
);
726 if (constructor_type
->is_matrix()) {
727 if (result
->type
!= constructor_type
->column_type()) {
728 _mesa_glsl_error(loc
, state
, "type error in matrix constructor: "
729 "expected: %s, found %s",
730 constructor_type
->column_type()->name
,
732 return ir_rvalue::error_value(ctx
);
734 } else if (result
->type
!= constructor_type
->get_scalar_type()) {
735 _mesa_glsl_error(loc
, state
, "type error in vector constructor: "
736 "expected: %s, found %s",
737 constructor_type
->get_scalar_type()->name
,
739 return ir_rvalue::error_value(ctx
);
742 /* Attempt to convert the parameter to a constant valued expression.
743 * After doing so, track whether or not all the parameters to the
744 * constructor are trivially constant valued expressions.
746 ir_rvalue
*const constant
= result
->constant_expression_value();
748 if (constant
!= NULL
)
751 all_parameters_are_constant
= false;
753 ir
->replace_with(result
);
756 if (all_parameters_are_constant
)
757 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
759 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "vec_mat_ctor",
761 instructions
->push_tail(var
);
765 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
766 ir_instruction
*assignment
= NULL
;
768 if (var
->type
->is_matrix()) {
769 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
770 new(ctx
) ir_constant(i
));
771 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
773 /* use writemask rather than index for vector */
774 assert(var
->type
->is_vector());
776 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
777 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, (unsigned)(1 << i
));
780 instructions
->push_tail(assignment
);
785 return new(ctx
) ir_dereference_variable(var
);
790 process_array_constructor(exec_list
*instructions
,
791 const glsl_type
*constructor_type
,
792 YYLTYPE
*loc
, exec_list
*parameters
,
793 struct _mesa_glsl_parse_state
*state
)
796 /* Array constructors come in two forms: sized and unsized. Sized array
797 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
798 * variables. In this case the number of parameters must exactly match the
799 * specified size of the array.
801 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
802 * are vec4 variables. In this case the size of the array being constructed
803 * is determined by the number of parameters.
805 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
807 * "There must be exactly the same number of arguments as the size of
808 * the array being constructed. If no size is present in the
809 * constructor, then the array is explicitly sized to the number of
810 * arguments provided. The arguments are assigned in order, starting at
811 * element 0, to the elements of the constructed array. Each argument
812 * must be the same type as the element type of the array, or be a type
813 * that can be converted to the element type of the array according to
814 * Section 4.1.10 "Implicit Conversions.""
816 exec_list actual_parameters
;
817 const unsigned parameter_count
=
818 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
819 bool is_unsized_array
= constructor_type
->is_unsized_array();
821 if ((parameter_count
== 0) ||
822 (!is_unsized_array
&& (constructor_type
->length
!= parameter_count
))) {
823 const unsigned min_param
= is_unsized_array
824 ? 1 : constructor_type
->length
;
826 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
828 is_unsized_array
? "at least" : "exactly",
829 min_param
, (min_param
<= 1) ? "" : "s");
830 return ir_rvalue::error_value(ctx
);
833 if (is_unsized_array
) {
835 glsl_type::get_array_instance(constructor_type
->element_type(),
837 assert(constructor_type
!= NULL
);
838 assert(constructor_type
->length
== parameter_count
);
841 bool all_parameters_are_constant
= true;
843 /* Type cast each parameter and, if possible, fold constants. */
844 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
845 ir_rvalue
*result
= ir
;
847 /* Apply implicit conversions (not the scalar constructor rules!). See
848 * the spec quote above. */
849 if (constructor_type
->element_type()->is_float()) {
850 const glsl_type
*desired_type
=
851 glsl_type::get_instance(GLSL_TYPE_FLOAT
,
852 ir
->type
->vector_elements
,
853 ir
->type
->matrix_columns
);
854 if (result
->type
->can_implicitly_convert_to(desired_type
, state
)) {
855 /* Even though convert_component() implements the constructor
856 * conversion rules (not the implicit conversion rules), its safe
857 * to use it here because we already checked that the implicit
858 * conversion is legal.
860 result
= convert_component(ir
, desired_type
);
864 if (result
->type
!= constructor_type
->element_type()) {
865 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
866 "expected: %s, found %s",
867 constructor_type
->element_type()->name
,
869 return ir_rvalue::error_value(ctx
);
872 /* Attempt to convert the parameter to a constant valued expression.
873 * After doing so, track whether or not all the parameters to the
874 * constructor are trivially constant valued expressions.
876 ir_rvalue
*const constant
= result
->constant_expression_value();
878 if (constant
!= NULL
)
881 all_parameters_are_constant
= false;
883 ir
->replace_with(result
);
886 if (all_parameters_are_constant
)
887 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
889 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
891 instructions
->push_tail(var
);
894 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
895 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
896 new(ctx
) ir_constant(i
));
898 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
899 instructions
->push_tail(assignment
);
904 return new(ctx
) ir_dereference_variable(var
);
909 * Try to convert a record constructor to a constant expression
912 constant_record_constructor(const glsl_type
*constructor_type
,
913 exec_list
*parameters
, void *mem_ctx
)
915 foreach_in_list(ir_instruction
, node
, parameters
) {
916 ir_constant
*constant
= node
->as_constant();
917 if (constant
== NULL
)
919 node
->replace_with(constant
);
922 return new(mem_ctx
) ir_constant(constructor_type
, parameters
);
927 * Determine if a list consists of a single scalar r-value
930 single_scalar_parameter(exec_list
*parameters
)
932 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
933 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
935 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
940 * Generate inline code for a vector constructor
942 * The generated constructor code will consist of a temporary variable
943 * declaration of the same type as the constructor. A sequence of assignments
944 * from constructor parameters to the temporary will follow.
947 * An \c ir_dereference_variable of the temprorary generated in the constructor
951 emit_inline_vector_constructor(const glsl_type
*type
,
952 exec_list
*instructions
,
953 exec_list
*parameters
,
956 assert(!parameters
->is_empty());
958 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
959 instructions
->push_tail(var
);
961 /* There are two kinds of vector constructors.
963 * - Construct a vector from a single scalar by replicating that scalar to
964 * all components of the vector.
966 * - Construct a vector from an arbirary combination of vectors and
967 * scalars. The components of the constructor parameters are assigned
968 * to the vector in order until the vector is full.
970 const unsigned lhs_components
= type
->components();
971 if (single_scalar_parameter(parameters
)) {
972 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
973 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
975 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
976 const unsigned mask
= (1U << lhs_components
) - 1;
978 assert(rhs
->type
== lhs
->type
);
980 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
981 instructions
->push_tail(inst
);
983 unsigned base_component
= 0;
984 unsigned base_lhs_component
= 0;
985 ir_constant_data data
;
986 unsigned constant_mask
= 0, constant_components
= 0;
988 memset(&data
, 0, sizeof(data
));
990 foreach_in_list(ir_rvalue
, param
, parameters
) {
991 unsigned rhs_components
= param
->type
->components();
993 /* Do not try to assign more components to the vector than it has!
995 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
996 rhs_components
= lhs_components
- base_lhs_component
;
999 const ir_constant
*const c
= param
->as_constant();
1001 for (unsigned i
= 0; i
< rhs_components
; i
++) {
1002 switch (c
->type
->base_type
) {
1003 case GLSL_TYPE_UINT
:
1004 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
1007 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
1009 case GLSL_TYPE_FLOAT
:
1010 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
1012 case GLSL_TYPE_BOOL
:
1013 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
1016 assert(!"Should not get here.");
1021 /* Mask of fields to be written in the assignment.
1023 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
1024 constant_components
+= rhs_components
;
1026 base_component
+= rhs_components
;
1028 /* Advance the component index by the number of components
1029 * that were just assigned.
1031 base_lhs_component
+= rhs_components
;
1034 if (constant_mask
!= 0) {
1035 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1036 const glsl_type
*rhs_type
= glsl_type::get_instance(var
->type
->base_type
,
1037 constant_components
,
1039 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
1041 ir_instruction
*inst
=
1042 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
1043 instructions
->push_tail(inst
);
1047 foreach_in_list(ir_rvalue
, param
, parameters
) {
1048 unsigned rhs_components
= param
->type
->components();
1050 /* Do not try to assign more components to the vector than it has!
1052 if ((rhs_components
+ base_component
) > lhs_components
) {
1053 rhs_components
= lhs_components
- base_component
;
1056 const ir_constant
*const c
= param
->as_constant();
1058 /* Mask of fields to be written in the assignment.
1060 const unsigned write_mask
= ((1U << rhs_components
) - 1)
1063 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1065 /* Generate a swizzle so that LHS and RHS sizes match.
1068 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
1070 ir_instruction
*inst
=
1071 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1072 instructions
->push_tail(inst
);
1075 /* Advance the component index by the number of components that were
1078 base_component
+= rhs_components
;
1081 return new(ctx
) ir_dereference_variable(var
);
1086 * Generate assignment of a portion of a vector to a portion of a matrix column
1088 * \param src_base First component of the source to be used in assignment
1089 * \param column Column of destination to be assiged
1090 * \param row_base First component of the destination column to be assigned
1091 * \param count Number of components to be assigned
1094 * \c src_base + \c count must be less than or equal to the number of components
1095 * in the source vector.
1098 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
1099 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
1102 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
1103 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
, col_idx
);
1105 assert(column_ref
->type
->components() >= (row_base
+ count
));
1106 assert(src
->type
->components() >= (src_base
+ count
));
1108 /* Generate a swizzle that extracts the number of components from the source
1109 * that are to be assigned to the column of the matrix.
1111 if (count
< src
->type
->vector_elements
) {
1112 src
= new(mem_ctx
) ir_swizzle(src
,
1113 src_base
+ 0, src_base
+ 1,
1114 src_base
+ 2, src_base
+ 3,
1118 /* Mask of fields to be written in the assignment.
1120 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
1122 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
1127 * Generate inline code for a matrix constructor
1129 * The generated constructor code will consist of a temporary variable
1130 * declaration of the same type as the constructor. A sequence of assignments
1131 * from constructor parameters to the temporary will follow.
1134 * An \c ir_dereference_variable of the temprorary generated in the constructor
1138 emit_inline_matrix_constructor(const glsl_type
*type
,
1139 exec_list
*instructions
,
1140 exec_list
*parameters
,
1143 assert(!parameters
->is_empty());
1145 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
1146 instructions
->push_tail(var
);
1148 /* There are three kinds of matrix constructors.
1150 * - Construct a matrix from a single scalar by replicating that scalar to
1151 * along the diagonal of the matrix and setting all other components to
1154 * - Construct a matrix from an arbirary combination of vectors and
1155 * scalars. The components of the constructor parameters are assigned
1156 * to the matrix in colum-major order until the matrix is full.
1158 * - Construct a matrix from a single matrix. The source matrix is copied
1159 * to the upper left portion of the constructed matrix, and the remaining
1160 * elements take values from the identity matrix.
1162 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
1163 if (single_scalar_parameter(parameters
)) {
1164 /* Assign the scalar to the X component of a vec4, and fill the remaining
1165 * components with zero.
1167 ir_variable
*rhs_var
=
1168 new(ctx
) ir_variable(glsl_type::vec4_type
, "mat_ctor_vec",
1170 instructions
->push_tail(rhs_var
);
1172 ir_constant_data zero
;
1178 ir_instruction
*inst
=
1179 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
1180 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
1182 instructions
->push_tail(inst
);
1184 ir_dereference
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1186 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
1187 instructions
->push_tail(inst
);
1189 /* Assign the temporary vector to each column of the destination matrix
1190 * with a swizzle that puts the X component on the diagonal of the
1191 * matrix. In some cases this may mean that the X component does not
1192 * get assigned into the column at all (i.e., when the matrix has more
1193 * columns than rows).
1195 static const unsigned rhs_swiz
[4][4] = {
1202 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
1203 type
->vector_elements
);
1204 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
1205 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1206 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
1208 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1209 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
1210 type
->vector_elements
);
1212 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1213 instructions
->push_tail(inst
);
1216 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
1217 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1218 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
1220 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1221 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
1222 type
->vector_elements
);
1224 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1225 instructions
->push_tail(inst
);
1227 } else if (first_param
->type
->is_matrix()) {
1228 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1230 * "If a matrix is constructed from a matrix, then each component
1231 * (column i, row j) in the result that has a corresponding
1232 * component (column i, row j) in the argument will be initialized
1233 * from there. All other components will be initialized to the
1234 * identity matrix. If a matrix argument is given to a matrix
1235 * constructor, it is an error to have any other arguments."
1237 assert(first_param
->next
->is_tail_sentinel());
1238 ir_rvalue
*const src_matrix
= first_param
;
1240 /* If the source matrix is smaller, pre-initialize the relavent parts of
1241 * the destination matrix to the identity matrix.
1243 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
1244 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
1246 /* If the source matrix has fewer rows, every column of the destination
1247 * must be initialized. Otherwise only the columns in the destination
1248 * that do not exist in the source must be initialized.
1251 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
1252 ? 0 : src_matrix
->type
->matrix_columns
;
1254 const glsl_type
*const col_type
= var
->type
->column_type();
1255 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
1256 ir_constant_data ident
;
1265 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
1267 ir_rvalue
*const lhs
=
1268 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
1270 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1271 instructions
->push_tail(inst
);
1275 /* Assign columns from the source matrix to the destination matrix.
1277 * Since the parameter will be used in the RHS of multiple assignments,
1278 * generate a temporary and copy the paramter there.
1280 ir_variable
*const rhs_var
=
1281 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1283 instructions
->push_tail(rhs_var
);
1285 ir_dereference
*const rhs_var_ref
=
1286 new(ctx
) ir_dereference_variable(rhs_var
);
1287 ir_instruction
*const inst
=
1288 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
1289 instructions
->push_tail(inst
);
1291 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1292 var
->type
->vector_elements
);
1293 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1294 var
->type
->matrix_columns
);
1296 unsigned swiz
[4] = { 0, 0, 0, 0 };
1297 for (unsigned i
= 1; i
< last_row
; i
++)
1300 const unsigned write_mask
= (1U << last_row
) - 1;
1302 for (unsigned i
= 0; i
< last_col
; i
++) {
1303 ir_dereference
*const lhs
=
1304 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1305 ir_rvalue
*const rhs_col
=
1306 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1308 /* If one matrix has columns that are smaller than the columns of the
1309 * other matrix, wrap the column access of the larger with a swizzle
1310 * so that the LHS and RHS of the assignment have the same size (and
1311 * therefore have the same type).
1313 * It would be perfectly valid to unconditionally generate the
1314 * swizzles, this this will typically result in a more compact IR tree.
1317 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1318 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1323 ir_instruction
*inst
=
1324 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1325 instructions
->push_tail(inst
);
1328 const unsigned cols
= type
->matrix_columns
;
1329 const unsigned rows
= type
->vector_elements
;
1330 unsigned col_idx
= 0;
1331 unsigned row_idx
= 0;
1333 foreach_in_list(ir_rvalue
, rhs
, parameters
) {
1334 const unsigned components_remaining_this_column
= rows
- row_idx
;
1335 unsigned rhs_components
= rhs
->type
->components();
1336 unsigned rhs_base
= 0;
1338 /* Since the parameter might be used in the RHS of two assignments,
1339 * generate a temporary and copy the paramter there.
1341 ir_variable
*rhs_var
=
1342 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1343 instructions
->push_tail(rhs_var
);
1345 ir_dereference
*rhs_var_ref
=
1346 new(ctx
) ir_dereference_variable(rhs_var
);
1347 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
1348 instructions
->push_tail(inst
);
1350 /* Assign the current parameter to as many components of the matrix
1353 * NOTE: A single vector parameter can span two matrix columns. A
1354 * single vec4, for example, can completely fill a mat2.
1356 if (rhs_components
>= components_remaining_this_column
) {
1357 const unsigned count
= MIN2(rhs_components
,
1358 components_remaining_this_column
);
1360 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1362 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1366 instructions
->push_tail(inst
);
1374 /* If there is data left in the parameter and components left to be
1375 * set in the destination, emit another assignment. It is possible
1376 * that the assignment could be of a vec4 to the last element of the
1377 * matrix. In this case col_idx==cols, but there is still data
1378 * left in the source parameter. Obviously, don't emit an assignment
1379 * to data outside the destination matrix.
1381 if ((col_idx
< cols
) && (rhs_base
< rhs_components
)) {
1382 const unsigned count
= rhs_components
- rhs_base
;
1384 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1386 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1391 instructions
->push_tail(inst
);
1398 return new(ctx
) ir_dereference_variable(var
);
1403 emit_inline_record_constructor(const glsl_type
*type
,
1404 exec_list
*instructions
,
1405 exec_list
*parameters
,
1408 ir_variable
*const var
=
1409 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1410 ir_dereference_variable
*const d
= new(mem_ctx
) ir_dereference_variable(var
);
1412 instructions
->push_tail(var
);
1414 exec_node
*node
= parameters
->head
;
1415 for (unsigned i
= 0; i
< type
->length
; i
++) {
1416 assert(!node
->is_tail_sentinel());
1418 ir_dereference
*const lhs
=
1419 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1420 type
->fields
.structure
[i
].name
);
1422 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1423 assert(rhs
!= NULL
);
1425 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
, NULL
);
1427 instructions
->push_tail(assign
);
1436 process_record_constructor(exec_list
*instructions
,
1437 const glsl_type
*constructor_type
,
1438 YYLTYPE
*loc
, exec_list
*parameters
,
1439 struct _mesa_glsl_parse_state
*state
)
1442 exec_list actual_parameters
;
1444 process_parameters(instructions
, &actual_parameters
,
1447 exec_node
*node
= actual_parameters
.head
;
1448 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
1449 ir_rvalue
*ir
= (ir_rvalue
*) node
;
1451 if (node
->is_tail_sentinel()) {
1452 _mesa_glsl_error(loc
, state
,
1453 "insufficient parameters to constructor for `%s'",
1454 constructor_type
->name
);
1455 return ir_rvalue::error_value(ctx
);
1458 if (apply_implicit_conversion(constructor_type
->fields
.structure
[i
].type
,
1460 node
->replace_with(ir
);
1462 _mesa_glsl_error(loc
, state
,
1463 "parameter type mismatch in constructor for `%s.%s' "
1465 constructor_type
->name
,
1466 constructor_type
->fields
.structure
[i
].name
,
1468 constructor_type
->fields
.structure
[i
].type
->name
);
1469 return ir_rvalue::error_value(ctx
);;
1475 if (!node
->is_tail_sentinel()) {
1476 _mesa_glsl_error(loc
, state
, "too many parameters in constructor "
1477 "for `%s'", constructor_type
->name
);
1478 return ir_rvalue::error_value(ctx
);
1481 ir_rvalue
*const constant
=
1482 constant_record_constructor(constructor_type
, &actual_parameters
,
1485 return (constant
!= NULL
)
1487 : emit_inline_record_constructor(constructor_type
, instructions
,
1488 &actual_parameters
, state
);
1493 ast_function_expression::hir(exec_list
*instructions
,
1494 struct _mesa_glsl_parse_state
*state
)
1497 /* There are three sorts of function calls.
1499 * 1. constructors - The first subexpression is an ast_type_specifier.
1500 * 2. methods - Only the .length() method of array types.
1501 * 3. functions - Calls to regular old functions.
1503 * Method calls are actually detected when the ast_field_selection
1504 * expression is handled.
1506 if (is_constructor()) {
1507 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
1508 YYLTYPE loc
= type
->get_location();
1511 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
1513 /* constructor_type can be NULL if a variable with the same name as the
1514 * structure has come into scope.
1516 if (constructor_type
== NULL
) {
1517 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
1518 "may be shadowed by a variable with the same name)",
1520 return ir_rvalue::error_value(ctx
);
1524 /* Constructors for samplers are illegal.
1526 if (constructor_type
->is_sampler()) {
1527 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
1528 constructor_type
->name
);
1529 return ir_rvalue::error_value(ctx
);
1532 if (constructor_type
->is_array()) {
1533 if (!state
->check_version(120, 300, &loc
,
1534 "array constructors forbidden")) {
1535 return ir_rvalue::error_value(ctx
);
1538 return process_array_constructor(instructions
, constructor_type
,
1539 & loc
, &this->expressions
, state
);
1543 /* There are two kinds of constructor calls. Constructors for arrays and
1544 * structures must have the exact number of arguments with matching types
1545 * in the correct order. These constructors follow essentially the same
1546 * type matching rules as functions.
1548 * Constructors for built-in language types, such as mat4 and vec2, are
1549 * free form. The only requirements are that the parameters must provide
1550 * enough values of the correct scalar type and that no arguments are
1551 * given past the last used argument.
1553 * When using the C-style initializer syntax from GLSL 4.20, constructors
1554 * must have the exact number of arguments with matching types in the
1557 if (constructor_type
->is_record()) {
1558 return process_record_constructor(instructions
, constructor_type
,
1559 &loc
, &this->expressions
,
1563 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
1564 return ir_rvalue::error_value(ctx
);
1566 /* Total number of components of the type being constructed. */
1567 const unsigned type_components
= constructor_type
->components();
1569 /* Number of components from parameters that have actually been
1570 * consumed. This is used to perform several kinds of error checking.
1572 unsigned components_used
= 0;
1574 unsigned matrix_parameters
= 0;
1575 unsigned nonmatrix_parameters
= 0;
1576 exec_list actual_parameters
;
1578 foreach_list_typed(ast_node
, ast
, link
, &this->expressions
) {
1579 ir_rvalue
*result
= ast
->hir(instructions
, state
);
1581 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1583 * "It is an error to provide extra arguments beyond this
1584 * last used argument."
1586 if (components_used
>= type_components
) {
1587 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
1589 constructor_type
->name
);
1590 return ir_rvalue::error_value(ctx
);
1593 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
1594 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1595 "non-numeric data type",
1596 constructor_type
->name
);
1597 return ir_rvalue::error_value(ctx
);
1600 /* Count the number of matrix and nonmatrix parameters. This
1601 * is used below to enforce some of the constructor rules.
1603 if (result
->type
->is_matrix())
1604 matrix_parameters
++;
1606 nonmatrix_parameters
++;
1608 actual_parameters
.push_tail(result
);
1609 components_used
+= result
->type
->components();
1612 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1614 * "It is an error to construct matrices from other matrices. This
1615 * is reserved for future use."
1617 if (matrix_parameters
> 0
1618 && constructor_type
->is_matrix()
1619 && !state
->check_version(120, 100, &loc
,
1620 "cannot construct `%s' from a matrix",
1621 constructor_type
->name
)) {
1622 return ir_rvalue::error_value(ctx
);
1625 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1627 * "If a matrix argument is given to a matrix constructor, it is
1628 * an error to have any other arguments."
1630 if ((matrix_parameters
> 0)
1631 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1632 && constructor_type
->is_matrix()) {
1633 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1634 "matrix must be only parameter",
1635 constructor_type
->name
);
1636 return ir_rvalue::error_value(ctx
);
1639 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1641 * "In these cases, there must be enough components provided in the
1642 * arguments to provide an initializer for every component in the
1643 * constructed value."
1645 if (components_used
< type_components
&& components_used
!= 1
1646 && matrix_parameters
== 0) {
1647 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1649 constructor_type
->name
);
1650 return ir_rvalue::error_value(ctx
);
1653 /* Later, we cast each parameter to the same base type as the
1654 * constructor. Since there are no non-floating point matrices, we
1655 * need to break them up into a series of column vectors.
1657 if (constructor_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1658 foreach_in_list_safe(ir_rvalue
, matrix
, &actual_parameters
) {
1659 if (!matrix
->type
->is_matrix())
1662 /* Create a temporary containing the matrix. */
1663 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1665 instructions
->push_tail(var
);
1666 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1667 ir_dereference_variable(var
), matrix
, NULL
));
1668 var
->constant_value
= matrix
->constant_expression_value();
1670 /* Replace the matrix with dereferences of its columns. */
1671 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1672 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1673 new(ctx
) ir_constant(i
)));
1679 bool all_parameters_are_constant
= true;
1681 /* Type cast each parameter and, if possible, fold constants.*/
1682 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1683 const glsl_type
*desired_type
=
1684 glsl_type::get_instance(constructor_type
->base_type
,
1685 ir
->type
->vector_elements
,
1686 ir
->type
->matrix_columns
);
1687 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1689 /* Attempt to convert the parameter to a constant valued expression.
1690 * After doing so, track whether or not all the parameters to the
1691 * constructor are trivially constant valued expressions.
1693 ir_rvalue
*const constant
= result
->constant_expression_value();
1695 if (constant
!= NULL
)
1698 all_parameters_are_constant
= false;
1701 ir
->replace_with(result
);
1705 /* If all of the parameters are trivially constant, create a
1706 * constant representing the complete collection of parameters.
1708 if (all_parameters_are_constant
) {
1709 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1710 } else if (constructor_type
->is_scalar()) {
1711 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1713 } else if (constructor_type
->is_vector()) {
1714 return emit_inline_vector_constructor(constructor_type
,
1719 assert(constructor_type
->is_matrix());
1720 return emit_inline_matrix_constructor(constructor_type
,
1726 const ast_expression
*id
= subexpressions
[0];
1727 const char *func_name
= id
->primary_expression
.identifier
;
1728 YYLTYPE loc
= get_location();
1729 exec_list actual_parameters
;
1731 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
1734 ir_function_signature
*sig
=
1735 match_function_by_name(func_name
, &actual_parameters
, state
);
1737 ir_rvalue
*value
= NULL
;
1739 no_matching_function_error(func_name
, &loc
, &actual_parameters
, state
);
1740 value
= ir_rvalue::error_value(ctx
);
1741 } else if (!verify_parameter_modes(state
, sig
, actual_parameters
, this->expressions
)) {
1742 /* an error has already been emitted */
1743 value
= ir_rvalue::error_value(ctx
);
1745 value
= generate_call(instructions
, sig
, &actual_parameters
, state
);
1751 return ir_rvalue::error_value(ctx
);
1755 ast_aggregate_initializer::hir(exec_list
*instructions
,
1756 struct _mesa_glsl_parse_state
*state
)
1759 YYLTYPE loc
= this->get_location();
1761 if (!this->constructor_type
) {
1762 _mesa_glsl_error(&loc
, state
, "type of C-style initializer unknown");
1763 return ir_rvalue::error_value(ctx
);
1765 const glsl_type
*const constructor_type
= this->constructor_type
;
1767 if (!state
->ARB_shading_language_420pack_enable
) {
1768 _mesa_glsl_error(&loc
, state
, "C-style initialization requires the "
1769 "GL_ARB_shading_language_420pack extension");
1770 return ir_rvalue::error_value(ctx
);
1773 if (constructor_type
->is_array()) {
1774 return process_array_constructor(instructions
, constructor_type
, &loc
,
1775 &this->expressions
, state
);
1778 if (constructor_type
->is_record()) {
1779 return process_record_constructor(instructions
, constructor_type
, &loc
,
1780 &this->expressions
, state
);
1783 return process_vec_mat_constructor(instructions
, constructor_type
, &loc
,
1784 &this->expressions
, state
);