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.
25 #include "glsl_symbol_table.h"
27 #include "glsl_types.h"
31 process_parameters(exec_list
*instructions
, exec_list
*actual_parameters
,
32 exec_list
*parameters
,
33 struct _mesa_glsl_parse_state
*state
)
37 foreach_list (n
, parameters
) {
38 ast_node
*const ast
= exec_node_data(ast_node
, n
, link
);
39 ir_rvalue
*result
= ast
->hir(instructions
, state
);
41 ir_constant
*const constant
= result
->constant_expression_value();
45 actual_parameters
->push_tail(result
);
54 process_call(exec_list
*instructions
, ir_function
*f
,
55 YYLTYPE
*loc
, exec_list
*actual_parameters
,
56 struct _mesa_glsl_parse_state
*state
)
58 const ir_function_signature
*sig
=
59 f
->matching_signature(actual_parameters
);
61 /* The instructions param will be used when the FINISHMEs below are done */
65 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
66 * isn't done in ir_function::matching_signature because that function
67 * cannot generate the necessary diagnostics.
69 exec_list_iterator actual_iter
= actual_parameters
->iterator();
70 exec_list_iterator formal_iter
= sig
->parameters
.iterator();
72 while (actual_iter
.has_next()) {
73 ir_rvalue
*actual
= (ir_rvalue
*) actual_iter
.get();
74 ir_variable
*formal
= (ir_variable
*) formal_iter
.get();
76 assert(actual
!= NULL
);
77 assert(formal
!= NULL
);
79 if ((formal
->mode
== ir_var_out
)
80 || (formal
->mode
== ir_var_inout
)) {
81 if (! actual
->is_lvalue()) {
82 /* FINISHME: Log a better diagnostic here. There is no way
83 * FINISHME: to tell the user which parameter is invalid.
85 _mesa_glsl_error(loc
, state
, "`%s' parameter is not lvalue",
86 (formal
->mode
== ir_var_out
) ? "out" : "inout");
94 /* FINISHME: The list of actual parameters needs to be modified to
95 * FINISHME: include any necessary conversions.
97 return new ir_call(sig
, actual_parameters
);
99 /* FINISHME: Log a better error message here. G++ will show the types
100 * FINISHME: of the actual parameters and the set of candidate
101 * FINISHME: functions. A different error should also be logged when
102 * FINISHME: multiple functions match.
104 _mesa_glsl_error(loc
, state
, "no matching function for call to `%s'",
106 return ir_call::get_error_instruction();
112 match_function_by_name(exec_list
*instructions
, const char *name
,
113 YYLTYPE
*loc
, exec_list
*actual_parameters
,
114 struct _mesa_glsl_parse_state
*state
)
116 ir_function
*f
= state
->symbols
->get_function(name
);
119 _mesa_glsl_error(loc
, state
, "function `%s' undeclared", name
);
120 return ir_call::get_error_instruction();
123 /* Once we've determined that the function being called might exist, try
124 * to find an overload of the function that matches the parameters.
126 return process_call(instructions
, f
, loc
, actual_parameters
, state
);
131 * Perform automatic type conversion of constructor parameters
134 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
136 const unsigned a
= desired_type
->base_type
;
137 const unsigned b
= src
->type
->base_type
;
138 ir_expression
*result
= NULL
;
140 if (src
->type
->is_error())
143 assert(a
<= GLSL_TYPE_BOOL
);
144 assert(b
<= GLSL_TYPE_BOOL
);
146 if ((a
== b
) || (src
->type
->is_integer() && desired_type
->is_integer()))
152 if (b
== GLSL_TYPE_FLOAT
)
153 result
= new ir_expression(ir_unop_f2i
, desired_type
, src
, NULL
);
155 assert(b
== GLSL_TYPE_BOOL
);
156 result
= new ir_expression(ir_unop_b2i
, desired_type
, src
, NULL
);
159 case GLSL_TYPE_FLOAT
:
162 result
= new ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
165 result
= new ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
168 result
= new ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
172 case GLSL_TYPE_BOOL
: {
173 ir_constant
*zero
= NULL
;
176 case GLSL_TYPE_UINT
: zero
= new ir_constant(unsigned(0)); break;
177 case GLSL_TYPE_INT
: zero
= new ir_constant(int(0)); break;
178 case GLSL_TYPE_FLOAT
: zero
= new ir_constant(0.0f
); break;
181 result
= new ir_expression(ir_binop_nequal
, desired_type
, src
, zero
);
185 assert(result
!= NULL
);
187 ir_constant
*const constant
= result
->constant_expression_value();
188 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
193 * Dereference a specific component from a scalar, vector, or matrix
196 dereference_component(ir_rvalue
*src
, unsigned component
)
198 assert(component
< src
->type
->components());
200 /* If the source is a constant, just create a new constant instead of a
201 * dereference of the existing constant.
203 ir_constant
*constant
= src
->as_constant();
205 return new ir_constant(constant
, component
);
207 if (src
->type
->is_scalar()) {
209 } else if (src
->type
->is_vector()) {
210 return new ir_swizzle(src
, component
, 0, 0, 0, 1);
212 assert(src
->type
->is_matrix());
214 /* Dereference a row of the matrix, then call this function again to get
215 * a specific element from that row.
217 const int c
= component
/ src
->type
->column_type()->vector_elements
;
218 const int r
= component
% src
->type
->column_type()->vector_elements
;
219 ir_constant
*const col_index
= new ir_constant(c
);
220 ir_dereference
*const col
= new ir_dereference_array(src
, col_index
);
222 col
->type
= src
->type
->column_type();
224 return dereference_component(col
, r
);
227 assert(!"Should not get here.");
233 process_array_constructor(exec_list
*instructions
,
234 const glsl_type
*constructor_type
,
235 YYLTYPE
*loc
, exec_list
*parameters
,
236 struct _mesa_glsl_parse_state
*state
)
238 /* Array constructors come in two forms: sized and unsized. Sized array
239 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
240 * variables. In this case the number of parameters must exactly match the
241 * specified size of the array.
243 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
244 * are vec4 variables. In this case the size of the array being constructed
245 * is determined by the number of parameters.
247 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
249 * "There must be exactly the same number of arguments as the size of
250 * the array being constructed. If no size is present in the
251 * constructor, then the array is explicitly sized to the number of
252 * arguments provided. The arguments are assigned in order, starting at
253 * element 0, to the elements of the constructed array. Each argument
254 * must be the same type as the element type of the array, or be a type
255 * that can be converted to the element type of the array according to
256 * Section 4.1.10 "Implicit Conversions.""
258 exec_list actual_parameters
;
259 const unsigned parameter_count
=
260 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
262 if ((parameter_count
== 0)
263 || ((constructor_type
->length
!= 0)
264 && (constructor_type
->length
!= parameter_count
))) {
265 const unsigned min_param
= (constructor_type
->length
== 0)
266 ? 1 : constructor_type
->length
;
268 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
270 (constructor_type
->length
!= 0) ? "at least" : "exactly",
271 min_param
, (min_param
<= 1) ? "" : "s");
272 return ir_call::get_error_instruction();
275 if (constructor_type
->length
== 0) {
277 glsl_type::get_array_instance(constructor_type
->element_type(),
279 assert(constructor_type
!= NULL
);
280 assert(constructor_type
->length
== parameter_count
);
283 ir_function
*f
= state
->symbols
->get_function(constructor_type
->name
);
285 /* If the constructor for this type of array does not exist, generate the
286 * prototype and add it to the symbol table.
289 f
= constructor_type
->generate_constructor(state
->symbols
);
293 process_call(instructions
, f
, loc
, &actual_parameters
, state
);
296 assert(r
->type
->is_error() || (r
->type
== constructor_type
));
303 * Try to convert a record constructor to a constant expression
306 constant_record_constructor(const glsl_type
*constructor_type
,
307 YYLTYPE
*loc
, exec_list
*parameters
,
308 struct _mesa_glsl_parse_state
*state
)
310 bool all_parameters_are_constant
= true;
312 exec_node
*node
= parameters
->head
;
313 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
314 ir_instruction
*ir
= (ir_instruction
*) node
;
316 if (node
->is_tail_sentinal()) {
317 _mesa_glsl_error(loc
, state
,
318 "insufficient parameters to constructor for `%s'",
319 constructor_type
->name
);
323 if (ir
->type
!= constructor_type
->fields
.structure
[i
].type
) {
324 _mesa_glsl_error(loc
, state
,
325 "parameter type mismatch in constructor for `%s' "
327 constructor_type
->name
,
329 constructor_type
->fields
.structure
[i
].type
->name
);
333 if (ir
->as_constant() == NULL
)
334 all_parameters_are_constant
= false;
339 if (!all_parameters_are_constant
)
342 return new ir_constant(constructor_type
, parameters
);
347 * Generate data for a constant matrix constructor w/a single scalar parameter
349 * Matrix constructors in GLSL can be passed a single scalar of the
350 * approriate type. In these cases, the resulting matrix is the identity
351 * matrix multipled by the specified scalar. This function generates data for
354 * \param type Type of the desired matrix.
355 * \param initializer Scalar value used to initialize the matrix diagonal.
356 * \param data Location to store the resulting matrix.
359 generate_constructor_matrix(const glsl_type
*type
, ir_constant
*initializer
,
360 ir_constant_data
*data
)
362 switch (type
->base_type
) {
365 for (unsigned i
= 0; i
< type
->components(); i
++)
368 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
369 /* The array offset of the ith row and column of the matrix.
371 const unsigned idx
= (i
* type
->vector_elements
) + i
;
373 data
->u
[idx
] = initializer
->value
.u
[0];
377 case GLSL_TYPE_FLOAT
:
378 for (unsigned i
= 0; i
< type
->components(); i
++)
381 for (unsigned i
= 0; i
< type
->matrix_columns
; i
++) {
382 /* The array offset of the ith row and column of the matrix.
384 const unsigned idx
= (i
* type
->vector_elements
) + i
;
386 data
->f
[idx
] = initializer
->value
.f
[0];
392 assert(!"Should not get here.");
399 * Generate data for a constant vector constructor w/a single scalar parameter
401 * Vector constructors in GLSL can be passed a single scalar of the
402 * approriate type. In these cases, the resulting vector contains the specified
403 * value in all components. This function generates data for that vector.
405 * \param type Type of the desired vector.
406 * \param initializer Scalar value used to initialize the vector.
407 * \param data Location to store the resulting vector data.
410 generate_constructor_vector(const glsl_type
*type
, ir_constant
*initializer
,
411 ir_constant_data
*data
)
413 switch (type
->base_type
) {
416 for (unsigned i
= 0; i
< type
->components(); i
++)
417 data
->u
[i
] = initializer
->value
.u
[0];
421 case GLSL_TYPE_FLOAT
:
422 for (unsigned i
= 0; i
< type
->components(); i
++)
423 data
->f
[i
] = initializer
->value
.f
[0];
428 for (unsigned i
= 0; i
< type
->components(); i
++)
429 data
->b
[i
] = initializer
->value
.b
[0];
434 assert(!"Should not get here.");
441 ast_function_expression::hir(exec_list
*instructions
,
442 struct _mesa_glsl_parse_state
*state
)
444 /* There are three sorts of function calls.
446 * 1. contstructors - The first subexpression is an ast_type_specifier.
447 * 2. methods - Only the .length() method of array types.
448 * 3. functions - Calls to regular old functions.
450 * Method calls are actually detected when the ast_field_selection
451 * expression is handled.
453 if (is_constructor()) {
454 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
455 YYLTYPE loc
= type
->get_location();
458 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
461 /* Constructors for samplers are illegal.
463 if (constructor_type
->is_sampler()) {
464 _mesa_glsl_error(& loc
, state
, "cannot construct sampler type `%s'",
465 constructor_type
->name
);
466 return ir_call::get_error_instruction();
469 if (constructor_type
->is_array()) {
470 if (state
->language_version
<= 110) {
471 _mesa_glsl_error(& loc
, state
,
472 "array constructors forbidden in GLSL 1.10");
473 return ir_call::get_error_instruction();
476 return process_array_constructor(instructions
, constructor_type
,
477 & loc
, &this->expressions
, state
);
480 /* There are two kinds of constructor call. Constructors for built-in
481 * language types, such as mat4 and vec2, are free form. The only
482 * requirement is that the parameters must provide enough values of the
483 * correct scalar type. Constructors for arrays and structures must
484 * have the exact number of parameters with matching types in the
485 * correct order. These constructors follow essentially the same type
486 * matching rules as functions.
488 if (constructor_type
->is_numeric() || constructor_type
->is_boolean()) {
489 /* Constructing a numeric type has a couple steps. First all values
490 * passed to the constructor are broken into individual parameters
491 * and type converted to the base type of the thing being constructed.
493 * At that point we have some number of values that match the base
494 * type of the thing being constructed. Now the constructor can be
495 * treated like a function call. Each numeric type has a small set
496 * of constructor functions. The set of new parameters will either
497 * match one of those functions or the original constructor is
500 const glsl_type
*const base_type
= constructor_type
->get_base_type();
502 /* Total number of components of the type being constructed.
504 const unsigned type_components
= constructor_type
->components();
506 /* Number of components from parameters that have actually been
507 * consumed. This is used to perform several kinds of error checking.
509 unsigned components_used
= 0;
511 unsigned matrix_parameters
= 0;
512 unsigned nonmatrix_parameters
= 0;
513 exec_list actual_parameters
;
515 bool all_parameters_are_constant
= true;
517 assert(!this->expressions
.is_empty());
519 foreach_list (n
, &this->expressions
) {
520 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
522 ast
->hir(instructions
, state
)->as_rvalue();
524 /* Attempt to convert the parameter to a constant valued expression.
525 * After doing so, track whether or not all the parameters to the
526 * constructor are trivially constant valued expressions.
528 ir_rvalue
*const constant
=
529 result
->constant_expression_value();
531 if (constant
!= NULL
)
534 all_parameters_are_constant
= false;
536 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
538 * "It is an error to provide extra arguments beyond this
539 * last used argument."
541 if (components_used
>= type_components
) {
542 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
544 constructor_type
->name
);
545 return ir_call::get_error_instruction();
548 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
549 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
550 "non-numeric data type",
551 constructor_type
->name
);
552 return ir_call::get_error_instruction();
555 /* Count the number of matrix and nonmatrix parameters. This
556 * is used below to enforce some of the constructor rules.
558 if (result
->type
->is_matrix())
561 nonmatrix_parameters
++;
564 /* Process each of the components of the parameter. Dereference
565 * each component individually, perform any type conversions, and
566 * add it to the parameter list for the constructor.
568 for (unsigned i
= 0; i
< result
->type
->components(); i
++) {
569 if (components_used
>= type_components
)
572 ir_rvalue
*const component
=
573 convert_component(dereference_component(result
, i
),
576 /* All cases that could result in component->type being the
577 * error type should have already been caught above.
579 assert(component
->type
== base_type
);
581 if (component
->as_constant() == NULL
)
582 all_parameters_are_constant
= false;
584 /* Don't actually generate constructor calls for scalars.
585 * Instead, do the usual component selection and conversion,
586 * and return the single component.
588 if (constructor_type
->is_scalar())
591 actual_parameters
.push_tail(component
);
596 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
598 * "It is an error to construct matrices from other matrices. This
599 * is reserved for future use."
601 if ((state
->language_version
<= 110) && (matrix_parameters
> 0)
602 && constructor_type
->is_matrix()) {
603 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
604 "matrix in GLSL 1.10",
605 constructor_type
->name
);
606 return ir_call::get_error_instruction();
609 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
611 * "If a matrix argument is given to a matrix constructor, it is
612 * an error to have any other arguments."
614 if ((matrix_parameters
> 0)
615 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
616 && constructor_type
->is_matrix()) {
617 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
618 "matrix must be only parameter",
619 constructor_type
->name
);
620 return ir_call::get_error_instruction();
623 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
625 * "In these cases, there must be enough components provided in the
626 * arguments to provide an initializer for every component in the
627 * constructed value."
629 if ((components_used
< type_components
) && (components_used
!= 1)) {
630 _mesa_glsl_error(& loc
, state
, "too few components to construct "
632 constructor_type
->name
);
633 return ir_call::get_error_instruction();
636 ir_function
*f
= state
->symbols
->get_function(constructor_type
->name
);
638 _mesa_glsl_error(& loc
, state
, "no constructor for type `%s'",
639 constructor_type
->name
);
640 return ir_call::get_error_instruction();
643 const ir_function_signature
*sig
=
644 f
->matching_signature(& actual_parameters
);
646 /* If all of the parameters are trivially constant, create a
647 * constant representing the complete collection of parameters.
649 if (all_parameters_are_constant
) {
650 if (components_used
>= type_components
)
651 return new ir_constant(sig
->return_type
, & actual_parameters
);
653 assert(sig
->return_type
->is_vector()
654 || sig
->return_type
->is_matrix());
656 /* Constructors with exactly one component are special for
657 * vectors and matrices. For vectors it causes all elements of
658 * the vector to be filled with the value. For matrices it
659 * causes the matrix to be filled with 0 and the diagonal to be
660 * filled with the value.
662 ir_constant_data data
;
663 ir_constant
*const initializer
=
664 (ir_constant
*) actual_parameters
.head
;
665 if (sig
->return_type
->is_matrix())
666 generate_constructor_matrix(sig
->return_type
, initializer
,
669 generate_constructor_vector(sig
->return_type
, initializer
,
672 return new ir_constant(sig
->return_type
, &data
);
674 return new ir_call(sig
, & actual_parameters
);
676 /* FINISHME: Log a better error message here. G++ will show the
677 * FINSIHME: types of the actual parameters and the set of
678 * FINSIHME: candidate functions. A different error should also be
679 * FINSIHME: logged when multiple functions match.
681 _mesa_glsl_error(& loc
, state
, "no matching constructor for `%s'",
682 constructor_type
->name
);
683 return ir_call::get_error_instruction();
687 return ir_call::get_error_instruction();
689 const ast_expression
*id
= subexpressions
[0];
690 YYLTYPE loc
= id
->get_location();
691 exec_list actual_parameters
;
693 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
696 const glsl_type
*const type
=
697 state
->symbols
->get_type(id
->primary_expression
.identifier
);
699 if ((type
!= NULL
) && type
->is_record()) {
700 ir_constant
*constant
=
701 constant_record_constructor(type
, &loc
, &actual_parameters
, state
);
703 if (constant
!= NULL
)
707 return match_function_by_name(instructions
,
708 id
->primary_expression
.identifier
, & loc
,
709 &actual_parameters
, state
);
712 return ir_call::get_error_instruction();