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
, YYLTYPE
*loc
)
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 _mesa_glsl_error(loc
, state
,
148 "Operands to arithmetic operators must be numeric");
149 return glsl_type::error_type
;
153 /* "If one operand is floating-point based and the other is
154 * not, then the conversions from Section 4.1.10 "Implicit
155 * Conversions" are applied to the non-floating-point-based operand."
157 if (!apply_implicit_conversion(type_a
, value_b
, state
)
158 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
159 _mesa_glsl_error(loc
, state
,
160 "Could not implicitly convert operands to "
161 "arithmetic operator");
162 return glsl_type::error_type
;
165 /* "If the operands are integer types, they must both be signed or
168 * From this rule and the preceeding conversion it can be inferred that
169 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
170 * The is_numeric check above already filtered out the case where either
171 * type is not one of these, so now the base types need only be tested for
174 if (type_a
->base_type
!= type_b
->base_type
) {
175 _mesa_glsl_error(loc
, state
,
176 "base type mismatch for arithmetic operator");
177 return glsl_type::error_type
;
180 /* "All arithmetic binary operators result in the same fundamental type
181 * (signed integer, unsigned integer, or floating-point) as the
182 * operands they operate on, after operand type conversion. After
183 * conversion, the following cases are valid
185 * * The two operands are scalars. In this case the operation is
186 * applied, resulting in a scalar."
188 if (type_a
->is_scalar() && type_b
->is_scalar())
191 /* "* One operand is a scalar, and the other is a vector or matrix.
192 * In this case, the scalar operation is applied independently to each
193 * component of the vector or matrix, resulting in the same size
196 if (type_a
->is_scalar()) {
197 if (!type_b
->is_scalar())
199 } else if (type_b
->is_scalar()) {
203 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
204 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
207 assert(!type_a
->is_scalar());
208 assert(!type_b
->is_scalar());
210 /* "* The two operands are vectors of the same size. In this case, the
211 * operation is done component-wise resulting in the same size
214 if (type_a
->is_vector() && type_b
->is_vector()) {
215 if (type_a
== type_b
) {
218 _mesa_glsl_error(loc
, state
,
219 "vector size mismatch for arithmetic operator");
220 return glsl_type::error_type
;
224 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
225 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
226 * <vector, vector> have been handled. At least one of the operands must
227 * be matrix. Further, since there are no integer matrix types, the base
228 * type of both operands must be float.
230 assert(type_a
->is_matrix() || type_b
->is_matrix());
231 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
232 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
234 /* "* The operator is add (+), subtract (-), or divide (/), and the
235 * operands are matrices with the same number of rows and the same
236 * number of columns. In this case, the operation is done component-
237 * wise resulting in the same size matrix."
238 * * The operator is multiply (*), where both operands are matrices or
239 * one operand is a vector and the other a matrix. A right vector
240 * operand is treated as a column vector and a left vector operand as a
241 * row vector. In all these cases, it is required that the number of
242 * columns of the left operand is equal to the number of rows of the
243 * right operand. Then, the multiply (*) operation does a linear
244 * algebraic multiply, yielding an object that has the same number of
245 * rows as the left operand and the same number of columns as the right
246 * operand. Section 5.10 "Vector and Matrix Operations" explains in
247 * more detail how vectors and matrices are operated on."
250 if (type_a
== type_b
)
253 if (type_a
->is_matrix() && type_b
->is_matrix()) {
254 /* Matrix multiply. The columns of A must match the rows of B. Given
255 * the other previously tested constraints, this means the vector type
256 * of a row from A must be the same as the vector type of a column from
259 if (type_a
->row_type() == type_b
->column_type()) {
260 /* The resulting matrix has the number of columns of matrix B and
261 * the number of rows of matrix A. We get the row count of A by
262 * looking at the size of a vector that makes up a column. The
263 * transpose (size of a row) is done for B.
265 const glsl_type
*const type
=
266 glsl_type::get_instance(type_a
->base_type
,
267 type_a
->column_type()->vector_elements
,
268 type_b
->row_type()->vector_elements
);
269 assert(type
!= glsl_type::error_type
);
273 } else if (type_a
->is_matrix()) {
274 /* A is a matrix and B is a column vector. Columns of A must match
275 * rows of B. Given the other previously tested constraints, this
276 * means the vector type of a row from A must be the same as the
277 * vector the type of B.
279 if (type_a
->row_type() == type_b
)
282 assert(type_b
->is_matrix());
284 /* A is a row vector and B is a matrix. Columns of A must match rows
285 * of B. Given the other previously tested constraints, this means
286 * the type of A must be the same as the vector type of a column from
289 if (type_a
== type_b
->column_type())
293 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
294 return glsl_type::error_type
;
298 /* "All other cases are illegal."
300 _mesa_glsl_error(loc
, state
, "type mismatch");
301 return glsl_type::error_type
;
305 static const struct glsl_type
*
306 unary_arithmetic_result_type(const struct glsl_type
*type
,
307 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
309 /* From GLSL 1.50 spec, page 57:
311 * "The arithmetic unary operators negate (-), post- and pre-increment
312 * and decrement (-- and ++) operate on integer or floating-point
313 * values (including vectors and matrices). All unary operators work
314 * component-wise on their operands. These result with the same type
317 if (!type
->is_numeric()) {
318 _mesa_glsl_error(loc
, state
,
319 "Operands to arithmetic operators must be numeric");
320 return glsl_type::error_type
;
327 static const struct glsl_type
*
328 modulus_result_type(const struct glsl_type
*type_a
,
329 const struct glsl_type
*type_b
,
330 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
332 /* From GLSL 1.50 spec, page 56:
333 * "The operator modulus (%) operates on signed or unsigned integers or
334 * integer vectors. The operand types must both be signed or both be
337 if (!type_a
->is_integer() || !type_b
->is_integer()
338 || (type_a
->base_type
!= type_b
->base_type
)) {
339 _mesa_glsl_error(loc
, state
, "type mismatch");
340 return glsl_type::error_type
;
343 /* "The operands cannot be vectors of differing size. If one operand is
344 * a scalar and the other vector, then the scalar is applied component-
345 * wise to the vector, resulting in the same type as the vector. If both
346 * are vectors of the same size, the result is computed component-wise."
348 if (type_a
->is_vector()) {
349 if (!type_b
->is_vector()
350 || (type_a
->vector_elements
== type_b
->vector_elements
))
355 /* "The operator modulus (%) is not defined for any other data types
356 * (non-integer types)."
358 _mesa_glsl_error(loc
, state
, "type mismatch");
359 return glsl_type::error_type
;
363 static const struct glsl_type
*
364 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
365 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
367 const glsl_type
*const type_a
= value_a
->type
;
368 const glsl_type
*const type_b
= value_b
->type
;
370 /* From GLSL 1.50 spec, page 56:
371 * "The relational operators greater than (>), less than (<), greater
372 * than or equal (>=), and less than or equal (<=) operate only on
373 * scalar integer and scalar floating-point expressions."
375 if (!type_a
->is_numeric()
376 || !type_b
->is_numeric()
377 || !type_a
->is_scalar()
378 || !type_b
->is_scalar()) {
379 _mesa_glsl_error(loc
, state
,
380 "Operands to relational operators must be scalar and "
382 return glsl_type::error_type
;
385 /* "Either the operands' types must match, or the conversions from
386 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
387 * operand, after which the types must match."
389 if (!apply_implicit_conversion(type_a
, value_b
, state
)
390 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
391 _mesa_glsl_error(loc
, state
,
392 "Could not implicitly convert operands to "
393 "relational operator");
394 return glsl_type::error_type
;
397 if (type_a
->base_type
!= type_b
->base_type
) {
398 _mesa_glsl_error(loc
, state
, "base type mismatch");
399 return glsl_type::error_type
;
402 /* "The result is scalar Boolean."
404 return glsl_type::bool_type
;
409 * Validates that a value can be assigned to a location with a specified type
411 * Validates that \c rhs can be assigned to some location. If the types are
412 * not an exact match but an automatic conversion is possible, \c rhs will be
416 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
417 * Otherwise the actual RHS to be assigned will be returned. This may be
418 * \c rhs, or it may be \c rhs after some type conversion.
421 * In addition to being used for assignments, this function is used to
422 * type-check return values.
425 validate_assignment(const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
427 const glsl_type
*const rhs_type
= rhs
->type
;
429 /* If there is already some error in the RHS, just return it. Anything
430 * else will lead to an avalanche of error message back to the user.
432 if (rhs_type
->is_error())
435 /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
437 /* If the types are identical, the assignment can trivially proceed.
439 if (rhs_type
== lhs_type
)
442 /* FINISHME: Check for and apply automatic conversions. */
447 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
448 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
451 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
453 if (!error_emitted
) {
454 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
455 if (!lhs
->is_lvalue()) {
456 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
457 error_emitted
= true;
461 ir_rvalue
*new_rhs
= validate_assignment(lhs
->type
, rhs
);
462 if (new_rhs
== NULL
) {
463 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
468 ir_instruction
*tmp
= new ir_assignment(lhs
, rhs
, NULL
);
469 instructions
->push_tail(tmp
);
476 * Generate a new temporary and add its declaration to the instruction stream
479 generate_temporary(const glsl_type
*type
, exec_list
*instructions
,
480 struct _mesa_glsl_parse_state
*state
)
482 char *name
= (char *) malloc(sizeof(char) * 13);
484 snprintf(name
, 13, "tmp_%08X", state
->temp_index
);
487 ir_variable
*const var
= new ir_variable(type
, name
);
488 instructions
->push_tail(var
);
495 get_lvalue_copy(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
496 ir_rvalue
*lvalue
, YYLTYPE loc
)
499 ir_rvalue
*var_deref
;
501 /* FINISHME: Give unique names to the temporaries. */
502 var
= new ir_variable(lvalue
->type
, "_internal_tmp");
503 var
->mode
= ir_var_auto
;
505 var_deref
= new ir_dereference(var
);
506 do_assignment(instructions
, state
, var_deref
, lvalue
, loc
);
508 /* Once we've created this temporary, mark it read only so it's no
509 * longer considered an lvalue.
511 var
->read_only
= true;
518 ast_node::hir(exec_list
*instructions
,
519 struct _mesa_glsl_parse_state
*state
)
529 ast_expression::hir(exec_list
*instructions
,
530 struct _mesa_glsl_parse_state
*state
)
532 static const int operations
[AST_NUM_OPERATORS
] = {
533 -1, /* ast_assign doesn't convert to ir_expression. */
534 -1, /* ast_plus doesn't convert to ir_expression. */
558 /* Note: The following block of expression types actually convert
559 * to multiple IR instructions.
561 ir_binop_mul
, /* ast_mul_assign */
562 ir_binop_div
, /* ast_div_assign */
563 ir_binop_mod
, /* ast_mod_assign */
564 ir_binop_add
, /* ast_add_assign */
565 ir_binop_sub
, /* ast_sub_assign */
566 ir_binop_lshift
, /* ast_ls_assign */
567 ir_binop_rshift
, /* ast_rs_assign */
568 ir_binop_bit_and
, /* ast_and_assign */
569 ir_binop_bit_xor
, /* ast_xor_assign */
570 ir_binop_bit_or
, /* ast_or_assign */
572 -1, /* ast_conditional doesn't convert to ir_expression. */
573 ir_binop_add
, /* ast_pre_inc. */
574 ir_binop_sub
, /* ast_pre_dec. */
575 ir_binop_add
, /* ast_post_inc. */
576 ir_binop_sub
, /* ast_post_dec. */
577 -1, /* ast_field_selection doesn't conv to ir_expression. */
578 -1, /* ast_array_index doesn't convert to ir_expression. */
579 -1, /* ast_function_call doesn't conv to ir_expression. */
580 -1, /* ast_identifier doesn't convert to ir_expression. */
581 -1, /* ast_int_constant doesn't convert to ir_expression. */
582 -1, /* ast_uint_constant doesn't conv to ir_expression. */
583 -1, /* ast_float_constant doesn't conv to ir_expression. */
584 -1, /* ast_bool_constant doesn't conv to ir_expression. */
585 -1, /* ast_sequence doesn't convert to ir_expression. */
587 ir_rvalue
*result
= NULL
;
589 struct simple_node op_list
;
590 const struct glsl_type
*type
= glsl_type::error_type
;
591 bool error_emitted
= false;
594 loc
= this->get_location();
595 make_empty_list(& op_list
);
597 switch (this->oper
) {
599 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
600 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
602 result
= do_assignment(instructions
, state
, op
[0], op
[1],
603 this->subexpressions
[0]->get_location());
604 error_emitted
= result
->type
->is_error();
610 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
612 error_emitted
= op
[0]->type
->is_error();
613 if (type
->is_error())
620 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
622 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
624 error_emitted
= type
->is_error();
626 result
= new ir_expression(operations
[this->oper
], type
,
634 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
635 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
637 type
= arithmetic_result_type(op
[0], op
[1],
638 (this->oper
== ast_mul
),
640 error_emitted
= type
->is_error();
642 result
= new ir_expression(operations
[this->oper
], type
,
647 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
648 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
650 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
652 assert(operations
[this->oper
] == ir_binop_mod
);
654 result
= new ir_expression(operations
[this->oper
], type
,
656 error_emitted
= type
->is_error();
661 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-shift operators");
662 error_emitted
= true;
669 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
670 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
672 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
674 /* The relational operators must either generate an error or result
675 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
677 assert(type
->is_error()
678 || ((type
->base_type
== GLSL_TYPE_BOOL
)
679 && type
->is_scalar()));
681 result
= new ir_expression(operations
[this->oper
], type
,
683 error_emitted
= type
->is_error();
688 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
689 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
691 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
693 * "The equality operators equal (==), and not equal (!=)
694 * operate on all types. They result in a scalar Boolean. If
695 * the operand types do not match, then there must be a
696 * conversion from Section 4.1.10 "Implicit Conversions"
697 * applied to one operand that can make them match, in which
698 * case this conversion is done."
700 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
701 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
702 || (op
[0]->type
!= op
[1]->type
)) {
703 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
704 "type", (this->oper
== ast_equal
) ? "==" : "!=");
705 error_emitted
= true;
706 } else if ((state
->language_version
<= 110)
707 && (op
[0]->type
->is_array() || op
[1]->type
->is_array())) {
708 _mesa_glsl_error(& loc
, state
, "array comparisons forbidden in "
710 error_emitted
= true;
713 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
715 type
= glsl_type::bool_type
;
717 assert(result
->type
== glsl_type::bool_type
);
724 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-wise operators");
725 error_emitted
= true;
731 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
732 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
734 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
735 YYLTYPE loc
= this->subexpressions
[0]->get_location();
737 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
738 operator_string(this->oper
));
739 error_emitted
= true;
742 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
743 YYLTYPE loc
= this->subexpressions
[1]->get_location();
745 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
746 operator_string(this->oper
));
747 error_emitted
= true;
750 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
752 type
= glsl_type::bool_type
;
756 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
758 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
759 YYLTYPE loc
= this->subexpressions
[0]->get_location();
761 _mesa_glsl_error(& loc
, state
,
762 "operand of `!' must be scalar boolean");
763 error_emitted
= true;
766 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
768 type
= glsl_type::bool_type
;
774 case ast_sub_assign
: {
775 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
776 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
778 type
= arithmetic_result_type(op
[0], op
[1],
779 (this->oper
== ast_mul_assign
),
782 ir_rvalue
*temp_rhs
= new ir_expression(operations
[this->oper
], type
,
785 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
786 this->subexpressions
[0]->get_location());
788 error_emitted
= (op
[0]->type
->is_error());
790 /* GLSL 1.10 does not allow array assignment. However, we don't have to
791 * explicitly test for this because none of the binary expression
792 * operators allow array operands either.
798 case ast_mod_assign
: {
799 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
800 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
802 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
804 assert(operations
[this->oper
] == ir_binop_mod
);
806 struct ir_rvalue
*temp_rhs
;
807 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
810 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
811 this->subexpressions
[0]->get_location());
813 error_emitted
= type
->is_error();
819 _mesa_glsl_error(& loc
, state
,
820 "FINISHME: implement bit-shift assignment operators");
821 error_emitted
= true;
827 _mesa_glsl_error(& loc
, state
,
828 "FINISHME: implement logic assignment operators");
829 error_emitted
= true;
832 case ast_conditional
: {
833 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
835 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
837 * "The ternary selection operator (?:). It operates on three
838 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
839 * first expression, which must result in a scalar Boolean."
841 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
842 YYLTYPE loc
= this->subexpressions
[0]->get_location();
844 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
845 error_emitted
= true;
848 /* The :? operator is implemented by generating an anonymous temporary
849 * followed by an if-statement. The last instruction in each branch of
850 * the if-statement assigns a value to the anonymous temporary. This
851 * temporary is the r-value of the expression.
853 ir_variable
*const tmp
= generate_temporary(glsl_type::error_type
,
854 instructions
, state
);
856 ir_if
*const stmt
= new ir_if(op
[0]);
857 instructions
->push_tail(stmt
);
859 op
[1] = this->subexpressions
[1]->hir(& stmt
->then_instructions
, state
);
860 ir_dereference
*const then_deref
= new ir_dereference(tmp
);
861 ir_assignment
*const then_assign
=
862 new ir_assignment(then_deref
, op
[1], NULL
);
863 stmt
->then_instructions
.push_tail(then_assign
);
865 op
[2] = this->subexpressions
[2]->hir(& stmt
->else_instructions
, state
);
866 ir_dereference
*const else_deref
= new ir_dereference(tmp
);
867 ir_assignment
*const else_assign
=
868 new ir_assignment(else_deref
, op
[2], NULL
);
869 stmt
->else_instructions
.push_tail(else_assign
);
871 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
873 * "The second and third expressions can be any type, as
874 * long their types match, or there is a conversion in
875 * Section 4.1.10 "Implicit Conversions" that can be applied
876 * to one of the expressions to make their types match. This
877 * resulting matching type is the type of the entire
880 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
881 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
882 || (op
[1]->type
!= op
[2]->type
)) {
883 YYLTYPE loc
= this->subexpressions
[1]->get_location();
885 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
886 "operator must have matching types.");
887 error_emitted
= true;
889 tmp
->type
= op
[1]->type
;
892 result
= new ir_dereference(tmp
);
899 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
900 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
901 op
[1] = new ir_constant(1.0f
);
903 op
[1] = new ir_constant(1);
905 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
907 struct ir_rvalue
*temp_rhs
;
908 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
911 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
912 this->subexpressions
[0]->get_location());
914 error_emitted
= op
[0]->type
->is_error();
920 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
921 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
922 op
[1] = new ir_constant(1.0f
);
924 op
[1] = new ir_constant(1);
926 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
928 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
930 struct ir_rvalue
*temp_rhs
;
931 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
934 /* Get a temporary of a copy of the lvalue before it's modified.
935 * This may get thrown away later.
937 result
= get_lvalue_copy(instructions
, state
, op
[0],
938 this->subexpressions
[0]->get_location());
940 (void)do_assignment(instructions
, state
, op
[0], temp_rhs
,
941 this->subexpressions
[0]->get_location());
944 error_emitted
= op
[0]->type
->is_error();
948 case ast_field_selection
:
949 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
953 case ast_array_index
: {
954 YYLTYPE index_loc
= subexpressions
[1]->get_location();
956 op
[0] = subexpressions
[0]->hir(instructions
, state
);
957 op
[1] = subexpressions
[1]->hir(instructions
, state
);
959 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
961 ir_dereference
*const lhs
= op
[0]->as_dereference();
962 ir_instruction
*array
;
964 && (lhs
->mode
== ir_dereference::ir_reference_variable
)) {
965 result
= new ir_dereference(lhs
->var
, op
[1]);
970 result
= new ir_dereference(op
[0], op
[1]);
974 /* Do not use op[0] after this point. Use array.
982 /* FINISHME: Handle vectors and matrices accessed with []. */
983 if (!array
->type
->is_array()) {
984 _mesa_glsl_error(& index_loc
, state
,
985 "cannot dereference non-array");
986 error_emitted
= true;
989 if (!op
[1]->type
->is_integer()) {
990 _mesa_glsl_error(& index_loc
, state
,
991 "array index must be integer type");
992 error_emitted
= true;
993 } else if (!op
[1]->type
->is_scalar()) {
994 _mesa_glsl_error(& index_loc
, state
,
995 "array index must be scalar");
996 error_emitted
= true;
999 /* If the array index is a constant expression and the array has a
1000 * declared size, ensure that the access is in-bounds. If the array
1001 * index is not a constant expression, ensure that the array has a
1004 ir_constant
*const const_index
= op
[1]->constant_expression_value();
1005 if (const_index
!= NULL
) {
1006 const int idx
= const_index
->value
.i
[0];
1008 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1010 * "It is illegal to declare an array with a size, and then
1011 * later (in the same shader) index the same array with an
1012 * integral constant expression greater than or equal to the
1013 * declared size. It is also illegal to index an array with a
1014 * negative constant expression."
1016 if ((array
->type
->array_size() > 0)
1017 && (array
->type
->array_size() <= idx
)) {
1018 _mesa_glsl_error(& loc
, state
,
1019 "array index must be < %u",
1020 array
->type
->array_size());
1021 error_emitted
= true;
1025 _mesa_glsl_error(& loc
, state
,
1026 "array index must be >= 0");
1027 error_emitted
= true;
1030 ir_variable
*const v
= array
->as_variable();
1031 if ((v
!= NULL
) && (unsigned(idx
) > v
->max_array_access
))
1032 v
->max_array_access
= idx
;
1036 result
->type
= glsl_type::error_type
;
1038 type
= result
->type
;
1042 case ast_function_call
:
1043 /* Should *NEVER* get here. ast_function_call should always be handled
1044 * by ast_function_expression::hir.
1049 case ast_identifier
: {
1050 /* ast_identifier can appear several places in a full abstract syntax
1051 * tree. This particular use must be at location specified in the grammar
1052 * as 'variable_identifier'.
1055 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1057 result
= new ir_dereference(var
);
1060 type
= result
->type
;
1062 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1063 this->primary_expression
.identifier
);
1065 error_emitted
= true;
1070 case ast_int_constant
:
1071 type
= glsl_type::int_type
;
1072 result
= new ir_constant(type
, & this->primary_expression
);
1075 case ast_uint_constant
:
1076 type
= glsl_type::uint_type
;
1077 result
= new ir_constant(type
, & this->primary_expression
);
1080 case ast_float_constant
:
1081 type
= glsl_type::float_type
;
1082 result
= new ir_constant(type
, & this->primary_expression
);
1085 case ast_bool_constant
:
1086 type
= glsl_type::bool_type
;
1087 result
= new ir_constant(type
, & this->primary_expression
);
1090 case ast_sequence
: {
1091 struct simple_node
*ptr
;
1093 /* It should not be possible to generate a sequence in the AST without
1094 * any expressions in it.
1096 assert(!is_empty_list(&this->expressions
));
1098 /* The r-value of a sequence is the last expression in the sequence. If
1099 * the other expressions in the sequence do not have side-effects (and
1100 * therefore add instructions to the instruction list), they get dropped
1103 foreach (ptr
, &this->expressions
)
1104 result
= ((ast_node
*)ptr
)->hir(instructions
, state
);
1106 type
= result
->type
;
1108 /* Any errors should have already been emitted in the loop above.
1110 error_emitted
= true;
1115 if (type
->is_error() && !error_emitted
)
1116 _mesa_glsl_error(& loc
, state
, "type mismatch");
1123 ast_expression_statement::hir(exec_list
*instructions
,
1124 struct _mesa_glsl_parse_state
*state
)
1126 /* It is possible to have expression statements that don't have an
1127 * expression. This is the solitary semicolon:
1129 * for (i = 0; i < 5; i++)
1132 * In this case the expression will be NULL. Test for NULL and don't do
1133 * anything in that case.
1135 if (expression
!= NULL
)
1136 expression
->hir(instructions
, state
);
1138 /* Statements do not have r-values.
1145 ast_compound_statement::hir(exec_list
*instructions
,
1146 struct _mesa_glsl_parse_state
*state
)
1148 struct simple_node
*ptr
;
1152 state
->symbols
->push_scope();
1154 foreach (ptr
, &statements
)
1155 ((ast_node
*)ptr
)->hir(instructions
, state
);
1158 state
->symbols
->pop_scope();
1160 /* Compound statements do not have r-values.
1166 static const glsl_type
*
1167 process_array_type(const glsl_type
*base
, ast_node
*array_size
,
1168 struct _mesa_glsl_parse_state
*state
)
1170 unsigned length
= 0;
1172 /* FINISHME: Reject delcarations of multidimensional arrays. */
1174 if (array_size
!= NULL
) {
1175 exec_list dummy_instructions
;
1176 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1177 YYLTYPE loc
= array_size
->get_location();
1179 /* FINISHME: Verify that the grammar forbids side-effects in array
1180 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1182 assert(dummy_instructions
.is_empty());
1185 if (!ir
->type
->is_integer()) {
1186 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1187 } else if (!ir
->type
->is_scalar()) {
1188 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1190 ir_constant
*const size
= ir
->constant_expression_value();
1193 _mesa_glsl_error(& loc
, state
, "array size must be a "
1194 "constant valued expression");
1195 } else if (size
->value
.i
[0] <= 0) {
1196 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1198 assert(size
->type
== ir
->type
);
1199 length
= size
->value
.u
[0];
1205 return glsl_type::get_array_instance(base
, length
);
1210 ast_type_specifier::glsl_type(const char **name
,
1211 struct _mesa_glsl_parse_state
*state
) const
1213 const struct glsl_type
*type
;
1215 if (this->type_specifier
== ast_struct
) {
1216 /* FINISHME: Handle annonymous structures. */
1219 type
= state
->symbols
->get_type(this->type_name
);
1220 *name
= this->type_name
;
1222 if (this->is_array
) {
1223 type
= process_array_type(type
, this->array_size
, state
);
1232 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1233 struct ir_variable
*var
,
1234 struct _mesa_glsl_parse_state
*state
,
1237 if (qual
->invariant
)
1240 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1241 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
1242 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1248 if (qual
->attribute
&& state
->target
== fragment_shader
) {
1249 var
->type
= glsl_type::error_type
;
1250 _mesa_glsl_error(loc
, state
,
1251 "`attribute' variables may not be declared in the "
1255 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1257 * "The varying qualifier can be used only with the data types
1258 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1261 if (qual
->varying
&& var
->type
->base_type
!= GLSL_TYPE_FLOAT
) {
1262 var
->type
= glsl_type::error_type
;
1263 _mesa_glsl_error(loc
, state
,
1264 "varying variables must be of base type float");
1267 if (qual
->in
&& qual
->out
)
1268 var
->mode
= ir_var_inout
;
1269 else if (qual
->attribute
|| qual
->in
1270 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1271 var
->mode
= ir_var_in
;
1272 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
1273 var
->mode
= ir_var_out
;
1274 else if (qual
->uniform
)
1275 var
->mode
= ir_var_uniform
;
1277 var
->mode
= ir_var_auto
;
1280 var
->interpolation
= ir_var_flat
;
1281 else if (qual
->noperspective
)
1282 var
->interpolation
= ir_var_noperspective
;
1284 var
->interpolation
= ir_var_smooth
;
1289 ast_declarator_list::hir(exec_list
*instructions
,
1290 struct _mesa_glsl_parse_state
*state
)
1292 struct simple_node
*ptr
;
1293 const struct glsl_type
*decl_type
;
1294 const char *type_name
= NULL
;
1297 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1298 * FINISHME: include a type. These re-declare built-in variables to be
1299 * FINISHME: invariant.
1302 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1304 foreach (ptr
, &this->declarations
) {
1305 struct ast_declaration
*const decl
= (struct ast_declaration
* )ptr
;
1306 const struct glsl_type
*var_type
;
1307 struct ir_variable
*var
;
1308 YYLTYPE loc
= this->get_location();
1310 /* FINISHME: Emit a warning if a variable declaration shadows a
1311 * FINISHME: declaration at a higher scope.
1314 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1315 if (type_name
!= NULL
) {
1316 _mesa_glsl_error(& loc
, state
,
1317 "invalid type `%s' in declaration of `%s'",
1318 type_name
, decl
->identifier
);
1320 _mesa_glsl_error(& loc
, state
,
1321 "invalid type in declaration of `%s'",
1327 if (decl
->is_array
) {
1328 var_type
= process_array_type(decl_type
, decl
->array_size
, state
);
1330 var_type
= decl_type
;
1333 var
= new ir_variable(var_type
, decl
->identifier
);
1335 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1337 * "Global variables can only use the qualifiers const,
1338 * attribute, uni form, or varying. Only one may be
1341 * Local variables can only use the qualifier const."
1343 * This is relaxed in GLSL 1.30.
1345 if (state
->language_version
< 120) {
1346 if (this->type
->qualifier
.out
) {
1347 _mesa_glsl_error(& loc
, state
,
1348 "`out' qualifier in declaration of `%s' "
1349 "only valid for function parameters in GLSL 1.10.",
1352 if (this->type
->qualifier
.in
) {
1353 _mesa_glsl_error(& loc
, state
,
1354 "`in' qualifier in declaration of `%s' "
1355 "only valid for function parameters in GLSL 1.10.",
1358 /* FINISHME: Test for other invalid qualifiers. */
1361 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1364 /* Attempt to add the variable to the symbol table. If this fails, it
1365 * means the variable has already been declared at this scope. Arrays
1366 * fudge this rule a little bit.
1368 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1370 * "It is legal to declare an array without a size and then
1371 * later re-declare the same name as an array of the same
1372 * type and specify a size."
1374 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
1375 ir_variable
*const earlier
=
1376 state
->symbols
->get_variable(decl
->identifier
);
1378 if ((earlier
!= NULL
)
1379 && (earlier
->type
->array_size() == 0)
1380 && var
->type
->is_array()
1381 && (var
->type
->element_type() == earlier
->type
->element_type())) {
1382 /* FINISHME: This doesn't match the qualifiers on the two
1383 * FINISHME: declarations. It's not 100% clear whether this is
1384 * FINISHME: required or not.
1387 if (var
->type
->array_size() <= earlier
->max_array_access
) {
1388 YYLTYPE loc
= this->get_location();
1390 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
1392 earlier
->max_array_access
);
1395 earlier
->type
= var
->type
;
1399 YYLTYPE loc
= this->get_location();
1401 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
1408 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1410 * "Identifiers starting with "gl_" are reserved for use by
1411 * OpenGL, and may not be declared in a shader as either a
1412 * variable or a function."
1414 if (strncmp(decl
->identifier
, "gl_", 3) == 0) {
1415 /* FINISHME: This should only trigger if we're not redefining
1416 * FINISHME: a builtin (to add a qualifier, for example).
1418 _mesa_glsl_error(& loc
, state
,
1419 "identifier `%s' uses reserved `gl_' prefix",
1423 instructions
->push_tail(var
);
1425 if (state
->current_function
!= NULL
) {
1426 const char *mode
= NULL
;
1427 const char *extra
= "";
1429 /* There is no need to check for 'inout' here because the parser will
1430 * only allow that in function parameter lists.
1432 if (this->type
->qualifier
.attribute
) {
1434 } else if (this->type
->qualifier
.uniform
) {
1436 } else if (this->type
->qualifier
.varying
) {
1438 } else if (this->type
->qualifier
.in
) {
1440 extra
= " or in function parameter list";
1441 } else if (this->type
->qualifier
.out
) {
1443 extra
= " or in function parameter list";
1447 _mesa_glsl_error(& loc
, state
,
1448 "%s variable `%s' must be declared at "
1450 mode
, var
->name
, extra
);
1452 } else if (var
->mode
== ir_var_in
) {
1453 if (state
->target
== vertex_shader
) {
1454 bool error_emitted
= false;
1456 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1458 * "Vertex shader inputs can only be float, floating-point
1459 * vectors, matrices, signed and unsigned integers and integer
1460 * vectors. Vertex shader inputs can also form arrays of these
1461 * types, but not structures."
1463 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1465 * "Vertex shader inputs can only be float, floating-point
1466 * vectors, matrices, signed and unsigned integers and integer
1467 * vectors. They cannot be arrays or structures."
1469 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1471 * "The attribute qualifier can be used only with float,
1472 * floating-point vectors, and matrices. Attribute variables
1473 * cannot be declared as arrays or structures."
1475 const glsl_type
*check_type
= var
->type
->is_array()
1476 ? var
->type
->fields
.array
: var
->type
;
1478 switch (check_type
->base_type
) {
1479 case GLSL_TYPE_FLOAT
:
1481 case GLSL_TYPE_UINT
:
1483 if (state
->language_version
> 120)
1487 _mesa_glsl_error(& loc
, state
,
1488 "vertex shader input / attribute cannot have "
1490 var
->type
->is_array() ? "array of " : "",
1492 error_emitted
= true;
1495 if (!error_emitted
&& (state
->language_version
<= 130)
1496 && var
->type
->is_array()) {
1497 _mesa_glsl_error(& loc
, state
,
1498 "vertex shader input / attribute cannot have "
1500 error_emitted
= true;
1505 if (decl
->initializer
!= NULL
) {
1506 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
1508 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1510 * "All uniform variables are read-only and are initialized either
1511 * directly by an application via API commands, or indirectly by
1514 if ((state
->language_version
<= 110)
1515 && (var
->mode
== ir_var_uniform
)) {
1516 _mesa_glsl_error(& initializer_loc
, state
,
1517 "cannot initialize uniforms in GLSL 1.10");
1520 if (var
->type
->is_sampler()) {
1521 _mesa_glsl_error(& initializer_loc
, state
,
1522 "cannot initialize samplers");
1525 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1526 _mesa_glsl_error(& initializer_loc
, state
,
1527 "cannot initialize %s shader input / %s",
1528 (state
->target
== vertex_shader
)
1529 ? "vertex" : "fragment",
1530 (state
->target
== vertex_shader
)
1531 ? "attribute" : "varying");
1534 ir_dereference
*const lhs
= new ir_dereference(var
);
1535 ir_rvalue
*rhs
= decl
->initializer
->hir(instructions
, state
);
1537 /* Calculate the constant value if this is a const
1540 if (this->type
->qualifier
.constant
) {
1541 rhs
= rhs
->constant_expression_value();
1543 _mesa_glsl_error(& initializer_loc
, state
,
1544 "initializer of const variable `%s' must be a "
1545 "constant expression",
1550 if (rhs
&& !rhs
->type
->is_error()) {
1551 bool temp
= var
->read_only
;
1552 if (this->type
->qualifier
.constant
)
1553 var
->read_only
= false;
1554 (void) do_assignment(instructions
, state
, lhs
, rhs
,
1555 this->get_location());
1556 var
->read_only
= temp
;
1560 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1562 * "It is an error to write to a const variable outside of
1563 * its declaration, so they must be initialized when
1566 if (this->type
->qualifier
.constant
&& decl
->initializer
== NULL
) {
1567 _mesa_glsl_error(& loc
, state
,
1568 "const declaration of `%s' must be initialized");
1571 /* Add the vairable to the symbol table after processing the initializer.
1572 * This differs from most C-like languages, but it follows the GLSL
1573 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1576 * "Within a declaration, the scope of a name starts immediately
1577 * after the initializer if present or immediately after the name
1578 * being declared if not."
1580 const bool added_variable
=
1581 state
->symbols
->add_variable(decl
->identifier
, var
);
1582 assert(added_variable
);
1585 /* Variable declarations do not have r-values.
1592 ast_parameter_declarator::hir(exec_list
*instructions
,
1593 struct _mesa_glsl_parse_state
*state
)
1595 const struct glsl_type
*type
;
1596 const char *name
= NULL
;
1597 YYLTYPE loc
= this->get_location();
1599 type
= this->type
->specifier
->glsl_type(& name
, state
);
1603 _mesa_glsl_error(& loc
, state
,
1604 "invalid type `%s' in declaration of `%s'",
1605 name
, this->identifier
);
1607 _mesa_glsl_error(& loc
, state
,
1608 "invalid type in declaration of `%s'",
1612 type
= glsl_type::error_type
;
1615 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1617 * "Functions that accept no input arguments need not use void in the
1618 * argument list because prototypes (or definitions) are required and
1619 * therefore there is no ambiguity when an empty argument list "( )" is
1620 * declared. The idiom "(void)" as a parameter list is provided for
1623 * Placing this check here prevents a void parameter being set up
1624 * for a function, which avoids tripping up checks for main taking
1625 * parameters and lookups of an unnamed symbol.
1627 if (type
->is_void() && (this->identifier
== NULL
))
1630 ir_variable
*var
= new ir_variable(type
, this->identifier
);
1632 /* FINISHME: Handle array declarations. Note that this requires
1633 * FINISHME: complete handling of constant expressions.
1636 /* Apply any specified qualifiers to the parameter declaration. Note that
1637 * for function parameters the default mode is 'in'.
1639 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
1640 if (var
->mode
== ir_var_auto
)
1641 var
->mode
= ir_var_in
;
1643 instructions
->push_tail(var
);
1645 /* Parameter declarations do not have r-values.
1652 ast_function_parameters_to_hir(struct simple_node
*ast_parameters
,
1653 exec_list
*ir_parameters
,
1654 struct _mesa_glsl_parse_state
*state
)
1656 struct simple_node
*ptr
;
1658 foreach (ptr
, ast_parameters
) {
1659 ast_node
*param
= (ast_node
*)ptr
;
1660 param
->hir(ir_parameters
, state
);
1667 parameter_lists_match(exec_list
*list_a
, exec_list
*list_b
)
1669 exec_list_iterator iter_a
= list_a
->iterator();
1670 exec_list_iterator iter_b
= list_b
->iterator();
1672 while (iter_a
.has_next()) {
1673 ir_variable
*a
= (ir_variable
*)iter_a
.get();
1674 ir_variable
*b
= (ir_variable
*)iter_b
.get();
1676 /* If all of the parameters from the other parameter list have been
1677 * exhausted, the lists have different length and, by definition,
1680 if (!iter_b
.has_next())
1683 /* If the types of the parameters do not match, the parameters lists
1686 if (a
->type
!= b
->type
)
1698 ast_function::hir(exec_list
*instructions
,
1699 struct _mesa_glsl_parse_state
*state
)
1701 ir_function
*f
= NULL
;
1702 ir_function_signature
*sig
= NULL
;
1703 exec_list hir_parameters
;
1706 /* The prototype part of a function does not generate anything in the IR
1707 * instruction stream.
1709 (void) instructions
;
1711 /* Convert the list of function parameters to HIR now so that they can be
1712 * used below to compare this function's signature with previously seen
1713 * signatures for functions with the same name.
1715 ast_function_parameters_to_hir(& this->parameters
, & hir_parameters
, state
);
1717 const char *return_type_name
;
1718 const glsl_type
*return_type
=
1719 this->return_type
->specifier
->glsl_type(& return_type_name
, state
);
1721 assert(return_type
!= NULL
);
1723 /* Verify that this function's signature either doesn't match a previously
1724 * seen signature for a function with the same name, or, if a match is found,
1725 * that the previously seen signature does not have an associated definition.
1727 const char *const name
= identifier
;
1728 f
= state
->symbols
->get_function(name
);
1730 foreach_iter(exec_list_iterator
, iter
, *f
) {
1731 sig
= (struct ir_function_signature
*) iter
.get();
1733 /* Compare the parameter list of the function being defined to the
1734 * existing function. If the parameter lists match, then the return
1735 * type must also match and the existing function must not have a
1738 if (parameter_lists_match(& hir_parameters
, & sig
->parameters
)) {
1739 /* FINISHME: Compare return types. */
1741 if (is_definition
&& (sig
->definition
!= NULL
)) {
1742 YYLTYPE loc
= this->get_location();
1744 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
1753 } else if (state
->symbols
->name_declared_this_scope(name
)) {
1754 /* This function name shadows a non-function use of the same name.
1756 YYLTYPE loc
= this->get_location();
1758 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
1759 "non-function", name
);
1762 f
= new ir_function(name
);
1763 state
->symbols
->add_function(f
->name
, f
);
1766 /* Verify the return type of main() */
1767 if (strcmp(name
, "main") == 0) {
1768 if (! return_type
->is_void()) {
1769 YYLTYPE loc
= this->get_location();
1771 _mesa_glsl_error(& loc
, state
, "main() must return void");
1774 if (!hir_parameters
.is_empty()) {
1775 YYLTYPE loc
= this->get_location();
1777 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
1781 /* Finish storing the information about this new function in its signature.
1784 sig
= new ir_function_signature(return_type
);
1785 f
->add_signature(sig
);
1786 } else if (is_definition
) {
1787 /* Destroy all of the previous parameter information. The previous
1788 * parameter information comes from the function prototype, and it can
1789 * either include invalid parameter names or may not have names at all.
1791 foreach_iter(exec_list_iterator
, iter
, sig
->parameters
) {
1792 assert(((ir_instruction
*) iter
.get())->as_variable() != NULL
);
1799 hir_parameters
.move_nodes_to(& sig
->parameters
);
1802 /* Function declarations (prototypes) do not have r-values.
1809 ast_function_definition::hir(exec_list
*instructions
,
1810 struct _mesa_glsl_parse_state
*state
)
1812 prototype
->is_definition
= true;
1813 prototype
->hir(instructions
, state
);
1815 ir_function_signature
*signature
= prototype
->signature
;
1817 assert(state
->current_function
== NULL
);
1818 state
->current_function
= signature
;
1820 ir_label
*label
= new ir_label(signature
->function_name());
1821 if (signature
->definition
== NULL
) {
1822 signature
->definition
= label
;
1824 instructions
->push_tail(label
);
1826 /* Duplicate parameters declared in the prototype as concrete variables.
1827 * Add these to the symbol table.
1829 state
->symbols
->push_scope();
1830 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
1831 ir_variable
*const proto
= ((ir_instruction
*) iter
.get())->as_variable();
1833 assert(proto
!= NULL
);
1835 ir_variable
*const var
= proto
->clone();
1837 instructions
->push_tail(var
);
1839 /* The only way a parameter would "exist" is if two parameters have
1842 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
1843 YYLTYPE loc
= this->get_location();
1845 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
1847 state
->symbols
->add_variable(var
->name
, var
);
1851 /* Convert the body of the function to HIR, and append the resulting
1852 * instructions to the list that currently consists of the function label
1853 * and the function parameters.
1855 this->body
->hir(instructions
, state
);
1857 state
->symbols
->pop_scope();
1859 assert(state
->current_function
== signature
);
1860 state
->current_function
= NULL
;
1862 /* Function definitions do not have r-values.
1869 ast_jump_statement::hir(exec_list
*instructions
,
1870 struct _mesa_glsl_parse_state
*state
)
1873 if (mode
== ast_return
) {
1875 assert(state
->current_function
);
1877 if (opt_return_value
) {
1878 if (state
->current_function
->return_type
->base_type
==
1880 YYLTYPE loc
= this->get_location();
1882 _mesa_glsl_error(& loc
, state
,
1883 "`return` with a value, in function `%s' "
1885 state
->current_function
->definition
->label
);
1888 ir_expression
*const ret
= (ir_expression
*)
1889 opt_return_value
->hir(instructions
, state
);
1890 assert(ret
!= NULL
);
1892 /* FINISHME: Make sure the type of the return value matches the return
1893 * FINISHME: type of the enclosing function.
1896 inst
= new ir_return(ret
);
1898 if (state
->current_function
->return_type
->base_type
!=
1900 YYLTYPE loc
= this->get_location();
1902 _mesa_glsl_error(& loc
, state
,
1903 "`return' with no value, in function %s returning "
1905 state
->current_function
->definition
->label
);
1907 inst
= new ir_return
;
1910 instructions
->push_tail(inst
);
1913 if (mode
== ast_discard
) {
1914 /* FINISHME: discard support */
1915 if (state
->target
!= fragment_shader
) {
1916 YYLTYPE loc
= this->get_location();
1918 _mesa_glsl_error(& loc
, state
,
1919 "`discard' may only appear in a fragment shader");
1923 /* Jump instructions do not have r-values.
1930 ast_selection_statement::hir(exec_list
*instructions
,
1931 struct _mesa_glsl_parse_state
*state
)
1933 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
1935 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
1937 * "Any expression whose type evaluates to a Boolean can be used as the
1938 * conditional expression bool-expression. Vector types are not accepted
1939 * as the expression to if."
1941 * The checks are separated so that higher quality diagnostics can be
1942 * generated for cases where both rules are violated.
1944 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
1945 YYLTYPE loc
= this->condition
->get_location();
1947 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
1951 ir_if
*const stmt
= new ir_if(condition
);
1953 if (then_statement
!= NULL
) {
1954 ast_node
*node
= (ast_node
*) then_statement
;
1956 node
->hir(& stmt
->then_instructions
, state
);
1957 node
= (ast_node
*) node
->next
;
1958 } while (node
!= then_statement
);
1961 if (else_statement
!= NULL
) {
1962 ast_node
*node
= (ast_node
*) else_statement
;
1964 node
->hir(& stmt
->else_instructions
, state
);
1965 node
= (ast_node
*) node
->next
;
1966 } while (node
!= else_statement
);
1969 instructions
->push_tail(stmt
);
1971 /* if-statements do not have r-values.