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 struct simple_node
*ptr
;
64 _mesa_glsl_initialize_variables(instructions
, state
);
65 _mesa_glsl_initialize_constructors(instructions
, state
);
66 _mesa_glsl_initialize_functions(instructions
, state
);
68 state
->current_function
= NULL
;
70 foreach (ptr
, & state
->translation_unit
) {
71 ((ast_node
*)ptr
)->hir(instructions
, state
);
77 * If a conversion is available, convert one operand to a different type
79 * The \c from \c ir_rvalue is converted "in place".
81 * \param to Type that the operand it to be converted to
82 * \param from Operand that is being converted
83 * \param state GLSL compiler state
86 * If a conversion is possible (or unnecessary), \c true is returned.
87 * Otherwise \c false is returned.
90 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
91 struct _mesa_glsl_parse_state
*state
)
93 if (to
->base_type
== from
->type
->base_type
)
96 /* This conversion was added in GLSL 1.20. If the compilation mode is
97 * GLSL 1.10, the conversion is skipped.
99 if (state
->language_version
< 120)
102 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
104 * "There are no implicit array or structure conversions. For
105 * example, an array of int cannot be implicitly converted to an
106 * array of float. There are no implicit conversions between
107 * signed and unsigned integers."
109 /* FINISHME: The above comment is partially a lie. There is int/uint
110 * FINISHME: conversion for immediate constants.
112 if (!to
->is_float() || !from
->type
->is_numeric())
115 switch (from
->type
->base_type
) {
117 from
= new ir_expression(ir_unop_i2f
, to
, from
, NULL
);
120 from
= new ir_expression(ir_unop_u2f
, to
, from
, NULL
);
123 from
= new ir_expression(ir_unop_b2f
, to
, from
, NULL
);
133 static const struct glsl_type
*
134 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
136 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
138 const glsl_type
*const type_a
= value_a
->type
;
139 const glsl_type
*const type_b
= value_b
->type
;
141 /* From GLSL 1.50 spec, page 56:
143 * "The arithmetic binary operators add (+), subtract (-),
144 * multiply (*), and divide (/) operate on integer and
145 * floating-point scalars, vectors, and matrices."
147 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
148 _mesa_glsl_error(loc
, state
,
149 "Operands to arithmetic operators must be numeric");
150 return glsl_type::error_type
;
154 /* "If one operand is floating-point based and the other is
155 * not, then the conversions from Section 4.1.10 "Implicit
156 * Conversions" are applied to the non-floating-point-based operand."
158 if (!apply_implicit_conversion(type_a
, value_b
, state
)
159 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
160 _mesa_glsl_error(loc
, state
,
161 "Could not implicitly convert operands to "
162 "arithmetic operator");
163 return glsl_type::error_type
;
166 /* "If the operands are integer types, they must both be signed or
169 * From this rule and the preceeding conversion it can be inferred that
170 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
171 * The is_numeric check above already filtered out the case where either
172 * type is not one of these, so now the base types need only be tested for
175 if (type_a
->base_type
!= type_b
->base_type
) {
176 _mesa_glsl_error(loc
, state
,
177 "base type mismatch for arithmetic operator");
178 return glsl_type::error_type
;
181 /* "All arithmetic binary operators result in the same fundamental type
182 * (signed integer, unsigned integer, or floating-point) as the
183 * operands they operate on, after operand type conversion. After
184 * conversion, the following cases are valid
186 * * The two operands are scalars. In this case the operation is
187 * applied, resulting in a scalar."
189 if (type_a
->is_scalar() && type_b
->is_scalar())
192 /* "* One operand is a scalar, and the other is a vector or matrix.
193 * In this case, the scalar operation is applied independently to each
194 * component of the vector or matrix, resulting in the same size
197 if (type_a
->is_scalar()) {
198 if (!type_b
->is_scalar())
200 } else if (type_b
->is_scalar()) {
204 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
205 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
208 assert(!type_a
->is_scalar());
209 assert(!type_b
->is_scalar());
211 /* "* The two operands are vectors of the same size. In this case, the
212 * operation is done component-wise resulting in the same size
215 if (type_a
->is_vector() && type_b
->is_vector()) {
216 if (type_a
== type_b
) {
219 _mesa_glsl_error(loc
, state
,
220 "vector size mismatch for arithmetic operator");
221 return glsl_type::error_type
;
225 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
226 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
227 * <vector, vector> have been handled. At least one of the operands must
228 * be matrix. Further, since there are no integer matrix types, the base
229 * type of both operands must be float.
231 assert(type_a
->is_matrix() || type_b
->is_matrix());
232 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
233 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
235 /* "* The operator is add (+), subtract (-), or divide (/), and the
236 * operands are matrices with the same number of rows and the same
237 * number of columns. In this case, the operation is done component-
238 * wise resulting in the same size matrix."
239 * * The operator is multiply (*), where both operands are matrices or
240 * one operand is a vector and the other a matrix. A right vector
241 * operand is treated as a column vector and a left vector operand as a
242 * row vector. In all these cases, it is required that the number of
243 * columns of the left operand is equal to the number of rows of the
244 * right operand. Then, the multiply (*) operation does a linear
245 * algebraic multiply, yielding an object that has the same number of
246 * rows as the left operand and the same number of columns as the right
247 * operand. Section 5.10 "Vector and Matrix Operations" explains in
248 * more detail how vectors and matrices are operated on."
251 if (type_a
== type_b
)
254 if (type_a
->is_matrix() && type_b
->is_matrix()) {
255 /* Matrix multiply. The columns of A must match the rows of B. Given
256 * the other previously tested constraints, this means the vector type
257 * of a row from A must be the same as the vector type of a column from
260 if (type_a
->row_type() == type_b
->column_type()) {
261 /* The resulting matrix has the number of columns of matrix B and
262 * the number of rows of matrix A. We get the row count of A by
263 * looking at the size of a vector that makes up a column. The
264 * transpose (size of a row) is done for B.
266 const glsl_type
*const type
=
267 glsl_type::get_instance(type_a
->base_type
,
268 type_a
->column_type()->vector_elements
,
269 type_b
->row_type()->vector_elements
);
270 assert(type
!= glsl_type::error_type
);
274 } else if (type_a
->is_matrix()) {
275 /* A is a matrix and B is a column vector. Columns of A must match
276 * rows of B. Given the other previously tested constraints, this
277 * means the vector type of a row from A must be the same as the
278 * vector the type of B.
280 if (type_a
->row_type() == type_b
)
283 assert(type_b
->is_matrix());
285 /* A is a row vector and B is a matrix. Columns of A must match rows
286 * of B. Given the other previously tested constraints, this means
287 * the type of A must be the same as the vector type of a column from
290 if (type_a
== type_b
->column_type())
294 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
295 return glsl_type::error_type
;
299 /* "All other cases are illegal."
301 _mesa_glsl_error(loc
, state
, "type mismatch");
302 return glsl_type::error_type
;
306 static const struct glsl_type
*
307 unary_arithmetic_result_type(const struct glsl_type
*type
,
308 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
310 /* From GLSL 1.50 spec, page 57:
312 * "The arithmetic unary operators negate (-), post- and pre-increment
313 * and decrement (-- and ++) operate on integer or floating-point
314 * values (including vectors and matrices). All unary operators work
315 * component-wise on their operands. These result with the same type
318 if (!type
->is_numeric()) {
319 _mesa_glsl_error(loc
, state
,
320 "Operands to arithmetic operators must be numeric");
321 return glsl_type::error_type
;
328 static const struct glsl_type
*
329 modulus_result_type(const struct glsl_type
*type_a
,
330 const struct glsl_type
*type_b
,
331 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
333 /* From GLSL 1.50 spec, page 56:
334 * "The operator modulus (%) operates on signed or unsigned integers or
335 * integer vectors. The operand types must both be signed or both be
338 if (!type_a
->is_integer() || !type_b
->is_integer()
339 || (type_a
->base_type
!= type_b
->base_type
)) {
340 _mesa_glsl_error(loc
, state
, "type mismatch");
341 return glsl_type::error_type
;
344 /* "The operands cannot be vectors of differing size. If one operand is
345 * a scalar and the other vector, then the scalar is applied component-
346 * wise to the vector, resulting in the same type as the vector. If both
347 * are vectors of the same size, the result is computed component-wise."
349 if (type_a
->is_vector()) {
350 if (!type_b
->is_vector()
351 || (type_a
->vector_elements
== type_b
->vector_elements
))
356 /* "The operator modulus (%) is not defined for any other data types
357 * (non-integer types)."
359 _mesa_glsl_error(loc
, state
, "type mismatch");
360 return glsl_type::error_type
;
364 static const struct glsl_type
*
365 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
366 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
368 const glsl_type
*const type_a
= value_a
->type
;
369 const glsl_type
*const type_b
= value_b
->type
;
371 /* From GLSL 1.50 spec, page 56:
372 * "The relational operators greater than (>), less than (<), greater
373 * than or equal (>=), and less than or equal (<=) operate only on
374 * scalar integer and scalar floating-point expressions."
376 if (!type_a
->is_numeric()
377 || !type_b
->is_numeric()
378 || !type_a
->is_scalar()
379 || !type_b
->is_scalar()) {
380 _mesa_glsl_error(loc
, state
,
381 "Operands to relational operators must be scalar and "
383 return glsl_type::error_type
;
386 /* "Either the operands' types must match, or the conversions from
387 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
388 * operand, after which the types must match."
390 if (!apply_implicit_conversion(type_a
, value_b
, state
)
391 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
392 _mesa_glsl_error(loc
, state
,
393 "Could not implicitly convert operands to "
394 "relational operator");
395 return glsl_type::error_type
;
398 if (type_a
->base_type
!= type_b
->base_type
) {
399 _mesa_glsl_error(loc
, state
, "base type mismatch");
400 return glsl_type::error_type
;
403 /* "The result is scalar Boolean."
405 return glsl_type::bool_type
;
410 * Validates that a value can be assigned to a location with a specified type
412 * Validates that \c rhs can be assigned to some location. If the types are
413 * not an exact match but an automatic conversion is possible, \c rhs will be
417 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
418 * Otherwise the actual RHS to be assigned will be returned. This may be
419 * \c rhs, or it may be \c rhs after some type conversion.
422 * In addition to being used for assignments, this function is used to
423 * type-check return values.
426 validate_assignment(const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
428 const glsl_type
*const rhs_type
= rhs
->type
;
430 /* If there is already some error in the RHS, just return it. Anything
431 * else will lead to an avalanche of error message back to the user.
433 if (rhs_type
->is_error())
436 /* If the types are identical, the assignment can trivially proceed.
438 if (rhs_type
== lhs_type
)
441 /* If the array element types are the same and the size of the LHS is zero,
442 * the assignment is okay.
444 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
445 * is handled by ir_dereference::is_lvalue.
447 if (lhs_type
->is_array() && rhs
->type
->is_array()
448 && (lhs_type
->element_type() == rhs
->type
->element_type())
449 && (lhs_type
->array_size() == 0)) {
453 /* FINISHME: Check for and apply automatic conversions. */
458 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
459 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
462 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
464 if (!error_emitted
) {
465 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
466 if (!lhs
->is_lvalue()) {
467 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
468 error_emitted
= true;
472 ir_rvalue
*new_rhs
= validate_assignment(lhs
->type
, rhs
);
473 if (new_rhs
== NULL
) {
474 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
478 /* If the LHS array was not declared with a size, it takes it size from
479 * the RHS. If the LHS is an l-value and a whole array, it must be a
480 * dereference of a variable. Any other case would require that the LHS
481 * is either not an l-value or not a whole array.
483 if (lhs
->type
->array_size() == 0) {
484 ir_dereference
*const d
= lhs
->as_dereference();
488 ir_variable
*const var
= d
->var
->as_variable();
492 if (var
->max_array_access
>= unsigned(rhs
->type
->array_size())) {
493 /* FINISHME: This should actually log the location of the RHS. */
494 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
496 var
->max_array_access
);
499 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
500 rhs
->type
->array_size());
504 ir_instruction
*tmp
= new ir_assignment(lhs
, rhs
, NULL
);
505 instructions
->push_tail(tmp
);
512 * Generate a new temporary and add its declaration to the instruction stream
515 generate_temporary(const glsl_type
*type
, exec_list
*instructions
,
516 struct _mesa_glsl_parse_state
*state
)
518 char *name
= (char *) malloc(sizeof(char) * 13);
520 snprintf(name
, 13, "tmp_%08X", state
->temp_index
);
523 ir_variable
*const var
= new ir_variable(type
, name
);
524 instructions
->push_tail(var
);
531 get_lvalue_copy(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
532 ir_rvalue
*lvalue
, YYLTYPE loc
)
535 ir_rvalue
*var_deref
;
537 /* FINISHME: Give unique names to the temporaries. */
538 var
= new ir_variable(lvalue
->type
, "_internal_tmp");
539 var
->mode
= ir_var_auto
;
541 var_deref
= new ir_dereference(var
);
542 do_assignment(instructions
, state
, var_deref
, lvalue
, loc
);
544 /* Once we've created this temporary, mark it read only so it's no
545 * longer considered an lvalue.
547 var
->read_only
= true;
554 ast_node::hir(exec_list
*instructions
,
555 struct _mesa_glsl_parse_state
*state
)
565 ast_expression::hir(exec_list
*instructions
,
566 struct _mesa_glsl_parse_state
*state
)
568 static const int operations
[AST_NUM_OPERATORS
] = {
569 -1, /* ast_assign doesn't convert to ir_expression. */
570 -1, /* ast_plus doesn't convert to ir_expression. */
594 /* Note: The following block of expression types actually convert
595 * to multiple IR instructions.
597 ir_binop_mul
, /* ast_mul_assign */
598 ir_binop_div
, /* ast_div_assign */
599 ir_binop_mod
, /* ast_mod_assign */
600 ir_binop_add
, /* ast_add_assign */
601 ir_binop_sub
, /* ast_sub_assign */
602 ir_binop_lshift
, /* ast_ls_assign */
603 ir_binop_rshift
, /* ast_rs_assign */
604 ir_binop_bit_and
, /* ast_and_assign */
605 ir_binop_bit_xor
, /* ast_xor_assign */
606 ir_binop_bit_or
, /* ast_or_assign */
608 -1, /* ast_conditional doesn't convert to ir_expression. */
609 ir_binop_add
, /* ast_pre_inc. */
610 ir_binop_sub
, /* ast_pre_dec. */
611 ir_binop_add
, /* ast_post_inc. */
612 ir_binop_sub
, /* ast_post_dec. */
613 -1, /* ast_field_selection doesn't conv to ir_expression. */
614 -1, /* ast_array_index doesn't convert to ir_expression. */
615 -1, /* ast_function_call doesn't conv to ir_expression. */
616 -1, /* ast_identifier doesn't convert to ir_expression. */
617 -1, /* ast_int_constant doesn't convert to ir_expression. */
618 -1, /* ast_uint_constant doesn't conv to ir_expression. */
619 -1, /* ast_float_constant doesn't conv to ir_expression. */
620 -1, /* ast_bool_constant doesn't conv to ir_expression. */
621 -1, /* ast_sequence doesn't convert to ir_expression. */
623 ir_rvalue
*result
= NULL
;
625 struct simple_node op_list
;
626 const struct glsl_type
*type
= glsl_type::error_type
;
627 bool error_emitted
= false;
630 loc
= this->get_location();
631 make_empty_list(& op_list
);
633 switch (this->oper
) {
635 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
636 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
638 result
= do_assignment(instructions
, state
, op
[0], op
[1],
639 this->subexpressions
[0]->get_location());
640 error_emitted
= result
->type
->is_error();
646 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
648 error_emitted
= op
[0]->type
->is_error();
649 if (type
->is_error())
656 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
658 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
660 error_emitted
= type
->is_error();
662 result
= new ir_expression(operations
[this->oper
], type
,
670 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
671 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
673 type
= arithmetic_result_type(op
[0], op
[1],
674 (this->oper
== ast_mul
),
676 error_emitted
= type
->is_error();
678 result
= new ir_expression(operations
[this->oper
], type
,
683 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
684 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
686 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
688 assert(operations
[this->oper
] == ir_binop_mod
);
690 result
= new ir_expression(operations
[this->oper
], type
,
692 error_emitted
= type
->is_error();
697 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-shift operators");
698 error_emitted
= true;
705 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
706 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
708 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
710 /* The relational operators must either generate an error or result
711 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
713 assert(type
->is_error()
714 || ((type
->base_type
== GLSL_TYPE_BOOL
)
715 && type
->is_scalar()));
717 result
= new ir_expression(operations
[this->oper
], type
,
719 error_emitted
= type
->is_error();
724 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
725 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
727 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
729 * "The equality operators equal (==), and not equal (!=)
730 * operate on all types. They result in a scalar Boolean. If
731 * the operand types do not match, then there must be a
732 * conversion from Section 4.1.10 "Implicit Conversions"
733 * applied to one operand that can make them match, in which
734 * case this conversion is done."
736 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
737 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
738 || (op
[0]->type
!= op
[1]->type
)) {
739 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
740 "type", (this->oper
== ast_equal
) ? "==" : "!=");
741 error_emitted
= true;
742 } else if ((state
->language_version
<= 110)
743 && (op
[0]->type
->is_array() || op
[1]->type
->is_array())) {
744 _mesa_glsl_error(& loc
, state
, "array comparisons forbidden in "
746 error_emitted
= true;
749 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
751 type
= glsl_type::bool_type
;
753 assert(result
->type
== glsl_type::bool_type
);
760 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-wise operators");
761 error_emitted
= true;
764 case ast_logic_and
: {
765 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
767 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
768 YYLTYPE loc
= this->subexpressions
[0]->get_location();
770 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
771 operator_string(this->oper
));
772 error_emitted
= true;
775 ir_constant
*op0_const
= op
[0]->constant_expression_value();
777 if (op0_const
->value
.b
[0]) {
778 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
780 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
781 YYLTYPE loc
= this->subexpressions
[1]->get_location();
783 _mesa_glsl_error(& loc
, state
,
784 "RHS of `%s' must be scalar boolean",
785 operator_string(this->oper
));
786 error_emitted
= true;
792 type
= glsl_type::bool_type
;
794 ir_if
*const stmt
= new ir_if(op
[0]);
795 instructions
->push_tail(stmt
);
797 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
799 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
800 YYLTYPE loc
= this->subexpressions
[1]->get_location();
802 _mesa_glsl_error(& loc
, state
,
803 "RHS of `%s' must be scalar boolean",
804 operator_string(this->oper
));
805 error_emitted
= true;
808 ir_variable
*const tmp
= generate_temporary(glsl_type::bool_type
,
809 instructions
, state
);
811 ir_dereference
*const then_deref
= new ir_dereference(tmp
);
812 ir_assignment
*const then_assign
=
813 new ir_assignment(then_deref
, op
[1], NULL
);
814 stmt
->then_instructions
.push_tail(then_assign
);
816 ir_dereference
*const else_deref
= new ir_dereference(tmp
);
817 ir_assignment
*const else_assign
=
818 new ir_assignment(else_deref
, new ir_constant(false), NULL
);
819 stmt
->else_instructions
.push_tail(else_assign
);
821 result
= new ir_dereference(tmp
);
828 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
830 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
831 YYLTYPE loc
= this->subexpressions
[0]->get_location();
833 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
834 operator_string(this->oper
));
835 error_emitted
= true;
838 ir_constant
*op0_const
= op
[0]->constant_expression_value();
840 if (op0_const
->value
.b
[0]) {
843 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
845 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
846 YYLTYPE loc
= this->subexpressions
[1]->get_location();
848 _mesa_glsl_error(& loc
, state
,
849 "RHS of `%s' must be scalar boolean",
850 operator_string(this->oper
));
851 error_emitted
= true;
855 type
= glsl_type::bool_type
;
857 ir_if
*const stmt
= new ir_if(op
[0]);
858 instructions
->push_tail(stmt
);
860 ir_variable
*const tmp
= generate_temporary(glsl_type::bool_type
,
861 instructions
, state
);
863 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
865 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
866 YYLTYPE loc
= this->subexpressions
[1]->get_location();
868 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
869 operator_string(this->oper
));
870 error_emitted
= true;
873 ir_dereference
*const then_deref
= new ir_dereference(tmp
);
874 ir_assignment
*const then_assign
=
875 new ir_assignment(then_deref
, new ir_constant(true), NULL
);
876 stmt
->then_instructions
.push_tail(then_assign
);
878 ir_dereference
*const else_deref
= new ir_dereference(tmp
);
879 ir_assignment
*const else_assign
=
880 new ir_assignment(else_deref
, op
[1], NULL
);
881 stmt
->else_instructions
.push_tail(else_assign
);
883 result
= new ir_dereference(tmp
);
890 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
891 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
894 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
896 type
= glsl_type::bool_type
;
900 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
902 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
903 YYLTYPE loc
= this->subexpressions
[0]->get_location();
905 _mesa_glsl_error(& loc
, state
,
906 "operand of `!' must be scalar boolean");
907 error_emitted
= true;
910 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
912 type
= glsl_type::bool_type
;
918 case ast_sub_assign
: {
919 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
920 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
922 type
= arithmetic_result_type(op
[0], op
[1],
923 (this->oper
== ast_mul_assign
),
926 ir_rvalue
*temp_rhs
= new ir_expression(operations
[this->oper
], type
,
929 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
930 this->subexpressions
[0]->get_location());
932 error_emitted
= (op
[0]->type
->is_error());
934 /* GLSL 1.10 does not allow array assignment. However, we don't have to
935 * explicitly test for this because none of the binary expression
936 * operators allow array operands either.
942 case ast_mod_assign
: {
943 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
944 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
946 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
948 assert(operations
[this->oper
] == ir_binop_mod
);
950 struct ir_rvalue
*temp_rhs
;
951 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
954 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
955 this->subexpressions
[0]->get_location());
957 error_emitted
= type
->is_error();
963 _mesa_glsl_error(& loc
, state
,
964 "FINISHME: implement bit-shift assignment operators");
965 error_emitted
= true;
971 _mesa_glsl_error(& loc
, state
,
972 "FINISHME: implement logic assignment operators");
973 error_emitted
= true;
976 case ast_conditional
: {
977 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
979 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
981 * "The ternary selection operator (?:). It operates on three
982 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
983 * first expression, which must result in a scalar Boolean."
985 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
986 YYLTYPE loc
= this->subexpressions
[0]->get_location();
988 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
989 error_emitted
= true;
992 /* The :? operator is implemented by generating an anonymous temporary
993 * followed by an if-statement. The last instruction in each branch of
994 * the if-statement assigns a value to the anonymous temporary. This
995 * temporary is the r-value of the expression.
997 ir_variable
*const tmp
= generate_temporary(glsl_type::error_type
,
998 instructions
, state
);
1000 ir_if
*const stmt
= new ir_if(op
[0]);
1001 instructions
->push_tail(stmt
);
1003 op
[1] = this->subexpressions
[1]->hir(& stmt
->then_instructions
, state
);
1004 ir_dereference
*const then_deref
= new ir_dereference(tmp
);
1005 ir_assignment
*const then_assign
=
1006 new ir_assignment(then_deref
, op
[1], NULL
);
1007 stmt
->then_instructions
.push_tail(then_assign
);
1009 op
[2] = this->subexpressions
[2]->hir(& stmt
->else_instructions
, state
);
1010 ir_dereference
*const else_deref
= new ir_dereference(tmp
);
1011 ir_assignment
*const else_assign
=
1012 new ir_assignment(else_deref
, op
[2], NULL
);
1013 stmt
->else_instructions
.push_tail(else_assign
);
1015 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1017 * "The second and third expressions can be any type, as
1018 * long their types match, or there is a conversion in
1019 * Section 4.1.10 "Implicit Conversions" that can be applied
1020 * to one of the expressions to make their types match. This
1021 * resulting matching type is the type of the entire
1024 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1025 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1026 || (op
[1]->type
!= op
[2]->type
)) {
1027 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1029 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
1030 "operator must have matching types.");
1031 error_emitted
= true;
1033 tmp
->type
= op
[1]->type
;
1036 result
= new ir_dereference(tmp
);
1043 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1044 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1045 op
[1] = new ir_constant(1.0f
);
1047 op
[1] = new ir_constant(1);
1049 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1051 struct ir_rvalue
*temp_rhs
;
1052 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
1055 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
1056 this->subexpressions
[0]->get_location());
1057 type
= result
->type
;
1058 error_emitted
= op
[0]->type
->is_error();
1063 case ast_post_dec
: {
1064 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1065 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1066 op
[1] = new ir_constant(1.0f
);
1068 op
[1] = new ir_constant(1);
1070 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1072 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1074 struct ir_rvalue
*temp_rhs
;
1075 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
1078 /* Get a temporary of a copy of the lvalue before it's modified.
1079 * This may get thrown away later.
1081 result
= get_lvalue_copy(instructions
, state
, op
[0],
1082 this->subexpressions
[0]->get_location());
1084 (void)do_assignment(instructions
, state
, op
[0], temp_rhs
,
1085 this->subexpressions
[0]->get_location());
1087 type
= result
->type
;
1088 error_emitted
= op
[0]->type
->is_error();
1092 case ast_field_selection
:
1093 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1094 type
= result
->type
;
1097 case ast_array_index
: {
1098 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1100 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1101 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1103 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1105 ir_dereference
*const lhs
= op
[0]->as_dereference();
1106 ir_instruction
*array
;
1108 && (lhs
->mode
== ir_dereference::ir_reference_variable
)) {
1109 result
= new ir_dereference(lhs
->var
, op
[1]);
1114 result
= new ir_dereference(op
[0], op
[1]);
1118 /* Do not use op[0] after this point. Use array.
1126 if (!array
->type
->is_array()
1127 && !array
->type
->is_matrix()
1128 && !array
->type
->is_vector()) {
1129 _mesa_glsl_error(& index_loc
, state
,
1130 "cannot dereference non-array / non-matrix / "
1132 error_emitted
= true;
1135 if (!op
[1]->type
->is_integer()) {
1136 _mesa_glsl_error(& index_loc
, state
,
1137 "array index must be integer type");
1138 error_emitted
= true;
1139 } else if (!op
[1]->type
->is_scalar()) {
1140 _mesa_glsl_error(& index_loc
, state
,
1141 "array index must be scalar");
1142 error_emitted
= true;
1145 /* If the array index is a constant expression and the array has a
1146 * declared size, ensure that the access is in-bounds. If the array
1147 * index is not a constant expression, ensure that the array has a
1150 ir_constant
*const const_index
= op
[1]->constant_expression_value();
1151 if (const_index
!= NULL
) {
1152 const int idx
= const_index
->value
.i
[0];
1153 const char *type_name
;
1156 if (array
->type
->is_matrix()) {
1157 type_name
= "matrix";
1158 } else if (array
->type
->is_vector()) {
1159 type_name
= "vector";
1161 type_name
= "array";
1164 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1166 * "It is illegal to declare an array with a size, and then
1167 * later (in the same shader) index the same array with an
1168 * integral constant expression greater than or equal to the
1169 * declared size. It is also illegal to index an array with a
1170 * negative constant expression."
1172 if (array
->type
->is_matrix()) {
1173 if (array
->type
->row_type()->vector_elements
<= idx
) {
1174 bound
= array
->type
->row_type()->vector_elements
;
1176 } else if (array
->type
->is_vector()) {
1177 if (array
->type
->vector_elements
<= idx
) {
1178 bound
= array
->type
->vector_elements
;
1181 if ((array
->type
->array_size() > 0)
1182 && (array
->type
->array_size() <= idx
)) {
1183 bound
= array
->type
->array_size();
1188 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
1190 error_emitted
= true;
1191 } else if (idx
< 0) {
1192 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
1194 error_emitted
= true;
1197 if (array
->type
->is_array()) {
1198 ir_variable
*const v
= array
->as_variable();
1199 if ((v
!= NULL
) && (unsigned(idx
) > v
->max_array_access
))
1200 v
->max_array_access
= idx
;
1205 result
->type
= glsl_type::error_type
;
1207 type
= result
->type
;
1211 case ast_function_call
:
1212 /* Should *NEVER* get here. ast_function_call should always be handled
1213 * by ast_function_expression::hir.
1218 case ast_identifier
: {
1219 /* ast_identifier can appear several places in a full abstract syntax
1220 * tree. This particular use must be at location specified in the grammar
1221 * as 'variable_identifier'.
1224 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1226 result
= new ir_dereference(var
);
1229 type
= result
->type
;
1231 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1232 this->primary_expression
.identifier
);
1234 error_emitted
= true;
1239 case ast_int_constant
:
1240 type
= glsl_type::int_type
;
1241 result
= new ir_constant(type
, & this->primary_expression
);
1244 case ast_uint_constant
:
1245 type
= glsl_type::uint_type
;
1246 result
= new ir_constant(type
, & this->primary_expression
);
1249 case ast_float_constant
:
1250 type
= glsl_type::float_type
;
1251 result
= new ir_constant(type
, & this->primary_expression
);
1254 case ast_bool_constant
:
1255 type
= glsl_type::bool_type
;
1256 result
= new ir_constant(type
, & this->primary_expression
);
1259 case ast_sequence
: {
1260 struct simple_node
*ptr
;
1262 /* It should not be possible to generate a sequence in the AST without
1263 * any expressions in it.
1265 assert(!is_empty_list(&this->expressions
));
1267 /* The r-value of a sequence is the last expression in the sequence. If
1268 * the other expressions in the sequence do not have side-effects (and
1269 * therefore add instructions to the instruction list), they get dropped
1272 foreach (ptr
, &this->expressions
)
1273 result
= ((ast_node
*)ptr
)->hir(instructions
, state
);
1275 type
= result
->type
;
1277 /* Any errors should have already been emitted in the loop above.
1279 error_emitted
= true;
1284 if (type
->is_error() && !error_emitted
)
1285 _mesa_glsl_error(& loc
, state
, "type mismatch");
1292 ast_expression_statement::hir(exec_list
*instructions
,
1293 struct _mesa_glsl_parse_state
*state
)
1295 /* It is possible to have expression statements that don't have an
1296 * expression. This is the solitary semicolon:
1298 * for (i = 0; i < 5; i++)
1301 * In this case the expression will be NULL. Test for NULL and don't do
1302 * anything in that case.
1304 if (expression
!= NULL
)
1305 expression
->hir(instructions
, state
);
1307 /* Statements do not have r-values.
1314 ast_compound_statement::hir(exec_list
*instructions
,
1315 struct _mesa_glsl_parse_state
*state
)
1317 struct simple_node
*ptr
;
1321 state
->symbols
->push_scope();
1323 foreach (ptr
, &statements
)
1324 ((ast_node
*)ptr
)->hir(instructions
, state
);
1327 state
->symbols
->pop_scope();
1329 /* Compound statements do not have r-values.
1335 static const glsl_type
*
1336 process_array_type(const glsl_type
*base
, ast_node
*array_size
,
1337 struct _mesa_glsl_parse_state
*state
)
1339 unsigned length
= 0;
1341 /* FINISHME: Reject delcarations of multidimensional arrays. */
1343 if (array_size
!= NULL
) {
1344 exec_list dummy_instructions
;
1345 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1346 YYLTYPE loc
= array_size
->get_location();
1348 /* FINISHME: Verify that the grammar forbids side-effects in array
1349 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1351 assert(dummy_instructions
.is_empty());
1354 if (!ir
->type
->is_integer()) {
1355 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1356 } else if (!ir
->type
->is_scalar()) {
1357 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1359 ir_constant
*const size
= ir
->constant_expression_value();
1362 _mesa_glsl_error(& loc
, state
, "array size must be a "
1363 "constant valued expression");
1364 } else if (size
->value
.i
[0] <= 0) {
1365 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1367 assert(size
->type
== ir
->type
);
1368 length
= size
->value
.u
[0];
1374 return glsl_type::get_array_instance(base
, length
);
1379 ast_type_specifier::glsl_type(const char **name
,
1380 struct _mesa_glsl_parse_state
*state
) const
1382 const struct glsl_type
*type
;
1384 if ((this->type_specifier
== ast_struct
) && (this->type_name
== NULL
)) {
1385 /* FINISHME: Handle annonymous structures. */
1388 type
= state
->symbols
->get_type(this->type_name
);
1389 *name
= this->type_name
;
1391 if (this->is_array
) {
1392 type
= process_array_type(type
, this->array_size
, state
);
1401 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1402 struct ir_variable
*var
,
1403 struct _mesa_glsl_parse_state
*state
,
1406 if (qual
->invariant
)
1409 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1410 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
1411 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1417 if (qual
->attribute
&& state
->target
!= vertex_shader
) {
1418 var
->type
= glsl_type::error_type
;
1419 _mesa_glsl_error(loc
, state
,
1420 "`attribute' variables may not be declared in the "
1422 _mesa_glsl_shader_target_name(state
->target
));
1425 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1427 * "The varying qualifier can be used only with the data types
1428 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1431 if (qual
->varying
&& var
->type
->base_type
!= GLSL_TYPE_FLOAT
) {
1432 var
->type
= glsl_type::error_type
;
1433 _mesa_glsl_error(loc
, state
,
1434 "varying variables must be of base type float");
1437 if (qual
->in
&& qual
->out
)
1438 var
->mode
= ir_var_inout
;
1439 else if (qual
->attribute
|| qual
->in
1440 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1441 var
->mode
= ir_var_in
;
1442 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
1443 var
->mode
= ir_var_out
;
1444 else if (qual
->uniform
)
1445 var
->mode
= ir_var_uniform
;
1447 var
->mode
= ir_var_auto
;
1450 var
->shader_in
= true;
1451 if (qual
->varying
) {
1453 var
->shader_in
= true;
1455 var
->shader_out
= true;
1459 var
->interpolation
= ir_var_flat
;
1460 else if (qual
->noperspective
)
1461 var
->interpolation
= ir_var_noperspective
;
1463 var
->interpolation
= ir_var_smooth
;
1465 if (var
->type
->is_array() && (state
->language_version
>= 120)) {
1466 var
->array_lvalue
= true;
1472 ast_declarator_list::hir(exec_list
*instructions
,
1473 struct _mesa_glsl_parse_state
*state
)
1475 struct simple_node
*ptr
;
1476 const struct glsl_type
*decl_type
;
1477 const char *type_name
= NULL
;
1478 ir_rvalue
*result
= NULL
;
1479 YYLTYPE loc
= this->get_location();
1481 /* The type specifier may contain a structure definition. Process that
1482 * before any of the variable declarations.
1484 (void) this->type
->specifier
->hir(instructions
, state
);
1486 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1487 * FINISHME: include a type. These re-declare built-in variables to be
1488 * FINISHME: invariant.
1491 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1492 if (is_empty_list(&this->declarations
)) {
1493 /* There are only two valid cases where the declaration list can be
1496 * 1. The declaration is setting the default precision of a built-in
1497 * type (e.g., 'precision highp vec4;').
1499 * 2. Adding 'invariant' to an existing vertex shader output.
1502 if (this->type
->qualifier
.invariant
) {
1503 } else if (decl_type
!= NULL
) {
1505 _mesa_glsl_error(& loc
, state
, "incomplete declaration");
1509 foreach (ptr
, &this->declarations
) {
1510 struct ast_declaration
*const decl
= (struct ast_declaration
* )ptr
;
1511 const struct glsl_type
*var_type
;
1512 struct ir_variable
*var
;
1514 /* FINISHME: Emit a warning if a variable declaration shadows a
1515 * FINISHME: declaration at a higher scope.
1518 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1519 if (type_name
!= NULL
) {
1520 _mesa_glsl_error(& loc
, state
,
1521 "invalid type `%s' in declaration of `%s'",
1522 type_name
, decl
->identifier
);
1524 _mesa_glsl_error(& loc
, state
,
1525 "invalid type in declaration of `%s'",
1531 if (decl
->is_array
) {
1532 var_type
= process_array_type(decl_type
, decl
->array_size
, state
);
1534 var_type
= decl_type
;
1537 var
= new ir_variable(var_type
, decl
->identifier
);
1539 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1541 * "Global variables can only use the qualifiers const,
1542 * attribute, uni form, or varying. Only one may be
1545 * Local variables can only use the qualifier const."
1547 * This is relaxed in GLSL 1.30.
1549 if (state
->language_version
< 120) {
1550 if (this->type
->qualifier
.out
) {
1551 _mesa_glsl_error(& loc
, state
,
1552 "`out' qualifier in declaration of `%s' "
1553 "only valid for function parameters in GLSL 1.10.",
1556 if (this->type
->qualifier
.in
) {
1557 _mesa_glsl_error(& loc
, state
,
1558 "`in' qualifier in declaration of `%s' "
1559 "only valid for function parameters in GLSL 1.10.",
1562 /* FINISHME: Test for other invalid qualifiers. */
1565 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1568 /* Attempt to add the variable to the symbol table. If this fails, it
1569 * means the variable has already been declared at this scope. Arrays
1570 * fudge this rule a little bit.
1572 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1574 * "It is legal to declare an array without a size and then
1575 * later re-declare the same name as an array of the same
1576 * type and specify a size."
1578 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
1579 ir_variable
*const earlier
=
1580 state
->symbols
->get_variable(decl
->identifier
);
1582 if ((earlier
!= NULL
)
1583 && (earlier
->type
->array_size() == 0)
1584 && var
->type
->is_array()
1585 && (var
->type
->element_type() == earlier
->type
->element_type())) {
1586 /* FINISHME: This doesn't match the qualifiers on the two
1587 * FINISHME: declarations. It's not 100% clear whether this is
1588 * FINISHME: required or not.
1591 if (var
->type
->array_size() <= (int)earlier
->max_array_access
) {
1592 YYLTYPE loc
= this->get_location();
1594 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
1596 earlier
->max_array_access
);
1599 earlier
->type
= var
->type
;
1603 YYLTYPE loc
= this->get_location();
1605 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
1612 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1614 * "Identifiers starting with "gl_" are reserved for use by
1615 * OpenGL, and may not be declared in a shader as either a
1616 * variable or a function."
1618 if (strncmp(decl
->identifier
, "gl_", 3) == 0) {
1619 /* FINISHME: This should only trigger if we're not redefining
1620 * FINISHME: a builtin (to add a qualifier, for example).
1622 _mesa_glsl_error(& loc
, state
,
1623 "identifier `%s' uses reserved `gl_' prefix",
1627 instructions
->push_tail(var
);
1629 if (state
->current_function
!= NULL
) {
1630 const char *mode
= NULL
;
1631 const char *extra
= "";
1633 /* There is no need to check for 'inout' here because the parser will
1634 * only allow that in function parameter lists.
1636 if (this->type
->qualifier
.attribute
) {
1638 } else if (this->type
->qualifier
.uniform
) {
1640 } else if (this->type
->qualifier
.varying
) {
1642 } else if (this->type
->qualifier
.in
) {
1644 extra
= " or in function parameter list";
1645 } else if (this->type
->qualifier
.out
) {
1647 extra
= " or in function parameter list";
1651 _mesa_glsl_error(& loc
, state
,
1652 "%s variable `%s' must be declared at "
1654 mode
, var
->name
, extra
);
1656 } else if (var
->mode
== ir_var_in
) {
1657 if (state
->target
== vertex_shader
) {
1658 bool error_emitted
= false;
1660 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1662 * "Vertex shader inputs can only be float, floating-point
1663 * vectors, matrices, signed and unsigned integers and integer
1664 * vectors. Vertex shader inputs can also form arrays of these
1665 * types, but not structures."
1667 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1669 * "Vertex shader inputs can only be float, floating-point
1670 * vectors, matrices, signed and unsigned integers and integer
1671 * vectors. They cannot be arrays or structures."
1673 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1675 * "The attribute qualifier can be used only with float,
1676 * floating-point vectors, and matrices. Attribute variables
1677 * cannot be declared as arrays or structures."
1679 const glsl_type
*check_type
= var
->type
->is_array()
1680 ? var
->type
->fields
.array
: var
->type
;
1682 switch (check_type
->base_type
) {
1683 case GLSL_TYPE_FLOAT
:
1685 case GLSL_TYPE_UINT
:
1687 if (state
->language_version
> 120)
1691 _mesa_glsl_error(& loc
, state
,
1692 "vertex shader input / attribute cannot have "
1694 var
->type
->is_array() ? "array of " : "",
1696 error_emitted
= true;
1699 if (!error_emitted
&& (state
->language_version
<= 130)
1700 && var
->type
->is_array()) {
1701 _mesa_glsl_error(& loc
, state
,
1702 "vertex shader input / attribute cannot have "
1704 error_emitted
= true;
1709 if (decl
->initializer
!= NULL
) {
1710 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
1712 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1714 * "All uniform variables are read-only and are initialized either
1715 * directly by an application via API commands, or indirectly by
1718 if ((state
->language_version
<= 110)
1719 && (var
->mode
== ir_var_uniform
)) {
1720 _mesa_glsl_error(& initializer_loc
, state
,
1721 "cannot initialize uniforms in GLSL 1.10");
1724 if (var
->type
->is_sampler()) {
1725 _mesa_glsl_error(& initializer_loc
, state
,
1726 "cannot initialize samplers");
1729 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1730 _mesa_glsl_error(& initializer_loc
, state
,
1731 "cannot initialize %s shader input / %s",
1732 _mesa_glsl_shader_target_name(state
->target
),
1733 (state
->target
== vertex_shader
)
1734 ? "attribute" : "varying");
1737 ir_dereference
*const lhs
= new ir_dereference(var
);
1738 ir_rvalue
*rhs
= decl
->initializer
->hir(instructions
, state
);
1740 /* Calculate the constant value if this is a const
1743 if (this->type
->qualifier
.constant
) {
1744 ir_constant
*constant_value
= rhs
->constant_expression_value();
1745 if (!constant_value
) {
1746 _mesa_glsl_error(& initializer_loc
, state
,
1747 "initializer of const variable `%s' must be a "
1748 "constant expression",
1751 rhs
= constant_value
;
1752 var
->constant_value
= constant_value
;
1756 if (rhs
&& !rhs
->type
->is_error()) {
1757 bool temp
= var
->read_only
;
1758 if (this->type
->qualifier
.constant
)
1759 var
->read_only
= false;
1760 result
= do_assignment(instructions
, state
, lhs
, rhs
,
1761 this->get_location());
1762 var
->read_only
= temp
;
1766 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1768 * "It is an error to write to a const variable outside of
1769 * its declaration, so they must be initialized when
1772 if (this->type
->qualifier
.constant
&& decl
->initializer
== NULL
) {
1773 _mesa_glsl_error(& loc
, state
,
1774 "const declaration of `%s' must be initialized");
1777 /* Add the vairable to the symbol table after processing the initializer.
1778 * This differs from most C-like languages, but it follows the GLSL
1779 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1782 * "Within a declaration, the scope of a name starts immediately
1783 * after the initializer if present or immediately after the name
1784 * being declared if not."
1786 const bool added_variable
=
1787 state
->symbols
->add_variable(decl
->identifier
, var
);
1788 assert(added_variable
);
1792 /* Generally, variable declarations do not have r-values. However,
1793 * one is used for the declaration in
1795 * while (bool b = some_condition()) {
1799 * so we return the rvalue from the last seen declaration here.
1806 ast_parameter_declarator::hir(exec_list
*instructions
,
1807 struct _mesa_glsl_parse_state
*state
)
1809 const struct glsl_type
*type
;
1810 const char *name
= NULL
;
1811 YYLTYPE loc
= this->get_location();
1813 type
= this->type
->specifier
->glsl_type(& name
, state
);
1817 _mesa_glsl_error(& loc
, state
,
1818 "invalid type `%s' in declaration of `%s'",
1819 name
, this->identifier
);
1821 _mesa_glsl_error(& loc
, state
,
1822 "invalid type in declaration of `%s'",
1826 type
= glsl_type::error_type
;
1829 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1831 * "Functions that accept no input arguments need not use void in the
1832 * argument list because prototypes (or definitions) are required and
1833 * therefore there is no ambiguity when an empty argument list "( )" is
1834 * declared. The idiom "(void)" as a parameter list is provided for
1837 * Placing this check here prevents a void parameter being set up
1838 * for a function, which avoids tripping up checks for main taking
1839 * parameters and lookups of an unnamed symbol.
1841 if (type
->is_void()) {
1842 if (this->identifier
!= NULL
)
1843 _mesa_glsl_error(& loc
, state
,
1844 "named parameter cannot have type `void'");
1850 if (formal_parameter
&& (this->identifier
== NULL
)) {
1851 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
1856 ir_variable
*var
= new ir_variable(type
, this->identifier
);
1858 /* FINISHME: Handle array declarations. Note that this requires
1859 * FINISHME: complete handling of constant expressions.
1862 /* Apply any specified qualifiers to the parameter declaration. Note that
1863 * for function parameters the default mode is 'in'.
1865 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
1866 if (var
->mode
== ir_var_auto
)
1867 var
->mode
= ir_var_in
;
1869 instructions
->push_tail(var
);
1871 /* Parameter declarations do not have r-values.
1878 ast_parameter_declarator::parameters_to_hir(struct simple_node
*ast_parameters
,
1880 exec_list
*ir_parameters
,
1881 _mesa_glsl_parse_state
*state
)
1883 struct simple_node
*ptr
;
1884 ast_parameter_declarator
*void_param
= NULL
;
1887 foreach (ptr
, ast_parameters
) {
1888 ast_parameter_declarator
*param
= (ast_parameter_declarator
*)ptr
;
1889 param
->formal_parameter
= formal
;
1890 param
->hir(ir_parameters
, state
);
1898 if ((void_param
!= NULL
) && (count
> 1)) {
1899 YYLTYPE loc
= void_param
->get_location();
1901 _mesa_glsl_error(& loc
, state
,
1902 "`void' parameter must be only parameter");
1908 ast_function::hir(exec_list
*instructions
,
1909 struct _mesa_glsl_parse_state
*state
)
1911 ir_function
*f
= NULL
;
1912 ir_function_signature
*sig
= NULL
;
1913 exec_list hir_parameters
;
1916 /* Convert the list of function parameters to HIR now so that they can be
1917 * used below to compare this function's signature with previously seen
1918 * signatures for functions with the same name.
1920 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
1922 & hir_parameters
, state
);
1924 const char *return_type_name
;
1925 const glsl_type
*return_type
=
1926 this->return_type
->specifier
->glsl_type(& return_type_name
, state
);
1928 assert(return_type
!= NULL
);
1930 /* Verify that this function's signature either doesn't match a previously
1931 * seen signature for a function with the same name, or, if a match is found,
1932 * that the previously seen signature does not have an associated definition.
1934 const char *const name
= identifier
;
1935 f
= state
->symbols
->get_function(name
);
1937 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
1939 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
1940 if (badvar
!= NULL
) {
1941 YYLTYPE loc
= this->get_location();
1943 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
1944 "qualifiers don't match prototype", name
, badvar
);
1947 if (sig
->return_type
!= return_type
) {
1948 YYLTYPE loc
= this->get_location();
1950 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
1951 "match prototype", name
);
1954 if (is_definition
&& sig
->is_defined
) {
1955 YYLTYPE loc
= this->get_location();
1957 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
1961 } else if (state
->symbols
->name_declared_this_scope(name
)) {
1962 /* This function name shadows a non-function use of the same name.
1964 YYLTYPE loc
= this->get_location();
1966 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
1967 "non-function", name
);
1970 f
= new ir_function(name
);
1971 state
->symbols
->add_function(f
->name
, f
);
1973 /* Emit the new function header */
1974 instructions
->push_tail(f
);
1977 /* Verify the return type of main() */
1978 if (strcmp(name
, "main") == 0) {
1979 if (! return_type
->is_void()) {
1980 YYLTYPE loc
= this->get_location();
1982 _mesa_glsl_error(& loc
, state
, "main() must return void");
1985 if (!hir_parameters
.is_empty()) {
1986 YYLTYPE loc
= this->get_location();
1988 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
1992 /* Finish storing the information about this new function in its signature.
1995 sig
= new ir_function_signature(return_type
);
1996 f
->add_signature(sig
);
1999 sig
->replace_parameters(&hir_parameters
);
2002 /* Function declarations (prototypes) do not have r-values.
2009 ast_function_definition::hir(exec_list
*instructions
,
2010 struct _mesa_glsl_parse_state
*state
)
2012 prototype
->is_definition
= true;
2013 prototype
->hir(instructions
, state
);
2015 ir_function_signature
*signature
= prototype
->signature
;
2017 assert(state
->current_function
== NULL
);
2018 state
->current_function
= signature
;
2020 /* Duplicate parameters declared in the prototype as concrete variables.
2021 * Add these to the symbol table.
2023 state
->symbols
->push_scope();
2024 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
2025 ir_variable
*const var
= ((ir_instruction
*) iter
.get())->as_variable();
2027 assert(var
!= NULL
);
2029 /* The only way a parameter would "exist" is if two parameters have
2032 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
2033 YYLTYPE loc
= this->get_location();
2035 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
2037 state
->symbols
->add_variable(var
->name
, var
);
2041 /* Convert the body of the function to HIR. */
2042 this->body
->hir(&signature
->body
, state
);
2043 signature
->is_defined
= true;
2045 state
->symbols
->pop_scope();
2047 assert(state
->current_function
== signature
);
2048 state
->current_function
= NULL
;
2050 /* Function definitions do not have r-values.
2057 ast_jump_statement::hir(exec_list
*instructions
,
2058 struct _mesa_glsl_parse_state
*state
)
2064 assert(state
->current_function
);
2066 if (opt_return_value
) {
2067 if (state
->current_function
->return_type
->base_type
==
2069 YYLTYPE loc
= this->get_location();
2071 _mesa_glsl_error(& loc
, state
,
2072 "`return` with a value, in function `%s' "
2074 state
->current_function
->function_name());
2077 ir_expression
*const ret
= (ir_expression
*)
2078 opt_return_value
->hir(instructions
, state
);
2079 assert(ret
!= NULL
);
2081 /* FINISHME: Make sure the type of the return value matches the return
2082 * FINISHME: type of the enclosing function.
2085 inst
= new ir_return(ret
);
2087 if (state
->current_function
->return_type
->base_type
!=
2089 YYLTYPE loc
= this->get_location();
2091 _mesa_glsl_error(& loc
, state
,
2092 "`return' with no value, in function %s returning "
2094 state
->current_function
->function_name());
2096 inst
= new ir_return
;
2099 instructions
->push_tail(inst
);
2104 /* FINISHME: discard support */
2105 if (state
->target
!= fragment_shader
) {
2106 YYLTYPE loc
= this->get_location();
2108 _mesa_glsl_error(& loc
, state
,
2109 "`discard' may only appear in a fragment shader");
2115 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2116 * FINISHME: and they use a different IR instruction for 'break'.
2118 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2119 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2122 if (state
->loop_or_switch_nesting
== NULL
) {
2123 YYLTYPE loc
= this->get_location();
2125 _mesa_glsl_error(& loc
, state
,
2126 "`%s' may only appear in a loop",
2127 (mode
== ast_break
) ? "break" : "continue");
2129 ir_loop
*const loop
= state
->loop_or_switch_nesting
->as_loop();
2132 ir_loop_jump
*const jump
=
2133 new ir_loop_jump(loop
,
2135 ? ir_loop_jump::jump_break
2136 : ir_loop_jump::jump_continue
);
2137 instructions
->push_tail(jump
);
2144 /* Jump instructions do not have r-values.
2151 ast_selection_statement::hir(exec_list
*instructions
,
2152 struct _mesa_glsl_parse_state
*state
)
2154 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
2156 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2158 * "Any expression whose type evaluates to a Boolean can be used as the
2159 * conditional expression bool-expression. Vector types are not accepted
2160 * as the expression to if."
2162 * The checks are separated so that higher quality diagnostics can be
2163 * generated for cases where both rules are violated.
2165 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
2166 YYLTYPE loc
= this->condition
->get_location();
2168 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
2172 ir_if
*const stmt
= new ir_if(condition
);
2174 if (then_statement
!= NULL
) {
2175 ast_node
*node
= (ast_node
*) then_statement
;
2177 node
->hir(& stmt
->then_instructions
, state
);
2178 node
= (ast_node
*) node
->next
;
2179 } while (node
!= then_statement
);
2182 if (else_statement
!= NULL
) {
2183 ast_node
*node
= (ast_node
*) else_statement
;
2185 node
->hir(& stmt
->else_instructions
, state
);
2186 node
= (ast_node
*) node
->next
;
2187 } while (node
!= else_statement
);
2190 instructions
->push_tail(stmt
);
2192 /* if-statements do not have r-values.
2199 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
2200 struct _mesa_glsl_parse_state
*state
)
2202 if (condition
!= NULL
) {
2203 ir_rvalue
*const cond
=
2204 condition
->hir(& stmt
->body_instructions
, state
);
2207 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
2208 YYLTYPE loc
= condition
->get_location();
2210 _mesa_glsl_error(& loc
, state
,
2211 "loop condition must be scalar boolean");
2213 /* As the first code in the loop body, generate a block that looks
2214 * like 'if (!condition) break;' as the loop termination condition.
2216 ir_rvalue
*const not_cond
=
2217 new ir_expression(ir_unop_logic_not
, glsl_type::bool_type
, cond
,
2220 ir_if
*const if_stmt
= new ir_if(not_cond
);
2222 ir_jump
*const break_stmt
=
2223 new ir_loop_jump(stmt
, ir_loop_jump::jump_break
);
2225 if_stmt
->then_instructions
.push_tail(break_stmt
);
2226 stmt
->body_instructions
.push_tail(if_stmt
);
2233 ast_iteration_statement::hir(exec_list
*instructions
,
2234 struct _mesa_glsl_parse_state
*state
)
2236 /* For-loops and while-loops start a new scope, but do-while loops do not.
2238 if (mode
!= ast_do_while
)
2239 state
->symbols
->push_scope();
2241 if (init_statement
!= NULL
)
2242 init_statement
->hir(instructions
, state
);
2244 ir_loop
*const stmt
= new ir_loop();
2245 instructions
->push_tail(stmt
);
2247 /* Track the current loop and / or switch-statement nesting.
2249 ir_instruction
*const nesting
= state
->loop_or_switch_nesting
;
2250 state
->loop_or_switch_nesting
= stmt
;
2252 if (mode
!= ast_do_while
)
2253 condition_to_hir(stmt
, state
);
2256 ast_node
*node
= (ast_node
*) body
;
2258 node
->hir(& stmt
->body_instructions
, state
);
2259 node
= (ast_node
*) node
->next
;
2260 } while (node
!= body
);
2263 if (rest_expression
!= NULL
)
2264 rest_expression
->hir(& stmt
->body_instructions
, state
);
2266 if (mode
== ast_do_while
)
2267 condition_to_hir(stmt
, state
);
2269 if (mode
!= ast_do_while
)
2270 state
->symbols
->pop_scope();
2272 /* Restore previous nesting before returning.
2274 state
->loop_or_switch_nesting
= nesting
;
2276 /* Loops do not have r-values.
2283 ast_type_specifier::hir(exec_list
*instructions
,
2284 struct _mesa_glsl_parse_state
*state
)
2286 if (this->structure
!= NULL
)
2287 return this->structure
->hir(instructions
, state
);
2294 ast_struct_specifier::hir(exec_list
*instructions
,
2295 struct _mesa_glsl_parse_state
*state
)
2298 unsigned decl_count
= 0;
2300 /* Make an initial pass over the list of structure fields to determine how
2301 * many there are. Each element in this list is an ast_declarator_list.
2302 * This means that we actually need to count the number of elements in the
2303 * 'declarations' list in each of the elements.
2305 foreach (ptr
, & this->declarations
) {
2306 ast_declarator_list
*decl_list
= (ast_declarator_list
*) ptr
;
2307 simple_node
*decl_ptr
;
2309 foreach (decl_ptr
, & decl_list
->declarations
) {
2315 /* Allocate storage for the structure fields and process the field
2316 * declarations. As the declarations are processed, try to also convert
2317 * the types to HIR. This ensures that structure definitions embedded in
2318 * other structure definitions are processed.
2320 glsl_struct_field
*const fields
= (glsl_struct_field
*)
2321 malloc(sizeof(*fields
) * decl_count
);
2324 foreach (ptr
, & this->declarations
) {
2325 ast_declarator_list
*decl_list
= (ast_declarator_list
*) ptr
;
2326 simple_node
*decl_ptr
;
2327 const char *type_name
;
2329 decl_list
->type
->specifier
->hir(instructions
, state
);
2331 const glsl_type
*decl_type
=
2332 decl_list
->type
->specifier
->glsl_type(& type_name
, state
);
2334 foreach (decl_ptr
, & decl_list
->declarations
) {
2335 ast_declaration
*const decl
= (ast_declaration
*) decl_ptr
;
2336 const struct glsl_type
*const field_type
=
2338 ? process_array_type(decl_type
, decl
->array_size
, state
)
2341 fields
[i
].type
= (field_type
!= NULL
)
2342 ? field_type
: glsl_type::error_type
;
2343 fields
[i
].name
= decl
->identifier
;
2348 assert(i
== decl_count
);
2351 if (this->name
== NULL
) {
2352 static unsigned anon_count
= 1;
2355 snprintf(buf
, sizeof(buf
), "#anon_struct_%04x", anon_count
);
2363 glsl_type
*t
= new glsl_type(fields
, decl_count
, name
);
2365 YYLTYPE loc
= this->get_location();
2366 if (!state
->symbols
->add_type(name
, t
)) {
2367 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
2369 /* This logic is a bit tricky. It is an error to declare a structure at
2370 * global scope if there is also a function with the same name.
2372 if ((state
->current_function
== NULL
)
2373 && (state
->symbols
->get_function(name
) != NULL
)) {
2374 _mesa_glsl_error(& loc
, state
, "name `%s' previously defined", name
);
2376 t
->generate_constructor(state
->symbols
);
2379 const glsl_type
**s
= (const glsl_type
**)
2380 realloc(state
->user_structures
,
2381 sizeof(state
->user_structures
[0]) *
2382 (state
->num_user_structures
+ 1));
2384 s
[state
->num_user_structures
] = t
;
2385 state
->user_structures
= s
;
2386 state
->num_user_structures
++;
2390 /* Structure type definitions do not have r-values.