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
);
806 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-wise operators");
807 error_emitted
= true;
810 case ast_logic_and
: {
811 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
813 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
814 YYLTYPE loc
= this->subexpressions
[0]->get_location();
816 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
817 operator_string(this->oper
));
818 error_emitted
= true;
821 ir_constant
*op0_const
= op
[0]->constant_expression_value();
823 if (op0_const
->value
.b
[0]) {
824 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
826 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
827 YYLTYPE loc
= this->subexpressions
[1]->get_location();
829 _mesa_glsl_error(& loc
, state
,
830 "RHS of `%s' must be scalar boolean",
831 operator_string(this->oper
));
832 error_emitted
= true;
838 type
= glsl_type::bool_type
;
840 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
843 instructions
->push_tail(tmp
);
845 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
846 instructions
->push_tail(stmt
);
848 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
850 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
851 YYLTYPE loc
= this->subexpressions
[1]->get_location();
853 _mesa_glsl_error(& loc
, state
,
854 "RHS of `%s' must be scalar boolean",
855 operator_string(this->oper
));
856 error_emitted
= true;
859 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
860 ir_assignment
*const then_assign
=
861 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
862 stmt
->then_instructions
.push_tail(then_assign
);
864 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
865 ir_assignment
*const else_assign
=
866 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false), NULL
);
867 stmt
->else_instructions
.push_tail(else_assign
);
869 result
= new(ctx
) ir_dereference_variable(tmp
);
876 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
878 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
879 YYLTYPE loc
= this->subexpressions
[0]->get_location();
881 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
882 operator_string(this->oper
));
883 error_emitted
= true;
886 ir_constant
*op0_const
= op
[0]->constant_expression_value();
888 if (op0_const
->value
.b
[0]) {
891 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
893 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
894 YYLTYPE loc
= this->subexpressions
[1]->get_location();
896 _mesa_glsl_error(& loc
, state
,
897 "RHS of `%s' must be scalar boolean",
898 operator_string(this->oper
));
899 error_emitted
= true;
903 type
= glsl_type::bool_type
;
905 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
908 instructions
->push_tail(tmp
);
910 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
911 instructions
->push_tail(stmt
);
913 op
[1] = this->subexpressions
[1]->hir(&stmt
->else_instructions
, state
);
915 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
916 YYLTYPE loc
= this->subexpressions
[1]->get_location();
918 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
919 operator_string(this->oper
));
920 error_emitted
= true;
923 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
924 ir_assignment
*const then_assign
=
925 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true), NULL
);
926 stmt
->then_instructions
.push_tail(then_assign
);
928 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
929 ir_assignment
*const else_assign
=
930 new(ctx
) ir_assignment(else_deref
, op
[1], NULL
);
931 stmt
->else_instructions
.push_tail(else_assign
);
933 result
= new(ctx
) ir_dereference_variable(tmp
);
940 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
941 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
944 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
946 type
= glsl_type::bool_type
;
950 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
952 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
953 YYLTYPE loc
= this->subexpressions
[0]->get_location();
955 _mesa_glsl_error(& loc
, state
,
956 "operand of `!' must be scalar boolean");
957 error_emitted
= true;
960 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
962 type
= glsl_type::bool_type
;
968 case ast_sub_assign
: {
969 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
970 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
972 type
= arithmetic_result_type(op
[0], op
[1],
973 (this->oper
== ast_mul_assign
),
976 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
979 result
= do_assignment(instructions
, state
,
980 op
[0]->clone(ctx
, NULL
), temp_rhs
,
981 this->subexpressions
[0]->get_location());
983 error_emitted
= (op
[0]->type
->is_error());
985 /* GLSL 1.10 does not allow array assignment. However, we don't have to
986 * explicitly test for this because none of the binary expression
987 * operators allow array operands either.
993 case ast_mod_assign
: {
994 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
995 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
997 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
999 assert(operations
[this->oper
] == ir_binop_mod
);
1001 ir_rvalue
*temp_rhs
;
1002 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1005 result
= do_assignment(instructions
, state
,
1006 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1007 this->subexpressions
[0]->get_location());
1008 type
= result
->type
;
1009 error_emitted
= type
->is_error();
1015 _mesa_glsl_error(& loc
, state
,
1016 "FINISHME: implement bit-shift assignment operators");
1017 error_emitted
= true;
1020 case ast_and_assign
:
1021 case ast_xor_assign
:
1023 _mesa_glsl_error(& loc
, state
,
1024 "FINISHME: implement logic assignment operators");
1025 error_emitted
= true;
1028 case ast_conditional
: {
1029 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1031 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1033 * "The ternary selection operator (?:). It operates on three
1034 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1035 * first expression, which must result in a scalar Boolean."
1037 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
1038 YYLTYPE loc
= this->subexpressions
[0]->get_location();
1040 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
1041 error_emitted
= true;
1044 /* The :? operator is implemented by generating an anonymous temporary
1045 * followed by an if-statement. The last instruction in each branch of
1046 * the if-statement assigns a value to the anonymous temporary. This
1047 * temporary is the r-value of the expression.
1049 exec_list then_instructions
;
1050 exec_list else_instructions
;
1052 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1053 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1055 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1057 * "The second and third expressions can be any type, as
1058 * long their types match, or there is a conversion in
1059 * Section 4.1.10 "Implicit Conversions" that can be applied
1060 * to one of the expressions to make their types match. This
1061 * resulting matching type is the type of the entire
1064 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1065 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1066 || (op
[1]->type
!= op
[2]->type
)) {
1067 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1069 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
1070 "operator must have matching types.");
1071 error_emitted
= true;
1072 type
= glsl_type::error_type
;
1077 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1078 ir_constant
*then_val
= op
[1]->constant_expression_value();
1079 ir_constant
*else_val
= op
[2]->constant_expression_value();
1081 if (then_instructions
.is_empty()
1082 && else_instructions
.is_empty()
1083 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1084 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1086 ir_variable
*const tmp
=
1087 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1088 instructions
->push_tail(tmp
);
1090 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1091 instructions
->push_tail(stmt
);
1093 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1094 ir_dereference
*const then_deref
=
1095 new(ctx
) ir_dereference_variable(tmp
);
1096 ir_assignment
*const then_assign
=
1097 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
1098 stmt
->then_instructions
.push_tail(then_assign
);
1100 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1101 ir_dereference
*const else_deref
=
1102 new(ctx
) ir_dereference_variable(tmp
);
1103 ir_assignment
*const else_assign
=
1104 new(ctx
) ir_assignment(else_deref
, op
[2], NULL
);
1105 stmt
->else_instructions
.push_tail(else_assign
);
1107 result
= new(ctx
) ir_dereference_variable(tmp
);
1114 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1115 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1116 op
[1] = new(ctx
) ir_constant(1.0f
);
1118 op
[1] = new(ctx
) ir_constant(1);
1120 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1122 ir_rvalue
*temp_rhs
;
1123 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1126 result
= do_assignment(instructions
, state
,
1127 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1128 this->subexpressions
[0]->get_location());
1129 type
= result
->type
;
1130 error_emitted
= op
[0]->type
->is_error();
1135 case ast_post_dec
: {
1136 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1137 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1138 op
[1] = new(ctx
) ir_constant(1.0f
);
1140 op
[1] = new(ctx
) ir_constant(1);
1142 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1144 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1146 ir_rvalue
*temp_rhs
;
1147 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1150 /* Get a temporary of a copy of the lvalue before it's modified.
1151 * This may get thrown away later.
1153 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1155 (void)do_assignment(instructions
, state
,
1156 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1157 this->subexpressions
[0]->get_location());
1159 type
= result
->type
;
1160 error_emitted
= op
[0]->type
->is_error();
1164 case ast_field_selection
:
1165 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1166 type
= result
->type
;
1169 case ast_array_index
: {
1170 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1172 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1173 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1175 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1177 ir_rvalue
*const array
= op
[0];
1179 result
= new(ctx
) ir_dereference_array(op
[0], op
[1]);
1181 /* Do not use op[0] after this point. Use array.
1189 if (!array
->type
->is_array()
1190 && !array
->type
->is_matrix()
1191 && !array
->type
->is_vector()) {
1192 _mesa_glsl_error(& index_loc
, state
,
1193 "cannot dereference non-array / non-matrix / "
1195 error_emitted
= true;
1198 if (!op
[1]->type
->is_integer()) {
1199 _mesa_glsl_error(& index_loc
, state
,
1200 "array index must be integer type");
1201 error_emitted
= true;
1202 } else if (!op
[1]->type
->is_scalar()) {
1203 _mesa_glsl_error(& index_loc
, state
,
1204 "array index must be scalar");
1205 error_emitted
= true;
1208 /* If the array index is a constant expression and the array has a
1209 * declared size, ensure that the access is in-bounds. If the array
1210 * index is not a constant expression, ensure that the array has a
1213 ir_constant
*const const_index
= op
[1]->constant_expression_value();
1214 if (const_index
!= NULL
) {
1215 const int idx
= const_index
->value
.i
[0];
1216 const char *type_name
;
1219 if (array
->type
->is_matrix()) {
1220 type_name
= "matrix";
1221 } else if (array
->type
->is_vector()) {
1222 type_name
= "vector";
1224 type_name
= "array";
1227 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1229 * "It is illegal to declare an array with a size, and then
1230 * later (in the same shader) index the same array with an
1231 * integral constant expression greater than or equal to the
1232 * declared size. It is also illegal to index an array with a
1233 * negative constant expression."
1235 if (array
->type
->is_matrix()) {
1236 if (array
->type
->row_type()->vector_elements
<= idx
) {
1237 bound
= array
->type
->row_type()->vector_elements
;
1239 } else if (array
->type
->is_vector()) {
1240 if (array
->type
->vector_elements
<= idx
) {
1241 bound
= array
->type
->vector_elements
;
1244 if ((array
->type
->array_size() > 0)
1245 && (array
->type
->array_size() <= idx
)) {
1246 bound
= array
->type
->array_size();
1251 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
1253 error_emitted
= true;
1254 } else if (idx
< 0) {
1255 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
1257 error_emitted
= true;
1260 if (array
->type
->is_array()) {
1261 /* If the array is a variable dereference, it dereferences the
1262 * whole array, by definition. Use this to get the variable.
1264 * FINISHME: Should some methods for getting / setting / testing
1265 * FINISHME: array access limits be added to ir_dereference?
1267 ir_variable
*const v
= array
->whole_variable_referenced();
1268 if ((v
!= NULL
) && (unsigned(idx
) > v
->max_array_access
))
1269 v
->max_array_access
= idx
;
1271 } else if (array
->type
->array_size() == 0) {
1272 _mesa_glsl_error(&loc
, state
, "unsized array index must be constant");
1274 if (array
->type
->is_array()) {
1275 /* whole_variable_referenced can return NULL if the array is a
1276 * member of a structure. In this case it is safe to not update
1277 * the max_array_access field because it is never used for fields
1280 ir_variable
*v
= array
->whole_variable_referenced();
1282 v
->max_array_access
= array
->type
->array_size();
1287 result
->type
= glsl_type::error_type
;
1289 type
= result
->type
;
1293 case ast_function_call
:
1294 /* Should *NEVER* get here. ast_function_call should always be handled
1295 * by ast_function_expression::hir.
1300 case ast_identifier
: {
1301 /* ast_identifier can appear several places in a full abstract syntax
1302 * tree. This particular use must be at location specified in the grammar
1303 * as 'variable_identifier'.
1306 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1308 result
= new(ctx
) ir_dereference_variable(var
);
1311 type
= result
->type
;
1313 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1314 this->primary_expression
.identifier
);
1316 error_emitted
= true;
1321 case ast_int_constant
:
1322 type
= glsl_type::int_type
;
1323 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1326 case ast_uint_constant
:
1327 type
= glsl_type::uint_type
;
1328 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1331 case ast_float_constant
:
1332 type
= glsl_type::float_type
;
1333 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1336 case ast_bool_constant
:
1337 type
= glsl_type::bool_type
;
1338 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1341 case ast_sequence
: {
1342 /* It should not be possible to generate a sequence in the AST without
1343 * any expressions in it.
1345 assert(!this->expressions
.is_empty());
1347 /* The r-value of a sequence is the last expression in the sequence. If
1348 * the other expressions in the sequence do not have side-effects (and
1349 * therefore add instructions to the instruction list), they get dropped
1352 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
)
1353 result
= ast
->hir(instructions
, state
);
1355 type
= result
->type
;
1357 /* Any errors should have already been emitted in the loop above.
1359 error_emitted
= true;
1364 if (type
->is_error() && !error_emitted
)
1365 _mesa_glsl_error(& loc
, state
, "type mismatch");
1372 ast_expression_statement::hir(exec_list
*instructions
,
1373 struct _mesa_glsl_parse_state
*state
)
1375 /* It is possible to have expression statements that don't have an
1376 * expression. This is the solitary semicolon:
1378 * for (i = 0; i < 5; i++)
1381 * In this case the expression will be NULL. Test for NULL and don't do
1382 * anything in that case.
1384 if (expression
!= NULL
)
1385 expression
->hir(instructions
, state
);
1387 /* Statements do not have r-values.
1394 ast_compound_statement::hir(exec_list
*instructions
,
1395 struct _mesa_glsl_parse_state
*state
)
1398 state
->symbols
->push_scope();
1400 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1401 ast
->hir(instructions
, state
);
1404 state
->symbols
->pop_scope();
1406 /* Compound statements do not have r-values.
1412 static const glsl_type
*
1413 process_array_type(const glsl_type
*base
, ast_node
*array_size
,
1414 struct _mesa_glsl_parse_state
*state
)
1416 unsigned length
= 0;
1418 /* FINISHME: Reject delcarations of multidimensional arrays. */
1420 if (array_size
!= NULL
) {
1421 exec_list dummy_instructions
;
1422 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1423 YYLTYPE loc
= array_size
->get_location();
1425 /* FINISHME: Verify that the grammar forbids side-effects in array
1426 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1428 assert(dummy_instructions
.is_empty());
1431 if (!ir
->type
->is_integer()) {
1432 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1433 } else if (!ir
->type
->is_scalar()) {
1434 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1436 ir_constant
*const size
= ir
->constant_expression_value();
1439 _mesa_glsl_error(& loc
, state
, "array size must be a "
1440 "constant valued expression");
1441 } else if (size
->value
.i
[0] <= 0) {
1442 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1444 assert(size
->type
== ir
->type
);
1445 length
= size
->value
.u
[0];
1451 return glsl_type::get_array_instance(base
, length
);
1456 ast_type_specifier::glsl_type(const char **name
,
1457 struct _mesa_glsl_parse_state
*state
) const
1459 const struct glsl_type
*type
;
1461 if ((this->type_specifier
== ast_struct
) && (this->type_name
== NULL
)) {
1462 /* FINISHME: Handle annonymous structures. */
1465 type
= state
->symbols
->get_type(this->type_name
);
1466 *name
= this->type_name
;
1468 if (this->is_array
) {
1469 type
= process_array_type(type
, this->array_size
, state
);
1478 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1480 struct _mesa_glsl_parse_state
*state
,
1483 if (qual
->invariant
)
1486 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1487 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
1488 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1494 if (qual
->attribute
&& state
->target
!= vertex_shader
) {
1495 var
->type
= glsl_type::error_type
;
1496 _mesa_glsl_error(loc
, state
,
1497 "`attribute' variables may not be declared in the "
1499 _mesa_glsl_shader_target_name(state
->target
));
1502 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1504 * "The varying qualifier can be used only with the data types
1505 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1508 if (qual
->varying
) {
1509 const glsl_type
*non_array_type
;
1511 if (var
->type
&& var
->type
->is_array())
1512 non_array_type
= var
->type
->fields
.array
;
1514 non_array_type
= var
->type
;
1516 if (non_array_type
&& non_array_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1517 var
->type
= glsl_type::error_type
;
1518 _mesa_glsl_error(loc
, state
,
1519 "varying variables must be of base type float");
1523 /* If there is no qualifier that changes the mode of the variable, leave
1524 * the setting alone.
1526 if (qual
->in
&& qual
->out
)
1527 var
->mode
= ir_var_inout
;
1528 else if (qual
->attribute
|| qual
->in
1529 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1530 var
->mode
= ir_var_in
;
1531 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
1532 var
->mode
= ir_var_out
;
1533 else if (qual
->uniform
)
1534 var
->mode
= ir_var_uniform
;
1537 var
->interpolation
= ir_var_flat
;
1538 else if (qual
->noperspective
)
1539 var
->interpolation
= ir_var_noperspective
;
1541 var
->interpolation
= ir_var_smooth
;
1543 var
->pixel_center_integer
= qual
->pixel_center_integer
;
1544 var
->origin_upper_left
= qual
->origin_upper_left
;
1545 if ((qual
->origin_upper_left
|| qual
->pixel_center_integer
)
1546 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
1547 const char *const qual_string
= (qual
->origin_upper_left
)
1548 ? "origin_upper_left" : "pixel_center_integer";
1550 _mesa_glsl_error(loc
, state
,
1551 "layout qualifier `%s' can only be applied to "
1552 "fragment shader input `gl_FragCoord'",
1556 if (var
->type
->is_array() && (state
->language_version
>= 120)) {
1557 var
->array_lvalue
= true;
1563 ast_declarator_list::hir(exec_list
*instructions
,
1564 struct _mesa_glsl_parse_state
*state
)
1567 const struct glsl_type
*decl_type
;
1568 const char *type_name
= NULL
;
1569 ir_rvalue
*result
= NULL
;
1570 YYLTYPE loc
= this->get_location();
1572 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
1574 * "To ensure that a particular output variable is invariant, it is
1575 * necessary to use the invariant qualifier. It can either be used to
1576 * qualify a previously declared variable as being invariant
1578 * invariant gl_Position; // make existing gl_Position be invariant"
1580 * In these cases the parser will set the 'invariant' flag in the declarator
1581 * list, and the type will be NULL.
1583 if (this->invariant
) {
1584 assert(this->type
== NULL
);
1586 if (state
->current_function
!= NULL
) {
1587 _mesa_glsl_error(& loc
, state
,
1588 "All uses of `invariant' keyword must be at global "
1592 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1593 assert(!decl
->is_array
);
1594 assert(decl
->array_size
== NULL
);
1595 assert(decl
->initializer
== NULL
);
1597 ir_variable
*const earlier
=
1598 state
->symbols
->get_variable(decl
->identifier
);
1599 if (earlier
== NULL
) {
1600 _mesa_glsl_error(& loc
, state
,
1601 "Undeclared variable `%s' cannot be marked "
1602 "invariant\n", decl
->identifier
);
1603 } else if ((state
->target
== vertex_shader
)
1604 && (earlier
->mode
!= ir_var_out
)) {
1605 _mesa_glsl_error(& loc
, state
,
1606 "`%s' cannot be marked invariant, vertex shader "
1607 "outputs only\n", decl
->identifier
);
1608 } else if ((state
->target
== fragment_shader
)
1609 && (earlier
->mode
!= ir_var_in
)) {
1610 _mesa_glsl_error(& loc
, state
,
1611 "`%s' cannot be marked invariant, fragment shader "
1612 "inputs only\n", decl
->identifier
);
1614 earlier
->invariant
= true;
1618 /* Invariant redeclarations do not have r-values.
1623 assert(this->type
!= NULL
);
1624 assert(!this->invariant
);
1626 /* The type specifier may contain a structure definition. Process that
1627 * before any of the variable declarations.
1629 (void) this->type
->specifier
->hir(instructions
, state
);
1631 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1632 if (this->declarations
.is_empty()) {
1633 /* The only valid case where the declaration list can be empty is when
1634 * the declaration is setting the default precision of a built-in type
1635 * (e.g., 'precision highp vec4;').
1638 if (decl_type
!= NULL
) {
1640 _mesa_glsl_error(& loc
, state
, "incomplete declaration");
1644 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1645 const struct glsl_type
*var_type
;
1648 /* FINISHME: Emit a warning if a variable declaration shadows a
1649 * FINISHME: declaration at a higher scope.
1652 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1653 if (type_name
!= NULL
) {
1654 _mesa_glsl_error(& loc
, state
,
1655 "invalid type `%s' in declaration of `%s'",
1656 type_name
, decl
->identifier
);
1658 _mesa_glsl_error(& loc
, state
,
1659 "invalid type in declaration of `%s'",
1665 if (decl
->is_array
) {
1666 var_type
= process_array_type(decl_type
, decl
->array_size
, state
);
1668 var_type
= decl_type
;
1671 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
1673 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1675 * "Global variables can only use the qualifiers const,
1676 * attribute, uni form, or varying. Only one may be
1679 * Local variables can only use the qualifier const."
1681 * This is relaxed in GLSL 1.30.
1683 if (state
->language_version
< 120) {
1684 if (this->type
->qualifier
.out
) {
1685 _mesa_glsl_error(& loc
, state
,
1686 "`out' qualifier in declaration of `%s' "
1687 "only valid for function parameters in GLSL 1.10.",
1690 if (this->type
->qualifier
.in
) {
1691 _mesa_glsl_error(& loc
, state
,
1692 "`in' qualifier in declaration of `%s' "
1693 "only valid for function parameters in GLSL 1.10.",
1696 /* FINISHME: Test for other invalid qualifiers. */
1699 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1702 if (this->type
->qualifier
.invariant
) {
1703 if ((state
->target
== vertex_shader
) && !(var
->mode
== ir_var_out
||
1704 var
->mode
== ir_var_inout
)) {
1705 /* FINISHME: Note that this doesn't work for invariant on
1706 * a function signature outval
1708 _mesa_glsl_error(& loc
, state
,
1709 "`%s' cannot be marked invariant, vertex shader "
1710 "outputs only\n", var
->name
);
1711 } else if ((state
->target
== fragment_shader
) &&
1712 !(var
->mode
== ir_var_in
|| var
->mode
== ir_var_inout
)) {
1713 /* FINISHME: Note that this doesn't work for invariant on
1714 * a function signature inval
1716 _mesa_glsl_error(& loc
, state
,
1717 "`%s' cannot be marked invariant, fragment shader "
1718 "inputs only\n", var
->name
);
1722 if (state
->current_function
!= NULL
) {
1723 const char *mode
= NULL
;
1724 const char *extra
= "";
1726 /* There is no need to check for 'inout' here because the parser will
1727 * only allow that in function parameter lists.
1729 if (this->type
->qualifier
.attribute
) {
1731 } else if (this->type
->qualifier
.uniform
) {
1733 } else if (this->type
->qualifier
.varying
) {
1735 } else if (this->type
->qualifier
.in
) {
1737 extra
= " or in function parameter list";
1738 } else if (this->type
->qualifier
.out
) {
1740 extra
= " or in function parameter list";
1744 _mesa_glsl_error(& loc
, state
,
1745 "%s variable `%s' must be declared at "
1747 mode
, var
->name
, extra
);
1749 } else if (var
->mode
== ir_var_in
) {
1750 if (state
->target
== vertex_shader
) {
1751 bool error_emitted
= false;
1753 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1755 * "Vertex shader inputs can only be float, floating-point
1756 * vectors, matrices, signed and unsigned integers and integer
1757 * vectors. Vertex shader inputs can also form arrays of these
1758 * types, but not structures."
1760 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1762 * "Vertex shader inputs can only be float, floating-point
1763 * vectors, matrices, signed and unsigned integers and integer
1764 * vectors. They cannot be arrays or structures."
1766 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1768 * "The attribute qualifier can be used only with float,
1769 * floating-point vectors, and matrices. Attribute variables
1770 * cannot be declared as arrays or structures."
1772 const glsl_type
*check_type
= var
->type
->is_array()
1773 ? var
->type
->fields
.array
: var
->type
;
1775 switch (check_type
->base_type
) {
1776 case GLSL_TYPE_FLOAT
:
1778 case GLSL_TYPE_UINT
:
1780 if (state
->language_version
> 120)
1784 _mesa_glsl_error(& loc
, state
,
1785 "vertex shader input / attribute cannot have "
1787 var
->type
->is_array() ? "array of " : "",
1789 error_emitted
= true;
1792 if (!error_emitted
&& (state
->language_version
<= 130)
1793 && var
->type
->is_array()) {
1794 _mesa_glsl_error(& loc
, state
,
1795 "vertex shader input / attribute cannot have "
1797 error_emitted
= true;
1802 /* Process the initializer and add its instructions to a temporary
1803 * list. This list will be added to the instruction stream (below) after
1804 * the declaration is added. This is done because in some cases (such as
1805 * redeclarations) the declaration may not actually be added to the
1806 * instruction stream.
1808 exec_list initializer_instructions
;
1809 if (decl
->initializer
!= NULL
) {
1810 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
1812 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1814 * "All uniform variables are read-only and are initialized either
1815 * directly by an application via API commands, or indirectly by
1818 if ((state
->language_version
<= 110)
1819 && (var
->mode
== ir_var_uniform
)) {
1820 _mesa_glsl_error(& initializer_loc
, state
,
1821 "cannot initialize uniforms in GLSL 1.10");
1824 if (var
->type
->is_sampler()) {
1825 _mesa_glsl_error(& initializer_loc
, state
,
1826 "cannot initialize samplers");
1829 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1830 _mesa_glsl_error(& initializer_loc
, state
,
1831 "cannot initialize %s shader input / %s",
1832 _mesa_glsl_shader_target_name(state
->target
),
1833 (state
->target
== vertex_shader
)
1834 ? "attribute" : "varying");
1837 ir_dereference
*const lhs
= new(ctx
) ir_dereference_variable(var
);
1838 ir_rvalue
*rhs
= decl
->initializer
->hir(&initializer_instructions
,
1841 /* Calculate the constant value if this is a const or uniform
1844 if (this->type
->qualifier
.constant
|| this->type
->qualifier
.uniform
) {
1845 ir_rvalue
*new_rhs
= validate_assignment(state
, var
->type
, rhs
);
1846 if (new_rhs
!= NULL
) {
1849 ir_constant
*constant_value
= rhs
->constant_expression_value();
1850 if (!constant_value
) {
1851 _mesa_glsl_error(& initializer_loc
, state
,
1852 "initializer of %s variable `%s' must be a "
1853 "constant expression",
1854 (this->type
->qualifier
.constant
)
1855 ? "const" : "uniform",
1857 if (var
->type
->is_numeric()) {
1858 /* Reduce cascading errors. */
1859 var
->constant_value
= ir_constant::zero(ctx
, var
->type
);
1862 rhs
= constant_value
;
1863 var
->constant_value
= constant_value
;
1866 _mesa_glsl_error(&initializer_loc
, state
,
1867 "initializer of type %s cannot be assigned to "
1868 "variable of type %s",
1869 rhs
->type
->name
, var
->type
->name
);
1870 if (var
->type
->is_numeric()) {
1871 /* Reduce cascading errors. */
1872 var
->constant_value
= ir_constant::zero(ctx
, var
->type
);
1877 if (rhs
&& !rhs
->type
->is_error()) {
1878 bool temp
= var
->read_only
;
1879 if (this->type
->qualifier
.constant
)
1880 var
->read_only
= false;
1882 /* Never emit code to initialize a uniform.
1884 if (!this->type
->qualifier
.uniform
)
1885 result
= do_assignment(&initializer_instructions
, state
,
1887 this->get_location());
1888 var
->read_only
= temp
;
1892 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1894 * "It is an error to write to a const variable outside of
1895 * its declaration, so they must be initialized when
1898 if (this->type
->qualifier
.constant
&& decl
->initializer
== NULL
) {
1899 _mesa_glsl_error(& loc
, state
,
1900 "const declaration of `%s' must be initialized");
1903 /* Check if this declaration is actually a re-declaration, either to
1904 * resize an array or add qualifiers to an existing variable.
1906 * This is allowed for variables in the current scope, or when at
1907 * global scope (for built-ins in the implicit outer scope).
1909 ir_variable
*earlier
= state
->symbols
->get_variable(decl
->identifier
);
1910 if (earlier
!= NULL
&& (state
->current_function
== NULL
||
1911 state
->symbols
->name_declared_this_scope(decl
->identifier
))) {
1913 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1915 * "It is legal to declare an array without a size and then
1916 * later re-declare the same name as an array of the same
1917 * type and specify a size."
1919 if ((earlier
->type
->array_size() == 0)
1920 && var
->type
->is_array()
1921 && (var
->type
->element_type() == earlier
->type
->element_type())) {
1922 /* FINISHME: This doesn't match the qualifiers on the two
1923 * FINISHME: declarations. It's not 100% clear whether this is
1924 * FINISHME: required or not.
1927 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1929 * "The size [of gl_TexCoord] can be at most
1930 * gl_MaxTextureCoords."
1932 const unsigned size
= unsigned(var
->type
->array_size());
1933 if ((strcmp("gl_TexCoord", var
->name
) == 0)
1934 && (size
> state
->Const
.MaxTextureCoords
)) {
1935 YYLTYPE loc
= this->get_location();
1937 _mesa_glsl_error(& loc
, state
, "`gl_TexCoord' array size cannot "
1938 "be larger than gl_MaxTextureCoords (%u)\n",
1939 state
->Const
.MaxTextureCoords
);
1940 } else if ((size
> 0) && (size
<= earlier
->max_array_access
)) {
1941 YYLTYPE loc
= this->get_location();
1943 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
1945 earlier
->max_array_access
);
1948 earlier
->type
= var
->type
;
1951 } else if (state
->extensions
->ARB_fragment_coord_conventions
1952 && strcmp(var
->name
, "gl_FragCoord") == 0
1953 && earlier
->type
== var
->type
1954 && earlier
->mode
== var
->mode
) {
1955 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
1958 earlier
->origin_upper_left
= var
->origin_upper_left
;
1959 earlier
->pixel_center_integer
= var
->pixel_center_integer
;
1961 YYLTYPE loc
= this->get_location();
1962 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", decl
->identifier
);
1968 /* By now, we know it's a new variable declaration (we didn't hit the
1969 * above "continue").
1971 * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1973 * "Identifiers starting with "gl_" are reserved for use by
1974 * OpenGL, and may not be declared in a shader as either a
1975 * variable or a function."
1977 if (strncmp(decl
->identifier
, "gl_", 3) == 0)
1978 _mesa_glsl_error(& loc
, state
,
1979 "identifier `%s' uses reserved `gl_' prefix",
1982 /* Add the variable to the symbol table. Note that the initializer's
1983 * IR was already processed earlier (though it hasn't been emitted yet),
1984 * without the variable in scope.
1986 * This differs from most C-like languages, but it follows the GLSL
1987 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1990 * "Within a declaration, the scope of a name starts immediately
1991 * after the initializer if present or immediately after the name
1992 * being declared if not."
1994 if (!state
->symbols
->add_variable(var
->name
, var
)) {
1995 YYLTYPE loc
= this->get_location();
1996 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
1997 "current scope", decl
->identifier
);
2001 /* Push the variable declaration to the top. It means that all
2002 * the variable declarations will appear in a funny
2003 * last-to-first order, but otherwise we run into trouble if a
2004 * function is prototyped, a global var is decled, then the
2005 * function is defined with usage of the global var. See
2006 * glslparsertest's CorrectModule.frag.
2008 instructions
->push_head(var
);
2009 instructions
->append_list(&initializer_instructions
);
2013 /* Generally, variable declarations do not have r-values. However,
2014 * one is used for the declaration in
2016 * while (bool b = some_condition()) {
2020 * so we return the rvalue from the last seen declaration here.
2027 ast_parameter_declarator::hir(exec_list
*instructions
,
2028 struct _mesa_glsl_parse_state
*state
)
2031 const struct glsl_type
*type
;
2032 const char *name
= NULL
;
2033 YYLTYPE loc
= this->get_location();
2035 type
= this->type
->specifier
->glsl_type(& name
, state
);
2039 _mesa_glsl_error(& loc
, state
,
2040 "invalid type `%s' in declaration of `%s'",
2041 name
, this->identifier
);
2043 _mesa_glsl_error(& loc
, state
,
2044 "invalid type in declaration of `%s'",
2048 type
= glsl_type::error_type
;
2051 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2053 * "Functions that accept no input arguments need not use void in the
2054 * argument list because prototypes (or definitions) are required and
2055 * therefore there is no ambiguity when an empty argument list "( )" is
2056 * declared. The idiom "(void)" as a parameter list is provided for
2059 * Placing this check here prevents a void parameter being set up
2060 * for a function, which avoids tripping up checks for main taking
2061 * parameters and lookups of an unnamed symbol.
2063 if (type
->is_void()) {
2064 if (this->identifier
!= NULL
)
2065 _mesa_glsl_error(& loc
, state
,
2066 "named parameter cannot have type `void'");
2072 if (formal_parameter
&& (this->identifier
== NULL
)) {
2073 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
2077 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
2078 * call already handled the "vec4[..] foo" case.
2080 if (this->is_array
) {
2081 type
= process_array_type(type
, this->array_size
, state
);
2084 if (type
->array_size() == 0) {
2085 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
2086 "a declared size.");
2087 type
= glsl_type::error_type
;
2091 ir_variable
*var
= new(ctx
) ir_variable(type
, this->identifier
, ir_var_in
);
2093 /* Apply any specified qualifiers to the parameter declaration. Note that
2094 * for function parameters the default mode is 'in'.
2096 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
2098 instructions
->push_tail(var
);
2100 /* Parameter declarations do not have r-values.
2107 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
2109 exec_list
*ir_parameters
,
2110 _mesa_glsl_parse_state
*state
)
2112 ast_parameter_declarator
*void_param
= NULL
;
2115 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
2116 param
->formal_parameter
= formal
;
2117 param
->hir(ir_parameters
, state
);
2125 if ((void_param
!= NULL
) && (count
> 1)) {
2126 YYLTYPE loc
= void_param
->get_location();
2128 _mesa_glsl_error(& loc
, state
,
2129 "`void' parameter must be only parameter");
2135 ast_function::hir(exec_list
*instructions
,
2136 struct _mesa_glsl_parse_state
*state
)
2139 ir_function
*f
= NULL
;
2140 ir_function_signature
*sig
= NULL
;
2141 exec_list hir_parameters
;
2143 const char *const name
= identifier
;
2145 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2147 * "Identifiers starting with "gl_" are reserved for use by
2148 * OpenGL, and may not be declared in a shader as either a
2149 * variable or a function."
2151 if (strncmp(name
, "gl_", 3) == 0) {
2152 YYLTYPE loc
= this->get_location();
2153 _mesa_glsl_error(&loc
, state
,
2154 "identifier `%s' uses reserved `gl_' prefix", name
);
2157 /* Convert the list of function parameters to HIR now so that they can be
2158 * used below to compare this function's signature with previously seen
2159 * signatures for functions with the same name.
2161 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
2163 & hir_parameters
, state
);
2165 const char *return_type_name
;
2166 const glsl_type
*return_type
=
2167 this->return_type
->specifier
->glsl_type(& return_type_name
, state
);
2170 YYLTYPE loc
= this->get_location();
2171 _mesa_glsl_error(&loc
, state
,
2172 "function `%s' has undeclared return type `%s'",
2173 name
, return_type_name
);
2174 return_type
= glsl_type::error_type
;
2177 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2178 * "No qualifier is allowed on the return type of a function."
2180 if (this->return_type
->has_qualifiers()) {
2181 YYLTYPE loc
= this->get_location();
2182 _mesa_glsl_error(& loc
, state
,
2183 "function `%s' return type has qualifiers", name
);
2186 /* Verify that this function's signature either doesn't match a previously
2187 * seen signature for a function with the same name, or, if a match is found,
2188 * that the previously seen signature does not have an associated definition.
2190 f
= state
->symbols
->get_function(name
, false);
2191 if (f
!= NULL
&& !f
->is_builtin
) {
2192 sig
= f
->exact_matching_signature(&hir_parameters
);
2194 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
2195 if (badvar
!= NULL
) {
2196 YYLTYPE loc
= this->get_location();
2198 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
2199 "qualifiers don't match prototype", name
, badvar
);
2202 if (sig
->return_type
!= return_type
) {
2203 YYLTYPE loc
= this->get_location();
2205 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
2206 "match prototype", name
);
2209 if (is_definition
&& sig
->is_defined
) {
2210 YYLTYPE loc
= this->get_location();
2212 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
2216 f
= new(ctx
) ir_function(name
);
2217 if (!state
->symbols
->add_function(f
->name
, f
)) {
2218 /* This function name shadows a non-function use of the same name. */
2219 YYLTYPE loc
= this->get_location();
2221 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
2222 "non-function", name
);
2226 /* Emit the new function header */
2227 instructions
->push_tail(f
);
2230 /* Verify the return type of main() */
2231 if (strcmp(name
, "main") == 0) {
2232 if (! return_type
->is_void()) {
2233 YYLTYPE loc
= this->get_location();
2235 _mesa_glsl_error(& loc
, state
, "main() must return void");
2238 if (!hir_parameters
.is_empty()) {
2239 YYLTYPE loc
= this->get_location();
2241 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
2245 /* Finish storing the information about this new function in its signature.
2248 sig
= new(ctx
) ir_function_signature(return_type
);
2249 f
->add_signature(sig
);
2252 sig
->replace_parameters(&hir_parameters
);
2255 /* Function declarations (prototypes) do not have r-values.
2262 ast_function_definition::hir(exec_list
*instructions
,
2263 struct _mesa_glsl_parse_state
*state
)
2265 prototype
->is_definition
= true;
2266 prototype
->hir(instructions
, state
);
2268 ir_function_signature
*signature
= prototype
->signature
;
2269 if (signature
== NULL
)
2272 assert(state
->current_function
== NULL
);
2273 state
->current_function
= signature
;
2274 state
->found_return
= false;
2276 /* Duplicate parameters declared in the prototype as concrete variables.
2277 * Add these to the symbol table.
2279 state
->symbols
->push_scope();
2280 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
2281 ir_variable
*const var
= ((ir_instruction
*) iter
.get())->as_variable();
2283 assert(var
!= NULL
);
2285 /* The only way a parameter would "exist" is if two parameters have
2288 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
2289 YYLTYPE loc
= this->get_location();
2291 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
2293 state
->symbols
->add_variable(var
->name
, var
);
2297 /* Convert the body of the function to HIR. */
2298 this->body
->hir(&signature
->body
, state
);
2299 signature
->is_defined
= true;
2301 state
->symbols
->pop_scope();
2303 assert(state
->current_function
== signature
);
2304 state
->current_function
= NULL
;
2306 if (!signature
->return_type
->is_void() && !state
->found_return
) {
2307 YYLTYPE loc
= this->get_location();
2308 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
2309 "%s, but no return statement",
2310 signature
->function_name(),
2311 signature
->return_type
->name
);
2314 /* Function definitions do not have r-values.
2321 ast_jump_statement::hir(exec_list
*instructions
,
2322 struct _mesa_glsl_parse_state
*state
)
2329 assert(state
->current_function
);
2331 if (opt_return_value
) {
2332 if (state
->current_function
->return_type
->base_type
==
2334 YYLTYPE loc
= this->get_location();
2336 _mesa_glsl_error(& loc
, state
,
2337 "`return` with a value, in function `%s' "
2339 state
->current_function
->function_name());
2342 ir_expression
*const ret
= (ir_expression
*)
2343 opt_return_value
->hir(instructions
, state
);
2344 assert(ret
!= NULL
);
2346 /* Implicit conversions are not allowed for return values. */
2347 if (state
->current_function
->return_type
!= ret
->type
) {
2348 YYLTYPE loc
= this->get_location();
2350 _mesa_glsl_error(& loc
, state
,
2351 "`return' with wrong type %s, in function `%s' "
2354 state
->current_function
->function_name(),
2355 state
->current_function
->return_type
->name
);
2358 inst
= new(ctx
) ir_return(ret
);
2360 if (state
->current_function
->return_type
->base_type
!=
2362 YYLTYPE loc
= this->get_location();
2364 _mesa_glsl_error(& loc
, state
,
2365 "`return' with no value, in function %s returning "
2367 state
->current_function
->function_name());
2369 inst
= new(ctx
) ir_return
;
2372 state
->found_return
= true;
2373 instructions
->push_tail(inst
);
2378 if (state
->target
!= fragment_shader
) {
2379 YYLTYPE loc
= this->get_location();
2381 _mesa_glsl_error(& loc
, state
,
2382 "`discard' may only appear in a fragment shader");
2384 instructions
->push_tail(new(ctx
) ir_discard
);
2389 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2390 * FINISHME: and they use a different IR instruction for 'break'.
2392 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2393 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2396 if (state
->loop_or_switch_nesting
== NULL
) {
2397 YYLTYPE loc
= this->get_location();
2399 _mesa_glsl_error(& loc
, state
,
2400 "`%s' may only appear in a loop",
2401 (mode
== ast_break
) ? "break" : "continue");
2403 ir_loop
*const loop
= state
->loop_or_switch_nesting
->as_loop();
2405 /* Inline the for loop expression again, since we don't know
2406 * where near the end of the loop body the normal copy of it
2407 * is going to be placed.
2409 if (mode
== ast_continue
&&
2410 state
->loop_or_switch_nesting_ast
->rest_expression
) {
2411 state
->loop_or_switch_nesting_ast
->rest_expression
->hir(instructions
,
2416 ir_loop_jump
*const jump
=
2417 new(ctx
) ir_loop_jump((mode
== ast_break
)
2418 ? ir_loop_jump::jump_break
2419 : ir_loop_jump::jump_continue
);
2420 instructions
->push_tail(jump
);
2427 /* Jump instructions do not have r-values.
2434 ast_selection_statement::hir(exec_list
*instructions
,
2435 struct _mesa_glsl_parse_state
*state
)
2439 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
2441 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2443 * "Any expression whose type evaluates to a Boolean can be used as the
2444 * conditional expression bool-expression. Vector types are not accepted
2445 * as the expression to if."
2447 * The checks are separated so that higher quality diagnostics can be
2448 * generated for cases where both rules are violated.
2450 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
2451 YYLTYPE loc
= this->condition
->get_location();
2453 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
2457 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
2459 if (then_statement
!= NULL
) {
2460 state
->symbols
->push_scope();
2461 then_statement
->hir(& stmt
->then_instructions
, state
);
2462 state
->symbols
->pop_scope();
2465 if (else_statement
!= NULL
) {
2466 state
->symbols
->push_scope();
2467 else_statement
->hir(& stmt
->else_instructions
, state
);
2468 state
->symbols
->pop_scope();
2471 instructions
->push_tail(stmt
);
2473 /* if-statements do not have r-values.
2480 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
2481 struct _mesa_glsl_parse_state
*state
)
2485 if (condition
!= NULL
) {
2486 ir_rvalue
*const cond
=
2487 condition
->hir(& stmt
->body_instructions
, state
);
2490 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
2491 YYLTYPE loc
= condition
->get_location();
2493 _mesa_glsl_error(& loc
, state
,
2494 "loop condition must be scalar boolean");
2496 /* As the first code in the loop body, generate a block that looks
2497 * like 'if (!condition) break;' as the loop termination condition.
2499 ir_rvalue
*const not_cond
=
2500 new(ctx
) ir_expression(ir_unop_logic_not
, glsl_type::bool_type
, cond
,
2503 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
2505 ir_jump
*const break_stmt
=
2506 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
2508 if_stmt
->then_instructions
.push_tail(break_stmt
);
2509 stmt
->body_instructions
.push_tail(if_stmt
);
2516 ast_iteration_statement::hir(exec_list
*instructions
,
2517 struct _mesa_glsl_parse_state
*state
)
2521 /* For-loops and while-loops start a new scope, but do-while loops do not.
2523 if (mode
!= ast_do_while
)
2524 state
->symbols
->push_scope();
2526 if (init_statement
!= NULL
)
2527 init_statement
->hir(instructions
, state
);
2529 ir_loop
*const stmt
= new(ctx
) ir_loop();
2530 instructions
->push_tail(stmt
);
2532 /* Track the current loop and / or switch-statement nesting.
2534 ir_instruction
*const nesting
= state
->loop_or_switch_nesting
;
2535 ast_iteration_statement
*nesting_ast
= state
->loop_or_switch_nesting_ast
;
2537 state
->loop_or_switch_nesting
= stmt
;
2538 state
->loop_or_switch_nesting_ast
= this;
2540 if (mode
!= ast_do_while
)
2541 condition_to_hir(stmt
, state
);
2544 body
->hir(& stmt
->body_instructions
, state
);
2546 if (rest_expression
!= NULL
)
2547 rest_expression
->hir(& stmt
->body_instructions
, state
);
2549 if (mode
== ast_do_while
)
2550 condition_to_hir(stmt
, state
);
2552 if (mode
!= ast_do_while
)
2553 state
->symbols
->pop_scope();
2555 /* Restore previous nesting before returning.
2557 state
->loop_or_switch_nesting
= nesting
;
2558 state
->loop_or_switch_nesting_ast
= nesting_ast
;
2560 /* Loops do not have r-values.
2567 ast_type_specifier::hir(exec_list
*instructions
,
2568 struct _mesa_glsl_parse_state
*state
)
2570 if (this->structure
!= NULL
)
2571 return this->structure
->hir(instructions
, state
);
2578 ast_struct_specifier::hir(exec_list
*instructions
,
2579 struct _mesa_glsl_parse_state
*state
)
2581 unsigned decl_count
= 0;
2583 /* Make an initial pass over the list of structure fields to determine how
2584 * many there are. Each element in this list is an ast_declarator_list.
2585 * This means that we actually need to count the number of elements in the
2586 * 'declarations' list in each of the elements.
2588 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2589 &this->declarations
) {
2590 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
2596 /* Allocate storage for the structure fields and process the field
2597 * declarations. As the declarations are processed, try to also convert
2598 * the types to HIR. This ensures that structure definitions embedded in
2599 * other structure definitions are processed.
2601 glsl_struct_field
*const fields
= talloc_array(state
, glsl_struct_field
,
2605 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2606 &this->declarations
) {
2607 const char *type_name
;
2609 decl_list
->type
->specifier
->hir(instructions
, state
);
2611 const glsl_type
*decl_type
=
2612 decl_list
->type
->specifier
->glsl_type(& type_name
, state
);
2614 foreach_list_typed (ast_declaration
, decl
, link
,
2615 &decl_list
->declarations
) {
2616 const struct glsl_type
*const field_type
=
2618 ? process_array_type(decl_type
, decl
->array_size
, state
)
2621 fields
[i
].type
= (field_type
!= NULL
)
2622 ? field_type
: glsl_type::error_type
;
2623 fields
[i
].name
= decl
->identifier
;
2628 assert(i
== decl_count
);
2631 if (this->name
== NULL
) {
2632 static unsigned anon_count
= 1;
2635 snprintf(buf
, sizeof(buf
), "#anon_struct_%04x", anon_count
);
2643 const glsl_type
*t
=
2644 glsl_type::get_record_instance(fields
, decl_count
, name
);
2646 YYLTYPE loc
= this->get_location();
2647 ir_function
*ctor
= t
->generate_constructor();
2648 if (!state
->symbols
->add_type(name
, t
, ctor
)) {
2649 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
2652 const glsl_type
**s
= (const glsl_type
**)
2653 realloc(state
->user_structures
,
2654 sizeof(state
->user_structures
[0]) *
2655 (state
->num_user_structures
+ 1));
2657 s
[state
->num_user_structures
] = t
;
2658 state
->user_structures
= s
;
2659 state
->num_user_structures
++;
2663 /* Structure type definitions do not have r-values.