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 "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 foreach (ptr
, & state
->translation_unit
) {
67 ((ast_node
*)ptr
)->hir(instructions
, state
);
72 static const struct glsl_type
*
73 arithmetic_result_type(const struct glsl_type
*type_a
,
74 const struct glsl_type
*type_b
,
76 struct _mesa_glsl_parse_state
*state
)
78 /* From GLSL 1.50 spec, page 56:
80 * "The arithmetic binary operators add (+), subtract (-),
81 * multiply (*), and divide (/) operate on integer and
82 * floating-point scalars, vectors, and matrices."
84 if (! is_numeric_base_type(type_a
->base_type
)
85 || ! is_numeric_base_type(type_b
->base_type
)) {
86 return glsl_error_type
;
90 /* "If one operand is floating-point based and the other is
91 * not, then the conversions from Section 4.1.10 "Implicit
92 * Conversions" are applied to the non-floating-point-based operand."
94 * This conversion was added in GLSL 1.20. If the compilation mode is
95 * GLSL 1.10, the conversion is skipped.
97 if (state
->language_version
>= 120) {
98 if ((type_a
->base_type
== GLSL_TYPE_FLOAT
)
99 && (type_b
->base_type
!= GLSL_TYPE_FLOAT
)) {
100 } else if ((type_a
->base_type
!= GLSL_TYPE_FLOAT
)
101 && (type_b
->base_type
== GLSL_TYPE_FLOAT
)) {
105 /* "If the operands are integer types, they must both be signed or
108 * From this rule and the preceeding conversion it can be inferred that
109 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
110 * The is_numeric_base_type check above already filtered out the case
111 * where either type is not one of these, so now the base types need only
112 * be tested for equality.
114 if (type_a
->base_type
!= type_b
->base_type
) {
115 return glsl_error_type
;
118 /* "All arithmetic binary operators result in the same fundamental type
119 * (signed integer, unsigned integer, or floating-point) as the
120 * operands they operate on, after operand type conversion. After
121 * conversion, the following cases are valid
123 * * The two operands are scalars. In this case the operation is
124 * applied, resulting in a scalar."
126 if (type_a
->is_scalar() && type_b
->is_scalar())
129 /* "* One operand is a scalar, and the other is a vector or matrix.
130 * In this case, the scalar operation is applied independently to each
131 * component of the vector or matrix, resulting in the same size
134 if (type_a
->is_scalar()) {
135 if (!type_b
->is_scalar())
137 } else if (type_b
->is_scalar()) {
141 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
142 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
145 assert(type_a
->vector_elements
> 1);
146 assert(type_b
->vector_elements
> 1);
148 /* "* The two operands are vectors of the same size. In this case, the
149 * operation is done component-wise resulting in the same size
152 if (type_a
->is_vector() && type_b
->is_vector()) {
153 if (type_a
->vector_elements
== type_b
->vector_elements
)
156 return glsl_error_type
;
159 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
160 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
161 * <vector, vector> have been handled. At least one of the operands must
162 * be matrix. Further, since there are no integer matrix types, the base
163 * type of both operands must be float.
165 assert((type_a
->matrix_rows
> 1) || (type_b
->matrix_rows
> 1));
166 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
167 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
169 /* "* The operator is add (+), subtract (-), or divide (/), and the
170 * operands are matrices with the same number of rows and the same
171 * number of columns. In this case, the operation is done component-
172 * wise resulting in the same size matrix."
173 * * The operator is multiply (*), where both operands are matrices or
174 * one operand is a vector and the other a matrix. A right vector
175 * operand is treated as a column vector and a left vector operand as a
176 * row vector. In all these cases, it is required that the number of
177 * columns of the left operand is equal to the number of rows of the
178 * right operand. Then, the multiply (*) operation does a linear
179 * algebraic multiply, yielding an object that has the same number of
180 * rows as the left operand and the same number of columns as the right
181 * operand. Section 5.10 "Vector and Matrix Operations" explains in
182 * more detail how vectors and matrices are operated on."
185 if (type_a
->is_matrix() && type_b
->is_matrix()
186 && (type_a
->vector_elements
== type_b
->vector_elements
)
187 && (type_a
->matrix_rows
== type_b
->matrix_rows
))
190 return glsl_error_type
;
192 if (type_a
->is_matrix() && type_b
->is_matrix()) {
193 if (type_a
->vector_elements
== type_b
->matrix_rows
) {
195 const struct glsl_type
*t
;
201 if (type_a
->matrix_rows
== type_b
->vector_elements
) {
202 type_name
[3] = '0' + type_a
->matrix_rows
;
205 type_name
[3] = '0' + type_a
->matrix_rows
;
207 type_name
[5] = '0' + type_b
->vector_elements
;
212 _mesa_symbol_table_find_symbol(state
->symbols
, 0, type_name
);
213 return (t
!= NULL
) ? t
: glsl_error_type
;
215 } else if (type_a
->is_matrix()) {
216 /* A is a matrix and B is a column vector. Columns of A must match
219 if (type_a
->vector_elements
== type_b
->vector_elements
)
222 assert(type_b
->is_matrix());
224 /* A is a row vector and B is a matrix. Columns of A must match
227 if (type_a
->vector_elements
== type_b
->matrix_rows
)
233 /* "All other cases are illegal."
235 return glsl_error_type
;
239 static const struct glsl_type
*
240 unary_arithmetic_result_type(const struct glsl_type
*type
)
242 /* From GLSL 1.50 spec, page 57:
244 * "The arithmetic unary operators negate (-), post- and pre-increment
245 * and decrement (-- and ++) operate on integer or floating-point
246 * values (including vectors and matrices). All unary operators work
247 * component-wise on their operands. These result with the same type
250 if (!is_numeric_base_type(type
->base_type
))
251 return glsl_error_type
;
257 static const struct glsl_type
*
258 modulus_result_type(const struct glsl_type
*type_a
,
259 const struct glsl_type
*type_b
)
261 /* From GLSL 1.50 spec, page 56:
262 * "The operator modulus (%) operates on signed or unsigned integers or
263 * integer vectors. The operand types must both be signed or both be
266 if (! is_integer_base_type(type_a
->base_type
)
267 || ! is_integer_base_type(type_b
->base_type
)
268 || (type_a
->base_type
!= type_b
->base_type
)) {
269 return glsl_error_type
;
272 /* "The operands cannot be vectors of differing size. If one operand is
273 * a scalar and the other vector, then the scalar is applied component-
274 * wise to the vector, resulting in the same type as the vector. If both
275 * are vectors of the same size, the result is computed component-wise."
277 if (type_a
->is_vector()) {
278 if (!type_b
->is_vector()
279 || (type_a
->vector_elements
== type_b
->vector_elements
))
284 /* "The operator modulus (%) is not defined for any other data types
285 * (non-integer types)."
287 return glsl_error_type
;
291 static const struct glsl_type
*
292 relational_result_type(const struct glsl_type
*type_a
,
293 const struct glsl_type
*type_b
,
294 struct _mesa_glsl_parse_state
*state
)
296 /* From GLSL 1.50 spec, page 56:
297 * "The relational operators greater than (>), less than (<), greater
298 * than or equal (>=), and less than or equal (<=) operate only on
299 * scalar integer and scalar floating-point expressions."
301 if (! is_numeric_base_type(type_a
->base_type
)
302 || ! is_numeric_base_type(type_b
->base_type
)
303 || !type_a
->is_scalar()
304 || !type_b
->is_scalar())
305 return glsl_error_type
;
307 /* "Either the operands' types must match, or the conversions from
308 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
309 * operand, after which the types must match."
311 * This conversion was added in GLSL 1.20. If the compilation mode is
312 * GLSL 1.10, the conversion is skipped.
314 if (state
->language_version
>= 120) {
315 if ((type_a
->base_type
== GLSL_TYPE_FLOAT
)
316 && (type_b
->base_type
!= GLSL_TYPE_FLOAT
)) {
317 /* FINISHME: Generate the implicit type conversion. */
318 } else if ((type_a
->base_type
!= GLSL_TYPE_FLOAT
)
319 && (type_b
->base_type
== GLSL_TYPE_FLOAT
)) {
320 /* FINISHME: Generate the implicit type conversion. */
324 if (type_a
->base_type
!= type_b
->base_type
)
325 return glsl_error_type
;
327 /* "The result is scalar Boolean."
329 return glsl_bool_type
;
334 ast_node::hir(exec_list
*instructions
,
335 struct _mesa_glsl_parse_state
*state
)
345 ast_expression::hir(exec_list
*instructions
,
346 struct _mesa_glsl_parse_state
*state
)
348 static const int operations
[AST_NUM_OPERATORS
] = {
349 -1, /* ast_assign doesn't convert to ir_expression. */
350 -1, /* ast_plus doesn't convert to ir_expression. */
374 /* Note: The following block of expression types actually convert
375 * to multiple IR instructions.
377 ir_binop_mul
, /* ast_mul_assign */
378 ir_binop_div
, /* ast_div_assign */
379 ir_binop_mod
, /* ast_mod_assign */
380 ir_binop_add
, /* ast_add_assign */
381 ir_binop_sub
, /* ast_sub_assign */
382 ir_binop_lshift
, /* ast_ls_assign */
383 ir_binop_rshift
, /* ast_rs_assign */
384 ir_binop_bit_and
, /* ast_and_assign */
385 ir_binop_bit_xor
, /* ast_xor_assign */
386 ir_binop_bit_or
, /* ast_or_assign */
388 -1, /* ast_conditional doesn't convert to ir_expression. */
389 -1, /* ast_pre_inc doesn't convert to ir_expression. */
390 -1, /* ast_pre_dec doesn't convert to ir_expression. */
391 -1, /* ast_post_inc doesn't convert to ir_expression. */
392 -1, /* ast_post_dec doesn't convert to ir_expression. */
393 -1, /* ast_field_selection doesn't conv to ir_expression. */
394 -1, /* ast_array_index doesn't convert to ir_expression. */
395 -1, /* ast_function_call doesn't conv to ir_expression. */
396 -1, /* ast_identifier doesn't convert to ir_expression. */
397 -1, /* ast_int_constant doesn't convert to ir_expression. */
398 -1, /* ast_uint_constant doesn't conv to ir_expression. */
399 -1, /* ast_float_constant doesn't conv to ir_expression. */
400 -1, /* ast_bool_constant doesn't conv to ir_expression. */
401 -1, /* ast_sequence doesn't convert to ir_expression. */
403 ir_instruction
*result
= NULL
;
404 ir_instruction
*op
[2];
405 struct simple_node op_list
;
406 const struct glsl_type
*type
= glsl_error_type
;
407 bool error_emitted
= false;
410 loc
= this->get_location();
411 make_empty_list(& op_list
);
413 switch (this->oper
) {
415 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
416 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
418 error_emitted
= ((op
[0]->type
== glsl_error_type
)
419 || (op
[1]->type
== glsl_error_type
));
422 if (!error_emitted
) {
425 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
426 loc
= this->subexpressions
[0]->get_location();
427 if (op
[0]->mode
!= ir_op_dereference
) {
428 _mesa_glsl_error(& loc
, state
, "invalid lvalue in assignment");
429 error_emitted
= true;
431 type
= glsl_error_type
;
433 const struct ir_dereference
*const ref
=
434 (struct ir_dereference
*) op
[0];
435 const struct ir_variable
*const var
=
436 (struct ir_variable
*) ref
->var
;
439 && (var
->mode
== ir_op_var_decl
)
440 && (var
->read_only
)) {
441 _mesa_glsl_error(& loc
, state
, "cannot assign to read-only "
442 "variable `%s'", var
->name
);
443 error_emitted
= true;
445 type
= glsl_error_type
;
450 /* FINISHME: Check that the LHS and RHS have matching types. */
451 /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
453 ir_instruction
*tmp
= new ir_assignment(op
[0], op
[1], NULL
);
454 instructions
->push_tail(tmp
);
461 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
463 error_emitted
= (op
[0]->type
== glsl_error_type
);
464 if (type
== glsl_error_type
)
471 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
473 type
= unary_arithmetic_result_type(op
[0]->type
);
475 error_emitted
= (op
[0]->type
== glsl_error_type
);
477 result
= new ir_expression(operations
[this->oper
], type
,
485 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
486 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
488 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
489 (this->oper
== ast_mul
),
492 result
= new ir_expression(operations
[this->oper
], type
,
497 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
498 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
500 error_emitted
= ((op
[0]->type
== glsl_error_type
)
501 || (op
[1]->type
== glsl_error_type
));
503 type
= modulus_result_type(op
[0]->type
, op
[1]->type
);
505 assert(operations
[this->oper
] == ir_binop_mod
);
507 result
= new ir_expression(operations
[this->oper
], type
,
513 /* FINISHME: Implement bit-shift operators. */
520 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
521 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
523 error_emitted
= ((op
[0]->type
== glsl_error_type
)
524 || (op
[1]->type
== glsl_error_type
));
526 type
= relational_result_type(op
[0]->type
, op
[1]->type
, state
);
528 /* The relational operators must either generate an error or result
529 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
531 assert((type
== glsl_error_type
)
532 || ((type
->base_type
== GLSL_TYPE_BOOL
)
533 && type
->is_scalar()));
535 result
= new ir_expression(operations
[this->oper
], type
,
541 /* FINISHME: Implement equality operators. */
548 /* FINISHME: Implement bit-wise operators. */
555 /* FINISHME: Implement logical operators. */
561 case ast_sub_assign
: {
562 struct ir_instruction
*temp_rhs
;
564 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
565 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
567 error_emitted
= ((op
[0]->type
== glsl_error_type
)
568 || (op
[1]->type
== glsl_error_type
));
570 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
571 (this->oper
== ast_mul_assign
),
574 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
577 /* FINISHME: Check that the LHS is assignable. */
579 /* We still have to test that the LHS and RHS have matching type. For
580 * example, the following GLSL code should generate a type error:
582 * mat4 m; vec4 v; m *= v;
584 * The type of (m*v) is a vec4, but the type of m is a mat4.
586 * FINISHME: Is multiplication between a matrix and a vector the only
587 * FINISHME: case that resuls in mismatched types?
589 /* FINISHME: Check that the LHS and RHS have matching types. */
591 /* GLSL 1.10 does not allow array assignment. However, we don't have to
592 * explicitly test for this because none of the binary expression
593 * operators allow array operands either.
596 /* FINISHME: This is wrong. The operation should assign to a new
597 * FINISHME: temporary. This assignment should then be added to the
598 * FINISHME: instruction list. Another assignment to the real
599 * FINISHME: destination should be generated. The temporary should then
600 * FINISHME: be returned as the r-value.
602 result
= new ir_assignment(op
[0], temp_rhs
, NULL
);
615 case ast_conditional
:
624 case ast_field_selection
:
625 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
629 case ast_array_index
:
632 case ast_function_call
:
633 /* Should *NEVER* get here. ast_function_call should always be handled
634 * by ast_function_expression::hir.
639 case ast_identifier
: {
640 /* ast_identifier can appear several places in a full abstract syntax
641 * tree. This particular use must be at location specified in the grammar
642 * as 'variable_identifier'.
644 ir_variable
*var
= (ir_variable
*)
645 _mesa_symbol_table_find_symbol(state
->symbols
, 0,
646 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_function_expression::hir(exec_list
*instructions
,
715 struct _mesa_glsl_parse_state
*state
)
717 /* There are three sorts of function calls.
719 * 1. contstructors - The first subexpression is an ast_type_specifier.
720 * 2. methods - Only the .length() method of array types.
721 * 3. functions - Calls to regular old functions.
723 * Method calls are actually detected when the ast_field_selection
724 * expression is handled.
728 return ir_call::get_error_instruction();
732 ast_expression_statement::hir(exec_list
*instructions
,
733 struct _mesa_glsl_parse_state
*state
)
735 /* It is possible to have expression statements that don't have an
736 * expression. This is the solitary semicolon:
738 * for (i = 0; i < 5; i++)
741 * In this case the expression will be NULL. Test for NULL and don't do
742 * anything in that case.
744 if (expression
!= NULL
)
745 expression
->hir(instructions
, state
);
747 /* Statements do not have r-values.
754 ast_compound_statement::hir(exec_list
*instructions
,
755 struct _mesa_glsl_parse_state
*state
)
757 struct simple_node
*ptr
;
761 _mesa_symbol_table_push_scope(state
->symbols
);
763 foreach (ptr
, &statements
)
764 ((ast_node
*)ptr
)->hir(instructions
, state
);
767 _mesa_symbol_table_pop_scope(state
->symbols
);
769 /* Compound statements do not have r-values.
775 static const struct glsl_type
*
776 type_specifier_to_glsl_type(const struct ast_type_specifier
*spec
,
778 struct _mesa_glsl_parse_state
*state
)
780 static const char *const type_names
[] = {
816 "sampler1DArrayShadow",
817 "sampler2DArrayShadow",
831 NULL
, /* ast_struct */
832 NULL
/* ast_type_name */
834 struct glsl_type
*type
;
835 const char *type_name
= NULL
;
837 if (spec
->type_specifier
== ast_struct
) {
838 /* FINISHME: Handle annonymous structures. */
841 type_name
= (spec
->type_specifier
== ast_type_name
)
842 ? spec
->type_name
: type_names
[spec
->type_specifier
];
845 _mesa_symbol_table_find_symbol(state
->symbols
, 0, type_name
);
848 /* FINISHME: Handle array declarations. Note that this requires complete
849 * FINSIHME: handling of constant expressions.
858 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
859 struct ir_variable
*var
,
860 struct _mesa_glsl_parse_state
*state
)
865 /* FINISHME: Mark 'in' variables at global scope as read-only. */
866 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
867 || (qual
->varying
&& (state
->target
== fragment_shader
)))
873 if (qual
->in
&& qual
->out
)
874 var
->mode
= ir_var_inout
;
875 else if (qual
->attribute
|| qual
->in
876 || (qual
->varying
&& (state
->target
== fragment_shader
)))
877 var
->mode
= ir_var_in
;
878 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
879 var
->mode
= ir_var_out
;
880 else if (qual
->uniform
)
881 var
->mode
= ir_var_uniform
;
883 var
->mode
= ir_var_auto
;
886 var
->interpolation
= ir_var_flat
;
887 else if (qual
->noperspective
)
888 var
->interpolation
= ir_var_noperspective
;
890 var
->interpolation
= ir_var_smooth
;
895 ast_declarator_list::hir(exec_list
*instructions
,
896 struct _mesa_glsl_parse_state
*state
)
898 struct simple_node
*ptr
;
899 const struct glsl_type
*decl_type
;
900 const char *type_name
= NULL
;
903 /* FINISHME: Handle vertex shader "invariant" declarations that do not
904 * FINISHME: include a type. These re-declare built-in variables to be
905 * FINISHME: invariant.
908 decl_type
= type_specifier_to_glsl_type(this->type
->specifier
,
911 foreach (ptr
, &this->declarations
) {
912 struct ast_declaration
*const decl
= (struct ast_declaration
* )ptr
;
913 const struct glsl_type
*var_type
;
914 struct ir_variable
*var
;
917 /* FINISHME: Emit a warning if a variable declaration shadows a
918 * FINISHME: declaration at a higher scope.
921 if (decl_type
== NULL
) {
924 loc
= this->get_location();
925 if (type_name
!= NULL
) {
926 _mesa_glsl_error(& loc
, state
,
927 "invalid type `%s' in declaration of `%s'",
928 type_name
, decl
->identifier
);
930 _mesa_glsl_error(& loc
, state
,
931 "invalid type in declaration of `%s'",
937 if (decl
->is_array
) {
938 /* FINISHME: Handle array declarations. Note that this requires
939 * FINISHME: complete handling of constant expressions.
942 /* FINISHME: Reject delcarations of multidimensional arrays. */
944 var_type
= decl_type
;
947 var
= new ir_variable(var_type
, decl
->identifier
);
949 /* FINSIHME: Variables that are attribute, uniform, varying, in, or
950 * FINISHME: out varibles must be declared either at global scope or
951 * FINISHME: in a parameter list (in and out only).
954 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
);
956 /* Attempt to add the variable to the symbol table. If this fails, it
957 * means the variable has already been declared at this scope.
959 if (_mesa_symbol_table_add_symbol(state
->symbols
, 0, decl
->identifier
,
961 YYLTYPE loc
= this->get_location();
963 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
968 instructions
->push_tail(var
);
970 /* FINISHME: Process the declaration initializer. */
973 /* Variable declarations do not have r-values.
980 ast_parameter_declarator::hir(exec_list
*instructions
,
981 struct _mesa_glsl_parse_state
*state
)
983 const struct glsl_type
*type
;
984 const char *name
= NULL
;
987 type
= type_specifier_to_glsl_type(this->type
->specifier
, & name
, state
);
990 YYLTYPE loc
= this->get_location();
992 _mesa_glsl_error(& loc
, state
,
993 "invalid type `%s' in declaration of `%s'",
994 name
, this->identifier
);
996 _mesa_glsl_error(& loc
, state
,
997 "invalid type in declaration of `%s'",
1001 type
= glsl_error_type
;
1004 ir_variable
*var
= new ir_variable(type
, this->identifier
);
1006 /* FINISHME: Handle array declarations. Note that this requires
1007 * FINISHME: complete handling of constant expressions.
1010 /* Apply any specified qualifiers to the parameter declaration. Note that
1011 * for function parameters the default mode is 'in'.
1013 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
);
1014 if (var
->mode
== ir_var_auto
)
1015 var
->mode
= ir_var_in
;
1017 instructions
->push_tail(var
);
1019 /* Parameter declarations do not have r-values.
1026 ast_function_parameters_to_hir(struct simple_node
*ast_parameters
,
1027 exec_list
*ir_parameters
,
1028 struct _mesa_glsl_parse_state
*state
)
1030 struct simple_node
*ptr
;
1032 foreach (ptr
, ast_parameters
) {
1033 ((ast_node
*)ptr
)->hir(ir_parameters
, state
);
1039 parameter_lists_match(exec_list
*list_a
, exec_list
*list_b
)
1041 exec_list_iterator iter_a
= list_a
->iterator();
1042 exec_list_iterator iter_b
= list_b
->iterator();
1044 while (iter_a
.has_next()) {
1045 /* If all of the parameters from the other parameter list have been
1046 * exhausted, the lists have different length and, by definition,
1049 if (!iter_b
.has_next())
1052 /* If the types of the parameters do not match, the parameters lists
1067 ast_function_definition::hir(exec_list
*instructions
,
1068 struct _mesa_glsl_parse_state
*state
)
1071 ir_function_signature
*signature
= NULL
;
1072 ir_function
*f
= NULL
;
1073 exec_list parameters
;
1076 /* Convert the list of function parameters to HIR now so that they can be
1077 * used below to compare this function's signature with previously seen
1078 * signatures for functions with the same name.
1080 ast_function_parameters_to_hir(& this->prototype
->parameters
, & parameters
,
1084 /* Verify that this function's signature either doesn't match a previously
1085 * seen signature for a function with the same name, or, if a match is found,
1086 * that the previously seen signature does not have an associated definition.
1089 _mesa_symbol_table_find_symbol(state
->symbols
, 0,
1090 this->prototype
->identifier
);
1092 foreach_iter(exec_list_iterator
, iter
, f
->signatures
) {
1093 signature
= (struct ir_function_signature
*) iter
.get();
1095 /* Compare the parameter list of the function being defined to the
1096 * existing function. If the parameter lists match, then the return
1097 * type must also match and the existing function must not have a
1100 if (parameter_lists_match(& parameters
, & signature
->parameters
)) {
1101 /* FINISHME: Compare return types. */
1103 if (signature
->definition
!= NULL
) {
1104 YYLTYPE loc
= this->get_location();
1106 _mesa_glsl_error(& loc
, state
, "function `%s' redefined",
1107 this->prototype
->identifier
);
1117 f
= new ir_function();
1118 f
->name
= this->prototype
->identifier
;
1120 _mesa_symbol_table_add_symbol(state
->symbols
, 0, f
->name
, f
);
1124 /* Finish storing the information about this new function in its signature.
1126 if (signature
== NULL
) {
1127 signature
= new ir_function_signature();
1128 f
->signatures
.push_tail(signature
);
1130 /* Destroy all of the previous parameter information. The previous
1131 * parameter information comes from the function prototype, and it can
1132 * either include invalid parameter names or may not have names at all.
1134 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
1135 assert(((struct ir_instruction
*)iter
.get())->mode
== ir_op_var_decl
);
1143 ast_function_parameters_to_hir(& this->prototype
->parameters
,
1144 & signature
->parameters
,
1146 /* FINISHME: Set signature->return_type */
1148 label
= new ir_label(this->prototype
->identifier
);
1149 if (signature
->definition
== NULL
) {
1150 signature
->definition
= label
;
1152 instructions
->push_tail(label
);
1154 /* Add the function parameters to the symbol table. During this step the
1155 * parameter declarations are also moved from the temporary "parameters" list
1156 * to the instruction list. There are other more efficient ways to do this,
1157 * but they involve ugly linked-list gymnastics.
1159 _mesa_symbol_table_push_scope(state
->symbols
);
1160 foreach_iter(exec_list_iterator
, iter
, parameters
) {
1161 ir_variable
*const var
= (ir_variable
*) iter
.get();
1163 assert(((ir_instruction
*)var
)->mode
== ir_op_var_decl
);
1166 instructions
->push_tail(var
);
1168 _mesa_symbol_table_add_symbol(state
->symbols
, 0, var
->name
, var
);
1171 /* Convert the body of the function to HIR, and append the resulting
1172 * instructions to the list that currently consists of the function label
1173 * and the function parameters.
1175 this->body
->hir(instructions
, state
);
1177 _mesa_symbol_table_pop_scope(state
->symbols
);
1180 /* Function definitions do not have r-values.