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
);
66 state
->current_function
= NULL
;
68 foreach (ptr
, & state
->translation_unit
) {
69 ((ast_node
*)ptr
)->hir(instructions
, state
);
74 static const struct glsl_type
*
75 arithmetic_result_type(const struct glsl_type
*type_a
,
76 const struct glsl_type
*type_b
,
78 struct _mesa_glsl_parse_state
*state
)
80 /* From GLSL 1.50 spec, page 56:
82 * "The arithmetic binary operators add (+), subtract (-),
83 * multiply (*), and divide (/) operate on integer and
84 * floating-point scalars, vectors, and matrices."
86 if (! is_numeric_base_type(type_a
->base_type
)
87 || ! is_numeric_base_type(type_b
->base_type
)) {
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_base_type check above already filtered out the case
113 * where either type is not one of these, so now the base types need only
114 * be tested for equality.
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
->vector_elements
> 1);
148 assert(type_b
->vector_elements
> 1);
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 if (type_a
->vector_elements
== type_b
->vector_elements
)
158 return glsl_error_type
;
161 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
162 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
163 * <vector, vector> have been handled. At least one of the operands must
164 * be matrix. Further, since there are no integer matrix types, the base
165 * type of both operands must be float.
167 assert((type_a
->matrix_rows
> 1) || (type_b
->matrix_rows
> 1));
168 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
169 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
171 /* "* The operator is add (+), subtract (-), or divide (/), and the
172 * operands are matrices with the same number of rows and the same
173 * number of columns. In this case, the operation is done component-
174 * wise resulting in the same size matrix."
175 * * The operator is multiply (*), where both operands are matrices or
176 * one operand is a vector and the other a matrix. A right vector
177 * operand is treated as a column vector and a left vector operand as a
178 * row vector. In all these cases, it is required that the number of
179 * columns of the left operand is equal to the number of rows of the
180 * right operand. Then, the multiply (*) operation does a linear
181 * algebraic multiply, yielding an object that has the same number of
182 * rows as the left operand and the same number of columns as the right
183 * operand. Section 5.10 "Vector and Matrix Operations" explains in
184 * more detail how vectors and matrices are operated on."
187 if (type_a
->is_matrix() && type_b
->is_matrix()
188 && (type_a
->vector_elements
== type_b
->vector_elements
)
189 && (type_a
->matrix_rows
== type_b
->matrix_rows
))
192 return glsl_error_type
;
194 if (type_a
->is_matrix() && type_b
->is_matrix()) {
195 if (type_a
->vector_elements
== type_b
->matrix_rows
) {
197 const struct glsl_type
*t
;
203 if (type_a
->matrix_rows
== type_b
->vector_elements
) {
204 type_name
[3] = '0' + type_a
->matrix_rows
;
207 type_name
[3] = '0' + type_a
->matrix_rows
;
209 type_name
[5] = '0' + type_b
->vector_elements
;
213 t
= state
->symbols
->get_type(type_name
);
214 return (t
!= NULL
) ? t
: glsl_error_type
;
216 } else if (type_a
->is_matrix()) {
217 /* A is a matrix and B is a column vector. Columns of A must match
220 if (type_a
->vector_elements
== type_b
->vector_elements
)
223 assert(type_b
->is_matrix());
225 /* A is a row vector and B is a matrix. Columns of A must match
228 if (type_a
->vector_elements
== type_b
->matrix_rows
)
234 /* "All other cases are illegal."
236 return glsl_error_type
;
240 static const struct glsl_type
*
241 unary_arithmetic_result_type(const struct glsl_type
*type
)
243 /* From GLSL 1.50 spec, page 57:
245 * "The arithmetic unary operators negate (-), post- and pre-increment
246 * and decrement (-- and ++) operate on integer or floating-point
247 * values (including vectors and matrices). All unary operators work
248 * component-wise on their operands. These result with the same type
251 if (!is_numeric_base_type(type
->base_type
))
252 return glsl_error_type
;
258 static const struct glsl_type
*
259 modulus_result_type(const struct glsl_type
*type_a
,
260 const struct glsl_type
*type_b
)
262 /* From GLSL 1.50 spec, page 56:
263 * "The operator modulus (%) operates on signed or unsigned integers or
264 * integer vectors. The operand types must both be signed or both be
267 if (! is_integer_base_type(type_a
->base_type
)
268 || ! is_integer_base_type(type_b
->base_type
)
269 || (type_a
->base_type
!= type_b
->base_type
)) {
270 return glsl_error_type
;
273 /* "The operands cannot be vectors of differing size. If one operand is
274 * a scalar and the other vector, then the scalar is applied component-
275 * wise to the vector, resulting in the same type as the vector. If both
276 * are vectors of the same size, the result is computed component-wise."
278 if (type_a
->is_vector()) {
279 if (!type_b
->is_vector()
280 || (type_a
->vector_elements
== type_b
->vector_elements
))
285 /* "The operator modulus (%) is not defined for any other data types
286 * (non-integer types)."
288 return glsl_error_type
;
292 static const struct glsl_type
*
293 relational_result_type(const struct glsl_type
*type_a
,
294 const struct glsl_type
*type_b
,
295 struct _mesa_glsl_parse_state
*state
)
297 /* From GLSL 1.50 spec, page 56:
298 * "The relational operators greater than (>), less than (<), greater
299 * than or equal (>=), and less than or equal (<=) operate only on
300 * scalar integer and scalar floating-point expressions."
302 if (! is_numeric_base_type(type_a
->base_type
)
303 || ! is_numeric_base_type(type_b
->base_type
)
304 || !type_a
->is_scalar()
305 || !type_b
->is_scalar())
306 return glsl_error_type
;
308 /* "Either the operands' types must match, or the conversions from
309 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
310 * operand, after which the types must match."
312 * This conversion was added in GLSL 1.20. If the compilation mode is
313 * GLSL 1.10, the conversion is skipped.
315 if (state
->language_version
>= 120) {
316 if ((type_a
->base_type
== GLSL_TYPE_FLOAT
)
317 && (type_b
->base_type
!= GLSL_TYPE_FLOAT
)) {
318 /* FINISHME: Generate the implicit type conversion. */
319 } else if ((type_a
->base_type
!= GLSL_TYPE_FLOAT
)
320 && (type_b
->base_type
== GLSL_TYPE_FLOAT
)) {
321 /* FINISHME: Generate the implicit type conversion. */
325 if (type_a
->base_type
!= type_b
->base_type
)
326 return glsl_error_type
;
328 /* "The result is scalar Boolean."
330 return glsl_bool_type
;
335 * Validates that a value can be assigned to a location with a specified type
337 * Validates that \c rhs can be assigned to some location. If the types are
338 * not an exact match but an automatic conversion is possible, \c rhs will be
342 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
343 * Otherwise the actual RHS to be assigned will be returned. This may be
344 * \c rhs, or it may be \c rhs after some type conversion.
347 * In addition to being used for assignments, this function is used to
348 * type-check return values.
351 validate_assignment(const glsl_type
*lhs_type
, ir_instruction
*rhs
)
353 const glsl_type
*const rhs_type
= rhs
->type
;
355 /* If there is already some error in the RHS, just return it. Anything
356 * else will lead to an avalanche of error message back to the user.
358 if (rhs_type
->is_error())
361 /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
363 /* If the types are identical, the assignment can trivially proceed.
365 if (rhs_type
== lhs_type
)
368 /* FINISHME: Check for and apply automatic conversions. */
374 ast_node::hir(exec_list
*instructions
,
375 struct _mesa_glsl_parse_state
*state
)
385 ast_expression::hir(exec_list
*instructions
,
386 struct _mesa_glsl_parse_state
*state
)
388 static const int operations
[AST_NUM_OPERATORS
] = {
389 -1, /* ast_assign doesn't convert to ir_expression. */
390 -1, /* ast_plus doesn't convert to ir_expression. */
414 /* Note: The following block of expression types actually convert
415 * to multiple IR instructions.
417 ir_binop_mul
, /* ast_mul_assign */
418 ir_binop_div
, /* ast_div_assign */
419 ir_binop_mod
, /* ast_mod_assign */
420 ir_binop_add
, /* ast_add_assign */
421 ir_binop_sub
, /* ast_sub_assign */
422 ir_binop_lshift
, /* ast_ls_assign */
423 ir_binop_rshift
, /* ast_rs_assign */
424 ir_binop_bit_and
, /* ast_and_assign */
425 ir_binop_bit_xor
, /* ast_xor_assign */
426 ir_binop_bit_or
, /* ast_or_assign */
428 -1, /* ast_conditional doesn't convert to ir_expression. */
429 -1, /* ast_pre_inc doesn't convert to ir_expression. */
430 -1, /* ast_pre_dec doesn't convert to ir_expression. */
431 -1, /* ast_post_inc doesn't convert to ir_expression. */
432 -1, /* ast_post_dec doesn't convert to ir_expression. */
433 -1, /* ast_field_selection doesn't conv to ir_expression. */
434 -1, /* ast_array_index doesn't convert to ir_expression. */
435 -1, /* ast_function_call doesn't conv to ir_expression. */
436 -1, /* ast_identifier doesn't convert to ir_expression. */
437 -1, /* ast_int_constant doesn't convert to ir_expression. */
438 -1, /* ast_uint_constant doesn't conv to ir_expression. */
439 -1, /* ast_float_constant doesn't conv to ir_expression. */
440 -1, /* ast_bool_constant doesn't conv to ir_expression. */
441 -1, /* ast_sequence doesn't convert to ir_expression. */
443 ir_instruction
*result
= NULL
;
444 ir_instruction
*op
[2];
445 struct simple_node op_list
;
446 const struct glsl_type
*type
= glsl_error_type
;
447 bool error_emitted
= false;
450 loc
= this->get_location();
451 make_empty_list(& op_list
);
453 switch (this->oper
) {
455 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
456 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
458 error_emitted
= ((op
[0]->type
== glsl_error_type
)
459 || (op
[1]->type
== glsl_error_type
));
462 if (!error_emitted
) {
465 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
466 loc
= this->subexpressions
[0]->get_location();
467 if (op
[0]->mode
!= ir_op_dereference
) {
468 _mesa_glsl_error(& loc
, state
, "invalid lvalue in assignment");
469 error_emitted
= true;
471 type
= glsl_error_type
;
473 const struct ir_dereference
*const ref
=
474 (struct ir_dereference
*) op
[0];
475 const struct ir_variable
*const var
=
476 (struct ir_variable
*) ref
->var
;
479 && (var
->mode
== ir_op_var_decl
)
480 && (var
->read_only
)) {
481 _mesa_glsl_error(& loc
, state
, "cannot assign to read-only "
482 "variable `%s'", var
->name
);
483 error_emitted
= true;
485 type
= glsl_error_type
;
490 ir_instruction
*rhs
= validate_assignment(op
[0]->type
, op
[1]);
492 type
= glsl_error_type
;
496 ir_instruction
*tmp
= new ir_assignment(op
[0], op
[1], NULL
);
497 instructions
->push_tail(tmp
);
504 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
506 error_emitted
= (op
[0]->type
== glsl_error_type
);
507 if (type
== glsl_error_type
)
514 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
516 type
= unary_arithmetic_result_type(op
[0]->type
);
518 error_emitted
= (op
[0]->type
== glsl_error_type
);
520 result
= new ir_expression(operations
[this->oper
], type
,
528 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
529 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
531 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
532 (this->oper
== ast_mul
),
535 result
= new ir_expression(operations
[this->oper
], type
,
540 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
541 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
543 error_emitted
= ((op
[0]->type
== glsl_error_type
)
544 || (op
[1]->type
== glsl_error_type
));
546 type
= modulus_result_type(op
[0]->type
, op
[1]->type
);
548 assert(operations
[this->oper
] == ir_binop_mod
);
550 result
= new ir_expression(operations
[this->oper
], type
,
556 /* FINISHME: Implement bit-shift operators. */
563 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
564 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
566 error_emitted
= ((op
[0]->type
== glsl_error_type
)
567 || (op
[1]->type
== glsl_error_type
));
569 type
= relational_result_type(op
[0]->type
, op
[1]->type
, state
);
571 /* The relational operators must either generate an error or result
572 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
574 assert((type
== glsl_error_type
)
575 || ((type
->base_type
== GLSL_TYPE_BOOL
)
576 && type
->is_scalar()));
578 result
= new ir_expression(operations
[this->oper
], type
,
584 /* FINISHME: Implement equality operators. */
591 /* FINISHME: Implement bit-wise operators. */
598 /* FINISHME: Implement logical operators. */
604 case ast_sub_assign
: {
605 struct ir_instruction
*temp_rhs
;
607 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
608 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
610 error_emitted
= ((op
[0]->type
== glsl_error_type
)
611 || (op
[1]->type
== glsl_error_type
));
613 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
614 (this->oper
== ast_mul_assign
),
617 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
620 /* FINISHME: Check that the LHS is assignable. */
622 /* We still have to test that the LHS and RHS have matching type. For
623 * example, the following GLSL code should generate a type error:
625 * mat4 m; vec4 v; m *= v;
627 * The type of (m*v) is a vec4, but the type of m is a mat4.
629 * FINISHME: Is multiplication between a matrix and a vector the only
630 * FINISHME: case that resuls in mismatched types?
632 /* FINISHME: Check that the LHS and RHS have matching types. */
634 /* GLSL 1.10 does not allow array assignment. However, we don't have to
635 * explicitly test for this because none of the binary expression
636 * operators allow array operands either.
639 /* FINISHME: This is wrong. The operation should assign to a new
640 * FINISHME: temporary. This assignment should then be added to the
641 * FINISHME: instruction list. Another assignment to the real
642 * FINISHME: destination should be generated. The temporary should then
643 * FINISHME: be returned as the r-value.
645 result
= new ir_assignment(op
[0], temp_rhs
, NULL
);
658 case ast_conditional
:
667 case ast_field_selection
:
668 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
672 case ast_array_index
:
675 case ast_function_call
:
676 /* Should *NEVER* get here. ast_function_call should always be handled
677 * by ast_function_expression::hir.
682 case ast_identifier
: {
683 /* ast_identifier can appear several places in a full abstract syntax
684 * tree. This particular use must be at location specified in the grammar
685 * as 'variable_identifier'.
688 state
->symbols
->get_variable(this->primary_expression
.identifier
);
690 result
= new ir_dereference(var
);
695 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
696 this->primary_expression
.identifier
);
698 error_emitted
= true;
703 case ast_int_constant
:
704 type
= glsl_int_type
;
705 result
= new ir_constant(type
, & this->primary_expression
);
708 case ast_uint_constant
:
709 type
= glsl_uint_type
;
710 result
= new ir_constant(type
, & this->primary_expression
);
713 case ast_float_constant
:
714 type
= glsl_float_type
;
715 result
= new ir_constant(type
, & this->primary_expression
);
718 case ast_bool_constant
:
719 type
= glsl_bool_type
;
720 result
= new ir_constant(type
, & this->primary_expression
);
724 struct simple_node
*ptr
;
726 /* It should not be possible to generate a sequence in the AST without
727 * any expressions in it.
729 assert(!is_empty_list(&this->expressions
));
731 /* The r-value of a sequence is the last expression in the sequence. If
732 * the other expressions in the sequence do not have side-effects (and
733 * therefore add instructions to the instruction list), they get dropped
736 foreach (ptr
, &this->expressions
)
737 result
= ((ast_node
*)ptr
)->hir(instructions
, state
);
741 /* Any errors should have already been emitted in the loop above.
743 error_emitted
= true;
748 if (is_error_type(type
) && !error_emitted
)
749 _mesa_glsl_error(& loc
, state
, "type mismatch");
756 ast_expression_statement::hir(exec_list
*instructions
,
757 struct _mesa_glsl_parse_state
*state
)
759 /* It is possible to have expression statements that don't have an
760 * expression. This is the solitary semicolon:
762 * for (i = 0; i < 5; i++)
765 * In this case the expression will be NULL. Test for NULL and don't do
766 * anything in that case.
768 if (expression
!= NULL
)
769 expression
->hir(instructions
, state
);
771 /* Statements do not have r-values.
778 ast_compound_statement::hir(exec_list
*instructions
,
779 struct _mesa_glsl_parse_state
*state
)
781 struct simple_node
*ptr
;
785 state
->symbols
->push_scope();
787 foreach (ptr
, &statements
)
788 ((ast_node
*)ptr
)->hir(instructions
, state
);
791 state
->symbols
->pop_scope();
793 /* Compound statements do not have r-values.
799 static const struct glsl_type
*
800 type_specifier_to_glsl_type(const struct ast_type_specifier
*spec
,
802 struct _mesa_glsl_parse_state
*state
)
804 struct glsl_type
*type
;
806 if (spec
->type_specifier
== ast_struct
) {
807 /* FINISHME: Handle annonymous structures. */
810 type
= state
->symbols
->get_type(spec
->type_name
);
811 *name
= spec
->type_name
;
813 /* FINISHME: Handle array declarations. Note that this requires complete
814 * FINISHME: handling of constant expressions.
823 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
824 struct ir_variable
*var
,
825 struct _mesa_glsl_parse_state
*state
)
830 /* FINISHME: Mark 'in' variables at global scope as read-only. */
831 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
832 || (qual
->varying
&& (state
->target
== fragment_shader
)))
838 if (qual
->in
&& qual
->out
)
839 var
->mode
= ir_var_inout
;
840 else if (qual
->attribute
|| qual
->in
841 || (qual
->varying
&& (state
->target
== fragment_shader
)))
842 var
->mode
= ir_var_in
;
843 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
844 var
->mode
= ir_var_out
;
845 else if (qual
->uniform
)
846 var
->mode
= ir_var_uniform
;
848 var
->mode
= ir_var_auto
;
851 var
->interpolation
= ir_var_flat
;
852 else if (qual
->noperspective
)
853 var
->interpolation
= ir_var_noperspective
;
855 var
->interpolation
= ir_var_smooth
;
860 ast_declarator_list::hir(exec_list
*instructions
,
861 struct _mesa_glsl_parse_state
*state
)
863 struct simple_node
*ptr
;
864 const struct glsl_type
*decl_type
;
865 const char *type_name
= NULL
;
868 /* FINISHME: Handle vertex shader "invariant" declarations that do not
869 * FINISHME: include a type. These re-declare built-in variables to be
870 * FINISHME: invariant.
873 decl_type
= type_specifier_to_glsl_type(this->type
->specifier
,
876 foreach (ptr
, &this->declarations
) {
877 struct ast_declaration
*const decl
= (struct ast_declaration
* )ptr
;
878 const struct glsl_type
*var_type
;
879 struct ir_variable
*var
;
882 /* FINISHME: Emit a warning if a variable declaration shadows a
883 * FINISHME: declaration at a higher scope.
886 if ((decl_type
== NULL
) || decl_type
->is_void()) {
889 loc
= this->get_location();
890 if (type_name
!= NULL
) {
891 _mesa_glsl_error(& loc
, state
,
892 "invalid type `%s' in declaration of `%s'",
893 type_name
, decl
->identifier
);
895 _mesa_glsl_error(& loc
, state
,
896 "invalid type in declaration of `%s'",
902 if (decl
->is_array
) {
903 /* FINISHME: Handle array declarations. Note that this requires
904 * FINISHME: complete handling of constant expressions.
907 /* FINISHME: Reject delcarations of multidimensional arrays. */
909 var_type
= decl_type
;
912 var
= new ir_variable(var_type
, decl
->identifier
);
914 /* FINISHME: Variables that are attribute, uniform, varying, in, or
915 * FINISHME: out varibles must be declared either at global scope or
916 * FINISHME: in a parameter list (in and out only).
919 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
);
921 /* Attempt to add the variable to the symbol table. If this fails, it
922 * means the variable has already been declared at this scope.
924 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
925 YYLTYPE loc
= this->get_location();
927 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
932 const bool added_variable
=
933 state
->symbols
->add_variable(decl
->identifier
, var
);
934 assert(added_variable
);
936 instructions
->push_tail(var
);
938 /* FINISHME: Process the declaration initializer. */
941 /* Variable declarations do not have r-values.
948 ast_parameter_declarator::hir(exec_list
*instructions
,
949 struct _mesa_glsl_parse_state
*state
)
951 const struct glsl_type
*type
;
952 const char *name
= NULL
;
955 type
= type_specifier_to_glsl_type(this->type
->specifier
, & name
, state
);
958 YYLTYPE loc
= this->get_location();
960 _mesa_glsl_error(& loc
, state
,
961 "invalid type `%s' in declaration of `%s'",
962 name
, this->identifier
);
964 _mesa_glsl_error(& loc
, state
,
965 "invalid type in declaration of `%s'",
969 type
= glsl_error_type
;
972 ir_variable
*var
= new ir_variable(type
, this->identifier
);
974 /* FINISHME: Handle array declarations. Note that this requires
975 * FINISHME: complete handling of constant expressions.
978 /* Apply any specified qualifiers to the parameter declaration. Note that
979 * for function parameters the default mode is 'in'.
981 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
);
982 if (var
->mode
== ir_var_auto
)
983 var
->mode
= ir_var_in
;
985 instructions
->push_tail(var
);
987 /* Parameter declarations do not have r-values.
994 ast_function_parameters_to_hir(struct simple_node
*ast_parameters
,
995 exec_list
*ir_parameters
,
996 struct _mesa_glsl_parse_state
*state
)
998 struct simple_node
*ptr
;
1000 foreach (ptr
, ast_parameters
) {
1001 ((ast_node
*)ptr
)->hir(ir_parameters
, state
);
1007 parameter_lists_match(exec_list
*list_a
, exec_list
*list_b
)
1009 exec_list_iterator iter_a
= list_a
->iterator();
1010 exec_list_iterator iter_b
= list_b
->iterator();
1012 while (iter_a
.has_next()) {
1013 /* If all of the parameters from the other parameter list have been
1014 * exhausted, the lists have different length and, by definition,
1017 if (!iter_b
.has_next())
1020 /* If the types of the parameters do not match, the parameters lists
1035 ast_function_definition::hir(exec_list
*instructions
,
1036 struct _mesa_glsl_parse_state
*state
)
1039 ir_function_signature
*signature
= NULL
;
1040 ir_function
*f
= NULL
;
1041 exec_list parameters
;
1044 /* Convert the list of function parameters to HIR now so that they can be
1045 * used below to compare this function's signature with previously seen
1046 * signatures for functions with the same name.
1048 ast_function_parameters_to_hir(& this->prototype
->parameters
, & parameters
,
1051 const char *return_type_name
;
1052 const glsl_type
*return_type
=
1053 type_specifier_to_glsl_type(this->prototype
->return_type
->specifier
,
1054 & return_type_name
, state
);
1056 assert(return_type
!= NULL
);
1059 /* Verify that this function's signature either doesn't match a previously
1060 * seen signature for a function with the same name, or, if a match is found,
1061 * that the previously seen signature does not have an associated definition.
1063 const char *const name
= this->prototype
->identifier
;
1064 f
= state
->symbols
->get_function(name
);
1066 foreach_iter(exec_list_iterator
, iter
, f
->signatures
) {
1067 signature
= (struct ir_function_signature
*) iter
.get();
1069 /* Compare the parameter list of the function being defined to the
1070 * existing function. If the parameter lists match, then the return
1071 * type must also match and the existing function must not have a
1074 if (parameter_lists_match(& parameters
, & signature
->parameters
)) {
1075 /* FINISHME: Compare return types. */
1077 if (signature
->definition
!= NULL
) {
1078 YYLTYPE loc
= this->get_location();
1080 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
1089 } else if (state
->symbols
->name_declared_this_scope(name
)) {
1090 /* This function name shadows a non-function use of the same name.
1092 YYLTYPE loc
= this->get_location();
1094 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
1095 "non-function", name
);
1098 f
= new ir_function(name
);
1099 state
->symbols
->add_function(f
->name
, f
);
1103 /* Finish storing the information about this new function in its signature.
1105 if (signature
== NULL
) {
1106 signature
= new ir_function_signature(return_type
);
1107 f
->signatures
.push_tail(signature
);
1109 /* Destroy all of the previous parameter information. The previous
1110 * parameter information comes from the function prototype, and it can
1111 * either include invalid parameter names or may not have names at all.
1113 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
1114 assert(((struct ir_instruction
*)iter
.get())->mode
== ir_op_var_decl
);
1122 assert(state
->current_function
== NULL
);
1123 state
->current_function
= signature
;
1125 ast_function_parameters_to_hir(& this->prototype
->parameters
,
1126 & signature
->parameters
,
1128 /* FINISHME: Set signature->return_type */
1130 label
= new ir_label(name
);
1131 if (signature
->definition
== NULL
) {
1132 signature
->definition
= label
;
1134 instructions
->push_tail(label
);
1136 /* Add the function parameters to the symbol table. During this step the
1137 * parameter declarations are also moved from the temporary "parameters" list
1138 * to the instruction list. There are other more efficient ways to do this,
1139 * but they involve ugly linked-list gymnastics.
1141 state
->symbols
->push_scope();
1142 foreach_iter(exec_list_iterator
, iter
, parameters
) {
1143 ir_variable
*const var
= (ir_variable
*) iter
.get();
1145 assert(((ir_instruction
*)var
)->mode
== ir_op_var_decl
);
1148 instructions
->push_tail(var
);
1150 /* The only way a parameter would "exist" is if two parameters have
1153 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
1154 YYLTYPE loc
= this->get_location();
1156 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
1158 state
->symbols
->add_variable(var
->name
, var
);
1162 /* Convert the body of the function to HIR, and append the resulting
1163 * instructions to the list that currently consists of the function label
1164 * and the function parameters.
1166 this->body
->hir(instructions
, state
);
1168 state
->symbols
->pop_scope();
1170 assert(state
->current_function
== signature
);
1171 state
->current_function
= NULL
;
1173 /* Function definitions do not have r-values.
1180 ast_jump_statement::hir(exec_list
*instructions
,
1181 struct _mesa_glsl_parse_state
*state
)
1184 if (mode
== ast_return
) {
1187 if (opt_return_value
) {
1188 /* FINISHME: Make sure the enclosing function has a non-void return
1192 ir_expression
*const ret
= (ir_expression
*)
1193 opt_return_value
->hir(instructions
, state
);
1194 assert(ret
!= NULL
);
1196 /* FINISHME: Make sure the type of the return value matches the return
1197 * FINISHME: type of the enclosing function.
1200 inst
= new ir_return(ret
);
1202 /* FINISHME: Make sure the enclosing function has a void return type.
1204 inst
= new ir_return
;
1207 instructions
->push_tail(inst
);
1210 /* Jump instructions do not have r-values.