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
->current_function
= NULL
;
67 /* Section 4.2 of the GLSL 1.20 specification states:
68 * "The built-in functions are scoped in a scope outside the global scope
69 * users declare global variables in. That is, a shader's global scope,
70 * available for user-defined functions and global variables, is nested
71 * inside the scope containing the built-in functions."
73 * Since built-in functions like ftransform() access built-in variables,
74 * it follows that those must be in the outer scope as well.
76 * We push scope here to create this nesting effect...but don't pop.
77 * This way, a shader's globals are still in the symbol table for use
80 state
->symbols
->push_scope();
82 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
83 ast
->hir(instructions
, state
);
88 * If a conversion is available, convert one operand to a different type
90 * The \c from \c ir_rvalue is converted "in place".
92 * \param to Type that the operand it to be converted to
93 * \param from Operand that is being converted
94 * \param state GLSL compiler state
97 * If a conversion is possible (or unnecessary), \c true is returned.
98 * Otherwise \c false is returned.
101 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
102 struct _mesa_glsl_parse_state
*state
)
105 if (to
->base_type
== from
->type
->base_type
)
108 /* This conversion was added in GLSL 1.20. If the compilation mode is
109 * GLSL 1.10, the conversion is skipped.
111 if (state
->language_version
< 120)
114 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
116 * "There are no implicit array or structure conversions. For
117 * example, an array of int cannot be implicitly converted to an
118 * array of float. There are no implicit conversions between
119 * signed and unsigned integers."
121 /* FINISHME: The above comment is partially a lie. There is int/uint
122 * FINISHME: conversion for immediate constants.
124 if (!to
->is_float() || !from
->type
->is_numeric())
127 /* Convert to a floating point type with the same number of components
128 * as the original type - i.e. int to float, not int to vec4.
130 to
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, from
->type
->vector_elements
,
131 from
->type
->matrix_columns
);
133 switch (from
->type
->base_type
) {
135 from
= new(ctx
) ir_expression(ir_unop_i2f
, to
, from
, NULL
);
138 from
= new(ctx
) ir_expression(ir_unop_u2f
, to
, from
, NULL
);
141 from
= new(ctx
) ir_expression(ir_unop_b2f
, to
, from
, NULL
);
151 static const struct glsl_type
*
152 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
154 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
156 const glsl_type
*type_a
= value_a
->type
;
157 const glsl_type
*type_b
= value_b
->type
;
159 /* From GLSL 1.50 spec, page 56:
161 * "The arithmetic binary operators add (+), subtract (-),
162 * multiply (*), and divide (/) operate on integer and
163 * floating-point scalars, vectors, and matrices."
165 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
166 _mesa_glsl_error(loc
, state
,
167 "Operands to arithmetic operators must be numeric");
168 return glsl_type::error_type
;
172 /* "If one operand is floating-point based and the other is
173 * not, then the conversions from Section 4.1.10 "Implicit
174 * Conversions" are applied to the non-floating-point-based operand."
176 if (!apply_implicit_conversion(type_a
, value_b
, state
)
177 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
178 _mesa_glsl_error(loc
, state
,
179 "Could not implicitly convert operands to "
180 "arithmetic operator");
181 return glsl_type::error_type
;
183 type_a
= value_a
->type
;
184 type_b
= value_b
->type
;
186 /* "If the operands are integer types, they must both be signed or
189 * From this rule and the preceeding conversion it can be inferred that
190 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
191 * The is_numeric check above already filtered out the case where either
192 * type is not one of these, so now the base types need only be tested for
195 if (type_a
->base_type
!= type_b
->base_type
) {
196 _mesa_glsl_error(loc
, state
,
197 "base type mismatch for arithmetic operator");
198 return glsl_type::error_type
;
201 /* "All arithmetic binary operators result in the same fundamental type
202 * (signed integer, unsigned integer, or floating-point) as the
203 * operands they operate on, after operand type conversion. After
204 * conversion, the following cases are valid
206 * * The two operands are scalars. In this case the operation is
207 * applied, resulting in a scalar."
209 if (type_a
->is_scalar() && type_b
->is_scalar())
212 /* "* One operand is a scalar, and the other is a vector or matrix.
213 * In this case, the scalar operation is applied independently to each
214 * component of the vector or matrix, resulting in the same size
217 if (type_a
->is_scalar()) {
218 if (!type_b
->is_scalar())
220 } else if (type_b
->is_scalar()) {
224 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
225 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
228 assert(!type_a
->is_scalar());
229 assert(!type_b
->is_scalar());
231 /* "* The two operands are vectors of the same size. In this case, the
232 * operation is done component-wise resulting in the same size
235 if (type_a
->is_vector() && type_b
->is_vector()) {
236 if (type_a
== type_b
) {
239 _mesa_glsl_error(loc
, state
,
240 "vector size mismatch for arithmetic operator");
241 return glsl_type::error_type
;
245 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
246 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
247 * <vector, vector> have been handled. At least one of the operands must
248 * be matrix. Further, since there are no integer matrix types, the base
249 * type of both operands must be float.
251 assert(type_a
->is_matrix() || type_b
->is_matrix());
252 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
253 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
255 /* "* The operator is add (+), subtract (-), or divide (/), and the
256 * operands are matrices with the same number of rows and the same
257 * number of columns. In this case, the operation is done component-
258 * wise resulting in the same size matrix."
259 * * The operator is multiply (*), where both operands are matrices or
260 * one operand is a vector and the other a matrix. A right vector
261 * operand is treated as a column vector and a left vector operand as a
262 * row vector. In all these cases, it is required that the number of
263 * columns of the left operand is equal to the number of rows of the
264 * right operand. Then, the multiply (*) operation does a linear
265 * algebraic multiply, yielding an object that has the same number of
266 * rows as the left operand and the same number of columns as the right
267 * operand. Section 5.10 "Vector and Matrix Operations" explains in
268 * more detail how vectors and matrices are operated on."
271 if (type_a
== type_b
)
274 if (type_a
->is_matrix() && type_b
->is_matrix()) {
275 /* Matrix multiply. The columns of A must match the rows of B. Given
276 * the other previously tested constraints, this means the vector type
277 * of a row from A must be the same as the vector type of a column from
280 if (type_a
->row_type() == type_b
->column_type()) {
281 /* The resulting matrix has the number of columns of matrix B and
282 * the number of rows of matrix A. We get the row count of A by
283 * looking at the size of a vector that makes up a column. The
284 * transpose (size of a row) is done for B.
286 const glsl_type
*const type
=
287 glsl_type::get_instance(type_a
->base_type
,
288 type_a
->column_type()->vector_elements
,
289 type_b
->row_type()->vector_elements
);
290 assert(type
!= glsl_type::error_type
);
294 } else if (type_a
->is_matrix()) {
295 /* A is a matrix and B is a column vector. Columns of A must match
296 * rows of B. Given the other previously tested constraints, this
297 * means the vector type of a row from A must be the same as the
298 * vector the type of B.
300 if (type_a
->row_type() == type_b
) {
301 /* The resulting vector has a number of elements equal to
302 * the number of rows of matrix A. */
303 const glsl_type
*const type
=
304 glsl_type::get_instance(type_a
->base_type
,
305 type_a
->column_type()->vector_elements
,
307 assert(type
!= glsl_type::error_type
);
312 assert(type_b
->is_matrix());
314 /* A is a row vector and B is a matrix. Columns of A must match rows
315 * of B. Given the other previously tested constraints, this means
316 * the type of A must be the same as the vector type of a column from
319 if (type_a
== type_b
->column_type()) {
320 /* The resulting vector has a number of elements equal to
321 * the number of columns of matrix B. */
322 const glsl_type
*const type
=
323 glsl_type::get_instance(type_a
->base_type
,
324 type_b
->row_type()->vector_elements
,
326 assert(type
!= glsl_type::error_type
);
332 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
333 return glsl_type::error_type
;
337 /* "All other cases are illegal."
339 _mesa_glsl_error(loc
, state
, "type mismatch");
340 return glsl_type::error_type
;
344 static const struct glsl_type
*
345 unary_arithmetic_result_type(const struct glsl_type
*type
,
346 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
348 /* From GLSL 1.50 spec, page 57:
350 * "The arithmetic unary operators negate (-), post- and pre-increment
351 * and decrement (-- and ++) operate on integer or floating-point
352 * values (including vectors and matrices). All unary operators work
353 * component-wise on their operands. These result with the same type
356 if (!type
->is_numeric()) {
357 _mesa_glsl_error(loc
, state
,
358 "Operands to arithmetic operators must be numeric");
359 return glsl_type::error_type
;
366 static const struct glsl_type
*
367 modulus_result_type(const struct glsl_type
*type_a
,
368 const struct glsl_type
*type_b
,
369 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
371 /* From GLSL 1.50 spec, page 56:
372 * "The operator modulus (%) operates on signed or unsigned integers or
373 * integer vectors. The operand types must both be signed or both be
376 if (!type_a
->is_integer() || !type_b
->is_integer()
377 || (type_a
->base_type
!= type_b
->base_type
)) {
378 _mesa_glsl_error(loc
, state
, "type mismatch");
379 return glsl_type::error_type
;
382 /* "The operands cannot be vectors of differing size. If one operand is
383 * a scalar and the other vector, then the scalar is applied component-
384 * wise to the vector, resulting in the same type as the vector. If both
385 * are vectors of the same size, the result is computed component-wise."
387 if (type_a
->is_vector()) {
388 if (!type_b
->is_vector()
389 || (type_a
->vector_elements
== type_b
->vector_elements
))
394 /* "The operator modulus (%) is not defined for any other data types
395 * (non-integer types)."
397 _mesa_glsl_error(loc
, state
, "type mismatch");
398 return glsl_type::error_type
;
402 static const struct glsl_type
*
403 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
404 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
406 const glsl_type
*type_a
= value_a
->type
;
407 const glsl_type
*type_b
= value_b
->type
;
409 /* From GLSL 1.50 spec, page 56:
410 * "The relational operators greater than (>), less than (<), greater
411 * than or equal (>=), and less than or equal (<=) operate only on
412 * scalar integer and scalar floating-point expressions."
414 if (!type_a
->is_numeric()
415 || !type_b
->is_numeric()
416 || !type_a
->is_scalar()
417 || !type_b
->is_scalar()) {
418 _mesa_glsl_error(loc
, state
,
419 "Operands to relational operators must be scalar and "
421 return glsl_type::error_type
;
424 /* "Either the operands' types must match, or the conversions from
425 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
426 * operand, after which the types must match."
428 if (!apply_implicit_conversion(type_a
, value_b
, state
)
429 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
430 _mesa_glsl_error(loc
, state
,
431 "Could not implicitly convert operands to "
432 "relational operator");
433 return glsl_type::error_type
;
435 type_a
= value_a
->type
;
436 type_b
= value_b
->type
;
438 if (type_a
->base_type
!= type_b
->base_type
) {
439 _mesa_glsl_error(loc
, state
, "base type mismatch");
440 return glsl_type::error_type
;
443 /* "The result is scalar Boolean."
445 return glsl_type::bool_type
;
450 * Validates that a value can be assigned to a location with a specified type
452 * Validates that \c rhs can be assigned to some location. If the types are
453 * not an exact match but an automatic conversion is possible, \c rhs will be
457 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
458 * Otherwise the actual RHS to be assigned will be returned. This may be
459 * \c rhs, or it may be \c rhs after some type conversion.
462 * In addition to being used for assignments, this function is used to
463 * type-check return values.
466 validate_assignment(struct _mesa_glsl_parse_state
*state
,
467 const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
469 const glsl_type
*rhs_type
= rhs
->type
;
471 /* If there is already some error in the RHS, just return it. Anything
472 * else will lead to an avalanche of error message back to the user.
474 if (rhs_type
->is_error())
477 /* If the types are identical, the assignment can trivially proceed.
479 if (rhs_type
== lhs_type
)
482 /* If the array element types are the same and the size of the LHS is zero,
483 * the assignment is okay.
485 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
486 * is handled by ir_dereference::is_lvalue.
488 if (lhs_type
->is_array() && rhs
->type
->is_array()
489 && (lhs_type
->element_type() == rhs
->type
->element_type())
490 && (lhs_type
->array_size() == 0)) {
494 /* Check for implicit conversion in GLSL 1.20 */
495 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
496 rhs_type
= rhs
->type
;
497 if (rhs_type
== lhs_type
)
505 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
506 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
510 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
512 if (!error_emitted
) {
513 if (!lhs
->is_lvalue()) {
514 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
515 error_emitted
= true;
519 ir_rvalue
*new_rhs
= validate_assignment(state
, lhs
->type
, rhs
);
520 if (new_rhs
== NULL
) {
521 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
525 /* If the LHS array was not declared with a size, it takes it size from
526 * the RHS. If the LHS is an l-value and a whole array, it must be a
527 * dereference of a variable. Any other case would require that the LHS
528 * is either not an l-value or not a whole array.
530 if (lhs
->type
->array_size() == 0) {
531 ir_dereference
*const d
= lhs
->as_dereference();
535 ir_variable
*const var
= d
->variable_referenced();
539 if (var
->max_array_access
>= unsigned(rhs
->type
->array_size())) {
540 /* FINISHME: This should actually log the location of the RHS. */
541 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
543 var
->max_array_access
);
546 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
547 rhs
->type
->array_size());
552 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
553 * but not post_inc) need the converted assigned value as an rvalue
554 * to handle things like:
558 * So we always just store the computed value being assigned to a
559 * temporary and return a deref of that temporary. If the rvalue
560 * ends up not being used, the temp will get copy-propagated out.
562 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
564 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
565 instructions
->push_tail(var
);
566 instructions
->push_tail(new(ctx
) ir_assignment(deref_var
,
569 deref_var
= new(ctx
) ir_dereference_variable(var
);
572 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
, NULL
));
574 return new(ctx
) ir_dereference_variable(var
);
578 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
580 void *ctx
= talloc_parent(lvalue
);
583 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
585 instructions
->push_tail(var
);
586 var
->mode
= ir_var_auto
;
588 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
591 /* Once we've created this temporary, mark it read only so it's no
592 * longer considered an lvalue.
594 var
->read_only
= true;
596 return new(ctx
) ir_dereference_variable(var
);
601 ast_node::hir(exec_list
*instructions
,
602 struct _mesa_glsl_parse_state
*state
)
612 ast_expression::hir(exec_list
*instructions
,
613 struct _mesa_glsl_parse_state
*state
)
616 static const int operations
[AST_NUM_OPERATORS
] = {
617 -1, /* ast_assign doesn't convert to ir_expression. */
618 -1, /* ast_plus doesn't convert to ir_expression. */
642 /* Note: The following block of expression types actually convert
643 * to multiple IR instructions.
645 ir_binop_mul
, /* ast_mul_assign */
646 ir_binop_div
, /* ast_div_assign */
647 ir_binop_mod
, /* ast_mod_assign */
648 ir_binop_add
, /* ast_add_assign */
649 ir_binop_sub
, /* ast_sub_assign */
650 ir_binop_lshift
, /* ast_ls_assign */
651 ir_binop_rshift
, /* ast_rs_assign */
652 ir_binop_bit_and
, /* ast_and_assign */
653 ir_binop_bit_xor
, /* ast_xor_assign */
654 ir_binop_bit_or
, /* ast_or_assign */
656 -1, /* ast_conditional doesn't convert to ir_expression. */
657 ir_binop_add
, /* ast_pre_inc. */
658 ir_binop_sub
, /* ast_pre_dec. */
659 ir_binop_add
, /* ast_post_inc. */
660 ir_binop_sub
, /* ast_post_dec. */
661 -1, /* ast_field_selection doesn't conv to ir_expression. */
662 -1, /* ast_array_index doesn't convert to ir_expression. */
663 -1, /* ast_function_call doesn't conv to ir_expression. */
664 -1, /* ast_identifier doesn't convert to ir_expression. */
665 -1, /* ast_int_constant doesn't convert to ir_expression. */
666 -1, /* ast_uint_constant doesn't conv to ir_expression. */
667 -1, /* ast_float_constant doesn't conv to ir_expression. */
668 -1, /* ast_bool_constant doesn't conv to ir_expression. */
669 -1, /* ast_sequence doesn't convert to ir_expression. */
671 ir_rvalue
*result
= NULL
;
673 const struct glsl_type
*type
= glsl_type::error_type
;
674 bool error_emitted
= false;
677 loc
= this->get_location();
679 switch (this->oper
) {
681 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
682 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
684 result
= do_assignment(instructions
, state
, op
[0], op
[1],
685 this->subexpressions
[0]->get_location());
686 error_emitted
= result
->type
->is_error();
692 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
694 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
696 error_emitted
= type
->is_error();
702 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
704 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
706 error_emitted
= type
->is_error();
708 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
716 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
717 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
719 type
= arithmetic_result_type(op
[0], op
[1],
720 (this->oper
== ast_mul
),
722 error_emitted
= type
->is_error();
724 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
729 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
730 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
732 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
734 assert(operations
[this->oper
] == ir_binop_mod
);
736 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
738 error_emitted
= type
->is_error();
743 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-shift operators");
744 error_emitted
= true;
751 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
752 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
754 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
756 /* The relational operators must either generate an error or result
757 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
759 assert(type
->is_error()
760 || ((type
->base_type
== GLSL_TYPE_BOOL
)
761 && type
->is_scalar()));
763 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
765 error_emitted
= type
->is_error();
770 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
771 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
773 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
775 * "The equality operators equal (==), and not equal (!=)
776 * operate on all types. They result in a scalar Boolean. If
777 * the operand types do not match, then there must be a
778 * conversion from Section 4.1.10 "Implicit Conversions"
779 * applied to one operand that can make them match, in which
780 * case this conversion is done."
782 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
783 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
784 || (op
[0]->type
!= op
[1]->type
)) {
785 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
786 "type", (this->oper
== ast_equal
) ? "==" : "!=");
787 error_emitted
= true;
788 } else if ((state
->language_version
<= 110)
789 && (op
[0]->type
->is_array() || op
[1]->type
->is_array())) {
790 _mesa_glsl_error(& loc
, state
, "array comparisons forbidden in "
792 error_emitted
= true;
795 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
797 type
= glsl_type::bool_type
;
799 assert(result
->type
== glsl_type::bool_type
);
805 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
806 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
808 if (state
->language_version
< 130) {
809 _mesa_glsl_error(&loc
, state
, "bit-wise operations require GLSL 1.30");
810 error_emitted
= true;
813 if (!op
[0]->type
->is_integer()) {
814 _mesa_glsl_error(&loc
, state
, "LHS of `%s' must be an integer",
815 operator_string(this->oper
));
816 error_emitted
= true;
819 if (!op
[1]->type
->is_integer()) {
820 _mesa_glsl_error(&loc
, state
, "RHS of `%s' must be an integer",
821 operator_string(this->oper
));
822 error_emitted
= true;
825 if (op
[0]->type
->base_type
!= op
[1]->type
->base_type
) {
826 _mesa_glsl_error(&loc
, state
, "operands of `%s' must have the same "
827 "base type", operator_string(this->oper
));
828 error_emitted
= true;
831 if (op
[0]->type
->is_vector() && op
[1]->type
->is_vector()
832 && op
[0]->type
->vector_elements
!= op
[1]->type
->vector_elements
) {
833 _mesa_glsl_error(&loc
, state
, "operands of `%s' cannot be vectors of "
834 "different sizes", operator_string(this->oper
));
835 error_emitted
= true;
838 type
= op
[0]->type
->is_scalar() ? op
[1]->type
: op
[0]->type
;
839 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
841 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
845 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
847 if (state
->language_version
< 130) {
848 _mesa_glsl_error(&loc
, state
, "bit-wise operations require GLSL 1.30");
849 error_emitted
= true;
852 if (!op
[0]->type
->is_integer()) {
853 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
854 error_emitted
= true;
858 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
861 case ast_logic_and
: {
862 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
864 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
865 YYLTYPE loc
= this->subexpressions
[0]->get_location();
867 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
868 operator_string(this->oper
));
869 error_emitted
= true;
872 ir_constant
*op0_const
= op
[0]->constant_expression_value();
874 if (op0_const
->value
.b
[0]) {
875 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
877 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
878 YYLTYPE loc
= this->subexpressions
[1]->get_location();
880 _mesa_glsl_error(& loc
, state
,
881 "RHS of `%s' must be scalar boolean",
882 operator_string(this->oper
));
883 error_emitted
= true;
889 type
= glsl_type::bool_type
;
891 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
894 instructions
->push_tail(tmp
);
896 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
897 instructions
->push_tail(stmt
);
899 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
901 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
902 YYLTYPE loc
= this->subexpressions
[1]->get_location();
904 _mesa_glsl_error(& loc
, state
,
905 "RHS of `%s' must be scalar boolean",
906 operator_string(this->oper
));
907 error_emitted
= true;
910 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
911 ir_assignment
*const then_assign
=
912 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
913 stmt
->then_instructions
.push_tail(then_assign
);
915 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
916 ir_assignment
*const else_assign
=
917 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false), NULL
);
918 stmt
->else_instructions
.push_tail(else_assign
);
920 result
= new(ctx
) ir_dereference_variable(tmp
);
927 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
929 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
930 YYLTYPE loc
= this->subexpressions
[0]->get_location();
932 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
933 operator_string(this->oper
));
934 error_emitted
= true;
937 ir_constant
*op0_const
= op
[0]->constant_expression_value();
939 if (op0_const
->value
.b
[0]) {
942 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
944 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
945 YYLTYPE loc
= this->subexpressions
[1]->get_location();
947 _mesa_glsl_error(& loc
, state
,
948 "RHS of `%s' must be scalar boolean",
949 operator_string(this->oper
));
950 error_emitted
= true;
954 type
= glsl_type::bool_type
;
956 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
959 instructions
->push_tail(tmp
);
961 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
962 instructions
->push_tail(stmt
);
964 op
[1] = this->subexpressions
[1]->hir(&stmt
->else_instructions
, state
);
966 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
967 YYLTYPE loc
= this->subexpressions
[1]->get_location();
969 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
970 operator_string(this->oper
));
971 error_emitted
= true;
974 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
975 ir_assignment
*const then_assign
=
976 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true), NULL
);
977 stmt
->then_instructions
.push_tail(then_assign
);
979 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
980 ir_assignment
*const else_assign
=
981 new(ctx
) ir_assignment(else_deref
, op
[1], NULL
);
982 stmt
->else_instructions
.push_tail(else_assign
);
984 result
= new(ctx
) ir_dereference_variable(tmp
);
991 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
992 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
995 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
997 type
= glsl_type::bool_type
;
1001 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1003 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
1004 YYLTYPE loc
= this->subexpressions
[0]->get_location();
1006 _mesa_glsl_error(& loc
, state
,
1007 "operand of `!' must be scalar boolean");
1008 error_emitted
= true;
1011 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1013 type
= glsl_type::bool_type
;
1016 case ast_mul_assign
:
1017 case ast_div_assign
:
1018 case ast_add_assign
:
1019 case ast_sub_assign
: {
1020 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1021 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1023 type
= arithmetic_result_type(op
[0], op
[1],
1024 (this->oper
== ast_mul_assign
),
1027 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1030 result
= do_assignment(instructions
, state
,
1031 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1032 this->subexpressions
[0]->get_location());
1033 type
= result
->type
;
1034 error_emitted
= (op
[0]->type
->is_error());
1036 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1037 * explicitly test for this because none of the binary expression
1038 * operators allow array operands either.
1044 case ast_mod_assign
: {
1045 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1046 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1048 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1050 assert(operations
[this->oper
] == ir_binop_mod
);
1052 ir_rvalue
*temp_rhs
;
1053 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1056 result
= do_assignment(instructions
, state
,
1057 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1058 this->subexpressions
[0]->get_location());
1059 type
= result
->type
;
1060 error_emitted
= type
->is_error();
1066 _mesa_glsl_error(& loc
, state
,
1067 "FINISHME: implement bit-shift assignment operators");
1068 error_emitted
= true;
1071 case ast_and_assign
:
1072 case ast_xor_assign
:
1074 _mesa_glsl_error(& loc
, state
,
1075 "FINISHME: implement logic assignment operators");
1076 error_emitted
= true;
1079 case ast_conditional
: {
1080 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1082 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1084 * "The ternary selection operator (?:). It operates on three
1085 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1086 * first expression, which must result in a scalar Boolean."
1088 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
1089 YYLTYPE loc
= this->subexpressions
[0]->get_location();
1091 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
1092 error_emitted
= true;
1095 /* The :? operator is implemented by generating an anonymous temporary
1096 * followed by an if-statement. The last instruction in each branch of
1097 * the if-statement assigns a value to the anonymous temporary. This
1098 * temporary is the r-value of the expression.
1100 exec_list then_instructions
;
1101 exec_list else_instructions
;
1103 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1104 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1106 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1108 * "The second and third expressions can be any type, as
1109 * long their types match, or there is a conversion in
1110 * Section 4.1.10 "Implicit Conversions" that can be applied
1111 * to one of the expressions to make their types match. This
1112 * resulting matching type is the type of the entire
1115 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1116 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1117 || (op
[1]->type
!= op
[2]->type
)) {
1118 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1120 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
1121 "operator must have matching types.");
1122 error_emitted
= true;
1123 type
= glsl_type::error_type
;
1128 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1129 ir_constant
*then_val
= op
[1]->constant_expression_value();
1130 ir_constant
*else_val
= op
[2]->constant_expression_value();
1132 if (then_instructions
.is_empty()
1133 && else_instructions
.is_empty()
1134 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1135 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1137 ir_variable
*const tmp
=
1138 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1139 instructions
->push_tail(tmp
);
1141 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1142 instructions
->push_tail(stmt
);
1144 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1145 ir_dereference
*const then_deref
=
1146 new(ctx
) ir_dereference_variable(tmp
);
1147 ir_assignment
*const then_assign
=
1148 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
1149 stmt
->then_instructions
.push_tail(then_assign
);
1151 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1152 ir_dereference
*const else_deref
=
1153 new(ctx
) ir_dereference_variable(tmp
);
1154 ir_assignment
*const else_assign
=
1155 new(ctx
) ir_assignment(else_deref
, op
[2], NULL
);
1156 stmt
->else_instructions
.push_tail(else_assign
);
1158 result
= new(ctx
) ir_dereference_variable(tmp
);
1165 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1166 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1167 op
[1] = new(ctx
) ir_constant(1.0f
);
1169 op
[1] = new(ctx
) ir_constant(1);
1171 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1173 ir_rvalue
*temp_rhs
;
1174 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1177 result
= do_assignment(instructions
, state
,
1178 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1179 this->subexpressions
[0]->get_location());
1180 type
= result
->type
;
1181 error_emitted
= op
[0]->type
->is_error();
1186 case ast_post_dec
: {
1187 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1188 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1189 op
[1] = new(ctx
) ir_constant(1.0f
);
1191 op
[1] = new(ctx
) ir_constant(1);
1193 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1195 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1197 ir_rvalue
*temp_rhs
;
1198 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1201 /* Get a temporary of a copy of the lvalue before it's modified.
1202 * This may get thrown away later.
1204 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1206 (void)do_assignment(instructions
, state
,
1207 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1208 this->subexpressions
[0]->get_location());
1210 type
= result
->type
;
1211 error_emitted
= op
[0]->type
->is_error();
1215 case ast_field_selection
:
1216 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1217 type
= result
->type
;
1220 case ast_array_index
: {
1221 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1223 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1224 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1226 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1228 ir_rvalue
*const array
= op
[0];
1230 result
= new(ctx
) ir_dereference_array(op
[0], op
[1]);
1232 /* Do not use op[0] after this point. Use array.
1240 if (!array
->type
->is_array()
1241 && !array
->type
->is_matrix()
1242 && !array
->type
->is_vector()) {
1243 _mesa_glsl_error(& index_loc
, state
,
1244 "cannot dereference non-array / non-matrix / "
1246 error_emitted
= true;
1249 if (!op
[1]->type
->is_integer()) {
1250 _mesa_glsl_error(& index_loc
, state
,
1251 "array index must be integer type");
1252 error_emitted
= true;
1253 } else if (!op
[1]->type
->is_scalar()) {
1254 _mesa_glsl_error(& index_loc
, state
,
1255 "array index must be scalar");
1256 error_emitted
= true;
1259 /* If the array index is a constant expression and the array has a
1260 * declared size, ensure that the access is in-bounds. If the array
1261 * index is not a constant expression, ensure that the array has a
1264 ir_constant
*const const_index
= op
[1]->constant_expression_value();
1265 if (const_index
!= NULL
) {
1266 const int idx
= const_index
->value
.i
[0];
1267 const char *type_name
;
1270 if (array
->type
->is_matrix()) {
1271 type_name
= "matrix";
1272 } else if (array
->type
->is_vector()) {
1273 type_name
= "vector";
1275 type_name
= "array";
1278 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1280 * "It is illegal to declare an array with a size, and then
1281 * later (in the same shader) index the same array with an
1282 * integral constant expression greater than or equal to the
1283 * declared size. It is also illegal to index an array with a
1284 * negative constant expression."
1286 if (array
->type
->is_matrix()) {
1287 if (array
->type
->row_type()->vector_elements
<= idx
) {
1288 bound
= array
->type
->row_type()->vector_elements
;
1290 } else if (array
->type
->is_vector()) {
1291 if (array
->type
->vector_elements
<= idx
) {
1292 bound
= array
->type
->vector_elements
;
1295 if ((array
->type
->array_size() > 0)
1296 && (array
->type
->array_size() <= idx
)) {
1297 bound
= array
->type
->array_size();
1302 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
1304 error_emitted
= true;
1305 } else if (idx
< 0) {
1306 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
1308 error_emitted
= true;
1311 if (array
->type
->is_array()) {
1312 /* If the array is a variable dereference, it dereferences the
1313 * whole array, by definition. Use this to get the variable.
1315 * FINISHME: Should some methods for getting / setting / testing
1316 * FINISHME: array access limits be added to ir_dereference?
1318 ir_variable
*const v
= array
->whole_variable_referenced();
1319 if ((v
!= NULL
) && (unsigned(idx
) > v
->max_array_access
))
1320 v
->max_array_access
= idx
;
1322 } else if (array
->type
->array_size() == 0) {
1323 _mesa_glsl_error(&loc
, state
, "unsized array index must be constant");
1325 if (array
->type
->is_array()) {
1326 /* whole_variable_referenced can return NULL if the array is a
1327 * member of a structure. In this case it is safe to not update
1328 * the max_array_access field because it is never used for fields
1331 ir_variable
*v
= array
->whole_variable_referenced();
1333 v
->max_array_access
= array
->type
->array_size();
1338 result
->type
= glsl_type::error_type
;
1340 type
= result
->type
;
1344 case ast_function_call
:
1345 /* Should *NEVER* get here. ast_function_call should always be handled
1346 * by ast_function_expression::hir.
1351 case ast_identifier
: {
1352 /* ast_identifier can appear several places in a full abstract syntax
1353 * tree. This particular use must be at location specified in the grammar
1354 * as 'variable_identifier'.
1357 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1359 result
= new(ctx
) ir_dereference_variable(var
);
1362 type
= result
->type
;
1364 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1365 this->primary_expression
.identifier
);
1367 error_emitted
= true;
1372 case ast_int_constant
:
1373 type
= glsl_type::int_type
;
1374 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1377 case ast_uint_constant
:
1378 type
= glsl_type::uint_type
;
1379 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1382 case ast_float_constant
:
1383 type
= glsl_type::float_type
;
1384 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1387 case ast_bool_constant
:
1388 type
= glsl_type::bool_type
;
1389 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1392 case ast_sequence
: {
1393 /* It should not be possible to generate a sequence in the AST without
1394 * any expressions in it.
1396 assert(!this->expressions
.is_empty());
1398 /* The r-value of a sequence is the last expression in the sequence. If
1399 * the other expressions in the sequence do not have side-effects (and
1400 * therefore add instructions to the instruction list), they get dropped
1403 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
)
1404 result
= ast
->hir(instructions
, state
);
1406 type
= result
->type
;
1408 /* Any errors should have already been emitted in the loop above.
1410 error_emitted
= true;
1415 if (type
->is_error() && !error_emitted
)
1416 _mesa_glsl_error(& loc
, state
, "type mismatch");
1423 ast_expression_statement::hir(exec_list
*instructions
,
1424 struct _mesa_glsl_parse_state
*state
)
1426 /* It is possible to have expression statements that don't have an
1427 * expression. This is the solitary semicolon:
1429 * for (i = 0; i < 5; i++)
1432 * In this case the expression will be NULL. Test for NULL and don't do
1433 * anything in that case.
1435 if (expression
!= NULL
)
1436 expression
->hir(instructions
, state
);
1438 /* Statements do not have r-values.
1445 ast_compound_statement::hir(exec_list
*instructions
,
1446 struct _mesa_glsl_parse_state
*state
)
1449 state
->symbols
->push_scope();
1451 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1452 ast
->hir(instructions
, state
);
1455 state
->symbols
->pop_scope();
1457 /* Compound statements do not have r-values.
1463 static const glsl_type
*
1464 process_array_type(const glsl_type
*base
, ast_node
*array_size
,
1465 struct _mesa_glsl_parse_state
*state
)
1467 unsigned length
= 0;
1469 /* FINISHME: Reject delcarations of multidimensional arrays. */
1471 if (array_size
!= NULL
) {
1472 exec_list dummy_instructions
;
1473 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1474 YYLTYPE loc
= array_size
->get_location();
1476 /* FINISHME: Verify that the grammar forbids side-effects in array
1477 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1479 assert(dummy_instructions
.is_empty());
1482 if (!ir
->type
->is_integer()) {
1483 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1484 } else if (!ir
->type
->is_scalar()) {
1485 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1487 ir_constant
*const size
= ir
->constant_expression_value();
1490 _mesa_glsl_error(& loc
, state
, "array size must be a "
1491 "constant valued expression");
1492 } else if (size
->value
.i
[0] <= 0) {
1493 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1495 assert(size
->type
== ir
->type
);
1496 length
= size
->value
.u
[0];
1502 return glsl_type::get_array_instance(base
, length
);
1507 ast_type_specifier::glsl_type(const char **name
,
1508 struct _mesa_glsl_parse_state
*state
) const
1510 const struct glsl_type
*type
;
1512 if ((this->type_specifier
== ast_struct
) && (this->type_name
== NULL
)) {
1513 /* FINISHME: Handle annonymous structures. */
1516 type
= state
->symbols
->get_type(this->type_name
);
1517 *name
= this->type_name
;
1519 if (this->is_array
) {
1520 type
= process_array_type(type
, this->array_size
, state
);
1529 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1531 struct _mesa_glsl_parse_state
*state
,
1534 if (qual
->invariant
)
1537 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1538 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
1539 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1545 if (qual
->attribute
&& state
->target
!= vertex_shader
) {
1546 var
->type
= glsl_type::error_type
;
1547 _mesa_glsl_error(loc
, state
,
1548 "`attribute' variables may not be declared in the "
1550 _mesa_glsl_shader_target_name(state
->target
));
1553 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1555 * "The varying qualifier can be used only with the data types
1556 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1559 if (qual
->varying
) {
1560 const glsl_type
*non_array_type
;
1562 if (var
->type
&& var
->type
->is_array())
1563 non_array_type
= var
->type
->fields
.array
;
1565 non_array_type
= var
->type
;
1567 if (non_array_type
&& non_array_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1568 var
->type
= glsl_type::error_type
;
1569 _mesa_glsl_error(loc
, state
,
1570 "varying variables must be of base type float");
1574 /* If there is no qualifier that changes the mode of the variable, leave
1575 * the setting alone.
1577 if (qual
->in
&& qual
->out
)
1578 var
->mode
= ir_var_inout
;
1579 else if (qual
->attribute
|| qual
->in
1580 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1581 var
->mode
= ir_var_in
;
1582 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
1583 var
->mode
= ir_var_out
;
1584 else if (qual
->uniform
)
1585 var
->mode
= ir_var_uniform
;
1588 var
->interpolation
= ir_var_flat
;
1589 else if (qual
->noperspective
)
1590 var
->interpolation
= ir_var_noperspective
;
1592 var
->interpolation
= ir_var_smooth
;
1594 var
->pixel_center_integer
= qual
->pixel_center_integer
;
1595 var
->origin_upper_left
= qual
->origin_upper_left
;
1596 if ((qual
->origin_upper_left
|| qual
->pixel_center_integer
)
1597 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
1598 const char *const qual_string
= (qual
->origin_upper_left
)
1599 ? "origin_upper_left" : "pixel_center_integer";
1601 _mesa_glsl_error(loc
, state
,
1602 "layout qualifier `%s' can only be applied to "
1603 "fragment shader input `gl_FragCoord'",
1607 if (var
->type
->is_array() && (state
->language_version
>= 120)) {
1608 var
->array_lvalue
= true;
1614 ast_declarator_list::hir(exec_list
*instructions
,
1615 struct _mesa_glsl_parse_state
*state
)
1618 const struct glsl_type
*decl_type
;
1619 const char *type_name
= NULL
;
1620 ir_rvalue
*result
= NULL
;
1621 YYLTYPE loc
= this->get_location();
1623 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
1625 * "To ensure that a particular output variable is invariant, it is
1626 * necessary to use the invariant qualifier. It can either be used to
1627 * qualify a previously declared variable as being invariant
1629 * invariant gl_Position; // make existing gl_Position be invariant"
1631 * In these cases the parser will set the 'invariant' flag in the declarator
1632 * list, and the type will be NULL.
1634 if (this->invariant
) {
1635 assert(this->type
== NULL
);
1637 if (state
->current_function
!= NULL
) {
1638 _mesa_glsl_error(& loc
, state
,
1639 "All uses of `invariant' keyword must be at global "
1643 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1644 assert(!decl
->is_array
);
1645 assert(decl
->array_size
== NULL
);
1646 assert(decl
->initializer
== NULL
);
1648 ir_variable
*const earlier
=
1649 state
->symbols
->get_variable(decl
->identifier
);
1650 if (earlier
== NULL
) {
1651 _mesa_glsl_error(& loc
, state
,
1652 "Undeclared variable `%s' cannot be marked "
1653 "invariant\n", decl
->identifier
);
1654 } else if ((state
->target
== vertex_shader
)
1655 && (earlier
->mode
!= ir_var_out
)) {
1656 _mesa_glsl_error(& loc
, state
,
1657 "`%s' cannot be marked invariant, vertex shader "
1658 "outputs only\n", decl
->identifier
);
1659 } else if ((state
->target
== fragment_shader
)
1660 && (earlier
->mode
!= ir_var_in
)) {
1661 _mesa_glsl_error(& loc
, state
,
1662 "`%s' cannot be marked invariant, fragment shader "
1663 "inputs only\n", decl
->identifier
);
1665 earlier
->invariant
= true;
1669 /* Invariant redeclarations do not have r-values.
1674 assert(this->type
!= NULL
);
1675 assert(!this->invariant
);
1677 /* The type specifier may contain a structure definition. Process that
1678 * before any of the variable declarations.
1680 (void) this->type
->specifier
->hir(instructions
, state
);
1682 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1683 if (this->declarations
.is_empty()) {
1684 /* The only valid case where the declaration list can be empty is when
1685 * the declaration is setting the default precision of a built-in type
1686 * (e.g., 'precision highp vec4;').
1689 if (decl_type
!= NULL
) {
1691 _mesa_glsl_error(& loc
, state
, "incomplete declaration");
1695 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1696 const struct glsl_type
*var_type
;
1699 /* FINISHME: Emit a warning if a variable declaration shadows a
1700 * FINISHME: declaration at a higher scope.
1703 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1704 if (type_name
!= NULL
) {
1705 _mesa_glsl_error(& loc
, state
,
1706 "invalid type `%s' in declaration of `%s'",
1707 type_name
, decl
->identifier
);
1709 _mesa_glsl_error(& loc
, state
,
1710 "invalid type in declaration of `%s'",
1716 if (decl
->is_array
) {
1717 var_type
= process_array_type(decl_type
, decl
->array_size
, state
);
1719 var_type
= decl_type
;
1722 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
1724 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1726 * "Global variables can only use the qualifiers const,
1727 * attribute, uni form, or varying. Only one may be
1730 * Local variables can only use the qualifier const."
1732 * This is relaxed in GLSL 1.30.
1734 if (state
->language_version
< 120) {
1735 if (this->type
->qualifier
.out
) {
1736 _mesa_glsl_error(& loc
, state
,
1737 "`out' qualifier in declaration of `%s' "
1738 "only valid for function parameters in GLSL 1.10.",
1741 if (this->type
->qualifier
.in
) {
1742 _mesa_glsl_error(& loc
, state
,
1743 "`in' qualifier in declaration of `%s' "
1744 "only valid for function parameters in GLSL 1.10.",
1747 /* FINISHME: Test for other invalid qualifiers. */
1750 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1753 if (this->type
->qualifier
.invariant
) {
1754 if ((state
->target
== vertex_shader
) && !(var
->mode
== ir_var_out
||
1755 var
->mode
== ir_var_inout
)) {
1756 /* FINISHME: Note that this doesn't work for invariant on
1757 * a function signature outval
1759 _mesa_glsl_error(& loc
, state
,
1760 "`%s' cannot be marked invariant, vertex shader "
1761 "outputs only\n", var
->name
);
1762 } else if ((state
->target
== fragment_shader
) &&
1763 !(var
->mode
== ir_var_in
|| var
->mode
== ir_var_inout
)) {
1764 /* FINISHME: Note that this doesn't work for invariant on
1765 * a function signature inval
1767 _mesa_glsl_error(& loc
, state
,
1768 "`%s' cannot be marked invariant, fragment shader "
1769 "inputs only\n", var
->name
);
1773 if (state
->current_function
!= NULL
) {
1774 const char *mode
= NULL
;
1775 const char *extra
= "";
1777 /* There is no need to check for 'inout' here because the parser will
1778 * only allow that in function parameter lists.
1780 if (this->type
->qualifier
.attribute
) {
1782 } else if (this->type
->qualifier
.uniform
) {
1784 } else if (this->type
->qualifier
.varying
) {
1786 } else if (this->type
->qualifier
.in
) {
1788 extra
= " or in function parameter list";
1789 } else if (this->type
->qualifier
.out
) {
1791 extra
= " or in function parameter list";
1795 _mesa_glsl_error(& loc
, state
,
1796 "%s variable `%s' must be declared at "
1798 mode
, var
->name
, extra
);
1800 } else if (var
->mode
== ir_var_in
) {
1801 if (state
->target
== vertex_shader
) {
1802 bool error_emitted
= false;
1804 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1806 * "Vertex shader inputs can only be float, floating-point
1807 * vectors, matrices, signed and unsigned integers and integer
1808 * vectors. Vertex shader inputs can also form arrays of these
1809 * types, but not structures."
1811 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1813 * "Vertex shader inputs can only be float, floating-point
1814 * vectors, matrices, signed and unsigned integers and integer
1815 * vectors. They cannot be arrays or structures."
1817 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1819 * "The attribute qualifier can be used only with float,
1820 * floating-point vectors, and matrices. Attribute variables
1821 * cannot be declared as arrays or structures."
1823 const glsl_type
*check_type
= var
->type
->is_array()
1824 ? var
->type
->fields
.array
: var
->type
;
1826 switch (check_type
->base_type
) {
1827 case GLSL_TYPE_FLOAT
:
1829 case GLSL_TYPE_UINT
:
1831 if (state
->language_version
> 120)
1835 _mesa_glsl_error(& loc
, state
,
1836 "vertex shader input / attribute cannot have "
1838 var
->type
->is_array() ? "array of " : "",
1840 error_emitted
= true;
1843 if (!error_emitted
&& (state
->language_version
<= 130)
1844 && var
->type
->is_array()) {
1845 _mesa_glsl_error(& loc
, state
,
1846 "vertex shader input / attribute cannot have "
1848 error_emitted
= true;
1853 /* Process the initializer and add its instructions to a temporary
1854 * list. This list will be added to the instruction stream (below) after
1855 * the declaration is added. This is done because in some cases (such as
1856 * redeclarations) the declaration may not actually be added to the
1857 * instruction stream.
1859 exec_list initializer_instructions
;
1860 if (decl
->initializer
!= NULL
) {
1861 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
1863 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1865 * "All uniform variables are read-only and are initialized either
1866 * directly by an application via API commands, or indirectly by
1869 if ((state
->language_version
<= 110)
1870 && (var
->mode
== ir_var_uniform
)) {
1871 _mesa_glsl_error(& initializer_loc
, state
,
1872 "cannot initialize uniforms in GLSL 1.10");
1875 if (var
->type
->is_sampler()) {
1876 _mesa_glsl_error(& initializer_loc
, state
,
1877 "cannot initialize samplers");
1880 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1881 _mesa_glsl_error(& initializer_loc
, state
,
1882 "cannot initialize %s shader input / %s",
1883 _mesa_glsl_shader_target_name(state
->target
),
1884 (state
->target
== vertex_shader
)
1885 ? "attribute" : "varying");
1888 ir_dereference
*const lhs
= new(ctx
) ir_dereference_variable(var
);
1889 ir_rvalue
*rhs
= decl
->initializer
->hir(&initializer_instructions
,
1892 /* Calculate the constant value if this is a const or uniform
1895 if (this->type
->qualifier
.constant
|| this->type
->qualifier
.uniform
) {
1896 ir_rvalue
*new_rhs
= validate_assignment(state
, var
->type
, rhs
);
1897 if (new_rhs
!= NULL
) {
1900 ir_constant
*constant_value
= rhs
->constant_expression_value();
1901 if (!constant_value
) {
1902 _mesa_glsl_error(& initializer_loc
, state
,
1903 "initializer of %s variable `%s' must be a "
1904 "constant expression",
1905 (this->type
->qualifier
.constant
)
1906 ? "const" : "uniform",
1908 if (var
->type
->is_numeric()) {
1909 /* Reduce cascading errors. */
1910 var
->constant_value
= ir_constant::zero(ctx
, var
->type
);
1913 rhs
= constant_value
;
1914 var
->constant_value
= constant_value
;
1917 _mesa_glsl_error(&initializer_loc
, state
,
1918 "initializer of type %s cannot be assigned to "
1919 "variable of type %s",
1920 rhs
->type
->name
, var
->type
->name
);
1921 if (var
->type
->is_numeric()) {
1922 /* Reduce cascading errors. */
1923 var
->constant_value
= ir_constant::zero(ctx
, var
->type
);
1928 if (rhs
&& !rhs
->type
->is_error()) {
1929 bool temp
= var
->read_only
;
1930 if (this->type
->qualifier
.constant
)
1931 var
->read_only
= false;
1933 /* Never emit code to initialize a uniform.
1935 if (!this->type
->qualifier
.uniform
)
1936 result
= do_assignment(&initializer_instructions
, state
,
1938 this->get_location());
1939 var
->read_only
= temp
;
1943 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1945 * "It is an error to write to a const variable outside of
1946 * its declaration, so they must be initialized when
1949 if (this->type
->qualifier
.constant
&& decl
->initializer
== NULL
) {
1950 _mesa_glsl_error(& loc
, state
,
1951 "const declaration of `%s' must be initialized");
1954 /* Check if this declaration is actually a re-declaration, either to
1955 * resize an array or add qualifiers to an existing variable.
1957 * This is allowed for variables in the current scope, or when at
1958 * global scope (for built-ins in the implicit outer scope).
1960 ir_variable
*earlier
= state
->symbols
->get_variable(decl
->identifier
);
1961 if (earlier
!= NULL
&& (state
->current_function
== NULL
||
1962 state
->symbols
->name_declared_this_scope(decl
->identifier
))) {
1964 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1966 * "It is legal to declare an array without a size and then
1967 * later re-declare the same name as an array of the same
1968 * type and specify a size."
1970 if ((earlier
->type
->array_size() == 0)
1971 && var
->type
->is_array()
1972 && (var
->type
->element_type() == earlier
->type
->element_type())) {
1973 /* FINISHME: This doesn't match the qualifiers on the two
1974 * FINISHME: declarations. It's not 100% clear whether this is
1975 * FINISHME: required or not.
1978 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1980 * "The size [of gl_TexCoord] can be at most
1981 * gl_MaxTextureCoords."
1983 const unsigned size
= unsigned(var
->type
->array_size());
1984 if ((strcmp("gl_TexCoord", var
->name
) == 0)
1985 && (size
> state
->Const
.MaxTextureCoords
)) {
1986 YYLTYPE loc
= this->get_location();
1988 _mesa_glsl_error(& loc
, state
, "`gl_TexCoord' array size cannot "
1989 "be larger than gl_MaxTextureCoords (%u)\n",
1990 state
->Const
.MaxTextureCoords
);
1991 } else if ((size
> 0) && (size
<= earlier
->max_array_access
)) {
1992 YYLTYPE loc
= this->get_location();
1994 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
1996 earlier
->max_array_access
);
1999 earlier
->type
= var
->type
;
2002 } else if (state
->extensions
->ARB_fragment_coord_conventions
2003 && strcmp(var
->name
, "gl_FragCoord") == 0
2004 && earlier
->type
== var
->type
2005 && earlier
->mode
== var
->mode
) {
2006 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2009 earlier
->origin_upper_left
= var
->origin_upper_left
;
2010 earlier
->pixel_center_integer
= var
->pixel_center_integer
;
2012 YYLTYPE loc
= this->get_location();
2013 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", decl
->identifier
);
2019 /* By now, we know it's a new variable declaration (we didn't hit the
2020 * above "continue").
2022 * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2024 * "Identifiers starting with "gl_" are reserved for use by
2025 * OpenGL, and may not be declared in a shader as either a
2026 * variable or a function."
2028 if (strncmp(decl
->identifier
, "gl_", 3) == 0)
2029 _mesa_glsl_error(& loc
, state
,
2030 "identifier `%s' uses reserved `gl_' prefix",
2033 /* Add the variable to the symbol table. Note that the initializer's
2034 * IR was already processed earlier (though it hasn't been emitted yet),
2035 * without the variable in scope.
2037 * This differs from most C-like languages, but it follows the GLSL
2038 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
2041 * "Within a declaration, the scope of a name starts immediately
2042 * after the initializer if present or immediately after the name
2043 * being declared if not."
2045 if (!state
->symbols
->add_variable(var
->name
, var
)) {
2046 YYLTYPE loc
= this->get_location();
2047 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
2048 "current scope", decl
->identifier
);
2052 /* Push the variable declaration to the top. It means that all
2053 * the variable declarations will appear in a funny
2054 * last-to-first order, but otherwise we run into trouble if a
2055 * function is prototyped, a global var is decled, then the
2056 * function is defined with usage of the global var. See
2057 * glslparsertest's CorrectModule.frag.
2059 instructions
->push_head(var
);
2060 instructions
->append_list(&initializer_instructions
);
2064 /* Generally, variable declarations do not have r-values. However,
2065 * one is used for the declaration in
2067 * while (bool b = some_condition()) {
2071 * so we return the rvalue from the last seen declaration here.
2078 ast_parameter_declarator::hir(exec_list
*instructions
,
2079 struct _mesa_glsl_parse_state
*state
)
2082 const struct glsl_type
*type
;
2083 const char *name
= NULL
;
2084 YYLTYPE loc
= this->get_location();
2086 type
= this->type
->specifier
->glsl_type(& name
, state
);
2090 _mesa_glsl_error(& loc
, state
,
2091 "invalid type `%s' in declaration of `%s'",
2092 name
, this->identifier
);
2094 _mesa_glsl_error(& loc
, state
,
2095 "invalid type in declaration of `%s'",
2099 type
= glsl_type::error_type
;
2102 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2104 * "Functions that accept no input arguments need not use void in the
2105 * argument list because prototypes (or definitions) are required and
2106 * therefore there is no ambiguity when an empty argument list "( )" is
2107 * declared. The idiom "(void)" as a parameter list is provided for
2110 * Placing this check here prevents a void parameter being set up
2111 * for a function, which avoids tripping up checks for main taking
2112 * parameters and lookups of an unnamed symbol.
2114 if (type
->is_void()) {
2115 if (this->identifier
!= NULL
)
2116 _mesa_glsl_error(& loc
, state
,
2117 "named parameter cannot have type `void'");
2123 if (formal_parameter
&& (this->identifier
== NULL
)) {
2124 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
2128 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
2129 * call already handled the "vec4[..] foo" case.
2131 if (this->is_array
) {
2132 type
= process_array_type(type
, this->array_size
, state
);
2135 if (type
->array_size() == 0) {
2136 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
2137 "a declared size.");
2138 type
= glsl_type::error_type
;
2142 ir_variable
*var
= new(ctx
) ir_variable(type
, this->identifier
, ir_var_in
);
2144 /* Apply any specified qualifiers to the parameter declaration. Note that
2145 * for function parameters the default mode is 'in'.
2147 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
2149 instructions
->push_tail(var
);
2151 /* Parameter declarations do not have r-values.
2158 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
2160 exec_list
*ir_parameters
,
2161 _mesa_glsl_parse_state
*state
)
2163 ast_parameter_declarator
*void_param
= NULL
;
2166 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
2167 param
->formal_parameter
= formal
;
2168 param
->hir(ir_parameters
, state
);
2176 if ((void_param
!= NULL
) && (count
> 1)) {
2177 YYLTYPE loc
= void_param
->get_location();
2179 _mesa_glsl_error(& loc
, state
,
2180 "`void' parameter must be only parameter");
2186 ast_function::hir(exec_list
*instructions
,
2187 struct _mesa_glsl_parse_state
*state
)
2190 ir_function
*f
= NULL
;
2191 ir_function_signature
*sig
= NULL
;
2192 exec_list hir_parameters
;
2194 const char *const name
= identifier
;
2196 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
2198 * "Function declarations (prototypes) cannot occur inside of functions;
2199 * they must be at global scope, or for the built-in functions, outside
2200 * the global scope."
2202 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
2204 * "User defined functions may only be defined within the global scope."
2206 * Note that this language does not appear in GLSL 1.10.
2208 if ((state
->current_function
!= NULL
) && (state
->language_version
!= 110)) {
2209 YYLTYPE loc
= this->get_location();
2210 _mesa_glsl_error(&loc
, state
,
2211 "declaration of function `%s' not allowed within "
2212 "function body", name
);
2215 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2217 * "Identifiers starting with "gl_" are reserved for use by
2218 * OpenGL, and may not be declared in a shader as either a
2219 * variable or a function."
2221 if (strncmp(name
, "gl_", 3) == 0) {
2222 YYLTYPE loc
= this->get_location();
2223 _mesa_glsl_error(&loc
, state
,
2224 "identifier `%s' uses reserved `gl_' prefix", name
);
2227 /* Convert the list of function parameters to HIR now so that they can be
2228 * used below to compare this function's signature with previously seen
2229 * signatures for functions with the same name.
2231 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
2233 & hir_parameters
, state
);
2235 const char *return_type_name
;
2236 const glsl_type
*return_type
=
2237 this->return_type
->specifier
->glsl_type(& return_type_name
, state
);
2240 YYLTYPE loc
= this->get_location();
2241 _mesa_glsl_error(&loc
, state
,
2242 "function `%s' has undeclared return type `%s'",
2243 name
, return_type_name
);
2244 return_type
= glsl_type::error_type
;
2247 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2248 * "No qualifier is allowed on the return type of a function."
2250 if (this->return_type
->has_qualifiers()) {
2251 YYLTYPE loc
= this->get_location();
2252 _mesa_glsl_error(& loc
, state
,
2253 "function `%s' return type has qualifiers", name
);
2256 /* Verify that this function's signature either doesn't match a previously
2257 * seen signature for a function with the same name, or, if a match is found,
2258 * that the previously seen signature does not have an associated definition.
2260 f
= state
->symbols
->get_function(name
);
2261 if (f
!= NULL
&& !f
->is_builtin
) {
2262 sig
= f
->exact_matching_signature(&hir_parameters
);
2264 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
2265 if (badvar
!= NULL
) {
2266 YYLTYPE loc
= this->get_location();
2268 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
2269 "qualifiers don't match prototype", name
, badvar
);
2272 if (sig
->return_type
!= return_type
) {
2273 YYLTYPE loc
= this->get_location();
2275 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
2276 "match prototype", name
);
2279 if (is_definition
&& sig
->is_defined
) {
2280 YYLTYPE loc
= this->get_location();
2282 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
2286 f
= new(ctx
) ir_function(name
);
2287 if (!state
->symbols
->add_function(f
->name
, f
)) {
2288 /* This function name shadows a non-function use of the same name. */
2289 YYLTYPE loc
= this->get_location();
2291 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
2292 "non-function", name
);
2296 /* Emit the new function header */
2297 if (state
->current_function
== NULL
)
2298 instructions
->push_tail(f
);
2300 /* IR invariants disallow function declarations or definitions nested
2301 * within other function definitions. Insert the new ir_function
2302 * block in the instruction sequence before the ir_function block
2303 * containing the current ir_function_signature.
2305 * This can only happen in a GLSL 1.10 shader. In all other GLSL
2306 * versions this nesting is disallowed. There is a check for this at
2307 * the top of this function.
2309 ir_function
*const curr
=
2310 const_cast<ir_function
*>(state
->current_function
->function());
2312 curr
->insert_before(f
);
2316 /* Verify the return type of main() */
2317 if (strcmp(name
, "main") == 0) {
2318 if (! return_type
->is_void()) {
2319 YYLTYPE loc
= this->get_location();
2321 _mesa_glsl_error(& loc
, state
, "main() must return void");
2324 if (!hir_parameters
.is_empty()) {
2325 YYLTYPE loc
= this->get_location();
2327 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
2331 /* Finish storing the information about this new function in its signature.
2334 sig
= new(ctx
) ir_function_signature(return_type
);
2335 f
->add_signature(sig
);
2338 sig
->replace_parameters(&hir_parameters
);
2341 /* Function declarations (prototypes) do not have r-values.
2348 ast_function_definition::hir(exec_list
*instructions
,
2349 struct _mesa_glsl_parse_state
*state
)
2351 prototype
->is_definition
= true;
2352 prototype
->hir(instructions
, state
);
2354 ir_function_signature
*signature
= prototype
->signature
;
2355 if (signature
== NULL
)
2358 assert(state
->current_function
== NULL
);
2359 state
->current_function
= signature
;
2360 state
->found_return
= false;
2362 /* Duplicate parameters declared in the prototype as concrete variables.
2363 * Add these to the symbol table.
2365 state
->symbols
->push_scope();
2366 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
2367 ir_variable
*const var
= ((ir_instruction
*) iter
.get())->as_variable();
2369 assert(var
!= NULL
);
2371 /* The only way a parameter would "exist" is if two parameters have
2374 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
2375 YYLTYPE loc
= this->get_location();
2377 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
2379 state
->symbols
->add_variable(var
->name
, var
);
2383 /* Convert the body of the function to HIR. */
2384 this->body
->hir(&signature
->body
, state
);
2385 signature
->is_defined
= true;
2387 state
->symbols
->pop_scope();
2389 assert(state
->current_function
== signature
);
2390 state
->current_function
= NULL
;
2392 if (!signature
->return_type
->is_void() && !state
->found_return
) {
2393 YYLTYPE loc
= this->get_location();
2394 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
2395 "%s, but no return statement",
2396 signature
->function_name(),
2397 signature
->return_type
->name
);
2400 /* Function definitions do not have r-values.
2407 ast_jump_statement::hir(exec_list
*instructions
,
2408 struct _mesa_glsl_parse_state
*state
)
2415 assert(state
->current_function
);
2417 if (opt_return_value
) {
2418 if (state
->current_function
->return_type
->base_type
==
2420 YYLTYPE loc
= this->get_location();
2422 _mesa_glsl_error(& loc
, state
,
2423 "`return` with a value, in function `%s' "
2425 state
->current_function
->function_name());
2428 ir_expression
*const ret
= (ir_expression
*)
2429 opt_return_value
->hir(instructions
, state
);
2430 assert(ret
!= NULL
);
2432 /* Implicit conversions are not allowed for return values. */
2433 if (state
->current_function
->return_type
!= ret
->type
) {
2434 YYLTYPE loc
= this->get_location();
2436 _mesa_glsl_error(& loc
, state
,
2437 "`return' with wrong type %s, in function `%s' "
2440 state
->current_function
->function_name(),
2441 state
->current_function
->return_type
->name
);
2444 inst
= new(ctx
) ir_return(ret
);
2446 if (state
->current_function
->return_type
->base_type
!=
2448 YYLTYPE loc
= this->get_location();
2450 _mesa_glsl_error(& loc
, state
,
2451 "`return' with no value, in function %s returning "
2453 state
->current_function
->function_name());
2455 inst
= new(ctx
) ir_return
;
2458 state
->found_return
= true;
2459 instructions
->push_tail(inst
);
2464 if (state
->target
!= fragment_shader
) {
2465 YYLTYPE loc
= this->get_location();
2467 _mesa_glsl_error(& loc
, state
,
2468 "`discard' may only appear in a fragment shader");
2470 instructions
->push_tail(new(ctx
) ir_discard
);
2475 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2476 * FINISHME: and they use a different IR instruction for 'break'.
2478 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2479 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2482 if (state
->loop_or_switch_nesting
== NULL
) {
2483 YYLTYPE loc
= this->get_location();
2485 _mesa_glsl_error(& loc
, state
,
2486 "`%s' may only appear in a loop",
2487 (mode
== ast_break
) ? "break" : "continue");
2489 ir_loop
*const loop
= state
->loop_or_switch_nesting
->as_loop();
2491 /* Inline the for loop expression again, since we don't know
2492 * where near the end of the loop body the normal copy of it
2493 * is going to be placed.
2495 if (mode
== ast_continue
&&
2496 state
->loop_or_switch_nesting_ast
->rest_expression
) {
2497 state
->loop_or_switch_nesting_ast
->rest_expression
->hir(instructions
,
2502 ir_loop_jump
*const jump
=
2503 new(ctx
) ir_loop_jump((mode
== ast_break
)
2504 ? ir_loop_jump::jump_break
2505 : ir_loop_jump::jump_continue
);
2506 instructions
->push_tail(jump
);
2513 /* Jump instructions do not have r-values.
2520 ast_selection_statement::hir(exec_list
*instructions
,
2521 struct _mesa_glsl_parse_state
*state
)
2525 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
2527 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2529 * "Any expression whose type evaluates to a Boolean can be used as the
2530 * conditional expression bool-expression. Vector types are not accepted
2531 * as the expression to if."
2533 * The checks are separated so that higher quality diagnostics can be
2534 * generated for cases where both rules are violated.
2536 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
2537 YYLTYPE loc
= this->condition
->get_location();
2539 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
2543 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
2545 if (then_statement
!= NULL
) {
2546 state
->symbols
->push_scope();
2547 then_statement
->hir(& stmt
->then_instructions
, state
);
2548 state
->symbols
->pop_scope();
2551 if (else_statement
!= NULL
) {
2552 state
->symbols
->push_scope();
2553 else_statement
->hir(& stmt
->else_instructions
, state
);
2554 state
->symbols
->pop_scope();
2557 instructions
->push_tail(stmt
);
2559 /* if-statements do not have r-values.
2566 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
2567 struct _mesa_glsl_parse_state
*state
)
2571 if (condition
!= NULL
) {
2572 ir_rvalue
*const cond
=
2573 condition
->hir(& stmt
->body_instructions
, state
);
2576 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
2577 YYLTYPE loc
= condition
->get_location();
2579 _mesa_glsl_error(& loc
, state
,
2580 "loop condition must be scalar boolean");
2582 /* As the first code in the loop body, generate a block that looks
2583 * like 'if (!condition) break;' as the loop termination condition.
2585 ir_rvalue
*const not_cond
=
2586 new(ctx
) ir_expression(ir_unop_logic_not
, glsl_type::bool_type
, cond
,
2589 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
2591 ir_jump
*const break_stmt
=
2592 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
2594 if_stmt
->then_instructions
.push_tail(break_stmt
);
2595 stmt
->body_instructions
.push_tail(if_stmt
);
2602 ast_iteration_statement::hir(exec_list
*instructions
,
2603 struct _mesa_glsl_parse_state
*state
)
2607 /* For-loops and while-loops start a new scope, but do-while loops do not.
2609 if (mode
!= ast_do_while
)
2610 state
->symbols
->push_scope();
2612 if (init_statement
!= NULL
)
2613 init_statement
->hir(instructions
, state
);
2615 ir_loop
*const stmt
= new(ctx
) ir_loop();
2616 instructions
->push_tail(stmt
);
2618 /* Track the current loop and / or switch-statement nesting.
2620 ir_instruction
*const nesting
= state
->loop_or_switch_nesting
;
2621 ast_iteration_statement
*nesting_ast
= state
->loop_or_switch_nesting_ast
;
2623 state
->loop_or_switch_nesting
= stmt
;
2624 state
->loop_or_switch_nesting_ast
= this;
2626 if (mode
!= ast_do_while
)
2627 condition_to_hir(stmt
, state
);
2630 body
->hir(& stmt
->body_instructions
, state
);
2632 if (rest_expression
!= NULL
)
2633 rest_expression
->hir(& stmt
->body_instructions
, state
);
2635 if (mode
== ast_do_while
)
2636 condition_to_hir(stmt
, state
);
2638 if (mode
!= ast_do_while
)
2639 state
->symbols
->pop_scope();
2641 /* Restore previous nesting before returning.
2643 state
->loop_or_switch_nesting
= nesting
;
2644 state
->loop_or_switch_nesting_ast
= nesting_ast
;
2646 /* Loops do not have r-values.
2653 ast_type_specifier::hir(exec_list
*instructions
,
2654 struct _mesa_glsl_parse_state
*state
)
2656 if (this->structure
!= NULL
)
2657 return this->structure
->hir(instructions
, state
);
2664 ast_struct_specifier::hir(exec_list
*instructions
,
2665 struct _mesa_glsl_parse_state
*state
)
2667 unsigned decl_count
= 0;
2669 /* Make an initial pass over the list of structure fields to determine how
2670 * many there are. Each element in this list is an ast_declarator_list.
2671 * This means that we actually need to count the number of elements in the
2672 * 'declarations' list in each of the elements.
2674 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2675 &this->declarations
) {
2676 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
2682 /* Allocate storage for the structure fields and process the field
2683 * declarations. As the declarations are processed, try to also convert
2684 * the types to HIR. This ensures that structure definitions embedded in
2685 * other structure definitions are processed.
2687 glsl_struct_field
*const fields
= talloc_array(state
, glsl_struct_field
,
2691 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2692 &this->declarations
) {
2693 const char *type_name
;
2695 decl_list
->type
->specifier
->hir(instructions
, state
);
2697 const glsl_type
*decl_type
=
2698 decl_list
->type
->specifier
->glsl_type(& type_name
, state
);
2700 foreach_list_typed (ast_declaration
, decl
, link
,
2701 &decl_list
->declarations
) {
2702 const struct glsl_type
*const field_type
=
2704 ? process_array_type(decl_type
, decl
->array_size
, state
)
2707 fields
[i
].type
= (field_type
!= NULL
)
2708 ? field_type
: glsl_type::error_type
;
2709 fields
[i
].name
= decl
->identifier
;
2714 assert(i
== decl_count
);
2717 if (this->name
== NULL
) {
2718 static unsigned anon_count
= 1;
2721 snprintf(buf
, sizeof(buf
), "#anon_struct_%04x", anon_count
);
2729 const glsl_type
*t
=
2730 glsl_type::get_record_instance(fields
, decl_count
, name
);
2732 YYLTYPE loc
= this->get_location();
2733 if (!state
->symbols
->add_type(name
, t
)) {
2734 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
2737 const glsl_type
**s
= (const glsl_type
**)
2738 realloc(state
->user_structures
,
2739 sizeof(state
->user_structures
[0]) *
2740 (state
->num_user_structures
+ 1));
2742 s
[state
->num_user_structures
] = t
;
2743 state
->user_structures
= s
;
2744 state
->num_user_structures
++;
2748 /* Structure type definitions do not have r-values.