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 assert(!"FINISHME: Convert bool to float.");
132 static const struct glsl_type
*
133 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
135 struct _mesa_glsl_parse_state
*state
)
137 const glsl_type
*const type_a
= value_a
->type
;
138 const glsl_type
*const type_b
= value_b
->type
;
140 /* From GLSL 1.50 spec, page 56:
142 * "The arithmetic binary operators add (+), subtract (-),
143 * multiply (*), and divide (/) operate on integer and
144 * floating-point scalars, vectors, and matrices."
146 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
147 return glsl_type::error_type
;
151 /* "If one operand is floating-point based and the other is
152 * not, then the conversions from Section 4.1.10 "Implicit
153 * Conversions" are applied to the non-floating-point-based operand."
155 if (!apply_implicit_conversion(type_a
, value_b
, state
)
156 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
157 return glsl_type::error_type
;
160 /* "If the operands are integer types, they must both be signed or
163 * From this rule and the preceeding conversion it can be inferred that
164 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
165 * The is_numeric check above already filtered out the case where either
166 * type is not one of these, so now the base types need only be tested for
169 if (type_a
->base_type
!= type_b
->base_type
) {
170 return glsl_type::error_type
;
173 /* "All arithmetic binary operators result in the same fundamental type
174 * (signed integer, unsigned integer, or floating-point) as the
175 * operands they operate on, after operand type conversion. After
176 * conversion, the following cases are valid
178 * * The two operands are scalars. In this case the operation is
179 * applied, resulting in a scalar."
181 if (type_a
->is_scalar() && type_b
->is_scalar())
184 /* "* One operand is a scalar, and the other is a vector or matrix.
185 * In this case, the scalar operation is applied independently to each
186 * component of the vector or matrix, resulting in the same size
189 if (type_a
->is_scalar()) {
190 if (!type_b
->is_scalar())
192 } else if (type_b
->is_scalar()) {
196 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
197 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
200 assert(!type_a
->is_scalar());
201 assert(!type_b
->is_scalar());
203 /* "* The two operands are vectors of the same size. In this case, the
204 * operation is done component-wise resulting in the same size
207 if (type_a
->is_vector() && type_b
->is_vector()) {
208 return (type_a
== type_b
) ? type_a
: glsl_type::error_type
;
211 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
212 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
213 * <vector, vector> have been handled. At least one of the operands must
214 * be matrix. Further, since there are no integer matrix types, the base
215 * type of both operands must be float.
217 assert(type_a
->is_matrix() || type_b
->is_matrix());
218 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
219 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
221 /* "* The operator is add (+), subtract (-), or divide (/), and the
222 * operands are matrices with the same number of rows and the same
223 * number of columns. In this case, the operation is done component-
224 * wise resulting in the same size matrix."
225 * * The operator is multiply (*), where both operands are matrices or
226 * one operand is a vector and the other a matrix. A right vector
227 * operand is treated as a column vector and a left vector operand as a
228 * row vector. In all these cases, it is required that the number of
229 * columns of the left operand is equal to the number of rows of the
230 * right operand. Then, the multiply (*) operation does a linear
231 * algebraic multiply, yielding an object that has the same number of
232 * rows as the left operand and the same number of columns as the right
233 * operand. Section 5.10 "Vector and Matrix Operations" explains in
234 * more detail how vectors and matrices are operated on."
237 return (type_a
== type_b
) ? type_a
: glsl_type::error_type
;
239 if (type_a
->is_matrix() && type_b
->is_matrix()) {
240 /* Matrix multiply. The columns of A must match the rows of B. Given
241 * the other previously tested constraints, this means the vector type
242 * of a row from A must be the same as the vector type of a column from
245 if (type_a
->row_type() == type_b
->column_type()) {
246 /* The resulting matrix has the number of columns of matrix B and
247 * the number of rows of matrix A. We get the row count of A by
248 * looking at the size of a vector that makes up a column. The
249 * transpose (size of a row) is done for B.
252 glsl_type::get_instance(type_a
->base_type
,
253 type_a
->column_type()->vector_elements
,
254 type_b
->row_type()->vector_elements
);
256 } else if (type_a
->is_matrix()) {
257 /* A is a matrix and B is a column vector. Columns of A must match
258 * rows of B. Given the other previously tested constraints, this
259 * means the vector type of a row from A must be the same as the
260 * vector the type of B.
262 if (type_a
->row_type() == type_b
)
265 assert(type_b
->is_matrix());
267 /* A is a row vector and B is a matrix. Columns of A must match rows
268 * of B. Given the other previously tested constraints, this means
269 * the type of A must be the same as the vector type of a column from
272 if (type_a
== type_b
->column_type())
278 /* "All other cases are illegal."
280 return glsl_type::error_type
;
284 static const struct glsl_type
*
285 unary_arithmetic_result_type(const struct glsl_type
*type
)
287 /* From GLSL 1.50 spec, page 57:
289 * "The arithmetic unary operators negate (-), post- and pre-increment
290 * and decrement (-- and ++) operate on integer or floating-point
291 * values (including vectors and matrices). All unary operators work
292 * component-wise on their operands. These result with the same type
295 if (!type
->is_numeric())
296 return glsl_type::error_type
;
302 static const struct glsl_type
*
303 modulus_result_type(const struct glsl_type
*type_a
,
304 const struct glsl_type
*type_b
)
306 /* From GLSL 1.50 spec, page 56:
307 * "The operator modulus (%) operates on signed or unsigned integers or
308 * integer vectors. The operand types must both be signed or both be
311 if (!type_a
->is_integer() || !type_b
->is_integer()
312 || (type_a
->base_type
!= type_b
->base_type
)) {
313 return glsl_type::error_type
;
316 /* "The operands cannot be vectors of differing size. If one operand is
317 * a scalar and the other vector, then the scalar is applied component-
318 * wise to the vector, resulting in the same type as the vector. If both
319 * are vectors of the same size, the result is computed component-wise."
321 if (type_a
->is_vector()) {
322 if (!type_b
->is_vector()
323 || (type_a
->vector_elements
== type_b
->vector_elements
))
328 /* "The operator modulus (%) is not defined for any other data types
329 * (non-integer types)."
331 return glsl_type::error_type
;
335 static const struct glsl_type
*
336 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
337 struct _mesa_glsl_parse_state
*state
)
339 const glsl_type
*const type_a
= value_a
->type
;
340 const glsl_type
*const type_b
= value_b
->type
;
342 /* From GLSL 1.50 spec, page 56:
343 * "The relational operators greater than (>), less than (<), greater
344 * than or equal (>=), and less than or equal (<=) operate only on
345 * scalar integer and scalar floating-point expressions."
347 if (!type_a
->is_numeric()
348 || !type_b
->is_numeric()
349 || !type_a
->is_scalar()
350 || !type_b
->is_scalar())
351 return glsl_type::error_type
;
353 /* "Either the operands' types must match, or the conversions from
354 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
355 * operand, after which the types must match."
357 if (!apply_implicit_conversion(type_a
, value_b
, state
)
358 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
359 return glsl_type::error_type
;
362 if (type_a
->base_type
!= type_b
->base_type
)
363 return glsl_type::error_type
;
365 /* "The result is scalar Boolean."
367 return glsl_type::bool_type
;
372 * Validates that a value can be assigned to a location with a specified type
374 * Validates that \c rhs can be assigned to some location. If the types are
375 * not an exact match but an automatic conversion is possible, \c rhs will be
379 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
380 * Otherwise the actual RHS to be assigned will be returned. This may be
381 * \c rhs, or it may be \c rhs after some type conversion.
384 * In addition to being used for assignments, this function is used to
385 * type-check return values.
388 validate_assignment(const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
390 const glsl_type
*const rhs_type
= rhs
->type
;
392 /* If there is already some error in the RHS, just return it. Anything
393 * else will lead to an avalanche of error message back to the user.
395 if (rhs_type
->is_error())
398 /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
400 /* If the types are identical, the assignment can trivially proceed.
402 if (rhs_type
== lhs_type
)
405 /* FINISHME: Check for and apply automatic conversions. */
410 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
411 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
414 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
416 if (!error_emitted
) {
417 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
418 if (!lhs
->is_lvalue()) {
419 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
420 error_emitted
= true;
424 ir_rvalue
*new_rhs
= validate_assignment(lhs
->type
, rhs
);
425 if (new_rhs
== NULL
) {
426 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
431 ir_instruction
*tmp
= new ir_assignment(lhs
, rhs
, NULL
);
432 instructions
->push_tail(tmp
);
439 * Generate a new temporary and add its declaration to the instruction stream
442 generate_temporary(const glsl_type
*type
, exec_list
*instructions
,
443 struct _mesa_glsl_parse_state
*state
)
445 char *name
= (char *) malloc(sizeof(char) * 13);
447 snprintf(name
, 13, "tmp_%08X", state
->temp_index
);
450 ir_variable
*const var
= new ir_variable(type
, name
);
451 instructions
->push_tail(var
);
458 get_lvalue_copy(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
459 ir_rvalue
*lvalue
, YYLTYPE loc
)
462 ir_rvalue
*var_deref
;
464 /* FINISHME: Give unique names to the temporaries. */
465 var
= new ir_variable(lvalue
->type
, "_internal_tmp");
466 var
->mode
= ir_var_auto
;
468 var_deref
= new ir_dereference(var
);
469 do_assignment(instructions
, state
, var_deref
, lvalue
, loc
);
471 /* Once we've created this temporary, mark it read only so it's no
472 * longer considered an lvalue.
474 var
->read_only
= true;
481 ast_node::hir(exec_list
*instructions
,
482 struct _mesa_glsl_parse_state
*state
)
492 ast_expression::hir(exec_list
*instructions
,
493 struct _mesa_glsl_parse_state
*state
)
495 static const int operations
[AST_NUM_OPERATORS
] = {
496 -1, /* ast_assign doesn't convert to ir_expression. */
497 -1, /* ast_plus doesn't convert to ir_expression. */
521 /* Note: The following block of expression types actually convert
522 * to multiple IR instructions.
524 ir_binop_mul
, /* ast_mul_assign */
525 ir_binop_div
, /* ast_div_assign */
526 ir_binop_mod
, /* ast_mod_assign */
527 ir_binop_add
, /* ast_add_assign */
528 ir_binop_sub
, /* ast_sub_assign */
529 ir_binop_lshift
, /* ast_ls_assign */
530 ir_binop_rshift
, /* ast_rs_assign */
531 ir_binop_bit_and
, /* ast_and_assign */
532 ir_binop_bit_xor
, /* ast_xor_assign */
533 ir_binop_bit_or
, /* ast_or_assign */
535 -1, /* ast_conditional doesn't convert to ir_expression. */
536 ir_binop_add
, /* ast_pre_inc. */
537 ir_binop_sub
, /* ast_pre_dec. */
538 ir_binop_add
, /* ast_post_inc. */
539 ir_binop_sub
, /* ast_post_dec. */
540 -1, /* ast_field_selection doesn't conv to ir_expression. */
541 -1, /* ast_array_index doesn't convert to ir_expression. */
542 -1, /* ast_function_call doesn't conv to ir_expression. */
543 -1, /* ast_identifier doesn't convert to ir_expression. */
544 -1, /* ast_int_constant doesn't convert to ir_expression. */
545 -1, /* ast_uint_constant doesn't conv to ir_expression. */
546 -1, /* ast_float_constant doesn't conv to ir_expression. */
547 -1, /* ast_bool_constant doesn't conv to ir_expression. */
548 -1, /* ast_sequence doesn't convert to ir_expression. */
550 ir_rvalue
*result
= NULL
;
552 struct simple_node op_list
;
553 const struct glsl_type
*type
= glsl_type::error_type
;
554 bool error_emitted
= false;
557 loc
= this->get_location();
558 make_empty_list(& op_list
);
560 switch (this->oper
) {
562 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
563 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
565 result
= do_assignment(instructions
, state
, op
[0], op
[1],
566 this->subexpressions
[0]->get_location());
567 error_emitted
= result
->type
->is_error();
573 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
575 error_emitted
= op
[0]->type
->is_error();
576 if (type
->is_error())
583 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
585 type
= unary_arithmetic_result_type(op
[0]->type
);
587 error_emitted
= op
[0]->type
->is_error();
589 result
= new ir_expression(operations
[this->oper
], type
,
597 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
598 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
600 type
= arithmetic_result_type(op
[0], op
[1],
601 (this->oper
== ast_mul
),
604 result
= new ir_expression(operations
[this->oper
], type
,
609 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
610 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
612 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
614 type
= modulus_result_type(op
[0]->type
, op
[1]->type
);
616 assert(operations
[this->oper
] == ir_binop_mod
);
618 result
= new ir_expression(operations
[this->oper
], type
,
624 /* FINISHME: Implement bit-shift operators. */
631 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
632 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
634 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
636 type
= relational_result_type(op
[0], op
[1], state
);
638 /* The relational operators must either generate an error or result
639 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
641 assert(type
->is_error()
642 || ((type
->base_type
== GLSL_TYPE_BOOL
)
643 && type
->is_scalar()));
645 result
= new ir_expression(operations
[this->oper
], type
,
651 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
652 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
654 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
656 * "The equality operators equal (==), and not equal (!=)
657 * operate on all types. They result in a scalar Boolean. If
658 * the operand types do not match, then there must be a
659 * conversion from Section 4.1.10 "Implicit Conversions"
660 * applied to one operand that can make them match, in which
661 * case this conversion is done."
663 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
664 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
665 || (op
[0]->type
!= op
[1]->type
)) {
666 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
667 "type", (this->oper
== ast_equal
) ? "==" : "!=");
668 error_emitted
= true;
669 } else if ((state
->language_version
<= 110)
670 && (op
[0]->type
->is_array() || op
[1]->type
->is_array())) {
671 _mesa_glsl_error(& loc
, state
, "array comparisons forbidden in "
673 error_emitted
= true;
676 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
678 type
= glsl_type::bool_type
;
680 assert(result
->type
== glsl_type::bool_type
);
687 /* FINISHME: Implement bit-wise operators. */
694 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
695 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
697 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
698 YYLTYPE loc
= this->subexpressions
[0]->get_location();
700 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
701 operator_string(this->oper
));
704 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
705 YYLTYPE loc
= this->subexpressions
[1]->get_location();
707 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
708 operator_string(this->oper
));
711 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
718 case ast_sub_assign
: {
719 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
720 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
722 type
= arithmetic_result_type(op
[0], op
[1],
723 (this->oper
== ast_mul_assign
),
726 ir_rvalue
*temp_rhs
= new ir_expression(operations
[this->oper
], type
,
729 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
730 this->subexpressions
[0]->get_location());
732 error_emitted
= (op
[0]->type
->is_error());
734 /* GLSL 1.10 does not allow array assignment. However, we don't have to
735 * explicitly test for this because none of the binary expression
736 * operators allow array operands either.
742 case ast_mod_assign
: {
743 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
744 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
746 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
748 type
= modulus_result_type(op
[0]->type
, op
[1]->type
);
750 assert(operations
[this->oper
] == ir_binop_mod
);
752 struct ir_rvalue
*temp_rhs
;
753 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
756 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
757 this->subexpressions
[0]->get_location());
759 error_emitted
= op
[0]->type
->is_error();
772 case ast_conditional
: {
773 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
775 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
777 * "The ternary selection operator (?:). It operates on three
778 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
779 * first expression, which must result in a scalar Boolean."
781 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
782 YYLTYPE loc
= this->subexpressions
[0]->get_location();
784 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
785 error_emitted
= true;
788 /* The :? operator is implemented by generating an anonymous temporary
789 * followed by an if-statement. The last instruction in each branch of
790 * the if-statement assigns a value to the anonymous temporary. This
791 * temporary is the r-value of the expression.
793 ir_variable
*const tmp
= generate_temporary(glsl_type::error_type
,
794 instructions
, state
);
796 ir_if
*const stmt
= new ir_if(op
[0]);
797 instructions
->push_tail(stmt
);
799 op
[1] = this->subexpressions
[1]->hir(& stmt
->then_instructions
, state
);
800 ir_dereference
*const then_deref
= new ir_dereference(tmp
);
801 ir_assignment
*const then_assign
=
802 new ir_assignment(then_deref
, op
[1], NULL
);
803 stmt
->then_instructions
.push_tail(then_assign
);
805 op
[2] = this->subexpressions
[2]->hir(& stmt
->else_instructions
, state
);
806 ir_dereference
*const else_deref
= new ir_dereference(tmp
);
807 ir_assignment
*const else_assign
=
808 new ir_assignment(else_deref
, op
[2], NULL
);
809 stmt
->else_instructions
.push_tail(else_assign
);
811 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
813 * "The second and third expressions can be any type, as
814 * long their types match, or there is a conversion in
815 * Section 4.1.10 "Implicit Conversions" that can be applied
816 * to one of the expressions to make their types match. This
817 * resulting matching type is the type of the entire
820 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
821 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
822 || (op
[1]->type
!= op
[2]->type
)) {
823 YYLTYPE loc
= this->subexpressions
[1]->get_location();
825 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
826 "operator must have matching types.");
827 error_emitted
= true;
829 tmp
->type
= op
[1]->type
;
832 result
= new ir_dereference(tmp
);
839 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
840 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
841 op
[1] = new ir_constant(1.0f
);
843 op
[1] = new ir_constant(1);
845 type
= arithmetic_result_type(op
[0], op
[1], false, state
);
847 struct ir_rvalue
*temp_rhs
;
848 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
851 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
852 this->subexpressions
[0]->get_location());
854 error_emitted
= op
[0]->type
->is_error();
860 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
861 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
862 op
[1] = new ir_constant(1.0f
);
864 op
[1] = new ir_constant(1);
866 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
868 type
= arithmetic_result_type(op
[0], op
[1], false, state
);
870 struct ir_rvalue
*temp_rhs
;
871 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
874 /* Get a temporary of a copy of the lvalue before it's modified.
875 * This may get thrown away later.
877 result
= get_lvalue_copy(instructions
, state
, op
[0],
878 this->subexpressions
[0]->get_location());
880 (void)do_assignment(instructions
, state
, op
[0], temp_rhs
,
881 this->subexpressions
[0]->get_location());
884 error_emitted
= op
[0]->type
->is_error();
888 case ast_field_selection
:
889 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
893 case ast_array_index
:
896 case ast_function_call
:
897 /* Should *NEVER* get here. ast_function_call should always be handled
898 * by ast_function_expression::hir.
903 case ast_identifier
: {
904 /* ast_identifier can appear several places in a full abstract syntax
905 * tree. This particular use must be at location specified in the grammar
906 * as 'variable_identifier'.
909 state
->symbols
->get_variable(this->primary_expression
.identifier
);
911 result
= new ir_dereference(var
);
916 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
917 this->primary_expression
.identifier
);
919 error_emitted
= true;
924 case ast_int_constant
:
925 type
= glsl_type::int_type
;
926 result
= new ir_constant(type
, & this->primary_expression
);
929 case ast_uint_constant
:
930 type
= glsl_type::uint_type
;
931 result
= new ir_constant(type
, & this->primary_expression
);
934 case ast_float_constant
:
935 type
= glsl_type::float_type
;
936 result
= new ir_constant(type
, & this->primary_expression
);
939 case ast_bool_constant
:
940 type
= glsl_type::bool_type
;
941 result
= new ir_constant(type
, & this->primary_expression
);
945 struct simple_node
*ptr
;
947 /* It should not be possible to generate a sequence in the AST without
948 * any expressions in it.
950 assert(!is_empty_list(&this->expressions
));
952 /* The r-value of a sequence is the last expression in the sequence. If
953 * the other expressions in the sequence do not have side-effects (and
954 * therefore add instructions to the instruction list), they get dropped
957 foreach (ptr
, &this->expressions
)
958 result
= ((ast_node
*)ptr
)->hir(instructions
, state
);
962 /* Any errors should have already been emitted in the loop above.
964 error_emitted
= true;
969 if (type
->is_error() && !error_emitted
)
970 _mesa_glsl_error(& loc
, state
, "type mismatch");
977 ast_expression_statement::hir(exec_list
*instructions
,
978 struct _mesa_glsl_parse_state
*state
)
980 /* It is possible to have expression statements that don't have an
981 * expression. This is the solitary semicolon:
983 * for (i = 0; i < 5; i++)
986 * In this case the expression will be NULL. Test for NULL and don't do
987 * anything in that case.
989 if (expression
!= NULL
)
990 expression
->hir(instructions
, state
);
992 /* Statements do not have r-values.
999 ast_compound_statement::hir(exec_list
*instructions
,
1000 struct _mesa_glsl_parse_state
*state
)
1002 struct simple_node
*ptr
;
1006 state
->symbols
->push_scope();
1008 foreach (ptr
, &statements
)
1009 ((ast_node
*)ptr
)->hir(instructions
, state
);
1012 state
->symbols
->pop_scope();
1014 /* Compound statements do not have r-values.
1020 static const glsl_type
*
1021 process_array_type(const glsl_type
*base
, ast_node
*array_size
,
1022 struct _mesa_glsl_parse_state
*state
)
1024 unsigned length
= 0;
1026 /* FINISHME: Reject delcarations of multidimensional arrays. */
1028 if (array_size
!= NULL
) {
1029 exec_list dummy_instructions
;
1030 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1031 YYLTYPE loc
= array_size
->get_location();
1033 /* FINISHME: Verify that the grammar forbids side-effects in array
1034 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1036 assert(dummy_instructions
.is_empty());
1039 if (!ir
->type
->is_integer()) {
1040 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1041 } else if (!ir
->type
->is_scalar()) {
1042 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1044 ir_constant
*const size
= ir
->constant_expression_value();
1047 _mesa_glsl_error(& loc
, state
, "array size must be a "
1048 "constant valued expression");
1049 } else if (size
->value
.i
[0] <= 0) {
1050 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1052 assert(size
->type
== ir
->type
);
1053 length
= size
->value
.u
[0];
1059 return glsl_type::get_array_instance(base
, length
);
1064 ast_type_specifier::glsl_type(const char **name
,
1065 struct _mesa_glsl_parse_state
*state
) const
1067 const struct glsl_type
*type
;
1069 if (this->type_specifier
== ast_struct
) {
1070 /* FINISHME: Handle annonymous structures. */
1073 type
= state
->symbols
->get_type(this->type_name
);
1074 *name
= this->type_name
;
1076 if (this->is_array
) {
1077 type
= process_array_type(type
, this->array_size
, state
);
1086 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1087 struct ir_variable
*var
,
1088 struct _mesa_glsl_parse_state
*state
,
1091 if (qual
->invariant
)
1094 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1095 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
1096 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1102 if (qual
->attribute
&& state
->target
== fragment_shader
) {
1103 var
->type
= glsl_type::error_type
;
1104 _mesa_glsl_error(loc
, state
,
1105 "`attribute' variables may not be declared in the "
1109 if (qual
->in
&& qual
->out
)
1110 var
->mode
= ir_var_inout
;
1111 else if (qual
->attribute
|| qual
->in
1112 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1113 var
->mode
= ir_var_in
;
1114 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
1115 var
->mode
= ir_var_out
;
1116 else if (qual
->uniform
)
1117 var
->mode
= ir_var_uniform
;
1119 var
->mode
= ir_var_auto
;
1122 var
->interpolation
= ir_var_flat
;
1123 else if (qual
->noperspective
)
1124 var
->interpolation
= ir_var_noperspective
;
1126 var
->interpolation
= ir_var_smooth
;
1131 ast_declarator_list::hir(exec_list
*instructions
,
1132 struct _mesa_glsl_parse_state
*state
)
1134 struct simple_node
*ptr
;
1135 const struct glsl_type
*decl_type
;
1136 const char *type_name
= NULL
;
1139 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1140 * FINISHME: include a type. These re-declare built-in variables to be
1141 * FINISHME: invariant.
1144 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1146 foreach (ptr
, &this->declarations
) {
1147 struct ast_declaration
*const decl
= (struct ast_declaration
* )ptr
;
1148 const struct glsl_type
*var_type
;
1149 struct ir_variable
*var
;
1150 YYLTYPE loc
= this->get_location();
1152 /* FINISHME: Emit a warning if a variable declaration shadows a
1153 * FINISHME: declaration at a higher scope.
1156 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1157 if (type_name
!= NULL
) {
1158 _mesa_glsl_error(& loc
, state
,
1159 "invalid type `%s' in declaration of `%s'",
1160 type_name
, decl
->identifier
);
1162 _mesa_glsl_error(& loc
, state
,
1163 "invalid type in declaration of `%s'",
1169 if (decl
->is_array
) {
1170 var_type
= process_array_type(decl_type
, decl
->array_size
, state
);
1172 var_type
= decl_type
;
1175 var
= new ir_variable(var_type
, decl
->identifier
);
1177 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1180 /* Attempt to add the variable to the symbol table. If this fails, it
1181 * means the variable has already been declared at this scope.
1183 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
1184 YYLTYPE loc
= this->get_location();
1186 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
1191 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1193 * "Identifiers starting with "gl_" are reserved for use by
1194 * OpenGL, and may not be declared in a shader as either a
1195 * variable or a function."
1197 if (strncmp(decl
->identifier
, "gl_", 3) == 0) {
1198 /* FINISHME: This should only trigger if we're not redefining
1199 * FINISHME: a builtin (to add a qualifier, for example).
1201 _mesa_glsl_error(& loc
, state
,
1202 "identifier `%s' uses reserved `gl_' prefix",
1206 instructions
->push_tail(var
);
1208 if (state
->current_function
!= NULL
) {
1209 const char *mode
= NULL
;
1210 const char *extra
= "";
1212 /* There is no need to check for 'inout' here because the parser will
1213 * only allow that in function parameter lists.
1215 if (this->type
->qualifier
.attribute
) {
1217 } else if (this->type
->qualifier
.uniform
) {
1219 } else if (this->type
->qualifier
.varying
) {
1221 } else if (this->type
->qualifier
.in
) {
1223 extra
= " or in function parameter list";
1224 } else if (this->type
->qualifier
.out
) {
1226 extra
= " or in function parameter list";
1230 _mesa_glsl_error(& loc
, state
,
1231 "%s variable `%s' must be declared at "
1233 mode
, var
->name
, extra
);
1235 } else if (var
->mode
== ir_var_in
) {
1236 if (state
->target
== vertex_shader
) {
1237 bool error_emitted
= false;
1239 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1241 * "Vertex shader inputs can only be float, floating-point
1242 * vectors, matrices, signed and unsigned integers and integer
1243 * vectors. Vertex shader inputs can also form arrays of these
1244 * types, but not structures."
1246 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1248 * "Vertex shader inputs can only be float, floating-point
1249 * vectors, matrices, signed and unsigned integers and integer
1250 * vectors. They cannot be arrays or structures."
1252 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1254 * "The attribute qualifier can be used only with float,
1255 * floating-point vectors, and matrices. Attribute variables
1256 * cannot be declared as arrays or structures."
1258 const glsl_type
*check_type
= var
->type
->is_array()
1259 ? var
->type
->fields
.array
: var
->type
;
1261 switch (check_type
->base_type
) {
1262 case GLSL_TYPE_FLOAT
:
1264 case GLSL_TYPE_UINT
:
1266 if (state
->language_version
> 120)
1270 _mesa_glsl_error(& loc
, state
,
1271 "vertex shader input / attribute cannot have "
1273 var
->type
->is_array() ? "array of " : "",
1275 error_emitted
= true;
1278 if (!error_emitted
&& (state
->language_version
<= 130)
1279 && var
->type
->is_array()) {
1280 _mesa_glsl_error(& loc
, state
,
1281 "vertex shader input / attribute cannot have "
1283 error_emitted
= true;
1288 if (decl
->initializer
!= NULL
) {
1289 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
1291 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1293 * "All uniform variables are read-only and are initialized either
1294 * directly by an application via API commands, or indirectly by
1297 if ((state
->language_version
<= 110)
1298 && (var
->mode
== ir_var_uniform
)) {
1299 _mesa_glsl_error(& initializer_loc
, state
,
1300 "cannot initialize uniforms in GLSL 1.10");
1303 if (var
->type
->is_sampler()) {
1304 _mesa_glsl_error(& initializer_loc
, state
,
1305 "cannot initialize samplers");
1308 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1309 _mesa_glsl_error(& initializer_loc
, state
,
1310 "cannot initialize %s shader input / %s",
1311 (state
->target
== vertex_shader
)
1312 ? "vertex" : "fragment",
1313 (state
->target
== vertex_shader
)
1314 ? "attribute" : "varying");
1317 ir_dereference
*const lhs
= new ir_dereference(var
);
1318 ir_rvalue
*const rhs
= decl
->initializer
->hir(instructions
, state
);
1320 /* FINISHME: If the declaration is either 'const' or 'uniform', the
1321 * FINISHME: initializer (rhs) must be a constant expression.
1324 if (!rhs
->type
->is_error()) {
1325 (void) do_assignment(instructions
, state
, lhs
, rhs
,
1326 this->get_location());
1330 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1332 * "It is an error to write to a const variable outside of
1333 * its declaration, so they must be initialized when
1336 if (this->type
->qualifier
.constant
&& decl
->initializer
== NULL
) {
1337 _mesa_glsl_error(& loc
, state
,
1338 "const declaration of `%s' must be initialized");
1341 /* Add the vairable to the symbol table after processing the initializer.
1342 * This differs from most C-like languages, but it follows the GLSL
1343 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1346 * "Within a declaration, the scope of a name starts immediately
1347 * after the initializer if present or immediately after the name
1348 * being declared if not."
1350 const bool added_variable
=
1351 state
->symbols
->add_variable(decl
->identifier
, var
);
1352 assert(added_variable
);
1355 /* Variable declarations do not have r-values.
1362 ast_parameter_declarator::hir(exec_list
*instructions
,
1363 struct _mesa_glsl_parse_state
*state
)
1365 const struct glsl_type
*type
;
1366 const char *name
= NULL
;
1367 YYLTYPE loc
= this->get_location();
1369 type
= this->type
->specifier
->glsl_type(& name
, state
);
1373 _mesa_glsl_error(& loc
, state
,
1374 "invalid type `%s' in declaration of `%s'",
1375 name
, this->identifier
);
1377 _mesa_glsl_error(& loc
, state
,
1378 "invalid type in declaration of `%s'",
1382 type
= glsl_type::error_type
;
1385 ir_variable
*var
= new ir_variable(type
, this->identifier
);
1387 /* FINISHME: Handle array declarations. Note that this requires
1388 * FINISHME: complete handling of constant expressions.
1391 /* Apply any specified qualifiers to the parameter declaration. Note that
1392 * for function parameters the default mode is 'in'.
1394 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
1395 if (var
->mode
== ir_var_auto
)
1396 var
->mode
= ir_var_in
;
1398 instructions
->push_tail(var
);
1400 /* Parameter declarations do not have r-values.
1407 ast_function_parameters_to_hir(struct simple_node
*ast_parameters
,
1408 exec_list
*ir_parameters
,
1409 struct _mesa_glsl_parse_state
*state
)
1411 struct simple_node
*ptr
;
1413 foreach (ptr
, ast_parameters
) {
1414 ((ast_node
*)ptr
)->hir(ir_parameters
, state
);
1420 parameter_lists_match(exec_list
*list_a
, exec_list
*list_b
)
1422 exec_list_iterator iter_a
= list_a
->iterator();
1423 exec_list_iterator iter_b
= list_b
->iterator();
1425 while (iter_a
.has_next()) {
1426 /* If all of the parameters from the other parameter list have been
1427 * exhausted, the lists have different length and, by definition,
1430 if (!iter_b
.has_next())
1433 /* If the types of the parameters do not match, the parameters lists
1448 ast_function_definition::hir(exec_list
*instructions
,
1449 struct _mesa_glsl_parse_state
*state
)
1452 ir_function_signature
*signature
= NULL
;
1453 ir_function
*f
= NULL
;
1454 exec_list parameters
;
1457 /* Convert the list of function parameters to HIR now so that they can be
1458 * used below to compare this function's signature with previously seen
1459 * signatures for functions with the same name.
1461 ast_function_parameters_to_hir(& this->prototype
->parameters
, & parameters
,
1464 const char *return_type_name
;
1465 const glsl_type
*return_type
=
1466 this->prototype
->return_type
->specifier
->glsl_type(& return_type_name
,
1469 assert(return_type
!= NULL
);
1471 /* Verify that this function's signature either doesn't match a previously
1472 * seen signature for a function with the same name, or, if a match is found,
1473 * that the previously seen signature does not have an associated definition.
1475 const char *const name
= this->prototype
->identifier
;
1476 f
= state
->symbols
->get_function(name
);
1478 foreach_iter(exec_list_iterator
, iter
, *f
) {
1479 signature
= (struct ir_function_signature
*) iter
.get();
1481 /* Compare the parameter list of the function being defined to the
1482 * existing function. If the parameter lists match, then the return
1483 * type must also match and the existing function must not have a
1486 if (parameter_lists_match(& parameters
, & signature
->parameters
)) {
1487 /* FINISHME: Compare return types. */
1489 if (signature
->definition
!= NULL
) {
1490 YYLTYPE loc
= this->get_location();
1492 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
1501 } else if (state
->symbols
->name_declared_this_scope(name
)) {
1502 /* This function name shadows a non-function use of the same name.
1504 YYLTYPE loc
= this->get_location();
1506 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
1507 "non-function", name
);
1510 f
= new ir_function(name
);
1511 state
->symbols
->add_function(f
->name
, f
);
1514 /* Verify the return type of main() */
1515 if (strcmp(name
, "main") == 0) {
1516 if (return_type
!= glsl_type::get_instance(GLSL_TYPE_VOID
, 0, 0)) {
1517 YYLTYPE loc
= this->get_location();
1519 _mesa_glsl_error(& loc
, state
, "main() must return void");
1522 if (!parameters
.is_empty()) {
1523 YYLTYPE loc
= this->get_location();
1525 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
1529 /* Finish storing the information about this new function in its signature.
1531 if (signature
== NULL
) {
1532 signature
= new ir_function_signature(return_type
);
1533 f
->add_signature(signature
);
1535 /* Destroy all of the previous parameter information. The previous
1536 * parameter information comes from the function prototype, and it can
1537 * either include invalid parameter names or may not have names at all.
1539 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
1540 assert(((ir_instruction
*) iter
.get())->as_variable() != NULL
);
1548 assert(state
->current_function
== NULL
);
1549 state
->current_function
= signature
;
1551 ast_function_parameters_to_hir(& this->prototype
->parameters
,
1552 & signature
->parameters
,
1554 /* FINISHME: Set signature->return_type */
1556 label
= new ir_label(name
);
1557 if (signature
->definition
== NULL
) {
1558 signature
->definition
= label
;
1560 instructions
->push_tail(label
);
1562 /* Add the function parameters to the symbol table. During this step the
1563 * parameter declarations are also moved from the temporary "parameters" list
1564 * to the instruction list. There are other more efficient ways to do this,
1565 * but they involve ugly linked-list gymnastics.
1567 state
->symbols
->push_scope();
1568 foreach_iter(exec_list_iterator
, iter
, parameters
) {
1569 ir_variable
*const var
= (ir_variable
*) iter
.get();
1571 assert(((ir_instruction
*) var
)->as_variable() != NULL
);
1574 instructions
->push_tail(var
);
1576 /* The only way a parameter would "exist" is if two parameters have
1579 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
1580 YYLTYPE loc
= this->get_location();
1582 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
1584 state
->symbols
->add_variable(var
->name
, var
);
1588 /* Convert the body of the function to HIR, and append the resulting
1589 * instructions to the list that currently consists of the function label
1590 * and the function parameters.
1592 this->body
->hir(instructions
, state
);
1594 state
->symbols
->pop_scope();
1596 assert(state
->current_function
== signature
);
1597 state
->current_function
= NULL
;
1599 /* Function definitions do not have r-values.
1606 ast_jump_statement::hir(exec_list
*instructions
,
1607 struct _mesa_glsl_parse_state
*state
)
1610 if (mode
== ast_return
) {
1612 assert(state
->current_function
);
1614 if (opt_return_value
) {
1615 if (state
->current_function
->return_type
->base_type
==
1617 YYLTYPE loc
= this->get_location();
1619 _mesa_glsl_error(& loc
, state
,
1620 "`return` with a value, in function `%s' "
1622 state
->current_function
->definition
->label
);
1625 ir_expression
*const ret
= (ir_expression
*)
1626 opt_return_value
->hir(instructions
, state
);
1627 assert(ret
!= NULL
);
1629 /* FINISHME: Make sure the type of the return value matches the return
1630 * FINISHME: type of the enclosing function.
1633 inst
= new ir_return(ret
);
1635 if (state
->current_function
->return_type
->base_type
!=
1637 YYLTYPE loc
= this->get_location();
1639 _mesa_glsl_error(& loc
, state
,
1640 "`return' with no value, in function %s returning "
1642 state
->current_function
->definition
->label
);
1644 inst
= new ir_return
;
1647 instructions
->push_tail(inst
);
1650 if (mode
== ast_discard
) {
1651 /* FINISHME: discard support */
1652 if (state
->target
!= fragment_shader
) {
1653 YYLTYPE loc
= this->get_location();
1655 _mesa_glsl_error(& loc
, state
,
1656 "`discard' may only appear in a fragment shader");
1660 /* Jump instructions do not have r-values.
1667 ast_selection_statement::hir(exec_list
*instructions
,
1668 struct _mesa_glsl_parse_state
*state
)
1670 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
1672 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
1674 * "Any expression whose type evaluates to a Boolean can be used as the
1675 * conditional expression bool-expression. Vector types are not accepted
1676 * as the expression to if."
1678 * The checks are separated so that higher quality diagnostics can be
1679 * generated for cases where both rules are violated.
1681 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
1682 YYLTYPE loc
= this->condition
->get_location();
1684 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
1688 ir_if
*const stmt
= new ir_if(condition
);
1690 if (then_statement
!= NULL
) {
1691 ast_node
*node
= (ast_node
*) then_statement
;
1693 node
->hir(& stmt
->then_instructions
, state
);
1694 node
= (ast_node
*) node
->next
;
1695 } while (node
!= then_statement
);
1698 if (else_statement
!= NULL
) {
1699 ast_node
*node
= (ast_node
*) else_statement
;
1701 node
->hir(& stmt
->else_instructions
, state
);
1702 node
= (ast_node
*) node
->next
;
1703 } while (node
!= else_statement
);
1706 instructions
->push_tail(stmt
);
1708 /* if-statements do not have r-values.