2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
31 * * Symbol table management
35 * The majority of this work could be done during parsing, and the parser could
36 * probably generate HIR directly. However, this results in frequent changes
37 * to the parser code. Since we do not assume that every system this complier
38 * is built on will have Flex and Bison installed, we have to store the code
39 * generated by these tools in our version control system. In other parts of
40 * the system we've seen problems where a parser was changed but the generated
41 * code was not committed, merge conflicts where created because two developers
42 * had slightly different versions of Bison installed, etc.
44 * I have also noticed that running Bison generated parsers in GDB is very
45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very
46 * well 'print $1' in GDB.
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
52 #include "main/core.h" /* for struct gl_extensions */
53 #include "glsl_symbol_table.h"
54 #include "glsl_parser_extras.h"
56 #include "glsl_types.h"
60 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
62 _mesa_glsl_initialize_variables(instructions
, state
);
63 _mesa_glsl_initialize_functions(instructions
, state
);
65 state
->symbols
->language_version
= state
->language_version
;
67 state
->current_function
= NULL
;
69 /* Section 4.2 of the GLSL 1.20 specification states:
70 * "The built-in functions are scoped in a scope outside the global scope
71 * users declare global variables in. That is, a shader's global scope,
72 * available for user-defined functions and global variables, is nested
73 * inside the scope containing the built-in functions."
75 * Since built-in functions like ftransform() access built-in variables,
76 * it follows that those must be in the outer scope as well.
78 * We push scope here to create this nesting effect...but don't pop.
79 * This way, a shader's globals are still in the symbol table for use
82 state
->symbols
->push_scope();
84 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
85 ast
->hir(instructions
, state
);
90 * If a conversion is available, convert one operand to a different type
92 * The \c from \c ir_rvalue is converted "in place".
94 * \param to Type that the operand it to be converted to
95 * \param from Operand that is being converted
96 * \param state GLSL compiler state
99 * If a conversion is possible (or unnecessary), \c true is returned.
100 * Otherwise \c false is returned.
103 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
104 struct _mesa_glsl_parse_state
*state
)
107 if (to
->base_type
== from
->type
->base_type
)
110 /* This conversion was added in GLSL 1.20. If the compilation mode is
111 * GLSL 1.10, the conversion is skipped.
113 if (state
->language_version
< 120)
116 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
118 * "There are no implicit array or structure conversions. For
119 * example, an array of int cannot be implicitly converted to an
120 * array of float. There are no implicit conversions between
121 * signed and unsigned integers."
123 /* FINISHME: The above comment is partially a lie. There is int/uint
124 * FINISHME: conversion for immediate constants.
126 if (!to
->is_float() || !from
->type
->is_numeric())
129 /* Convert to a floating point type with the same number of components
130 * as the original type - i.e. int to float, not int to vec4.
132 to
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, from
->type
->vector_elements
,
133 from
->type
->matrix_columns
);
135 switch (from
->type
->base_type
) {
137 from
= new(ctx
) ir_expression(ir_unop_i2f
, to
, from
, NULL
);
140 from
= new(ctx
) ir_expression(ir_unop_u2f
, to
, from
, NULL
);
143 from
= new(ctx
) ir_expression(ir_unop_b2f
, to
, from
, NULL
);
153 static const struct glsl_type
*
154 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
156 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
158 const glsl_type
*type_a
= value_a
->type
;
159 const glsl_type
*type_b
= value_b
->type
;
161 /* From GLSL 1.50 spec, page 56:
163 * "The arithmetic binary operators add (+), subtract (-),
164 * multiply (*), and divide (/) operate on integer and
165 * floating-point scalars, vectors, and matrices."
167 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
168 _mesa_glsl_error(loc
, state
,
169 "Operands to arithmetic operators must be numeric");
170 return glsl_type::error_type
;
174 /* "If one operand is floating-point based and the other is
175 * not, then the conversions from Section 4.1.10 "Implicit
176 * Conversions" are applied to the non-floating-point-based operand."
178 if (!apply_implicit_conversion(type_a
, value_b
, state
)
179 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
180 _mesa_glsl_error(loc
, state
,
181 "Could not implicitly convert operands to "
182 "arithmetic operator");
183 return glsl_type::error_type
;
185 type_a
= value_a
->type
;
186 type_b
= value_b
->type
;
188 /* "If the operands are integer types, they must both be signed or
191 * From this rule and the preceeding conversion it can be inferred that
192 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
193 * The is_numeric check above already filtered out the case where either
194 * type is not one of these, so now the base types need only be tested for
197 if (type_a
->base_type
!= type_b
->base_type
) {
198 _mesa_glsl_error(loc
, state
,
199 "base type mismatch for arithmetic operator");
200 return glsl_type::error_type
;
203 /* "All arithmetic binary operators result in the same fundamental type
204 * (signed integer, unsigned integer, or floating-point) as the
205 * operands they operate on, after operand type conversion. After
206 * conversion, the following cases are valid
208 * * The two operands are scalars. In this case the operation is
209 * applied, resulting in a scalar."
211 if (type_a
->is_scalar() && type_b
->is_scalar())
214 /* "* One operand is a scalar, and the other is a vector or matrix.
215 * In this case, the scalar operation is applied independently to each
216 * component of the vector or matrix, resulting in the same size
219 if (type_a
->is_scalar()) {
220 if (!type_b
->is_scalar())
222 } else if (type_b
->is_scalar()) {
226 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
227 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
230 assert(!type_a
->is_scalar());
231 assert(!type_b
->is_scalar());
233 /* "* The two operands are vectors of the same size. In this case, the
234 * operation is done component-wise resulting in the same size
237 if (type_a
->is_vector() && type_b
->is_vector()) {
238 if (type_a
== type_b
) {
241 _mesa_glsl_error(loc
, state
,
242 "vector size mismatch for arithmetic operator");
243 return glsl_type::error_type
;
247 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
248 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
249 * <vector, vector> have been handled. At least one of the operands must
250 * be matrix. Further, since there are no integer matrix types, the base
251 * type of both operands must be float.
253 assert(type_a
->is_matrix() || type_b
->is_matrix());
254 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
255 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
257 /* "* The operator is add (+), subtract (-), or divide (/), and the
258 * operands are matrices with the same number of rows and the same
259 * number of columns. In this case, the operation is done component-
260 * wise resulting in the same size matrix."
261 * * The operator is multiply (*), where both operands are matrices or
262 * one operand is a vector and the other a matrix. A right vector
263 * operand is treated as a column vector and a left vector operand as a
264 * row vector. In all these cases, it is required that the number of
265 * columns of the left operand is equal to the number of rows of the
266 * right operand. Then, the multiply (*) operation does a linear
267 * algebraic multiply, yielding an object that has the same number of
268 * rows as the left operand and the same number of columns as the right
269 * operand. Section 5.10 "Vector and Matrix Operations" explains in
270 * more detail how vectors and matrices are operated on."
273 if (type_a
== type_b
)
276 if (type_a
->is_matrix() && type_b
->is_matrix()) {
277 /* Matrix multiply. The columns of A must match the rows of B. Given
278 * the other previously tested constraints, this means the vector type
279 * of a row from A must be the same as the vector type of a column from
282 if (type_a
->row_type() == type_b
->column_type()) {
283 /* The resulting matrix has the number of columns of matrix B and
284 * the number of rows of matrix A. We get the row count of A by
285 * looking at the size of a vector that makes up a column. The
286 * transpose (size of a row) is done for B.
288 const glsl_type
*const type
=
289 glsl_type::get_instance(type_a
->base_type
,
290 type_a
->column_type()->vector_elements
,
291 type_b
->row_type()->vector_elements
);
292 assert(type
!= glsl_type::error_type
);
296 } else if (type_a
->is_matrix()) {
297 /* A is a matrix and B is a column vector. Columns of A must match
298 * rows of B. Given the other previously tested constraints, this
299 * means the vector type of a row from A must be the same as the
300 * vector the type of B.
302 if (type_a
->row_type() == type_b
) {
303 /* The resulting vector has a number of elements equal to
304 * the number of rows of matrix A. */
305 const glsl_type
*const type
=
306 glsl_type::get_instance(type_a
->base_type
,
307 type_a
->column_type()->vector_elements
,
309 assert(type
!= glsl_type::error_type
);
314 assert(type_b
->is_matrix());
316 /* A is a row vector and B is a matrix. Columns of A must match rows
317 * of B. Given the other previously tested constraints, this means
318 * the type of A must be the same as the vector type of a column from
321 if (type_a
== type_b
->column_type()) {
322 /* The resulting vector has a number of elements equal to
323 * the number of columns of matrix B. */
324 const glsl_type
*const type
=
325 glsl_type::get_instance(type_a
->base_type
,
326 type_b
->row_type()->vector_elements
,
328 assert(type
!= glsl_type::error_type
);
334 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
335 return glsl_type::error_type
;
339 /* "All other cases are illegal."
341 _mesa_glsl_error(loc
, state
, "type mismatch");
342 return glsl_type::error_type
;
346 static const struct glsl_type
*
347 unary_arithmetic_result_type(const struct glsl_type
*type
,
348 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
350 /* From GLSL 1.50 spec, page 57:
352 * "The arithmetic unary operators negate (-), post- and pre-increment
353 * and decrement (-- and ++) operate on integer or floating-point
354 * values (including vectors and matrices). All unary operators work
355 * component-wise on their operands. These result with the same type
358 if (!type
->is_numeric()) {
359 _mesa_glsl_error(loc
, state
,
360 "Operands to arithmetic operators must be numeric");
361 return glsl_type::error_type
;
368 static const struct glsl_type
*
369 modulus_result_type(const struct glsl_type
*type_a
,
370 const struct glsl_type
*type_b
,
371 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
373 /* From GLSL 1.50 spec, page 56:
374 * "The operator modulus (%) operates on signed or unsigned integers or
375 * integer vectors. The operand types must both be signed or both be
378 if (!type_a
->is_integer() || !type_b
->is_integer()
379 || (type_a
->base_type
!= type_b
->base_type
)) {
380 _mesa_glsl_error(loc
, state
, "type mismatch");
381 return glsl_type::error_type
;
384 /* "The operands cannot be vectors of differing size. If one operand is
385 * a scalar and the other vector, then the scalar is applied component-
386 * wise to the vector, resulting in the same type as the vector. If both
387 * are vectors of the same size, the result is computed component-wise."
389 if (type_a
->is_vector()) {
390 if (!type_b
->is_vector()
391 || (type_a
->vector_elements
== type_b
->vector_elements
))
396 /* "The operator modulus (%) is not defined for any other data types
397 * (non-integer types)."
399 _mesa_glsl_error(loc
, state
, "type mismatch");
400 return glsl_type::error_type
;
404 static const struct glsl_type
*
405 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
406 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
408 const glsl_type
*type_a
= value_a
->type
;
409 const glsl_type
*type_b
= value_b
->type
;
411 /* From GLSL 1.50 spec, page 56:
412 * "The relational operators greater than (>), less than (<), greater
413 * than or equal (>=), and less than or equal (<=) operate only on
414 * scalar integer and scalar floating-point expressions."
416 if (!type_a
->is_numeric()
417 || !type_b
->is_numeric()
418 || !type_a
->is_scalar()
419 || !type_b
->is_scalar()) {
420 _mesa_glsl_error(loc
, state
,
421 "Operands to relational operators must be scalar and "
423 return glsl_type::error_type
;
426 /* "Either the operands' types must match, or the conversions from
427 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
428 * operand, after which the types must match."
430 if (!apply_implicit_conversion(type_a
, value_b
, state
)
431 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
432 _mesa_glsl_error(loc
, state
,
433 "Could not implicitly convert operands to "
434 "relational operator");
435 return glsl_type::error_type
;
437 type_a
= value_a
->type
;
438 type_b
= value_b
->type
;
440 if (type_a
->base_type
!= type_b
->base_type
) {
441 _mesa_glsl_error(loc
, state
, "base type mismatch");
442 return glsl_type::error_type
;
445 /* "The result is scalar Boolean."
447 return glsl_type::bool_type
;
451 * \brief Return the result type of a bit-shift operation.
453 * If the given types to the bit-shift operator are invalid, return
454 * glsl_type::error_type.
456 * \param type_a Type of LHS of bit-shift op
457 * \param type_b Type of RHS of bit-shift op
459 static const struct glsl_type
*
460 shift_result_type(const struct glsl_type
*type_a
,
461 const struct glsl_type
*type_b
,
463 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
465 if (state
->language_version
< 130) {
466 _mesa_glsl_error(loc
, state
, "bit operations require GLSL 1.30");
467 return glsl_type::error_type
;
470 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
472 * "The shift operators (<<) and (>>). For both operators, the operands
473 * must be signed or unsigned integers or integer vectors. One operand
474 * can be signed while the other is unsigned."
476 if (!type_a
->is_integer()) {
477 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
478 "integer vector", ast_expression::operator_string(op
));
479 return glsl_type::error_type
;
482 if (!type_b
->is_integer()) {
483 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
484 "integer vector", ast_expression::operator_string(op
));
485 return glsl_type::error_type
;
488 /* "If the first operand is a scalar, the second operand has to be
491 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
492 _mesa_glsl_error(loc
, state
, "If the first operand of %s is scalar, the "
493 "second must be scalar as well",
494 ast_expression::operator_string(op
));
495 return glsl_type::error_type
;
498 /* If both operands are vectors, check that they have same number of
501 if (type_a
->is_vector() &&
502 type_b
->is_vector() &&
503 type_a
->vector_elements
!= type_b
->vector_elements
) {
504 _mesa_glsl_error(loc
, state
, "Vector operands to operator %s must "
505 "have same number of elements",
506 ast_expression::operator_string(op
));
507 return glsl_type::error_type
;
510 /* "In all cases, the resulting type will be the same type as the left
517 * Validates that a value can be assigned to a location with a specified type
519 * Validates that \c rhs can be assigned to some location. If the types are
520 * not an exact match but an automatic conversion is possible, \c rhs will be
524 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
525 * Otherwise the actual RHS to be assigned will be returned. This may be
526 * \c rhs, or it may be \c rhs after some type conversion.
529 * In addition to being used for assignments, this function is used to
530 * type-check return values.
533 validate_assignment(struct _mesa_glsl_parse_state
*state
,
534 const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
536 const glsl_type
*rhs_type
= rhs
->type
;
538 /* If there is already some error in the RHS, just return it. Anything
539 * else will lead to an avalanche of error message back to the user.
541 if (rhs_type
->is_error())
544 /* If the types are identical, the assignment can trivially proceed.
546 if (rhs_type
== lhs_type
)
549 /* If the array element types are the same and the size of the LHS is zero,
550 * the assignment is okay.
552 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
553 * is handled by ir_dereference::is_lvalue.
555 if (lhs_type
->is_array() && rhs
->type
->is_array()
556 && (lhs_type
->element_type() == rhs
->type
->element_type())
557 && (lhs_type
->array_size() == 0)) {
561 /* Check for implicit conversion in GLSL 1.20 */
562 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
563 rhs_type
= rhs
->type
;
564 if (rhs_type
== lhs_type
)
572 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
573 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
577 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
579 if (!error_emitted
) {
580 if (!lhs
->is_lvalue()) {
581 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
582 error_emitted
= true;
585 if (state
->es_shader
&& lhs
->type
->is_array()) {
586 _mesa_glsl_error(&lhs_loc
, state
, "whole array assignment is not "
587 "allowed in GLSL ES 1.00.");
588 error_emitted
= true;
592 ir_rvalue
*new_rhs
= validate_assignment(state
, lhs
->type
, rhs
);
593 if (new_rhs
== NULL
) {
594 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
598 /* If the LHS array was not declared with a size, it takes it size from
599 * the RHS. If the LHS is an l-value and a whole array, it must be a
600 * dereference of a variable. Any other case would require that the LHS
601 * is either not an l-value or not a whole array.
603 if (lhs
->type
->array_size() == 0) {
604 ir_dereference
*const d
= lhs
->as_dereference();
608 ir_variable
*const var
= d
->variable_referenced();
612 if (var
->max_array_access
>= unsigned(rhs
->type
->array_size())) {
613 /* FINISHME: This should actually log the location of the RHS. */
614 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
616 var
->max_array_access
);
619 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
620 rhs
->type
->array_size());
625 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
626 * but not post_inc) need the converted assigned value as an rvalue
627 * to handle things like:
631 * So we always just store the computed value being assigned to a
632 * temporary and return a deref of that temporary. If the rvalue
633 * ends up not being used, the temp will get copy-propagated out.
635 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
637 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
638 instructions
->push_tail(var
);
639 instructions
->push_tail(new(ctx
) ir_assignment(deref_var
,
642 deref_var
= new(ctx
) ir_dereference_variable(var
);
645 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
, NULL
));
647 return new(ctx
) ir_dereference_variable(var
);
651 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
653 void *ctx
= talloc_parent(lvalue
);
656 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
658 instructions
->push_tail(var
);
659 var
->mode
= ir_var_auto
;
661 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
664 /* Once we've created this temporary, mark it read only so it's no
665 * longer considered an lvalue.
667 var
->read_only
= true;
669 return new(ctx
) ir_dereference_variable(var
);
674 ast_node::hir(exec_list
*instructions
,
675 struct _mesa_glsl_parse_state
*state
)
685 ast_expression::hir(exec_list
*instructions
,
686 struct _mesa_glsl_parse_state
*state
)
689 static const int operations
[AST_NUM_OPERATORS
] = {
690 -1, /* ast_assign doesn't convert to ir_expression. */
691 -1, /* ast_plus doesn't convert to ir_expression. */
715 /* Note: The following block of expression types actually convert
716 * to multiple IR instructions.
718 ir_binop_mul
, /* ast_mul_assign */
719 ir_binop_div
, /* ast_div_assign */
720 ir_binop_mod
, /* ast_mod_assign */
721 ir_binop_add
, /* ast_add_assign */
722 ir_binop_sub
, /* ast_sub_assign */
723 ir_binop_lshift
, /* ast_ls_assign */
724 ir_binop_rshift
, /* ast_rs_assign */
725 ir_binop_bit_and
, /* ast_and_assign */
726 ir_binop_bit_xor
, /* ast_xor_assign */
727 ir_binop_bit_or
, /* ast_or_assign */
729 -1, /* ast_conditional doesn't convert to ir_expression. */
730 ir_binop_add
, /* ast_pre_inc. */
731 ir_binop_sub
, /* ast_pre_dec. */
732 ir_binop_add
, /* ast_post_inc. */
733 ir_binop_sub
, /* ast_post_dec. */
734 -1, /* ast_field_selection doesn't conv to ir_expression. */
735 -1, /* ast_array_index doesn't convert to ir_expression. */
736 -1, /* ast_function_call doesn't conv to ir_expression. */
737 -1, /* ast_identifier doesn't convert to ir_expression. */
738 -1, /* ast_int_constant doesn't convert to ir_expression. */
739 -1, /* ast_uint_constant doesn't conv to ir_expression. */
740 -1, /* ast_float_constant doesn't conv to ir_expression. */
741 -1, /* ast_bool_constant doesn't conv to ir_expression. */
742 -1, /* ast_sequence doesn't convert to ir_expression. */
744 ir_rvalue
*result
= NULL
;
746 const struct glsl_type
*type
= glsl_type::error_type
;
747 bool error_emitted
= false;
750 loc
= this->get_location();
752 switch (this->oper
) {
754 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
755 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
757 result
= do_assignment(instructions
, state
, op
[0], op
[1],
758 this->subexpressions
[0]->get_location());
759 error_emitted
= result
->type
->is_error();
765 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
767 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
769 error_emitted
= type
->is_error();
775 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
777 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
779 error_emitted
= type
->is_error();
781 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
789 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
790 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
792 type
= arithmetic_result_type(op
[0], op
[1],
793 (this->oper
== ast_mul
),
795 error_emitted
= type
->is_error();
797 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
802 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
803 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
805 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
807 assert(operations
[this->oper
] == ir_binop_mod
);
809 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
811 error_emitted
= type
->is_error();
816 if (state
->language_version
< 130) {
817 _mesa_glsl_error(&loc
, state
, "operator %s requires GLSL 1.30",
818 operator_string(this->oper
));
819 error_emitted
= true;
822 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
823 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
824 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
826 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
828 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
835 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
836 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
838 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
840 /* The relational operators must either generate an error or result
841 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
843 assert(type
->is_error()
844 || ((type
->base_type
== GLSL_TYPE_BOOL
)
845 && type
->is_scalar()));
847 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
849 error_emitted
= type
->is_error();
854 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
855 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
857 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
859 * "The equality operators equal (==), and not equal (!=)
860 * operate on all types. They result in a scalar Boolean. If
861 * the operand types do not match, then there must be a
862 * conversion from Section 4.1.10 "Implicit Conversions"
863 * applied to one operand that can make them match, in which
864 * case this conversion is done."
866 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
867 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
868 || (op
[0]->type
!= op
[1]->type
)) {
869 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
870 "type", (this->oper
== ast_equal
) ? "==" : "!=");
871 error_emitted
= true;
872 } else if ((state
->language_version
<= 110)
873 && (op
[0]->type
->is_array() || op
[1]->type
->is_array())) {
874 _mesa_glsl_error(& loc
, state
, "array comparisons forbidden in "
876 error_emitted
= true;
879 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
881 type
= glsl_type::bool_type
;
883 assert(result
->type
== glsl_type::bool_type
);
889 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
890 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
892 if (state
->language_version
< 130) {
893 _mesa_glsl_error(&loc
, state
, "bit-wise operations require GLSL 1.30");
894 error_emitted
= true;
897 if (!op
[0]->type
->is_integer()) {
898 _mesa_glsl_error(&loc
, state
, "LHS of `%s' must be an integer",
899 operator_string(this->oper
));
900 error_emitted
= true;
903 if (!op
[1]->type
->is_integer()) {
904 _mesa_glsl_error(&loc
, state
, "RHS of `%s' must be an integer",
905 operator_string(this->oper
));
906 error_emitted
= true;
909 if (op
[0]->type
->base_type
!= op
[1]->type
->base_type
) {
910 _mesa_glsl_error(&loc
, state
, "operands of `%s' must have the same "
911 "base type", operator_string(this->oper
));
912 error_emitted
= true;
915 if (op
[0]->type
->is_vector() && op
[1]->type
->is_vector()
916 && op
[0]->type
->vector_elements
!= op
[1]->type
->vector_elements
) {
917 _mesa_glsl_error(&loc
, state
, "operands of `%s' cannot be vectors of "
918 "different sizes", operator_string(this->oper
));
919 error_emitted
= true;
922 type
= op
[0]->type
->is_scalar() ? op
[1]->type
: op
[0]->type
;
923 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
925 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
929 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
931 if (state
->language_version
< 130) {
932 _mesa_glsl_error(&loc
, state
, "bit-wise operations require GLSL 1.30");
933 error_emitted
= true;
936 if (!op
[0]->type
->is_integer()) {
937 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
938 error_emitted
= true;
942 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
945 case ast_logic_and
: {
946 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
948 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
949 YYLTYPE loc
= this->subexpressions
[0]->get_location();
951 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
952 operator_string(this->oper
));
953 error_emitted
= true;
956 ir_constant
*op0_const
= op
[0]->constant_expression_value();
958 if (op0_const
->value
.b
[0]) {
959 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
961 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
962 YYLTYPE loc
= this->subexpressions
[1]->get_location();
964 _mesa_glsl_error(& loc
, state
,
965 "RHS of `%s' must be scalar boolean",
966 operator_string(this->oper
));
967 error_emitted
= true;
973 type
= glsl_type::bool_type
;
975 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
978 instructions
->push_tail(tmp
);
980 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
981 instructions
->push_tail(stmt
);
983 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
985 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
986 YYLTYPE loc
= this->subexpressions
[1]->get_location();
988 _mesa_glsl_error(& loc
, state
,
989 "RHS of `%s' must be scalar boolean",
990 operator_string(this->oper
));
991 error_emitted
= true;
994 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
995 ir_assignment
*const then_assign
=
996 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
997 stmt
->then_instructions
.push_tail(then_assign
);
999 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1000 ir_assignment
*const else_assign
=
1001 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false), NULL
);
1002 stmt
->else_instructions
.push_tail(else_assign
);
1004 result
= new(ctx
) ir_dereference_variable(tmp
);
1010 case ast_logic_or
: {
1011 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1013 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
1014 YYLTYPE loc
= this->subexpressions
[0]->get_location();
1016 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
1017 operator_string(this->oper
));
1018 error_emitted
= true;
1021 ir_constant
*op0_const
= op
[0]->constant_expression_value();
1023 if (op0_const
->value
.b
[0]) {
1026 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1028 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
1029 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1031 _mesa_glsl_error(& loc
, state
,
1032 "RHS of `%s' must be scalar boolean",
1033 operator_string(this->oper
));
1034 error_emitted
= true;
1038 type
= glsl_type::bool_type
;
1040 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1043 instructions
->push_tail(tmp
);
1045 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1046 instructions
->push_tail(stmt
);
1048 op
[1] = this->subexpressions
[1]->hir(&stmt
->else_instructions
, state
);
1050 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
1051 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1053 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
1054 operator_string(this->oper
));
1055 error_emitted
= true;
1058 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1059 ir_assignment
*const then_assign
=
1060 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true), NULL
);
1061 stmt
->then_instructions
.push_tail(then_assign
);
1063 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1064 ir_assignment
*const else_assign
=
1065 new(ctx
) ir_assignment(else_deref
, op
[1], NULL
);
1066 stmt
->else_instructions
.push_tail(else_assign
);
1068 result
= new(ctx
) ir_dereference_variable(tmp
);
1075 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1076 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1079 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1081 type
= glsl_type::bool_type
;
1085 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1087 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
1088 YYLTYPE loc
= this->subexpressions
[0]->get_location();
1090 _mesa_glsl_error(& loc
, state
,
1091 "operand of `!' must be scalar boolean");
1092 error_emitted
= true;
1095 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1097 type
= glsl_type::bool_type
;
1100 case ast_mul_assign
:
1101 case ast_div_assign
:
1102 case ast_add_assign
:
1103 case ast_sub_assign
: {
1104 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1105 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1107 type
= arithmetic_result_type(op
[0], op
[1],
1108 (this->oper
== ast_mul_assign
),
1111 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1114 result
= do_assignment(instructions
, state
,
1115 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1116 this->subexpressions
[0]->get_location());
1117 type
= result
->type
;
1118 error_emitted
= (op
[0]->type
->is_error());
1120 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1121 * explicitly test for this because none of the binary expression
1122 * operators allow array operands either.
1128 case ast_mod_assign
: {
1129 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1130 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1132 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1134 assert(operations
[this->oper
] == ir_binop_mod
);
1136 ir_rvalue
*temp_rhs
;
1137 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1140 result
= do_assignment(instructions
, state
,
1141 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1142 this->subexpressions
[0]->get_location());
1143 type
= result
->type
;
1144 error_emitted
= type
->is_error();
1150 _mesa_glsl_error(& loc
, state
,
1151 "FINISHME: implement bit-shift assignment operators");
1152 error_emitted
= true;
1155 case ast_and_assign
:
1156 case ast_xor_assign
:
1158 _mesa_glsl_error(& loc
, state
,
1159 "FINISHME: implement logic assignment operators");
1160 error_emitted
= true;
1163 case ast_conditional
: {
1164 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1166 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1168 * "The ternary selection operator (?:). It operates on three
1169 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1170 * first expression, which must result in a scalar Boolean."
1172 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
1173 YYLTYPE loc
= this->subexpressions
[0]->get_location();
1175 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
1176 error_emitted
= true;
1179 /* The :? operator is implemented by generating an anonymous temporary
1180 * followed by an if-statement. The last instruction in each branch of
1181 * the if-statement assigns a value to the anonymous temporary. This
1182 * temporary is the r-value of the expression.
1184 exec_list then_instructions
;
1185 exec_list else_instructions
;
1187 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1188 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1190 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1192 * "The second and third expressions can be any type, as
1193 * long their types match, or there is a conversion in
1194 * Section 4.1.10 "Implicit Conversions" that can be applied
1195 * to one of the expressions to make their types match. This
1196 * resulting matching type is the type of the entire
1199 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1200 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1201 || (op
[1]->type
!= op
[2]->type
)) {
1202 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1204 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
1205 "operator must have matching types.");
1206 error_emitted
= true;
1207 type
= glsl_type::error_type
;
1212 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1214 * "The second and third expressions must be the same type, but can
1215 * be of any type other than an array."
1217 if ((state
->language_version
<= 110) && type
->is_array()) {
1218 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
1219 "operator must not be arrays.");
1220 error_emitted
= true;
1223 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1224 ir_constant
*then_val
= op
[1]->constant_expression_value();
1225 ir_constant
*else_val
= op
[2]->constant_expression_value();
1227 if (then_instructions
.is_empty()
1228 && else_instructions
.is_empty()
1229 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1230 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1232 ir_variable
*const tmp
=
1233 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1234 instructions
->push_tail(tmp
);
1236 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1237 instructions
->push_tail(stmt
);
1239 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1240 ir_dereference
*const then_deref
=
1241 new(ctx
) ir_dereference_variable(tmp
);
1242 ir_assignment
*const then_assign
=
1243 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
1244 stmt
->then_instructions
.push_tail(then_assign
);
1246 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1247 ir_dereference
*const else_deref
=
1248 new(ctx
) ir_dereference_variable(tmp
);
1249 ir_assignment
*const else_assign
=
1250 new(ctx
) ir_assignment(else_deref
, op
[2], NULL
);
1251 stmt
->else_instructions
.push_tail(else_assign
);
1253 result
= new(ctx
) ir_dereference_variable(tmp
);
1260 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1261 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1262 op
[1] = new(ctx
) ir_constant(1.0f
);
1264 op
[1] = new(ctx
) ir_constant(1);
1266 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1268 ir_rvalue
*temp_rhs
;
1269 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1272 result
= do_assignment(instructions
, state
,
1273 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1274 this->subexpressions
[0]->get_location());
1275 type
= result
->type
;
1276 error_emitted
= op
[0]->type
->is_error();
1281 case ast_post_dec
: {
1282 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1283 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1284 op
[1] = new(ctx
) ir_constant(1.0f
);
1286 op
[1] = new(ctx
) ir_constant(1);
1288 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1290 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1292 ir_rvalue
*temp_rhs
;
1293 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1296 /* Get a temporary of a copy of the lvalue before it's modified.
1297 * This may get thrown away later.
1299 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1301 (void)do_assignment(instructions
, state
,
1302 op
[0]->clone(ctx
, NULL
), temp_rhs
,
1303 this->subexpressions
[0]->get_location());
1305 type
= result
->type
;
1306 error_emitted
= op
[0]->type
->is_error();
1310 case ast_field_selection
:
1311 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1312 type
= result
->type
;
1315 case ast_array_index
: {
1316 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1318 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1319 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1321 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1323 ir_rvalue
*const array
= op
[0];
1325 result
= new(ctx
) ir_dereference_array(op
[0], op
[1]);
1327 /* Do not use op[0] after this point. Use array.
1335 if (!array
->type
->is_array()
1336 && !array
->type
->is_matrix()
1337 && !array
->type
->is_vector()) {
1338 _mesa_glsl_error(& index_loc
, state
,
1339 "cannot dereference non-array / non-matrix / "
1341 error_emitted
= true;
1344 if (!op
[1]->type
->is_integer()) {
1345 _mesa_glsl_error(& index_loc
, state
,
1346 "array index must be integer type");
1347 error_emitted
= true;
1348 } else if (!op
[1]->type
->is_scalar()) {
1349 _mesa_glsl_error(& index_loc
, state
,
1350 "array index must be scalar");
1351 error_emitted
= true;
1354 /* If the array index is a constant expression and the array has a
1355 * declared size, ensure that the access is in-bounds. If the array
1356 * index is not a constant expression, ensure that the array has a
1359 ir_constant
*const const_index
= op
[1]->constant_expression_value();
1360 if (const_index
!= NULL
) {
1361 const int idx
= const_index
->value
.i
[0];
1362 const char *type_name
;
1365 if (array
->type
->is_matrix()) {
1366 type_name
= "matrix";
1367 } else if (array
->type
->is_vector()) {
1368 type_name
= "vector";
1370 type_name
= "array";
1373 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1375 * "It is illegal to declare an array with a size, and then
1376 * later (in the same shader) index the same array with an
1377 * integral constant expression greater than or equal to the
1378 * declared size. It is also illegal to index an array with a
1379 * negative constant expression."
1381 if (array
->type
->is_matrix()) {
1382 if (array
->type
->row_type()->vector_elements
<= idx
) {
1383 bound
= array
->type
->row_type()->vector_elements
;
1385 } else if (array
->type
->is_vector()) {
1386 if (array
->type
->vector_elements
<= idx
) {
1387 bound
= array
->type
->vector_elements
;
1390 if ((array
->type
->array_size() > 0)
1391 && (array
->type
->array_size() <= idx
)) {
1392 bound
= array
->type
->array_size();
1397 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
1399 error_emitted
= true;
1400 } else if (idx
< 0) {
1401 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
1403 error_emitted
= true;
1406 if (array
->type
->is_array()) {
1407 /* If the array is a variable dereference, it dereferences the
1408 * whole array, by definition. Use this to get the variable.
1410 * FINISHME: Should some methods for getting / setting / testing
1411 * FINISHME: array access limits be added to ir_dereference?
1413 ir_variable
*const v
= array
->whole_variable_referenced();
1414 if ((v
!= NULL
) && (unsigned(idx
) > v
->max_array_access
))
1415 v
->max_array_access
= idx
;
1417 } else if (array
->type
->array_size() == 0) {
1418 _mesa_glsl_error(&loc
, state
, "unsized array index must be constant");
1420 if (array
->type
->is_array()) {
1421 /* whole_variable_referenced can return NULL if the array is a
1422 * member of a structure. In this case it is safe to not update
1423 * the max_array_access field because it is never used for fields
1426 ir_variable
*v
= array
->whole_variable_referenced();
1428 v
->max_array_access
= array
->type
->array_size();
1433 result
->type
= glsl_type::error_type
;
1435 type
= result
->type
;
1439 case ast_function_call
:
1440 /* Should *NEVER* get here. ast_function_call should always be handled
1441 * by ast_function_expression::hir.
1446 case ast_identifier
: {
1447 /* ast_identifier can appear several places in a full abstract syntax
1448 * tree. This particular use must be at location specified in the grammar
1449 * as 'variable_identifier'.
1452 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1454 result
= new(ctx
) ir_dereference_variable(var
);
1457 type
= result
->type
;
1459 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1460 this->primary_expression
.identifier
);
1462 error_emitted
= true;
1467 case ast_int_constant
:
1468 type
= glsl_type::int_type
;
1469 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1472 case ast_uint_constant
:
1473 type
= glsl_type::uint_type
;
1474 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1477 case ast_float_constant
:
1478 type
= glsl_type::float_type
;
1479 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1482 case ast_bool_constant
:
1483 type
= glsl_type::bool_type
;
1484 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1487 case ast_sequence
: {
1488 /* It should not be possible to generate a sequence in the AST without
1489 * any expressions in it.
1491 assert(!this->expressions
.is_empty());
1493 /* The r-value of a sequence is the last expression in the sequence. If
1494 * the other expressions in the sequence do not have side-effects (and
1495 * therefore add instructions to the instruction list), they get dropped
1498 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
)
1499 result
= ast
->hir(instructions
, state
);
1501 type
= result
->type
;
1503 /* Any errors should have already been emitted in the loop above.
1505 error_emitted
= true;
1510 if (type
->is_error() && !error_emitted
)
1511 _mesa_glsl_error(& loc
, state
, "type mismatch");
1518 ast_expression_statement::hir(exec_list
*instructions
,
1519 struct _mesa_glsl_parse_state
*state
)
1521 /* It is possible to have expression statements that don't have an
1522 * expression. This is the solitary semicolon:
1524 * for (i = 0; i < 5; i++)
1527 * In this case the expression will be NULL. Test for NULL and don't do
1528 * anything in that case.
1530 if (expression
!= NULL
)
1531 expression
->hir(instructions
, state
);
1533 /* Statements do not have r-values.
1540 ast_compound_statement::hir(exec_list
*instructions
,
1541 struct _mesa_glsl_parse_state
*state
)
1544 state
->symbols
->push_scope();
1546 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1547 ast
->hir(instructions
, state
);
1550 state
->symbols
->pop_scope();
1552 /* Compound statements do not have r-values.
1558 static const glsl_type
*
1559 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
, ast_node
*array_size
,
1560 struct _mesa_glsl_parse_state
*state
)
1562 unsigned length
= 0;
1564 /* FINISHME: Reject delcarations of multidimensional arrays. */
1566 if (array_size
!= NULL
) {
1567 exec_list dummy_instructions
;
1568 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1569 YYLTYPE loc
= array_size
->get_location();
1571 /* FINISHME: Verify that the grammar forbids side-effects in array
1572 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1574 assert(dummy_instructions
.is_empty());
1577 if (!ir
->type
->is_integer()) {
1578 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1579 } else if (!ir
->type
->is_scalar()) {
1580 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1582 ir_constant
*const size
= ir
->constant_expression_value();
1585 _mesa_glsl_error(& loc
, state
, "array size must be a "
1586 "constant valued expression");
1587 } else if (size
->value
.i
[0] <= 0) {
1588 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1590 assert(size
->type
== ir
->type
);
1591 length
= size
->value
.u
[0];
1595 } else if (state
->es_shader
) {
1596 /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1597 * array declarations have been removed from the language.
1599 _mesa_glsl_error(loc
, state
, "unsized array declarations are not "
1600 "allowed in GLSL ES 1.00.");
1603 return glsl_type::get_array_instance(base
, length
);
1608 ast_type_specifier::glsl_type(const char **name
,
1609 struct _mesa_glsl_parse_state
*state
) const
1611 const struct glsl_type
*type
;
1613 type
= state
->symbols
->get_type(this->type_name
);
1614 *name
= this->type_name
;
1616 if (this->is_array
) {
1617 YYLTYPE loc
= this->get_location();
1618 type
= process_array_type(&loc
, type
, this->array_size
, state
);
1626 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1628 struct _mesa_glsl_parse_state
*state
,
1631 if (qual
->flags
.q
.invariant
)
1634 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1635 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
1636 || qual
->flags
.q
.uniform
1637 || (qual
->flags
.q
.varying
&& (state
->target
== fragment_shader
)))
1640 if (qual
->flags
.q
.centroid
)
1643 if (qual
->flags
.q
.attribute
&& state
->target
!= vertex_shader
) {
1644 var
->type
= glsl_type::error_type
;
1645 _mesa_glsl_error(loc
, state
,
1646 "`attribute' variables may not be declared in the "
1648 _mesa_glsl_shader_target_name(state
->target
));
1651 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1653 * "The varying qualifier can be used only with the data types
1654 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1657 if (qual
->flags
.q
.varying
) {
1658 const glsl_type
*non_array_type
;
1660 if (var
->type
&& var
->type
->is_array())
1661 non_array_type
= var
->type
->fields
.array
;
1663 non_array_type
= var
->type
;
1665 if (non_array_type
&& non_array_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1666 var
->type
= glsl_type::error_type
;
1667 _mesa_glsl_error(loc
, state
,
1668 "varying variables must be of base type float");
1672 /* If there is no qualifier that changes the mode of the variable, leave
1673 * the setting alone.
1675 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
1676 var
->mode
= ir_var_inout
;
1677 else if (qual
->flags
.q
.attribute
|| qual
->flags
.q
.in
1678 || (qual
->flags
.q
.varying
&& (state
->target
== fragment_shader
)))
1679 var
->mode
= ir_var_in
;
1680 else if (qual
->flags
.q
.out
1681 || (qual
->flags
.q
.varying
&& (state
->target
== vertex_shader
)))
1682 var
->mode
= ir_var_out
;
1683 else if (qual
->flags
.q
.uniform
)
1684 var
->mode
= ir_var_uniform
;
1686 if (qual
->flags
.q
.flat
)
1687 var
->interpolation
= ir_var_flat
;
1688 else if (qual
->flags
.q
.noperspective
)
1689 var
->interpolation
= ir_var_noperspective
;
1691 var
->interpolation
= ir_var_smooth
;
1693 var
->pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
1694 var
->origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
1695 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
1696 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
1697 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
1698 ? "origin_upper_left" : "pixel_center_integer";
1700 _mesa_glsl_error(loc
, state
,
1701 "layout qualifier `%s' can only be applied to "
1702 "fragment shader input `gl_FragCoord'",
1706 if (qual
->flags
.q
.explicit_location
) {
1707 const bool global_scope
= (state
->current_function
== NULL
);
1709 const char *string
= "";
1711 /* In the vertex shader only shader inputs can be given explicit
1714 * In the fragment shader only shader outputs can be given explicit
1717 switch (state
->target
) {
1719 if (!global_scope
|| (var
->mode
!= ir_var_in
)) {
1725 case geometry_shader
:
1726 _mesa_glsl_error(loc
, state
,
1727 "geometry shader variables cannot be given "
1728 "explicit locations\n");
1731 case fragment_shader
:
1732 if (!global_scope
|| (var
->mode
!= ir_var_in
)) {
1740 _mesa_glsl_error(loc
, state
,
1741 "only %s shader %s variables can be given an "
1742 "explicit location\n",
1743 _mesa_glsl_shader_target_name(state
->target
),
1746 var
->explicit_location
= true;
1748 /* This bit of silliness is needed because invalid explicit locations
1749 * are supposed to be flagged during linking. Small negative values
1750 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
1751 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
1752 * The linker needs to be able to differentiate these cases. This
1753 * ensures that negative values stay negative.
1755 if (qual
->location
>= 0) {
1756 var
->location
= (state
->target
== vertex_shader
)
1757 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
1758 : (qual
->location
+ FRAG_RESULT_DATA0
);
1760 var
->location
= qual
->location
;
1765 if (var
->type
->is_array() && state
->language_version
!= 110) {
1766 var
->array_lvalue
= true;
1772 ast_declarator_list::hir(exec_list
*instructions
,
1773 struct _mesa_glsl_parse_state
*state
)
1776 const struct glsl_type
*decl_type
;
1777 const char *type_name
= NULL
;
1778 ir_rvalue
*result
= NULL
;
1779 YYLTYPE loc
= this->get_location();
1781 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
1783 * "To ensure that a particular output variable is invariant, it is
1784 * necessary to use the invariant qualifier. It can either be used to
1785 * qualify a previously declared variable as being invariant
1787 * invariant gl_Position; // make existing gl_Position be invariant"
1789 * In these cases the parser will set the 'invariant' flag in the declarator
1790 * list, and the type will be NULL.
1792 if (this->invariant
) {
1793 assert(this->type
== NULL
);
1795 if (state
->current_function
!= NULL
) {
1796 _mesa_glsl_error(& loc
, state
,
1797 "All uses of `invariant' keyword must be at global "
1801 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1802 assert(!decl
->is_array
);
1803 assert(decl
->array_size
== NULL
);
1804 assert(decl
->initializer
== NULL
);
1806 ir_variable
*const earlier
=
1807 state
->symbols
->get_variable(decl
->identifier
);
1808 if (earlier
== NULL
) {
1809 _mesa_glsl_error(& loc
, state
,
1810 "Undeclared variable `%s' cannot be marked "
1811 "invariant\n", decl
->identifier
);
1812 } else if ((state
->target
== vertex_shader
)
1813 && (earlier
->mode
!= ir_var_out
)) {
1814 _mesa_glsl_error(& loc
, state
,
1815 "`%s' cannot be marked invariant, vertex shader "
1816 "outputs only\n", decl
->identifier
);
1817 } else if ((state
->target
== fragment_shader
)
1818 && (earlier
->mode
!= ir_var_in
)) {
1819 _mesa_glsl_error(& loc
, state
,
1820 "`%s' cannot be marked invariant, fragment shader "
1821 "inputs only\n", decl
->identifier
);
1823 earlier
->invariant
= true;
1827 /* Invariant redeclarations do not have r-values.
1832 assert(this->type
!= NULL
);
1833 assert(!this->invariant
);
1835 /* The type specifier may contain a structure definition. Process that
1836 * before any of the variable declarations.
1838 (void) this->type
->specifier
->hir(instructions
, state
);
1840 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1841 if (this->declarations
.is_empty()) {
1842 /* The only valid case where the declaration list can be empty is when
1843 * the declaration is setting the default precision of a built-in type
1844 * (e.g., 'precision highp vec4;').
1847 if (decl_type
!= NULL
) {
1849 _mesa_glsl_error(& loc
, state
, "incomplete declaration");
1853 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1854 const struct glsl_type
*var_type
;
1857 /* FINISHME: Emit a warning if a variable declaration shadows a
1858 * FINISHME: declaration at a higher scope.
1861 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1862 if (type_name
!= NULL
) {
1863 _mesa_glsl_error(& loc
, state
,
1864 "invalid type `%s' in declaration of `%s'",
1865 type_name
, decl
->identifier
);
1867 _mesa_glsl_error(& loc
, state
,
1868 "invalid type in declaration of `%s'",
1874 if (decl
->is_array
) {
1875 var_type
= process_array_type(&loc
, decl_type
, decl
->array_size
,
1878 var_type
= decl_type
;
1881 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
1883 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1885 * "Global variables can only use the qualifiers const,
1886 * attribute, uni form, or varying. Only one may be
1889 * Local variables can only use the qualifier const."
1891 * This is relaxed in GLSL 1.30.
1893 if (state
->language_version
< 120) {
1894 if (this->type
->qualifier
.flags
.q
.out
) {
1895 _mesa_glsl_error(& loc
, state
,
1896 "`out' qualifier in declaration of `%s' "
1897 "only valid for function parameters in GLSL 1.10.",
1900 if (this->type
->qualifier
.flags
.q
.in
) {
1901 _mesa_glsl_error(& loc
, state
,
1902 "`in' qualifier in declaration of `%s' "
1903 "only valid for function parameters in GLSL 1.10.",
1906 /* FINISHME: Test for other invalid qualifiers. */
1909 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1912 if (this->type
->qualifier
.flags
.q
.invariant
) {
1913 if ((state
->target
== vertex_shader
) && !(var
->mode
== ir_var_out
||
1914 var
->mode
== ir_var_inout
)) {
1915 /* FINISHME: Note that this doesn't work for invariant on
1916 * a function signature outval
1918 _mesa_glsl_error(& loc
, state
,
1919 "`%s' cannot be marked invariant, vertex shader "
1920 "outputs only\n", var
->name
);
1921 } else if ((state
->target
== fragment_shader
) &&
1922 !(var
->mode
== ir_var_in
|| var
->mode
== ir_var_inout
)) {
1923 /* FINISHME: Note that this doesn't work for invariant on
1924 * a function signature inval
1926 _mesa_glsl_error(& loc
, state
,
1927 "`%s' cannot be marked invariant, fragment shader "
1928 "inputs only\n", var
->name
);
1932 if (state
->current_function
!= NULL
) {
1933 const char *mode
= NULL
;
1934 const char *extra
= "";
1936 /* There is no need to check for 'inout' here because the parser will
1937 * only allow that in function parameter lists.
1939 if (this->type
->qualifier
.flags
.q
.attribute
) {
1941 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
1943 } else if (this->type
->qualifier
.flags
.q
.varying
) {
1945 } else if (this->type
->qualifier
.flags
.q
.in
) {
1947 extra
= " or in function parameter list";
1948 } else if (this->type
->qualifier
.flags
.q
.out
) {
1950 extra
= " or in function parameter list";
1954 _mesa_glsl_error(& loc
, state
,
1955 "%s variable `%s' must be declared at "
1957 mode
, var
->name
, extra
);
1959 } else if (var
->mode
== ir_var_in
) {
1960 if (state
->target
== vertex_shader
) {
1961 bool error_emitted
= false;
1963 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1965 * "Vertex shader inputs can only be float, floating-point
1966 * vectors, matrices, signed and unsigned integers and integer
1967 * vectors. Vertex shader inputs can also form arrays of these
1968 * types, but not structures."
1970 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1972 * "Vertex shader inputs can only be float, floating-point
1973 * vectors, matrices, signed and unsigned integers and integer
1974 * vectors. They cannot be arrays or structures."
1976 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1978 * "The attribute qualifier can be used only with float,
1979 * floating-point vectors, and matrices. Attribute variables
1980 * cannot be declared as arrays or structures."
1982 const glsl_type
*check_type
= var
->type
->is_array()
1983 ? var
->type
->fields
.array
: var
->type
;
1985 switch (check_type
->base_type
) {
1986 case GLSL_TYPE_FLOAT
:
1988 case GLSL_TYPE_UINT
:
1990 if (state
->language_version
> 120)
1994 _mesa_glsl_error(& loc
, state
,
1995 "vertex shader input / attribute cannot have "
1997 var
->type
->is_array() ? "array of " : "",
1999 error_emitted
= true;
2002 if (!error_emitted
&& (state
->language_version
<= 130)
2003 && var
->type
->is_array()) {
2004 _mesa_glsl_error(& loc
, state
,
2005 "vertex shader input / attribute cannot have "
2007 error_emitted
= true;
2012 /* Process the initializer and add its instructions to a temporary
2013 * list. This list will be added to the instruction stream (below) after
2014 * the declaration is added. This is done because in some cases (such as
2015 * redeclarations) the declaration may not actually be added to the
2016 * instruction stream.
2018 exec_list initializer_instructions
;
2019 if (decl
->initializer
!= NULL
) {
2020 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
2022 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2024 * "All uniform variables are read-only and are initialized either
2025 * directly by an application via API commands, or indirectly by
2028 if ((state
->language_version
<= 110)
2029 && (var
->mode
== ir_var_uniform
)) {
2030 _mesa_glsl_error(& initializer_loc
, state
,
2031 "cannot initialize uniforms in GLSL 1.10");
2034 if (var
->type
->is_sampler()) {
2035 _mesa_glsl_error(& initializer_loc
, state
,
2036 "cannot initialize samplers");
2039 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
2040 _mesa_glsl_error(& initializer_loc
, state
,
2041 "cannot initialize %s shader input / %s",
2042 _mesa_glsl_shader_target_name(state
->target
),
2043 (state
->target
== vertex_shader
)
2044 ? "attribute" : "varying");
2047 ir_dereference
*const lhs
= new(ctx
) ir_dereference_variable(var
);
2048 ir_rvalue
*rhs
= decl
->initializer
->hir(&initializer_instructions
,
2051 /* Calculate the constant value if this is a const or uniform
2054 if (this->type
->qualifier
.flags
.q
.constant
2055 || this->type
->qualifier
.flags
.q
.uniform
) {
2056 ir_rvalue
*new_rhs
= validate_assignment(state
, var
->type
, rhs
);
2057 if (new_rhs
!= NULL
) {
2060 ir_constant
*constant_value
= rhs
->constant_expression_value();
2061 if (!constant_value
) {
2062 _mesa_glsl_error(& initializer_loc
, state
,
2063 "initializer of %s variable `%s' must be a "
2064 "constant expression",
2065 (this->type
->qualifier
.flags
.q
.constant
)
2066 ? "const" : "uniform",
2068 if (var
->type
->is_numeric()) {
2069 /* Reduce cascading errors. */
2070 var
->constant_value
= ir_constant::zero(ctx
, var
->type
);
2073 rhs
= constant_value
;
2074 var
->constant_value
= constant_value
;
2077 _mesa_glsl_error(&initializer_loc
, state
,
2078 "initializer of type %s cannot be assigned to "
2079 "variable of type %s",
2080 rhs
->type
->name
, var
->type
->name
);
2081 if (var
->type
->is_numeric()) {
2082 /* Reduce cascading errors. */
2083 var
->constant_value
= ir_constant::zero(ctx
, var
->type
);
2088 if (rhs
&& !rhs
->type
->is_error()) {
2089 bool temp
= var
->read_only
;
2090 if (this->type
->qualifier
.flags
.q
.constant
)
2091 var
->read_only
= false;
2093 /* Never emit code to initialize a uniform.
2095 if (!this->type
->qualifier
.flags
.q
.uniform
)
2096 result
= do_assignment(&initializer_instructions
, state
,
2098 this->get_location());
2099 var
->read_only
= temp
;
2103 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
2105 * "It is an error to write to a const variable outside of
2106 * its declaration, so they must be initialized when
2109 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
2110 _mesa_glsl_error(& loc
, state
,
2111 "const declaration of `%s' must be initialized");
2114 /* Check if this declaration is actually a re-declaration, either to
2115 * resize an array or add qualifiers to an existing variable.
2117 * This is allowed for variables in the current scope, or when at
2118 * global scope (for built-ins in the implicit outer scope).
2120 ir_variable
*earlier
= state
->symbols
->get_variable(decl
->identifier
);
2121 if (earlier
!= NULL
&& (state
->current_function
== NULL
||
2122 state
->symbols
->name_declared_this_scope(decl
->identifier
))) {
2124 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2126 * "It is legal to declare an array without a size and then
2127 * later re-declare the same name as an array of the same
2128 * type and specify a size."
2130 if ((earlier
->type
->array_size() == 0)
2131 && var
->type
->is_array()
2132 && (var
->type
->element_type() == earlier
->type
->element_type())) {
2133 /* FINISHME: This doesn't match the qualifiers on the two
2134 * FINISHME: declarations. It's not 100% clear whether this is
2135 * FINISHME: required or not.
2138 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
2140 * "The size [of gl_TexCoord] can be at most
2141 * gl_MaxTextureCoords."
2143 const unsigned size
= unsigned(var
->type
->array_size());
2144 if ((strcmp("gl_TexCoord", var
->name
) == 0)
2145 && (size
> state
->Const
.MaxTextureCoords
)) {
2146 YYLTYPE loc
= this->get_location();
2148 _mesa_glsl_error(& loc
, state
, "`gl_TexCoord' array size cannot "
2149 "be larger than gl_MaxTextureCoords (%u)\n",
2150 state
->Const
.MaxTextureCoords
);
2151 } else if ((size
> 0) && (size
<= earlier
->max_array_access
)) {
2152 YYLTYPE loc
= this->get_location();
2154 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2156 earlier
->max_array_access
);
2159 earlier
->type
= var
->type
;
2162 } else if (state
->extensions
->ARB_fragment_coord_conventions
2163 && strcmp(var
->name
, "gl_FragCoord") == 0
2164 && earlier
->type
== var
->type
2165 && earlier
->mode
== var
->mode
) {
2166 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2169 earlier
->origin_upper_left
= var
->origin_upper_left
;
2170 earlier
->pixel_center_integer
= var
->pixel_center_integer
;
2172 YYLTYPE loc
= this->get_location();
2173 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", decl
->identifier
);
2179 /* By now, we know it's a new variable declaration (we didn't hit the
2180 * above "continue").
2182 * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2184 * "Identifiers starting with "gl_" are reserved for use by
2185 * OpenGL, and may not be declared in a shader as either a
2186 * variable or a function."
2188 if (strncmp(decl
->identifier
, "gl_", 3) == 0)
2189 _mesa_glsl_error(& loc
, state
,
2190 "identifier `%s' uses reserved `gl_' prefix",
2193 /* Add the variable to the symbol table. Note that the initializer's
2194 * IR was already processed earlier (though it hasn't been emitted yet),
2195 * without the variable in scope.
2197 * This differs from most C-like languages, but it follows the GLSL
2198 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
2201 * "Within a declaration, the scope of a name starts immediately
2202 * after the initializer if present or immediately after the name
2203 * being declared if not."
2205 if (!state
->symbols
->add_variable(var
->name
, var
)) {
2206 YYLTYPE loc
= this->get_location();
2207 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
2208 "current scope", decl
->identifier
);
2212 /* Push the variable declaration to the top. It means that all
2213 * the variable declarations will appear in a funny
2214 * last-to-first order, but otherwise we run into trouble if a
2215 * function is prototyped, a global var is decled, then the
2216 * function is defined with usage of the global var. See
2217 * glslparsertest's CorrectModule.frag.
2219 instructions
->push_head(var
);
2220 instructions
->append_list(&initializer_instructions
);
2224 /* Generally, variable declarations do not have r-values. However,
2225 * one is used for the declaration in
2227 * while (bool b = some_condition()) {
2231 * so we return the rvalue from the last seen declaration here.
2238 ast_parameter_declarator::hir(exec_list
*instructions
,
2239 struct _mesa_glsl_parse_state
*state
)
2242 const struct glsl_type
*type
;
2243 const char *name
= NULL
;
2244 YYLTYPE loc
= this->get_location();
2246 type
= this->type
->specifier
->glsl_type(& name
, state
);
2250 _mesa_glsl_error(& loc
, state
,
2251 "invalid type `%s' in declaration of `%s'",
2252 name
, this->identifier
);
2254 _mesa_glsl_error(& loc
, state
,
2255 "invalid type in declaration of `%s'",
2259 type
= glsl_type::error_type
;
2262 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2264 * "Functions that accept no input arguments need not use void in the
2265 * argument list because prototypes (or definitions) are required and
2266 * therefore there is no ambiguity when an empty argument list "( )" is
2267 * declared. The idiom "(void)" as a parameter list is provided for
2270 * Placing this check here prevents a void parameter being set up
2271 * for a function, which avoids tripping up checks for main taking
2272 * parameters and lookups of an unnamed symbol.
2274 if (type
->is_void()) {
2275 if (this->identifier
!= NULL
)
2276 _mesa_glsl_error(& loc
, state
,
2277 "named parameter cannot have type `void'");
2283 if (formal_parameter
&& (this->identifier
== NULL
)) {
2284 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
2288 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
2289 * call already handled the "vec4[..] foo" case.
2291 if (this->is_array
) {
2292 type
= process_array_type(&loc
, type
, this->array_size
, state
);
2295 if (type
->array_size() == 0) {
2296 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
2297 "a declared size.");
2298 type
= glsl_type::error_type
;
2302 ir_variable
*var
= new(ctx
) ir_variable(type
, this->identifier
, ir_var_in
);
2304 /* Apply any specified qualifiers to the parameter declaration. Note that
2305 * for function parameters the default mode is 'in'.
2307 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
2309 instructions
->push_tail(var
);
2311 /* Parameter declarations do not have r-values.
2318 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
2320 exec_list
*ir_parameters
,
2321 _mesa_glsl_parse_state
*state
)
2323 ast_parameter_declarator
*void_param
= NULL
;
2326 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
2327 param
->formal_parameter
= formal
;
2328 param
->hir(ir_parameters
, state
);
2336 if ((void_param
!= NULL
) && (count
> 1)) {
2337 YYLTYPE loc
= void_param
->get_location();
2339 _mesa_glsl_error(& loc
, state
,
2340 "`void' parameter must be only parameter");
2346 ast_function::hir(exec_list
*instructions
,
2347 struct _mesa_glsl_parse_state
*state
)
2350 ir_function
*f
= NULL
;
2351 ir_function_signature
*sig
= NULL
;
2352 exec_list hir_parameters
;
2354 const char *const name
= identifier
;
2356 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
2358 * "Function declarations (prototypes) cannot occur inside of functions;
2359 * they must be at global scope, or for the built-in functions, outside
2360 * the global scope."
2362 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
2364 * "User defined functions may only be defined within the global scope."
2366 * Note that this language does not appear in GLSL 1.10.
2368 if ((state
->current_function
!= NULL
) && (state
->language_version
!= 110)) {
2369 YYLTYPE loc
= this->get_location();
2370 _mesa_glsl_error(&loc
, state
,
2371 "declaration of function `%s' not allowed within "
2372 "function body", name
);
2375 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2377 * "Identifiers starting with "gl_" are reserved for use by
2378 * OpenGL, and may not be declared in a shader as either a
2379 * variable or a function."
2381 if (strncmp(name
, "gl_", 3) == 0) {
2382 YYLTYPE loc
= this->get_location();
2383 _mesa_glsl_error(&loc
, state
,
2384 "identifier `%s' uses reserved `gl_' prefix", name
);
2387 /* Convert the list of function parameters to HIR now so that they can be
2388 * used below to compare this function's signature with previously seen
2389 * signatures for functions with the same name.
2391 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
2393 & hir_parameters
, state
);
2395 const char *return_type_name
;
2396 const glsl_type
*return_type
=
2397 this->return_type
->specifier
->glsl_type(& return_type_name
, state
);
2400 YYLTYPE loc
= this->get_location();
2401 _mesa_glsl_error(&loc
, state
,
2402 "function `%s' has undeclared return type `%s'",
2403 name
, return_type_name
);
2404 return_type
= glsl_type::error_type
;
2407 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2408 * "No qualifier is allowed on the return type of a function."
2410 if (this->return_type
->has_qualifiers()) {
2411 YYLTYPE loc
= this->get_location();
2412 _mesa_glsl_error(& loc
, state
,
2413 "function `%s' return type has qualifiers", name
);
2416 /* Verify that this function's signature either doesn't match a previously
2417 * seen signature for a function with the same name, or, if a match is found,
2418 * that the previously seen signature does not have an associated definition.
2420 f
= state
->symbols
->get_function(name
);
2421 if (f
!= NULL
&& (state
->es_shader
|| f
->has_user_signature())) {
2422 sig
= f
->exact_matching_signature(&hir_parameters
);
2424 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
2425 if (badvar
!= NULL
) {
2426 YYLTYPE loc
= this->get_location();
2428 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
2429 "qualifiers don't match prototype", name
, badvar
);
2432 if (sig
->return_type
!= return_type
) {
2433 YYLTYPE loc
= this->get_location();
2435 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
2436 "match prototype", name
);
2439 if (is_definition
&& sig
->is_defined
) {
2440 YYLTYPE loc
= this->get_location();
2442 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
2446 f
= new(ctx
) ir_function(name
);
2447 if (!state
->symbols
->add_function(f
->name
, f
)) {
2448 /* This function name shadows a non-function use of the same name. */
2449 YYLTYPE loc
= this->get_location();
2451 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
2452 "non-function", name
);
2456 /* Emit the new function header */
2457 if (state
->current_function
== NULL
)
2458 instructions
->push_tail(f
);
2460 /* IR invariants disallow function declarations or definitions nested
2461 * within other function definitions. Insert the new ir_function
2462 * block in the instruction sequence before the ir_function block
2463 * containing the current ir_function_signature.
2465 * This can only happen in a GLSL 1.10 shader. In all other GLSL
2466 * versions this nesting is disallowed. There is a check for this at
2467 * the top of this function.
2469 ir_function
*const curr
=
2470 const_cast<ir_function
*>(state
->current_function
->function());
2472 curr
->insert_before(f
);
2476 /* Verify the return type of main() */
2477 if (strcmp(name
, "main") == 0) {
2478 if (! return_type
->is_void()) {
2479 YYLTYPE loc
= this->get_location();
2481 _mesa_glsl_error(& loc
, state
, "main() must return void");
2484 if (!hir_parameters
.is_empty()) {
2485 YYLTYPE loc
= this->get_location();
2487 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
2491 /* Finish storing the information about this new function in its signature.
2494 sig
= new(ctx
) ir_function_signature(return_type
);
2495 f
->add_signature(sig
);
2498 sig
->replace_parameters(&hir_parameters
);
2501 /* Function declarations (prototypes) do not have r-values.
2508 ast_function_definition::hir(exec_list
*instructions
,
2509 struct _mesa_glsl_parse_state
*state
)
2511 prototype
->is_definition
= true;
2512 prototype
->hir(instructions
, state
);
2514 ir_function_signature
*signature
= prototype
->signature
;
2515 if (signature
== NULL
)
2518 assert(state
->current_function
== NULL
);
2519 state
->current_function
= signature
;
2520 state
->found_return
= false;
2522 /* Duplicate parameters declared in the prototype as concrete variables.
2523 * Add these to the symbol table.
2525 state
->symbols
->push_scope();
2526 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
2527 ir_variable
*const var
= ((ir_instruction
*) iter
.get())->as_variable();
2529 assert(var
!= NULL
);
2531 /* The only way a parameter would "exist" is if two parameters have
2534 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
2535 YYLTYPE loc
= this->get_location();
2537 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
2539 state
->symbols
->add_variable(var
->name
, var
);
2543 /* Convert the body of the function to HIR. */
2544 this->body
->hir(&signature
->body
, state
);
2545 signature
->is_defined
= true;
2547 state
->symbols
->pop_scope();
2549 assert(state
->current_function
== signature
);
2550 state
->current_function
= NULL
;
2552 if (!signature
->return_type
->is_void() && !state
->found_return
) {
2553 YYLTYPE loc
= this->get_location();
2554 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
2555 "%s, but no return statement",
2556 signature
->function_name(),
2557 signature
->return_type
->name
);
2560 /* Function definitions do not have r-values.
2567 ast_jump_statement::hir(exec_list
*instructions
,
2568 struct _mesa_glsl_parse_state
*state
)
2575 assert(state
->current_function
);
2577 if (opt_return_value
) {
2578 if (state
->current_function
->return_type
->base_type
==
2580 YYLTYPE loc
= this->get_location();
2582 _mesa_glsl_error(& loc
, state
,
2583 "`return` with a value, in function `%s' "
2585 state
->current_function
->function_name());
2588 ir_expression
*const ret
= (ir_expression
*)
2589 opt_return_value
->hir(instructions
, state
);
2590 assert(ret
!= NULL
);
2592 /* Implicit conversions are not allowed for return values. */
2593 if (state
->current_function
->return_type
!= ret
->type
) {
2594 YYLTYPE loc
= this->get_location();
2596 _mesa_glsl_error(& loc
, state
,
2597 "`return' with wrong type %s, in function `%s' "
2600 state
->current_function
->function_name(),
2601 state
->current_function
->return_type
->name
);
2604 inst
= new(ctx
) ir_return(ret
);
2606 if (state
->current_function
->return_type
->base_type
!=
2608 YYLTYPE loc
= this->get_location();
2610 _mesa_glsl_error(& loc
, state
,
2611 "`return' with no value, in function %s returning "
2613 state
->current_function
->function_name());
2615 inst
= new(ctx
) ir_return
;
2618 state
->found_return
= true;
2619 instructions
->push_tail(inst
);
2624 if (state
->target
!= fragment_shader
) {
2625 YYLTYPE loc
= this->get_location();
2627 _mesa_glsl_error(& loc
, state
,
2628 "`discard' may only appear in a fragment shader");
2630 instructions
->push_tail(new(ctx
) ir_discard
);
2635 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2636 * FINISHME: and they use a different IR instruction for 'break'.
2638 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2639 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2642 if (state
->loop_or_switch_nesting
== NULL
) {
2643 YYLTYPE loc
= this->get_location();
2645 _mesa_glsl_error(& loc
, state
,
2646 "`%s' may only appear in a loop",
2647 (mode
== ast_break
) ? "break" : "continue");
2649 ir_loop
*const loop
= state
->loop_or_switch_nesting
->as_loop();
2651 /* Inline the for loop expression again, since we don't know
2652 * where near the end of the loop body the normal copy of it
2653 * is going to be placed.
2655 if (mode
== ast_continue
&&
2656 state
->loop_or_switch_nesting_ast
->rest_expression
) {
2657 state
->loop_or_switch_nesting_ast
->rest_expression
->hir(instructions
,
2662 ir_loop_jump
*const jump
=
2663 new(ctx
) ir_loop_jump((mode
== ast_break
)
2664 ? ir_loop_jump::jump_break
2665 : ir_loop_jump::jump_continue
);
2666 instructions
->push_tail(jump
);
2673 /* Jump instructions do not have r-values.
2680 ast_selection_statement::hir(exec_list
*instructions
,
2681 struct _mesa_glsl_parse_state
*state
)
2685 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
2687 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2689 * "Any expression whose type evaluates to a Boolean can be used as the
2690 * conditional expression bool-expression. Vector types are not accepted
2691 * as the expression to if."
2693 * The checks are separated so that higher quality diagnostics can be
2694 * generated for cases where both rules are violated.
2696 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
2697 YYLTYPE loc
= this->condition
->get_location();
2699 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
2703 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
2705 if (then_statement
!= NULL
) {
2706 state
->symbols
->push_scope();
2707 then_statement
->hir(& stmt
->then_instructions
, state
);
2708 state
->symbols
->pop_scope();
2711 if (else_statement
!= NULL
) {
2712 state
->symbols
->push_scope();
2713 else_statement
->hir(& stmt
->else_instructions
, state
);
2714 state
->symbols
->pop_scope();
2717 instructions
->push_tail(stmt
);
2719 /* if-statements do not have r-values.
2726 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
2727 struct _mesa_glsl_parse_state
*state
)
2731 if (condition
!= NULL
) {
2732 ir_rvalue
*const cond
=
2733 condition
->hir(& stmt
->body_instructions
, state
);
2736 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
2737 YYLTYPE loc
= condition
->get_location();
2739 _mesa_glsl_error(& loc
, state
,
2740 "loop condition must be scalar boolean");
2742 /* As the first code in the loop body, generate a block that looks
2743 * like 'if (!condition) break;' as the loop termination condition.
2745 ir_rvalue
*const not_cond
=
2746 new(ctx
) ir_expression(ir_unop_logic_not
, glsl_type::bool_type
, cond
,
2749 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
2751 ir_jump
*const break_stmt
=
2752 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
2754 if_stmt
->then_instructions
.push_tail(break_stmt
);
2755 stmt
->body_instructions
.push_tail(if_stmt
);
2762 ast_iteration_statement::hir(exec_list
*instructions
,
2763 struct _mesa_glsl_parse_state
*state
)
2767 /* For-loops and while-loops start a new scope, but do-while loops do not.
2769 if (mode
!= ast_do_while
)
2770 state
->symbols
->push_scope();
2772 if (init_statement
!= NULL
)
2773 init_statement
->hir(instructions
, state
);
2775 ir_loop
*const stmt
= new(ctx
) ir_loop();
2776 instructions
->push_tail(stmt
);
2778 /* Track the current loop and / or switch-statement nesting.
2780 ir_instruction
*const nesting
= state
->loop_or_switch_nesting
;
2781 ast_iteration_statement
*nesting_ast
= state
->loop_or_switch_nesting_ast
;
2783 state
->loop_or_switch_nesting
= stmt
;
2784 state
->loop_or_switch_nesting_ast
= this;
2786 if (mode
!= ast_do_while
)
2787 condition_to_hir(stmt
, state
);
2790 body
->hir(& stmt
->body_instructions
, state
);
2792 if (rest_expression
!= NULL
)
2793 rest_expression
->hir(& stmt
->body_instructions
, state
);
2795 if (mode
== ast_do_while
)
2796 condition_to_hir(stmt
, state
);
2798 if (mode
!= ast_do_while
)
2799 state
->symbols
->pop_scope();
2801 /* Restore previous nesting before returning.
2803 state
->loop_or_switch_nesting
= nesting
;
2804 state
->loop_or_switch_nesting_ast
= nesting_ast
;
2806 /* Loops do not have r-values.
2813 ast_type_specifier::hir(exec_list
*instructions
,
2814 struct _mesa_glsl_parse_state
*state
)
2816 if (this->structure
!= NULL
)
2817 return this->structure
->hir(instructions
, state
);
2824 ast_struct_specifier::hir(exec_list
*instructions
,
2825 struct _mesa_glsl_parse_state
*state
)
2827 unsigned decl_count
= 0;
2829 /* Make an initial pass over the list of structure fields to determine how
2830 * many there are. Each element in this list is an ast_declarator_list.
2831 * This means that we actually need to count the number of elements in the
2832 * 'declarations' list in each of the elements.
2834 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2835 &this->declarations
) {
2836 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
2841 /* Allocate storage for the structure fields and process the field
2842 * declarations. As the declarations are processed, try to also convert
2843 * the types to HIR. This ensures that structure definitions embedded in
2844 * other structure definitions are processed.
2846 glsl_struct_field
*const fields
= talloc_array(state
, glsl_struct_field
,
2850 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2851 &this->declarations
) {
2852 const char *type_name
;
2854 decl_list
->type
->specifier
->hir(instructions
, state
);
2856 /* Section 10.9 of the GLSL ES 1.00 specification states that
2857 * embedded structure definitions have been removed from the language.
2859 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
2860 YYLTYPE loc
= this->get_location();
2861 _mesa_glsl_error(&loc
, state
, "Embedded structure definitions are "
2862 "not allowed in GLSL ES 1.00.");
2865 const glsl_type
*decl_type
=
2866 decl_list
->type
->specifier
->glsl_type(& type_name
, state
);
2868 foreach_list_typed (ast_declaration
, decl
, link
,
2869 &decl_list
->declarations
) {
2870 const struct glsl_type
*field_type
= decl_type
;
2871 if (decl
->is_array
) {
2872 YYLTYPE loc
= decl
->get_location();
2873 field_type
= process_array_type(&loc
, decl_type
, decl
->array_size
,
2876 fields
[i
].type
= (field_type
!= NULL
)
2877 ? field_type
: glsl_type::error_type
;
2878 fields
[i
].name
= decl
->identifier
;
2883 assert(i
== decl_count
);
2885 const glsl_type
*t
=
2886 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
2888 YYLTYPE loc
= this->get_location();
2889 if (!state
->symbols
->add_type(name
, t
)) {
2890 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
2893 const glsl_type
**s
= (const glsl_type
**)
2894 realloc(state
->user_structures
,
2895 sizeof(state
->user_structures
[0]) *
2896 (state
->num_user_structures
+ 1));
2898 s
[state
->num_user_structures
] = t
;
2899 state
->user_structures
= s
;
2900 state
->num_user_structures
++;
2904 /* Structure type definitions do not have r-values.