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 ast_node::hir(exec_list
*instructions
,
336 struct _mesa_glsl_parse_state
*state
)
346 ast_expression::hir(exec_list
*instructions
,
347 struct _mesa_glsl_parse_state
*state
)
349 static const int operations
[AST_NUM_OPERATORS
] = {
350 -1, /* ast_assign doesn't convert to ir_expression. */
351 -1, /* ast_plus doesn't convert to ir_expression. */
375 /* Note: The following block of expression types actually convert
376 * to multiple IR instructions.
378 ir_binop_mul
, /* ast_mul_assign */
379 ir_binop_div
, /* ast_div_assign */
380 ir_binop_mod
, /* ast_mod_assign */
381 ir_binop_add
, /* ast_add_assign */
382 ir_binop_sub
, /* ast_sub_assign */
383 ir_binop_lshift
, /* ast_ls_assign */
384 ir_binop_rshift
, /* ast_rs_assign */
385 ir_binop_bit_and
, /* ast_and_assign */
386 ir_binop_bit_xor
, /* ast_xor_assign */
387 ir_binop_bit_or
, /* ast_or_assign */
389 -1, /* ast_conditional doesn't convert to ir_expression. */
390 -1, /* ast_pre_inc doesn't convert to ir_expression. */
391 -1, /* ast_pre_dec doesn't convert to ir_expression. */
392 -1, /* ast_post_inc doesn't convert to ir_expression. */
393 -1, /* ast_post_dec doesn't convert to ir_expression. */
394 -1, /* ast_field_selection doesn't conv to ir_expression. */
395 -1, /* ast_array_index doesn't convert to ir_expression. */
396 -1, /* ast_function_call doesn't conv to ir_expression. */
397 -1, /* ast_identifier doesn't convert to ir_expression. */
398 -1, /* ast_int_constant doesn't convert to ir_expression. */
399 -1, /* ast_uint_constant doesn't conv to ir_expression. */
400 -1, /* ast_float_constant doesn't conv to ir_expression. */
401 -1, /* ast_bool_constant doesn't conv to ir_expression. */
402 -1, /* ast_sequence doesn't convert to ir_expression. */
404 ir_instruction
*result
= NULL
;
405 ir_instruction
*op
[2];
406 struct simple_node op_list
;
407 const struct glsl_type
*type
= glsl_error_type
;
408 bool error_emitted
= false;
411 loc
= this->get_location();
412 make_empty_list(& op_list
);
414 switch (this->oper
) {
416 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
417 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
419 error_emitted
= ((op
[0]->type
== glsl_error_type
)
420 || (op
[1]->type
== glsl_error_type
));
423 if (!error_emitted
) {
426 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
427 loc
= this->subexpressions
[0]->get_location();
428 if (op
[0]->mode
!= ir_op_dereference
) {
429 _mesa_glsl_error(& loc
, state
, "invalid lvalue in assignment");
430 error_emitted
= true;
432 type
= glsl_error_type
;
434 const struct ir_dereference
*const ref
=
435 (struct ir_dereference
*) op
[0];
436 const struct ir_variable
*const var
=
437 (struct ir_variable
*) ref
->var
;
440 && (var
->mode
== ir_op_var_decl
)
441 && (var
->read_only
)) {
442 _mesa_glsl_error(& loc
, state
, "cannot assign to read-only "
443 "variable `%s'", var
->name
);
444 error_emitted
= true;
446 type
= glsl_error_type
;
451 /* FINISHME: Check that the LHS and RHS have matching types. */
452 /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
454 ir_instruction
*tmp
= new ir_assignment(op
[0], op
[1], NULL
);
455 instructions
->push_tail(tmp
);
462 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
464 error_emitted
= (op
[0]->type
== glsl_error_type
);
465 if (type
== glsl_error_type
)
472 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
474 type
= unary_arithmetic_result_type(op
[0]->type
);
476 error_emitted
= (op
[0]->type
== glsl_error_type
);
478 result
= new ir_expression(operations
[this->oper
], type
,
486 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
487 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
489 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
490 (this->oper
== ast_mul
),
493 result
= new ir_expression(operations
[this->oper
], type
,
498 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
499 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
501 error_emitted
= ((op
[0]->type
== glsl_error_type
)
502 || (op
[1]->type
== glsl_error_type
));
504 type
= modulus_result_type(op
[0]->type
, op
[1]->type
);
506 assert(operations
[this->oper
] == ir_binop_mod
);
508 result
= new ir_expression(operations
[this->oper
], type
,
514 /* FINISHME: Implement bit-shift operators. */
521 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
522 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
524 error_emitted
= ((op
[0]->type
== glsl_error_type
)
525 || (op
[1]->type
== glsl_error_type
));
527 type
= relational_result_type(op
[0]->type
, op
[1]->type
, state
);
529 /* The relational operators must either generate an error or result
530 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
532 assert((type
== glsl_error_type
)
533 || ((type
->base_type
== GLSL_TYPE_BOOL
)
534 && type
->is_scalar()));
536 result
= new ir_expression(operations
[this->oper
], type
,
542 /* FINISHME: Implement equality operators. */
549 /* FINISHME: Implement bit-wise operators. */
556 /* FINISHME: Implement logical operators. */
562 case ast_sub_assign
: {
563 struct ir_instruction
*temp_rhs
;
565 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
566 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
568 error_emitted
= ((op
[0]->type
== glsl_error_type
)
569 || (op
[1]->type
== glsl_error_type
));
571 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
572 (this->oper
== ast_mul_assign
),
575 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
578 /* FINISHME: Check that the LHS is assignable. */
580 /* We still have to test that the LHS and RHS have matching type. For
581 * example, the following GLSL code should generate a type error:
583 * mat4 m; vec4 v; m *= v;
585 * The type of (m*v) is a vec4, but the type of m is a mat4.
587 * FINISHME: Is multiplication between a matrix and a vector the only
588 * FINISHME: case that resuls in mismatched types?
590 /* FINISHME: Check that the LHS and RHS have matching types. */
592 /* GLSL 1.10 does not allow array assignment. However, we don't have to
593 * explicitly test for this because none of the binary expression
594 * operators allow array operands either.
597 /* FINISHME: This is wrong. The operation should assign to a new
598 * FINISHME: temporary. This assignment should then be added to the
599 * FINISHME: instruction list. Another assignment to the real
600 * FINISHME: destination should be generated. The temporary should then
601 * FINISHME: be returned as the r-value.
603 result
= new ir_assignment(op
[0], temp_rhs
, NULL
);
616 case ast_conditional
:
625 case ast_field_selection
:
626 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
630 case ast_array_index
:
633 case ast_function_call
:
634 /* Should *NEVER* get here. ast_function_call should always be handled
635 * by ast_function_expression::hir.
640 case ast_identifier
: {
641 /* ast_identifier can appear several places in a full abstract syntax
642 * tree. This particular use must be at location specified in the grammar
643 * as 'variable_identifier'.
646 state
->symbols
->get_variable(this->primary_expression
.identifier
);
648 result
= new ir_dereference(var
);
653 _mesa_glsl_error(& loc
, NULL
, "`%s' undeclared",
654 this->primary_expression
.identifier
);
656 error_emitted
= true;
661 case ast_int_constant
:
662 type
= glsl_int_type
;
663 result
= new ir_constant(type
, & this->primary_expression
);
666 case ast_uint_constant
:
667 type
= glsl_uint_type
;
668 result
= new ir_constant(type
, & this->primary_expression
);
671 case ast_float_constant
:
672 type
= glsl_float_type
;
673 result
= new ir_constant(type
, & this->primary_expression
);
676 case ast_bool_constant
:
677 type
= glsl_bool_type
;
678 result
= new ir_constant(type
, & this->primary_expression
);
682 struct simple_node
*ptr
;
684 /* It should not be possible to generate a sequence in the AST without
685 * any expressions in it.
687 assert(!is_empty_list(&this->expressions
));
689 /* The r-value of a sequence is the last expression in the sequence. If
690 * the other expressions in the sequence do not have side-effects (and
691 * therefore add instructions to the instruction list), they get dropped
694 foreach (ptr
, &this->expressions
)
695 result
= ((ast_node
*)ptr
)->hir(instructions
, state
);
699 /* Any errors should have already been emitted in the loop above.
701 error_emitted
= true;
706 if (is_error_type(type
) && !error_emitted
)
707 _mesa_glsl_error(& loc
, NULL
, "type mismatch");
714 ast_expression_statement::hir(exec_list
*instructions
,
715 struct _mesa_glsl_parse_state
*state
)
717 /* It is possible to have expression statements that don't have an
718 * expression. This is the solitary semicolon:
720 * for (i = 0; i < 5; i++)
723 * In this case the expression will be NULL. Test for NULL and don't do
724 * anything in that case.
726 if (expression
!= NULL
)
727 expression
->hir(instructions
, state
);
729 /* Statements do not have r-values.
736 ast_compound_statement::hir(exec_list
*instructions
,
737 struct _mesa_glsl_parse_state
*state
)
739 struct simple_node
*ptr
;
743 state
->symbols
->push_scope();
745 foreach (ptr
, &statements
)
746 ((ast_node
*)ptr
)->hir(instructions
, state
);
749 state
->symbols
->pop_scope();
751 /* Compound statements do not have r-values.
757 static const struct glsl_type
*
758 type_specifier_to_glsl_type(const struct ast_type_specifier
*spec
,
760 struct _mesa_glsl_parse_state
*state
)
762 struct glsl_type
*type
;
764 if (spec
->type_specifier
== ast_struct
) {
765 /* FINISHME: Handle annonymous structures. */
768 type
= state
->symbols
->get_type(spec
->type_name
);
769 *name
= spec
->type_name
;
771 /* FINISHME: Handle array declarations. Note that this requires complete
772 * FINSIHME: handling of constant expressions.
781 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
782 struct ir_variable
*var
,
783 struct _mesa_glsl_parse_state
*state
)
788 /* FINISHME: Mark 'in' variables at global scope as read-only. */
789 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
790 || (qual
->varying
&& (state
->target
== fragment_shader
)))
796 if (qual
->in
&& qual
->out
)
797 var
->mode
= ir_var_inout
;
798 else if (qual
->attribute
|| qual
->in
799 || (qual
->varying
&& (state
->target
== fragment_shader
)))
800 var
->mode
= ir_var_in
;
801 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
802 var
->mode
= ir_var_out
;
803 else if (qual
->uniform
)
804 var
->mode
= ir_var_uniform
;
806 var
->mode
= ir_var_auto
;
809 var
->interpolation
= ir_var_flat
;
810 else if (qual
->noperspective
)
811 var
->interpolation
= ir_var_noperspective
;
813 var
->interpolation
= ir_var_smooth
;
818 ast_declarator_list::hir(exec_list
*instructions
,
819 struct _mesa_glsl_parse_state
*state
)
821 struct simple_node
*ptr
;
822 const struct glsl_type
*decl_type
;
823 const char *type_name
= NULL
;
826 /* FINISHME: Handle vertex shader "invariant" declarations that do not
827 * FINISHME: include a type. These re-declare built-in variables to be
828 * FINISHME: invariant.
831 decl_type
= type_specifier_to_glsl_type(this->type
->specifier
,
834 foreach (ptr
, &this->declarations
) {
835 struct ast_declaration
*const decl
= (struct ast_declaration
* )ptr
;
836 const struct glsl_type
*var_type
;
837 struct ir_variable
*var
;
840 /* FINISHME: Emit a warning if a variable declaration shadows a
841 * FINISHME: declaration at a higher scope.
844 if (decl_type
== NULL
) {
847 loc
= this->get_location();
848 if (type_name
!= NULL
) {
849 _mesa_glsl_error(& loc
, state
,
850 "invalid type `%s' in declaration of `%s'",
851 type_name
, decl
->identifier
);
853 _mesa_glsl_error(& loc
, state
,
854 "invalid type in declaration of `%s'",
860 if (decl
->is_array
) {
861 /* FINISHME: Handle array declarations. Note that this requires
862 * FINISHME: complete handling of constant expressions.
865 /* FINISHME: Reject delcarations of multidimensional arrays. */
867 var_type
= decl_type
;
870 var
= new ir_variable(var_type
, decl
->identifier
);
872 /* FINSIHME: Variables that are attribute, uniform, varying, in, or
873 * FINISHME: out varibles must be declared either at global scope or
874 * FINISHME: in a parameter list (in and out only).
877 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
);
879 /* Attempt to add the variable to the symbol table. If this fails, it
880 * means the variable has already been declared at this scope.
882 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
883 YYLTYPE loc
= this->get_location();
885 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
890 const bool added_variable
=
891 state
->symbols
->add_variable(decl
->identifier
, var
);
892 assert(added_variable
);
894 instructions
->push_tail(var
);
896 /* FINISHME: Process the declaration initializer. */
899 /* Variable declarations do not have r-values.
906 ast_parameter_declarator::hir(exec_list
*instructions
,
907 struct _mesa_glsl_parse_state
*state
)
909 const struct glsl_type
*type
;
910 const char *name
= NULL
;
913 type
= type_specifier_to_glsl_type(this->type
->specifier
, & name
, state
);
916 YYLTYPE loc
= this->get_location();
918 _mesa_glsl_error(& loc
, state
,
919 "invalid type `%s' in declaration of `%s'",
920 name
, this->identifier
);
922 _mesa_glsl_error(& loc
, state
,
923 "invalid type in declaration of `%s'",
927 type
= glsl_error_type
;
930 ir_variable
*var
= new ir_variable(type
, this->identifier
);
932 /* FINISHME: Handle array declarations. Note that this requires
933 * FINISHME: complete handling of constant expressions.
936 /* Apply any specified qualifiers to the parameter declaration. Note that
937 * for function parameters the default mode is 'in'.
939 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
);
940 if (var
->mode
== ir_var_auto
)
941 var
->mode
= ir_var_in
;
943 instructions
->push_tail(var
);
945 /* Parameter declarations do not have r-values.
952 ast_function_parameters_to_hir(struct simple_node
*ast_parameters
,
953 exec_list
*ir_parameters
,
954 struct _mesa_glsl_parse_state
*state
)
956 struct simple_node
*ptr
;
958 foreach (ptr
, ast_parameters
) {
959 ((ast_node
*)ptr
)->hir(ir_parameters
, state
);
965 parameter_lists_match(exec_list
*list_a
, exec_list
*list_b
)
967 exec_list_iterator iter_a
= list_a
->iterator();
968 exec_list_iterator iter_b
= list_b
->iterator();
970 while (iter_a
.has_next()) {
971 /* If all of the parameters from the other parameter list have been
972 * exhausted, the lists have different length and, by definition,
975 if (!iter_b
.has_next())
978 /* If the types of the parameters do not match, the parameters lists
993 ast_function_definition::hir(exec_list
*instructions
,
994 struct _mesa_glsl_parse_state
*state
)
997 ir_function_signature
*signature
= NULL
;
998 ir_function
*f
= NULL
;
999 exec_list parameters
;
1002 /* Convert the list of function parameters to HIR now so that they can be
1003 * used below to compare this function's signature with previously seen
1004 * signatures for functions with the same name.
1006 ast_function_parameters_to_hir(& this->prototype
->parameters
, & parameters
,
1010 /* Verify that this function's signature either doesn't match a previously
1011 * seen signature for a function with the same name, or, if a match is found,
1012 * that the previously seen signature does not have an associated definition.
1014 const char *const name
= this->prototype
->identifier
;
1015 f
= state
->symbols
->get_function(name
);
1017 foreach_iter(exec_list_iterator
, iter
, f
->signatures
) {
1018 signature
= (struct ir_function_signature
*) iter
.get();
1020 /* Compare the parameter list of the function being defined to the
1021 * existing function. If the parameter lists match, then the return
1022 * type must also match and the existing function must not have a
1025 if (parameter_lists_match(& parameters
, & signature
->parameters
)) {
1026 /* FINISHME: Compare return types. */
1028 if (signature
->definition
!= NULL
) {
1029 YYLTYPE loc
= this->get_location();
1031 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
1040 } else if (state
->symbols
->name_declared_this_scope(name
)) {
1041 /* This function name shadows a non-function use of the same name.
1043 YYLTYPE loc
= this->get_location();
1045 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
1046 "non-function", name
);
1049 f
= new ir_function();
1052 state
->symbols
->add_function(f
->name
, f
);
1056 /* Finish storing the information about this new function in its signature.
1058 if (signature
== NULL
) {
1059 signature
= new ir_function_signature();
1060 f
->signatures
.push_tail(signature
);
1062 /* Destroy all of the previous parameter information. The previous
1063 * parameter information comes from the function prototype, and it can
1064 * either include invalid parameter names or may not have names at all.
1066 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
1067 assert(((struct ir_instruction
*)iter
.get())->mode
== ir_op_var_decl
);
1075 assert(state
->current_function
== NULL
);
1076 state
->current_function
= signature
;
1078 ast_function_parameters_to_hir(& this->prototype
->parameters
,
1079 & signature
->parameters
,
1081 /* FINISHME: Set signature->return_type */
1083 label
= new ir_label(name
);
1084 if (signature
->definition
== NULL
) {
1085 signature
->definition
= label
;
1087 instructions
->push_tail(label
);
1089 /* Add the function parameters to the symbol table. During this step the
1090 * parameter declarations are also moved from the temporary "parameters" list
1091 * to the instruction list. There are other more efficient ways to do this,
1092 * but they involve ugly linked-list gymnastics.
1094 state
->symbols
->push_scope();
1095 foreach_iter(exec_list_iterator
, iter
, parameters
) {
1096 ir_variable
*const var
= (ir_variable
*) iter
.get();
1098 assert(((ir_instruction
*)var
)->mode
== ir_op_var_decl
);
1101 instructions
->push_tail(var
);
1103 /* The only way a parameter would "exist" is if two parameters have
1106 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
1107 YYLTYPE loc
= this->get_location();
1109 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
1111 state
->symbols
->add_variable(var
->name
, var
);
1115 /* Convert the body of the function to HIR, and append the resulting
1116 * instructions to the list that currently consists of the function label
1117 * and the function parameters.
1119 this->body
->hir(instructions
, state
);
1121 state
->symbols
->pop_scope();
1123 assert(state
->current_function
== signature
);
1124 state
->current_function
= NULL
;
1126 /* Function definitions do not have r-values.
1133 ast_jump_statement::hir(exec_list
*instructions
,
1134 struct _mesa_glsl_parse_state
*state
)
1137 if (mode
== ast_return
) {
1140 if (opt_return_value
) {
1141 /* FINISHME: Make sure the enclosing function has a non-void return
1145 ir_expression
*const ret
= (ir_expression
*)
1146 opt_return_value
->hir(instructions
, state
);
1147 assert(ret
!= NULL
);
1149 /* FINISHME: Make sure the type of the return value matches the return
1150 * FINISHME: type of the enclosing function.
1153 inst
= new ir_return(ret
);
1155 /* FINISHME: Make sure the enclosing function has a void return type.
1157 inst
= new ir_return
;
1160 instructions
->push_tail(inst
);
1163 /* Jump instructions do not have r-values.