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.
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
31 * * Symbol table management
35 * The majority of this work could be done during parsing, and the parser could
36 * probably generate HIR directly. However, this results in frequent changes
37 * to the parser code. Since we do not assume that every system this complier
38 * is built on will have Flex and Bison installed, we have to store the code
39 * generated by these tools in our version control system. In other parts of
40 * the system we've seen problems where a parser was changed but the generated
41 * code was not committed, merge conflicts where created because two developers
42 * had slightly different versions of Bison installed, etc.
44 * I have also noticed that running Bison generated parsers in GDB is very
45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very
46 * well 'print $1' in GDB.
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
52 #include "main/core.h" /* for struct gl_extensions */
53 #include "glsl_symbol_table.h"
54 #include "glsl_parser_extras.h"
56 #include "glsl_types.h"
60 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
62 _mesa_glsl_initialize_variables(instructions
, state
);
63 _mesa_glsl_initialize_functions(instructions
, state
);
65 state
->symbols
->language_version
= state
->language_version
;
67 state
->current_function
= NULL
;
69 /* Section 4.2 of the GLSL 1.20 specification states:
70 * "The built-in functions are scoped in a scope outside the global scope
71 * users declare global variables in. That is, a shader's global scope,
72 * available for user-defined functions and global variables, is nested
73 * inside the scope containing the built-in functions."
75 * Since built-in functions like ftransform() access built-in variables,
76 * it follows that those must be in the outer scope as well.
78 * We push scope here to create this nesting effect...but don't pop.
79 * This way, a shader's globals are still in the symbol table for use
82 state
->symbols
->push_scope();
84 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
85 ast
->hir(instructions
, state
);
90 * If a conversion is available, convert one operand to a different type
92 * The \c from \c ir_rvalue is converted "in place".
94 * \param to Type that the operand it to be converted to
95 * \param from Operand that is being converted
96 * \param state GLSL compiler state
99 * If a conversion is possible (or unnecessary), \c true is returned.
100 * Otherwise \c false is returned.
103 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
104 struct _mesa_glsl_parse_state
*state
)
107 if (to
->base_type
== from
->type
->base_type
)
110 /* This conversion was added in GLSL 1.20. If the compilation mode is
111 * GLSL 1.10, the conversion is skipped.
113 if (state
->language_version
< 120)
116 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
118 * "There are no implicit array or structure conversions. For
119 * example, an array of int cannot be implicitly converted to an
120 * array of float. There are no implicit conversions between
121 * signed and unsigned integers."
123 /* FINISHME: The above comment is partially a lie. There is int/uint
124 * FINISHME: conversion for immediate constants.
126 if (!to
->is_float() || !from
->type
->is_numeric())
129 /* Convert to a floating point type with the same number of components
130 * as the original type - i.e. int to float, not int to vec4.
132 to
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, from
->type
->vector_elements
,
133 from
->type
->matrix_columns
);
135 switch (from
->type
->base_type
) {
137 from
= new(ctx
) ir_expression(ir_unop_i2f
, to
, from
, NULL
);
140 from
= new(ctx
) ir_expression(ir_unop_u2f
, to
, from
, NULL
);
143 from
= new(ctx
) ir_expression(ir_unop_b2f
, to
, from
, NULL
);
153 static const struct glsl_type
*
154 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
156 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
158 const glsl_type
*type_a
= value_a
->type
;
159 const glsl_type
*type_b
= value_b
->type
;
161 /* From GLSL 1.50 spec, page 56:
163 * "The arithmetic binary operators add (+), subtract (-),
164 * multiply (*), and divide (/) operate on integer and
165 * floating-point scalars, vectors, and matrices."
167 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
168 _mesa_glsl_error(loc
, state
,
169 "Operands to arithmetic operators must be numeric");
170 return glsl_type::error_type
;
174 /* "If one operand is floating-point based and the other is
175 * not, then the conversions from Section 4.1.10 "Implicit
176 * Conversions" are applied to the non-floating-point-based operand."
178 if (!apply_implicit_conversion(type_a
, value_b
, state
)
179 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
180 _mesa_glsl_error(loc
, state
,
181 "Could not implicitly convert operands to "
182 "arithmetic operator");
183 return glsl_type::error_type
;
185 type_a
= value_a
->type
;
186 type_b
= value_b
->type
;
188 /* "If the operands are integer types, they must both be signed or
191 * From this rule and the preceeding conversion it can be inferred that
192 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
193 * The is_numeric check above already filtered out the case where either
194 * type is not one of these, so now the base types need only be tested for
197 if (type_a
->base_type
!= type_b
->base_type
) {
198 _mesa_glsl_error(loc
, state
,
199 "base type mismatch for arithmetic operator");
200 return glsl_type::error_type
;
203 /* "All arithmetic binary operators result in the same fundamental type
204 * (signed integer, unsigned integer, or floating-point) as the
205 * operands they operate on, after operand type conversion. After
206 * conversion, the following cases are valid
208 * * The two operands are scalars. In this case the operation is
209 * applied, resulting in a scalar."
211 if (type_a
->is_scalar() && type_b
->is_scalar())
214 /* "* One operand is a scalar, and the other is a vector or matrix.
215 * In this case, the scalar operation is applied independently to each
216 * component of the vector or matrix, resulting in the same size
219 if (type_a
->is_scalar()) {
220 if (!type_b
->is_scalar())
222 } else if (type_b
->is_scalar()) {
226 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
227 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
230 assert(!type_a
->is_scalar());
231 assert(!type_b
->is_scalar());
233 /* "* The two operands are vectors of the same size. In this case, the
234 * operation is done component-wise resulting in the same size
237 if (type_a
->is_vector() && type_b
->is_vector()) {
238 if (type_a
== type_b
) {
241 _mesa_glsl_error(loc
, state
,
242 "vector size mismatch for arithmetic operator");
243 return glsl_type::error_type
;
247 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
248 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
249 * <vector, vector> have been handled. At least one of the operands must
250 * be matrix. Further, since there are no integer matrix types, the base
251 * type of both operands must be float.
253 assert(type_a
->is_matrix() || type_b
->is_matrix());
254 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
255 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
257 /* "* The operator is add (+), subtract (-), or divide (/), and the
258 * operands are matrices with the same number of rows and the same
259 * number of columns. In this case, the operation is done component-
260 * wise resulting in the same size matrix."
261 * * The operator is multiply (*), where both operands are matrices or
262 * one operand is a vector and the other a matrix. A right vector
263 * operand is treated as a column vector and a left vector operand as a
264 * row vector. In all these cases, it is required that the number of
265 * columns of the left operand is equal to the number of rows of the
266 * right operand. Then, the multiply (*) operation does a linear
267 * algebraic multiply, yielding an object that has the same number of
268 * rows as the left operand and the same number of columns as the right
269 * operand. Section 5.10 "Vector and Matrix Operations" explains in
270 * more detail how vectors and matrices are operated on."
273 if (type_a
== type_b
)
276 if (type_a
->is_matrix() && type_b
->is_matrix()) {
277 /* Matrix multiply. The columns of A must match the rows of B. Given
278 * the other previously tested constraints, this means the vector type
279 * of a row from A must be the same as the vector type of a column from
282 if (type_a
->row_type() == type_b
->column_type()) {
283 /* The resulting matrix has the number of columns of matrix B and
284 * the number of rows of matrix A. We get the row count of A by
285 * looking at the size of a vector that makes up a column. The
286 * transpose (size of a row) is done for B.
288 const glsl_type
*const type
=
289 glsl_type::get_instance(type_a
->base_type
,
290 type_a
->column_type()->vector_elements
,
291 type_b
->row_type()->vector_elements
);
292 assert(type
!= glsl_type::error_type
);
296 } else if (type_a
->is_matrix()) {
297 /* A is a matrix and B is a column vector. Columns of A must match
298 * rows of B. Given the other previously tested constraints, this
299 * means the vector type of a row from A must be the same as the
300 * vector the type of B.
302 if (type_a
->row_type() == type_b
) {
303 /* The resulting vector has a number of elements equal to
304 * the number of rows of matrix A. */
305 const glsl_type
*const type
=
306 glsl_type::get_instance(type_a
->base_type
,
307 type_a
->column_type()->vector_elements
,
309 assert(type
!= glsl_type::error_type
);
314 assert(type_b
->is_matrix());
316 /* A is a row vector and B is a matrix. Columns of A must match rows
317 * of B. Given the other previously tested constraints, this means
318 * the type of A must be the same as the vector type of a column from
321 if (type_a
== type_b
->column_type()) {
322 /* The resulting vector has a number of elements equal to
323 * the number of columns of matrix B. */
324 const glsl_type
*const type
=
325 glsl_type::get_instance(type_a
->base_type
,
326 type_b
->row_type()->vector_elements
,
328 assert(type
!= glsl_type::error_type
);
334 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
335 return glsl_type::error_type
;
339 /* "All other cases are illegal."
341 _mesa_glsl_error(loc
, state
, "type mismatch");
342 return glsl_type::error_type
;
346 static const struct glsl_type
*
347 unary_arithmetic_result_type(const struct glsl_type
*type
,
348 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
350 /* From GLSL 1.50 spec, page 57:
352 * "The arithmetic unary operators negate (-), post- and pre-increment
353 * and decrement (-- and ++) operate on integer or floating-point
354 * values (including vectors and matrices). All unary operators work
355 * component-wise on their operands. These result with the same type
358 if (!type
->is_numeric()) {
359 _mesa_glsl_error(loc
, state
,
360 "Operands to arithmetic operators must be numeric");
361 return glsl_type::error_type
;
368 static const struct glsl_type
*
369 modulus_result_type(const struct glsl_type
*type_a
,
370 const struct glsl_type
*type_b
,
371 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
373 /* From GLSL 1.50 spec, page 56:
374 * "The operator modulus (%) operates on signed or unsigned integers or
375 * integer vectors. The operand types must both be signed or both be
378 if (!type_a
->is_integer() || !type_b
->is_integer()
379 || (type_a
->base_type
!= type_b
->base_type
)) {
380 _mesa_glsl_error(loc
, state
, "type mismatch");
381 return glsl_type::error_type
;
384 /* "The operands cannot be vectors of differing size. If one operand is
385 * a scalar and the other vector, then the scalar is applied component-
386 * wise to the vector, resulting in the same type as the vector. If both
387 * are vectors of the same size, the result is computed component-wise."
389 if (type_a
->is_vector()) {
390 if (!type_b
->is_vector()
391 || (type_a
->vector_elements
== type_b
->vector_elements
))
396 /* "The operator modulus (%) is not defined for any other data types
397 * (non-integer types)."
399 _mesa_glsl_error(loc
, state
, "type mismatch");
400 return glsl_type::error_type
;
404 static const struct glsl_type
*
405 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
406 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
408 const glsl_type
*type_a
= value_a
->type
;
409 const glsl_type
*type_b
= value_b
->type
;
411 /* From GLSL 1.50 spec, page 56:
412 * "The relational operators greater than (>), less than (<), greater
413 * than or equal (>=), and less than or equal (<=) operate only on
414 * scalar integer and scalar floating-point expressions."
416 if (!type_a
->is_numeric()
417 || !type_b
->is_numeric()
418 || !type_a
->is_scalar()
419 || !type_b
->is_scalar()) {
420 _mesa_glsl_error(loc
, state
,
421 "Operands to relational operators must be scalar and "
423 return glsl_type::error_type
;
426 /* "Either the operands' types must match, or the conversions from
427 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
428 * operand, after which the types must match."
430 if (!apply_implicit_conversion(type_a
, value_b
, state
)
431 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
432 _mesa_glsl_error(loc
, state
,
433 "Could not implicitly convert operands to "
434 "relational operator");
435 return glsl_type::error_type
;
437 type_a
= value_a
->type
;
438 type_b
= value_b
->type
;
440 if (type_a
->base_type
!= type_b
->base_type
) {
441 _mesa_glsl_error(loc
, state
, "base type mismatch");
442 return glsl_type::error_type
;
445 /* "The result is scalar Boolean."
447 return glsl_type::bool_type
;
452 * Validates that a value can be assigned to a location with a specified type
454 * Validates that \c rhs can be assigned to some location. If the types are
455 * not an exact match but an automatic conversion is possible, \c rhs will be
459 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
460 * Otherwise the actual RHS to be assigned will be returned. This may be
461 * \c rhs, or it may be \c rhs after some type conversion.
464 * In addition to being used for assignments, this function is used to
465 * type-check return values.
468 validate_assignment(struct _mesa_glsl_parse_state
*state
,
469 const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
471 const glsl_type
*rhs_type
= rhs
->type
;
473 /* If there is already some error in the RHS, just return it. Anything
474 * else will lead to an avalanche of error message back to the user.
476 if (rhs_type
->is_error())
479 /* If the types are identical, the assignment can trivially proceed.
481 if (rhs_type
== lhs_type
)
484 /* If the array element types are the same and the size of the LHS is zero,
485 * the assignment is okay.
487 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
488 * is handled by ir_dereference::is_lvalue.
490 if (lhs_type
->is_array() && rhs
->type
->is_array()
491 && (lhs_type
->element_type() == rhs
->type
->element_type())
492 && (lhs_type
->array_size() == 0)) {
496 /* Check for implicit conversion in GLSL 1.20 */
497 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
498 rhs_type
= rhs
->type
;
499 if (rhs_type
== lhs_type
)
507 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
508 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
512 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
514 if (!error_emitted
) {
515 if (!lhs
->is_lvalue()) {
516 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
517 error_emitted
= true;
521 ir_rvalue
*new_rhs
= validate_assignment(state
, lhs
->type
, rhs
);
522 if (new_rhs
== NULL
) {
523 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
527 /* If the LHS array was not declared with a size, it takes it size from
528 * the RHS. If the LHS is an l-value and a whole array, it must be a
529 * dereference of a variable. Any other case would require that the LHS
530 * is either not an l-value or not a whole array.
532 if (lhs
->type
->array_size() == 0) {
533 ir_dereference
*const d
= lhs
->as_dereference();
537 ir_variable
*const var
= d
->variable_referenced();
541 if (var
->max_array_access
>= unsigned(rhs
->type
->array_size())) {
542 /* FINISHME: This should actually log the location of the RHS. */
543 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
545 var
->max_array_access
);
548 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
549 rhs
->type
->array_size());
554 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
555 * but not post_inc) need the converted assigned value as an rvalue
556 * to handle things like:
560 * So we always just store the computed value being assigned to a
561 * temporary and return a deref of that temporary. If the rvalue
562 * ends up not being used, the temp will get copy-propagated out.
564 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
566 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
567 instructions
->push_tail(var
);
568 instructions
->push_tail(new(ctx
) ir_assignment(deref_var
,
571 deref_var
= new(ctx
) ir_dereference_variable(var
);
574 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
, NULL
));
576 return new(ctx
) ir_dereference_variable(var
);
580 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
582 void *ctx
= talloc_parent(lvalue
);
585 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
587 instructions
->push_tail(var
);
588 var
->mode
= ir_var_auto
;
590 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
593 /* Once we've created this temporary, mark it read only so it's no
594 * longer considered an lvalue.
596 var
->read_only
= true;
598 return new(ctx
) ir_dereference_variable(var
);
603 ast_node::hir(exec_list
*instructions
,
604 struct _mesa_glsl_parse_state
*state
)
614 ast_expression::hir(exec_list
*instructions
,
615 struct _mesa_glsl_parse_state
*state
)
618 static const int operations
[AST_NUM_OPERATORS
] = {
619 -1, /* ast_assign doesn't convert to ir_expression. */
620 -1, /* ast_plus doesn't convert to ir_expression. */
644 /* Note: The following block of expression types actually convert
645 * to multiple IR instructions.
647 ir_binop_mul
, /* ast_mul_assign */
648 ir_binop_div
, /* ast_div_assign */
649 ir_binop_mod
, /* ast_mod_assign */
650 ir_binop_add
, /* ast_add_assign */
651 ir_binop_sub
, /* ast_sub_assign */
652 ir_binop_lshift
, /* ast_ls_assign */
653 ir_binop_rshift
, /* ast_rs_assign */
654 ir_binop_bit_and
, /* ast_and_assign */
655 ir_binop_bit_xor
, /* ast_xor_assign */
656 ir_binop_bit_or
, /* ast_or_assign */
658 -1, /* ast_conditional doesn't convert to ir_expression. */
659 ir_binop_add
, /* ast_pre_inc. */
660 ir_binop_sub
, /* ast_pre_dec. */
661 ir_binop_add
, /* ast_post_inc. */
662 ir_binop_sub
, /* ast_post_dec. */
663 -1, /* ast_field_selection doesn't conv to ir_expression. */
664 -1, /* ast_array_index doesn't convert to ir_expression. */
665 -1, /* ast_function_call doesn't conv to ir_expression. */
666 -1, /* ast_identifier doesn't convert to ir_expression. */
667 -1, /* ast_int_constant doesn't convert to ir_expression. */
668 -1, /* ast_uint_constant doesn't conv to ir_expression. */
669 -1, /* ast_float_constant doesn't conv to ir_expression. */
670 -1, /* ast_bool_constant doesn't conv to ir_expression. */
671 -1, /* ast_sequence doesn't convert to ir_expression. */
673 ir_rvalue
*result
= NULL
;
675 const struct glsl_type
*type
= glsl_type::error_type
;
676 bool error_emitted
= false;
679 loc
= this->get_location();
681 switch (this->oper
) {
683 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
684 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
686 result
= do_assignment(instructions
, state
, op
[0], op
[1],
687 this->subexpressions
[0]->get_location());
688 error_emitted
= result
->type
->is_error();
694 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
696 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
698 error_emitted
= type
->is_error();
704 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
706 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
708 error_emitted
= type
->is_error();
710 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
718 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
719 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
721 type
= arithmetic_result_type(op
[0], op
[1],
722 (this->oper
== ast_mul
),
724 error_emitted
= type
->is_error();
726 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
731 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
732 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
734 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
736 assert(operations
[this->oper
] == ir_binop_mod
);
738 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
740 error_emitted
= type
->is_error();
745 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-shift operators");
746 error_emitted
= true;
753 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
754 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
756 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
758 /* The relational operators must either generate an error or result
759 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
761 assert(type
->is_error()
762 || ((type
->base_type
== GLSL_TYPE_BOOL
)
763 && type
->is_scalar()));
765 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
767 error_emitted
= type
->is_error();
772 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
773 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
775 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
777 * "The equality operators equal (==), and not equal (!=)
778 * operate on all types. They result in a scalar Boolean. If
779 * the operand types do not match, then there must be a
780 * conversion from Section 4.1.10 "Implicit Conversions"
781 * applied to one operand that can make them match, in which
782 * case this conversion is done."
784 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
785 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
786 || (op
[0]->type
!= op
[1]->type
)) {
787 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
788 "type", (this->oper
== ast_equal
) ? "==" : "!=");
789 error_emitted
= true;
790 } else if ((state
->language_version
<= 110)
791 && (op
[0]->type
->is_array() || op
[1]->type
->is_array())) {
792 _mesa_glsl_error(& loc
, state
, "array comparisons forbidden in "
794 error_emitted
= true;
797 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
799 type
= glsl_type::bool_type
;
801 assert(result
->type
== glsl_type::bool_type
);
807 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
808 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
810 if (state
->language_version
< 130) {
811 _mesa_glsl_error(&loc
, state
, "bit-wise operations require GLSL 1.30");
812 error_emitted
= true;
815 if (!op
[0]->type
->is_integer()) {
816 _mesa_glsl_error(&loc
, state
, "LHS of `%s' must be an integer",
817 operator_string(this->oper
));
818 error_emitted
= true;
821 if (!op
[1]->type
->is_integer()) {
822 _mesa_glsl_error(&loc
, state
, "RHS of `%s' must be an integer",
823 operator_string(this->oper
));
824 error_emitted
= true;
827 if (op
[0]->type
->base_type
!= op
[1]->type
->base_type
) {
828 _mesa_glsl_error(&loc
, state
, "operands of `%s' must have the same "
829 "base type", operator_string(this->oper
));
830 error_emitted
= true;
833 if (op
[0]->type
->is_vector() && op
[1]->type
->is_vector()
834 && op
[0]->type
->vector_elements
!= op
[1]->type
->vector_elements
) {
835 _mesa_glsl_error(&loc
, state
, "operands of `%s' cannot be vectors of "
836 "different sizes", operator_string(this->oper
));
837 error_emitted
= true;
840 type
= op
[0]->type
->is_scalar() ? op
[1]->type
: op
[0]->type
;
841 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
843 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
847 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
849 if (state
->language_version
< 130) {
850 _mesa_glsl_error(&loc
, state
, "bit-wise operations require GLSL 1.30");
851 error_emitted
= true;
854 if (!op
[0]->type
->is_integer()) {
855 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
856 error_emitted
= true;
860 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
863 case ast_logic_and
: {
864 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
866 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
867 YYLTYPE loc
= this->subexpressions
[0]->get_location();
869 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
870 operator_string(this->oper
));
871 error_emitted
= true;
874 ir_constant
*op0_const
= op
[0]->constant_expression_value();
876 if (op0_const
->value
.b
[0]) {
877 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
879 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
880 YYLTYPE loc
= this->subexpressions
[1]->get_location();
882 _mesa_glsl_error(& loc
, state
,
883 "RHS of `%s' must be scalar boolean",
884 operator_string(this->oper
));
885 error_emitted
= true;
891 type
= glsl_type::bool_type
;
893 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
896 instructions
->push_tail(tmp
);
898 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
899 instructions
->push_tail(stmt
);
901 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
903 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
904 YYLTYPE loc
= this->subexpressions
[1]->get_location();
906 _mesa_glsl_error(& loc
, state
,
907 "RHS of `%s' must be scalar boolean",
908 operator_string(this->oper
));
909 error_emitted
= true;
912 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
913 ir_assignment
*const then_assign
=
914 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
915 stmt
->then_instructions
.push_tail(then_assign
);
917 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
918 ir_assignment
*const else_assign
=
919 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false), NULL
);
920 stmt
->else_instructions
.push_tail(else_assign
);
922 result
= new(ctx
) ir_dereference_variable(tmp
);
929 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
931 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
932 YYLTYPE loc
= this->subexpressions
[0]->get_location();
934 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
935 operator_string(this->oper
));
936 error_emitted
= true;
939 ir_constant
*op0_const
= op
[0]->constant_expression_value();
941 if (op0_const
->value
.b
[0]) {
944 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
946 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
947 YYLTYPE loc
= this->subexpressions
[1]->get_location();
949 _mesa_glsl_error(& loc
, state
,
950 "RHS of `%s' must be scalar boolean",
951 operator_string(this->oper
));
952 error_emitted
= true;
956 type
= glsl_type::bool_type
;
958 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
961 instructions
->push_tail(tmp
);
963 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
964 instructions
->push_tail(stmt
);
966 op
[1] = this->subexpressions
[1]->hir(&stmt
->else_instructions
, state
);
968 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
969 YYLTYPE loc
= this->subexpressions
[1]->get_location();
971 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
972 operator_string(this->oper
));
973 error_emitted
= true;
976 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
977 ir_assignment
*const then_assign
=
978 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true), NULL
);
979 stmt
->then_instructions
.push_tail(then_assign
);
981 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
982 ir_assignment
*const else_assign
=
983 new(ctx
) ir_assignment(else_deref
, op
[1], NULL
);
984 stmt
->else_instructions
.push_tail(else_assign
);
986 result
= new(ctx
) ir_dereference_variable(tmp
);
993 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
994 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
997 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
999 type
= glsl_type::bool_type
;
1003 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1005 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
1006 YYLTYPE loc
= this->subexpressions
[0]->get_location();
1008 _mesa_glsl_error(& loc
, state
,
1009 "operand of `!' must be scalar boolean");
1010 error_emitted
= true;
1013 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1015 type
= glsl_type::bool_type
;
1018 case ast_mul_assign
:
1019 case ast_div_assign
:
1020 case ast_add_assign
:
1021 case ast_sub_assign
: {
1022 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1023 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1025 type
= arithmetic_result_type(op
[0], op
[1],
1026 (this->oper
== ast_mul_assign
),
1029 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1032 result
= do_assignment(instructions
, state
,
1033 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1034 this->subexpressions
[0]->get_location());
1035 type
= result
->type
;
1036 error_emitted
= (op
[0]->type
->is_error());
1038 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1039 * explicitly test for this because none of the binary expression
1040 * operators allow array operands either.
1046 case ast_mod_assign
: {
1047 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1048 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1050 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1052 assert(operations
[this->oper
] == ir_binop_mod
);
1054 ir_rvalue
*temp_rhs
;
1055 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1058 result
= do_assignment(instructions
, state
,
1059 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1060 this->subexpressions
[0]->get_location());
1061 type
= result
->type
;
1062 error_emitted
= type
->is_error();
1068 _mesa_glsl_error(& loc
, state
,
1069 "FINISHME: implement bit-shift assignment operators");
1070 error_emitted
= true;
1073 case ast_and_assign
:
1074 case ast_xor_assign
:
1076 _mesa_glsl_error(& loc
, state
,
1077 "FINISHME: implement logic assignment operators");
1078 error_emitted
= true;
1081 case ast_conditional
: {
1082 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1084 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1086 * "The ternary selection operator (?:). It operates on three
1087 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1088 * first expression, which must result in a scalar Boolean."
1090 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
1091 YYLTYPE loc
= this->subexpressions
[0]->get_location();
1093 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
1094 error_emitted
= true;
1097 /* The :? operator is implemented by generating an anonymous temporary
1098 * followed by an if-statement. The last instruction in each branch of
1099 * the if-statement assigns a value to the anonymous temporary. This
1100 * temporary is the r-value of the expression.
1102 exec_list then_instructions
;
1103 exec_list else_instructions
;
1105 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1106 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1108 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1110 * "The second and third expressions can be any type, as
1111 * long their types match, or there is a conversion in
1112 * Section 4.1.10 "Implicit Conversions" that can be applied
1113 * to one of the expressions to make their types match. This
1114 * resulting matching type is the type of the entire
1117 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1118 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1119 || (op
[1]->type
!= op
[2]->type
)) {
1120 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1122 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
1123 "operator must have matching types.");
1124 error_emitted
= true;
1125 type
= glsl_type::error_type
;
1130 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1132 * "The second and third expressions must be the same type, but can
1133 * be of any type other than an array."
1135 if ((state
->language_version
<= 110) && type
->is_array()) {
1136 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
1137 "operator must not be arrays.");
1138 error_emitted
= true;
1141 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1142 ir_constant
*then_val
= op
[1]->constant_expression_value();
1143 ir_constant
*else_val
= op
[2]->constant_expression_value();
1145 if (then_instructions
.is_empty()
1146 && else_instructions
.is_empty()
1147 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1148 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1150 ir_variable
*const tmp
=
1151 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1152 instructions
->push_tail(tmp
);
1154 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1155 instructions
->push_tail(stmt
);
1157 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1158 ir_dereference
*const then_deref
=
1159 new(ctx
) ir_dereference_variable(tmp
);
1160 ir_assignment
*const then_assign
=
1161 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
1162 stmt
->then_instructions
.push_tail(then_assign
);
1164 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1165 ir_dereference
*const else_deref
=
1166 new(ctx
) ir_dereference_variable(tmp
);
1167 ir_assignment
*const else_assign
=
1168 new(ctx
) ir_assignment(else_deref
, op
[2], NULL
);
1169 stmt
->else_instructions
.push_tail(else_assign
);
1171 result
= new(ctx
) ir_dereference_variable(tmp
);
1178 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1179 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1180 op
[1] = new(ctx
) ir_constant(1.0f
);
1182 op
[1] = new(ctx
) ir_constant(1);
1184 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1186 ir_rvalue
*temp_rhs
;
1187 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1190 result
= do_assignment(instructions
, state
,
1191 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1192 this->subexpressions
[0]->get_location());
1193 type
= result
->type
;
1194 error_emitted
= op
[0]->type
->is_error();
1199 case ast_post_dec
: {
1200 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1201 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1202 op
[1] = new(ctx
) ir_constant(1.0f
);
1204 op
[1] = new(ctx
) ir_constant(1);
1206 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1208 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1210 ir_rvalue
*temp_rhs
;
1211 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1214 /* Get a temporary of a copy of the lvalue before it's modified.
1215 * This may get thrown away later.
1217 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1219 (void)do_assignment(instructions
, state
,
1220 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1221 this->subexpressions
[0]->get_location());
1223 type
= result
->type
;
1224 error_emitted
= op
[0]->type
->is_error();
1228 case ast_field_selection
:
1229 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1230 type
= result
->type
;
1233 case ast_array_index
: {
1234 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1236 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1237 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1239 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1241 ir_rvalue
*const array
= op
[0];
1243 result
= new(ctx
) ir_dereference_array(op
[0], op
[1]);
1245 /* Do not use op[0] after this point. Use array.
1253 if (!array
->type
->is_array()
1254 && !array
->type
->is_matrix()
1255 && !array
->type
->is_vector()) {
1256 _mesa_glsl_error(& index_loc
, state
,
1257 "cannot dereference non-array / non-matrix / "
1259 error_emitted
= true;
1262 if (!op
[1]->type
->is_integer()) {
1263 _mesa_glsl_error(& index_loc
, state
,
1264 "array index must be integer type");
1265 error_emitted
= true;
1266 } else if (!op
[1]->type
->is_scalar()) {
1267 _mesa_glsl_error(& index_loc
, state
,
1268 "array index must be scalar");
1269 error_emitted
= true;
1272 /* If the array index is a constant expression and the array has a
1273 * declared size, ensure that the access is in-bounds. If the array
1274 * index is not a constant expression, ensure that the array has a
1277 ir_constant
*const const_index
= op
[1]->constant_expression_value();
1278 if (const_index
!= NULL
) {
1279 const int idx
= const_index
->value
.i
[0];
1280 const char *type_name
;
1283 if (array
->type
->is_matrix()) {
1284 type_name
= "matrix";
1285 } else if (array
->type
->is_vector()) {
1286 type_name
= "vector";
1288 type_name
= "array";
1291 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1293 * "It is illegal to declare an array with a size, and then
1294 * later (in the same shader) index the same array with an
1295 * integral constant expression greater than or equal to the
1296 * declared size. It is also illegal to index an array with a
1297 * negative constant expression."
1299 if (array
->type
->is_matrix()) {
1300 if (array
->type
->row_type()->vector_elements
<= idx
) {
1301 bound
= array
->type
->row_type()->vector_elements
;
1303 } else if (array
->type
->is_vector()) {
1304 if (array
->type
->vector_elements
<= idx
) {
1305 bound
= array
->type
->vector_elements
;
1308 if ((array
->type
->array_size() > 0)
1309 && (array
->type
->array_size() <= idx
)) {
1310 bound
= array
->type
->array_size();
1315 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
1317 error_emitted
= true;
1318 } else if (idx
< 0) {
1319 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
1321 error_emitted
= true;
1324 if (array
->type
->is_array()) {
1325 /* If the array is a variable dereference, it dereferences the
1326 * whole array, by definition. Use this to get the variable.
1328 * FINISHME: Should some methods for getting / setting / testing
1329 * FINISHME: array access limits be added to ir_dereference?
1331 ir_variable
*const v
= array
->whole_variable_referenced();
1332 if ((v
!= NULL
) && (unsigned(idx
) > v
->max_array_access
))
1333 v
->max_array_access
= idx
;
1335 } else if (array
->type
->array_size() == 0) {
1336 _mesa_glsl_error(&loc
, state
, "unsized array index must be constant");
1338 if (array
->type
->is_array()) {
1339 /* whole_variable_referenced can return NULL if the array is a
1340 * member of a structure. In this case it is safe to not update
1341 * the max_array_access field because it is never used for fields
1344 ir_variable
*v
= array
->whole_variable_referenced();
1346 v
->max_array_access
= array
->type
->array_size();
1351 result
->type
= glsl_type::error_type
;
1353 type
= result
->type
;
1357 case ast_function_call
:
1358 /* Should *NEVER* get here. ast_function_call should always be handled
1359 * by ast_function_expression::hir.
1364 case ast_identifier
: {
1365 /* ast_identifier can appear several places in a full abstract syntax
1366 * tree. This particular use must be at location specified in the grammar
1367 * as 'variable_identifier'.
1370 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1372 result
= new(ctx
) ir_dereference_variable(var
);
1375 type
= result
->type
;
1377 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1378 this->primary_expression
.identifier
);
1380 error_emitted
= true;
1385 case ast_int_constant
:
1386 type
= glsl_type::int_type
;
1387 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1390 case ast_uint_constant
:
1391 type
= glsl_type::uint_type
;
1392 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1395 case ast_float_constant
:
1396 type
= glsl_type::float_type
;
1397 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1400 case ast_bool_constant
:
1401 type
= glsl_type::bool_type
;
1402 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1405 case ast_sequence
: {
1406 /* It should not be possible to generate a sequence in the AST without
1407 * any expressions in it.
1409 assert(!this->expressions
.is_empty());
1411 /* The r-value of a sequence is the last expression in the sequence. If
1412 * the other expressions in the sequence do not have side-effects (and
1413 * therefore add instructions to the instruction list), they get dropped
1416 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
)
1417 result
= ast
->hir(instructions
, state
);
1419 type
= result
->type
;
1421 /* Any errors should have already been emitted in the loop above.
1423 error_emitted
= true;
1428 if (type
->is_error() && !error_emitted
)
1429 _mesa_glsl_error(& loc
, state
, "type mismatch");
1436 ast_expression_statement::hir(exec_list
*instructions
,
1437 struct _mesa_glsl_parse_state
*state
)
1439 /* It is possible to have expression statements that don't have an
1440 * expression. This is the solitary semicolon:
1442 * for (i = 0; i < 5; i++)
1445 * In this case the expression will be NULL. Test for NULL and don't do
1446 * anything in that case.
1448 if (expression
!= NULL
)
1449 expression
->hir(instructions
, state
);
1451 /* Statements do not have r-values.
1458 ast_compound_statement::hir(exec_list
*instructions
,
1459 struct _mesa_glsl_parse_state
*state
)
1462 state
->symbols
->push_scope();
1464 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1465 ast
->hir(instructions
, state
);
1468 state
->symbols
->pop_scope();
1470 /* Compound statements do not have r-values.
1476 static const glsl_type
*
1477 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
, ast_node
*array_size
,
1478 struct _mesa_glsl_parse_state
*state
)
1480 unsigned length
= 0;
1482 /* FINISHME: Reject delcarations of multidimensional arrays. */
1484 if (array_size
!= NULL
) {
1485 exec_list dummy_instructions
;
1486 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1487 YYLTYPE loc
= array_size
->get_location();
1489 /* FINISHME: Verify that the grammar forbids side-effects in array
1490 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1492 assert(dummy_instructions
.is_empty());
1495 if (!ir
->type
->is_integer()) {
1496 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1497 } else if (!ir
->type
->is_scalar()) {
1498 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1500 ir_constant
*const size
= ir
->constant_expression_value();
1503 _mesa_glsl_error(& loc
, state
, "array size must be a "
1504 "constant valued expression");
1505 } else if (size
->value
.i
[0] <= 0) {
1506 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1508 assert(size
->type
== ir
->type
);
1509 length
= size
->value
.u
[0];
1513 } else if (state
->es_shader
) {
1514 /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1515 * array declarations have been removed from the language.
1517 _mesa_glsl_error(loc
, state
, "unsized array declarations are not "
1518 "allowed in GLSL ES 1.00.");
1521 return glsl_type::get_array_instance(base
, length
);
1526 ast_type_specifier::glsl_type(const char **name
,
1527 struct _mesa_glsl_parse_state
*state
) const
1529 const struct glsl_type
*type
;
1531 if ((this->type_specifier
== ast_struct
) && (this->type_name
== NULL
)) {
1532 /* FINISHME: Handle annonymous structures. */
1535 type
= state
->symbols
->get_type(this->type_name
);
1536 *name
= this->type_name
;
1538 if (this->is_array
) {
1539 YYLTYPE loc
= this->get_location();
1540 type
= process_array_type(&loc
, type
, this->array_size
, state
);
1549 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1551 struct _mesa_glsl_parse_state
*state
,
1554 if (qual
->invariant
)
1557 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1558 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
1559 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1565 if (qual
->attribute
&& state
->target
!= vertex_shader
) {
1566 var
->type
= glsl_type::error_type
;
1567 _mesa_glsl_error(loc
, state
,
1568 "`attribute' variables may not be declared in the "
1570 _mesa_glsl_shader_target_name(state
->target
));
1573 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1575 * "The varying qualifier can be used only with the data types
1576 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1579 if (qual
->varying
) {
1580 const glsl_type
*non_array_type
;
1582 if (var
->type
&& var
->type
->is_array())
1583 non_array_type
= var
->type
->fields
.array
;
1585 non_array_type
= var
->type
;
1587 if (non_array_type
&& non_array_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1588 var
->type
= glsl_type::error_type
;
1589 _mesa_glsl_error(loc
, state
,
1590 "varying variables must be of base type float");
1594 /* If there is no qualifier that changes the mode of the variable, leave
1595 * the setting alone.
1597 if (qual
->in
&& qual
->out
)
1598 var
->mode
= ir_var_inout
;
1599 else if (qual
->attribute
|| qual
->in
1600 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1601 var
->mode
= ir_var_in
;
1602 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
1603 var
->mode
= ir_var_out
;
1604 else if (qual
->uniform
)
1605 var
->mode
= ir_var_uniform
;
1608 var
->interpolation
= ir_var_flat
;
1609 else if (qual
->noperspective
)
1610 var
->interpolation
= ir_var_noperspective
;
1612 var
->interpolation
= ir_var_smooth
;
1614 var
->pixel_center_integer
= qual
->pixel_center_integer
;
1615 var
->origin_upper_left
= qual
->origin_upper_left
;
1616 if ((qual
->origin_upper_left
|| qual
->pixel_center_integer
)
1617 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
1618 const char *const qual_string
= (qual
->origin_upper_left
)
1619 ? "origin_upper_left" : "pixel_center_integer";
1621 _mesa_glsl_error(loc
, state
,
1622 "layout qualifier `%s' can only be applied to "
1623 "fragment shader input `gl_FragCoord'",
1627 if (var
->type
->is_array() && (state
->language_version
>= 120)) {
1628 var
->array_lvalue
= true;
1634 ast_declarator_list::hir(exec_list
*instructions
,
1635 struct _mesa_glsl_parse_state
*state
)
1638 const struct glsl_type
*decl_type
;
1639 const char *type_name
= NULL
;
1640 ir_rvalue
*result
= NULL
;
1641 YYLTYPE loc
= this->get_location();
1643 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
1645 * "To ensure that a particular output variable is invariant, it is
1646 * necessary to use the invariant qualifier. It can either be used to
1647 * qualify a previously declared variable as being invariant
1649 * invariant gl_Position; // make existing gl_Position be invariant"
1651 * In these cases the parser will set the 'invariant' flag in the declarator
1652 * list, and the type will be NULL.
1654 if (this->invariant
) {
1655 assert(this->type
== NULL
);
1657 if (state
->current_function
!= NULL
) {
1658 _mesa_glsl_error(& loc
, state
,
1659 "All uses of `invariant' keyword must be at global "
1663 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1664 assert(!decl
->is_array
);
1665 assert(decl
->array_size
== NULL
);
1666 assert(decl
->initializer
== NULL
);
1668 ir_variable
*const earlier
=
1669 state
->symbols
->get_variable(decl
->identifier
);
1670 if (earlier
== NULL
) {
1671 _mesa_glsl_error(& loc
, state
,
1672 "Undeclared variable `%s' cannot be marked "
1673 "invariant\n", decl
->identifier
);
1674 } else if ((state
->target
== vertex_shader
)
1675 && (earlier
->mode
!= ir_var_out
)) {
1676 _mesa_glsl_error(& loc
, state
,
1677 "`%s' cannot be marked invariant, vertex shader "
1678 "outputs only\n", decl
->identifier
);
1679 } else if ((state
->target
== fragment_shader
)
1680 && (earlier
->mode
!= ir_var_in
)) {
1681 _mesa_glsl_error(& loc
, state
,
1682 "`%s' cannot be marked invariant, fragment shader "
1683 "inputs only\n", decl
->identifier
);
1685 earlier
->invariant
= true;
1689 /* Invariant redeclarations do not have r-values.
1694 assert(this->type
!= NULL
);
1695 assert(!this->invariant
);
1697 /* The type specifier may contain a structure definition. Process that
1698 * before any of the variable declarations.
1700 (void) this->type
->specifier
->hir(instructions
, state
);
1702 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1703 if (this->declarations
.is_empty()) {
1704 /* The only valid case where the declaration list can be empty is when
1705 * the declaration is setting the default precision of a built-in type
1706 * (e.g., 'precision highp vec4;').
1709 if (decl_type
!= NULL
) {
1711 _mesa_glsl_error(& loc
, state
, "incomplete declaration");
1715 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1716 const struct glsl_type
*var_type
;
1719 /* FINISHME: Emit a warning if a variable declaration shadows a
1720 * FINISHME: declaration at a higher scope.
1723 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1724 if (type_name
!= NULL
) {
1725 _mesa_glsl_error(& loc
, state
,
1726 "invalid type `%s' in declaration of `%s'",
1727 type_name
, decl
->identifier
);
1729 _mesa_glsl_error(& loc
, state
,
1730 "invalid type in declaration of `%s'",
1736 if (decl
->is_array
) {
1737 var_type
= process_array_type(&loc
, decl_type
, decl
->array_size
,
1740 var_type
= decl_type
;
1743 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
1745 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1747 * "Global variables can only use the qualifiers const,
1748 * attribute, uni form, or varying. Only one may be
1751 * Local variables can only use the qualifier const."
1753 * This is relaxed in GLSL 1.30.
1755 if (state
->language_version
< 120) {
1756 if (this->type
->qualifier
.out
) {
1757 _mesa_glsl_error(& loc
, state
,
1758 "`out' qualifier in declaration of `%s' "
1759 "only valid for function parameters in GLSL 1.10.",
1762 if (this->type
->qualifier
.in
) {
1763 _mesa_glsl_error(& loc
, state
,
1764 "`in' qualifier in declaration of `%s' "
1765 "only valid for function parameters in GLSL 1.10.",
1768 /* FINISHME: Test for other invalid qualifiers. */
1771 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1774 if (this->type
->qualifier
.invariant
) {
1775 if ((state
->target
== vertex_shader
) && !(var
->mode
== ir_var_out
||
1776 var
->mode
== ir_var_inout
)) {
1777 /* FINISHME: Note that this doesn't work for invariant on
1778 * a function signature outval
1780 _mesa_glsl_error(& loc
, state
,
1781 "`%s' cannot be marked invariant, vertex shader "
1782 "outputs only\n", var
->name
);
1783 } else if ((state
->target
== fragment_shader
) &&
1784 !(var
->mode
== ir_var_in
|| var
->mode
== ir_var_inout
)) {
1785 /* FINISHME: Note that this doesn't work for invariant on
1786 * a function signature inval
1788 _mesa_glsl_error(& loc
, state
,
1789 "`%s' cannot be marked invariant, fragment shader "
1790 "inputs only\n", var
->name
);
1794 if (state
->current_function
!= NULL
) {
1795 const char *mode
= NULL
;
1796 const char *extra
= "";
1798 /* There is no need to check for 'inout' here because the parser will
1799 * only allow that in function parameter lists.
1801 if (this->type
->qualifier
.attribute
) {
1803 } else if (this->type
->qualifier
.uniform
) {
1805 } else if (this->type
->qualifier
.varying
) {
1807 } else if (this->type
->qualifier
.in
) {
1809 extra
= " or in function parameter list";
1810 } else if (this->type
->qualifier
.out
) {
1812 extra
= " or in function parameter list";
1816 _mesa_glsl_error(& loc
, state
,
1817 "%s variable `%s' must be declared at "
1819 mode
, var
->name
, extra
);
1821 } else if (var
->mode
== ir_var_in
) {
1822 if (state
->target
== vertex_shader
) {
1823 bool error_emitted
= false;
1825 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1827 * "Vertex shader inputs can only be float, floating-point
1828 * vectors, matrices, signed and unsigned integers and integer
1829 * vectors. Vertex shader inputs can also form arrays of these
1830 * types, but not structures."
1832 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1834 * "Vertex shader inputs can only be float, floating-point
1835 * vectors, matrices, signed and unsigned integers and integer
1836 * vectors. They cannot be arrays or structures."
1838 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1840 * "The attribute qualifier can be used only with float,
1841 * floating-point vectors, and matrices. Attribute variables
1842 * cannot be declared as arrays or structures."
1844 const glsl_type
*check_type
= var
->type
->is_array()
1845 ? var
->type
->fields
.array
: var
->type
;
1847 switch (check_type
->base_type
) {
1848 case GLSL_TYPE_FLOAT
:
1850 case GLSL_TYPE_UINT
:
1852 if (state
->language_version
> 120)
1856 _mesa_glsl_error(& loc
, state
,
1857 "vertex shader input / attribute cannot have "
1859 var
->type
->is_array() ? "array of " : "",
1861 error_emitted
= true;
1864 if (!error_emitted
&& (state
->language_version
<= 130)
1865 && var
->type
->is_array()) {
1866 _mesa_glsl_error(& loc
, state
,
1867 "vertex shader input / attribute cannot have "
1869 error_emitted
= true;
1874 /* Process the initializer and add its instructions to a temporary
1875 * list. This list will be added to the instruction stream (below) after
1876 * the declaration is added. This is done because in some cases (such as
1877 * redeclarations) the declaration may not actually be added to the
1878 * instruction stream.
1880 exec_list initializer_instructions
;
1881 if (decl
->initializer
!= NULL
) {
1882 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
1884 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1886 * "All uniform variables are read-only and are initialized either
1887 * directly by an application via API commands, or indirectly by
1890 if ((state
->language_version
<= 110)
1891 && (var
->mode
== ir_var_uniform
)) {
1892 _mesa_glsl_error(& initializer_loc
, state
,
1893 "cannot initialize uniforms in GLSL 1.10");
1896 if (var
->type
->is_sampler()) {
1897 _mesa_glsl_error(& initializer_loc
, state
,
1898 "cannot initialize samplers");
1901 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1902 _mesa_glsl_error(& initializer_loc
, state
,
1903 "cannot initialize %s shader input / %s",
1904 _mesa_glsl_shader_target_name(state
->target
),
1905 (state
->target
== vertex_shader
)
1906 ? "attribute" : "varying");
1909 ir_dereference
*const lhs
= new(ctx
) ir_dereference_variable(var
);
1910 ir_rvalue
*rhs
= decl
->initializer
->hir(&initializer_instructions
,
1913 /* Calculate the constant value if this is a const or uniform
1916 if (this->type
->qualifier
.constant
|| this->type
->qualifier
.uniform
) {
1917 ir_rvalue
*new_rhs
= validate_assignment(state
, var
->type
, rhs
);
1918 if (new_rhs
!= NULL
) {
1921 ir_constant
*constant_value
= rhs
->constant_expression_value();
1922 if (!constant_value
) {
1923 _mesa_glsl_error(& initializer_loc
, state
,
1924 "initializer of %s variable `%s' must be a "
1925 "constant expression",
1926 (this->type
->qualifier
.constant
)
1927 ? "const" : "uniform",
1929 if (var
->type
->is_numeric()) {
1930 /* Reduce cascading errors. */
1931 var
->constant_value
= ir_constant::zero(ctx
, var
->type
);
1934 rhs
= constant_value
;
1935 var
->constant_value
= constant_value
;
1938 _mesa_glsl_error(&initializer_loc
, state
,
1939 "initializer of type %s cannot be assigned to "
1940 "variable of type %s",
1941 rhs
->type
->name
, var
->type
->name
);
1942 if (var
->type
->is_numeric()) {
1943 /* Reduce cascading errors. */
1944 var
->constant_value
= ir_constant::zero(ctx
, var
->type
);
1949 if (rhs
&& !rhs
->type
->is_error()) {
1950 bool temp
= var
->read_only
;
1951 if (this->type
->qualifier
.constant
)
1952 var
->read_only
= false;
1954 /* Never emit code to initialize a uniform.
1956 if (!this->type
->qualifier
.uniform
)
1957 result
= do_assignment(&initializer_instructions
, state
,
1959 this->get_location());
1960 var
->read_only
= temp
;
1964 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1966 * "It is an error to write to a const variable outside of
1967 * its declaration, so they must be initialized when
1970 if (this->type
->qualifier
.constant
&& decl
->initializer
== NULL
) {
1971 _mesa_glsl_error(& loc
, state
,
1972 "const declaration of `%s' must be initialized");
1975 /* Check if this declaration is actually a re-declaration, either to
1976 * resize an array or add qualifiers to an existing variable.
1978 * This is allowed for variables in the current scope, or when at
1979 * global scope (for built-ins in the implicit outer scope).
1981 ir_variable
*earlier
= state
->symbols
->get_variable(decl
->identifier
);
1982 if (earlier
!= NULL
&& (state
->current_function
== NULL
||
1983 state
->symbols
->name_declared_this_scope(decl
->identifier
))) {
1985 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1987 * "It is legal to declare an array without a size and then
1988 * later re-declare the same name as an array of the same
1989 * type and specify a size."
1991 if ((earlier
->type
->array_size() == 0)
1992 && var
->type
->is_array()
1993 && (var
->type
->element_type() == earlier
->type
->element_type())) {
1994 /* FINISHME: This doesn't match the qualifiers on the two
1995 * FINISHME: declarations. It's not 100% clear whether this is
1996 * FINISHME: required or not.
1999 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
2001 * "The size [of gl_TexCoord] can be at most
2002 * gl_MaxTextureCoords."
2004 const unsigned size
= unsigned(var
->type
->array_size());
2005 if ((strcmp("gl_TexCoord", var
->name
) == 0)
2006 && (size
> state
->Const
.MaxTextureCoords
)) {
2007 YYLTYPE loc
= this->get_location();
2009 _mesa_glsl_error(& loc
, state
, "`gl_TexCoord' array size cannot "
2010 "be larger than gl_MaxTextureCoords (%u)\n",
2011 state
->Const
.MaxTextureCoords
);
2012 } else if ((size
> 0) && (size
<= earlier
->max_array_access
)) {
2013 YYLTYPE loc
= this->get_location();
2015 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2017 earlier
->max_array_access
);
2020 earlier
->type
= var
->type
;
2023 } else if (state
->extensions
->ARB_fragment_coord_conventions
2024 && strcmp(var
->name
, "gl_FragCoord") == 0
2025 && earlier
->type
== var
->type
2026 && earlier
->mode
== var
->mode
) {
2027 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2030 earlier
->origin_upper_left
= var
->origin_upper_left
;
2031 earlier
->pixel_center_integer
= var
->pixel_center_integer
;
2033 YYLTYPE loc
= this->get_location();
2034 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", decl
->identifier
);
2040 /* By now, we know it's a new variable declaration (we didn't hit the
2041 * above "continue").
2043 * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2045 * "Identifiers starting with "gl_" are reserved for use by
2046 * OpenGL, and may not be declared in a shader as either a
2047 * variable or a function."
2049 if (strncmp(decl
->identifier
, "gl_", 3) == 0)
2050 _mesa_glsl_error(& loc
, state
,
2051 "identifier `%s' uses reserved `gl_' prefix",
2054 /* Add the variable to the symbol table. Note that the initializer's
2055 * IR was already processed earlier (though it hasn't been emitted yet),
2056 * without the variable in scope.
2058 * This differs from most C-like languages, but it follows the GLSL
2059 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
2062 * "Within a declaration, the scope of a name starts immediately
2063 * after the initializer if present or immediately after the name
2064 * being declared if not."
2066 if (!state
->symbols
->add_variable(var
->name
, var
)) {
2067 YYLTYPE loc
= this->get_location();
2068 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
2069 "current scope", decl
->identifier
);
2073 /* Push the variable declaration to the top. It means that all
2074 * the variable declarations will appear in a funny
2075 * last-to-first order, but otherwise we run into trouble if a
2076 * function is prototyped, a global var is decled, then the
2077 * function is defined with usage of the global var. See
2078 * glslparsertest's CorrectModule.frag.
2080 instructions
->push_head(var
);
2081 instructions
->append_list(&initializer_instructions
);
2085 /* Generally, variable declarations do not have r-values. However,
2086 * one is used for the declaration in
2088 * while (bool b = some_condition()) {
2092 * so we return the rvalue from the last seen declaration here.
2099 ast_parameter_declarator::hir(exec_list
*instructions
,
2100 struct _mesa_glsl_parse_state
*state
)
2103 const struct glsl_type
*type
;
2104 const char *name
= NULL
;
2105 YYLTYPE loc
= this->get_location();
2107 type
= this->type
->specifier
->glsl_type(& name
, state
);
2111 _mesa_glsl_error(& loc
, state
,
2112 "invalid type `%s' in declaration of `%s'",
2113 name
, this->identifier
);
2115 _mesa_glsl_error(& loc
, state
,
2116 "invalid type in declaration of `%s'",
2120 type
= glsl_type::error_type
;
2123 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2125 * "Functions that accept no input arguments need not use void in the
2126 * argument list because prototypes (or definitions) are required and
2127 * therefore there is no ambiguity when an empty argument list "( )" is
2128 * declared. The idiom "(void)" as a parameter list is provided for
2131 * Placing this check here prevents a void parameter being set up
2132 * for a function, which avoids tripping up checks for main taking
2133 * parameters and lookups of an unnamed symbol.
2135 if (type
->is_void()) {
2136 if (this->identifier
!= NULL
)
2137 _mesa_glsl_error(& loc
, state
,
2138 "named parameter cannot have type `void'");
2144 if (formal_parameter
&& (this->identifier
== NULL
)) {
2145 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
2149 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
2150 * call already handled the "vec4[..] foo" case.
2152 if (this->is_array
) {
2153 type
= process_array_type(&loc
, type
, this->array_size
, state
);
2156 if (type
->array_size() == 0) {
2157 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
2158 "a declared size.");
2159 type
= glsl_type::error_type
;
2163 ir_variable
*var
= new(ctx
) ir_variable(type
, this->identifier
, ir_var_in
);
2165 /* Apply any specified qualifiers to the parameter declaration. Note that
2166 * for function parameters the default mode is 'in'.
2168 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
2170 instructions
->push_tail(var
);
2172 /* Parameter declarations do not have r-values.
2179 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
2181 exec_list
*ir_parameters
,
2182 _mesa_glsl_parse_state
*state
)
2184 ast_parameter_declarator
*void_param
= NULL
;
2187 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
2188 param
->formal_parameter
= formal
;
2189 param
->hir(ir_parameters
, state
);
2197 if ((void_param
!= NULL
) && (count
> 1)) {
2198 YYLTYPE loc
= void_param
->get_location();
2200 _mesa_glsl_error(& loc
, state
,
2201 "`void' parameter must be only parameter");
2207 ast_function::hir(exec_list
*instructions
,
2208 struct _mesa_glsl_parse_state
*state
)
2211 ir_function
*f
= NULL
;
2212 ir_function_signature
*sig
= NULL
;
2213 exec_list hir_parameters
;
2215 const char *const name
= identifier
;
2217 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
2219 * "Function declarations (prototypes) cannot occur inside of functions;
2220 * they must be at global scope, or for the built-in functions, outside
2221 * the global scope."
2223 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
2225 * "User defined functions may only be defined within the global scope."
2227 * Note that this language does not appear in GLSL 1.10.
2229 if ((state
->current_function
!= NULL
) && (state
->language_version
!= 110)) {
2230 YYLTYPE loc
= this->get_location();
2231 _mesa_glsl_error(&loc
, state
,
2232 "declaration of function `%s' not allowed within "
2233 "function body", name
);
2236 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2238 * "Identifiers starting with "gl_" are reserved for use by
2239 * OpenGL, and may not be declared in a shader as either a
2240 * variable or a function."
2242 if (strncmp(name
, "gl_", 3) == 0) {
2243 YYLTYPE loc
= this->get_location();
2244 _mesa_glsl_error(&loc
, state
,
2245 "identifier `%s' uses reserved `gl_' prefix", name
);
2248 /* Convert the list of function parameters to HIR now so that they can be
2249 * used below to compare this function's signature with previously seen
2250 * signatures for functions with the same name.
2252 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
2254 & hir_parameters
, state
);
2256 const char *return_type_name
;
2257 const glsl_type
*return_type
=
2258 this->return_type
->specifier
->glsl_type(& return_type_name
, state
);
2261 YYLTYPE loc
= this->get_location();
2262 _mesa_glsl_error(&loc
, state
,
2263 "function `%s' has undeclared return type `%s'",
2264 name
, return_type_name
);
2265 return_type
= glsl_type::error_type
;
2268 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2269 * "No qualifier is allowed on the return type of a function."
2271 if (this->return_type
->has_qualifiers()) {
2272 YYLTYPE loc
= this->get_location();
2273 _mesa_glsl_error(& loc
, state
,
2274 "function `%s' return type has qualifiers", name
);
2277 /* Verify that this function's signature either doesn't match a previously
2278 * seen signature for a function with the same name, or, if a match is found,
2279 * that the previously seen signature does not have an associated definition.
2281 f
= state
->symbols
->get_function(name
);
2282 if (f
!= NULL
&& !f
->is_builtin
) {
2283 sig
= f
->exact_matching_signature(&hir_parameters
);
2285 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
2286 if (badvar
!= NULL
) {
2287 YYLTYPE loc
= this->get_location();
2289 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
2290 "qualifiers don't match prototype", name
, badvar
);
2293 if (sig
->return_type
!= return_type
) {
2294 YYLTYPE loc
= this->get_location();
2296 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
2297 "match prototype", name
);
2300 if (is_definition
&& sig
->is_defined
) {
2301 YYLTYPE loc
= this->get_location();
2303 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
2307 f
= new(ctx
) ir_function(name
);
2308 if (!state
->symbols
->add_function(f
->name
, f
)) {
2309 /* This function name shadows a non-function use of the same name. */
2310 YYLTYPE loc
= this->get_location();
2312 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
2313 "non-function", name
);
2317 /* Emit the new function header */
2318 if (state
->current_function
== NULL
)
2319 instructions
->push_tail(f
);
2321 /* IR invariants disallow function declarations or definitions nested
2322 * within other function definitions. Insert the new ir_function
2323 * block in the instruction sequence before the ir_function block
2324 * containing the current ir_function_signature.
2326 * This can only happen in a GLSL 1.10 shader. In all other GLSL
2327 * versions this nesting is disallowed. There is a check for this at
2328 * the top of this function.
2330 ir_function
*const curr
=
2331 const_cast<ir_function
*>(state
->current_function
->function());
2333 curr
->insert_before(f
);
2337 /* Verify the return type of main() */
2338 if (strcmp(name
, "main") == 0) {
2339 if (! return_type
->is_void()) {
2340 YYLTYPE loc
= this->get_location();
2342 _mesa_glsl_error(& loc
, state
, "main() must return void");
2345 if (!hir_parameters
.is_empty()) {
2346 YYLTYPE loc
= this->get_location();
2348 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
2352 /* Finish storing the information about this new function in its signature.
2355 sig
= new(ctx
) ir_function_signature(return_type
);
2356 f
->add_signature(sig
);
2359 sig
->replace_parameters(&hir_parameters
);
2362 /* Function declarations (prototypes) do not have r-values.
2369 ast_function_definition::hir(exec_list
*instructions
,
2370 struct _mesa_glsl_parse_state
*state
)
2372 prototype
->is_definition
= true;
2373 prototype
->hir(instructions
, state
);
2375 ir_function_signature
*signature
= prototype
->signature
;
2376 if (signature
== NULL
)
2379 assert(state
->current_function
== NULL
);
2380 state
->current_function
= signature
;
2381 state
->found_return
= false;
2383 /* Duplicate parameters declared in the prototype as concrete variables.
2384 * Add these to the symbol table.
2386 state
->symbols
->push_scope();
2387 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
2388 ir_variable
*const var
= ((ir_instruction
*) iter
.get())->as_variable();
2390 assert(var
!= NULL
);
2392 /* The only way a parameter would "exist" is if two parameters have
2395 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
2396 YYLTYPE loc
= this->get_location();
2398 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
2400 state
->symbols
->add_variable(var
->name
, var
);
2404 /* Convert the body of the function to HIR. */
2405 this->body
->hir(&signature
->body
, state
);
2406 signature
->is_defined
= true;
2408 state
->symbols
->pop_scope();
2410 assert(state
->current_function
== signature
);
2411 state
->current_function
= NULL
;
2413 if (!signature
->return_type
->is_void() && !state
->found_return
) {
2414 YYLTYPE loc
= this->get_location();
2415 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
2416 "%s, but no return statement",
2417 signature
->function_name(),
2418 signature
->return_type
->name
);
2421 /* Function definitions do not have r-values.
2428 ast_jump_statement::hir(exec_list
*instructions
,
2429 struct _mesa_glsl_parse_state
*state
)
2436 assert(state
->current_function
);
2438 if (opt_return_value
) {
2439 if (state
->current_function
->return_type
->base_type
==
2441 YYLTYPE loc
= this->get_location();
2443 _mesa_glsl_error(& loc
, state
,
2444 "`return` with a value, in function `%s' "
2446 state
->current_function
->function_name());
2449 ir_expression
*const ret
= (ir_expression
*)
2450 opt_return_value
->hir(instructions
, state
);
2451 assert(ret
!= NULL
);
2453 /* Implicit conversions are not allowed for return values. */
2454 if (state
->current_function
->return_type
!= ret
->type
) {
2455 YYLTYPE loc
= this->get_location();
2457 _mesa_glsl_error(& loc
, state
,
2458 "`return' with wrong type %s, in function `%s' "
2461 state
->current_function
->function_name(),
2462 state
->current_function
->return_type
->name
);
2465 inst
= new(ctx
) ir_return(ret
);
2467 if (state
->current_function
->return_type
->base_type
!=
2469 YYLTYPE loc
= this->get_location();
2471 _mesa_glsl_error(& loc
, state
,
2472 "`return' with no value, in function %s returning "
2474 state
->current_function
->function_name());
2476 inst
= new(ctx
) ir_return
;
2479 state
->found_return
= true;
2480 instructions
->push_tail(inst
);
2485 if (state
->target
!= fragment_shader
) {
2486 YYLTYPE loc
= this->get_location();
2488 _mesa_glsl_error(& loc
, state
,
2489 "`discard' may only appear in a fragment shader");
2491 instructions
->push_tail(new(ctx
) ir_discard
);
2496 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2497 * FINISHME: and they use a different IR instruction for 'break'.
2499 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2500 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2503 if (state
->loop_or_switch_nesting
== NULL
) {
2504 YYLTYPE loc
= this->get_location();
2506 _mesa_glsl_error(& loc
, state
,
2507 "`%s' may only appear in a loop",
2508 (mode
== ast_break
) ? "break" : "continue");
2510 ir_loop
*const loop
= state
->loop_or_switch_nesting
->as_loop();
2512 /* Inline the for loop expression again, since we don't know
2513 * where near the end of the loop body the normal copy of it
2514 * is going to be placed.
2516 if (mode
== ast_continue
&&
2517 state
->loop_or_switch_nesting_ast
->rest_expression
) {
2518 state
->loop_or_switch_nesting_ast
->rest_expression
->hir(instructions
,
2523 ir_loop_jump
*const jump
=
2524 new(ctx
) ir_loop_jump((mode
== ast_break
)
2525 ? ir_loop_jump::jump_break
2526 : ir_loop_jump::jump_continue
);
2527 instructions
->push_tail(jump
);
2534 /* Jump instructions do not have r-values.
2541 ast_selection_statement::hir(exec_list
*instructions
,
2542 struct _mesa_glsl_parse_state
*state
)
2546 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
2548 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2550 * "Any expression whose type evaluates to a Boolean can be used as the
2551 * conditional expression bool-expression. Vector types are not accepted
2552 * as the expression to if."
2554 * The checks are separated so that higher quality diagnostics can be
2555 * generated for cases where both rules are violated.
2557 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
2558 YYLTYPE loc
= this->condition
->get_location();
2560 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
2564 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
2566 if (then_statement
!= NULL
) {
2567 state
->symbols
->push_scope();
2568 then_statement
->hir(& stmt
->then_instructions
, state
);
2569 state
->symbols
->pop_scope();
2572 if (else_statement
!= NULL
) {
2573 state
->symbols
->push_scope();
2574 else_statement
->hir(& stmt
->else_instructions
, state
);
2575 state
->symbols
->pop_scope();
2578 instructions
->push_tail(stmt
);
2580 /* if-statements do not have r-values.
2587 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
2588 struct _mesa_glsl_parse_state
*state
)
2592 if (condition
!= NULL
) {
2593 ir_rvalue
*const cond
=
2594 condition
->hir(& stmt
->body_instructions
, state
);
2597 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
2598 YYLTYPE loc
= condition
->get_location();
2600 _mesa_glsl_error(& loc
, state
,
2601 "loop condition must be scalar boolean");
2603 /* As the first code in the loop body, generate a block that looks
2604 * like 'if (!condition) break;' as the loop termination condition.
2606 ir_rvalue
*const not_cond
=
2607 new(ctx
) ir_expression(ir_unop_logic_not
, glsl_type::bool_type
, cond
,
2610 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
2612 ir_jump
*const break_stmt
=
2613 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
2615 if_stmt
->then_instructions
.push_tail(break_stmt
);
2616 stmt
->body_instructions
.push_tail(if_stmt
);
2623 ast_iteration_statement::hir(exec_list
*instructions
,
2624 struct _mesa_glsl_parse_state
*state
)
2628 /* For-loops and while-loops start a new scope, but do-while loops do not.
2630 if (mode
!= ast_do_while
)
2631 state
->symbols
->push_scope();
2633 if (init_statement
!= NULL
)
2634 init_statement
->hir(instructions
, state
);
2636 ir_loop
*const stmt
= new(ctx
) ir_loop();
2637 instructions
->push_tail(stmt
);
2639 /* Track the current loop and / or switch-statement nesting.
2641 ir_instruction
*const nesting
= state
->loop_or_switch_nesting
;
2642 ast_iteration_statement
*nesting_ast
= state
->loop_or_switch_nesting_ast
;
2644 state
->loop_or_switch_nesting
= stmt
;
2645 state
->loop_or_switch_nesting_ast
= this;
2647 if (mode
!= ast_do_while
)
2648 condition_to_hir(stmt
, state
);
2651 body
->hir(& stmt
->body_instructions
, state
);
2653 if (rest_expression
!= NULL
)
2654 rest_expression
->hir(& stmt
->body_instructions
, state
);
2656 if (mode
== ast_do_while
)
2657 condition_to_hir(stmt
, state
);
2659 if (mode
!= ast_do_while
)
2660 state
->symbols
->pop_scope();
2662 /* Restore previous nesting before returning.
2664 state
->loop_or_switch_nesting
= nesting
;
2665 state
->loop_or_switch_nesting_ast
= nesting_ast
;
2667 /* Loops do not have r-values.
2674 ast_type_specifier::hir(exec_list
*instructions
,
2675 struct _mesa_glsl_parse_state
*state
)
2677 if (this->structure
!= NULL
)
2678 return this->structure
->hir(instructions
, state
);
2685 ast_struct_specifier::hir(exec_list
*instructions
,
2686 struct _mesa_glsl_parse_state
*state
)
2688 unsigned decl_count
= 0;
2690 /* Make an initial pass over the list of structure fields to determine how
2691 * many there are. Each element in this list is an ast_declarator_list.
2692 * This means that we actually need to count the number of elements in the
2693 * 'declarations' list in each of the elements.
2695 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2696 &this->declarations
) {
2697 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
2703 /* Allocate storage for the structure fields and process the field
2704 * declarations. As the declarations are processed, try to also convert
2705 * the types to HIR. This ensures that structure definitions embedded in
2706 * other structure definitions are processed.
2708 glsl_struct_field
*const fields
= talloc_array(state
, glsl_struct_field
,
2712 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2713 &this->declarations
) {
2714 const char *type_name
;
2716 decl_list
->type
->specifier
->hir(instructions
, state
);
2718 const glsl_type
*decl_type
=
2719 decl_list
->type
->specifier
->glsl_type(& type_name
, state
);
2721 foreach_list_typed (ast_declaration
, decl
, link
,
2722 &decl_list
->declarations
) {
2723 const struct glsl_type
*field_type
= decl_type
;
2724 if (decl
->is_array
) {
2725 YYLTYPE loc
= decl
->get_location();
2726 field_type
= process_array_type(&loc
, decl_type
, decl
->array_size
,
2729 fields
[i
].type
= (field_type
!= NULL
)
2730 ? field_type
: glsl_type::error_type
;
2731 fields
[i
].name
= decl
->identifier
;
2736 assert(i
== decl_count
);
2739 if (this->name
== NULL
) {
2740 static unsigned anon_count
= 1;
2743 snprintf(buf
, sizeof(buf
), "#anon_struct_%04x", anon_count
);
2751 const glsl_type
*t
=
2752 glsl_type::get_record_instance(fields
, decl_count
, name
);
2754 YYLTYPE loc
= this->get_location();
2755 if (!state
->symbols
->add_type(name
, t
)) {
2756 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
2759 const glsl_type
**s
= (const glsl_type
**)
2760 realloc(state
->user_structures
,
2761 sizeof(state
->user_structures
[0]) *
2762 (state
->num_user_structures
+ 1));
2764 s
[state
->num_user_structures
] = t
;
2765 state
->user_structures
= s
;
2766 state
->num_user_structures
++;
2770 /* Structure type definitions do not have r-values.