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 "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_constructors(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
)
90 if (to
->base_type
== from
->type
->base_type
)
93 /* This conversion was added in GLSL 1.20. If the compilation mode is
94 * GLSL 1.10, the conversion is skipped.
96 if (state
->language_version
< 120)
99 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
101 * "There are no implicit array or structure conversions. For
102 * example, an array of int cannot be implicitly converted to an
103 * array of float. There are no implicit conversions between
104 * signed and unsigned integers."
106 /* FINISHME: The above comment is partially a lie. There is int/uint
107 * FINISHME: conversion for immediate constants.
109 if (!to
->is_float() || !from
->type
->is_numeric())
112 switch (from
->type
->base_type
) {
114 from
= new ir_expression(ir_unop_i2f
, to
, from
, NULL
);
117 from
= new ir_expression(ir_unop_u2f
, to
, from
, NULL
);
120 from
= new ir_expression(ir_unop_b2f
, to
, from
, NULL
);
130 static const struct glsl_type
*
131 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
133 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
135 const glsl_type
*type_a
= value_a
->type
;
136 const glsl_type
*type_b
= value_b
->type
;
138 /* From GLSL 1.50 spec, page 56:
140 * "The arithmetic binary operators add (+), subtract (-),
141 * multiply (*), and divide (/) operate on integer and
142 * floating-point scalars, vectors, and matrices."
144 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
145 _mesa_glsl_error(loc
, state
,
146 "Operands to arithmetic operators must be numeric");
147 return glsl_type::error_type
;
151 /* "If one operand is floating-point based and the other is
152 * not, then the conversions from Section 4.1.10 "Implicit
153 * Conversions" are applied to the non-floating-point-based operand."
155 if (!apply_implicit_conversion(type_a
, value_b
, state
)
156 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
157 _mesa_glsl_error(loc
, state
,
158 "Could not implicitly convert operands to "
159 "arithmetic operator");
160 return glsl_type::error_type
;
162 type_a
= value_a
->type
;
163 type_b
= value_b
->type
;
165 /* "If the operands are integer types, they must both be signed or
168 * From this rule and the preceeding conversion it can be inferred that
169 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
170 * The is_numeric check above already filtered out the case where either
171 * type is not one of these, so now the base types need only be tested for
174 if (type_a
->base_type
!= type_b
->base_type
) {
175 _mesa_glsl_error(loc
, state
,
176 "base type mismatch for arithmetic operator");
177 return glsl_type::error_type
;
180 /* "All arithmetic binary operators result in the same fundamental type
181 * (signed integer, unsigned integer, or floating-point) as the
182 * operands they operate on, after operand type conversion. After
183 * conversion, the following cases are valid
185 * * The two operands are scalars. In this case the operation is
186 * applied, resulting in a scalar."
188 if (type_a
->is_scalar() && type_b
->is_scalar())
191 /* "* One operand is a scalar, and the other is a vector or matrix.
192 * In this case, the scalar operation is applied independently to each
193 * component of the vector or matrix, resulting in the same size
196 if (type_a
->is_scalar()) {
197 if (!type_b
->is_scalar())
199 } else if (type_b
->is_scalar()) {
203 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
204 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
207 assert(!type_a
->is_scalar());
208 assert(!type_b
->is_scalar());
210 /* "* The two operands are vectors of the same size. In this case, the
211 * operation is done component-wise resulting in the same size
214 if (type_a
->is_vector() && type_b
->is_vector()) {
215 if (type_a
== type_b
) {
218 _mesa_glsl_error(loc
, state
,
219 "vector size mismatch for arithmetic operator");
220 return glsl_type::error_type
;
224 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
225 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
226 * <vector, vector> have been handled. At least one of the operands must
227 * be matrix. Further, since there are no integer matrix types, the base
228 * type of both operands must be float.
230 assert(type_a
->is_matrix() || type_b
->is_matrix());
231 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
232 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
234 /* "* The operator is add (+), subtract (-), or divide (/), and the
235 * operands are matrices with the same number of rows and the same
236 * number of columns. In this case, the operation is done component-
237 * wise resulting in the same size matrix."
238 * * The operator is multiply (*), where both operands are matrices or
239 * one operand is a vector and the other a matrix. A right vector
240 * operand is treated as a column vector and a left vector operand as a
241 * row vector. In all these cases, it is required that the number of
242 * columns of the left operand is equal to the number of rows of the
243 * right operand. Then, the multiply (*) operation does a linear
244 * algebraic multiply, yielding an object that has the same number of
245 * rows as the left operand and the same number of columns as the right
246 * operand. Section 5.10 "Vector and Matrix Operations" explains in
247 * more detail how vectors and matrices are operated on."
250 if (type_a
== type_b
)
253 if (type_a
->is_matrix() && type_b
->is_matrix()) {
254 /* Matrix multiply. The columns of A must match the rows of B. Given
255 * the other previously tested constraints, this means the vector type
256 * of a row from A must be the same as the vector type of a column from
259 if (type_a
->row_type() == type_b
->column_type()) {
260 /* The resulting matrix has the number of columns of matrix B and
261 * the number of rows of matrix A. We get the row count of A by
262 * looking at the size of a vector that makes up a column. The
263 * transpose (size of a row) is done for B.
265 const glsl_type
*const type
=
266 glsl_type::get_instance(type_a
->base_type
,
267 type_a
->column_type()->vector_elements
,
268 type_b
->row_type()->vector_elements
);
269 assert(type
!= glsl_type::error_type
);
273 } else if (type_a
->is_matrix()) {
274 /* A is a matrix and B is a column vector. Columns of A must match
275 * rows of B. Given the other previously tested constraints, this
276 * means the vector type of a row from A must be the same as the
277 * vector the type of B.
279 if (type_a
->row_type() == type_b
)
282 assert(type_b
->is_matrix());
284 /* A is a row vector and B is a matrix. Columns of A must match rows
285 * of B. Given the other previously tested constraints, this means
286 * the type of A must be the same as the vector type of a column from
289 if (type_a
== type_b
->column_type())
293 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
294 return glsl_type::error_type
;
298 /* "All other cases are illegal."
300 _mesa_glsl_error(loc
, state
, "type mismatch");
301 return glsl_type::error_type
;
305 static const struct glsl_type
*
306 unary_arithmetic_result_type(const struct glsl_type
*type
,
307 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
309 /* From GLSL 1.50 spec, page 57:
311 * "The arithmetic unary operators negate (-), post- and pre-increment
312 * and decrement (-- and ++) operate on integer or floating-point
313 * values (including vectors and matrices). All unary operators work
314 * component-wise on their operands. These result with the same type
317 if (!type
->is_numeric()) {
318 _mesa_glsl_error(loc
, state
,
319 "Operands to arithmetic operators must be numeric");
320 return glsl_type::error_type
;
327 static const struct glsl_type
*
328 modulus_result_type(const struct glsl_type
*type_a
,
329 const struct glsl_type
*type_b
,
330 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
332 /* From GLSL 1.50 spec, page 56:
333 * "The operator modulus (%) operates on signed or unsigned integers or
334 * integer vectors. The operand types must both be signed or both be
337 if (!type_a
->is_integer() || !type_b
->is_integer()
338 || (type_a
->base_type
!= type_b
->base_type
)) {
339 _mesa_glsl_error(loc
, state
, "type mismatch");
340 return glsl_type::error_type
;
343 /* "The operands cannot be vectors of differing size. If one operand is
344 * a scalar and the other vector, then the scalar is applied component-
345 * wise to the vector, resulting in the same type as the vector. If both
346 * are vectors of the same size, the result is computed component-wise."
348 if (type_a
->is_vector()) {
349 if (!type_b
->is_vector()
350 || (type_a
->vector_elements
== type_b
->vector_elements
))
355 /* "The operator modulus (%) is not defined for any other data types
356 * (non-integer types)."
358 _mesa_glsl_error(loc
, state
, "type mismatch");
359 return glsl_type::error_type
;
363 static const struct glsl_type
*
364 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
365 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
367 const glsl_type
*type_a
= value_a
->type
;
368 const glsl_type
*type_b
= value_b
->type
;
370 /* From GLSL 1.50 spec, page 56:
371 * "The relational operators greater than (>), less than (<), greater
372 * than or equal (>=), and less than or equal (<=) operate only on
373 * scalar integer and scalar floating-point expressions."
375 if (!type_a
->is_numeric()
376 || !type_b
->is_numeric()
377 || !type_a
->is_scalar()
378 || !type_b
->is_scalar()) {
379 _mesa_glsl_error(loc
, state
,
380 "Operands to relational operators must be scalar and "
382 return glsl_type::error_type
;
385 /* "Either the operands' types must match, or the conversions from
386 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
387 * operand, after which the types must match."
389 if (!apply_implicit_conversion(type_a
, value_b
, state
)
390 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
391 _mesa_glsl_error(loc
, state
,
392 "Could not implicitly convert operands to "
393 "relational operator");
394 return glsl_type::error_type
;
396 type_a
= value_a
->type
;
397 type_b
= value_b
->type
;
399 if (type_a
->base_type
!= type_b
->base_type
) {
400 _mesa_glsl_error(loc
, state
, "base type mismatch");
401 return glsl_type::error_type
;
404 /* "The result is scalar Boolean."
406 return glsl_type::bool_type
;
411 * Validates that a value can be assigned to a location with a specified type
413 * Validates that \c rhs can be assigned to some location. If the types are
414 * not an exact match but an automatic conversion is possible, \c rhs will be
418 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
419 * Otherwise the actual RHS to be assigned will be returned. This may be
420 * \c rhs, or it may be \c rhs after some type conversion.
423 * In addition to being used for assignments, this function is used to
424 * type-check return values.
427 validate_assignment(struct _mesa_glsl_parse_state
*state
,
428 const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
430 const glsl_type
*rhs_type
= rhs
->type
;
432 /* If there is already some error in the RHS, just return it. Anything
433 * else will lead to an avalanche of error message back to the user.
435 if (rhs_type
->is_error())
438 /* If the types are identical, the assignment can trivially proceed.
440 if (rhs_type
== lhs_type
)
443 /* If the array element types are the same and the size of the LHS is zero,
444 * the assignment is okay.
446 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
447 * is handled by ir_dereference::is_lvalue.
449 if (lhs_type
->is_array() && rhs
->type
->is_array()
450 && (lhs_type
->element_type() == rhs
->type
->element_type())
451 && (lhs_type
->array_size() == 0)) {
455 /* Check for implicit conversion in GLSL 1.20 */
456 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
457 rhs_type
= rhs
->type
;
458 if (rhs_type
== lhs_type
)
466 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
467 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
470 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
472 if (!error_emitted
) {
473 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
474 if (!lhs
->is_lvalue()) {
475 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
476 error_emitted
= true;
480 ir_rvalue
*new_rhs
= validate_assignment(state
, lhs
->type
, rhs
);
481 if (new_rhs
== NULL
) {
482 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
486 /* If the LHS array was not declared with a size, it takes it size from
487 * the RHS. If the LHS is an l-value and a whole array, it must be a
488 * dereference of a variable. Any other case would require that the LHS
489 * is either not an l-value or not a whole array.
491 if (lhs
->type
->array_size() == 0) {
492 ir_dereference
*const d
= lhs
->as_dereference();
496 ir_variable
*const var
= d
->variable_referenced();
500 if (var
->max_array_access
>= unsigned(rhs
->type
->array_size())) {
501 /* FINISHME: This should actually log the location of the RHS. */
502 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
504 var
->max_array_access
);
507 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
508 rhs
->type
->array_size());
512 ir_instruction
*tmp
= new ir_assignment(lhs
, rhs
, NULL
);
513 instructions
->push_tail(tmp
);
520 * Generate a new temporary and add its declaration to the instruction stream
523 generate_temporary(const glsl_type
*type
, exec_list
*instructions
,
524 struct _mesa_glsl_parse_state
*state
)
526 char *name
= (char *) malloc(sizeof(char) * 13);
528 snprintf(name
, 13, "tmp_%08X", state
->temp_index
);
531 ir_variable
*const var
= new ir_variable(type
, name
);
532 instructions
->push_tail(var
);
539 get_lvalue_copy(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
540 ir_rvalue
*lvalue
, YYLTYPE loc
)
543 ir_rvalue
*var_deref
;
545 /* FINISHME: Give unique names to the temporaries. */
546 var
= new ir_variable(lvalue
->type
, "_internal_tmp");
547 var
->mode
= ir_var_auto
;
549 var_deref
= new ir_dereference_variable(var
);
550 do_assignment(instructions
, state
, var_deref
, lvalue
, loc
);
552 /* Once we've created this temporary, mark it read only so it's no
553 * longer considered an lvalue.
555 var
->read_only
= true;
562 ast_node::hir(exec_list
*instructions
,
563 struct _mesa_glsl_parse_state
*state
)
573 ast_expression::hir(exec_list
*instructions
,
574 struct _mesa_glsl_parse_state
*state
)
576 static const int operations
[AST_NUM_OPERATORS
] = {
577 -1, /* ast_assign doesn't convert to ir_expression. */
578 -1, /* ast_plus doesn't convert to ir_expression. */
602 /* Note: The following block of expression types actually convert
603 * to multiple IR instructions.
605 ir_binop_mul
, /* ast_mul_assign */
606 ir_binop_div
, /* ast_div_assign */
607 ir_binop_mod
, /* ast_mod_assign */
608 ir_binop_add
, /* ast_add_assign */
609 ir_binop_sub
, /* ast_sub_assign */
610 ir_binop_lshift
, /* ast_ls_assign */
611 ir_binop_rshift
, /* ast_rs_assign */
612 ir_binop_bit_and
, /* ast_and_assign */
613 ir_binop_bit_xor
, /* ast_xor_assign */
614 ir_binop_bit_or
, /* ast_or_assign */
616 -1, /* ast_conditional doesn't convert to ir_expression. */
617 ir_binop_add
, /* ast_pre_inc. */
618 ir_binop_sub
, /* ast_pre_dec. */
619 ir_binop_add
, /* ast_post_inc. */
620 ir_binop_sub
, /* ast_post_dec. */
621 -1, /* ast_field_selection doesn't conv to ir_expression. */
622 -1, /* ast_array_index doesn't convert to ir_expression. */
623 -1, /* ast_function_call doesn't conv to ir_expression. */
624 -1, /* ast_identifier doesn't convert to ir_expression. */
625 -1, /* ast_int_constant doesn't convert to ir_expression. */
626 -1, /* ast_uint_constant doesn't conv to ir_expression. */
627 -1, /* ast_float_constant doesn't conv to ir_expression. */
628 -1, /* ast_bool_constant doesn't conv to ir_expression. */
629 -1, /* ast_sequence doesn't convert to ir_expression. */
631 ir_rvalue
*result
= NULL
;
633 const struct glsl_type
*type
= glsl_type::error_type
;
634 bool error_emitted
= false;
637 loc
= this->get_location();
639 switch (this->oper
) {
641 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
642 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
644 result
= do_assignment(instructions
, state
, op
[0], op
[1],
645 this->subexpressions
[0]->get_location());
646 error_emitted
= result
->type
->is_error();
652 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
654 error_emitted
= op
[0]->type
->is_error();
655 if (type
->is_error())
662 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
664 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
666 error_emitted
= type
->is_error();
668 result
= new ir_expression(operations
[this->oper
], type
,
676 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
677 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
679 type
= arithmetic_result_type(op
[0], op
[1],
680 (this->oper
== ast_mul
),
682 error_emitted
= type
->is_error();
684 result
= new ir_expression(operations
[this->oper
], type
,
689 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
690 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
692 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
694 assert(operations
[this->oper
] == ir_binop_mod
);
696 result
= new ir_expression(operations
[this->oper
], type
,
698 error_emitted
= type
->is_error();
703 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-shift operators");
704 error_emitted
= true;
711 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
712 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
714 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
716 /* The relational operators must either generate an error or result
717 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
719 assert(type
->is_error()
720 || ((type
->base_type
== GLSL_TYPE_BOOL
)
721 && type
->is_scalar()));
723 result
= new ir_expression(operations
[this->oper
], type
,
725 error_emitted
= type
->is_error();
730 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
731 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
733 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
735 * "The equality operators equal (==), and not equal (!=)
736 * operate on all types. They result in a scalar Boolean. If
737 * the operand types do not match, then there must be a
738 * conversion from Section 4.1.10 "Implicit Conversions"
739 * applied to one operand that can make them match, in which
740 * case this conversion is done."
742 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
743 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
744 || (op
[0]->type
!= op
[1]->type
)) {
745 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
746 "type", (this->oper
== ast_equal
) ? "==" : "!=");
747 error_emitted
= true;
748 } else if ((state
->language_version
<= 110)
749 && (op
[0]->type
->is_array() || op
[1]->type
->is_array())) {
750 _mesa_glsl_error(& loc
, state
, "array comparisons forbidden in "
752 error_emitted
= true;
755 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
757 type
= glsl_type::bool_type
;
759 assert(result
->type
== glsl_type::bool_type
);
766 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-wise operators");
767 error_emitted
= true;
770 case ast_logic_and
: {
771 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
773 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
774 YYLTYPE loc
= this->subexpressions
[0]->get_location();
776 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
777 operator_string(this->oper
));
778 error_emitted
= true;
781 ir_constant
*op0_const
= op
[0]->constant_expression_value();
783 if (op0_const
->value
.b
[0]) {
784 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
786 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
787 YYLTYPE loc
= this->subexpressions
[1]->get_location();
789 _mesa_glsl_error(& loc
, state
,
790 "RHS of `%s' must be scalar boolean",
791 operator_string(this->oper
));
792 error_emitted
= true;
798 type
= glsl_type::bool_type
;
800 ir_if
*const stmt
= new ir_if(op
[0]);
801 instructions
->push_tail(stmt
);
803 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
805 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
806 YYLTYPE loc
= this->subexpressions
[1]->get_location();
808 _mesa_glsl_error(& loc
, state
,
809 "RHS of `%s' must be scalar boolean",
810 operator_string(this->oper
));
811 error_emitted
= true;
814 ir_variable
*const tmp
= generate_temporary(glsl_type::bool_type
,
815 instructions
, state
);
817 ir_dereference
*const then_deref
= new ir_dereference_variable(tmp
);
818 ir_assignment
*const then_assign
=
819 new ir_assignment(then_deref
, op
[1], NULL
);
820 stmt
->then_instructions
.push_tail(then_assign
);
822 ir_dereference
*const else_deref
= new ir_dereference_variable(tmp
);
823 ir_assignment
*const else_assign
=
824 new ir_assignment(else_deref
, new ir_constant(false), NULL
);
825 stmt
->else_instructions
.push_tail(else_assign
);
827 result
= new ir_dereference_variable(tmp
);
834 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
836 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
837 YYLTYPE loc
= this->subexpressions
[0]->get_location();
839 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
840 operator_string(this->oper
));
841 error_emitted
= true;
844 ir_constant
*op0_const
= op
[0]->constant_expression_value();
846 if (op0_const
->value
.b
[0]) {
849 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
851 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
852 YYLTYPE loc
= this->subexpressions
[1]->get_location();
854 _mesa_glsl_error(& loc
, state
,
855 "RHS of `%s' must be scalar boolean",
856 operator_string(this->oper
));
857 error_emitted
= true;
861 type
= glsl_type::bool_type
;
863 ir_if
*const stmt
= new ir_if(op
[0]);
864 instructions
->push_tail(stmt
);
866 ir_variable
*const tmp
= generate_temporary(glsl_type::bool_type
,
867 instructions
, state
);
869 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
871 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
872 YYLTYPE loc
= this->subexpressions
[1]->get_location();
874 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
875 operator_string(this->oper
));
876 error_emitted
= true;
879 ir_dereference
*const then_deref
= new ir_dereference_variable(tmp
);
880 ir_assignment
*const then_assign
=
881 new ir_assignment(then_deref
, new ir_constant(true), NULL
);
882 stmt
->then_instructions
.push_tail(then_assign
);
884 ir_dereference
*const else_deref
= new ir_dereference_variable(tmp
);
885 ir_assignment
*const else_assign
=
886 new ir_assignment(else_deref
, op
[1], NULL
);
887 stmt
->else_instructions
.push_tail(else_assign
);
889 result
= new ir_dereference_variable(tmp
);
896 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
897 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
900 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
902 type
= glsl_type::bool_type
;
906 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
908 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
909 YYLTYPE loc
= this->subexpressions
[0]->get_location();
911 _mesa_glsl_error(& loc
, state
,
912 "operand of `!' must be scalar boolean");
913 error_emitted
= true;
916 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
918 type
= glsl_type::bool_type
;
924 case ast_sub_assign
: {
925 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
926 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
928 type
= arithmetic_result_type(op
[0], op
[1],
929 (this->oper
== ast_mul_assign
),
932 ir_rvalue
*temp_rhs
= new ir_expression(operations
[this->oper
], type
,
935 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
936 this->subexpressions
[0]->get_location());
938 error_emitted
= (op
[0]->type
->is_error());
940 /* GLSL 1.10 does not allow array assignment. However, we don't have to
941 * explicitly test for this because none of the binary expression
942 * operators allow array operands either.
948 case ast_mod_assign
: {
949 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
950 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
952 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
954 assert(operations
[this->oper
] == ir_binop_mod
);
956 struct ir_rvalue
*temp_rhs
;
957 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
960 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
961 this->subexpressions
[0]->get_location());
963 error_emitted
= type
->is_error();
969 _mesa_glsl_error(& loc
, state
,
970 "FINISHME: implement bit-shift assignment operators");
971 error_emitted
= true;
977 _mesa_glsl_error(& loc
, state
,
978 "FINISHME: implement logic assignment operators");
979 error_emitted
= true;
982 case ast_conditional
: {
983 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
985 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
987 * "The ternary selection operator (?:). It operates on three
988 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
989 * first expression, which must result in a scalar Boolean."
991 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
992 YYLTYPE loc
= this->subexpressions
[0]->get_location();
994 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
995 error_emitted
= true;
998 /* The :? operator is implemented by generating an anonymous temporary
999 * followed by an if-statement. The last instruction in each branch of
1000 * the if-statement assigns a value to the anonymous temporary. This
1001 * temporary is the r-value of the expression.
1003 exec_list then_instructions
;
1004 exec_list else_instructions
;
1006 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1007 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1009 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1011 * "The second and third expressions can be any type, as
1012 * long their types match, or there is a conversion in
1013 * Section 4.1.10 "Implicit Conversions" that can be applied
1014 * to one of the expressions to make their types match. This
1015 * resulting matching type is the type of the entire
1018 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1019 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1020 || (op
[1]->type
!= op
[2]->type
)) {
1021 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1023 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
1024 "operator must have matching types.");
1025 error_emitted
= true;
1026 type
= glsl_type::error_type
;
1031 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1032 ir_constant
*then_val
= op
[1]->constant_expression_value();
1033 ir_constant
*else_val
= op
[2]->constant_expression_value();
1035 if (then_instructions
.is_empty()
1036 && else_instructions
.is_empty()
1037 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1038 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1040 ir_variable
*const tmp
= generate_temporary(type
,
1041 instructions
, state
);
1043 ir_if
*const stmt
= new ir_if(op
[0]);
1044 instructions
->push_tail(stmt
);
1046 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1047 ir_dereference
*const then_deref
= new ir_dereference_variable(tmp
);
1048 ir_assignment
*const then_assign
=
1049 new ir_assignment(then_deref
, op
[1], NULL
);
1050 stmt
->then_instructions
.push_tail(then_assign
);
1052 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1053 ir_dereference
*const else_deref
= new ir_dereference_variable(tmp
);
1054 ir_assignment
*const else_assign
=
1055 new ir_assignment(else_deref
, op
[2], NULL
);
1056 stmt
->else_instructions
.push_tail(else_assign
);
1058 result
= new ir_dereference_variable(tmp
);
1065 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1066 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1067 op
[1] = new ir_constant(1.0f
);
1069 op
[1] = new ir_constant(1);
1071 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1073 struct ir_rvalue
*temp_rhs
;
1074 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
1077 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
1078 this->subexpressions
[0]->get_location());
1079 type
= result
->type
;
1080 error_emitted
= op
[0]->type
->is_error();
1085 case ast_post_dec
: {
1086 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1087 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1088 op
[1] = new ir_constant(1.0f
);
1090 op
[1] = new ir_constant(1);
1092 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1094 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1096 struct ir_rvalue
*temp_rhs
;
1097 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
1100 /* Get a temporary of a copy of the lvalue before it's modified.
1101 * This may get thrown away later.
1103 result
= get_lvalue_copy(instructions
, state
, op
[0],
1104 this->subexpressions
[0]->get_location());
1106 (void)do_assignment(instructions
, state
, op
[0], temp_rhs
,
1107 this->subexpressions
[0]->get_location());
1109 type
= result
->type
;
1110 error_emitted
= op
[0]->type
->is_error();
1114 case ast_field_selection
:
1115 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1116 type
= result
->type
;
1119 case ast_array_index
: {
1120 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1122 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1123 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1125 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1127 ir_rvalue
*const array
= op
[0];
1129 result
= new ir_dereference_array(op
[0], op
[1]);
1131 /* Do not use op[0] after this point. Use array.
1139 if (!array
->type
->is_array()
1140 && !array
->type
->is_matrix()
1141 && !array
->type
->is_vector()) {
1142 _mesa_glsl_error(& index_loc
, state
,
1143 "cannot dereference non-array / non-matrix / "
1145 error_emitted
= true;
1148 if (!op
[1]->type
->is_integer()) {
1149 _mesa_glsl_error(& index_loc
, state
,
1150 "array index must be integer type");
1151 error_emitted
= true;
1152 } else if (!op
[1]->type
->is_scalar()) {
1153 _mesa_glsl_error(& index_loc
, state
,
1154 "array index must be scalar");
1155 error_emitted
= true;
1158 /* If the array index is a constant expression and the array has a
1159 * declared size, ensure that the access is in-bounds. If the array
1160 * index is not a constant expression, ensure that the array has a
1163 ir_constant
*const const_index
= op
[1]->constant_expression_value();
1164 if (const_index
!= NULL
) {
1165 const int idx
= const_index
->value
.i
[0];
1166 const char *type_name
;
1169 if (array
->type
->is_matrix()) {
1170 type_name
= "matrix";
1171 } else if (array
->type
->is_vector()) {
1172 type_name
= "vector";
1174 type_name
= "array";
1177 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1179 * "It is illegal to declare an array with a size, and then
1180 * later (in the same shader) index the same array with an
1181 * integral constant expression greater than or equal to the
1182 * declared size. It is also illegal to index an array with a
1183 * negative constant expression."
1185 if (array
->type
->is_matrix()) {
1186 if (array
->type
->row_type()->vector_elements
<= idx
) {
1187 bound
= array
->type
->row_type()->vector_elements
;
1189 } else if (array
->type
->is_vector()) {
1190 if (array
->type
->vector_elements
<= idx
) {
1191 bound
= array
->type
->vector_elements
;
1194 if ((array
->type
->array_size() > 0)
1195 && (array
->type
->array_size() <= idx
)) {
1196 bound
= array
->type
->array_size();
1201 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
1203 error_emitted
= true;
1204 } else if (idx
< 0) {
1205 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
1207 error_emitted
= true;
1210 if (array
->type
->is_array()) {
1211 /* If the array is a variable dereference, it dereferences the
1212 * whole array, by definition. Use this to get the variable.
1214 * FINISHME: Should some methods for getting / setting / testing
1215 * FINISHME: array access limits be added to ir_dereference?
1217 ir_variable
*const v
= array
->whole_variable_referenced();
1218 if ((v
!= NULL
) && (unsigned(idx
) > v
->max_array_access
))
1219 v
->max_array_access
= idx
;
1224 result
->type
= glsl_type::error_type
;
1226 type
= result
->type
;
1230 case ast_function_call
:
1231 /* Should *NEVER* get here. ast_function_call should always be handled
1232 * by ast_function_expression::hir.
1237 case ast_identifier
: {
1238 /* ast_identifier can appear several places in a full abstract syntax
1239 * tree. This particular use must be at location specified in the grammar
1240 * as 'variable_identifier'.
1243 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1245 result
= new ir_dereference_variable(var
);
1248 type
= result
->type
;
1250 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1251 this->primary_expression
.identifier
);
1253 error_emitted
= true;
1258 case ast_int_constant
:
1259 type
= glsl_type::int_type
;
1260 result
= new ir_constant(this->primary_expression
.int_constant
);
1263 case ast_uint_constant
:
1264 type
= glsl_type::uint_type
;
1265 result
= new ir_constant(this->primary_expression
.uint_constant
);
1268 case ast_float_constant
:
1269 type
= glsl_type::float_type
;
1270 result
= new ir_constant(this->primary_expression
.float_constant
);
1273 case ast_bool_constant
:
1274 type
= glsl_type::bool_type
;
1275 result
= new ir_constant(bool(this->primary_expression
.bool_constant
));
1278 case ast_sequence
: {
1279 /* It should not be possible to generate a sequence in the AST without
1280 * any expressions in it.
1282 assert(!this->expressions
.is_empty());
1284 /* The r-value of a sequence is the last expression in the sequence. If
1285 * the other expressions in the sequence do not have side-effects (and
1286 * therefore add instructions to the instruction list), they get dropped
1289 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
)
1290 result
= ast
->hir(instructions
, state
);
1292 type
= result
->type
;
1294 /* Any errors should have already been emitted in the loop above.
1296 error_emitted
= true;
1301 if (type
->is_error() && !error_emitted
)
1302 _mesa_glsl_error(& loc
, state
, "type mismatch");
1309 ast_expression_statement::hir(exec_list
*instructions
,
1310 struct _mesa_glsl_parse_state
*state
)
1312 /* It is possible to have expression statements that don't have an
1313 * expression. This is the solitary semicolon:
1315 * for (i = 0; i < 5; i++)
1318 * In this case the expression will be NULL. Test for NULL and don't do
1319 * anything in that case.
1321 if (expression
!= NULL
)
1322 expression
->hir(instructions
, state
);
1324 /* Statements do not have r-values.
1331 ast_compound_statement::hir(exec_list
*instructions
,
1332 struct _mesa_glsl_parse_state
*state
)
1335 state
->symbols
->push_scope();
1337 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1338 ast
->hir(instructions
, state
);
1341 state
->symbols
->pop_scope();
1343 /* Compound statements do not have r-values.
1349 static const glsl_type
*
1350 process_array_type(const glsl_type
*base
, ast_node
*array_size
,
1351 struct _mesa_glsl_parse_state
*state
)
1353 unsigned length
= 0;
1355 /* FINISHME: Reject delcarations of multidimensional arrays. */
1357 if (array_size
!= NULL
) {
1358 exec_list dummy_instructions
;
1359 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1360 YYLTYPE loc
= array_size
->get_location();
1362 /* FINISHME: Verify that the grammar forbids side-effects in array
1363 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1365 assert(dummy_instructions
.is_empty());
1368 if (!ir
->type
->is_integer()) {
1369 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1370 } else if (!ir
->type
->is_scalar()) {
1371 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1373 ir_constant
*const size
= ir
->constant_expression_value();
1376 _mesa_glsl_error(& loc
, state
, "array size must be a "
1377 "constant valued expression");
1378 } else if (size
->value
.i
[0] <= 0) {
1379 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1381 assert(size
->type
== ir
->type
);
1382 length
= size
->value
.u
[0];
1388 return glsl_type::get_array_instance(base
, length
);
1393 ast_type_specifier::glsl_type(const char **name
,
1394 struct _mesa_glsl_parse_state
*state
) const
1396 const struct glsl_type
*type
;
1398 if ((this->type_specifier
== ast_struct
) && (this->type_name
== NULL
)) {
1399 /* FINISHME: Handle annonymous structures. */
1402 type
= state
->symbols
->get_type(this->type_name
);
1403 *name
= this->type_name
;
1405 if (this->is_array
) {
1406 type
= process_array_type(type
, this->array_size
, state
);
1415 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1416 struct ir_variable
*var
,
1417 struct _mesa_glsl_parse_state
*state
,
1420 if (qual
->invariant
)
1423 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1424 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
1425 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1431 if (qual
->attribute
&& state
->target
!= vertex_shader
) {
1432 var
->type
= glsl_type::error_type
;
1433 _mesa_glsl_error(loc
, state
,
1434 "`attribute' variables may not be declared in the "
1436 _mesa_glsl_shader_target_name(state
->target
));
1439 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1441 * "The varying qualifier can be used only with the data types
1442 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1445 if (qual
->varying
) {
1446 const glsl_type
*non_array_type
;
1448 if (var
->type
&& var
->type
->is_array())
1449 non_array_type
= var
->type
->fields
.array
;
1451 non_array_type
= var
->type
;
1453 if (non_array_type
&& non_array_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1454 var
->type
= glsl_type::error_type
;
1455 _mesa_glsl_error(loc
, state
,
1456 "varying variables must be of base type float");
1460 if (qual
->in
&& qual
->out
)
1461 var
->mode
= ir_var_inout
;
1462 else if (qual
->attribute
|| qual
->in
1463 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1464 var
->mode
= ir_var_in
;
1465 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
1466 var
->mode
= ir_var_out
;
1467 else if (qual
->uniform
)
1468 var
->mode
= ir_var_uniform
;
1470 var
->mode
= ir_var_auto
;
1473 var
->shader_in
= true;
1475 /* Any 'in' or 'inout' variables at global scope must be marked as being
1476 * shader inputs. Likewise, any 'out' or 'inout' variables at global scope
1477 * must be marked as being shader outputs.
1479 if (state
->current_function
== NULL
) {
1480 switch (var
->mode
) {
1482 case ir_var_uniform
:
1483 var
->shader_in
= true;
1486 var
->shader_out
= true;
1489 var
->shader_in
= true;
1490 var
->shader_out
= true;
1498 var
->interpolation
= ir_var_flat
;
1499 else if (qual
->noperspective
)
1500 var
->interpolation
= ir_var_noperspective
;
1502 var
->interpolation
= ir_var_smooth
;
1504 if (var
->type
->is_array() && (state
->language_version
>= 120)) {
1505 var
->array_lvalue
= true;
1511 ast_declarator_list::hir(exec_list
*instructions
,
1512 struct _mesa_glsl_parse_state
*state
)
1514 const struct glsl_type
*decl_type
;
1515 const char *type_name
= NULL
;
1516 ir_rvalue
*result
= NULL
;
1517 YYLTYPE loc
= this->get_location();
1519 /* The type specifier may contain a structure definition. Process that
1520 * before any of the variable declarations.
1522 (void) this->type
->specifier
->hir(instructions
, state
);
1524 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1525 * FINISHME: include a type. These re-declare built-in variables to be
1526 * FINISHME: invariant.
1529 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1530 if (this->declarations
.is_empty()) {
1531 /* There are only two valid cases where the declaration list can be
1534 * 1. The declaration is setting the default precision of a built-in
1535 * type (e.g., 'precision highp vec4;').
1537 * 2. Adding 'invariant' to an existing vertex shader output.
1540 if (this->type
->qualifier
.invariant
) {
1541 } else if (decl_type
!= NULL
) {
1543 _mesa_glsl_error(& loc
, state
, "incomplete declaration");
1547 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1548 const struct glsl_type
*var_type
;
1549 struct ir_variable
*var
;
1551 /* FINISHME: Emit a warning if a variable declaration shadows a
1552 * FINISHME: declaration at a higher scope.
1555 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1556 if (type_name
!= NULL
) {
1557 _mesa_glsl_error(& loc
, state
,
1558 "invalid type `%s' in declaration of `%s'",
1559 type_name
, decl
->identifier
);
1561 _mesa_glsl_error(& loc
, state
,
1562 "invalid type in declaration of `%s'",
1568 if (decl
->is_array
) {
1569 var_type
= process_array_type(decl_type
, decl
->array_size
, state
);
1571 var_type
= decl_type
;
1574 var
= new ir_variable(var_type
, decl
->identifier
);
1576 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1578 * "Global variables can only use the qualifiers const,
1579 * attribute, uni form, or varying. Only one may be
1582 * Local variables can only use the qualifier const."
1584 * This is relaxed in GLSL 1.30.
1586 if (state
->language_version
< 120) {
1587 if (this->type
->qualifier
.out
) {
1588 _mesa_glsl_error(& loc
, state
,
1589 "`out' qualifier in declaration of `%s' "
1590 "only valid for function parameters in GLSL 1.10.",
1593 if (this->type
->qualifier
.in
) {
1594 _mesa_glsl_error(& loc
, state
,
1595 "`in' qualifier in declaration of `%s' "
1596 "only valid for function parameters in GLSL 1.10.",
1599 /* FINISHME: Test for other invalid qualifiers. */
1602 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1605 /* Attempt to add the variable to the symbol table. If this fails, it
1606 * means the variable has already been declared at this scope. Arrays
1607 * fudge this rule a little bit.
1609 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1611 * "It is legal to declare an array without a size and then
1612 * later re-declare the same name as an array of the same
1613 * type and specify a size."
1615 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
1616 ir_variable
*const earlier
=
1617 state
->symbols
->get_variable(decl
->identifier
);
1619 if ((earlier
!= NULL
)
1620 && (earlier
->type
->array_size() == 0)
1621 && var
->type
->is_array()
1622 && (var
->type
->element_type() == earlier
->type
->element_type())) {
1623 /* FINISHME: This doesn't match the qualifiers on the two
1624 * FINISHME: declarations. It's not 100% clear whether this is
1625 * FINISHME: required or not.
1628 if (var
->type
->array_size() <= (int)earlier
->max_array_access
) {
1629 YYLTYPE loc
= this->get_location();
1631 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
1633 earlier
->max_array_access
);
1636 earlier
->type
= var
->type
;
1640 YYLTYPE loc
= this->get_location();
1642 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
1649 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1651 * "Identifiers starting with "gl_" are reserved for use by
1652 * OpenGL, and may not be declared in a shader as either a
1653 * variable or a function."
1655 if (strncmp(decl
->identifier
, "gl_", 3) == 0) {
1656 /* FINISHME: This should only trigger if we're not redefining
1657 * FINISHME: a builtin (to add a qualifier, for example).
1659 _mesa_glsl_error(& loc
, state
,
1660 "identifier `%s' uses reserved `gl_' prefix",
1664 instructions
->push_tail(var
);
1666 if (state
->current_function
!= NULL
) {
1667 const char *mode
= NULL
;
1668 const char *extra
= "";
1670 /* There is no need to check for 'inout' here because the parser will
1671 * only allow that in function parameter lists.
1673 if (this->type
->qualifier
.attribute
) {
1675 } else if (this->type
->qualifier
.uniform
) {
1677 } else if (this->type
->qualifier
.varying
) {
1679 } else if (this->type
->qualifier
.in
) {
1681 extra
= " or in function parameter list";
1682 } else if (this->type
->qualifier
.out
) {
1684 extra
= " or in function parameter list";
1688 _mesa_glsl_error(& loc
, state
,
1689 "%s variable `%s' must be declared at "
1691 mode
, var
->name
, extra
);
1693 } else if (var
->mode
== ir_var_in
) {
1694 if (state
->target
== vertex_shader
) {
1695 bool error_emitted
= false;
1697 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1699 * "Vertex shader inputs can only be float, floating-point
1700 * vectors, matrices, signed and unsigned integers and integer
1701 * vectors. Vertex shader inputs can also form arrays of these
1702 * types, but not structures."
1704 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1706 * "Vertex shader inputs can only be float, floating-point
1707 * vectors, matrices, signed and unsigned integers and integer
1708 * vectors. They cannot be arrays or structures."
1710 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1712 * "The attribute qualifier can be used only with float,
1713 * floating-point vectors, and matrices. Attribute variables
1714 * cannot be declared as arrays or structures."
1716 const glsl_type
*check_type
= var
->type
->is_array()
1717 ? var
->type
->fields
.array
: var
->type
;
1719 switch (check_type
->base_type
) {
1720 case GLSL_TYPE_FLOAT
:
1722 case GLSL_TYPE_UINT
:
1724 if (state
->language_version
> 120)
1728 _mesa_glsl_error(& loc
, state
,
1729 "vertex shader input / attribute cannot have "
1731 var
->type
->is_array() ? "array of " : "",
1733 error_emitted
= true;
1736 if (!error_emitted
&& (state
->language_version
<= 130)
1737 && var
->type
->is_array()) {
1738 _mesa_glsl_error(& loc
, state
,
1739 "vertex shader input / attribute cannot have "
1741 error_emitted
= true;
1746 if (decl
->initializer
!= NULL
) {
1747 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
1749 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1751 * "All uniform variables are read-only and are initialized either
1752 * directly by an application via API commands, or indirectly by
1755 if ((state
->language_version
<= 110)
1756 && (var
->mode
== ir_var_uniform
)) {
1757 _mesa_glsl_error(& initializer_loc
, state
,
1758 "cannot initialize uniforms in GLSL 1.10");
1761 if (var
->type
->is_sampler()) {
1762 _mesa_glsl_error(& initializer_loc
, state
,
1763 "cannot initialize samplers");
1766 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1767 _mesa_glsl_error(& initializer_loc
, state
,
1768 "cannot initialize %s shader input / %s",
1769 _mesa_glsl_shader_target_name(state
->target
),
1770 (state
->target
== vertex_shader
)
1771 ? "attribute" : "varying");
1774 ir_dereference
*const lhs
= new ir_dereference_variable(var
);
1775 ir_rvalue
*rhs
= decl
->initializer
->hir(instructions
, state
);
1777 /* Calculate the constant value if this is a const or uniform
1780 if (this->type
->qualifier
.constant
|| this->type
->qualifier
.uniform
) {
1781 ir_constant
*constant_value
= rhs
->constant_expression_value();
1782 if (!constant_value
) {
1783 _mesa_glsl_error(& initializer_loc
, state
,
1784 "initializer of %s variable `%s' must be a "
1785 "constant expression",
1786 (this->type
->qualifier
.constant
)
1787 ? "const" : "uniform",
1790 rhs
= constant_value
;
1791 var
->constant_value
= constant_value
;
1795 if (rhs
&& !rhs
->type
->is_error()) {
1796 bool temp
= var
->read_only
;
1797 if (this->type
->qualifier
.constant
)
1798 var
->read_only
= false;
1800 /* Never emit code to initialize a uniform.
1802 if (!this->type
->qualifier
.uniform
)
1803 result
= do_assignment(instructions
, state
, lhs
, rhs
,
1804 this->get_location());
1805 var
->read_only
= temp
;
1809 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1811 * "It is an error to write to a const variable outside of
1812 * its declaration, so they must be initialized when
1815 if (this->type
->qualifier
.constant
&& decl
->initializer
== NULL
) {
1816 _mesa_glsl_error(& loc
, state
,
1817 "const declaration of `%s' must be initialized");
1820 /* Add the vairable to the symbol table after processing the initializer.
1821 * This differs from most C-like languages, but it follows the GLSL
1822 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1825 * "Within a declaration, the scope of a name starts immediately
1826 * after the initializer if present or immediately after the name
1827 * being declared if not."
1829 const bool added_variable
=
1830 state
->symbols
->add_variable(decl
->identifier
, var
);
1831 assert(added_variable
);
1835 /* Generally, variable declarations do not have r-values. However,
1836 * one is used for the declaration in
1838 * while (bool b = some_condition()) {
1842 * so we return the rvalue from the last seen declaration here.
1849 ast_parameter_declarator::hir(exec_list
*instructions
,
1850 struct _mesa_glsl_parse_state
*state
)
1852 const struct glsl_type
*type
;
1853 const char *name
= NULL
;
1854 YYLTYPE loc
= this->get_location();
1856 type
= this->type
->specifier
->glsl_type(& name
, state
);
1860 _mesa_glsl_error(& loc
, state
,
1861 "invalid type `%s' in declaration of `%s'",
1862 name
, this->identifier
);
1864 _mesa_glsl_error(& loc
, state
,
1865 "invalid type in declaration of `%s'",
1869 type
= glsl_type::error_type
;
1872 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1874 * "Functions that accept no input arguments need not use void in the
1875 * argument list because prototypes (or definitions) are required and
1876 * therefore there is no ambiguity when an empty argument list "( )" is
1877 * declared. The idiom "(void)" as a parameter list is provided for
1880 * Placing this check here prevents a void parameter being set up
1881 * for a function, which avoids tripping up checks for main taking
1882 * parameters and lookups of an unnamed symbol.
1884 if (type
->is_void()) {
1885 if (this->identifier
!= NULL
)
1886 _mesa_glsl_error(& loc
, state
,
1887 "named parameter cannot have type `void'");
1893 if (formal_parameter
&& (this->identifier
== NULL
)) {
1894 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
1899 ir_variable
*var
= new ir_variable(type
, this->identifier
);
1901 /* FINISHME: Handle array declarations. Note that this requires
1902 * FINISHME: complete handling of constant expressions.
1905 /* Apply any specified qualifiers to the parameter declaration. Note that
1906 * for function parameters the default mode is 'in'.
1908 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
1909 if (var
->mode
== ir_var_auto
)
1910 var
->mode
= ir_var_in
;
1912 instructions
->push_tail(var
);
1914 /* Parameter declarations do not have r-values.
1921 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
1923 exec_list
*ir_parameters
,
1924 _mesa_glsl_parse_state
*state
)
1926 ast_parameter_declarator
*void_param
= NULL
;
1929 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
1930 param
->formal_parameter
= formal
;
1931 param
->hir(ir_parameters
, state
);
1939 if ((void_param
!= NULL
) && (count
> 1)) {
1940 YYLTYPE loc
= void_param
->get_location();
1942 _mesa_glsl_error(& loc
, state
,
1943 "`void' parameter must be only parameter");
1949 ast_function::hir(exec_list
*instructions
,
1950 struct _mesa_glsl_parse_state
*state
)
1952 ir_function
*f
= NULL
;
1953 ir_function_signature
*sig
= NULL
;
1954 exec_list hir_parameters
;
1957 /* Convert the list of function parameters to HIR now so that they can be
1958 * used below to compare this function's signature with previously seen
1959 * signatures for functions with the same name.
1961 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
1963 & hir_parameters
, state
);
1965 const char *return_type_name
;
1966 const glsl_type
*return_type
=
1967 this->return_type
->specifier
->glsl_type(& return_type_name
, state
);
1969 assert(return_type
!= NULL
);
1971 /* Verify that this function's signature either doesn't match a previously
1972 * seen signature for a function with the same name, or, if a match is found,
1973 * that the previously seen signature does not have an associated definition.
1975 const char *const name
= identifier
;
1976 f
= state
->symbols
->get_function(name
);
1978 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
1980 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
1981 if (badvar
!= NULL
) {
1982 YYLTYPE loc
= this->get_location();
1984 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
1985 "qualifiers don't match prototype", name
, badvar
);
1988 if (sig
->return_type
!= return_type
) {
1989 YYLTYPE loc
= this->get_location();
1991 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
1992 "match prototype", name
);
1995 if (is_definition
&& sig
->is_defined
) {
1996 YYLTYPE loc
= this->get_location();
1998 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
2002 } else if (state
->symbols
->name_declared_this_scope(name
)) {
2003 /* This function name shadows a non-function use of the same name.
2005 YYLTYPE loc
= this->get_location();
2007 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
2008 "non-function", name
);
2011 f
= new ir_function(name
);
2012 state
->symbols
->add_function(f
->name
, f
);
2014 /* Emit the new function header */
2015 instructions
->push_tail(f
);
2018 /* Verify the return type of main() */
2019 if (strcmp(name
, "main") == 0) {
2020 if (! return_type
->is_void()) {
2021 YYLTYPE loc
= this->get_location();
2023 _mesa_glsl_error(& loc
, state
, "main() must return void");
2026 if (!hir_parameters
.is_empty()) {
2027 YYLTYPE loc
= this->get_location();
2029 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
2033 /* Finish storing the information about this new function in its signature.
2036 sig
= new ir_function_signature(return_type
);
2037 f
->add_signature(sig
);
2040 sig
->replace_parameters(&hir_parameters
);
2043 /* Function declarations (prototypes) do not have r-values.
2050 ast_function_definition::hir(exec_list
*instructions
,
2051 struct _mesa_glsl_parse_state
*state
)
2053 prototype
->is_definition
= true;
2054 prototype
->hir(instructions
, state
);
2056 ir_function_signature
*signature
= prototype
->signature
;
2058 assert(state
->current_function
== NULL
);
2059 state
->current_function
= signature
;
2061 /* Duplicate parameters declared in the prototype as concrete variables.
2062 * Add these to the symbol table.
2064 state
->symbols
->push_scope();
2065 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
2066 ir_variable
*const var
= ((ir_instruction
*) iter
.get())->as_variable();
2068 assert(var
!= NULL
);
2070 /* The only way a parameter would "exist" is if two parameters have
2073 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
2074 YYLTYPE loc
= this->get_location();
2076 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
2078 state
->symbols
->add_variable(var
->name
, var
);
2082 /* Convert the body of the function to HIR. */
2083 this->body
->hir(&signature
->body
, state
);
2084 signature
->is_defined
= true;
2086 state
->symbols
->pop_scope();
2088 assert(state
->current_function
== signature
);
2089 state
->current_function
= NULL
;
2091 /* Function definitions do not have r-values.
2098 ast_jump_statement::hir(exec_list
*instructions
,
2099 struct _mesa_glsl_parse_state
*state
)
2105 assert(state
->current_function
);
2107 if (opt_return_value
) {
2108 if (state
->current_function
->return_type
->base_type
==
2110 YYLTYPE loc
= this->get_location();
2112 _mesa_glsl_error(& loc
, state
,
2113 "`return` with a value, in function `%s' "
2115 state
->current_function
->function_name());
2118 ir_expression
*const ret
= (ir_expression
*)
2119 opt_return_value
->hir(instructions
, state
);
2120 assert(ret
!= NULL
);
2122 /* FINISHME: Make sure the type of the return value matches the return
2123 * FINISHME: type of the enclosing function.
2126 inst
= new ir_return(ret
);
2128 if (state
->current_function
->return_type
->base_type
!=
2130 YYLTYPE loc
= this->get_location();
2132 _mesa_glsl_error(& loc
, state
,
2133 "`return' with no value, in function %s returning "
2135 state
->current_function
->function_name());
2137 inst
= new ir_return
;
2140 instructions
->push_tail(inst
);
2145 /* FINISHME: discard support */
2146 if (state
->target
!= fragment_shader
) {
2147 YYLTYPE loc
= this->get_location();
2149 _mesa_glsl_error(& loc
, state
,
2150 "`discard' may only appear in a fragment shader");
2156 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2157 * FINISHME: and they use a different IR instruction for 'break'.
2159 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2160 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2163 if (state
->loop_or_switch_nesting
== NULL
) {
2164 YYLTYPE loc
= this->get_location();
2166 _mesa_glsl_error(& loc
, state
,
2167 "`%s' may only appear in a loop",
2168 (mode
== ast_break
) ? "break" : "continue");
2170 ir_loop
*const loop
= state
->loop_or_switch_nesting
->as_loop();
2173 ir_loop_jump
*const jump
=
2174 new ir_loop_jump(loop
,
2176 ? ir_loop_jump::jump_break
2177 : ir_loop_jump::jump_continue
);
2178 instructions
->push_tail(jump
);
2185 /* Jump instructions do not have r-values.
2192 ast_selection_statement::hir(exec_list
*instructions
,
2193 struct _mesa_glsl_parse_state
*state
)
2195 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
2197 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2199 * "Any expression whose type evaluates to a Boolean can be used as the
2200 * conditional expression bool-expression. Vector types are not accepted
2201 * as the expression to if."
2203 * The checks are separated so that higher quality diagnostics can be
2204 * generated for cases where both rules are violated.
2206 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
2207 YYLTYPE loc
= this->condition
->get_location();
2209 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
2213 ir_if
*const stmt
= new ir_if(condition
);
2215 if (then_statement
!= NULL
)
2216 then_statement
->hir(& stmt
->then_instructions
, state
);
2218 if (else_statement
!= NULL
)
2219 else_statement
->hir(& stmt
->else_instructions
, state
);
2221 instructions
->push_tail(stmt
);
2223 /* if-statements do not have r-values.
2230 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
2231 struct _mesa_glsl_parse_state
*state
)
2233 if (condition
!= NULL
) {
2234 ir_rvalue
*const cond
=
2235 condition
->hir(& stmt
->body_instructions
, state
);
2238 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
2239 YYLTYPE loc
= condition
->get_location();
2241 _mesa_glsl_error(& loc
, state
,
2242 "loop condition must be scalar boolean");
2244 /* As the first code in the loop body, generate a block that looks
2245 * like 'if (!condition) break;' as the loop termination condition.
2247 ir_rvalue
*const not_cond
=
2248 new ir_expression(ir_unop_logic_not
, glsl_type::bool_type
, cond
,
2251 ir_if
*const if_stmt
= new ir_if(not_cond
);
2253 ir_jump
*const break_stmt
=
2254 new ir_loop_jump(stmt
, ir_loop_jump::jump_break
);
2256 if_stmt
->then_instructions
.push_tail(break_stmt
);
2257 stmt
->body_instructions
.push_tail(if_stmt
);
2264 ast_iteration_statement::hir(exec_list
*instructions
,
2265 struct _mesa_glsl_parse_state
*state
)
2267 /* For-loops and while-loops start a new scope, but do-while loops do not.
2269 if (mode
!= ast_do_while
)
2270 state
->symbols
->push_scope();
2272 if (init_statement
!= NULL
)
2273 init_statement
->hir(instructions
, state
);
2275 ir_loop
*const stmt
= new ir_loop();
2276 instructions
->push_tail(stmt
);
2278 /* Track the current loop and / or switch-statement nesting.
2280 ir_instruction
*const nesting
= state
->loop_or_switch_nesting
;
2281 state
->loop_or_switch_nesting
= stmt
;
2283 if (mode
!= ast_do_while
)
2284 condition_to_hir(stmt
, state
);
2287 body
->hir(& stmt
->body_instructions
, state
);
2289 if (rest_expression
!= NULL
)
2290 rest_expression
->hir(& stmt
->body_instructions
, state
);
2292 if (mode
== ast_do_while
)
2293 condition_to_hir(stmt
, state
);
2295 if (mode
!= ast_do_while
)
2296 state
->symbols
->pop_scope();
2298 /* Restore previous nesting before returning.
2300 state
->loop_or_switch_nesting
= nesting
;
2302 /* Loops do not have r-values.
2309 ast_type_specifier::hir(exec_list
*instructions
,
2310 struct _mesa_glsl_parse_state
*state
)
2312 if (this->structure
!= NULL
)
2313 return this->structure
->hir(instructions
, state
);
2320 ast_struct_specifier::hir(exec_list
*instructions
,
2321 struct _mesa_glsl_parse_state
*state
)
2323 unsigned decl_count
= 0;
2325 /* Make an initial pass over the list of structure fields to determine how
2326 * many there are. Each element in this list is an ast_declarator_list.
2327 * This means that we actually need to count the number of elements in the
2328 * 'declarations' list in each of the elements.
2330 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2331 &this->declarations
) {
2332 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
2338 /* Allocate storage for the structure fields and process the field
2339 * declarations. As the declarations are processed, try to also convert
2340 * the types to HIR. This ensures that structure definitions embedded in
2341 * other structure definitions are processed.
2343 glsl_struct_field
*const fields
= (glsl_struct_field
*)
2344 malloc(sizeof(*fields
) * decl_count
);
2347 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2348 &this->declarations
) {
2349 const char *type_name
;
2351 decl_list
->type
->specifier
->hir(instructions
, state
);
2353 const glsl_type
*decl_type
=
2354 decl_list
->type
->specifier
->glsl_type(& type_name
, state
);
2356 foreach_list_typed (ast_declaration
, decl
, link
,
2357 &decl_list
->declarations
) {
2358 const struct glsl_type
*const field_type
=
2360 ? process_array_type(decl_type
, decl
->array_size
, state
)
2363 fields
[i
].type
= (field_type
!= NULL
)
2364 ? field_type
: glsl_type::error_type
;
2365 fields
[i
].name
= decl
->identifier
;
2370 assert(i
== decl_count
);
2373 if (this->name
== NULL
) {
2374 static unsigned anon_count
= 1;
2377 snprintf(buf
, sizeof(buf
), "#anon_struct_%04x", anon_count
);
2385 glsl_type
*t
= new glsl_type(fields
, decl_count
, name
);
2387 YYLTYPE loc
= this->get_location();
2388 if (!state
->symbols
->add_type(name
, t
)) {
2389 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
2391 /* This logic is a bit tricky. It is an error to declare a structure at
2392 * global scope if there is also a function with the same name.
2394 if ((state
->current_function
== NULL
)
2395 && (state
->symbols
->get_function(name
) != NULL
)) {
2396 _mesa_glsl_error(& loc
, state
, "name `%s' previously defined", name
);
2398 t
->generate_constructor(state
->symbols
);
2401 const glsl_type
**s
= (const glsl_type
**)
2402 realloc(state
->user_structures
,
2403 sizeof(state
->user_structures
[0]) *
2404 (state
->num_user_structures
+ 1));
2406 s
[state
->num_user_structures
] = t
;
2407 state
->user_structures
= s
;
2408 state
->num_user_structures
++;
2412 /* Structure type definitions do not have r-values.