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
);
76 static const struct glsl_type
*
77 arithmetic_result_type(const struct glsl_type
*type_a
,
78 const struct glsl_type
*type_b
,
80 struct _mesa_glsl_parse_state
*state
)
82 /* From GLSL 1.50 spec, page 56:
84 * "The arithmetic binary operators add (+), subtract (-),
85 * multiply (*), and divide (/) operate on integer and
86 * floating-point scalars, vectors, and matrices."
88 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
89 return glsl_type::error_type
;
93 /* "If one operand is floating-point based and the other is
94 * not, then the conversions from Section 4.1.10 "Implicit
95 * Conversions" are applied to the non-floating-point-based operand."
97 * This conversion was added in GLSL 1.20. If the compilation mode is
98 * GLSL 1.10, the conversion is skipped.
100 if (state
->language_version
>= 120) {
101 if ((type_a
->base_type
== GLSL_TYPE_FLOAT
)
102 && (type_b
->base_type
!= GLSL_TYPE_FLOAT
)) {
103 } else if ((type_a
->base_type
!= GLSL_TYPE_FLOAT
)
104 && (type_b
->base_type
== GLSL_TYPE_FLOAT
)) {
108 /* "If the operands are integer types, they must both be signed or
111 * From this rule and the preceeding conversion it can be inferred that
112 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
113 * The is_numeric check above already filtered out the case where either
114 * type is not one of these, so now the base types need only be tested for
117 if (type_a
->base_type
!= type_b
->base_type
) {
118 return glsl_type::error_type
;
121 /* "All arithmetic binary operators result in the same fundamental type
122 * (signed integer, unsigned integer, or floating-point) as the
123 * operands they operate on, after operand type conversion. After
124 * conversion, the following cases are valid
126 * * The two operands are scalars. In this case the operation is
127 * applied, resulting in a scalar."
129 if (type_a
->is_scalar() && type_b
->is_scalar())
132 /* "* One operand is a scalar, and the other is a vector or matrix.
133 * In this case, the scalar operation is applied independently to each
134 * component of the vector or matrix, resulting in the same size
137 if (type_a
->is_scalar()) {
138 if (!type_b
->is_scalar())
140 } else if (type_b
->is_scalar()) {
144 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
145 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
148 assert(!type_a
->is_scalar());
149 assert(!type_b
->is_scalar());
151 /* "* The two operands are vectors of the same size. In this case, the
152 * operation is done component-wise resulting in the same size
155 if (type_a
->is_vector() && type_b
->is_vector()) {
156 return (type_a
== type_b
) ? type_a
: glsl_type::error_type
;
159 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
160 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
161 * <vector, vector> have been handled. At least one of the operands must
162 * be matrix. Further, since there are no integer matrix types, the base
163 * type of both operands must be float.
165 assert(type_a
->is_matrix() || type_b
->is_matrix());
166 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
167 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
169 /* "* The operator is add (+), subtract (-), or divide (/), and the
170 * operands are matrices with the same number of rows and the same
171 * number of columns. In this case, the operation is done component-
172 * wise resulting in the same size matrix."
173 * * The operator is multiply (*), where both operands are matrices or
174 * one operand is a vector and the other a matrix. A right vector
175 * operand is treated as a column vector and a left vector operand as a
176 * row vector. In all these cases, it is required that the number of
177 * columns of the left operand is equal to the number of rows of the
178 * right operand. Then, the multiply (*) operation does a linear
179 * algebraic multiply, yielding an object that has the same number of
180 * rows as the left operand and the same number of columns as the right
181 * operand. Section 5.10 "Vector and Matrix Operations" explains in
182 * more detail how vectors and matrices are operated on."
185 return (type_a
== type_b
) ? type_a
: glsl_type::error_type
;
187 if (type_a
->is_matrix() && type_b
->is_matrix()) {
188 /* Matrix multiply. The columns of A must match the rows of B. Given
189 * the other previously tested constraints, this means the vector type
190 * of a row from A must be the same as the vector type of a column from
193 if (type_a
->row_type() == type_b
->column_type()) {
194 /* The resulting matrix has the number of columns of matrix B and
195 * the number of rows of matrix A. We get the row count of A by
196 * looking at the size of a vector that makes up a column. The
197 * transpose (size of a row) is done for B.
200 glsl_type::get_instance(type_a
->base_type
,
201 type_a
->column_type()->vector_elements
,
202 type_b
->row_type()->vector_elements
);
204 } else if (type_a
->is_matrix()) {
205 /* A is a matrix and B is a column vector. Columns of A must match
206 * rows of B. Given the other previously tested constraints, this
207 * means the vector type of a row from A must be the same as the
208 * vector the type of B.
210 if (type_a
->row_type() == type_b
)
213 assert(type_b
->is_matrix());
215 /* A is a row vector and B is a matrix. Columns of A must match rows
216 * of B. Given the other previously tested constraints, this means
217 * the type of A must be the same as the vector type of a column from
220 if (type_a
== type_b
->column_type())
226 /* "All other cases are illegal."
228 return glsl_type::error_type
;
232 static const struct glsl_type
*
233 unary_arithmetic_result_type(const struct glsl_type
*type
)
235 /* From GLSL 1.50 spec, page 57:
237 * "The arithmetic unary operators negate (-), post- and pre-increment
238 * and decrement (-- and ++) operate on integer or floating-point
239 * values (including vectors and matrices). All unary operators work
240 * component-wise on their operands. These result with the same type
243 if (!type
->is_numeric())
244 return glsl_type::error_type
;
250 static const struct glsl_type
*
251 modulus_result_type(const struct glsl_type
*type_a
,
252 const struct glsl_type
*type_b
)
254 /* From GLSL 1.50 spec, page 56:
255 * "The operator modulus (%) operates on signed or unsigned integers or
256 * integer vectors. The operand types must both be signed or both be
259 if (!type_a
->is_integer() || !type_b
->is_integer()
260 || (type_a
->base_type
!= type_b
->base_type
)) {
261 return glsl_type::error_type
;
264 /* "The operands cannot be vectors of differing size. If one operand is
265 * a scalar and the other vector, then the scalar is applied component-
266 * wise to the vector, resulting in the same type as the vector. If both
267 * are vectors of the same size, the result is computed component-wise."
269 if (type_a
->is_vector()) {
270 if (!type_b
->is_vector()
271 || (type_a
->vector_elements
== type_b
->vector_elements
))
276 /* "The operator modulus (%) is not defined for any other data types
277 * (non-integer types)."
279 return glsl_type::error_type
;
283 static const struct glsl_type
*
284 relational_result_type(const struct glsl_type
*type_a
,
285 const struct glsl_type
*type_b
,
286 struct _mesa_glsl_parse_state
*state
)
288 /* From GLSL 1.50 spec, page 56:
289 * "The relational operators greater than (>), less than (<), greater
290 * than or equal (>=), and less than or equal (<=) operate only on
291 * scalar integer and scalar floating-point expressions."
293 if (!type_a
->is_numeric()
294 || !type_b
->is_numeric()
295 || !type_a
->is_scalar()
296 || !type_b
->is_scalar())
297 return glsl_type::error_type
;
299 /* "Either the operands' types must match, or the conversions from
300 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
301 * operand, after which the types must match."
303 * This conversion was added in GLSL 1.20. If the compilation mode is
304 * GLSL 1.10, the conversion is skipped.
306 if (state
->language_version
>= 120) {
307 if ((type_a
->base_type
== GLSL_TYPE_FLOAT
)
308 && (type_b
->base_type
!= GLSL_TYPE_FLOAT
)) {
309 /* FINISHME: Generate the implicit type conversion. */
310 } else if ((type_a
->base_type
!= GLSL_TYPE_FLOAT
)
311 && (type_b
->base_type
== GLSL_TYPE_FLOAT
)) {
312 /* FINISHME: Generate the implicit type conversion. */
316 if (type_a
->base_type
!= type_b
->base_type
)
317 return glsl_type::error_type
;
319 /* "The result is scalar Boolean."
321 return glsl_type::bool_type
;
326 * Validates that a value can be assigned to a location with a specified type
328 * Validates that \c rhs can be assigned to some location. If the types are
329 * not an exact match but an automatic conversion is possible, \c rhs will be
333 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
334 * Otherwise the actual RHS to be assigned will be returned. This may be
335 * \c rhs, or it may be \c rhs after some type conversion.
338 * In addition to being used for assignments, this function is used to
339 * type-check return values.
342 validate_assignment(const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
344 const glsl_type
*const rhs_type
= rhs
->type
;
346 /* If there is already some error in the RHS, just return it. Anything
347 * else will lead to an avalanche of error message back to the user.
349 if (rhs_type
->is_error())
352 /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
354 /* If the types are identical, the assignment can trivially proceed.
356 if (rhs_type
== lhs_type
)
359 /* FINISHME: Check for and apply automatic conversions. */
364 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
365 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
368 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
370 if (!error_emitted
) {
371 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
372 if (!lhs
->is_lvalue()) {
373 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
374 error_emitted
= true;
378 ir_rvalue
*new_rhs
= validate_assignment(lhs
->type
, rhs
);
379 if (new_rhs
== NULL
) {
380 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
385 ir_instruction
*tmp
= new ir_assignment(lhs
, rhs
, NULL
);
386 instructions
->push_tail(tmp
);
392 get_lvalue_copy(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
393 ir_rvalue
*lvalue
, YYLTYPE loc
)
396 ir_rvalue
*var_deref
;
398 /* FINISHME: Give unique names to the temporaries. */
399 var
= new ir_variable(lvalue
->type
, "_internal_tmp");
400 var
->mode
= ir_var_auto
;
402 var_deref
= new ir_dereference(var
);
403 do_assignment(instructions
, state
, var_deref
, lvalue
, loc
);
405 /* Once we've created this temporary, mark it read only so it's no
406 * longer considered an lvalue.
408 var
->read_only
= true;
415 ast_node::hir(exec_list
*instructions
,
416 struct _mesa_glsl_parse_state
*state
)
426 ast_expression::hir(exec_list
*instructions
,
427 struct _mesa_glsl_parse_state
*state
)
429 static const int operations
[AST_NUM_OPERATORS
] = {
430 -1, /* ast_assign doesn't convert to ir_expression. */
431 -1, /* ast_plus doesn't convert to ir_expression. */
455 /* Note: The following block of expression types actually convert
456 * to multiple IR instructions.
458 ir_binop_mul
, /* ast_mul_assign */
459 ir_binop_div
, /* ast_div_assign */
460 ir_binop_mod
, /* ast_mod_assign */
461 ir_binop_add
, /* ast_add_assign */
462 ir_binop_sub
, /* ast_sub_assign */
463 ir_binop_lshift
, /* ast_ls_assign */
464 ir_binop_rshift
, /* ast_rs_assign */
465 ir_binop_bit_and
, /* ast_and_assign */
466 ir_binop_bit_xor
, /* ast_xor_assign */
467 ir_binop_bit_or
, /* ast_or_assign */
469 -1, /* ast_conditional doesn't convert to ir_expression. */
470 ir_binop_add
, /* ast_pre_inc. */
471 ir_binop_sub
, /* ast_pre_dec. */
472 ir_binop_add
, /* ast_post_inc. */
473 ir_binop_sub
, /* ast_post_dec. */
474 -1, /* ast_field_selection doesn't conv to ir_expression. */
475 -1, /* ast_array_index doesn't convert to ir_expression. */
476 -1, /* ast_function_call doesn't conv to ir_expression. */
477 -1, /* ast_identifier doesn't convert to ir_expression. */
478 -1, /* ast_int_constant doesn't convert to ir_expression. */
479 -1, /* ast_uint_constant doesn't conv to ir_expression. */
480 -1, /* ast_float_constant doesn't conv to ir_expression. */
481 -1, /* ast_bool_constant doesn't conv to ir_expression. */
482 -1, /* ast_sequence doesn't convert to ir_expression. */
484 ir_rvalue
*result
= NULL
;
486 struct simple_node op_list
;
487 const struct glsl_type
*type
= glsl_type::error_type
;
488 bool error_emitted
= false;
491 loc
= this->get_location();
492 make_empty_list(& op_list
);
494 switch (this->oper
) {
496 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
497 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
499 result
= do_assignment(instructions
, state
, op
[0], op
[1],
500 this->subexpressions
[0]->get_location());
501 error_emitted
= result
->type
->is_error();
507 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
509 error_emitted
= op
[0]->type
->is_error();
510 if (type
->is_error())
517 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
519 type
= unary_arithmetic_result_type(op
[0]->type
);
521 error_emitted
= op
[0]->type
->is_error();
523 result
= new ir_expression(operations
[this->oper
], type
,
531 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
532 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
534 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
535 (this->oper
== ast_mul
),
538 result
= new ir_expression(operations
[this->oper
], type
,
543 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
544 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
546 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
548 type
= modulus_result_type(op
[0]->type
, op
[1]->type
);
550 assert(operations
[this->oper
] == ir_binop_mod
);
552 result
= new ir_expression(operations
[this->oper
], type
,
558 /* FINISHME: Implement bit-shift operators. */
565 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
566 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
568 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
570 type
= relational_result_type(op
[0]->type
, op
[1]->type
, state
);
572 /* The relational operators must either generate an error or result
573 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
575 assert(type
->is_error()
576 || ((type
->base_type
== GLSL_TYPE_BOOL
)
577 && type
->is_scalar()));
579 result
= new ir_expression(operations
[this->oper
], type
,
585 /* FINISHME: Implement equality operators. */
592 /* FINISHME: Implement bit-wise operators. */
599 /* FINISHME: Implement logical operators. */
605 case ast_sub_assign
: {
606 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
607 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
609 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
610 (this->oper
== ast_mul_assign
),
613 ir_rvalue
*temp_rhs
= new ir_expression(operations
[this->oper
], type
,
616 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
617 this->subexpressions
[0]->get_location());
619 error_emitted
= (op
[0]->type
->is_error());
621 /* GLSL 1.10 does not allow array assignment. However, we don't have to
622 * explicitly test for this because none of the binary expression
623 * operators allow array operands either.
629 case ast_mod_assign
: {
630 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
631 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
633 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
635 type
= modulus_result_type(op
[0]->type
, op
[1]->type
);
637 assert(operations
[this->oper
] == ir_binop_mod
);
639 struct ir_rvalue
*temp_rhs
;
640 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
643 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
644 this->subexpressions
[0]->get_location());
646 error_emitted
= op
[0]->type
->is_error();
657 case ast_conditional
:
661 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
662 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
663 op
[1] = new ir_constant(1.0f
);
665 op
[1] = new ir_constant(1);
667 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
670 struct ir_rvalue
*temp_rhs
;
671 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
674 result
= do_assignment(instructions
, state
, op
[0], temp_rhs
,
675 this->subexpressions
[0]->get_location());
677 error_emitted
= op
[0]->type
->is_error();
683 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
684 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
685 op
[1] = new ir_constant(1.0f
);
687 op
[1] = new ir_constant(1);
689 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
691 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
694 struct ir_rvalue
*temp_rhs
;
695 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
698 /* Get a temporary of a copy of the lvalue before it's modified.
699 * This may get thrown away later.
701 result
= get_lvalue_copy(instructions
, state
, op
[0],
702 this->subexpressions
[0]->get_location());
704 (void)do_assignment(instructions
, state
, op
[0], temp_rhs
,
705 this->subexpressions
[0]->get_location());
708 error_emitted
= op
[0]->type
->is_error();
712 case ast_field_selection
:
713 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
717 case ast_array_index
:
720 case ast_function_call
:
721 /* Should *NEVER* get here. ast_function_call should always be handled
722 * by ast_function_expression::hir.
727 case ast_identifier
: {
728 /* ast_identifier can appear several places in a full abstract syntax
729 * tree. This particular use must be at location specified in the grammar
730 * as 'variable_identifier'.
733 state
->symbols
->get_variable(this->primary_expression
.identifier
);
735 result
= new ir_dereference(var
);
740 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
741 this->primary_expression
.identifier
);
743 error_emitted
= true;
748 case ast_int_constant
:
749 type
= glsl_type::int_type
;
750 result
= new ir_constant(type
, & this->primary_expression
);
753 case ast_uint_constant
:
754 type
= glsl_type::uint_type
;
755 result
= new ir_constant(type
, & this->primary_expression
);
758 case ast_float_constant
:
759 type
= glsl_type::float_type
;
760 result
= new ir_constant(type
, & this->primary_expression
);
763 case ast_bool_constant
:
764 type
= glsl_type::bool_type
;
765 result
= new ir_constant(type
, & this->primary_expression
);
769 struct simple_node
*ptr
;
771 /* It should not be possible to generate a sequence in the AST without
772 * any expressions in it.
774 assert(!is_empty_list(&this->expressions
));
776 /* The r-value of a sequence is the last expression in the sequence. If
777 * the other expressions in the sequence do not have side-effects (and
778 * therefore add instructions to the instruction list), they get dropped
781 foreach (ptr
, &this->expressions
)
782 result
= ((ast_node
*)ptr
)->hir(instructions
, state
);
786 /* Any errors should have already been emitted in the loop above.
788 error_emitted
= true;
793 if (type
->is_error() && !error_emitted
)
794 _mesa_glsl_error(& loc
, state
, "type mismatch");
801 ast_expression_statement::hir(exec_list
*instructions
,
802 struct _mesa_glsl_parse_state
*state
)
804 /* It is possible to have expression statements that don't have an
805 * expression. This is the solitary semicolon:
807 * for (i = 0; i < 5; i++)
810 * In this case the expression will be NULL. Test for NULL and don't do
811 * anything in that case.
813 if (expression
!= NULL
)
814 expression
->hir(instructions
, state
);
816 /* Statements do not have r-values.
823 ast_compound_statement::hir(exec_list
*instructions
,
824 struct _mesa_glsl_parse_state
*state
)
826 struct simple_node
*ptr
;
830 state
->symbols
->push_scope();
832 foreach (ptr
, &statements
)
833 ((ast_node
*)ptr
)->hir(instructions
, state
);
836 state
->symbols
->pop_scope();
838 /* Compound statements do not have r-values.
844 static const struct glsl_type
*
845 type_specifier_to_glsl_type(const struct ast_type_specifier
*spec
,
847 struct _mesa_glsl_parse_state
*state
)
849 struct glsl_type
*type
;
851 if (spec
->type_specifier
== ast_struct
) {
852 /* FINISHME: Handle annonymous structures. */
855 type
= state
->symbols
->get_type(spec
->type_name
);
856 *name
= spec
->type_name
;
858 /* FINISHME: Handle array declarations. Note that this requires complete
859 * FINISHME: handling of constant expressions.
868 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
869 struct ir_variable
*var
,
870 struct _mesa_glsl_parse_state
*state
,
876 /* FINISHME: Mark 'in' variables at global scope as read-only. */
877 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
878 || (qual
->varying
&& (state
->target
== fragment_shader
)))
884 if (qual
->attribute
&& state
->target
== fragment_shader
) {
885 var
->type
= glsl_type::error_type
;
886 _mesa_glsl_error(loc
, state
,
887 "`attribute' variables may not be declared in the "
891 if (qual
->in
&& qual
->out
)
892 var
->mode
= ir_var_inout
;
893 else if (qual
->attribute
|| qual
->in
894 || (qual
->varying
&& (state
->target
== fragment_shader
)))
895 var
->mode
= ir_var_in
;
896 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
897 var
->mode
= ir_var_out
;
898 else if (qual
->uniform
)
899 var
->mode
= ir_var_uniform
;
901 var
->mode
= ir_var_auto
;
904 var
->interpolation
= ir_var_flat
;
905 else if (qual
->noperspective
)
906 var
->interpolation
= ir_var_noperspective
;
908 var
->interpolation
= ir_var_smooth
;
913 ast_declarator_list::hir(exec_list
*instructions
,
914 struct _mesa_glsl_parse_state
*state
)
916 struct simple_node
*ptr
;
917 const struct glsl_type
*decl_type
;
918 const char *type_name
= NULL
;
921 /* FINISHME: Handle vertex shader "invariant" declarations that do not
922 * FINISHME: include a type. These re-declare built-in variables to be
923 * FINISHME: invariant.
926 decl_type
= type_specifier_to_glsl_type(this->type
->specifier
,
929 foreach (ptr
, &this->declarations
) {
930 struct ast_declaration
*const decl
= (struct ast_declaration
* )ptr
;
931 const struct glsl_type
*var_type
;
932 struct ir_variable
*var
;
933 YYLTYPE loc
= this->get_location();
935 /* FINISHME: Emit a warning if a variable declaration shadows a
936 * FINISHME: declaration at a higher scope.
939 if ((decl_type
== NULL
) || decl_type
->is_void()) {
940 if (type_name
!= NULL
) {
941 _mesa_glsl_error(& loc
, state
,
942 "invalid type `%s' in declaration of `%s'",
943 type_name
, decl
->identifier
);
945 _mesa_glsl_error(& loc
, state
,
946 "invalid type in declaration of `%s'",
952 if (decl
->is_array
) {
953 /* FINISHME: Handle array declarations. Note that this requires
954 * FINISHME: complete handling of constant expressions.
956 var_type
= glsl_type::error_type
;
958 /* FINISHME: Reject delcarations of multidimensional arrays. */
960 var_type
= decl_type
;
963 var
= new ir_variable(var_type
, decl
->identifier
);
965 /* FINISHME: Variables that are attribute, uniform, varying, in, or
966 * FINISHME: out varibles must be declared either at global scope or
967 * FINISHME: in a parameter list (in and out only).
970 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
973 /* Attempt to add the variable to the symbol table. If this fails, it
974 * means the variable has already been declared at this scope.
976 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
977 YYLTYPE loc
= this->get_location();
979 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
984 const bool added_variable
=
985 state
->symbols
->add_variable(decl
->identifier
, var
);
986 assert(added_variable
);
988 instructions
->push_tail(var
);
990 if (decl
->initializer
!= NULL
) {
991 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
993 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
995 * "All uniform variables are read-only and are initialized either
996 * directly by an application via API commands, or indirectly by
999 if ((state
->language_version
<= 110)
1000 && (var
->mode
== ir_var_uniform
)) {
1001 _mesa_glsl_error(& initializer_loc
, state
,
1002 "cannot initialize uniforms in GLSL 1.10");
1005 if (var
->type
->is_sampler()) {
1006 _mesa_glsl_error(& initializer_loc
, state
,
1007 "cannot initialize samplers");
1010 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1011 _mesa_glsl_error(& initializer_loc
, state
,
1012 "cannot initialize %s shader input / %s",
1013 (state
->target
== vertex_shader
)
1014 ? "vertex" : "fragment",
1015 (state
->target
== vertex_shader
)
1016 ? "attribute" : "varying");
1019 ir_dereference
*const lhs
= new ir_dereference(var
);
1020 ir_rvalue
*const rhs
= decl
->initializer
->hir(instructions
, state
);
1022 /* FINISHME: If the declaration is either 'const' or 'uniform', the
1023 * FINISHME: initializer (rhs) must be a constant expression.
1026 if (!rhs
->type
->is_error()) {
1027 (void) do_assignment(instructions
, state
, lhs
, rhs
,
1028 this->get_location());
1033 /* Variable declarations do not have r-values.
1040 ast_parameter_declarator::hir(exec_list
*instructions
,
1041 struct _mesa_glsl_parse_state
*state
)
1043 const struct glsl_type
*type
;
1044 const char *name
= NULL
;
1045 YYLTYPE loc
= this->get_location();
1047 type
= type_specifier_to_glsl_type(this->type
->specifier
, & name
, state
);
1051 _mesa_glsl_error(& loc
, state
,
1052 "invalid type `%s' in declaration of `%s'",
1053 name
, this->identifier
);
1055 _mesa_glsl_error(& loc
, state
,
1056 "invalid type in declaration of `%s'",
1060 type
= glsl_type::error_type
;
1063 ir_variable
*var
= new ir_variable(type
, this->identifier
);
1065 /* FINISHME: Handle array declarations. Note that this requires
1066 * FINISHME: complete handling of constant expressions.
1069 /* Apply any specified qualifiers to the parameter declaration. Note that
1070 * for function parameters the default mode is 'in'.
1072 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
1073 if (var
->mode
== ir_var_auto
)
1074 var
->mode
= ir_var_in
;
1076 instructions
->push_tail(var
);
1078 /* Parameter declarations do not have r-values.
1085 ast_function_parameters_to_hir(struct simple_node
*ast_parameters
,
1086 exec_list
*ir_parameters
,
1087 struct _mesa_glsl_parse_state
*state
)
1089 struct simple_node
*ptr
;
1091 foreach (ptr
, ast_parameters
) {
1092 ((ast_node
*)ptr
)->hir(ir_parameters
, state
);
1098 parameter_lists_match(exec_list
*list_a
, exec_list
*list_b
)
1100 exec_list_iterator iter_a
= list_a
->iterator();
1101 exec_list_iterator iter_b
= list_b
->iterator();
1103 while (iter_a
.has_next()) {
1104 /* If all of the parameters from the other parameter list have been
1105 * exhausted, the lists have different length and, by definition,
1108 if (!iter_b
.has_next())
1111 /* If the types of the parameters do not match, the parameters lists
1126 ast_function_definition::hir(exec_list
*instructions
,
1127 struct _mesa_glsl_parse_state
*state
)
1130 ir_function_signature
*signature
= NULL
;
1131 ir_function
*f
= NULL
;
1132 exec_list parameters
;
1135 /* Convert the list of function parameters to HIR now so that they can be
1136 * used below to compare this function's signature with previously seen
1137 * signatures for functions with the same name.
1139 ast_function_parameters_to_hir(& this->prototype
->parameters
, & parameters
,
1142 const char *return_type_name
;
1143 const glsl_type
*return_type
=
1144 type_specifier_to_glsl_type(this->prototype
->return_type
->specifier
,
1145 & return_type_name
, state
);
1147 assert(return_type
!= NULL
);
1150 /* Verify that this function's signature either doesn't match a previously
1151 * seen signature for a function with the same name, or, if a match is found,
1152 * that the previously seen signature does not have an associated definition.
1154 const char *const name
= this->prototype
->identifier
;
1155 f
= state
->symbols
->get_function(name
);
1157 foreach_iter(exec_list_iterator
, iter
, f
->signatures
) {
1158 signature
= (struct ir_function_signature
*) iter
.get();
1160 /* Compare the parameter list of the function being defined to the
1161 * existing function. If the parameter lists match, then the return
1162 * type must also match and the existing function must not have a
1165 if (parameter_lists_match(& parameters
, & signature
->parameters
)) {
1166 /* FINISHME: Compare return types. */
1168 if (signature
->definition
!= NULL
) {
1169 YYLTYPE loc
= this->get_location();
1171 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
1180 } else if (state
->symbols
->name_declared_this_scope(name
)) {
1181 /* This function name shadows a non-function use of the same name.
1183 YYLTYPE loc
= this->get_location();
1185 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
1186 "non-function", name
);
1189 f
= new ir_function(name
);
1190 state
->symbols
->add_function(f
->name
, f
);
1194 /* Finish storing the information about this new function in its signature.
1196 if (signature
== NULL
) {
1197 signature
= new ir_function_signature(return_type
);
1198 f
->signatures
.push_tail(signature
);
1200 /* Destroy all of the previous parameter information. The previous
1201 * parameter information comes from the function prototype, and it can
1202 * either include invalid parameter names or may not have names at all.
1204 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
1205 assert(((ir_instruction
*) iter
.get())->as_variable() != NULL
);
1213 assert(state
->current_function
== NULL
);
1214 state
->current_function
= signature
;
1216 ast_function_parameters_to_hir(& this->prototype
->parameters
,
1217 & signature
->parameters
,
1219 /* FINISHME: Set signature->return_type */
1221 label
= new ir_label(name
);
1222 if (signature
->definition
== NULL
) {
1223 signature
->definition
= label
;
1225 instructions
->push_tail(label
);
1227 /* Add the function parameters to the symbol table. During this step the
1228 * parameter declarations are also moved from the temporary "parameters" list
1229 * to the instruction list. There are other more efficient ways to do this,
1230 * but they involve ugly linked-list gymnastics.
1232 state
->symbols
->push_scope();
1233 foreach_iter(exec_list_iterator
, iter
, parameters
) {
1234 ir_variable
*const var
= (ir_variable
*) iter
.get();
1236 assert(((ir_instruction
*) var
)->as_variable() != NULL
);
1239 instructions
->push_tail(var
);
1241 /* The only way a parameter would "exist" is if two parameters have
1244 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
1245 YYLTYPE loc
= this->get_location();
1247 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
1249 state
->symbols
->add_variable(var
->name
, var
);
1253 /* Convert the body of the function to HIR, and append the resulting
1254 * instructions to the list that currently consists of the function label
1255 * and the function parameters.
1257 this->body
->hir(instructions
, state
);
1259 state
->symbols
->pop_scope();
1261 assert(state
->current_function
== signature
);
1262 state
->current_function
= NULL
;
1264 /* Function definitions do not have r-values.
1271 ast_jump_statement::hir(exec_list
*instructions
,
1272 struct _mesa_glsl_parse_state
*state
)
1275 if (mode
== ast_return
) {
1278 if (opt_return_value
) {
1279 /* FINISHME: Make sure the enclosing function has a non-void return
1283 ir_expression
*const ret
= (ir_expression
*)
1284 opt_return_value
->hir(instructions
, state
);
1285 assert(ret
!= NULL
);
1287 /* FINISHME: Make sure the type of the return value matches the return
1288 * FINISHME: type of the enclosing function.
1291 inst
= new ir_return(ret
);
1293 /* FINISHME: Make sure the enclosing function has a void return type.
1295 inst
= new ir_return
;
1298 instructions
->push_tail(inst
);
1301 /* Jump instructions do not have r-values.