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
);
67 state
->current_function
= NULL
;
69 foreach (ptr
, & state
->translation_unit
) {
70 ((ast_node
*)ptr
)->hir(instructions
, state
);
75 static const struct glsl_type
*
76 arithmetic_result_type(const struct glsl_type
*type_a
,
77 const struct glsl_type
*type_b
,
79 struct _mesa_glsl_parse_state
*state
)
81 /* From GLSL 1.50 spec, page 56:
83 * "The arithmetic binary operators add (+), subtract (-),
84 * multiply (*), and divide (/) operate on integer and
85 * floating-point scalars, vectors, and matrices."
87 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
88 return glsl_error_type
;
92 /* "If one operand is floating-point based and the other is
93 * not, then the conversions from Section 4.1.10 "Implicit
94 * Conversions" are applied to the non-floating-point-based operand."
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) {
100 if ((type_a
->base_type
== GLSL_TYPE_FLOAT
)
101 && (type_b
->base_type
!= GLSL_TYPE_FLOAT
)) {
102 } else if ((type_a
->base_type
!= GLSL_TYPE_FLOAT
)
103 && (type_b
->base_type
== GLSL_TYPE_FLOAT
)) {
107 /* "If the operands are integer types, they must both be signed or
110 * From this rule and the preceeding conversion it can be inferred that
111 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
112 * The is_numeric check above already filtered out the case where either
113 * type is not one of these, so now the base types need only be tested for
116 if (type_a
->base_type
!= type_b
->base_type
) {
117 return glsl_error_type
;
120 /* "All arithmetic binary operators result in the same fundamental type
121 * (signed integer, unsigned integer, or floating-point) as the
122 * operands they operate on, after operand type conversion. After
123 * conversion, the following cases are valid
125 * * The two operands are scalars. In this case the operation is
126 * applied, resulting in a scalar."
128 if (type_a
->is_scalar() && type_b
->is_scalar())
131 /* "* One operand is a scalar, and the other is a vector or matrix.
132 * In this case, the scalar operation is applied independently to each
133 * component of the vector or matrix, resulting in the same size
136 if (type_a
->is_scalar()) {
137 if (!type_b
->is_scalar())
139 } else if (type_b
->is_scalar()) {
143 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
144 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
147 assert(!type_a
->is_scalar());
148 assert(!type_b
->is_scalar());
150 /* "* The two operands are vectors of the same size. In this case, the
151 * operation is done component-wise resulting in the same size
154 if (type_a
->is_vector() && type_b
->is_vector()) {
155 return (type_a
== type_b
) ? type_a
: glsl_error_type
;
158 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
159 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
160 * <vector, vector> have been handled. At least one of the operands must
161 * be matrix. Further, since there are no integer matrix types, the base
162 * type of both operands must be float.
164 assert(type_a
->is_matrix() || type_b
->is_matrix());
165 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
166 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
168 /* "* The operator is add (+), subtract (-), or divide (/), and the
169 * operands are matrices with the same number of rows and the same
170 * number of columns. In this case, the operation is done component-
171 * wise resulting in the same size matrix."
172 * * The operator is multiply (*), where both operands are matrices or
173 * one operand is a vector and the other a matrix. A right vector
174 * operand is treated as a column vector and a left vector operand as a
175 * row vector. In all these cases, it is required that the number of
176 * columns of the left operand is equal to the number of rows of the
177 * right operand. Then, the multiply (*) operation does a linear
178 * algebraic multiply, yielding an object that has the same number of
179 * rows as the left operand and the same number of columns as the right
180 * operand. Section 5.10 "Vector and Matrix Operations" explains in
181 * more detail how vectors and matrices are operated on."
184 return (type_a
== type_b
) ? type_a
: glsl_error_type
;
186 if (type_a
->is_matrix() && type_b
->is_matrix()) {
187 /* Matrix multiply. The columns of A must match the rows of B. Given
188 * the other previously tested constraints, this means the vector type
189 * of a row from A must be the same as the vector type of a column from
192 if (type_a
->row_type() == type_b
->column_type()) {
193 /* The resulting matrix has the number of columns of matrix B and
194 * the number of rows of matrix A. We get the row count of A by
195 * looking at the size of a vector that makes up a column. The
196 * transpose (size of a row) is done for B.
199 glsl_type::get_instance(type_a
->base_type
,
200 type_a
->column_type()->vector_elements
,
201 type_b
->row_type()->vector_elements
);
203 } else if (type_a
->is_matrix()) {
204 /* A is a matrix and B is a column vector. Columns of A must match
205 * rows of B. Given the other previously tested constraints, this
206 * means the vector type of a row from A must be the same as the
207 * vector the type of B.
209 if (type_a
->row_type() == type_b
)
212 assert(type_b
->is_matrix());
214 /* A is a row vector and B is a matrix. Columns of A must match rows
215 * of B. Given the other previously tested constraints, this means
216 * the type of A must be the same as the vector type of a column from
219 if (type_a
== type_b
->column_type())
225 /* "All other cases are illegal."
227 return glsl_error_type
;
231 static const struct glsl_type
*
232 unary_arithmetic_result_type(const struct glsl_type
*type
)
234 /* From GLSL 1.50 spec, page 57:
236 * "The arithmetic unary operators negate (-), post- and pre-increment
237 * and decrement (-- and ++) operate on integer or floating-point
238 * values (including vectors and matrices). All unary operators work
239 * component-wise on their operands. These result with the same type
242 if (!is_numeric_base_type(type
->base_type
))
243 return glsl_error_type
;
249 static const struct glsl_type
*
250 modulus_result_type(const struct glsl_type
*type_a
,
251 const struct glsl_type
*type_b
)
253 /* From GLSL 1.50 spec, page 56:
254 * "The operator modulus (%) operates on signed or unsigned integers or
255 * integer vectors. The operand types must both be signed or both be
258 if (! is_integer_base_type(type_a
->base_type
)
259 || ! is_integer_base_type(type_b
->base_type
)
260 || (type_a
->base_type
!= type_b
->base_type
)) {
261 return glsl_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_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 (! is_numeric_base_type(type_a
->base_type
)
294 || ! is_numeric_base_type(type_b
->base_type
)
295 || !type_a
->is_scalar()
296 || !type_b
->is_scalar())
297 return glsl_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_error_type
;
319 /* "The result is scalar Boolean."
321 return glsl_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_instruction
*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. */
365 ast_node::hir(exec_list
*instructions
,
366 struct _mesa_glsl_parse_state
*state
)
376 ast_expression::hir(exec_list
*instructions
,
377 struct _mesa_glsl_parse_state
*state
)
379 static const int operations
[AST_NUM_OPERATORS
] = {
380 -1, /* ast_assign doesn't convert to ir_expression. */
381 -1, /* ast_plus doesn't convert to ir_expression. */
405 /* Note: The following block of expression types actually convert
406 * to multiple IR instructions.
408 ir_binop_mul
, /* ast_mul_assign */
409 ir_binop_div
, /* ast_div_assign */
410 ir_binop_mod
, /* ast_mod_assign */
411 ir_binop_add
, /* ast_add_assign */
412 ir_binop_sub
, /* ast_sub_assign */
413 ir_binop_lshift
, /* ast_ls_assign */
414 ir_binop_rshift
, /* ast_rs_assign */
415 ir_binop_bit_and
, /* ast_and_assign */
416 ir_binop_bit_xor
, /* ast_xor_assign */
417 ir_binop_bit_or
, /* ast_or_assign */
419 -1, /* ast_conditional doesn't convert to ir_expression. */
420 -1, /* ast_pre_inc doesn't convert to ir_expression. */
421 -1, /* ast_pre_dec doesn't convert to ir_expression. */
422 -1, /* ast_post_inc doesn't convert to ir_expression. */
423 -1, /* ast_post_dec doesn't convert to ir_expression. */
424 -1, /* ast_field_selection doesn't conv to ir_expression. */
425 -1, /* ast_array_index doesn't convert to ir_expression. */
426 -1, /* ast_function_call doesn't conv to ir_expression. */
427 -1, /* ast_identifier doesn't convert to ir_expression. */
428 -1, /* ast_int_constant doesn't convert to ir_expression. */
429 -1, /* ast_uint_constant doesn't conv to ir_expression. */
430 -1, /* ast_float_constant doesn't conv to ir_expression. */
431 -1, /* ast_bool_constant doesn't conv to ir_expression. */
432 -1, /* ast_sequence doesn't convert to ir_expression. */
434 ir_instruction
*result
= NULL
;
435 ir_instruction
*op
[2];
436 struct simple_node op_list
;
437 const struct glsl_type
*type
= glsl_error_type
;
438 bool error_emitted
= false;
441 loc
= this->get_location();
442 make_empty_list(& op_list
);
444 switch (this->oper
) {
446 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
447 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
449 error_emitted
= ((op
[0]->type
== glsl_error_type
)
450 || (op
[1]->type
== glsl_error_type
));
453 if (!error_emitted
) {
456 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
457 loc
= this->subexpressions
[0]->get_location();
458 const ir_dereference
*const ref
= op
[0]->as_dereference();
460 _mesa_glsl_error(& loc
, state
, "invalid lvalue in assignment");
461 error_emitted
= true;
463 type
= glsl_error_type
;
465 const ir_variable
*const var
= (ir_variable
*) ref
->var
;
467 if (var
!= NULL
&& var
->read_only
) {
468 _mesa_glsl_error(& loc
, state
, "cannot assign to read-only "
469 "variable `%s'", var
->name
);
470 error_emitted
= true;
472 type
= glsl_error_type
;
477 ir_instruction
*rhs
= validate_assignment(op
[0]->type
, op
[1]);
479 type
= glsl_error_type
;
483 ir_instruction
*tmp
= new ir_assignment(op
[0], op
[1], NULL
);
484 instructions
->push_tail(tmp
);
491 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
493 error_emitted
= (op
[0]->type
== glsl_error_type
);
494 if (type
== glsl_error_type
)
501 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
503 type
= unary_arithmetic_result_type(op
[0]->type
);
505 error_emitted
= (op
[0]->type
== glsl_error_type
);
507 result
= new ir_expression(operations
[this->oper
], type
,
515 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
516 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
518 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
519 (this->oper
== ast_mul
),
522 result
= new ir_expression(operations
[this->oper
], type
,
527 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
528 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
530 error_emitted
= ((op
[0]->type
== glsl_error_type
)
531 || (op
[1]->type
== glsl_error_type
));
533 type
= modulus_result_type(op
[0]->type
, op
[1]->type
);
535 assert(operations
[this->oper
] == ir_binop_mod
);
537 result
= new ir_expression(operations
[this->oper
], type
,
543 /* FINISHME: Implement bit-shift operators. */
550 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
551 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
553 error_emitted
= ((op
[0]->type
== glsl_error_type
)
554 || (op
[1]->type
== glsl_error_type
));
556 type
= relational_result_type(op
[0]->type
, op
[1]->type
, state
);
558 /* The relational operators must either generate an error or result
559 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
561 assert((type
== glsl_error_type
)
562 || ((type
->base_type
== GLSL_TYPE_BOOL
)
563 && type
->is_scalar()));
565 result
= new ir_expression(operations
[this->oper
], type
,
571 /* FINISHME: Implement equality operators. */
578 /* FINISHME: Implement bit-wise operators. */
585 /* FINISHME: Implement logical operators. */
591 case ast_sub_assign
: {
592 struct ir_instruction
*temp_rhs
;
594 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
595 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
597 error_emitted
= ((op
[0]->type
== glsl_error_type
)
598 || (op
[1]->type
== glsl_error_type
));
600 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
601 (this->oper
== ast_mul_assign
),
604 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
607 /* FINISHME: This is copied from ast_assign above. It should
608 * FINISHME: probably be consolidated.
610 error_emitted
= ((op
[0]->type
== glsl_error_type
)
611 || (temp_rhs
->type
== glsl_error_type
));
614 if (!error_emitted
) {
617 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
618 loc
= this->subexpressions
[0]->get_location();
619 const ir_dereference
*const ref
= op
[0]->as_dereference();
621 _mesa_glsl_error(& loc
, state
, "invalid lvalue in assignment");
622 error_emitted
= true;
624 type
= glsl_error_type
;
626 const ir_variable
*const var
= (ir_variable
*) ref
->var
;
628 if (var
!= NULL
&& var
->read_only
) {
629 _mesa_glsl_error(& loc
, state
, "cannot assign to read-only "
630 "variable `%s'", var
->name
);
631 error_emitted
= true;
633 type
= glsl_error_type
;
638 ir_instruction
*rhs
= validate_assignment(op
[0]->type
, temp_rhs
);
640 type
= glsl_error_type
;
644 ir_instruction
*tmp
= new ir_assignment(op
[0], rhs
, NULL
);
645 instructions
->push_tail(tmp
);
647 /* GLSL 1.10 does not allow array assignment. However, we don't have to
648 * explicitly test for this because none of the binary expression
649 * operators allow array operands either.
665 case ast_conditional
:
674 case ast_field_selection
:
675 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
679 case ast_array_index
:
682 case ast_function_call
:
683 /* Should *NEVER* get here. ast_function_call should always be handled
684 * by ast_function_expression::hir.
689 case ast_identifier
: {
690 /* ast_identifier can appear several places in a full abstract syntax
691 * tree. This particular use must be at location specified in the grammar
692 * as 'variable_identifier'.
695 state
->symbols
->get_variable(this->primary_expression
.identifier
);
697 result
= new ir_dereference(var
);
702 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
703 this->primary_expression
.identifier
);
705 error_emitted
= true;
710 case ast_int_constant
:
711 type
= glsl_int_type
;
712 result
= new ir_constant(type
, & this->primary_expression
);
715 case ast_uint_constant
:
716 type
= glsl_uint_type
;
717 result
= new ir_constant(type
, & this->primary_expression
);
720 case ast_float_constant
:
721 type
= glsl_float_type
;
722 result
= new ir_constant(type
, & this->primary_expression
);
725 case ast_bool_constant
:
726 type
= glsl_bool_type
;
727 result
= new ir_constant(type
, & this->primary_expression
);
731 struct simple_node
*ptr
;
733 /* It should not be possible to generate a sequence in the AST without
734 * any expressions in it.
736 assert(!is_empty_list(&this->expressions
));
738 /* The r-value of a sequence is the last expression in the sequence. If
739 * the other expressions in the sequence do not have side-effects (and
740 * therefore add instructions to the instruction list), they get dropped
743 foreach (ptr
, &this->expressions
)
744 result
= ((ast_node
*)ptr
)->hir(instructions
, state
);
748 /* Any errors should have already been emitted in the loop above.
750 error_emitted
= true;
755 if (is_error_type(type
) && !error_emitted
)
756 _mesa_glsl_error(& loc
, state
, "type mismatch");
763 ast_expression_statement::hir(exec_list
*instructions
,
764 struct _mesa_glsl_parse_state
*state
)
766 /* It is possible to have expression statements that don't have an
767 * expression. This is the solitary semicolon:
769 * for (i = 0; i < 5; i++)
772 * In this case the expression will be NULL. Test for NULL and don't do
773 * anything in that case.
775 if (expression
!= NULL
)
776 expression
->hir(instructions
, state
);
778 /* Statements do not have r-values.
785 ast_compound_statement::hir(exec_list
*instructions
,
786 struct _mesa_glsl_parse_state
*state
)
788 struct simple_node
*ptr
;
792 state
->symbols
->push_scope();
794 foreach (ptr
, &statements
)
795 ((ast_node
*)ptr
)->hir(instructions
, state
);
798 state
->symbols
->pop_scope();
800 /* Compound statements do not have r-values.
806 static const struct glsl_type
*
807 type_specifier_to_glsl_type(const struct ast_type_specifier
*spec
,
809 struct _mesa_glsl_parse_state
*state
)
811 struct glsl_type
*type
;
813 if (spec
->type_specifier
== ast_struct
) {
814 /* FINISHME: Handle annonymous structures. */
817 type
= state
->symbols
->get_type(spec
->type_name
);
818 *name
= spec
->type_name
;
820 /* FINISHME: Handle array declarations. Note that this requires complete
821 * FINISHME: handling of constant expressions.
830 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
831 struct ir_variable
*var
,
832 struct _mesa_glsl_parse_state
*state
)
837 /* FINISHME: Mark 'in' variables at global scope as read-only. */
838 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
839 || (qual
->varying
&& (state
->target
== fragment_shader
)))
845 if (qual
->in
&& qual
->out
)
846 var
->mode
= ir_var_inout
;
847 else if (qual
->attribute
|| qual
->in
848 || (qual
->varying
&& (state
->target
== fragment_shader
)))
849 var
->mode
= ir_var_in
;
850 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
851 var
->mode
= ir_var_out
;
852 else if (qual
->uniform
)
853 var
->mode
= ir_var_uniform
;
855 var
->mode
= ir_var_auto
;
858 var
->interpolation
= ir_var_flat
;
859 else if (qual
->noperspective
)
860 var
->interpolation
= ir_var_noperspective
;
862 var
->interpolation
= ir_var_smooth
;
867 ast_declarator_list::hir(exec_list
*instructions
,
868 struct _mesa_glsl_parse_state
*state
)
870 struct simple_node
*ptr
;
871 const struct glsl_type
*decl_type
;
872 const char *type_name
= NULL
;
875 /* FINISHME: Handle vertex shader "invariant" declarations that do not
876 * FINISHME: include a type. These re-declare built-in variables to be
877 * FINISHME: invariant.
880 decl_type
= type_specifier_to_glsl_type(this->type
->specifier
,
883 foreach (ptr
, &this->declarations
) {
884 struct ast_declaration
*const decl
= (struct ast_declaration
* )ptr
;
885 const struct glsl_type
*var_type
;
886 struct ir_variable
*var
;
889 /* FINISHME: Emit a warning if a variable declaration shadows a
890 * FINISHME: declaration at a higher scope.
893 if ((decl_type
== NULL
) || decl_type
->is_void()) {
896 loc
= this->get_location();
897 if (type_name
!= NULL
) {
898 _mesa_glsl_error(& loc
, state
,
899 "invalid type `%s' in declaration of `%s'",
900 type_name
, decl
->identifier
);
902 _mesa_glsl_error(& loc
, state
,
903 "invalid type in declaration of `%s'",
909 if (decl
->is_array
) {
910 /* FINISHME: Handle array declarations. Note that this requires
911 * FINISHME: complete handling of constant expressions.
914 /* FINISHME: Reject delcarations of multidimensional arrays. */
916 var_type
= decl_type
;
919 var
= new ir_variable(var_type
, decl
->identifier
);
921 /* FINISHME: Variables that are attribute, uniform, varying, in, or
922 * FINISHME: out varibles must be declared either at global scope or
923 * FINISHME: in a parameter list (in and out only).
926 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
);
928 /* Attempt to add the variable to the symbol table. If this fails, it
929 * means the variable has already been declared at this scope.
931 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
932 YYLTYPE loc
= this->get_location();
934 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
939 const bool added_variable
=
940 state
->symbols
->add_variable(decl
->identifier
, var
);
941 assert(added_variable
);
943 instructions
->push_tail(var
);
945 /* FINISHME: Process the declaration initializer. */
948 /* Variable declarations do not have r-values.
955 ast_parameter_declarator::hir(exec_list
*instructions
,
956 struct _mesa_glsl_parse_state
*state
)
958 const struct glsl_type
*type
;
959 const char *name
= NULL
;
962 type
= type_specifier_to_glsl_type(this->type
->specifier
, & name
, state
);
965 YYLTYPE loc
= this->get_location();
967 _mesa_glsl_error(& loc
, state
,
968 "invalid type `%s' in declaration of `%s'",
969 name
, this->identifier
);
971 _mesa_glsl_error(& loc
, state
,
972 "invalid type in declaration of `%s'",
976 type
= glsl_error_type
;
979 ir_variable
*var
= new ir_variable(type
, this->identifier
);
981 /* FINISHME: Handle array declarations. Note that this requires
982 * FINISHME: complete handling of constant expressions.
985 /* Apply any specified qualifiers to the parameter declaration. Note that
986 * for function parameters the default mode is 'in'.
988 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
);
989 if (var
->mode
== ir_var_auto
)
990 var
->mode
= ir_var_in
;
992 instructions
->push_tail(var
);
994 /* Parameter declarations do not have r-values.
1001 ast_function_parameters_to_hir(struct simple_node
*ast_parameters
,
1002 exec_list
*ir_parameters
,
1003 struct _mesa_glsl_parse_state
*state
)
1005 struct simple_node
*ptr
;
1007 foreach (ptr
, ast_parameters
) {
1008 ((ast_node
*)ptr
)->hir(ir_parameters
, state
);
1014 parameter_lists_match(exec_list
*list_a
, exec_list
*list_b
)
1016 exec_list_iterator iter_a
= list_a
->iterator();
1017 exec_list_iterator iter_b
= list_b
->iterator();
1019 while (iter_a
.has_next()) {
1020 /* If all of the parameters from the other parameter list have been
1021 * exhausted, the lists have different length and, by definition,
1024 if (!iter_b
.has_next())
1027 /* If the types of the parameters do not match, the parameters lists
1042 ast_function_definition::hir(exec_list
*instructions
,
1043 struct _mesa_glsl_parse_state
*state
)
1046 ir_function_signature
*signature
= NULL
;
1047 ir_function
*f
= NULL
;
1048 exec_list parameters
;
1051 /* Convert the list of function parameters to HIR now so that they can be
1052 * used below to compare this function's signature with previously seen
1053 * signatures for functions with the same name.
1055 ast_function_parameters_to_hir(& this->prototype
->parameters
, & parameters
,
1058 const char *return_type_name
;
1059 const glsl_type
*return_type
=
1060 type_specifier_to_glsl_type(this->prototype
->return_type
->specifier
,
1061 & return_type_name
, state
);
1063 assert(return_type
!= NULL
);
1066 /* Verify that this function's signature either doesn't match a previously
1067 * seen signature for a function with the same name, or, if a match is found,
1068 * that the previously seen signature does not have an associated definition.
1070 const char *const name
= this->prototype
->identifier
;
1071 f
= state
->symbols
->get_function(name
);
1073 foreach_iter(exec_list_iterator
, iter
, f
->signatures
) {
1074 signature
= (struct ir_function_signature
*) iter
.get();
1076 /* Compare the parameter list of the function being defined to the
1077 * existing function. If the parameter lists match, then the return
1078 * type must also match and the existing function must not have a
1081 if (parameter_lists_match(& parameters
, & signature
->parameters
)) {
1082 /* FINISHME: Compare return types. */
1084 if (signature
->definition
!= NULL
) {
1085 YYLTYPE loc
= this->get_location();
1087 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
1096 } else if (state
->symbols
->name_declared_this_scope(name
)) {
1097 /* This function name shadows a non-function use of the same name.
1099 YYLTYPE loc
= this->get_location();
1101 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
1102 "non-function", name
);
1105 f
= new ir_function(name
);
1106 state
->symbols
->add_function(f
->name
, f
);
1110 /* Finish storing the information about this new function in its signature.
1112 if (signature
== NULL
) {
1113 signature
= new ir_function_signature(return_type
);
1114 f
->signatures
.push_tail(signature
);
1116 /* Destroy all of the previous parameter information. The previous
1117 * parameter information comes from the function prototype, and it can
1118 * either include invalid parameter names or may not have names at all.
1120 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
1121 assert(((ir_instruction
*) iter
.get())->as_variable() != NULL
);
1129 assert(state
->current_function
== NULL
);
1130 state
->current_function
= signature
;
1132 ast_function_parameters_to_hir(& this->prototype
->parameters
,
1133 & signature
->parameters
,
1135 /* FINISHME: Set signature->return_type */
1137 label
= new ir_label(name
);
1138 if (signature
->definition
== NULL
) {
1139 signature
->definition
= label
;
1141 instructions
->push_tail(label
);
1143 /* Add the function parameters to the symbol table. During this step the
1144 * parameter declarations are also moved from the temporary "parameters" list
1145 * to the instruction list. There are other more efficient ways to do this,
1146 * but they involve ugly linked-list gymnastics.
1148 state
->symbols
->push_scope();
1149 foreach_iter(exec_list_iterator
, iter
, parameters
) {
1150 ir_variable
*const var
= (ir_variable
*) iter
.get();
1152 assert(((ir_instruction
*) var
)->as_variable() != NULL
);
1155 instructions
->push_tail(var
);
1157 /* The only way a parameter would "exist" is if two parameters have
1160 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
1161 YYLTYPE loc
= this->get_location();
1163 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
1165 state
->symbols
->add_variable(var
->name
, var
);
1169 /* Convert the body of the function to HIR, and append the resulting
1170 * instructions to the list that currently consists of the function label
1171 * and the function parameters.
1173 this->body
->hir(instructions
, state
);
1175 state
->symbols
->pop_scope();
1177 assert(state
->current_function
== signature
);
1178 state
->current_function
= NULL
;
1180 /* Function definitions do not have r-values.
1187 ast_jump_statement::hir(exec_list
*instructions
,
1188 struct _mesa_glsl_parse_state
*state
)
1191 if (mode
== ast_return
) {
1194 if (opt_return_value
) {
1195 /* FINISHME: Make sure the enclosing function has a non-void return
1199 ir_expression
*const ret
= (ir_expression
*)
1200 opt_return_value
->hir(instructions
, state
);
1201 assert(ret
!= NULL
);
1203 /* FINISHME: Make sure the type of the return value matches the return
1204 * FINISHME: type of the enclosing function.
1207 inst
= new ir_return(ret
);
1209 /* FINISHME: Make sure the enclosing function has a void return type.
1211 inst
= new ir_return
;
1214 instructions
->push_tail(inst
);
1217 /* Jump instructions do not have r-values.