2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
31 * * Symbol table management
35 * The majority of this work could be done during parsing, and the parser could
36 * probably generate HIR directly. However, this results in frequent changes
37 * to the parser code. Since we do not assume that every system this complier
38 * is built on will have Flex and Bison installed, we have to store the code
39 * generated by these tools in our version control system. In other parts of
40 * the system we've seen problems where a parser was changed but the generated
41 * code was not committed, merge conflicts where created because two developers
42 * had slightly different versions of Bison installed, etc.
44 * I have also noticed that running Bison generated parsers in GDB is very
45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very
46 * well 'print $1' in GDB.
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
52 #include "main/imports.h"
53 #include "glsl_symbol_table.h"
54 #include "glsl_parser_extras.h"
56 #include "glsl_types.h"
60 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
62 struct simple_node
*ptr
;
64 _mesa_glsl_initialize_variables(instructions
, state
);
65 _mesa_glsl_initialize_constructors(instructions
, state
);
66 _mesa_glsl_initialize_functions(instructions
, state
);
68 state
->current_function
= NULL
;
70 foreach (ptr
, & state
->translation_unit
) {
71 ((ast_node
*)ptr
)->hir(instructions
, state
);
77 * If a conversion is available, convert one operand to a different type
79 * The \c from \c ir_rvalue is converted "in place".
81 * \param to Type that the operand it to be converted to
82 * \param from Operand that is being converted
83 * \param state GLSL compiler state
86 * If a conversion is possible (or unnecessary), \c true is returned.
87 * Otherwise \c false is returned.
90 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
91 struct _mesa_glsl_parse_state
*state
)
93 if (to
->base_type
== from
->type
->base_type
)
96 /* This conversion was added in GLSL 1.20. If the compilation mode is
97 * GLSL 1.10, the conversion is skipped.
99 if (state
->language_version
< 120)
102 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
104 * "There are no implicit array or structure conversions. For
105 * example, an array of int cannot be implicitly converted to an
106 * array of float. There are no implicit conversions between
107 * signed and unsigned integers."
109 /* FINISHME: The above comment is partially a lie. There is int/uint
110 * FINISHME: conversion for immediate constants.
112 if (!to
->is_float() || !from
->type
->is_numeric())
115 switch (from
->type
->base_type
) {
117 from
= new ir_expression(ir_unop_i2f
, to
, from
, NULL
);
120 from
= new ir_expression(ir_unop_u2f
, to
, from
, NULL
);
123 from
= new ir_expression(ir_unop_b2f
, to
, from
, NULL
);
133 static const struct glsl_type
*
134 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
136 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
138 const glsl_type
*const type_a
= value_a
->type
;
139 const glsl_type
*const type_b
= value_b
->type
;
141 /* From GLSL 1.50 spec, page 56:
143 * "The arithmetic binary operators add (+), subtract (-),
144 * multiply (*), and divide (/) operate on integer and
145 * floating-point scalars, vectors, and matrices."
147 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
148 _mesa_glsl_error(loc
, state
,
149 "Operands to arithmetic operators must be numeric");
150 return glsl_type::error_type
;
154 /* "If one operand is floating-point based and the other is
155 * not, then the conversions from Section 4.1.10 "Implicit
156 * Conversions" are applied to the non-floating-point-based operand."
158 if (!apply_implicit_conversion(type_a
, value_b
, state
)
159 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
160 _mesa_glsl_error(loc
, state
,
161 "Could not implicitly convert operands to "
162 "arithmetic operator");
163 return glsl_type::error_type
;
166 /* "If the operands are integer types, they must both be signed or
169 * From this rule and the preceeding conversion it can be inferred that
170 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
171 * The is_numeric check above already filtered out the case where either
172 * type is not one of these, so now the base types need only be tested for
175 if (type_a
->base_type
!= type_b
->base_type
) {
176 _mesa_glsl_error(loc
, state
,
177 "base type mismatch for arithmetic operator");
178 return glsl_type::error_type
;
181 /* "All arithmetic binary operators result in the same fundamental type
182 * (signed integer, unsigned integer, or floating-point) as the
183 * operands they operate on, after operand type conversion. After
184 * conversion, the following cases are valid
186 * * The two operands are scalars. In this case the operation is
187 * applied, resulting in a scalar."
189 if (type_a
->is_scalar() && type_b
->is_scalar())
192 /* "* One operand is a scalar, and the other is a vector or matrix.
193 * In this case, the scalar operation is applied independently to each
194 * component of the vector or matrix, resulting in the same size
197 if (type_a
->is_scalar()) {
198 if (!type_b
->is_scalar())
200 } else if (type_b
->is_scalar()) {
204 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
205 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
208 assert(!type_a
->is_scalar());
209 assert(!type_b
->is_scalar());
211 /* "* The two operands are vectors of the same size. In this case, the
212 * operation is done component-wise resulting in the same size
215 if (type_a
->is_vector() && type_b
->is_vector()) {
216 if (type_a
== type_b
) {
219 _mesa_glsl_error(loc
, state
,
220 "vector size mismatch for arithmetic operator");
221 return glsl_type::error_type
;
225 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
226 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
227 * <vector, vector> have been handled. At least one of the operands must
228 * be matrix. Further, since there are no integer matrix types, the base
229 * type of both operands must be float.
231 assert(type_a
->is_matrix() || type_b
->is_matrix());
232 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
233 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
235 /* "* The operator is add (+), subtract (-), or divide (/), and the
236 * operands are matrices with the same number of rows and the same
237 * number of columns. In this case, the operation is done component-
238 * wise resulting in the same size matrix."
239 * * The operator is multiply (*), where both operands are matrices or
240 * one operand is a vector and the other a matrix. A right vector
241 * operand is treated as a column vector and a left vector operand as a
242 * row vector. In all these cases, it is required that the number of
243 * columns of the left operand is equal to the number of rows of the
244 * right operand. Then, the multiply (*) operation does a linear
245 * algebraic multiply, yielding an object that has the same number of
246 * rows as the left operand and the same number of columns as the right
247 * operand. Section 5.10 "Vector and Matrix Operations" explains in
248 * more detail how vectors and matrices are operated on."
251 if (type_a
== type_b
)
254 if (type_a
->is_matrix() && type_b
->is_matrix()) {
255 /* Matrix multiply. The columns of A must match the rows of B. Given
256 * the other previously tested constraints, this means the vector type
257 * of a row from A must be the same as the vector type of a column from
260 if (type_a
->row_type() == type_b
->column_type()) {
261 /* The resulting matrix has the number of columns of matrix B and
262 * the number of rows of matrix A. We get the row count of A by
263 * looking at the size of a vector that makes up a column. The
264 * transpose (size of a row) is done for B.
266 const glsl_type
*const type
=
267 glsl_type::get_instance(type_a
->base_type
,
268 type_a
->column_type()->vector_elements
,
269 type_b
->row_type()->vector_elements
);
270 assert(type
!= glsl_type::error_type
);
274 } else if (type_a
->is_matrix()) {
275 /* A is a matrix and B is a column vector. Columns of A must match
276 * rows of B. Given the other previously tested constraints, this
277 * means the vector type of a row from A must be the same as the
278 * vector the type of B.
280 if (type_a
->row_type() == type_b
)
283 assert(type_b
->is_matrix());
285 /* A is a row vector and B is a matrix. Columns of A must match rows
286 * of B. Given the other previously tested constraints, this means
287 * the type of A must be the same as the vector type of a column from
290 if (type_a
== type_b
->column_type())
294 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
295 return glsl_type::error_type
;
299 /* "All other cases are illegal."
301 _mesa_glsl_error(loc
, state
, "type mismatch");
302 return glsl_type::error_type
;
306 static const struct glsl_type
*
307 unary_arithmetic_result_type(const struct glsl_type
*type
,
308 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
310 /* From GLSL 1.50 spec, page 57:
312 * "The arithmetic unary operators negate (-), post- and pre-increment
313 * and decrement (-- and ++) operate on integer or floating-point
314 * values (including vectors and matrices). All unary operators work
315 * component-wise on their operands. These result with the same type
318 if (!type
->is_numeric()) {
319 _mesa_glsl_error(loc
, state
,
320 "Operands to arithmetic operators must be numeric");
321 return glsl_type::error_type
;
328 static const struct glsl_type
*
329 modulus_result_type(const struct glsl_type
*type_a
,
330 const struct glsl_type
*type_b
,
331 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
333 /* From GLSL 1.50 spec, page 56:
334 * "The operator modulus (%) operates on signed or unsigned integers or
335 * integer vectors. The operand types must both be signed or both be
338 if (!type_a
->is_integer() || !type_b
->is_integer()
339 || (type_a
->base_type
!= type_b
->base_type
)) {
340 _mesa_glsl_error(loc
, state
, "type mismatch");
341 return glsl_type::error_type
;
344 /* "The operands cannot be vectors of differing size. If one operand is
345 * a scalar and the other vector, then the scalar is applied component-
346 * wise to the vector, resulting in the same type as the vector. If both
347 * are vectors of the same size, the result is computed component-wise."
349 if (type_a
->is_vector()) {
350 if (!type_b
->is_vector()
351 || (type_a
->vector_elements
== type_b
->vector_elements
))
356 /* "The operator modulus (%) is not defined for any other data types
357 * (non-integer types)."
359 _mesa_glsl_error(loc
, state
, "type mismatch");
360 return glsl_type::error_type
;
364 static const struct glsl_type
*
365 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
366 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
368 const glsl_type
*const type_a
= value_a
->type
;
369 const glsl_type
*const type_b
= value_b
->type
;
371 /* From GLSL 1.50 spec, page 56:
372 * "The relational operators greater than (>), less than (<), greater
373 * than or equal (>=), and less than or equal (<=) operate only on
374 * scalar integer and scalar floating-point expressions."
376 if (!type_a
->is_numeric()
377 || !type_b
->is_numeric()
378 || !type_a
->is_scalar()
379 || !type_b
->is_scalar()) {
380 _mesa_glsl_error(loc
, state
,
381 "Operands to relational operators must be scalar and "
383 return glsl_type::error_type
;
386 /* "Either the operands' types must match, or the conversions from
387 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
388 * operand, after which the types must match."
390 if (!apply_implicit_conversion(type_a
, value_b
, state
)
391 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
392 _mesa_glsl_error(loc
, state
,
393 "Could not implicitly convert operands to "
394 "relational operator");
395 return glsl_type::error_type
;
398 if (type_a
->base_type
!= type_b
->base_type
) {
399 _mesa_glsl_error(loc
, state
, "base type mismatch");
400 return glsl_type::error_type
;
403 /* "The result is scalar Boolean."
405 return glsl_type::bool_type
;
410 * Validates that a value can be assigned to a location with a specified type
412 * Validates that \c rhs can be assigned to some location. If the types are
413 * not an exact match but an automatic conversion is possible, \c rhs will be
417 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
418 * Otherwise the actual RHS to be assigned will be returned. This may be
419 * \c rhs, or it may be \c rhs after some type conversion.
422 * In addition to being used for assignments, this function is used to
423 * type-check return values.
426 validate_assignment(const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
428 const glsl_type
*const rhs_type
= rhs
->type
;
430 /* If there is already some error in the RHS, just return it. Anything
431 * else will lead to an avalanche of error message back to the user.
433 if (rhs_type
->is_error())
436 /* If the types are identical, the assignment can trivially proceed.
438 if (rhs_type
== lhs_type
)
441 /* If the array element types are the same and the size of the LHS is zero,
442 * the assignment is okay.
444 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
445 * is handled by ir_dereference::is_lvalue.
447 if (lhs_type
->is_array() && rhs
->type
->is_array()
448 && (lhs_type
->element_type() == rhs
->type
->element_type())
449 && (lhs_type
->array_size() == 0)) {
453 /* FINISHME: Check for and apply automatic conversions. */
458 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
459 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
462 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
464 if (!error_emitted
) {
465 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
466 if (!lhs
->is_lvalue()) {
467 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
468 error_emitted
= true;
472 ir_rvalue
*new_rhs
= validate_assignment(lhs
->type
, rhs
);
473 if (new_rhs
== NULL
) {
474 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
478 /* If the LHS array was not declared with a size, it takes it size from
479 * the RHS. If the LHS is an l-value and a whole array, it must be a
480 * dereference of a variable. Any other case would require that the LHS
481 * is either not an l-value or not a whole array.
483 if (lhs
->type
->array_size() == 0) {
484 ir_dereference
*const d
= lhs
->as_dereference();
488 ir_variable
*const var
= d
->var
->as_variable();
492 if (var
->max_array_access
>= unsigned(rhs
->type
->array_size())) {
493 /* FINISHME: This should actually log the location of the RHS. */
494 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
496 var
->max_array_access
);
499 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
500 rhs
->type
->array_size());
504 ir_instruction
*tmp
= new ir_assignment(lhs
, rhs
, NULL
);
505 instructions
->push_tail(tmp
);
512 * Generate a new temporary and add its declaration to the instruction stream
515 generate_temporary(const glsl_type
*type
, exec_list
*instructions
,
516 struct _mesa_glsl_parse_state
*state
)
518 char *name
= (char *) malloc(sizeof(char) * 13);
520 snprintf(name
, 13, "tmp_%08X", state
->temp_index
);
523 ir_variable
*const var
= new ir_variable(type
, name
);
524 instructions
->push_tail(var
);
531 get_lvalue_copy(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
532 ir_rvalue
*lvalue
, YYLTYPE loc
)
535 ir_rvalue
*var_deref
;
537 /* FINISHME: Give unique names to the temporaries. */
538 var
= new ir_variable(lvalue
->type
, "_internal_tmp");
539 var
->mode
= ir_var_auto
;
541 var_deref
= new ir_dereference(var
);
542 do_assignment(instructions
, state
, var_deref
, lvalue
, loc
);
544 /* Once we've created this temporary, mark it read only so it's no
545 * longer considered an lvalue.
547 var
->read_only
= true;
554 ast_node::hir(exec_list
*instructions
,
555 struct _mesa_glsl_parse_state
*state
)
565 ast_expression::hir(exec_list
*instructions
,
566 struct _mesa_glsl_parse_state
*state
)
568 static const int operations
[AST_NUM_OPERATORS
] = {
569 -1, /* ast_assign doesn't convert to ir_expression. */
570 -1, /* ast_plus doesn't convert to ir_expression. */
594 /* Note: The following block of expression types actually convert
595 * to multiple IR instructions.
597 ir_binop_mul
, /* ast_mul_assign */
598 ir_binop_div
, /* ast_div_assign */
599 ir_binop_mod
, /* ast_mod_assign */
600 ir_binop_add
, /* ast_add_assign */
601 ir_binop_sub
, /* ast_sub_assign */
602 ir_binop_lshift
, /* ast_ls_assign */
603 ir_binop_rshift
, /* ast_rs_assign */
604 ir_binop_bit_and
, /* ast_and_assign */
605 ir_binop_bit_xor
, /* ast_xor_assign */
606 ir_binop_bit_or
, /* ast_or_assign */
608 -1, /* ast_conditional doesn't convert to ir_expression. */
609 ir_binop_add
, /* ast_pre_inc. */
610 ir_binop_sub
, /* ast_pre_dec. */
611 ir_binop_add
, /* ast_post_inc. */
612 ir_binop_sub
, /* ast_post_dec. */
613 -1, /* ast_field_selection doesn't conv to ir_expression. */
614 -1, /* ast_array_index doesn't convert to ir_expression. */
615 -1, /* ast_function_call doesn't conv to ir_expression. */
616 -1, /* ast_identifier doesn't convert to ir_expression. */
617 -1, /* ast_int_constant doesn't convert to ir_expression. */
618 -1, /* ast_uint_constant doesn't conv to ir_expression. */
619 -1, /* ast_float_constant doesn't conv to ir_expression. */
620 -1, /* ast_bool_constant doesn't conv to ir_expression. */
621 -1, /* ast_sequence doesn't convert to ir_expression. */
623 ir_rvalue
*result
= NULL
;
625 struct simple_node op_list
;
626 const struct glsl_type
*type
= glsl_type::error_type
;
627 bool error_emitted
= false;
630 loc
= this->get_location();
631 make_empty_list(& op_list
);
633 switch (this->oper
) {
635 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
636 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
638 result
= do_assignment(instructions
, state
, op
[0], op
[1],
639 this->subexpressions
[0]->get_location());
640 error_emitted
= result
->type
->is_error();
646 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
648 error_emitted
= op
[0]->type
->is_error();
649 if (type
->is_error())
656 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
658 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
660 error_emitted
= type
->is_error();
662 result
= new ir_expression(operations
[this->oper
], type
,
670 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
671 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
673 type
= arithmetic_result_type(op
[0], op
[1],
674 (this->oper
== ast_mul
),
676 error_emitted
= type
->is_error();
678 result
= new ir_expression(operations
[this->oper
], type
,
683 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
684 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
686 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
688 assert(operations
[this->oper
] == ir_binop_mod
);
690 result
= new ir_expression(operations
[this->oper
], type
,
692 error_emitted
= type
->is_error();
697 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-shift operators");
698 error_emitted
= true;
705 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
706 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
708 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
710 /* The relational operators must either generate an error or result
711 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
713 assert(type
->is_error()
714 || ((type
->base_type
== GLSL_TYPE_BOOL
)
715 && type
->is_scalar()));
717 result
= new ir_expression(operations
[this->oper
], type
,
719 error_emitted
= type
->is_error();
724 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
725 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
727 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
729 * "The equality operators equal (==), and not equal (!=)
730 * operate on all types. They result in a scalar Boolean. If
731 * the operand types do not match, then there must be a
732 * conversion from Section 4.1.10 "Implicit Conversions"
733 * applied to one operand that can make them match, in which
734 * case this conversion is done."
736 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
737 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
738 || (op
[0]->type
!= op
[1]->type
)) {
739 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
740 "type", (this->oper
== ast_equal
) ? "==" : "!=");
741 error_emitted
= true;
742 } else if ((state
->language_version
<= 110)
743 && (op
[0]->type
->is_array() || op
[1]->type
->is_array())) {
744 _mesa_glsl_error(& loc
, state
, "array comparisons forbidden in "
746 error_emitted
= true;
749 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
751 type
= glsl_type::bool_type
;
753 assert(result
->type
== glsl_type::bool_type
);
760 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-wise operators");
761 error_emitted
= true;
767 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
768 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
770 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
771 YYLTYPE loc
= this->subexpressions
[0]->get_location();
773 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
774 operator_string(this->oper
));
775 error_emitted
= true;
778 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
779 YYLTYPE loc
= this->subexpressions
[1]->get_location();
781 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
782 operator_string(this->oper
));
783 error_emitted
= true;
786 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
788 type
= glsl_type::bool_type
;
792 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
794 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
795 YYLTYPE loc
= this->subexpressions
[0]->get_location();
797 _mesa_glsl_error(& loc
, state
,
798 "operand of `!' must be scalar boolean");
799 error_emitted
= true;
802 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
804 type
= glsl_type::bool_type
;
810 case ast_sub_assign
: {
811 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
812 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
814 type
= arithmetic_result_type(op
[0], op
[1],
815 (this->oper
== ast_mul_assign
),
818 ir_rvalue
*temp_rhs
= new ir_expression(operations
[this->oper
], type
,
821 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
822 this->subexpressions
[0]->get_location());
824 error_emitted
= (op
[0]->type
->is_error());
826 /* GLSL 1.10 does not allow array assignment. However, we don't have to
827 * explicitly test for this because none of the binary expression
828 * operators allow array operands either.
834 case ast_mod_assign
: {
835 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
836 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
838 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
840 assert(operations
[this->oper
] == ir_binop_mod
);
842 struct ir_rvalue
*temp_rhs
;
843 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
846 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
847 this->subexpressions
[0]->get_location());
849 error_emitted
= type
->is_error();
855 _mesa_glsl_error(& loc
, state
,
856 "FINISHME: implement bit-shift assignment operators");
857 error_emitted
= true;
863 _mesa_glsl_error(& loc
, state
,
864 "FINISHME: implement logic assignment operators");
865 error_emitted
= true;
868 case ast_conditional
: {
869 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
871 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
873 * "The ternary selection operator (?:). It operates on three
874 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
875 * first expression, which must result in a scalar Boolean."
877 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
878 YYLTYPE loc
= this->subexpressions
[0]->get_location();
880 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
881 error_emitted
= true;
884 /* The :? operator is implemented by generating an anonymous temporary
885 * followed by an if-statement. The last instruction in each branch of
886 * the if-statement assigns a value to the anonymous temporary. This
887 * temporary is the r-value of the expression.
889 ir_variable
*const tmp
= generate_temporary(glsl_type::error_type
,
890 instructions
, state
);
892 ir_if
*const stmt
= new ir_if(op
[0]);
893 instructions
->push_tail(stmt
);
895 op
[1] = this->subexpressions
[1]->hir(& stmt
->then_instructions
, state
);
896 ir_dereference
*const then_deref
= new ir_dereference(tmp
);
897 ir_assignment
*const then_assign
=
898 new ir_assignment(then_deref
, op
[1], NULL
);
899 stmt
->then_instructions
.push_tail(then_assign
);
901 op
[2] = this->subexpressions
[2]->hir(& stmt
->else_instructions
, state
);
902 ir_dereference
*const else_deref
= new ir_dereference(tmp
);
903 ir_assignment
*const else_assign
=
904 new ir_assignment(else_deref
, op
[2], NULL
);
905 stmt
->else_instructions
.push_tail(else_assign
);
907 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
909 * "The second and third expressions can be any type, as
910 * long their types match, or there is a conversion in
911 * Section 4.1.10 "Implicit Conversions" that can be applied
912 * to one of the expressions to make their types match. This
913 * resulting matching type is the type of the entire
916 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
917 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
918 || (op
[1]->type
!= op
[2]->type
)) {
919 YYLTYPE loc
= this->subexpressions
[1]->get_location();
921 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
922 "operator must have matching types.");
923 error_emitted
= true;
925 tmp
->type
= op
[1]->type
;
928 result
= new ir_dereference(tmp
);
935 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
936 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
937 op
[1] = new ir_constant(1.0f
);
939 op
[1] = new ir_constant(1);
941 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
943 struct ir_rvalue
*temp_rhs
;
944 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
947 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
948 this->subexpressions
[0]->get_location());
950 error_emitted
= op
[0]->type
->is_error();
956 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
957 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
958 op
[1] = new ir_constant(1.0f
);
960 op
[1] = new ir_constant(1);
962 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
964 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
966 struct ir_rvalue
*temp_rhs
;
967 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
970 /* Get a temporary of a copy of the lvalue before it's modified.
971 * This may get thrown away later.
973 result
= get_lvalue_copy(instructions
, state
, op
[0],
974 this->subexpressions
[0]->get_location());
976 (void)do_assignment(instructions
, state
, op
[0], temp_rhs
,
977 this->subexpressions
[0]->get_location());
980 error_emitted
= op
[0]->type
->is_error();
984 case ast_field_selection
:
985 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
989 case ast_array_index
: {
990 YYLTYPE index_loc
= subexpressions
[1]->get_location();
992 op
[0] = subexpressions
[0]->hir(instructions
, state
);
993 op
[1] = subexpressions
[1]->hir(instructions
, state
);
995 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
997 ir_dereference
*const lhs
= op
[0]->as_dereference();
998 ir_instruction
*array
;
1000 && (lhs
->mode
== ir_dereference::ir_reference_variable
)) {
1001 result
= new ir_dereference(lhs
->var
, op
[1]);
1006 result
= new ir_dereference(op
[0], op
[1]);
1010 /* Do not use op[0] after this point. Use array.
1018 if (!array
->type
->is_array()
1019 && !array
->type
->is_matrix()
1020 && !array
->type
->is_vector()) {
1021 _mesa_glsl_error(& index_loc
, state
,
1022 "cannot dereference non-array / non-matrix / "
1024 error_emitted
= true;
1027 if (!op
[1]->type
->is_integer()) {
1028 _mesa_glsl_error(& index_loc
, state
,
1029 "array index must be integer type");
1030 error_emitted
= true;
1031 } else if (!op
[1]->type
->is_scalar()) {
1032 _mesa_glsl_error(& index_loc
, state
,
1033 "array index must be scalar");
1034 error_emitted
= true;
1037 /* If the array index is a constant expression and the array has a
1038 * declared size, ensure that the access is in-bounds. If the array
1039 * index is not a constant expression, ensure that the array has a
1042 ir_constant
*const const_index
= op
[1]->constant_expression_value();
1043 if (const_index
!= NULL
) {
1044 const int idx
= const_index
->value
.i
[0];
1045 const char *type_name
;
1048 if (array
->type
->is_matrix()) {
1049 type_name
= "matrix";
1050 } else if (array
->type
->is_vector()) {
1051 type_name
= "vector";
1053 type_name
= "array";
1056 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1058 * "It is illegal to declare an array with a size, and then
1059 * later (in the same shader) index the same array with an
1060 * integral constant expression greater than or equal to the
1061 * declared size. It is also illegal to index an array with a
1062 * negative constant expression."
1064 if (array
->type
->is_matrix()) {
1065 if (array
->type
->row_type()->vector_elements
<= idx
) {
1066 bound
= array
->type
->row_type()->vector_elements
;
1068 } else if (array
->type
->is_vector()) {
1069 if (array
->type
->vector_elements
<= idx
) {
1070 bound
= array
->type
->vector_elements
;
1073 if ((array
->type
->array_size() > 0)
1074 && (array
->type
->array_size() <= idx
)) {
1075 bound
= array
->type
->array_size();
1080 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
1082 error_emitted
= true;
1083 } else if (idx
< 0) {
1084 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
1086 error_emitted
= true;
1089 if (array
->type
->is_array()) {
1090 ir_variable
*const v
= array
->as_variable();
1091 if ((v
!= NULL
) && (unsigned(idx
) > v
->max_array_access
))
1092 v
->max_array_access
= idx
;
1097 result
->type
= glsl_type::error_type
;
1099 type
= result
->type
;
1103 case ast_function_call
:
1104 /* Should *NEVER* get here. ast_function_call should always be handled
1105 * by ast_function_expression::hir.
1110 case ast_identifier
: {
1111 /* ast_identifier can appear several places in a full abstract syntax
1112 * tree. This particular use must be at location specified in the grammar
1113 * as 'variable_identifier'.
1116 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1118 result
= new ir_dereference(var
);
1121 type
= result
->type
;
1123 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1124 this->primary_expression
.identifier
);
1126 error_emitted
= true;
1131 case ast_int_constant
:
1132 type
= glsl_type::int_type
;
1133 result
= new ir_constant(type
, & this->primary_expression
);
1136 case ast_uint_constant
:
1137 type
= glsl_type::uint_type
;
1138 result
= new ir_constant(type
, & this->primary_expression
);
1141 case ast_float_constant
:
1142 type
= glsl_type::float_type
;
1143 result
= new ir_constant(type
, & this->primary_expression
);
1146 case ast_bool_constant
:
1147 type
= glsl_type::bool_type
;
1148 result
= new ir_constant(type
, & this->primary_expression
);
1151 case ast_sequence
: {
1152 struct simple_node
*ptr
;
1154 /* It should not be possible to generate a sequence in the AST without
1155 * any expressions in it.
1157 assert(!is_empty_list(&this->expressions
));
1159 /* The r-value of a sequence is the last expression in the sequence. If
1160 * the other expressions in the sequence do not have side-effects (and
1161 * therefore add instructions to the instruction list), they get dropped
1164 foreach (ptr
, &this->expressions
)
1165 result
= ((ast_node
*)ptr
)->hir(instructions
, state
);
1167 type
= result
->type
;
1169 /* Any errors should have already been emitted in the loop above.
1171 error_emitted
= true;
1176 if (type
->is_error() && !error_emitted
)
1177 _mesa_glsl_error(& loc
, state
, "type mismatch");
1184 ast_expression_statement::hir(exec_list
*instructions
,
1185 struct _mesa_glsl_parse_state
*state
)
1187 /* It is possible to have expression statements that don't have an
1188 * expression. This is the solitary semicolon:
1190 * for (i = 0; i < 5; i++)
1193 * In this case the expression will be NULL. Test for NULL and don't do
1194 * anything in that case.
1196 if (expression
!= NULL
)
1197 expression
->hir(instructions
, state
);
1199 /* Statements do not have r-values.
1206 ast_compound_statement::hir(exec_list
*instructions
,
1207 struct _mesa_glsl_parse_state
*state
)
1209 struct simple_node
*ptr
;
1213 state
->symbols
->push_scope();
1215 foreach (ptr
, &statements
)
1216 ((ast_node
*)ptr
)->hir(instructions
, state
);
1219 state
->symbols
->pop_scope();
1221 /* Compound statements do not have r-values.
1227 static const glsl_type
*
1228 process_array_type(const glsl_type
*base
, ast_node
*array_size
,
1229 struct _mesa_glsl_parse_state
*state
)
1231 unsigned length
= 0;
1233 /* FINISHME: Reject delcarations of multidimensional arrays. */
1235 if (array_size
!= NULL
) {
1236 exec_list dummy_instructions
;
1237 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1238 YYLTYPE loc
= array_size
->get_location();
1240 /* FINISHME: Verify that the grammar forbids side-effects in array
1241 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1243 assert(dummy_instructions
.is_empty());
1246 if (!ir
->type
->is_integer()) {
1247 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1248 } else if (!ir
->type
->is_scalar()) {
1249 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1251 ir_constant
*const size
= ir
->constant_expression_value();
1254 _mesa_glsl_error(& loc
, state
, "array size must be a "
1255 "constant valued expression");
1256 } else if (size
->value
.i
[0] <= 0) {
1257 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1259 assert(size
->type
== ir
->type
);
1260 length
= size
->value
.u
[0];
1266 return glsl_type::get_array_instance(base
, length
);
1271 ast_type_specifier::glsl_type(const char **name
,
1272 struct _mesa_glsl_parse_state
*state
) const
1274 const struct glsl_type
*type
;
1276 if (this->type_specifier
== ast_struct
) {
1277 /* FINISHME: Handle annonymous structures. */
1280 type
= state
->symbols
->get_type(this->type_name
);
1281 *name
= this->type_name
;
1283 if (this->is_array
) {
1284 type
= process_array_type(type
, this->array_size
, state
);
1293 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1294 struct ir_variable
*var
,
1295 struct _mesa_glsl_parse_state
*state
,
1298 if (qual
->invariant
)
1301 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1302 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
1303 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1309 if (qual
->attribute
&& state
->target
== fragment_shader
) {
1310 var
->type
= glsl_type::error_type
;
1311 _mesa_glsl_error(loc
, state
,
1312 "`attribute' variables may not be declared in the "
1316 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1318 * "The varying qualifier can be used only with the data types
1319 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1322 if (qual
->varying
&& var
->type
->base_type
!= GLSL_TYPE_FLOAT
) {
1323 var
->type
= glsl_type::error_type
;
1324 _mesa_glsl_error(loc
, state
,
1325 "varying variables must be of base type float");
1328 if (qual
->in
&& qual
->out
)
1329 var
->mode
= ir_var_inout
;
1330 else if (qual
->attribute
|| qual
->in
1331 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1332 var
->mode
= ir_var_in
;
1333 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
1334 var
->mode
= ir_var_out
;
1335 else if (qual
->uniform
)
1336 var
->mode
= ir_var_uniform
;
1338 var
->mode
= ir_var_auto
;
1341 var
->interpolation
= ir_var_flat
;
1342 else if (qual
->noperspective
)
1343 var
->interpolation
= ir_var_noperspective
;
1345 var
->interpolation
= ir_var_smooth
;
1347 if (var
->type
->is_array() && (state
->language_version
>= 120)) {
1348 var
->array_lvalue
= true;
1354 ast_declarator_list::hir(exec_list
*instructions
,
1355 struct _mesa_glsl_parse_state
*state
)
1357 struct simple_node
*ptr
;
1358 const struct glsl_type
*decl_type
;
1359 const char *type_name
= NULL
;
1362 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1363 * FINISHME: include a type. These re-declare built-in variables to be
1364 * FINISHME: invariant.
1367 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1369 foreach (ptr
, &this->declarations
) {
1370 struct ast_declaration
*const decl
= (struct ast_declaration
* )ptr
;
1371 const struct glsl_type
*var_type
;
1372 struct ir_variable
*var
;
1373 YYLTYPE loc
= this->get_location();
1375 /* FINISHME: Emit a warning if a variable declaration shadows a
1376 * FINISHME: declaration at a higher scope.
1379 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1380 if (type_name
!= NULL
) {
1381 _mesa_glsl_error(& loc
, state
,
1382 "invalid type `%s' in declaration of `%s'",
1383 type_name
, decl
->identifier
);
1385 _mesa_glsl_error(& loc
, state
,
1386 "invalid type in declaration of `%s'",
1392 if (decl
->is_array
) {
1393 var_type
= process_array_type(decl_type
, decl
->array_size
, state
);
1395 var_type
= decl_type
;
1398 var
= new ir_variable(var_type
, decl
->identifier
);
1400 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1402 * "Global variables can only use the qualifiers const,
1403 * attribute, uni form, or varying. Only one may be
1406 * Local variables can only use the qualifier const."
1408 * This is relaxed in GLSL 1.30.
1410 if (state
->language_version
< 120) {
1411 if (this->type
->qualifier
.out
) {
1412 _mesa_glsl_error(& loc
, state
,
1413 "`out' qualifier in declaration of `%s' "
1414 "only valid for function parameters in GLSL 1.10.",
1417 if (this->type
->qualifier
.in
) {
1418 _mesa_glsl_error(& loc
, state
,
1419 "`in' qualifier in declaration of `%s' "
1420 "only valid for function parameters in GLSL 1.10.",
1423 /* FINISHME: Test for other invalid qualifiers. */
1426 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1429 /* Attempt to add the variable to the symbol table. If this fails, it
1430 * means the variable has already been declared at this scope. Arrays
1431 * fudge this rule a little bit.
1433 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1435 * "It is legal to declare an array without a size and then
1436 * later re-declare the same name as an array of the same
1437 * type and specify a size."
1439 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
1440 ir_variable
*const earlier
=
1441 state
->symbols
->get_variable(decl
->identifier
);
1443 if ((earlier
!= NULL
)
1444 && (earlier
->type
->array_size() == 0)
1445 && var
->type
->is_array()
1446 && (var
->type
->element_type() == earlier
->type
->element_type())) {
1447 /* FINISHME: This doesn't match the qualifiers on the two
1448 * FINISHME: declarations. It's not 100% clear whether this is
1449 * FINISHME: required or not.
1452 if (var
->type
->array_size() <= earlier
->max_array_access
) {
1453 YYLTYPE loc
= this->get_location();
1455 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
1457 earlier
->max_array_access
);
1460 earlier
->type
= var
->type
;
1464 YYLTYPE loc
= this->get_location();
1466 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
1473 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1475 * "Identifiers starting with "gl_" are reserved for use by
1476 * OpenGL, and may not be declared in a shader as either a
1477 * variable or a function."
1479 if (strncmp(decl
->identifier
, "gl_", 3) == 0) {
1480 /* FINISHME: This should only trigger if we're not redefining
1481 * FINISHME: a builtin (to add a qualifier, for example).
1483 _mesa_glsl_error(& loc
, state
,
1484 "identifier `%s' uses reserved `gl_' prefix",
1488 instructions
->push_tail(var
);
1490 if (state
->current_function
!= NULL
) {
1491 const char *mode
= NULL
;
1492 const char *extra
= "";
1494 /* There is no need to check for 'inout' here because the parser will
1495 * only allow that in function parameter lists.
1497 if (this->type
->qualifier
.attribute
) {
1499 } else if (this->type
->qualifier
.uniform
) {
1501 } else if (this->type
->qualifier
.varying
) {
1503 } else if (this->type
->qualifier
.in
) {
1505 extra
= " or in function parameter list";
1506 } else if (this->type
->qualifier
.out
) {
1508 extra
= " or in function parameter list";
1512 _mesa_glsl_error(& loc
, state
,
1513 "%s variable `%s' must be declared at "
1515 mode
, var
->name
, extra
);
1517 } else if (var
->mode
== ir_var_in
) {
1518 if (state
->target
== vertex_shader
) {
1519 bool error_emitted
= false;
1521 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1523 * "Vertex shader inputs can only be float, floating-point
1524 * vectors, matrices, signed and unsigned integers and integer
1525 * vectors. Vertex shader inputs can also form arrays of these
1526 * types, but not structures."
1528 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1530 * "Vertex shader inputs can only be float, floating-point
1531 * vectors, matrices, signed and unsigned integers and integer
1532 * vectors. They cannot be arrays or structures."
1534 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1536 * "The attribute qualifier can be used only with float,
1537 * floating-point vectors, and matrices. Attribute variables
1538 * cannot be declared as arrays or structures."
1540 const glsl_type
*check_type
= var
->type
->is_array()
1541 ? var
->type
->fields
.array
: var
->type
;
1543 switch (check_type
->base_type
) {
1544 case GLSL_TYPE_FLOAT
:
1546 case GLSL_TYPE_UINT
:
1548 if (state
->language_version
> 120)
1552 _mesa_glsl_error(& loc
, state
,
1553 "vertex shader input / attribute cannot have "
1555 var
->type
->is_array() ? "array of " : "",
1557 error_emitted
= true;
1560 if (!error_emitted
&& (state
->language_version
<= 130)
1561 && var
->type
->is_array()) {
1562 _mesa_glsl_error(& loc
, state
,
1563 "vertex shader input / attribute cannot have "
1565 error_emitted
= true;
1570 if (decl
->initializer
!= NULL
) {
1571 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
1573 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1575 * "All uniform variables are read-only and are initialized either
1576 * directly by an application via API commands, or indirectly by
1579 if ((state
->language_version
<= 110)
1580 && (var
->mode
== ir_var_uniform
)) {
1581 _mesa_glsl_error(& initializer_loc
, state
,
1582 "cannot initialize uniforms in GLSL 1.10");
1585 if (var
->type
->is_sampler()) {
1586 _mesa_glsl_error(& initializer_loc
, state
,
1587 "cannot initialize samplers");
1590 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1591 _mesa_glsl_error(& initializer_loc
, state
,
1592 "cannot initialize %s shader input / %s",
1593 (state
->target
== vertex_shader
)
1594 ? "vertex" : "fragment",
1595 (state
->target
== vertex_shader
)
1596 ? "attribute" : "varying");
1599 ir_dereference
*const lhs
= new ir_dereference(var
);
1600 ir_rvalue
*rhs
= decl
->initializer
->hir(instructions
, state
);
1602 /* Calculate the constant value if this is a const
1605 if (this->type
->qualifier
.constant
) {
1606 ir_constant
*constant_value
= rhs
->constant_expression_value();
1607 if (!constant_value
) {
1608 _mesa_glsl_error(& initializer_loc
, state
,
1609 "initializer of const variable `%s' must be a "
1610 "constant expression",
1613 rhs
= constant_value
;
1614 var
->constant_value
= constant_value
;
1618 if (rhs
&& !rhs
->type
->is_error()) {
1619 bool temp
= var
->read_only
;
1620 if (this->type
->qualifier
.constant
)
1621 var
->read_only
= false;
1622 (void) do_assignment(instructions
, state
, lhs
, rhs
,
1623 this->get_location());
1624 var
->read_only
= temp
;
1628 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1630 * "It is an error to write to a const variable outside of
1631 * its declaration, so they must be initialized when
1634 if (this->type
->qualifier
.constant
&& decl
->initializer
== NULL
) {
1635 _mesa_glsl_error(& loc
, state
,
1636 "const declaration of `%s' must be initialized");
1639 /* Add the vairable to the symbol table after processing the initializer.
1640 * This differs from most C-like languages, but it follows the GLSL
1641 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1644 * "Within a declaration, the scope of a name starts immediately
1645 * after the initializer if present or immediately after the name
1646 * being declared if not."
1648 const bool added_variable
=
1649 state
->symbols
->add_variable(decl
->identifier
, var
);
1650 assert(added_variable
);
1653 /* Variable declarations do not have r-values.
1660 ast_parameter_declarator::hir(exec_list
*instructions
,
1661 struct _mesa_glsl_parse_state
*state
)
1663 const struct glsl_type
*type
;
1664 const char *name
= NULL
;
1665 YYLTYPE loc
= this->get_location();
1667 type
= this->type
->specifier
->glsl_type(& name
, state
);
1671 _mesa_glsl_error(& loc
, state
,
1672 "invalid type `%s' in declaration of `%s'",
1673 name
, this->identifier
);
1675 _mesa_glsl_error(& loc
, state
,
1676 "invalid type in declaration of `%s'",
1680 type
= glsl_type::error_type
;
1683 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1685 * "Functions that accept no input arguments need not use void in the
1686 * argument list because prototypes (or definitions) are required and
1687 * therefore there is no ambiguity when an empty argument list "( )" is
1688 * declared. The idiom "(void)" as a parameter list is provided for
1691 * Placing this check here prevents a void parameter being set up
1692 * for a function, which avoids tripping up checks for main taking
1693 * parameters and lookups of an unnamed symbol.
1695 if (type
->is_void()) {
1696 if (this->identifier
!= NULL
)
1697 _mesa_glsl_error(& loc
, state
,
1698 "named parameter cannot have type `void'");
1704 if (formal_parameter
&& (this->identifier
== NULL
)) {
1705 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
1710 ir_variable
*var
= new ir_variable(type
, this->identifier
);
1712 /* FINISHME: Handle array declarations. Note that this requires
1713 * FINISHME: complete handling of constant expressions.
1716 /* Apply any specified qualifiers to the parameter declaration. Note that
1717 * for function parameters the default mode is 'in'.
1719 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
1720 if (var
->mode
== ir_var_auto
)
1721 var
->mode
= ir_var_in
;
1723 instructions
->push_tail(var
);
1725 /* Parameter declarations do not have r-values.
1732 ast_parameter_declarator::parameters_to_hir(struct simple_node
*ast_parameters
,
1734 exec_list
*ir_parameters
,
1735 _mesa_glsl_parse_state
*state
)
1737 struct simple_node
*ptr
;
1738 ast_parameter_declarator
*void_param
= NULL
;
1741 foreach (ptr
, ast_parameters
) {
1742 ast_parameter_declarator
*param
= (ast_parameter_declarator
*)ptr
;
1743 param
->formal_parameter
= formal
;
1744 param
->hir(ir_parameters
, state
);
1752 if ((void_param
!= NULL
) && (count
> 1)) {
1753 YYLTYPE loc
= void_param
->get_location();
1755 _mesa_glsl_error(& loc
, state
,
1756 "`void' parameter must be only parameter");
1762 parameter_lists_match(exec_list
*list_a
, exec_list
*list_b
)
1764 exec_list_iterator iter_a
= list_a
->iterator();
1765 exec_list_iterator iter_b
= list_b
->iterator();
1767 while (iter_a
.has_next()) {
1768 ir_variable
*a
= (ir_variable
*)iter_a
.get();
1769 ir_variable
*b
= (ir_variable
*)iter_b
.get();
1771 /* If all of the parameters from the other parameter list have been
1772 * exhausted, the lists have different length and, by definition,
1775 if (!iter_b
.has_next())
1778 /* If the types of the parameters do not match, the parameters lists
1781 if (a
->type
!= b
->type
)
1793 ast_function::hir(exec_list
*instructions
,
1794 struct _mesa_glsl_parse_state
*state
)
1796 ir_function
*f
= NULL
;
1797 ir_function_signature
*sig
= NULL
;
1798 exec_list hir_parameters
;
1801 /* The prototype part of a function does not generate anything in the IR
1802 * instruction stream.
1804 (void) instructions
;
1806 /* Convert the list of function parameters to HIR now so that they can be
1807 * used below to compare this function's signature with previously seen
1808 * signatures for functions with the same name.
1810 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
1812 & hir_parameters
, state
);
1814 const char *return_type_name
;
1815 const glsl_type
*return_type
=
1816 this->return_type
->specifier
->glsl_type(& return_type_name
, state
);
1818 assert(return_type
!= NULL
);
1820 /* Verify that this function's signature either doesn't match a previously
1821 * seen signature for a function with the same name, or, if a match is found,
1822 * that the previously seen signature does not have an associated definition.
1824 const char *const name
= identifier
;
1825 f
= state
->symbols
->get_function(name
);
1827 foreach_iter(exec_list_iterator
, iter
, *f
) {
1828 sig
= (struct ir_function_signature
*) iter
.get();
1830 /* Compare the parameter list of the function being defined to the
1831 * existing function. If the parameter lists match, then the return
1832 * type must also match and the existing function must not have a
1835 if (parameter_lists_match(& hir_parameters
, & sig
->parameters
)) {
1836 /* FINISHME: Compare return types. */
1838 if (is_definition
&& (sig
->definition
!= NULL
)) {
1839 YYLTYPE loc
= this->get_location();
1841 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
1850 } else if (state
->symbols
->name_declared_this_scope(name
)) {
1851 /* This function name shadows a non-function use of the same name.
1853 YYLTYPE loc
= this->get_location();
1855 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
1856 "non-function", name
);
1859 f
= new ir_function(name
);
1860 state
->symbols
->add_function(f
->name
, f
);
1863 /* Verify the return type of main() */
1864 if (strcmp(name
, "main") == 0) {
1865 if (! return_type
->is_void()) {
1866 YYLTYPE loc
= this->get_location();
1868 _mesa_glsl_error(& loc
, state
, "main() must return void");
1871 if (!hir_parameters
.is_empty()) {
1872 YYLTYPE loc
= this->get_location();
1874 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
1878 /* Finish storing the information about this new function in its signature.
1881 sig
= new ir_function_signature(return_type
);
1882 f
->add_signature(sig
);
1883 } else if (is_definition
) {
1884 /* Destroy all of the previous parameter information. The previous
1885 * parameter information comes from the function prototype, and it can
1886 * either include invalid parameter names or may not have names at all.
1888 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
1889 assert(((ir_instruction
*) iter
.get())->as_variable() != NULL
);
1896 hir_parameters
.move_nodes_to(& sig
->parameters
);
1899 /* Function declarations (prototypes) do not have r-values.
1906 ast_function_definition::hir(exec_list
*instructions
,
1907 struct _mesa_glsl_parse_state
*state
)
1909 prototype
->is_definition
= true;
1910 prototype
->hir(instructions
, state
);
1912 ir_function_signature
*signature
= prototype
->signature
;
1914 assert(state
->current_function
== NULL
);
1915 state
->current_function
= signature
;
1917 ir_label
*label
= new ir_label(signature
->function_name());
1918 if (signature
->definition
== NULL
) {
1919 signature
->definition
= label
;
1921 instructions
->push_tail(label
);
1923 /* Duplicate parameters declared in the prototype as concrete variables.
1924 * Add these to the symbol table.
1926 state
->symbols
->push_scope();
1927 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
1928 ir_variable
*const proto
= ((ir_instruction
*) iter
.get())->as_variable();
1930 assert(proto
!= NULL
);
1932 ir_variable
*const var
= proto
->clone();
1934 instructions
->push_tail(var
);
1936 /* The only way a parameter would "exist" is if two parameters have
1939 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
1940 YYLTYPE loc
= this->get_location();
1942 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
1944 state
->symbols
->add_variable(var
->name
, var
);
1948 /* Convert the body of the function to HIR, and append the resulting
1949 * instructions to the list that currently consists of the function label
1950 * and the function parameters.
1952 this->body
->hir(instructions
, state
);
1954 state
->symbols
->pop_scope();
1956 assert(state
->current_function
== signature
);
1957 state
->current_function
= NULL
;
1959 /* Function definitions do not have r-values.
1966 ast_jump_statement::hir(exec_list
*instructions
,
1967 struct _mesa_glsl_parse_state
*state
)
1973 assert(state
->current_function
);
1975 if (opt_return_value
) {
1976 if (state
->current_function
->return_type
->base_type
==
1978 YYLTYPE loc
= this->get_location();
1980 _mesa_glsl_error(& loc
, state
,
1981 "`return` with a value, in function `%s' "
1983 state
->current_function
->definition
->label
);
1986 ir_expression
*const ret
= (ir_expression
*)
1987 opt_return_value
->hir(instructions
, state
);
1988 assert(ret
!= NULL
);
1990 /* FINISHME: Make sure the type of the return value matches the return
1991 * FINISHME: type of the enclosing function.
1994 inst
= new ir_return(ret
);
1996 if (state
->current_function
->return_type
->base_type
!=
1998 YYLTYPE loc
= this->get_location();
2000 _mesa_glsl_error(& loc
, state
,
2001 "`return' with no value, in function %s returning "
2003 state
->current_function
->definition
->label
);
2005 inst
= new ir_return
;
2008 instructions
->push_tail(inst
);
2013 /* FINISHME: discard support */
2014 if (state
->target
!= fragment_shader
) {
2015 YYLTYPE loc
= this->get_location();
2017 _mesa_glsl_error(& loc
, state
,
2018 "`discard' may only appear in a fragment shader");
2027 /* Jump instructions do not have r-values.
2034 ast_selection_statement::hir(exec_list
*instructions
,
2035 struct _mesa_glsl_parse_state
*state
)
2037 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
2039 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2041 * "Any expression whose type evaluates to a Boolean can be used as the
2042 * conditional expression bool-expression. Vector types are not accepted
2043 * as the expression to if."
2045 * The checks are separated so that higher quality diagnostics can be
2046 * generated for cases where both rules are violated.
2048 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
2049 YYLTYPE loc
= this->condition
->get_location();
2051 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
2055 ir_if
*const stmt
= new ir_if(condition
);
2057 if (then_statement
!= NULL
) {
2058 ast_node
*node
= (ast_node
*) then_statement
;
2060 node
->hir(& stmt
->then_instructions
, state
);
2061 node
= (ast_node
*) node
->next
;
2062 } while (node
!= then_statement
);
2065 if (else_statement
!= NULL
) {
2066 ast_node
*node
= (ast_node
*) else_statement
;
2068 node
->hir(& stmt
->else_instructions
, state
);
2069 node
= (ast_node
*) node
->next
;
2070 } while (node
!= else_statement
);
2073 instructions
->push_tail(stmt
);
2075 /* if-statements do not have r-values.
2082 ast_iteration_statement::hir(exec_list
*instructions
,
2083 struct _mesa_glsl_parse_state
*state
)
2085 /* For loops start a new scope, but while and do-while loops do not.
2087 if (mode
== ast_for
)
2088 state
->symbols
->push_scope();
2090 if (init_statement
!= NULL
)
2091 init_statement
->hir(instructions
, state
);
2093 ir_loop
*const stmt
= new ir_loop();
2094 instructions
->push_tail(stmt
);
2096 if (condition
!= NULL
) {
2097 ir_rvalue
*const cond
=
2098 condition
->hir(& stmt
->body_instructions
, state
);
2101 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
2102 YYLTYPE loc
= condition
->get_location();
2104 _mesa_glsl_error(& loc
, state
,
2105 "loop condition must be scalar boolean");
2107 /* As the first code in the loop body, generate a block that looks
2108 * like 'if (!condition) break;' as the loop termination condition.
2110 ir_rvalue
*const not_cond
=
2111 new ir_expression(ir_unop_logic_not
, glsl_type::bool_type
, cond
,
2114 ir_if
*const if_stmt
= new ir_if(not_cond
);
2116 ir_jump
*const break_stmt
=
2117 new ir_loop_jump(stmt
, ir_loop_jump::jump_break
);
2119 if_stmt
->then_instructions
.push_tail(break_stmt
);
2120 stmt
->body_instructions
.push_tail(if_stmt
);
2125 ast_node
*node
= (ast_node
*) body
;
2127 node
->hir(& stmt
->body_instructions
, state
);
2128 node
= (ast_node
*) node
->next
;
2129 } while (node
!= body
);
2132 if (rest_expression
!= NULL
)
2133 rest_expression
->hir(& stmt
->body_instructions
, state
);
2135 if (mode
== ast_for
)
2136 state
->symbols
->pop_scope();
2138 /* Loops do not have r-values.