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/imports.h"
53 #include "main/extensions.h"
54 #include "glsl_symbol_table.h"
55 #include "glsl_parser_extras.h"
57 #include "glsl_types.h"
61 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
63 _mesa_glsl_initialize_variables(instructions
, state
);
64 _mesa_glsl_initialize_functions(instructions
, state
);
66 state
->current_function
= NULL
;
68 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
69 ast
->hir(instructions
, state
);
74 * If a conversion is available, convert one operand to a different type
76 * The \c from \c ir_rvalue is converted "in place".
78 * \param to Type that the operand it to be converted to
79 * \param from Operand that is being converted
80 * \param state GLSL compiler state
83 * If a conversion is possible (or unnecessary), \c true is returned.
84 * Otherwise \c false is returned.
87 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
88 struct _mesa_glsl_parse_state
*state
)
91 if (to
->base_type
== from
->type
->base_type
)
94 /* This conversion was added in GLSL 1.20. If the compilation mode is
95 * GLSL 1.10, the conversion is skipped.
97 if (state
->language_version
< 120)
100 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
102 * "There are no implicit array or structure conversions. For
103 * example, an array of int cannot be implicitly converted to an
104 * array of float. There are no implicit conversions between
105 * signed and unsigned integers."
107 /* FINISHME: The above comment is partially a lie. There is int/uint
108 * FINISHME: conversion for immediate constants.
110 if (!to
->is_float() || !from
->type
->is_numeric())
113 /* Convert to a floating point type with the same number of components
114 * as the original type - i.e. int to float, not int to vec4.
116 to
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, from
->type
->vector_elements
,
117 from
->type
->matrix_columns
);
119 switch (from
->type
->base_type
) {
121 from
= new(ctx
) ir_expression(ir_unop_i2f
, to
, from
, NULL
);
124 from
= new(ctx
) ir_expression(ir_unop_u2f
, to
, from
, NULL
);
127 from
= new(ctx
) ir_expression(ir_unop_b2f
, to
, from
, NULL
);
137 static const struct glsl_type
*
138 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
140 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
142 const glsl_type
*type_a
= value_a
->type
;
143 const glsl_type
*type_b
= value_b
->type
;
145 /* From GLSL 1.50 spec, page 56:
147 * "The arithmetic binary operators add (+), subtract (-),
148 * multiply (*), and divide (/) operate on integer and
149 * floating-point scalars, vectors, and matrices."
151 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
152 _mesa_glsl_error(loc
, state
,
153 "Operands to arithmetic operators must be numeric");
154 return glsl_type::error_type
;
158 /* "If one operand is floating-point based and the other is
159 * not, then the conversions from Section 4.1.10 "Implicit
160 * Conversions" are applied to the non-floating-point-based operand."
162 if (!apply_implicit_conversion(type_a
, value_b
, state
)
163 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
164 _mesa_glsl_error(loc
, state
,
165 "Could not implicitly convert operands to "
166 "arithmetic operator");
167 return glsl_type::error_type
;
169 type_a
= value_a
->type
;
170 type_b
= value_b
->type
;
172 /* "If the operands are integer types, they must both be signed or
175 * From this rule and the preceeding conversion it can be inferred that
176 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
177 * The is_numeric check above already filtered out the case where either
178 * type is not one of these, so now the base types need only be tested for
181 if (type_a
->base_type
!= type_b
->base_type
) {
182 _mesa_glsl_error(loc
, state
,
183 "base type mismatch for arithmetic operator");
184 return glsl_type::error_type
;
187 /* "All arithmetic binary operators result in the same fundamental type
188 * (signed integer, unsigned integer, or floating-point) as the
189 * operands they operate on, after operand type conversion. After
190 * conversion, the following cases are valid
192 * * The two operands are scalars. In this case the operation is
193 * applied, resulting in a scalar."
195 if (type_a
->is_scalar() && type_b
->is_scalar())
198 /* "* One operand is a scalar, and the other is a vector or matrix.
199 * In this case, the scalar operation is applied independently to each
200 * component of the vector or matrix, resulting in the same size
203 if (type_a
->is_scalar()) {
204 if (!type_b
->is_scalar())
206 } else if (type_b
->is_scalar()) {
210 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
211 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
214 assert(!type_a
->is_scalar());
215 assert(!type_b
->is_scalar());
217 /* "* The two operands are vectors of the same size. In this case, the
218 * operation is done component-wise resulting in the same size
221 if (type_a
->is_vector() && type_b
->is_vector()) {
222 if (type_a
== type_b
) {
225 _mesa_glsl_error(loc
, state
,
226 "vector size mismatch for arithmetic operator");
227 return glsl_type::error_type
;
231 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
232 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
233 * <vector, vector> have been handled. At least one of the operands must
234 * be matrix. Further, since there are no integer matrix types, the base
235 * type of both operands must be float.
237 assert(type_a
->is_matrix() || type_b
->is_matrix());
238 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
239 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
241 /* "* The operator is add (+), subtract (-), or divide (/), and the
242 * operands are matrices with the same number of rows and the same
243 * number of columns. In this case, the operation is done component-
244 * wise resulting in the same size matrix."
245 * * The operator is multiply (*), where both operands are matrices or
246 * one operand is a vector and the other a matrix. A right vector
247 * operand is treated as a column vector and a left vector operand as a
248 * row vector. In all these cases, it is required that the number of
249 * columns of the left operand is equal to the number of rows of the
250 * right operand. Then, the multiply (*) operation does a linear
251 * algebraic multiply, yielding an object that has the same number of
252 * rows as the left operand and the same number of columns as the right
253 * operand. Section 5.10 "Vector and Matrix Operations" explains in
254 * more detail how vectors and matrices are operated on."
257 if (type_a
== type_b
)
260 if (type_a
->is_matrix() && type_b
->is_matrix()) {
261 /* Matrix multiply. The columns of A must match the rows of B. Given
262 * the other previously tested constraints, this means the vector type
263 * of a row from A must be the same as the vector type of a column from
266 if (type_a
->row_type() == type_b
->column_type()) {
267 /* The resulting matrix has the number of columns of matrix B and
268 * the number of rows of matrix A. We get the row count of A by
269 * looking at the size of a vector that makes up a column. The
270 * transpose (size of a row) is done for B.
272 const glsl_type
*const type
=
273 glsl_type::get_instance(type_a
->base_type
,
274 type_a
->column_type()->vector_elements
,
275 type_b
->row_type()->vector_elements
);
276 assert(type
!= glsl_type::error_type
);
280 } else if (type_a
->is_matrix()) {
281 /* A is a matrix and B is a column vector. Columns of A must match
282 * rows of B. Given the other previously tested constraints, this
283 * means the vector type of a row from A must be the same as the
284 * vector the type of B.
286 if (type_a
->row_type() == type_b
) {
287 /* The resulting vector has a number of elements equal to
288 * the number of rows of matrix A. */
289 const glsl_type
*const type
=
290 glsl_type::get_instance(type_a
->base_type
,
291 type_a
->column_type()->vector_elements
,
293 assert(type
!= glsl_type::error_type
);
298 assert(type_b
->is_matrix());
300 /* A is a row vector and B is a matrix. Columns of A must match rows
301 * of B. Given the other previously tested constraints, this means
302 * the type of A must be the same as the vector type of a column from
305 if (type_a
== type_b
->column_type()) {
306 /* The resulting vector has a number of elements equal to
307 * the number of columns of matrix B. */
308 const glsl_type
*const type
=
309 glsl_type::get_instance(type_a
->base_type
,
310 type_b
->row_type()->vector_elements
,
312 assert(type
!= glsl_type::error_type
);
318 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
319 return glsl_type::error_type
;
323 /* "All other cases are illegal."
325 _mesa_glsl_error(loc
, state
, "type mismatch");
326 return glsl_type::error_type
;
330 static const struct glsl_type
*
331 unary_arithmetic_result_type(const struct glsl_type
*type
,
332 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
334 /* From GLSL 1.50 spec, page 57:
336 * "The arithmetic unary operators negate (-), post- and pre-increment
337 * and decrement (-- and ++) operate on integer or floating-point
338 * values (including vectors and matrices). All unary operators work
339 * component-wise on their operands. These result with the same type
342 if (!type
->is_numeric()) {
343 _mesa_glsl_error(loc
, state
,
344 "Operands to arithmetic operators must be numeric");
345 return glsl_type::error_type
;
352 static const struct glsl_type
*
353 modulus_result_type(const struct glsl_type
*type_a
,
354 const struct glsl_type
*type_b
,
355 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
357 /* From GLSL 1.50 spec, page 56:
358 * "The operator modulus (%) operates on signed or unsigned integers or
359 * integer vectors. The operand types must both be signed or both be
362 if (!type_a
->is_integer() || !type_b
->is_integer()
363 || (type_a
->base_type
!= type_b
->base_type
)) {
364 _mesa_glsl_error(loc
, state
, "type mismatch");
365 return glsl_type::error_type
;
368 /* "The operands cannot be vectors of differing size. If one operand is
369 * a scalar and the other vector, then the scalar is applied component-
370 * wise to the vector, resulting in the same type as the vector. If both
371 * are vectors of the same size, the result is computed component-wise."
373 if (type_a
->is_vector()) {
374 if (!type_b
->is_vector()
375 || (type_a
->vector_elements
== type_b
->vector_elements
))
380 /* "The operator modulus (%) is not defined for any other data types
381 * (non-integer types)."
383 _mesa_glsl_error(loc
, state
, "type mismatch");
384 return glsl_type::error_type
;
388 static const struct glsl_type
*
389 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
390 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
392 const glsl_type
*type_a
= value_a
->type
;
393 const glsl_type
*type_b
= value_b
->type
;
395 /* From GLSL 1.50 spec, page 56:
396 * "The relational operators greater than (>), less than (<), greater
397 * than or equal (>=), and less than or equal (<=) operate only on
398 * scalar integer and scalar floating-point expressions."
400 if (!type_a
->is_numeric()
401 || !type_b
->is_numeric()
402 || !type_a
->is_scalar()
403 || !type_b
->is_scalar()) {
404 _mesa_glsl_error(loc
, state
,
405 "Operands to relational operators must be scalar and "
407 return glsl_type::error_type
;
410 /* "Either the operands' types must match, or the conversions from
411 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
412 * operand, after which the types must match."
414 if (!apply_implicit_conversion(type_a
, value_b
, state
)
415 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
416 _mesa_glsl_error(loc
, state
,
417 "Could not implicitly convert operands to "
418 "relational operator");
419 return glsl_type::error_type
;
421 type_a
= value_a
->type
;
422 type_b
= value_b
->type
;
424 if (type_a
->base_type
!= type_b
->base_type
) {
425 _mesa_glsl_error(loc
, state
, "base type mismatch");
426 return glsl_type::error_type
;
429 /* "The result is scalar Boolean."
431 return glsl_type::bool_type
;
436 * Validates that a value can be assigned to a location with a specified type
438 * Validates that \c rhs can be assigned to some location. If the types are
439 * not an exact match but an automatic conversion is possible, \c rhs will be
443 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
444 * Otherwise the actual RHS to be assigned will be returned. This may be
445 * \c rhs, or it may be \c rhs after some type conversion.
448 * In addition to being used for assignments, this function is used to
449 * type-check return values.
452 validate_assignment(struct _mesa_glsl_parse_state
*state
,
453 const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
455 const glsl_type
*rhs_type
= rhs
->type
;
457 /* If there is already some error in the RHS, just return it. Anything
458 * else will lead to an avalanche of error message back to the user.
460 if (rhs_type
->is_error())
463 /* If the types are identical, the assignment can trivially proceed.
465 if (rhs_type
== lhs_type
)
468 /* If the array element types are the same and the size of the LHS is zero,
469 * the assignment is okay.
471 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
472 * is handled by ir_dereference::is_lvalue.
474 if (lhs_type
->is_array() && rhs
->type
->is_array()
475 && (lhs_type
->element_type() == rhs
->type
->element_type())
476 && (lhs_type
->array_size() == 0)) {
480 /* Check for implicit conversion in GLSL 1.20 */
481 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
482 rhs_type
= rhs
->type
;
483 if (rhs_type
== lhs_type
)
491 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
492 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
496 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
498 if (!error_emitted
) {
499 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
500 if (!lhs
->is_lvalue()) {
501 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
502 error_emitted
= true;
506 ir_rvalue
*new_rhs
= validate_assignment(state
, lhs
->type
, rhs
);
507 if (new_rhs
== NULL
) {
508 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
512 /* If the LHS array was not declared with a size, it takes it size from
513 * the RHS. If the LHS is an l-value and a whole array, it must be a
514 * dereference of a variable. Any other case would require that the LHS
515 * is either not an l-value or not a whole array.
517 if (lhs
->type
->array_size() == 0) {
518 ir_dereference
*const d
= lhs
->as_dereference();
522 ir_variable
*const var
= d
->variable_referenced();
526 if (var
->max_array_access
>= unsigned(rhs
->type
->array_size())) {
527 /* FINISHME: This should actually log the location of the RHS. */
528 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
530 var
->max_array_access
);
533 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
534 rhs
->type
->array_size());
539 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
540 * but not post_inc) need the converted assigned value as an rvalue
541 * to handle things like:
545 * So we always just store the computed value being assigned to a
546 * temporary and return a deref of that temporary. If the rvalue
547 * ends up not being used, the temp will get copy-propagated out.
549 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
551 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
552 instructions
->push_tail(var
);
553 instructions
->push_tail(new(ctx
) ir_assignment(deref_var
,
556 deref_var
= new(ctx
) ir_dereference_variable(var
);
559 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
, NULL
));
561 return new(ctx
) ir_dereference_variable(var
);
565 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
567 void *ctx
= talloc_parent(lvalue
);
570 /* FINISHME: Give unique names to the temporaries. */
571 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
573 instructions
->push_tail(var
);
574 var
->mode
= ir_var_auto
;
576 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
579 /* Once we've created this temporary, mark it read only so it's no
580 * longer considered an lvalue.
582 var
->read_only
= true;
584 return new(ctx
) ir_dereference_variable(var
);
589 ast_node::hir(exec_list
*instructions
,
590 struct _mesa_glsl_parse_state
*state
)
600 ast_expression::hir(exec_list
*instructions
,
601 struct _mesa_glsl_parse_state
*state
)
604 static const int operations
[AST_NUM_OPERATORS
] = {
605 -1, /* ast_assign doesn't convert to ir_expression. */
606 -1, /* ast_plus doesn't convert to ir_expression. */
630 /* Note: The following block of expression types actually convert
631 * to multiple IR instructions.
633 ir_binop_mul
, /* ast_mul_assign */
634 ir_binop_div
, /* ast_div_assign */
635 ir_binop_mod
, /* ast_mod_assign */
636 ir_binop_add
, /* ast_add_assign */
637 ir_binop_sub
, /* ast_sub_assign */
638 ir_binop_lshift
, /* ast_ls_assign */
639 ir_binop_rshift
, /* ast_rs_assign */
640 ir_binop_bit_and
, /* ast_and_assign */
641 ir_binop_bit_xor
, /* ast_xor_assign */
642 ir_binop_bit_or
, /* ast_or_assign */
644 -1, /* ast_conditional doesn't convert to ir_expression. */
645 ir_binop_add
, /* ast_pre_inc. */
646 ir_binop_sub
, /* ast_pre_dec. */
647 ir_binop_add
, /* ast_post_inc. */
648 ir_binop_sub
, /* ast_post_dec. */
649 -1, /* ast_field_selection doesn't conv to ir_expression. */
650 -1, /* ast_array_index doesn't convert to ir_expression. */
651 -1, /* ast_function_call doesn't conv to ir_expression. */
652 -1, /* ast_identifier doesn't convert to ir_expression. */
653 -1, /* ast_int_constant doesn't convert to ir_expression. */
654 -1, /* ast_uint_constant doesn't conv to ir_expression. */
655 -1, /* ast_float_constant doesn't conv to ir_expression. */
656 -1, /* ast_bool_constant doesn't conv to ir_expression. */
657 -1, /* ast_sequence doesn't convert to ir_expression. */
659 ir_rvalue
*result
= NULL
;
661 const struct glsl_type
*type
= glsl_type::error_type
;
662 bool error_emitted
= false;
665 loc
= this->get_location();
667 switch (this->oper
) {
669 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
670 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
672 result
= do_assignment(instructions
, state
, op
[0], op
[1],
673 this->subexpressions
[0]->get_location());
674 error_emitted
= result
->type
->is_error();
680 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
682 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
684 error_emitted
= type
->is_error();
690 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
692 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
694 error_emitted
= type
->is_error();
696 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
704 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
705 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
707 type
= arithmetic_result_type(op
[0], op
[1],
708 (this->oper
== ast_mul
),
710 error_emitted
= type
->is_error();
712 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
717 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
718 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
720 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
722 assert(operations
[this->oper
] == ir_binop_mod
);
724 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
726 error_emitted
= type
->is_error();
731 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-shift operators");
732 error_emitted
= true;
739 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
740 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
742 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
744 /* The relational operators must either generate an error or result
745 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
747 assert(type
->is_error()
748 || ((type
->base_type
== GLSL_TYPE_BOOL
)
749 && type
->is_scalar()));
751 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
753 error_emitted
= type
->is_error();
758 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
759 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
761 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
763 * "The equality operators equal (==), and not equal (!=)
764 * operate on all types. They result in a scalar Boolean. If
765 * the operand types do not match, then there must be a
766 * conversion from Section 4.1.10 "Implicit Conversions"
767 * applied to one operand that can make them match, in which
768 * case this conversion is done."
770 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
771 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
772 || (op
[0]->type
!= op
[1]->type
)) {
773 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
774 "type", (this->oper
== ast_equal
) ? "==" : "!=");
775 error_emitted
= true;
776 } else if ((state
->language_version
<= 110)
777 && (op
[0]->type
->is_array() || op
[1]->type
->is_array())) {
778 _mesa_glsl_error(& loc
, state
, "array comparisons forbidden in "
780 error_emitted
= true;
783 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
785 type
= glsl_type::bool_type
;
787 assert(result
->type
== glsl_type::bool_type
);
794 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-wise operators");
795 error_emitted
= true;
798 case ast_logic_and
: {
799 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
801 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
802 YYLTYPE loc
= this->subexpressions
[0]->get_location();
804 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
805 operator_string(this->oper
));
806 error_emitted
= true;
809 ir_constant
*op0_const
= op
[0]->constant_expression_value();
811 if (op0_const
->value
.b
[0]) {
812 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
814 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
815 YYLTYPE loc
= this->subexpressions
[1]->get_location();
817 _mesa_glsl_error(& loc
, state
,
818 "RHS of `%s' must be scalar boolean",
819 operator_string(this->oper
));
820 error_emitted
= true;
826 type
= glsl_type::bool_type
;
828 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
831 instructions
->push_tail(tmp
);
833 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
834 instructions
->push_tail(stmt
);
836 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
838 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
839 YYLTYPE loc
= this->subexpressions
[1]->get_location();
841 _mesa_glsl_error(& loc
, state
,
842 "RHS of `%s' must be scalar boolean",
843 operator_string(this->oper
));
844 error_emitted
= true;
847 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
848 ir_assignment
*const then_assign
=
849 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
850 stmt
->then_instructions
.push_tail(then_assign
);
852 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
853 ir_assignment
*const else_assign
=
854 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false), NULL
);
855 stmt
->else_instructions
.push_tail(else_assign
);
857 result
= new(ctx
) ir_dereference_variable(tmp
);
864 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
866 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
867 YYLTYPE loc
= this->subexpressions
[0]->get_location();
869 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
870 operator_string(this->oper
));
871 error_emitted
= true;
874 ir_constant
*op0_const
= op
[0]->constant_expression_value();
876 if (op0_const
->value
.b
[0]) {
879 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
881 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
882 YYLTYPE loc
= this->subexpressions
[1]->get_location();
884 _mesa_glsl_error(& loc
, state
,
885 "RHS of `%s' must be scalar boolean",
886 operator_string(this->oper
));
887 error_emitted
= true;
891 type
= glsl_type::bool_type
;
893 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
896 instructions
->push_tail(tmp
);
898 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
899 instructions
->push_tail(stmt
);
901 op
[1] = this->subexpressions
[1]->hir(&stmt
->else_instructions
, state
);
903 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
904 YYLTYPE loc
= this->subexpressions
[1]->get_location();
906 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
907 operator_string(this->oper
));
908 error_emitted
= true;
911 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
912 ir_assignment
*const then_assign
=
913 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true), NULL
);
914 stmt
->then_instructions
.push_tail(then_assign
);
916 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
917 ir_assignment
*const else_assign
=
918 new(ctx
) ir_assignment(else_deref
, op
[1], NULL
);
919 stmt
->else_instructions
.push_tail(else_assign
);
921 result
= new(ctx
) ir_dereference_variable(tmp
);
928 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
929 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
932 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
934 type
= glsl_type::bool_type
;
938 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
940 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
941 YYLTYPE loc
= this->subexpressions
[0]->get_location();
943 _mesa_glsl_error(& loc
, state
,
944 "operand of `!' must be scalar boolean");
945 error_emitted
= true;
948 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
950 type
= glsl_type::bool_type
;
956 case ast_sub_assign
: {
957 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
958 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
960 type
= arithmetic_result_type(op
[0], op
[1],
961 (this->oper
== ast_mul_assign
),
964 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
967 result
= do_assignment(instructions
, state
,
968 op
[0]->clone(ctx
, NULL
), temp_rhs
,
969 this->subexpressions
[0]->get_location());
971 error_emitted
= (op
[0]->type
->is_error());
973 /* GLSL 1.10 does not allow array assignment. However, we don't have to
974 * explicitly test for this because none of the binary expression
975 * operators allow array operands either.
981 case ast_mod_assign
: {
982 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
983 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
985 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
987 assert(operations
[this->oper
] == ir_binop_mod
);
990 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
993 result
= do_assignment(instructions
, state
,
994 op
[0]->clone(ctx
, NULL
), temp_rhs
,
995 this->subexpressions
[0]->get_location());
997 error_emitted
= type
->is_error();
1003 _mesa_glsl_error(& loc
, state
,
1004 "FINISHME: implement bit-shift assignment operators");
1005 error_emitted
= true;
1008 case ast_and_assign
:
1009 case ast_xor_assign
:
1011 _mesa_glsl_error(& loc
, state
,
1012 "FINISHME: implement logic assignment operators");
1013 error_emitted
= true;
1016 case ast_conditional
: {
1017 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1019 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1021 * "The ternary selection operator (?:). It operates on three
1022 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1023 * first expression, which must result in a scalar Boolean."
1025 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
1026 YYLTYPE loc
= this->subexpressions
[0]->get_location();
1028 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
1029 error_emitted
= true;
1032 /* The :? operator is implemented by generating an anonymous temporary
1033 * followed by an if-statement. The last instruction in each branch of
1034 * the if-statement assigns a value to the anonymous temporary. This
1035 * temporary is the r-value of the expression.
1037 exec_list then_instructions
;
1038 exec_list else_instructions
;
1040 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1041 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1043 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1045 * "The second and third expressions can be any type, as
1046 * long their types match, or there is a conversion in
1047 * Section 4.1.10 "Implicit Conversions" that can be applied
1048 * to one of the expressions to make their types match. This
1049 * resulting matching type is the type of the entire
1052 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1053 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1054 || (op
[1]->type
!= op
[2]->type
)) {
1055 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1057 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
1058 "operator must have matching types.");
1059 error_emitted
= true;
1060 type
= glsl_type::error_type
;
1065 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1066 ir_constant
*then_val
= op
[1]->constant_expression_value();
1067 ir_constant
*else_val
= op
[2]->constant_expression_value();
1069 if (then_instructions
.is_empty()
1070 && else_instructions
.is_empty()
1071 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1072 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1074 ir_variable
*const tmp
=
1075 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1076 instructions
->push_tail(tmp
);
1078 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1079 instructions
->push_tail(stmt
);
1081 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1082 ir_dereference
*const then_deref
=
1083 new(ctx
) ir_dereference_variable(tmp
);
1084 ir_assignment
*const then_assign
=
1085 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
1086 stmt
->then_instructions
.push_tail(then_assign
);
1088 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1089 ir_dereference
*const else_deref
=
1090 new(ctx
) ir_dereference_variable(tmp
);
1091 ir_assignment
*const else_assign
=
1092 new(ctx
) ir_assignment(else_deref
, op
[2], NULL
);
1093 stmt
->else_instructions
.push_tail(else_assign
);
1095 result
= new(ctx
) ir_dereference_variable(tmp
);
1102 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1103 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1104 op
[1] = new(ctx
) ir_constant(1.0f
);
1106 op
[1] = new(ctx
) ir_constant(1);
1108 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1110 ir_rvalue
*temp_rhs
;
1111 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1114 result
= do_assignment(instructions
, state
,
1115 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1116 this->subexpressions
[0]->get_location());
1117 type
= result
->type
;
1118 error_emitted
= op
[0]->type
->is_error();
1123 case ast_post_dec
: {
1124 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1125 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1126 op
[1] = new(ctx
) ir_constant(1.0f
);
1128 op
[1] = new(ctx
) ir_constant(1);
1130 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1132 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1134 ir_rvalue
*temp_rhs
;
1135 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1138 /* Get a temporary of a copy of the lvalue before it's modified.
1139 * This may get thrown away later.
1141 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1143 (void)do_assignment(instructions
, state
,
1144 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1145 this->subexpressions
[0]->get_location());
1147 type
= result
->type
;
1148 error_emitted
= op
[0]->type
->is_error();
1152 case ast_field_selection
:
1153 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1154 type
= result
->type
;
1157 case ast_array_index
: {
1158 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1160 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1161 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1163 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1165 ir_rvalue
*const array
= op
[0];
1167 result
= new(ctx
) ir_dereference_array(op
[0], op
[1]);
1169 /* Do not use op[0] after this point. Use array.
1177 if (!array
->type
->is_array()
1178 && !array
->type
->is_matrix()
1179 && !array
->type
->is_vector()) {
1180 _mesa_glsl_error(& index_loc
, state
,
1181 "cannot dereference non-array / non-matrix / "
1183 error_emitted
= true;
1186 if (!op
[1]->type
->is_integer()) {
1187 _mesa_glsl_error(& index_loc
, state
,
1188 "array index must be integer type");
1189 error_emitted
= true;
1190 } else if (!op
[1]->type
->is_scalar()) {
1191 _mesa_glsl_error(& index_loc
, state
,
1192 "array index must be scalar");
1193 error_emitted
= true;
1196 /* If the array index is a constant expression and the array has a
1197 * declared size, ensure that the access is in-bounds. If the array
1198 * index is not a constant expression, ensure that the array has a
1201 ir_constant
*const const_index
= op
[1]->constant_expression_value();
1202 if (const_index
!= NULL
) {
1203 const int idx
= const_index
->value
.i
[0];
1204 const char *type_name
;
1207 if (array
->type
->is_matrix()) {
1208 type_name
= "matrix";
1209 } else if (array
->type
->is_vector()) {
1210 type_name
= "vector";
1212 type_name
= "array";
1215 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1217 * "It is illegal to declare an array with a size, and then
1218 * later (in the same shader) index the same array with an
1219 * integral constant expression greater than or equal to the
1220 * declared size. It is also illegal to index an array with a
1221 * negative constant expression."
1223 if (array
->type
->is_matrix()) {
1224 if (array
->type
->row_type()->vector_elements
<= idx
) {
1225 bound
= array
->type
->row_type()->vector_elements
;
1227 } else if (array
->type
->is_vector()) {
1228 if (array
->type
->vector_elements
<= idx
) {
1229 bound
= array
->type
->vector_elements
;
1232 if ((array
->type
->array_size() > 0)
1233 && (array
->type
->array_size() <= idx
)) {
1234 bound
= array
->type
->array_size();
1239 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
1241 error_emitted
= true;
1242 } else if (idx
< 0) {
1243 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
1245 error_emitted
= true;
1248 if (array
->type
->is_array()) {
1249 /* If the array is a variable dereference, it dereferences the
1250 * whole array, by definition. Use this to get the variable.
1252 * FINISHME: Should some methods for getting / setting / testing
1253 * FINISHME: array access limits be added to ir_dereference?
1255 ir_variable
*const v
= array
->whole_variable_referenced();
1256 if ((v
!= NULL
) && (unsigned(idx
) > v
->max_array_access
))
1257 v
->max_array_access
= idx
;
1259 } else if (array
->type
->array_size() == 0) {
1260 _mesa_glsl_error(&loc
, state
, "unsized array index must be constant");
1264 result
->type
= glsl_type::error_type
;
1266 type
= result
->type
;
1270 case ast_function_call
:
1271 /* Should *NEVER* get here. ast_function_call should always be handled
1272 * by ast_function_expression::hir.
1277 case ast_identifier
: {
1278 /* ast_identifier can appear several places in a full abstract syntax
1279 * tree. This particular use must be at location specified in the grammar
1280 * as 'variable_identifier'.
1283 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1285 result
= new(ctx
) ir_dereference_variable(var
);
1288 type
= result
->type
;
1290 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1291 this->primary_expression
.identifier
);
1293 error_emitted
= true;
1298 case ast_int_constant
:
1299 type
= glsl_type::int_type
;
1300 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1303 case ast_uint_constant
:
1304 type
= glsl_type::uint_type
;
1305 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1308 case ast_float_constant
:
1309 type
= glsl_type::float_type
;
1310 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1313 case ast_bool_constant
:
1314 type
= glsl_type::bool_type
;
1315 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1318 case ast_sequence
: {
1319 /* It should not be possible to generate a sequence in the AST without
1320 * any expressions in it.
1322 assert(!this->expressions
.is_empty());
1324 /* The r-value of a sequence is the last expression in the sequence. If
1325 * the other expressions in the sequence do not have side-effects (and
1326 * therefore add instructions to the instruction list), they get dropped
1329 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
)
1330 result
= ast
->hir(instructions
, state
);
1332 type
= result
->type
;
1334 /* Any errors should have already been emitted in the loop above.
1336 error_emitted
= true;
1341 if (type
->is_error() && !error_emitted
)
1342 _mesa_glsl_error(& loc
, state
, "type mismatch");
1349 ast_expression_statement::hir(exec_list
*instructions
,
1350 struct _mesa_glsl_parse_state
*state
)
1352 /* It is possible to have expression statements that don't have an
1353 * expression. This is the solitary semicolon:
1355 * for (i = 0; i < 5; i++)
1358 * In this case the expression will be NULL. Test for NULL and don't do
1359 * anything in that case.
1361 if (expression
!= NULL
)
1362 expression
->hir(instructions
, state
);
1364 /* Statements do not have r-values.
1371 ast_compound_statement::hir(exec_list
*instructions
,
1372 struct _mesa_glsl_parse_state
*state
)
1375 state
->symbols
->push_scope();
1377 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1378 ast
->hir(instructions
, state
);
1381 state
->symbols
->pop_scope();
1383 /* Compound statements do not have r-values.
1389 static const glsl_type
*
1390 process_array_type(const glsl_type
*base
, ast_node
*array_size
,
1391 struct _mesa_glsl_parse_state
*state
)
1393 unsigned length
= 0;
1395 /* FINISHME: Reject delcarations of multidimensional arrays. */
1397 if (array_size
!= NULL
) {
1398 exec_list dummy_instructions
;
1399 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1400 YYLTYPE loc
= array_size
->get_location();
1402 /* FINISHME: Verify that the grammar forbids side-effects in array
1403 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1405 assert(dummy_instructions
.is_empty());
1408 if (!ir
->type
->is_integer()) {
1409 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1410 } else if (!ir
->type
->is_scalar()) {
1411 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1413 ir_constant
*const size
= ir
->constant_expression_value();
1416 _mesa_glsl_error(& loc
, state
, "array size must be a "
1417 "constant valued expression");
1418 } else if (size
->value
.i
[0] <= 0) {
1419 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1421 assert(size
->type
== ir
->type
);
1422 length
= size
->value
.u
[0];
1428 return glsl_type::get_array_instance(base
, length
);
1433 ast_type_specifier::glsl_type(const char **name
,
1434 struct _mesa_glsl_parse_state
*state
) const
1436 const struct glsl_type
*type
;
1438 if ((this->type_specifier
== ast_struct
) && (this->type_name
== NULL
)) {
1439 /* FINISHME: Handle annonymous structures. */
1442 type
= state
->symbols
->get_type(this->type_name
);
1443 *name
= this->type_name
;
1445 if (this->is_array
) {
1446 type
= process_array_type(type
, this->array_size
, state
);
1455 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1457 struct _mesa_glsl_parse_state
*state
,
1460 if (qual
->invariant
)
1463 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1464 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
1465 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1471 if (qual
->attribute
&& state
->target
!= vertex_shader
) {
1472 var
->type
= glsl_type::error_type
;
1473 _mesa_glsl_error(loc
, state
,
1474 "`attribute' variables may not be declared in the "
1476 _mesa_glsl_shader_target_name(state
->target
));
1479 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1481 * "The varying qualifier can be used only with the data types
1482 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1485 if (qual
->varying
) {
1486 const glsl_type
*non_array_type
;
1488 if (var
->type
&& var
->type
->is_array())
1489 non_array_type
= var
->type
->fields
.array
;
1491 non_array_type
= var
->type
;
1493 if (non_array_type
&& non_array_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1494 var
->type
= glsl_type::error_type
;
1495 _mesa_glsl_error(loc
, state
,
1496 "varying variables must be of base type float");
1500 /* If there is no qualifier that changes the mode of the variable, leave
1501 * the setting alone.
1503 if (qual
->in
&& qual
->out
)
1504 var
->mode
= ir_var_inout
;
1505 else if (qual
->attribute
|| qual
->in
1506 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1507 var
->mode
= ir_var_in
;
1508 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
1509 var
->mode
= ir_var_out
;
1510 else if (qual
->uniform
)
1511 var
->mode
= ir_var_uniform
;
1514 var
->interpolation
= ir_var_flat
;
1515 else if (qual
->noperspective
)
1516 var
->interpolation
= ir_var_noperspective
;
1518 var
->interpolation
= ir_var_smooth
;
1520 var
->pixel_center_integer
= qual
->pixel_center_integer
;
1521 var
->origin_upper_left
= qual
->origin_upper_left
;
1522 if ((qual
->origin_upper_left
|| qual
->pixel_center_integer
)
1523 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
1524 const char *const qual_string
= (qual
->origin_upper_left
)
1525 ? "origin_upper_left" : "pixel_center_integer";
1527 _mesa_glsl_error(loc
, state
,
1528 "layout qualifier `%s' can only be applied to "
1529 "fragment shader input `gl_FragCoord'",
1533 if (var
->type
->is_array() && (state
->language_version
>= 120)) {
1534 var
->array_lvalue
= true;
1540 ast_declarator_list::hir(exec_list
*instructions
,
1541 struct _mesa_glsl_parse_state
*state
)
1544 const struct glsl_type
*decl_type
;
1545 const char *type_name
= NULL
;
1546 ir_rvalue
*result
= NULL
;
1547 YYLTYPE loc
= this->get_location();
1549 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
1551 * "To ensure that a particular output variable is invariant, it is
1552 * necessary to use the invariant qualifier. It can either be used to
1553 * qualify a previously declared variable as being invariant
1555 * invariant gl_Position; // make existing gl_Position be invariant"
1557 * In these cases the parser will set the 'invariant' flag in the declarator
1558 * list, and the type will be NULL.
1560 if (this->invariant
) {
1561 assert(this->type
== NULL
);
1563 if (state
->current_function
!= NULL
) {
1564 _mesa_glsl_error(& loc
, state
,
1565 "All uses of `invariant' keyword must be at global "
1569 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1570 assert(!decl
->is_array
);
1571 assert(decl
->array_size
== NULL
);
1572 assert(decl
->initializer
== NULL
);
1574 ir_variable
*const earlier
=
1575 state
->symbols
->get_variable(decl
->identifier
);
1576 if (earlier
== NULL
) {
1577 _mesa_glsl_error(& loc
, state
,
1578 "Undeclared variable `%s' cannot be marked "
1579 "invariant\n", decl
->identifier
);
1580 } else if ((state
->target
== vertex_shader
)
1581 && (earlier
->mode
!= ir_var_out
)) {
1582 _mesa_glsl_error(& loc
, state
,
1583 "`%s' cannot be marked invariant, vertex shader "
1584 "outputs only\n", decl
->identifier
);
1585 } else if ((state
->target
== fragment_shader
)
1586 && (earlier
->mode
!= ir_var_in
)) {
1587 _mesa_glsl_error(& loc
, state
,
1588 "`%s' cannot be marked invariant, fragment shader "
1589 "inputs only\n", decl
->identifier
);
1591 earlier
->invariant
= true;
1595 /* Invariant redeclarations do not have r-values.
1600 assert(this->type
!= NULL
);
1601 assert(!this->invariant
);
1603 /* The type specifier may contain a structure definition. Process that
1604 * before any of the variable declarations.
1606 (void) this->type
->specifier
->hir(instructions
, state
);
1608 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1609 if (this->declarations
.is_empty()) {
1610 /* The only valid case where the declaration list can be empty is when
1611 * the declaration is setting the default precision of a built-in type
1612 * (e.g., 'precision highp vec4;').
1615 if (decl_type
!= NULL
) {
1617 _mesa_glsl_error(& loc
, state
, "incomplete declaration");
1621 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1622 const struct glsl_type
*var_type
;
1625 /* FINISHME: Emit a warning if a variable declaration shadows a
1626 * FINISHME: declaration at a higher scope.
1629 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1630 if (type_name
!= NULL
) {
1631 _mesa_glsl_error(& loc
, state
,
1632 "invalid type `%s' in declaration of `%s'",
1633 type_name
, decl
->identifier
);
1635 _mesa_glsl_error(& loc
, state
,
1636 "invalid type in declaration of `%s'",
1642 if (decl
->is_array
) {
1643 var_type
= process_array_type(decl_type
, decl
->array_size
, state
);
1645 var_type
= decl_type
;
1648 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
1650 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1652 * "Global variables can only use the qualifiers const,
1653 * attribute, uni form, or varying. Only one may be
1656 * Local variables can only use the qualifier const."
1658 * This is relaxed in GLSL 1.30.
1660 if (state
->language_version
< 120) {
1661 if (this->type
->qualifier
.out
) {
1662 _mesa_glsl_error(& loc
, state
,
1663 "`out' qualifier in declaration of `%s' "
1664 "only valid for function parameters in GLSL 1.10.",
1667 if (this->type
->qualifier
.in
) {
1668 _mesa_glsl_error(& loc
, state
,
1669 "`in' qualifier in declaration of `%s' "
1670 "only valid for function parameters in GLSL 1.10.",
1673 /* FINISHME: Test for other invalid qualifiers. */
1676 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1679 if (this->type
->qualifier
.invariant
) {
1680 if ((state
->target
== vertex_shader
) && !(var
->mode
== ir_var_out
||
1681 var
->mode
== ir_var_inout
)) {
1682 /* FINISHME: Note that this doesn't work for invariant on
1683 * a function signature outval
1685 _mesa_glsl_error(& loc
, state
,
1686 "`%s' cannot be marked invariant, vertex shader "
1687 "outputs only\n", var
->name
);
1688 } else if ((state
->target
== fragment_shader
) &&
1689 !(var
->mode
== ir_var_in
|| var
->mode
== ir_var_inout
)) {
1690 /* FINISHME: Note that this doesn't work for invariant on
1691 * a function signature inval
1693 _mesa_glsl_error(& loc
, state
,
1694 "`%s' cannot be marked invariant, fragment shader "
1695 "inputs only\n", var
->name
);
1699 if (state
->current_function
!= NULL
) {
1700 const char *mode
= NULL
;
1701 const char *extra
= "";
1703 /* There is no need to check for 'inout' here because the parser will
1704 * only allow that in function parameter lists.
1706 if (this->type
->qualifier
.attribute
) {
1708 } else if (this->type
->qualifier
.uniform
) {
1710 } else if (this->type
->qualifier
.varying
) {
1712 } else if (this->type
->qualifier
.in
) {
1714 extra
= " or in function parameter list";
1715 } else if (this->type
->qualifier
.out
) {
1717 extra
= " or in function parameter list";
1721 _mesa_glsl_error(& loc
, state
,
1722 "%s variable `%s' must be declared at "
1724 mode
, var
->name
, extra
);
1726 } else if (var
->mode
== ir_var_in
) {
1727 if (state
->target
== vertex_shader
) {
1728 bool error_emitted
= false;
1730 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1732 * "Vertex shader inputs can only be float, floating-point
1733 * vectors, matrices, signed and unsigned integers and integer
1734 * vectors. Vertex shader inputs can also form arrays of these
1735 * types, but not structures."
1737 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1739 * "Vertex shader inputs can only be float, floating-point
1740 * vectors, matrices, signed and unsigned integers and integer
1741 * vectors. They cannot be arrays or structures."
1743 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1745 * "The attribute qualifier can be used only with float,
1746 * floating-point vectors, and matrices. Attribute variables
1747 * cannot be declared as arrays or structures."
1749 const glsl_type
*check_type
= var
->type
->is_array()
1750 ? var
->type
->fields
.array
: var
->type
;
1752 switch (check_type
->base_type
) {
1753 case GLSL_TYPE_FLOAT
:
1755 case GLSL_TYPE_UINT
:
1757 if (state
->language_version
> 120)
1761 _mesa_glsl_error(& loc
, state
,
1762 "vertex shader input / attribute cannot have "
1764 var
->type
->is_array() ? "array of " : "",
1766 error_emitted
= true;
1769 if (!error_emitted
&& (state
->language_version
<= 130)
1770 && var
->type
->is_array()) {
1771 _mesa_glsl_error(& loc
, state
,
1772 "vertex shader input / attribute cannot have "
1774 error_emitted
= true;
1779 /* Process the initializer and add its instructions to a temporary
1780 * list. This list will be added to the instruction stream (below) after
1781 * the declaration is added. This is done because in some cases (such as
1782 * redeclarations) the declaration may not actually be added to the
1783 * instruction stream.
1785 exec_list initializer_instructions
;
1786 if (decl
->initializer
!= NULL
) {
1787 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
1789 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1791 * "All uniform variables are read-only and are initialized either
1792 * directly by an application via API commands, or indirectly by
1795 if ((state
->language_version
<= 110)
1796 && (var
->mode
== ir_var_uniform
)) {
1797 _mesa_glsl_error(& initializer_loc
, state
,
1798 "cannot initialize uniforms in GLSL 1.10");
1801 if (var
->type
->is_sampler()) {
1802 _mesa_glsl_error(& initializer_loc
, state
,
1803 "cannot initialize samplers");
1806 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1807 _mesa_glsl_error(& initializer_loc
, state
,
1808 "cannot initialize %s shader input / %s",
1809 _mesa_glsl_shader_target_name(state
->target
),
1810 (state
->target
== vertex_shader
)
1811 ? "attribute" : "varying");
1814 ir_dereference
*const lhs
= new(ctx
) ir_dereference_variable(var
);
1815 ir_rvalue
*rhs
= decl
->initializer
->hir(&initializer_instructions
,
1818 /* Calculate the constant value if this is a const or uniform
1821 if (this->type
->qualifier
.constant
|| this->type
->qualifier
.uniform
) {
1822 ir_rvalue
*new_rhs
= validate_assignment(state
, var
->type
, rhs
);
1823 if (new_rhs
!= NULL
) {
1826 _mesa_glsl_error(&initializer_loc
, state
,
1827 "initializer of type %s cannot be assigned to "
1828 "variable of type %s",
1829 rhs
->type
->name
, var
->type
->name
);
1832 ir_constant
*constant_value
= rhs
->constant_expression_value();
1833 if (!constant_value
) {
1834 _mesa_glsl_error(& initializer_loc
, state
,
1835 "initializer of %s variable `%s' must be a "
1836 "constant expression",
1837 (this->type
->qualifier
.constant
)
1838 ? "const" : "uniform",
1841 rhs
= constant_value
;
1842 var
->constant_value
= constant_value
;
1846 if (rhs
&& !rhs
->type
->is_error()) {
1847 bool temp
= var
->read_only
;
1848 if (this->type
->qualifier
.constant
)
1849 var
->read_only
= false;
1851 /* Never emit code to initialize a uniform.
1853 if (!this->type
->qualifier
.uniform
)
1854 result
= do_assignment(&initializer_instructions
, state
,
1856 this->get_location());
1857 var
->read_only
= temp
;
1861 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1863 * "It is an error to write to a const variable outside of
1864 * its declaration, so they must be initialized when
1867 if (this->type
->qualifier
.constant
&& decl
->initializer
== NULL
) {
1868 _mesa_glsl_error(& loc
, state
,
1869 "const declaration of `%s' must be initialized");
1872 /* Attempt to add the variable to the symbol table. If this fails, it
1873 * means the variable has already been declared at this scope. Arrays
1874 * fudge this rule a little bit.
1876 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1878 * "It is legal to declare an array without a size and then
1879 * later re-declare the same name as an array of the same
1880 * type and specify a size."
1882 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
1883 ir_variable
*const earlier
=
1884 state
->symbols
->get_variable(decl
->identifier
);
1886 if ((earlier
!= NULL
)
1887 && (earlier
->type
->array_size() == 0)
1888 && var
->type
->is_array()
1889 && (var
->type
->element_type() == earlier
->type
->element_type())) {
1890 /* FINISHME: This doesn't match the qualifiers on the two
1891 * FINISHME: declarations. It's not 100% clear whether this is
1892 * FINISHME: required or not.
1895 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1897 * "The size [of gl_TexCoord] can be at most
1898 * gl_MaxTextureCoords."
1900 const unsigned size
= unsigned(var
->type
->array_size());
1901 if ((strcmp("gl_TexCoord", var
->name
) == 0)
1902 && (size
> state
->Const
.MaxTextureCoords
)) {
1903 YYLTYPE loc
= this->get_location();
1905 _mesa_glsl_error(& loc
, state
, "`gl_TexCoord' array size cannot "
1906 "be larger than gl_MaxTextureCoords (%u)\n",
1907 state
->Const
.MaxTextureCoords
);
1908 } else if ((size
> 0) && (size
<= earlier
->max_array_access
)) {
1909 YYLTYPE loc
= this->get_location();
1911 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
1913 earlier
->max_array_access
);
1916 earlier
->type
= var
->type
;
1919 } else if (state
->extensions
->ARB_fragment_coord_conventions
&&
1920 (earlier
!= NULL
) &&
1921 (strcmp(var
->name
, "gl_FragCoord") == 0) &&
1922 earlier
->type
== var
->type
&&
1923 earlier
->mode
== var
->mode
) {
1924 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
1927 earlier
->origin_upper_left
= var
->origin_upper_left
;
1928 earlier
->pixel_center_integer
= var
->pixel_center_integer
;
1930 YYLTYPE loc
= this->get_location();
1932 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
1939 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1941 * "Identifiers starting with "gl_" are reserved for use by
1942 * OpenGL, and may not be declared in a shader as either a
1943 * variable or a function."
1945 if (strncmp(decl
->identifier
, "gl_", 3) == 0) {
1946 /* FINISHME: This should only trigger if we're not redefining
1947 * FINISHME: a builtin (to add a qualifier, for example).
1949 _mesa_glsl_error(& loc
, state
,
1950 "identifier `%s' uses reserved `gl_' prefix",
1954 /* Push the variable declaration to the top. It means that all
1955 * the variable declarations will appear in a funny
1956 * last-to-first order, but otherwise we run into trouble if a
1957 * function is prototyped, a global var is decled, then the
1958 * function is defined with usage of the global var. See
1959 * glslparsertest's CorrectModule.frag.
1961 instructions
->push_head(var
);
1962 instructions
->append_list(&initializer_instructions
);
1964 /* Add the variable to the symbol table after processing the initializer.
1965 * This differs from most C-like languages, but it follows the GLSL
1966 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1969 * "Within a declaration, the scope of a name starts immediately
1970 * after the initializer if present or immediately after the name
1971 * being declared if not."
1973 const bool added_variable
=
1974 state
->symbols
->add_variable(var
->name
, var
);
1975 assert(added_variable
);
1979 /* Generally, variable declarations do not have r-values. However,
1980 * one is used for the declaration in
1982 * while (bool b = some_condition()) {
1986 * so we return the rvalue from the last seen declaration here.
1993 ast_parameter_declarator::hir(exec_list
*instructions
,
1994 struct _mesa_glsl_parse_state
*state
)
1997 const struct glsl_type
*type
;
1998 const char *name
= NULL
;
1999 YYLTYPE loc
= this->get_location();
2001 type
= this->type
->specifier
->glsl_type(& name
, state
);
2005 _mesa_glsl_error(& loc
, state
,
2006 "invalid type `%s' in declaration of `%s'",
2007 name
, this->identifier
);
2009 _mesa_glsl_error(& loc
, state
,
2010 "invalid type in declaration of `%s'",
2014 type
= glsl_type::error_type
;
2017 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2019 * "Functions that accept no input arguments need not use void in the
2020 * argument list because prototypes (or definitions) are required and
2021 * therefore there is no ambiguity when an empty argument list "( )" is
2022 * declared. The idiom "(void)" as a parameter list is provided for
2025 * Placing this check here prevents a void parameter being set up
2026 * for a function, which avoids tripping up checks for main taking
2027 * parameters and lookups of an unnamed symbol.
2029 if (type
->is_void()) {
2030 if (this->identifier
!= NULL
)
2031 _mesa_glsl_error(& loc
, state
,
2032 "named parameter cannot have type `void'");
2038 if (formal_parameter
&& (this->identifier
== NULL
)) {
2039 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
2044 ir_variable
*var
= new(ctx
) ir_variable(type
, this->identifier
, ir_var_in
);
2046 /* FINISHME: Handle array declarations. Note that this requires
2047 * FINISHME: complete handling of constant expressions.
2050 /* Apply any specified qualifiers to the parameter declaration. Note that
2051 * for function parameters the default mode is 'in'.
2053 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
2055 instructions
->push_tail(var
);
2057 /* Parameter declarations do not have r-values.
2064 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
2066 exec_list
*ir_parameters
,
2067 _mesa_glsl_parse_state
*state
)
2069 ast_parameter_declarator
*void_param
= NULL
;
2072 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
2073 param
->formal_parameter
= formal
;
2074 param
->hir(ir_parameters
, state
);
2082 if ((void_param
!= NULL
) && (count
> 1)) {
2083 YYLTYPE loc
= void_param
->get_location();
2085 _mesa_glsl_error(& loc
, state
,
2086 "`void' parameter must be only parameter");
2092 ast_function::hir(exec_list
*instructions
,
2093 struct _mesa_glsl_parse_state
*state
)
2096 ir_function
*f
= NULL
;
2097 ir_function_signature
*sig
= NULL
;
2098 exec_list hir_parameters
;
2100 const char *const name
= identifier
;
2102 /* Convert the list of function parameters to HIR now so that they can be
2103 * used below to compare this function's signature with previously seen
2104 * signatures for functions with the same name.
2106 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
2108 & hir_parameters
, state
);
2110 const char *return_type_name
;
2111 const glsl_type
*return_type
=
2112 this->return_type
->specifier
->glsl_type(& return_type_name
, state
);
2114 assert(return_type
!= NULL
);
2116 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2117 * "No qualifier is allowed on the return type of a function."
2119 if (this->return_type
->has_qualifiers()) {
2120 YYLTYPE loc
= this->get_location();
2121 _mesa_glsl_error(& loc
, state
,
2122 "function `%s' return type has qualifiers", name
);
2125 /* Verify that this function's signature either doesn't match a previously
2126 * seen signature for a function with the same name, or, if a match is found,
2127 * that the previously seen signature does not have an associated definition.
2129 f
= state
->symbols
->get_function(name
);
2131 sig
= f
->exact_matching_signature(&hir_parameters
);
2133 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
2134 if (badvar
!= NULL
) {
2135 YYLTYPE loc
= this->get_location();
2137 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
2138 "qualifiers don't match prototype", name
, badvar
);
2141 if (sig
->return_type
!= return_type
) {
2142 YYLTYPE loc
= this->get_location();
2144 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
2145 "match prototype", name
);
2148 if (is_definition
&& sig
->is_defined
) {
2149 YYLTYPE loc
= this->get_location();
2151 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
2155 } else if (state
->symbols
->name_declared_this_scope(name
)) {
2156 /* This function name shadows a non-function use of the same name.
2158 YYLTYPE loc
= this->get_location();
2160 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
2161 "non-function", name
);
2164 f
= new(ctx
) ir_function(name
);
2165 state
->symbols
->add_function(f
->name
, f
);
2167 /* Emit the new function header */
2168 instructions
->push_tail(f
);
2171 /* Verify the return type of main() */
2172 if (strcmp(name
, "main") == 0) {
2173 if (! return_type
->is_void()) {
2174 YYLTYPE loc
= this->get_location();
2176 _mesa_glsl_error(& loc
, state
, "main() must return void");
2179 if (!hir_parameters
.is_empty()) {
2180 YYLTYPE loc
= this->get_location();
2182 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
2186 /* Finish storing the information about this new function in its signature.
2189 sig
= new(ctx
) ir_function_signature(return_type
);
2190 f
->add_signature(sig
);
2193 sig
->replace_parameters(&hir_parameters
);
2196 /* Function declarations (prototypes) do not have r-values.
2203 ast_function_definition::hir(exec_list
*instructions
,
2204 struct _mesa_glsl_parse_state
*state
)
2206 prototype
->is_definition
= true;
2207 prototype
->hir(instructions
, state
);
2209 ir_function_signature
*signature
= prototype
->signature
;
2211 assert(state
->current_function
== NULL
);
2212 state
->current_function
= signature
;
2213 state
->found_return
= false;
2215 /* Duplicate parameters declared in the prototype as concrete variables.
2216 * Add these to the symbol table.
2218 state
->symbols
->push_scope();
2219 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
2220 ir_variable
*const var
= ((ir_instruction
*) iter
.get())->as_variable();
2222 assert(var
!= NULL
);
2224 /* The only way a parameter would "exist" is if two parameters have
2227 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
2228 YYLTYPE loc
= this->get_location();
2230 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
2232 state
->symbols
->add_variable(var
->name
, var
);
2236 /* Convert the body of the function to HIR. */
2237 this->body
->hir(&signature
->body
, state
);
2238 signature
->is_defined
= true;
2240 state
->symbols
->pop_scope();
2242 assert(state
->current_function
== signature
);
2243 state
->current_function
= NULL
;
2245 if (!signature
->return_type
->is_void() && !state
->found_return
) {
2246 YYLTYPE loc
= this->get_location();
2247 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
2248 "%s, but no return statement",
2249 signature
->function_name(),
2250 signature
->return_type
->name
);
2253 /* Function definitions do not have r-values.
2260 ast_jump_statement::hir(exec_list
*instructions
,
2261 struct _mesa_glsl_parse_state
*state
)
2268 assert(state
->current_function
);
2270 if (opt_return_value
) {
2271 if (state
->current_function
->return_type
->base_type
==
2273 YYLTYPE loc
= this->get_location();
2275 _mesa_glsl_error(& loc
, state
,
2276 "`return` with a value, in function `%s' "
2278 state
->current_function
->function_name());
2281 ir_expression
*const ret
= (ir_expression
*)
2282 opt_return_value
->hir(instructions
, state
);
2283 assert(ret
!= NULL
);
2285 /* Implicit conversions are not allowed for return values. */
2286 if (state
->current_function
->return_type
!= ret
->type
) {
2287 YYLTYPE loc
= this->get_location();
2289 _mesa_glsl_error(& loc
, state
,
2290 "`return' with wrong type %s, in function `%s' "
2293 state
->current_function
->function_name(),
2294 state
->current_function
->return_type
->name
);
2297 inst
= new(ctx
) ir_return(ret
);
2299 if (state
->current_function
->return_type
->base_type
!=
2301 YYLTYPE loc
= this->get_location();
2303 _mesa_glsl_error(& loc
, state
,
2304 "`return' with no value, in function %s returning "
2306 state
->current_function
->function_name());
2308 inst
= new(ctx
) ir_return
;
2311 state
->found_return
= true;
2312 instructions
->push_tail(inst
);
2317 if (state
->target
!= fragment_shader
) {
2318 YYLTYPE loc
= this->get_location();
2320 _mesa_glsl_error(& loc
, state
,
2321 "`discard' may only appear in a fragment shader");
2323 instructions
->push_tail(new(ctx
) ir_discard
);
2328 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2329 * FINISHME: and they use a different IR instruction for 'break'.
2331 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2332 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2335 if (state
->loop_or_switch_nesting
== NULL
) {
2336 YYLTYPE loc
= this->get_location();
2338 _mesa_glsl_error(& loc
, state
,
2339 "`%s' may only appear in a loop",
2340 (mode
== ast_break
) ? "break" : "continue");
2342 ir_loop
*const loop
= state
->loop_or_switch_nesting
->as_loop();
2344 /* Inline the for loop expression again, since we don't know
2345 * where near the end of the loop body the normal copy of it
2346 * is going to be placed.
2348 if (mode
== ast_continue
&&
2349 state
->loop_or_switch_nesting_ast
->rest_expression
) {
2350 state
->loop_or_switch_nesting_ast
->rest_expression
->hir(instructions
,
2355 ir_loop_jump
*const jump
=
2356 new(ctx
) ir_loop_jump((mode
== ast_break
)
2357 ? ir_loop_jump::jump_break
2358 : ir_loop_jump::jump_continue
);
2359 instructions
->push_tail(jump
);
2366 /* Jump instructions do not have r-values.
2373 ast_selection_statement::hir(exec_list
*instructions
,
2374 struct _mesa_glsl_parse_state
*state
)
2378 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
2380 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2382 * "Any expression whose type evaluates to a Boolean can be used as the
2383 * conditional expression bool-expression. Vector types are not accepted
2384 * as the expression to if."
2386 * The checks are separated so that higher quality diagnostics can be
2387 * generated for cases where both rules are violated.
2389 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
2390 YYLTYPE loc
= this->condition
->get_location();
2392 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
2396 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
2398 if (then_statement
!= NULL
)
2399 then_statement
->hir(& stmt
->then_instructions
, state
);
2401 if (else_statement
!= NULL
)
2402 else_statement
->hir(& stmt
->else_instructions
, state
);
2404 instructions
->push_tail(stmt
);
2406 /* if-statements do not have r-values.
2413 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
2414 struct _mesa_glsl_parse_state
*state
)
2418 if (condition
!= NULL
) {
2419 ir_rvalue
*const cond
=
2420 condition
->hir(& stmt
->body_instructions
, state
);
2423 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
2424 YYLTYPE loc
= condition
->get_location();
2426 _mesa_glsl_error(& loc
, state
,
2427 "loop condition must be scalar boolean");
2429 /* As the first code in the loop body, generate a block that looks
2430 * like 'if (!condition) break;' as the loop termination condition.
2432 ir_rvalue
*const not_cond
=
2433 new(ctx
) ir_expression(ir_unop_logic_not
, glsl_type::bool_type
, cond
,
2436 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
2438 ir_jump
*const break_stmt
=
2439 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
2441 if_stmt
->then_instructions
.push_tail(break_stmt
);
2442 stmt
->body_instructions
.push_tail(if_stmt
);
2449 ast_iteration_statement::hir(exec_list
*instructions
,
2450 struct _mesa_glsl_parse_state
*state
)
2454 /* For-loops and while-loops start a new scope, but do-while loops do not.
2456 if (mode
!= ast_do_while
)
2457 state
->symbols
->push_scope();
2459 if (init_statement
!= NULL
)
2460 init_statement
->hir(instructions
, state
);
2462 ir_loop
*const stmt
= new(ctx
) ir_loop();
2463 instructions
->push_tail(stmt
);
2465 /* Track the current loop and / or switch-statement nesting.
2467 ir_instruction
*const nesting
= state
->loop_or_switch_nesting
;
2468 ast_iteration_statement
*nesting_ast
= state
->loop_or_switch_nesting_ast
;
2470 state
->loop_or_switch_nesting
= stmt
;
2471 state
->loop_or_switch_nesting_ast
= this;
2473 if (mode
!= ast_do_while
)
2474 condition_to_hir(stmt
, state
);
2477 body
->hir(& stmt
->body_instructions
, state
);
2479 if (rest_expression
!= NULL
)
2480 rest_expression
->hir(& stmt
->body_instructions
, state
);
2482 if (mode
== ast_do_while
)
2483 condition_to_hir(stmt
, state
);
2485 if (mode
!= ast_do_while
)
2486 state
->symbols
->pop_scope();
2488 /* Restore previous nesting before returning.
2490 state
->loop_or_switch_nesting
= nesting
;
2491 state
->loop_or_switch_nesting_ast
= nesting_ast
;
2493 /* Loops do not have r-values.
2500 ast_type_specifier::hir(exec_list
*instructions
,
2501 struct _mesa_glsl_parse_state
*state
)
2503 if (this->structure
!= NULL
)
2504 return this->structure
->hir(instructions
, state
);
2511 ast_struct_specifier::hir(exec_list
*instructions
,
2512 struct _mesa_glsl_parse_state
*state
)
2514 unsigned decl_count
= 0;
2516 /* Make an initial pass over the list of structure fields to determine how
2517 * many there are. Each element in this list is an ast_declarator_list.
2518 * This means that we actually need to count the number of elements in the
2519 * 'declarations' list in each of the elements.
2521 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2522 &this->declarations
) {
2523 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
2529 /* Allocate storage for the structure fields and process the field
2530 * declarations. As the declarations are processed, try to also convert
2531 * the types to HIR. This ensures that structure definitions embedded in
2532 * other structure definitions are processed.
2534 glsl_struct_field
*const fields
= talloc_array(state
, glsl_struct_field
,
2538 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2539 &this->declarations
) {
2540 const char *type_name
;
2542 decl_list
->type
->specifier
->hir(instructions
, state
);
2544 const glsl_type
*decl_type
=
2545 decl_list
->type
->specifier
->glsl_type(& type_name
, state
);
2547 foreach_list_typed (ast_declaration
, decl
, link
,
2548 &decl_list
->declarations
) {
2549 const struct glsl_type
*const field_type
=
2551 ? process_array_type(decl_type
, decl
->array_size
, state
)
2554 fields
[i
].type
= (field_type
!= NULL
)
2555 ? field_type
: glsl_type::error_type
;
2556 fields
[i
].name
= decl
->identifier
;
2561 assert(i
== decl_count
);
2564 if (this->name
== NULL
) {
2565 static unsigned anon_count
= 1;
2568 snprintf(buf
, sizeof(buf
), "#anon_struct_%04x", anon_count
);
2576 const glsl_type
*t
=
2577 glsl_type::get_record_instance(fields
, decl_count
, name
);
2579 YYLTYPE loc
= this->get_location();
2580 if (!state
->symbols
->add_type(name
, t
)) {
2581 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
2583 /* This logic is a bit tricky. It is an error to declare a structure at
2584 * global scope if there is also a function with the same name.
2586 if ((state
->current_function
== NULL
)
2587 && (state
->symbols
->get_function(name
) != NULL
)) {
2588 _mesa_glsl_error(& loc
, state
, "name `%s' previously defined", name
);
2590 t
->generate_constructor(state
->symbols
);
2593 const glsl_type
**s
= (const glsl_type
**)
2594 realloc(state
->user_structures
,
2595 sizeof(state
->user_structures
[0]) *
2596 (state
->num_user_structures
+ 1));
2598 s
[state
->num_user_structures
] = t
;
2599 state
->user_structures
= s
;
2600 state
->num_user_structures
++;
2604 /* Structure type definitions do not have r-values.