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"
30 process_parameters(exec_list
*instructions
, exec_list
*actual_parameters
,
31 exec_list
*parameters
,
32 struct _mesa_glsl_parse_state
*state
)
36 foreach_list (n
, parameters
) {
37 ast_node
*const ast
= exec_node_data(ast_node
, n
, link
);
38 ir_rvalue
*result
= ast
->hir(instructions
, state
);
40 ir_constant
*const constant
= result
->constant_expression_value();
44 actual_parameters
->push_tail(result
);
53 process_call(exec_list
*instructions
, ir_function
*f
,
54 YYLTYPE
*loc
, exec_list
*actual_parameters
,
55 struct _mesa_glsl_parse_state
*state
)
57 const ir_function_signature
*sig
=
58 f
->matching_signature(actual_parameters
);
60 /* The instructions param will be used when the FINISHMEs below are done */
64 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
65 * isn't done in ir_function::matching_signature because that function
66 * cannot generate the necessary diagnostics.
68 exec_list_iterator actual_iter
= actual_parameters
->iterator();
69 exec_list_iterator formal_iter
= sig
->parameters
.iterator();
71 while (actual_iter
.has_next()) {
72 ir_rvalue
*actual
= (ir_rvalue
*) actual_iter
.get();
73 ir_variable
*formal
= (ir_variable
*) formal_iter
.get();
75 assert(actual
!= NULL
);
76 assert(formal
!= NULL
);
78 if ((formal
->mode
== ir_var_out
)
79 || (formal
->mode
== ir_var_inout
)) {
80 if (! actual
->is_lvalue()) {
81 /* FINISHME: Log a better diagnostic here. There is no way
82 * FINISHME: to tell the user which parameter is invalid.
84 _mesa_glsl_error(loc
, state
, "`%s' parameter is not lvalue",
85 (formal
->mode
== ir_var_out
) ? "out" : "inout");
93 /* FINISHME: The list of actual parameters needs to be modified to
94 * FINISHME: include any necessary conversions.
96 return new ir_call(sig
, actual_parameters
);
98 /* FINISHME: Log a better error message here. G++ will show the types
99 * FINISHME: of the actual parameters and the set of candidate
100 * FINISHME: functions. A different error should also be logged when
101 * FINISHME: multiple functions match.
103 _mesa_glsl_error(loc
, state
, "no matching function for call to `%s'",
105 return ir_call::get_error_instruction();
111 match_function_by_name(exec_list
*instructions
, const char *name
,
112 YYLTYPE
*loc
, exec_list
*actual_parameters
,
113 struct _mesa_glsl_parse_state
*state
)
115 ir_function
*f
= state
->symbols
->get_function(name
);
118 _mesa_glsl_error(loc
, state
, "function `%s' undeclared", name
);
119 return ir_call::get_error_instruction();
122 /* Once we've determined that the function being called might exist, try
123 * to find an overload of the function that matches the parameters.
125 return process_call(instructions
, f
, loc
, actual_parameters
, state
);
130 * Perform automatic type conversion of constructor parameters
133 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
135 const unsigned a
= desired_type
->base_type
;
136 const unsigned b
= src
->type
->base_type
;
137 ir_expression
*result
= NULL
;
139 if (src
->type
->is_error())
142 assert(a
<= GLSL_TYPE_BOOL
);
143 assert(b
<= GLSL_TYPE_BOOL
);
145 if ((a
== b
) || (src
->type
->is_integer() && desired_type
->is_integer()))
151 if (b
== GLSL_TYPE_FLOAT
)
152 result
= new ir_expression(ir_unop_f2i
, desired_type
, src
, NULL
);
154 assert(b
== GLSL_TYPE_BOOL
);
155 result
= new ir_expression(ir_unop_b2i
, desired_type
, src
, NULL
);
158 case GLSL_TYPE_FLOAT
:
161 result
= new ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
164 result
= new ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
167 result
= new ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
171 case GLSL_TYPE_BOOL
: {
172 ir_constant
*zero
= NULL
;
175 case GLSL_TYPE_UINT
: zero
= new ir_constant(unsigned(0)); break;
176 case GLSL_TYPE_INT
: zero
= new ir_constant(int(0)); break;
177 case GLSL_TYPE_FLOAT
: zero
= new ir_constant(0.0f
); break;
180 result
= new ir_expression(ir_binop_nequal
, desired_type
, src
, zero
);
184 assert(result
!= NULL
);
186 ir_constant
*const constant
= result
->constant_expression_value();
187 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
192 * Dereference a specific component from a scalar, vector, or matrix
195 dereference_component(ir_rvalue
*src
, unsigned component
)
197 assert(component
< src
->type
->components());
199 /* If the source is a constant, just create a new constant instead of a
200 * dereference of the existing constant.
202 ir_constant
*constant
= src
->as_constant();
204 return new ir_constant(constant
, component
);
206 if (src
->type
->is_scalar()) {
208 } else if (src
->type
->is_vector()) {
209 return new ir_swizzle(src
, component
, 0, 0, 0, 1);
211 assert(src
->type
->is_matrix());
213 /* Dereference a row of the matrix, then call this function again to get
214 * a specific element from that row.
216 const int c
= component
/ src
->type
->column_type()->vector_elements
;
217 const int r
= component
% src
->type
->column_type()->vector_elements
;
218 ir_constant
*const col_index
= new ir_constant(c
);
219 ir_dereference
*const col
= new ir_dereference_array(src
, col_index
);
221 col
->type
= src
->type
->column_type();
223 return dereference_component(col
, r
);
226 assert(!"Should not get here.");
232 process_array_constructor(exec_list
*instructions
,
233 const glsl_type
*constructor_type
,
234 YYLTYPE
*loc
, exec_list
*parameters
,
235 struct _mesa_glsl_parse_state
*state
)
237 /* Array constructors come in two forms: sized and unsized. Sized array
238 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
239 * variables. In this case the number of parameters must exactly match the
240 * specified size of the array.
242 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
243 * are vec4 variables. In this case the size of the array being constructed
244 * is determined by the number of parameters.
246 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
248 * "There must be exactly the same number of arguments as the size of
249 * the array being constructed. If no size is present in the
250 * constructor, then the array is explicitly sized to the number of
251 * arguments provided. The arguments are assigned in order, starting at
252 * element 0, to the elements of the constructed array. Each argument
253 * must be the same type as the element type of the array, or be a type
254 * that can be converted to the element type of the array according to
255 * Section 4.1.10 "Implicit Conversions.""
257 exec_list actual_parameters
;
258 const unsigned parameter_count
=
259 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
261 if ((parameter_count
== 0)
262 || ((constructor_type
->length
!= 0)
263 && (constructor_type
->length
!= parameter_count
))) {
264 const unsigned min_param
= (constructor_type
->length
== 0)
265 ? 1 : constructor_type
->length
;
267 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
269 (constructor_type
->length
!= 0) ? "at least" : "exactly",
270 min_param
, (min_param
<= 1) ? "" : "s");
271 return ir_call::get_error_instruction();
274 if (constructor_type
->length
== 0) {
276 glsl_type::get_array_instance(constructor_type
->element_type(),
278 assert(constructor_type
!= NULL
);
279 assert(constructor_type
->length
== parameter_count
);
282 ir_function
*f
= state
->symbols
->get_function(constructor_type
->name
);
284 /* If the constructor for this type of array does not exist, generate the
285 * prototype and add it to the symbol table.
288 f
= constructor_type
->generate_constructor(state
->symbols
);
292 process_call(instructions
, f
, loc
, &actual_parameters
, state
);
295 assert(r
->type
->is_error() || (r
->type
== constructor_type
));
302 * Try to convert a record constructor to a constant expression
305 constant_record_constructor(const glsl_type
*constructor_type
,
306 YYLTYPE
*loc
, exec_list
*parameters
,
307 struct _mesa_glsl_parse_state
*state
)
309 bool all_parameters_are_constant
= true;
311 exec_node
*node
= parameters
->head
;
312 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
313 ir_instruction
*ir
= (ir_instruction
*) node
;
315 if (node
->is_tail_sentinal()) {
316 _mesa_glsl_error(loc
, state
,
317 "insufficient parameters to constructor for `%s'",
318 constructor_type
->name
);
322 if (ir
->type
!= constructor_type
->fields
.structure
[i
].type
) {
323 _mesa_glsl_error(loc
, state
,
324 "parameter type mismatch in constructor for `%s' "
326 constructor_type
->name
,
328 constructor_type
->fields
.structure
[i
].type
->name
);
332 if (ir
->as_constant() == NULL
)
333 all_parameters_are_constant
= false;
338 if (!all_parameters_are_constant
)
341 return new ir_constant(constructor_type
, parameters
);
346 * Generate data for a constant matrix constructor w/a single scalar parameter
348 * Matrix constructors in GLSL can be passed a single scalar of the
349 * approriate type. In these cases, the resulting matrix is the identity
350 * matrix multipled by the specified scalar. This function generates data for
353 * \param type Type of the desired matrix.
354 * \param initializer Scalar value used to initialize the matrix diagonal.
355 * \param data Location to store the resulting matrix.
358 generate_constructor_matrix(const glsl_type
*type
, ir_constant
*initializer
,
359 ir_constant_data
*data
)
361 switch (type
->base_type
) {
364 for (unsigned i
= 0; i
< type
->components(); i
++)
367 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
368 /* The array offset of the ith row and column of the matrix.
370 const unsigned idx
= (i
* type
->vector_elements
) + i
;
372 data
->u
[idx
] = initializer
->value
.u
[0];
376 case GLSL_TYPE_FLOAT
:
377 for (unsigned i
= 0; i
< type
->components(); i
++)
380 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
381 /* The array offset of the ith row and column of the matrix.
383 const unsigned idx
= (i
* type
->vector_elements
) + i
;
385 data
->f
[idx
] = initializer
->value
.f
[0];
391 assert(!"Should not get here.");
398 * Generate data for a constant vector constructor w/a single scalar parameter
400 * Vector constructors in GLSL can be passed a single scalar of the
401 * approriate type. In these cases, the resulting vector contains the specified
402 * value in all components. This function generates data for that vector.
404 * \param type Type of the desired vector.
405 * \param initializer Scalar value used to initialize the vector.
406 * \param data Location to store the resulting vector data.
409 generate_constructor_vector(const glsl_type
*type
, ir_constant
*initializer
,
410 ir_constant_data
*data
)
412 switch (type
->base_type
) {
415 for (unsigned i
= 0; i
< type
->components(); i
++)
416 data
->u
[i
] = initializer
->value
.u
[0];
420 case GLSL_TYPE_FLOAT
:
421 for (unsigned i
= 0; i
< type
->components(); i
++)
422 data
->f
[i
] = initializer
->value
.f
[0];
427 for (unsigned i
= 0; i
< type
->components(); i
++)
428 data
->b
[i
] = initializer
->value
.b
[0];
433 assert(!"Should not get here.");
440 ast_function_expression::hir(exec_list
*instructions
,
441 struct _mesa_glsl_parse_state
*state
)
443 /* There are three sorts of function calls.
445 * 1. contstructors - The first subexpression is an ast_type_specifier.
446 * 2. methods - Only the .length() method of array types.
447 * 3. functions - Calls to regular old functions.
449 * Method calls are actually detected when the ast_field_selection
450 * expression is handled.
452 if (is_constructor()) {
453 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
454 YYLTYPE loc
= type
->get_location();
457 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
460 /* Constructors for samplers are illegal.
462 if (constructor_type
->is_sampler()) {
463 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
464 constructor_type
->name
);
465 return ir_call::get_error_instruction();
468 if (constructor_type
->is_array()) {
469 if (state
->language_version
<= 110) {
470 _mesa_glsl_error(& loc
, state
,
471 "array constructors forbidden in GLSL 1.10");
472 return ir_call::get_error_instruction();
475 return process_array_constructor(instructions
, constructor_type
,
476 & loc
, &this->expressions
, state
);
479 /* There are two kinds of constructor call. Constructors for built-in
480 * language types, such as mat4 and vec2, are free form. The only
481 * requirement is that the parameters must provide enough values of the
482 * correct scalar type. Constructors for arrays and structures must
483 * have the exact number of parameters with matching types in the
484 * correct order. These constructors follow essentially the same type
485 * matching rules as functions.
487 if (constructor_type
->is_numeric() || constructor_type
->is_boolean()) {
488 /* Constructing a numeric type has a couple steps. First all values
489 * passed to the constructor are broken into individual parameters
490 * and type converted to the base type of the thing being constructed.
492 * At that point we have some number of values that match the base
493 * type of the thing being constructed. Now the constructor can be
494 * treated like a function call. Each numeric type has a small set
495 * of constructor functions. The set of new parameters will either
496 * match one of those functions or the original constructor is
499 const glsl_type
*const base_type
= constructor_type
->get_base_type();
501 /* Total number of components of the type being constructed.
503 const unsigned type_components
= constructor_type
->components();
505 /* Number of components from parameters that have actually been
506 * consumed. This is used to perform several kinds of error checking.
508 unsigned components_used
= 0;
510 unsigned matrix_parameters
= 0;
511 unsigned nonmatrix_parameters
= 0;
512 exec_list actual_parameters
;
514 bool all_parameters_are_constant
= true;
516 /* This handles invalid constructor calls such as 'vec4 v = vec4();'
518 if (this->expressions
.is_empty()) {
519 _mesa_glsl_error(& loc
, state
, "too few components to construct "
521 constructor_type
->name
);
522 return ir_call::get_error_instruction();
525 foreach_list (n
, &this->expressions
) {
526 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
528 ast
->hir(instructions
, state
)->as_rvalue();
529 ir_variable
*result_var
= NULL
;
531 /* Attempt to convert the parameter to a constant valued expression.
532 * After doing so, track whether or not all the parameters to the
533 * constructor are trivially constant valued expressions.
535 ir_rvalue
*const constant
=
536 result
->constant_expression_value();
538 if (constant
!= NULL
)
541 all_parameters_are_constant
= false;
543 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
545 * "It is an error to provide extra arguments beyond this
546 * last used argument."
548 if (components_used
>= type_components
) {
549 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
551 constructor_type
->name
);
552 return ir_call::get_error_instruction();
555 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
556 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
557 "non-numeric data type",
558 constructor_type
->name
);
559 return ir_call::get_error_instruction();
562 /* Count the number of matrix and nonmatrix parameters. This
563 * is used below to enforce some of the constructor rules.
565 if (result
->type
->is_matrix())
568 nonmatrix_parameters
++;
570 /* We can't use the same instruction node in the multiple
571 * swizzle dereferences that happen, so assign it to a
572 * variable and deref that. Plus it saves computation for
573 * complicated expressions and handles
574 * glsl-vs-constructor-call.shader_test.
576 if (result
->type
->components() >= 1 && !result
->as_constant()) {
577 result_var
= new ir_variable(result
->type
, "constructor_tmp");
578 ir_dereference_variable
*lhs
;
580 lhs
= new ir_dereference_variable(result_var
);
581 instructions
->push_tail(new ir_assignment(lhs
, result
, NULL
));
584 /* Process each of the components of the parameter. Dereference
585 * each component individually, perform any type conversions, and
586 * add it to the parameter list for the constructor.
588 for (unsigned i
= 0; i
< result
->type
->components(); i
++) {
589 if (components_used
>= type_components
)
592 ir_rvalue
*component
;
595 ir_dereference
*d
= new ir_dereference_variable(result_var
);
596 component
= dereference_component(d
, i
);
598 component
= dereference_component(result
, i
);
600 component
= convert_component(component
, base_type
);
602 /* All cases that could result in component->type being the
603 * error type should have already been caught above.
605 assert(component
->type
== base_type
);
607 if (component
->as_constant() == NULL
)
608 all_parameters_are_constant
= false;
610 /* Don't actually generate constructor calls for scalars.
611 * Instead, do the usual component selection and conversion,
612 * and return the single component.
614 if (constructor_type
->is_scalar())
617 actual_parameters
.push_tail(component
);
622 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
624 * "It is an error to construct matrices from other matrices. This
625 * is reserved for future use."
627 if ((state
->language_version
<= 110) && (matrix_parameters
> 0)
628 && constructor_type
->is_matrix()) {
629 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
630 "matrix in GLSL 1.10",
631 constructor_type
->name
);
632 return ir_call::get_error_instruction();
635 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
637 * "If a matrix argument is given to a matrix constructor, it is
638 * an error to have any other arguments."
640 if ((matrix_parameters
> 0)
641 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
642 && constructor_type
->is_matrix()) {
643 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
644 "matrix must be only parameter",
645 constructor_type
->name
);
646 return ir_call::get_error_instruction();
649 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
651 * "In these cases, there must be enough components provided in the
652 * arguments to provide an initializer for every component in the
653 * constructed value."
655 if ((components_used
< type_components
) && (components_used
!= 1)) {
656 _mesa_glsl_error(& loc
, state
, "too few components to construct "
658 constructor_type
->name
);
659 return ir_call::get_error_instruction();
662 ir_function
*f
= state
->symbols
->get_function(constructor_type
->name
);
664 _mesa_glsl_error(& loc
, state
, "no constructor for type `%s'",
665 constructor_type
->name
);
666 return ir_call::get_error_instruction();
669 const ir_function_signature
*sig
=
670 f
->matching_signature(& actual_parameters
);
672 /* If all of the parameters are trivially constant, create a
673 * constant representing the complete collection of parameters.
675 if (all_parameters_are_constant
) {
676 if (components_used
>= type_components
)
677 return new ir_constant(sig
->return_type
, & actual_parameters
);
679 assert(sig
->return_type
->is_vector()
680 || sig
->return_type
->is_matrix());
682 /* Constructors with exactly one component are special for
683 * vectors and matrices. For vectors it causes all elements of
684 * the vector to be filled with the value. For matrices it
685 * causes the matrix to be filled with 0 and the diagonal to be
686 * filled with the value.
688 ir_constant_data data
;
689 ir_constant
*const initializer
=
690 (ir_constant
*) actual_parameters
.head
;
691 if (sig
->return_type
->is_matrix())
692 generate_constructor_matrix(sig
->return_type
, initializer
,
695 generate_constructor_vector(sig
->return_type
, initializer
,
698 return new ir_constant(sig
->return_type
, &data
);
700 return new ir_call(sig
, & actual_parameters
);
702 /* FINISHME: Log a better error message here. G++ will show the
703 * FINSIHME: types of the actual parameters and the set of
704 * FINSIHME: candidate functions. A different error should also be
705 * FINSIHME: logged when multiple functions match.
707 _mesa_glsl_error(& loc
, state
, "no matching constructor for `%s'",
708 constructor_type
->name
);
709 return ir_call::get_error_instruction();
713 return ir_call::get_error_instruction();
715 const ast_expression
*id
= subexpressions
[0];
716 YYLTYPE loc
= id
->get_location();
717 exec_list actual_parameters
;
719 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
722 const glsl_type
*const type
=
723 state
->symbols
->get_type(id
->primary_expression
.identifier
);
725 if ((type
!= NULL
) && type
->is_record()) {
726 ir_constant
*constant
=
727 constant_record_constructor(type
, &loc
, &actual_parameters
, state
);
729 if (constant
!= NULL
)
733 return match_function_by_name(instructions
,
734 id
->primary_expression
.identifier
, & loc
,
735 &actual_parameters
, state
);
738 return ir_call::get_error_instruction();